v_blockh is split into clean and dirty lists; count active I/O's
[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 *
7188ac27
KM
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 *
0668615d 17 * @(#)ffs_vfsops.c 7.32 (Berkeley) %G%
da7c5cc6 18 */
71e4e98b 19
94368568
JB
20#include "param.h"
21#include "systm.h"
7188ac27
KM
22#include "time.h"
23#include "kernel.h"
24#include "namei.h"
25#include "vnode.h"
94368568 26#include "mount.h"
7188ac27 27#include "buf.h"
a937f856 28#include "ucred.h"
94368568 29#include "file.h"
ec67a3ce 30#include "disklabel.h"
7188ac27
KM
31#include "ioctl.h"
32#include "errno.h"
4def0c5e 33#include "malloc.h"
7188ac27
KM
34#include "../ufs/fs.h"
35#include "../ufs/ufsmount.h"
36#include "../ufs/inode.h"
609e7cfa
MK
37#include "ioctl.h"
38#include "disklabel.h"
39#include "stat.h"
71e4e98b 40
7188ac27
KM
41/*
42 * ufs vfs operations.
43 */
44int ufs_mount();
5bf9d21f 45int ufs_start();
7188ac27
KM
46int ufs_unmount();
47int ufs_root();
48int ufs_statfs();
49int ufs_sync();
50int ufs_fhtovp();
51int ufs_vptofh();
de0d8667 52int ufs_init();
7188ac27
KM
53
54struct vfsops ufs_vfsops = {
55 ufs_mount,
5bf9d21f 56 ufs_start,
7188ac27
KM
57 ufs_unmount,
58 ufs_root,
59 ufs_statfs,
60 ufs_sync,
61 ufs_fhtovp,
de0d8667
KM
62 ufs_vptofh,
63 ufs_init
7188ac27
KM
64};
65
66/*
67 * ufs mount table.
68 */
69struct ufsmount mounttab[NMOUNT];
70
71/*
d48157d5 72 * Called by vfs_mountroot when ufs is going to be mounted as root.
7188ac27 73 *
d48157d5 74 * Name is updated by mount(8) after booting.
7188ac27 75 */
8a4911ca 76#define ROOTNAME "root_device"
7188ac27
KM
77
78ufs_mountroot()
71e4e98b 79{
7188ac27
KM
80 register struct mount *mp;
81 extern struct vnode *rootvp;
82 struct ufsmount *ump;
71e4e98b 83 register struct fs *fs;
7188ac27
KM
84 u_int size;
85 int error;
71e4e98b 86
7188ac27
KM
87 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
88 M_MOUNT, M_WAITOK);
89 mp->m_op = &ufs_vfsops;
5d96a9ad 90 mp->m_flag = M_RDONLY;
7188ac27 91 mp->m_exroot = 0;
31593ba7 92 mp->m_mounth = (struct vnode *)0;
7188ac27
KM
93 error = mountfs(rootvp, mp);
94 if (error) {
95 free((caddr_t)mp, M_MOUNT);
96 return (error);
71e4e98b 97 }
d48157d5 98 if (error = vfs_lock(mp)) {
7188ac27
KM
99 (void)ufs_unmount(mp, 0);
100 free((caddr_t)mp, M_MOUNT);
101 return (error);
6d07f4cd 102 }
d48157d5
KM
103 rootfs = mp;
104 mp->m_next = mp;
105 mp->m_prev = mp;
106 mp->m_vnodecovered = (struct vnode *)0;
7188ac27
KM
107 ump = VFSTOUFS(mp);
108 fs = ump->um_fs;
109 fs->fs_fsmnt[0] = '/';
110 bzero(fs->fs_fsmnt + 1, sizeof(fs->fs_fsmnt) - 1);
111 (void) copystr(ROOTNAME, ump->um_mntname, MNAMELEN - 1, &size);
112 bzero(ump->um_mntname + size, MNAMELEN - size);
113 vfs_unlock(mp);
114 inittodr(fs->fs_time);
115 return (0);
116}
117
118/*
119 * VFS Operations.
120 *
121 * mount system call
122 */
123ufs_mount(mp, path, data, ndp)
124 struct mount *mp;
125 char *path;
126 caddr_t data;
127 struct nameidata *ndp;
128{
129 struct vnode *devvp;
130 struct ufs_args args;
131 struct ufsmount *ump;
132 register struct fs *fs;
133 u_int size;
134 int error;
135
136 if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
137 return (error);
138 if ((error = getmdev(&devvp, args.fspec, ndp)) != 0)
139 return (error);
d48157d5
KM
140 if ((mp->m_flag & M_UPDATE) == 0) {
141 error = mountfs(devvp, mp);
142 } else {
143 ump = VFSTOUFS(mp);
144 fs = ump->um_fs;
145 if (fs->fs_ronly && (mp->m_flag & M_RDONLY) == 0)
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 */
151 if (devvp != ump->um_devvp)
152 error = EINVAL; /* needs translation */
153 }
7188ac27
KM
154 if (error) {
155 vrele(devvp);
156 return (error);
27d00e76 157 }
7188ac27
KM
158 ump = VFSTOUFS(mp);
159 fs = ump->um_fs;
160 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
161 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
162 (void) copyinstr(args.fspec, ump->um_mntname, MNAMELEN - 1, &size);
163 bzero(ump->um_mntname + size, MNAMELEN - size);
164 return (0);
71e4e98b
SL
165}
166
7188ac27
KM
167/*
168 * Common code for mount and mountroot
169 */
170mountfs(devvp, mp)
171 struct vnode *devvp;
172 struct mount *mp;
71e4e98b 173{
7188ac27
KM
174 register struct ufsmount *ump;
175 struct ufsmount *fmp = NULL;
176 struct buf *bp = NULL;
71e4e98b 177 register struct fs *fs;
7188ac27 178 dev_t dev = devvp->v_rdev;
ec67a3ce
MK
179 struct partinfo dpart;
180 int havepart = 0, blks;
27d00e76 181 caddr_t base, space;
7188ac27
KM
182 int havepart = 0, blks;
183 int error, i, size;
6d07f4cd 184 int needclose = 0;
7188ac27 185 int ronly = (mp->m_flag & M_RDONLY) != 0;
71e4e98b 186
7188ac27
KM
187 for (ump = &mounttab[0]; ump < &mounttab[NMOUNT]; ump++) {
188 if (ump->um_fs == NULL) {
27d00e76 189 if (fmp == NULL)
7188ac27
KM
190 fmp = ump;
191 } else if (dev == ump->um_dev) {
192 return (EBUSY); /* needs translation */
27d00e76
KM
193 }
194 }
ec67a3ce
MK
195 (*bdevsw[major(dev)].d_open)(dev, ronly ? FREAD : FREAD|FWRITE,
196 S_IFBLK);
27d00e76 197 if (error) {
7188ac27
KM
198 ump->um_fs = NULL;
199 return (error);
27d00e76 200 }
6d07f4cd 201 needclose = 1;
a937f856 202 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED) != 0)
609e7cfa 203 size = DEV_BSIZE;
7188ac27 204 else {
ec67a3ce
MK
205 havepart = 1;
206 size = dpart.disklab->d_secsize;
7188ac27 207 }
a937f856 208 if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) {
7188ac27 209 ump->um_fs = NULL;
71e4e98b 210 goto out;
27d00e76 211 }
e018935f 212 fs = bp->b_un.b_fs;
7188ac27
KM
213 ump->um_fs = NULL;
214 error = EINVAL; /* XXX also needs translation */
1c281610
MK
215 goto out;
216 }
7188ac27 217 ump->um_fs = (struct fs *)malloc((u_long)fs->fs_sbsize, M_SUPERBLK,
5adcb337 218 M_WAITOK);
7188ac27 219 bcopy((caddr_t)bp->b_un.b_addr, (caddr_t)ump->um_fs,
71e4e98b 220 (u_int)fs->fs_sbsize);
5d96a9ad
KM
221 if (fs->fs_sbsize < SBSIZE)
222 bp->b_flags |= B_INVAL;
e018935f
MK
223 brelse(bp);
224 bp = NULL;
7188ac27
KM
225 fs = ump->um_fs;
226 fs->fs_ronly = ronly;
71e4e98b
SL
227 if (ronly == 0)
228 fs->fs_fmod = 1;
609e7cfa
MK
229 if (havepart) {
230 dpart.part->p_fstype = FS_BSDFFS;
231 dpart.part->p_fsize = fs->fs_fsize;
232 dpart.part->p_frag = fs->fs_frag;
42ff4c2e 233 dpart.part->p_cpg = fs->fs_cpg;
609e7cfa 234 }
ec67a3ce
MK
235#ifdef SECSIZE
236 /*
237 * If we have a disk label, force per-partition
238 * filesystem information to be correct
239 * and set correct current fsbtodb shift.
240 */
241#endif SECSIZE
242 if (havepart) {
243 dpart.part->p_fstype = FS_BSDFFS;
244 dpart.part->p_fsize = fs->fs_fsize;
245 dpart.part->p_frag = fs->fs_frag;
246#ifdef SECSIZE
247#ifdef tahoe
248 /*
249 * Save the original fsbtodb shift to restore on updates.
250 * (Console doesn't understand fsbtodb changes.)
251 */
252 fs->fs_sparecon[0] = fs->fs_fsbtodb;
253#endif
254 i = fs->fs_fsize / size;
255 for (fs->fs_fsbtodb = 0; i > 1; i >>= 1)
256 fs->fs_fsbtodb++;
257#endif SECSIZE
258 fs->fs_dbsize = size;
259 }
71e4e98b 260 blks = howmany(fs->fs_cssize, fs->fs_fsize);
5adcb337
KM
261 base = space = (caddr_t)malloc((u_long)fs->fs_cssize, M_SUPERBLK,
262 M_WAITOK);
71e4e98b
SL
263 for (i = 0; i < blks; i += fs->fs_frag) {
264 size = fs->fs_bsize;
265 if (i + fs->fs_frag > blks)
266 size = (blks - i) * fs->fs_fsize;
ec67a3ce
MK
267#ifdef SECSIZE
268 tp = bread(dev, fsbtodb(fs, fs->fs_csaddr + i), size,
269 fs->fs_dbsize);
270#else SECSIZE
a937f856
KM
271 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
272 NOCRED, &bp);
7188ac27 273 if (error) {
5adcb337 274 free((caddr_t)base, M_SUPERBLK);
71e4e98b
SL
275 goto out;
276 }
e018935f 277 bcopy((caddr_t)bp->b_un.b_addr, space, (u_int)size);
60f9c9e3 278 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
71e4e98b 279 space += size;
e018935f
MK
280 brelse(bp);
281 bp = NULL;
71e4e98b 282 }
7188ac27
KM
283 mp->m_data = (qaddr_t)ump;
284 mp->m_bsize = fs->fs_bsize;
285 mp->m_fsize = fs->fs_fsize;
286 mp->m_fsid.val[0] = (long)dev;
287 mp->m_fsid.val[1] = MOUNT_UFS;
288 ump->um_mountp = mp;
289 ump->um_dev = dev;
290 ump->um_devvp = devvp;
291 ump->um_qinod = NULL;
292
94354803
KM
293 /* Sanity checks for old file systems. XXX */
294 fs->fs_npsect = MAX(fs->fs_npsect, fs->fs_nsect); /* XXX */
295 fs->fs_interleave = MAX(fs->fs_interleave, 1); /* XXX */
2af59000
KM
296 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
297 fs->fs_nrpos = 8; /* XXX */
609e7cfa 298
7188ac27 299 return (0);
71e4e98b 300out:
609e7cfa 301 if (needclose)
a937f856 302 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED);
7188ac27
KM
303 if (ump->um_fs) {
304 free((caddr_t)ump->um_fs, M_SUPERBLK);
305 ump->um_fs = NULL;
27d00e76 306 }
e018935f
MK
307 if (bp)
308 brelse(bp);
7188ac27 309 return (error);
71e4e98b
SL
310}
311
5bf9d21f
KM
312/*
313 * Make a filesystem operational.
314 * Nothing to do at the moment.
315 */
31593ba7 316/* ARGSUSED */
5bf9d21f
KM
317ufs_start(mp, flags)
318 struct mount *mp;
319 int flags;
320{
321
322 return (0);
323}
71e4e98b 324
7188ac27
KM
325/*
326 * unmount system call
327 */
328ufs_unmount(mp, flags)
329 struct mount *mp;
330 int flags;
71e4e98b 331{
7188ac27 332 register struct ufsmount *ump;
71e4e98b 333 register struct fs *fs;
7188ac27 334 int error, ronly;
71e4e98b 335
7188ac27
KM
336 if (flags & MNT_FORCE)
337 return (EINVAL);
5d96a9ad
KM
338 mntflushbuf(mp, 0);
339 if (mntinvalbuf(mp))
340 return (EBUSY);
7188ac27 341 ump = VFSTOUFS(mp);
2309ea70 342 if (error = vflush(mp, ITOV(ump->um_qinod), flags))
ec67a3ce 343 return (error);
71e4e98b 344#ifdef QUOTA
31593ba7 345 (void) closedq(ump);
71e4e98b 346 /*
ec67a3ce 347 * A drag, but it would be ugly to cheat, & this doesn't happen often.
71e4e98b 348 */
2309ea70
KM
349 if (vflush(mp, (struct vnode *)NULL, MNT_NOFORCE))
350 panic("ufs_unmount: quota");
71e4e98b 351#endif
7188ac27
KM
352 fs = ump->um_fs;
353 ronly = !fs->fs_ronly;
4def0c5e 354 free((caddr_t)fs->fs_csp[0], M_SUPERBLK);
ec67a3ce
MK
355 error = closei(dev, IFBLK, fs->fs_ronly? FREAD : FREAD|FWRITE);
356 irele(ip);
357 return (error);
71e4e98b
SL
358}
359
7188ac27
KM
360/*
361 * Return root of a filesystem
362 */
363ufs_root(mp, vpp)
364 struct mount *mp;
365 struct vnode **vpp;
366{
31593ba7
KM
367 register struct inode *ip;
368 struct inode *nip;
369 struct vnode tvp;
7188ac27
KM
370 int error;
371
31593ba7
KM
372 tvp.v_mount = mp;
373 ip = VTOI(&tvp);
374 ip->i_vnode = &tvp;
375 ip->i_dev = VFSTOUFS(mp)->um_dev;
376 error = iget(ip, (ino_t)ROOTINO, &nip);
7188ac27
KM
377 if (error)
378 return (error);
31593ba7 379 *vpp = ITOV(nip);
7188ac27
KM
380 return (0);
381}
382
383/*
384 * Get file system statistics.
385 */
386ufs_statfs(mp, sbp)
387 struct mount *mp;
388 register struct statfs *sbp;
389{
390 register struct ufsmount *ump;
391 register struct fs *fs;
392
393 ump = VFSTOUFS(mp);
394 fs = ump->um_fs;
395 if (fs->fs_magic != FS_MAGIC)
396 panic("ufs_statfs");
397 sbp->f_type = MOUNT_UFS;
7188ac27
KM
398 sbp->f_fsize = fs->fs_fsize;
399 sbp->f_bsize = fs->fs_bsize;
400 sbp->f_blocks = fs->fs_dsize;
401 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
402 fs->fs_cstotal.cs_nffree;
403 sbp->f_bavail = (fs->fs_dsize * (100 - fs->fs_minfree) / 100) -
404 (fs->fs_dsize - sbp->f_bfree);
5c41c19e 405 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
7188ac27 406 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
7188ac27
KM
407 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
408 bcopy((caddr_t)ump->um_mntname, (caddr_t)&sbp->f_mntfromname[0],
409 MNAMELEN);
410 return (0);
411}
412
413int syncprt = 0;
414
415/*
416 * Go through the disk queues to initiate sandbagged IO;
417 * go through the inodes to write those that have been modified;
418 * initiate the writing of the super block if it has been modified.
419 */
420ufs_sync(mp, waitfor)
71e4e98b 421 struct mount *mp;
7188ac27 422 int waitfor;
71e4e98b 423{
31593ba7 424 register struct vnode *vp;
7188ac27
KM
425 register struct inode *ip;
426 register struct ufsmount *ump = VFSTOUFS(mp);
427 register struct fs *fs;
812b91f3 428 int error, allerror = 0;
7188ac27
KM
429 static int updlock = 0;
430
431 if (syncprt)
432 bufstats();
433 if (updlock)
434 return (EBUSY);
435 fs = ump->um_fs;
436 if (fs == (struct fs *)1)
437 return (0);
438 updlock++;
439 /*
440 * Write back modified superblock.
441 * Consistency check that the superblock
442 * is still in the buffer cache.
443 */
444 if (fs->fs_fmod != 0) {
445 if (fs->fs_ronly != 0) { /* XXX */
446 printf("fs = %s\n", fs->fs_fsmnt);
447 panic("update: rofs mod");
448 }
449 fs->fs_fmod = 0;
450 fs->fs_time = time.tv_sec;
451 error = sbupdate(ump, waitfor);
452 }
453 /*
454 * Write back each (modified) inode.
455 */
31593ba7
KM
456 for (vp = mp->m_mounth; vp; vp = vp->v_mountf) {
457 ip = VTOI(vp);
0668615d 458 if ((ip->i_flag & ILOCKED) != 0 || vp->v_usecount == 0 ||
7188ac27
KM
459 (ip->i_flag & (IMOD|IACC|IUPD|ICHG)) == 0)
460 continue;
31593ba7 461 VREF(vp);
812b91f3 462 VOP_LOCK(vp);
5d96a9ad 463 if (error = VOP_FSYNC(vp, 0, NOCRED, MNT_NOWAIT))
812b91f3
KM
464 allerror = error;
465 vput(vp);
7188ac27
KM
466 }
467 updlock = 0;
468 /*
5d96a9ad 469 * Force stale file system control information to be flushed.
7188ac27 470 */
5d96a9ad 471 vflushbuf(ump->um_devvp, waitfor == MNT_WAIT ? B_SYNC : 0);
812b91f3 472 return (allerror);
7188ac27
KM
473}
474
475/*
476 * Write a superblock and associated information back to disk.
477 */
478sbupdate(mp, waitfor)
479 struct ufsmount *mp;
480 int waitfor;
481{
482 register struct fs *fs = mp->um_fs;
71e4e98b
SL
483 register struct buf *bp;
484 int blks;
485 caddr_t space;
7188ac27 486 int i, size, error = 0;
71e4e98b 487
ec67a3ce
MK
488#ifdef SECSIZE
489 bp = getblk(mp->m_dev, (daddr_t)fsbtodb(fs, SBOFF / fs->fs_fsize),
490 (int)fs->fs_sbsize, fs->fs_dbsize);
491#else SECSIZE
7188ac27 492 bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize);
ec67a3ce 493#endif SECSIZE
71e4e98b 494 bcopy((caddr_t)fs, bp->b_un.b_addr, (u_int)fs->fs_sbsize);
2af59000
KM
495 /* Restore compatibility to old file systems. XXX */
496 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
497 bp->b_un.b_fs->fs_nrpos = -1; /* XXX */
ec67a3ce
MK
498#ifdef SECSIZE
499#ifdef tahoe
500 /* restore standard fsbtodb shift */
501 bp->b_un.b_fs->fs_fsbtodb = fs->fs_sparecon[0];
502 bp->b_un.b_fs->fs_sparecon[0] = 0;
503#endif
504#endif SECSIZE
7188ac27
KM
505 if (waitfor == MNT_WAIT)
506 error = bwrite(bp);
507 else
508 bawrite(bp);
71e4e98b
SL
509 blks = howmany(fs->fs_cssize, fs->fs_fsize);
510 space = (caddr_t)fs->fs_csp[0];
511 for (i = 0; i < blks; i += fs->fs_frag) {
512 size = fs->fs_bsize;
513 if (i + fs->fs_frag > blks)
514 size = (blks - i) * fs->fs_fsize;
ec67a3ce
MK
515#ifdef SECSIZE
516 bp = getblk(mp->m_dev, fsbtodb(fs, fs->fs_csaddr + i), size,
517 fs->fs_dbsize);
518#else SECSIZE
7188ac27 519 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), size);
ec67a3ce 520#endif SECSIZE
71e4e98b
SL
521 bcopy(space, bp->b_un.b_addr, (u_int)size);
522 space += size;
7188ac27
KM
523 if (waitfor == MNT_WAIT)
524 error = bwrite(bp);
525 else
526 bawrite(bp);
71e4e98b 527 }
7188ac27 528 return (error);
71e4e98b
SL
529}
530
531/*
7188ac27
KM
532 * Print out statistics on the current allocation of the buffer pool.
533 * Can be enabled to print out on every ``sync'' by setting "syncprt"
534 * above.
535 */
536bufstats()
537{
538 int s, i, j, count;
539 register struct buf *bp, *dp;
540 int counts[MAXBSIZE/CLBYTES+1];
541 static char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE", "EMPTY" };
542
543 for (bp = bfreelist, i = 0; bp < &bfreelist[BQUEUES]; bp++, i++) {
544 count = 0;
545 for (j = 0; j <= MAXBSIZE/CLBYTES; j++)
546 counts[j] = 0;
547 s = splbio();
548 for (dp = bp->av_forw; dp != bp; dp = dp->av_forw) {
549 counts[dp->b_bufsize/CLBYTES]++;
550 count++;
551 }
552 splx(s);
553 printf("%s: total-%d", bname[i], count);
554 for (j = 0; j <= MAXBSIZE/CLBYTES; j++)
555 if (counts[j] != 0)
556 printf(", %d-%d", j * CLBYTES, counts[j]);
557 printf("\n");
558 }
559}
560
561/*
562 * File handle to vnode
113c35f2
KM
563 *
564 * Have to be really careful about stale file handles:
565 * - check that the inode number is in range
566 * - call iget() to get the locked inode
567 * - check for an unallocated inode (i_mode == 0)
568 * - check that the generation number matches
7188ac27
KM
569 */
570ufs_fhtovp(mp, fhp, vpp)
113c35f2 571 register struct mount *mp;
7188ac27
KM
572 struct fid *fhp;
573 struct vnode **vpp;
574{
575 register struct ufid *ufhp;
113c35f2 576 register struct fs *fs;
31593ba7
KM
577 register struct inode *ip;
578 struct inode *nip;
579 struct vnode tvp;
7188ac27
KM
580 int error;
581
582 ufhp = (struct ufid *)fhp;
113c35f2
KM
583 fs = VFSTOUFS(mp)->um_fs;
584 if (ufhp->ufid_ino < ROOTINO ||
585 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg) {
586 *vpp = (struct vnode *)0;
587 return (EINVAL);
588 }
31593ba7
KM
589 tvp.v_mount = mp;
590 ip = VTOI(&tvp);
591 ip->i_vnode = &tvp;
592 ip->i_dev = VFSTOUFS(mp)->um_dev;
593 if (error = iget(ip, ufhp->ufid_ino, &nip)) {
113c35f2 594 *vpp = (struct vnode *)0;
7188ac27
KM
595 return (error);
596 }
31593ba7 597 ip = nip;
113c35f2
KM
598 if (ip->i_mode == 0) {
599 iput(ip);
600 *vpp = (struct vnode *)0;
601 return (EINVAL);
602 }
7188ac27
KM
603 if (ip->i_gen != ufhp->ufid_gen) {
604 iput(ip);
113c35f2 605 *vpp = (struct vnode *)0;
7188ac27
KM
606 return (EINVAL);
607 }
608 *vpp = ITOV(ip);
609 return (0);
610}
611
612/*
29e77c27 613 * Vnode pointer to File handle
7188ac27
KM
614 */
615/* ARGSUSED */
758e3529
KM
616ufs_vptofh(vp, fhp)
617 struct vnode *vp;
7188ac27 618 struct fid *fhp;
7188ac27 619{
758e3529
KM
620 register struct inode *ip = VTOI(vp);
621 register struct ufid *ufhp;
7188ac27 622
758e3529
KM
623 ufhp = (struct ufid *)fhp;
624 ufhp->ufid_len = sizeof(struct ufid);
625 ufhp->ufid_ino = ip->i_number;
626 ufhp->ufid_gen = ip->i_gen;
627 return (0);
7188ac27
KM
628}
629
630/*
631 * Common code for mount and quota.
71e4e98b
SL
632 * Check that the user's argument is a reasonable
633 * thing on which to mount, and return the device number if so.
634 */
7188ac27
KM
635getmdev(devvpp, fname, ndp)
636 struct vnode **devvpp;
715baff1 637 caddr_t fname;
7188ac27 638 register struct nameidata *ndp;
71e4e98b 639{
7188ac27
KM
640 register struct vnode *vp;
641 int error;
71e4e98b 642
7188ac27 643 ndp->ni_nameiop = LOOKUP | LOCKLEAF | FOLLOW;
715baff1
KM
644 ndp->ni_segflg = UIO_USERSPACE;
645 ndp->ni_dirp = fname;
7188ac27
KM
646 if (error = namei(ndp)) {
647 if (error == ENOENT)
648 return (ENODEV); /* needs translation */
649 return (error);
6d07f4cd 650 }
7188ac27
KM
651 vp = ndp->ni_vp;
652 if (vp->v_type != VBLK) {
653 vput(vp);
71e4e98b 654 return (ENOTBLK);
e95cdf5c 655 }
e547d435
KM
656 if (major(vp->v_rdev) >= nblkdev) {
657 vput(vp);
71e4e98b 658 return (ENXIO);
e547d435 659 }
7188ac27
KM
660 iunlock(VTOI(vp));
661 *devvpp = vp;
71e4e98b
SL
662 return (0);
663}