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