xfs_fsops.c 18 KB

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