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