lfs_vfsops.c revision 1.113 1 /* $NetBSD: lfs_vfsops.c,v 1.113 2003/04/02 10:39:42 fvdl Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 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.113 2003/04/02 10:39:42 fvdl 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/kthread.h>
88 #include <sys/buf.h>
89 #include <sys/device.h>
90 #include <sys/mbuf.h>
91 #include <sys/file.h>
92 #include <sys/disklabel.h>
93 #include <sys/ioctl.h>
94 #include <sys/errno.h>
95 #include <sys/malloc.h>
96 #include <sys/pool.h>
97 #include <sys/socket.h>
98 #include <uvm/uvm_extern.h>
99 #include <sys/sysctl.h>
100 #include <sys/conf.h>
101
102 #include <miscfs/specfs/specdev.h>
103
104 #include <ufs/ufs/quota.h>
105 #include <ufs/ufs/inode.h>
106 #include <ufs/ufs/ufsmount.h>
107 #include <ufs/ufs/ufs_extern.h>
108
109 #include <uvm/uvm.h>
110 #include <uvm/uvm_stat.h>
111 #include <uvm/uvm_pager.h>
112 #include <uvm/uvm_pdaemon.h>
113
114 #include <ufs/lfs/lfs.h>
115 #include <ufs/lfs/lfs_extern.h>
116
117 #include <miscfs/genfs/genfs.h>
118 #include <miscfs/genfs/genfs_node.h>
119 static int lfs_gop_write(struct vnode *, struct vm_page **, int, int);
120
121 static int lfs_mountfs(struct vnode *, struct mount *, struct proc *);
122
123 extern const struct vnodeopv_desc lfs_vnodeop_opv_desc;
124 extern const struct vnodeopv_desc lfs_specop_opv_desc;
125 extern const struct vnodeopv_desc lfs_fifoop_opv_desc;
126 extern int lfs_subsys_pages;
127 extern int locked_queue_count;
128 extern long locked_queue_bytes;
129 extern struct simplelock lfs_subsys_lock;
130
131 pid_t lfs_writer_daemon = 0;
132 int lfs_do_flush = 0;
133
134 const struct vnodeopv_desc * const lfs_vnodeopv_descs[] = {
135 &lfs_vnodeop_opv_desc,
136 &lfs_specop_opv_desc,
137 &lfs_fifoop_opv_desc,
138 NULL,
139 };
140
141 struct vfsops lfs_vfsops = {
142 MOUNT_LFS,
143 lfs_mount,
144 ufs_start,
145 lfs_unmount,
146 ufs_root,
147 ufs_quotactl,
148 lfs_statfs,
149 lfs_sync,
150 lfs_vget,
151 lfs_fhtovp,
152 lfs_vptofh,
153 lfs_init,
154 lfs_reinit,
155 lfs_done,
156 lfs_sysctl,
157 lfs_mountroot,
158 ufs_check_export,
159 lfs_vnodeopv_descs,
160 };
161
162 struct genfs_ops lfs_genfsops = {
163 lfs_gop_size,
164 ufs_gop_alloc,
165 lfs_gop_write,
166 };
167
168 struct pool lfs_inode_pool;
169 struct pool lfs_dinode_pool;
170 struct pool lfs_inoext_pool;
171
172 /*
173 * The writer daemon. UVM keeps track of how many dirty pages we are holding
174 * in lfs_subsys_pages; the daemon flushes the filesystem when this value
175 * crosses the (user-defined) threshhold LFS_MAX_PAGES.
176 */
177 static void
178 lfs_writerd(void *arg)
179 {
180 #ifdef LFS_PD
181 struct mount *mp, *nmp;
182 struct lfs *fs;
183 #endif
184
185 lfs_writer_daemon = curproc->p_pid;
186
187 for (;;) {
188 tsleep(&lfs_writer_daemon, PVM, "lfswriter", 0);
189
190 #ifdef LFS_PD
191 /*
192 * Look through the list of LFSs to see if any of them
193 * have requested pageouts.
194 */
195 simple_lock(&mountlist_slock);
196 for (mp = mountlist.cqh_first; mp != (void *)&mountlist;
197 mp = nmp) {
198 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
199 nmp = mp->mnt_list.cqe_next;
200 continue;
201 }
202 if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS,
203 MFSNAMELEN) == 0) {
204 fs = ((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs;
205 if (fs->lfs_pdflush ||
206 !TAILQ_EMPTY(&fs->lfs_pchainhd)) {
207 fs->lfs_pdflush = 0;
208 lfs_flush_fs(fs, 0);
209 }
210 }
211
212 simple_lock(&mountlist_slock);
213 nmp = mp->mnt_list.cqe_next;
214 vfs_unbusy(mp);
215 }
216 simple_unlock(&mountlist_slock);
217 #endif /* LFS_PD */
218
219 /*
220 * If global state wants a flush, flush everything.
221 */
222 while (lfs_do_flush || locked_queue_count > LFS_MAX_BUFS ||
223 locked_queue_bytes > LFS_MAX_BYTES ||
224 lfs_subsys_pages > LFS_MAX_PAGES) {
225
226 #ifdef DEBUG_LFS_FLUSH
227 if (lfs_do_flush)
228 printf("daemon: lfs_do_flush\n");
229 if (locked_queue_count > LFS_MAX_BUFS)
230 printf("daemon: lqc = %d, max %d\n",
231 locked_queue_count, LFS_MAX_BUFS);
232 if (locked_queue_bytes > LFS_MAX_BYTES)
233 printf("daemon: lqb = %ld, max %d\n",
234 locked_queue_bytes, LFS_MAX_BYTES);
235 if (lfs_subsys_pages > LFS_MAX_PAGES)
236 printf("daemon: lssp = %d, max %d\n",
237 lfs_subsys_pages, LFS_MAX_PAGES);
238 #endif /* DEBUG_LFS_FLUSH */
239 lfs_flush(NULL, SEGM_WRITERD);
240 lfs_do_flush = 0;
241 }
242 wakeup(&lfs_subsys_pages);
243 }
244 /* NOTREACHED */
245 }
246
247 #if 0
248 extern struct malloc_type *debug_malloc_type;
249 extern int debug_malloc_size;
250 extern int debug_malloc_size_lo;
251 extern int debug_malloc_size_hi;
252 #endif
253
254 /*
255 * Initialize the filesystem, most work done by ufs_init.
256 */
257 void
258 lfs_init()
259 {
260 ufs_init();
261
262 /*
263 * XXX Same structure as FFS inodes? Should we share a common pool?
264 */
265 pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
266 "lfsinopl", &pool_allocator_nointr);
267 pool_init(&lfs_dinode_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
268 "lfsdinopl", &pool_allocator_nointr);
269 pool_init(&lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0,
270 "lfsinoextpl", &pool_allocator_nointr);
271 #ifdef DEBUG
272 memset(lfs_log, 0, sizeof(lfs_log));
273 #endif
274 simple_lock_init(&lfs_subsys_lock);
275 #if 0
276 debug_malloc_type = M_SEGMENT;
277 debug_malloc_size = 0;
278 debug_malloc_size_lo = 1;
279 debug_malloc_size_hi = 65536;
280 #endif
281 }
282
283 void
284 lfs_reinit()
285 {
286 ufs_reinit();
287 }
288
289 void
290 lfs_done()
291 {
292 ufs_done();
293 pool_destroy(&lfs_inode_pool);
294 pool_destroy(&lfs_inoext_pool);
295 }
296
297 /*
298 * Called by main() when ufs is going to be mounted as root.
299 */
300 int
301 lfs_mountroot()
302 {
303 extern struct vnode *rootvp;
304 struct mount *mp;
305 struct proc *p = curproc; /* XXX */
306 int error;
307
308 if (root_device->dv_class != DV_DISK)
309 return (ENODEV);
310
311 if (rootdev == NODEV)
312 return (ENODEV);
313 /*
314 * Get vnodes for swapdev and rootdev.
315 */
316 if ((error = bdevvp(rootdev, &rootvp))) {
317 printf("lfs_mountroot: can't setup bdevvp's");
318 return (error);
319 }
320 if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
321 vrele(rootvp);
322 return (error);
323 }
324 if ((error = lfs_mountfs(rootvp, mp, p))) {
325 mp->mnt_op->vfs_refcount--;
326 vfs_unbusy(mp);
327 free(mp, M_MOUNT);
328 vrele(rootvp);
329 return (error);
330 }
331 simple_lock(&mountlist_slock);
332 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
333 simple_unlock(&mountlist_slock);
334 (void)lfs_statfs(mp, &mp->mnt_stat, p);
335 vfs_unbusy(mp);
336 inittodr(VFSTOUFS(mp)->um_lfs->lfs_tstamp);
337 return (0);
338 }
339
340 /*
341 * VFS Operations.
342 *
343 * mount system call
344 */
345 int
346 lfs_mount(struct mount *mp, const char *path, void *data, struct nameidata *ndp, struct proc *p)
347 {
348 struct vnode *devvp;
349 struct ufs_args args;
350 struct ufsmount *ump = NULL;
351 struct lfs *fs = NULL; /* LFS */
352 size_t size;
353 int error;
354 mode_t accessmode;
355
356 if (mp->mnt_flag & MNT_GETARGS) {
357 ump = VFSTOUFS(mp);
358 if (ump == NULL)
359 return EIO;
360 args.fspec = NULL;
361 vfs_showexport(mp, &args.export, &ump->um_export);
362 return copyout(&args, data, sizeof(args));
363 }
364 error = copyin(data, &args, sizeof (struct ufs_args));
365 if (error)
366 return (error);
367
368 #if 0
369 /* Until LFS can do NFS right. XXX */
370 if (args.export.ex_flags & MNT_EXPORTED)
371 return (EINVAL);
372 #endif
373
374 /*
375 * If updating, check whether changing from read-only to
376 * read/write; if there is no device name, that's all we do.
377 */
378 if (mp->mnt_flag & MNT_UPDATE) {
379 ump = VFSTOUFS(mp);
380 fs = ump->um_lfs;
381 if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
382 /*
383 * If upgrade to read-write by non-root, then verify
384 * that user has necessary permissions on the device.
385 */
386 if (p->p_ucred->cr_uid != 0) {
387 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
388 error = VOP_ACCESS(ump->um_devvp, VREAD|VWRITE,
389 p->p_ucred, p);
390 VOP_UNLOCK(ump->um_devvp, 0);
391 if (error)
392 return (error);
393 }
394 fs->lfs_ronly = 0;
395 }
396 if (args.fspec == 0) {
397 /*
398 * Process export requests.
399 */
400 return (vfs_export(mp, &ump->um_export, &args.export));
401 }
402 }
403 /*
404 * Not an update, or updating the name: look up the name
405 * and verify that it refers to a sensible block device.
406 */
407 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
408 if ((error = namei(ndp)) != 0)
409 return (error);
410 devvp = ndp->ni_vp;
411 if (devvp->v_type != VBLK) {
412 vrele(devvp);
413 return (ENOTBLK);
414 }
415 if (bdevsw_lookup(devvp->v_rdev) == NULL) {
416 vrele(devvp);
417 return (ENXIO);
418 }
419 /*
420 * If mount by non-root, then verify that user has necessary
421 * permissions on the device.
422 */
423 if (p->p_ucred->cr_uid != 0) {
424 accessmode = VREAD;
425 if ((mp->mnt_flag & MNT_RDONLY) == 0)
426 accessmode |= VWRITE;
427 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
428 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
429 if (error) {
430 vput(devvp);
431 return (error);
432 }
433 VOP_UNLOCK(devvp, 0);
434 }
435 if ((mp->mnt_flag & MNT_UPDATE) == 0)
436 error = lfs_mountfs(devvp, mp, p); /* LFS */
437 else {
438 if (devvp != ump->um_devvp)
439 error = EINVAL; /* needs translation */
440 else
441 vrele(devvp);
442 }
443 if (error) {
444 vrele(devvp);
445 return (error);
446 }
447 ump = VFSTOUFS(mp);
448 fs = ump->um_lfs; /* LFS */
449 (void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
450 bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
451 bcopy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
452 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
453 &size);
454 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
455 return (0);
456 }
457
458 /*
459 * Roll-forward code.
460 */
461
462 /*
463 * Load the appropriate indirect block, and change the appropriate pointer.
464 * Mark the block dirty. Do segment and avail accounting.
465 */
466 static int
467 update_meta(struct lfs *fs, ino_t ino, int version, daddr_t lbn,
468 daddr_t ndaddr, size_t size, struct proc *p)
469 {
470 int error;
471 struct vnode *vp;
472 struct inode *ip;
473 daddr_t odaddr, ooff;
474 struct indir a[NIADDR], *ap;
475 struct buf *bp;
476 SEGUSE *sup;
477 int num;
478
479 if ((error = lfs_rf_valloc(fs, ino, version, p, &vp)) != 0) {
480 #ifdef DEBUG_LFS_RFW
481 printf("update_meta: ino %d: lfs_rf_valloc returned %d\n", ino,
482 error);
483 #endif
484 return error;
485 }
486
487 if ((error = VOP_BALLOC(vp, (lbn << fs->lfs_bshift), size,
488 NOCRED, 0, &bp)) != 0) {
489 vput(vp);
490 return (error);
491 }
492 /* No need to write, the block is already on disk */
493 if (bp->b_flags & B_DELWRI) {
494 LFS_UNLOCK_BUF(bp);
495 fs->lfs_avail += btofsb(fs, bp->b_bcount);
496 }
497 bp->b_flags |= B_INVAL;
498 brelse(bp);
499
500 /*
501 * Extend the file, if it is not large enough already.
502 * XXX this is not exactly right, we don't know how much of the
503 * XXX last block is actually used. We hope that an inode will
504 * XXX appear later to give the correct size.
505 */
506 ip = VTOI(vp);
507 if (ip->i_size <= (lbn << fs->lfs_bshift)) {
508 if (lbn < NDADDR)
509 ip->i_size = ip->i_ffs1_size = (lbn << fs->lfs_bshift) +
510 (size - fs->lfs_fsize) + 1;
511 else
512 ip->i_size = ip->i_ffs1_size =
513 (lbn << fs->lfs_bshift) + 1;
514 }
515
516 error = ufs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL);
517 if (error) {
518 #ifdef DEBUG_LFS_RFW
519 printf("update_meta: ufs_bmaparray returned %d\n", error);
520 #endif
521 vput(vp);
522 return error;
523 }
524 switch (num) {
525 case 0:
526 ooff = ip->i_ffs1_db[lbn];
527 if (ooff == UNWRITTEN)
528 ip->i_ffs1_blocks += btofsb(fs, size);
529 /* XXX what about fragment extension? */
530 ip->i_ffs1_db[lbn] = ndaddr;
531 break;
532 case 1:
533 ooff = ip->i_ffs1_ib[a[0].in_off];
534 if (ooff == UNWRITTEN)
535 ip->i_ffs1_blocks += btofsb(fs, size);
536 ip->i_ffs1_ib[a[0].in_off] = ndaddr;
537 break;
538 default:
539 ap = &a[num - 1];
540 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
541 panic("update_meta: bread bno %lld",
542 (long long)ap->in_lbn);
543
544 /* XXX ondisk32 */
545 ooff = ((int32_t *)bp->b_data)[ap->in_off];
546 if (ooff == UNWRITTEN)
547 ip->i_ffs1_blocks += btofsb(fs, size);
548 /* XXX ondisk32 */
549 ((int32_t *)bp->b_data)[ap->in_off] = ndaddr;
550 (void) VOP_BWRITE(bp);
551 }
552 LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
553
554 /* Update segment usage information. */
555 if (odaddr > 0) {
556 LFS_SEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, odaddr)), bp);
557 #ifdef DIAGNOSTIC
558 if (sup->su_nbytes < size) {
559 panic("update_meta: negative bytes "
560 "(segment %" PRIu32 " short by %ld)\n",
561 dtosn(fs, dbtofsb(fs, odaddr)), (long)size - sup->su_nbytes);
562 sup->su_nbytes = size;
563 }
564 #endif
565 sup->su_nbytes -= size;
566 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, odaddr)), bp);
567 }
568 LFS_SEGENTRY(sup, fs, dtosn(fs, ndaddr), bp);
569 sup->su_nbytes += size;
570 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, ndaddr), bp);
571
572 /* Fix this so it can be released */
573 /* ip->i_lfs_effnblks = ip->i_ffs1_blocks; */
574
575 #ifdef DEBUG_LFS_RFW
576 /* Now look again to make sure it worked */
577 ufs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL );
578 if (dbtofsb(fs, odaddr) != ndaddr)
579 printf("update_meta: failed setting ino %d lbn %" PRId64
580 " to %" PRId64 "\n", ino, lbn, ndaddr);
581 #endif
582 vput(vp);
583 return 0;
584 }
585
586 static int
587 update_inoblk(struct lfs *fs, daddr_t offset, struct ucred *cred,
588 struct proc *p)
589 {
590 struct vnode *devvp, *vp;
591 struct inode *ip;
592 struct ufs1_dinode *dip;
593 struct buf *dbp, *ibp;
594 int error;
595 daddr_t daddr;
596 IFILE *ifp;
597 SEGUSE *sup;
598
599 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
600
601 /*
602 * Get the inode, update times and perms.
603 * DO NOT update disk blocks, we do that separately.
604 */
605 error = bread(devvp, fsbtodb(fs, offset), fs->lfs_ibsize, cred, &dbp);
606 if (error) {
607 #ifdef DEBUG_LFS_RFW
608 printf("update_inoblk: bread returned %d\n", error);
609 #endif
610 return error;
611 }
612 dip = ((struct ufs1_dinode *)(dbp->b_data)) + INOPB(fs);
613 while (--dip >= (struct ufs1_dinode *)dbp->b_data) {
614 if (dip->di_inumber > LFS_IFILE_INUM) {
615 /* printf("ino %d version %d\n", dip->di_inumber,
616 dip->di_gen); */
617 error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen,
618 p, &vp);
619 if (error) {
620 #ifdef DEBUG_LFS_RFW
621 printf("update_inoblk: lfs_rf_valloc returned %d\n", error);
622 #endif
623 continue;
624 }
625 ip = VTOI(vp);
626 if (dip->di_size != ip->i_size)
627 VOP_TRUNCATE(vp, dip->di_size, 0, NOCRED, p);
628 /* Get mode, link count, size, and times */
629 memcpy(ip->i_din.ffs1_din, dip,
630 offsetof(struct ufs1_dinode, di_db[0]));
631
632 /* Then the rest, except di_blocks */
633 ip->i_flags = ip->i_ffs1_flags = dip->di_flags;
634 ip->i_gen = ip->i_ffs1_gen = dip->di_gen;
635 ip->i_uid = ip->i_ffs1_uid = dip->di_uid;
636 ip->i_gid = ip->i_ffs1_gid = dip->di_gid;
637
638 ip->i_mode = ip->i_ffs1_mode;
639 ip->i_nlink = ip->i_ffs_effnlink = ip->i_ffs1_nlink;
640 ip->i_size = ip->i_ffs1_size;
641
642 LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
643
644 /* Re-initialize to get type right */
645 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
646 &vp);
647 vput(vp);
648
649 /* Record change in location */
650 LFS_IENTRY(ifp, fs, dip->di_inumber, ibp);
651 daddr = ifp->if_daddr;
652 ifp->if_daddr = dbtofsb(fs, dbp->b_blkno);
653 error = LFS_BWRITE_LOG(ibp); /* Ifile */
654 /* And do segment accounting */
655 if (dtosn(fs, daddr) != dtosn(fs, dbtofsb(fs, dbp->b_blkno))) {
656 if (daddr > 0) {
657 LFS_SEGENTRY(sup, fs, dtosn(fs, daddr),
658 ibp);
659 sup->su_nbytes -= sizeof (struct ufs1_dinode);
660 LFS_WRITESEGENTRY(sup, fs,
661 dtosn(fs, daddr),
662 ibp);
663 }
664 LFS_SEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
665 ibp);
666 sup->su_nbytes += sizeof (struct ufs1_dinode);
667 LFS_WRITESEGENTRY(sup, fs,
668 dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
669 ibp);
670 }
671 }
672 }
673 dbp->b_flags |= B_AGE;
674 brelse(dbp);
675
676 return 0;
677 }
678
679 #define CHECK_CKSUM 0x0001 /* Check the checksum to make sure it's valid */
680 #define CHECK_UPDATE 0x0002 /* Update Ifile for new data blocks / inodes */
681
682 static daddr_t
683 check_segsum(struct lfs *fs, daddr_t offset,
684 struct ucred *cred, int flags, int *pseg_flags, struct proc *p)
685 {
686 struct vnode *devvp;
687 struct buf *bp, *dbp;
688 int error, nblocks, ninos, i, j;
689 SEGSUM *ssp;
690 u_long *dp, *datap; /* XXX u_int32_t */
691 daddr_t oldoffset;
692 int32_t *iaddr; /* XXX ondisk32 */
693 FINFO *fip;
694 SEGUSE *sup;
695 size_t size;
696 u_int64_t serial;
697
698 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
699 /*
700 * If the segment has a superblock and we're at the top
701 * of the segment, skip the superblock.
702 */
703 if (sntod(fs, dtosn(fs, offset)) == offset) {
704 LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
705 if (sup->su_flags & SEGUSE_SUPERBLOCK)
706 offset += btofsb(fs, LFS_SBPAD);
707 brelse(bp);
708 }
709
710 /* Read in the segment summary */
711 error = bread(devvp, offset, fs->lfs_sumsize, cred, &bp);
712 if (error)
713 return -1;
714
715 /* Check summary checksum */
716 ssp = (SEGSUM *)bp->b_data;
717 if (flags & CHECK_CKSUM) {
718 if (ssp->ss_sumsum != cksum(&ssp->ss_datasum,
719 fs->lfs_sumsize -
720 sizeof(ssp->ss_sumsum))) {
721 #ifdef DEBUG_LFS_RFW
722 printf("Sumsum error at 0x%" PRIx64 "\n", offset);
723 #endif
724 offset = -1;
725 goto err1;
726 }
727 if (ssp->ss_nfinfo == 0 && ssp->ss_ninos == 0) {
728 #ifdef DEBUG_LFS_RFW
729 printf("Empty pseg at 0x%" PRIx64 "\n", offset);
730 #endif
731 offset = -1;
732 goto err1;
733 }
734 if (ssp->ss_create < fs->lfs_tstamp) {
735 #ifdef DEBUG_LFS_RFW
736 printf("Old data at 0x%" PRIx64 "\n", offset);
737 #endif
738 offset = -1;
739 goto err1;
740 }
741 }
742 if (fs->lfs_version > 1) {
743 serial = ssp->ss_serial;
744 if (serial != fs->lfs_serial + 1) {
745 #ifdef DEBUG_LFS_RFW
746 printf("Unexpected serial number at 0x%" PRIx64
747 "\n", offset);
748 #endif
749 offset = -1;
750 goto err1;
751 }
752 if (ssp->ss_ident != fs->lfs_ident) {
753 #ifdef DEBUG_LFS_RFW
754 printf("Incorrect fsid (0x%x vs 0x%x) at 0x%"
755 PRIx64 "\n", ssp->ss_ident, fs->lfs_ident, offset);
756 #endif
757 offset = -1;
758 goto err1;
759 }
760 }
761 if (pseg_flags)
762 *pseg_flags = ssp->ss_flags;
763 oldoffset = offset;
764 offset += btofsb(fs, fs->lfs_sumsize);
765
766 ninos = howmany(ssp->ss_ninos, INOPB(fs));
767 /* XXX ondisk32 */
768 iaddr = (int32_t *)(bp->b_data + fs->lfs_sumsize - sizeof(int32_t));
769 if (flags & CHECK_CKSUM) {
770 /* Count blocks */
771 nblocks = 0;
772 fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs));
773 for (i = 0; i < ssp->ss_nfinfo; ++i) {
774 nblocks += fip->fi_nblocks;
775 if (fip->fi_nblocks <= 0)
776 break;
777 /* XXX ondisk32 */
778 fip = (FINFO *)(((char *)fip) + FINFOSIZE +
779 (fip->fi_nblocks * sizeof(int32_t)));
780 }
781 nblocks += ninos;
782 /* Create the sum array */
783 datap = dp = (u_long *)malloc(nblocks * sizeof(u_long),
784 M_SEGMENT, M_WAITOK);
785 }
786
787 /* Handle individual blocks */
788 fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs));
789 for (i = 0; i < ssp->ss_nfinfo || ninos; ++i) {
790 /* Inode block? */
791 if (ninos && *iaddr == offset) {
792 if (flags & CHECK_CKSUM) {
793 /* Read in the head and add to the buffer */
794 error = bread(devvp, fsbtodb(fs, offset), fs->lfs_bsize,
795 cred, &dbp);
796 if (error) {
797 offset = -1;
798 goto err2;
799 }
800 (*dp++) = ((u_long *)(dbp->b_data))[0];
801 dbp->b_flags |= B_AGE;
802 brelse(dbp);
803 }
804 if (flags & CHECK_UPDATE) {
805 if ((error = update_inoblk(fs, offset, cred, p))
806 != 0) {
807 offset = -1;
808 goto err2;
809 }
810 }
811 offset += btofsb(fs, fs->lfs_ibsize);
812 --iaddr;
813 --ninos;
814 --i; /* compensate */
815 continue;
816 }
817 /* printf("check: blocks from ino %d version %d\n",
818 fip->fi_ino, fip->fi_version); */
819 size = fs->lfs_bsize;
820 for (j = 0; j < fip->fi_nblocks; ++j) {
821 if (j == fip->fi_nblocks - 1)
822 size = fip->fi_lastlength;
823 if (flags & CHECK_CKSUM) {
824 error = bread(devvp, fsbtodb(fs, offset), size, cred, &dbp);
825 if (error) {
826 offset = -1;
827 goto err2;
828 }
829 (*dp++) = ((u_long *)(dbp->b_data))[0];
830 dbp->b_flags |= B_AGE;
831 brelse(dbp);
832 }
833 /* Account for and update any direct blocks */
834 if ((flags & CHECK_UPDATE) &&
835 fip->fi_ino > LFS_IFILE_INUM &&
836 fip->fi_blocks[j] >= 0) {
837 update_meta(fs, fip->fi_ino, fip->fi_version,
838 fip->fi_blocks[j], offset, size, p);
839 }
840 offset += btofsb(fs, size);
841 }
842 /* XXX ondisk32 */
843 fip = (FINFO *)(((char *)fip) + FINFOSIZE
844 + fip->fi_nblocks * sizeof(int32_t));
845 }
846 /* Checksum the array, compare */
847 if ((flags & CHECK_CKSUM) &&
848 ssp->ss_datasum != cksum(datap, nblocks * sizeof(u_long)))
849 {
850 #ifdef DEBUG_LFS_RFW
851 printf("Datasum error at 0x%" PRIx64 " (wanted %x got %x)\n",
852 offset, ssp->ss_datasum, cksum(datap, nblocks *
853 sizeof(u_long)));
854 #endif
855 offset = -1;
856 goto err2;
857 }
858
859 /* If we're at the end of the segment, move to the next */
860 if (dtosn(fs, offset + btofsb(fs, fs->lfs_sumsize + fs->lfs_bsize)) !=
861 dtosn(fs, offset)) {
862 if (dtosn(fs, offset) == dtosn(fs, ssp->ss_next)) {
863 offset = -1;
864 goto err2;
865 }
866 offset = ssp->ss_next;
867 #ifdef DEBUG_LFS_RFW
868 printf("LFS roll forward: moving on to offset 0x%" PRIx64
869 " -> segment %d\n", offset, dtosn(fs,offset));
870 #endif
871 }
872
873 if (flags & CHECK_UPDATE) {
874 fs->lfs_avail -= (offset - oldoffset);
875 /* Don't clog the buffer queue */
876 if (locked_queue_count > LFS_MAX_BUFS ||
877 locked_queue_bytes > LFS_MAX_BYTES) {
878 ++fs->lfs_writer;
879 lfs_flush(fs, SEGM_CKP);
880 if (--fs->lfs_writer == 0)
881 wakeup(&fs->lfs_dirops);
882 }
883 }
884
885 err2:
886 if (flags & CHECK_CKSUM)
887 free(datap, M_SEGMENT);
888 err1:
889 bp->b_flags |= B_AGE;
890 brelse(bp);
891
892 /* XXX should we update the serial number even for bad psegs? */
893 if ((flags & CHECK_UPDATE) && offset > 0 && fs->lfs_version > 1)
894 fs->lfs_serial = serial;
895 return offset;
896 }
897
898 /*
899 * Common code for mount and mountroot
900 * LFS specific
901 */
902 int
903 lfs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p)
904 {
905 extern struct vnode *rootvp;
906 struct dlfs *tdfs, *dfs, *adfs;
907 struct lfs *fs;
908 struct ufsmount *ump;
909 struct vnode *vp;
910 struct buf *bp, *abp;
911 struct partinfo dpart;
912 dev_t dev;
913 int error, i, ronly, secsize, fsbsize;
914 struct ucred *cred;
915 CLEANERINFO *cip;
916 SEGUSE *sup;
917 int flags, dirty, do_rollforward;
918 daddr_t offset, oldoffset, lastgoodpseg, sb_addr;
919 int sn, curseg;
920
921 cred = p ? p->p_ucred : NOCRED;
922 /*
923 * Disallow multiple mounts of the same device.
924 * Disallow mounting of a device that is currently in use
925 * (except for root, which might share swap device for miniroot).
926 * Flush out any old buffers remaining from a previous use.
927 */
928 if ((error = vfs_mountedon(devvp)) != 0)
929 return (error);
930 if (vcount(devvp) > 1 && devvp != rootvp)
931 return (EBUSY);
932 if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
933 return (error);
934
935 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
936 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
937 if (error)
938 return (error);
939 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) != 0)
940 secsize = DEV_BSIZE;
941 else
942 secsize = dpart.disklab->d_secsize;
943
944 /* Don't free random space on error. */
945 bp = NULL;
946 abp = NULL;
947 ump = NULL;
948
949 sb_addr = LFS_LABELPAD / secsize;
950 while (1) {
951 /* Read in the superblock. */
952 error = bread(devvp, sb_addr, LFS_SBPAD, cred, &bp);
953 if (error)
954 goto out;
955 dfs = (struct dlfs *)bp->b_data;
956
957 /* Check the basics. */
958 if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize >= MAXBSIZE ||
959 dfs->dlfs_version > LFS_VERSION ||
960 dfs->dlfs_bsize < sizeof(struct dlfs)) {
961 #ifdef DEBUG_LFS
962 printf("lfs_mountfs: primary superblock sanity failed\n");
963 #endif
964 error = EINVAL; /* XXX needs translation */
965 goto out;
966 }
967 if (dfs->dlfs_inodefmt > LFS_MAXINODEFMT)
968 printf("lfs_mountfs: warning: unknown inode format %d\n",
969 dfs->dlfs_inodefmt);
970
971 if (dfs->dlfs_version == 1)
972 fsbsize = secsize;
973 else {
974 fsbsize = 1 << (dfs->dlfs_bshift - dfs->dlfs_blktodb +
975 dfs->dlfs_fsbtodb);
976 /*
977 * Could be, if the frag size is large enough, that we
978 * don't have the "real" primary superblock. If that's
979 * the case, get the real one, and try again.
980 */
981 if (sb_addr != dfs->dlfs_sboffs[0] <<
982 dfs->dlfs_fsbtodb) {
983 /* #ifdef DEBUG_LFS */
984 printf("lfs_mountfs: sb daddr 0x%llx is not right, trying 0x%llx\n",
985 (long long)sb_addr, (long long)(dfs->dlfs_sboffs[0] <<
986 dfs->dlfs_fsbtodb));
987 /* #endif */
988 sb_addr = dfs->dlfs_sboffs[0] <<
989 dfs->dlfs_fsbtodb;
990 brelse(bp);
991 continue;
992 }
993 }
994 break;
995 }
996
997 /*
998 * Check the second superblock to see which is newer; then mount
999 * using the older of the two. This is necessary to ensure that
1000 * the filesystem is valid if it was not unmounted cleanly.
1001 */
1002
1003 if (dfs->dlfs_sboffs[1] &&
1004 dfs->dlfs_sboffs[1] - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize)
1005 {
1006 error = bread(devvp, dfs->dlfs_sboffs[1] * (fsbsize / secsize),
1007 LFS_SBPAD, cred, &abp);
1008 if (error)
1009 goto out;
1010 adfs = (struct dlfs *)abp->b_data;
1011
1012 if (dfs->dlfs_version == 1) {
1013 /* 1s resolution comparison */
1014 if (adfs->dlfs_tstamp < dfs->dlfs_tstamp)
1015 tdfs = adfs;
1016 else
1017 tdfs = dfs;
1018 } else {
1019 /* monotonic infinite-resolution comparison */
1020 if (adfs->dlfs_serial < dfs->dlfs_serial)
1021 tdfs = adfs;
1022 else
1023 tdfs = dfs;
1024 }
1025
1026 /* Check the basics. */
1027 if (tdfs->dlfs_magic != LFS_MAGIC ||
1028 tdfs->dlfs_bsize > MAXBSIZE ||
1029 tdfs->dlfs_version > LFS_VERSION ||
1030 tdfs->dlfs_bsize < sizeof(struct dlfs)) {
1031 #ifdef DEBUG_LFS
1032 printf("lfs_mountfs: alt superblock sanity failed\n");
1033 #endif
1034 error = EINVAL; /* XXX needs translation */
1035 goto out;
1036 }
1037 } else {
1038 #ifdef DEBUG_LFS
1039 printf("lfs_mountfs: invalid alt superblock daddr=0x%x\n",
1040 dfs->dlfs_sboffs[1]);
1041 #endif
1042 error = EINVAL;
1043 goto out;
1044 }
1045
1046 /* Allocate the mount structure, copy the superblock into it. */
1047 fs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK | M_ZERO);
1048 memcpy(&fs->lfs_dlfs, tdfs, sizeof(struct dlfs));
1049
1050 /* Compatibility */
1051 if (fs->lfs_version < 2) {
1052 fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
1053 fs->lfs_ibsize = fs->lfs_bsize;
1054 fs->lfs_start = fs->lfs_sboffs[0];
1055 fs->lfs_tstamp = fs->lfs_otstamp;
1056 fs->lfs_fsbtodb = 0;
1057 }
1058
1059 /* Before rolling forward, lock so vget will sleep for other procs */
1060 fs->lfs_flags = LFS_NOTYET;
1061 fs->lfs_rfpid = p->p_pid;
1062
1063 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
1064 ump->um_lfs = fs;
1065 ump->um_fstype = UFS1;
1066 if (sizeof(struct lfs) < LFS_SBPAD) { /* XXX why? */
1067 bp->b_flags |= B_INVAL;
1068 abp->b_flags |= B_INVAL;
1069 }
1070 brelse(bp);
1071 bp = NULL;
1072 brelse(abp);
1073 abp = NULL;
1074
1075 /* Set up the I/O information */
1076 fs->lfs_devbsize = secsize;
1077 fs->lfs_iocount = 0;
1078 fs->lfs_diropwait = 0;
1079 fs->lfs_activesb = 0;
1080 fs->lfs_uinodes = 0;
1081 fs->lfs_ravail = 0;
1082 fs->lfs_sbactive = 0;
1083
1084 /* Set up the ifile and lock aflags */
1085 fs->lfs_doifile = 0;
1086 fs->lfs_writer = 0;
1087 fs->lfs_dirops = 0;
1088 fs->lfs_nadirop = 0;
1089 fs->lfs_seglock = 0;
1090 fs->lfs_pdflush = 0;
1091 fs->lfs_sleepers = 0;
1092 simple_lock_init(&fs->lfs_interlock);
1093 lockinit(&fs->lfs_fraglock, PINOD, "lfs_fraglock", 0, 0);
1094
1095 /* Set the file system readonly/modify bits. */
1096 fs->lfs_ronly = ronly;
1097 if (ronly == 0)
1098 fs->lfs_fmod = 1;
1099
1100 /* Initialize the mount structure. */
1101 dev = devvp->v_rdev;
1102 mp->mnt_data = ump;
1103 mp->mnt_stat.f_fsid.val[0] = (long)dev;
1104 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_LFS);
1105 mp->mnt_stat.f_iosize = fs->lfs_bsize;
1106 mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen;
1107 mp->mnt_flag |= MNT_LOCAL;
1108 mp->mnt_fs_bshift = fs->lfs_bshift;
1109 ump->um_flags = 0;
1110 ump->um_mountp = mp;
1111 ump->um_dev = dev;
1112 ump->um_devvp = devvp;
1113 ump->um_bptrtodb = fs->lfs_fsbtodb;
1114 ump->um_seqinc = fragstofsb(fs, fs->lfs_frag);
1115 ump->um_nindir = fs->lfs_nindir;
1116 ump->um_lognindir = ffs(fs->lfs_nindir) - 1;
1117 for (i = 0; i < MAXQUOTAS; i++)
1118 ump->um_quotas[i] = NULLVP;
1119 devvp->v_specmountpoint = mp;
1120
1121 /* Set up reserved memory for pageout */
1122 lfs_setup_resblks(fs);
1123 /* Set up vdirop tailq */
1124 TAILQ_INIT(&fs->lfs_dchainhd);
1125 /* and paging tailq */
1126 TAILQ_INIT(&fs->lfs_pchainhd);
1127 #if 0 /* XXXDEBUG */
1128 fs->lfs_lastwrit = dbtofsb(fs, fs->lfs_offset - 1);
1129 #endif
1130
1131 /*
1132 * We use the ifile vnode for almost every operation. Instead of
1133 * retrieving it from the hash table each time we retrieve it here,
1134 * artificially increment the reference count and keep a pointer
1135 * to it in the incore copy of the superblock.
1136 */
1137 if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0) {
1138 #ifdef DEBUG
1139 printf("lfs_mountfs: ifile vget failed, error=%d\n", error);
1140 #endif
1141 goto out;
1142 }
1143 fs->lfs_ivnode = vp;
1144 VREF(vp);
1145
1146 /* Set up segment usage flags for the autocleaner. */
1147 fs->lfs_nactive = 0;
1148 fs->lfs_suflags = (u_int32_t **)malloc(2 * sizeof(u_int32_t *),
1149 M_SEGMENT, M_WAITOK);
1150 fs->lfs_suflags[0] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
1151 M_SEGMENT, M_WAITOK);
1152 fs->lfs_suflags[1] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
1153 M_SEGMENT, M_WAITOK);
1154 memset(fs->lfs_suflags[1], 0, fs->lfs_nseg * sizeof(u_int32_t));
1155 for (i = 0; i < fs->lfs_nseg; i++) {
1156 int changed;
1157
1158 LFS_SEGENTRY(sup, fs, i, bp);
1159 changed = 0;
1160 if (!ronly) {
1161 if (sup->su_nbytes == 0 &&
1162 !(sup->su_flags & SEGUSE_EMPTY)) {
1163 sup->su_flags |= SEGUSE_EMPTY;
1164 ++changed;
1165 } else if (!(sup->su_nbytes == 0) &&
1166 (sup->su_flags & SEGUSE_EMPTY)) {
1167 sup->su_flags &= ~SEGUSE_EMPTY;
1168 ++changed;
1169 }
1170 if (sup->su_flags & SEGUSE_ACTIVE) {
1171 sup->su_flags &= ~SEGUSE_ACTIVE;
1172 ++changed;
1173 }
1174 }
1175 fs->lfs_suflags[0][i] = sup->su_flags;
1176 if (changed)
1177 LFS_WRITESEGENTRY(sup, fs, i, bp);
1178 else
1179 brelse(bp);
1180 }
1181
1182 /*
1183 * Roll forward.
1184 *
1185 * We don't automatically roll forward for v1 filesystems, because
1186 * of the danger that the clock was turned back between the last
1187 * checkpoint and crash. This would roll forward garbage.
1188 *
1189 * v2 filesystems don't have this problem because they use a
1190 * monotonically increasing serial number instead of a timestamp.
1191 */
1192 #ifdef LFS_DO_ROLLFORWARD
1193 do_rollforward = !fs->lfs_ronly;
1194 #else
1195 do_rollforward = (fs->lfs_version > 1 && !fs->lfs_ronly &&
1196 !(fs->lfs_pflags & LFS_PF_CLEAN));
1197 #endif
1198 if (do_rollforward) {
1199 /*
1200 * Phase I: Find the address of the last good partial
1201 * segment that was written after the checkpoint. Mark
1202 * the segments in question dirty, so they won't be
1203 * reallocated.
1204 */
1205 lastgoodpseg = oldoffset = offset = fs->lfs_offset;
1206 flags = 0x0;
1207 #ifdef DEBUG_LFS_RFW
1208 printf("LFS roll forward phase 1: starting at offset 0x%"
1209 PRIx64 "\n", offset);
1210 #endif
1211 LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
1212 if (!(sup->su_flags & SEGUSE_DIRTY))
1213 --fs->lfs_nclean;
1214 sup->su_flags |= SEGUSE_DIRTY;
1215 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, offset), bp);
1216 while ((offset = check_segsum(fs, offset, cred, CHECK_CKSUM,
1217 &flags, p)) > 0)
1218 {
1219 if (sntod(fs, oldoffset) != sntod(fs, offset)) {
1220 LFS_SEGENTRY(sup, fs, dtosn(fs, oldoffset),
1221 bp);
1222 if (!(sup->su_flags & SEGUSE_DIRTY))
1223 --fs->lfs_nclean;
1224 sup->su_flags |= SEGUSE_DIRTY;
1225 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, oldoffset),
1226 bp);
1227 }
1228
1229 #ifdef DEBUG_LFS_RFW
1230 printf("LFS roll forward phase 1: offset=0x%"
1231 PRIx64 "\n", offset);
1232 if (flags & SS_DIROP) {
1233 printf("lfs_mountfs: dirops at 0x%" PRIx64 "\n",
1234 oldoffset);
1235 if (!(flags & SS_CONT))
1236 printf("lfs_mountfs: dirops end "
1237 "at 0x%" PRIx64 "\n", oldoffset);
1238 }
1239 #endif
1240 if (!(flags & SS_CONT))
1241 lastgoodpseg = offset;
1242 oldoffset = offset;
1243 }
1244 #ifdef DEBUG_LFS_RFW
1245 if (flags & SS_CONT) {
1246 printf("LFS roll forward: warning: incomplete "
1247 "dirops discarded\n");
1248 }
1249 printf("LFS roll forward phase 1: completed: "
1250 "lastgoodpseg=0x%" PRIx64 "\n", lastgoodpseg);
1251 #endif
1252 oldoffset = fs->lfs_offset;
1253 if (fs->lfs_offset != lastgoodpseg) {
1254 /* Don't overwrite what we're trying to preserve */
1255 offset = fs->lfs_offset;
1256 fs->lfs_offset = lastgoodpseg;
1257 fs->lfs_curseg = sntod(fs, dtosn(fs, fs->lfs_offset));
1258 for (sn = curseg = dtosn(fs, fs->lfs_curseg);;) {
1259 sn = (sn + 1) % fs->lfs_nseg;
1260 if (sn == curseg)
1261 panic("lfs_mountfs: no clean segments");
1262 LFS_SEGENTRY(sup, fs, sn, bp);
1263 dirty = (sup->su_flags & SEGUSE_DIRTY);
1264 brelse(bp);
1265 if (!dirty)
1266 break;
1267 }
1268 fs->lfs_nextseg = sntod(fs, sn);
1269
1270 /*
1271 * Phase II: Roll forward from the first superblock.
1272 */
1273 while (offset != lastgoodpseg) {
1274 #ifdef DEBUG_LFS_RFW
1275 printf("LFS roll forward phase 2: 0x%"
1276 PRIx64 "\n", offset);
1277 #endif
1278 offset = check_segsum(fs, offset, cred,
1279 CHECK_UPDATE, NULL, p);
1280 }
1281
1282 /*
1283 * Finish: flush our changes to disk.
1284 */
1285 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
1286 printf("lfs_mountfs: roll forward recovered %lld blocks\n",
1287 (long long)(lastgoodpseg - oldoffset));
1288 }
1289 #ifdef DEBUG_LFS_RFW
1290 printf("LFS roll forward complete\n");
1291 #endif
1292 }
1293 /* If writing, sb is not clean; record in case of immediate crash */
1294 if (!fs->lfs_ronly) {
1295 fs->lfs_pflags &= ~LFS_PF_CLEAN;
1296 lfs_writesuper(fs, fs->lfs_sboffs[0]);
1297 lfs_writesuper(fs, fs->lfs_sboffs[1]);
1298 }
1299
1300 /* Allow vget now that roll-forward is complete */
1301 fs->lfs_flags &= ~(LFS_NOTYET);
1302 wakeup(&fs->lfs_flags);
1303
1304 /*
1305 * Initialize the ifile cleaner info with information from
1306 * the superblock.
1307 */
1308 LFS_CLEANERINFO(cip, fs, bp);
1309 cip->clean = fs->lfs_nclean;
1310 cip->dirty = fs->lfs_nseg - fs->lfs_nclean;
1311 cip->avail = fs->lfs_avail;
1312 cip->bfree = fs->lfs_bfree;
1313 (void) LFS_BWRITE_LOG(bp); /* Ifile */
1314
1315 /*
1316 * Mark the current segment as ACTIVE, since we're going to
1317 * be writing to it.
1318 */
1319 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp);
1320 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1321 fs->lfs_nactive++;
1322 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp); /* Ifile */
1323
1324 /* Now that roll-forward is done, unlock the Ifile */
1325 vput(vp);
1326
1327 /* Comment on ifile size if it is too large */
1328 if (fs->lfs_ivnode->v_size / fs->lfs_bsize > LFS_MAX_BUFS) {
1329 fs->lfs_flags |= LFS_WARNED;
1330 printf("lfs_mountfs: please consider increasing NBUF to at least %lld\n",
1331 (long long)(fs->lfs_ivnode->v_size / fs->lfs_bsize) * (nbuf / LFS_MAX_BUFS));
1332 }
1333 if (fs->lfs_ivnode->v_size > LFS_MAX_BYTES) {
1334 fs->lfs_flags |= LFS_WARNED;
1335 printf("lfs_mountfs: please consider increasing BUFPAGES to at least %lld\n",
1336 (long long)fs->lfs_ivnode->v_size * bufpages / LFS_MAX_BYTES);
1337 }
1338
1339 return (0);
1340 out:
1341 if (bp)
1342 brelse(bp);
1343 if (abp)
1344 brelse(abp);
1345 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1346 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
1347 VOP_UNLOCK(devvp, 0);
1348 if (ump) {
1349 free(ump->um_lfs, M_UFSMNT);
1350 free(ump, M_UFSMNT);
1351 mp->mnt_data = NULL;
1352 }
1353
1354 /* Start the pagedaemon-anticipating daemon */
1355 if (lfs_writer_daemon == 0 &&
1356 kthread_create1(lfs_writerd, NULL, NULL, "lfs_writer") != 0)
1357 panic("fork lfs_writer");
1358
1359 return (error);
1360 }
1361
1362 /*
1363 * unmount system call
1364 */
1365 int
1366 lfs_unmount(struct mount *mp, int mntflags, struct proc *p)
1367 {
1368 struct ufsmount *ump;
1369 struct lfs *fs;
1370 int error, flags, ronly;
1371 int s;
1372
1373 flags = 0;
1374 if (mntflags & MNT_FORCE)
1375 flags |= FORCECLOSE;
1376
1377 ump = VFSTOUFS(mp);
1378 fs = ump->um_lfs;
1379
1380 /* wake up the cleaner so it can die */
1381 wakeup(&fs->lfs_nextseg);
1382 wakeup(&lfs_allclean_wakeup);
1383 simple_lock(&fs->lfs_interlock);
1384 while (fs->lfs_sleepers)
1385 ltsleep(&fs->lfs_sleepers, PRIBIO + 1, "lfs_sleepers", 0,
1386 &fs->lfs_interlock);
1387 simple_unlock(&fs->lfs_interlock);
1388
1389 #ifdef QUOTA
1390 if (mp->mnt_flag & MNT_QUOTA) {
1391 int i;
1392 error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
1393 if (error)
1394 return (error);
1395 for (i = 0; i < MAXQUOTAS; i++) {
1396 if (ump->um_quotas[i] == NULLVP)
1397 continue;
1398 quotaoff(p, mp, i);
1399 }
1400 /*
1401 * Here we fall through to vflush again to ensure
1402 * that we have gotten rid of all the system vnodes.
1403 */
1404 }
1405 #endif
1406 if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
1407 return (error);
1408 if ((error = VFS_SYNC(mp, 1, p->p_ucred, p)) != 0)
1409 return (error);
1410 s = splbio();
1411 if (LIST_FIRST(&fs->lfs_ivnode->v_dirtyblkhd))
1412 panic("lfs_unmount: still dirty blocks on ifile vnode");
1413 splx(s);
1414
1415 /* Comment on ifile size if it has become too large */
1416 if (!(fs->lfs_flags & LFS_WARNED)) {
1417 if (fs->lfs_ivnode->v_size / fs->lfs_bsize > LFS_MAX_BUFS)
1418 printf("lfs_unmount: please consider increasing"
1419 " NBUF to at least %lld\n",
1420 (long long)(fs->lfs_ivnode->v_size /
1421 fs->lfs_bsize) *
1422 (long long)(nbuf / LFS_MAX_BUFS));
1423 if (fs->lfs_ivnode->v_size > LFS_MAX_BYTES)
1424 printf("lfs_unmount: please consider increasing"
1425 " BUFPAGES to at least %lld\n",
1426 (long long)fs->lfs_ivnode->v_size *
1427 bufpages / LFS_MAX_BYTES);
1428 }
1429
1430 /* Explicitly write the superblock, to update serial and pflags */
1431 fs->lfs_pflags |= LFS_PF_CLEAN;
1432 lfs_writesuper(fs, fs->lfs_sboffs[0]);
1433 lfs_writesuper(fs, fs->lfs_sboffs[1]);
1434 while (fs->lfs_iocount)
1435 tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0);
1436
1437 /* Finish with the Ifile, now that we're done with it */
1438 vrele(fs->lfs_ivnode);
1439 vgone(fs->lfs_ivnode);
1440
1441 ronly = !fs->lfs_ronly;
1442 if (ump->um_devvp->v_type != VBAD)
1443 ump->um_devvp->v_specmountpoint = NULL;
1444 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1445 error = VOP_CLOSE(ump->um_devvp,
1446 ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
1447 vput(ump->um_devvp);
1448
1449 /* Free per-mount data structures */
1450 free(fs->lfs_suflags[0], M_SEGMENT);
1451 free(fs->lfs_suflags[1], M_SEGMENT);
1452 free(fs->lfs_suflags, M_SEGMENT);
1453 lfs_free_resblks(fs);
1454 free(fs, M_UFSMNT);
1455 free(ump, M_UFSMNT);
1456
1457 mp->mnt_data = NULL;
1458 mp->mnt_flag &= ~MNT_LOCAL;
1459 return (error);
1460 }
1461
1462 /*
1463 * Get file system statistics.
1464 */
1465 int
1466 lfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
1467 {
1468 struct lfs *fs;
1469 struct ufsmount *ump;
1470
1471 ump = VFSTOUFS(mp);
1472 fs = ump->um_lfs;
1473 if (fs->lfs_magic != LFS_MAGIC)
1474 panic("lfs_statfs: magic");
1475
1476 sbp->f_type = 0;
1477 sbp->f_bsize = fs->lfs_fsize;
1478 sbp->f_iosize = fs->lfs_bsize;
1479 sbp->f_blocks = fsbtofrags(fs, LFS_EST_NONMETA(fs));
1480 sbp->f_bfree = fsbtofrags(fs, LFS_EST_BFREE(fs));
1481 sbp->f_bavail = fsbtofrags(fs, (long)LFS_EST_BFREE(fs) -
1482 (long)LFS_EST_RSVD(fs));
1483
1484 sbp->f_files = fs->lfs_bfree / btofsb(fs, fs->lfs_ibsize) * INOPB(fs);
1485 sbp->f_ffree = sbp->f_files - fs->lfs_nfiles;
1486 if (sbp != &mp->mnt_stat) {
1487 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
1488 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
1489 }
1490 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
1491 return (0);
1492 }
1493
1494 /*
1495 * Go through the disk queues to initiate sandbagged IO;
1496 * go through the inodes to write those that have been modified;
1497 * initiate the writing of the super block if it has been modified.
1498 *
1499 * Note: we are always called with the filesystem marked `MPBUSY'.
1500 */
1501 int
1502 lfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
1503 {
1504 int error;
1505 struct lfs *fs;
1506
1507 fs = ((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs;
1508 if (fs->lfs_ronly)
1509 return 0;
1510 while (fs->lfs_dirops)
1511 error = tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs_dirops", 0);
1512 fs->lfs_writer++;
1513
1514 /* All syncs must be checkpoints until roll-forward is implemented. */
1515 error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
1516 if (--fs->lfs_writer == 0)
1517 wakeup(&fs->lfs_dirops);
1518 #ifdef QUOTA
1519 qsync(mp);
1520 #endif
1521 return (error);
1522 }
1523
1524 extern struct lock ufs_hashlock;
1525
1526 /*
1527 * Look up an LFS dinode number to find its incore vnode. If not already
1528 * in core, read it in from the specified device. Return the inode locked.
1529 * Detection and handling of mount points must be done by the calling routine.
1530 */
1531 int
1532 lfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1533 {
1534 struct lfs *fs;
1535 struct ufs1_dinode *dip;
1536 struct inode *ip;
1537 struct buf *bp;
1538 struct ifile *ifp;
1539 struct vnode *vp;
1540 struct ufsmount *ump;
1541 daddr_t daddr;
1542 dev_t dev;
1543 int error, retries;
1544 struct timespec ts;
1545
1546 ump = VFSTOUFS(mp);
1547 dev = ump->um_dev;
1548 fs = ump->um_lfs;
1549
1550 /*
1551 * If the filesystem is not completely mounted yet, suspend
1552 * any access requests (wait for roll-forward to complete).
1553 */
1554 while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
1555 tsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0);
1556
1557 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1558 return (0);
1559
1560 if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
1561 *vpp = NULL;
1562 return (error);
1563 }
1564
1565 do {
1566 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
1567 ungetnewvnode(vp);
1568 return (0);
1569 }
1570 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
1571
1572 /* Translate the inode number to a disk address. */
1573 if (ino == LFS_IFILE_INUM)
1574 daddr = fs->lfs_idaddr;
1575 else {
1576 /* XXX bounds-check this too */
1577 LFS_IENTRY(ifp, fs, ino, bp);
1578 daddr = ifp->if_daddr;
1579 if (fs->lfs_version > 1) {
1580 ts.tv_sec = ifp->if_atime_sec;
1581 ts.tv_nsec = ifp->if_atime_nsec;
1582 }
1583
1584 brelse(bp);
1585 if (daddr == LFS_UNUSED_DADDR) {
1586 *vpp = NULLVP;
1587 ungetnewvnode(vp);
1588 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1589 return (ENOENT);
1590 }
1591 }
1592
1593 /* Allocate/init new vnode/inode. */
1594 lfs_vcreate(mp, ino, vp);
1595
1596 /*
1597 * Put it onto its hash chain and lock it so that other requests for
1598 * this inode will block if they arrive while we are sleeping waiting
1599 * for old data structures to be purged or for the contents of the
1600 * disk portion of this inode to be read.
1601 */
1602 ip = VTOI(vp);
1603 ufs_ihashins(ip);
1604 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1605
1606 /*
1607 * XXX
1608 * This may not need to be here, logically it should go down with
1609 * the i_devvp initialization.
1610 * Ask Kirk.
1611 */
1612 ip->i_lfs = ump->um_lfs;
1613
1614 /* Read in the disk contents for the inode, copy into the inode. */
1615 retries = 0;
1616 again:
1617 error = bread(ump->um_devvp, fsbtodb(fs, daddr),
1618 (fs->lfs_version == 1 ? fs->lfs_bsize : fs->lfs_ibsize),
1619 NOCRED, &bp);
1620 if (error) {
1621 /*
1622 * The inode does not contain anything useful, so it would
1623 * be misleading to leave it on its hash chain. With mode
1624 * still zero, it will be unlinked and returned to the free
1625 * list by vput().
1626 */
1627 vput(vp);
1628 brelse(bp);
1629 *vpp = NULL;
1630 return (error);
1631 }
1632
1633 dip = lfs_ifind(fs, ino, bp);
1634 if (dip == NULL) {
1635 /* Assume write has not completed yet; try again */
1636 bp->b_flags |= B_INVAL;
1637 brelse(bp);
1638 ++retries;
1639 if (retries > LFS_IFIND_RETRIES) {
1640 #ifdef DEBUG
1641 /* If the seglock is held look at the bpp to see
1642 what is there anyway */
1643 if (fs->lfs_seglock > 0) {
1644 struct buf **bpp;
1645 struct ufs1_dinode *dp;
1646 int i;
1647
1648 for (bpp = fs->lfs_sp->bpp;
1649 bpp != fs->lfs_sp->cbpp; ++bpp) {
1650 if ((*bpp)->b_vp == fs->lfs_ivnode &&
1651 bpp != fs->lfs_sp->bpp) {
1652 /* Inode block */
1653 printf("block 0x%" PRIx64 ": ",
1654 (*bpp)->b_blkno);
1655 dp = (struct ufs1_dinode *)(*bpp)->b_data;
1656 for (i = 0; i < INOPB(fs); i++)
1657 if (dp[i].di_u.inumber)
1658 printf("%d ", dp[i].di_u.inumber);
1659 printf("\n");
1660 }
1661 }
1662 }
1663 #endif
1664 panic("lfs_vget: dinode not found");
1665 }
1666 printf("lfs_vget: dinode %d not found, retrying...\n", ino);
1667 (void)tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs ifind", 1);
1668 goto again;
1669 }
1670 *ip->i_din.ffs1_din = *dip;
1671 brelse(bp);
1672
1673 if (fs->lfs_version > 1) {
1674 ip->i_ffs1_atime = ts.tv_sec;
1675 ip->i_ffs1_atimensec = ts.tv_nsec;
1676 }
1677
1678 lfs_vinit(mp, vp);
1679
1680 *vpp = vp;
1681
1682 KASSERT(VOP_ISLOCKED(vp));
1683
1684 return (0);
1685 }
1686
1687 /*
1688 * File handle to vnode
1689 *
1690 * Have to be really careful about stale file handles:
1691 * - check that the inode number is valid
1692 * - call lfs_vget() to get the locked inode
1693 * - check for an unallocated inode (i_mode == 0)
1694 *
1695 * XXX
1696 * use ifile to see if inode is allocated instead of reading off disk
1697 * what is the relationship between my generational number and the NFS
1698 * generational number.
1699 */
1700 int
1701 lfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1702 {
1703 struct ufid *ufhp;
1704
1705 ufhp = (struct ufid *)fhp;
1706 if (ufhp->ufid_ino < ROOTINO)
1707 return (ESTALE);
1708 return (ufs_fhtovp(mp, ufhp, vpp));
1709 }
1710
1711 /*
1712 * Vnode pointer to File handle
1713 */
1714 /* ARGSUSED */
1715 int
1716 lfs_vptofh(struct vnode *vp, struct fid *fhp)
1717 {
1718 struct inode *ip;
1719 struct ufid *ufhp;
1720
1721 ip = VTOI(vp);
1722 ufhp = (struct ufid *)fhp;
1723 ufhp->ufid_len = sizeof(struct ufid);
1724 ufhp->ufid_ino = ip->i_number;
1725 ufhp->ufid_gen = ip->i_gen;
1726 return (0);
1727 }
1728
1729 int
1730 lfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen, struct proc *p)
1731 {
1732 extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead;
1733 extern struct lfs_stats lfs_stats;
1734 int error;
1735
1736 /* all sysctl names at this level are terminal */
1737 if (namelen != 1)
1738 return (ENOTDIR);
1739
1740 switch (name[0]) {
1741 case LFS_WRITEINDIR:
1742 return (sysctl_int(oldp, oldlenp, newp, newlen,
1743 &lfs_writeindir));
1744 case LFS_CLEAN_VNHEAD:
1745 return (sysctl_int(oldp, oldlenp, newp, newlen,
1746 &lfs_clean_vnhead));
1747 case LFS_DOSTATS:
1748 if ((error = sysctl_int(oldp, oldlenp, newp, newlen,
1749 &lfs_dostats)))
1750 return error;
1751 if (lfs_dostats == 0)
1752 memset(&lfs_stats,0,sizeof(lfs_stats));
1753 return 0;
1754 default:
1755 return (EOPNOTSUPP);
1756 }
1757 /* NOTREACHED */
1758 }
1759
1760 /*
1761 * lfs_gop_write functions exactly like genfs_gop_write, except that
1762 * (1) it requires the seglock to be held by its caller, and sp->fip
1763 * to be properly initialized (it will return without re-initializing
1764 * sp->fip, and without calling lfs_writeseg).
1765 * (2) it uses the remaining space in the segment, rather than VOP_BMAP,
1766 * to determine how large a block it can write at once (though it does
1767 * still use VOP_BMAP to find holes in the file);
1768 * (3) it calls lfs_gatherblock instead of VOP_STRATEGY on its blocks
1769 * (leaving lfs_writeseg to deal with the cluster blocks, so we might
1770 * now have clusters of clusters, ick.)
1771 */
1772 static int
1773 lfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1774 {
1775 int i, s, error, run;
1776 int fs_bshift, dev_bshift;
1777 vaddr_t kva;
1778 off_t eof, offset, startoffset;
1779 size_t bytes, iobytes, skipbytes;
1780 daddr_t lbn, blkno;
1781 struct vm_page *pg;
1782 struct buf *mbp, *bp;
1783 struct vnode *devvp;
1784 struct inode *ip = VTOI(vp);
1785 struct lfs *fs = ip->i_lfs;
1786 struct segment *sp = fs->lfs_sp;
1787 UVMHIST_FUNC("lfs_gop_write"); UVMHIST_CALLED(ubchist);
1788
1789 /* The Ifile lives in the buffer cache */
1790 if (vp == fs->lfs_ivnode)
1791 return genfs_compat_gop_write(vp, pgs, npages, flags);
1792
1793 /*
1794 * Sometimes things slip past the filters in lfs_putpages,
1795 * and the pagedaemon tries to write pages---problem is
1796 * that the pagedaemon never acquires the segment lock.
1797 *
1798 * Unbusy and unclean the pages, and put them on the ACTIVE
1799 * queue under the hypothesis that they couldn't have got here
1800 * unless they were modified *quite* recently.
1801 *
1802 * XXXUBC that last statement is an oversimplification of course.
1803 */
1804 if (!(fs->lfs_seglock) || fs->lfs_lockpid != curproc->p_pid) {
1805 simple_lock(&vp->v_interlock);
1806 #ifdef DEBUG
1807 printf("lfs_gop_write: seglock not held\n");
1808 #endif
1809 uvm_lock_pageq();
1810 for (i = 0; i < npages; i++) {
1811 pg = pgs[i];
1812
1813 if (pg->flags & PG_PAGEOUT)
1814 uvmexp.paging--;
1815 if (pg->flags & PG_DELWRI) {
1816 uvm_pageunwire(pg);
1817 }
1818 uvm_pageactivate(pg);
1819 pg->flags &= ~(PG_CLEAN|PG_DELWRI|PG_PAGEOUT|PG_RELEASED);
1820 #ifdef DEBUG_LFS
1821 printf("pg[%d]->flags = %x\n", i, pg->flags);
1822 printf("pg[%d]->pqflags = %x\n", i, pg->pqflags);
1823 printf("pg[%d]->uanon = %p\n", i, pg->uanon);
1824 printf("pg[%d]->uobject = %p\n", i, pg->uobject);
1825 printf("pg[%d]->wire_count = %d\n", i, pg->wire_count);
1826 printf("pg[%d]->loan_count = %d\n", i, pg->loan_count);
1827 #endif
1828 }
1829 /* uvm_pageunbusy takes care of PG_BUSY, PG_WANTED */
1830 uvm_page_unbusy(pgs, npages);
1831 uvm_unlock_pageq();
1832 simple_unlock(&vp->v_interlock);
1833 return EAGAIN;
1834 }
1835
1836 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1837 vp, pgs, npages, flags);
1838
1839 GOP_SIZE(vp, vp->v_size, &eof, GOP_SIZE_WRITE);
1840
1841 if (vp->v_type == VREG) {
1842 fs_bshift = vp->v_mount->mnt_fs_bshift;
1843 dev_bshift = vp->v_mount->mnt_dev_bshift;
1844 } else {
1845 fs_bshift = DEV_BSHIFT;
1846 dev_bshift = DEV_BSHIFT;
1847 }
1848 error = 0;
1849 pg = pgs[0];
1850 startoffset = pg->offset;
1851 bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
1852 skipbytes = 0;
1853
1854 KASSERT(bytes != 0);
1855
1856 /* Swap PG_DELWRI for PG_PAGEOUT */
1857 for (i = 0; i < npages; i++)
1858 if (pgs[i]->flags & PG_DELWRI) {
1859 KASSERT(!(pgs[i]->flags & PG_PAGEOUT));
1860 pgs[i]->flags &= ~PG_DELWRI;
1861 pgs[i]->flags |= PG_PAGEOUT;
1862 uvmexp.paging++;
1863 uvm_lock_pageq();
1864 uvm_pageunwire(pgs[i]);
1865 uvm_unlock_pageq();
1866 }
1867
1868 /*
1869 * Check to make sure we're starting on a block boundary.
1870 * We'll check later to make sure we always write entire
1871 * blocks (or fragments).
1872 */
1873 if (startoffset & fs->lfs_bmask)
1874 printf("%" PRId64 " & %" PRId64 " = %" PRId64 "\n",
1875 startoffset, fs->lfs_bmask,
1876 startoffset & fs->lfs_bmask);
1877 KASSERT((startoffset & fs->lfs_bmask) == 0);
1878 if (bytes & fs->lfs_ffmask) {
1879 printf("lfs_gop_write: asked to write %ld bytes\n", (long)bytes);
1880 panic("lfs_gop_write: non-integer blocks");
1881 }
1882
1883 kva = uvm_pagermapin(pgs, npages,
1884 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1885
1886 s = splbio();
1887 simple_lock(&global_v_numoutput_slock);
1888 vp->v_numoutput += 2; /* one for biodone, one for aiodone */
1889 simple_unlock(&global_v_numoutput_slock);
1890 mbp = pool_get(&bufpool, PR_WAITOK);
1891 splx(s);
1892
1893 memset(mbp, 0, sizeof(*bp));
1894 BUF_INIT(mbp);
1895 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
1896 vp, mbp, vp->v_numoutput, bytes);
1897 mbp->b_bufsize = npages << PAGE_SHIFT;
1898 mbp->b_data = (void *)kva;
1899 mbp->b_resid = mbp->b_bcount = bytes;
1900 mbp->b_flags = B_BUSY|B_WRITE|B_AGE|B_CALL;
1901 mbp->b_iodone = uvm_aio_biodone;
1902 mbp->b_vp = vp;
1903
1904 bp = NULL;
1905 for (offset = startoffset;
1906 bytes > 0;
1907 offset += iobytes, bytes -= iobytes) {
1908 lbn = offset >> fs_bshift;
1909 error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
1910 if (error) {
1911 UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
1912 skipbytes += bytes;
1913 bytes = 0;
1914 break;
1915 }
1916
1917 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
1918 bytes);
1919 if (blkno == (daddr_t)-1) {
1920 skipbytes += iobytes;
1921 continue;
1922 }
1923
1924 /*
1925 * Discover how much we can really pack into this buffer.
1926 */
1927 /* If no room in the current segment, finish it up */
1928 if (sp->sum_bytes_left < sizeof(int32_t) ||
1929 sp->seg_bytes_left < (1 << fs->lfs_bshift)) {
1930 int version;
1931
1932 lfs_updatemeta(sp);
1933
1934 version = sp->fip->fi_version;
1935 (void) lfs_writeseg(fs, sp);
1936
1937 sp->fip->fi_version = version;
1938 sp->fip->fi_ino = ip->i_number;
1939 /* Add the current file to the segment summary. */
1940 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
1941 sp->sum_bytes_left -= FINFOSIZE;
1942 }
1943 /* Check both for space in segment and space in segsum */
1944 iobytes = MIN(iobytes, (sp->seg_bytes_left >> fs_bshift)
1945 << fs_bshift);
1946 iobytes = MIN(iobytes, (sp->sum_bytes_left / sizeof(int32_t))
1947 << fs_bshift);
1948 KASSERT(iobytes > 0);
1949
1950 /* if it's really one i/o, don't make a second buf */
1951 if (offset == startoffset && iobytes == bytes) {
1952 bp = mbp;
1953 /* printf("bp is mbp\n"); */
1954 /* correct overcount if there is no second buffer */
1955 s = splbio();
1956 simple_lock(&global_v_numoutput_slock);
1957 --vp->v_numoutput;
1958 simple_unlock(&global_v_numoutput_slock);
1959 splx(s);
1960 } else {
1961 /* printf("bp is not mbp\n"); */
1962 s = splbio();
1963 bp = pool_get(&bufpool, PR_WAITOK);
1964 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1965 vp, bp, vp->v_numoutput, 0);
1966 splx(s);
1967 memset(bp, 0, sizeof(*bp));
1968 BUF_INIT(bp);
1969 bp->b_data = (char *)kva +
1970 (vaddr_t)(offset - pg->offset);
1971 bp->b_resid = bp->b_bcount = iobytes;
1972 bp->b_flags = B_BUSY|B_WRITE|B_CALL;
1973 bp->b_iodone = uvm_aio_biodone1;
1974 }
1975
1976 /* XXX This is silly ... is this necessary? */
1977 bp->b_vp = NULL;
1978 s = splbio();
1979 bgetvp(vp, bp);
1980 splx(s);
1981
1982 bp->b_lblkno = lblkno(fs, offset);
1983 bp->b_private = mbp;
1984 if (devvp->v_type == VBLK) {
1985 bp->b_dev = devvp->v_rdev;
1986 }
1987 VOP_BWRITE(bp);
1988 while (lfs_gatherblock(sp, bp, NULL))
1989 continue;
1990 }
1991
1992 if (skipbytes) {
1993 UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
1994 s = splbio();
1995 if (error) {
1996 mbp->b_flags |= B_ERROR;
1997 mbp->b_error = error;
1998 }
1999 mbp->b_resid -= skipbytes;
2000 if (mbp->b_resid == 0) {
2001 biodone(mbp);
2002 }
2003 splx(s);
2004 }
2005 UVMHIST_LOG(ubchist, "returning 0", 0,0,0,0);
2006 return (0);
2007 }
2008
2009 /*
2010 * finish vnode/inode initialization.
2011 * used by lfs_vget and lfs_fastvget.
2012 */
2013 void
2014 lfs_vinit(struct mount *mp, struct vnode *vp)
2015 {
2016 struct inode *ip = VTOI(vp);
2017 struct ufsmount *ump = VFSTOUFS(mp);
2018 int i;
2019
2020 ip->i_mode = ip->i_ffs1_mode;
2021 ip->i_ffs_effnlink = ip->i_nlink = ip->i_ffs1_nlink;
2022 ip->i_lfs_osize = ip->i_size = ip->i_ffs1_size;
2023 ip->i_flags = ip->i_ffs1_flags;
2024 ip->i_gen = ip->i_ffs1_gen;
2025 ip->i_uid = ip->i_ffs1_uid;
2026 ip->i_gid = ip->i_ffs1_gid;
2027
2028 ip->i_lfs_effnblks = ip->i_ffs1_blocks;
2029
2030 /*
2031 * Initialize the vnode from the inode, check for aliases. In all
2032 * cases re-init ip, the underlying vnode/inode may have changed.
2033 */
2034 ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
2035
2036 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
2037 if (vp->v_type != VLNK ||
2038 VTOI(vp)->i_size >= vp->v_mount->mnt_maxsymlinklen) {
2039 struct lfs *fs = ump->um_lfs;
2040 #ifdef DEBUG
2041 for (i = (ip->i_size + fs->lfs_bsize - 1) >> fs->lfs_bshift;
2042 i < NDADDR; i++) {
2043 if (ip->i_ffs1_db[i] != 0) {
2044 inconsistent:
2045 lfs_dump_dinode(ip->i_din.ffs1_din);
2046 panic("inconsistent inode");
2047 }
2048 }
2049 for ( ; i < NDADDR + NIADDR; i++) {
2050 if (ip->i_ffs1_ib[i - NDADDR] != 0) {
2051 goto inconsistent;
2052 }
2053 }
2054 #endif /* DEBUG */
2055 for (i = 0; i < NDADDR; i++)
2056 if (ip->i_ffs1_db[i] != 0)
2057 ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
2058 }
2059
2060 #ifdef DEBUG
2061 if (vp->v_type == VNON) {
2062 printf("lfs_vinit: ino %d is type VNON! (ifmt=%o)\n",
2063 ip->i_number, (ip->i_mode & IFMT) >> 12);
2064 lfs_dump_dinode(ip->i_din.ffs1_din);
2065 #ifdef DDB
2066 Debugger();
2067 #endif /* DDB */
2068 }
2069 #endif /* DEBUG */
2070
2071 /*
2072 * Finish inode initialization now that aliasing has been resolved.
2073 */
2074
2075 ip->i_devvp = ump->um_devvp;
2076 VREF(ip->i_devvp);
2077 genfs_node_init(vp, &lfs_genfsops);
2078 uvm_vnp_setsize(vp, ip->i_size);
2079 }
2080