allow multiple references when mounting miniroot
[unix-history] / usr / src / sys / ufs / lfs / lfs_vfsops.c
CommitLineData
da7c5cc6 1/*
7188ac27
KM
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
b702c21d 5 * %sccs.include.redist.c%
7188ac27 6 *
1c26e003 7 * @(#)lfs_vfsops.c 7.50 (Berkeley) %G%
da7c5cc6 8 */
71e4e98b 9
94368568
JB
10#include "param.h"
11#include "systm.h"
8dc876c1
KM
12#include "user.h"
13#include "proc.h"
7188ac27 14#include "kernel.h"
7188ac27 15#include "vnode.h"
0f93ba7b 16#include "specdev.h"
94368568 17#include "mount.h"
7188ac27 18#include "buf.h"
94368568 19#include "file.h"
ec67a3ce 20#include "disklabel.h"
7188ac27
KM
21#include "ioctl.h"
22#include "errno.h"
4def0c5e 23#include "malloc.h"
8dc876c1 24#include "../ufs/quota.h"
7188ac27
KM
25#include "../ufs/fs.h"
26#include "../ufs/ufsmount.h"
27#include "../ufs/inode.h"
609e7cfa
MK
28#include "ioctl.h"
29#include "disklabel.h"
30#include "stat.h"
71e4e98b 31
7188ac27
KM
32/*
33 * ufs vfs operations.
34 */
35int ufs_mount();
5bf9d21f 36int ufs_start();
7188ac27
KM
37int ufs_unmount();
38int ufs_root();
8dc876c1 39int ufs_quotactl();
7188ac27
KM
40int ufs_statfs();
41int ufs_sync();
42int ufs_fhtovp();
43int ufs_vptofh();
de0d8667 44int ufs_init();
7188ac27
KM
45
46struct vfsops ufs_vfsops = {
47 ufs_mount,
5bf9d21f 48 ufs_start,
7188ac27
KM
49 ufs_unmount,
50 ufs_root,
8dc876c1 51 ufs_quotactl,
7188ac27
KM
52 ufs_statfs,
53 ufs_sync,
54 ufs_fhtovp,
de0d8667
KM
55 ufs_vptofh,
56 ufs_init
7188ac27
KM
57};
58
7188ac27 59/*
d48157d5 60 * Called by vfs_mountroot when ufs is going to be mounted as root.
7188ac27 61 *
d48157d5 62 * Name is updated by mount(8) after booting.
7188ac27 63 */
8a4911ca 64#define ROOTNAME "root_device"
7188ac27
KM
65
66ufs_mountroot()
71e4e98b 67{
7188ac27
KM
68 register struct mount *mp;
69 extern struct vnode *rootvp;
70 struct ufsmount *ump;
71e4e98b 71 register struct fs *fs;
7188ac27
KM
72 u_int size;
73 int error;
71e4e98b 74
7188ac27
KM
75 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
76 M_MOUNT, M_WAITOK);
82161bc8
KM
77 mp->mnt_op = &ufs_vfsops;
78 mp->mnt_flag = MNT_RDONLY;
79 mp->mnt_exroot = 0;
80 mp->mnt_mounth = NULLVP;
7188ac27
KM
81 error = mountfs(rootvp, mp);
82 if (error) {
83 free((caddr_t)mp, M_MOUNT);
84 return (error);
71e4e98b 85 }
d48157d5 86 if (error = vfs_lock(mp)) {
7188ac27
KM
87 (void)ufs_unmount(mp, 0);
88 free((caddr_t)mp, M_MOUNT);
89 return (error);
6d07f4cd 90 }
d48157d5 91 rootfs = mp;
82161bc8
KM
92 mp->mnt_next = mp;
93 mp->mnt_prev = mp;
94 mp->mnt_vnodecovered = NULLVP;
7188ac27
KM
95 ump = VFSTOUFS(mp);
96 fs = ump->um_fs;
d45de50d 97 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
7188ac27 98 fs->fs_fsmnt[0] = '/';
82161bc8
KM
99 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
100 MNAMELEN);
101 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
102 &size);
103 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
104 (void) ufs_statfs(mp, &mp->mnt_stat);
7188ac27
KM
105 vfs_unlock(mp);
106 inittodr(fs->fs_time);
107 return (0);
108}
109
110/*
111 * VFS Operations.
112 *
113 * mount system call
114 */
115ufs_mount(mp, path, data, ndp)
d45de50d 116 register struct mount *mp;
7188ac27
KM
117 char *path;
118 caddr_t data;
119 struct nameidata *ndp;
120{
121 struct vnode *devvp;
122 struct ufs_args args;
123 struct ufsmount *ump;
124 register struct fs *fs;
125 u_int size;
126 int error;
127
128 if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
129 return (error);
3b931949
KM
130 /*
131 * Process export requests.
132 */
82161bc8
KM
133 if ((args.exflags & MNT_EXPORTED) || (mp->mnt_flag & MNT_EXPORTED)) {
134 if (args.exflags & MNT_EXPORTED)
135 mp->mnt_flag |= MNT_EXPORTED;
3b931949 136 else
82161bc8
KM
137 mp->mnt_flag &= ~MNT_EXPORTED;
138 if (args.exflags & MNT_EXRDONLY)
139 mp->mnt_flag |= MNT_EXRDONLY;
3b931949 140 else
82161bc8
KM
141 mp->mnt_flag &= ~MNT_EXRDONLY;
142 mp->mnt_exroot = args.exroot;
3b931949 143 }
82161bc8 144 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
3b931949
KM
145 if ((error = getmdev(&devvp, args.fspec, ndp)) != 0)
146 return (error);
d48157d5
KM
147 error = mountfs(devvp, mp);
148 } else {
149 ump = VFSTOUFS(mp);
150 fs = ump->um_fs;
82161bc8 151 if (fs->fs_ronly && (mp->mnt_flag & MNT_RDONLY) == 0)
d48157d5
KM
152 fs->fs_ronly = 0;
153 /*
154 * Verify that the specified device is the one that
155 * is really being used for the root file system.
156 */
3b931949
KM
157 if (args.fspec == 0)
158 return (0);
159 if ((error = getmdev(&devvp, args.fspec, ndp)) != 0)
160 return (error);
d48157d5
KM
161 if (devvp != ump->um_devvp)
162 error = EINVAL; /* needs translation */
2a7dbb09
KM
163 else
164 vrele(devvp);
d48157d5 165 }
7188ac27
KM
166 if (error) {
167 vrele(devvp);
168 return (error);
27d00e76 169 }
7188ac27
KM
170 ump = VFSTOUFS(mp);
171 fs = ump->um_fs;
172 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
173 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
82161bc8
KM
174 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
175 MNAMELEN);
176 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
177 &size);
178 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
179 (void) ufs_statfs(mp, &mp->mnt_stat);
7188ac27 180 return (0);
71e4e98b
SL
181}
182
7188ac27
KM
183/*
184 * Common code for mount and mountroot
185 */
186mountfs(devvp, mp)
1182ae61 187 register struct vnode *devvp;
7188ac27 188 struct mount *mp;
71e4e98b 189{
8dc876c1 190 register struct ufsmount *ump = (struct ufsmount *)0;
7188ac27 191 struct buf *bp = NULL;
71e4e98b 192 register struct fs *fs;
7188ac27 193 dev_t dev = devvp->v_rdev;
ec67a3ce
MK
194 struct partinfo dpart;
195 int havepart = 0, blks;
27d00e76 196 caddr_t base, space;
7188ac27
KM
197 int havepart = 0, blks;
198 int error, i, size;
6d07f4cd 199 int needclose = 0;
82161bc8 200 int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
1c26e003 201 extern struct vnode *rootvp;
71e4e98b 202
8dc876c1 203 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED))
7188ac27 204 return (error);
6d07f4cd 205 needclose = 1;
8dc876c1 206 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED) != 0) {
609e7cfa 207 size = DEV_BSIZE;
8dc876c1 208 } else {
ec67a3ce
MK
209 havepart = 1;
210 size = dpart.disklab->d_secsize;
7188ac27 211 }
8dc876c1 212 if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
71e4e98b 213 goto out;
e018935f 214 fs = bp->b_un.b_fs;
8dc876c1 215 error = EINVAL; /* XXX needs translation */
1c281610
MK
216 goto out;
217 }
8dc876c1 218 ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
7188ac27 219 ump->um_fs = (struct fs *)malloc((u_long)fs->fs_sbsize, M_SUPERBLK,
5adcb337 220 M_WAITOK);
7188ac27 221 bcopy((caddr_t)bp->b_un.b_addr, (caddr_t)ump->um_fs,
71e4e98b 222 (u_int)fs->fs_sbsize);
5d96a9ad
KM
223 if (fs->fs_sbsize < SBSIZE)
224 bp->b_flags |= B_INVAL;
e018935f
MK
225 brelse(bp);
226 bp = NULL;
7188ac27
KM
227 fs = ump->um_fs;
228 fs->fs_ronly = ronly;
71e4e98b
SL
229 if (ronly == 0)
230 fs->fs_fmod = 1;
609e7cfa
MK
231 if (havepart) {
232 dpart.part->p_fstype = FS_BSDFFS;
233 dpart.part->p_fsize = fs->fs_fsize;
234 dpart.part->p_frag = fs->fs_frag;
42ff4c2e 235 dpart.part->p_cpg = fs->fs_cpg;
609e7cfa 236 }
ec67a3ce
MK
237#ifdef SECSIZE
238 /*
239 * If we have a disk label, force per-partition
240 * filesystem information to be correct
241 * and set correct current fsbtodb shift.
242 */
243#endif SECSIZE
244 if (havepart) {
245 dpart.part->p_fstype = FS_BSDFFS;
246 dpart.part->p_fsize = fs->fs_fsize;
247 dpart.part->p_frag = fs->fs_frag;
248#ifdef SECSIZE
249#ifdef tahoe
250 /*
251 * Save the original fsbtodb shift to restore on updates.
252 * (Console doesn't understand fsbtodb changes.)
253 */
254 fs->fs_sparecon[0] = fs->fs_fsbtodb;
255#endif
256 i = fs->fs_fsize / size;
257 for (fs->fs_fsbtodb = 0; i > 1; i >>= 1)
258 fs->fs_fsbtodb++;
259#endif SECSIZE
260 fs->fs_dbsize = size;
261 }
71e4e98b 262 blks = howmany(fs->fs_cssize, fs->fs_fsize);
5adcb337
KM
263 base = space = (caddr_t)malloc((u_long)fs->fs_cssize, M_SUPERBLK,
264 M_WAITOK);
71e4e98b
SL
265 for (i = 0; i < blks; i += fs->fs_frag) {
266 size = fs->fs_bsize;
267 if (i + fs->fs_frag > blks)
268 size = (blks - i) * fs->fs_fsize;
ec67a3ce
MK
269#ifdef SECSIZE
270 tp = bread(dev, fsbtodb(fs, fs->fs_csaddr + i), size,
271 fs->fs_dbsize);
272#else SECSIZE
a937f856
KM
273 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
274 NOCRED, &bp);
7188ac27 275 if (error) {
5adcb337 276 free((caddr_t)base, M_SUPERBLK);
71e4e98b
SL
277 goto out;
278 }
e018935f 279 bcopy((caddr_t)bp->b_un.b_addr, space, (u_int)size);
60f9c9e3 280 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
71e4e98b 281 space += size;
e018935f
MK
282 brelse(bp);
283 bp = NULL;
71e4e98b 284 }
82161bc8
KM
285 mp->mnt_data = (qaddr_t)ump;
286 mp->mnt_stat.f_fsid.val[0] = (long)dev;
287 mp->mnt_stat.f_fsid.val[1] = MOUNT_UFS;
288 mp->mnt_flag |= MNT_LOCAL;
7188ac27
KM
289 ump->um_mountp = mp;
290 ump->um_dev = dev;
291 ump->um_devvp = devvp;
8dc876c1
KM
292 for (i = 0; i < MAXQUOTAS; i++)
293 ump->um_quotas[i] = NULLVP;
0f93ba7b 294 devvp->v_specflags |= SI_MOUNTEDON;
7188ac27 295
94354803
KM
296 /* Sanity checks for old file systems. XXX */
297 fs->fs_npsect = MAX(fs->fs_npsect, fs->fs_nsect); /* XXX */
298 fs->fs_interleave = MAX(fs->fs_interleave, 1); /* XXX */
2af59000
KM
299 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
300 fs->fs_nrpos = 8; /* XXX */
609e7cfa 301
7188ac27 302 return (0);
71e4e98b 303out:
c86c9b6e
KM
304 if (bp)
305 brelse(bp);
609e7cfa 306 if (needclose)
a937f856 307 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED);
8dc876c1 308 if (ump) {
7188ac27 309 free((caddr_t)ump->um_fs, M_SUPERBLK);
8dc876c1 310 free((caddr_t)ump, M_UFSMNT);
82161bc8 311 mp->mnt_data = (qaddr_t)0;
27d00e76 312 }
7188ac27 313 return (error);
71e4e98b
SL
314}
315
5bf9d21f
KM
316/*
317 * Make a filesystem operational.
318 * Nothing to do at the moment.
319 */
31593ba7 320/* ARGSUSED */
5bf9d21f
KM
321ufs_start(mp, flags)
322 struct mount *mp;
323 int flags;
324{
325
326 return (0);
327}
71e4e98b 328
7188ac27
KM
329/*
330 * unmount system call
331 */
8dc876c1 332ufs_unmount(mp, mntflags)
7188ac27 333 struct mount *mp;
8dc876c1 334 int mntflags;
71e4e98b 335{
7188ac27 336 register struct ufsmount *ump;
71e4e98b 337 register struct fs *fs;
8dc876c1 338 int i, error, ronly, flags = 0;
71e4e98b 339
8dc876c1 340 if (mntflags & MNT_FORCE)
7188ac27 341 return (EINVAL);
8dc876c1
KM
342 if (mntflags & MNT_FORCE)
343 flags |= FORCECLOSE;
5d96a9ad
KM
344 mntflushbuf(mp, 0);
345 if (mntinvalbuf(mp))
346 return (EBUSY);
7188ac27 347 ump = VFSTOUFS(mp);
ec67a3ce 348 return (error);
71e4e98b 349#ifdef QUOTA
82161bc8 350 if (mp->mnt_flag & MNT_QUOTA) {
8dc876c1 351 if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
6d943995 352 return (error);
8dc876c1
KM
353 for (i = 0; i < MAXQUOTAS; i++) {
354 if (ump->um_quotas[i] == NULLVP)
355 continue;
356 quotaoff(mp, i);
357 }
6d943995 358 /*
8dc876c1
KM
359 * Here we fall through to vflush again to ensure
360 * that we have gotten rid of all the system vnodes.
6d943995 361 */
8dc876c1 362 }
71e4e98b 363#endif
8dc876c1 364 if (error = vflush(mp, NULLVP, flags))
6d943995 365 return (error);
7188ac27
KM
366 fs = ump->um_fs;
367 ronly = !fs->fs_ronly;
ec67a3ce
MK
368 error = closei(dev, IFBLK, fs->fs_ronly? FREAD : FREAD|FWRITE);
369 irele(ip);
370 return (error);
71e4e98b
SL
371}
372
7188ac27
KM
373/*
374 * Return root of a filesystem
375 */
376ufs_root(mp, vpp)
377 struct mount *mp;
378 struct vnode **vpp;
379{
31593ba7
KM
380 register struct inode *ip;
381 struct inode *nip;
382 struct vnode tvp;
7188ac27
KM
383 int error;
384
31593ba7
KM
385 tvp.v_mount = mp;
386 ip = VTOI(&tvp);
387 ip->i_vnode = &tvp;
388 ip->i_dev = VFSTOUFS(mp)->um_dev;
389 error = iget(ip, (ino_t)ROOTINO, &nip);
7188ac27
KM
390 if (error)
391 return (error);
31593ba7 392 *vpp = ITOV(nip);
7188ac27
KM
393 return (0);
394}
395
8dc876c1
KM
396/*
397 * Do operations associated with quotas
398 */
399ufs_quotactl(mp, cmds, uid, arg)
400 struct mount *mp;
401 int cmds;
402 uid_t uid;
403 caddr_t arg;
404{
405 register struct nameidata *ndp = &u.u_nd;
406 struct ufsmount *ump = VFSTOUFS(mp);
8e940f59 407 struct proc *p = u.u_procp; /* XXX */
8dc876c1
KM
408 int cmd, type, error;
409
410#ifndef QUOTA
411 return (EOPNOTSUPP);
412#else
413 if (uid == -1)
8e940f59 414 uid = p->p_ruid;
8dc876c1
KM
415 cmd = cmds >> SUBCMDSHIFT;
416
417 switch (cmd) {
418 case Q_GETQUOTA:
419 case Q_SYNC:
8e940f59 420 if (uid == p->p_ruid)
8dc876c1
KM
421 break;
422 /* fall through */
423 default:
424 if (error = suser(ndp->ni_cred, &u.u_acflag))
425 return (error);
426 }
427
428 type = cmd & SUBCMDMASK;
429 if ((u_int)type >= MAXQUOTAS)
430 return (EINVAL);
431
432 switch (cmd) {
433
434 case Q_QUOTAON:
435 return (quotaon(ndp, mp, type, arg));
436
437 case Q_QUOTAOFF:
438 if (vfs_busy(mp))
439 return (0);
440 error = quotaoff(mp, type);
441 vfs_unbusy(mp);
442 return (error);
443
444 case Q_SETQUOTA:
445 return (setquota(mp, uid, type, arg));
446
447 case Q_SETUSE:
448 return (setuse(mp, uid, type, arg));
449
450 case Q_GETQUOTA:
451 return (getquota(mp, uid, type, arg));
452
453 case Q_SYNC:
454 if (vfs_busy(mp))
455 return (0);
456 error = qsync(mp);
457 vfs_unbusy(mp);
458 return (error);
459
460 default:
461 return (EINVAL);
462 }
463 /* NOTREACHED */
464#endif
465}
466
7188ac27
KM
467/*
468 * Get file system statistics.
469 */
470ufs_statfs(mp, sbp)
471 struct mount *mp;
472 register struct statfs *sbp;
473{
474 register struct ufsmount *ump;
475 register struct fs *fs;
476
477 ump = VFSTOUFS(mp);
478 fs = ump->um_fs;
479 if (fs->fs_magic != FS_MAGIC)
480 panic("ufs_statfs");
481 sbp->f_type = MOUNT_UFS;
7188ac27
KM
482 sbp->f_fsize = fs->fs_fsize;
483 sbp->f_bsize = fs->fs_bsize;
484 sbp->f_blocks = fs->fs_dsize;
485 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
486 fs->fs_cstotal.cs_nffree;
487 sbp->f_bavail = (fs->fs_dsize * (100 - fs->fs_minfree) / 100) -
488 (fs->fs_dsize - sbp->f_bfree);
5c41c19e 489 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
7188ac27 490 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
82161bc8
KM
491 if (sbp != &mp->mnt_stat) {
492 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
d45de50d 493 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
82161bc8 494 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
d45de50d
KM
495 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
496 }
7188ac27
KM
497 return (0);
498}
499
500int syncprt = 0;
501
502/*
503 * Go through the disk queues to initiate sandbagged IO;
504 * go through the inodes to write those that have been modified;
505 * initiate the writing of the super block if it has been modified.
8dc876c1
KM
506 *
507 * Note: we are always called with the filesystem marked `MPBUSY'.
7188ac27
KM
508 */
509ufs_sync(mp, waitfor)
71e4e98b 510 struct mount *mp;
7188ac27 511 int waitfor;
71e4e98b 512{
31593ba7 513 register struct vnode *vp;
7188ac27
KM
514 register struct inode *ip;
515 register struct ufsmount *ump = VFSTOUFS(mp);
516 register struct fs *fs;
812b91f3 517 int error, allerror = 0;
7188ac27
KM
518
519 if (syncprt)
520 bufstats();
7188ac27 521 fs = ump->um_fs;
7188ac27
KM
522 /*
523 * Write back modified superblock.
524 * Consistency check that the superblock
525 * is still in the buffer cache.
526 */
527 if (fs->fs_fmod != 0) {
528 if (fs->fs_ronly != 0) { /* XXX */
529 printf("fs = %s\n", fs->fs_fsmnt);
530 panic("update: rofs mod");
531 }
532 fs->fs_fmod = 0;
533 fs->fs_time = time.tv_sec;
881f1b87 534 allerror = sbupdate(ump, waitfor);
7188ac27
KM
535 }
536 /*
537 * Write back each (modified) inode.
538 */
25b5cf58 539loop:
a242f429
KM
540 for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
541 /*
542 * If the vnode that we are about to sync is no longer
543 * associated with this mount point, start over.
544 */
545 if (vp->v_mount != mp)
546 goto loop;
31593ba7 547 ip = VTOI(vp);
25b5cf58
KM
548 if ((ip->i_flag & (IMOD|IACC|IUPD|ICHG)) == 0 &&
549 vp->v_dirtyblkhd == NULL)
7188ac27 550 continue;
25b5cf58
KM
551 if (vget(vp))
552 goto loop;
553 if (vp->v_dirtyblkhd)
554 vflushbuf(vp, 0);
555 if ((ip->i_flag & (IMOD|IACC|IUPD|ICHG)) &&
556 (error = iupdat(ip, &time, &time, 0)))
812b91f3
KM
557 allerror = error;
558 vput(vp);
7188ac27 559 }
7188ac27 560 /*
5d96a9ad 561 * Force stale file system control information to be flushed.
7188ac27 562 */
5d96a9ad 563 vflushbuf(ump->um_devvp, waitfor == MNT_WAIT ? B_SYNC : 0);
8dc876c1
KM
564#ifdef QUOTA
565 qsync(mp);
566#endif
812b91f3 567 return (allerror);
7188ac27
KM
568}
569
570/*
571 * Write a superblock and associated information back to disk.
572 */
573sbupdate(mp, waitfor)
574 struct ufsmount *mp;
575 int waitfor;
576{
577 register struct fs *fs = mp->um_fs;
71e4e98b
SL
578 register struct buf *bp;
579 int blks;
580 caddr_t space;
7188ac27 581 int i, size, error = 0;
71e4e98b 582
ec67a3ce
MK
583#ifdef SECSIZE
584 bp = getblk(mp->m_dev, (daddr_t)fsbtodb(fs, SBOFF / fs->fs_fsize),
585 (int)fs->fs_sbsize, fs->fs_dbsize);
586#else SECSIZE
7188ac27 587 bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize);
ec67a3ce 588#endif SECSIZE
71e4e98b 589 bcopy((caddr_t)fs, bp->b_un.b_addr, (u_int)fs->fs_sbsize);
2af59000
KM
590 /* Restore compatibility to old file systems. XXX */
591 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
592 bp->b_un.b_fs->fs_nrpos = -1; /* XXX */
ec67a3ce
MK
593#ifdef SECSIZE
594#ifdef tahoe
595 /* restore standard fsbtodb shift */
596 bp->b_un.b_fs->fs_fsbtodb = fs->fs_sparecon[0];
597 bp->b_un.b_fs->fs_sparecon[0] = 0;
598#endif
599#endif SECSIZE
7188ac27
KM
600 if (waitfor == MNT_WAIT)
601 error = bwrite(bp);
602 else
603 bawrite(bp);
71e4e98b
SL
604 blks = howmany(fs->fs_cssize, fs->fs_fsize);
605 space = (caddr_t)fs->fs_csp[0];
606 for (i = 0; i < blks; i += fs->fs_frag) {
607 size = fs->fs_bsize;
608 if (i + fs->fs_frag > blks)
609 size = (blks - i) * fs->fs_fsize;
ec67a3ce
MK
610#ifdef SECSIZE
611 bp = getblk(mp->m_dev, fsbtodb(fs, fs->fs_csaddr + i), size,
612 fs->fs_dbsize);
613#else SECSIZE
7188ac27 614 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), size);
ec67a3ce 615#endif SECSIZE
71e4e98b
SL
616 bcopy(space, bp->b_un.b_addr, (u_int)size);
617 space += size;
7188ac27
KM
618 if (waitfor == MNT_WAIT)
619 error = bwrite(bp);
620 else
621 bawrite(bp);
71e4e98b 622 }
7188ac27 623 return (error);
71e4e98b
SL
624}
625
626/*
7188ac27
KM
627 * Print out statistics on the current allocation of the buffer pool.
628 * Can be enabled to print out on every ``sync'' by setting "syncprt"
629 * above.
630 */
631bufstats()
632{
633 int s, i, j, count;
634 register struct buf *bp, *dp;
635 int counts[MAXBSIZE/CLBYTES+1];
636 static char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE", "EMPTY" };
637
638 for (bp = bfreelist, i = 0; bp < &bfreelist[BQUEUES]; bp++, i++) {
639 count = 0;
640 for (j = 0; j <= MAXBSIZE/CLBYTES; j++)
641 counts[j] = 0;
642 s = splbio();
643 for (dp = bp->av_forw; dp != bp; dp = dp->av_forw) {
644 counts[dp->b_bufsize/CLBYTES]++;
645 count++;
646 }
647 splx(s);
648 printf("%s: total-%d", bname[i], count);
649 for (j = 0; j <= MAXBSIZE/CLBYTES; j++)
650 if (counts[j] != 0)
651 printf(", %d-%d", j * CLBYTES, counts[j]);
652 printf("\n");
653 }
654}
655
656/*
657 * File handle to vnode
113c35f2
KM
658 *
659 * Have to be really careful about stale file handles:
660 * - check that the inode number is in range
661 * - call iget() to get the locked inode
662 * - check for an unallocated inode (i_mode == 0)
663 * - check that the generation number matches
7188ac27
KM
664 */
665ufs_fhtovp(mp, fhp, vpp)
113c35f2 666 register struct mount *mp;
7188ac27
KM
667 struct fid *fhp;
668 struct vnode **vpp;
669{
670 register struct ufid *ufhp;
113c35f2 671 register struct fs *fs;
31593ba7
KM
672 register struct inode *ip;
673 struct inode *nip;
674 struct vnode tvp;
7188ac27
KM
675 int error;
676
677 ufhp = (struct ufid *)fhp;
113c35f2
KM
678 fs = VFSTOUFS(mp)->um_fs;
679 if (ufhp->ufid_ino < ROOTINO ||
680 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg) {
82161bc8 681 *vpp = NULLVP;
113c35f2
KM
682 return (EINVAL);
683 }
31593ba7
KM
684 tvp.v_mount = mp;
685 ip = VTOI(&tvp);
686 ip->i_vnode = &tvp;
687 ip->i_dev = VFSTOUFS(mp)->um_dev;
688 if (error = iget(ip, ufhp->ufid_ino, &nip)) {
82161bc8 689 *vpp = NULLVP;
7188ac27
KM
690 return (error);
691 }
31593ba7 692 ip = nip;
113c35f2
KM
693 if (ip->i_mode == 0) {
694 iput(ip);
82161bc8 695 *vpp = NULLVP;
113c35f2
KM
696 return (EINVAL);
697 }
7188ac27
KM
698 if (ip->i_gen != ufhp->ufid_gen) {
699 iput(ip);
82161bc8 700 *vpp = NULLVP;
7188ac27
KM
701 return (EINVAL);
702 }
703 *vpp = ITOV(ip);
704 return (0);
705}
706
707/*
29e77c27 708 * Vnode pointer to File handle
7188ac27
KM
709 */
710/* ARGSUSED */
758e3529
KM
711ufs_vptofh(vp, fhp)
712 struct vnode *vp;
7188ac27 713 struct fid *fhp;
7188ac27 714{
758e3529
KM
715 register struct inode *ip = VTOI(vp);
716 register struct ufid *ufhp;
7188ac27 717
758e3529
KM
718 ufhp = (struct ufid *)fhp;
719 ufhp->ufid_len = sizeof(struct ufid);
720 ufhp->ufid_ino = ip->i_number;
721 ufhp->ufid_gen = ip->i_gen;
722 return (0);
7188ac27
KM
723}
724
725/*
71e4e98b
SL
726 * Check that the user's argument is a reasonable
727 * thing on which to mount, and return the device number if so.
728 */
7188ac27
KM
729getmdev(devvpp, fname, ndp)
730 struct vnode **devvpp;
715baff1 731 caddr_t fname;
7188ac27 732 register struct nameidata *ndp;
71e4e98b 733{
7188ac27
KM
734 register struct vnode *vp;
735 int error;
71e4e98b 736
6c845185 737 ndp->ni_nameiop = LOOKUP | FOLLOW;
715baff1
KM
738 ndp->ni_segflg = UIO_USERSPACE;
739 ndp->ni_dirp = fname;
43c93704 740 if (error = namei(ndp))
7188ac27 741 return (error);
7188ac27
KM
742 vp = ndp->ni_vp;
743 if (vp->v_type != VBLK) {
6c845185 744 vrele(vp);
71e4e98b 745 return (ENOTBLK);
e95cdf5c 746 }
e547d435 747 if (major(vp->v_rdev) >= nblkdev) {
6c845185 748 vrele(vp);
71e4e98b 749 return (ENXIO);
e547d435 750 }
7188ac27 751 *devvpp = vp;
71e4e98b
SL
752 return (0);
753}