xfs_sync.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. * When remounting a filesystem read-only or freezing the filesystem, we have
  280. * two phases to execute. This first phase is syncing the data before we
  281. * quiesce the filesystem, and the second is flushing all the inodes out after
  282. * we've waited for all the transactions created by the first phase to
  283. * complete. The second phase ensures that the inodes are written to their
  284. * location on disk rather than just existing in transactions in the log. This
  285. * means after a quiesce there is no log replay required to write the inodes to
  286. * disk (this is the main difference between a sync and a quiesce).
  287. */
  288. /*
  289. * First stage of freeze - no writers will make progress now we are here,
  290. * so we flush delwri and delalloc buffers here, then wait for all I/O to
  291. * complete. Data is frozen at that point. Metadata is not frozen,
  292. * transactions can still occur here so don't bother flushing the buftarg
  293. * because it'll just get dirty again.
  294. */
  295. int
  296. xfs_quiesce_data(
  297. struct xfs_mount *mp)
  298. {
  299. int error;
  300. /* push non-blocking */
  301. xfs_sync_inodes(mp, SYNC_DELWRI|SYNC_BDFLUSH);
  302. XFS_QM_DQSYNC(mp, SYNC_BDFLUSH);
  303. xfs_filestream_flush(mp);
  304. /* push and block */
  305. xfs_sync_inodes(mp, SYNC_DELWRI|SYNC_WAIT|SYNC_IOWAIT);
  306. XFS_QM_DQSYNC(mp, SYNC_WAIT);
  307. /* write superblock and hoover up shutdown errors */
  308. error = xfs_sync_fsdata(mp, 0);
  309. /* flush data-only devices */
  310. if (mp->m_rtdev_targp)
  311. XFS_bflush(mp->m_rtdev_targp);
  312. return error;
  313. }
  314. /*
  315. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  316. * Doing this has two advantages:
  317. * - It saves on stack space, which is tight in certain situations
  318. * - It can be used (with care) as a mechanism to avoid deadlocks.
  319. * Flushing while allocating in a full filesystem requires both.
  320. */
  321. STATIC void
  322. xfs_syncd_queue_work(
  323. struct xfs_mount *mp,
  324. void *data,
  325. void (*syncer)(struct xfs_mount *, void *))
  326. {
  327. struct bhv_vfs_sync_work *work;
  328. work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
  329. INIT_LIST_HEAD(&work->w_list);
  330. work->w_syncer = syncer;
  331. work->w_data = data;
  332. work->w_mount = mp;
  333. spin_lock(&mp->m_sync_lock);
  334. list_add_tail(&work->w_list, &mp->m_sync_list);
  335. spin_unlock(&mp->m_sync_lock);
  336. wake_up_process(mp->m_sync_task);
  337. }
  338. /*
  339. * Flush delayed allocate data, attempting to free up reserved space
  340. * from existing allocations. At this point a new allocation attempt
  341. * has failed with ENOSPC and we are in the process of scratching our
  342. * heads, looking about for more room...
  343. */
  344. STATIC void
  345. xfs_flush_inode_work(
  346. struct xfs_mount *mp,
  347. void *arg)
  348. {
  349. struct inode *inode = arg;
  350. filemap_flush(inode->i_mapping);
  351. iput(inode);
  352. }
  353. void
  354. xfs_flush_inode(
  355. xfs_inode_t *ip)
  356. {
  357. struct inode *inode = VFS_I(ip);
  358. igrab(inode);
  359. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inode_work);
  360. delay(msecs_to_jiffies(500));
  361. }
  362. /*
  363. * This is the "bigger hammer" version of xfs_flush_inode_work...
  364. * (IOW, "If at first you don't succeed, use a Bigger Hammer").
  365. */
  366. STATIC void
  367. xfs_flush_device_work(
  368. struct xfs_mount *mp,
  369. void *arg)
  370. {
  371. struct inode *inode = arg;
  372. sync_blockdev(mp->m_super->s_bdev);
  373. iput(inode);
  374. }
  375. void
  376. xfs_flush_device(
  377. xfs_inode_t *ip)
  378. {
  379. struct inode *inode = VFS_I(ip);
  380. igrab(inode);
  381. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_device_work);
  382. delay(msecs_to_jiffies(500));
  383. xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
  384. }
  385. /*
  386. * Every sync period we need to unpin all items, reclaim inodes, sync
  387. * quota and write out the superblock. We might need to cover the log
  388. * to indicate it is idle.
  389. */
  390. STATIC void
  391. xfs_sync_worker(
  392. struct xfs_mount *mp,
  393. void *unused)
  394. {
  395. int error;
  396. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  397. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  398. xfs_finish_reclaim_all(mp, 1, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  399. /* dgc: errors ignored here */
  400. error = XFS_QM_DQSYNC(mp, SYNC_BDFLUSH);
  401. error = xfs_sync_fsdata(mp, SYNC_BDFLUSH);
  402. if (xfs_log_need_covered(mp))
  403. error = xfs_commit_dummy_trans(mp, XFS_LOG_FORCE);
  404. }
  405. mp->m_sync_seq++;
  406. wake_up(&mp->m_wait_single_sync_task);
  407. }
  408. STATIC int
  409. xfssyncd(
  410. void *arg)
  411. {
  412. struct xfs_mount *mp = arg;
  413. long timeleft;
  414. bhv_vfs_sync_work_t *work, *n;
  415. LIST_HEAD (tmp);
  416. set_freezable();
  417. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  418. for (;;) {
  419. timeleft = schedule_timeout_interruptible(timeleft);
  420. /* swsusp */
  421. try_to_freeze();
  422. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  423. break;
  424. spin_lock(&mp->m_sync_lock);
  425. /*
  426. * We can get woken by laptop mode, to do a sync -
  427. * that's the (only!) case where the list would be
  428. * empty with time remaining.
  429. */
  430. if (!timeleft || list_empty(&mp->m_sync_list)) {
  431. if (!timeleft)
  432. timeleft = xfs_syncd_centisecs *
  433. msecs_to_jiffies(10);
  434. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  435. list_add_tail(&mp->m_sync_work.w_list,
  436. &mp->m_sync_list);
  437. }
  438. list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
  439. list_move(&work->w_list, &tmp);
  440. spin_unlock(&mp->m_sync_lock);
  441. list_for_each_entry_safe(work, n, &tmp, w_list) {
  442. (*work->w_syncer)(mp, work->w_data);
  443. list_del(&work->w_list);
  444. if (work == &mp->m_sync_work)
  445. continue;
  446. kmem_free(work);
  447. }
  448. }
  449. return 0;
  450. }
  451. int
  452. xfs_syncd_init(
  453. struct xfs_mount *mp)
  454. {
  455. mp->m_sync_work.w_syncer = xfs_sync_worker;
  456. mp->m_sync_work.w_mount = mp;
  457. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
  458. if (IS_ERR(mp->m_sync_task))
  459. return -PTR_ERR(mp->m_sync_task);
  460. return 0;
  461. }
  462. void
  463. xfs_syncd_stop(
  464. struct xfs_mount *mp)
  465. {
  466. kthread_stop(mp->m_sync_task);
  467. }