xfs_fsops.c 25 KB

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