add global flag to allow forcible unmounts to be attempted;
[unix-history] / usr / src / sys / ufs / ffs / ufs_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 *
d85a9d1b 7 * @(#)ufs_vfsops.c 7.54 (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
d85a9d1b
KM
46/*
47 * Flag to allow forcible unmounting.
48 */
49int doforce = 1;
50
7188ac27 51/*
d48157d5 52 * Called by vfs_mountroot when ufs is going to be mounted as root.
7188ac27 53 *
d48157d5 54 * Name is updated by mount(8) after booting.
7188ac27 55 */
8a4911ca 56#define ROOTNAME "root_device"
7188ac27
KM
57
58ufs_mountroot()
71e4e98b 59{
7188ac27
KM
60 register struct mount *mp;
61 extern struct vnode *rootvp;
0eb6f54a 62 struct proc *p = curproc; /* XXX */
7188ac27 63 struct ufsmount *ump;
71e4e98b 64 register struct fs *fs;
7188ac27
KM
65 u_int size;
66 int error;
71e4e98b 67
7188ac27
KM
68 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
69 M_MOUNT, M_WAITOK);
82161bc8
KM
70 mp->mnt_op = &ufs_vfsops;
71 mp->mnt_flag = MNT_RDONLY;
72 mp->mnt_exroot = 0;
73 mp->mnt_mounth = NULLVP;
0eb6f54a 74 error = mountfs(rootvp, mp, p);
7188ac27
KM
75 if (error) {
76 free((caddr_t)mp, M_MOUNT);
77 return (error);
71e4e98b 78 }
d48157d5 79 if (error = vfs_lock(mp)) {
0eb6f54a 80 (void)ufs_unmount(mp, 0, p);
7188ac27
KM
81 free((caddr_t)mp, M_MOUNT);
82 return (error);
6d07f4cd 83 }
d48157d5 84 rootfs = mp;
82161bc8
KM
85 mp->mnt_next = mp;
86 mp->mnt_prev = mp;
87 mp->mnt_vnodecovered = NULLVP;
7188ac27
KM
88 ump = VFSTOUFS(mp);
89 fs = ump->um_fs;
d45de50d 90 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
7188ac27 91 fs->fs_fsmnt[0] = '/';
82161bc8
KM
92 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
93 MNAMELEN);
94 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
95 &size);
96 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
0eb6f54a 97 (void) ufs_statfs(mp, &mp->mnt_stat, p);
7188ac27
KM
98 vfs_unlock(mp);
99 inittodr(fs->fs_time);
100 return (0);
101}
102
103/*
104 * VFS Operations.
105 *
106 * mount system call
107 */
0eb6f54a 108ufs_mount(mp, path, data, ndp, p)
d45de50d 109 register struct mount *mp;
7188ac27
KM
110 char *path;
111 caddr_t data;
112 struct nameidata *ndp;
0eb6f54a 113 struct proc *p;
7188ac27
KM
114{
115 struct vnode *devvp;
116 struct ufs_args args;
117 struct ufsmount *ump;
118 register struct fs *fs;
119 u_int size;
120 int error;
121
122 if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
123 return (error);
3b931949
KM
124 /*
125 * Process export requests.
126 */
82161bc8
KM
127 if ((args.exflags & MNT_EXPORTED) || (mp->mnt_flag & MNT_EXPORTED)) {
128 if (args.exflags & MNT_EXPORTED)
129 mp->mnt_flag |= MNT_EXPORTED;
3b931949 130 else
82161bc8
KM
131 mp->mnt_flag &= ~MNT_EXPORTED;
132 if (args.exflags & MNT_EXRDONLY)
133 mp->mnt_flag |= MNT_EXRDONLY;
3b931949 134 else
82161bc8
KM
135 mp->mnt_flag &= ~MNT_EXRDONLY;
136 mp->mnt_exroot = args.exroot;
3b931949 137 }
82161bc8 138 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
0eb6f54a 139 if ((error = getmdev(&devvp, args.fspec, ndp, p)) != 0)
3b931949 140 return (error);
0eb6f54a 141 error = mountfs(devvp, mp, p);
d48157d5
KM
142 } else {
143 ump = VFSTOUFS(mp);
144 fs = ump->um_fs;
82161bc8 145 if (fs->fs_ronly && (mp->mnt_flag & MNT_RDONLY) == 0)
d48157d5
KM
146 fs->fs_ronly = 0;
147 /*
148 * Verify that the specified device is the one that
149 * is really being used for the root file system.
150 */
3b931949
KM
151 if (args.fspec == 0)
152 return (0);
0eb6f54a 153 if ((error = getmdev(&devvp, args.fspec, ndp, p)) != 0)
3b931949 154 return (error);
d48157d5
KM
155 if (devvp != ump->um_devvp)
156 error = EINVAL; /* needs translation */
2a7dbb09
KM
157 else
158 vrele(devvp);
d48157d5 159 }
7188ac27
KM
160 if (error) {
161 vrele(devvp);
162 return (error);
27d00e76 163 }
7188ac27
KM
164 ump = VFSTOUFS(mp);
165 fs = ump->um_fs;
166 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
167 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
82161bc8
KM
168 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
169 MNAMELEN);
170 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
171 &size);
172 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
0eb6f54a 173 (void) ufs_statfs(mp, &mp->mnt_stat, p);
7188ac27 174 return (0);
71e4e98b
SL
175}
176
7188ac27
KM
177/*
178 * Common code for mount and mountroot
179 */
0eb6f54a 180mountfs(devvp, mp, p)
1182ae61 181 register struct vnode *devvp;
7188ac27 182 struct mount *mp;
0eb6f54a 183 struct proc *p;
71e4e98b 184{
8dc876c1 185 register struct ufsmount *ump = (struct ufsmount *)0;
7188ac27 186 struct buf *bp = NULL;
71e4e98b 187 register struct fs *fs;
7188ac27 188 dev_t dev = devvp->v_rdev;
ec67a3ce
MK
189 struct partinfo dpart;
190 int havepart = 0, blks;
27d00e76 191 caddr_t base, space;
7188ac27
KM
192 int havepart = 0, blks;
193 int error, i, size;
6d07f4cd 194 int needclose = 0;
82161bc8 195 int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
1c26e003 196 extern struct vnode *rootvp;
71e4e98b 197
0eb6f54a 198 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p))
7188ac27 199 return (error);
6d07f4cd 200 needclose = 1;
0eb6f54a 201 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
609e7cfa 202 size = DEV_BSIZE;
0eb6f54a 203 else {
ec67a3ce
MK
204 havepart = 1;
205 size = dpart.disklab->d_secsize;
7188ac27 206 }
8dc876c1 207 if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
71e4e98b 208 goto out;
e018935f 209 fs = bp->b_un.b_fs;
8dc876c1 210 error = EINVAL; /* XXX needs translation */
1c281610
MK
211 goto out;
212 }
8dc876c1 213 ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
7188ac27 214 ump->um_fs = (struct fs *)malloc((u_long)fs->fs_sbsize, M_SUPERBLK,
5adcb337 215 M_WAITOK);
7188ac27 216 bcopy((caddr_t)bp->b_un.b_addr, (caddr_t)ump->um_fs,
71e4e98b 217 (u_int)fs->fs_sbsize);
5d96a9ad
KM
218 if (fs->fs_sbsize < SBSIZE)
219 bp->b_flags |= B_INVAL;
e018935f
MK
220 brelse(bp);
221 bp = NULL;
7188ac27
KM
222 fs = ump->um_fs;
223 fs->fs_ronly = ronly;
71e4e98b
SL
224 if (ronly == 0)
225 fs->fs_fmod = 1;
609e7cfa
MK
226 if (havepart) {
227 dpart.part->p_fstype = FS_BSDFFS;
228 dpart.part->p_fsize = fs->fs_fsize;
229 dpart.part->p_frag = fs->fs_frag;
42ff4c2e 230 dpart.part->p_cpg = fs->fs_cpg;
609e7cfa 231 }
ec67a3ce
MK
232#ifdef SECSIZE
233 /*
234 * If we have a disk label, force per-partition
235 * filesystem information to be correct
236 * and set correct current fsbtodb shift.
237 */
238#endif SECSIZE
239 if (havepart) {
240 dpart.part->p_fstype = FS_BSDFFS;
241 dpart.part->p_fsize = fs->fs_fsize;
242 dpart.part->p_frag = fs->fs_frag;
243#ifdef SECSIZE
244#ifdef tahoe
245 /*
246 * Save the original fsbtodb shift to restore on updates.
247 * (Console doesn't understand fsbtodb changes.)
248 */
249 fs->fs_sparecon[0] = fs->fs_fsbtodb;
250#endif
251 i = fs->fs_fsize / size;
252 for (fs->fs_fsbtodb = 0; i > 1; i >>= 1)
253 fs->fs_fsbtodb++;
254#endif SECSIZE
255 fs->fs_dbsize = size;
256 }
71e4e98b 257 blks = howmany(fs->fs_cssize, fs->fs_fsize);
5adcb337
KM
258 base = space = (caddr_t)malloc((u_long)fs->fs_cssize, M_SUPERBLK,
259 M_WAITOK);
71e4e98b
SL
260 for (i = 0; i < blks; i += fs->fs_frag) {
261 size = fs->fs_bsize;
262 if (i + fs->fs_frag > blks)
263 size = (blks - i) * fs->fs_fsize;
ec67a3ce
MK
264#ifdef SECSIZE
265 tp = bread(dev, fsbtodb(fs, fs->fs_csaddr + i), size,
266 fs->fs_dbsize);
267#else SECSIZE
a937f856
KM
268 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
269 NOCRED, &bp);
7188ac27 270 if (error) {
5adcb337 271 free((caddr_t)base, M_SUPERBLK);
71e4e98b
SL
272 goto out;
273 }
e018935f 274 bcopy((caddr_t)bp->b_un.b_addr, space, (u_int)size);
60f9c9e3 275 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
71e4e98b 276 space += size;
e018935f
MK
277 brelse(bp);
278 bp = NULL;
71e4e98b 279 }
82161bc8
KM
280 mp->mnt_data = (qaddr_t)ump;
281 mp->mnt_stat.f_fsid.val[0] = (long)dev;
282 mp->mnt_stat.f_fsid.val[1] = MOUNT_UFS;
283 mp->mnt_flag |= MNT_LOCAL;
7188ac27
KM
284 ump->um_mountp = mp;
285 ump->um_dev = dev;
286 ump->um_devvp = devvp;
8dc876c1
KM
287 for (i = 0; i < MAXQUOTAS; i++)
288 ump->um_quotas[i] = NULLVP;
0f93ba7b 289 devvp->v_specflags |= SI_MOUNTEDON;
7188ac27 290
94354803
KM
291 /* Sanity checks for old file systems. XXX */
292 fs->fs_npsect = MAX(fs->fs_npsect, fs->fs_nsect); /* XXX */
293 fs->fs_interleave = MAX(fs->fs_interleave, 1); /* XXX */
2af59000
KM
294 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
295 fs->fs_nrpos = 8; /* XXX */
609e7cfa 296
7188ac27 297 return (0);
71e4e98b 298out:
c86c9b6e
KM
299 if (bp)
300 brelse(bp);
609e7cfa 301 if (needclose)
0eb6f54a 302 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
8dc876c1 303 if (ump) {
7188ac27 304 free((caddr_t)ump->um_fs, M_SUPERBLK);
8dc876c1 305 free((caddr_t)ump, M_UFSMNT);
82161bc8 306 mp->mnt_data = (qaddr_t)0;
27d00e76 307 }
7188ac27 308 return (error);
71e4e98b
SL
309}
310
5bf9d21f
KM
311/*
312 * Make a filesystem operational.
313 * Nothing to do at the moment.
314 */
31593ba7 315/* ARGSUSED */
0eb6f54a 316ufs_start(mp, flags, p)
5bf9d21f
KM
317 struct mount *mp;
318 int flags;
0eb6f54a 319 struct proc *p;
5bf9d21f
KM
320{
321
322 return (0);
323}
71e4e98b 324
7188ac27
KM
325/*
326 * unmount system call
327 */
0eb6f54a 328ufs_unmount(mp, mntflags, p)
7188ac27 329 struct mount *mp;
8dc876c1 330 int mntflags;
0eb6f54a 331 struct proc *p;
71e4e98b 332{
7188ac27 333 register struct ufsmount *ump;
71e4e98b 334 register struct fs *fs;
8dc876c1 335 int i, error, ronly, flags = 0;
71e4e98b 336
87be6db2 337 if (mntflags & MNT_FORCE) {
d85a9d1b 338 if (!doforce || mp == rootfs)
87be6db2 339 return (EINVAL);
8dc876c1 340 flags |= FORCECLOSE;
87be6db2 341 }
5d96a9ad
KM
342 mntflushbuf(mp, 0);
343 if (mntinvalbuf(mp))
344 return (EBUSY);
7188ac27 345 ump = VFSTOUFS(mp);
ec67a3ce 346 return (error);
71e4e98b 347#ifdef QUOTA
82161bc8 348 if (mp->mnt_flag & MNT_QUOTA) {
8dc876c1 349 if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
6d943995 350 return (error);
8dc876c1
KM
351 for (i = 0; i < MAXQUOTAS; i++) {
352 if (ump->um_quotas[i] == NULLVP)
353 continue;
354 quotaoff(mp, i);
355 }
6d943995 356 /*
8dc876c1
KM
357 * Here we fall through to vflush again to ensure
358 * that we have gotten rid of all the system vnodes.
6d943995 359 */
8dc876c1 360 }
71e4e98b 361#endif
8dc876c1 362 if (error = vflush(mp, NULLVP, flags))
6d943995 363 return (error);
7188ac27
KM
364 fs = ump->um_fs;
365 ronly = !fs->fs_ronly;
ec67a3ce
MK
366 error = closei(dev, IFBLK, fs->fs_ronly? FREAD : FREAD|FWRITE);
367 irele(ip);
368 return (error);
71e4e98b
SL
369}
370
7188ac27
KM
371/*
372 * Return root of a filesystem
373 */
374ufs_root(mp, vpp)
375 struct mount *mp;
376 struct vnode **vpp;
377{
31593ba7
KM
378 register struct inode *ip;
379 struct inode *nip;
380 struct vnode tvp;
7188ac27
KM
381 int error;
382
31593ba7
KM
383 tvp.v_mount = mp;
384 ip = VTOI(&tvp);
385 ip->i_vnode = &tvp;
386 ip->i_dev = VFSTOUFS(mp)->um_dev;
387 error = iget(ip, (ino_t)ROOTINO, &nip);
7188ac27
KM
388 if (error)
389 return (error);
31593ba7 390 *vpp = ITOV(nip);
7188ac27
KM
391 return (0);
392}
393
8dc876c1
KM
394/*
395 * Do operations associated with quotas
396 */
0eb6f54a 397ufs_quotactl(mp, cmds, uid, arg, p)
8dc876c1
KM
398 struct mount *mp;
399 int cmds;
400 uid_t uid;
401 caddr_t arg;
0eb6f54a 402 struct proc *p;
8dc876c1 403{
8dc876c1
KM
404 struct ufsmount *ump = VFSTOUFS(mp);
405 int cmd, type, error;
406
407#ifndef QUOTA
408 return (EOPNOTSUPP);
409#else
410 if (uid == -1)
c6f5111d 411 uid = p->p_cred->p_ruid;
8dc876c1
KM
412 cmd = cmds >> SUBCMDSHIFT;
413
414 switch (cmd) {
415 case Q_GETQUOTA:
416 case Q_SYNC:
c6f5111d 417 if (uid == p->p_cred->p_ruid)
8dc876c1
KM
418 break;
419 /* fall through */
420 default:
c6f5111d 421 if (error = suser(p->p_ucred, &p->p_acflag))
8dc876c1
KM
422 return (error);
423 }
424
425 type = cmd & SUBCMDMASK;
426 if ((u_int)type >= MAXQUOTAS)
427 return (EINVAL);
428
429 switch (cmd) {
430
431 case Q_QUOTAON:
c6f5111d 432 return (quotaon(p, mp, type, arg));
8dc876c1
KM
433
434 case Q_QUOTAOFF:
435 if (vfs_busy(mp))
436 return (0);
437 error = quotaoff(mp, type);
438 vfs_unbusy(mp);
439 return (error);
440
441 case Q_SETQUOTA:
442 return (setquota(mp, uid, type, arg));
443
444 case Q_SETUSE:
445 return (setuse(mp, uid, type, arg));
446
447 case Q_GETQUOTA:
448 return (getquota(mp, uid, type, arg));
449
450 case Q_SYNC:
451 if (vfs_busy(mp))
452 return (0);
453 error = qsync(mp);
454 vfs_unbusy(mp);
455 return (error);
456
457 default:
458 return (EINVAL);
459 }
460 /* NOTREACHED */
461#endif
462}
463
7188ac27
KM
464/*
465 * Get file system statistics.
466 */
0eb6f54a 467ufs_statfs(mp, sbp, p)
7188ac27
KM
468 struct mount *mp;
469 register struct statfs *sbp;
0eb6f54a 470 struct proc *p;
7188ac27
KM
471{
472 register struct ufsmount *ump;
473 register struct fs *fs;
474
475 ump = VFSTOUFS(mp);
476 fs = ump->um_fs;
477 if (fs->fs_magic != FS_MAGIC)
478 panic("ufs_statfs");
479 sbp->f_type = MOUNT_UFS;
7188ac27
KM
480 sbp->f_fsize = fs->fs_fsize;
481 sbp->f_bsize = fs->fs_bsize;
482 sbp->f_blocks = fs->fs_dsize;
483 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
484 fs->fs_cstotal.cs_nffree;
485 sbp->f_bavail = (fs->fs_dsize * (100 - fs->fs_minfree) / 100) -
486 (fs->fs_dsize - sbp->f_bfree);
5c41c19e 487 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
7188ac27 488 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
82161bc8
KM
489 if (sbp != &mp->mnt_stat) {
490 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
d45de50d 491 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
82161bc8 492 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
d45de50d
KM
493 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
494 }
7188ac27
KM
495 return (0);
496}
497
498int syncprt = 0;
499
500/*
501 * Go through the disk queues to initiate sandbagged IO;
502 * go through the inodes to write those that have been modified;
503 * initiate the writing of the super block if it has been modified.
8dc876c1
KM
504 *
505 * Note: we are always called with the filesystem marked `MPBUSY'.
7188ac27
KM
506 */
507ufs_sync(mp, waitfor)
71e4e98b 508 struct mount *mp;
7188ac27 509 int waitfor;
71e4e98b 510{
31593ba7 511 register struct vnode *vp;
7188ac27
KM
512 register struct inode *ip;
513 register struct ufsmount *ump = VFSTOUFS(mp);
514 register struct fs *fs;
812b91f3 515 int error, allerror = 0;
7188ac27
KM
516
517 if (syncprt)
518 bufstats();
7188ac27 519 fs = ump->um_fs;
7188ac27
KM
520 /*
521 * Write back modified superblock.
522 * Consistency check that the superblock
523 * is still in the buffer cache.
524 */
525 if (fs->fs_fmod != 0) {
526 if (fs->fs_ronly != 0) { /* XXX */
527 printf("fs = %s\n", fs->fs_fsmnt);
528 panic("update: rofs mod");
529 }
530 fs->fs_fmod = 0;
531 fs->fs_time = time.tv_sec;
881f1b87 532 allerror = sbupdate(ump, waitfor);
7188ac27
KM
533 }
534 /*
535 * Write back each (modified) inode.
536 */
25b5cf58 537loop:
a242f429
KM
538 for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
539 /*
540 * If the vnode that we are about to sync is no longer
541 * associated with this mount point, start over.
542 */
543 if (vp->v_mount != mp)
544 goto loop;
d85a9d1b
KM
545 if (VOP_ISLOCKED(vp))
546 continue;
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 */
0eb6f54a 729getmdev(devvpp, fname, ndp, p)
7188ac27 730 struct vnode **devvpp;
715baff1 731 caddr_t fname;
7188ac27 732 register struct nameidata *ndp;
0eb6f54a 733 struct proc *p;
71e4e98b 734{
7188ac27
KM
735 register struct vnode *vp;
736 int error;
71e4e98b 737
6c845185 738 ndp->ni_nameiop = LOOKUP | FOLLOW;
715baff1
KM
739 ndp->ni_segflg = UIO_USERSPACE;
740 ndp->ni_dirp = fname;
0eb6f54a 741 if (error = namei(ndp, p))
7188ac27 742 return (error);
7188ac27
KM
743 vp = ndp->ni_vp;
744 if (vp->v_type != VBLK) {
6c845185 745 vrele(vp);
71e4e98b 746 return (ENOTBLK);
e95cdf5c 747 }
e547d435 748 if (major(vp->v_rdev) >= nblkdev) {
6c845185 749 vrele(vp);
71e4e98b 750 return (ENXIO);
e547d435 751 }
7188ac27 752 *devvpp = vp;
71e4e98b
SL
753 return (0);
754}