xfs_sync.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. * xfs sync routine for internal use
  119. *
  120. * This routine supports all of the flags defined for the generic vfs_sync
  121. * interface as explained above under xfs_sync.
  122. *
  123. */
  124. int
  125. xfs_sync_inodes(
  126. xfs_mount_t *mp,
  127. int flags,
  128. int *bypassed)
  129. {
  130. xfs_inode_t *ip = NULL;
  131. struct inode *vp = NULL;
  132. int error;
  133. int last_error;
  134. uint64_t fflag;
  135. uint lock_flags;
  136. uint base_lock_flags;
  137. boolean_t mount_locked;
  138. boolean_t vnode_refed;
  139. int preempt;
  140. xfs_iptr_t *ipointer;
  141. #ifdef DEBUG
  142. boolean_t ipointer_in = B_FALSE;
  143. #define IPOINTER_SET ipointer_in = B_TRUE
  144. #define IPOINTER_CLR ipointer_in = B_FALSE
  145. #else
  146. #define IPOINTER_SET
  147. #define IPOINTER_CLR
  148. #endif
  149. /* Insert a marker record into the inode list after inode ip. The list
  150. * must be locked when this is called. After the call the list will no
  151. * longer be locked.
  152. */
  153. #define IPOINTER_INSERT(ip, mp) { \
  154. ASSERT(ipointer_in == B_FALSE); \
  155. ipointer->ip_mnext = ip->i_mnext; \
  156. ipointer->ip_mprev = ip; \
  157. ip->i_mnext = (xfs_inode_t *)ipointer; \
  158. ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
  159. preempt = 0; \
  160. XFS_MOUNT_IUNLOCK(mp); \
  161. mount_locked = B_FALSE; \
  162. IPOINTER_SET; \
  163. }
  164. /* Remove the marker from the inode list. If the marker was the only item
  165. * in the list then there are no remaining inodes and we should zero out
  166. * the whole list. If we are the current head of the list then move the head
  167. * past us.
  168. */
  169. #define IPOINTER_REMOVE(ip, mp) { \
  170. ASSERT(ipointer_in == B_TRUE); \
  171. if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
  172. ip = ipointer->ip_mnext; \
  173. ip->i_mprev = ipointer->ip_mprev; \
  174. ipointer->ip_mprev->i_mnext = ip; \
  175. if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
  176. mp->m_inodes = ip; \
  177. } \
  178. } else { \
  179. ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
  180. mp->m_inodes = NULL; \
  181. ip = NULL; \
  182. } \
  183. IPOINTER_CLR; \
  184. }
  185. #define XFS_PREEMPT_MASK 0x7f
  186. ASSERT(!(flags & SYNC_BDFLUSH));
  187. if (bypassed)
  188. *bypassed = 0;
  189. if (mp->m_flags & XFS_MOUNT_RDONLY)
  190. return 0;
  191. error = 0;
  192. last_error = 0;
  193. preempt = 0;
  194. /* Allocate a reference marker */
  195. ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
  196. fflag = XFS_B_ASYNC; /* default is don't wait */
  197. if (flags & SYNC_DELWRI)
  198. fflag = XFS_B_DELWRI;
  199. if (flags & SYNC_WAIT)
  200. fflag = 0; /* synchronous overrides all */
  201. base_lock_flags = XFS_ILOCK_SHARED;
  202. if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
  203. /*
  204. * We need the I/O lock if we're going to call any of
  205. * the flush/inval routines.
  206. */
  207. base_lock_flags |= XFS_IOLOCK_SHARED;
  208. }
  209. XFS_MOUNT_ILOCK(mp);
  210. ip = mp->m_inodes;
  211. mount_locked = B_TRUE;
  212. vnode_refed = B_FALSE;
  213. IPOINTER_CLR;
  214. do {
  215. ASSERT(ipointer_in == B_FALSE);
  216. ASSERT(vnode_refed == B_FALSE);
  217. lock_flags = base_lock_flags;
  218. /*
  219. * There were no inodes in the list, just break out
  220. * of the loop.
  221. */
  222. if (ip == NULL) {
  223. break;
  224. }
  225. /*
  226. * We found another sync thread marker - skip it
  227. */
  228. if (ip->i_mount == NULL) {
  229. ip = ip->i_mnext;
  230. continue;
  231. }
  232. vp = VFS_I(ip);
  233. /*
  234. * If the vnode is gone then this is being torn down,
  235. * call reclaim if it is flushed, else let regular flush
  236. * code deal with it later in the loop.
  237. */
  238. if (vp == NULL) {
  239. /* Skip ones already in reclaim */
  240. if (ip->i_flags & XFS_IRECLAIM) {
  241. ip = ip->i_mnext;
  242. continue;
  243. }
  244. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
  245. ip = ip->i_mnext;
  246. } else if ((xfs_ipincount(ip) == 0) &&
  247. xfs_iflock_nowait(ip)) {
  248. IPOINTER_INSERT(ip, mp);
  249. xfs_finish_reclaim(ip, 1,
  250. XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  251. XFS_MOUNT_ILOCK(mp);
  252. mount_locked = B_TRUE;
  253. IPOINTER_REMOVE(ip, mp);
  254. } else {
  255. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  256. ip = ip->i_mnext;
  257. }
  258. continue;
  259. }
  260. if (VN_BAD(vp)) {
  261. ip = ip->i_mnext;
  262. continue;
  263. }
  264. if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
  265. XFS_MOUNT_IUNLOCK(mp);
  266. kmem_free(ipointer);
  267. return 0;
  268. }
  269. /*
  270. * Try to lock without sleeping. We're out of order with
  271. * the inode list lock here, so if we fail we need to drop
  272. * the mount lock and try again. If we're called from
  273. * bdflush() here, then don't bother.
  274. *
  275. * The inode lock here actually coordinates with the
  276. * almost spurious inode lock in xfs_ireclaim() to prevent
  277. * the vnode we handle here without a reference from
  278. * being freed while we reference it. If we lock the inode
  279. * while it's on the mount list here, then the spurious inode
  280. * lock in xfs_ireclaim() after the inode is pulled from
  281. * the mount list will sleep until we release it here.
  282. * This keeps the vnode from being freed while we reference
  283. * it.
  284. */
  285. if (xfs_ilock_nowait(ip, lock_flags) == 0) {
  286. if (vp == NULL) {
  287. ip = ip->i_mnext;
  288. continue;
  289. }
  290. vp = vn_grab(vp);
  291. if (vp == NULL) {
  292. ip = ip->i_mnext;
  293. continue;
  294. }
  295. IPOINTER_INSERT(ip, mp);
  296. xfs_ilock(ip, lock_flags);
  297. ASSERT(vp == VFS_I(ip));
  298. ASSERT(ip->i_mount == mp);
  299. vnode_refed = B_TRUE;
  300. }
  301. /* From here on in the loop we may have a marker record
  302. * in the inode list.
  303. */
  304. /*
  305. * If we have to flush data or wait for I/O completion
  306. * we need to drop the ilock that we currently hold.
  307. * If we need to drop the lock, insert a marker if we
  308. * have not already done so.
  309. */
  310. if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
  311. ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
  312. if (mount_locked) {
  313. IPOINTER_INSERT(ip, mp);
  314. }
  315. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  316. if (flags & SYNC_CLOSE) {
  317. /* Shutdown case. Flush and invalidate. */
  318. if (XFS_FORCED_SHUTDOWN(mp))
  319. xfs_tosspages(ip, 0, -1,
  320. FI_REMAPF);
  321. else
  322. error = xfs_flushinval_pages(ip,
  323. 0, -1, FI_REMAPF);
  324. } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
  325. error = xfs_flush_pages(ip, 0,
  326. -1, fflag, FI_NONE);
  327. }
  328. /*
  329. * When freezing, we need to wait ensure all I/O (including direct
  330. * I/O) is complete to ensure no further data modification can take
  331. * place after this point
  332. */
  333. if (flags & SYNC_IOWAIT)
  334. vn_iowait(ip);
  335. xfs_ilock(ip, XFS_ILOCK_SHARED);
  336. }
  337. if ((flags & SYNC_ATTR) &&
  338. (ip->i_update_core ||
  339. (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
  340. if (mount_locked)
  341. IPOINTER_INSERT(ip, mp);
  342. if (flags & SYNC_WAIT) {
  343. xfs_iflock(ip);
  344. error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
  345. /*
  346. * If we can't acquire the flush lock, then the inode
  347. * is already being flushed so don't bother waiting.
  348. *
  349. * If we can lock it then do a delwri flush so we can
  350. * combine multiple inode flushes in each disk write.
  351. */
  352. } else if (xfs_iflock_nowait(ip)) {
  353. error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
  354. } else if (bypassed) {
  355. (*bypassed)++;
  356. }
  357. }
  358. if (lock_flags != 0) {
  359. xfs_iunlock(ip, lock_flags);
  360. }
  361. if (vnode_refed) {
  362. /*
  363. * If we had to take a reference on the vnode
  364. * above, then wait until after we've unlocked
  365. * the inode to release the reference. This is
  366. * because we can be already holding the inode
  367. * lock when IRELE() calls xfs_inactive().
  368. *
  369. * Make sure to drop the mount lock before calling
  370. * IRELE() so that we don't trip over ourselves if
  371. * we have to go for the mount lock again in the
  372. * inactive code.
  373. */
  374. if (mount_locked) {
  375. IPOINTER_INSERT(ip, mp);
  376. }
  377. IRELE(ip);
  378. vnode_refed = B_FALSE;
  379. }
  380. if (error) {
  381. last_error = error;
  382. }
  383. /*
  384. * bail out if the filesystem is corrupted.
  385. */
  386. if (error == EFSCORRUPTED) {
  387. if (!mount_locked) {
  388. XFS_MOUNT_ILOCK(mp);
  389. IPOINTER_REMOVE(ip, mp);
  390. }
  391. XFS_MOUNT_IUNLOCK(mp);
  392. ASSERT(ipointer_in == B_FALSE);
  393. kmem_free(ipointer);
  394. return XFS_ERROR(error);
  395. }
  396. /* Let other threads have a chance at the mount lock
  397. * if we have looped many times without dropping the
  398. * lock.
  399. */
  400. if ((++preempt & XFS_PREEMPT_MASK) == 0) {
  401. if (mount_locked) {
  402. IPOINTER_INSERT(ip, mp);
  403. }
  404. }
  405. if (mount_locked == B_FALSE) {
  406. XFS_MOUNT_ILOCK(mp);
  407. mount_locked = B_TRUE;
  408. IPOINTER_REMOVE(ip, mp);
  409. continue;
  410. }
  411. ASSERT(ipointer_in == B_FALSE);
  412. ip = ip->i_mnext;
  413. } while (ip != mp->m_inodes);
  414. XFS_MOUNT_IUNLOCK(mp);
  415. ASSERT(ipointer_in == B_FALSE);
  416. kmem_free(ipointer);
  417. return XFS_ERROR(last_error);
  418. }
  419. /*
  420. * xfs sync routine for internal use
  421. *
  422. * This routine supports all of the flags defined for the generic vfs_sync
  423. * interface as explained above under xfs_sync.
  424. *
  425. */
  426. int
  427. xfs_syncsub(
  428. xfs_mount_t *mp,
  429. int flags,
  430. int *bypassed)
  431. {
  432. int error = 0;
  433. int last_error = 0;
  434. uint log_flags = XFS_LOG_FORCE;
  435. xfs_buf_t *bp;
  436. xfs_buf_log_item_t *bip;
  437. /*
  438. * Sync out the log. This ensures that the log is periodically
  439. * flushed even if there is not enough activity to fill it up.
  440. */
  441. if (flags & SYNC_WAIT)
  442. log_flags |= XFS_LOG_SYNC;
  443. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  444. if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
  445. if (flags & SYNC_BDFLUSH)
  446. xfs_finish_reclaim_all(mp, 1, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  447. else
  448. error = xfs_sync_inodes(mp, flags, bypassed);
  449. }
  450. /*
  451. * Flushing out dirty data above probably generated more
  452. * log activity, so if this isn't vfs_sync() then flush
  453. * the log again.
  454. */
  455. if (flags & SYNC_DELWRI) {
  456. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  457. }
  458. if (flags & SYNC_FSDATA) {
  459. /*
  460. * If this is vfs_sync() then only sync the superblock
  461. * if we can lock it without sleeping and it is not pinned.
  462. */
  463. if (flags & SYNC_BDFLUSH) {
  464. bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
  465. if (bp != NULL) {
  466. bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
  467. if ((bip != NULL) &&
  468. xfs_buf_item_dirty(bip)) {
  469. if (!(XFS_BUF_ISPINNED(bp))) {
  470. XFS_BUF_ASYNC(bp);
  471. error = xfs_bwrite(mp, bp);
  472. } else {
  473. xfs_buf_relse(bp);
  474. }
  475. } else {
  476. xfs_buf_relse(bp);
  477. }
  478. }
  479. } else {
  480. bp = xfs_getsb(mp, 0);
  481. /*
  482. * If the buffer is pinned then push on the log so
  483. * we won't get stuck waiting in the write for
  484. * someone, maybe ourselves, to flush the log.
  485. * Even though we just pushed the log above, we
  486. * did not have the superblock buffer locked at
  487. * that point so it can become pinned in between
  488. * there and here.
  489. */
  490. if (XFS_BUF_ISPINNED(bp))
  491. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  492. if (flags & SYNC_WAIT)
  493. XFS_BUF_UNASYNC(bp);
  494. else
  495. XFS_BUF_ASYNC(bp);
  496. error = xfs_bwrite(mp, bp);
  497. }
  498. if (error) {
  499. last_error = error;
  500. }
  501. }
  502. /*
  503. * Now check to see if the log needs a "dummy" transaction.
  504. */
  505. if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
  506. xfs_trans_t *tp;
  507. xfs_inode_t *ip;
  508. /*
  509. * Put a dummy transaction in the log to tell
  510. * recovery that all others are OK.
  511. */
  512. tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
  513. if ((error = xfs_trans_reserve(tp, 0,
  514. XFS_ICHANGE_LOG_RES(mp),
  515. 0, 0, 0))) {
  516. xfs_trans_cancel(tp, 0);
  517. return error;
  518. }
  519. ip = mp->m_rootip;
  520. xfs_ilock(ip, XFS_ILOCK_EXCL);
  521. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  522. xfs_trans_ihold(tp, ip);
  523. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  524. error = xfs_trans_commit(tp, 0);
  525. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  526. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  527. }
  528. /*
  529. * When shutting down, we need to insure that the AIL is pushed
  530. * to disk or the filesystem can appear corrupt from the PROM.
  531. */
  532. if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
  533. XFS_bflush(mp->m_ddev_targp);
  534. if (mp->m_rtdev_targp) {
  535. XFS_bflush(mp->m_rtdev_targp);
  536. }
  537. }
  538. return XFS_ERROR(last_error);
  539. }
  540. /*
  541. * Enqueue a work item to be picked up by the vfs xfssyncd thread.
  542. * Doing this has two advantages:
  543. * - It saves on stack space, which is tight in certain situations
  544. * - It can be used (with care) as a mechanism to avoid deadlocks.
  545. * Flushing while allocating in a full filesystem requires both.
  546. */
  547. STATIC void
  548. xfs_syncd_queue_work(
  549. struct xfs_mount *mp,
  550. void *data,
  551. void (*syncer)(struct xfs_mount *, void *))
  552. {
  553. struct bhv_vfs_sync_work *work;
  554. work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
  555. INIT_LIST_HEAD(&work->w_list);
  556. work->w_syncer = syncer;
  557. work->w_data = data;
  558. work->w_mount = mp;
  559. spin_lock(&mp->m_sync_lock);
  560. list_add_tail(&work->w_list, &mp->m_sync_list);
  561. spin_unlock(&mp->m_sync_lock);
  562. wake_up_process(mp->m_sync_task);
  563. }
  564. /*
  565. * Flush delayed allocate data, attempting to free up reserved space
  566. * from existing allocations. At this point a new allocation attempt
  567. * has failed with ENOSPC and we are in the process of scratching our
  568. * heads, looking about for more room...
  569. */
  570. STATIC void
  571. xfs_flush_inode_work(
  572. struct xfs_mount *mp,
  573. void *arg)
  574. {
  575. struct inode *inode = arg;
  576. filemap_flush(inode->i_mapping);
  577. iput(inode);
  578. }
  579. void
  580. xfs_flush_inode(
  581. xfs_inode_t *ip)
  582. {
  583. struct inode *inode = VFS_I(ip);
  584. igrab(inode);
  585. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inode_work);
  586. delay(msecs_to_jiffies(500));
  587. }
  588. /*
  589. * This is the "bigger hammer" version of xfs_flush_inode_work...
  590. * (IOW, "If at first you don't succeed, use a Bigger Hammer").
  591. */
  592. STATIC void
  593. xfs_flush_device_work(
  594. struct xfs_mount *mp,
  595. void *arg)
  596. {
  597. struct inode *inode = arg;
  598. sync_blockdev(mp->m_super->s_bdev);
  599. iput(inode);
  600. }
  601. void
  602. xfs_flush_device(
  603. xfs_inode_t *ip)
  604. {
  605. struct inode *inode = VFS_I(ip);
  606. igrab(inode);
  607. xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_device_work);
  608. delay(msecs_to_jiffies(500));
  609. xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
  610. }
  611. STATIC void
  612. xfs_sync_worker(
  613. struct xfs_mount *mp,
  614. void *unused)
  615. {
  616. int error;
  617. if (!(mp->m_flags & XFS_MOUNT_RDONLY))
  618. error = xfs_sync(mp, SYNC_FSDATA | SYNC_BDFLUSH | SYNC_ATTR);
  619. mp->m_sync_seq++;
  620. wake_up(&mp->m_wait_single_sync_task);
  621. }
  622. STATIC int
  623. xfssyncd(
  624. void *arg)
  625. {
  626. struct xfs_mount *mp = arg;
  627. long timeleft;
  628. bhv_vfs_sync_work_t *work, *n;
  629. LIST_HEAD (tmp);
  630. set_freezable();
  631. timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
  632. for (;;) {
  633. timeleft = schedule_timeout_interruptible(timeleft);
  634. /* swsusp */
  635. try_to_freeze();
  636. if (kthread_should_stop() && list_empty(&mp->m_sync_list))
  637. break;
  638. spin_lock(&mp->m_sync_lock);
  639. /*
  640. * We can get woken by laptop mode, to do a sync -
  641. * that's the (only!) case where the list would be
  642. * empty with time remaining.
  643. */
  644. if (!timeleft || list_empty(&mp->m_sync_list)) {
  645. if (!timeleft)
  646. timeleft = xfs_syncd_centisecs *
  647. msecs_to_jiffies(10);
  648. INIT_LIST_HEAD(&mp->m_sync_work.w_list);
  649. list_add_tail(&mp->m_sync_work.w_list,
  650. &mp->m_sync_list);
  651. }
  652. list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
  653. list_move(&work->w_list, &tmp);
  654. spin_unlock(&mp->m_sync_lock);
  655. list_for_each_entry_safe(work, n, &tmp, w_list) {
  656. (*work->w_syncer)(mp, work->w_data);
  657. list_del(&work->w_list);
  658. if (work == &mp->m_sync_work)
  659. continue;
  660. kmem_free(work);
  661. }
  662. }
  663. return 0;
  664. }
  665. int
  666. xfs_syncd_init(
  667. struct xfs_mount *mp)
  668. {
  669. mp->m_sync_work.w_syncer = xfs_sync_worker;
  670. mp->m_sync_work.w_mount = mp;
  671. mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
  672. if (IS_ERR(mp->m_sync_task))
  673. return -PTR_ERR(mp->m_sync_task);
  674. return 0;
  675. }
  676. void
  677. xfs_syncd_stop(
  678. struct xfs_mount *mp)
  679. {
  680. kthread_stop(mp->m_sync_task);
  681. }