xfs_fsops.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_log.h"
  22. #include "xfs_trans.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_bmap_btree.h"
  27. #include "xfs_alloc_btree.h"
  28. #include "xfs_ialloc_btree.h"
  29. #include "xfs_dinode.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_inode_item.h"
  32. #include "xfs_btree.h"
  33. #include "xfs_error.h"
  34. #include "xfs_alloc.h"
  35. #include "xfs_ialloc.h"
  36. #include "xfs_fsops.h"
  37. #include "xfs_itable.h"
  38. #include "xfs_trans_space.h"
  39. #include "xfs_rtalloc.h"
  40. #include "xfs_filestream.h"
  41. #include "xfs_trace.h"
  42. /*
  43. * File system operations
  44. */
  45. int
  46. xfs_fs_geometry(
  47. xfs_mount_t *mp,
  48. xfs_fsop_geom_t *geo,
  49. int new_version)
  50. {
  51. memset(geo, 0, sizeof(*geo));
  52. geo->blocksize = mp->m_sb.sb_blocksize;
  53. geo->rtextsize = mp->m_sb.sb_rextsize;
  54. geo->agblocks = mp->m_sb.sb_agblocks;
  55. geo->agcount = mp->m_sb.sb_agcount;
  56. geo->logblocks = mp->m_sb.sb_logblocks;
  57. geo->sectsize = mp->m_sb.sb_sectsize;
  58. geo->inodesize = mp->m_sb.sb_inodesize;
  59. geo->imaxpct = mp->m_sb.sb_imax_pct;
  60. geo->datablocks = mp->m_sb.sb_dblocks;
  61. geo->rtblocks = mp->m_sb.sb_rblocks;
  62. geo->rtextents = mp->m_sb.sb_rextents;
  63. geo->logstart = mp->m_sb.sb_logstart;
  64. ASSERT(sizeof(geo->uuid)==sizeof(mp->m_sb.sb_uuid));
  65. memcpy(geo->uuid, &mp->m_sb.sb_uuid, sizeof(mp->m_sb.sb_uuid));
  66. if (new_version >= 2) {
  67. geo->sunit = mp->m_sb.sb_unit;
  68. geo->swidth = mp->m_sb.sb_width;
  69. }
  70. if (new_version >= 3) {
  71. geo->version = XFS_FSOP_GEOM_VERSION;
  72. geo->flags =
  73. (xfs_sb_version_hasattr(&mp->m_sb) ?
  74. XFS_FSOP_GEOM_FLAGS_ATTR : 0) |
  75. (xfs_sb_version_hasnlink(&mp->m_sb) ?
  76. XFS_FSOP_GEOM_FLAGS_NLINK : 0) |
  77. (xfs_sb_version_hasquota(&mp->m_sb) ?
  78. XFS_FSOP_GEOM_FLAGS_QUOTA : 0) |
  79. (xfs_sb_version_hasalign(&mp->m_sb) ?
  80. XFS_FSOP_GEOM_FLAGS_IALIGN : 0) |
  81. (xfs_sb_version_hasdalign(&mp->m_sb) ?
  82. XFS_FSOP_GEOM_FLAGS_DALIGN : 0) |
  83. (xfs_sb_version_hasshared(&mp->m_sb) ?
  84. XFS_FSOP_GEOM_FLAGS_SHARED : 0) |
  85. (xfs_sb_version_hasextflgbit(&mp->m_sb) ?
  86. XFS_FSOP_GEOM_FLAGS_EXTFLG : 0) |
  87. (xfs_sb_version_hasdirv2(&mp->m_sb) ?
  88. XFS_FSOP_GEOM_FLAGS_DIRV2 : 0) |
  89. (xfs_sb_version_hassector(&mp->m_sb) ?
  90. XFS_FSOP_GEOM_FLAGS_SECTOR : 0) |
  91. (xfs_sb_version_hasasciici(&mp->m_sb) ?
  92. XFS_FSOP_GEOM_FLAGS_DIRV2CI : 0) |
  93. (xfs_sb_version_haslazysbcount(&mp->m_sb) ?
  94. XFS_FSOP_GEOM_FLAGS_LAZYSB : 0) |
  95. (xfs_sb_version_hasattr2(&mp->m_sb) ?
  96. XFS_FSOP_GEOM_FLAGS_ATTR2 : 0);
  97. geo->logsectsize = xfs_sb_version_hassector(&mp->m_sb) ?
  98. mp->m_sb.sb_logsectsize : BBSIZE;
  99. geo->rtsectsize = mp->m_sb.sb_blocksize;
  100. geo->dirblocksize = mp->m_dirblksize;
  101. }
  102. if (new_version >= 4) {
  103. geo->flags |=
  104. (xfs_sb_version_haslogv2(&mp->m_sb) ?
  105. XFS_FSOP_GEOM_FLAGS_LOGV2 : 0);
  106. geo->logsunit = mp->m_sb.sb_logsunit;
  107. }
  108. return 0;
  109. }
  110. static int
  111. xfs_growfs_data_private(
  112. xfs_mount_t *mp, /* mount point for filesystem */
  113. xfs_growfs_data_t *in) /* growfs data input struct */
  114. {
  115. xfs_agf_t *agf;
  116. xfs_agi_t *agi;
  117. xfs_agnumber_t agno;
  118. xfs_extlen_t agsize;
  119. xfs_extlen_t tmpsize;
  120. xfs_alloc_rec_t *arec;
  121. struct xfs_btree_block *block;
  122. xfs_buf_t *bp;
  123. int bucket;
  124. int dpct;
  125. int error;
  126. xfs_agnumber_t nagcount;
  127. xfs_agnumber_t nagimax = 0;
  128. xfs_rfsblock_t nb, nb_mod;
  129. xfs_rfsblock_t new;
  130. xfs_rfsblock_t nfree;
  131. xfs_agnumber_t oagcount;
  132. int pct;
  133. xfs_trans_t *tp;
  134. nb = in->newblocks;
  135. pct = in->imaxpct;
  136. if (nb < mp->m_sb.sb_dblocks || pct < 0 || pct > 100)
  137. return XFS_ERROR(EINVAL);
  138. if ((error = xfs_sb_validate_fsb_count(&mp->m_sb, nb)))
  139. return error;
  140. dpct = pct - mp->m_sb.sb_imax_pct;
  141. bp = xfs_buf_read_uncached(mp->m_ddev_targp,
  142. XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
  143. XFS_FSS_TO_BB(mp, 1), 0);
  144. if (!bp)
  145. return EIO;
  146. xfs_buf_relse(bp);
  147. new = nb; /* use new as a temporary here */
  148. nb_mod = do_div(new, mp->m_sb.sb_agblocks);
  149. nagcount = new + (nb_mod != 0);
  150. if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
  151. nagcount--;
  152. nb = (xfs_rfsblock_t)nagcount * mp->m_sb.sb_agblocks;
  153. if (nb < mp->m_sb.sb_dblocks)
  154. return XFS_ERROR(EINVAL);
  155. }
  156. new = nb - mp->m_sb.sb_dblocks;
  157. oagcount = mp->m_sb.sb_agcount;
  158. /* allocate the new per-ag structures */
  159. if (nagcount > oagcount) {
  160. error = xfs_initialize_perag(mp, nagcount, &nagimax);
  161. if (error)
  162. return error;
  163. }
  164. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFS);
  165. tp->t_flags |= XFS_TRANS_RESERVE;
  166. if ((error = xfs_trans_reserve(tp, XFS_GROWFS_SPACE_RES(mp),
  167. XFS_GROWDATA_LOG_RES(mp), 0, 0, 0))) {
  168. xfs_trans_cancel(tp, 0);
  169. return error;
  170. }
  171. /*
  172. * Write new AG headers to disk. Non-transactional, but written
  173. * synchronously so they are completed prior to the growfs transaction
  174. * being logged.
  175. */
  176. nfree = 0;
  177. for (agno = nagcount - 1; agno >= oagcount; agno--, new -= agsize) {
  178. /*
  179. * AG freelist header block
  180. */
  181. bp = xfs_buf_get(mp->m_ddev_targp,
  182. XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
  183. XFS_FSS_TO_BB(mp, 1), 0);
  184. if (!bp) {
  185. error = ENOMEM;
  186. goto error0;
  187. }
  188. agf = XFS_BUF_TO_AGF(bp);
  189. memset(agf, 0, mp->m_sb.sb_sectsize);
  190. agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
  191. agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
  192. agf->agf_seqno = cpu_to_be32(agno);
  193. if (agno == nagcount - 1)
  194. agsize =
  195. nb -
  196. (agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
  197. else
  198. agsize = mp->m_sb.sb_agblocks;
  199. agf->agf_length = cpu_to_be32(agsize);
  200. agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
  201. agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
  202. agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
  203. agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
  204. agf->agf_flfirst = 0;
  205. agf->agf_fllast = cpu_to_be32(XFS_AGFL_SIZE(mp) - 1);
  206. agf->agf_flcount = 0;
  207. tmpsize = agsize - XFS_PREALLOC_BLOCKS(mp);
  208. agf->agf_freeblks = cpu_to_be32(tmpsize);
  209. agf->agf_longest = cpu_to_be32(tmpsize);
  210. error = xfs_bwrite(bp);
  211. xfs_buf_relse(bp);
  212. if (error)
  213. goto error0;
  214. /*
  215. * AG inode header block
  216. */
  217. bp = xfs_buf_get(mp->m_ddev_targp,
  218. XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
  219. XFS_FSS_TO_BB(mp, 1), 0);
  220. if (!bp) {
  221. error = ENOMEM;
  222. goto error0;
  223. }
  224. agi = XFS_BUF_TO_AGI(bp);
  225. memset(agi, 0, mp->m_sb.sb_sectsize);
  226. agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
  227. agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
  228. agi->agi_seqno = cpu_to_be32(agno);
  229. agi->agi_length = cpu_to_be32(agsize);
  230. agi->agi_count = 0;
  231. agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
  232. agi->agi_level = cpu_to_be32(1);
  233. agi->agi_freecount = 0;
  234. agi->agi_newino = cpu_to_be32(NULLAGINO);
  235. agi->agi_dirino = cpu_to_be32(NULLAGINO);
  236. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
  237. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  238. error = xfs_bwrite(bp);
  239. xfs_buf_relse(bp);
  240. if (error)
  241. goto error0;
  242. /*
  243. * BNO btree root block
  244. */
  245. bp = xfs_buf_get(mp->m_ddev_targp,
  246. XFS_AGB_TO_DADDR(mp, agno, XFS_BNO_BLOCK(mp)),
  247. BTOBB(mp->m_sb.sb_blocksize), 0);
  248. if (!bp) {
  249. error = ENOMEM;
  250. goto error0;
  251. }
  252. block = XFS_BUF_TO_BLOCK(bp);
  253. memset(block, 0, mp->m_sb.sb_blocksize);
  254. block->bb_magic = cpu_to_be32(XFS_ABTB_MAGIC);
  255. block->bb_level = 0;
  256. block->bb_numrecs = cpu_to_be16(1);
  257. block->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  258. block->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  259. arec = XFS_ALLOC_REC_ADDR(mp, block, 1);
  260. arec->ar_startblock = cpu_to_be32(XFS_PREALLOC_BLOCKS(mp));
  261. arec->ar_blockcount = cpu_to_be32(
  262. agsize - be32_to_cpu(arec->ar_startblock));
  263. error = xfs_bwrite(bp);
  264. xfs_buf_relse(bp);
  265. if (error)
  266. goto error0;
  267. /*
  268. * CNT btree root block
  269. */
  270. bp = xfs_buf_get(mp->m_ddev_targp,
  271. XFS_AGB_TO_DADDR(mp, agno, XFS_CNT_BLOCK(mp)),
  272. BTOBB(mp->m_sb.sb_blocksize), 0);
  273. if (!bp) {
  274. error = ENOMEM;
  275. goto error0;
  276. }
  277. block = XFS_BUF_TO_BLOCK(bp);
  278. memset(block, 0, mp->m_sb.sb_blocksize);
  279. block->bb_magic = cpu_to_be32(XFS_ABTC_MAGIC);
  280. block->bb_level = 0;
  281. block->bb_numrecs = cpu_to_be16(1);
  282. block->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  283. block->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  284. arec = XFS_ALLOC_REC_ADDR(mp, block, 1);
  285. arec->ar_startblock = cpu_to_be32(XFS_PREALLOC_BLOCKS(mp));
  286. arec->ar_blockcount = cpu_to_be32(
  287. agsize - be32_to_cpu(arec->ar_startblock));
  288. nfree += be32_to_cpu(arec->ar_blockcount);
  289. error = xfs_bwrite(bp);
  290. xfs_buf_relse(bp);
  291. if (error)
  292. goto error0;
  293. /*
  294. * INO btree root block
  295. */
  296. bp = xfs_buf_get(mp->m_ddev_targp,
  297. XFS_AGB_TO_DADDR(mp, agno, XFS_IBT_BLOCK(mp)),
  298. BTOBB(mp->m_sb.sb_blocksize), 0);
  299. if (!bp) {
  300. error = ENOMEM;
  301. goto error0;
  302. }
  303. block = XFS_BUF_TO_BLOCK(bp);
  304. memset(block, 0, mp->m_sb.sb_blocksize);
  305. block->bb_magic = cpu_to_be32(XFS_IBT_MAGIC);
  306. block->bb_level = 0;
  307. block->bb_numrecs = 0;
  308. block->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  309. block->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  310. error = xfs_bwrite(bp);
  311. xfs_buf_relse(bp);
  312. if (error)
  313. goto error0;
  314. }
  315. xfs_trans_agblocks_delta(tp, nfree);
  316. /*
  317. * There are new blocks in the old last a.g.
  318. */
  319. if (new) {
  320. /*
  321. * Change the agi length.
  322. */
  323. error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
  324. if (error) {
  325. goto error0;
  326. }
  327. ASSERT(bp);
  328. agi = XFS_BUF_TO_AGI(bp);
  329. be32_add_cpu(&agi->agi_length, new);
  330. ASSERT(nagcount == oagcount ||
  331. be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks);
  332. xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
  333. /*
  334. * Change agf length.
  335. */
  336. error = xfs_alloc_read_agf(mp, tp, agno, 0, &bp);
  337. if (error) {
  338. goto error0;
  339. }
  340. ASSERT(bp);
  341. agf = XFS_BUF_TO_AGF(bp);
  342. be32_add_cpu(&agf->agf_length, new);
  343. ASSERT(be32_to_cpu(agf->agf_length) ==
  344. be32_to_cpu(agi->agi_length));
  345. xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH);
  346. /*
  347. * Free the new space.
  348. */
  349. error = xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, agno,
  350. be32_to_cpu(agf->agf_length) - new), new);
  351. if (error) {
  352. goto error0;
  353. }
  354. }
  355. /*
  356. * Update changed superblock fields transactionally. These are not
  357. * seen by the rest of the world until the transaction commit applies
  358. * them atomically to the superblock.
  359. */
  360. if (nagcount > oagcount)
  361. xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
  362. if (nb > mp->m_sb.sb_dblocks)
  363. xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
  364. nb - mp->m_sb.sb_dblocks);
  365. if (nfree)
  366. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, nfree);
  367. if (dpct)
  368. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
  369. error = xfs_trans_commit(tp, 0);
  370. if (error)
  371. return error;
  372. /* New allocation groups fully initialized, so update mount struct */
  373. if (nagimax)
  374. mp->m_maxagi = nagimax;
  375. if (mp->m_sb.sb_imax_pct) {
  376. __uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
  377. do_div(icount, 100);
  378. mp->m_maxicount = icount << mp->m_sb.sb_inopblog;
  379. } else
  380. mp->m_maxicount = 0;
  381. xfs_set_low_space_thresholds(mp);
  382. /* update secondary superblocks. */
  383. for (agno = 1; agno < nagcount; agno++) {
  384. error = 0;
  385. /*
  386. * new secondary superblocks need to be zeroed, not read from
  387. * disk as the contents of the new area we are growing into is
  388. * completely unknown.
  389. */
  390. if (agno < oagcount) {
  391. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  392. XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
  393. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  394. } else {
  395. bp = xfs_trans_get_buf(NULL, mp->m_ddev_targp,
  396. XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
  397. XFS_FSS_TO_BB(mp, 1), 0);
  398. if (bp)
  399. xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
  400. else
  401. error = ENOMEM;
  402. }
  403. if (error) {
  404. xfs_warn(mp,
  405. "error %d reading secondary superblock for ag %d",
  406. error, agno);
  407. break;
  408. }
  409. xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, XFS_SB_ALL_BITS);
  410. /*
  411. * If we get an error writing out the alternate superblocks,
  412. * just issue a warning and continue. The real work is
  413. * already done and committed.
  414. */
  415. error = xfs_bwrite(bp);
  416. xfs_buf_relse(bp);
  417. if (error) {
  418. xfs_warn(mp,
  419. "write error %d updating secondary superblock for ag %d",
  420. error, agno);
  421. break; /* no point in continuing */
  422. }
  423. }
  424. return error;
  425. error0:
  426. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  427. return error;
  428. }
  429. static int
  430. xfs_growfs_log_private(
  431. xfs_mount_t *mp, /* mount point for filesystem */
  432. xfs_growfs_log_t *in) /* growfs log input struct */
  433. {
  434. xfs_extlen_t nb;
  435. nb = in->newblocks;
  436. if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
  437. return XFS_ERROR(EINVAL);
  438. if (nb == mp->m_sb.sb_logblocks &&
  439. in->isint == (mp->m_sb.sb_logstart != 0))
  440. return XFS_ERROR(EINVAL);
  441. /*
  442. * Moving the log is hard, need new interfaces to sync
  443. * the log first, hold off all activity while moving it.
  444. * Can have shorter or longer log in the same space,
  445. * or transform internal to external log or vice versa.
  446. */
  447. return XFS_ERROR(ENOSYS);
  448. }
  449. /*
  450. * protected versions of growfs function acquire and release locks on the mount
  451. * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
  452. * XFS_IOC_FSGROWFSRT
  453. */
  454. int
  455. xfs_growfs_data(
  456. xfs_mount_t *mp,
  457. xfs_growfs_data_t *in)
  458. {
  459. int error;
  460. if (!capable(CAP_SYS_ADMIN))
  461. return XFS_ERROR(EPERM);
  462. if (!mutex_trylock(&mp->m_growlock))
  463. return XFS_ERROR(EWOULDBLOCK);
  464. error = xfs_growfs_data_private(mp, in);
  465. mutex_unlock(&mp->m_growlock);
  466. return error;
  467. }
  468. int
  469. xfs_growfs_log(
  470. xfs_mount_t *mp,
  471. xfs_growfs_log_t *in)
  472. {
  473. int error;
  474. if (!capable(CAP_SYS_ADMIN))
  475. return XFS_ERROR(EPERM);
  476. if (!mutex_trylock(&mp->m_growlock))
  477. return XFS_ERROR(EWOULDBLOCK);
  478. error = xfs_growfs_log_private(mp, in);
  479. mutex_unlock(&mp->m_growlock);
  480. return error;
  481. }
  482. /*
  483. * exported through ioctl XFS_IOC_FSCOUNTS
  484. */
  485. int
  486. xfs_fs_counts(
  487. xfs_mount_t *mp,
  488. xfs_fsop_counts_t *cnt)
  489. {
  490. xfs_icsb_sync_counters(mp, XFS_ICSB_LAZY_COUNT);
  491. spin_lock(&mp->m_sb_lock);
  492. cnt->freedata = mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  493. cnt->freertx = mp->m_sb.sb_frextents;
  494. cnt->freeino = mp->m_sb.sb_ifree;
  495. cnt->allocino = mp->m_sb.sb_icount;
  496. spin_unlock(&mp->m_sb_lock);
  497. return 0;
  498. }
  499. /*
  500. * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
  501. *
  502. * xfs_reserve_blocks is called to set m_resblks
  503. * in the in-core mount table. The number of unused reserved blocks
  504. * is kept in m_resblks_avail.
  505. *
  506. * Reserve the requested number of blocks if available. Otherwise return
  507. * as many as possible to satisfy the request. The actual number
  508. * reserved are returned in outval
  509. *
  510. * A null inval pointer indicates that only the current reserved blocks
  511. * available should be returned no settings are changed.
  512. */
  513. int
  514. xfs_reserve_blocks(
  515. xfs_mount_t *mp,
  516. __uint64_t *inval,
  517. xfs_fsop_resblks_t *outval)
  518. {
  519. __int64_t lcounter, delta, fdblks_delta;
  520. __uint64_t request;
  521. /* If inval is null, report current values and return */
  522. if (inval == (__uint64_t *)NULL) {
  523. if (!outval)
  524. return EINVAL;
  525. outval->resblks = mp->m_resblks;
  526. outval->resblks_avail = mp->m_resblks_avail;
  527. return 0;
  528. }
  529. request = *inval;
  530. /*
  531. * With per-cpu counters, this becomes an interesting
  532. * problem. we needto work out if we are freeing or allocation
  533. * blocks first, then we can do the modification as necessary.
  534. *
  535. * We do this under the m_sb_lock so that if we are near
  536. * ENOSPC, we will hold out any changes while we work out
  537. * what to do. This means that the amount of free space can
  538. * change while we do this, so we need to retry if we end up
  539. * trying to reserve more space than is available.
  540. *
  541. * We also use the xfs_mod_incore_sb() interface so that we
  542. * don't have to care about whether per cpu counter are
  543. * enabled, disabled or even compiled in....
  544. */
  545. retry:
  546. spin_lock(&mp->m_sb_lock);
  547. xfs_icsb_sync_counters_locked(mp, 0);
  548. /*
  549. * If our previous reservation was larger than the current value,
  550. * then move any unused blocks back to the free pool.
  551. */
  552. fdblks_delta = 0;
  553. if (mp->m_resblks > request) {
  554. lcounter = mp->m_resblks_avail - request;
  555. if (lcounter > 0) { /* release unused blocks */
  556. fdblks_delta = lcounter;
  557. mp->m_resblks_avail -= lcounter;
  558. }
  559. mp->m_resblks = request;
  560. } else {
  561. __int64_t free;
  562. free = mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  563. if (!free)
  564. goto out; /* ENOSPC and fdblks_delta = 0 */
  565. delta = request - mp->m_resblks;
  566. lcounter = free - delta;
  567. if (lcounter < 0) {
  568. /* We can't satisfy the request, just get what we can */
  569. mp->m_resblks += free;
  570. mp->m_resblks_avail += free;
  571. fdblks_delta = -free;
  572. } else {
  573. fdblks_delta = -delta;
  574. mp->m_resblks = request;
  575. mp->m_resblks_avail += delta;
  576. }
  577. }
  578. out:
  579. if (outval) {
  580. outval->resblks = mp->m_resblks;
  581. outval->resblks_avail = mp->m_resblks_avail;
  582. }
  583. spin_unlock(&mp->m_sb_lock);
  584. if (fdblks_delta) {
  585. /*
  586. * If we are putting blocks back here, m_resblks_avail is
  587. * already at its max so this will put it in the free pool.
  588. *
  589. * If we need space, we'll either succeed in getting it
  590. * from the free block count or we'll get an enospc. If
  591. * we get a ENOSPC, it means things changed while we were
  592. * calculating fdblks_delta and so we should try again to
  593. * see if there is anything left to reserve.
  594. *
  595. * Don't set the reserved flag here - we don't want to reserve
  596. * the extra reserve blocks from the reserve.....
  597. */
  598. int error;
  599. error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
  600. fdblks_delta, 0);
  601. if (error == ENOSPC)
  602. goto retry;
  603. }
  604. return 0;
  605. }
  606. /*
  607. * Dump a transaction into the log that contains no real change. This is needed
  608. * to be able to make the log dirty or stamp the current tail LSN into the log
  609. * during the covering operation.
  610. *
  611. * We cannot use an inode here for this - that will push dirty state back up
  612. * into the VFS and then periodic inode flushing will prevent log covering from
  613. * making progress. Hence we log a field in the superblock instead and use a
  614. * synchronous transaction to ensure the superblock is immediately unpinned
  615. * and can be written back.
  616. */
  617. int
  618. xfs_fs_log_dummy(
  619. xfs_mount_t *mp)
  620. {
  621. xfs_trans_t *tp;
  622. int error;
  623. tp = _xfs_trans_alloc(mp, XFS_TRANS_DUMMY1, KM_SLEEP);
  624. error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
  625. XFS_DEFAULT_LOG_COUNT);
  626. if (error) {
  627. xfs_trans_cancel(tp, 0);
  628. return error;
  629. }
  630. /* log the UUID because it is an unchanging field */
  631. xfs_mod_sb(tp, XFS_SB_UUID);
  632. xfs_trans_set_sync(tp);
  633. return xfs_trans_commit(tp, 0);
  634. }
  635. int
  636. xfs_fs_goingdown(
  637. xfs_mount_t *mp,
  638. __uint32_t inflags)
  639. {
  640. switch (inflags) {
  641. case XFS_FSOP_GOING_FLAGS_DEFAULT: {
  642. struct super_block *sb = freeze_bdev(mp->m_super->s_bdev);
  643. if (sb && !IS_ERR(sb)) {
  644. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  645. thaw_bdev(sb->s_bdev, sb);
  646. }
  647. break;
  648. }
  649. case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
  650. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  651. break;
  652. case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
  653. xfs_force_shutdown(mp,
  654. SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
  655. break;
  656. default:
  657. return XFS_ERROR(EINVAL);
  658. }
  659. return 0;
  660. }
  661. /*
  662. * Force a shutdown of the filesystem instantly while keeping the filesystem
  663. * consistent. We don't do an unmount here; just shutdown the shop, make sure
  664. * that absolutely nothing persistent happens to this filesystem after this
  665. * point.
  666. */
  667. void
  668. xfs_do_force_shutdown(
  669. xfs_mount_t *mp,
  670. int flags,
  671. char *fname,
  672. int lnnum)
  673. {
  674. int logerror;
  675. logerror = flags & SHUTDOWN_LOG_IO_ERROR;
  676. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  677. xfs_notice(mp,
  678. "%s(0x%x) called from line %d of file %s. Return address = 0x%p",
  679. __func__, flags, lnnum, fname, __return_address);
  680. }
  681. /*
  682. * No need to duplicate efforts.
  683. */
  684. if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
  685. return;
  686. /*
  687. * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
  688. * queue up anybody new on the log reservations, and wakes up
  689. * everybody who's sleeping on log reservations to tell them
  690. * the bad news.
  691. */
  692. if (xfs_log_force_umount(mp, logerror))
  693. return;
  694. if (flags & SHUTDOWN_CORRUPT_INCORE) {
  695. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_CORRUPT,
  696. "Corruption of in-memory data detected. Shutting down filesystem");
  697. if (XFS_ERRLEVEL_HIGH <= xfs_error_level)
  698. xfs_stack_trace();
  699. } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  700. if (logerror) {
  701. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_LOGERROR,
  702. "Log I/O Error Detected. Shutting down filesystem");
  703. } else if (flags & SHUTDOWN_DEVICE_REQ) {
  704. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
  705. "All device paths lost. Shutting down filesystem");
  706. } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
  707. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
  708. "I/O Error Detected. Shutting down filesystem");
  709. }
  710. }
  711. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  712. xfs_alert(mp,
  713. "Please umount the filesystem and rectify the problem(s)");
  714. }
  715. }