xfs_sync.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 | SYNC_CLOSE)) {
  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) && !(flags & SYNC_CLOSE)) {
  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_CLOSE) {
  143. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  144. if (XFS_FORCED_SHUTDOWN(mp))
  145. xfs_tosspages(ip, 0, -1, FI_REMAPF);
  146. else
  147. error = xfs_flushinval_pages(ip, 0, -1,
  148. FI_REMAPF);
  149. /* wait for I/O on freeze */
  150. if (flags & SYNC_IOWAIT)
  151. vn_iowait(ip);
  152. xfs_ilock(ip, XFS_ILOCK_SHARED);
  153. }
  154. if ((flags & SYNC_DELWRI) && VN_DIRTY(inode)) {
  155. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  156. error = xfs_flush_pages(ip, 0, -1, fflag, FI_NONE);
  157. if (flags & SYNC_IOWAIT)
  158. vn_iowait(ip);
  159. xfs_ilock(ip, XFS_ILOCK_SHARED);
  160. }
  161. if ((flags & SYNC_ATTR) && !xfs_inode_clean(ip)) {
  162. if (flags & SYNC_WAIT) {
  163. xfs_iflock(ip);
  164. if (!xfs_inode_clean(ip))
  165. error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
  166. else
  167. xfs_ifunlock(ip);
  168. } else if (xfs_iflock_nowait(ip)) {
  169. if (!xfs_inode_clean(ip))
  170. error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
  171. else
  172. xfs_ifunlock(ip);
  173. }
  174. }
  175. if (lock_flags)
  176. xfs_iunlock(ip, lock_flags);
  177. if (inode_refed) {
  178. IRELE(ip);
  179. }
  180. if (error)
  181. last_error = error;
  182. /*
  183. * bail out if the filesystem is corrupted.
  184. */
  185. if (error == EFSCORRUPTED)
  186. return XFS_ERROR(error);
  187. } while (nr_found);
  188. return last_error;
  189. }
  190. int
  191. xfs_sync_inodes(
  192. xfs_mount_t *mp,
  193. int flags)
  194. {
  195. int error;
  196. int last_error;
  197. int i;
  198. if (mp->m_flags & XFS_MOUNT_RDONLY)
  199. return 0;
  200. error = 0;
  201. last_error = 0;
  202. for (i = 0; i < mp->m_sb.sb_agcount; i++) {
  203. if (!mp->m_perag[i].pag_ici_init)
  204. continue;
  205. error = xfs_sync_inodes_ag(mp, i, flags);
  206. if (error)
  207. last_error = error;
  208. if (error == EFSCORRUPTED)
  209. break;
  210. }
  211. return XFS_ERROR(last_error);
  212. }
  213. STATIC int
  214. xfs_commit_dummy_trans(
  215. struct xfs_mount *mp,
  216. uint log_flags)
  217. {
  218. struct xfs_inode *ip = mp->m_rootip;
  219. struct xfs_trans *tp;
  220. int error;
  221. /*
  222. * Put a dummy transaction in the log to tell recovery
  223. * that all others are OK.
  224. */
  225. tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
  226. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  227. if (error) {
  228. xfs_trans_cancel(tp, 0);
  229. return error;
  230. }
  231. xfs_ilock(ip, XFS_ILOCK_EXCL);
  232. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  233. xfs_trans_ihold(tp, ip);
  234. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  235. /* XXX(hch): ignoring the error here.. */
  236. error = xfs_trans_commit(tp, 0);
  237. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  238. xfs_log_force(mp, 0, log_flags);
  239. return 0;
  240. }
  241. STATIC int
  242. xfs_sync_fsdata(
  243. struct xfs_mount *mp,
  244. int flags)
  245. {
  246. struct xfs_buf *bp;
  247. struct xfs_buf_log_item *bip;
  248. int error = 0;
  249. /*
  250. * If this is xfssyncd() then only sync the superblock if we can
  251. * lock it without sleeping and it is not pinned.
  252. */
  253. if (flags & SYNC_BDFLUSH) {
  254. ASSERT(!(flags & SYNC_WAIT));
  255. bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
  256. if (!bp)
  257. goto out;
  258. bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
  259. if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
  260. goto out_brelse;
  261. } else {
  262. bp = xfs_getsb(mp, 0);
  263. /*
  264. * If the buffer is pinned then push on the log so we won't
  265. * get stuck waiting in the write for someone, maybe
  266. * ourselves, to flush the log.
  267. *
  268. * Even though we just pushed the log above, we did not have
  269. * the superblock buffer locked at that point so it can
  270. * become pinned in between there and here.
  271. */
  272. if (XFS_BUF_ISPINNED(bp))
  273. xfs_log_force(mp, 0, XFS_LOG_FORCE);
  274. }
  275. if (flags & SYNC_WAIT)
  276. XFS_BUF_UNASYNC(bp);
  277. else
  278. XFS_BUF_ASYNC(bp);
  279. return xfs_bwrite(mp, bp);
  280. out_brelse:
  281. xfs_buf_relse(bp);
  282. out:
  283. return error;
  284. }
  285. /*
  286. * xfs_sync flushes any pending I/O to file system vfsp.
  287. *
  288. * This routine is called by vfs_sync() to make sure that things make it
  289. * out to disk eventually, on sync() system calls to flush out everything,
  290. * and when the file system is unmounted. For the vfs_sync() case, all
  291. * we really need to do is sync out the log to make all of our meta-data
  292. * updates permanent (except for timestamps). For calls from pflushd(),
  293. * dirty pages are kept moving by calling pdflush() on the inodes
  294. * containing them. We also flush the inodes that we can lock without
  295. * sleeping and the superblock if we can lock it without sleeping from
  296. * vfs_sync() so that items at the tail of the log are always moving out.
  297. *
  298. * Flags:
  299. * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
  300. * to sleep if we can help it. All we really need
  301. * to do is ensure that the log is synced at least
  302. * periodically. We also push the inodes and
  303. * superblock if we can lock them without sleeping
  304. * and they are not pinned.
  305. * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
  306. * set, then we really want to lock each inode and flush
  307. * it.
  308. * SYNC_WAIT - All the flushes that take place in this call should
  309. * be synchronous.
  310. * SYNC_DELWRI - This tells us to push dirty pages associated with
  311. * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
  312. * determine if they should be flushed sync, async, or
  313. * delwri.
  314. * SYNC_CLOSE - This flag is passed when the system is being
  315. * unmounted. We should sync and invalidate everything.
  316. * SYNC_FSDATA - This indicates that the caller would like to make
  317. * sure the superblock is safe on disk. We can ensure
  318. * this by simply making sure the log gets flushed
  319. * if SYNC_BDFLUSH is set, and by actually writing it
  320. * out otherwise.
  321. * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
  322. * before we return (including direct I/O). Forms the drain
  323. * side of the write barrier needed to safely quiesce the
  324. * filesystem.
  325. *
  326. */
  327. int
  328. xfs_sync(
  329. xfs_mount_t *mp,
  330. int flags)
  331. {
  332. int error;
  333. int last_error = 0;
  334. uint log_flags = XFS_LOG_FORCE;
  335. /*
  336. * Get the Quota Manager to flush the dquots.
  337. *
  338. * If XFS quota support is not enabled or this filesystem
  339. * instance does not use quotas XFS_QM_DQSYNC will always
  340. * return zero.
  341. */
  342. error = XFS_QM_DQSYNC(mp, flags);
  343. if (error) {
  344. /*
  345. * If we got an IO error, we will be shutting down.
  346. * So, there's nothing more for us to do here.
  347. */
  348. ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
  349. if (XFS_FORCED_SHUTDOWN(mp))
  350. return XFS_ERROR(error);
  351. }
  352. if (flags & SYNC_IOWAIT)
  353. xfs_filestream_flush(mp);
  354. /*
  355. * Sync out the log. This ensures that the log is periodically
  356. * flushed even if there is not enough activity to fill it up.
  357. */
  358. if (flags & SYNC_WAIT)
  359. log_flags |= XFS_LOG_SYNC;
  360. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  361. if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
  362. if (flags & SYNC_BDFLUSH)
  363. xfs_finish_reclaim_all(mp, 1, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  364. else
  365. error = xfs_sync_inodes(mp, flags);
  366. }
  367. /*
  368. * Flushing out dirty data above probably generated more
  369. * log activity, so if this isn't vfs_sync() then flush
  370. * the log again.
  371. */
  372. if (flags & SYNC_DELWRI)
  373. xfs_log_force(mp, 0, log_flags);
  374. if (flags & SYNC_FSDATA) {
  375. error = xfs_sync_fsdata(mp, flags);
  376. if (error)
  377. last_error = error;
  378. }
  379. /*
  380. * Now check to see if the log needs a "dummy" transaction.
  381. */
  382. if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
  383. error = xfs_commit_dummy_trans(mp, log_flags);
  384. if (error)
  385. return error;
  386. }
  387. /*
  388. * When shutting down, we need to insure that the AIL is pushed
  389. * to disk or the filesystem can appear corrupt from the PROM.
  390. */
  391. if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
  392. XFS_bflush(mp->m_ddev_targp);
  393. if (mp->m_rtdev_targp) {
  394. XFS_bflush(mp->m_rtdev_targp);
  395. }
  396. }
  397. return XFS_ERROR(last_error);
  398. }
  399. /*
  400. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  401. * Doing this has two advantages:
  402. * - It saves on stack space, which is tight in certain situations
  403. * - It can be used (with care) as a mechanism to avoid deadlocks.
  404. * Flushing while allocating in a full filesystem requires both.
  405. */
  406. STATIC void
  407. xfs_syncd_queue_work(
  408. struct xfs_mount *mp,
  409. void *data,
  410. void (*syncer)(struct xfs_mount *, void *))
  411. {
  412. struct bhv_vfs_sync_work *work;
  413. work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
  414. INIT_LIST_HEAD(&work->w_list);
  415. work->w_syncer = syncer;
  416. work->w_data = data;
  417. work->w_mount = mp;
  418. spin_lock(&mp->m_sync_lock);
  419. list_add_tail(&work->w_list, &mp->m_sync_list);
  420. spin_unlock(&mp->m_sync_lock);
  421. wake_up_process(mp->m_sync_task);
  422. }
  423. /*
  424. * Flush delayed allocate data, attempting to free up reserved space
  425. * from existing allocations. At this point a new allocation attempt
  426. * has failed with ENOSPC and we are in the process of scratching our
  427. * heads, looking about for more room...
  428. */
  429. STATIC void
  430. xfs_flush_inode_work(
  431. struct xfs_mount *mp,
  432. void *arg)
  433. {
  434. struct inode *inode = arg;
  435. filemap_flush(inode->i_mapping);
  436. iput(inode);
  437. }
  438. void
  439. xfs_flush_inode(
  440. xfs_inode_t *ip)
  441. {
  442. struct inode *inode = VFS_I(ip);
  443. igrab(inode);
  444. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inode_work);
  445. delay(msecs_to_jiffies(500));
  446. }
  447. /*
  448. * This is the "bigger hammer" version of xfs_flush_inode_work...
  449. * (IOW, "If at first you don't succeed, use a Bigger Hammer").
  450. */
  451. STATIC void
  452. xfs_flush_device_work(
  453. struct xfs_mount *mp,
  454. void *arg)
  455. {
  456. struct inode *inode = arg;
  457. sync_blockdev(mp->m_super->s_bdev);
  458. iput(inode);
  459. }
  460. void
  461. xfs_flush_device(
  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_device_work);
  467. delay(msecs_to_jiffies(500));
  468. xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
  469. }
  470. STATIC void
  471. xfs_sync_worker(
  472. struct xfs_mount *mp,
  473. void *unused)
  474. {
  475. int error;
  476. if (!(mp->m_flags & XFS_MOUNT_RDONLY))
  477. error = xfs_sync(mp, SYNC_FSDATA | SYNC_BDFLUSH | SYNC_ATTR);
  478. mp->m_sync_seq++;
  479. wake_up(&mp->m_wait_single_sync_task);
  480. }
  481. STATIC int
  482. xfssyncd(
  483. void *arg)
  484. {
  485. struct xfs_mount *mp = arg;
  486. long timeleft;
  487. bhv_vfs_sync_work_t *work, *n;
  488. LIST_HEAD (tmp);
  489. set_freezable();
  490. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  491. for (;;) {
  492. timeleft = schedule_timeout_interruptible(timeleft);
  493. /* swsusp */
  494. try_to_freeze();
  495. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  496. break;
  497. spin_lock(&mp->m_sync_lock);
  498. /*
  499. * We can get woken by laptop mode, to do a sync -
  500. * that's the (only!) case where the list would be
  501. * empty with time remaining.
  502. */
  503. if (!timeleft || list_empty(&mp->m_sync_list)) {
  504. if (!timeleft)
  505. timeleft = xfs_syncd_centisecs *
  506. msecs_to_jiffies(10);
  507. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  508. list_add_tail(&mp->m_sync_work.w_list,
  509. &mp->m_sync_list);
  510. }
  511. list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
  512. list_move(&work->w_list, &tmp);
  513. spin_unlock(&mp->m_sync_lock);
  514. list_for_each_entry_safe(work, n, &tmp, w_list) {
  515. (*work->w_syncer)(mp, work->w_data);
  516. list_del(&work->w_list);
  517. if (work == &mp->m_sync_work)
  518. continue;
  519. kmem_free(work);
  520. }
  521. }
  522. return 0;
  523. }
  524. int
  525. xfs_syncd_init(
  526. struct xfs_mount *mp)
  527. {
  528. mp->m_sync_work.w_syncer = xfs_sync_worker;
  529. mp->m_sync_work.w_mount = mp;
  530. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
  531. if (IS_ERR(mp->m_sync_task))
  532. return -PTR_ERR(mp->m_sync_task);
  533. return 0;
  534. }
  535. void
  536. xfs_syncd_stop(
  537. struct xfs_mount *mp)
  538. {
  539. kthread_stop(mp->m_sync_task);
  540. }