xfs_vfsops.c 22 KB

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