xfs_fsops.c 23 KB

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