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