Fix compile warning.
[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.18 2004/05/19 22:53:06 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 file system
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 file system or updating a file system
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 file system is still read-only. */
502         newfs->fs_ronly = 1;
503         bcopy(newfs, fs, (u_int)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, (u_int)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 
553 int
554 ffs_reload_scan1(struct mount *mp, struct vnode *vp, void *data)
555 {
556         struct scaninfo *info = data;
557
558         /*
559          * Step 4: invalidate all inactive vnodes. 
560          */
561         if (vrecycle(vp, NULL, info->td)) {
562                 info->rescan = 1;
563                 return(-1);     /* continue loop, do not call scan2 */
564         }
565         return(0);
566 }
567
568 static
569 int
570 ffs_reload_scan2(struct mount *mp, struct vnode *vp, lwkt_tokref_t vlock, void *data)
571 {
572         struct scaninfo *info = data;
573         struct inode *ip;
574         struct buf *bp;
575         int error;
576
577         /*
578          * Step 5: invalidate all cached file data.
579          */
580         if (vget(vp, vlock, LK_EXCLUSIVE | LK_INTERLOCK, info->td)) {
581                 info->rescan = 1;
582                 return(0);
583         }
584         if (vinvalbuf(vp, 0, info->td, 0, 0))
585                 panic("ffs_reload: dirty2");
586         /*
587          * Step 6: re-read inode data for all active vnodes.
588          */
589         ip = VTOI(vp);
590         error = bread(info->devvp,
591                         fsbtodb(info->fs, ino_to_fsba(info->fs, ip->i_number)),
592                         (int)info->fs->fs_bsize, &bp);
593         if (error) {
594                 vput(vp);
595                 return (error);
596         }
597         ip->i_din = *((struct dinode *)bp->b_data +
598             ino_to_fsbo(info->fs, ip->i_number));
599         ip->i_effnlink = ip->i_nlink;
600         brelse(bp);
601         vput(vp);
602         return(0);
603 }
604
605 /*
606  * Common code for mount and mountroot
607  */
608 int
609 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td,
610             struct malloc_type *malloctype)
611 {
612         struct ufsmount *ump;
613         struct buf *bp;
614         struct fs *fs;
615         dev_t dev;
616         struct partinfo dpart;
617         void *space;
618         int error, i, blks, size, ronly;
619         lwkt_tokref vlock;
620         int32_t *lp;
621         u_int64_t maxfilesize;                                  /* XXX */
622         size_t strsize;
623
624         /*
625          * Disallow multiple mounts of the same device.
626          * Disallow mounting of a device that is currently in use
627          * (except for root, which might share swap device for miniroot).
628          * Flush out any old buffers remaining from a previous use.
629          */
630         error = vfs_mountedon(devvp);
631         if (error)
632                 return (error);
633         if (count_udev(devvp->v_udev) > 0 && devvp != rootvp)
634                 return (EBUSY);
635         vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
636         error = vinvalbuf(devvp, V_SAVE, td, 0, 0);
637         VOP_UNLOCK(devvp, NULL, 0, td);
638         if (error)
639                 return (error);
640
641         /*
642          * Only VMIO the backing device if the backing device is a real
643          * block device.  This excludes the original MFS implementation.
644          * Note that it is optional that the backing device be VMIOed.  This
645          * increases the opportunity for metadata caching.
646          */
647         if (devvp->v_tag != VT_MFS && vn_isdisk(devvp, NULL)) {
648                 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
649                 vfs_object_create(devvp, td);
650                 lwkt_gettoken(&vlock, devvp->v_interlock);
651                 VOP_UNLOCK(devvp, &vlock, LK_INTERLOCK, td);
652         }
653
654         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
655         vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
656         error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, td);
657         VOP_UNLOCK(devvp, NULL, 0, td);
658         if (error)
659                 return (error);
660         dev = devvp->v_rdev;
661         if (dev->si_iosize_max != 0)
662                 mp->mnt_iosize_max = dev->si_iosize_max;
663         if (mp->mnt_iosize_max > MAXPHYS)
664                 mp->mnt_iosize_max = MAXPHYS;
665
666         if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, proc0.p_ucred, td) != 0)
667                 size = DEV_BSIZE;
668         else
669                 size = dpart.disklab->d_secsize;
670
671         bp = NULL;
672         ump = NULL;
673         if ((error = bread(devvp, SBLOCK, SBSIZE, &bp)) != 0)
674                 goto out;
675         fs = (struct fs *)bp->b_data;
676         if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
677             fs->fs_bsize < sizeof(struct fs)) {
678                 error = EINVAL;         /* XXX needs translation */
679                 goto out;
680         }
681         fs->fs_fmod = 0;
682         fs->fs_flags &= ~FS_UNCLEAN;
683         if (fs->fs_clean == 0) {
684                 fs->fs_flags |= FS_UNCLEAN;
685                 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
686                         printf(
687 "WARNING: %s was not properly dismounted\n",
688                             fs->fs_fsmnt);
689                 } else {
690                         printf(
691 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
692                             fs->fs_fsmnt);
693                         error = EPERM;
694                         goto out;
695                 }
696         }
697         /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
698         if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
699                 error = EROFS;          /* needs translation */
700                 goto out;
701         }
702         ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
703         bzero((caddr_t)ump, sizeof *ump);
704         ump->um_malloctype = malloctype;
705         ump->um_i_effnlink_valid = 1;
706         ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
707             M_WAITOK);
708         ump->um_blkatoff = ffs_blkatoff;
709         ump->um_truncate = ffs_truncate;
710         ump->um_update = ffs_update;
711         ump->um_valloc = ffs_valloc;
712         ump->um_vfree = ffs_vfree;
713         bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
714         if (fs->fs_sbsize < SBSIZE)
715                 bp->b_flags |= B_INVAL;
716         brelse(bp);
717         bp = NULL;
718         fs = ump->um_fs;
719         fs->fs_ronly = ronly;
720         size = fs->fs_cssize;
721         blks = howmany(size, fs->fs_fsize);
722         if (fs->fs_contigsumsize > 0)
723                 size += fs->fs_ncg * sizeof(int32_t);
724         size += fs->fs_ncg * sizeof(u_int8_t);
725         space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
726         fs->fs_csp = space;
727         for (i = 0; i < blks; i += fs->fs_frag) {
728                 size = fs->fs_bsize;
729                 if (i + fs->fs_frag > blks)
730                         size = (blks - i) * fs->fs_fsize;
731                 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
732                     &bp)) != 0) {
733                         free(fs->fs_csp, M_UFSMNT);
734                         goto out;
735                 }
736                 bcopy(bp->b_data, space, (u_int)size);
737                 space = (char *)space + size;
738                 brelse(bp);
739                 bp = NULL;
740         }
741         if (fs->fs_contigsumsize > 0) {
742                 fs->fs_maxcluster = lp = space;
743                 for (i = 0; i < fs->fs_ncg; i++)
744                         *lp++ = fs->fs_contigsumsize;
745                 space = lp;
746         }
747         size = fs->fs_ncg * sizeof(u_int8_t);
748         fs->fs_contigdirs = (u_int8_t *)space;
749         bzero(fs->fs_contigdirs, size);
750         /* Compatibility for old filesystems       XXX */
751         if (fs->fs_avgfilesize <= 0)            /* XXX */
752                 fs->fs_avgfilesize = AVFILESIZ; /* XXX */
753         if (fs->fs_avgfpdir <= 0)               /* XXX */
754                 fs->fs_avgfpdir = AFPDIR;       /* XXX */
755         mp->mnt_data = (qaddr_t)ump;
756         mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
757         mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
758         if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 || 
759             vfs_getvfs(&mp->mnt_stat.f_fsid)) 
760                 vfs_getnewfsid(mp);
761         mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
762         mp->mnt_flag |= MNT_LOCAL;
763         ump->um_mountp = mp;
764         ump->um_dev = dev;
765         ump->um_devvp = devvp;
766         ump->um_nindir = fs->fs_nindir;
767         ump->um_bptrtodb = fs->fs_fsbtodb;
768         ump->um_seqinc = fs->fs_frag;
769         for (i = 0; i < MAXQUOTAS; i++)
770                 ump->um_quotas[i] = NULLVP;
771         dev->si_mountpoint = mp;
772         ffs_oldfscompat(fs);
773
774         /*
775          * Set FS local "last mounted on" information (NULL pad)
776          */
777         copystr(        mp->mnt_stat.f_mntonname,       /* mount point*/
778                         fs->fs_fsmnt,                   /* copy area*/
779                         sizeof(fs->fs_fsmnt) - 1,       /* max size*/
780                         &strsize);                      /* real size*/
781         bzero( fs->fs_fsmnt + strsize, sizeof(fs->fs_fsmnt) - strsize);
782
783         if( mp->mnt_flag & MNT_ROOTFS) {
784                 /*
785                  * Root mount; update timestamp in mount structure.
786                  * this will be used by the common root mount code
787                  * to update the system clock.
788                  */
789                 mp->mnt_time = fs->fs_time;
790         }
791
792         ump->um_savedmaxfilesize = fs->fs_maxfilesize;          /* XXX */
793         maxfilesize = (u_int64_t)0x40000000 * fs->fs_bsize - 1; /* XXX */
794         /* Enforce limit caused by vm object backing (32 bits vm_pindex_t). */
795         if (maxfilesize > (u_int64_t)0x80000000u * PAGE_SIZE - 1)
796                 maxfilesize = (u_int64_t)0x80000000u * PAGE_SIZE - 1;
797         if (fs->fs_maxfilesize > maxfilesize)                   /* XXX */
798                 fs->fs_maxfilesize = maxfilesize;               /* XXX */
799         if (ronly == 0) {
800                 if ((fs->fs_flags & FS_DOSOFTDEP) &&
801                     (error = softdep_mount(devvp, mp, fs)) != 0) {
802                         free(fs->fs_csp, M_UFSMNT);
803                         goto out;
804                 }
805                 fs->fs_fmod = 1;
806                 fs->fs_clean = 0;
807                 (void) ffs_sbupdate(ump, MNT_WAIT);
808         }
809         return (0);
810 out:
811         dev->si_mountpoint = NULL;
812         if (bp)
813                 brelse(bp);
814         VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
815         if (ump) {
816                 free(ump->um_fs, M_UFSMNT);
817                 free(ump, M_UFSMNT);
818                 mp->mnt_data = (qaddr_t)0;
819         }
820         return (error);
821 }
822
823 /*
824  * Sanity checks for old file systems.
825  *
826  * XXX - goes away some day.
827  */
828 static int
829 ffs_oldfscompat(struct fs *fs)
830 {
831         fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);       /* XXX */
832         fs->fs_interleave = max(fs->fs_interleave, 1);          /* XXX */
833         if (fs->fs_postblformat == FS_42POSTBLFMT)              /* XXX */
834                 fs->fs_nrpos = 8;                               /* XXX */
835         if (fs->fs_inodefmt < FS_44INODEFMT) {                  /* XXX */
836 #if 0
837                 int i;                                          /* XXX */
838                 u_int64_t sizepb = fs->fs_bsize;                /* XXX */
839                                                                 /* XXX */
840                 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
841                 for (i = 0; i < NIADDR; i++) {                  /* XXX */
842                         sizepb *= NINDIR(fs);                   /* XXX */
843                         fs->fs_maxfilesize += sizepb;           /* XXX */
844                 }                                               /* XXX */
845 #endif
846                 fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
847                 fs->fs_qbmask = ~fs->fs_bmask;                  /* XXX */
848                 fs->fs_qfmask = ~fs->fs_fmask;                  /* XXX */
849         }                                                       /* XXX */
850         return (0);
851 }
852
853 /*
854  * unmount system call
855  */
856 int
857 ffs_unmount(struct mount *mp, int mntflags, struct thread *td)
858 {
859         struct ufsmount *ump;
860         struct fs *fs;
861         int error, flags;
862
863         flags = 0;
864         if (mntflags & MNT_FORCE) {
865                 flags |= FORCECLOSE;
866         }
867         if (mp->mnt_flag & MNT_SOFTDEP) {
868                 if ((error = softdep_flushfiles(mp, flags, td)) != 0)
869                         return (error);
870         } else {
871                 if ((error = ffs_flushfiles(mp, flags, td)) != 0)
872                         return (error);
873         }
874         ump = VFSTOUFS(mp);
875         fs = ump->um_fs;
876         if (fs->fs_ronly == 0) {
877                 fs->fs_clean = fs->fs_flags & FS_UNCLEAN ? 0 : 1;
878                 error = ffs_sbupdate(ump, MNT_WAIT);
879                 if (error) {
880                         fs->fs_clean = 0;
881                         return (error);
882                 }
883         }
884         ump->um_devvp->v_rdev->si_mountpoint = NULL;
885
886         vinvalbuf(ump->um_devvp, V_SAVE, td, 0, 0);
887         error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE, td);
888
889         vrele(ump->um_devvp);
890
891         free(fs->fs_csp, M_UFSMNT);
892         free(fs, M_UFSMNT);
893         free(ump, M_UFSMNT);
894         mp->mnt_data = (qaddr_t)0;
895         mp->mnt_flag &= ~MNT_LOCAL;
896         return (error);
897 }
898
899 /*
900  * Flush out all the files in a filesystem.
901  */
902 int
903 ffs_flushfiles(struct mount *mp, int flags, struct thread *td)
904 {
905         struct ufsmount *ump;
906         int error;
907
908         ump = VFSTOUFS(mp);
909 #ifdef QUOTA
910         if (mp->mnt_flag & MNT_QUOTA) {
911                 int i;
912                 error = vflush(mp, 0, SKIPSYSTEM|flags);
913                 if (error)
914                         return (error);
915                 for (i = 0; i < MAXQUOTAS; i++) {
916                         if (ump->um_quotas[i] == NULLVP)
917                                 continue;
918                         quotaoff(td, mp, i);
919                 }
920                 /*
921                  * Here we fall through to vflush again to ensure
922                  * that we have gotten rid of all the system vnodes.
923                  */
924         }
925 #endif
926         /*
927          * Flush all the files.
928          */
929         if ((error = vflush(mp, 0, flags)) != 0)
930                 return (error);
931         /*
932          * Flush filesystem metadata.
933          */
934         vn_lock(ump->um_devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
935         error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
936         VOP_UNLOCK(ump->um_devvp, NULL, 0, td);
937         return (error);
938 }
939
940 /*
941  * Get file system statistics.
942  */
943 int
944 ffs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
945 {
946         struct ufsmount *ump;
947         struct fs *fs;
948
949         ump = VFSTOUFS(mp);
950         fs = ump->um_fs;
951         if (fs->fs_magic != FS_MAGIC)
952                 panic("ffs_statfs");
953         sbp->f_bsize = fs->fs_fsize;
954         sbp->f_iosize = fs->fs_bsize;
955         sbp->f_blocks = fs->fs_dsize;
956         sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
957                 fs->fs_cstotal.cs_nffree;
958         sbp->f_bavail = freespace(fs, fs->fs_minfree);
959         sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
960         sbp->f_ffree = fs->fs_cstotal.cs_nifree;
961         if (sbp != &mp->mnt_stat) {
962                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
963                 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
964                         (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
965                 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
966                         (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
967         }
968         return (0);
969 }
970
971 /*
972  * Go through the disk queues to initiate sandbagged IO;
973  * go through the inodes to write those that have been modified;
974  * initiate the writing of the super block if it has been modified.
975  *
976  * Note: we are always called with the filesystem marked `MPBUSY'.
977  */
978
979
980 static int ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
981 static int ffs_sync_scan2(struct mount *mp, struct vnode *vp,
982                 lwkt_tokref_t vlock, void *data);
983
984 int
985 ffs_sync(struct mount *mp, int waitfor, struct thread *td)
986 {
987         struct ufsmount *ump = VFSTOUFS(mp);
988         struct fs *fs;
989         int error;
990         struct scaninfo scaninfo;
991
992         fs = ump->um_fs;
993         if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {            /* XXX */
994                 printf("fs = %s\n", fs->fs_fsmnt);
995                 panic("ffs_sync: rofs mod");
996         }
997
998         /*
999          * Write back each (modified) inode.
1000          */
1001         scaninfo.allerror = 0;
1002         scaninfo.rescan = 1;
1003         scaninfo.waitfor = waitfor;
1004         while (scaninfo.rescan) {
1005                 scaninfo.rescan = 0;
1006                 vmntvnodescan(mp, ffs_sync_scan1, ffs_sync_scan2, &scaninfo);
1007         }
1008
1009         /*
1010          * Force stale file system control information to be flushed.
1011          */
1012         if (waitfor != MNT_LAZY) {
1013                 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
1014                         waitfor = MNT_NOWAIT;
1015                 vn_lock(ump->um_devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1016                 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
1017                         scaninfo.allerror = error;
1018                 VOP_UNLOCK(ump->um_devvp, NULL, 0, td);
1019         }
1020 #ifdef QUOTA
1021         qsync(mp);
1022 #endif
1023         /*
1024          * Write back modified superblock.
1025          */
1026         if (fs->fs_fmod != 0 && (error = ffs_sbupdate(ump, waitfor)) != 0)
1027                 scaninfo.allerror = error;
1028         return (scaninfo.allerror);
1029 }
1030
1031 static
1032 int
1033 ffs_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1034 {
1035         struct inode *ip;
1036
1037         /*
1038          * Depend on the mount list's vnode lock to keep things stable 
1039          * enough for a quick test.  Since there might be hundreds of 
1040          * thousands of vnodes, we cannot afford even a subroutine
1041          * call unless there's a good chance that we have work to do.
1042          */
1043         ip = VTOI(vp);
1044         if (vp->v_type == VNON || ((ip->i_flag &
1045              (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1046              TAILQ_EMPTY(&vp->v_dirtyblkhd))) {
1047                 return(-1);
1048         }
1049         return(0);
1050 }
1051
1052 static
1053 int 
1054 ffs_sync_scan2(struct mount *mp, struct vnode *vp,
1055                lwkt_tokref_t vlock, void *data)
1056 {
1057         struct scaninfo *info = data;
1058         thread_t td = curthread;        /* XXX */
1059         struct inode *ip;
1060         int error;
1061
1062         /*
1063          * We have to recheck after having obtained the vnode interlock.
1064          */
1065         ip = VTOI(vp);
1066         if (vp->v_type == VNON || ((ip->i_flag &
1067              (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1068              TAILQ_EMPTY(&vp->v_dirtyblkhd))) {
1069                 lwkt_reltoken(vlock);
1070                 return(0);
1071         }
1072         if (vp->v_type != VCHR) {
1073                 error = vget(vp, vlock, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT, td);
1074                 if (error) {
1075                         if (error == ENOENT)
1076                                 info->rescan = 1;
1077                 } else {
1078                         if ((error = VOP_FSYNC(vp, info->waitfor, td)) != 0)
1079                                 info->allerror = error;
1080                         VOP_UNLOCK(vp, NULL, 0, td);
1081                         vrele(vp);
1082                 }
1083         } else {
1084                 /*
1085                  * We must reference the vp to prevent it from
1086                  * getting ripped out from under UFS_UPDATE, since
1087                  * we are not holding a vnode lock.
1088                  */
1089                 vref(vp);
1090                 lwkt_reltoken(vlock);
1091                 /* UFS_UPDATE(vp, waitfor == MNT_WAIT); */
1092                 UFS_UPDATE(vp, 0);
1093                 vrele(vp);
1094         }
1095         return(0);
1096 }
1097
1098 /*
1099  * Look up a FFS dinode number to find its incore vnode, otherwise read it
1100  * in from disk.  If it is in core, wait for the lock bit to clear, then
1101  * return the inode locked.  Detection and handling of mount points must be
1102  * done by the calling routine.
1103  */
1104 static int ffs_inode_hash_lock;
1105
1106 int
1107 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1108 {
1109         struct fs *fs;
1110         struct inode *ip;
1111         struct ufsmount *ump;
1112         struct buf *bp;
1113         struct vnode *vp;
1114         dev_t dev;
1115         int error;
1116
1117         ump = VFSTOUFS(mp);
1118         dev = ump->um_dev;
1119 restart:
1120         if ((*vpp = ufs_ihashget(dev, ino)) != NULL) {
1121                 return (0);
1122         }
1123
1124         /*
1125          * Lock out the creation of new entries in the FFS hash table in
1126          * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1127          * may occur!
1128          */
1129         if (ffs_inode_hash_lock) {
1130                 while (ffs_inode_hash_lock) {
1131                         ffs_inode_hash_lock = -1;
1132                         tsleep(&ffs_inode_hash_lock, 0, "ffsvgt", 0);
1133                 }
1134                 goto restart;
1135         }
1136         ffs_inode_hash_lock = 1;
1137
1138         /*
1139          * If this MALLOC() is performed after the getnewvnode()
1140          * it might block, leaving a vnode with a NULL v_data to be
1141          * found by ffs_sync() if a sync happens to fire right then,
1142          * which will cause a panic because ffs_sync() blindly
1143          * dereferences vp->v_data (as well it should).
1144          */
1145         MALLOC(ip, struct inode *, sizeof(struct inode), 
1146             ump->um_malloctype, M_WAITOK);
1147
1148         /* Allocate a new vnode/inode. */
1149         error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp);
1150         if (error) {
1151                 if (ffs_inode_hash_lock < 0)
1152                         wakeup(&ffs_inode_hash_lock);
1153                 ffs_inode_hash_lock = 0;
1154                 *vpp = NULL;
1155                 FREE(ip, ump->um_malloctype);
1156                 return (error);
1157         }
1158         bzero((caddr_t)ip, sizeof(struct inode));
1159         lockinit(&ip->i_lock, 0, "inode", VLKTIMEOUT, LK_CANRECURSE);
1160         vp->v_data = ip;
1161         /*
1162          * FFS supports lock sharing in the stack of vnodes
1163          */
1164         vp->v_vnlock = &ip->i_lock;
1165         ip->i_vnode = vp;
1166         ip->i_fs = fs = ump->um_fs;
1167         ip->i_dev = dev;
1168         ip->i_number = ino;
1169 #ifdef QUOTA
1170         {
1171                 int i;
1172                 for (i = 0; i < MAXQUOTAS; i++)
1173                         ip->i_dquot[i] = NODQUOT;
1174         }
1175 #endif
1176         /*
1177          * Put it onto its hash chain and lock it so that other requests for
1178          * this inode will block if they arrive while we are sleeping waiting
1179          * for old data structures to be purged or for the contents of the
1180          * disk portion of this inode to be read.
1181          */
1182         ufs_ihashins(ip);
1183
1184         if (ffs_inode_hash_lock < 0)
1185                 wakeup(&ffs_inode_hash_lock);
1186         ffs_inode_hash_lock = 0;
1187
1188         /* Read in the disk contents for the inode, copy into the inode. */
1189         error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1190             (int)fs->fs_bsize, &bp);
1191         if (error) {
1192                 /*
1193                  * The inode does not contain anything useful, so it would
1194                  * be misleading to leave it on its hash chain. With mode
1195                  * still zero, it will be unlinked and returned to the free
1196                  * list by vput().
1197                  */
1198                 brelse(bp);
1199                 vput(vp);
1200                 *vpp = NULL;
1201                 return (error);
1202         }
1203         ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
1204         if (DOINGSOFTDEP(vp))
1205                 softdep_load_inodeblock(ip);
1206         else
1207                 ip->i_effnlink = ip->i_nlink;
1208         bqrelse(bp);
1209
1210         /*
1211          * Initialize the vnode from the inode, check for aliases.
1212          * Note that the underlying vnode may have changed.
1213          */
1214         error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1215         if (error) {
1216                 vput(vp);
1217                 *vpp = NULL;
1218                 return (error);
1219         }
1220         /*
1221          * Finish inode initialization now that aliasing has been resolved.
1222          */
1223         ip->i_devvp = ump->um_devvp;
1224         vref(ip->i_devvp);
1225         /*
1226          * Set up a generation number for this inode if it does not
1227          * already have one. This should only happen on old filesystems.
1228          */
1229         if (ip->i_gen == 0) {
1230                 ip->i_gen = random() / 2 + 1;
1231                 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1232                         ip->i_flag |= IN_MODIFIED;
1233         }
1234         /*
1235          * Ensure that uid and gid are correct. This is a temporary
1236          * fix until fsck has been changed to do the update.
1237          */
1238         if (fs->fs_inodefmt < FS_44INODEFMT) {          /* XXX */
1239                 ip->i_uid = ip->i_din.di_ouid;          /* XXX */
1240                 ip->i_gid = ip->i_din.di_ogid;          /* XXX */
1241         }                                               /* XXX */
1242
1243         *vpp = vp;
1244         return (0);
1245 }
1246
1247 /*
1248  * File handle to vnode
1249  *
1250  * Have to be really careful about stale file handles:
1251  * - check that the inode number is valid
1252  * - call ffs_vget() to get the locked inode
1253  * - check for an unallocated inode (i_mode == 0)
1254  * - check that the given client host has export rights and return
1255  *   those rights via. exflagsp and credanonp
1256  */
1257 int
1258 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1259 {
1260         struct ufid *ufhp;
1261         struct fs *fs;
1262
1263         ufhp = (struct ufid *)fhp;
1264         fs = VFSTOUFS(mp)->um_fs;
1265         if (ufhp->ufid_ino < ROOTINO ||
1266             ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1267                 return (ESTALE);
1268         return (ufs_fhtovp(mp, ufhp, vpp));
1269 }
1270
1271 /*
1272  * Vnode pointer to File handle
1273  */
1274 /* ARGSUSED */
1275 int
1276 ffs_vptofh(struct vnode *vp, struct fid *fhp)
1277 {
1278         struct inode *ip;
1279         struct ufid *ufhp;
1280
1281         ip = VTOI(vp);
1282         ufhp = (struct ufid *)fhp;
1283         ufhp->ufid_len = sizeof(struct ufid);
1284         ufhp->ufid_ino = ip->i_number;
1285         ufhp->ufid_gen = ip->i_gen;
1286         return (0);
1287 }
1288
1289 /*
1290  * Initialize the filesystem; just use ufs_init.
1291  */
1292 static int
1293 ffs_init(struct vfsconf *vfsp)
1294 {
1295         softdep_initialize();
1296         return (ufs_init(vfsp));
1297 }
1298
1299 /*
1300  * Write a superblock and associated information back to disk.
1301  */
1302 static int
1303 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1304 {
1305         struct fs *dfs, *fs = mp->um_fs;
1306         struct buf *bp;
1307         int blks;
1308         void *space;
1309         int i, size, error, allerror = 0;
1310
1311         /*
1312          * First write back the summary information.
1313          */
1314         blks = howmany(fs->fs_cssize, fs->fs_fsize);
1315         space = fs->fs_csp;
1316         for (i = 0; i < blks; i += fs->fs_frag) {
1317                 size = fs->fs_bsize;
1318                 if (i + fs->fs_frag > blks)
1319                         size = (blks - i) * fs->fs_fsize;
1320                 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1321                     size, 0, 0);
1322                 bcopy(space, bp->b_data, (u_int)size);
1323                 space = (char *)space + size;
1324                 if (waitfor != MNT_WAIT)
1325                         bawrite(bp);
1326                 else if ((error = bwrite(bp)) != 0)
1327                         allerror = error;
1328         }
1329         /*
1330          * Now write back the superblock itself. If any errors occurred
1331          * up to this point, then fail so that the superblock avoids
1332          * being written out as clean.
1333          */
1334         if (allerror)
1335                 return (allerror);
1336         bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize, 0, 0);
1337         fs->fs_fmod = 0;
1338         fs->fs_time = time_second;
1339         bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
1340         /* Restore compatibility to old file systems.              XXX */
1341         dfs = (struct fs *)bp->b_data;                          /* XXX */
1342         if (fs->fs_postblformat == FS_42POSTBLFMT)              /* XXX */
1343                 dfs->fs_nrpos = -1;                             /* XXX */
1344         if (fs->fs_inodefmt < FS_44INODEFMT) {                  /* XXX */
1345                 int32_t *lp, tmp;                               /* XXX */
1346                                                                 /* XXX */
1347                 lp = (int32_t *)&dfs->fs_qbmask;                /* XXX */
1348                 tmp = lp[4];                                    /* XXX */
1349                 for (i = 4; i > 0; i--)                         /* XXX */
1350                         lp[i] = lp[i-1];                        /* XXX */
1351                 lp[0] = tmp;                                    /* XXX */
1352         }                                                       /* XXX */
1353         dfs->fs_maxfilesize = mp->um_savedmaxfilesize;          /* XXX */
1354         if (waitfor != MNT_WAIT)
1355                 bawrite(bp);
1356         else if ((error = bwrite(bp)) != 0)
1357                 allerror = error;
1358         return (allerror);
1359 }