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