xfs_vfsops.c 19 KB

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