xfs_fsops.c 23 KB

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