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