xfs_fsops.c 23 KB

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