xfs_fsops.c 21 KB

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