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