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