xfs_sync.c 15 KB

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