xfs_sync.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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_log.h"
  23. #include "xfs_inum.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_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_dinode.h"
  38. #include "xfs_error.h"
  39. #include "xfs_mru_cache.h"
  40. #include "xfs_filestream.h"
  41. #include "xfs_vnodeops.h"
  42. #include "xfs_utils.h"
  43. #include "xfs_buf_item.h"
  44. #include "xfs_inode_item.h"
  45. #include "xfs_rw.h"
  46. #include <linux/kthread.h>
  47. #include <linux/freezer.h>
  48. /*
  49. * xfs_sync flushes any pending I/O to file system vfsp.
  50. *
  51. * This routine is called by vfs_sync() to make sure that things make it
  52. * out to disk eventually, on sync() system calls to flush out everything,
  53. * and when the file system is unmounted. For the vfs_sync() case, all
  54. * we really need to do is sync out the log to make all of our meta-data
  55. * updates permanent (except for timestamps). For calls from pflushd(),
  56. * dirty pages are kept moving by calling pdflush() on the inodes
  57. * containing them. We also flush the inodes that we can lock without
  58. * sleeping and the superblock if we can lock it without sleeping from
  59. * vfs_sync() so that items at the tail of the log are always moving out.
  60. *
  61. * Flags:
  62. * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
  63. * to sleep if we can help it. All we really need
  64. * to do is ensure that the log is synced at least
  65. * periodically. We also push the inodes and
  66. * superblock if we can lock them without sleeping
  67. * and they are not pinned.
  68. * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
  69. * set, then we really want to lock each inode and flush
  70. * it.
  71. * SYNC_WAIT - All the flushes that take place in this call should
  72. * be synchronous.
  73. * SYNC_DELWRI - This tells us to push dirty pages associated with
  74. * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
  75. * determine if they should be flushed sync, async, or
  76. * delwri.
  77. * SYNC_CLOSE - This flag is passed when the system is being
  78. * unmounted. We should sync and invalidate everything.
  79. * SYNC_FSDATA - This indicates that the caller would like to make
  80. * sure the superblock is safe on disk. We can ensure
  81. * this by simply making sure the log gets flushed
  82. * if SYNC_BDFLUSH is set, and by actually writing it
  83. * out otherwise.
  84. * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
  85. * before we return (including direct I/O). Forms the drain
  86. * side of the write barrier needed to safely quiesce the
  87. * filesystem.
  88. *
  89. */
  90. int
  91. xfs_sync(
  92. xfs_mount_t *mp,
  93. int flags)
  94. {
  95. int error;
  96. /*
  97. * Get the Quota Manager to flush the dquots.
  98. *
  99. * If XFS quota support is not enabled or this filesystem
  100. * instance does not use quotas XFS_QM_DQSYNC will always
  101. * return zero.
  102. */
  103. error = XFS_QM_DQSYNC(mp, flags);
  104. if (error) {
  105. /*
  106. * If we got an IO error, we will be shutting down.
  107. * So, there's nothing more for us to do here.
  108. */
  109. ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
  110. if (XFS_FORCED_SHUTDOWN(mp))
  111. return XFS_ERROR(error);
  112. }
  113. if (flags & SYNC_IOWAIT)
  114. xfs_filestream_flush(mp);
  115. return xfs_syncsub(mp, flags, NULL);
  116. }
  117. /*
  118. * Sync all the inodes in the given AG according to the
  119. * direction given by the flags.
  120. */
  121. STATIC int
  122. xfs_sync_inodes_ag(
  123. xfs_mount_t *mp,
  124. int ag,
  125. int flags,
  126. int *bypassed)
  127. {
  128. xfs_perag_t *pag = &mp->m_perag[ag];
  129. int nr_found;
  130. int first_index = 0;
  131. int error = 0;
  132. int last_error = 0;
  133. int fflag = XFS_B_ASYNC;
  134. int lock_flags = XFS_ILOCK_SHARED;
  135. if (flags & SYNC_DELWRI)
  136. fflag = XFS_B_DELWRI;
  137. if (flags & SYNC_WAIT)
  138. fflag = 0; /* synchronous overrides all */
  139. if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
  140. /*
  141. * We need the I/O lock if we're going to call any of
  142. * the flush/inval routines.
  143. */
  144. lock_flags |= XFS_IOLOCK_SHARED;
  145. }
  146. do {
  147. struct inode *inode;
  148. boolean_t inode_refed;
  149. xfs_inode_t *ip = NULL;
  150. /*
  151. * use a gang lookup to find the next inode in the tree
  152. * as the tree is sparse and a gang lookup walks to find
  153. * the number of objects requested.
  154. */
  155. read_lock(&pag->pag_ici_lock);
  156. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
  157. (void**)&ip, first_index, 1);
  158. if (!nr_found) {
  159. read_unlock(&pag->pag_ici_lock);
  160. break;
  161. }
  162. /* update the index for the next lookup */
  163. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
  164. /*
  165. * skip inodes in reclaim. Let xfs_syncsub do that for
  166. * us so we don't need to worry.
  167. */
  168. if (xfs_iflags_test(ip, (XFS_IRECLAIM|XFS_IRECLAIMABLE))) {
  169. read_unlock(&pag->pag_ici_lock);
  170. continue;
  171. }
  172. /* bad inodes are dealt with elsewhere */
  173. inode = VFS_I(ip);
  174. if (is_bad_inode(inode)) {
  175. read_unlock(&pag->pag_ici_lock);
  176. continue;
  177. }
  178. /* nothing to sync during shutdown */
  179. if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
  180. read_unlock(&pag->pag_ici_lock);
  181. return 0;
  182. }
  183. /*
  184. * If we can't get a reference on the VFS_I, the inode must be
  185. * in reclaim. If we can get the inode lock without blocking,
  186. * it is safe to flush the inode because we hold the tree lock
  187. * and xfs_iextract will block right now. Hence if we lock the
  188. * inode while holding the tree lock, xfs_ireclaim() is
  189. * guaranteed to block on the inode lock we now hold and hence
  190. * it is safe to reference the inode until we drop the inode
  191. * locks completely.
  192. */
  193. inode_refed = B_FALSE;
  194. if (igrab(inode)) {
  195. read_unlock(&pag->pag_ici_lock);
  196. xfs_ilock(ip, lock_flags);
  197. inode_refed = B_TRUE;
  198. } else {
  199. if (!xfs_ilock_nowait(ip, lock_flags)) {
  200. /* leave it to reclaim */
  201. read_unlock(&pag->pag_ici_lock);
  202. continue;
  203. }
  204. read_unlock(&pag->pag_ici_lock);
  205. }
  206. /*
  207. * If we have to flush data or wait for I/O completion
  208. * we need to drop the ilock that we currently hold.
  209. * If we need to drop the lock, insert a marker if we
  210. * have not already done so.
  211. */
  212. if (flags & SYNC_CLOSE) {
  213. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  214. if (XFS_FORCED_SHUTDOWN(mp))
  215. xfs_tosspages(ip, 0, -1, FI_REMAPF);
  216. else
  217. error = xfs_flushinval_pages(ip, 0, -1,
  218. FI_REMAPF);
  219. /* wait for I/O on freeze */
  220. if (flags & SYNC_IOWAIT)
  221. vn_iowait(ip);
  222. xfs_ilock(ip, XFS_ILOCK_SHARED);
  223. }
  224. if ((flags & SYNC_DELWRI) && VN_DIRTY(inode)) {
  225. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  226. error = xfs_flush_pages(ip, 0, -1, fflag, FI_NONE);
  227. if (flags & SYNC_IOWAIT)
  228. vn_iowait(ip);
  229. xfs_ilock(ip, XFS_ILOCK_SHARED);
  230. }
  231. if ((flags & SYNC_ATTR) && !xfs_inode_clean(ip)) {
  232. if (flags & SYNC_WAIT) {
  233. xfs_iflock(ip);
  234. if (!xfs_inode_clean(ip))
  235. error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
  236. else
  237. xfs_ifunlock(ip);
  238. } else if (xfs_iflock_nowait(ip)) {
  239. if (!xfs_inode_clean(ip))
  240. error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
  241. else
  242. xfs_ifunlock(ip);
  243. } else if (bypassed) {
  244. (*bypassed)++;
  245. }
  246. }
  247. if (lock_flags)
  248. xfs_iunlock(ip, lock_flags);
  249. if (inode_refed) {
  250. IRELE(ip);
  251. }
  252. if (error)
  253. last_error = error;
  254. /*
  255. * bail out if the filesystem is corrupted.
  256. */
  257. if (error == EFSCORRUPTED)
  258. return XFS_ERROR(error);
  259. } while (nr_found);
  260. return last_error;
  261. }
  262. int
  263. xfs_sync_inodes(
  264. xfs_mount_t *mp,
  265. int flags,
  266. int *bypassed)
  267. {
  268. int error;
  269. int last_error;
  270. int i;
  271. if (bypassed)
  272. *bypassed = 0;
  273. if (mp->m_flags & XFS_MOUNT_RDONLY)
  274. return 0;
  275. error = 0;
  276. last_error = 0;
  277. for (i = 0; i < mp->m_sb.sb_agcount; i++) {
  278. if (!mp->m_perag[i].pag_ici_init)
  279. continue;
  280. error = xfs_sync_inodes_ag(mp, i, flags, bypassed);
  281. if (error)
  282. last_error = error;
  283. if (error == EFSCORRUPTED)
  284. break;
  285. }
  286. return XFS_ERROR(last_error);
  287. }
  288. STATIC int
  289. xfs_commit_dummy_trans(
  290. struct xfs_mount *mp,
  291. uint log_flags)
  292. {
  293. struct xfs_inode *ip = mp->m_rootip;
  294. struct xfs_trans *tp;
  295. int error;
  296. /*
  297. * Put a dummy transaction in the log to tell recovery
  298. * that all others are OK.
  299. */
  300. tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
  301. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  302. if (error) {
  303. xfs_trans_cancel(tp, 0);
  304. return error;
  305. }
  306. xfs_ilock(ip, XFS_ILOCK_EXCL);
  307. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  308. xfs_trans_ihold(tp, ip);
  309. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  310. /* XXX(hch): ignoring the error here.. */
  311. error = xfs_trans_commit(tp, 0);
  312. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  313. xfs_log_force(mp, 0, log_flags);
  314. return 0;
  315. }
  316. STATIC int
  317. xfs_sync_fsdata(
  318. struct xfs_mount *mp,
  319. int flags)
  320. {
  321. struct xfs_buf *bp;
  322. struct xfs_buf_log_item *bip;
  323. int error = 0;
  324. /*
  325. * If this is xfssyncd() then only sync the superblock if we can
  326. * lock it without sleeping and it is not pinned.
  327. */
  328. if (flags & SYNC_BDFLUSH) {
  329. ASSERT(!(flags & SYNC_WAIT));
  330. bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
  331. if (!bp)
  332. goto out;
  333. bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
  334. if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
  335. goto out_brelse;
  336. } else {
  337. bp = xfs_getsb(mp, 0);
  338. /*
  339. * If the buffer is pinned then push on the log so we won't
  340. * get stuck waiting in the write for someone, maybe
  341. * ourselves, to flush the log.
  342. *
  343. * Even though we just pushed the log above, we did not have
  344. * the superblock buffer locked at that point so it can
  345. * become pinned in between there and here.
  346. */
  347. if (XFS_BUF_ISPINNED(bp))
  348. xfs_log_force(mp, 0, XFS_LOG_FORCE);
  349. }
  350. if (flags & SYNC_WAIT)
  351. XFS_BUF_UNASYNC(bp);
  352. else
  353. XFS_BUF_ASYNC(bp);
  354. return xfs_bwrite(mp, bp);
  355. out_brelse:
  356. xfs_buf_relse(bp);
  357. out:
  358. return error;
  359. }
  360. /*
  361. * xfs sync routine for internal use
  362. *
  363. * This routine supports all of the flags defined for the generic vfs_sync
  364. * interface as explained above under xfs_sync.
  365. *
  366. */
  367. int
  368. xfs_syncsub(
  369. xfs_mount_t *mp,
  370. int flags,
  371. int *bypassed)
  372. {
  373. int error = 0;
  374. int last_error = 0;
  375. uint log_flags = XFS_LOG_FORCE;
  376. /*
  377. * Sync out the log. This ensures that the log is periodically
  378. * flushed even if there is not enough activity to fill it up.
  379. */
  380. if (flags & SYNC_WAIT)
  381. log_flags |= XFS_LOG_SYNC;
  382. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  383. if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
  384. if (flags & SYNC_BDFLUSH)
  385. xfs_finish_reclaim_all(mp, 1, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  386. else
  387. error = xfs_sync_inodes(mp, flags, bypassed);
  388. }
  389. /*
  390. * Flushing out dirty data above probably generated more
  391. * log activity, so if this isn't vfs_sync() then flush
  392. * the log again.
  393. */
  394. if (flags & SYNC_DELWRI)
  395. xfs_log_force(mp, 0, log_flags);
  396. if (flags & SYNC_FSDATA) {
  397. error = xfs_sync_fsdata(mp, flags);
  398. if (error)
  399. last_error = error;
  400. }
  401. /*
  402. * Now check to see if the log needs a "dummy" transaction.
  403. */
  404. if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
  405. error = xfs_commit_dummy_trans(mp, log_flags);
  406. if (error)
  407. return error;
  408. }
  409. /*
  410. * When shutting down, we need to insure that the AIL is pushed
  411. * to disk or the filesystem can appear corrupt from the PROM.
  412. */
  413. if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
  414. XFS_bflush(mp->m_ddev_targp);
  415. if (mp->m_rtdev_targp) {
  416. XFS_bflush(mp->m_rtdev_targp);
  417. }
  418. }
  419. return XFS_ERROR(last_error);
  420. }
  421. /*
  422. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  423. * Doing this has two advantages:
  424. * - It saves on stack space, which is tight in certain situations
  425. * - It can be used (with care) as a mechanism to avoid deadlocks.
  426. * Flushing while allocating in a full filesystem requires both.
  427. */
  428. STATIC void
  429. xfs_syncd_queue_work(
  430. struct xfs_mount *mp,
  431. void *data,
  432. void (*syncer)(struct xfs_mount *, void *))
  433. {
  434. struct bhv_vfs_sync_work *work;
  435. work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
  436. INIT_LIST_HEAD(&work->w_list);
  437. work->w_syncer = syncer;
  438. work->w_data = data;
  439. work->w_mount = mp;
  440. spin_lock(&mp->m_sync_lock);
  441. list_add_tail(&work->w_list, &mp->m_sync_list);
  442. spin_unlock(&mp->m_sync_lock);
  443. wake_up_process(mp->m_sync_task);
  444. }
  445. /*
  446. * Flush delayed allocate data, attempting to free up reserved space
  447. * from existing allocations. At this point a new allocation attempt
  448. * has failed with ENOSPC and we are in the process of scratching our
  449. * heads, looking about for more room...
  450. */
  451. STATIC void
  452. xfs_flush_inode_work(
  453. struct xfs_mount *mp,
  454. void *arg)
  455. {
  456. struct inode *inode = arg;
  457. filemap_flush(inode->i_mapping);
  458. iput(inode);
  459. }
  460. void
  461. xfs_flush_inode(
  462. xfs_inode_t *ip)
  463. {
  464. struct inode *inode = VFS_I(ip);
  465. igrab(inode);
  466. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inode_work);
  467. delay(msecs_to_jiffies(500));
  468. }
  469. /*
  470. * This is the "bigger hammer" version of xfs_flush_inode_work...
  471. * (IOW, "If at first you don't succeed, use a Bigger Hammer").
  472. */
  473. STATIC void
  474. xfs_flush_device_work(
  475. struct xfs_mount *mp,
  476. void *arg)
  477. {
  478. struct inode *inode = arg;
  479. sync_blockdev(mp->m_super->s_bdev);
  480. iput(inode);
  481. }
  482. void
  483. xfs_flush_device(
  484. xfs_inode_t *ip)
  485. {
  486. struct inode *inode = VFS_I(ip);
  487. igrab(inode);
  488. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_device_work);
  489. delay(msecs_to_jiffies(500));
  490. xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
  491. }
  492. STATIC void
  493. xfs_sync_worker(
  494. struct xfs_mount *mp,
  495. void *unused)
  496. {
  497. int error;
  498. if (!(mp->m_flags & XFS_MOUNT_RDONLY))
  499. error = xfs_sync(mp, SYNC_FSDATA | SYNC_BDFLUSH | SYNC_ATTR);
  500. mp->m_sync_seq++;
  501. wake_up(&mp->m_wait_single_sync_task);
  502. }
  503. STATIC int
  504. xfssyncd(
  505. void *arg)
  506. {
  507. struct xfs_mount *mp = arg;
  508. long timeleft;
  509. bhv_vfs_sync_work_t *work, *n;
  510. LIST_HEAD (tmp);
  511. set_freezable();
  512. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  513. for (;;) {
  514. timeleft = schedule_timeout_interruptible(timeleft);
  515. /* swsusp */
  516. try_to_freeze();
  517. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  518. break;
  519. spin_lock(&mp->m_sync_lock);
  520. /*
  521. * We can get woken by laptop mode, to do a sync -
  522. * that's the (only!) case where the list would be
  523. * empty with time remaining.
  524. */
  525. if (!timeleft || list_empty(&mp->m_sync_list)) {
  526. if (!timeleft)
  527. timeleft = xfs_syncd_centisecs *
  528. msecs_to_jiffies(10);
  529. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  530. list_add_tail(&mp->m_sync_work.w_list,
  531. &mp->m_sync_list);
  532. }
  533. list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
  534. list_move(&work->w_list, &tmp);
  535. spin_unlock(&mp->m_sync_lock);
  536. list_for_each_entry_safe(work, n, &tmp, w_list) {
  537. (*work->w_syncer)(mp, work->w_data);
  538. list_del(&work->w_list);
  539. if (work == &mp->m_sync_work)
  540. continue;
  541. kmem_free(work);
  542. }
  543. }
  544. return 0;
  545. }
  546. int
  547. xfs_syncd_init(
  548. struct xfs_mount *mp)
  549. {
  550. mp->m_sync_work.w_syncer = xfs_sync_worker;
  551. mp->m_sync_work.w_mount = mp;
  552. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
  553. if (IS_ERR(mp->m_sync_task))
  554. return -PTR_ERR(mp->m_sync_task);
  555. return 0;
  556. }
  557. void
  558. xfs_syncd_stop(
  559. struct xfs_mount *mp)
  560. {
  561. kthread_stop(mp->m_sync_task);
  562. }