lfs_vfsops.c revision 1.28 1 /* $NetBSD: lfs_vfsops.c,v 1.28 1999/03/25 21:39:19 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*-
39 * Copyright (c) 1989, 1991, 1993, 1994
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_vfsops.c 8.20 (Berkeley) 6/10/95
71 */
72
73 #if defined(_KERNEL) && !defined(_LKM)
74 #include "opt_quota.h"
75 #endif
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/namei.h>
80 #include <sys/proc.h>
81 #include <sys/kernel.h>
82 #include <sys/vnode.h>
83 #include <sys/mount.h>
84 #include <sys/buf.h>
85 #include <sys/mbuf.h>
86 #include <sys/file.h>
87 #include <sys/disklabel.h>
88 #include <sys/ioctl.h>
89 #include <sys/errno.h>
90 #include <sys/malloc.h>
91 #include <sys/pool.h>
92 #include <sys/socket.h>
93 #include <vm/vm.h>
94 #include <sys/sysctl.h>
95
96 #include <miscfs/specfs/specdev.h>
97
98 #include <ufs/ufs/quota.h>
99 #include <ufs/ufs/inode.h>
100 #include <ufs/ufs/ufsmount.h>
101 #include <ufs/ufs/ufs_extern.h>
102
103 #include <ufs/lfs/lfs.h>
104 #include <ufs/lfs/lfs_extern.h>
105
106 int lfs_mountfs __P((struct vnode *, struct mount *, struct proc *));
107
108 extern struct vnodeopv_desc lfs_vnodeop_opv_desc;
109 extern struct vnodeopv_desc lfs_specop_opv_desc;
110 extern struct vnodeopv_desc lfs_fifoop_opv_desc;
111
112 struct vnodeopv_desc *lfs_vnodeopv_descs[] = {
113 &lfs_vnodeop_opv_desc,
114 &lfs_specop_opv_desc,
115 &lfs_fifoop_opv_desc,
116 NULL,
117 };
118
119 struct vfsops lfs_vfsops = {
120 MOUNT_LFS,
121 lfs_mount,
122 ufs_start,
123 lfs_unmount,
124 ufs_root,
125 ufs_quotactl,
126 lfs_statfs,
127 lfs_sync,
128 lfs_vget,
129 lfs_fhtovp,
130 lfs_vptofh,
131 lfs_init,
132 lfs_sysctl,
133 NULL,
134 ufs_check_export,
135 lfs_vnodeopv_descs,
136 };
137
138 struct pool lfs_inode_pool;
139
140 /*
141 * Initialize the filesystem, most work done by ufs_init.
142 */
143 void
144 lfs_init()
145 {
146 ufs_init();
147
148 /*
149 * XXX Same structure as FFS inodes? Should we share a common pool?
150 */
151 pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
152 "lfsinopl", 0, pool_page_alloc_nointr, pool_page_free_nointr,
153 M_LFSNODE);
154 }
155
156 /*
157 * Called by main() when ufs is going to be mounted as root.
158 */
159 int
160 lfs_mountroot()
161 {
162 extern struct vnode *rootvp;
163 struct mount *mp;
164 struct proc *p = curproc; /* XXX */
165 int error;
166
167 /*
168 * Get vnodes for swapdev and rootdev.
169 */
170 if ((error = bdevvp(rootdev, &rootvp))) {
171 printf("lfs_mountroot: can't setup bdevvp's");
172 return (error);
173 }
174 if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp)))
175 return (error);
176 if ((error = lfs_mountfs(rootvp, mp, p))) {
177 mp->mnt_op->vfs_refcount--;
178 vfs_unbusy(mp);
179 free(mp, M_MOUNT);
180 return (error);
181 }
182 simple_lock(&mountlist_slock);
183 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
184 simple_unlock(&mountlist_slock);
185 (void)lfs_statfs(mp, &mp->mnt_stat, p);
186 vfs_unbusy(mp);
187 return (0);
188 }
189
190 /*
191 * VFS Operations.
192 *
193 * mount system call
194 */
195 int
196 lfs_mount(mp, path, data, ndp, p)
197 register struct mount *mp;
198 const char *path;
199 void *data;
200 struct nameidata *ndp;
201 struct proc *p;
202 {
203 struct vnode *devvp;
204 struct ufs_args args;
205 struct ufsmount *ump = NULL;
206 register struct lfs *fs = NULL; /* LFS */
207 size_t size;
208 int error;
209 mode_t accessmode;
210
211 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
212 if (error)
213 return (error);
214
215 /* Until LFS can do NFS right. XXX */
216 if (args.export.ex_flags & MNT_EXPORTED)
217 return (EINVAL);
218
219 /*
220 * If updating, check whether changing from read-only to
221 * read/write; if there is no device name, that's all we do.
222 */
223 if (mp->mnt_flag & MNT_UPDATE) {
224 ump = VFSTOUFS(mp);
225 if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
226 /*
227 * If upgrade to read-write by non-root, then verify
228 * that user has necessary permissions on the device.
229 */
230 if (p->p_ucred->cr_uid != 0) {
231 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
232 error = VOP_ACCESS(ump->um_devvp, VREAD|VWRITE,
233 p->p_ucred, p);
234 VOP_UNLOCK(ump->um_devvp, 0);
235 if (error)
236 return (error);
237 }
238 fs->lfs_ronly = 0;
239 }
240 if (args.fspec == 0) {
241 /*
242 * Process export requests.
243 */
244 return (vfs_export(mp, &ump->um_export, &args.export));
245 }
246 }
247 /*
248 * Not an update, or updating the name: look up the name
249 * and verify that it refers to a sensible block device.
250 */
251 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
252 if ((error = namei(ndp)) != 0)
253 return (error);
254 devvp = ndp->ni_vp;
255 if (devvp->v_type != VBLK) {
256 vrele(devvp);
257 return (ENOTBLK);
258 }
259 if (major(devvp->v_rdev) >= nblkdev) {
260 vrele(devvp);
261 return (ENXIO);
262 }
263 /*
264 * If mount by non-root, then verify that user has necessary
265 * permissions on the device.
266 */
267 if (p->p_ucred->cr_uid != 0) {
268 accessmode = VREAD;
269 if ((mp->mnt_flag & MNT_RDONLY) == 0)
270 accessmode |= VWRITE;
271 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
272 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
273 if (error) {
274 vput(devvp);
275 return (error);
276 }
277 VOP_UNLOCK(devvp, 0);
278 }
279 if ((mp->mnt_flag & MNT_UPDATE) == 0)
280 error = lfs_mountfs(devvp, mp, p); /* LFS */
281 else {
282 if (devvp != ump->um_devvp)
283 error = EINVAL; /* needs translation */
284 else
285 vrele(devvp);
286 }
287 if (error) {
288 vrele(devvp);
289 return (error);
290 }
291 ump = VFSTOUFS(mp);
292 fs = ump->um_lfs; /* LFS */
293 (void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
294 bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
295 bcopy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
296 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
297 &size);
298 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
299 return (0);
300 }
301
302 /*
303 * Common code for mount and mountroot
304 * LFS specific
305 */
306 int
307 lfs_mountfs(devvp, mp, p)
308 register struct vnode *devvp;
309 struct mount *mp;
310 struct proc *p;
311 {
312 extern struct vnode *rootvp;
313 struct dlfs *dfs, *adfs;
314 register struct lfs *fs;
315 register struct ufsmount *ump;
316 struct vnode *vp;
317 struct buf *bp, *abp;
318 struct partinfo dpart;
319 dev_t dev;
320 int error, i, ronly, size;
321 struct ucred *cred;
322
323 cred = p ? p->p_ucred : NOCRED;
324 /*
325 * Disallow multiple mounts of the same device.
326 * Disallow mounting of a device that is currently in use
327 * (except for root, which might share swap device for miniroot).
328 * Flush out any old buffers remaining from a previous use.
329 */
330 if ((error = vfs_mountedon(devvp)) != 0)
331 return (error);
332 if (vcount(devvp) > 1 && devvp != rootvp)
333 return (EBUSY);
334 if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
335 return (error);
336
337 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
338 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
339 if (error)
340 return (error);
341
342 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
343 size = DEV_BSIZE;
344 else {
345 size = dpart.disklab->d_secsize;
346 }
347
348 /* Don't free random space on error. */
349 bp = NULL;
350 ump = NULL;
351
352 /* Read in the superblock. */
353 error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, cred, &bp);
354 if (error)
355 goto out;
356 dfs = (struct dlfs *)bp->b_data;
357
358 /*
359 * Check the second superblock to see which is newer; then mount
360 * using the older of the two. This is necessary to ensure that
361 * the filesystem is valid if it was not unmounted cleanly.
362 */
363 error = bread(devvp, dfs->dlfs_sboffs[1], LFS_SBPAD, cred, &abp);
364 if (error)
365 goto out;
366 adfs = (struct dlfs *)abp->b_data;
367 if(adfs->dlfs_tstamp < dfs->dlfs_tstamp) /* XXX KS - 1s resolution? */
368 dfs = adfs;
369
370 /* Check the basics. */
371 if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize > MAXBSIZE ||
372 dfs->dlfs_bsize < sizeof(struct dlfs)) {
373 error = EINVAL; /* XXX needs translation */
374 goto out;
375 }
376
377 /* Allocate the mount structure, copy the superblock into it. */
378 ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
379 fs = ump->um_lfs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK);
380 bcopy(bp->b_data, &(fs->lfs_dlfs), sizeof(struct dlfs));
381 if (sizeof(struct lfs) < LFS_SBPAD) /* XXX why? */
382 bp->b_flags |= B_INVAL;
383 brelse(bp);
384 bp = NULL;
385 brelse(abp);
386 abp = NULL;
387
388 /* Set up the I/O information */
389 fs->lfs_iocount = 0;
390 fs->lfs_dirvcount = 0;
391 fs->lfs_activesb = 0;
392 #ifdef LFS_CANNOT_ROLLFW
393 fs->lfs_sbactive = NULL;
394 #endif
395 #ifdef LFS_TRACK_IOS
396 for(i=0;i<LFS_THROTTLE;i++)
397 fs->lfs_pending[i] = LFS_UNUSED_DADDR;
398 #endif
399
400 /* Set up the ifile and lock aflags */
401 fs->lfs_doifile = 0;
402 fs->lfs_writer = 0;
403 fs->lfs_dirops = 0;
404 fs->lfs_seglock = 0;
405
406 /* Set the file system readonly/modify bits. */
407 fs->lfs_ronly = ronly;
408 if (ronly == 0)
409 fs->lfs_fmod = 1;
410
411 /* Initialize the mount structure. */
412 dev = devvp->v_rdev;
413 mp->mnt_data = (qaddr_t)ump;
414 mp->mnt_stat.f_fsid.val[0] = (long)dev;
415 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_LFS);
416 mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen;
417 mp->mnt_flag |= MNT_LOCAL;
418 ump->um_flags = 0;
419 ump->um_mountp = mp;
420 ump->um_dev = dev;
421 ump->um_devvp = devvp;
422 ump->um_bptrtodb = 0;
423 ump->um_seqinc = 1 << fs->lfs_fsbtodb;
424 ump->um_nindir = fs->lfs_nindir;
425 for (i = 0; i < MAXQUOTAS; i++)
426 ump->um_quotas[i] = NULLVP;
427 devvp->v_specflags |= SI_MOUNTEDON;
428
429 /*
430 * We use the ifile vnode for almost every operation. Instead of
431 * retrieving it from the hash table each time we retrieve it here,
432 * artificially increment the reference count and keep a pointer
433 * to it in the incore copy of the superblock.
434 */
435 if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0)
436 goto out;
437 fs->lfs_ivnode = vp;
438 VREF(vp);
439 vput(vp);
440
441 return (0);
442 out:
443 if (bp)
444 brelse(bp);
445 if (abp)
446 brelse(abp);
447 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
448 if (ump) {
449 free(ump->um_lfs, M_UFSMNT);
450 free(ump, M_UFSMNT);
451 mp->mnt_data = (qaddr_t)0;
452 }
453 return (error);
454 }
455
456 /*
457 * unmount system call
458 */
459 int
460 lfs_unmount(mp, mntflags, p)
461 struct mount *mp;
462 int mntflags;
463 struct proc *p;
464 {
465 register struct ufsmount *ump;
466 register struct lfs *fs;
467 int error, flags, ronly;
468 extern int lfs_allclean_wakeup;
469
470 flags = 0;
471 if (mntflags & MNT_FORCE)
472 flags |= FORCECLOSE;
473
474 ump = VFSTOUFS(mp);
475 fs = ump->um_lfs;
476 #ifdef QUOTA
477 if (mp->mnt_flag & MNT_QUOTA) {
478 int i;
479 error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
480 if (error)
481 return (error);
482 for (i = 0; i < MAXQUOTAS; i++) {
483 if (ump->um_quotas[i] == NULLVP)
484 continue;
485 quotaoff(p, mp, i);
486 }
487 /*
488 * Here we fall through to vflush again to ensure
489 * that we have gotten rid of all the system vnodes.
490 */
491 }
492 #endif
493 if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
494 return (error);
495 fs->lfs_clean = 1;
496 if ((error = VFS_SYNC(mp, 1, p->p_ucred, p)) != 0)
497 return (error);
498 if (fs->lfs_ivnode->v_dirtyblkhd.lh_first)
499 panic("lfs_unmount: still dirty blocks on ifile vnode\n");
500 vrele(fs->lfs_ivnode);
501 vgone(fs->lfs_ivnode);
502
503 ronly = !fs->lfs_ronly;
504 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
505 error = VOP_CLOSE(ump->um_devvp,
506 ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
507 vrele(ump->um_devvp);
508
509 /* XXX KS - wake up the cleaner so it can die */
510 wakeup(&fs->lfs_nextseg);
511 wakeup(&lfs_allclean_wakeup);
512
513 free(fs, M_UFSMNT);
514 free(ump, M_UFSMNT);
515 mp->mnt_data = (qaddr_t)0;
516 mp->mnt_flag &= ~MNT_LOCAL;
517 return (error);
518 }
519
520 /*
521 * Get file system statistics.
522 */
523 int
524 lfs_statfs(mp, sbp, p)
525 struct mount *mp;
526 register struct statfs *sbp;
527 struct proc *p;
528 {
529 register struct lfs *fs;
530 register struct ufsmount *ump;
531
532 ump = VFSTOUFS(mp);
533 fs = ump->um_lfs;
534 if (fs->lfs_magic != LFS_MAGIC)
535 panic("lfs_statfs: magic");
536 sbp->f_type = 0;
537 sbp->f_bsize = fs->lfs_fsize;
538 sbp->f_iosize = fs->lfs_bsize;
539 sbp->f_blocks = dbtofrags(fs, fs->lfs_dsize);
540 sbp->f_bfree = dbtofrags(fs, fs->lfs_bfree);
541 /*
542 * To compute the available space. Subtract the minimum free
543 * from the total number of blocks in the file system. Set avail
544 * to the smaller of this number and fs->lfs_bfree.
545 *
546 * XXX KS - is my modification below what is desired? (This
547 * will, e.g., change the report when the cleaner runs.)
548 */
549 #if 0
550 sbp->f_bavail = (long) ((u_int64_t) fs->lfs_dsize * (u_int64_t)
551 (100 - fs->lfs_minfree) / (u_int64_t) 100);
552 sbp->f_bavail =
553 sbp->f_bavail > fs->lfs_bfree ? fs->lfs_bfree : sbp->f_bavail;
554 #else
555 sbp->f_bavail = (long) ((u_int64_t) fs->lfs_dsize * (u_int64_t)
556 (100 - fs->lfs_minfree) / (u_int64_t) 100)
557 - (u_int64_t)(fs->lfs_dsize - fs->lfs_bfree);
558 #endif
559 sbp->f_bavail = dbtofrags(fs, sbp->f_bavail);
560 sbp->f_files = fs->lfs_nfiles;
561 sbp->f_ffree = sbp->f_bfree * INOPB(fs);
562 if (sbp != &mp->mnt_stat) {
563 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
564 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
565 }
566 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
567 return (0);
568 }
569
570 /*
571 * Go through the disk queues to initiate sandbagged IO;
572 * go through the inodes to write those that have been modified;
573 * initiate the writing of the super block if it has been modified.
574 *
575 * Note: we are always called with the filesystem marked `MPBUSY'.
576 */
577 int
578 lfs_sync(mp, waitfor, cred, p)
579 struct mount *mp;
580 int waitfor;
581 struct ucred *cred;
582 struct proc *p;
583 {
584 int error;
585 struct lfs *fs;
586
587 fs = ((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs;
588 while(fs->lfs_dirops)
589 error = tsleep(&fs->lfs_dirops, PRIBIO + 1, "lfs_dirops", 0);
590 fs->lfs_writer++;
591
592 /* All syncs must be checkpoints until roll-forward is implemented. */
593 error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
594 if(--fs->lfs_writer==0)
595 wakeup(&fs->lfs_dirops);
596 #ifdef QUOTA
597 qsync(mp);
598 #endif
599 return (error);
600 }
601
602 #ifdef USE_UFS_HASHLOCK
603 extern struct lock ufs_hashlock;
604 #endif
605
606 /*
607 * Look up an LFS dinode number to find its incore vnode. If not already
608 * in core, read it in from the specified device. Return the inode locked.
609 * Detection and handling of mount points must be done by the calling routine.
610 */
611 int
612 lfs_vget(mp, ino, vpp)
613 struct mount *mp;
614 ino_t ino;
615 struct vnode **vpp;
616 {
617 register struct lfs *fs;
618 register struct inode *ip;
619 struct buf *bp;
620 struct ifile *ifp;
621 struct vnode *vp;
622 struct ufsmount *ump;
623 ufs_daddr_t daddr;
624 dev_t dev;
625 int error;
626 #ifdef LFS_ATIME_IFILE
627 struct timespec ts;
628 #endif
629
630 ump = VFSTOUFS(mp);
631 dev = ump->um_dev;
632
633 #ifdef USE_UFS_HASHLOCK
634 do {
635 #endif
636 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
637 return (0);
638 #ifdef USE_UFS_HASHLOCK
639 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
640 #endif
641
642 /* Translate the inode number to a disk address. */
643 fs = ump->um_lfs;
644 if (ino == LFS_IFILE_INUM)
645 daddr = fs->lfs_idaddr;
646 else {
647 LFS_IENTRY(ifp, fs, ino, bp);
648 daddr = ifp->if_daddr;
649 #ifdef LFS_ATIME_IFILE
650 ts = ifp->if_atime; /* structure copy */
651 #endif
652 brelse(bp);
653 if (daddr == LFS_UNUSED_DADDR) {
654 #ifdef USE_UFS_HASHLOCK
655 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
656 #endif
657 return (ENOENT);
658 }
659 }
660
661 /* Allocate new vnode/inode. */
662 if ((error = lfs_vcreate(mp, ino, &vp)) != 0) {
663 *vpp = NULL;
664 #ifdef USE_UFS_HASHLOCK
665 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
666 #endif
667 return (error);
668 }
669
670 /*
671 * Put it onto its hash chain and lock it so that other requests for
672 * this inode will block if they arrive while we are sleeping waiting
673 * for old data structures to be purged or for the contents of the
674 * disk portion of this inode to be read.
675 */
676 ip = VTOI(vp);
677 ufs_ihashins(ip);
678 #ifdef USE_UFS_HASHLOCK
679 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
680 #endif
681
682 /*
683 * XXX
684 * This may not need to be here, logically it should go down with
685 * the i_devvp initialization.
686 * Ask Kirk.
687 */
688 ip->i_lfs = ump->um_lfs;
689
690 /* Read in the disk contents for the inode, copy into the inode. */
691 error = bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp);
692 if (error) {
693 /*
694 * The inode does not contain anything useful, so it would
695 * be misleading to leave it on its hash chain. With mode
696 * still zero, it will be unlinked and returned to the free
697 * list by vput().
698 */
699 vput(vp);
700 brelse(bp);
701 *vpp = NULL;
702 return (error);
703 }
704 ip->i_din.ffs_din = *lfs_ifind(fs, ino, (struct dinode *)bp->b_data);
705 #ifdef LFS_ATIME_IFILE
706 ip->i_ffs_atime = ts.tv_sec;
707 ip->i_ffs_atimensec = ts.tv_nsec;
708 #endif
709 brelse(bp);
710
711 /*
712 * Initialize the vnode from the inode, check for aliases. In all
713 * cases re-init ip, the underlying vnode/inode may have changed.
714 */
715 error = ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
716 if (error) {
717 vput(vp);
718 *vpp = NULL;
719 return (error);
720 }
721 if(vp->v_type == VNON) {
722 printf("lfs_vget: ino %d is type VNON! (ifmt %o)\n", ip->i_number, (ip->i_ffs_mode&IFMT)>>12);
723 #ifdef DDB
724 Debugger();
725 #endif
726 }
727 /*
728 * Finish inode initialization now that aliasing has been resolved.
729 */
730 ip->i_devvp = ump->um_devvp;
731 VREF(ip->i_devvp);
732 *vpp = vp;
733
734 return (0);
735 }
736
737 /*
738 * File handle to vnode
739 *
740 * Have to be really careful about stale file handles:
741 * - check that the inode number is valid
742 * - call lfs_vget() to get the locked inode
743 * - check for an unallocated inode (i_mode == 0)
744 *
745 * XXX
746 * use ifile to see if inode is allocated instead of reading off disk
747 * what is the relationship between my generational number and the NFS
748 * generational number.
749 */
750 int
751 lfs_fhtovp(mp, fhp, vpp)
752 register struct mount *mp;
753 struct fid *fhp;
754 struct vnode **vpp;
755 {
756 register struct ufid *ufhp;
757
758 ufhp = (struct ufid *)fhp;
759 if (ufhp->ufid_ino < ROOTINO)
760 return (ESTALE);
761 return (ufs_fhtovp(mp, ufhp, vpp));
762 }
763
764 /*
765 * Vnode pointer to File handle
766 */
767 /* ARGSUSED */
768 int
769 lfs_vptofh(vp, fhp)
770 struct vnode *vp;
771 struct fid *fhp;
772 {
773 register struct inode *ip;
774 register struct ufid *ufhp;
775
776 ip = VTOI(vp);
777 ufhp = (struct ufid *)fhp;
778 ufhp->ufid_len = sizeof(struct ufid);
779 ufhp->ufid_ino = ip->i_number;
780 ufhp->ufid_gen = ip->i_ffs_gen;
781 return (0);
782 }
783
784 int
785 lfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
786 int *name;
787 u_int namelen;
788 void *oldp;
789 size_t *oldlenp;
790 void *newp;
791 size_t newlen;
792 struct proc *p;
793 {
794 extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead;
795 extern struct lfs_stats lfs_stats;
796 int error;
797
798 /* all sysctl names at this level are terminal */
799 if (namelen != 1)
800 return (ENOTDIR);
801
802 switch (name[0]) {
803 case LFS_WRITEINDIR:
804 return (sysctl_int(oldp, oldlenp, newp, newlen,
805 &lfs_writeindir));
806 case LFS_CLEAN_VNHEAD:
807 return (sysctl_int(oldp, oldlenp, newp, newlen,
808 &lfs_clean_vnhead));
809 case LFS_DOSTATS:
810 if((error = sysctl_int(oldp, oldlenp, newp, newlen,
811 &lfs_dostats)))
812 return error;
813 if(lfs_dostats == 0)
814 memset(&lfs_stats,0,sizeof(lfs_stats));
815 return 0;
816 case LFS_STATS:
817 return (sysctl_rdstruct(oldp, oldlenp, newp,
818 &lfs_stats, sizeof(lfs_stats)));
819 default:
820 return (EOPNOTSUPP);
821 }
822 /* NOTREACHED */
823 }
824