xfs_vfsops.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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_da_btree.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_alloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_inode_item.h"
  39. #include "xfs_btree.h"
  40. #include "xfs_alloc.h"
  41. #include "xfs_ialloc.h"
  42. #include "xfs_quota.h"
  43. #include "xfs_error.h"
  44. #include "xfs_bmap.h"
  45. #include "xfs_rw.h"
  46. #include "xfs_buf_item.h"
  47. #include "xfs_log_priv.h"
  48. #include "xfs_dir2_trace.h"
  49. #include "xfs_extfree_item.h"
  50. #include "xfs_acl.h"
  51. #include "xfs_attr.h"
  52. #include "xfs_clnt.h"
  53. #include "xfs_mru_cache.h"
  54. #include "xfs_filestream.h"
  55. #include "xfs_fsops.h"
  56. #include "xfs_vnodeops.h"
  57. #include "xfs_vfsops.h"
  58. #include "xfs_utils.h"
  59. STATIC void
  60. xfs_quiesce_fs(
  61. xfs_mount_t *mp)
  62. {
  63. int count = 0, pincount;
  64. xfs_flush_buftarg(mp->m_ddev_targp, 0);
  65. xfs_finish_reclaim_all(mp, 0);
  66. /* This loop must run at least twice.
  67. * The first instance of the loop will flush
  68. * most meta data but that will generate more
  69. * meta data (typically directory updates).
  70. * Which then must be flushed and logged before
  71. * we can write the unmount record.
  72. */
  73. do {
  74. xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL);
  75. pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
  76. if (!pincount) {
  77. delay(50);
  78. count++;
  79. }
  80. } while (count < 2);
  81. }
  82. /*
  83. * Second stage of a quiesce. The data is already synced, now we have to take
  84. * care of the metadata. New transactions are already blocked, so we need to
  85. * wait for any remaining transactions to drain out before proceding.
  86. */
  87. void
  88. xfs_attr_quiesce(
  89. xfs_mount_t *mp)
  90. {
  91. int error = 0;
  92. /* wait for all modifications to complete */
  93. while (atomic_read(&mp->m_active_trans) > 0)
  94. delay(100);
  95. /* flush inodes and push all remaining buffers out to disk */
  96. xfs_quiesce_fs(mp);
  97. ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
  98. /* Push the superblock and write an unmount record */
  99. error = xfs_log_sbcount(mp, 1);
  100. if (error)
  101. xfs_fs_cmn_err(CE_WARN, mp,
  102. "xfs_attr_quiesce: failed to log sb changes. "
  103. "Frozen image may not be consistent.");
  104. xfs_log_unmount_write(mp);
  105. xfs_unmountfs_writesb(mp);
  106. }
  107. /*
  108. * xfs_unmount_flush implements a set of flush operation on special
  109. * inodes, which are needed as a separate set of operations so that
  110. * they can be called as part of relocation process.
  111. */
  112. int
  113. xfs_unmount_flush(
  114. xfs_mount_t *mp, /* Mount structure we are getting
  115. rid of. */
  116. int relocation) /* Called from vfs relocation. */
  117. {
  118. xfs_inode_t *rip = mp->m_rootip;
  119. xfs_inode_t *rbmip;
  120. xfs_inode_t *rsumip = NULL;
  121. bhv_vnode_t *rvp = VFS_I(rip);
  122. int error;
  123. xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  124. xfs_iflock(rip);
  125. /*
  126. * Flush out the real time inodes.
  127. */
  128. if ((rbmip = mp->m_rbmip) != NULL) {
  129. xfs_ilock(rbmip, XFS_ILOCK_EXCL);
  130. xfs_iflock(rbmip);
  131. error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC);
  132. xfs_iunlock(rbmip, XFS_ILOCK_EXCL);
  133. if (error == EFSCORRUPTED)
  134. goto fscorrupt_out;
  135. ASSERT(vn_count(VFS_I(rbmip)) == 1);
  136. rsumip = mp->m_rsumip;
  137. xfs_ilock(rsumip, XFS_ILOCK_EXCL);
  138. xfs_iflock(rsumip);
  139. error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC);
  140. xfs_iunlock(rsumip, XFS_ILOCK_EXCL);
  141. if (error == EFSCORRUPTED)
  142. goto fscorrupt_out;
  143. ASSERT(vn_count(VFS_I(rsumip)) == 1);
  144. }
  145. /*
  146. * Synchronously flush root inode to disk
  147. */
  148. error = xfs_iflush(rip, XFS_IFLUSH_SYNC);
  149. if (error == EFSCORRUPTED)
  150. goto fscorrupt_out2;
  151. if (vn_count(rvp) != 1 && !relocation) {
  152. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  153. return XFS_ERROR(EBUSY);
  154. }
  155. /*
  156. * Release dquot that rootinode, rbmino and rsumino might be holding,
  157. * flush and purge the quota inodes.
  158. */
  159. error = XFS_QM_UNMOUNT(mp);
  160. if (error == EFSCORRUPTED)
  161. goto fscorrupt_out2;
  162. if (rbmip) {
  163. IRELE(rbmip);
  164. IRELE(rsumip);
  165. }
  166. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  167. return 0;
  168. fscorrupt_out:
  169. xfs_ifunlock(rip);
  170. fscorrupt_out2:
  171. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  172. return XFS_ERROR(EFSCORRUPTED);
  173. }
  174. /*
  175. * xfs_sync flushes any pending I/O to file system vfsp.
  176. *
  177. * This routine is called by vfs_sync() to make sure that things make it
  178. * out to disk eventually, on sync() system calls to flush out everything,
  179. * and when the file system is unmounted. For the vfs_sync() case, all
  180. * we really need to do is sync out the log to make all of our meta-data
  181. * updates permanent (except for timestamps). For calls from pflushd(),
  182. * dirty pages are kept moving by calling pdflush() on the inodes
  183. * containing them. We also flush the inodes that we can lock without
  184. * sleeping and the superblock if we can lock it without sleeping from
  185. * vfs_sync() so that items at the tail of the log are always moving out.
  186. *
  187. * Flags:
  188. * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
  189. * to sleep if we can help it. All we really need
  190. * to do is ensure that the log is synced at least
  191. * periodically. We also push the inodes and
  192. * superblock if we can lock them without sleeping
  193. * and they are not pinned.
  194. * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
  195. * set, then we really want to lock each inode and flush
  196. * it.
  197. * SYNC_WAIT - All the flushes that take place in this call should
  198. * be synchronous.
  199. * SYNC_DELWRI - This tells us to push dirty pages associated with
  200. * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
  201. * determine if they should be flushed sync, async, or
  202. * delwri.
  203. * SYNC_CLOSE - This flag is passed when the system is being
  204. * unmounted. We should sync and invalidate everything.
  205. * SYNC_FSDATA - This indicates that the caller would like to make
  206. * sure the superblock is safe on disk. We can ensure
  207. * this by simply making sure the log gets flushed
  208. * if SYNC_BDFLUSH is set, and by actually writing it
  209. * out otherwise.
  210. * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
  211. * before we return (including direct I/O). Forms the drain
  212. * side of the write barrier needed to safely quiesce the
  213. * filesystem.
  214. *
  215. */
  216. int
  217. xfs_sync(
  218. xfs_mount_t *mp,
  219. int flags)
  220. {
  221. int error;
  222. /*
  223. * Get the Quota Manager to flush the dquots.
  224. *
  225. * If XFS quota support is not enabled or this filesystem
  226. * instance does not use quotas XFS_QM_DQSYNC will always
  227. * return zero.
  228. */
  229. error = XFS_QM_DQSYNC(mp, flags);
  230. if (error) {
  231. /*
  232. * If we got an IO error, we will be shutting down.
  233. * So, there's nothing more for us to do here.
  234. */
  235. ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
  236. if (XFS_FORCED_SHUTDOWN(mp))
  237. return XFS_ERROR(error);
  238. }
  239. if (flags & SYNC_IOWAIT)
  240. xfs_filestream_flush(mp);
  241. return xfs_syncsub(mp, flags, NULL);
  242. }
  243. /*
  244. * xfs sync routine for internal use
  245. *
  246. * This routine supports all of the flags defined for the generic vfs_sync
  247. * interface as explained above under xfs_sync.
  248. *
  249. */
  250. int
  251. xfs_sync_inodes(
  252. xfs_mount_t *mp,
  253. int flags,
  254. int *bypassed)
  255. {
  256. xfs_inode_t *ip = NULL;
  257. bhv_vnode_t *vp = NULL;
  258. int error;
  259. int last_error;
  260. uint64_t fflag;
  261. uint lock_flags;
  262. uint base_lock_flags;
  263. boolean_t mount_locked;
  264. boolean_t vnode_refed;
  265. int preempt;
  266. xfs_iptr_t *ipointer;
  267. #ifdef DEBUG
  268. boolean_t ipointer_in = B_FALSE;
  269. #define IPOINTER_SET ipointer_in = B_TRUE
  270. #define IPOINTER_CLR ipointer_in = B_FALSE
  271. #else
  272. #define IPOINTER_SET
  273. #define IPOINTER_CLR
  274. #endif
  275. /* Insert a marker record into the inode list after inode ip. The list
  276. * must be locked when this is called. After the call the list will no
  277. * longer be locked.
  278. */
  279. #define IPOINTER_INSERT(ip, mp) { \
  280. ASSERT(ipointer_in == B_FALSE); \
  281. ipointer->ip_mnext = ip->i_mnext; \
  282. ipointer->ip_mprev = ip; \
  283. ip->i_mnext = (xfs_inode_t *)ipointer; \
  284. ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
  285. preempt = 0; \
  286. XFS_MOUNT_IUNLOCK(mp); \
  287. mount_locked = B_FALSE; \
  288. IPOINTER_SET; \
  289. }
  290. /* Remove the marker from the inode list. If the marker was the only item
  291. * in the list then there are no remaining inodes and we should zero out
  292. * the whole list. If we are the current head of the list then move the head
  293. * past us.
  294. */
  295. #define IPOINTER_REMOVE(ip, mp) { \
  296. ASSERT(ipointer_in == B_TRUE); \
  297. if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
  298. ip = ipointer->ip_mnext; \
  299. ip->i_mprev = ipointer->ip_mprev; \
  300. ipointer->ip_mprev->i_mnext = ip; \
  301. if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
  302. mp->m_inodes = ip; \
  303. } \
  304. } else { \
  305. ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
  306. mp->m_inodes = NULL; \
  307. ip = NULL; \
  308. } \
  309. IPOINTER_CLR; \
  310. }
  311. #define XFS_PREEMPT_MASK 0x7f
  312. ASSERT(!(flags & SYNC_BDFLUSH));
  313. if (bypassed)
  314. *bypassed = 0;
  315. if (mp->m_flags & XFS_MOUNT_RDONLY)
  316. return 0;
  317. error = 0;
  318. last_error = 0;
  319. preempt = 0;
  320. /* Allocate a reference marker */
  321. ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
  322. fflag = XFS_B_ASYNC; /* default is don't wait */
  323. if (flags & SYNC_DELWRI)
  324. fflag = XFS_B_DELWRI;
  325. if (flags & SYNC_WAIT)
  326. fflag = 0; /* synchronous overrides all */
  327. base_lock_flags = XFS_ILOCK_SHARED;
  328. if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
  329. /*
  330. * We need the I/O lock if we're going to call any of
  331. * the flush/inval routines.
  332. */
  333. base_lock_flags |= XFS_IOLOCK_SHARED;
  334. }
  335. XFS_MOUNT_ILOCK(mp);
  336. ip = mp->m_inodes;
  337. mount_locked = B_TRUE;
  338. vnode_refed = B_FALSE;
  339. IPOINTER_CLR;
  340. do {
  341. ASSERT(ipointer_in == B_FALSE);
  342. ASSERT(vnode_refed == B_FALSE);
  343. lock_flags = base_lock_flags;
  344. /*
  345. * There were no inodes in the list, just break out
  346. * of the loop.
  347. */
  348. if (ip == NULL) {
  349. break;
  350. }
  351. /*
  352. * We found another sync thread marker - skip it
  353. */
  354. if (ip->i_mount == NULL) {
  355. ip = ip->i_mnext;
  356. continue;
  357. }
  358. vp = VFS_I(ip);
  359. /*
  360. * If the vnode is gone then this is being torn down,
  361. * call reclaim if it is flushed, else let regular flush
  362. * code deal with it later in the loop.
  363. */
  364. if (vp == NULL) {
  365. /* Skip ones already in reclaim */
  366. if (ip->i_flags & XFS_IRECLAIM) {
  367. ip = ip->i_mnext;
  368. continue;
  369. }
  370. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
  371. ip = ip->i_mnext;
  372. } else if ((xfs_ipincount(ip) == 0) &&
  373. xfs_iflock_nowait(ip)) {
  374. IPOINTER_INSERT(ip, mp);
  375. xfs_finish_reclaim(ip, 1,
  376. XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  377. XFS_MOUNT_ILOCK(mp);
  378. mount_locked = B_TRUE;
  379. IPOINTER_REMOVE(ip, mp);
  380. } else {
  381. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  382. ip = ip->i_mnext;
  383. }
  384. continue;
  385. }
  386. if (VN_BAD(vp)) {
  387. ip = ip->i_mnext;
  388. continue;
  389. }
  390. if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
  391. XFS_MOUNT_IUNLOCK(mp);
  392. kmem_free(ipointer);
  393. return 0;
  394. }
  395. /*
  396. * Try to lock without sleeping. We're out of order with
  397. * the inode list lock here, so if we fail we need to drop
  398. * the mount lock and try again. If we're called from
  399. * bdflush() here, then don't bother.
  400. *
  401. * The inode lock here actually coordinates with the
  402. * almost spurious inode lock in xfs_ireclaim() to prevent
  403. * the vnode we handle here without a reference from
  404. * being freed while we reference it. If we lock the inode
  405. * while it's on the mount list here, then the spurious inode
  406. * lock in xfs_ireclaim() after the inode is pulled from
  407. * the mount list will sleep until we release it here.
  408. * This keeps the vnode from being freed while we reference
  409. * it.
  410. */
  411. if (xfs_ilock_nowait(ip, lock_flags) == 0) {
  412. if (vp == NULL) {
  413. ip = ip->i_mnext;
  414. continue;
  415. }
  416. vp = vn_grab(vp);
  417. if (vp == NULL) {
  418. ip = ip->i_mnext;
  419. continue;
  420. }
  421. IPOINTER_INSERT(ip, mp);
  422. xfs_ilock(ip, lock_flags);
  423. ASSERT(vp == VFS_I(ip));
  424. ASSERT(ip->i_mount == mp);
  425. vnode_refed = B_TRUE;
  426. }
  427. /* From here on in the loop we may have a marker record
  428. * in the inode list.
  429. */
  430. /*
  431. * If we have to flush data or wait for I/O completion
  432. * we need to drop the ilock that we currently hold.
  433. * If we need to drop the lock, insert a marker if we
  434. * have not already done so.
  435. */
  436. if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
  437. ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
  438. if (mount_locked) {
  439. IPOINTER_INSERT(ip, mp);
  440. }
  441. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  442. if (flags & SYNC_CLOSE) {
  443. /* Shutdown case. Flush and invalidate. */
  444. if (XFS_FORCED_SHUTDOWN(mp))
  445. xfs_tosspages(ip, 0, -1,
  446. FI_REMAPF);
  447. else
  448. error = xfs_flushinval_pages(ip,
  449. 0, -1, FI_REMAPF);
  450. } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
  451. error = xfs_flush_pages(ip, 0,
  452. -1, fflag, FI_NONE);
  453. }
  454. /*
  455. * When freezing, we need to wait ensure all I/O (including direct
  456. * I/O) is complete to ensure no further data modification can take
  457. * place after this point
  458. */
  459. if (flags & SYNC_IOWAIT)
  460. vn_iowait(ip);
  461. xfs_ilock(ip, XFS_ILOCK_SHARED);
  462. }
  463. if ((flags & SYNC_ATTR) &&
  464. (ip->i_update_core ||
  465. (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
  466. if (mount_locked)
  467. IPOINTER_INSERT(ip, mp);
  468. if (flags & SYNC_WAIT) {
  469. xfs_iflock(ip);
  470. error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
  471. /*
  472. * If we can't acquire the flush lock, then the inode
  473. * is already being flushed so don't bother waiting.
  474. *
  475. * If we can lock it then do a delwri flush so we can
  476. * combine multiple inode flushes in each disk write.
  477. */
  478. } else if (xfs_iflock_nowait(ip)) {
  479. error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
  480. } else if (bypassed) {
  481. (*bypassed)++;
  482. }
  483. }
  484. if (lock_flags != 0) {
  485. xfs_iunlock(ip, lock_flags);
  486. }
  487. if (vnode_refed) {
  488. /*
  489. * If we had to take a reference on the vnode
  490. * above, then wait until after we've unlocked
  491. * the inode to release the reference. This is
  492. * because we can be already holding the inode
  493. * lock when IRELE() calls xfs_inactive().
  494. *
  495. * Make sure to drop the mount lock before calling
  496. * IRELE() so that we don't trip over ourselves if
  497. * we have to go for the mount lock again in the
  498. * inactive code.
  499. */
  500. if (mount_locked) {
  501. IPOINTER_INSERT(ip, mp);
  502. }
  503. IRELE(ip);
  504. vnode_refed = B_FALSE;
  505. }
  506. if (error) {
  507. last_error = error;
  508. }
  509. /*
  510. * bail out if the filesystem is corrupted.
  511. */
  512. if (error == EFSCORRUPTED) {
  513. if (!mount_locked) {
  514. XFS_MOUNT_ILOCK(mp);
  515. IPOINTER_REMOVE(ip, mp);
  516. }
  517. XFS_MOUNT_IUNLOCK(mp);
  518. ASSERT(ipointer_in == B_FALSE);
  519. kmem_free(ipointer);
  520. return XFS_ERROR(error);
  521. }
  522. /* Let other threads have a chance at the mount lock
  523. * if we have looped many times without dropping the
  524. * lock.
  525. */
  526. if ((++preempt & XFS_PREEMPT_MASK) == 0) {
  527. if (mount_locked) {
  528. IPOINTER_INSERT(ip, mp);
  529. }
  530. }
  531. if (mount_locked == B_FALSE) {
  532. XFS_MOUNT_ILOCK(mp);
  533. mount_locked = B_TRUE;
  534. IPOINTER_REMOVE(ip, mp);
  535. continue;
  536. }
  537. ASSERT(ipointer_in == B_FALSE);
  538. ip = ip->i_mnext;
  539. } while (ip != mp->m_inodes);
  540. XFS_MOUNT_IUNLOCK(mp);
  541. ASSERT(ipointer_in == B_FALSE);
  542. kmem_free(ipointer);
  543. return XFS_ERROR(last_error);
  544. }
  545. /*
  546. * xfs sync routine for internal use
  547. *
  548. * This routine supports all of the flags defined for the generic vfs_sync
  549. * interface as explained above under xfs_sync.
  550. *
  551. */
  552. int
  553. xfs_syncsub(
  554. xfs_mount_t *mp,
  555. int flags,
  556. int *bypassed)
  557. {
  558. int error = 0;
  559. int last_error = 0;
  560. uint log_flags = XFS_LOG_FORCE;
  561. xfs_buf_t *bp;
  562. xfs_buf_log_item_t *bip;
  563. /*
  564. * Sync out the log. This ensures that the log is periodically
  565. * flushed even if there is not enough activity to fill it up.
  566. */
  567. if (flags & SYNC_WAIT)
  568. log_flags |= XFS_LOG_SYNC;
  569. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  570. if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
  571. if (flags & SYNC_BDFLUSH)
  572. xfs_finish_reclaim_all(mp, 1);
  573. else
  574. error = xfs_sync_inodes(mp, flags, bypassed);
  575. }
  576. /*
  577. * Flushing out dirty data above probably generated more
  578. * log activity, so if this isn't vfs_sync() then flush
  579. * the log again.
  580. */
  581. if (flags & SYNC_DELWRI) {
  582. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  583. }
  584. if (flags & SYNC_FSDATA) {
  585. /*
  586. * If this is vfs_sync() then only sync the superblock
  587. * if we can lock it without sleeping and it is not pinned.
  588. */
  589. if (flags & SYNC_BDFLUSH) {
  590. bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
  591. if (bp != NULL) {
  592. bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
  593. if ((bip != NULL) &&
  594. xfs_buf_item_dirty(bip)) {
  595. if (!(XFS_BUF_ISPINNED(bp))) {
  596. XFS_BUF_ASYNC(bp);
  597. error = xfs_bwrite(mp, bp);
  598. } else {
  599. xfs_buf_relse(bp);
  600. }
  601. } else {
  602. xfs_buf_relse(bp);
  603. }
  604. }
  605. } else {
  606. bp = xfs_getsb(mp, 0);
  607. /*
  608. * If the buffer is pinned then push on the log so
  609. * we won't get stuck waiting in the write for
  610. * someone, maybe ourselves, to flush the log.
  611. * Even though we just pushed the log above, we
  612. * did not have the superblock buffer locked at
  613. * that point so it can become pinned in between
  614. * there and here.
  615. */
  616. if (XFS_BUF_ISPINNED(bp))
  617. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  618. if (flags & SYNC_WAIT)
  619. XFS_BUF_UNASYNC(bp);
  620. else
  621. XFS_BUF_ASYNC(bp);
  622. error = xfs_bwrite(mp, bp);
  623. }
  624. if (error) {
  625. last_error = error;
  626. }
  627. }
  628. /*
  629. * Now check to see if the log needs a "dummy" transaction.
  630. */
  631. if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
  632. xfs_trans_t *tp;
  633. xfs_inode_t *ip;
  634. /*
  635. * Put a dummy transaction in the log to tell
  636. * recovery that all others are OK.
  637. */
  638. tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
  639. if ((error = xfs_trans_reserve(tp, 0,
  640. XFS_ICHANGE_LOG_RES(mp),
  641. 0, 0, 0))) {
  642. xfs_trans_cancel(tp, 0);
  643. return error;
  644. }
  645. ip = mp->m_rootip;
  646. xfs_ilock(ip, XFS_ILOCK_EXCL);
  647. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  648. xfs_trans_ihold(tp, ip);
  649. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  650. error = xfs_trans_commit(tp, 0);
  651. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  652. xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
  653. }
  654. /*
  655. * When shutting down, we need to insure that the AIL is pushed
  656. * to disk or the filesystem can appear corrupt from the PROM.
  657. */
  658. if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
  659. XFS_bflush(mp->m_ddev_targp);
  660. if (mp->m_rtdev_targp) {
  661. XFS_bflush(mp->m_rtdev_targp);
  662. }
  663. }
  664. return XFS_ERROR(last_error);
  665. }