xfs_fsops.c 25 KB

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