xfs_fsops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_sb.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_error.h"
  16. #include "xfs_alloc.h"
  17. #include "xfs_fsops.h"
  18. #include "xfs_trans_space.h"
  19. #include "xfs_log.h"
  20. #include "xfs_ag.h"
  21. #include "xfs_ag_resv.h"
  22. /*
  23. * growfs operations
  24. */
  25. static int
  26. xfs_growfs_data_private(
  27. xfs_mount_t *mp, /* mount point for filesystem */
  28. xfs_growfs_data_t *in) /* growfs data input struct */
  29. {
  30. xfs_buf_t *bp;
  31. int error;
  32. xfs_agnumber_t nagcount;
  33. xfs_agnumber_t nagimax = 0;
  34. xfs_rfsblock_t nb, nb_mod;
  35. xfs_rfsblock_t new;
  36. xfs_agnumber_t oagcount;
  37. xfs_trans_t *tp;
  38. struct aghdr_init_data id = {};
  39. nb = in->newblocks;
  40. if (nb < mp->m_sb.sb_dblocks)
  41. return -EINVAL;
  42. if ((error = xfs_sb_validate_fsb_count(&mp->m_sb, nb)))
  43. return error;
  44. error = xfs_buf_read_uncached(mp->m_ddev_targp,
  45. XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
  46. XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
  47. if (error)
  48. return error;
  49. xfs_buf_relse(bp);
  50. new = nb; /* use new as a temporary here */
  51. nb_mod = do_div(new, mp->m_sb.sb_agblocks);
  52. nagcount = new + (nb_mod != 0);
  53. if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
  54. nagcount--;
  55. nb = (xfs_rfsblock_t)nagcount * mp->m_sb.sb_agblocks;
  56. if (nb < mp->m_sb.sb_dblocks)
  57. return -EINVAL;
  58. }
  59. new = nb - mp->m_sb.sb_dblocks;
  60. oagcount = mp->m_sb.sb_agcount;
  61. /* allocate the new per-ag structures */
  62. if (nagcount > oagcount) {
  63. error = xfs_initialize_perag(mp, nagcount, &nagimax);
  64. if (error)
  65. return error;
  66. }
  67. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
  68. XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
  69. if (error)
  70. return error;
  71. /*
  72. * Write new AG headers to disk. Non-transactional, but need to be
  73. * written and completed prior to the growfs transaction being logged.
  74. * To do this, we use a delayed write buffer list and wait for
  75. * submission and IO completion of the list as a whole. This allows the
  76. * IO subsystem to merge all the AG headers in a single AG into a single
  77. * IO and hide most of the latency of the IO from us.
  78. *
  79. * This also means that if we get an error whilst building the buffer
  80. * list to write, we can cancel the entire list without having written
  81. * anything.
  82. */
  83. INIT_LIST_HEAD(&id.buffer_list);
  84. for (id.agno = nagcount - 1;
  85. id.agno >= oagcount;
  86. id.agno--, new -= id.agsize) {
  87. if (id.agno == nagcount - 1)
  88. id.agsize = nb -
  89. (id.agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
  90. else
  91. id.agsize = mp->m_sb.sb_agblocks;
  92. error = xfs_ag_init_headers(mp, &id);
  93. if (error) {
  94. xfs_buf_delwri_cancel(&id.buffer_list);
  95. goto out_trans_cancel;
  96. }
  97. }
  98. error = xfs_buf_delwri_submit(&id.buffer_list);
  99. if (error)
  100. goto out_trans_cancel;
  101. xfs_trans_agblocks_delta(tp, id.nfree);
  102. /* If there are new blocks in the old last AG, extend it. */
  103. if (new) {
  104. error = xfs_ag_extend_space(mp, tp, &id, new);
  105. if (error)
  106. goto out_trans_cancel;
  107. }
  108. /*
  109. * Update changed superblock fields transactionally. These are not
  110. * seen by the rest of the world until the transaction commit applies
  111. * them atomically to the superblock.
  112. */
  113. if (nagcount > oagcount)
  114. xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
  115. if (nb > mp->m_sb.sb_dblocks)
  116. xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
  117. nb - mp->m_sb.sb_dblocks);
  118. if (id.nfree)
  119. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, id.nfree);
  120. xfs_trans_set_sync(tp);
  121. error = xfs_trans_commit(tp);
  122. if (error)
  123. return error;
  124. /* New allocation groups fully initialized, so update mount struct */
  125. if (nagimax)
  126. mp->m_maxagi = nagimax;
  127. xfs_set_low_space_thresholds(mp);
  128. mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
  129. /*
  130. * If we expanded the last AG, free the per-AG reservation
  131. * so we can reinitialize it with the new size.
  132. */
  133. if (new) {
  134. struct xfs_perag *pag;
  135. pag = xfs_perag_get(mp, id.agno);
  136. error = xfs_ag_resv_free(pag);
  137. xfs_perag_put(pag);
  138. if (error)
  139. return error;
  140. }
  141. /*
  142. * Reserve AG metadata blocks. ENOSPC here does not mean there was a
  143. * growfs failure, just that there still isn't space for new user data
  144. * after the grow has been run.
  145. */
  146. error = xfs_fs_reserve_ag_blocks(mp);
  147. if (error == -ENOSPC)
  148. error = 0;
  149. return error;
  150. out_trans_cancel:
  151. xfs_trans_cancel(tp);
  152. return error;
  153. }
  154. static int
  155. xfs_growfs_log_private(
  156. xfs_mount_t *mp, /* mount point for filesystem */
  157. xfs_growfs_log_t *in) /* growfs log input struct */
  158. {
  159. xfs_extlen_t nb;
  160. nb = in->newblocks;
  161. if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
  162. return -EINVAL;
  163. if (nb == mp->m_sb.sb_logblocks &&
  164. in->isint == (mp->m_sb.sb_logstart != 0))
  165. return -EINVAL;
  166. /*
  167. * Moving the log is hard, need new interfaces to sync
  168. * the log first, hold off all activity while moving it.
  169. * Can have shorter or longer log in the same space,
  170. * or transform internal to external log or vice versa.
  171. */
  172. return -ENOSYS;
  173. }
  174. static int
  175. xfs_growfs_imaxpct(
  176. struct xfs_mount *mp,
  177. __u32 imaxpct)
  178. {
  179. struct xfs_trans *tp;
  180. int dpct;
  181. int error;
  182. if (imaxpct > 100)
  183. return -EINVAL;
  184. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
  185. XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
  186. if (error)
  187. return error;
  188. dpct = imaxpct - mp->m_sb.sb_imax_pct;
  189. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
  190. xfs_trans_set_sync(tp);
  191. return xfs_trans_commit(tp);
  192. }
  193. /*
  194. * protected versions of growfs function acquire and release locks on the mount
  195. * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
  196. * XFS_IOC_FSGROWFSRT
  197. */
  198. int
  199. xfs_growfs_data(
  200. struct xfs_mount *mp,
  201. struct xfs_growfs_data *in)
  202. {
  203. int error = 0;
  204. if (!capable(CAP_SYS_ADMIN))
  205. return -EPERM;
  206. if (!mutex_trylock(&mp->m_growlock))
  207. return -EWOULDBLOCK;
  208. /* update imaxpct separately to the physical grow of the filesystem */
  209. if (in->imaxpct != mp->m_sb.sb_imax_pct) {
  210. error = xfs_growfs_imaxpct(mp, in->imaxpct);
  211. if (error)
  212. goto out_error;
  213. }
  214. if (in->newblocks != mp->m_sb.sb_dblocks) {
  215. error = xfs_growfs_data_private(mp, in);
  216. if (error)
  217. goto out_error;
  218. }
  219. /* Post growfs calculations needed to reflect new state in operations */
  220. if (mp->m_sb.sb_imax_pct) {
  221. uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
  222. do_div(icount, 100);
  223. M_IGEO(mp)->maxicount = XFS_FSB_TO_INO(mp, icount);
  224. } else
  225. M_IGEO(mp)->maxicount = 0;
  226. /* Update secondary superblocks now the physical grow has completed */
  227. error = xfs_update_secondary_sbs(mp);
  228. out_error:
  229. /*
  230. * Increment the generation unconditionally, the error could be from
  231. * updating the secondary superblocks, in which case the new size
  232. * is live already.
  233. */
  234. mp->m_generation++;
  235. mutex_unlock(&mp->m_growlock);
  236. return error;
  237. }
  238. int
  239. xfs_growfs_log(
  240. xfs_mount_t *mp,
  241. xfs_growfs_log_t *in)
  242. {
  243. int error;
  244. if (!capable(CAP_SYS_ADMIN))
  245. return -EPERM;
  246. if (!mutex_trylock(&mp->m_growlock))
  247. return -EWOULDBLOCK;
  248. error = xfs_growfs_log_private(mp, in);
  249. mutex_unlock(&mp->m_growlock);
  250. return error;
  251. }
  252. /*
  253. * exported through ioctl XFS_IOC_FSCOUNTS
  254. */
  255. void
  256. xfs_fs_counts(
  257. xfs_mount_t *mp,
  258. xfs_fsop_counts_t *cnt)
  259. {
  260. cnt->allocino = percpu_counter_read_positive(&mp->m_icount);
  261. cnt->freeino = percpu_counter_read_positive(&mp->m_ifree);
  262. cnt->freedata = percpu_counter_read_positive(&mp->m_fdblocks) -
  263. mp->m_alloc_set_aside;
  264. spin_lock(&mp->m_sb_lock);
  265. cnt->freertx = mp->m_sb.sb_frextents;
  266. spin_unlock(&mp->m_sb_lock);
  267. }
  268. /*
  269. * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
  270. *
  271. * xfs_reserve_blocks is called to set m_resblks
  272. * in the in-core mount table. The number of unused reserved blocks
  273. * is kept in m_resblks_avail.
  274. *
  275. * Reserve the requested number of blocks if available. Otherwise return
  276. * as many as possible to satisfy the request. The actual number
  277. * reserved are returned in outval
  278. *
  279. * A null inval pointer indicates that only the current reserved blocks
  280. * available should be returned no settings are changed.
  281. */
  282. int
  283. xfs_reserve_blocks(
  284. xfs_mount_t *mp,
  285. uint64_t *inval,
  286. xfs_fsop_resblks_t *outval)
  287. {
  288. int64_t lcounter, delta;
  289. int64_t fdblks_delta = 0;
  290. uint64_t request;
  291. int64_t free;
  292. int error = 0;
  293. /* If inval is null, report current values and return */
  294. if (inval == (uint64_t *)NULL) {
  295. if (!outval)
  296. return -EINVAL;
  297. outval->resblks = mp->m_resblks;
  298. outval->resblks_avail = mp->m_resblks_avail;
  299. return 0;
  300. }
  301. request = *inval;
  302. /*
  303. * With per-cpu counters, this becomes an interesting problem. we need
  304. * to work out if we are freeing or allocation blocks first, then we can
  305. * do the modification as necessary.
  306. *
  307. * We do this under the m_sb_lock so that if we are near ENOSPC, we will
  308. * hold out any changes while we work out what to do. This means that
  309. * the amount of free space can change while we do this, so we need to
  310. * retry if we end up trying to reserve more space than is available.
  311. */
  312. spin_lock(&mp->m_sb_lock);
  313. /*
  314. * If our previous reservation was larger than the current value,
  315. * then move any unused blocks back to the free pool. Modify the resblks
  316. * counters directly since we shouldn't have any problems unreserving
  317. * space.
  318. */
  319. if (mp->m_resblks > request) {
  320. lcounter = mp->m_resblks_avail - request;
  321. if (lcounter > 0) { /* release unused blocks */
  322. fdblks_delta = lcounter;
  323. mp->m_resblks_avail -= lcounter;
  324. }
  325. mp->m_resblks = request;
  326. if (fdblks_delta) {
  327. spin_unlock(&mp->m_sb_lock);
  328. error = xfs_mod_fdblocks(mp, fdblks_delta, 0);
  329. spin_lock(&mp->m_sb_lock);
  330. }
  331. goto out;
  332. }
  333. /*
  334. * If the request is larger than the current reservation, reserve the
  335. * blocks before we update the reserve counters. Sample m_fdblocks and
  336. * perform a partial reservation if the request exceeds free space.
  337. */
  338. error = -ENOSPC;
  339. do {
  340. free = percpu_counter_sum(&mp->m_fdblocks) -
  341. mp->m_alloc_set_aside;
  342. if (free <= 0)
  343. break;
  344. delta = request - mp->m_resblks;
  345. lcounter = free - delta;
  346. if (lcounter < 0)
  347. /* We can't satisfy the request, just get what we can */
  348. fdblks_delta = free;
  349. else
  350. fdblks_delta = delta;
  351. /*
  352. * We'll either succeed in getting space from the free block
  353. * count or we'll get an ENOSPC. If we get a ENOSPC, it means
  354. * things changed while we were calculating fdblks_delta and so
  355. * we should try again to see if there is anything left to
  356. * reserve.
  357. *
  358. * Don't set the reserved flag here - we don't want to reserve
  359. * the extra reserve blocks from the reserve.....
  360. */
  361. spin_unlock(&mp->m_sb_lock);
  362. error = xfs_mod_fdblocks(mp, -fdblks_delta, 0);
  363. spin_lock(&mp->m_sb_lock);
  364. } while (error == -ENOSPC);
  365. /*
  366. * Update the reserve counters if blocks have been successfully
  367. * allocated.
  368. */
  369. if (!error && fdblks_delta) {
  370. mp->m_resblks += fdblks_delta;
  371. mp->m_resblks_avail += fdblks_delta;
  372. }
  373. out:
  374. if (outval) {
  375. outval->resblks = mp->m_resblks;
  376. outval->resblks_avail = mp->m_resblks_avail;
  377. }
  378. spin_unlock(&mp->m_sb_lock);
  379. return error;
  380. }
  381. int
  382. xfs_fs_goingdown(
  383. xfs_mount_t *mp,
  384. uint32_t inflags)
  385. {
  386. switch (inflags) {
  387. case XFS_FSOP_GOING_FLAGS_DEFAULT: {
  388. if (!freeze_bdev(mp->m_super->s_bdev)) {
  389. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  390. thaw_bdev(mp->m_super->s_bdev);
  391. }
  392. break;
  393. }
  394. case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
  395. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  396. break;
  397. case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
  398. xfs_force_shutdown(mp,
  399. SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
  400. break;
  401. default:
  402. return -EINVAL;
  403. }
  404. return 0;
  405. }
  406. /*
  407. * Force a shutdown of the filesystem instantly while keeping the filesystem
  408. * consistent. We don't do an unmount here; just shutdown the shop, make sure
  409. * that absolutely nothing persistent happens to this filesystem after this
  410. * point.
  411. */
  412. void
  413. xfs_do_force_shutdown(
  414. struct xfs_mount *mp,
  415. int flags,
  416. char *fname,
  417. int lnnum)
  418. {
  419. bool logerror = flags & SHUTDOWN_LOG_IO_ERROR;
  420. /*
  421. * No need to duplicate efforts.
  422. */
  423. if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
  424. return;
  425. /*
  426. * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
  427. * queue up anybody new on the log reservations, and wakes up
  428. * everybody who's sleeping on log reservations to tell them
  429. * the bad news.
  430. */
  431. if (xfs_log_force_umount(mp, logerror))
  432. return;
  433. if (flags & SHUTDOWN_FORCE_UMOUNT) {
  434. xfs_alert(mp,
  435. "User initiated shutdown received. Shutting down filesystem");
  436. return;
  437. }
  438. xfs_notice(mp,
  439. "%s(0x%x) called from line %d of file %s. Return address = "PTR_FMT,
  440. __func__, flags, lnnum, fname, __return_address);
  441. if (flags & SHUTDOWN_CORRUPT_INCORE) {
  442. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_CORRUPT,
  443. "Corruption of in-memory data detected. Shutting down filesystem");
  444. if (XFS_ERRLEVEL_HIGH <= xfs_error_level)
  445. xfs_stack_trace();
  446. } else if (logerror) {
  447. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_LOGERROR,
  448. "Log I/O Error Detected. Shutting down filesystem");
  449. } else {
  450. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
  451. "I/O Error Detected. Shutting down filesystem");
  452. }
  453. xfs_alert(mp,
  454. "Please unmount the filesystem and rectify the problem(s)");
  455. }
  456. /*
  457. * Reserve free space for per-AG metadata.
  458. */
  459. int
  460. xfs_fs_reserve_ag_blocks(
  461. struct xfs_mount *mp)
  462. {
  463. xfs_agnumber_t agno;
  464. struct xfs_perag *pag;
  465. int error = 0;
  466. int err2;
  467. mp->m_finobt_nores = false;
  468. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  469. pag = xfs_perag_get(mp, agno);
  470. err2 = xfs_ag_resv_init(pag, NULL);
  471. xfs_perag_put(pag);
  472. if (err2 && !error)
  473. error = err2;
  474. }
  475. if (error && error != -ENOSPC) {
  476. xfs_warn(mp,
  477. "Error %d reserving per-AG metadata reserve pool.", error);
  478. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  479. }
  480. return error;
  481. }
  482. /*
  483. * Free space reserved for per-AG metadata.
  484. */
  485. int
  486. xfs_fs_unreserve_ag_blocks(
  487. struct xfs_mount *mp)
  488. {
  489. xfs_agnumber_t agno;
  490. struct xfs_perag *pag;
  491. int error = 0;
  492. int err2;
  493. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  494. pag = xfs_perag_get(mp, agno);
  495. err2 = xfs_ag_resv_free(pag);
  496. xfs_perag_put(pag);
  497. if (err2 && !error)
  498. error = err2;
  499. }
  500. if (error)
  501. xfs_warn(mp,
  502. "Error %d freeing per-AG metadata reserve pool.", error);
  503. return error;
  504. }