]> err.no Git - linux-2.6/blob - fs/xfs/xfs_vfsops.c
[XFS] Remove the xlog_ticket allocator
[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 }
190
191 /*
192  * xfs_start_flags
193  *
194  * This function fills in xfs_mount_t fields based on mount args.
195  * Note: the superblock has _not_ yet been read in.
196  */
197 STATIC int
198 xfs_start_flags(
199         struct xfs_mount_args   *ap,
200         struct xfs_mount        *mp)
201 {
202         /* Values are in BBs */
203         if ((ap->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
204                 /*
205                  * At this point the superblock has not been read
206                  * in, therefore we do not know the block size.
207                  * Before the mount call ends we will convert
208                  * these to FSBs.
209                  */
210                 mp->m_dalign = ap->sunit;
211                 mp->m_swidth = ap->swidth;
212         }
213
214         if (ap->logbufs != -1 &&
215             ap->logbufs != 0 &&
216             (ap->logbufs < XLOG_MIN_ICLOGS ||
217              ap->logbufs > XLOG_MAX_ICLOGS)) {
218                 cmn_err(CE_WARN,
219                         "XFS: invalid logbufs value: %d [not %d-%d]",
220                         ap->logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
221                 return XFS_ERROR(EINVAL);
222         }
223         mp->m_logbufs = ap->logbufs;
224         if (ap->logbufsize != -1 &&
225             ap->logbufsize !=  0 &&
226             (ap->logbufsize < XLOG_MIN_RECORD_BSIZE ||
227              ap->logbufsize > XLOG_MAX_RECORD_BSIZE ||
228              !is_power_of_2(ap->logbufsize))) {
229                 cmn_err(CE_WARN,
230         "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
231                         ap->logbufsize);
232                 return XFS_ERROR(EINVAL);
233         }
234         mp->m_logbsize = ap->logbufsize;
235         mp->m_fsname_len = strlen(ap->fsname) + 1;
236         mp->m_fsname = kmem_alloc(mp->m_fsname_len, KM_SLEEP);
237         strcpy(mp->m_fsname, ap->fsname);
238         if (ap->rtname[0]) {
239                 mp->m_rtname = kmem_alloc(strlen(ap->rtname) + 1, KM_SLEEP);
240                 strcpy(mp->m_rtname, ap->rtname);
241         }
242         if (ap->logname[0]) {
243                 mp->m_logname = kmem_alloc(strlen(ap->logname) + 1, KM_SLEEP);
244                 strcpy(mp->m_logname, ap->logname);
245         }
246
247         if (ap->flags & XFSMNT_WSYNC)
248                 mp->m_flags |= XFS_MOUNT_WSYNC;
249 #if XFS_BIG_INUMS
250         if (ap->flags & XFSMNT_INO64) {
251                 mp->m_flags |= XFS_MOUNT_INO64;
252                 mp->m_inoadd = XFS_INO64_OFFSET;
253         }
254 #endif
255         if (ap->flags & XFSMNT_RETERR)
256                 mp->m_flags |= XFS_MOUNT_RETERR;
257         if (ap->flags & XFSMNT_NOALIGN)
258                 mp->m_flags |= XFS_MOUNT_NOALIGN;
259         if (ap->flags & XFSMNT_SWALLOC)
260                 mp->m_flags |= XFS_MOUNT_SWALLOC;
261         if (ap->flags & XFSMNT_OSYNCISOSYNC)
262                 mp->m_flags |= XFS_MOUNT_OSYNCISOSYNC;
263         if (ap->flags & XFSMNT_32BITINODES)
264                 mp->m_flags |= XFS_MOUNT_32BITINODES;
265
266         if (ap->flags & XFSMNT_IOSIZE) {
267                 if (ap->iosizelog > XFS_MAX_IO_LOG ||
268                     ap->iosizelog < XFS_MIN_IO_LOG) {
269                         cmn_err(CE_WARN,
270                 "XFS: invalid log iosize: %d [not %d-%d]",
271                                 ap->iosizelog, XFS_MIN_IO_LOG,
272                                 XFS_MAX_IO_LOG);
273                         return XFS_ERROR(EINVAL);
274                 }
275
276                 mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
277                 mp->m_readio_log = mp->m_writeio_log = ap->iosizelog;
278         }
279
280         if (ap->flags & XFSMNT_IKEEP)
281                 mp->m_flags |= XFS_MOUNT_IKEEP;
282         if (ap->flags & XFSMNT_DIRSYNC)
283                 mp->m_flags |= XFS_MOUNT_DIRSYNC;
284         if (ap->flags & XFSMNT_ATTR2)
285                 mp->m_flags |= XFS_MOUNT_ATTR2;
286
287         if (ap->flags2 & XFSMNT2_COMPAT_IOSIZE)
288                 mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
289
290         /*
291          * no recovery flag requires a read-only mount
292          */
293         if (ap->flags & XFSMNT_NORECOVERY) {
294                 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
295                         cmn_err(CE_WARN,
296         "XFS: tried to mount a FS read-write without recovery!");
297                         return XFS_ERROR(EINVAL);
298                 }
299                 mp->m_flags |= XFS_MOUNT_NORECOVERY;
300         }
301
302         if (ap->flags & XFSMNT_NOUUID)
303                 mp->m_flags |= XFS_MOUNT_NOUUID;
304         if (ap->flags & XFSMNT_BARRIER)
305                 mp->m_flags |= XFS_MOUNT_BARRIER;
306         else
307                 mp->m_flags &= ~XFS_MOUNT_BARRIER;
308
309         if (ap->flags2 & XFSMNT2_FILESTREAMS)
310                 mp->m_flags |= XFS_MOUNT_FILESTREAMS;
311
312         if (ap->flags & XFSMNT_DMAPI)
313                 mp->m_flags |= XFS_MOUNT_DMAPI;
314         return 0;
315 }
316
317 /*
318  * This function fills in xfs_mount_t fields based on mount args.
319  * Note: the superblock _has_ now been read in.
320  */
321 STATIC int
322 xfs_finish_flags(
323         struct xfs_mount_args   *ap,
324         struct xfs_mount        *mp)
325 {
326         int                     ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
327
328         /* Fail a mount where the logbuf is smaller then the log stripe */
329         if (xfs_sb_version_haslogv2(&mp->m_sb)) {
330                 if ((ap->logbufsize <= 0) &&
331                     (mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE)) {
332                         mp->m_logbsize = mp->m_sb.sb_logsunit;
333                 } else if (ap->logbufsize > 0 &&
334                            ap->logbufsize < mp->m_sb.sb_logsunit) {
335                         cmn_err(CE_WARN,
336         "XFS: logbuf size must be greater than or equal to log stripe size");
337                         return XFS_ERROR(EINVAL);
338                 }
339         } else {
340                 /* Fail a mount if the logbuf is larger than 32K */
341                 if (ap->logbufsize > XLOG_BIG_RECORD_BSIZE) {
342                         cmn_err(CE_WARN,
343         "XFS: logbuf size for version 1 logs must be 16K or 32K");
344                         return XFS_ERROR(EINVAL);
345                 }
346         }
347
348         if (xfs_sb_version_hasattr2(&mp->m_sb))
349                 mp->m_flags |= XFS_MOUNT_ATTR2;
350
351         /*
352          * prohibit r/w mounts of read-only filesystems
353          */
354         if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
355                 cmn_err(CE_WARN,
356         "XFS: cannot mount a read-only filesystem as read-write");
357                 return XFS_ERROR(EROFS);
358         }
359
360         /*
361          * check for shared mount.
362          */
363         if (ap->flags & XFSMNT_SHARED) {
364                 if (!xfs_sb_version_hasshared(&mp->m_sb))
365                         return XFS_ERROR(EINVAL);
366
367                 /*
368                  * For IRIX 6.5, shared mounts must have the shared
369                  * version bit set, have the persistent readonly
370                  * field set, must be version 0 and can only be mounted
371                  * read-only.
372                  */
373                 if (!ronly || !(mp->m_sb.sb_flags & XFS_SBF_READONLY) ||
374                      (mp->m_sb.sb_shared_vn != 0))
375                         return XFS_ERROR(EINVAL);
376
377                 mp->m_flags |= XFS_MOUNT_SHARED;
378
379                 /*
380                  * Shared XFS V0 can't deal with DMI.  Return EINVAL.
381                  */
382                 if (mp->m_sb.sb_shared_vn == 0 && (ap->flags & XFSMNT_DMAPI))
383                         return XFS_ERROR(EINVAL);
384         }
385
386         if (ap->flags & XFSMNT_UQUOTA) {
387                 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
388                 if (ap->flags & XFSMNT_UQUOTAENF)
389                         mp->m_qflags |= XFS_UQUOTA_ENFD;
390         }
391
392         if (ap->flags & XFSMNT_GQUOTA) {
393                 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
394                 if (ap->flags & XFSMNT_GQUOTAENF)
395                         mp->m_qflags |= XFS_OQUOTA_ENFD;
396         } else if (ap->flags & XFSMNT_PQUOTA) {
397                 mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
398                 if (ap->flags & XFSMNT_PQUOTAENF)
399                         mp->m_qflags |= XFS_OQUOTA_ENFD;
400         }
401
402         return 0;
403 }
404
405 /*
406  * xfs_mount
407  *
408  * The file system configurations are:
409  *      (1) device (partition) with data and internal log
410  *      (2) logical volume with data and log subvolumes.
411  *      (3) logical volume with data, log, and realtime subvolumes.
412  *
413  * We only have to handle opening the log and realtime volumes here if
414  * they are present.  The data subvolume has already been opened by
415  * get_sb_bdev() and is stored in vfsp->vfs_super->s_bdev.
416  */
417 int
418 xfs_mount(
419         struct xfs_mount        *mp,
420         struct xfs_mount_args   *args,
421         cred_t                  *credp)
422 {
423         struct block_device     *ddev, *logdev, *rtdev;
424         int                     flags = 0, error;
425
426         ddev = mp->m_super->s_bdev;
427         logdev = rtdev = NULL;
428
429         error = xfs_dmops_get(mp, args);
430         if (error)
431                 return error;
432         error = xfs_qmops_get(mp, args);
433         if (error)
434                 return error;
435
436         if (args->flags & XFSMNT_QUIET)
437                 flags |= XFS_MFSI_QUIET;
438
439         /*
440          * Open real time and log devices - order is important.
441          */
442         if (args->logname[0]) {
443                 error = xfs_blkdev_get(mp, args->logname, &logdev);
444                 if (error)
445                         return error;
446         }
447         if (args->rtname[0]) {
448                 error = xfs_blkdev_get(mp, args->rtname, &rtdev);
449                 if (error) {
450                         xfs_blkdev_put(logdev);
451                         return error;
452                 }
453
454                 if (rtdev == ddev || rtdev == logdev) {
455                         cmn_err(CE_WARN,
456         "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
457                         xfs_blkdev_put(logdev);
458                         xfs_blkdev_put(rtdev);
459                         return EINVAL;
460                 }
461         }
462
463         /*
464          * Setup xfs_mount buffer target pointers
465          */
466         error = ENOMEM;
467         mp->m_ddev_targp = xfs_alloc_buftarg(ddev, 0);
468         if (!mp->m_ddev_targp) {
469                 xfs_blkdev_put(logdev);
470                 xfs_blkdev_put(rtdev);
471                 return error;
472         }
473         if (rtdev) {
474                 mp->m_rtdev_targp = xfs_alloc_buftarg(rtdev, 1);
475                 if (!mp->m_rtdev_targp) {
476                         xfs_blkdev_put(logdev);
477                         xfs_blkdev_put(rtdev);
478                         goto error0;
479                 }
480         }
481         mp->m_logdev_targp = (logdev && logdev != ddev) ?
482                                 xfs_alloc_buftarg(logdev, 1) : mp->m_ddev_targp;
483         if (!mp->m_logdev_targp) {
484                 xfs_blkdev_put(logdev);
485                 xfs_blkdev_put(rtdev);
486                 goto error0;
487         }
488
489         /*
490          * Setup flags based on mount(2) options and then the superblock
491          */
492         error = xfs_start_flags(args, mp);
493         if (error)
494                 goto error1;
495         error = xfs_readsb(mp, flags);
496         if (error)
497                 goto error1;
498         error = xfs_finish_flags(args, mp);
499         if (error)
500                 goto error2;
501
502         /*
503          * Setup xfs_mount buffer target pointers based on superblock
504          */
505         error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_blocksize,
506                                     mp->m_sb.sb_sectsize);
507         if (!error && logdev && logdev != ddev) {
508                 unsigned int    log_sector_size = BBSIZE;
509
510                 if (xfs_sb_version_hassector(&mp->m_sb))
511                         log_sector_size = mp->m_sb.sb_logsectsize;
512                 error = xfs_setsize_buftarg(mp->m_logdev_targp,
513                                             mp->m_sb.sb_blocksize,
514                                             log_sector_size);
515         }
516         if (!error && rtdev)
517                 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
518                                             mp->m_sb.sb_blocksize,
519                                             mp->m_sb.sb_sectsize);
520         if (error)
521                 goto error2;
522
523         if (mp->m_flags & XFS_MOUNT_BARRIER)
524                 xfs_mountfs_check_barriers(mp);
525
526         if ((error = xfs_filestream_mount(mp)))
527                 goto error2;
528
529         error = xfs_mountfs(mp, flags);
530         if (error)
531                 goto error2;
532
533         XFS_SEND_MOUNT(mp, DM_RIGHT_NULL, args->mtpt, args->fsname);
534
535         return 0;
536
537 error2:
538         if (mp->m_sb_bp)
539                 xfs_freesb(mp);
540 error1:
541         xfs_binval(mp->m_ddev_targp);
542         if (logdev && logdev != ddev)
543                 xfs_binval(mp->m_logdev_targp);
544         if (rtdev)
545                 xfs_binval(mp->m_rtdev_targp);
546 error0:
547         xfs_unmountfs_close(mp, credp);
548         xfs_qmops_put(mp);
549         xfs_dmops_put(mp);
550         return error;
551 }
552
553 int
554 xfs_unmount(
555         xfs_mount_t     *mp,
556         int             flags,
557         cred_t          *credp)
558 {
559         xfs_inode_t     *rip;
560         bhv_vnode_t     *rvp;
561         int             unmount_event_wanted = 0;
562         int             unmount_event_flags = 0;
563         int             xfs_unmountfs_needed = 0;
564         int             error;
565
566         rip = mp->m_rootip;
567         rvp = XFS_ITOV(rip);
568
569 #ifdef HAVE_DMAPI
570         if (mp->m_flags & XFS_MOUNT_DMAPI) {
571                 error = XFS_SEND_PREUNMOUNT(mp,
572                                 rip, DM_RIGHT_NULL, rip, DM_RIGHT_NULL,
573                                 NULL, NULL, 0, 0,
574                                 (mp->m_dmevmask & (1<<DM_EVENT_PREUNMOUNT))?
575                                         0:DM_FLAGS_UNWANTED);
576                         if (error)
577                                 return XFS_ERROR(error);
578                 unmount_event_wanted = 1;
579                 unmount_event_flags = (mp->m_dmevmask & (1<<DM_EVENT_UNMOUNT))?
580                                         0 : DM_FLAGS_UNWANTED;
581         }
582 #endif
583
584         /*
585          * Blow away any referenced inode in the filestreams cache.
586          * This can and will cause log traffic as inodes go inactive
587          * here.
588          */
589         xfs_filestream_unmount(mp);
590
591         XFS_bflush(mp->m_ddev_targp);
592         error = xfs_unmount_flush(mp, 0);
593         if (error)
594                 goto out;
595
596         ASSERT(vn_count(rvp) == 1);
597
598         /*
599          * Drop the reference count
600          */
601         IRELE(rip);
602
603         /*
604          * If we're forcing a shutdown, typically because of a media error,
605          * we want to make sure we invalidate dirty pages that belong to
606          * referenced vnodes as well.
607          */
608         if (XFS_FORCED_SHUTDOWN(mp)) {
609                 error = xfs_sync(mp, SYNC_WAIT | SYNC_CLOSE);
610                 ASSERT(error != EFSCORRUPTED);
611         }
612         xfs_unmountfs_needed = 1;
613
614 out:
615         /*      Send DMAPI event, if required.
616          *      Then do xfs_unmountfs() if needed.
617          *      Then return error (or zero).
618          */
619         if (unmount_event_wanted) {
620                 /* Note: mp structure must still exist for
621                  * XFS_SEND_UNMOUNT() call.
622                  */
623                 XFS_SEND_UNMOUNT(mp, error == 0 ? rip : NULL,
624                         DM_RIGHT_NULL, 0, error, unmount_event_flags);
625         }
626         if (xfs_unmountfs_needed) {
627                 /*
628                  * Call common unmount function to flush to disk
629                  * and free the super block buffer & mount structures.
630                  */
631                 xfs_unmountfs(mp, credp);
632                 xfs_qmops_put(mp);
633                 xfs_dmops_put(mp);
634                 kmem_free(mp, sizeof(xfs_mount_t));
635         }
636
637         return XFS_ERROR(error);
638 }
639
640 STATIC int
641 xfs_quiesce_fs(
642         xfs_mount_t             *mp)
643 {
644         int                     count = 0, pincount;
645
646         xfs_flush_buftarg(mp->m_ddev_targp, 0);
647         xfs_finish_reclaim_all(mp, 0);
648
649         /* This loop must run at least twice.
650          * The first instance of the loop will flush
651          * most meta data but that will generate more
652          * meta data (typically directory updates).
653          * Which then must be flushed and logged before
654          * we can write the unmount record.
655          */
656         do {
657                 xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL);
658                 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
659                 if (!pincount) {
660                         delay(50);
661                         count++;
662                 }
663         } while (count < 2);
664
665         return 0;
666 }
667
668 /*
669  * Second stage of a quiesce. The data is already synced, now we have to take
670  * care of the metadata. New transactions are already blocked, so we need to
671  * wait for any remaining transactions to drain out before proceding.
672  */
673 void
674 xfs_attr_quiesce(
675         xfs_mount_t     *mp)
676 {
677         /* wait for all modifications to complete */
678         while (atomic_read(&mp->m_active_trans) > 0)
679                 delay(100);
680
681         /* flush inodes and push all remaining buffers out to disk */
682         xfs_quiesce_fs(mp);
683
684         ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
685
686         /* Push the superblock and write an unmount record */
687         xfs_log_sbcount(mp, 1);
688         xfs_log_unmount_write(mp);
689         xfs_unmountfs_writesb(mp);
690 }
691
692 int
693 xfs_mntupdate(
694         struct xfs_mount                *mp,
695         int                             *flags,
696         struct xfs_mount_args           *args)
697 {
698         if (!(*flags & MS_RDONLY)) {                    /* rw/ro -> rw */
699                 if (mp->m_flags & XFS_MOUNT_RDONLY)
700                         mp->m_flags &= ~XFS_MOUNT_RDONLY;
701                 if (args->flags & XFSMNT_BARRIER) {
702                         mp->m_flags |= XFS_MOUNT_BARRIER;
703                         xfs_mountfs_check_barriers(mp);
704                 } else {
705                         mp->m_flags &= ~XFS_MOUNT_BARRIER;
706                 }
707         } else if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { /* rw -> ro */
708                 xfs_filestream_flush(mp);
709                 xfs_sync(mp, SYNC_DATA_QUIESCE);
710                 xfs_attr_quiesce(mp);
711                 mp->m_flags |= XFS_MOUNT_RDONLY;
712         }
713         return 0;
714 }
715
716 /*
717  * xfs_unmount_flush implements a set of flush operation on special
718  * inodes, which are needed as a separate set of operations so that
719  * they can be called as part of relocation process.
720  */
721 int
722 xfs_unmount_flush(
723         xfs_mount_t     *mp,            /* Mount structure we are getting
724                                            rid of. */
725         int             relocation)     /* Called from vfs relocation. */
726 {
727         xfs_inode_t     *rip = mp->m_rootip;
728         xfs_inode_t     *rbmip;
729         xfs_inode_t     *rsumip = NULL;
730         bhv_vnode_t     *rvp = XFS_ITOV(rip);
731         int             error;
732
733         xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
734         xfs_iflock(rip);
735
736         /*
737          * Flush out the real time inodes.
738          */
739         if ((rbmip = mp->m_rbmip) != NULL) {
740                 xfs_ilock(rbmip, XFS_ILOCK_EXCL);
741                 xfs_iflock(rbmip);
742                 error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC);
743                 xfs_iunlock(rbmip, XFS_ILOCK_EXCL);
744
745                 if (error == EFSCORRUPTED)
746                         goto fscorrupt_out;
747
748                 ASSERT(vn_count(XFS_ITOV(rbmip)) == 1);
749
750                 rsumip = mp->m_rsumip;
751                 xfs_ilock(rsumip, XFS_ILOCK_EXCL);
752                 xfs_iflock(rsumip);
753                 error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC);
754                 xfs_iunlock(rsumip, XFS_ILOCK_EXCL);
755
756                 if (error == EFSCORRUPTED)
757                         goto fscorrupt_out;
758
759                 ASSERT(vn_count(XFS_ITOV(rsumip)) == 1);
760         }
761
762         /*
763          * Synchronously flush root inode to disk
764          */
765         error = xfs_iflush(rip, XFS_IFLUSH_SYNC);
766         if (error == EFSCORRUPTED)
767                 goto fscorrupt_out2;
768
769         if (vn_count(rvp) != 1 && !relocation) {
770                 xfs_iunlock(rip, XFS_ILOCK_EXCL);
771                 return XFS_ERROR(EBUSY);
772         }
773
774         /*
775          * Release dquot that rootinode, rbmino and rsumino might be holding,
776          * flush and purge the quota inodes.
777          */
778         error = XFS_QM_UNMOUNT(mp);
779         if (error == EFSCORRUPTED)
780                 goto fscorrupt_out2;
781
782         if (rbmip) {
783                 IRELE(rbmip);
784                 IRELE(rsumip);
785         }
786
787         xfs_iunlock(rip, XFS_ILOCK_EXCL);
788         return 0;
789
790 fscorrupt_out:
791         xfs_ifunlock(rip);
792
793 fscorrupt_out2:
794         xfs_iunlock(rip, XFS_ILOCK_EXCL);
795
796         return XFS_ERROR(EFSCORRUPTED);
797 }
798
799 /*
800  * xfs_sync flushes any pending I/O to file system vfsp.
801  *
802  * This routine is called by vfs_sync() to make sure that things make it
803  * out to disk eventually, on sync() system calls to flush out everything,
804  * and when the file system is unmounted.  For the vfs_sync() case, all
805  * we really need to do is sync out the log to make all of our meta-data
806  * updates permanent (except for timestamps).  For calls from pflushd(),
807  * dirty pages are kept moving by calling pdflush() on the inodes
808  * containing them.  We also flush the inodes that we can lock without
809  * sleeping and the superblock if we can lock it without sleeping from
810  * vfs_sync() so that items at the tail of the log are always moving out.
811  *
812  * Flags:
813  *      SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
814  *                     to sleep if we can help it.  All we really need
815  *                     to do is ensure that the log is synced at least
816  *                     periodically.  We also push the inodes and
817  *                     superblock if we can lock them without sleeping
818  *                      and they are not pinned.
819  *      SYNC_ATTR    - We need to flush the inodes.  If SYNC_BDFLUSH is not
820  *                     set, then we really want to lock each inode and flush
821  *                     it.
822  *      SYNC_WAIT    - All the flushes that take place in this call should
823  *                     be synchronous.
824  *      SYNC_DELWRI  - This tells us to push dirty pages associated with
825  *                     inodes.  SYNC_WAIT and SYNC_BDFLUSH are used to
826  *                     determine if they should be flushed sync, async, or
827  *                     delwri.
828  *      SYNC_CLOSE   - This flag is passed when the system is being
829  *                     unmounted.  We should sync and invalidate everything.
830  *      SYNC_FSDATA  - This indicates that the caller would like to make
831  *                     sure the superblock is safe on disk.  We can ensure
832  *                     this by simply making sure the log gets flushed
833  *                     if SYNC_BDFLUSH is set, and by actually writing it
834  *                     out otherwise.
835  *      SYNC_IOWAIT  - The caller wants us to wait for all data I/O to complete
836  *                     before we return (including direct I/O). Forms the drain
837  *                     side of the write barrier needed to safely quiesce the
838  *                     filesystem.
839  *
840  */
841 int
842 xfs_sync(
843         xfs_mount_t     *mp,
844         int             flags)
845 {
846         int             error;
847
848         /*
849          * Get the Quota Manager to flush the dquots.
850          *
851          * If XFS quota support is not enabled or this filesystem
852          * instance does not use quotas XFS_QM_DQSYNC will always
853          * return zero.
854          */
855         error = XFS_QM_DQSYNC(mp, flags);
856         if (error) {
857                 /*
858                  * If we got an IO error, we will be shutting down.
859                  * So, there's nothing more for us to do here.
860                  */
861                 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
862                 if (XFS_FORCED_SHUTDOWN(mp))
863                         return XFS_ERROR(error);
864         }
865
866         if (flags & SYNC_IOWAIT)
867                 xfs_filestream_flush(mp);
868
869         return xfs_syncsub(mp, flags, NULL);
870 }
871
872 /*
873  * xfs sync routine for internal use
874  *
875  * This routine supports all of the flags defined for the generic vfs_sync
876  * interface as explained above under xfs_sync.
877  *
878  */
879 int
880 xfs_sync_inodes(
881         xfs_mount_t     *mp,
882         int             flags,
883         int             *bypassed)
884 {
885         xfs_inode_t     *ip = NULL;
886         bhv_vnode_t     *vp = NULL;
887         int             error;
888         int             last_error;
889         uint64_t        fflag;
890         uint            lock_flags;
891         uint            base_lock_flags;
892         boolean_t       mount_locked;
893         boolean_t       vnode_refed;
894         int             preempt;
895         xfs_iptr_t      *ipointer;
896 #ifdef DEBUG
897         boolean_t       ipointer_in = B_FALSE;
898
899 #define IPOINTER_SET    ipointer_in = B_TRUE
900 #define IPOINTER_CLR    ipointer_in = B_FALSE
901 #else
902 #define IPOINTER_SET
903 #define IPOINTER_CLR
904 #endif
905
906
907 /* Insert a marker record into the inode list after inode ip. The list
908  * must be locked when this is called. After the call the list will no
909  * longer be locked.
910  */
911 #define IPOINTER_INSERT(ip, mp) { \
912                 ASSERT(ipointer_in == B_FALSE); \
913                 ipointer->ip_mnext = ip->i_mnext; \
914                 ipointer->ip_mprev = ip; \
915                 ip->i_mnext = (xfs_inode_t *)ipointer; \
916                 ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
917                 preempt = 0; \
918                 XFS_MOUNT_IUNLOCK(mp); \
919                 mount_locked = B_FALSE; \
920                 IPOINTER_SET; \
921         }
922
923 /* Remove the marker from the inode list. If the marker was the only item
924  * in the list then there are no remaining inodes and we should zero out
925  * the whole list. If we are the current head of the list then move the head
926  * past us.
927  */
928 #define IPOINTER_REMOVE(ip, mp) { \
929                 ASSERT(ipointer_in == B_TRUE); \
930                 if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
931                         ip = ipointer->ip_mnext; \
932                         ip->i_mprev = ipointer->ip_mprev; \
933                         ipointer->ip_mprev->i_mnext = ip; \
934                         if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
935                                 mp->m_inodes = ip; \
936                         } \
937                 } else { \
938                         ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
939                         mp->m_inodes = NULL; \
940                         ip = NULL; \
941                 } \
942                 IPOINTER_CLR; \
943         }
944
945 #define XFS_PREEMPT_MASK        0x7f
946
947         ASSERT(!(flags & SYNC_BDFLUSH));
948
949         if (bypassed)
950                 *bypassed = 0;
951         if (mp->m_flags & XFS_MOUNT_RDONLY)
952                 return 0;
953         error = 0;
954         last_error = 0;
955         preempt = 0;
956
957         /* Allocate a reference marker */
958         ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
959
960         fflag = XFS_B_ASYNC;            /* default is don't wait */
961         if (flags & SYNC_DELWRI)
962                 fflag = XFS_B_DELWRI;
963         if (flags & SYNC_WAIT)
964                 fflag = 0;              /* synchronous overrides all */
965
966         base_lock_flags = XFS_ILOCK_SHARED;
967         if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
968                 /*
969                  * We need the I/O lock if we're going to call any of
970                  * the flush/inval routines.
971                  */
972                 base_lock_flags |= XFS_IOLOCK_SHARED;
973         }
974
975         XFS_MOUNT_ILOCK(mp);
976
977         ip = mp->m_inodes;
978
979         mount_locked = B_TRUE;
980         vnode_refed  = B_FALSE;
981
982         IPOINTER_CLR;
983
984         do {
985                 ASSERT(ipointer_in == B_FALSE);
986                 ASSERT(vnode_refed == B_FALSE);
987
988                 lock_flags = base_lock_flags;
989
990                 /*
991                  * There were no inodes in the list, just break out
992                  * of the loop.
993                  */
994                 if (ip == NULL) {
995                         break;
996                 }
997
998                 /*
999                  * We found another sync thread marker - skip it
1000                  */
1001                 if (ip->i_mount == NULL) {
1002                         ip = ip->i_mnext;
1003                         continue;
1004                 }
1005
1006                 vp = XFS_ITOV_NULL(ip);
1007
1008                 /*
1009                  * If the vnode is gone then this is being torn down,
1010                  * call reclaim if it is flushed, else let regular flush
1011                  * code deal with it later in the loop.
1012                  */
1013
1014                 if (vp == NULL) {
1015                         /* Skip ones already in reclaim */
1016                         if (ip->i_flags & XFS_IRECLAIM) {
1017                                 ip = ip->i_mnext;
1018                                 continue;
1019                         }
1020                         if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1021                                 ip = ip->i_mnext;
1022                         } else if ((xfs_ipincount(ip) == 0) &&
1023                                     xfs_iflock_nowait(ip)) {
1024                                 IPOINTER_INSERT(ip, mp);
1025
1026                                 xfs_finish_reclaim(ip, 1,
1027                                                 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
1028
1029                                 XFS_MOUNT_ILOCK(mp);
1030                                 mount_locked = B_TRUE;
1031                                 IPOINTER_REMOVE(ip, mp);
1032                         } else {
1033                                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1034                                 ip = ip->i_mnext;
1035                         }
1036                         continue;
1037                 }
1038
1039                 if (VN_BAD(vp)) {
1040                         ip = ip->i_mnext;
1041                         continue;
1042                 }
1043
1044                 if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
1045                         XFS_MOUNT_IUNLOCK(mp);
1046                         kmem_free(ipointer, sizeof(xfs_iptr_t));
1047                         return 0;
1048                 }
1049
1050                 /*
1051                  * Try to lock without sleeping.  We're out of order with
1052                  * the inode list lock here, so if we fail we need to drop
1053                  * the mount lock and try again.  If we're called from
1054                  * bdflush() here, then don't bother.
1055                  *
1056                  * The inode lock here actually coordinates with the
1057                  * almost spurious inode lock in xfs_ireclaim() to prevent
1058                  * the vnode we handle here without a reference from
1059                  * being freed while we reference it.  If we lock the inode
1060                  * while it's on the mount list here, then the spurious inode
1061                  * lock in xfs_ireclaim() after the inode is pulled from
1062                  * the mount list will sleep until we release it here.
1063                  * This keeps the vnode from being freed while we reference
1064                  * it.
1065                  */
1066                 if (xfs_ilock_nowait(ip, lock_flags) == 0) {
1067                         if (vp == NULL) {
1068                                 ip = ip->i_mnext;
1069                                 continue;
1070                         }
1071
1072                         vp = vn_grab(vp);
1073                         if (vp == NULL) {
1074                                 ip = ip->i_mnext;
1075                                 continue;
1076                         }
1077
1078                         IPOINTER_INSERT(ip, mp);
1079                         xfs_ilock(ip, lock_flags);
1080
1081                         ASSERT(vp == XFS_ITOV(ip));
1082                         ASSERT(ip->i_mount == mp);
1083
1084                         vnode_refed = B_TRUE;
1085                 }
1086
1087                 /* From here on in the loop we may have a marker record
1088                  * in the inode list.
1089                  */
1090
1091                 /*
1092                  * If we have to flush data or wait for I/O completion
1093                  * we need to drop the ilock that we currently hold.
1094                  * If we need to drop the lock, insert a marker if we
1095                  * have not already done so.
1096                  */
1097                 if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
1098                     ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
1099                         if (mount_locked) {
1100                                 IPOINTER_INSERT(ip, mp);
1101                         }
1102                         xfs_iunlock(ip, XFS_ILOCK_SHARED);
1103
1104                         if (flags & SYNC_CLOSE) {
1105                                 /* Shutdown case. Flush and invalidate. */
1106                                 if (XFS_FORCED_SHUTDOWN(mp))
1107                                         xfs_tosspages(ip, 0, -1,
1108                                                              FI_REMAPF);
1109                                 else
1110                                         error = xfs_flushinval_pages(ip,
1111                                                         0, -1, FI_REMAPF);
1112                         } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
1113                                 error = xfs_flush_pages(ip, 0,
1114                                                         -1, fflag, FI_NONE);
1115                         }
1116
1117                         /*
1118                          * When freezing, we need to wait ensure all I/O (including direct
1119                          * I/O) is complete to ensure no further data modification can take
1120                          * place after this point
1121                          */
1122                         if (flags & SYNC_IOWAIT)
1123                                 vn_iowait(ip);
1124
1125                         xfs_ilock(ip, XFS_ILOCK_SHARED);
1126                 }
1127
1128                 if ((flags & SYNC_ATTR) &&
1129                     (ip->i_update_core ||
1130                      (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
1131                         if (mount_locked)
1132                                 IPOINTER_INSERT(ip, mp);
1133
1134                         if (flags & SYNC_WAIT) {
1135                                 xfs_iflock(ip);
1136                                 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
1137
1138                         /*
1139                          * If we can't acquire the flush lock, then the inode
1140                          * is already being flushed so don't bother waiting.
1141                          *
1142                          * If we can lock it then do a delwri flush so we can
1143                          * combine multiple inode flushes in each disk write.
1144                          */
1145                         } else if (xfs_iflock_nowait(ip)) {
1146                                 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
1147                         } else if (bypassed) {
1148                                 (*bypassed)++;
1149                         }
1150                 }
1151
1152                 if (lock_flags != 0) {
1153                         xfs_iunlock(ip, lock_flags);
1154                 }
1155
1156                 if (vnode_refed) {
1157                         /*
1158                          * If we had to take a reference on the vnode
1159                          * above, then wait until after we've unlocked
1160                          * the inode to release the reference.  This is
1161                          * because we can be already holding the inode
1162                          * lock when IRELE() calls xfs_inactive().
1163                          *
1164                          * Make sure to drop the mount lock before calling
1165                          * IRELE() so that we don't trip over ourselves if
1166                          * we have to go for the mount lock again in the
1167                          * inactive code.
1168                          */
1169                         if (mount_locked) {
1170                                 IPOINTER_INSERT(ip, mp);
1171                         }
1172
1173                         IRELE(ip);
1174
1175                         vnode_refed = B_FALSE;
1176                 }
1177
1178                 if (error) {
1179                         last_error = error;
1180                 }
1181
1182                 /*
1183                  * bail out if the filesystem is corrupted.
1184                  */
1185                 if (error == EFSCORRUPTED)  {
1186                         if (!mount_locked) {
1187                                 XFS_MOUNT_ILOCK(mp);
1188                                 IPOINTER_REMOVE(ip, mp);
1189                         }
1190                         XFS_MOUNT_IUNLOCK(mp);
1191                         ASSERT(ipointer_in == B_FALSE);
1192                         kmem_free(ipointer, sizeof(xfs_iptr_t));
1193                         return XFS_ERROR(error);
1194                 }
1195
1196                 /* Let other threads have a chance at the mount lock
1197                  * if we have looped many times without dropping the
1198                  * lock.
1199                  */
1200                 if ((++preempt & XFS_PREEMPT_MASK) == 0) {
1201                         if (mount_locked) {
1202                                 IPOINTER_INSERT(ip, mp);
1203                         }
1204                 }
1205
1206                 if (mount_locked == B_FALSE) {
1207                         XFS_MOUNT_ILOCK(mp);
1208                         mount_locked = B_TRUE;
1209                         IPOINTER_REMOVE(ip, mp);
1210                         continue;
1211                 }
1212
1213                 ASSERT(ipointer_in == B_FALSE);
1214                 ip = ip->i_mnext;
1215
1216         } while (ip != mp->m_inodes);
1217
1218         XFS_MOUNT_IUNLOCK(mp);
1219
1220         ASSERT(ipointer_in == B_FALSE);
1221
1222         kmem_free(ipointer, sizeof(xfs_iptr_t));
1223         return XFS_ERROR(last_error);
1224 }
1225
1226 /*
1227  * xfs sync routine for internal use
1228  *
1229  * This routine supports all of the flags defined for the generic vfs_sync
1230  * interface as explained above under xfs_sync.
1231  *
1232  */
1233 int
1234 xfs_syncsub(
1235         xfs_mount_t     *mp,
1236         int             flags,
1237         int             *bypassed)
1238 {
1239         int             error = 0;
1240         int             last_error = 0;
1241         uint            log_flags = XFS_LOG_FORCE;
1242         xfs_buf_t       *bp;
1243         xfs_buf_log_item_t      *bip;
1244
1245         /*
1246          * Sync out the log.  This ensures that the log is periodically
1247          * flushed even if there is not enough activity to fill it up.
1248          */
1249         if (flags & SYNC_WAIT)
1250                 log_flags |= XFS_LOG_SYNC;
1251
1252         xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1253
1254         if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
1255                 if (flags & SYNC_BDFLUSH)
1256                         xfs_finish_reclaim_all(mp, 1);
1257                 else
1258                         error = xfs_sync_inodes(mp, flags, bypassed);
1259         }
1260
1261         /*
1262          * Flushing out dirty data above probably generated more
1263          * log activity, so if this isn't vfs_sync() then flush
1264          * the log again.
1265          */
1266         if (flags & SYNC_DELWRI) {
1267                 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1268         }
1269
1270         if (flags & SYNC_FSDATA) {
1271                 /*
1272                  * If this is vfs_sync() then only sync the superblock
1273                  * if we can lock it without sleeping and it is not pinned.
1274                  */
1275                 if (flags & SYNC_BDFLUSH) {
1276                         bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
1277                         if (bp != NULL) {
1278                                 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
1279                                 if ((bip != NULL) &&
1280                                     xfs_buf_item_dirty(bip)) {
1281                                         if (!(XFS_BUF_ISPINNED(bp))) {
1282                                                 XFS_BUF_ASYNC(bp);
1283                                                 error = xfs_bwrite(mp, bp);
1284                                         } else {
1285                                                 xfs_buf_relse(bp);
1286                                         }
1287                                 } else {
1288                                         xfs_buf_relse(bp);
1289                                 }
1290                         }
1291                 } else {
1292                         bp = xfs_getsb(mp, 0);
1293                         /*
1294                          * If the buffer is pinned then push on the log so
1295                          * we won't get stuck waiting in the write for
1296                          * someone, maybe ourselves, to flush the log.
1297                          * Even though we just pushed the log above, we
1298                          * did not have the superblock buffer locked at
1299                          * that point so it can become pinned in between
1300                          * there and here.
1301                          */
1302                         if (XFS_BUF_ISPINNED(bp))
1303                                 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1304                         if (flags & SYNC_WAIT)
1305                                 XFS_BUF_UNASYNC(bp);
1306                         else
1307                                 XFS_BUF_ASYNC(bp);
1308                         error = xfs_bwrite(mp, bp);
1309                 }
1310                 if (error) {
1311                         last_error = error;
1312                 }
1313         }
1314
1315         /*
1316          * If asked, update the disk superblock with incore counter values if we
1317          * are using non-persistent counters so that they don't get too far out
1318          * of sync if we crash or get a forced shutdown. We don't want to force
1319          * this to disk, just get a transaction into the iclogs....
1320          */
1321         if (flags & SYNC_SUPER)
1322                 xfs_log_sbcount(mp, 0);
1323
1324         /*
1325          * Now check to see if the log needs a "dummy" transaction.
1326          */
1327
1328         if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
1329                 xfs_trans_t *tp;
1330                 xfs_inode_t *ip;
1331
1332                 /*
1333                  * Put a dummy transaction in the log to tell
1334                  * recovery that all others are OK.
1335                  */
1336                 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
1337                 if ((error = xfs_trans_reserve(tp, 0,
1338                                 XFS_ICHANGE_LOG_RES(mp),
1339                                 0, 0, 0)))  {
1340                         xfs_trans_cancel(tp, 0);
1341                         return error;
1342                 }
1343
1344                 ip = mp->m_rootip;
1345                 xfs_ilock(ip, XFS_ILOCK_EXCL);
1346
1347                 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1348                 xfs_trans_ihold(tp, ip);
1349                 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1350                 error = xfs_trans_commit(tp, 0);
1351                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1352                 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1353         }
1354
1355         /*
1356          * When shutting down, we need to insure that the AIL is pushed
1357          * to disk or the filesystem can appear corrupt from the PROM.
1358          */
1359         if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
1360                 XFS_bflush(mp->m_ddev_targp);
1361                 if (mp->m_rtdev_targp) {
1362                         XFS_bflush(mp->m_rtdev_targp);
1363                 }
1364         }
1365
1366         return XFS_ERROR(last_error);
1367 }