xfs_fsops.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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(xfs_alloc, block, 1);
  248. arec->ar_startblock = cpu_to_be32(XFS_PREALLOC_BLOCKS(mp));
  249. arec->ar_blockcount = cpu_to_be32(
  250. agsize - be32_to_cpu(arec->ar_startblock));
  251. error = xfs_bwrite(mp, bp);
  252. if (error) {
  253. goto error0;
  254. }
  255. /*
  256. * CNT btree root block
  257. */
  258. bp = xfs_buf_get(mp->m_ddev_targp,
  259. XFS_AGB_TO_DADDR(mp, agno, XFS_CNT_BLOCK(mp)),
  260. BTOBB(mp->m_sb.sb_blocksize), 0);
  261. block = XFS_BUF_TO_SBLOCK(bp);
  262. memset(block, 0, mp->m_sb.sb_blocksize);
  263. block->bb_magic = cpu_to_be32(XFS_ABTC_MAGIC);
  264. block->bb_level = 0;
  265. block->bb_numrecs = cpu_to_be16(1);
  266. block->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  267. block->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  268. arec = XFS_BTREE_REC_ADDR(xfs_alloc, block, 1);
  269. arec->ar_startblock = cpu_to_be32(XFS_PREALLOC_BLOCKS(mp));
  270. arec->ar_blockcount = cpu_to_be32(
  271. agsize - be32_to_cpu(arec->ar_startblock));
  272. nfree += be32_to_cpu(arec->ar_blockcount);
  273. error = xfs_bwrite(mp, bp);
  274. if (error) {
  275. goto error0;
  276. }
  277. /*
  278. * INO btree root block
  279. */
  280. bp = xfs_buf_get(mp->m_ddev_targp,
  281. XFS_AGB_TO_DADDR(mp, agno, XFS_IBT_BLOCK(mp)),
  282. BTOBB(mp->m_sb.sb_blocksize), 0);
  283. block = XFS_BUF_TO_SBLOCK(bp);
  284. memset(block, 0, mp->m_sb.sb_blocksize);
  285. block->bb_magic = cpu_to_be32(XFS_IBT_MAGIC);
  286. block->bb_level = 0;
  287. block->bb_numrecs = 0;
  288. block->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  289. block->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  290. error = xfs_bwrite(mp, bp);
  291. if (error) {
  292. goto error0;
  293. }
  294. }
  295. xfs_trans_agblocks_delta(tp, nfree);
  296. /*
  297. * There are new blocks in the old last a.g.
  298. */
  299. if (new) {
  300. /*
  301. * Change the agi length.
  302. */
  303. error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
  304. if (error) {
  305. goto error0;
  306. }
  307. ASSERT(bp);
  308. agi = XFS_BUF_TO_AGI(bp);
  309. be32_add(&agi->agi_length, new);
  310. ASSERT(nagcount == oagcount ||
  311. be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks);
  312. xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
  313. /*
  314. * Change agf length.
  315. */
  316. error = xfs_alloc_read_agf(mp, tp, agno, 0, &bp);
  317. if (error) {
  318. goto error0;
  319. }
  320. ASSERT(bp);
  321. agf = XFS_BUF_TO_AGF(bp);
  322. be32_add(&agf->agf_length, new);
  323. ASSERT(be32_to_cpu(agf->agf_length) ==
  324. be32_to_cpu(agi->agi_length));
  325. /*
  326. * Free the new space.
  327. */
  328. error = xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, agno,
  329. be32_to_cpu(agf->agf_length) - new), new);
  330. if (error) {
  331. goto error0;
  332. }
  333. }
  334. if (nagcount > oagcount)
  335. xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
  336. if (nb > mp->m_sb.sb_dblocks)
  337. xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
  338. nb - mp->m_sb.sb_dblocks);
  339. if (nfree)
  340. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, nfree);
  341. if (dpct)
  342. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
  343. error = xfs_trans_commit(tp, 0, NULL);
  344. if (error) {
  345. return error;
  346. }
  347. /* New allocation groups fully initialized, so update mount struct */
  348. if (nagimax)
  349. mp->m_maxagi = nagimax;
  350. if (mp->m_sb.sb_imax_pct) {
  351. __uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
  352. do_div(icount, 100);
  353. mp->m_maxicount = icount << mp->m_sb.sb_inopblog;
  354. } else
  355. mp->m_maxicount = 0;
  356. for (agno = 1; agno < nagcount; agno++) {
  357. error = xfs_read_buf(mp, mp->m_ddev_targp,
  358. XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
  359. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  360. if (error) {
  361. xfs_fs_cmn_err(CE_WARN, mp,
  362. "error %d reading secondary superblock for ag %d",
  363. error, agno);
  364. break;
  365. }
  366. sbp = XFS_BUF_TO_SBP(bp);
  367. xfs_xlatesb(sbp, &mp->m_sb, -1, XFS_SB_ALL_BITS);
  368. /*
  369. * If we get an error writing out the alternate superblocks,
  370. * just issue a warning and continue. The real work is
  371. * already done and committed.
  372. */
  373. if (!(error = xfs_bwrite(mp, bp))) {
  374. continue;
  375. } else {
  376. xfs_fs_cmn_err(CE_WARN, mp,
  377. "write error %d updating secondary superblock for ag %d",
  378. error, agno);
  379. break; /* no point in continuing */
  380. }
  381. }
  382. return 0;
  383. error0:
  384. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  385. return error;
  386. }
  387. static int
  388. xfs_growfs_log_private(
  389. xfs_mount_t *mp, /* mount point for filesystem */
  390. xfs_growfs_log_t *in) /* growfs log input struct */
  391. {
  392. xfs_extlen_t nb;
  393. nb = in->newblocks;
  394. if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
  395. return XFS_ERROR(EINVAL);
  396. if (nb == mp->m_sb.sb_logblocks &&
  397. in->isint == (mp->m_sb.sb_logstart != 0))
  398. return XFS_ERROR(EINVAL);
  399. /*
  400. * Moving the log is hard, need new interfaces to sync
  401. * the log first, hold off all activity while moving it.
  402. * Can have shorter or longer log in the same space,
  403. * or transform internal to external log or vice versa.
  404. */
  405. return XFS_ERROR(ENOSYS);
  406. }
  407. /*
  408. * protected versions of growfs function acquire and release locks on the mount
  409. * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
  410. * XFS_IOC_FSGROWFSRT
  411. */
  412. int
  413. xfs_growfs_data(
  414. xfs_mount_t *mp,
  415. xfs_growfs_data_t *in)
  416. {
  417. int error;
  418. if (!cpsema(&mp->m_growlock))
  419. return XFS_ERROR(EWOULDBLOCK);
  420. error = xfs_growfs_data_private(mp, in);
  421. vsema(&mp->m_growlock);
  422. return error;
  423. }
  424. int
  425. xfs_growfs_log(
  426. xfs_mount_t *mp,
  427. xfs_growfs_log_t *in)
  428. {
  429. int error;
  430. if (!cpsema(&mp->m_growlock))
  431. return XFS_ERROR(EWOULDBLOCK);
  432. error = xfs_growfs_log_private(mp, in);
  433. vsema(&mp->m_growlock);
  434. return error;
  435. }
  436. /*
  437. * exported through ioctl XFS_IOC_FSCOUNTS
  438. */
  439. int
  440. xfs_fs_counts(
  441. xfs_mount_t *mp,
  442. xfs_fsop_counts_t *cnt)
  443. {
  444. unsigned long s;
  445. xfs_icsb_sync_counters_flags(mp, XFS_ICSB_LAZY_COUNT);
  446. s = XFS_SB_LOCK(mp);
  447. cnt->freedata = mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  448. cnt->freertx = mp->m_sb.sb_frextents;
  449. cnt->freeino = mp->m_sb.sb_ifree;
  450. cnt->allocino = mp->m_sb.sb_icount;
  451. XFS_SB_UNLOCK(mp, s);
  452. return 0;
  453. }
  454. /*
  455. * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
  456. *
  457. * xfs_reserve_blocks is called to set m_resblks
  458. * in the in-core mount table. The number of unused reserved blocks
  459. * is kept in m_resblks_avail.
  460. *
  461. * Reserve the requested number of blocks if available. Otherwise return
  462. * as many as possible to satisfy the request. The actual number
  463. * reserved are returned in outval
  464. *
  465. * A null inval pointer indicates that only the current reserved blocks
  466. * available should be returned no settings are changed.
  467. */
  468. int
  469. xfs_reserve_blocks(
  470. xfs_mount_t *mp,
  471. __uint64_t *inval,
  472. xfs_fsop_resblks_t *outval)
  473. {
  474. __int64_t lcounter, delta, fdblks_delta;
  475. __uint64_t request;
  476. unsigned long s;
  477. /* If inval is null, report current values and return */
  478. if (inval == (__uint64_t *)NULL) {
  479. outval->resblks = mp->m_resblks;
  480. outval->resblks_avail = mp->m_resblks_avail;
  481. return 0;
  482. }
  483. request = *inval;
  484. /*
  485. * With per-cpu counters, this becomes an interesting
  486. * problem. we needto work out if we are freeing or allocation
  487. * blocks first, then we can do the modification as necessary.
  488. *
  489. * We do this under the XFS_SB_LOCK so that if we are near
  490. * ENOSPC, we will hold out any changes while we work out
  491. * what to do. This means that the amount of free space can
  492. * change while we do this, so we need to retry if we end up
  493. * trying to reserve more space than is available.
  494. *
  495. * We also use the xfs_mod_incore_sb() interface so that we
  496. * don't have to care about whether per cpu counter are
  497. * enabled, disabled or even compiled in....
  498. */
  499. retry:
  500. s = XFS_SB_LOCK(mp);
  501. xfs_icsb_sync_counters_flags(mp, XFS_ICSB_SB_LOCKED);
  502. /*
  503. * If our previous reservation was larger than the current value,
  504. * then move any unused blocks back to the free pool.
  505. */
  506. fdblks_delta = 0;
  507. if (mp->m_resblks > request) {
  508. lcounter = mp->m_resblks_avail - request;
  509. if (lcounter > 0) { /* release unused blocks */
  510. fdblks_delta = lcounter;
  511. mp->m_resblks_avail -= lcounter;
  512. }
  513. mp->m_resblks = request;
  514. } else {
  515. __int64_t free;
  516. free = mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  517. if (!free)
  518. goto out; /* ENOSPC and fdblks_delta = 0 */
  519. delta = request - mp->m_resblks;
  520. lcounter = free - delta;
  521. if (lcounter < 0) {
  522. /* We can't satisfy the request, just get what we can */
  523. mp->m_resblks += free;
  524. mp->m_resblks_avail += free;
  525. fdblks_delta = -free;
  526. mp->m_sb.sb_fdblocks = XFS_ALLOC_SET_ASIDE(mp);
  527. } else {
  528. fdblks_delta = -delta;
  529. mp->m_sb.sb_fdblocks =
  530. lcounter + XFS_ALLOC_SET_ASIDE(mp);
  531. mp->m_resblks = request;
  532. mp->m_resblks_avail += delta;
  533. }
  534. }
  535. out:
  536. outval->resblks = mp->m_resblks;
  537. outval->resblks_avail = mp->m_resblks_avail;
  538. XFS_SB_UNLOCK(mp, s);
  539. if (fdblks_delta) {
  540. /*
  541. * If we are putting blocks back here, m_resblks_avail is
  542. * already at it's max so this will put it in the free pool.
  543. *
  544. * If we need space, we'll either succeed in getting it
  545. * from the free block count or we'll get an enospc. If
  546. * we get a ENOSPC, it means things changed while we were
  547. * calculating fdblks_delta and so we should try again to
  548. * see if there is anything left to reserve.
  549. *
  550. * Don't set the reserved flag here - we don't want to reserve
  551. * the extra reserve blocks from the reserve.....
  552. */
  553. int error;
  554. error = xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, fdblks_delta, 0);
  555. if (error == ENOSPC)
  556. goto retry;
  557. }
  558. return 0;
  559. }
  560. void
  561. xfs_fs_log_dummy(
  562. xfs_mount_t *mp)
  563. {
  564. xfs_trans_t *tp;
  565. xfs_inode_t *ip;
  566. tp = _xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
  567. if (xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0)) {
  568. xfs_trans_cancel(tp, 0);
  569. return;
  570. }
  571. ip = mp->m_rootip;
  572. xfs_ilock(ip, XFS_ILOCK_EXCL);
  573. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  574. xfs_trans_ihold(tp, ip);
  575. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  576. xfs_trans_set_sync(tp);
  577. xfs_trans_commit(tp, 0, NULL);
  578. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  579. }
  580. int
  581. xfs_fs_goingdown(
  582. xfs_mount_t *mp,
  583. __uint32_t inflags)
  584. {
  585. switch (inflags) {
  586. case XFS_FSOP_GOING_FLAGS_DEFAULT: {
  587. struct bhv_vfs *vfsp = XFS_MTOVFS(mp);
  588. struct super_block *sb = freeze_bdev(vfsp->vfs_super->s_bdev);
  589. if (sb && !IS_ERR(sb)) {
  590. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  591. thaw_bdev(sb->s_bdev, sb);
  592. }
  593. break;
  594. }
  595. case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
  596. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  597. break;
  598. case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
  599. xfs_force_shutdown(mp,
  600. SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
  601. break;
  602. default:
  603. return XFS_ERROR(EINVAL);
  604. }
  605. return 0;
  606. }