xfs_fsops.c 21 KB

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