Remove the thread argument from ffs_flushfiles(), ffs_mountfs(),
[dragonfly.git] / sys / vfs / ufs / 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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)ffs_vfsops.c        8.31 (Berkeley) 5/20/95
34  * $FreeBSD: src/sys/ufs/ffs/ffs_vfsops.c,v 1.117.2.10 2002/06/23 22:34:52 iedowse Exp $
35  * $DragonFly: src/sys/vfs/ufs/ffs_vfsops.c,v 1.42 2006/05/06 16:20:19 dillon Exp $
36  */
37
38 #include "opt_quota.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/nlookup.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/buf.h>
48 #include <sys/conf.h>
49 #include <sys/fcntl.h>
50 #include <sys/disklabel.h>
51 #include <sys/malloc.h>
52
53 #include "quota.h"
54 #include "ufsmount.h"
55 #include "inode.h"
56 #include "ufs_extern.h"
57
58 #include "fs.h"
59 #include "ffs_extern.h"
60
61 #include <vm/vm.h>
62 #include <vm/vm_page.h>
63 #include <vm/vm_zone.h>
64
65 static MALLOC_DEFINE(M_FFSNODE, "FFS node", "FFS vnode private part");
66
67 static int      ffs_sbupdate (struct ufsmount *, int);
68 static int      ffs_reload (struct mount *, struct ucred *);
69 static int      ffs_oldfscompat (struct fs *);
70 static int      ffs_mount (struct mount *, char *, caddr_t, struct thread *);
71 static int      ffs_init (struct vfsconf *);
72
73 static struct vfsops ufs_vfsops = {
74         .vfs_mount =            ffs_mount,
75         .vfs_unmount =          ffs_unmount,
76         .vfs_root =             ufs_root,
77         .vfs_quotactl =         ufs_quotactl,
78         .vfs_statfs =           ffs_statfs,
79         .vfs_sync =             ffs_sync,
80         .vfs_vget =             ffs_vget,
81         .vfs_fhtovp =           ffs_fhtovp,
82         .vfs_checkexp =         ufs_check_export,
83         .vfs_vptofh =           ffs_vptofh,
84         .vfs_init =             ffs_init,
85         .vfs_uninit =           ufs_uninit
86 };
87
88 VFS_SET(ufs_vfsops, ufs, 0);
89
90 extern struct vnodeopv_entry_desc ffs_vnodeop_entries[];
91 extern struct vnodeopv_entry_desc ffs_specop_entries[];
92 extern struct vnodeopv_entry_desc ffs_fifoop_entries[];
93
94
95 /*
96  * ffs_mount
97  *
98  * Called when mounting local physical media
99  *
100  * PARAMETERS:
101  *              mountroot
102  *                      mp      mount point structure
103  *                      path    NULL (flag for root mount!!!)
104  *                      data    <unused>
105  *                      p       process (user credentials check [statfs])
106  *
107  *              mount
108  *                      mp      mount point structure
109  *                      path    path to mount point
110  *                      data    pointer to argument struct in user space
111  *                      p       process (user credentials check)
112  *
113  * RETURNS:     0       Success
114  *              !0      error number (errno.h)
115  *
116  * LOCK STATE:
117  *
118  *              ENTRY
119  *                      mount point is locked
120  *              EXIT
121  *                      mount point is locked
122  *
123  * NOTES:
124  *              A NULL path can be used for a flag since the mount
125  *              system call will fail with EFAULT in copyinstr in
126  *              nlookup() if it is a genuine NULL from the user.
127  */
128 static int
129 ffs_mount(struct mount *mp,             /* mount struct pointer */
130           char *path,                   /* path to mount point */
131           caddr_t data,                 /* arguments to FS specific mount */
132           struct thread *td)            /* process requesting mount */
133 {
134         size_t          size;
135         int             error;
136         struct vnode    *devvp;
137
138         struct ufs_args args;
139         struct ufsmount *ump = 0;
140         struct fs *fs;
141         int flags, ronly = 0;
142         mode_t accessmode;
143         struct ucred *cred;
144         struct nlookupdata nd;
145         struct vnode *rootvp;
146
147         KKASSERT(td->td_proc);
148         cred = td->td_proc->p_ucred;
149         devvp = NULL;
150         error = 0;
151
152         /*
153          * Use NULL path to flag a root mount
154          */
155         if (path == NULL) {
156                 /*
157                  ***
158                  * Mounting root filesystem
159                  ***
160                  */
161         
162                 if ((error = bdevvp(rootdev, &rootvp))) {
163                         printf("ffs_mountroot: can't find rootvp\n");
164                         return (error);
165                 }
166
167                 if( ( error = ffs_mountfs(rootvp, mp, M_FFSNODE)) != 0) {
168                         /* fs specific cleanup (if any)*/
169                         goto error_1;
170                 }
171                 devvp = rootvp;
172
173                 goto dostatfs;          /* success*/
174
175         }
176
177         /*
178          ***
179          * Mounting non-root filesystem or updating a filesystem
180          ***
181          */
182
183         /* copy in user arguments*/
184         error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
185         if (error)
186                 goto error_1;           /* can't get arguments*/
187
188         /*
189          * If updating, check whether changing from read-only to
190          * read/write; if there is no device name, that's all we do.
191          */
192         if (mp->mnt_flag & MNT_UPDATE) {
193                 ump = VFSTOUFS(mp);
194                 fs = ump->um_fs;
195                 devvp = ump->um_devvp;
196                 error = 0;
197                 ronly = fs->fs_ronly;   /* MNT_RELOAD might change this */
198                 if (ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
199                         /*
200                          * Flush any dirty data.
201                          */
202                         VFS_SYNC(mp, MNT_WAIT);
203                         /*
204                          * Check for and optionally get rid of files open
205                          * for writing.
206                          */
207                         flags = WRITECLOSE;
208                         if (mp->mnt_flag & MNT_FORCE)
209                                 flags |= FORCECLOSE;
210                         if (mp->mnt_flag & MNT_SOFTDEP) {
211                                 error = softdep_flushfiles(mp, flags);
212                         } else {
213                                 error = ffs_flushfiles(mp, flags);
214                         }
215                         ronly = 1;
216                 }
217                 if (!error && (mp->mnt_flag & MNT_RELOAD)) {
218                         error = ffs_reload(mp, NULL);
219                 }
220                 if (error) {
221                         goto error_1;
222                 }
223                 if (ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
224                         /*
225                          * If upgrade to read-write by non-root, then verify
226                          * that user has necessary permissions on the device.
227                          */
228                         if (cred->cr_uid != 0) {
229                                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
230                                 if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
231                                     cred)) != 0) {
232                                         VOP_UNLOCK(devvp, 0);
233                                         return (error);
234                                 }
235                                 VOP_UNLOCK(devvp, 0);
236                         }
237
238                         fs->fs_flags &= ~FS_UNCLEAN;
239                         if (fs->fs_clean == 0) {
240                                 fs->fs_flags |= FS_UNCLEAN;
241                                 if (mp->mnt_flag & MNT_FORCE) {
242                                         printf(
243 "WARNING: %s was not properly dismounted\n",
244                                             fs->fs_fsmnt);
245                                 } else {
246                                         printf(
247 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
248                                             fs->fs_fsmnt);
249                                         error = EPERM;
250                                         goto error_1;
251                                 }
252                         }
253
254                         /* check to see if we need to start softdep */
255                         if (fs->fs_flags & FS_DOSOFTDEP) {
256                                 error = softdep_mount(devvp, mp, fs);
257                                 if (error)
258                                         goto error_1;
259                         }
260                         ronly = 0;
261                 }
262                 /*
263                  * Soft updates is incompatible with "async",
264                  * so if we are doing softupdates stop the user
265                  * from setting the async flag in an update.
266                  * Softdep_mount() clears it in an initial mount 
267                  * or ro->rw remount.
268                  */
269                 if (mp->mnt_flag & MNT_SOFTDEP) {
270                         mp->mnt_flag &= ~MNT_ASYNC;
271                 }
272                 /* if not updating name...*/
273                 if (args.fspec == 0) {
274                         /*
275                          * Process export requests.  Jumping to "success"
276                          * will return the vfs_export() error code.
277                          */
278                         error = vfs_export(mp, &ump->um_export, &args.export);
279                         goto success;
280                 }
281         }
282
283         /*
284          * Not an update, or updating the name: look up the name
285          * and verify that it refers to a sensible block device.
286          */
287         devvp = NULL;
288         error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
289         if (error == 0)
290                 error = nlookup(&nd);
291         if (error == 0)
292                 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp);
293         nlookup_done(&nd);
294         if (error)
295                 goto error_1;
296
297         if (!vn_isdisk(devvp, &error))
298                 goto error_2;
299
300         /*
301          * If mount by non-root, then verify that user has necessary
302          * permissions on the device.
303          */
304         if (cred->cr_uid != 0) {
305                 accessmode = VREAD;
306                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
307                         accessmode |= VWRITE;
308                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
309                 if ((error = VOP_ACCESS(devvp, accessmode, cred)) != 0) {
310                         vput(devvp);
311                         return (error);
312                 }
313                 VOP_UNLOCK(devvp, 0);
314         }
315
316         if (mp->mnt_flag & MNT_UPDATE) {
317                 /*
318                  * UPDATE - make sure the resolved vnode represents the same
319                  * device.  Note that devvp->v_rdev may be NULL since we 
320                  * haven't opened it, so compare udev instead.
321                  *
322                  * Our current open/writecount state is associated with
323                  * um_devvp, so continue using um_devvp and throw away devvp.
324                  */
325                 if (devvp != ump->um_devvp) {
326                         if (devvp->v_udev == ump->um_devvp->v_udev) {
327                                 vrele(devvp);
328                                 devvp = ump->um_devvp;
329                         } else {
330                                 printf("cannot update mount, udev does"
331                                         " not match %08x vs %08x\n",
332                                         devvp->v_udev, ump->um_devvp->v_udev);
333                                 error = EINVAL; /* needs translation */
334                         }
335                 } else {
336                         vrele(devvp);
337                 }
338                 /*
339                  * Update device name only on success
340                  */
341                 if (!error) {
342                         /* Save "mounted from" info for mount point (NULL pad)*/
343                         copyinstr(      args.fspec,
344                                         mp->mnt_stat.f_mntfromname,
345                                         MNAMELEN - 1,
346                                         &size);
347                         bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
348                 }
349         } else {
350                 /*
351                  ********************
352                  * NEW MOUNT
353                  ********************
354                  */
355
356                 /* Save "mounted from" info for mount point (NULL pad)*/
357                 copyinstr(      args.fspec,                     /* device name*/
358                                 mp->mnt_stat.f_mntfromname,     /* save area*/
359                                 MNAMELEN - 1,                   /* max size*/
360                                 &size);                         /* real size*/
361                 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
362
363                 error = ffs_mountfs(devvp, mp, M_FFSNODE);
364         }
365         if (error) {
366                 goto error_2;
367         }
368
369 dostatfs:
370         /*
371          * Initialize FS stat information in mount struct; uses
372          * mp->mnt_stat.f_mntfromname.
373          *
374          * This code is common to root and non-root mounts
375          */
376         (void)VFS_STATFS(mp, &mp->mnt_stat, td);
377
378         goto success;
379
380
381 error_2:        /* error with devvp held*/
382
383         /* release devvp before failing*/
384         vrele(devvp);
385
386 error_1:        /* no state to back out*/
387
388 success:
389         if (!error && path && (mp->mnt_flag & MNT_UPDATE)) {
390                 /* Update clean flag after changing read-onlyness. */
391                 fs = ump->um_fs;
392                 if (ronly != fs->fs_ronly) {
393                         fs->fs_ronly = ronly;
394                         fs->fs_clean = ronly &&
395                             (fs->fs_flags & FS_UNCLEAN) == 0 ? 1 : 0;
396
397                         /*
398                          * The device must be re-opened as appropriate or
399                          * the device close at unmount time will panic.
400                          */
401                         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
402                         if (ronly) {
403                                 VOP_OPEN(devvp, FREAD, FSCRED, NULL);
404                                 VOP_CLOSE(devvp, FREAD|FWRITE);
405                         } else {
406                                 VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
407                                 VOP_CLOSE(devvp, FREAD);
408                         }
409                         VOP_UNLOCK(devvp, 0);
410                         ffs_sbupdate(ump, MNT_WAIT);
411                 }
412         }
413         return (error);
414 }
415
416 /*
417  * Reload all incore data for a filesystem (used after running fsck on
418  * the root filesystem and finding things to fix). The filesystem must
419  * be mounted read-only.
420  *
421  * Things to do to update the mount:
422  *      1) invalidate all cached meta-data.
423  *      2) re-read superblock from disk.
424  *      3) re-read summary information from disk.
425  *      4) invalidate all inactive vnodes.
426  *      5) invalidate all cached file data.
427  *      6) re-read inode data for all active vnodes.
428  */
429
430 static int ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data);
431
432 struct scaninfo {
433         int rescan;
434         struct fs *fs;
435         struct vnode *devvp;
436         int waitfor;
437         int allerror;
438 };
439
440 static int
441 ffs_reload(struct mount *mp, struct ucred *cred)
442 {
443         struct vnode *devvp;
444         void *space;
445         struct buf *bp;
446         struct fs *fs, *newfs;
447         struct partinfo dpart;
448         dev_t dev;
449         int i, blks, size, error;
450         struct scaninfo scaninfo;
451         int32_t *lp;
452
453         if ((mp->mnt_flag & MNT_RDONLY) == 0)
454                 return (EINVAL);
455         /*
456          * Step 1: invalidate all cached meta-data.
457          */
458         devvp = VFSTOUFS(mp)->um_devvp;
459         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
460         error = vinvalbuf(devvp, 0, 0, 0);
461         VOP_UNLOCK(devvp, 0);
462         if (error)
463                 panic("ffs_reload: dirty1");
464
465         dev = devvp->v_rdev;
466
467         /*
468          * The backing device must be VMIO-capable because we use getblk().
469          * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
470          */
471         if (devvp->v_object == NULL)
472                 panic("ffs_reload: devvp has no VM object!");
473
474         /*
475          * Step 2: re-read superblock from disk.
476          */
477         if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED) != 0)
478                 size = DEV_BSIZE;
479         else
480                 size = dpart.disklab->d_secsize;
481         if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0) {
482                 brelse(bp);
483                 return (error);
484         }
485         newfs = (struct fs *)bp->b_data;
486         if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
487                 newfs->fs_bsize < sizeof(struct fs)) {
488                         brelse(bp);
489                         return (EIO);           /* XXX needs translation */
490         }
491         fs = VFSTOUFS(mp)->um_fs;
492         /*
493          * Copy pointer fields back into superblock before copying in   XXX
494          * new superblock. These should really be in the ufsmount.      XXX
495          * Note that important parameters (eg fs_ncg) are unchanged.
496          */
497         newfs->fs_csp = fs->fs_csp;
498         newfs->fs_maxcluster = fs->fs_maxcluster;
499         newfs->fs_contigdirs = fs->fs_contigdirs;
500         /* The filesystem is still read-only. */
501         newfs->fs_ronly = 1;
502         bcopy(newfs, fs, (uint)fs->fs_sbsize);
503         if (fs->fs_sbsize < SBSIZE)
504                 bp->b_flags |= B_INVAL;
505         brelse(bp);
506         mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
507         ffs_oldfscompat(fs);
508         /* An old fsck may have zeroed these fields, so recheck them. */
509         if (fs->fs_avgfilesize <= 0)            /* XXX */
510                 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
511         if (fs->fs_avgfpdir <= 0)               /* XXX */
512                 fs->fs_avgfpdir = AFPDIR;       /* XXX */
513
514         /*
515          * Step 3: re-read summary information from disk.
516          */
517         blks = howmany(fs->fs_cssize, fs->fs_fsize);
518         space = fs->fs_csp;
519         for (i = 0; i < blks; i += fs->fs_frag) {
520                 size = fs->fs_bsize;
521                 if (i + fs->fs_frag > blks)
522                         size = (blks - i) * fs->fs_fsize;
523                 error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i), size, &bp);
524                 if (error) {
525                         brelse(bp);
526                         return (error);
527                 }
528                 bcopy(bp->b_data, space, (uint)size);
529                 space = (char *)space + size;
530                 brelse(bp);
531         }
532         /*
533          * We no longer know anything about clusters per cylinder group.
534          */
535         if (fs->fs_contigsumsize > 0) {
536                 lp = fs->fs_maxcluster;
537                 for (i = 0; i < fs->fs_ncg; i++)
538                         *lp++ = fs->fs_contigsumsize;
539         }
540
541         scaninfo.rescan = 0;
542         scaninfo.fs = fs;
543         scaninfo.devvp = devvp;
544         while (error == 0 && scaninfo.rescan) {
545                 scaninfo.rescan = 0;
546                 error = vmntvnodescan(mp, VMSC_GETVX, 
547                                         NULL, ffs_reload_scan2, &scaninfo);
548         }
549         return(error);
550 }
551
552 static int
553 ffs_reload_scan2(struct mount *mp, struct vnode *vp, void *data)
554 {
555         struct scaninfo *info = data;
556         struct inode *ip;
557         struct buf *bp;
558         int error;
559
560         /*
561          * Try to recycle
562          */
563         if (vrecycle(vp))
564                 return(0);
565
566         if (vinvalbuf(vp, 0, 0, 0))
567                 panic("ffs_reload: dirty2");
568         /*
569          * Step 6: re-read inode data for all active vnodes.
570          */
571         ip = VTOI(vp);
572         error = bread(info->devvp,
573                     fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
574                     (int)info->fs->fs_bsize, &bp);
575         if (error) {
576                 brelse(bp);
577                 return (error);
578         }
579         ip->i_din = *((struct ufs1_dinode *)bp->b_data +
580             ino_to_fsbo(info->fs, ip->i_number));
581         ip->i_effnlink = ip->i_nlink;
582         brelse(bp);
583         return(0);
584 }
585
586 /*
587  * Common code for mount and mountroot
588  */
589 int
590 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct malloc_type *mtype)
591 {
592         struct ufsmount *ump;
593         struct buf *bp;
594         struct fs *fs;
595         dev_t dev;
596         struct partinfo dpart;
597         void *space;
598         int error, i, blks, size, ronly;
599         int32_t *lp;
600         uint64_t maxfilesize;                                   /* XXX */
601
602         /*
603          * Disallow multiple mounts of the same device.
604          * Disallow mounting of a device that is currently in use
605          * Flush out any old buffers remaining from a previous use.
606          */
607         error = vfs_mountedon(devvp);
608         if (error)
609                 return (error);
610         if (count_udev(devvp->v_udev) > 0)
611                 return (EBUSY);
612         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
613         error = vinvalbuf(devvp, V_SAVE, 0, 0);
614         VOP_UNLOCK(devvp, 0);
615         if (error)
616                 return (error);
617
618         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
619         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
620         error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
621         VOP_UNLOCK(devvp, 0);
622         if (error)
623                 return (error);
624         dev = devvp->v_rdev;
625         if (dev->si_iosize_max != 0)
626                 mp->mnt_iosize_max = dev->si_iosize_max;
627         if (mp->mnt_iosize_max > MAXPHYS)
628                 mp->mnt_iosize_max = MAXPHYS;
629
630         /*
631          * The backing device must be VMIO-capable because we use getblk().
632          * NOTE: the MFS driver now returns a VMIO-enabled descriptor.
633          * The VOP_OPEN() call above should have associated a VM object
634          * with devvp.
635          */
636         if (devvp->v_object == NULL)
637                 panic("ffs_reload: devvp has no VM object!");
638
639         if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, proc0.p_ucred) != 0)
640                 size = DEV_BSIZE;
641         else
642                 size = dpart.disklab->d_secsize;
643
644         bp = NULL;
645         ump = NULL;
646         if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
647                 goto out;
648         fs = (struct fs *)bp->b_data;
649         if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
650             fs->fs_bsize < sizeof(struct fs)) {
651                 error = EINVAL;         /* XXX needs translation */
652                 goto out;
653         }
654         fs->fs_fmod = 0;
655         fs->fs_flags &= ~FS_UNCLEAN;
656         if (fs->fs_clean == 0) {
657                 fs->fs_flags |= FS_UNCLEAN;
658                 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
659                         printf(
660 "WARNING: %s was not properly dismounted\n",
661                             fs->fs_fsmnt);
662                 } else {
663                         printf(
664 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
665                             fs->fs_fsmnt);
666                         error = EPERM;
667                         goto out;
668                 }
669         }
670         /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
671         if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
672                 error = EROFS;          /* needs translation */
673                 goto out;
674         }
675         ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
676         bzero((caddr_t)ump, sizeof *ump);
677         ump->um_malloctype = mtype;
678         ump->um_i_effnlink_valid = 1;
679         ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
680             M_WAITOK);
681         ump->um_blkatoff = ffs_blkatoff;
682         ump->um_truncate = ffs_truncate;
683         ump->um_update = ffs_update;
684         ump->um_valloc = ffs_valloc;
685         ump->um_vfree = ffs_vfree;
686         bcopy(bp->b_data, ump->um_fs, (uint)fs->fs_sbsize);
687         if (fs->fs_sbsize < SBSIZE)
688                 bp->b_flags |= B_INVAL;
689         brelse(bp);
690         bp = NULL;
691         fs = ump->um_fs;
692         fs->fs_ronly = ronly;
693         size = fs->fs_cssize;
694         blks = howmany(size, fs->fs_fsize);
695         if (fs->fs_contigsumsize > 0)
696                 size += fs->fs_ncg * sizeof(int32_t);
697         size += fs->fs_ncg * sizeof(uint8_t);
698         space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
699         fs->fs_csp = space;
700         for (i = 0; i < blks; i += fs->fs_frag) {
701                 size = fs->fs_bsize;
702                 if (i + fs->fs_frag > blks)
703                         size = (blks - i) * fs->fs_fsize;
704                 if ((error = bread(devvp, fsbtodoff(fs, fs->fs_csaddr + i),
705                                    size, &bp)) != 0) {
706                         free(fs->fs_csp, M_UFSMNT);
707                         goto out;
708                 }
709                 bcopy(bp->b_data, space, (uint)size);
710                 space = (char *)space + size;
711                 brelse(bp);
712                 bp = NULL;
713         }
714         if (fs->fs_contigsumsize > 0) {
715                 fs->fs_maxcluster = lp = space;
716                 for (i = 0; i < fs->fs_ncg; i++)
717                         *lp++ = fs->fs_contigsumsize;
718                 space = lp;
719         }
720         size = fs->fs_ncg * sizeof(uint8_t);
721         fs->fs_contigdirs = (uint8_t *)space;
722         bzero(fs->fs_contigdirs, size);
723         /* Compatibility for old filesystems       XXX */
724         if (fs->fs_avgfilesize <= 0)            /* XXX */
725                 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
726         if (fs->fs_avgfpdir <= 0)               /* XXX */
727                 fs->fs_avgfpdir = AFPDIR;       /* XXX */
728         mp->mnt_data = (qaddr_t)ump;
729         mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
730         mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
731         if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 || 
732             vfs_getvfs(&mp->mnt_stat.f_fsid)) 
733                 vfs_getnewfsid(mp);
734         mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
735         mp->mnt_flag |= MNT_LOCAL;
736         ump->um_mountp = mp;
737         ump->um_dev = dev;
738         ump->um_devvp = devvp;
739         ump->um_nindir = fs->fs_nindir;
740         ump->um_bptrtodb = fs->fs_fsbtodb;
741         ump->um_seqinc = fs->fs_frag;
742         for (i = 0; i < MAXQUOTAS; i++)
743                 ump->um_quotas[i] = NULLVP;
744         dev->si_mountpoint = mp;
745         ffs_oldfscompat(fs);
746
747         if( mp->mnt_flag & MNT_ROOTFS) {
748                 /*
749                  * Root mount; update timestamp in mount structure.
750                  * this will be used by the common root mount code
751                  * to update the system clock.
752                  */
753                 mp->mnt_time = fs->fs_time;
754         }
755
756         ump->um_savedmaxfilesize = fs->fs_maxfilesize;          /* XXX */
757         maxfilesize = (uint64_t)0x40000000 * fs->fs_bsize - 1;  /* XXX */
758         /* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */
759         if (maxfilesize > (uint64_t)0x80000000u * PAGE_SIZE - 1)
760                 maxfilesize = (uint64_t)0x80000000u * PAGE_SIZE - 1;
761         if (fs->fs_maxfilesize > maxfilesize)                   /* XXX */
762                 fs->fs_maxfilesize = maxfilesize;               /* XXX */
763         if (ronly == 0) {
764                 if ((fs->fs_flags & FS_DOSOFTDEP) &&
765                     (error = softdep_mount(devvp, mp, fs)) != 0) {
766                         free(fs->fs_csp, M_UFSMNT);
767                         goto out;
768                 }
769                 fs->fs_fmod = 1;
770                 fs->fs_clean = 0;
771                 (void) ffs_sbupdate(ump, MNT_WAIT);
772         }
773         vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops, ffs_vnodeop_entries, VVF_SUPPORTS_FSMID);
774         vfs_add_vnodeops(mp, &mp->mnt_vn_spec_ops, ffs_specop_entries, VVF_SUPPORTS_FSMID);
775         vfs_add_vnodeops(mp, &mp->mnt_vn_fifo_ops, ffs_fifoop_entries, VVF_SUPPORTS_FSMID); 
776
777         return (0);
778 out:
779         dev->si_mountpoint = NULL;
780         if (bp)
781                 brelse(bp);
782         VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE);
783         if (ump) {
784                 free(ump->um_fs, M_UFSMNT);
785                 free(ump, M_UFSMNT);
786                 mp->mnt_data = (qaddr_t)0;
787         }
788         return (error);
789 }
790
791 /*
792  * Sanity checks for old filesystems.
793  *
794  * XXX - goes away some day.
795  */
796 static int
797 ffs_oldfscompat(struct fs *fs)
798 {
799         fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);       /* XXX */
800         fs->fs_interleave = max(fs->fs_interleave, 1);          /* XXX */
801         if (fs->fs_postblformat == FS_42POSTBLFMT)              /* XXX */
802                 fs->fs_nrpos = 8;                               /* XXX */
803         if (fs->fs_inodefmt < FS_44INODEFMT) {                  /* XXX */
804 #if 0
805                 int i;                                          /* XXX */
806                 uint64_t sizepb = fs->fs_bsize;         /* XXX */
807                                                                 /* XXX */
808                 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
809                 for (i = 0; i < NIADDR; i++) {                  /* XXX */
810                         sizepb *= NINDIR(fs);                   /* XXX */
811                         fs->fs_maxfilesize += sizepb;           /* XXX */
812                 }                                               /* XXX */
813 #endif
814                 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
815                 fs->fs_qbmask = ~fs->fs_bmask;                  /* XXX */
816                 fs->fs_qfmask = ~fs->fs_fmask;                  /* XXX */
817         }                                                       /* XXX */
818         return (0);
819 }
820
821 /*
822  * unmount system call
823  */
824 int
825 ffs_unmount(struct mount *mp, int mntflags, struct thread *td)
826 {
827         struct ufsmount *ump;
828         struct fs *fs;
829         int error, flags;
830
831         flags = 0;
832         if (mntflags & MNT_FORCE) {
833                 flags |= FORCECLOSE;
834         }
835         if (mp->mnt_flag & MNT_SOFTDEP) {
836                 if ((error = softdep_flushfiles(mp, flags)) != 0)
837                         return (error);
838         } else {
839                 if ((error = ffs_flushfiles(mp, flags)) != 0)
840                         return (error);
841         }
842         ump = VFSTOUFS(mp);
843         fs = ump->um_fs;
844         if (fs->fs_ronly == 0) {
845                 fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1;
846                 error = ffs_sbupdate(ump, MNT_WAIT);
847                 if (error) {
848                         fs->fs_clean = 0;
849                         return (error);
850                 }
851         }
852         ump->um_devvp->v_rdev->si_mountpoint = NULL;
853
854         vinvalbuf(ump->um_devvp, V_SAVE, 0, 0);
855         error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE);
856
857         vrele(ump->um_devvp);
858
859         free(fs->fs_csp, M_UFSMNT);
860         free(fs, M_UFSMNT);
861         free(ump, M_UFSMNT);
862         mp->mnt_data = (qaddr_t)0;
863         mp->mnt_flag &= ~MNT_LOCAL;
864         return (error);
865 }
866
867 /*
868  * Flush out all the files in a filesystem.
869  */
870 int
871 ffs_flushfiles(struct mount *mp, int flags)
872 {
873         struct ufsmount *ump;
874         int error;
875
876         ump = VFSTOUFS(mp);
877 #ifdef QUOTA
878         if (mp->mnt_flag & MNT_QUOTA) {
879                 int i;
880                 error = vflush(mp, 0, SKIPSYSTEM|flags);
881                 if (error)
882                         return (error);
883                 /* Find out how many quota files  we have open. */
884                 for (i = 0; i < MAXQUOTAS; i++) {
885                         if (ump->um_quotas[i] == NULLVP)
886                                 continue;
887                         ufs_quotaoff(mp, i);
888                 }
889                 /*
890                  * Here we fall through to vflush again to ensure
891                  * that we have gotten rid of all the system vnodes.
892                  */
893         }
894 #endif
895         /*
896          * Flush all the files.
897          */
898         if ((error = vflush(mp, 0, flags)) != 0)
899                 return (error);
900         /*
901          * Flush filesystem metadata.
902          */
903         vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
904         error = VOP_FSYNC(ump->um_devvp, MNT_WAIT);
905         VOP_UNLOCK(ump->um_devvp, 0);
906         return (error);
907 }
908
909 /*
910  * Get filesystem statistics.
911  */
912 int
913 ffs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
914 {
915         struct ufsmount *ump;
916         struct fs *fs;
917
918         ump = VFSTOUFS(mp);
919         fs = ump->um_fs;
920         if (fs->fs_magic != FS_MAGIC)
921                 panic("ffs_statfs");
922         sbp->f_bsize = fs->fs_fsize;
923         sbp->f_iosize = fs->fs_bsize;
924         sbp->f_blocks = fs->fs_dsize;
925         sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
926                 fs->fs_cstotal.cs_nffree;
927         sbp->f_bavail = freespace(fs, fs->fs_minfree);
928         sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
929         sbp->f_ffree = fs->fs_cstotal.cs_nifree;
930         if (sbp != &mp->mnt_stat) {
931                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
932                 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
933                         (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
934         }
935         return (0);
936 }
937
938 /*
939  * Go through the disk queues to initiate sandbagged IO;
940  * go through the inodes to write those that have been modified;
941  * initiate the writing of the super block if it has been modified.
942  *
943  * Note: we are always called with the filesystem marked `MPBUSY'.
944  */
945
946
947 static int ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
948 static int ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
949
950 int
951 ffs_sync(struct mount *mp, int waitfor)
952 {
953         struct ufsmount *ump = VFSTOUFS(mp);
954         struct fs *fs;
955         int error;
956         struct scaninfo scaninfo;
957
958         fs = ump->um_fs;
959         if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {            /* XXX */
960                 printf("fs = %s\n", fs->fs_fsmnt);
961                 panic("ffs_sync: rofs mod");
962         }
963
964         /*
965          * Write back each (modified) inode.
966          */
967         scaninfo.allerror = 0;
968         scaninfo.rescan = 1;
969         scaninfo.waitfor = waitfor;
970         while (scaninfo.rescan) {
971                 scaninfo.rescan = 0;
972                 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
973                                 ffs_sync_scan1, ffs_sync_scan2, &scaninfo);
974         }
975
976         /*
977          * Force stale filesystem control information to be flushed.
978          */
979         if (waitfor != MNT_LAZY) {
980                 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
981                         waitfor = MNT_NOWAIT;
982                 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
983                 if ((error = VOP_FSYNC(ump->um_devvp, waitfor)) != 0)
984                         scaninfo.allerror = error;
985                 VOP_UNLOCK(ump->um_devvp, 0);
986         }
987 #ifdef QUOTA
988         ufs_qsync(mp);
989 #endif
990         /*
991          * Write back modified superblock.
992          */
993         if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
994                 scaninfo.allerror = error;
995         return (scaninfo.allerror);
996 }
997
998 static int
999 ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1000 {
1001         struct inode *ip;
1002
1003         /*
1004          * Depend on the mount list's vnode lock to keep things stable 
1005          * enough for a quick test.  Since there might be hundreds of 
1006          * thousands of vnodes, we cannot afford even a subroutine
1007          * call unless there's a good chance that we have work to do.
1008          */
1009         ip = VTOI(vp);
1010         /* Restart out whole search if this guy is locked
1011          * or is being reclaimed.
1012          */
1013         if (vp->v_type == VNON || ((ip->i_flag &
1014              (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1015              RB_EMPTY(&vp->v_rbdirty_tree))) {
1016                 return(-1);
1017         }
1018         return(0);
1019 }
1020
1021 static int 
1022 ffs_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
1023 {
1024         struct scaninfo *info = data;
1025         struct inode *ip;
1026         int error;
1027
1028         /*
1029          * We have to recheck after having obtained the vnode interlock.
1030          */
1031         ip = VTOI(vp);
1032         if (vp->v_type == VNON || ((ip->i_flag &
1033              (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1034              RB_EMPTY(&vp->v_rbdirty_tree))) {
1035                 return(0);
1036         }
1037         if (vp->v_type != VCHR) {
1038                 if ((error = VOP_FSYNC(vp, info->waitfor)) != 0)
1039                         info->allerror = error;
1040         } else {
1041                 /*
1042                  * We must reference the vp to prevent it from
1043                  * getting ripped out from under UFS_UPDATE, since
1044                  * we are not holding a vnode lock.
1045                  */
1046                 /* UFS_UPDATE(vp, waitfor == MNT_WAIT); */
1047                 UFS_UPDATE(vp, 0);
1048         }
1049         return(0);
1050 }
1051
1052 /*
1053  * Look up a FFS dinode number to find its incore vnode, otherwise read it
1054  * in from disk.  If it is in core, wait for the lock bit to clear, then
1055  * return the inode locked.  Detection and handling of mount points must be
1056  * done by the calling routine.
1057  */
1058
1059 int
1060 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1061 {
1062         struct fs *fs;
1063         struct inode *ip;
1064         struct ufsmount *ump;
1065         struct buf *bp;
1066         struct vnode *vp;
1067         dev_t dev;
1068         int error;
1069
1070         ump = VFSTOUFS(mp);
1071         dev = ump->um_dev;
1072 restart:
1073         if ((*vpp = ufs_ihashget(dev, ino)) != NULL) {
1074                 return (0);
1075         }
1076
1077         /*
1078          * If this MALLOC() is performed after the getnewvnode()
1079          * it might block, leaving a vnode with a NULL v_data to be
1080          * found by ffs_sync() if a sync happens to fire right then,
1081          * which will cause a panic because ffs_sync() blindly
1082          * dereferences vp->v_data (as well it should).
1083          *
1084          * XXX this may no longer be true since getnewvnode returns a
1085          * VX locked vnode now.
1086          */
1087         MALLOC(ip, struct inode *, sizeof(struct inode), 
1088             ump->um_malloctype, M_WAITOK);
1089
1090         /* Allocate a new vnode/inode. */
1091         error = getnewvnode(VT_UFS, mp, &vp, VLKTIMEOUT, LK_CANRECURSE);
1092         if (error) {
1093                 *vpp = NULL;
1094                 free(ip, ump->um_malloctype);
1095                 return (error);
1096         }
1097         bzero((caddr_t)ip, sizeof(struct inode));
1098         ip->i_vnode = vp;
1099         ip->i_fs = fs = ump->um_fs;
1100         ip->i_dev = dev;
1101         ip->i_number = ino;
1102 #ifdef QUOTA
1103         {
1104                 int i;
1105                 for (i = 0; i < MAXQUOTAS; i++)
1106                         ip->i_dquot[i] = NODQUOT;
1107         }
1108 #endif
1109
1110         /*
1111          * Insert it into the inode hash table and check for a collision.
1112          * If a collision occurs, throw away the vnode and try again.
1113          */
1114         if (ufs_ihashins(ip) != 0) {
1115                 printf("debug: ufs ihashins collision, retrying inode %ld\n",
1116                     (long)ip->i_number);
1117                 vx_put(vp);
1118                 free(ip, ump->um_malloctype);
1119                 goto restart;
1120         }
1121         vp->v_data = ip;
1122
1123         /* Read in the disk contents for the inode, copy into the inode. */
1124         error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1125             (int)fs->fs_bsize, &bp);
1126         if (error) {
1127                 /*
1128                  * The inode does not contain anything useful, so it would
1129                  * be misleading to leave it on its hash chain. With mode
1130                  * still zero, it will be unlinked and returned to the free
1131                  * list by vput().
1132                  */
1133                 brelse(bp);
1134                 vx_put(vp);
1135                 *vpp = NULL;
1136                 return (error);
1137         }
1138         ip->i_din = *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
1139         if (DOINGSOFTDEP(vp))
1140                 softdep_load_inodeblock(ip);
1141         else
1142                 ip->i_effnlink = ip->i_nlink;
1143         bqrelse(bp);
1144
1145         /*
1146          * Initialize the vnode from the inode, check for aliases.
1147          * Note that the underlying vnode may have changed.
1148          */
1149         error = ufs_vinit(mp, &vp);
1150         if (error) {
1151                 vx_put(vp);
1152                 *vpp = NULL;
1153                 return (error);
1154         }
1155         /*
1156          * Finish inode initialization now that aliasing has been resolved.
1157          */
1158         ip->i_devvp = ump->um_devvp;
1159         vref(ip->i_devvp);
1160         /*
1161          * Set up a generation number for this inode if it does not
1162          * already have one. This should only happen on old filesystems.
1163          */
1164         if (ip->i_gen == 0) {
1165                 ip->i_gen = random() / 2 + 1;
1166                 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1167                         ip->i_flag |= IN_MODIFIED;
1168         }
1169         /*
1170          * Ensure that uid and gid are correct. This is a temporary
1171          * fix until fsck has been changed to do the update.
1172          */
1173         if (fs->fs_inodefmt < FS_44INODEFMT) {          /* XXX */
1174                 ip->i_uid = ip->i_din.di_ouid;          /* XXX */
1175                 ip->i_gid = ip->i_din.di_ogid;          /* XXX */
1176         }                                               /* XXX */
1177
1178         /* 
1179          * return a VX locked and refd vnode (VX == same as normal vget()
1180          * vnode so we are ok)
1181          */
1182         *vpp = vp;
1183         return (0);
1184 }
1185
1186 /*
1187  * File handle to vnode
1188  *
1189  * Have to be really careful about stale file handles:
1190  * - check that the inode number is valid
1191  * - call ffs_vget() to get the locked inode
1192  * - check for an unallocated inode (i_mode == 0)
1193  * - check that the given client host has export rights and return
1194  *   those rights via. exflagsp and credanonp
1195  */
1196 int
1197 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1198 {
1199         struct ufid *ufhp;
1200         struct fs *fs;
1201
1202         ufhp = (struct ufid *)fhp;
1203         fs = VFSTOUFS(mp)->um_fs;
1204         if (ufhp->ufid_ino < ROOTINO ||
1205             ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1206                 return (ESTALE);
1207         return (ufs_fhtovp(mp, ufhp, vpp));
1208 }
1209
1210 /*
1211  * Vnode pointer to File handle
1212  */
1213 /* ARGSUSED */
1214 int
1215 ffs_vptofh(struct vnode *vp, struct fid *fhp)
1216 {
1217         struct inode *ip;
1218         struct ufid *ufhp;
1219
1220         ip = VTOI(vp);
1221         ufhp = (struct ufid *)fhp;
1222         ufhp->ufid_len = sizeof(struct ufid);
1223         ufhp->ufid_ino = ip->i_number;
1224         ufhp->ufid_gen = ip->i_gen;
1225         return (0);
1226 }
1227
1228 /*
1229  * Initialize the filesystem; just use ufs_init.
1230  */
1231 static int
1232 ffs_init(struct vfsconf *vfsp)
1233 {
1234         softdep_initialize();
1235         return (ufs_init(vfsp));
1236 }
1237
1238 /*
1239  * Write a superblock and associated information back to disk.
1240  */
1241 static int
1242 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1243 {
1244         struct fs *dfs, *fs = mp->um_fs;
1245         struct buf *bp;
1246         int blks;
1247         void *space;
1248         int i, size, error, allerror = 0;
1249
1250         /*
1251          * First write back the summary information.
1252          *
1253          * NOTE: the getblk is relative to the device vnode so bio1
1254          * contains the device block number.
1255          */
1256         blks = howmany(fs->fs_cssize, fs->fs_fsize);
1257         space = fs->fs_csp;
1258         for (i = 0; i < blks; i += fs->fs_frag) {
1259                 size = fs->fs_bsize;
1260                 if (i + fs->fs_frag > blks)
1261                         size = (blks - i) * fs->fs_fsize;
1262                 bp = getblk(mp->um_devvp, fsbtodoff(fs, fs->fs_csaddr + i),
1263                             size, 0, 0);
1264                 bcopy(space, bp->b_data, (uint)size);
1265                 space = (char *)space + size;
1266                 if (waitfor != MNT_WAIT)
1267                         bawrite(bp);
1268                 else if ((error = bwrite(bp)) != 0)
1269                         allerror = error;
1270         }
1271         /*
1272          * Now write back the superblock itself. If any errors occurred
1273          * up to this point, then fail so that the superblock avoids
1274          * being written out as clean.
1275          */
1276         if (allerror)
1277                 return (allerror);
1278         bp = getblk(mp->um_devvp, SBOFF, (int)fs->fs_sbsize, 0, 0);
1279         fs->fs_fmod = 0;
1280         fs->fs_time = time_second;
1281         bcopy((caddr_t)fs, bp->b_data, (uint)fs->fs_sbsize);
1282         /* Restore compatibility to old filesystems.               XXX */
1283         dfs = (struct fs *)bp->b_data;                          /* XXX */
1284         if (fs->fs_postblformat == FS_42POSTBLFMT)              /* XXX */
1285                 dfs->fs_nrpos = -1;                             /* XXX */
1286         if (fs->fs_inodefmt < FS_44INODEFMT) {                  /* XXX */
1287                 int32_t *lp, tmp;                               /* XXX */
1288                                                                 /* XXX */
1289                 lp = (int32_t *)&dfs->fs_qbmask;                /* XXX */
1290                 tmp = lp[4];                                    /* XXX */
1291                 for (i = 4; i > 0; i--)                         /* XXX */
1292                         lp[i] = lp[i-1];                        /* XXX */
1293                 lp[0] = tmp;                                    /* XXX */
1294         }                                                       /* XXX */
1295         dfs->fs_maxfilesize = mp->um_savedmaxfilesize;          /* XXX */
1296         if (waitfor != MNT_WAIT)
1297                 bawrite(bp);
1298         else if ((error = bwrite(bp)) != 0)
1299                 allerror = error;
1300         return (allerror);
1301 }