xfs_sync.c 16 KB

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