xfs_fsops.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. }