xfs_fsops.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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_bit.h"
  22. #include "xfs_inum.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_rw.h"
  43. #include "xfs_filestream.h"
  44. #include "xfs_trace.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 =
  76. (xfs_sb_version_hasattr(&mp->m_sb) ?
  77. XFS_FSOP_GEOM_FLAGS_ATTR : 0) |
  78. (xfs_sb_version_hasnlink(&mp->m_sb) ?
  79. XFS_FSOP_GEOM_FLAGS_NLINK : 0) |
  80. (xfs_sb_version_hasquota(&mp->m_sb) ?
  81. XFS_FSOP_GEOM_FLAGS_QUOTA : 0) |
  82. (xfs_sb_version_hasalign(&mp->m_sb) ?
  83. XFS_FSOP_GEOM_FLAGS_IALIGN : 0) |
  84. (xfs_sb_version_hasdalign(&mp->m_sb) ?
  85. XFS_FSOP_GEOM_FLAGS_DALIGN : 0) |
  86. (xfs_sb_version_hasshared(&mp->m_sb) ?
  87. XFS_FSOP_GEOM_FLAGS_SHARED : 0) |
  88. (xfs_sb_version_hasextflgbit(&mp->m_sb) ?
  89. XFS_FSOP_GEOM_FLAGS_EXTFLG : 0) |
  90. (xfs_sb_version_hasdirv2(&mp->m_sb) ?
  91. XFS_FSOP_GEOM_FLAGS_DIRV2 : 0) |
  92. (xfs_sb_version_hassector(&mp->m_sb) ?
  93. XFS_FSOP_GEOM_FLAGS_SECTOR : 0) |
  94. (xfs_sb_version_hasasciici(&mp->m_sb) ?
  95. XFS_FSOP_GEOM_FLAGS_DIRV2CI : 0) |
  96. (xfs_sb_version_haslazysbcount(&mp->m_sb) ?
  97. XFS_FSOP_GEOM_FLAGS_LAZYSB : 0) |
  98. (xfs_sb_version_hasattr2(&mp->m_sb) ?
  99. XFS_FSOP_GEOM_FLAGS_ATTR2 : 0);
  100. geo->logsectsize = xfs_sb_version_hassector(&mp->m_sb) ?
  101. mp->m_sb.sb_logsectsize : BBSIZE;
  102. geo->rtsectsize = mp->m_sb.sb_blocksize;
  103. geo->dirblocksize = mp->m_dirblksize;
  104. }
  105. if (new_version >= 4) {
  106. geo->flags |=
  107. (xfs_sb_version_haslogv2(&mp->m_sb) ?
  108. XFS_FSOP_GEOM_FLAGS_LOGV2 : 0);
  109. geo->logsunit = mp->m_sb.sb_logsunit;
  110. }
  111. return 0;
  112. }
  113. static int
  114. xfs_growfs_data_private(
  115. xfs_mount_t *mp, /* mount point for filesystem */
  116. xfs_growfs_data_t *in) /* growfs data input struct */
  117. {
  118. xfs_agf_t *agf;
  119. xfs_agi_t *agi;
  120. xfs_agnumber_t agno;
  121. xfs_extlen_t agsize;
  122. xfs_extlen_t tmpsize;
  123. xfs_alloc_rec_t *arec;
  124. struct xfs_btree_block *block;
  125. xfs_buf_t *bp;
  126. int bucket;
  127. int dpct;
  128. int error;
  129. xfs_agnumber_t nagcount;
  130. xfs_agnumber_t nagimax = 0;
  131. xfs_rfsblock_t nb, nb_mod;
  132. xfs_rfsblock_t new;
  133. xfs_rfsblock_t nfree;
  134. xfs_agnumber_t oagcount;
  135. int pct;
  136. xfs_trans_t *tp;
  137. nb = in->newblocks;
  138. pct = in->imaxpct;
  139. if (nb < mp->m_sb.sb_dblocks || pct < 0 || pct > 100)
  140. return XFS_ERROR(EINVAL);
  141. if ((error = xfs_sb_validate_fsb_count(&mp->m_sb, nb)))
  142. return error;
  143. dpct = pct - mp->m_sb.sb_imax_pct;
  144. bp = xfs_buf_read_uncached(mp, mp->m_ddev_targp,
  145. XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
  146. BBTOB(XFS_FSS_TO_BB(mp, 1)), 0);
  147. if (!bp)
  148. return EIO;
  149. xfs_buf_relse(bp);
  150. new = nb; /* use new as a temporary here */
  151. nb_mod = do_div(new, mp->m_sb.sb_agblocks);
  152. nagcount = new + (nb_mod != 0);
  153. if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
  154. nagcount--;
  155. nb = (xfs_rfsblock_t)nagcount * mp->m_sb.sb_agblocks;
  156. if (nb < mp->m_sb.sb_dblocks)
  157. return XFS_ERROR(EINVAL);
  158. }
  159. new = nb - mp->m_sb.sb_dblocks;
  160. oagcount = mp->m_sb.sb_agcount;
  161. /* allocate the new per-ag structures */
  162. if (nagcount > oagcount) {
  163. error = xfs_initialize_perag(mp, nagcount, &nagimax);
  164. if (error)
  165. return error;
  166. }
  167. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFS);
  168. tp->t_flags |= XFS_TRANS_RESERVE;
  169. if ((error = xfs_trans_reserve(tp, XFS_GROWFS_SPACE_RES(mp),
  170. XFS_GROWDATA_LOG_RES(mp), 0, 0, 0))) {
  171. xfs_trans_cancel(tp, 0);
  172. return error;
  173. }
  174. /*
  175. * Write new AG headers to disk. Non-transactional, but written
  176. * synchronously so they are completed prior to the growfs transaction
  177. * being logged.
  178. */
  179. nfree = 0;
  180. for (agno = nagcount - 1; agno >= oagcount; agno--, new -= agsize) {
  181. /*
  182. * AG freelist header block
  183. */
  184. bp = xfs_buf_get(mp->m_ddev_targp,
  185. XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
  186. XFS_FSS_TO_BB(mp, 1), XBF_LOCK | XBF_MAPPED);
  187. if (!bp) {
  188. error = ENOMEM;
  189. goto error0;
  190. }
  191. agf = XFS_BUF_TO_AGF(bp);
  192. memset(agf, 0, mp->m_sb.sb_sectsize);
  193. agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
  194. agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
  195. agf->agf_seqno = cpu_to_be32(agno);
  196. if (agno == nagcount - 1)
  197. agsize =
  198. nb -
  199. (agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
  200. else
  201. agsize = mp->m_sb.sb_agblocks;
  202. agf->agf_length = cpu_to_be32(agsize);
  203. agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
  204. agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
  205. agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
  206. agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
  207. agf->agf_flfirst = 0;
  208. agf->agf_fllast = cpu_to_be32(XFS_AGFL_SIZE(mp) - 1);
  209. agf->agf_flcount = 0;
  210. tmpsize = agsize - XFS_PREALLOC_BLOCKS(mp);
  211. agf->agf_freeblks = cpu_to_be32(tmpsize);
  212. agf->agf_longest = cpu_to_be32(tmpsize);
  213. error = xfs_bwrite(bp);
  214. xfs_buf_relse(bp);
  215. if (error)
  216. goto error0;
  217. /*
  218. * AG inode header block
  219. */
  220. bp = xfs_buf_get(mp->m_ddev_targp,
  221. XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
  222. XFS_FSS_TO_BB(mp, 1), XBF_LOCK | XBF_MAPPED);
  223. if (!bp) {
  224. error = ENOMEM;
  225. goto error0;
  226. }
  227. agi = XFS_BUF_TO_AGI(bp);
  228. memset(agi, 0, mp->m_sb.sb_sectsize);
  229. agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
  230. agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
  231. agi->agi_seqno = cpu_to_be32(agno);
  232. agi->agi_length = cpu_to_be32(agsize);
  233. agi->agi_count = 0;
  234. agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
  235. agi->agi_level = cpu_to_be32(1);
  236. agi->agi_freecount = 0;
  237. agi->agi_newino = cpu_to_be32(NULLAGINO);
  238. agi->agi_dirino = cpu_to_be32(NULLAGINO);
  239. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
  240. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  241. error = xfs_bwrite(bp);
  242. xfs_buf_relse(bp);
  243. if (error)
  244. goto error0;
  245. /*
  246. * BNO btree root block
  247. */
  248. bp = xfs_buf_get(mp->m_ddev_targp,
  249. XFS_AGB_TO_DADDR(mp, agno, XFS_BNO_BLOCK(mp)),
  250. BTOBB(mp->m_sb.sb_blocksize),
  251. XBF_LOCK | XBF_MAPPED);
  252. if (!bp) {
  253. error = ENOMEM;
  254. goto error0;
  255. }
  256. block = XFS_BUF_TO_BLOCK(bp);
  257. memset(block, 0, mp->m_sb.sb_blocksize);
  258. block->bb_magic = cpu_to_be32(XFS_ABTB_MAGIC);
  259. block->bb_level = 0;
  260. block->bb_numrecs = cpu_to_be16(1);
  261. block->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  262. block->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  263. arec = XFS_ALLOC_REC_ADDR(mp, block, 1);
  264. arec->ar_startblock = cpu_to_be32(XFS_PREALLOC_BLOCKS(mp));
  265. arec->ar_blockcount = cpu_to_be32(
  266. agsize - be32_to_cpu(arec->ar_startblock));
  267. error = xfs_bwrite(bp);
  268. xfs_buf_relse(bp);
  269. if (error)
  270. goto error0;
  271. /*
  272. * CNT btree root block
  273. */
  274. bp = xfs_buf_get(mp->m_ddev_targp,
  275. XFS_AGB_TO_DADDR(mp, agno, XFS_CNT_BLOCK(mp)),
  276. BTOBB(mp->m_sb.sb_blocksize),
  277. XBF_LOCK | XBF_MAPPED);
  278. if (!bp) {
  279. error = ENOMEM;
  280. goto error0;
  281. }
  282. block = XFS_BUF_TO_BLOCK(bp);
  283. memset(block, 0, mp->m_sb.sb_blocksize);
  284. block->bb_magic = cpu_to_be32(XFS_ABTC_MAGIC);
  285. block->bb_level = 0;
  286. block->bb_numrecs = cpu_to_be16(1);
  287. block->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  288. block->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  289. arec = XFS_ALLOC_REC_ADDR(mp, block, 1);
  290. arec->ar_startblock = cpu_to_be32(XFS_PREALLOC_BLOCKS(mp));
  291. arec->ar_blockcount = cpu_to_be32(
  292. agsize - be32_to_cpu(arec->ar_startblock));
  293. nfree += be32_to_cpu(arec->ar_blockcount);
  294. error = xfs_bwrite(bp);
  295. xfs_buf_relse(bp);
  296. if (error)
  297. goto error0;
  298. /*
  299. * INO btree root block
  300. */
  301. bp = xfs_buf_get(mp->m_ddev_targp,
  302. XFS_AGB_TO_DADDR(mp, agno, XFS_IBT_BLOCK(mp)),
  303. BTOBB(mp->m_sb.sb_blocksize),
  304. XBF_LOCK | XBF_MAPPED);
  305. if (!bp) {
  306. error = ENOMEM;
  307. goto error0;
  308. }
  309. block = XFS_BUF_TO_BLOCK(bp);
  310. memset(block, 0, mp->m_sb.sb_blocksize);
  311. block->bb_magic = cpu_to_be32(XFS_IBT_MAGIC);
  312. block->bb_level = 0;
  313. block->bb_numrecs = 0;
  314. block->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  315. block->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  316. error = xfs_bwrite(bp);
  317. xfs_buf_relse(bp);
  318. if (error)
  319. goto error0;
  320. }
  321. xfs_trans_agblocks_delta(tp, nfree);
  322. /*
  323. * There are new blocks in the old last a.g.
  324. */
  325. if (new) {
  326. /*
  327. * Change the agi length.
  328. */
  329. error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
  330. if (error) {
  331. goto error0;
  332. }
  333. ASSERT(bp);
  334. agi = XFS_BUF_TO_AGI(bp);
  335. be32_add_cpu(&agi->agi_length, new);
  336. ASSERT(nagcount == oagcount ||
  337. be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks);
  338. xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
  339. /*
  340. * Change agf length.
  341. */
  342. error = xfs_alloc_read_agf(mp, tp, agno, 0, &bp);
  343. if (error) {
  344. goto error0;
  345. }
  346. ASSERT(bp);
  347. agf = XFS_BUF_TO_AGF(bp);
  348. be32_add_cpu(&agf->agf_length, new);
  349. ASSERT(be32_to_cpu(agf->agf_length) ==
  350. be32_to_cpu(agi->agi_length));
  351. xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH);
  352. /*
  353. * Free the new space.
  354. */
  355. error = xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, agno,
  356. be32_to_cpu(agf->agf_length) - new), new);
  357. if (error) {
  358. goto error0;
  359. }
  360. }
  361. /*
  362. * Update changed superblock fields transactionally. These are not
  363. * seen by the rest of the world until the transaction commit applies
  364. * them atomically to the superblock.
  365. */
  366. if (nagcount > oagcount)
  367. xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
  368. if (nb > mp->m_sb.sb_dblocks)
  369. xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
  370. nb - mp->m_sb.sb_dblocks);
  371. if (nfree)
  372. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, nfree);
  373. if (dpct)
  374. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
  375. error = xfs_trans_commit(tp, 0);
  376. if (error)
  377. return error;
  378. /* New allocation groups fully initialized, so update mount struct */
  379. if (nagimax)
  380. mp->m_maxagi = nagimax;
  381. if (mp->m_sb.sb_imax_pct) {
  382. __uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
  383. do_div(icount, 100);
  384. mp->m_maxicount = icount << mp->m_sb.sb_inopblog;
  385. } else
  386. mp->m_maxicount = 0;
  387. xfs_set_low_space_thresholds(mp);
  388. /* update secondary superblocks. */
  389. for (agno = 1; agno < nagcount; agno++) {
  390. error = xfs_read_buf(mp, mp->m_ddev_targp,
  391. XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
  392. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  393. if (error) {
  394. xfs_warn(mp,
  395. "error %d reading secondary superblock for ag %d",
  396. error, agno);
  397. break;
  398. }
  399. xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, XFS_SB_ALL_BITS);
  400. /*
  401. * If we get an error writing out the alternate superblocks,
  402. * just issue a warning and continue. The real work is
  403. * already done and committed.
  404. */
  405. error = xfs_bwrite(bp);
  406. xfs_buf_relse(bp);
  407. if (error) {
  408. xfs_warn(mp,
  409. "write error %d updating secondary superblock for ag %d",
  410. error, agno);
  411. break; /* no point in continuing */
  412. }
  413. }
  414. return 0;
  415. error0:
  416. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  417. return error;
  418. }
  419. static int
  420. xfs_growfs_log_private(
  421. xfs_mount_t *mp, /* mount point for filesystem */
  422. xfs_growfs_log_t *in) /* growfs log input struct */
  423. {
  424. xfs_extlen_t nb;
  425. nb = in->newblocks;
  426. if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
  427. return XFS_ERROR(EINVAL);
  428. if (nb == mp->m_sb.sb_logblocks &&
  429. in->isint == (mp->m_sb.sb_logstart != 0))
  430. return XFS_ERROR(EINVAL);
  431. /*
  432. * Moving the log is hard, need new interfaces to sync
  433. * the log first, hold off all activity while moving it.
  434. * Can have shorter or longer log in the same space,
  435. * or transform internal to external log or vice versa.
  436. */
  437. return XFS_ERROR(ENOSYS);
  438. }
  439. /*
  440. * protected versions of growfs function acquire and release locks on the mount
  441. * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
  442. * XFS_IOC_FSGROWFSRT
  443. */
  444. int
  445. xfs_growfs_data(
  446. xfs_mount_t *mp,
  447. xfs_growfs_data_t *in)
  448. {
  449. int error;
  450. if (!capable(CAP_SYS_ADMIN))
  451. return XFS_ERROR(EPERM);
  452. if (!mutex_trylock(&mp->m_growlock))
  453. return XFS_ERROR(EWOULDBLOCK);
  454. error = xfs_growfs_data_private(mp, in);
  455. mutex_unlock(&mp->m_growlock);
  456. return error;
  457. }
  458. int
  459. xfs_growfs_log(
  460. xfs_mount_t *mp,
  461. xfs_growfs_log_t *in)
  462. {
  463. int error;
  464. if (!capable(CAP_SYS_ADMIN))
  465. return XFS_ERROR(EPERM);
  466. if (!mutex_trylock(&mp->m_growlock))
  467. return XFS_ERROR(EWOULDBLOCK);
  468. error = xfs_growfs_log_private(mp, in);
  469. mutex_unlock(&mp->m_growlock);
  470. return error;
  471. }
  472. /*
  473. * exported through ioctl XFS_IOC_FSCOUNTS
  474. */
  475. int
  476. xfs_fs_counts(
  477. xfs_mount_t *mp,
  478. xfs_fsop_counts_t *cnt)
  479. {
  480. xfs_icsb_sync_counters(mp, XFS_ICSB_LAZY_COUNT);
  481. spin_lock(&mp->m_sb_lock);
  482. cnt->freedata = mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  483. cnt->freertx = mp->m_sb.sb_frextents;
  484. cnt->freeino = mp->m_sb.sb_ifree;
  485. cnt->allocino = mp->m_sb.sb_icount;
  486. spin_unlock(&mp->m_sb_lock);
  487. return 0;
  488. }
  489. /*
  490. * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
  491. *
  492. * xfs_reserve_blocks is called to set m_resblks
  493. * in the in-core mount table. The number of unused reserved blocks
  494. * is kept in m_resblks_avail.
  495. *
  496. * Reserve the requested number of blocks if available. Otherwise return
  497. * as many as possible to satisfy the request. The actual number
  498. * reserved are returned in outval
  499. *
  500. * A null inval pointer indicates that only the current reserved blocks
  501. * available should be returned no settings are changed.
  502. */
  503. int
  504. xfs_reserve_blocks(
  505. xfs_mount_t *mp,
  506. __uint64_t *inval,
  507. xfs_fsop_resblks_t *outval)
  508. {
  509. __int64_t lcounter, delta, fdblks_delta;
  510. __uint64_t request;
  511. /* If inval is null, report current values and return */
  512. if (inval == (__uint64_t *)NULL) {
  513. if (!outval)
  514. return EINVAL;
  515. outval->resblks = mp->m_resblks;
  516. outval->resblks_avail = mp->m_resblks_avail;
  517. return 0;
  518. }
  519. request = *inval;
  520. /*
  521. * With per-cpu counters, this becomes an interesting
  522. * problem. we needto work out if we are freeing or allocation
  523. * blocks first, then we can do the modification as necessary.
  524. *
  525. * We do this under the m_sb_lock so that if we are near
  526. * ENOSPC, we will hold out any changes while we work out
  527. * what to do. This means that the amount of free space can
  528. * change while we do this, so we need to retry if we end up
  529. * trying to reserve more space than is available.
  530. *
  531. * We also use the xfs_mod_incore_sb() interface so that we
  532. * don't have to care about whether per cpu counter are
  533. * enabled, disabled or even compiled in....
  534. */
  535. retry:
  536. spin_lock(&mp->m_sb_lock);
  537. xfs_icsb_sync_counters_locked(mp, 0);
  538. /*
  539. * If our previous reservation was larger than the current value,
  540. * then move any unused blocks back to the free pool.
  541. */
  542. fdblks_delta = 0;
  543. if (mp->m_resblks > request) {
  544. lcounter = mp->m_resblks_avail - request;
  545. if (lcounter > 0) { /* release unused blocks */
  546. fdblks_delta = lcounter;
  547. mp->m_resblks_avail -= lcounter;
  548. }
  549. mp->m_resblks = request;
  550. } else {
  551. __int64_t free;
  552. free = mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  553. if (!free)
  554. goto out; /* ENOSPC and fdblks_delta = 0 */
  555. delta = request - mp->m_resblks;
  556. lcounter = free - delta;
  557. if (lcounter < 0) {
  558. /* We can't satisfy the request, just get what we can */
  559. mp->m_resblks += free;
  560. mp->m_resblks_avail += free;
  561. fdblks_delta = -free;
  562. } else {
  563. fdblks_delta = -delta;
  564. mp->m_resblks = request;
  565. mp->m_resblks_avail += delta;
  566. }
  567. }
  568. out:
  569. if (outval) {
  570. outval->resblks = mp->m_resblks;
  571. outval->resblks_avail = mp->m_resblks_avail;
  572. }
  573. spin_unlock(&mp->m_sb_lock);
  574. if (fdblks_delta) {
  575. /*
  576. * If we are putting blocks back here, m_resblks_avail is
  577. * already at its max so this will put it in the free pool.
  578. *
  579. * If we need space, we'll either succeed in getting it
  580. * from the free block count or we'll get an enospc. If
  581. * we get a ENOSPC, it means things changed while we were
  582. * calculating fdblks_delta and so we should try again to
  583. * see if there is anything left to reserve.
  584. *
  585. * Don't set the reserved flag here - we don't want to reserve
  586. * the extra reserve blocks from the reserve.....
  587. */
  588. int error;
  589. error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
  590. fdblks_delta, 0);
  591. if (error == ENOSPC)
  592. goto retry;
  593. }
  594. return 0;
  595. }
  596. /*
  597. * Dump a transaction into the log that contains no real change. This is needed
  598. * to be able to make the log dirty or stamp the current tail LSN into the log
  599. * during the covering operation.
  600. *
  601. * We cannot use an inode here for this - that will push dirty state back up
  602. * into the VFS and then periodic inode flushing will prevent log covering from
  603. * making progress. Hence we log a field in the superblock instead and use a
  604. * synchronous transaction to ensure the superblock is immediately unpinned
  605. * and can be written back.
  606. */
  607. int
  608. xfs_fs_log_dummy(
  609. xfs_mount_t *mp)
  610. {
  611. xfs_trans_t *tp;
  612. int error;
  613. tp = _xfs_trans_alloc(mp, XFS_TRANS_DUMMY1, KM_SLEEP);
  614. error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
  615. XFS_DEFAULT_LOG_COUNT);
  616. if (error) {
  617. xfs_trans_cancel(tp, 0);
  618. return error;
  619. }
  620. /* log the UUID because it is an unchanging field */
  621. xfs_mod_sb(tp, XFS_SB_UUID);
  622. xfs_trans_set_sync(tp);
  623. return xfs_trans_commit(tp, 0);
  624. }
  625. int
  626. xfs_fs_goingdown(
  627. xfs_mount_t *mp,
  628. __uint32_t inflags)
  629. {
  630. switch (inflags) {
  631. case XFS_FSOP_GOING_FLAGS_DEFAULT: {
  632. struct super_block *sb = freeze_bdev(mp->m_super->s_bdev);
  633. if (sb && !IS_ERR(sb)) {
  634. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  635. thaw_bdev(sb->s_bdev, sb);
  636. }
  637. break;
  638. }
  639. case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
  640. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  641. break;
  642. case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
  643. xfs_force_shutdown(mp,
  644. SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
  645. break;
  646. default:
  647. return XFS_ERROR(EINVAL);
  648. }
  649. return 0;
  650. }