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