]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/ufs/ffs/ffs_vfsops.c
MFC r362623:
[FreeBSD/stable/8.git] / sys / ufs / ffs / ffs_vfsops.c
1 /*-
2  * Copyright (c) 1989, 1991, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)ffs_vfsops.c        8.31 (Berkeley) 5/20/95
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_quota.h"
36 #include "opt_ufs.h"
37 #include "opt_ffs.h"
38 #include "opt_ddb.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/conf.h>
51 #include <sys/fcntl.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
54
55 #include <security/mac/mac_framework.h>
56
57 #include <ufs/ufs/extattr.h>
58 #include <ufs/ufs/gjournal.h>
59 #include <ufs/ufs/quota.h>
60 #include <ufs/ufs/ufsmount.h>
61 #include <ufs/ufs/inode.h>
62 #include <ufs/ufs/ufs_extern.h>
63
64 #include <ufs/ffs/fs.h>
65 #include <ufs/ffs/ffs_extern.h>
66
67 #include <vm/vm.h>
68 #include <vm/uma.h>
69 #include <vm/vm_page.h>
70
71 #include <geom/geom.h>
72 #include <geom/geom_vfs.h>
73
74 #include <ddb/ddb.h>
75
76 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
77
78 static int      ffs_reload(struct mount *, struct thread *);
79 static int      ffs_mountfs(struct vnode *, struct mount *, struct thread *);
80 static void     ffs_oldfscompat_read(struct fs *, struct ufsmount *,
81                     ufs2_daddr_t);
82 static void     ffs_oldfscompat_write(struct fs *, struct ufsmount *);
83 static void     ffs_ifree(struct ufsmount *ump, struct inode *ip);
84 static int      ffs_sync_lazy(struct mount *mp);
85
86 static vfs_init_t ffs_init;
87 static vfs_uninit_t ffs_uninit;
88 static vfs_extattrctl_t ffs_extattrctl;
89 static vfs_cmount_t ffs_cmount;
90 static vfs_unmount_t ffs_unmount;
91 static vfs_mount_t ffs_mount;
92 static vfs_statfs_t ffs_statfs;
93 static vfs_fhtovp_t ffs_fhtovp;
94 static vfs_sync_t ffs_sync;
95
96 static struct vfsops ufs_vfsops = {
97         .vfs_extattrctl =       ffs_extattrctl,
98         .vfs_fhtovp =           ffs_fhtovp,
99         .vfs_init =             ffs_init,
100         .vfs_mount =            ffs_mount,
101         .vfs_cmount =           ffs_cmount,
102         .vfs_quotactl =         ufs_quotactl,
103         .vfs_root =             ufs_root,
104         .vfs_statfs =           ffs_statfs,
105         .vfs_sync =             ffs_sync,
106         .vfs_uninit =           ffs_uninit,
107         .vfs_unmount =          ffs_unmount,
108         .vfs_vget =             ffs_vget,
109         .vfs_susp_clean =       process_deferred_inactive,
110 };
111
112 VFS_SET(ufs_vfsops, ufs, 0);
113 MODULE_VERSION(ufs, 1);
114
115 static b_strategy_t ffs_geom_strategy;
116 static b_write_t ffs_bufwrite;
117
118 static struct buf_ops ffs_ops = {
119         .bop_name =     "FFS",
120         .bop_write =    ffs_bufwrite,
121         .bop_strategy = ffs_geom_strategy,
122         .bop_sync =     bufsync,
123 #ifdef NO_FFS_SNAPSHOT
124         .bop_bdflush =  bufbdflush,
125 #else
126         .bop_bdflush =  ffs_bdflush,
127 #endif
128 };
129
130 /*
131  * Note that userquota and groupquota options are not currently used
132  * by UFS/FFS code and generally mount(8) does not pass those options
133  * from userland, but they can be passed by loader(8) via
134  * vfs.root.mountfrom.options.
135  */
136 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
137     "noclusterw", "noexec", "export", "force", "from", "groupquota",
138     "multilabel", "nfsv4acls", "snapshot", "nosuid", "suiddir", "nosymfollow",
139     "sync", "union", "userquota", NULL };
140
141 static int
142 ffs_mount(struct mount *mp)
143 {
144         struct vnode *devvp;
145         struct thread *td;
146         struct ufsmount *ump = 0;
147         struct fs *fs;
148         int error, flags;
149         u_int mntorflags, mntandnotflags;
150         accmode_t accmode;
151         struct nameidata ndp;
152         char *fspec;
153
154         td = curthread;
155         if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
156                 return (EINVAL);
157         if (uma_inode == NULL) {
158                 uma_inode = uma_zcreate("FFS inode",
159                     sizeof(struct inode), NULL, NULL, NULL, NULL,
160                     UMA_ALIGN_PTR, 0);
161                 uma_ufs1 = uma_zcreate("FFS1 dinode",
162                     sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
163                     UMA_ALIGN_PTR, 0);
164                 uma_ufs2 = uma_zcreate("FFS2 dinode",
165                     sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
166                     UMA_ALIGN_PTR, 0);
167         }
168
169         vfs_deleteopt(mp->mnt_optnew, "groupquota");
170         vfs_deleteopt(mp->mnt_optnew, "userquota");
171
172         fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
173         if (error)
174                 return (error);
175
176         mntorflags = 0;
177         mntandnotflags = 0;
178         if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
179                 mntorflags |= MNT_ACLS;
180
181         if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
182                 mntorflags |= MNT_SNAPSHOT;
183                 /*
184                  * Once we have set the MNT_SNAPSHOT flag, do not
185                  * persist "snapshot" in the options list.
186                  */
187                 vfs_deleteopt(mp->mnt_optnew, "snapshot");
188                 vfs_deleteopt(mp->mnt_opt, "snapshot");
189         }
190
191         if (vfs_getopt(mp->mnt_optnew, "nfsv4acls", NULL, NULL) == 0) {
192                 if (mntorflags & MNT_ACLS) {
193                         vfs_mount_error(mp,
194                             "\"acls\" and \"nfsv4acls\" options "
195                             "are mutually exclusive");
196                         return (EINVAL);
197                 }
198                 mntorflags |= MNT_NFS4ACLS;
199         }
200
201         MNT_ILOCK(mp);
202         mp->mnt_flag = (mp->mnt_flag | mntorflags) & ~mntandnotflags;
203         MNT_IUNLOCK(mp);
204         /*
205          * If updating, check whether changing from read-only to
206          * read/write; if there is no device name, that's all we do.
207          */
208         if (mp->mnt_flag & MNT_UPDATE) {
209                 ump = VFSTOUFS(mp);
210                 fs = ump->um_fs;
211                 devvp = ump->um_devvp;
212                 if (fs->fs_ronly == 0 &&
213                     vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
214                         /*
215                          * Flush any dirty data and suspend filesystem.
216                          */
217                         if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
218                                 return (error);
219                         for (;;) {
220                                 vn_finished_write(mp);
221                                 if ((error = vfs_write_suspend(mp)) != 0)
222                                         return (error);
223                                 MNT_ILOCK(mp);
224                                 if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
225                                         /*
226                                          * Allow the secondary writes
227                                          * to proceed.
228                                          */
229                                         mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
230                                             MNTK_SUSPEND2);
231                                         wakeup(&mp->mnt_flag);
232                                         MNT_IUNLOCK(mp);
233                                         /*
234                                          * Allow the curthread to
235                                          * ignore the suspension to
236                                          * synchronize on-disk state.
237                                          */
238                                         td->td_pflags |= TDP_IGNSUSP;
239                                         break;
240                                 }
241                                 MNT_IUNLOCK(mp);
242                                 vn_start_write(NULL, &mp, V_WAIT);
243                         }
244                         /*
245                          * Check for and optionally get rid of files open
246                          * for writing.
247                          */
248                         flags = WRITECLOSE;
249                         if (mp->mnt_flag & MNT_FORCE)
250                                 flags |= FORCECLOSE;
251                         if (mp->mnt_flag & MNT_SOFTDEP) {
252                                 error = softdep_flushfiles(mp, flags, td);
253                         } else {
254                                 error = ffs_flushfiles(mp, flags, td);
255                         }
256                         if (error) {
257                                 vfs_write_resume(mp);
258                                 return (error);
259                         }
260                         if (fs->fs_pendingblocks != 0 ||
261                             fs->fs_pendinginodes != 0) {
262                                 printf("WARNING: %s Update error: blocks %jd "
263                                     "files %d\n", fs->fs_fsmnt, 
264                                     (intmax_t)fs->fs_pendingblocks,
265                                     fs->fs_pendinginodes);
266                                 fs->fs_pendingblocks = 0;
267                                 fs->fs_pendinginodes = 0;
268                         }
269                         if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
270                                 fs->fs_clean = 1;
271                         if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
272                                 fs->fs_ronly = 0;
273                                 fs->fs_clean = 0;
274                                 vfs_write_resume(mp);
275                                 return (error);
276                         }
277                         DROP_GIANT();
278                         g_topology_lock();
279                         g_access(ump->um_cp, 0, -1, 0);
280                         g_topology_unlock();
281                         PICKUP_GIANT();
282                         fs->fs_ronly = 1;
283                         MNT_ILOCK(mp);
284                         mp->mnt_flag |= MNT_RDONLY;
285                         MNT_IUNLOCK(mp);
286                         /*
287                          * Allow the writers to note that filesystem
288                          * is ro now.
289                          */
290                         vfs_write_resume(mp);
291                 }
292                 if ((mp->mnt_flag & MNT_RELOAD) &&
293                     (error = ffs_reload(mp, td)) != 0)
294                         return (error);
295                 if (fs->fs_ronly &&
296                     !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
297                         /*
298                          * If upgrade to read-write by non-root, then verify
299                          * that user has necessary permissions on the device.
300                          */
301                         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
302                         error = VOP_ACCESS(devvp, VREAD | VWRITE,
303                             td->td_ucred, td);
304                         if (error)
305                                 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
306                         if (error) {
307                                 VOP_UNLOCK(devvp, 0);
308                                 return (error);
309                         }
310                         VOP_UNLOCK(devvp, 0);
311                         fs->fs_flags &= ~FS_UNCLEAN;
312                         if (fs->fs_clean == 0) {
313                                 fs->fs_flags |= FS_UNCLEAN;
314                                 if ((mp->mnt_flag & MNT_FORCE) ||
315                                     ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
316                                      (fs->fs_flags & FS_DOSOFTDEP))) {
317                                         printf("WARNING: %s was not properly "
318                                            "dismounted\n", fs->fs_fsmnt);
319                                 } else {
320                                         vfs_mount_error(mp,
321                                           "R/W mount of %s denied. %s.",
322                                           fs->fs_fsmnt,
323                                           "Filesystem is not clean - run fsck");
324                                         return (EPERM);
325                                 }
326                         }
327                         DROP_GIANT();
328                         g_topology_lock();
329                         /*
330                          * If we're the root device, we may not have an E count
331                          * yet, get it now.
332                          */
333                         if (ump->um_cp->ace == 0)
334                                 error = g_access(ump->um_cp, 0, 1, 1);
335                         else
336                                 error = g_access(ump->um_cp, 0, 1, 0);
337                         g_topology_unlock();
338                         PICKUP_GIANT();
339                         if (error)
340                                 return (error);
341                         if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
342                                 return (error);
343                         fs->fs_ronly = 0;
344                         MNT_ILOCK(mp);
345                         mp->mnt_flag &= ~MNT_RDONLY;
346                         MNT_IUNLOCK(mp);
347                         fs->fs_clean = 0;
348                         if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
349                                 vn_finished_write(mp);
350                                 return (error);
351                         }
352                         /* check to see if we need to start softdep */
353                         if ((fs->fs_flags & FS_DOSOFTDEP) &&
354                             (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
355                                 vn_finished_write(mp);
356                                 return (error);
357                         }
358                         if (fs->fs_snapinum[0] != 0)
359                                 ffs_snapshot_mount(mp);
360                         vn_finished_write(mp);
361                 }
362                 /*
363                  * Soft updates is incompatible with "async",
364                  * so if we are doing softupdates stop the user
365                  * from setting the async flag in an update.
366                  * Softdep_mount() clears it in an initial mount 
367                  * or ro->rw remount.
368                  */
369                 if (mp->mnt_flag & MNT_SOFTDEP) {
370                         /* XXX: Reset too late ? */
371                         MNT_ILOCK(mp);
372                         mp->mnt_flag &= ~MNT_ASYNC;
373                         MNT_IUNLOCK(mp);
374                 }
375                 /*
376                  * Keep MNT_ACLS flag if it is stored in superblock.
377                  */
378                 if ((fs->fs_flags & FS_ACLS) != 0) {
379                         /* XXX: Set too late ? */
380                         MNT_ILOCK(mp);
381                         mp->mnt_flag |= MNT_ACLS;
382                         MNT_IUNLOCK(mp);
383                 }
384
385                 if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
386                         /* XXX: Set too late ? */
387                         MNT_ILOCK(mp);
388                         mp->mnt_flag |= MNT_NFS4ACLS;
389                         MNT_IUNLOCK(mp);
390                 }
391         
392                 /*
393                  * If this is a snapshot request, take the snapshot.
394                  */
395                 if (mp->mnt_flag & MNT_SNAPSHOT)
396                         return (ffs_snapshot(mp, fspec));
397         }
398
399         /*
400          * Not an update, or updating the name: look up the name
401          * and verify that it refers to a sensible disk device.
402          */
403         NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
404         if ((error = namei(&ndp)) != 0)
405                 return (error);
406         NDFREE(&ndp, NDF_ONLY_PNBUF);
407         devvp = ndp.ni_vp;
408         if (!vn_isdisk(devvp, &error)) {
409                 vput(devvp);
410                 return (error);
411         }
412
413         /*
414          * If mount by non-root, then verify that user has necessary
415          * permissions on the device.
416          */
417         accmode = VREAD;
418         if ((mp->mnt_flag & MNT_RDONLY) == 0)
419                 accmode |= VWRITE;
420         error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
421         if (error)
422                 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
423         if (error) {
424                 vput(devvp);
425                 return (error);
426         }
427
428         if (mp->mnt_flag & MNT_UPDATE) {
429                 /*
430                  * Update only
431                  *
432                  * If it's not the same vnode, or at least the same device
433                  * then it's not correct.
434                  */
435
436                 if (devvp->v_rdev != ump->um_devvp->v_rdev)
437                         error = EINVAL; /* needs translation */
438                 vput(devvp);
439                 if (error)
440                         return (error);
441         } else {
442                 /*
443                  * New mount
444                  *
445                  * We need the name for the mount point (also used for
446                  * "last mounted on") copied in. If an error occurs,
447                  * the mount point is discarded by the upper level code.
448                  * Note that vfs_mount() populates f_mntonname for us.
449                  */
450                 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
451                         vrele(devvp);
452                         return (error);
453                 }
454         }
455         vfs_mountedfrom(mp, fspec);
456         return (0);
457 }
458
459 /*
460  * Compatibility with old mount system call.
461  */
462
463 static int
464 ffs_cmount(struct mntarg *ma, void *data, int flags)
465 {
466         struct ufs_args args;
467         struct export_args exp;
468         int error;
469
470         if (data == NULL)
471                 return (EINVAL);
472         error = copyin(data, &args, sizeof args);
473         if (error)
474                 return (error);
475         vfs_oexport_conv(&args.export, &exp);
476
477         ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
478         ma = mount_arg(ma, "export", &exp, sizeof(exp));
479         error = kernel_mount(ma, flags);
480
481         return (error);
482 }
483
484 /*
485  * Reload all incore data for a filesystem (used after running fsck on
486  * the root filesystem and finding things to fix). The filesystem must
487  * be mounted read-only.
488  *
489  * Things to do to update the mount:
490  *      1) invalidate all cached meta-data.
491  *      2) re-read superblock from disk.
492  *      3) re-read summary information from disk.
493  *      4) invalidate all inactive vnodes.
494  *      5) invalidate all cached file data.
495  *      6) re-read inode data for all active vnodes.
496  */
497 static int
498 ffs_reload(struct mount *mp, struct thread *td)
499 {
500         struct vnode *vp, *mvp, *devvp;
501         struct inode *ip;
502         void *space;
503         struct buf *bp;
504         struct fs *fs, *newfs;
505         struct ufsmount *ump;
506         ufs2_daddr_t sblockloc;
507         int i, blks, size, error;
508         int32_t *lp;
509
510         if ((mp->mnt_flag & MNT_RDONLY) == 0)
511                 return (EINVAL);
512         ump = VFSTOUFS(mp);
513         /*
514          * Step 1: invalidate all cached meta-data.
515          */
516         devvp = VFSTOUFS(mp)->um_devvp;
517         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
518         if (vinvalbuf(devvp, 0, 0, 0) != 0)
519                 panic("ffs_reload: dirty1");
520         VOP_UNLOCK(devvp, 0);
521
522         /*
523          * Step 2: re-read superblock from disk.
524          */
525         fs = VFSTOUFS(mp)->um_fs;
526         if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
527             NOCRED, &bp)) != 0)
528                 return (error);
529         newfs = (struct fs *)bp->b_data;
530         if ((newfs->fs_magic != FS_UFS1_MAGIC &&
531              newfs->fs_magic != FS_UFS2_MAGIC) ||
532             newfs->fs_bsize > MAXBSIZE ||
533             newfs->fs_bsize < sizeof(struct fs)) {
534                         brelse(bp);
535                         return (EIO);           /* XXX needs translation */
536         }
537         /*
538          * Copy pointer fields back into superblock before copying in   XXX
539          * new superblock. These should really be in the ufsmount.      XXX
540          * Note that important parameters (eg fs_ncg) are unchanged.
541          */
542         newfs->fs_csp = fs->fs_csp;
543         newfs->fs_maxcluster = fs->fs_maxcluster;
544         newfs->fs_contigdirs = fs->fs_contigdirs;
545         newfs->fs_active = fs->fs_active;
546         /* The file system is still read-only. */
547         newfs->fs_ronly = 1;
548         sblockloc = fs->fs_sblockloc;
549         bcopy(newfs, fs, (u_int)fs->fs_sbsize);
550         brelse(bp);
551         mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
552         ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
553         UFS_LOCK(ump);
554         if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
555                 printf("WARNING: %s: reload pending error: blocks %jd "
556                     "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
557                     fs->fs_pendinginodes);
558                 fs->fs_pendingblocks = 0;
559                 fs->fs_pendinginodes = 0;
560         }
561         UFS_UNLOCK(ump);
562
563         /*
564          * Step 3: re-read summary information from disk.
565          */
566         blks = howmany(fs->fs_cssize, fs->fs_fsize);
567         space = fs->fs_csp;
568         for (i = 0; i < blks; i += fs->fs_frag) {
569                 size = fs->fs_bsize;
570                 if (i + fs->fs_frag > blks)
571                         size = (blks - i) * fs->fs_fsize;
572                 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
573                     NOCRED, &bp);
574                 if (error)
575                         return (error);
576                 bcopy(bp->b_data, space, (u_int)size);
577                 space = (char *)space + size;
578                 brelse(bp);
579         }
580         /*
581          * We no longer know anything about clusters per cylinder group.
582          */
583         if (fs->fs_contigsumsize > 0) {
584                 lp = fs->fs_maxcluster;
585                 for (i = 0; i < fs->fs_ncg; i++)
586                         *lp++ = fs->fs_contigsumsize;
587         }
588
589 loop:
590         MNT_ILOCK(mp);
591         MNT_VNODE_FOREACH(vp, mp, mvp) {
592                 VI_LOCK(vp);
593                 if (vp->v_iflag & VI_DOOMED) {
594                         VI_UNLOCK(vp);
595                         continue;
596                 }
597                 MNT_IUNLOCK(mp);
598                 /*
599                  * Step 4: invalidate all cached file data.
600                  */
601                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
602                         MNT_VNODE_FOREACH_ABORT(mp, mvp);
603                         goto loop;
604                 }
605                 if (vinvalbuf(vp, 0, 0, 0))
606                         panic("ffs_reload: dirty2");
607                 /*
608                  * Step 5: re-read inode data for all active vnodes.
609                  */
610                 ip = VTOI(vp);
611                 error =
612                     bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
613                     (int)fs->fs_bsize, NOCRED, &bp);
614                 if (error) {
615                         VOP_UNLOCK(vp, 0);
616                         vrele(vp);
617                         MNT_VNODE_FOREACH_ABORT(mp, mvp);
618                         return (error);
619                 }
620                 ffs_load_inode(bp, ip, fs, ip->i_number);
621                 ip->i_effnlink = ip->i_nlink;
622                 brelse(bp);
623                 VOP_UNLOCK(vp, 0);
624                 vrele(vp);
625                 MNT_ILOCK(mp);
626         }
627         MNT_IUNLOCK(mp);
628         return (0);
629 }
630
631 /*
632  * Possible superblock locations ordered from most to least likely.
633  */
634 static int sblock_try[] = SBLOCKSEARCH;
635
636 /*
637  * Common code for mount and mountroot
638  */
639 static int
640 ffs_mountfs(devvp, mp, td)
641         struct vnode *devvp;
642         struct mount *mp;
643         struct thread *td;
644 {
645         struct ufsmount *ump;
646         struct buf *bp;
647         struct fs *fs;
648         struct cdev *dev;
649         void *space;
650         ufs2_daddr_t sblockloc;
651         int error, i, blks, size, ronly;
652         int32_t *lp;
653         struct ucred *cred;
654         struct g_consumer *cp;
655         struct mount *nmp;
656
657         bp = NULL;
658         ump = NULL;
659         cred = td ? td->td_ucred : NOCRED;
660         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
661
662         dev = devvp->v_rdev;
663         dev_ref(dev);
664         DROP_GIANT();
665         g_topology_lock();
666         error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
667
668         /*
669          * If we are a root mount, drop the E flag so fsck can do its magic.
670          * We will pick it up again when we remount R/W.
671          */
672         if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS))
673                 error = g_access(cp, 0, 0, -1);
674         g_topology_unlock();
675         PICKUP_GIANT();
676         VOP_UNLOCK(devvp, 0);
677         if (error)
678                 goto out;
679         if (devvp->v_rdev->si_iosize_max != 0)
680                 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
681         if (mp->mnt_iosize_max > MAXPHYS)
682                 mp->mnt_iosize_max = MAXPHYS;
683
684         devvp->v_bufobj.bo_ops = &ffs_ops;
685
686         fs = NULL;
687         sblockloc = 0;
688         /*
689          * Try reading the superblock in each of its possible locations.
690          */
691         for (i = 0; sblock_try[i] != -1; i++) {
692                 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
693                         error = EINVAL;
694                         vfs_mount_error(mp,
695                             "Invalid sectorsize %d for superblock size %d",
696                             cp->provider->sectorsize, SBLOCKSIZE);
697                         goto out;
698                 }
699                 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
700                     cred, &bp)) != 0)
701                         goto out;
702                 fs = (struct fs *)bp->b_data;
703                 sblockloc = sblock_try[i];
704                 if ((fs->fs_magic == FS_UFS1_MAGIC ||
705                      (fs->fs_magic == FS_UFS2_MAGIC &&
706                       (fs->fs_sblockloc == sblockloc ||
707                        (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
708                     fs->fs_bsize <= MAXBSIZE &&
709                     fs->fs_bsize >= sizeof(struct fs))
710                         break;
711                 brelse(bp);
712                 bp = NULL;
713         }
714         if (sblock_try[i] == -1) {
715                 error = EINVAL;         /* XXX needs translation */
716                 goto out;
717         }
718         fs->fs_fmod = 0;
719         fs->fs_flags &= ~FS_INDEXDIRS;  /* no support for directory indicies */
720         fs->fs_flags &= ~FS_UNCLEAN;
721         if (fs->fs_clean == 0) {
722                 fs->fs_flags |= FS_UNCLEAN;
723                 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
724                     ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
725                      (fs->fs_flags & FS_DOSOFTDEP))) {
726                         printf(
727 "WARNING: %s was not properly dismounted\n",
728                             fs->fs_fsmnt);
729                 } else {
730                         vfs_mount_error(mp, "R/W mount of %s denied. %s.",
731                             fs->fs_fsmnt, "Filesystem is not clean - run fsck");
732                         error = EPERM;
733                         goto out;
734                 }
735                 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
736                     (mp->mnt_flag & MNT_FORCE)) {
737                         printf("WARNING: %s: lost blocks %jd files %d\n",
738                             fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
739                             fs->fs_pendinginodes);
740                         fs->fs_pendingblocks = 0;
741                         fs->fs_pendinginodes = 0;
742                 }
743         }
744         if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
745                 printf("WARNING: %s: mount pending error: blocks %jd "
746                     "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
747                     fs->fs_pendinginodes);
748                 fs->fs_pendingblocks = 0;
749                 fs->fs_pendinginodes = 0;
750         }
751         if ((fs->fs_flags & FS_GJOURNAL) != 0) {
752 #ifdef UFS_GJOURNAL
753                 /*
754                  * Get journal provider name.
755                  */
756                 size = 1024;
757                 mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
758                 if (g_io_getattr("GJOURNAL::provider", cp, &size,
759                     mp->mnt_gjprovider) == 0) {
760                         mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
761                             M_UFSMNT, M_WAITOK);
762                         MNT_ILOCK(mp);
763                         mp->mnt_flag |= MNT_GJOURNAL;
764                         MNT_IUNLOCK(mp);
765                 } else {
766                         printf("WARNING: %s: GJOURNAL flag on fs "
767                             "but no gjournal provider below\n",
768                             mp->mnt_stat.f_mntonname);
769                         free(mp->mnt_gjprovider, M_UFSMNT);
770                         mp->mnt_gjprovider = NULL;
771                 }
772 #else
773                 printf("WARNING: %s: GJOURNAL flag on fs but no "
774                     "UFS_GJOURNAL support\n", mp->mnt_stat.f_mntonname);
775 #endif
776         } else {
777                 mp->mnt_gjprovider = NULL;
778         }
779         ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
780         ump->um_cp = cp;
781         ump->um_bo = &devvp->v_bufobj;
782         ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
783         if (fs->fs_magic == FS_UFS1_MAGIC) {
784                 ump->um_fstype = UFS1;
785                 ump->um_balloc = ffs_balloc_ufs1;
786         } else {
787                 ump->um_fstype = UFS2;
788                 ump->um_balloc = ffs_balloc_ufs2;
789         }
790         ump->um_blkatoff = ffs_blkatoff;
791         ump->um_truncate = ffs_truncate;
792         ump->um_update = ffs_update;
793         ump->um_valloc = ffs_valloc;
794         ump->um_vfree = ffs_vfree;
795         ump->um_ifree = ffs_ifree;
796         ump->um_rdonly = ffs_rdonly;
797         ump->um_snapgone = ffs_snapgone;
798         mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
799         bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
800         if (fs->fs_sbsize < SBLOCKSIZE)
801                 bp->b_flags |= B_INVAL | B_NOCACHE;
802         brelse(bp);
803         bp = NULL;
804         fs = ump->um_fs;
805         ffs_oldfscompat_read(fs, ump, sblockloc);
806         fs->fs_ronly = ronly;
807         size = fs->fs_cssize;
808         blks = howmany(size, fs->fs_fsize);
809         if (fs->fs_contigsumsize > 0)
810                 size += fs->fs_ncg * sizeof(int32_t);
811         size += fs->fs_ncg * sizeof(u_int8_t);
812         space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
813         fs->fs_csp = space;
814         for (i = 0; i < blks; i += fs->fs_frag) {
815                 size = fs->fs_bsize;
816                 if (i + fs->fs_frag > blks)
817                         size = (blks - i) * fs->fs_fsize;
818                 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
819                     cred, &bp)) != 0) {
820                         free(fs->fs_csp, M_UFSMNT);
821                         goto out;
822                 }
823                 bcopy(bp->b_data, space, (u_int)size);
824                 space = (char *)space + size;
825                 brelse(bp);
826                 bp = NULL;
827         }
828         if (fs->fs_contigsumsize > 0) {
829                 fs->fs_maxcluster = lp = space;
830                 for (i = 0; i < fs->fs_ncg; i++)
831                         *lp++ = fs->fs_contigsumsize;
832                 space = lp;
833         }
834         size = fs->fs_ncg * sizeof(u_int8_t);
835         fs->fs_contigdirs = (u_int8_t *)space;
836         bzero(fs->fs_contigdirs, size);
837         fs->fs_active = NULL;
838         mp->mnt_data = ump;
839         mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
840         mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
841         nmp = NULL;
842         if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 || 
843             (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
844                 if (nmp)
845                         vfs_rel(nmp);
846                 vfs_getnewfsid(mp);
847         }
848         mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
849         MNT_ILOCK(mp);
850         mp->mnt_flag |= MNT_LOCAL;
851         MNT_IUNLOCK(mp);
852         if ((fs->fs_flags & FS_MULTILABEL) != 0) {
853 #ifdef MAC
854                 MNT_ILOCK(mp);
855                 mp->mnt_flag |= MNT_MULTILABEL;
856                 MNT_IUNLOCK(mp);
857 #else
858                 printf("WARNING: %s: multilabel flag on fs but "
859                     "no MAC support\n", mp->mnt_stat.f_mntonname);
860 #endif
861         }
862         if ((fs->fs_flags & FS_ACLS) != 0) {
863 #ifdef UFS_ACL
864                 MNT_ILOCK(mp);
865
866                 if (mp->mnt_flag & MNT_NFS4ACLS)
867                         printf("WARNING: %s: ACLs flag on fs conflicts with "
868                             "\"nfsv4acls\" mount option; option ignored\n",
869                             mp->mnt_stat.f_mntonname);
870                 mp->mnt_flag &= ~MNT_NFS4ACLS;
871                 mp->mnt_flag |= MNT_ACLS;
872
873                 MNT_IUNLOCK(mp);
874 #else
875                 printf(
876 "WARNING: %s: ACLs flag on fs but no ACLs support\n",
877                     mp->mnt_stat.f_mntonname);
878 #endif
879         }
880         if ((fs->fs_flags & FS_NFS4ACLS) != 0) {
881 #ifdef UFS_ACL
882                 MNT_ILOCK(mp);
883
884                 if (mp->mnt_flag & MNT_ACLS)
885                         printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts "
886                             "with \"acls\" mount option; option ignored\n",
887                             mp->mnt_stat.f_mntonname);
888                 mp->mnt_flag &= ~MNT_ACLS;
889                 mp->mnt_flag |= MNT_NFS4ACLS;
890
891                 MNT_IUNLOCK(mp);
892 #else
893                 printf("WARNING: %s: NFSv4 ACLs flag on fs but no "
894                     "ACLs support\n", mp->mnt_stat.f_mntonname);
895 #endif
896         }
897         if ((fs->fs_flags & FS_TRIM) != 0) {
898                 size = sizeof(int);
899                 if (g_io_getattr("GEOM::candelete", cp, &size,
900                     &ump->um_candelete) == 0) {
901                         if (!ump->um_candelete)
902                                 printf("WARNING: %s: TRIM flag on fs but disk "
903                                     "does not support TRIM\n",
904                                     mp->mnt_stat.f_mntonname);
905                 } else {
906                         printf("WARNING: %s: TRIM flag on fs but disk does "
907                             "not confirm that it supports TRIM\n",
908                             mp->mnt_stat.f_mntonname);
909                         ump->um_candelete = 0;
910                 }
911         }
912
913         ump->um_mountp = mp;
914         ump->um_dev = dev;
915         ump->um_devvp = devvp;
916         ump->um_nindir = fs->fs_nindir;
917         ump->um_bptrtodb = fs->fs_fsbtodb;
918         ump->um_seqinc = fs->fs_frag;
919         for (i = 0; i < MAXQUOTAS; i++)
920                 ump->um_quotas[i] = NULLVP;
921 #ifdef UFS_EXTATTR
922         ufs_extattr_uepm_init(&ump->um_extattr);
923 #endif
924         /*
925          * Set FS local "last mounted on" information (NULL pad)
926          */
927         bzero(fs->fs_fsmnt, MAXMNTLEN);
928         strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
929
930         if( mp->mnt_flag & MNT_ROOTFS) {
931                 /*
932                  * Root mount; update timestamp in mount structure.
933                  * this will be used by the common root mount code
934                  * to update the system clock.
935                  */
936                 mp->mnt_time = fs->fs_time;
937         }
938
939         if (ronly == 0) {
940                 if ((fs->fs_flags & FS_DOSOFTDEP) &&
941                     (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
942                         free(fs->fs_csp, M_UFSMNT);
943                         goto out;
944                 }
945                 if (fs->fs_snapinum[0] != 0)
946                         ffs_snapshot_mount(mp);
947                 fs->fs_fmod = 1;
948                 fs->fs_clean = 0;
949                 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
950         }
951         /*
952          * Initialize filesystem stat information in mount struct.
953          */
954         MNT_ILOCK(mp);
955         mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED |
956             MNTK_EXTENDED_SHARED;
957         MNT_IUNLOCK(mp);
958 #ifdef UFS_EXTATTR
959 #ifdef UFS_EXTATTR_AUTOSTART
960         /*
961          *
962          * Auto-starting does the following:
963          *      - check for /.attribute in the fs, and extattr_start if so
964          *      - for each file in .attribute, enable that file with
965          *        an attribute of the same name.
966          * Not clear how to report errors -- probably eat them.
967          * This would all happen while the filesystem was busy/not
968          * available, so would effectively be "atomic".
969          */
970         mp->mnt_stat.f_iosize = fs->fs_bsize;
971         (void) ufs_extattr_autostart(mp, td);
972 #endif /* !UFS_EXTATTR_AUTOSTART */
973 #endif /* !UFS_EXTATTR */
974         return (0);
975 out:
976         if (bp)
977                 brelse(bp);
978         if (cp != NULL) {
979                 DROP_GIANT();
980                 g_topology_lock();
981                 g_vfs_close(cp);
982                 g_topology_unlock();
983                 PICKUP_GIANT();
984         }
985         if (ump) {
986                 mtx_destroy(UFS_MTX(ump));
987                 if (mp->mnt_gjprovider != NULL) {
988                         free(mp->mnt_gjprovider, M_UFSMNT);
989                         mp->mnt_gjprovider = NULL;
990                 }
991                 free(ump->um_fs, M_UFSMNT);
992                 free(ump, M_UFSMNT);
993                 mp->mnt_data = NULL;
994         }
995         dev_rel(dev);
996         return (error);
997 }
998
999 #include <sys/sysctl.h>
1000 static int bigcgs = 0;
1001 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
1002
1003 /*
1004  * Sanity checks for loading old filesystem superblocks.
1005  * See ffs_oldfscompat_write below for unwound actions.
1006  *
1007  * XXX - Parts get retired eventually.
1008  * Unfortunately new bits get added.
1009  */
1010 static void
1011 ffs_oldfscompat_read(fs, ump, sblockloc)
1012         struct fs *fs;
1013         struct ufsmount *ump;
1014         ufs2_daddr_t sblockloc;
1015 {
1016         off_t maxfilesize;
1017
1018         /*
1019          * If not yet done, update fs_flags location and value of fs_sblockloc.
1020          */
1021         if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
1022                 fs->fs_flags = fs->fs_old_flags;
1023                 fs->fs_old_flags |= FS_FLAGS_UPDATED;
1024                 fs->fs_sblockloc = sblockloc;
1025         }
1026         /*
1027          * If not yet done, update UFS1 superblock with new wider fields.
1028          */
1029         if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
1030                 fs->fs_maxbsize = fs->fs_bsize;
1031                 fs->fs_time = fs->fs_old_time;
1032                 fs->fs_size = fs->fs_old_size;
1033                 fs->fs_dsize = fs->fs_old_dsize;
1034                 fs->fs_csaddr = fs->fs_old_csaddr;
1035                 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
1036                 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
1037                 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
1038                 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
1039         }
1040         if (fs->fs_magic == FS_UFS1_MAGIC &&
1041             fs->fs_old_inodefmt < FS_44INODEFMT) {
1042                 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
1043                 fs->fs_qbmask = ~fs->fs_bmask;
1044                 fs->fs_qfmask = ~fs->fs_fmask;
1045         }
1046         if (fs->fs_magic == FS_UFS1_MAGIC) {
1047                 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
1048                 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
1049                 if (fs->fs_maxfilesize > maxfilesize)
1050                         fs->fs_maxfilesize = maxfilesize;
1051         }
1052         /* Compatibility for old filesystems */
1053         if (fs->fs_avgfilesize <= 0)
1054                 fs->fs_avgfilesize = AVFILESIZ;
1055         if (fs->fs_avgfpdir <= 0)
1056                 fs->fs_avgfpdir = AFPDIR;
1057         if (bigcgs) {
1058                 fs->fs_save_cgsize = fs->fs_cgsize;
1059                 fs->fs_cgsize = fs->fs_bsize;
1060         }
1061 }
1062
1063 /*
1064  * Unwinding superblock updates for old filesystems.
1065  * See ffs_oldfscompat_read above for details.
1066  *
1067  * XXX - Parts get retired eventually.
1068  * Unfortunately new bits get added.
1069  */
1070 static void
1071 ffs_oldfscompat_write(fs, ump)
1072         struct fs *fs;
1073         struct ufsmount *ump;
1074 {
1075
1076         /*
1077          * Copy back UFS2 updated fields that UFS1 inspects.
1078          */
1079         if (fs->fs_magic == FS_UFS1_MAGIC) {
1080                 fs->fs_old_time = fs->fs_time;
1081                 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
1082                 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
1083                 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
1084                 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1085                 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
1086         }
1087         if (bigcgs) {
1088                 fs->fs_cgsize = fs->fs_save_cgsize;
1089                 fs->fs_save_cgsize = 0;
1090         }
1091 }
1092
1093 /*
1094  * unmount system call
1095  */
1096 static int
1097 ffs_unmount(mp, mntflags)
1098         struct mount *mp;
1099         int mntflags;
1100 {
1101         struct thread *td;
1102         struct ufsmount *ump = VFSTOUFS(mp);
1103         struct fs *fs;
1104         int error, flags, susp;
1105 #ifdef UFS_EXTATTR
1106         int e_restart;
1107 #endif
1108
1109         flags = 0;
1110         td = curthread;
1111         fs = ump->um_fs;
1112         susp = 0;
1113         if (mntflags & MNT_FORCE) {
1114                 flags |= FORCECLOSE;
1115                 susp = fs->fs_ronly != 0;
1116         }
1117 #ifdef UFS_EXTATTR
1118         if ((error = ufs_extattr_stop(mp, td))) {
1119                 if (error != EOPNOTSUPP)
1120                         printf("WARNING: unmount %s: ufs_extattr_stop "
1121                             "returned errno %d\n", mp->mnt_stat.f_mntonname,
1122                             error);
1123                 e_restart = 0;
1124         } else {
1125                 ufs_extattr_uepm_destroy(&ump->um_extattr);
1126                 e_restart = 1;
1127         }
1128 #endif
1129         if (susp) {
1130                 /*
1131                  * dounmount already called vn_start_write().
1132                  */
1133                 for (;;) {
1134                         vn_finished_write(mp);
1135                         if ((error = vfs_write_suspend(mp)) != 0)
1136                                 return (error);
1137                         MNT_ILOCK(mp);
1138                         if (mp->mnt_kern_flag & MNTK_SUSPENDED) {
1139                                 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED |
1140                                     MNTK_SUSPEND2);
1141                                 wakeup(&mp->mnt_flag);
1142                                 MNT_IUNLOCK(mp);
1143                                 td->td_pflags |= TDP_IGNSUSP;
1144                                 break;
1145                         }
1146                         MNT_IUNLOCK(mp);
1147                         vn_start_write(NULL, &mp, V_WAIT);
1148                 }
1149         }
1150         if (mp->mnt_flag & MNT_SOFTDEP)
1151                 error = softdep_flushfiles(mp, flags, td);
1152         else
1153                 error = ffs_flushfiles(mp, flags, td);
1154         if (error != 0 && error != ENXIO)
1155                 goto fail;
1156
1157         UFS_LOCK(ump);
1158         if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1159                 printf("WARNING: unmount %s: pending error: blocks %jd "
1160                     "files %d\n", fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
1161                     fs->fs_pendinginodes);
1162                 fs->fs_pendingblocks = 0;
1163                 fs->fs_pendinginodes = 0;
1164         }
1165         UFS_UNLOCK(ump);
1166         if (fs->fs_ronly == 0) {
1167                 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
1168                 error = ffs_sbupdate(ump, MNT_WAIT, 0);
1169                 if (error && error != ENXIO) {
1170                         fs->fs_clean = 0;
1171                         goto fail;
1172                 }
1173         }
1174         if (susp) {
1175                 vfs_write_resume(mp);
1176                 vn_start_write(NULL, &mp, V_WAIT);
1177         }
1178         DROP_GIANT();
1179         g_topology_lock();
1180         g_vfs_close(ump->um_cp);
1181         g_topology_unlock();
1182         PICKUP_GIANT();
1183         vrele(ump->um_devvp);
1184         dev_rel(ump->um_dev);
1185         mtx_destroy(UFS_MTX(ump));
1186         if (mp->mnt_gjprovider != NULL) {
1187                 free(mp->mnt_gjprovider, M_UFSMNT);
1188                 mp->mnt_gjprovider = NULL;
1189         }
1190         free(fs->fs_csp, M_UFSMNT);
1191         free(fs, M_UFSMNT);
1192         free(ump, M_UFSMNT);
1193         mp->mnt_data = NULL;
1194         MNT_ILOCK(mp);
1195         mp->mnt_flag &= ~MNT_LOCAL;
1196         MNT_IUNLOCK(mp);
1197         return (error);
1198
1199 fail:
1200         if (susp) {
1201                 vfs_write_resume(mp);
1202                 vn_start_write(NULL, &mp, V_WAIT);
1203         }
1204 #ifdef UFS_EXTATTR
1205         if (e_restart) {
1206                 ufs_extattr_uepm_init(&ump->um_extattr);
1207 #ifdef UFS_EXTATTR_AUTOSTART
1208                 (void) ufs_extattr_autostart(mp, td);
1209 #endif
1210         }
1211 #endif
1212
1213         return (error);
1214 }
1215
1216 /*
1217  * Flush out all the files in a filesystem.
1218  */
1219 int
1220 ffs_flushfiles(mp, flags, td)
1221         struct mount *mp;
1222         int flags;
1223         struct thread *td;
1224 {
1225         struct ufsmount *ump;
1226         int error;
1227
1228         ump = VFSTOUFS(mp);
1229 #ifdef QUOTA
1230         if (mp->mnt_flag & MNT_QUOTA) {
1231                 int i;
1232                 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
1233                 if (error)
1234                         return (error);
1235                 for (i = 0; i < MAXQUOTAS; i++) {
1236                         quotaoff(td, mp, i);
1237                 }
1238                 /*
1239                  * Here we fall through to vflush again to ensure
1240                  * that we have gotten rid of all the system vnodes.
1241                  */
1242         }
1243 #endif
1244         ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
1245         if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
1246                 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
1247                         return (error);
1248                 ffs_snapshot_unmount(mp);
1249                 flags |= FORCECLOSE;
1250                 /*
1251                  * Here we fall through to vflush again to ensure
1252                  * that we have gotten rid of all the system vnodes.
1253                  */
1254         }
1255         /*
1256          * Flush all the files.
1257          */
1258         if ((error = vflush(mp, 0, flags, td)) != 0)
1259                 return (error);
1260         /*
1261          * Flush filesystem metadata.
1262          */
1263         vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1264         error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
1265         VOP_UNLOCK(ump->um_devvp, 0);
1266         return (error);
1267 }
1268
1269 /*
1270  * Get filesystem statistics.
1271  */
1272 static int
1273 ffs_statfs(mp, sbp)
1274         struct mount *mp;
1275         struct statfs *sbp;
1276 {
1277         struct ufsmount *ump;
1278         struct fs *fs;
1279
1280         ump = VFSTOUFS(mp);
1281         fs = ump->um_fs;
1282         if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
1283                 panic("ffs_statfs");
1284         sbp->f_version = STATFS_VERSION;
1285         sbp->f_bsize = fs->fs_fsize;
1286         sbp->f_iosize = fs->fs_bsize;
1287         sbp->f_blocks = fs->fs_dsize;
1288         UFS_LOCK(ump);
1289         sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
1290             fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1291         sbp->f_bavail = freespace(fs, fs->fs_minfree) +
1292             dbtofsb(fs, fs->fs_pendingblocks);
1293         sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
1294         sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1295         UFS_UNLOCK(ump);
1296         sbp->f_namemax = NAME_MAX;
1297         return (0);
1298 }
1299
1300 /*
1301  * For a lazy sync, we only care about access times, quotas and the
1302  * superblock.  Other filesystem changes are already converted to
1303  * cylinder group blocks or inode blocks updates and are written to
1304  * disk by syncer.
1305  */
1306 static int
1307 ffs_sync_lazy(mp)
1308      struct mount *mp;
1309 {
1310         struct vnode *mvp, *vp;
1311         struct inode *ip;
1312         struct thread *td;
1313         int allerror, error;
1314
1315         allerror = 0;
1316         td = curthread;
1317         if ((mp->mnt_flag & MNT_NOATIME) != 0)
1318                 goto qupdate;
1319         MNT_ILOCK(mp);
1320         MNT_VNODE_FOREACH(vp, mp, mvp) {
1321                 VI_LOCK(vp);
1322                 if (vp->v_iflag & VI_DOOMED || vp->v_type == VNON) {
1323                         VI_UNLOCK(vp);
1324                         continue;
1325                 }
1326                 ip = VTOI(vp);
1327
1328                 /*
1329                  * The IN_ACCESS flag is converted to IN_MODIFIED by
1330                  * ufs_close() and ufs_getattr() by the calls to
1331                  * ufs_itimes_locked(), without subsequent UFS_UPDATE().
1332                  * Test also all the other timestamp flags too, to pick up
1333                  * any other cases that could be missed.
1334                  */
1335                 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
1336                     IN_UPDATE)) == 0) {
1337                         VI_UNLOCK(vp);
1338                         continue;
1339                 }
1340                 MNT_IUNLOCK(mp);
1341                 if ((error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
1342                     td)) != 0) {
1343                         MNT_ILOCK(mp);
1344                         continue;
1345                 }
1346                 error = ffs_update(vp, 0);
1347                 if (error != 0)
1348                         allerror = error;
1349                 vput(vp);
1350                 MNT_ILOCK(mp);
1351         }
1352         MNT_IUNLOCK(mp);
1353
1354 qupdate:
1355 #ifdef QUOTA
1356         qsync(mp);
1357 #endif
1358
1359         if (VFSTOUFS(mp)->um_fs->fs_fmod != 0 &&
1360             (error = ffs_sbupdate(VFSTOUFS(mp), MNT_LAZY, 0)) != 0)
1361                 allerror = error;
1362         return (allerror);
1363 }
1364
1365 /*
1366  * Go through the disk queues to initiate sandbagged IO;
1367  * go through the inodes to write those that have been modified;
1368  * initiate the writing of the super block if it has been modified.
1369  *
1370  * Note: we are always called with the filesystem marked busy using
1371  * vfs_busy().
1372  */
1373 static int
1374 ffs_sync(mp, waitfor)
1375         struct mount *mp;
1376         int waitfor;
1377 {
1378         struct vnode *mvp, *vp, *devvp;
1379         struct thread *td;
1380         struct inode *ip;
1381         struct ufsmount *ump = VFSTOUFS(mp);
1382         struct fs *fs;
1383         int error, count, wait, lockreq, allerror = 0;
1384         int suspend;
1385         int suspended;
1386         int secondary_writes;
1387         int secondary_accwrites;
1388         int softdep_deps;
1389         int softdep_accdeps;
1390         struct bufobj *bo;
1391
1392         wait = 0;
1393         suspend = 0;
1394         suspended = 0;
1395         td = curthread;
1396         fs = ump->um_fs;
1397         if (fs->fs_fmod != 0 && fs->fs_ronly != 0)
1398                 panic("%s: ffs_sync: modification on read-only filesystem",
1399                     fs->fs_fsmnt);
1400         if (waitfor == MNT_LAZY)
1401                 return (ffs_sync_lazy(mp));
1402
1403         /*
1404          * Write back each (modified) inode.
1405          */
1406         lockreq = LK_EXCLUSIVE | LK_NOWAIT;
1407         if (waitfor == MNT_SUSPEND) {
1408                 suspend = 1;
1409                 waitfor = MNT_WAIT;
1410         }
1411         if (waitfor == MNT_WAIT) {
1412                 wait = 1;
1413                 lockreq = LK_EXCLUSIVE;
1414         }
1415         lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
1416         MNT_ILOCK(mp);
1417 loop:
1418         /* Grab snapshot of secondary write counts */
1419         secondary_writes = mp->mnt_secondary_writes;
1420         secondary_accwrites = mp->mnt_secondary_accwrites;
1421
1422         /* Grab snapshot of softdep dependency counts */
1423         MNT_IUNLOCK(mp);
1424         softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
1425         MNT_ILOCK(mp);
1426
1427         MNT_VNODE_FOREACH(vp, mp, mvp) {
1428                 /*
1429                  * Depend on the vnode interlock to keep things stable enough
1430                  * for a quick test.  Since there might be hundreds of
1431                  * thousands of vnodes, we cannot afford even a subroutine
1432                  * call unless there's a good chance that we have work to do.
1433                  */
1434                 VI_LOCK(vp);
1435                 if (vp->v_iflag & VI_DOOMED) {
1436                         VI_UNLOCK(vp);
1437                         continue;
1438                 }
1439                 ip = VTOI(vp);
1440                 if (vp->v_type == VNON || ((ip->i_flag &
1441                     (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1442                     vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
1443                         VI_UNLOCK(vp);
1444                         continue;
1445                 }
1446                 MNT_IUNLOCK(mp);
1447                 if ((error = vget(vp, lockreq, td)) != 0) {
1448                         MNT_ILOCK(mp);
1449                         if (error == ENOENT || error == ENOLCK) {
1450                                 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1451                                 goto loop;
1452                         }
1453                         continue;
1454                 }
1455                 if ((error = ffs_syncvnode(vp, waitfor)) != 0)
1456                         allerror = error;
1457                 vput(vp);
1458                 MNT_ILOCK(mp);
1459         }
1460         MNT_IUNLOCK(mp);
1461         /*
1462          * Force stale filesystem control information to be flushed.
1463          */
1464         if (waitfor == MNT_WAIT) {
1465                 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
1466                         allerror = error;
1467                 /* Flushed work items may create new vnodes to clean */
1468                 if (allerror == 0 && count) {
1469                         MNT_ILOCK(mp);
1470                         goto loop;
1471                 }
1472         }
1473 #ifdef QUOTA
1474         qsync(mp);
1475 #endif
1476
1477         devvp = ump->um_devvp;
1478         bo = &devvp->v_bufobj;
1479         BO_LOCK(bo);
1480         if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) {
1481                 BO_UNLOCK(bo);
1482                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1483                 if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
1484                         allerror = error;
1485                 VOP_UNLOCK(devvp, 0);
1486                 if (allerror == 0 && waitfor == MNT_WAIT) {
1487                         MNT_ILOCK(mp);
1488                         goto loop;
1489                 }
1490         } else if (suspend != 0) {
1491                 if (softdep_check_suspend(mp,
1492                                           devvp,
1493                                           softdep_deps,
1494                                           softdep_accdeps,
1495                                           secondary_writes,
1496                                           secondary_accwrites) != 0)
1497                         goto loop;      /* More work needed */
1498                 mtx_assert(MNT_MTX(mp), MA_OWNED);
1499                 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1500                 MNT_IUNLOCK(mp);
1501                 suspended = 1;
1502         } else
1503                 BO_UNLOCK(bo);
1504         /*
1505          * Write back modified superblock.
1506          */
1507         if (fs->fs_fmod != 0 &&
1508             (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
1509                 allerror = error;
1510         return (allerror);
1511 }
1512
1513 int
1514 ffs_vget(mp, ino, flags, vpp)
1515         struct mount *mp;
1516         ino_t ino;
1517         int flags;
1518         struct vnode **vpp;
1519 {
1520         return (ffs_vgetf(mp, ino, flags, vpp, 0));
1521 }
1522
1523 int
1524 ffs_vgetf(mp, ino, flags, vpp, ffs_flags)
1525         struct mount *mp;
1526         ino_t ino;
1527         int flags;
1528         struct vnode **vpp;
1529         int ffs_flags;
1530 {
1531         struct fs *fs;
1532         struct inode *ip;
1533         struct ufsmount *ump;
1534         struct buf *bp;
1535         struct vnode *vp;
1536         struct cdev *dev;
1537         int error;
1538
1539         error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
1540         if (error || *vpp != NULL)
1541                 return (error);
1542
1543         /*
1544          * We must promote to an exclusive lock for vnode creation.  This
1545          * can happen if lookup is passed LOCKSHARED.
1546          */
1547         if ((flags & LK_TYPE_MASK) == LK_SHARED) {
1548                 flags &= ~LK_TYPE_MASK;
1549                 flags |= LK_EXCLUSIVE;
1550         }
1551
1552         /*
1553          * We do not lock vnode creation as it is believed to be too
1554          * expensive for such rare case as simultaneous creation of vnode
1555          * for same ino by different processes. We just allow them to race
1556          * and check later to decide who wins. Let the race begin!
1557          */
1558
1559         ump = VFSTOUFS(mp);
1560         dev = ump->um_dev;
1561         fs = ump->um_fs;
1562
1563         /*
1564          * If this malloc() is performed after the getnewvnode()
1565          * it might block, leaving a vnode with a NULL v_data to be
1566          * found by ffs_sync() if a sync happens to fire right then,
1567          * which will cause a panic because ffs_sync() blindly
1568          * dereferences vp->v_data (as well it should).
1569          */
1570         ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
1571
1572         /* Allocate a new vnode/inode. */
1573         if (fs->fs_magic == FS_UFS1_MAGIC)
1574                 error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
1575         else
1576                 error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
1577         if (error) {
1578                 *vpp = NULL;
1579                 uma_zfree(uma_inode, ip);
1580                 return (error);
1581         }
1582         /*
1583          * FFS supports recursive locking.
1584          */
1585         VN_LOCK_AREC(vp);
1586         vp->v_data = ip;
1587         vp->v_bufobj.bo_bsize = fs->fs_bsize;
1588         ip->i_vnode = vp;
1589         ip->i_ump = ump;
1590         ip->i_fs = fs;
1591         ip->i_dev = dev;
1592         ip->i_number = ino;
1593         ip->i_ea_refs = 0;
1594 #ifdef QUOTA
1595         {
1596                 int i;
1597                 for (i = 0; i < MAXQUOTAS; i++)
1598                         ip->i_dquot[i] = NODQUOT;
1599         }
1600 #endif
1601
1602         lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1603         if (ffs_flags & FFSV_FORCEINSMQ)
1604                 vp->v_vflag |= VV_FORCEINSMQ;
1605         error = insmntque(vp, mp);
1606         if (error != 0) {
1607                 uma_zfree(uma_inode, ip);
1608                 *vpp = NULL;
1609                 return (error);
1610         }
1611         vp->v_vflag &= ~VV_FORCEINSMQ;
1612         error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
1613         if (error || *vpp != NULL)
1614                 return (error);
1615
1616         /* Read in the disk contents for the inode, copy into the inode. */
1617         error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1618             (int)fs->fs_bsize, NOCRED, &bp);
1619         if (error) {
1620                 /*
1621                  * The inode does not contain anything useful, so it would
1622                  * be misleading to leave it on its hash chain. With mode
1623                  * still zero, it will be unlinked and returned to the free
1624                  * list by vput().
1625                  */
1626                 brelse(bp);
1627                 vput(vp);
1628                 *vpp = NULL;
1629                 return (error);
1630         }
1631         if (ip->i_ump->um_fstype == UFS1)
1632                 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
1633         else
1634                 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
1635         ffs_load_inode(bp, ip, fs, ino);
1636         if (DOINGSOFTDEP(vp))
1637                 softdep_load_inodeblock(ip);
1638         else
1639                 ip->i_effnlink = ip->i_nlink;
1640         bqrelse(bp);
1641
1642         /*
1643          * Initialize the vnode from the inode, check for aliases.
1644          * Note that the underlying vnode may have changed.
1645          */
1646         if (ip->i_ump->um_fstype == UFS1)
1647                 error = ufs_vinit(mp, &ffs_fifoops1, &vp);
1648         else
1649                 error = ufs_vinit(mp, &ffs_fifoops2, &vp);
1650         if (error) {
1651                 vput(vp);
1652                 *vpp = NULL;
1653                 return (error);
1654         }
1655
1656         /*
1657          * Finish inode initialization.
1658          */
1659         if (vp->v_type != VFIFO) {
1660                 /* FFS supports shared locking for all files except fifos. */
1661                 VN_LOCK_ASHARE(vp);
1662         }
1663
1664         /*
1665          * Set up a generation number for this inode if it does not
1666          * already have one. This should only happen on old filesystems.
1667          */
1668         if (ip->i_gen == 0) {
1669                 ip->i_gen = arc4random() / 2 + 1;
1670                 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1671                         ip->i_flag |= IN_MODIFIED;
1672                         DIP_SET(ip, i_gen, ip->i_gen);
1673                 }
1674         }
1675         /*
1676          * Ensure that uid and gid are correct. This is a temporary
1677          * fix until fsck has been changed to do the update.
1678          */
1679         if (fs->fs_magic == FS_UFS1_MAGIC &&            /* XXX */
1680             fs->fs_old_inodefmt < FS_44INODEFMT) {      /* XXX */
1681                 ip->i_uid = ip->i_din1->di_ouid;        /* XXX */
1682                 ip->i_gid = ip->i_din1->di_ogid;        /* XXX */
1683         }                                               /* XXX */
1684
1685 #ifdef MAC
1686         if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
1687                 /*
1688                  * If this vnode is already allocated, and we're running
1689                  * multi-label, attempt to perform a label association
1690                  * from the extended attributes on the inode.
1691                  */
1692                 error = mac_vnode_associate_extattr(mp, vp);
1693                 if (error) {
1694                         /* ufs_inactive will release ip->i_devvp ref. */
1695                         vput(vp);
1696                         *vpp = NULL;
1697                         return (error);
1698                 }
1699         }
1700 #endif
1701
1702         *vpp = vp;
1703         return (0);
1704 }
1705
1706 /*
1707  * File handle to vnode
1708  *
1709  * Have to be really careful about stale file handles:
1710  * - check that the inode number is valid
1711  * - call ffs_vget() to get the locked inode
1712  * - check for an unallocated inode (i_mode == 0)
1713  * - check that the given client host has export rights and return
1714  *   those rights via. exflagsp and credanonp
1715  */
1716 static int
1717 ffs_fhtovp(mp, fhp, vpp)
1718         struct mount *mp;
1719         struct fid *fhp;
1720         struct vnode **vpp;
1721 {
1722         struct ufid *ufhp;
1723         struct fs *fs;
1724
1725         ufhp = (struct ufid *)fhp;
1726         fs = VFSTOUFS(mp)->um_fs;
1727         if (ufhp->ufid_ino < ROOTINO ||
1728             ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1729                 return (ESTALE);
1730         return (ufs_fhtovp(mp, ufhp, vpp));
1731 }
1732
1733 /*
1734  * Initialize the filesystem.
1735  */
1736 static int
1737 ffs_init(vfsp)
1738         struct vfsconf *vfsp;
1739 {
1740
1741         softdep_initialize();
1742         return (ufs_init(vfsp));
1743 }
1744
1745 /*
1746  * Undo the work of ffs_init().
1747  */
1748 static int
1749 ffs_uninit(vfsp)
1750         struct vfsconf *vfsp;
1751 {
1752         int ret;
1753
1754         ret = ufs_uninit(vfsp);
1755         softdep_uninitialize();
1756         return (ret);
1757 }
1758
1759 /*
1760  * Write a superblock and associated information back to disk.
1761  */
1762 int
1763 ffs_sbupdate(mp, waitfor, suspended)
1764         struct ufsmount *mp;
1765         int waitfor;
1766         int suspended;
1767 {
1768         struct fs *fs = mp->um_fs;
1769         struct buf *sbbp;
1770         struct buf *bp;
1771         int blks;
1772         void *space;
1773         int i, size, error, allerror = 0;
1774
1775         if (fs->fs_ronly == 1 &&
1776             (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) != 
1777             (MNT_RDONLY | MNT_UPDATE))
1778                 panic("ffs_sbupdate: write read-only filesystem");
1779         /*
1780          * We use the superblock's buf to serialize calls to ffs_sbupdate().
1781          */
1782         sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
1783             0, 0, 0);
1784         /*
1785          * First write back the summary information.
1786          */
1787         blks = howmany(fs->fs_cssize, fs->fs_fsize);
1788         space = fs->fs_csp;
1789         for (i = 0; i < blks; i += fs->fs_frag) {
1790                 size = fs->fs_bsize;
1791                 if (i + fs->fs_frag > blks)
1792                         size = (blks - i) * fs->fs_fsize;
1793                 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1794                     size, 0, 0, 0);
1795                 bcopy(space, bp->b_data, (u_int)size);
1796                 space = (char *)space + size;
1797                 if (suspended)
1798                         bp->b_flags |= B_VALIDSUSPWRT;
1799                 if (waitfor != MNT_WAIT)
1800                         bawrite(bp);
1801                 else if ((error = bwrite(bp)) != 0)
1802                         allerror = error;
1803         }
1804         /*
1805          * Now write back the superblock itself. If any errors occurred
1806          * up to this point, then fail so that the superblock avoids
1807          * being written out as clean.
1808          */
1809         if (allerror) {
1810                 brelse(sbbp);
1811                 return (allerror);
1812         }
1813         bp = sbbp;
1814         if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
1815             (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1816                 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1817                     fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
1818                 fs->fs_sblockloc = SBLOCK_UFS1;
1819         }
1820         if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
1821             (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
1822                 printf("WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
1823                     fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
1824                 fs->fs_sblockloc = SBLOCK_UFS2;
1825         }
1826         fs->fs_fmod = 0;
1827         fs->fs_time = time_second;
1828         bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1829         ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1830         if (suspended)
1831                 bp->b_flags |= B_VALIDSUSPWRT;
1832         if (waitfor != MNT_WAIT)
1833                 bawrite(bp);
1834         else if ((error = bwrite(bp)) != 0)
1835                 allerror = error;
1836         return (allerror);
1837 }
1838
1839 static int
1840 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
1841         int attrnamespace, const char *attrname)
1842 {
1843
1844 #ifdef UFS_EXTATTR
1845         return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
1846             attrname));
1847 #else
1848         return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
1849             attrname));
1850 #endif
1851 }
1852
1853 static void
1854 ffs_ifree(struct ufsmount *ump, struct inode *ip)
1855 {
1856
1857         if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
1858                 uma_zfree(uma_ufs1, ip->i_din1);
1859         else if (ip->i_din2 != NULL)
1860                 uma_zfree(uma_ufs2, ip->i_din2);
1861         uma_zfree(uma_inode, ip);
1862 }
1863
1864 static int dobkgrdwrite = 1;
1865 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
1866     "Do background writes (honoring the BV_BKGRDWRITE flag)?");
1867
1868 /*
1869  * Complete a background write started from bwrite.
1870  */
1871 static void
1872 ffs_backgroundwritedone(struct buf *bp)
1873 {
1874         struct bufobj *bufobj;
1875         struct buf *origbp;
1876
1877         /*
1878          * Find the original buffer that we are writing.
1879          */
1880         bufobj = bp->b_bufobj;
1881         BO_LOCK(bufobj);
1882         if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
1883                 panic("backgroundwritedone: lost buffer");
1884         /* Grab an extra reference to be dropped by the bufdone() below. */
1885         bufobj_wrefl(bufobj);
1886         BO_UNLOCK(bufobj);
1887         /*
1888          * Process dependencies then return any unfinished ones.
1889          */
1890         if (!LIST_EMPTY(&bp->b_dep))
1891                 buf_complete(bp);
1892 #ifdef SOFTUPDATES
1893         if (!LIST_EMPTY(&bp->b_dep))
1894                 softdep_move_dependencies(bp, origbp);
1895 #endif
1896         /*
1897          * This buffer is marked B_NOCACHE so when it is released
1898          * by biodone it will be tossed.
1899          */
1900         bp->b_flags |= B_NOCACHE;
1901         bp->b_flags &= ~B_CACHE;
1902         bufdone(bp);
1903         BO_LOCK(bufobj);
1904         /*
1905          * Clear the BV_BKGRDINPROG flag in the original buffer
1906          * and awaken it if it is waiting for the write to complete.
1907          * If BV_BKGRDINPROG is not set in the original buffer it must
1908          * have been released and re-instantiated - which is not legal.
1909          */
1910         KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
1911             ("backgroundwritedone: lost buffer2"));
1912         origbp->b_vflags &= ~BV_BKGRDINPROG;
1913         if (origbp->b_vflags & BV_BKGRDWAIT) {
1914                 origbp->b_vflags &= ~BV_BKGRDWAIT;
1915                 wakeup(&origbp->b_xflags);
1916         }
1917         BO_UNLOCK(bufobj);
1918 }
1919
1920
1921 /*
1922  * Write, release buffer on completion.  (Done by iodone
1923  * if async).  Do not bother writing anything if the buffer
1924  * is invalid.
1925  *
1926  * Note that we set B_CACHE here, indicating that buffer is
1927  * fully valid and thus cacheable.  This is true even of NFS
1928  * now so we set it generally.  This could be set either here 
1929  * or in biodone() since the I/O is synchronous.  We put it
1930  * here.
1931  */
1932 static int
1933 ffs_bufwrite(struct buf *bp)
1934 {
1935         int oldflags, s;
1936         struct buf *newbp;
1937
1938         CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1939         if (bp->b_flags & B_INVAL) {
1940                 brelse(bp);
1941                 return (0);
1942         }
1943
1944         oldflags = bp->b_flags;
1945
1946         if (!BUF_ISLOCKED(bp))
1947                 panic("bufwrite: buffer is not busy???");
1948         s = splbio();
1949         /*
1950          * If a background write is already in progress, delay
1951          * writing this block if it is asynchronous. Otherwise
1952          * wait for the background write to complete.
1953          */
1954         BO_LOCK(bp->b_bufobj);
1955         if (bp->b_vflags & BV_BKGRDINPROG) {
1956                 if (bp->b_flags & B_ASYNC) {
1957                         BO_UNLOCK(bp->b_bufobj);
1958                         splx(s);
1959                         bdwrite(bp);
1960                         return (0);
1961                 }
1962                 bp->b_vflags |= BV_BKGRDWAIT;
1963                 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
1964                 if (bp->b_vflags & BV_BKGRDINPROG)
1965                         panic("bufwrite: still writing");
1966         }
1967         BO_UNLOCK(bp->b_bufobj);
1968
1969         /* Mark the buffer clean */
1970         bundirty(bp);
1971
1972         /*
1973          * If this buffer is marked for background writing and we
1974          * do not have to wait for it, make a copy and write the
1975          * copy so as to leave this buffer ready for further use.
1976          *
1977          * This optimization eats a lot of memory.  If we have a page
1978          * or buffer shortfall we can't do it.
1979          */
1980         if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) && 
1981             (bp->b_flags & B_ASYNC) &&
1982             !vm_page_count_severe() &&
1983             !buf_dirty_count_severe()) {
1984                 KASSERT(bp->b_iodone == NULL,
1985                     ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
1986
1987                 /* get a new block */
1988                 newbp = geteblk(bp->b_bufsize, GB_NOWAIT_BD);
1989                 if (newbp == NULL)
1990                         goto normal_write;
1991
1992                 /*
1993                  * set it to be identical to the old block.  We have to
1994                  * set b_lblkno and BKGRDMARKER before calling bgetvp()
1995                  * to avoid confusing the splay tree and gbincore().
1996                  */
1997                 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
1998                 newbp->b_lblkno = bp->b_lblkno;
1999                 newbp->b_xflags |= BX_BKGRDMARKER;
2000                 BO_LOCK(bp->b_bufobj);
2001                 bp->b_vflags |= BV_BKGRDINPROG;
2002                 bgetvp(bp->b_vp, newbp);
2003                 BO_UNLOCK(bp->b_bufobj);
2004                 newbp->b_bufobj = &bp->b_vp->v_bufobj;
2005                 newbp->b_blkno = bp->b_blkno;
2006                 newbp->b_offset = bp->b_offset;
2007                 newbp->b_iodone = ffs_backgroundwritedone;
2008                 newbp->b_flags |= B_ASYNC;
2009                 newbp->b_flags &= ~B_INVAL;
2010
2011 #ifdef SOFTUPDATES
2012                 /* move over the dependencies */
2013                 if (!LIST_EMPTY(&bp->b_dep))
2014                         softdep_move_dependencies(bp, newbp);
2015 #endif 
2016
2017                 /*
2018                  * Initiate write on the copy, release the original to
2019                  * the B_LOCKED queue so that it cannot go away until
2020                  * the background write completes. If not locked it could go
2021                  * away and then be reconstituted while it was being written.
2022                  * If the reconstituted buffer were written, we could end up
2023                  * with two background copies being written at the same time.
2024                  */
2025                 bqrelse(bp);
2026                 bp = newbp;
2027         }
2028
2029         /* Let the normal bufwrite do the rest for us */
2030 normal_write:
2031         return (bufwrite(bp));
2032 }
2033
2034
2035 static void
2036 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
2037 {
2038         struct vnode *vp;
2039         int error;
2040         struct buf *tbp;
2041
2042         vp = bo->__bo_vnode;
2043         if (bp->b_iocmd == BIO_WRITE) {
2044                 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
2045                     bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
2046                     (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
2047                         panic("ffs_geom_strategy: bad I/O");
2048                 bp->b_flags &= ~B_VALIDSUSPWRT;
2049                 if ((vp->v_vflag & VV_COPYONWRITE) &&
2050                     vp->v_rdev->si_snapdata != NULL) {
2051                         if ((bp->b_flags & B_CLUSTER) != 0) {
2052                                 runningbufwakeup(bp);
2053                                 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2054                                               b_cluster.cluster_entry) {
2055                                         error = ffs_copyonwrite(vp, tbp);
2056                                         if (error != 0 &&
2057                                             error != EOPNOTSUPP) {
2058                                                 bp->b_error = error;
2059                                                 bp->b_ioflags |= BIO_ERROR;
2060                                                 bufdone(bp);
2061                                                 return;
2062                                         }
2063                                 }
2064                                 bp->b_runningbufspace = bp->b_bufsize;
2065                                 atomic_add_long(&runningbufspace,
2066                                                bp->b_runningbufspace);
2067                         } else {
2068                                 error = ffs_copyonwrite(vp, bp);
2069                                 if (error != 0 && error != EOPNOTSUPP) {
2070                                         bp->b_error = error;
2071                                         bp->b_ioflags |= BIO_ERROR;
2072                                         bufdone(bp);
2073                                         return;
2074                                 }
2075                         }
2076                 }
2077 #ifdef SOFTUPDATES
2078                 if ((bp->b_flags & B_CLUSTER) != 0) {
2079                         TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
2080                                       b_cluster.cluster_entry) {
2081                                 if (!LIST_EMPTY(&tbp->b_dep))
2082                                         buf_start(tbp);
2083                         }
2084                 } else {
2085                         if (!LIST_EMPTY(&bp->b_dep))
2086                                 buf_start(bp);
2087                 }
2088
2089 #endif
2090         }
2091         g_vfs_strategy(bo, bp);
2092 }
2093
2094 #ifdef  DDB
2095
2096 static void
2097 db_print_ffs(struct ufsmount *ump)
2098 {
2099         db_printf("mp %p %s devvp %p fs %p su_wl %d su_wl_in %d su_deps %d "
2100                   "su_req %d\n",
2101             ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
2102             ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
2103             ump->softdep_on_worklist_inprogress, ump->softdep_deps,
2104             ump->softdep_req);
2105 }
2106
2107 DB_SHOW_COMMAND(ffs, db_show_ffs)
2108 {
2109         struct mount *mp;
2110         struct ufsmount *ump;
2111
2112         if (have_addr) {
2113                 ump = VFSTOUFS((struct mount *)addr);
2114                 db_print_ffs(ump);
2115                 return;
2116         }
2117
2118         TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2119                 if (!strcmp(mp->mnt_stat.f_fstypename, ufs_vfsconf.vfc_name))
2120                         db_print_ffs(VFSTOUFS(mp));
2121         }
2122 }
2123
2124 #endif  /* DDB */