xfs_fsops.c 23 KB

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