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