xfs_fsops.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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. if (xfs_sb_version_hascrc(&mp->m_sb))
  328. xfs_btree_init_block(mp, bp, XFS_ABTB_CRC_MAGIC, 0, 1,
  329. agno, XFS_BTREE_CRC_BLOCKS);
  330. else
  331. xfs_btree_init_block(mp, bp, XFS_ABTB_MAGIC, 0, 1,
  332. agno, 0);
  333. arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
  334. arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
  335. arec->ar_blockcount = cpu_to_be32(
  336. agsize - be32_to_cpu(arec->ar_startblock));
  337. error = xfs_bwrite(bp);
  338. xfs_buf_relse(bp);
  339. if (error)
  340. goto error0;
  341. /*
  342. * CNT btree root block
  343. */
  344. bp = xfs_growfs_get_hdr_buf(mp,
  345. XFS_AGB_TO_DADDR(mp, agno, XFS_CNT_BLOCK(mp)),
  346. BTOBB(mp->m_sb.sb_blocksize), 0,
  347. &xfs_allocbt_buf_ops);
  348. if (!bp) {
  349. error = -ENOMEM;
  350. goto error0;
  351. }
  352. if (xfs_sb_version_hascrc(&mp->m_sb))
  353. xfs_btree_init_block(mp, bp, XFS_ABTC_CRC_MAGIC, 0, 1,
  354. agno, XFS_BTREE_CRC_BLOCKS);
  355. else
  356. xfs_btree_init_block(mp, bp, XFS_ABTC_MAGIC, 0, 1,
  357. agno, 0);
  358. arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
  359. arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
  360. arec->ar_blockcount = cpu_to_be32(
  361. agsize - be32_to_cpu(arec->ar_startblock));
  362. nfree += be32_to_cpu(arec->ar_blockcount);
  363. error = xfs_bwrite(bp);
  364. xfs_buf_relse(bp);
  365. if (error)
  366. goto error0;
  367. /* RMAP btree root block */
  368. if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
  369. struct xfs_rmap_rec *rrec;
  370. struct xfs_btree_block *block;
  371. bp = xfs_growfs_get_hdr_buf(mp,
  372. XFS_AGB_TO_DADDR(mp, agno, XFS_RMAP_BLOCK(mp)),
  373. BTOBB(mp->m_sb.sb_blocksize), 0,
  374. &xfs_rmapbt_buf_ops);
  375. if (!bp) {
  376. error = -ENOMEM;
  377. goto error0;
  378. }
  379. xfs_btree_init_block(mp, bp, XFS_RMAP_CRC_MAGIC, 0, 0,
  380. agno, XFS_BTREE_CRC_BLOCKS);
  381. block = XFS_BUF_TO_BLOCK(bp);
  382. /*
  383. * mark the AG header regions as static metadata The BNO
  384. * btree block is the first block after the headers, so
  385. * it's location defines the size of region the static
  386. * metadata consumes.
  387. *
  388. * Note: unlike mkfs, we never have to account for log
  389. * space when growing the data regions
  390. */
  391. rrec = XFS_RMAP_REC_ADDR(block, 1);
  392. rrec->rm_startblock = 0;
  393. rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
  394. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
  395. rrec->rm_offset = 0;
  396. be16_add_cpu(&block->bb_numrecs, 1);
  397. /* account freespace btree root blocks */
  398. rrec = XFS_RMAP_REC_ADDR(block, 2);
  399. rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
  400. rrec->rm_blockcount = cpu_to_be32(2);
  401. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
  402. rrec->rm_offset = 0;
  403. be16_add_cpu(&block->bb_numrecs, 1);
  404. /* account inode btree root blocks */
  405. rrec = XFS_RMAP_REC_ADDR(block, 3);
  406. rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
  407. rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
  408. XFS_IBT_BLOCK(mp));
  409. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
  410. rrec->rm_offset = 0;
  411. be16_add_cpu(&block->bb_numrecs, 1);
  412. /* account for rmap btree root */
  413. rrec = XFS_RMAP_REC_ADDR(block, 4);
  414. rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
  415. rrec->rm_blockcount = cpu_to_be32(1);
  416. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
  417. rrec->rm_offset = 0;
  418. be16_add_cpu(&block->bb_numrecs, 1);
  419. /* account for refc btree root */
  420. if (xfs_sb_version_hasreflink(&mp->m_sb)) {
  421. rrec = XFS_RMAP_REC_ADDR(block, 5);
  422. rrec->rm_startblock = cpu_to_be32(
  423. xfs_refc_block(mp));
  424. rrec->rm_blockcount = cpu_to_be32(1);
  425. rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC);
  426. rrec->rm_offset = 0;
  427. be16_add_cpu(&block->bb_numrecs, 1);
  428. }
  429. error = xfs_bwrite(bp);
  430. xfs_buf_relse(bp);
  431. if (error)
  432. goto error0;
  433. }
  434. /*
  435. * INO btree root block
  436. */
  437. bp = xfs_growfs_get_hdr_buf(mp,
  438. XFS_AGB_TO_DADDR(mp, agno, XFS_IBT_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_IBT_CRC_MAGIC, 0, 0,
  447. agno, XFS_BTREE_CRC_BLOCKS);
  448. else
  449. xfs_btree_init_block(mp, bp, XFS_IBT_MAGIC, 0, 0,
  450. agno, 0);
  451. error = xfs_bwrite(bp);
  452. xfs_buf_relse(bp);
  453. if (error)
  454. goto error0;
  455. /*
  456. * FINO btree root block
  457. */
  458. if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
  459. bp = xfs_growfs_get_hdr_buf(mp,
  460. XFS_AGB_TO_DADDR(mp, agno, XFS_FIBT_BLOCK(mp)),
  461. BTOBB(mp->m_sb.sb_blocksize), 0,
  462. &xfs_inobt_buf_ops);
  463. if (!bp) {
  464. error = -ENOMEM;
  465. goto error0;
  466. }
  467. if (xfs_sb_version_hascrc(&mp->m_sb))
  468. xfs_btree_init_block(mp, bp, XFS_FIBT_CRC_MAGIC,
  469. 0, 0, agno,
  470. XFS_BTREE_CRC_BLOCKS);
  471. else
  472. xfs_btree_init_block(mp, bp, XFS_FIBT_MAGIC, 0,
  473. 0, agno, 0);
  474. error = xfs_bwrite(bp);
  475. xfs_buf_relse(bp);
  476. if (error)
  477. goto error0;
  478. }
  479. /*
  480. * refcount btree root block
  481. */
  482. if (xfs_sb_version_hasreflink(&mp->m_sb)) {
  483. bp = xfs_growfs_get_hdr_buf(mp,
  484. XFS_AGB_TO_DADDR(mp, agno, xfs_refc_block(mp)),
  485. BTOBB(mp->m_sb.sb_blocksize), 0,
  486. &xfs_refcountbt_buf_ops);
  487. if (!bp) {
  488. error = -ENOMEM;
  489. goto error0;
  490. }
  491. xfs_btree_init_block(mp, bp, XFS_REFC_CRC_MAGIC,
  492. 0, 0, agno,
  493. XFS_BTREE_CRC_BLOCKS);
  494. error = xfs_bwrite(bp);
  495. xfs_buf_relse(bp);
  496. if (error)
  497. goto error0;
  498. }
  499. }
  500. xfs_trans_agblocks_delta(tp, nfree);
  501. /*
  502. * There are new blocks in the old last a.g.
  503. */
  504. if (new) {
  505. struct xfs_owner_info oinfo;
  506. /*
  507. * Change the agi length.
  508. */
  509. error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
  510. if (error) {
  511. goto error0;
  512. }
  513. ASSERT(bp);
  514. agi = XFS_BUF_TO_AGI(bp);
  515. be32_add_cpu(&agi->agi_length, new);
  516. ASSERT(nagcount == oagcount ||
  517. be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks);
  518. xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
  519. /*
  520. * Change agf length.
  521. */
  522. error = xfs_alloc_read_agf(mp, tp, agno, 0, &bp);
  523. if (error) {
  524. goto error0;
  525. }
  526. ASSERT(bp);
  527. agf = XFS_BUF_TO_AGF(bp);
  528. be32_add_cpu(&agf->agf_length, new);
  529. ASSERT(be32_to_cpu(agf->agf_length) ==
  530. be32_to_cpu(agi->agi_length));
  531. xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH);
  532. /*
  533. * Free the new space.
  534. *
  535. * XFS_RMAP_OWN_NULL is used here to tell the rmap btree that
  536. * this doesn't actually exist in the rmap btree.
  537. */
  538. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_NULL);
  539. error = xfs_free_extent(tp,
  540. XFS_AGB_TO_FSB(mp, agno,
  541. be32_to_cpu(agf->agf_length) - new),
  542. new, &oinfo, XFS_AG_RESV_NONE);
  543. if (error)
  544. goto error0;
  545. }
  546. /*
  547. * Update changed superblock fields transactionally. These are not
  548. * seen by the rest of the world until the transaction commit applies
  549. * them atomically to the superblock.
  550. */
  551. if (nagcount > oagcount)
  552. xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
  553. if (nb > mp->m_sb.sb_dblocks)
  554. xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
  555. nb - mp->m_sb.sb_dblocks);
  556. if (nfree)
  557. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, nfree);
  558. if (dpct)
  559. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
  560. xfs_trans_set_sync(tp);
  561. error = xfs_trans_commit(tp);
  562. if (error)
  563. return error;
  564. /* New allocation groups fully initialized, so update mount struct */
  565. if (nagimax)
  566. mp->m_maxagi = nagimax;
  567. if (mp->m_sb.sb_imax_pct) {
  568. __uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
  569. do_div(icount, 100);
  570. mp->m_maxicount = icount << mp->m_sb.sb_inopblog;
  571. } else
  572. mp->m_maxicount = 0;
  573. xfs_set_low_space_thresholds(mp);
  574. mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
  575. /*
  576. * If we expanded the last AG, free the per-AG reservation
  577. * so we can reinitialize it with the new size.
  578. */
  579. if (new) {
  580. struct xfs_perag *pag;
  581. pag = xfs_perag_get(mp, agno);
  582. error = xfs_ag_resv_free(pag);
  583. xfs_perag_put(pag);
  584. if (error)
  585. goto out;
  586. }
  587. /* Reserve AG metadata blocks. */
  588. error = xfs_fs_reserve_ag_blocks(mp);
  589. if (error && error != -ENOSPC)
  590. goto out;
  591. /* update secondary superblocks. */
  592. for (agno = 1; agno < nagcount; agno++) {
  593. error = 0;
  594. /*
  595. * new secondary superblocks need to be zeroed, not read from
  596. * disk as the contents of the new area we are growing into is
  597. * completely unknown.
  598. */
  599. if (agno < oagcount) {
  600. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  601. XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
  602. XFS_FSS_TO_BB(mp, 1), 0, &bp,
  603. &xfs_sb_buf_ops);
  604. } else {
  605. bp = xfs_trans_get_buf(NULL, mp->m_ddev_targp,
  606. XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
  607. XFS_FSS_TO_BB(mp, 1), 0);
  608. if (bp) {
  609. bp->b_ops = &xfs_sb_buf_ops;
  610. xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
  611. } else
  612. error = -ENOMEM;
  613. }
  614. /*
  615. * If we get an error reading or writing alternate superblocks,
  616. * continue. xfs_repair chooses the "best" superblock based
  617. * on most matches; if we break early, we'll leave more
  618. * superblocks un-updated than updated, and xfs_repair may
  619. * pick them over the properly-updated primary.
  620. */
  621. if (error) {
  622. xfs_warn(mp,
  623. "error %d reading secondary superblock for ag %d",
  624. error, agno);
  625. saved_error = error;
  626. continue;
  627. }
  628. xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
  629. error = xfs_bwrite(bp);
  630. xfs_buf_relse(bp);
  631. if (error) {
  632. xfs_warn(mp,
  633. "write error %d updating secondary superblock for ag %d",
  634. error, agno);
  635. saved_error = error;
  636. continue;
  637. }
  638. }
  639. out:
  640. return saved_error ? saved_error : error;
  641. error0:
  642. xfs_trans_cancel(tp);
  643. return error;
  644. }
  645. static int
  646. xfs_growfs_log_private(
  647. xfs_mount_t *mp, /* mount point for filesystem */
  648. xfs_growfs_log_t *in) /* growfs log input struct */
  649. {
  650. xfs_extlen_t nb;
  651. nb = in->newblocks;
  652. if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
  653. return -EINVAL;
  654. if (nb == mp->m_sb.sb_logblocks &&
  655. in->isint == (mp->m_sb.sb_logstart != 0))
  656. return -EINVAL;
  657. /*
  658. * Moving the log is hard, need new interfaces to sync
  659. * the log first, hold off all activity while moving it.
  660. * Can have shorter or longer log in the same space,
  661. * or transform internal to external log or vice versa.
  662. */
  663. return -ENOSYS;
  664. }
  665. /*
  666. * protected versions of growfs function acquire and release locks on the mount
  667. * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
  668. * XFS_IOC_FSGROWFSRT
  669. */
  670. int
  671. xfs_growfs_data(
  672. xfs_mount_t *mp,
  673. xfs_growfs_data_t *in)
  674. {
  675. int error;
  676. if (!capable(CAP_SYS_ADMIN))
  677. return -EPERM;
  678. if (!mutex_trylock(&mp->m_growlock))
  679. return -EWOULDBLOCK;
  680. error = xfs_growfs_data_private(mp, in);
  681. /*
  682. * Increment the generation unconditionally, the error could be from
  683. * updating the secondary superblocks, in which case the new size
  684. * is live already.
  685. */
  686. mp->m_generation++;
  687. mutex_unlock(&mp->m_growlock);
  688. return error;
  689. }
  690. int
  691. xfs_growfs_log(
  692. xfs_mount_t *mp,
  693. xfs_growfs_log_t *in)
  694. {
  695. int error;
  696. if (!capable(CAP_SYS_ADMIN))
  697. return -EPERM;
  698. if (!mutex_trylock(&mp->m_growlock))
  699. return -EWOULDBLOCK;
  700. error = xfs_growfs_log_private(mp, in);
  701. mutex_unlock(&mp->m_growlock);
  702. return error;
  703. }
  704. /*
  705. * exported through ioctl XFS_IOC_FSCOUNTS
  706. */
  707. int
  708. xfs_fs_counts(
  709. xfs_mount_t *mp,
  710. xfs_fsop_counts_t *cnt)
  711. {
  712. cnt->allocino = percpu_counter_read_positive(&mp->m_icount);
  713. cnt->freeino = percpu_counter_read_positive(&mp->m_ifree);
  714. cnt->freedata = percpu_counter_read_positive(&mp->m_fdblocks) -
  715. mp->m_alloc_set_aside;
  716. spin_lock(&mp->m_sb_lock);
  717. cnt->freertx = mp->m_sb.sb_frextents;
  718. spin_unlock(&mp->m_sb_lock);
  719. return 0;
  720. }
  721. /*
  722. * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
  723. *
  724. * xfs_reserve_blocks is called to set m_resblks
  725. * in the in-core mount table. The number of unused reserved blocks
  726. * is kept in m_resblks_avail.
  727. *
  728. * Reserve the requested number of blocks if available. Otherwise return
  729. * as many as possible to satisfy the request. The actual number
  730. * reserved are returned in outval
  731. *
  732. * A null inval pointer indicates that only the current reserved blocks
  733. * available should be returned no settings are changed.
  734. */
  735. int
  736. xfs_reserve_blocks(
  737. xfs_mount_t *mp,
  738. __uint64_t *inval,
  739. xfs_fsop_resblks_t *outval)
  740. {
  741. __int64_t lcounter, delta;
  742. __int64_t fdblks_delta = 0;
  743. __uint64_t request;
  744. __int64_t free;
  745. int error = 0;
  746. /* If inval is null, report current values and return */
  747. if (inval == (__uint64_t *)NULL) {
  748. if (!outval)
  749. return -EINVAL;
  750. outval->resblks = mp->m_resblks;
  751. outval->resblks_avail = mp->m_resblks_avail;
  752. return 0;
  753. }
  754. request = *inval;
  755. /*
  756. * With per-cpu counters, this becomes an interesting problem. we need
  757. * to work out if we are freeing or allocation blocks first, then we can
  758. * do the modification as necessary.
  759. *
  760. * We do this under the m_sb_lock so that if we are near ENOSPC, we will
  761. * hold out any changes while we work out what to do. This means that
  762. * the amount of free space can change while we do this, so we need to
  763. * retry if we end up trying to reserve more space than is available.
  764. */
  765. spin_lock(&mp->m_sb_lock);
  766. /*
  767. * If our previous reservation was larger than the current value,
  768. * then move any unused blocks back to the free pool. Modify the resblks
  769. * counters directly since we shouldn't have any problems unreserving
  770. * space.
  771. */
  772. if (mp->m_resblks > request) {
  773. lcounter = mp->m_resblks_avail - request;
  774. if (lcounter > 0) { /* release unused blocks */
  775. fdblks_delta = lcounter;
  776. mp->m_resblks_avail -= lcounter;
  777. }
  778. mp->m_resblks = request;
  779. if (fdblks_delta) {
  780. spin_unlock(&mp->m_sb_lock);
  781. error = xfs_mod_fdblocks(mp, fdblks_delta, 0);
  782. spin_lock(&mp->m_sb_lock);
  783. }
  784. goto out;
  785. }
  786. /*
  787. * If the request is larger than the current reservation, reserve the
  788. * blocks before we update the reserve counters. Sample m_fdblocks and
  789. * perform a partial reservation if the request exceeds free space.
  790. */
  791. error = -ENOSPC;
  792. do {
  793. free = percpu_counter_sum(&mp->m_fdblocks) -
  794. mp->m_alloc_set_aside;
  795. if (!free)
  796. break;
  797. delta = request - mp->m_resblks;
  798. lcounter = free - delta;
  799. if (lcounter < 0)
  800. /* We can't satisfy the request, just get what we can */
  801. fdblks_delta = free;
  802. else
  803. fdblks_delta = delta;
  804. /*
  805. * We'll either succeed in getting space from the free block
  806. * count or we'll get an ENOSPC. If we get a ENOSPC, it means
  807. * things changed while we were calculating fdblks_delta and so
  808. * we should try again to see if there is anything left to
  809. * reserve.
  810. *
  811. * Don't set the reserved flag here - we don't want to reserve
  812. * the extra reserve blocks from the reserve.....
  813. */
  814. spin_unlock(&mp->m_sb_lock);
  815. error = xfs_mod_fdblocks(mp, -fdblks_delta, 0);
  816. spin_lock(&mp->m_sb_lock);
  817. } while (error == -ENOSPC);
  818. /*
  819. * Update the reserve counters if blocks have been successfully
  820. * allocated.
  821. */
  822. if (!error && fdblks_delta) {
  823. mp->m_resblks += fdblks_delta;
  824. mp->m_resblks_avail += fdblks_delta;
  825. }
  826. out:
  827. if (outval) {
  828. outval->resblks = mp->m_resblks;
  829. outval->resblks_avail = mp->m_resblks_avail;
  830. }
  831. spin_unlock(&mp->m_sb_lock);
  832. return error;
  833. }
  834. int
  835. xfs_fs_goingdown(
  836. xfs_mount_t *mp,
  837. __uint32_t inflags)
  838. {
  839. switch (inflags) {
  840. case XFS_FSOP_GOING_FLAGS_DEFAULT: {
  841. struct super_block *sb = freeze_bdev(mp->m_super->s_bdev);
  842. if (sb && !IS_ERR(sb)) {
  843. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  844. thaw_bdev(sb->s_bdev, sb);
  845. }
  846. break;
  847. }
  848. case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
  849. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  850. break;
  851. case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
  852. xfs_force_shutdown(mp,
  853. SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
  854. break;
  855. default:
  856. return -EINVAL;
  857. }
  858. return 0;
  859. }
  860. /*
  861. * Force a shutdown of the filesystem instantly while keeping the filesystem
  862. * consistent. We don't do an unmount here; just shutdown the shop, make sure
  863. * that absolutely nothing persistent happens to this filesystem after this
  864. * point.
  865. */
  866. void
  867. xfs_do_force_shutdown(
  868. xfs_mount_t *mp,
  869. int flags,
  870. char *fname,
  871. int lnnum)
  872. {
  873. int logerror;
  874. logerror = flags & SHUTDOWN_LOG_IO_ERROR;
  875. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  876. xfs_notice(mp,
  877. "%s(0x%x) called from line %d of file %s. Return address = 0x%p",
  878. __func__, flags, lnnum, fname, __return_address);
  879. }
  880. /*
  881. * No need to duplicate efforts.
  882. */
  883. if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
  884. return;
  885. /*
  886. * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
  887. * queue up anybody new on the log reservations, and wakes up
  888. * everybody who's sleeping on log reservations to tell them
  889. * the bad news.
  890. */
  891. if (xfs_log_force_umount(mp, logerror))
  892. return;
  893. if (flags & SHUTDOWN_CORRUPT_INCORE) {
  894. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_CORRUPT,
  895. "Corruption of in-memory data detected. Shutting down filesystem");
  896. if (XFS_ERRLEVEL_HIGH <= xfs_error_level)
  897. xfs_stack_trace();
  898. } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  899. if (logerror) {
  900. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_LOGERROR,
  901. "Log I/O Error Detected. Shutting down filesystem");
  902. } else if (flags & SHUTDOWN_DEVICE_REQ) {
  903. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
  904. "All device paths lost. Shutting down filesystem");
  905. } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
  906. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
  907. "I/O Error Detected. Shutting down filesystem");
  908. }
  909. }
  910. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  911. xfs_alert(mp,
  912. "Please umount the filesystem and rectify the problem(s)");
  913. }
  914. }
  915. /*
  916. * Reserve free space for per-AG metadata.
  917. */
  918. int
  919. xfs_fs_reserve_ag_blocks(
  920. struct xfs_mount *mp)
  921. {
  922. xfs_agnumber_t agno;
  923. struct xfs_perag *pag;
  924. int error = 0;
  925. int err2;
  926. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  927. pag = xfs_perag_get(mp, agno);
  928. err2 = xfs_ag_resv_init(pag);
  929. xfs_perag_put(pag);
  930. if (err2 && !error)
  931. error = err2;
  932. }
  933. if (error && error != -ENOSPC) {
  934. xfs_warn(mp,
  935. "Error %d reserving per-AG metadata reserve pool.", error);
  936. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  937. }
  938. return error;
  939. }
  940. /*
  941. * Free space reserved for per-AG metadata.
  942. */
  943. int
  944. xfs_fs_unreserve_ag_blocks(
  945. struct xfs_mount *mp)
  946. {
  947. xfs_agnumber_t agno;
  948. struct xfs_perag *pag;
  949. int error = 0;
  950. int err2;
  951. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  952. pag = xfs_perag_get(mp, agno);
  953. err2 = xfs_ag_resv_free(pag);
  954. xfs_perag_put(pag);
  955. if (err2 && !error)
  956. error = err2;
  957. }
  958. if (error)
  959. xfs_warn(mp,
  960. "Error %d freeing per-AG metadata reserve pool.", error);
  961. return error;
  962. }