xfs_fsops.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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_dir.h"
  28. #include "xfs_dir2.h"
  29. #include "xfs_dmapi.h"
  30. #include "xfs_mount.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir_sf.h"
  35. #include "xfs_dir2_sf.h"
  36. #include "xfs_attr_sf.h"
  37. #include "xfs_dinode.h"
  38. #include "xfs_inode.h"
  39. #include "xfs_inode_item.h"
  40. #include "xfs_btree.h"
  41. #include "xfs_error.h"
  42. #include "xfs_alloc.h"
  43. #include "xfs_ialloc.h"
  44. #include "xfs_fsops.h"
  45. #include "xfs_itable.h"
  46. #include "xfs_trans_space.h"
  47. #include "xfs_rtalloc.h"
  48. #include "xfs_rw.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_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. xfs_btree_sblock_t *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_sb_t *sbp;
  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. dpct = pct - mp->m_sb.sb_imax_pct;
  142. error = xfs_read_buf(mp, mp->m_ddev_targp,
  143. XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
  144. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  145. if (error)
  146. return error;
  147. ASSERT(bp);
  148. xfs_buf_relse(bp);
  149. new = nb; /* use new as a temporary here */
  150. nb_mod = do_div(new, mp->m_sb.sb_agblocks);
  151. nagcount = new + (nb_mod != 0);
  152. if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
  153. nagcount--;
  154. nb = nagcount * mp->m_sb.sb_agblocks;
  155. if (nb < mp->m_sb.sb_dblocks)
  156. return XFS_ERROR(EINVAL);
  157. }
  158. new = nb - mp->m_sb.sb_dblocks;
  159. oagcount = mp->m_sb.sb_agcount;
  160. if (nagcount > oagcount) {
  161. down_write(&mp->m_peraglock);
  162. mp->m_perag = kmem_realloc(mp->m_perag,
  163. sizeof(xfs_perag_t) * nagcount,
  164. sizeof(xfs_perag_t) * oagcount,
  165. KM_SLEEP);
  166. memset(&mp->m_perag[oagcount], 0,
  167. (nagcount - oagcount) * sizeof(xfs_perag_t));
  168. mp->m_flags |= XFS_MOUNT_32BITINODES;
  169. nagimax = xfs_initialize_perag(XFS_MTOVFS(mp), mp, nagcount);
  170. up_write(&mp->m_peraglock);
  171. }
  172. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFS);
  173. if ((error = xfs_trans_reserve(tp, XFS_GROWFS_SPACE_RES(mp),
  174. XFS_GROWDATA_LOG_RES(mp), 0, 0, 0))) {
  175. xfs_trans_cancel(tp, 0);
  176. return error;
  177. }
  178. nfree = 0;
  179. for (agno = nagcount - 1; agno >= oagcount; agno--, new -= agsize) {
  180. /*
  181. * AG freelist header block
  182. */
  183. bp = xfs_buf_get(mp->m_ddev_targp,
  184. XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
  185. XFS_FSS_TO_BB(mp, 1), 0);
  186. agf = XFS_BUF_TO_AGF(bp);
  187. memset(agf, 0, mp->m_sb.sb_sectsize);
  188. agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
  189. agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
  190. agf->agf_seqno = cpu_to_be32(agno);
  191. if (agno == nagcount - 1)
  192. agsize =
  193. nb -
  194. (agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
  195. else
  196. agsize = mp->m_sb.sb_agblocks;
  197. agf->agf_length = cpu_to_be32(agsize);
  198. agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
  199. agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
  200. agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
  201. agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
  202. agf->agf_flfirst = 0;
  203. agf->agf_fllast = cpu_to_be32(XFS_AGFL_SIZE(mp) - 1);
  204. agf->agf_flcount = 0;
  205. tmpsize = agsize - XFS_PREALLOC_BLOCKS(mp);
  206. agf->agf_freeblks = cpu_to_be32(tmpsize);
  207. agf->agf_longest = cpu_to_be32(tmpsize);
  208. error = xfs_bwrite(mp, bp);
  209. if (error) {
  210. goto error0;
  211. }
  212. /*
  213. * AG inode header block
  214. */
  215. bp = xfs_buf_get(mp->m_ddev_targp,
  216. XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
  217. XFS_FSS_TO_BB(mp, 1), 0);
  218. agi = XFS_BUF_TO_AGI(bp);
  219. memset(agi, 0, mp->m_sb.sb_sectsize);
  220. agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
  221. agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
  222. agi->agi_seqno = cpu_to_be32(agno);
  223. agi->agi_length = cpu_to_be32(agsize);
  224. agi->agi_count = 0;
  225. agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
  226. agi->agi_level = cpu_to_be32(1);
  227. agi->agi_freecount = 0;
  228. agi->agi_newino = cpu_to_be32(NULLAGINO);
  229. agi->agi_dirino = cpu_to_be32(NULLAGINO);
  230. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
  231. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  232. error = xfs_bwrite(mp, bp);
  233. if (error) {
  234. goto error0;
  235. }
  236. /*
  237. * BNO btree root block
  238. */
  239. bp = xfs_buf_get(mp->m_ddev_targp,
  240. XFS_AGB_TO_DADDR(mp, agno, XFS_BNO_BLOCK(mp)),
  241. BTOBB(mp->m_sb.sb_blocksize), 0);
  242. block = XFS_BUF_TO_SBLOCK(bp);
  243. memset(block, 0, mp->m_sb.sb_blocksize);
  244. block->bb_magic = cpu_to_be32(XFS_ABTB_MAGIC);
  245. block->bb_level = 0;
  246. block->bb_numrecs = cpu_to_be16(1);
  247. block->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  248. block->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  249. arec = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_alloc,
  250. block, 1, mp->m_alloc_mxr[0]);
  251. arec->ar_startblock = cpu_to_be32(XFS_PREALLOC_BLOCKS(mp));
  252. arec->ar_blockcount = cpu_to_be32(
  253. agsize - be32_to_cpu(arec->ar_startblock));
  254. error = xfs_bwrite(mp, bp);
  255. if (error) {
  256. goto error0;
  257. }
  258. /*
  259. * CNT btree root block
  260. */
  261. bp = xfs_buf_get(mp->m_ddev_targp,
  262. XFS_AGB_TO_DADDR(mp, agno, XFS_CNT_BLOCK(mp)),
  263. BTOBB(mp->m_sb.sb_blocksize), 0);
  264. block = XFS_BUF_TO_SBLOCK(bp);
  265. memset(block, 0, mp->m_sb.sb_blocksize);
  266. block->bb_magic = cpu_to_be32(XFS_ABTC_MAGIC);
  267. block->bb_level = 0;
  268. block->bb_numrecs = cpu_to_be16(1);
  269. block->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  270. block->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  271. arec = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_alloc,
  272. block, 1, mp->m_alloc_mxr[0]);
  273. arec->ar_startblock = cpu_to_be32(XFS_PREALLOC_BLOCKS(mp));
  274. arec->ar_blockcount = cpu_to_be32(
  275. agsize - be32_to_cpu(arec->ar_startblock));
  276. nfree += be32_to_cpu(arec->ar_blockcount);
  277. error = xfs_bwrite(mp, bp);
  278. if (error) {
  279. goto error0;
  280. }
  281. /*
  282. * INO btree root block
  283. */
  284. bp = xfs_buf_get(mp->m_ddev_targp,
  285. XFS_AGB_TO_DADDR(mp, agno, XFS_IBT_BLOCK(mp)),
  286. BTOBB(mp->m_sb.sb_blocksize), 0);
  287. block = XFS_BUF_TO_SBLOCK(bp);
  288. memset(block, 0, mp->m_sb.sb_blocksize);
  289. block->bb_magic = cpu_to_be32(XFS_IBT_MAGIC);
  290. block->bb_level = 0;
  291. block->bb_numrecs = 0;
  292. block->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  293. block->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  294. error = xfs_bwrite(mp, bp);
  295. if (error) {
  296. goto error0;
  297. }
  298. }
  299. xfs_trans_agblocks_delta(tp, nfree);
  300. /*
  301. * There are new blocks in the old last a.g.
  302. */
  303. if (new) {
  304. /*
  305. * Change the agi length.
  306. */
  307. error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
  308. if (error) {
  309. goto error0;
  310. }
  311. ASSERT(bp);
  312. agi = XFS_BUF_TO_AGI(bp);
  313. be32_add(&agi->agi_length, new);
  314. ASSERT(nagcount == oagcount ||
  315. be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks);
  316. xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
  317. /*
  318. * Change agf length.
  319. */
  320. error = xfs_alloc_read_agf(mp, tp, agno, 0, &bp);
  321. if (error) {
  322. goto error0;
  323. }
  324. ASSERT(bp);
  325. agf = XFS_BUF_TO_AGF(bp);
  326. be32_add(&agf->agf_length, new);
  327. ASSERT(be32_to_cpu(agf->agf_length) ==
  328. be32_to_cpu(agi->agi_length));
  329. /*
  330. * Free the new space.
  331. */
  332. error = xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, agno,
  333. be32_to_cpu(agf->agf_length) - new), new);
  334. if (error) {
  335. goto error0;
  336. }
  337. }
  338. if (nagcount > oagcount)
  339. xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
  340. if (nb > mp->m_sb.sb_dblocks)
  341. xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
  342. nb - mp->m_sb.sb_dblocks);
  343. if (nfree)
  344. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, nfree);
  345. if (dpct)
  346. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
  347. error = xfs_trans_commit(tp, 0, NULL);
  348. if (error) {
  349. return error;
  350. }
  351. /* New allocation groups fully initialized, so update mount struct */
  352. if (nagimax)
  353. mp->m_maxagi = nagimax;
  354. if (mp->m_sb.sb_imax_pct) {
  355. __uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
  356. do_div(icount, 100);
  357. mp->m_maxicount = icount << mp->m_sb.sb_inopblog;
  358. } else
  359. mp->m_maxicount = 0;
  360. for (agno = 1; agno < nagcount; agno++) {
  361. error = xfs_read_buf(mp, mp->m_ddev_targp,
  362. XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
  363. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  364. if (error) {
  365. xfs_fs_cmn_err(CE_WARN, mp,
  366. "error %d reading secondary superblock for ag %d",
  367. error, agno);
  368. break;
  369. }
  370. sbp = XFS_BUF_TO_SBP(bp);
  371. xfs_xlatesb(sbp, &mp->m_sb, -1, XFS_SB_ALL_BITS);
  372. /*
  373. * If we get an error writing out the alternate superblocks,
  374. * just issue a warning and continue. The real work is
  375. * already done and committed.
  376. */
  377. if (!(error = xfs_bwrite(mp, bp))) {
  378. continue;
  379. } else {
  380. xfs_fs_cmn_err(CE_WARN, mp,
  381. "write error %d updating secondary superblock for ag %d",
  382. error, agno);
  383. break; /* no point in continuing */
  384. }
  385. }
  386. return 0;
  387. error0:
  388. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  389. return error;
  390. }
  391. static int
  392. xfs_growfs_log_private(
  393. xfs_mount_t *mp, /* mount point for filesystem */
  394. xfs_growfs_log_t *in) /* growfs log input struct */
  395. {
  396. xfs_extlen_t nb;
  397. nb = in->newblocks;
  398. if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
  399. return XFS_ERROR(EINVAL);
  400. if (nb == mp->m_sb.sb_logblocks &&
  401. in->isint == (mp->m_sb.sb_logstart != 0))
  402. return XFS_ERROR(EINVAL);
  403. /*
  404. * Moving the log is hard, need new interfaces to sync
  405. * the log first, hold off all activity while moving it.
  406. * Can have shorter or longer log in the same space,
  407. * or transform internal to external log or vice versa.
  408. */
  409. return XFS_ERROR(ENOSYS);
  410. }
  411. /*
  412. * protected versions of growfs function acquire and release locks on the mount
  413. * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
  414. * XFS_IOC_FSGROWFSRT
  415. */
  416. int
  417. xfs_growfs_data(
  418. xfs_mount_t *mp,
  419. xfs_growfs_data_t *in)
  420. {
  421. int error;
  422. if (!cpsema(&mp->m_growlock))
  423. return XFS_ERROR(EWOULDBLOCK);
  424. error = xfs_growfs_data_private(mp, in);
  425. vsema(&mp->m_growlock);
  426. return error;
  427. }
  428. int
  429. xfs_growfs_log(
  430. xfs_mount_t *mp,
  431. xfs_growfs_log_t *in)
  432. {
  433. int error;
  434. if (!cpsema(&mp->m_growlock))
  435. return XFS_ERROR(EWOULDBLOCK);
  436. error = xfs_growfs_log_private(mp, in);
  437. vsema(&mp->m_growlock);
  438. return error;
  439. }
  440. /*
  441. * exported through ioctl XFS_IOC_FSCOUNTS
  442. */
  443. int
  444. xfs_fs_counts(
  445. xfs_mount_t *mp,
  446. xfs_fsop_counts_t *cnt)
  447. {
  448. unsigned long s;
  449. xfs_icsb_sync_counters_lazy(mp);
  450. s = XFS_SB_LOCK(mp);
  451. cnt->freedata = mp->m_sb.sb_fdblocks;
  452. cnt->freertx = mp->m_sb.sb_frextents;
  453. cnt->freeino = mp->m_sb.sb_ifree;
  454. cnt->allocino = mp->m_sb.sb_icount;
  455. XFS_SB_UNLOCK(mp, s);
  456. return 0;
  457. }
  458. /*
  459. * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
  460. *
  461. * xfs_reserve_blocks is called to set m_resblks
  462. * in the in-core mount table. The number of unused reserved blocks
  463. * is kept in m_resblks_avail.
  464. *
  465. * Reserve the requested number of blocks if available. Otherwise return
  466. * as many as possible to satisfy the request. The actual number
  467. * reserved are returned in outval
  468. *
  469. * A null inval pointer indicates that only the current reserved blocks
  470. * available should be returned no settings are changed.
  471. */
  472. int
  473. xfs_reserve_blocks(
  474. xfs_mount_t *mp,
  475. __uint64_t *inval,
  476. xfs_fsop_resblks_t *outval)
  477. {
  478. __int64_t lcounter, delta;
  479. __uint64_t request;
  480. unsigned long s;
  481. /* If inval is null, report current values and return */
  482. if (inval == (__uint64_t *)NULL) {
  483. outval->resblks = mp->m_resblks;
  484. outval->resblks_avail = mp->m_resblks_avail;
  485. return 0;
  486. }
  487. request = *inval;
  488. s = XFS_SB_LOCK(mp);
  489. /*
  490. * If our previous reservation was larger than the current value,
  491. * then move any unused blocks back to the free pool.
  492. */
  493. if (mp->m_resblks > request) {
  494. lcounter = mp->m_resblks_avail - request;
  495. if (lcounter > 0) { /* release unused blocks */
  496. mp->m_sb.sb_fdblocks += lcounter;
  497. mp->m_resblks_avail -= lcounter;
  498. }
  499. mp->m_resblks = request;
  500. } else {
  501. delta = request - mp->m_resblks;
  502. lcounter = mp->m_sb.sb_fdblocks - delta;
  503. if (lcounter < 0) {
  504. /* We can't satisfy the request, just get what we can */
  505. mp->m_resblks += mp->m_sb.sb_fdblocks;
  506. mp->m_resblks_avail += mp->m_sb.sb_fdblocks;
  507. mp->m_sb.sb_fdblocks = 0;
  508. } else {
  509. mp->m_sb.sb_fdblocks = lcounter;
  510. mp->m_resblks = request;
  511. mp->m_resblks_avail += delta;
  512. }
  513. }
  514. outval->resblks = mp->m_resblks;
  515. outval->resblks_avail = mp->m_resblks_avail;
  516. XFS_SB_UNLOCK(mp, s);
  517. return 0;
  518. }
  519. void
  520. xfs_fs_log_dummy(xfs_mount_t *mp)
  521. {
  522. xfs_trans_t *tp;
  523. xfs_inode_t *ip;
  524. tp = _xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
  525. atomic_inc(&mp->m_active_trans);
  526. if (xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0)) {
  527. xfs_trans_cancel(tp, 0);
  528. return;
  529. }
  530. ip = mp->m_rootip;
  531. xfs_ilock(ip, XFS_ILOCK_EXCL);
  532. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  533. xfs_trans_ihold(tp, ip);
  534. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  535. xfs_trans_set_sync(tp);
  536. xfs_trans_commit(tp, 0, NULL);
  537. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  538. }
  539. int
  540. xfs_fs_goingdown(
  541. xfs_mount_t *mp,
  542. __uint32_t inflags)
  543. {
  544. switch (inflags) {
  545. case XFS_FSOP_GOING_FLAGS_DEFAULT: {
  546. struct vfs *vfsp = XFS_MTOVFS(mp);
  547. struct super_block *sb = freeze_bdev(vfsp->vfs_super->s_bdev);
  548. if (sb && !IS_ERR(sb)) {
  549. xfs_force_shutdown(mp, XFS_FORCE_UMOUNT);
  550. thaw_bdev(sb->s_bdev, sb);
  551. }
  552. break;
  553. }
  554. case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
  555. xfs_force_shutdown(mp, XFS_FORCE_UMOUNT);
  556. break;
  557. case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
  558. xfs_force_shutdown(mp, XFS_FORCE_UMOUNT|XFS_LOG_IO_ERROR);
  559. break;
  560. default:
  561. return XFS_ERROR(EINVAL);
  562. }
  563. return 0;
  564. }