xfs_sync.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. /*
  47. * xfs_sync flushes any pending I/O to file system vfsp.
  48. *
  49. * This routine is called by vfs_sync() to make sure that things make it
  50. * out to disk eventually, on sync() system calls to flush out everything,
  51. * and when the file system is unmounted. For the vfs_sync() case, all
  52. * we really need to do is sync out the log to make all of our meta-data
  53. * updates permanent (except for timestamps). For calls from pflushd(),
  54. * dirty pages are kept moving by calling pdflush() on the inodes
  55. * containing them. We also flush the inodes that we can lock without
  56. * sleeping and the superblock if we can lock it without sleeping from
  57. * vfs_sync() so that items at the tail of the log are always moving out.
  58. *
  59. * Flags:
  60. * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
  61. * to sleep if we can help it. All we really need
  62. * to do is ensure that the log is synced at least
  63. * periodically. We also push the inodes and
  64. * superblock if we can lock them without sleeping
  65. * and they are not pinned.
  66. * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
  67. * set, then we really want to lock each inode and flush
  68. * it.
  69. * SYNC_WAIT - All the flushes that take place in this call should
  70. * be synchronous.
  71. * SYNC_DELWRI - This tells us to push dirty pages associated with
  72. * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
  73. * determine if they should be flushed sync, async, or
  74. * delwri.
  75. * SYNC_CLOSE - This flag is passed when the system is being
  76. * unmounted. We should sync and invalidate everything.
  77. * SYNC_FSDATA - This indicates that the caller would like to make
  78. * sure the superblock is safe on disk. We can ensure
  79. * this by simply making sure the log gets flushed
  80. * if SYNC_BDFLUSH is set, and by actually writing it
  81. * out otherwise.
  82. * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
  83. * before we return (including direct I/O). Forms the drain
  84. * side of the write barrier needed to safely quiesce the
  85. * filesystem.
  86. *
  87. */
  88. int
  89. xfs_sync(
  90. xfs_mount_t *mp,
  91. int flags)
  92. {
  93. int error;
  94. /*
  95. * Get the Quota Manager to flush the dquots.
  96. *
  97. * If XFS quota support is not enabled or this filesystem
  98. * instance does not use quotas XFS_QM_DQSYNC will always
  99. * return zero.
  100. */
  101. error = XFS_QM_DQSYNC(mp, flags);
  102. if (error) {
  103. /*
  104. * If we got an IO error, we will be shutting down.
  105. * So, there's nothing more for us to do here.
  106. */
  107. ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
  108. if (XFS_FORCED_SHUTDOWN(mp))
  109. return XFS_ERROR(error);
  110. }
  111. if (flags & SYNC_IOWAIT)
  112. xfs_filestream_flush(mp);
  113. return xfs_syncsub(mp, flags, NULL);
  114. }
  115. /*
  116. * xfs sync routine for internal use
  117. *
  118. * This routine supports all of the flags defined for the generic vfs_sync
  119. * interface as explained above under xfs_sync.
  120. *
  121. */
  122. int
  123. xfs_sync_inodes(
  124. xfs_mount_t *mp,
  125. int flags,
  126. int *bypassed)
  127. {
  128. xfs_inode_t *ip = NULL;
  129. struct inode *vp = NULL;
  130. int error;
  131. int last_error;
  132. uint64_t fflag;
  133. uint lock_flags;
  134. uint base_lock_flags;
  135. boolean_t mount_locked;
  136. boolean_t vnode_refed;
  137. int preempt;
  138. xfs_iptr_t *ipointer;
  139. #ifdef DEBUG
  140. boolean_t ipointer_in = B_FALSE;
  141. #define IPOINTER_SET ipointer_in = B_TRUE
  142. #define IPOINTER_CLR ipointer_in = B_FALSE
  143. #else
  144. #define IPOINTER_SET
  145. #define IPOINTER_CLR
  146. #endif
  147. /* Insert a marker record into the inode list after inode ip. The list
  148. * must be locked when this is called. After the call the list will no
  149. * longer be locked.
  150. */
  151. #define IPOINTER_INSERT(ip, mp) { \
  152. ASSERT(ipointer_in == B_FALSE); \
  153. ipointer->ip_mnext = ip->i_mnext; \
  154. ipointer->ip_mprev = ip; \
  155. ip->i_mnext = (xfs_inode_t *)ipointer; \
  156. ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
  157. preempt = 0; \
  158. XFS_MOUNT_IUNLOCK(mp); \
  159. mount_locked = B_FALSE; \
  160. IPOINTER_SET; \
  161. }
  162. /* Remove the marker from the inode list. If the marker was the only item
  163. * in the list then there are no remaining inodes and we should zero out
  164. * the whole list. If we are the current head of the list then move the head
  165. * past us.
  166. */
  167. #define IPOINTER_REMOVE(ip, mp) { \
  168. ASSERT(ipointer_in == B_TRUE); \
  169. if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
  170. ip = ipointer->ip_mnext; \
  171. ip->i_mprev = ipointer->ip_mprev; \
  172. ipointer->ip_mprev->i_mnext = ip; \
  173. if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
  174. mp->m_inodes = ip; \
  175. } \
  176. } else { \
  177. ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
  178. mp->m_inodes = NULL; \
  179. ip = NULL; \
  180. } \
  181. IPOINTER_CLR; \
  182. }
  183. #define XFS_PREEMPT_MASK 0x7f
  184. ASSERT(!(flags & SYNC_BDFLUSH));
  185. if (bypassed)
  186. *bypassed = 0;
  187. if (mp->m_flags & XFS_MOUNT_RDONLY)
  188. return 0;
  189. error = 0;
  190. last_error = 0;
  191. preempt = 0;
  192. /* Allocate a reference marker */
  193. ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
  194. fflag = XFS_B_ASYNC; /* default is don't wait */
  195. if (flags & SYNC_DELWRI)
  196. fflag = XFS_B_DELWRI;
  197. if (flags & SYNC_WAIT)
  198. fflag = 0; /* synchronous overrides all */
  199. base_lock_flags = XFS_ILOCK_SHARED;
  200. if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
  201. /*
  202. * We need the I/O lock if we're going to call any of
  203. * the flush/inval routines.
  204. */
  205. base_lock_flags |= XFS_IOLOCK_SHARED;
  206. }
  207. XFS_MOUNT_ILOCK(mp);
  208. ip = mp->m_inodes;
  209. mount_locked = B_TRUE;
  210. vnode_refed = B_FALSE;
  211. IPOINTER_CLR;
  212. do {
  213. ASSERT(ipointer_in == B_FALSE);
  214. ASSERT(vnode_refed == B_FALSE);
  215. lock_flags = base_lock_flags;
  216. /*
  217. * There were no inodes in the list, just break out
  218. * of the loop.
  219. */
  220. if (ip == NULL) {
  221. break;
  222. }
  223. /*
  224. * We found another sync thread marker - skip it
  225. */
  226. if (ip->i_mount == NULL) {
  227. ip = ip->i_mnext;
  228. continue;
  229. }
  230. vp = VFS_I(ip);
  231. /*
  232. * If the vnode is gone then this is being torn down,
  233. * call reclaim if it is flushed, else let regular flush
  234. * code deal with it later in the loop.
  235. */
  236. if (vp == NULL) {
  237. /* Skip ones already in reclaim */
  238. if (ip->i_flags & XFS_IRECLAIM) {
  239. ip = ip->i_mnext;
  240. continue;
  241. }
  242. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
  243. ip = ip->i_mnext;
  244. } else if ((xfs_ipincount(ip) == 0) &&
  245. xfs_iflock_nowait(ip)) {
  246. IPOINTER_INSERT(ip, mp);
  247. xfs_finish_reclaim(ip, 1,
  248. XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  249. XFS_MOUNT_ILOCK(mp);
  250. mount_locked = B_TRUE;
  251. IPOINTER_REMOVE(ip, mp);
  252. } else {
  253. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  254. ip = ip->i_mnext;
  255. }
  256. continue;
  257. }
  258. if (VN_BAD(vp)) {
  259. ip = ip->i_mnext;
  260. continue;
  261. }
  262. if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
  263. XFS_MOUNT_IUNLOCK(mp);
  264. kmem_free(ipointer);
  265. return 0;
  266. }
  267. /*
  268. * Try to lock without sleeping. We're out of order with
  269. * the inode list lock here, so if we fail we need to drop
  270. * the mount lock and try again. If we're called from
  271. * bdflush() here, then don't bother.
  272. *
  273. * The inode lock here actually coordinates with the
  274. * almost spurious inode lock in xfs_ireclaim() to prevent
  275. * the vnode we handle here without a reference from
  276. * being freed while we reference it. If we lock the inode
  277. * while it's on the mount list here, then the spurious inode
  278. * lock in xfs_ireclaim() after the inode is pulled from
  279. * the mount list will sleep until we release it here.
  280. * This keeps the vnode from being freed while we reference
  281. * it.
  282. */
  283. if (xfs_ilock_nowait(ip, lock_flags) == 0) {
  284. if (vp == NULL) {
  285. ip = ip->i_mnext;
  286. continue;
  287. }
  288. vp = vn_grab(vp);
  289. if (vp == NULL) {
  290. ip = ip->i_mnext;
  291. continue;
  292. }
  293. IPOINTER_INSERT(ip, mp);
  294. xfs_ilock(ip, lock_flags);
  295. ASSERT(vp == VFS_I(ip));
  296. ASSERT(ip->i_mount == mp);
  297. vnode_refed = B_TRUE;
  298. }
  299. /* From here on in the loop we may have a marker record
  300. * in the inode list.
  301. */
  302. /*
  303. * If we have to flush data or wait for I/O completion
  304. * we need to drop the ilock that we currently hold.
  305. * If we need to drop the lock, insert a marker if we
  306. * have not already done so.
  307. */
  308. if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
  309. ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
  310. if (mount_locked) {
  311. IPOINTER_INSERT(ip, mp);
  312. }
  313. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  314. if (flags & SYNC_CLOSE) {
  315. /* Shutdown case. Flush and invalidate. */
  316. if (XFS_FORCED_SHUTDOWN(mp))
  317. xfs_tosspages(ip, 0, -1,
  318. FI_REMAPF);
  319. else
  320. error = xfs_flushinval_pages(ip,
  321. 0, -1, FI_REMAPF);
  322. } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
  323. error = xfs_flush_pages(ip, 0,
  324. -1, fflag, FI_NONE);
  325. }
  326. /*
  327. * When freezing, we need to wait ensure all I/O (including direct
  328. * I/O) is complete to ensure no further data modification can take
  329. * place after this point
  330. */
  331. if (flags & SYNC_IOWAIT)
  332. vn_iowait(ip);
  333. xfs_ilock(ip, XFS_ILOCK_SHARED);
  334. }
  335. if ((flags & SYNC_ATTR) &&
  336. (ip->i_update_core ||
  337. (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
  338. if (mount_locked)
  339. IPOINTER_INSERT(ip, mp);
  340. if (flags & SYNC_WAIT) {
  341. xfs_iflock(ip);
  342. error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
  343. /*
  344. * If we can't acquire the flush lock, then the inode
  345. * is already being flushed so don't bother waiting.
  346. *
  347. * If we can lock it then do a delwri flush so we can
  348. * combine multiple inode flushes in each disk write.
  349. */
  350. } else if (xfs_iflock_nowait(ip)) {
  351. error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
  352. } else if (bypassed) {
  353. (*bypassed)++;
  354. }
  355. }
  356. if (lock_flags != 0) {
  357. xfs_iunlock(ip, lock_flags);
  358. }
  359. if (vnode_refed) {
  360. /*
  361. * If we had to take a reference on the vnode
  362. * above, then wait until after we've unlocked
  363. * the inode to release the reference. This is
  364. * because we can be already holding the inode
  365. * lock when IRELE() calls xfs_inactive().
  366. *
  367. * Make sure to drop the mount lock before calling
  368. * IRELE() so that we don't trip over ourselves if
  369. * we have to go for the mount lock again in the
  370. * inactive code.
  371. */
  372. if (mount_locked) {
  373. IPOINTER_INSERT(ip, mp);
  374. }
  375. IRELE(ip);
  376. vnode_refed = B_FALSE;
  377. }
  378. if (error) {
  379. last_error = error;
  380. }
  381. /*
  382. * bail out if the filesystem is corrupted.
  383. */
  384. if (error == EFSCORRUPTED) {
  385. if (!mount_locked) {
  386. XFS_MOUNT_ILOCK(mp);
  387. IPOINTER_REMOVE(ip, mp);
  388. }
  389. XFS_MOUNT_IUNLOCK(mp);
  390. ASSERT(ipointer_in == B_FALSE);
  391. kmem_free(ipointer);
  392. return XFS_ERROR(error);
  393. }
  394. /* Let other threads have a chance at the mount lock
  395. * if we have looped many times without dropping the
  396. * lock.
  397. */
  398. if ((++preempt & XFS_PREEMPT_MASK) == 0) {
  399. if (mount_locked) {
  400. IPOINTER_INSERT(ip, mp);
  401. }
  402. }
  403. if (mount_locked == B_FALSE) {
  404. XFS_MOUNT_ILOCK(mp);
  405. mount_locked = B_TRUE;
  406. IPOINTER_REMOVE(ip, mp);
  407. continue;
  408. }
  409. ASSERT(ipointer_in == B_FALSE);
  410. ip = ip->i_mnext;
  411. } while (ip != mp->m_inodes);
  412. XFS_MOUNT_IUNLOCK(mp);
  413. ASSERT(ipointer_in == B_FALSE);
  414. kmem_free(ipointer);
  415. return XFS_ERROR(last_error);
  416. }
  417. /*
  418. * xfs sync routine for internal use
  419. *
  420. * This routine supports all of the flags defined for the generic vfs_sync
  421. * interface as explained above under xfs_sync.
  422. *
  423. */
  424. int
  425. xfs_syncsub(
  426. xfs_mount_t *mp,
  427. int flags,
  428. int *bypassed)
  429. {
  430. int error = 0;
  431. int last_error = 0;
  432. uint log_flags = XFS_LOG_FORCE;
  433. xfs_buf_t *bp;
  434. xfs_buf_log_item_t *bip;
  435. /*
  436. * Sync out the log. This ensures that the log is periodically
  437. * flushed even if there is not enough activity to fill it up.
  438. */
  439. if (flags & SYNC_WAIT)
  440. log_flags |= XFS_LOG_SYNC;
  441. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  442. if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
  443. if (flags & SYNC_BDFLUSH)
  444. xfs_finish_reclaim_all(mp, 1);
  445. else
  446. error = xfs_sync_inodes(mp, flags, bypassed);
  447. }
  448. /*
  449. * Flushing out dirty data above probably generated more
  450. * log activity, so if this isn't vfs_sync() then flush
  451. * the log again.
  452. */
  453. if (flags & SYNC_DELWRI) {
  454. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  455. }
  456. if (flags & SYNC_FSDATA) {
  457. /*
  458. * If this is vfs_sync() then only sync the superblock
  459. * if we can lock it without sleeping and it is not pinned.
  460. */
  461. if (flags & SYNC_BDFLUSH) {
  462. bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
  463. if (bp != NULL) {
  464. bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
  465. if ((bip != NULL) &&
  466. xfs_buf_item_dirty(bip)) {
  467. if (!(XFS_BUF_ISPINNED(bp))) {
  468. XFS_BUF_ASYNC(bp);
  469. error = xfs_bwrite(mp, bp);
  470. } else {
  471. xfs_buf_relse(bp);
  472. }
  473. } else {
  474. xfs_buf_relse(bp);
  475. }
  476. }
  477. } else {
  478. bp = xfs_getsb(mp, 0);
  479. /*
  480. * If the buffer is pinned then push on the log so
  481. * we won't get stuck waiting in the write for
  482. * someone, maybe ourselves, to flush the log.
  483. * Even though we just pushed the log above, we
  484. * did not have the superblock buffer locked at
  485. * that point so it can become pinned in between
  486. * there and here.
  487. */
  488. if (XFS_BUF_ISPINNED(bp))
  489. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  490. if (flags & SYNC_WAIT)
  491. XFS_BUF_UNASYNC(bp);
  492. else
  493. XFS_BUF_ASYNC(bp);
  494. error = xfs_bwrite(mp, bp);
  495. }
  496. if (error) {
  497. last_error = error;
  498. }
  499. }
  500. /*
  501. * Now check to see if the log needs a "dummy" transaction.
  502. */
  503. if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
  504. xfs_trans_t *tp;
  505. xfs_inode_t *ip;
  506. /*
  507. * Put a dummy transaction in the log to tell
  508. * recovery that all others are OK.
  509. */
  510. tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
  511. if ((error = xfs_trans_reserve(tp, 0,
  512. XFS_ICHANGE_LOG_RES(mp),
  513. 0, 0, 0))) {
  514. xfs_trans_cancel(tp, 0);
  515. return error;
  516. }
  517. ip = mp->m_rootip;
  518. xfs_ilock(ip, XFS_ILOCK_EXCL);
  519. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  520. xfs_trans_ihold(tp, ip);
  521. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  522. error = xfs_trans_commit(tp, 0);
  523. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  524. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  525. }
  526. /*
  527. * When shutting down, we need to insure that the AIL is pushed
  528. * to disk or the filesystem can appear corrupt from the PROM.
  529. */
  530. if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
  531. XFS_bflush(mp->m_ddev_targp);
  532. if (mp->m_rtdev_targp) {
  533. XFS_bflush(mp->m_rtdev_targp);
  534. }
  535. }
  536. return XFS_ERROR(last_error);
  537. }