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