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