new macro to calculate multiplication by fs_bsize
[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 *
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 *
25b5cf58 17 * @(#)ufs_vfsops.c 7.33 (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;
25b5cf58 428 struct vnode *nvp;
812b91f3 429 int error, allerror = 0;
7188ac27
KM
430 static int updlock = 0;
431
432 if (syncprt)
433 bufstats();
434 if (updlock)
435 return (EBUSY);
436 fs = ump->um_fs;
437 if (fs == (struct fs *)1)
438 return (0);
439 updlock++;
440 /*
441 * Write back modified superblock.
442 * Consistency check that the superblock
443 * is still in the buffer cache.
444 */
445 if (fs->fs_fmod != 0) {
446 if (fs->fs_ronly != 0) { /* XXX */
447 printf("fs = %s\n", fs->fs_fsmnt);
448 panic("update: rofs mod");
449 }
450 fs->fs_fmod = 0;
451 fs->fs_time = time.tv_sec;
452 error = sbupdate(ump, waitfor);
453 }
454 /*
455 * Write back each (modified) inode.
456 */
25b5cf58
KM
457loop:
458 for (vp = mp->m_mounth; vp; vp = nvp) {
459 nvp = vp->v_mountf;
31593ba7 460 ip = VTOI(vp);
25b5cf58
KM
461 if ((ip->i_flag & (IMOD|IACC|IUPD|ICHG)) == 0 &&
462 vp->v_dirtyblkhd == NULL)
7188ac27 463 continue;
25b5cf58
KM
464 if (vget(vp))
465 goto loop;
466 if (vp->v_dirtyblkhd)
467 vflushbuf(vp, 0);
468 if ((ip->i_flag & (IMOD|IACC|IUPD|ICHG)) &&
469 (error = iupdat(ip, &time, &time, 0)))
812b91f3
KM
470 allerror = error;
471 vput(vp);
7188ac27
KM
472 }
473 updlock = 0;
474 /*
5d96a9ad 475 * Force stale file system control information to be flushed.
7188ac27 476 */
5d96a9ad 477 vflushbuf(ump->um_devvp, waitfor == MNT_WAIT ? B_SYNC : 0);
812b91f3 478 return (allerror);
7188ac27
KM
479}
480
481/*
482 * Write a superblock and associated information back to disk.
483 */
484sbupdate(mp, waitfor)
485 struct ufsmount *mp;
486 int waitfor;
487{
488 register struct fs *fs = mp->um_fs;
71e4e98b
SL
489 register struct buf *bp;
490 int blks;
491 caddr_t space;
7188ac27 492 int i, size, error = 0;
71e4e98b 493
ec67a3ce
MK
494#ifdef SECSIZE
495 bp = getblk(mp->m_dev, (daddr_t)fsbtodb(fs, SBOFF / fs->fs_fsize),
496 (int)fs->fs_sbsize, fs->fs_dbsize);
497#else SECSIZE
7188ac27 498 bp = getblk(mp->um_devvp, SBLOCK, (int)fs->fs_sbsize);
ec67a3ce 499#endif SECSIZE
71e4e98b 500 bcopy((caddr_t)fs, bp->b_un.b_addr, (u_int)fs->fs_sbsize);
2af59000
KM
501 /* Restore compatibility to old file systems. XXX */
502 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
503 bp->b_un.b_fs->fs_nrpos = -1; /* XXX */
ec67a3ce
MK
504#ifdef SECSIZE
505#ifdef tahoe
506 /* restore standard fsbtodb shift */
507 bp->b_un.b_fs->fs_fsbtodb = fs->fs_sparecon[0];
508 bp->b_un.b_fs->fs_sparecon[0] = 0;
509#endif
510#endif SECSIZE
7188ac27
KM
511 if (waitfor == MNT_WAIT)
512 error = bwrite(bp);
513 else
514 bawrite(bp);
71e4e98b
SL
515 blks = howmany(fs->fs_cssize, fs->fs_fsize);
516 space = (caddr_t)fs->fs_csp[0];
517 for (i = 0; i < blks; i += fs->fs_frag) {
518 size = fs->fs_bsize;
519 if (i + fs->fs_frag > blks)
520 size = (blks - i) * fs->fs_fsize;
ec67a3ce
MK
521#ifdef SECSIZE
522 bp = getblk(mp->m_dev, fsbtodb(fs, fs->fs_csaddr + i), size,
523 fs->fs_dbsize);
524#else SECSIZE
7188ac27 525 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i), size);
ec67a3ce 526#endif SECSIZE
71e4e98b
SL
527 bcopy(space, bp->b_un.b_addr, (u_int)size);
528 space += size;
7188ac27
KM
529 if (waitfor == MNT_WAIT)
530 error = bwrite(bp);
531 else
532 bawrite(bp);
71e4e98b 533 }
7188ac27 534 return (error);
71e4e98b
SL
535}
536
537/*
7188ac27
KM
538 * Print out statistics on the current allocation of the buffer pool.
539 * Can be enabled to print out on every ``sync'' by setting "syncprt"
540 * above.
541 */
542bufstats()
543{
544 int s, i, j, count;
545 register struct buf *bp, *dp;
546 int counts[MAXBSIZE/CLBYTES+1];
547 static char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE", "EMPTY" };
548
549 for (bp = bfreelist, i = 0; bp < &bfreelist[BQUEUES]; bp++, i++) {
550 count = 0;
551 for (j = 0; j <= MAXBSIZE/CLBYTES; j++)
552 counts[j] = 0;
553 s = splbio();
554 for (dp = bp->av_forw; dp != bp; dp = dp->av_forw) {
555 counts[dp->b_bufsize/CLBYTES]++;
556 count++;
557 }
558 splx(s);
559 printf("%s: total-%d", bname[i], count);
560 for (j = 0; j <= MAXBSIZE/CLBYTES; j++)
561 if (counts[j] != 0)
562 printf(", %d-%d", j * CLBYTES, counts[j]);
563 printf("\n");
564 }
565}
566
567/*
568 * File handle to vnode
113c35f2
KM
569 *
570 * Have to be really careful about stale file handles:
571 * - check that the inode number is in range
572 * - call iget() to get the locked inode
573 * - check for an unallocated inode (i_mode == 0)
574 * - check that the generation number matches
7188ac27
KM
575 */
576ufs_fhtovp(mp, fhp, vpp)
113c35f2 577 register struct mount *mp;
7188ac27
KM
578 struct fid *fhp;
579 struct vnode **vpp;
580{
581 register struct ufid *ufhp;
113c35f2 582 register struct fs *fs;
31593ba7
KM
583 register struct inode *ip;
584 struct inode *nip;
585 struct vnode tvp;
7188ac27
KM
586 int error;
587
588 ufhp = (struct ufid *)fhp;
113c35f2
KM
589 fs = VFSTOUFS(mp)->um_fs;
590 if (ufhp->ufid_ino < ROOTINO ||
591 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg) {
592 *vpp = (struct vnode *)0;
593 return (EINVAL);
594 }
31593ba7
KM
595 tvp.v_mount = mp;
596 ip = VTOI(&tvp);
597 ip->i_vnode = &tvp;
598 ip->i_dev = VFSTOUFS(mp)->um_dev;
599 if (error = iget(ip, ufhp->ufid_ino, &nip)) {
113c35f2 600 *vpp = (struct vnode *)0;
7188ac27
KM
601 return (error);
602 }
31593ba7 603 ip = nip;
113c35f2
KM
604 if (ip->i_mode == 0) {
605 iput(ip);
606 *vpp = (struct vnode *)0;
607 return (EINVAL);
608 }
7188ac27
KM
609 if (ip->i_gen != ufhp->ufid_gen) {
610 iput(ip);
113c35f2 611 *vpp = (struct vnode *)0;
7188ac27
KM
612 return (EINVAL);
613 }
614 *vpp = ITOV(ip);
615 return (0);
616}
617
618/*
29e77c27 619 * Vnode pointer to File handle
7188ac27
KM
620 */
621/* ARGSUSED */
758e3529
KM
622ufs_vptofh(vp, fhp)
623 struct vnode *vp;
7188ac27 624 struct fid *fhp;
7188ac27 625{
758e3529
KM
626 register struct inode *ip = VTOI(vp);
627 register struct ufid *ufhp;
7188ac27 628
758e3529
KM
629 ufhp = (struct ufid *)fhp;
630 ufhp->ufid_len = sizeof(struct ufid);
631 ufhp->ufid_ino = ip->i_number;
632 ufhp->ufid_gen = ip->i_gen;
633 return (0);
7188ac27
KM
634}
635
636/*
637 * Common code for mount and quota.
71e4e98b
SL
638 * Check that the user's argument is a reasonable
639 * thing on which to mount, and return the device number if so.
640 */
7188ac27
KM
641getmdev(devvpp, fname, ndp)
642 struct vnode **devvpp;
715baff1 643 caddr_t fname;
7188ac27 644 register struct nameidata *ndp;
71e4e98b 645{
7188ac27
KM
646 register struct vnode *vp;
647 int error;
71e4e98b 648
7188ac27 649 ndp->ni_nameiop = LOOKUP | LOCKLEAF | FOLLOW;
715baff1
KM
650 ndp->ni_segflg = UIO_USERSPACE;
651 ndp->ni_dirp = fname;
7188ac27
KM
652 if (error = namei(ndp)) {
653 if (error == ENOENT)
654 return (ENODEV); /* needs translation */
655 return (error);
6d07f4cd 656 }
7188ac27
KM
657 vp = ndp->ni_vp;
658 if (vp->v_type != VBLK) {
659 vput(vp);
71e4e98b 660 return (ENOTBLK);
e95cdf5c 661 }
e547d435
KM
662 if (major(vp->v_rdev) >= nblkdev) {
663 vput(vp);
71e4e98b 664 return (ENXIO);
e547d435 665 }
7188ac27
KM
666 iunlock(VTOI(vp));
667 *devvpp = vp;
71e4e98b
SL
668 return (0);
669}