xfs_fsops.c 27 KB

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