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