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