xfs_fsops.c 23 KB

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