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