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