xfs_sync.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. /*
  290. * xfs sync routine for internal use
  291. *
  292. * This routine supports all of the flags defined for the generic vfs_sync
  293. * interface as explained above under xfs_sync.
  294. *
  295. */
  296. int
  297. xfs_syncsub(
  298. xfs_mount_t *mp,
  299. int flags,
  300. int *bypassed)
  301. {
  302. int error = 0;
  303. int last_error = 0;
  304. uint log_flags = XFS_LOG_FORCE;
  305. xfs_buf_t *bp;
  306. xfs_buf_log_item_t *bip;
  307. /*
  308. * Sync out the log. This ensures that the log is periodically
  309. * flushed even if there is not enough activity to fill it up.
  310. */
  311. if (flags & SYNC_WAIT)
  312. log_flags |= XFS_LOG_SYNC;
  313. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  314. if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
  315. if (flags & SYNC_BDFLUSH)
  316. xfs_finish_reclaim_all(mp, 1, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  317. else
  318. error = xfs_sync_inodes(mp, flags, bypassed);
  319. }
  320. /*
  321. * Flushing out dirty data above probably generated more
  322. * log activity, so if this isn't vfs_sync() then flush
  323. * the log again.
  324. */
  325. if (flags & SYNC_DELWRI) {
  326. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  327. }
  328. if (flags & SYNC_FSDATA) {
  329. /*
  330. * If this is vfs_sync() then only sync the superblock
  331. * if we can lock it without sleeping and it is not pinned.
  332. */
  333. if (flags & SYNC_BDFLUSH) {
  334. bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
  335. if (bp != NULL) {
  336. bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
  337. if ((bip != NULL) &&
  338. xfs_buf_item_dirty(bip)) {
  339. if (!(XFS_BUF_ISPINNED(bp))) {
  340. XFS_BUF_ASYNC(bp);
  341. error = xfs_bwrite(mp, bp);
  342. } else {
  343. xfs_buf_relse(bp);
  344. }
  345. } else {
  346. xfs_buf_relse(bp);
  347. }
  348. }
  349. } else {
  350. bp = xfs_getsb(mp, 0);
  351. /*
  352. * If the buffer is pinned then push on the log so
  353. * we won't get stuck waiting in the write for
  354. * someone, maybe ourselves, to flush the log.
  355. * Even though we just pushed the log above, we
  356. * did not have the superblock buffer locked at
  357. * that point so it can become pinned in between
  358. * there and here.
  359. */
  360. if (XFS_BUF_ISPINNED(bp))
  361. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  362. if (flags & SYNC_WAIT)
  363. XFS_BUF_UNASYNC(bp);
  364. else
  365. XFS_BUF_ASYNC(bp);
  366. error = xfs_bwrite(mp, bp);
  367. }
  368. if (error) {
  369. last_error = error;
  370. }
  371. }
  372. /*
  373. * Now check to see if the log needs a "dummy" transaction.
  374. */
  375. if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
  376. xfs_trans_t *tp;
  377. xfs_inode_t *ip;
  378. /*
  379. * Put a dummy transaction in the log to tell
  380. * recovery that all others are OK.
  381. */
  382. tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
  383. if ((error = xfs_trans_reserve(tp, 0,
  384. XFS_ICHANGE_LOG_RES(mp),
  385. 0, 0, 0))) {
  386. xfs_trans_cancel(tp, 0);
  387. return error;
  388. }
  389. ip = mp->m_rootip;
  390. xfs_ilock(ip, XFS_ILOCK_EXCL);
  391. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  392. xfs_trans_ihold(tp, ip);
  393. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  394. error = xfs_trans_commit(tp, 0);
  395. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  396. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  397. }
  398. /*
  399. * When shutting down, we need to insure that the AIL is pushed
  400. * to disk or the filesystem can appear corrupt from the PROM.
  401. */
  402. if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
  403. XFS_bflush(mp->m_ddev_targp);
  404. if (mp->m_rtdev_targp) {
  405. XFS_bflush(mp->m_rtdev_targp);
  406. }
  407. }
  408. return XFS_ERROR(last_error);
  409. }
  410. /*
  411. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  412. * Doing this has two advantages:
  413. * - It saves on stack space, which is tight in certain situations
  414. * - It can be used (with care) as a mechanism to avoid deadlocks.
  415. * Flushing while allocating in a full filesystem requires both.
  416. */
  417. STATIC void
  418. xfs_syncd_queue_work(
  419. struct xfs_mount *mp,
  420. void *data,
  421. void (*syncer)(struct xfs_mount *, void *))
  422. {
  423. struct bhv_vfs_sync_work *work;
  424. work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
  425. INIT_LIST_HEAD(&work->w_list);
  426. work->w_syncer = syncer;
  427. work->w_data = data;
  428. work->w_mount = mp;
  429. spin_lock(&mp->m_sync_lock);
  430. list_add_tail(&work->w_list, &mp->m_sync_list);
  431. spin_unlock(&mp->m_sync_lock);
  432. wake_up_process(mp->m_sync_task);
  433. }
  434. /*
  435. * Flush delayed allocate data, attempting to free up reserved space
  436. * from existing allocations. At this point a new allocation attempt
  437. * has failed with ENOSPC and we are in the process of scratching our
  438. * heads, looking about for more room...
  439. */
  440. STATIC void
  441. xfs_flush_inode_work(
  442. struct xfs_mount *mp,
  443. void *arg)
  444. {
  445. struct inode *inode = arg;
  446. filemap_flush(inode->i_mapping);
  447. iput(inode);
  448. }
  449. void
  450. xfs_flush_inode(
  451. xfs_inode_t *ip)
  452. {
  453. struct inode *inode = VFS_I(ip);
  454. igrab(inode);
  455. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inode_work);
  456. delay(msecs_to_jiffies(500));
  457. }
  458. /*
  459. * This is the "bigger hammer" version of xfs_flush_inode_work...
  460. * (IOW, "If at first you don't succeed, use a Bigger Hammer").
  461. */
  462. STATIC void
  463. xfs_flush_device_work(
  464. struct xfs_mount *mp,
  465. void *arg)
  466. {
  467. struct inode *inode = arg;
  468. sync_blockdev(mp->m_super->s_bdev);
  469. iput(inode);
  470. }
  471. void
  472. xfs_flush_device(
  473. xfs_inode_t *ip)
  474. {
  475. struct inode *inode = VFS_I(ip);
  476. igrab(inode);
  477. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_device_work);
  478. delay(msecs_to_jiffies(500));
  479. xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
  480. }
  481. STATIC void
  482. xfs_sync_worker(
  483. struct xfs_mount *mp,
  484. void *unused)
  485. {
  486. int error;
  487. if (!(mp->m_flags & XFS_MOUNT_RDONLY))
  488. error = xfs_sync(mp, SYNC_FSDATA | SYNC_BDFLUSH | SYNC_ATTR);
  489. mp->m_sync_seq++;
  490. wake_up(&mp->m_wait_single_sync_task);
  491. }
  492. STATIC int
  493. xfssyncd(
  494. void *arg)
  495. {
  496. struct xfs_mount *mp = arg;
  497. long timeleft;
  498. bhv_vfs_sync_work_t *work, *n;
  499. LIST_HEAD (tmp);
  500. set_freezable();
  501. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  502. for (;;) {
  503. timeleft = schedule_timeout_interruptible(timeleft);
  504. /* swsusp */
  505. try_to_freeze();
  506. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  507. break;
  508. spin_lock(&mp->m_sync_lock);
  509. /*
  510. * We can get woken by laptop mode, to do a sync -
  511. * that's the (only!) case where the list would be
  512. * empty with time remaining.
  513. */
  514. if (!timeleft || list_empty(&mp->m_sync_list)) {
  515. if (!timeleft)
  516. timeleft = xfs_syncd_centisecs *
  517. msecs_to_jiffies(10);
  518. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  519. list_add_tail(&mp->m_sync_work.w_list,
  520. &mp->m_sync_list);
  521. }
  522. list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
  523. list_move(&work->w_list, &tmp);
  524. spin_unlock(&mp->m_sync_lock);
  525. list_for_each_entry_safe(work, n, &tmp, w_list) {
  526. (*work->w_syncer)(mp, work->w_data);
  527. list_del(&work->w_list);
  528. if (work == &mp->m_sync_work)
  529. continue;
  530. kmem_free(work);
  531. }
  532. }
  533. return 0;
  534. }
  535. int
  536. xfs_syncd_init(
  537. struct xfs_mount *mp)
  538. {
  539. mp->m_sync_work.w_syncer = xfs_sync_worker;
  540. mp->m_sync_work.w_mount = mp;
  541. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
  542. if (IS_ERR(mp->m_sync_task))
  543. return -PTR_ERR(mp->m_sync_task);
  544. return 0;
  545. }
  546. void
  547. xfs_syncd_stop(
  548. struct xfs_mount *mp)
  549. {
  550. kthread_stop(mp->m_sync_task);
  551. }