xfs_fsops.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. /*
  48. * File system operations
  49. */
  50. int
  51. xfs_fs_geometry(
  52. xfs_mount_t *mp,
  53. xfs_fsop_geom_t *geo,
  54. int new_version)
  55. {
  56. geo->blocksize = mp->m_sb.sb_blocksize;
  57. geo->rtextsize = mp->m_sb.sb_rextsize;
  58. geo->agblocks = mp->m_sb.sb_agblocks;
  59. geo->agcount = mp->m_sb.sb_agcount;
  60. geo->logblocks = mp->m_sb.sb_logblocks;
  61. geo->sectsize = mp->m_sb.sb_sectsize;
  62. geo->inodesize = mp->m_sb.sb_inodesize;
  63. geo->imaxpct = mp->m_sb.sb_imax_pct;
  64. geo->datablocks = mp->m_sb.sb_dblocks;
  65. geo->rtblocks = mp->m_sb.sb_rblocks;
  66. geo->rtextents = mp->m_sb.sb_rextents;
  67. geo->logstart = mp->m_sb.sb_logstart;
  68. ASSERT(sizeof(geo->uuid)==sizeof(mp->m_sb.sb_uuid));
  69. memcpy(geo->uuid, &mp->m_sb.sb_uuid, sizeof(mp->m_sb.sb_uuid));
  70. if (new_version >= 2) {
  71. geo->sunit = mp->m_sb.sb_unit;
  72. geo->swidth = mp->m_sb.sb_width;
  73. }
  74. if (new_version >= 3) {
  75. geo->version = XFS_FSOP_GEOM_VERSION;
  76. geo->flags =
  77. (XFS_SB_VERSION_HASATTR(&mp->m_sb) ?
  78. XFS_FSOP_GEOM_FLAGS_ATTR : 0) |
  79. (XFS_SB_VERSION_HASNLINK(&mp->m_sb) ?
  80. XFS_FSOP_GEOM_FLAGS_NLINK : 0) |
  81. (XFS_SB_VERSION_HASQUOTA(&mp->m_sb) ?
  82. XFS_FSOP_GEOM_FLAGS_QUOTA : 0) |
  83. (XFS_SB_VERSION_HASALIGN(&mp->m_sb) ?
  84. XFS_FSOP_GEOM_FLAGS_IALIGN : 0) |
  85. (XFS_SB_VERSION_HASDALIGN(&mp->m_sb) ?
  86. XFS_FSOP_GEOM_FLAGS_DALIGN : 0) |
  87. (XFS_SB_VERSION_HASSHARED(&mp->m_sb) ?
  88. XFS_FSOP_GEOM_FLAGS_SHARED : 0) |
  89. (XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb) ?
  90. XFS_FSOP_GEOM_FLAGS_EXTFLG : 0) |
  91. (XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ?
  92. XFS_FSOP_GEOM_FLAGS_DIRV2 : 0) |
  93. (XFS_SB_VERSION_HASSECTOR(&mp->m_sb) ?
  94. XFS_FSOP_GEOM_FLAGS_SECTOR : 0) |
  95. (XFS_SB_VERSION_HASATTR2(&mp->m_sb) ?
  96. XFS_FSOP_GEOM_FLAGS_ATTR2 : 0);
  97. geo->logsectsize = XFS_SB_VERSION_HASSECTOR(&mp->m_sb) ?
  98. mp->m_sb.sb_logsectsize : BBSIZE;
  99. geo->rtsectsize = mp->m_sb.sb_blocksize;
  100. geo->dirblocksize = mp->m_dirblksize;
  101. }
  102. if (new_version >= 4) {
  103. geo->flags |=
  104. (XFS_SB_VERSION_HASLOGV2(&mp->m_sb) ?
  105. XFS_FSOP_GEOM_FLAGS_LOGV2 : 0);
  106. geo->logsunit = mp->m_sb.sb_logsunit;
  107. }
  108. return 0;
  109. }
  110. static int
  111. xfs_growfs_data_private(
  112. xfs_mount_t *mp, /* mount point for filesystem */
  113. xfs_growfs_data_t *in) /* growfs data input struct */
  114. {
  115. xfs_agf_t *agf;
  116. xfs_agi_t *agi;
  117. xfs_agnumber_t agno;
  118. xfs_extlen_t agsize;
  119. xfs_extlen_t tmpsize;
  120. xfs_alloc_rec_t *arec;
  121. xfs_btree_sblock_t *block;
  122. xfs_buf_t *bp;
  123. int bucket;
  124. int dpct;
  125. int error;
  126. xfs_agnumber_t nagcount;
  127. xfs_agnumber_t nagimax = 0;
  128. xfs_rfsblock_t nb, nb_mod;
  129. xfs_rfsblock_t new;
  130. xfs_rfsblock_t nfree;
  131. xfs_agnumber_t oagcount;
  132. int pct;
  133. xfs_sb_t *sbp;
  134. xfs_trans_t *tp;
  135. nb = in->newblocks;
  136. pct = in->imaxpct;
  137. if (nb < mp->m_sb.sb_dblocks || pct < 0 || pct > 100)
  138. return XFS_ERROR(EINVAL);
  139. dpct = pct - mp->m_sb.sb_imax_pct;
  140. error = xfs_read_buf(mp, mp->m_ddev_targp,
  141. XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
  142. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  143. if (error)
  144. return error;
  145. ASSERT(bp);
  146. xfs_buf_relse(bp);
  147. new = nb; /* use new as a temporary here */
  148. nb_mod = do_div(new, mp->m_sb.sb_agblocks);
  149. nagcount = new + (nb_mod != 0);
  150. if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
  151. nagcount--;
  152. nb = nagcount * mp->m_sb.sb_agblocks;
  153. if (nb < mp->m_sb.sb_dblocks)
  154. return XFS_ERROR(EINVAL);
  155. }
  156. new = nb - mp->m_sb.sb_dblocks;
  157. oagcount = mp->m_sb.sb_agcount;
  158. if (nagcount > oagcount) {
  159. down_write(&mp->m_peraglock);
  160. mp->m_perag = kmem_realloc(mp->m_perag,
  161. sizeof(xfs_perag_t) * nagcount,
  162. sizeof(xfs_perag_t) * oagcount,
  163. KM_SLEEP);
  164. memset(&mp->m_perag[oagcount], 0,
  165. (nagcount - oagcount) * sizeof(xfs_perag_t));
  166. mp->m_flags |= XFS_MOUNT_32BITINODES;
  167. nagimax = xfs_initialize_perag(XFS_MTOVFS(mp), mp, nagcount);
  168. up_write(&mp->m_peraglock);
  169. }
  170. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFS);
  171. if ((error = xfs_trans_reserve(tp, XFS_GROWFS_SPACE_RES(mp),
  172. XFS_GROWDATA_LOG_RES(mp), 0, 0, 0))) {
  173. xfs_trans_cancel(tp, 0);
  174. return error;
  175. }
  176. nfree = 0;
  177. for (agno = nagcount - 1; agno >= oagcount; agno--, new -= agsize) {
  178. /*
  179. * AG freelist header block
  180. */
  181. bp = xfs_buf_get(mp->m_ddev_targp,
  182. XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
  183. XFS_FSS_TO_BB(mp, 1), 0);
  184. agf = XFS_BUF_TO_AGF(bp);
  185. memset(agf, 0, mp->m_sb.sb_sectsize);
  186. agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
  187. agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
  188. agf->agf_seqno = cpu_to_be32(agno);
  189. if (agno == nagcount - 1)
  190. agsize =
  191. nb -
  192. (agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
  193. else
  194. agsize = mp->m_sb.sb_agblocks;
  195. agf->agf_length = cpu_to_be32(agsize);
  196. agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
  197. agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
  198. agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
  199. agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
  200. agf->agf_flfirst = 0;
  201. agf->agf_fllast = cpu_to_be32(XFS_AGFL_SIZE(mp) - 1);
  202. agf->agf_flcount = 0;
  203. tmpsize = agsize - XFS_PREALLOC_BLOCKS(mp);
  204. agf->agf_freeblks = cpu_to_be32(tmpsize);
  205. agf->agf_longest = cpu_to_be32(tmpsize);
  206. error = xfs_bwrite(mp, bp);
  207. if (error) {
  208. goto error0;
  209. }
  210. /*
  211. * AG inode header block
  212. */
  213. bp = xfs_buf_get(mp->m_ddev_targp,
  214. XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
  215. XFS_FSS_TO_BB(mp, 1), 0);
  216. agi = XFS_BUF_TO_AGI(bp);
  217. memset(agi, 0, mp->m_sb.sb_sectsize);
  218. agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
  219. agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
  220. agi->agi_seqno = cpu_to_be32(agno);
  221. agi->agi_length = cpu_to_be32(agsize);
  222. agi->agi_count = 0;
  223. agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
  224. agi->agi_level = cpu_to_be32(1);
  225. agi->agi_freecount = 0;
  226. agi->agi_newino = cpu_to_be32(NULLAGINO);
  227. agi->agi_dirino = cpu_to_be32(NULLAGINO);
  228. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
  229. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  230. error = xfs_bwrite(mp, bp);
  231. if (error) {
  232. goto error0;
  233. }
  234. /*
  235. * BNO btree root block
  236. */
  237. bp = xfs_buf_get(mp->m_ddev_targp,
  238. XFS_AGB_TO_DADDR(mp, agno, XFS_BNO_BLOCK(mp)),
  239. BTOBB(mp->m_sb.sb_blocksize), 0);
  240. block = XFS_BUF_TO_SBLOCK(bp);
  241. memset(block, 0, mp->m_sb.sb_blocksize);
  242. block->bb_magic = cpu_to_be32(XFS_ABTB_MAGIC);
  243. block->bb_level = 0;
  244. block->bb_numrecs = cpu_to_be16(1);
  245. block->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  246. block->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  247. arec = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_alloc,
  248. block, 1, mp->m_alloc_mxr[0]);
  249. arec->ar_startblock = cpu_to_be32(XFS_PREALLOC_BLOCKS(mp));
  250. arec->ar_blockcount = cpu_to_be32(
  251. agsize - be32_to_cpu(arec->ar_startblock));
  252. error = xfs_bwrite(mp, bp);
  253. if (error) {
  254. goto error0;
  255. }
  256. /*
  257. * CNT btree root block
  258. */
  259. bp = xfs_buf_get(mp->m_ddev_targp,
  260. XFS_AGB_TO_DADDR(mp, agno, XFS_CNT_BLOCK(mp)),
  261. BTOBB(mp->m_sb.sb_blocksize), 0);
  262. block = XFS_BUF_TO_SBLOCK(bp);
  263. memset(block, 0, mp->m_sb.sb_blocksize);
  264. block->bb_magic = cpu_to_be32(XFS_ABTC_MAGIC);
  265. block->bb_level = 0;
  266. block->bb_numrecs = cpu_to_be16(1);
  267. block->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  268. block->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  269. arec = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_alloc,
  270. block, 1, mp->m_alloc_mxr[0]);
  271. arec->ar_startblock = cpu_to_be32(XFS_PREALLOC_BLOCKS(mp));
  272. arec->ar_blockcount = cpu_to_be32(
  273. agsize - be32_to_cpu(arec->ar_startblock));
  274. nfree += be32_to_cpu(arec->ar_blockcount);
  275. error = xfs_bwrite(mp, bp);
  276. if (error) {
  277. goto error0;
  278. }
  279. /*
  280. * INO btree root block
  281. */
  282. bp = xfs_buf_get(mp->m_ddev_targp,
  283. XFS_AGB_TO_DADDR(mp, agno, XFS_IBT_BLOCK(mp)),
  284. BTOBB(mp->m_sb.sb_blocksize), 0);
  285. block = XFS_BUF_TO_SBLOCK(bp);
  286. memset(block, 0, mp->m_sb.sb_blocksize);
  287. block->bb_magic = cpu_to_be32(XFS_IBT_MAGIC);
  288. block->bb_level = 0;
  289. block->bb_numrecs = 0;
  290. block->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  291. block->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  292. error = xfs_bwrite(mp, bp);
  293. if (error) {
  294. goto error0;
  295. }
  296. }
  297. xfs_trans_agblocks_delta(tp, nfree);
  298. /*
  299. * There are new blocks in the old last a.g.
  300. */
  301. if (new) {
  302. /*
  303. * Change the agi length.
  304. */
  305. error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
  306. if (error) {
  307. goto error0;
  308. }
  309. ASSERT(bp);
  310. agi = XFS_BUF_TO_AGI(bp);
  311. be32_add(&agi->agi_length, new);
  312. ASSERT(nagcount == oagcount ||
  313. be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks);
  314. xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
  315. /*
  316. * Change agf length.
  317. */
  318. error = xfs_alloc_read_agf(mp, tp, agno, 0, &bp);
  319. if (error) {
  320. goto error0;
  321. }
  322. ASSERT(bp);
  323. agf = XFS_BUF_TO_AGF(bp);
  324. be32_add(&agf->agf_length, new);
  325. ASSERT(be32_to_cpu(agf->agf_length) ==
  326. be32_to_cpu(agi->agi_length));
  327. /*
  328. * Free the new space.
  329. */
  330. error = xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, agno,
  331. be32_to_cpu(agf->agf_length) - new), new);
  332. if (error) {
  333. goto error0;
  334. }
  335. }
  336. if (nagcount > oagcount)
  337. xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
  338. if (nb > mp->m_sb.sb_dblocks)
  339. xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
  340. nb - mp->m_sb.sb_dblocks);
  341. if (nfree)
  342. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, nfree);
  343. if (dpct)
  344. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
  345. error = xfs_trans_commit(tp, 0, NULL);
  346. if (error) {
  347. return error;
  348. }
  349. /* New allocation groups fully initialized, so update mount struct */
  350. if (nagimax)
  351. mp->m_maxagi = nagimax;
  352. if (mp->m_sb.sb_imax_pct) {
  353. __uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
  354. do_div(icount, 100);
  355. mp->m_maxicount = icount << mp->m_sb.sb_inopblog;
  356. } else
  357. mp->m_maxicount = 0;
  358. for (agno = 1; agno < nagcount; agno++) {
  359. error = xfs_read_buf(mp, mp->m_ddev_targp,
  360. XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
  361. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  362. if (error) {
  363. xfs_fs_cmn_err(CE_WARN, mp,
  364. "error %d reading secondary superblock for ag %d",
  365. error, agno);
  366. break;
  367. }
  368. sbp = XFS_BUF_TO_SBP(bp);
  369. xfs_xlatesb(sbp, &mp->m_sb, -1, XFS_SB_ALL_BITS);
  370. /*
  371. * If we get an error writing out the alternate superblocks,
  372. * just issue a warning and continue. The real work is
  373. * already done and committed.
  374. */
  375. if (!(error = xfs_bwrite(mp, bp))) {
  376. continue;
  377. } else {
  378. xfs_fs_cmn_err(CE_WARN, mp,
  379. "write error %d updating secondary superblock for ag %d",
  380. error, agno);
  381. break; /* no point in continuing */
  382. }
  383. }
  384. return 0;
  385. error0:
  386. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  387. return error;
  388. }
  389. static int
  390. xfs_growfs_log_private(
  391. xfs_mount_t *mp, /* mount point for filesystem */
  392. xfs_growfs_log_t *in) /* growfs log input struct */
  393. {
  394. xfs_extlen_t nb;
  395. nb = in->newblocks;
  396. if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
  397. return XFS_ERROR(EINVAL);
  398. if (nb == mp->m_sb.sb_logblocks &&
  399. in->isint == (mp->m_sb.sb_logstart != 0))
  400. return XFS_ERROR(EINVAL);
  401. /*
  402. * Moving the log is hard, need new interfaces to sync
  403. * the log first, hold off all activity while moving it.
  404. * Can have shorter or longer log in the same space,
  405. * or transform internal to external log or vice versa.
  406. */
  407. return XFS_ERROR(ENOSYS);
  408. }
  409. /*
  410. * protected versions of growfs function acquire and release locks on the mount
  411. * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
  412. * XFS_IOC_FSGROWFSRT
  413. */
  414. int
  415. xfs_growfs_data(
  416. xfs_mount_t *mp,
  417. xfs_growfs_data_t *in)
  418. {
  419. int error;
  420. if (!cpsema(&mp->m_growlock))
  421. return XFS_ERROR(EWOULDBLOCK);
  422. error = xfs_growfs_data_private(mp, in);
  423. vsema(&mp->m_growlock);
  424. return error;
  425. }
  426. int
  427. xfs_growfs_log(
  428. xfs_mount_t *mp,
  429. xfs_growfs_log_t *in)
  430. {
  431. int error;
  432. if (!cpsema(&mp->m_growlock))
  433. return XFS_ERROR(EWOULDBLOCK);
  434. error = xfs_growfs_log_private(mp, in);
  435. vsema(&mp->m_growlock);
  436. return error;
  437. }
  438. /*
  439. * exported through ioctl XFS_IOC_FSCOUNTS
  440. */
  441. int
  442. xfs_fs_counts(
  443. xfs_mount_t *mp,
  444. xfs_fsop_counts_t *cnt)
  445. {
  446. unsigned long s;
  447. xfs_icsb_sync_counters_lazy(mp);
  448. s = XFS_SB_LOCK(mp);
  449. cnt->freedata = mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  450. cnt->freertx = mp->m_sb.sb_frextents;
  451. cnt->freeino = mp->m_sb.sb_ifree;
  452. cnt->allocino = mp->m_sb.sb_icount;
  453. XFS_SB_UNLOCK(mp, s);
  454. return 0;
  455. }
  456. /*
  457. * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
  458. *
  459. * xfs_reserve_blocks is called to set m_resblks
  460. * in the in-core mount table. The number of unused reserved blocks
  461. * is kept in m_resblks_avail.
  462. *
  463. * Reserve the requested number of blocks if available. Otherwise return
  464. * as many as possible to satisfy the request. The actual number
  465. * reserved are returned in outval
  466. *
  467. * A null inval pointer indicates that only the current reserved blocks
  468. * available should be returned no settings are changed.
  469. */
  470. int
  471. xfs_reserve_blocks(
  472. xfs_mount_t *mp,
  473. __uint64_t *inval,
  474. xfs_fsop_resblks_t *outval)
  475. {
  476. __int64_t lcounter, delta;
  477. __uint64_t request;
  478. unsigned long s;
  479. /* If inval is null, report current values and return */
  480. if (inval == (__uint64_t *)NULL) {
  481. outval->resblks = mp->m_resblks;
  482. outval->resblks_avail = mp->m_resblks_avail;
  483. return 0;
  484. }
  485. request = *inval;
  486. s = XFS_SB_LOCK(mp);
  487. /*
  488. * If our previous reservation was larger than the current value,
  489. * then move any unused blocks back to the free pool.
  490. */
  491. if (mp->m_resblks > request) {
  492. lcounter = mp->m_resblks_avail - request;
  493. if (lcounter > 0) { /* release unused blocks */
  494. mp->m_sb.sb_fdblocks += lcounter;
  495. mp->m_resblks_avail -= lcounter;
  496. }
  497. mp->m_resblks = request;
  498. } else {
  499. __int64_t free;
  500. free = mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  501. delta = request - mp->m_resblks;
  502. lcounter = free - delta;
  503. if (lcounter < 0) {
  504. /* We can't satisfy the request, just get what we can */
  505. mp->m_resblks += free;
  506. mp->m_resblks_avail += free;
  507. mp->m_sb.sb_fdblocks = XFS_ALLOC_SET_ASIDE(mp);
  508. } else {
  509. mp->m_sb.sb_fdblocks =
  510. lcounter + XFS_ALLOC_SET_ASIDE(mp);
  511. mp->m_resblks = request;
  512. mp->m_resblks_avail += delta;
  513. }
  514. }
  515. outval->resblks = mp->m_resblks;
  516. outval->resblks_avail = mp->m_resblks_avail;
  517. XFS_SB_UNLOCK(mp, s);
  518. return 0;
  519. }
  520. void
  521. xfs_fs_log_dummy(
  522. xfs_mount_t *mp)
  523. {
  524. xfs_trans_t *tp;
  525. xfs_inode_t *ip;
  526. tp = _xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
  527. if (xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0)) {
  528. xfs_trans_cancel(tp, 0);
  529. return;
  530. }
  531. ip = mp->m_rootip;
  532. xfs_ilock(ip, XFS_ILOCK_EXCL);
  533. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  534. xfs_trans_ihold(tp, ip);
  535. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  536. xfs_trans_set_sync(tp);
  537. xfs_trans_commit(tp, 0, NULL);
  538. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  539. }
  540. int
  541. xfs_fs_goingdown(
  542. xfs_mount_t *mp,
  543. __uint32_t inflags)
  544. {
  545. switch (inflags) {
  546. case XFS_FSOP_GOING_FLAGS_DEFAULT: {
  547. struct bhv_vfs *vfsp = XFS_MTOVFS(mp);
  548. struct super_block *sb = freeze_bdev(vfsp->vfs_super->s_bdev);
  549. if (sb && !IS_ERR(sb)) {
  550. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  551. thaw_bdev(sb->s_bdev, sb);
  552. }
  553. break;
  554. }
  555. case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
  556. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  557. break;
  558. case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
  559. xfs_force_shutdown(mp,
  560. SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
  561. break;
  562. default:
  563. return XFS_ERROR(EINVAL);
  564. }
  565. return 0;
  566. }