xfs_fsops.c 19 KB

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