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