lfs_vfsops.c revision 1.60 1 /* $NetBSD: lfs_vfsops.c,v 1.60 2000/11/27 03:33:57 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 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 <uvm/uvm_extern.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_done,
134 lfs_sysctl,
135 lfs_mountroot,
136 ufs_check_export,
137 lfs_vnodeopv_descs,
138 };
139
140 struct pool lfs_inode_pool;
141
142 extern int locked_queue_count;
143 extern long locked_queue_bytes;
144
145 /*
146 * Initialize the filesystem, most work done by ufs_init.
147 */
148 void
149 lfs_init()
150 {
151 ufs_init();
152
153 /*
154 * XXX Same structure as FFS inodes? Should we share a common pool?
155 */
156 pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
157 "lfsinopl", 0, pool_page_alloc_nointr, pool_page_free_nointr,
158 M_LFSNODE);
159 }
160
161 void
162 lfs_done()
163 {
164 ufs_done();
165 pool_destroy(&lfs_inode_pool);
166 }
167
168 /*
169 * Called by main() when ufs is going to be mounted as root.
170 */
171 int
172 lfs_mountroot()
173 {
174 extern struct vnode *rootvp;
175 struct mount *mp;
176 struct proc *p = curproc; /* XXX */
177 int error;
178
179 if (root_device->dv_class != DV_DISK)
180 return (ENODEV);
181
182 if (rootdev == NODEV)
183 return (ENODEV);
184 /*
185 * Get vnodes for swapdev and rootdev.
186 */
187 if ((error = bdevvp(rootdev, &rootvp))) {
188 printf("lfs_mountroot: can't setup bdevvp's");
189 return (error);
190 }
191 if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
192 vrele(rootvp);
193 return (error);
194 }
195 if ((error = lfs_mountfs(rootvp, mp, p))) {
196 mp->mnt_op->vfs_refcount--;
197 vfs_unbusy(mp);
198 free(mp, M_MOUNT);
199 vrele(rootvp);
200 return (error);
201 }
202 simple_lock(&mountlist_slock);
203 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
204 simple_unlock(&mountlist_slock);
205 (void)lfs_statfs(mp, &mp->mnt_stat, p);
206 vfs_unbusy(mp);
207 return (0);
208 }
209
210 /*
211 * VFS Operations.
212 *
213 * mount system call
214 */
215 int
216 lfs_mount(mp, path, data, ndp, p)
217 struct mount *mp;
218 const char *path;
219 void *data;
220 struct nameidata *ndp;
221 struct proc *p;
222 {
223 struct vnode *devvp;
224 struct ufs_args args;
225 struct ufsmount *ump = NULL;
226 struct lfs *fs = NULL; /* LFS */
227 size_t size;
228 int error;
229 mode_t accessmode;
230
231 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
232 if (error)
233 return (error);
234
235 #if 0
236 /* Until LFS can do NFS right. XXX */
237 if (args.export.ex_flags & MNT_EXPORTED)
238 return (EINVAL);
239 #endif
240
241 /*
242 * If updating, check whether changing from read-only to
243 * read/write; if there is no device name, that's all we do.
244 */
245 if (mp->mnt_flag & MNT_UPDATE) {
246 ump = VFSTOUFS(mp);
247 fs = ump->um_lfs;
248 if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
249 /*
250 * If upgrade to read-write by non-root, then verify
251 * that user has necessary permissions on the device.
252 */
253 if (p->p_ucred->cr_uid != 0) {
254 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
255 error = VOP_ACCESS(ump->um_devvp, VREAD|VWRITE,
256 p->p_ucred, p);
257 VOP_UNLOCK(ump->um_devvp, 0);
258 if (error)
259 return (error);
260 }
261 fs->lfs_ronly = 0;
262 }
263 if (args.fspec == 0) {
264 /*
265 * Process export requests.
266 */
267 return (vfs_export(mp, &ump->um_export, &args.export));
268 }
269 }
270 /*
271 * Not an update, or updating the name: look up the name
272 * and verify that it refers to a sensible block device.
273 */
274 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
275 if ((error = namei(ndp)) != 0)
276 return (error);
277 devvp = ndp->ni_vp;
278 if (devvp->v_type != VBLK) {
279 vrele(devvp);
280 return (ENOTBLK);
281 }
282 if (major(devvp->v_rdev) >= nblkdev) {
283 vrele(devvp);
284 return (ENXIO);
285 }
286 /*
287 * If mount by non-root, then verify that user has necessary
288 * permissions on the device.
289 */
290 if (p->p_ucred->cr_uid != 0) {
291 accessmode = VREAD;
292 if ((mp->mnt_flag & MNT_RDONLY) == 0)
293 accessmode |= VWRITE;
294 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
295 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
296 if (error) {
297 vput(devvp);
298 return (error);
299 }
300 VOP_UNLOCK(devvp, 0);
301 }
302 if ((mp->mnt_flag & MNT_UPDATE) == 0)
303 error = lfs_mountfs(devvp, mp, p); /* LFS */
304 else {
305 if (devvp != ump->um_devvp)
306 error = EINVAL; /* needs translation */
307 else
308 vrele(devvp);
309 }
310 if (error) {
311 vrele(devvp);
312 return (error);
313 }
314 ump = VFSTOUFS(mp);
315 fs = ump->um_lfs; /* LFS */
316 (void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
317 bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
318 bcopy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
319 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
320 &size);
321 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
322 return (0);
323 }
324
325 #ifdef LFS_DO_ROLLFORWARD
326 /*
327 * Roll-forward code.
328 */
329
330 /*
331 * Load the appropriate indirect block, and change the appropriate pointer.
332 * Mark the block dirty. Do segment and avail accounting.
333 */
334 static int
335 update_meta(struct lfs *fs, ino_t ino, int version, ufs_daddr_t lbn,
336 daddr_t ndaddr, size_t size, struct proc *p)
337 {
338 int error;
339 struct vnode *vp;
340 struct inode *ip;
341 daddr_t odaddr, ooff;
342 struct indir a[NIADDR], *ap;
343 struct buf *bp;
344 SEGUSE *sup;
345 int num;
346
347 if ((error = lfs_rf_valloc(fs, ino, version, p, &vp)) != 0) {
348 printf("update_meta: ino %d: lfs_rf_valloc returned %d\n", ino,
349 error);
350 return error;
351 }
352
353 if ((error = VOP_BALLOC(vp, (lbn << fs->lfs_bshift), size,
354 NOCRED, 0, &bp)) != 0) {
355 vput(vp);
356 return (error);
357 }
358 /* No need to write, the block is already on disk */
359 if (bp->b_flags & B_DELWRI) {
360 LFS_UNLOCK_BUF(bp);
361 fs->lfs_avail += btodb(bp->b_bcount);
362 }
363 bp->b_flags |= B_INVAL;
364 brelse(bp);
365
366 /*
367 * Extend the file, if it is not large enough already.
368 * XXX this is not exactly right, we don't know how much of the
369 * XXX last block is actually used. We hope that an inode will
370 * XXX appear later to give the correct size.
371 */
372 ip = VTOI(vp);
373 if (ip->i_ffs_size <= (lbn << fs->lfs_bshift)) {
374 if (lbn < NDADDR)
375 ip->i_ffs_size = (lbn << fs->lfs_bshift) +
376 (size - fs->lfs_fsize) + 1;
377 else
378 ip->i_ffs_size = (lbn << fs->lfs_bshift) + 1;
379 }
380
381 error = ufs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL);
382 if (error) {
383 printf("update_meta: ufs_bmaparray returned %d\n", error);
384 vput(vp);
385 return error;
386 }
387 switch (num) {
388 case 0:
389 ooff = ip->i_ffs_db[lbn];
390 if (ooff == UNWRITTEN)
391 ip->i_ffs_blocks += btodb(size);
392 ip->i_ffs_db[lbn] = ndaddr;
393 break;
394 case 1:
395 ooff = ip->i_ffs_ib[a[0].in_off];
396 if (ooff == UNWRITTEN)
397 ip->i_ffs_blocks += btodb(size);
398 ip->i_ffs_ib[a[0].in_off] = ndaddr;
399 break;
400 default:
401 ap = &a[num - 1];
402 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
403 panic("update_meta: bread bno %d", ap->in_lbn);
404
405 ooff = ((ufs_daddr_t *)bp->b_data)[ap->in_off];
406 if (ooff == UNWRITTEN)
407 ip->i_ffs_blocks += btodb(size);
408 ((ufs_daddr_t *)bp->b_data)[ap->in_off] = ndaddr;
409 (void) VOP_BWRITE(bp);
410 }
411 LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
412
413 /* Update segment usage information. */
414 if (odaddr > 0) {
415 LFS_SEGENTRY(sup, fs, datosn(fs, odaddr), bp);
416 #ifdef DIAGNOSTIC
417 if (sup->su_nbytes < size) {
418 panic("update_meta: negative bytes "
419 "(segment %d short by %ld)\n",
420 datosn(fs, odaddr), (long)size - sup->su_nbytes);
421 sup->su_nbytes = size;
422 }
423 #endif
424 sup->su_nbytes -= size;
425 VOP_BWRITE(bp);
426 }
427 LFS_SEGENTRY(sup, fs, datosn(fs, ndaddr), bp);
428 sup->su_nbytes += size;
429 VOP_BWRITE(bp);
430
431 /* Fix this so it can be released */
432 /* ip->i_lfs_effnblks = ip->i_ffs_blocks; */
433
434 /* Now look again to make sure it worked */
435 ufs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL );
436 if (odaddr != ndaddr)
437 printf("update_meta: failed setting ino %d lbn %d to %x\n",
438 ino, lbn, ndaddr);
439
440 vput(vp);
441 return 0;
442 }
443
444 static int
445 update_inoblk(struct lfs *fs, daddr_t offset, struct ucred *cred,
446 struct proc *p)
447 {
448 struct vnode *devvp, *vp;
449 struct inode *ip;
450 struct dinode *dip;
451 struct buf *dbp, *ibp;
452 int error;
453 daddr_t daddr;
454 IFILE *ifp;
455 SEGUSE *sup;
456
457 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
458
459 /*
460 * Get the inode, update times and perms.
461 * DO NOT update disk blocks, we do that separately.
462 */
463 error = bread(devvp, offset, fs->lfs_bsize, cred, &dbp);
464 if (error) {
465 printf("update_inoblk: bread returned %d\n", error);
466 return error;
467 }
468 dip = ((struct dinode *)(dbp->b_data)) + INOPB(fs);
469 while(--dip >= (struct dinode *)dbp->b_data) {
470 if(dip->di_inumber > LFS_IFILE_INUM) {
471 /* printf("ino %d version %d\n", dip->di_inumber,
472 dip->di_gen); */
473 error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen,
474 p, &vp);
475 if (error) {
476 printf("update_inoblk: lfs_rf_valloc returned %d\n", error);
477 continue;
478 }
479 ip = VTOI(vp);
480 if (dip->di_size != ip->i_ffs_size)
481 VOP_TRUNCATE(vp, dip->di_size, 0, NOCRED, p);
482 /* Get mode, link count, size, and times */
483 memcpy(&ip->i_din.ffs_din, dip,
484 offsetof(struct dinode, di_db[0]));
485
486 /* Then the rest, except di_blocks */
487 ip->i_ffs_flags = dip->di_flags;
488 ip->i_ffs_gen = dip->di_gen;
489 ip->i_ffs_uid = dip->di_uid;
490 ip->i_ffs_gid = dip->di_gid;
491
492 ip->i_ffs_effnlink = dip->di_nlink;
493
494 LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
495
496 /* Re-initialize to get type right */
497 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
498 &vp);
499 vput(vp);
500
501 /* Record change in location */
502 LFS_IENTRY(ifp, fs, dip->di_inumber, ibp);
503 daddr = ifp->if_daddr;
504 ifp->if_daddr = dbp->b_blkno;
505 error = VOP_BWRITE(ibp); /* Ifile */
506 /* And do segment accounting */
507 if (datosn(fs, daddr) != datosn(fs, dbp->b_blkno)) {
508 if (daddr > 0) {
509 LFS_SEGENTRY(sup, fs, datosn(fs, daddr),
510 ibp);
511 sup->su_nbytes -= DINODE_SIZE;
512 VOP_BWRITE(ibp);
513 }
514 LFS_SEGENTRY(sup, fs, datosn(fs, dbp->b_blkno),
515 ibp);
516 sup->su_nbytes += DINODE_SIZE;
517 VOP_BWRITE(ibp);
518 }
519 }
520 }
521 dbp->b_flags |= B_AGE;
522 brelse(dbp);
523
524 return 0;
525 }
526
527 #define CHECK_CKSUM 0x0001 /* Check the checksum to make sure it's valid */
528 #define CHECK_UPDATE 0x0002 /* Update Ifile for new data blocks / inodes */
529
530 static daddr_t
531 check_segsum(struct lfs *fs, daddr_t offset,
532 struct ucred *cred, int flags, int *pseg_flags, struct proc *p)
533 {
534 struct vnode *devvp;
535 struct buf *bp, *dbp;
536 int error, nblocks, ninos, i, j;
537 SEGSUM *ssp;
538 u_long *dp, *datap; /* XXX u_int32_t */
539 daddr_t *iaddr, oldoffset;
540 FINFO *fip;
541 SEGUSE *sup;
542 size_t size;
543
544 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
545 /*
546 * If the segment has a superblock and we're at the top
547 * of the segment, skip the superblock.
548 */
549 if(sntoda(fs, datosn(fs, offset)) == offset) {
550 LFS_SEGENTRY(sup, fs, datosn(fs, offset), bp);
551 if(sup->su_flags & SEGUSE_SUPERBLOCK)
552 offset += btodb(LFS_SBPAD);
553 brelse(bp);
554 }
555
556 /* Read in the segment summary */
557 error = bread(devvp, offset, LFS_SUMMARY_SIZE, cred, &bp);
558 if(error)
559 return -1;
560
561 /* Check summary checksum */
562 ssp = (SEGSUM *)bp->b_data;
563 if(flags & CHECK_CKSUM) {
564 if(ssp->ss_sumsum != cksum(&ssp->ss_datasum,
565 LFS_SUMMARY_SIZE -
566 sizeof(ssp->ss_sumsum))) {
567 #ifdef DEBUG_LFS_RFW
568 printf("Sumsum error at 0x%x\n", offset);
569 #endif
570 offset = -1;
571 goto err1;
572 }
573 if (ssp->ss_nfinfo == 0 && ssp->ss_ninos == 0) {
574 #ifdef DEBUG_LFS_RFW
575 printf("Empty pseg at 0x%x\n", offset);
576 #endif
577 offset = -1;
578 goto err1;
579 }
580 if (ssp->ss_create < fs->lfs_tstamp) {
581 #ifdef DEBUG_LFS_RFW
582 printf("Old data at 0x%x\n", offset);
583 #endif
584 offset = -1;
585 goto err1;
586 }
587 }
588 if(pseg_flags)
589 *pseg_flags = ssp->ss_flags;
590 oldoffset = offset;
591 offset += btodb(LFS_SUMMARY_SIZE);
592
593 ninos = howmany(ssp->ss_ninos, INOPB(fs));
594 iaddr = (daddr_t *)(bp->b_data + LFS_SUMMARY_SIZE - sizeof(daddr_t));
595 if(flags & CHECK_CKSUM) {
596 /* Count blocks */
597 nblocks = 0;
598 fip = (FINFO *)(bp->b_data + sizeof(SEGSUM));
599 for(i = 0; i < ssp->ss_nfinfo; ++i) {
600 nblocks += fip->fi_nblocks;
601 if(fip->fi_nblocks <= 0)
602 break;
603 fip = (FINFO *)(((char *)fip) + sizeof(FINFO) +
604 (fip->fi_nblocks - 1) *
605 sizeof(ufs_daddr_t));
606 }
607 nblocks += ninos;
608 /* Create the sum array */
609 datap = dp = (u_long *)malloc(nblocks * sizeof(u_long),
610 M_SEGMENT, M_WAITOK);
611 }
612
613 /* Handle individual blocks */
614 fip = (FINFO *)(bp->b_data + sizeof(SEGSUM));
615 for(i = 0; i < ssp->ss_nfinfo || ninos; ++i) {
616 /* Inode block? */
617 if(ninos && *iaddr == offset) {
618 if(flags & CHECK_CKSUM) {
619 /* Read in the head and add to the buffer */
620 error = bread(devvp, offset, fs->lfs_bsize,
621 cred, &dbp);
622 if(error) {
623 offset = -1;
624 goto err2;
625 }
626 (*dp++) = ((u_long *)(dbp->b_data))[0];
627 dbp->b_flags |= B_AGE;
628 brelse(dbp);
629 }
630 if(flags & CHECK_UPDATE) {
631 if ((error = update_inoblk(fs, offset, cred, p))
632 != 0) {
633 offset = -1;
634 goto err2;
635 }
636 }
637 offset += fsbtodb(fs,1);
638 --iaddr;
639 --ninos;
640 --i; /* compensate */
641 continue;
642 }
643 /* printf("check: blocks from ino %d version %d\n",
644 fip->fi_ino, fip->fi_version); */
645 size = fs->lfs_bsize;
646 for(j = 0; j < fip->fi_nblocks; ++j) {
647 if (j == fip->fi_nblocks - 1)
648 size = fip->fi_lastlength;
649 if(flags & CHECK_CKSUM) {
650 error = bread(devvp, offset, size, cred, &dbp);
651 if(error) {
652 offset = -1;
653 goto err2;
654 }
655 (*dp++) = ((u_long *)(dbp->b_data))[0];
656 dbp->b_flags |= B_AGE;
657 brelse(dbp);
658 }
659 /* Account for and update any direct blocks */
660 if((flags & CHECK_UPDATE) &&
661 fip->fi_ino > LFS_IFILE_INUM &&
662 fip->fi_blocks[j] >= 0) {
663 update_meta(fs, fip->fi_ino, fip->fi_version,
664 fip->fi_blocks[j], offset, size, p);
665 }
666 offset += btodb(size);
667 }
668 fip = (FINFO *)(((char *)fip) + sizeof(FINFO)
669 + (fip->fi_nblocks - 1) * sizeof(ufs_daddr_t));
670 }
671 /* Checksum the array, compare */
672 if((flags & CHECK_CKSUM) &&
673 ssp->ss_datasum != cksum(datap, nblocks * sizeof(u_long)))
674 {
675 printf("Datasum error at 0x%x (wanted %x got %x)\n", offset,
676 ssp->ss_datasum, cksum(datap, nblocks *
677 sizeof(u_long)));
678 offset = -1;
679 goto err2;
680 }
681
682 /* If we're at the end of the segment, move to the next */
683 if(datosn(fs, offset + btodb(LFS_SUMMARY_SIZE + fs->lfs_bsize)) !=
684 datosn(fs, offset)) {
685 if (datosn(fs, offset) == datosn(fs, ssp->ss_next)) {
686 offset = -1;
687 goto err2;
688 }
689 offset = ssp->ss_next;
690 #ifdef DEBUG_LFS_RFW
691 printf("LFS roll forward: moving on to offset 0x%x "
692 " -> segment %d\n", offset, datosn(fs,offset));
693 #endif
694 }
695
696 if (flags & CHECK_UPDATE) {
697 fs->lfs_avail -= (offset - oldoffset);
698 /* Don't clog the buffer queue */
699 if (locked_queue_count > LFS_MAX_BUFS ||
700 locked_queue_bytes > LFS_MAX_BYTES) {
701 ++fs->lfs_writer;
702 lfs_flush(fs, SEGM_CKP);
703 if(--fs->lfs_writer==0)
704 wakeup(&fs->lfs_dirops);
705 }
706 }
707
708 err2:
709 if(flags & CHECK_CKSUM)
710 free(datap, M_SEGMENT);
711 err1:
712 bp->b_flags |= B_AGE;
713 brelse(bp);
714
715 return offset;
716 }
717 #endif /* LFS_DO_ROLLFORWARD */
718
719 /*
720 * Common code for mount and mountroot
721 * LFS specific
722 */
723 int
724 lfs_mountfs(devvp, mp, p)
725 struct vnode *devvp;
726 struct mount *mp;
727 struct proc *p;
728 {
729 extern struct vnode *rootvp;
730 struct dlfs *tdfs, *dfs, *adfs;
731 struct lfs *fs;
732 struct ufsmount *ump;
733 struct vnode *vp;
734 struct buf *bp, *abp;
735 struct partinfo dpart;
736 dev_t dev;
737 int error, i, ronly, size;
738 struct ucred *cred;
739 CLEANERINFO *cip;
740 SEGUSE *sup;
741 #ifdef LFS_DO_ROLLFORWARD
742 int flags, dirty;
743 daddr_t offset, oldoffset, lastgoodpseg;
744 int sn, curseg;
745 #endif
746
747 cred = p ? p->p_ucred : NOCRED;
748 /*
749 * Disallow multiple mounts of the same device.
750 * Disallow mounting of a device that is currently in use
751 * (except for root, which might share swap device for miniroot).
752 * Flush out any old buffers remaining from a previous use.
753 */
754 if ((error = vfs_mountedon(devvp)) != 0)
755 return (error);
756 if (vcount(devvp) > 1 && devvp != rootvp)
757 return (EBUSY);
758 if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
759 return (error);
760
761 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
762 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
763 if (error)
764 return (error);
765 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
766 size = DEV_BSIZE;
767 else
768 size = dpart.disklab->d_secsize;
769
770 /* Don't free random space on error. */
771 bp = NULL;
772 abp = NULL;
773 ump = NULL;
774
775 /* Read in the superblock. */
776 error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, cred, &bp);
777 if (error)
778 goto out;
779 dfs = (struct dlfs *)bp->b_data;
780
781 /* Check the basics. */
782 if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize > MAXBSIZE ||
783 dfs->dlfs_version > LFS_VERSION ||
784 dfs->dlfs_bsize < sizeof(struct dlfs)) {
785 error = EINVAL; /* XXX needs translation */
786 goto out;
787 }
788
789 /*
790 * Check the second superblock to see which is newer; then mount
791 * using the older of the two. This is necessary to ensure that
792 * the filesystem is valid if it was not unmounted cleanly.
793 */
794
795 if (dfs->dlfs_sboffs[1] &&
796 dfs->dlfs_sboffs[1]-(LFS_LABELPAD/size) > LFS_SBPAD/size)
797 {
798 error = bread(devvp, dfs->dlfs_sboffs[1], LFS_SBPAD, cred, &abp);
799 if (error)
800 goto out;
801 adfs = (struct dlfs *)abp->b_data;
802
803 if (adfs->dlfs_tstamp < dfs->dlfs_tstamp) /* XXX 1s? */
804 tdfs = adfs;
805 else
806 tdfs = dfs;
807
808 /* Check the basics. */
809 if (tdfs->dlfs_magic != LFS_MAGIC ||
810 tdfs->dlfs_bsize > MAXBSIZE ||
811 tdfs->dlfs_version > LFS_VERSION ||
812 tdfs->dlfs_bsize < sizeof(struct dlfs)) {
813 error = EINVAL; /* XXX needs translation */
814 goto out;
815 }
816 } else {
817 printf("lfs_mountfs: invalid alt superblock daddr=0x%x\n",
818 dfs->dlfs_sboffs[1]);
819 error = EINVAL;
820 goto out;
821 }
822
823 /* Allocate the mount structure, copy the superblock into it. */
824 fs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK);
825 memcpy(&fs->lfs_dlfs, tdfs, sizeof(struct dlfs));
826
827 #ifdef LFS_DO_ROLLFORWARD
828 /* Before rolling forward, lock so vget will sleep for other procs */
829 fs->lfs_flags = LFS_NOTYET;
830 fs->lfs_rfpid = p->p_pid;
831 #else
832 fs->lfs_flags = 0;
833 #endif
834
835 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
836 memset((caddr_t)ump, 0, sizeof *ump);
837 ump->um_lfs = fs;
838 if (sizeof(struct lfs) < LFS_SBPAD) { /* XXX why? */
839 bp->b_flags |= B_INVAL;
840 abp->b_flags |= B_INVAL;
841 }
842 brelse(bp);
843 bp = NULL;
844 brelse(abp);
845 abp = NULL;
846
847 /* Set up the I/O information */
848 fs->lfs_iocount = 0;
849 fs->lfs_diropwait = 0;
850 fs->lfs_activesb = 0;
851 fs->lfs_uinodes = 0;
852 fs->lfs_ravail = 0;
853 #ifdef LFS_CANNOT_ROLLFW
854 fs->lfs_sbactive = 0;
855 #endif
856 #ifdef LFS_TRACK_IOS
857 for (i=0;i<LFS_THROTTLE;i++)
858 fs->lfs_pending[i] = LFS_UNUSED_DADDR;
859 #endif
860
861 /* Set up the ifile and lock aflags */
862 fs->lfs_doifile = 0;
863 fs->lfs_writer = 0;
864 fs->lfs_dirops = 0;
865 fs->lfs_nadirop = 0;
866 fs->lfs_seglock = 0;
867 lockinit(&fs->lfs_freelock, PINOD, "lfs_freelock", 0, 0);
868
869 /* Set the file system readonly/modify bits. */
870 fs->lfs_ronly = ronly;
871 if (ronly == 0)
872 fs->lfs_fmod = 1;
873
874 /* Initialize the mount structure. */
875 dev = devvp->v_rdev;
876 mp->mnt_data = (qaddr_t)ump;
877 mp->mnt_stat.f_fsid.val[0] = (long)dev;
878 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_LFS);
879 mp->mnt_stat.f_iosize = fs->lfs_bsize;
880 mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen;
881 mp->mnt_flag |= MNT_LOCAL;
882 ump->um_flags = 0;
883 ump->um_mountp = mp;
884 ump->um_dev = dev;
885 ump->um_devvp = devvp;
886 ump->um_bptrtodb = 0;
887 ump->um_seqinc = 1 << fs->lfs_fsbtodb;
888 ump->um_nindir = fs->lfs_nindir;
889 for (i = 0; i < MAXQUOTAS; i++)
890 ump->um_quotas[i] = NULLVP;
891 devvp->v_specmountpoint = mp;
892
893 /*
894 * We use the ifile vnode for almost every operation. Instead of
895 * retrieving it from the hash table each time we retrieve it here,
896 * artificially increment the reference count and keep a pointer
897 * to it in the incore copy of the superblock.
898 */
899 if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0)
900 goto out;
901 fs->lfs_ivnode = vp;
902 VREF(vp);
903 vput(vp);
904
905 #ifdef LFS_DO_ROLLFORWARD
906 /*
907 * Roll forward.
908 */
909 /*
910 * Phase I:
911 * Find the address of the last good partial segment that was written
912 * after the checkpoint. Mark the segments in question dirty, so
913 * they won't be reallocated.
914 */
915 lastgoodpseg = oldoffset = offset = fs->lfs_offset;
916 flags = 0x0;
917 #ifdef DEBUG_LFS_RFW
918 printf("LFS roll forward phase 1: starting at offset 0x%x\n", offset);
919 #endif
920 LFS_SEGENTRY(sup, fs, datosn(fs, offset), bp);
921 if (!(sup->su_flags & SEGUSE_DIRTY))
922 --fs->lfs_nclean;
923 sup->su_flags |= SEGUSE_DIRTY;
924 (void) VOP_BWRITE(bp);
925 while ((offset = check_segsum(fs, offset, cred, CHECK_CKSUM, &flags,
926 p)) > 0) {
927 if(sntoda(fs, oldoffset) != sntoda(fs, offset)) {
928 LFS_SEGENTRY(sup, fs, datosn(fs, oldoffset), bp);
929 if (!(sup->su_flags & SEGUSE_DIRTY))
930 --fs->lfs_nclean;
931 sup->su_flags |= SEGUSE_DIRTY;
932 (void) VOP_BWRITE(bp);
933 }
934
935 #ifdef DEBUG_LFS_RFW
936 printf("LFS roll forward phase 1: offset=0x%x\n", offset);
937 if(flags & SS_DIROP) {
938 printf("lfs_mountfs: dirops at 0x%x\n", oldoffset);
939 if(!(flags & SS_CONT))
940 printf("lfs_mountfs: dirops end at 0x%x\n",
941 oldoffset);
942 }
943 #endif
944 if(!(flags & SS_CONT))
945 lastgoodpseg = offset;
946 oldoffset = offset;
947 }
948 #ifdef DEBUG_LFS_RFW
949 if (flags & SS_CONT) {
950 printf("LFS roll forward: warning: incomplete dirops discarded\n");
951 }
952 printf("LFS roll forward phase 1: completed: lastgoodpseg=0x%x\n",
953 lastgoodpseg);
954 #endif
955
956 /* Don't accidentally overwrite what we're trying to preserve */
957 offset = fs->lfs_offset;
958 fs->lfs_offset = lastgoodpseg;
959 fs->lfs_curseg = sntoda(fs, datosn(fs, fs->lfs_offset));
960 for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
961 sn = (sn + 1) % fs->lfs_nseg;
962 if (sn == curseg)
963 panic("lfs_mountfs: no clean segments");
964 LFS_SEGENTRY(sup, fs, sn, bp);
965 dirty = (sup->su_flags & SEGUSE_DIRTY);
966 brelse(bp);
967 if (!dirty)
968 break;
969 }
970 fs->lfs_nextseg = sntoda(fs, sn);
971
972 /*
973 * Phase II: Roll forward from the first superblock.
974 */
975 while (offset != lastgoodpseg) {
976 #ifdef DEBUG_LFS_RFW
977 printf("LFS roll forward phase 2: 0x%x\n", offset);
978 #endif
979 oldoffset = offset;
980 offset = check_segsum(fs, offset, cred, CHECK_UPDATE, NULL, p);
981 }
982
983 /*
984 * Finish: flush our changes to disk.
985 */
986 lfs_segwrite(fs->lfs_ivnode->v_mount, SEGM_CKP | SEGM_SYNC);
987
988 #ifdef DEBUG_LFS_RFW
989 printf("LFS roll forward complete\n");
990 #endif
991 /* Allow vget now that roll-forward is complete */
992 fs->lfs_flags &= ~(LFS_NOTYET);
993 wakeup(&fs->lfs_flags);
994 #endif /* LFS_DO_ROLLFORWARD */
995
996 /*
997 * Initialize the ifile cleaner info with information from
998 * the superblock.
999 */
1000 LFS_CLEANERINFO(cip, fs, bp);
1001 cip->clean = fs->lfs_nclean;
1002 cip->dirty = fs->lfs_nseg - fs->lfs_nclean;
1003 cip->avail = fs->lfs_avail;
1004 cip->bfree = fs->lfs_bfree;
1005 (void) VOP_BWRITE(bp); /* Ifile */
1006
1007 /*
1008 * Mark the current segment as ACTIVE, since we're going to
1009 * be writing to it.
1010 */
1011 LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_offset), bp);
1012 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1013 (void) VOP_BWRITE(bp); /* Ifile */
1014
1015 return (0);
1016 out:
1017 if (bp)
1018 brelse(bp);
1019 if (abp)
1020 brelse(abp);
1021 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1022 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
1023 VOP_UNLOCK(devvp, 0);
1024 if (ump) {
1025 free(ump->um_lfs, M_UFSMNT);
1026 free(ump, M_UFSMNT);
1027 mp->mnt_data = (qaddr_t)0;
1028 }
1029 return (error);
1030 }
1031
1032 /*
1033 * unmount system call
1034 */
1035 int
1036 lfs_unmount(mp, mntflags, p)
1037 struct mount *mp;
1038 int mntflags;
1039 struct proc *p;
1040 {
1041 struct ufsmount *ump;
1042 struct lfs *fs;
1043 int error, flags, ronly;
1044 extern int lfs_allclean_wakeup;
1045
1046 flags = 0;
1047 if (mntflags & MNT_FORCE)
1048 flags |= FORCECLOSE;
1049
1050 ump = VFSTOUFS(mp);
1051 fs = ump->um_lfs;
1052 #ifdef QUOTA
1053 if (mp->mnt_flag & MNT_QUOTA) {
1054 int i;
1055 error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
1056 if (error)
1057 return (error);
1058 for (i = 0; i < MAXQUOTAS; i++) {
1059 if (ump->um_quotas[i] == NULLVP)
1060 continue;
1061 quotaoff(p, mp, i);
1062 }
1063 /*
1064 * Here we fall through to vflush again to ensure
1065 * that we have gotten rid of all the system vnodes.
1066 */
1067 }
1068 #endif
1069 if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
1070 return (error);
1071 fs->lfs_clean = 1;
1072 if ((error = VFS_SYNC(mp, 1, p->p_ucred, p)) != 0)
1073 return (error);
1074 if (fs->lfs_ivnode->v_dirtyblkhd.lh_first)
1075 panic("lfs_unmount: still dirty blocks on ifile vnode\n");
1076 vrele(fs->lfs_ivnode);
1077 vgone(fs->lfs_ivnode);
1078
1079 ronly = !fs->lfs_ronly;
1080 if (ump->um_devvp->v_type != VBAD)
1081 ump->um_devvp->v_specmountpoint = NULL;
1082 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1083 error = VOP_CLOSE(ump->um_devvp,
1084 ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
1085 vput(ump->um_devvp);
1086
1087 /* XXX KS - wake up the cleaner so it can die */
1088 wakeup(&fs->lfs_nextseg);
1089 wakeup(&lfs_allclean_wakeup);
1090
1091 free(fs, M_UFSMNT);
1092 free(ump, M_UFSMNT);
1093 mp->mnt_data = (qaddr_t)0;
1094 mp->mnt_flag &= ~MNT_LOCAL;
1095 return (error);
1096 }
1097
1098 /*
1099 * Get file system statistics.
1100 */
1101 int
1102 lfs_statfs(mp, sbp, p)
1103 struct mount *mp;
1104 struct statfs *sbp;
1105 struct proc *p;
1106 {
1107 struct lfs *fs;
1108 struct ufsmount *ump;
1109
1110 ump = VFSTOUFS(mp);
1111 fs = ump->um_lfs;
1112 if (fs->lfs_magic != LFS_MAGIC)
1113 panic("lfs_statfs: magic");
1114
1115 sbp->f_type = 0;
1116 sbp->f_bsize = fs->lfs_fsize;
1117 sbp->f_iosize = fs->lfs_bsize;
1118 sbp->f_blocks = dbtofrags(fs, LFS_EST_NONMETA(fs));
1119 sbp->f_bfree = dbtofrags(fs, LFS_EST_BFREE(fs));
1120 sbp->f_bavail = dbtofrags(fs, (long)LFS_EST_BFREE(fs) -
1121 (long)LFS_EST_RSVD(fs));
1122 sbp->f_files = dbtofsb(fs,fs->lfs_bfree) * INOPB(fs);
1123 sbp->f_ffree = sbp->f_files - fs->lfs_nfiles;
1124 if (sbp != &mp->mnt_stat) {
1125 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
1126 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
1127 }
1128 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
1129 return (0);
1130 }
1131
1132 /*
1133 * Go through the disk queues to initiate sandbagged IO;
1134 * go through the inodes to write those that have been modified;
1135 * initiate the writing of the super block if it has been modified.
1136 *
1137 * Note: we are always called with the filesystem marked `MPBUSY'.
1138 */
1139 int
1140 lfs_sync(mp, waitfor, cred, p)
1141 struct mount *mp;
1142 int waitfor;
1143 struct ucred *cred;
1144 struct proc *p;
1145 {
1146 int error;
1147 struct lfs *fs;
1148
1149 fs = ((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs;
1150 if (fs->lfs_ronly)
1151 return 0;
1152 while(fs->lfs_dirops)
1153 error = tsleep(&fs->lfs_dirops, PRIBIO + 1, "lfs_dirops", 0);
1154 fs->lfs_writer++;
1155
1156 /* All syncs must be checkpoints until roll-forward is implemented. */
1157 error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
1158 if(--fs->lfs_writer==0)
1159 wakeup(&fs->lfs_dirops);
1160 #ifdef QUOTA
1161 qsync(mp);
1162 #endif
1163 return (error);
1164 }
1165
1166 extern struct lock ufs_hashlock;
1167
1168 /*
1169 * Look up an LFS dinode number to find its incore vnode. If not already
1170 * in core, read it in from the specified device. Return the inode locked.
1171 * Detection and handling of mount points must be done by the calling routine.
1172 */
1173 int
1174 lfs_vget(mp, ino, vpp)
1175 struct mount *mp;
1176 ino_t ino;
1177 struct vnode **vpp;
1178 {
1179 struct lfs *fs;
1180 struct inode *ip;
1181 struct buf *bp;
1182 struct ifile *ifp;
1183 struct vnode *vp;
1184 struct ufsmount *ump;
1185 ufs_daddr_t daddr;
1186 dev_t dev;
1187 int error;
1188 #ifdef LFS_ATIME_IFILE
1189 struct timespec ts;
1190 #endif
1191
1192 ump = VFSTOUFS(mp);
1193 dev = ump->um_dev;
1194 fs = ump->um_lfs;
1195
1196 /*
1197 * If the filesystem is not completely mounted yet, suspend
1198 * any access requests (wait for roll-forward to complete).
1199 */
1200 while((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
1201 tsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0);
1202
1203 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1204 return (0);
1205
1206 if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
1207 *vpp = NULL;
1208 return (error);
1209 }
1210
1211 do {
1212 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
1213 ungetnewvnode(vp);
1214 return (0);
1215 }
1216 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
1217
1218 /* Translate the inode number to a disk address. */
1219 if (ino == LFS_IFILE_INUM)
1220 daddr = fs->lfs_idaddr;
1221 else {
1222 /* XXX bounds-check this too */
1223 LFS_IENTRY(ifp, fs, ino, bp);
1224 daddr = ifp->if_daddr;
1225 #ifdef LFS_ATIME_IFILE
1226 ts = ifp->if_atime; /* structure copy */
1227 #endif
1228 brelse(bp);
1229 if (daddr == LFS_UNUSED_DADDR) {
1230 *vpp = NULLVP;
1231 ungetnewvnode(vp);
1232 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1233 return (ENOENT);
1234 }
1235 }
1236
1237 /* Allocate/init new vnode/inode. */
1238 lfs_vcreate(mp, ino, vp);
1239
1240 /*
1241 * Put it onto its hash chain and lock it so that other requests for
1242 * this inode will block if they arrive while we are sleeping waiting
1243 * for old data structures to be purged or for the contents of the
1244 * disk portion of this inode to be read.
1245 */
1246 ip = VTOI(vp);
1247 ufs_ihashins(ip);
1248 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1249
1250 /*
1251 * XXX
1252 * This may not need to be here, logically it should go down with
1253 * the i_devvp initialization.
1254 * Ask Kirk.
1255 */
1256 ip->i_lfs = ump->um_lfs;
1257
1258 /* Read in the disk contents for the inode, copy into the inode. */
1259 error = bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp);
1260 if (error) {
1261 /*
1262 * The inode does not contain anything useful, so it would
1263 * be misleading to leave it on its hash chain. With mode
1264 * still zero, it will be unlinked and returned to the free
1265 * list by vput().
1266 */
1267 vput(vp);
1268 brelse(bp);
1269 *vpp = NULL;
1270 return (error);
1271 }
1272 ip->i_din.ffs_din = *lfs_ifind(fs, ino, bp);
1273 ip->i_ffs_effnlink = ip->i_ffs_nlink;
1274 ip->i_lfs_effnblks = ip->i_ffs_blocks;
1275 #ifdef LFS_ATIME_IFILE
1276 ip->i_ffs_atime = ts.tv_sec;
1277 ip->i_ffs_atimensec = ts.tv_nsec;
1278 #endif
1279 brelse(bp);
1280
1281 /*
1282 * Initialize the vnode from the inode, check for aliases. In all
1283 * cases re-init ip, the underlying vnode/inode may have changed.
1284 */
1285 error = ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
1286 if (error) {
1287 vput(vp);
1288 *vpp = NULL;
1289 return (error);
1290 }
1291 #ifdef DIAGNOSTIC
1292 if(vp->v_type == VNON) {
1293 panic("lfs_vget: ino %d is type VNON! (ifmt %o)\n",
1294 ip->i_number, (ip->i_ffs_mode & IFMT) >> 12);
1295 }
1296 #endif
1297 /*
1298 * Finish inode initialization now that aliasing has been resolved.
1299 */
1300 ip->i_devvp = ump->um_devvp;
1301 VREF(ip->i_devvp);
1302 *vpp = vp;
1303
1304 return (0);
1305 }
1306
1307 /*
1308 * File handle to vnode
1309 *
1310 * Have to be really careful about stale file handles:
1311 * - check that the inode number is valid
1312 * - call lfs_vget() to get the locked inode
1313 * - check for an unallocated inode (i_mode == 0)
1314 *
1315 * XXX
1316 * use ifile to see if inode is allocated instead of reading off disk
1317 * what is the relationship between my generational number and the NFS
1318 * generational number.
1319 */
1320 int
1321 lfs_fhtovp(mp, fhp, vpp)
1322 struct mount *mp;
1323 struct fid *fhp;
1324 struct vnode **vpp;
1325 {
1326 struct ufid *ufhp;
1327
1328 ufhp = (struct ufid *)fhp;
1329 if (ufhp->ufid_ino < ROOTINO)
1330 return (ESTALE);
1331 return (ufs_fhtovp(mp, ufhp, vpp));
1332 }
1333
1334 /*
1335 * Vnode pointer to File handle
1336 */
1337 /* ARGSUSED */
1338 int
1339 lfs_vptofh(vp, fhp)
1340 struct vnode *vp;
1341 struct fid *fhp;
1342 {
1343 struct inode *ip;
1344 struct ufid *ufhp;
1345
1346 ip = VTOI(vp);
1347 ufhp = (struct ufid *)fhp;
1348 ufhp->ufid_len = sizeof(struct ufid);
1349 ufhp->ufid_ino = ip->i_number;
1350 ufhp->ufid_gen = ip->i_ffs_gen;
1351 return (0);
1352 }
1353
1354 int
1355 lfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1356 int *name;
1357 u_int namelen;
1358 void *oldp;
1359 size_t *oldlenp;
1360 void *newp;
1361 size_t newlen;
1362 struct proc *p;
1363 {
1364 extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead;
1365 extern struct lfs_stats lfs_stats;
1366 int error;
1367
1368 /* all sysctl names at this level are terminal */
1369 if (namelen != 1)
1370 return (ENOTDIR);
1371
1372 switch (name[0]) {
1373 case LFS_WRITEINDIR:
1374 return (sysctl_int(oldp, oldlenp, newp, newlen,
1375 &lfs_writeindir));
1376 case LFS_CLEAN_VNHEAD:
1377 return (sysctl_int(oldp, oldlenp, newp, newlen,
1378 &lfs_clean_vnhead));
1379 case LFS_DOSTATS:
1380 if((error = sysctl_int(oldp, oldlenp, newp, newlen,
1381 &lfs_dostats)))
1382 return error;
1383 if(lfs_dostats == 0)
1384 memset(&lfs_stats,0,sizeof(lfs_stats));
1385 return 0;
1386 case LFS_STATS:
1387 return (sysctl_rdstruct(oldp, oldlenp, newp,
1388 &lfs_stats, sizeof(lfs_stats)));
1389 default:
1390 return (EOPNOTSUPP);
1391 }
1392 /* NOTREACHED */
1393 }
1394