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