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