lfs_vfsops.c revision 1.188 1 /* $NetBSD: lfs_vfsops.c,v 1.188 2005/09/27 06:48:56 yamt 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.188 2005/09/27 06:48:56 yamt 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 void warn_ifile_size(struct lfs *);
123 static daddr_t check_segsum(struct lfs *, daddr_t, u_int64_t,
124 struct ucred *, int, int *, struct proc *);
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 int lfs_do_rfw = 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_statvfs,
149 lfs_sync,
150 lfs_vget,
151 lfs_fhtovp,
152 lfs_vptofh,
153 lfs_init,
154 lfs_reinit,
155 lfs_done,
156 lfs_mountroot,
157 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
158 vfs_stdextattrctl,
159 lfs_vnodeopv_descs,
160 };
161 VFS_ATTACH(lfs_vfsops);
162
163 const struct genfs_ops lfs_genfsops = {
164 .gop_size = lfs_gop_size,
165 .gop_alloc = ufs_gop_alloc,
166 .gop_write = lfs_gop_write,
167 .gop_markupdate = ufs_gop_markupdate,
168 };
169
170 static const struct ufs_ops lfs_ufsops = {
171 .uo_itimes = NULL,
172 };
173
174 /*
175 * XXX Same structure as FFS inodes? Should we share a common pool?
176 */
177 POOL_INIT(lfs_inode_pool, sizeof(struct inode), 0, 0, 0, "lfsinopl",
178 &pool_allocator_nointr);
179 POOL_INIT(lfs_dinode_pool, sizeof(struct ufs1_dinode), 0, 0, 0, "lfsdinopl",
180 &pool_allocator_nointr);
181 POOL_INIT(lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0, "lfsinoextpl",
182 &pool_allocator_nointr);
183 POOL_INIT(lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0, "lfslbnpool",
184 &pool_allocator_nointr);
185
186 /*
187 * The writer daemon. UVM keeps track of how many dirty pages we are holding
188 * in lfs_subsys_pages; the daemon flushes the filesystem when this value
189 * crosses the (user-defined) threshhold LFS_MAX_PAGES.
190 */
191 static void
192 lfs_writerd(void *arg)
193 {
194 struct mount *mp, *nmp;
195 struct lfs *fs;
196 int loopcount;
197
198 lfs_writer_daemon = curproc->p_pid;
199
200 simple_lock(&lfs_subsys_lock);
201 for (;;) {
202 ltsleep(&lfs_writer_daemon, PVM | PNORELOCK, "lfswriter", hz/10,
203 &lfs_subsys_lock);
204
205 /*
206 * Look through the list of LFSs to see if any of them
207 * have requested pageouts.
208 */
209 simple_lock(&mountlist_slock);
210 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
211 mp = nmp) {
212 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
213 nmp = CIRCLEQ_NEXT(mp, mnt_list);
214 continue;
215 }
216 if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS,
217 MFSNAMELEN) == 0) {
218 fs = VFSTOUFS(mp)->um_lfs;
219 simple_lock(&fs->lfs_interlock);
220 if (fs->lfs_pdflush ||
221 !TAILQ_EMPTY(&fs->lfs_pchainhd)) {
222 DLOG((DLOG_FLUSH, "lfs_writerd: pdflush set\n"));
223 fs->lfs_pdflush = 0;
224 lfs_flush_fs(fs, 0);
225 }
226 simple_unlock(&fs->lfs_interlock);
227 }
228
229 simple_lock(&mountlist_slock);
230 nmp = CIRCLEQ_NEXT(mp, mnt_list);
231 vfs_unbusy(mp);
232 }
233 simple_unlock(&mountlist_slock);
234
235 /*
236 * If global state wants a flush, flush everything.
237 */
238 simple_lock(&lfs_subsys_lock);
239 loopcount = 0;
240 if (lfs_do_flush || locked_queue_count > LFS_MAX_BUFS ||
241 locked_queue_bytes > LFS_MAX_BYTES ||
242 lfs_subsys_pages > LFS_MAX_PAGES) {
243
244 if (lfs_do_flush)
245 DLOG((DLOG_FLUSH, "daemon: lfs_do_flush\n"));
246 if (locked_queue_count > LFS_MAX_BUFS)
247 DLOG((DLOG_FLUSH, "daemon: lqc = %d, max %d\n",
248 locked_queue_count, LFS_MAX_BUFS));
249 if (locked_queue_bytes > LFS_MAX_BYTES)
250 DLOG((DLOG_FLUSH, "daemon: lqb = %ld, max %ld\n",
251 locked_queue_bytes, LFS_MAX_BYTES));
252 if (lfs_subsys_pages > LFS_MAX_PAGES)
253 DLOG((DLOG_FLUSH, "daemon: lssp = %d, max %d\n",
254 lfs_subsys_pages, LFS_MAX_PAGES));
255
256 lfs_flush(NULL, SEGM_WRITERD, 0);
257 lfs_do_flush = 0;
258 }
259 }
260 /* NOTREACHED */
261 }
262
263 /*
264 * Initialize the filesystem, most work done by ufs_init.
265 */
266 void
267 lfs_init()
268 {
269 #ifdef _LKM
270 malloc_type_attach(M_SEGMENT);
271 pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
272 "lfsinopl", &pool_allocator_nointr);
273 pool_init(&lfs_dinode_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
274 "lfsdinopl", &pool_allocator_nointr);
275 pool_init(&lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0,
276 "lfsinoextpl", &pool_allocator_nointr);
277 pool_init(&lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0,
278 "lfslbnpool", &pool_allocator_nointr);
279 #endif
280 ufs_init();
281
282 #ifdef DEBUG
283 memset(lfs_log, 0, sizeof(lfs_log));
284 #endif
285 simple_lock_init(&lfs_subsys_lock);
286 }
287
288 void
289 lfs_reinit()
290 {
291 ufs_reinit();
292 }
293
294 void
295 lfs_done()
296 {
297 ufs_done();
298 #ifdef _LKM
299 pool_destroy(&lfs_inode_pool);
300 pool_destroy(&lfs_dinode_pool);
301 pool_destroy(&lfs_inoext_pool);
302 pool_destroy(&lfs_lbnentry_pool);
303 malloc_type_detach(M_SEGMENT);
304 #endif
305 }
306
307 /*
308 * Called by main() when ufs is going to be mounted as root.
309 */
310 int
311 lfs_mountroot()
312 {
313 extern struct vnode *rootvp;
314 struct mount *mp;
315 struct proc *p = curproc; /* XXX */
316 int error;
317
318 if (root_device->dv_class != DV_DISK)
319 return (ENODEV);
320
321 if (rootdev == NODEV)
322 return (ENODEV);
323 if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
324 vrele(rootvp);
325 return (error);
326 }
327 if ((error = lfs_mountfs(rootvp, mp, p))) {
328 mp->mnt_op->vfs_refcount--;
329 vfs_unbusy(mp);
330 free(mp, M_MOUNT);
331 return (error);
332 }
333 simple_lock(&mountlist_slock);
334 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
335 simple_unlock(&mountlist_slock);
336 (void)lfs_statvfs(mp, &mp->mnt_stat, p);
337 vfs_unbusy(mp);
338 setrootfstime((time_t)(VFSTOUFS(mp)->um_lfs->lfs_tstamp));
339 return (0);
340 }
341
342 /*
343 * VFS Operations.
344 *
345 * mount system call
346 */
347 int
348 lfs_mount(struct mount *mp, const char *path, void *data, struct nameidata *ndp, struct proc *p)
349 {
350 struct vnode *devvp;
351 struct ufs_args args;
352 struct ufsmount *ump = NULL;
353 struct lfs *fs = NULL; /* LFS */
354 int error, update;
355 mode_t accessmode;
356
357 if (mp->mnt_flag & MNT_GETARGS) {
358 ump = VFSTOUFS(mp);
359 if (ump == NULL)
360 return EIO;
361 args.fspec = NULL;
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 update = mp->mnt_flag & MNT_UPDATE;
369
370 /* Check arguments */
371 if (args.fspec != NULL) {
372 /*
373 * Look up the name and verify that it's sane.
374 */
375 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
376 if ((error = namei(ndp)) != 0)
377 return (error);
378 devvp = ndp->ni_vp;
379
380 if (!update) {
381 /*
382 * Be sure this is a valid block device
383 */
384 if (devvp->v_type != VBLK)
385 error = ENOTBLK;
386 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
387 error = ENXIO;
388 } else {
389 /*
390 * Be sure we're still naming the same device
391 * used for our initial mount
392 */
393 ump = VFSTOUFS(mp);
394 if (devvp != ump->um_devvp)
395 error = EINVAL;
396 }
397 } else {
398 if (!update) {
399 /* New mounts must have a filename for the device */
400 return (EINVAL);
401 } else {
402 /* Use the extant mount */
403 ump = VFSTOUFS(mp);
404 devvp = ump->um_devvp;
405 vref(devvp);
406 }
407 }
408
409
410 /*
411 * If mount by non-root, then verify that user has necessary
412 * permissions on the device.
413 */
414 if (error == 0 && p->p_ucred->cr_uid != 0) {
415 accessmode = VREAD;
416 if (update ?
417 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
418 (mp->mnt_flag & MNT_RDONLY) == 0)
419 accessmode |= VWRITE;
420 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
421 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
422 VOP_UNLOCK(devvp, 0);
423 }
424
425 if (error) {
426 vrele(devvp);
427 return (error);
428 }
429
430 if (!update) {
431 int flags;
432
433 /*
434 * Disallow multiple mounts of the same device.
435 * Disallow mounting of a device that is currently in use
436 * (except for root, which might share swap device for
437 * miniroot).
438 */
439 error = vfs_mountedon(devvp);
440 if (error)
441 goto fail;
442 if (vcount(devvp) > 1 && devvp != rootvp) {
443 error = EBUSY;
444 goto fail;
445 }
446 if (mp->mnt_flag & MNT_RDONLY)
447 flags = FREAD;
448 else
449 flags = FREAD|FWRITE;
450 error = VOP_OPEN(devvp, flags, FSCRED, p);
451 if (error)
452 goto fail;
453 error = lfs_mountfs(devvp, mp, p); /* LFS */
454 if (error) {
455 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
456 (void)VOP_CLOSE(devvp, flags, NOCRED, p);
457 VOP_UNLOCK(devvp, 0);
458 goto fail;
459 }
460
461 ump = VFSTOUFS(mp);
462 fs = ump->um_lfs;
463 } else {
464 /*
465 * Update the mount.
466 */
467
468 /*
469 * The initial mount got a reference on this
470 * device, so drop the one obtained via
471 * namei(), above.
472 */
473 vrele(devvp);
474
475 ump = VFSTOUFS(mp);
476 fs = ump->um_lfs;
477 if (fs->lfs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
478 /*
479 * Changing from read-only to read/write
480 */
481 fs->lfs_ronly = 0;
482 }
483 if (args.fspec == NULL)
484 return EINVAL;
485 }
486
487 error = set_statvfs_info(path, UIO_USERSPACE, args.fspec,
488 UIO_USERSPACE, mp, p);
489 if (error == 0)
490 (void)strncpy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname,
491 sizeof(fs->lfs_fsmnt));
492 return error;
493
494 fail:
495 vrele(devvp);
496 return (error);
497 }
498
499 /*
500 * Roll-forward code.
501 */
502
503 /*
504 * Load the appropriate indirect block, and change the appropriate pointer.
505 * Mark the block dirty. Do segment and avail accounting.
506 */
507 static int
508 update_meta(struct lfs *fs, ino_t ino, int vers, daddr_t lbn,
509 daddr_t ndaddr, size_t size, struct proc *p)
510 {
511 int error;
512 struct vnode *vp;
513 struct inode *ip;
514 #ifdef DEBUG
515 daddr_t odaddr;
516 struct indir a[NIADDR];
517 int num;
518 int i;
519 #endif /* DEBUG */
520 struct buf *bp;
521 SEGUSE *sup;
522
523 KASSERT(lbn >= 0); /* no indirect blocks */
524
525 if ((error = lfs_rf_valloc(fs, ino, vers, p, &vp)) != 0) {
526 DLOG((DLOG_RF, "update_meta: ino %d: lfs_rf_valloc"
527 " returned %d\n", ino, error));
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
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 DLOG((DLOG_RF, "update_meta: failed setting ino %d lbn %"
590 PRId64 " to %" PRId64 "\n", ino, lbn, ndaddr));
591 #endif /* DEBUG */
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 DLOG((DLOG_RF, "update_inoblk: bread returned %d\n", error));
618 return error;
619 }
620 dip = ((struct ufs1_dinode *)(dbp->b_data)) + INOPB(fs);
621 while (--dip >= (struct ufs1_dinode *)dbp->b_data) {
622 if (dip->di_inumber > LFS_IFILE_INUM) {
623 error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen,
624 p, &vp);
625 if (error) {
626 DLOG((DLOG_RF, "update_inoblk: lfs_rf_valloc"
627 " returned %d\n", error));
628 continue;
629 }
630 ip = VTOI(vp);
631 if (dip->di_size != ip->i_size)
632 VOP_TRUNCATE(vp, dip->di_size, 0, NOCRED, p);
633 /* Get mode, link count, size, and times */
634 memcpy(ip->i_din.ffs1_din, dip,
635 offsetof(struct ufs1_dinode, di_db[0]));
636
637 /* Then the rest, except di_blocks */
638 ip->i_flags = ip->i_ffs1_flags = dip->di_flags;
639 ip->i_gen = ip->i_ffs1_gen = dip->di_gen;
640 ip->i_uid = ip->i_ffs1_uid = dip->di_uid;
641 ip->i_gid = ip->i_ffs1_gid = dip->di_gid;
642
643 ip->i_mode = ip->i_ffs1_mode;
644 ip->i_nlink = ip->i_ffs_effnlink = ip->i_ffs1_nlink;
645 ip->i_size = ip->i_ffs1_size;
646
647 LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
648
649 /* Re-initialize to get type right */
650 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
651 &vp);
652 vput(vp);
653
654 /* Record change in location */
655 LFS_IENTRY(ifp, fs, dip->di_inumber, ibp);
656 daddr = ifp->if_daddr;
657 ifp->if_daddr = dbtofsb(fs, dbp->b_blkno);
658 error = LFS_BWRITE_LOG(ibp); /* Ifile */
659 /* And do segment accounting */
660 if (dtosn(fs, daddr) != dtosn(fs, dbtofsb(fs, dbp->b_blkno))) {
661 if (daddr > 0) {
662 LFS_SEGENTRY(sup, fs, dtosn(fs, daddr),
663 ibp);
664 sup->su_nbytes -= sizeof (struct ufs1_dinode);
665 LFS_WRITESEGENTRY(sup, fs,
666 dtosn(fs, daddr),
667 ibp);
668 }
669 LFS_SEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
670 ibp);
671 sup->su_nbytes += sizeof (struct ufs1_dinode);
672 LFS_WRITESEGENTRY(sup, fs,
673 dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
674 ibp);
675 }
676 }
677 }
678 dbp->b_flags |= B_AGE;
679 brelse(dbp);
680
681 return 0;
682 }
683
684 #define CHECK_CKSUM 0x0001 /* Check the checksum to make sure it's valid */
685 #define CHECK_UPDATE 0x0002 /* Update Ifile for new data blocks / inodes */
686
687 static daddr_t
688 check_segsum(struct lfs *fs, daddr_t offset, u_int64_t nextserial,
689 struct ucred *cred, int flags, int *pseg_flags, struct proc *p)
690 {
691 struct vnode *devvp;
692 struct buf *bp, *dbp;
693 int error, nblocks = 0, ninos, i, j; /* XXX: gcc */
694 SEGSUM *ssp;
695 u_long *dp = NULL, *datap = NULL; /* XXX u_int32_t */
696 daddr_t oldoffset;
697 int32_t *iaddr; /* XXX ondisk32 */
698 FINFO *fip;
699 SEGUSE *sup;
700 size_t size;
701
702 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
703 /*
704 * If the segment has a superblock and we're at the top
705 * of the segment, skip the superblock.
706 */
707 if (sntod(fs, dtosn(fs, offset)) == offset) {
708 LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
709 if (sup->su_flags & SEGUSE_SUPERBLOCK)
710 offset += btofsb(fs, LFS_SBPAD);
711 brelse(bp);
712 }
713
714 /* Read in the segment summary */
715 error = bread(devvp, fsbtodb(fs, offset), fs->lfs_sumsize, cred, &bp);
716 if (error)
717 return -1;
718
719 /* Check summary checksum */
720 ssp = (SEGSUM *)bp->b_data;
721 if (flags & CHECK_CKSUM) {
722 if (ssp->ss_sumsum != cksum(&ssp->ss_datasum,
723 fs->lfs_sumsize -
724 sizeof(ssp->ss_sumsum))) {
725 DLOG((DLOG_RF, "Sumsum error at 0x%" PRIx64 "\n", offset));
726 offset = -1;
727 goto err1;
728 }
729 if (ssp->ss_nfinfo == 0 && ssp->ss_ninos == 0) {
730 DLOG((DLOG_RF, "Empty pseg at 0x%" PRIx64 "\n", offset));
731 offset = -1;
732 goto err1;
733 }
734 if (ssp->ss_create < fs->lfs_tstamp) {
735 DLOG((DLOG_RF, "Old data at 0x%" PRIx64 "\n", offset));
736 offset = -1;
737 goto err1;
738 }
739 }
740 if (fs->lfs_version > 1) {
741 if (ssp->ss_serial != nextserial) {
742 DLOG((DLOG_RF, "Unexpected serial number at 0x%" PRIx64
743 "\n", offset));
744 offset = -1;
745 goto err1;
746 }
747 if (ssp->ss_ident != fs->lfs_ident) {
748 DLOG((DLOG_RF, "Incorrect fsid (0x%x vs 0x%x) at 0x%"
749 PRIx64 "\n", ssp->ss_ident, fs->lfs_ident, offset));
750 offset = -1;
751 goto err1;
752 }
753 }
754 if (pseg_flags)
755 *pseg_flags = ssp->ss_flags;
756 oldoffset = offset;
757 offset += btofsb(fs, fs->lfs_sumsize);
758
759 ninos = howmany(ssp->ss_ninos, INOPB(fs));
760 /* XXX ondisk32 */
761 iaddr = (int32_t *)(bp->b_data + fs->lfs_sumsize - sizeof(int32_t));
762 if (flags & CHECK_CKSUM) {
763 /* Count blocks */
764 nblocks = 0;
765 fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs));
766 for (i = 0; i < ssp->ss_nfinfo; ++i) {
767 nblocks += fip->fi_nblocks;
768 if (fip->fi_nblocks <= 0)
769 break;
770 /* XXX ondisk32 */
771 fip = (FINFO *)(((char *)fip) + FINFOSIZE +
772 (fip->fi_nblocks * sizeof(int32_t)));
773 }
774 nblocks += ninos;
775 /* Create the sum array */
776 datap = dp = (u_long *)malloc(nblocks * sizeof(u_long),
777 M_SEGMENT, M_WAITOK);
778 }
779
780 /* Handle individual blocks */
781 fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs));
782 for (i = 0; i < ssp->ss_nfinfo || ninos; ++i) {
783 /* Inode block? */
784 if (ninos && *iaddr == offset) {
785 if (flags & CHECK_CKSUM) {
786 /* Read in the head and add to the buffer */
787 error = bread(devvp, fsbtodb(fs, offset), fs->lfs_bsize,
788 cred, &dbp);
789 if (error) {
790 offset = -1;
791 goto err2;
792 }
793 (*dp++) = ((u_long *)(dbp->b_data))[0];
794 dbp->b_flags |= B_AGE;
795 brelse(dbp);
796 }
797 if (flags & CHECK_UPDATE) {
798 if ((error = update_inoblk(fs, offset, cred, p))
799 != 0) {
800 offset = -1;
801 goto err2;
802 }
803 }
804 offset += btofsb(fs, fs->lfs_ibsize);
805 --iaddr;
806 --ninos;
807 --i; /* compensate */
808 continue;
809 }
810 size = fs->lfs_bsize;
811 for (j = 0; j < fip->fi_nblocks; ++j) {
812 if (j == fip->fi_nblocks - 1)
813 size = fip->fi_lastlength;
814 if (flags & CHECK_CKSUM) {
815 error = bread(devvp, fsbtodb(fs, offset), size, cred, &dbp);
816 if (error) {
817 offset = -1;
818 goto err2;
819 }
820 (*dp++) = ((u_long *)(dbp->b_data))[0];
821 dbp->b_flags |= B_AGE;
822 brelse(dbp);
823 }
824 /* Account for and update any direct blocks */
825 if ((flags & CHECK_UPDATE) &&
826 fip->fi_ino > LFS_IFILE_INUM &&
827 fip->fi_blocks[j] >= 0) {
828 update_meta(fs, fip->fi_ino, fip->fi_version,
829 fip->fi_blocks[j], offset, size, p);
830 }
831 offset += btofsb(fs, size);
832 }
833 /* XXX ondisk32 */
834 fip = (FINFO *)(((char *)fip) + FINFOSIZE
835 + fip->fi_nblocks * sizeof(int32_t));
836 }
837 /* Checksum the array, compare */
838 if ((flags & CHECK_CKSUM) &&
839 ssp->ss_datasum != cksum(datap, nblocks * sizeof(u_long)))
840 {
841 DLOG((DLOG_RF, "Datasum error at 0x%" PRIx64
842 " (wanted %x got %x)\n",
843 offset, ssp->ss_datasum, cksum(datap, nblocks *
844 sizeof(u_long))));
845 offset = -1;
846 goto err2;
847 }
848
849 /* If we're at the end of the segment, move to the next */
850 if (dtosn(fs, offset + btofsb(fs, fs->lfs_sumsize + fs->lfs_bsize)) !=
851 dtosn(fs, offset)) {
852 if (dtosn(fs, offset) == dtosn(fs, ssp->ss_next)) {
853 offset = -1;
854 goto err2;
855 }
856 offset = ssp->ss_next;
857 DLOG((DLOG_RF, "LFS roll forward: moving to offset 0x%" PRIx64
858 " -> segment %d\n", offset, dtosn(fs,offset)));
859 }
860
861 if (flags & CHECK_UPDATE) {
862 fs->lfs_avail -= (offset - oldoffset);
863 /* Don't clog the buffer queue */
864 simple_lock(&lfs_subsys_lock);
865 if (locked_queue_count > LFS_MAX_BUFS ||
866 locked_queue_bytes > LFS_MAX_BYTES) {
867 lfs_flush(fs, SEGM_CKP, 0);
868 }
869 simple_unlock(&lfs_subsys_lock);
870 }
871
872 err2:
873 if (flags & CHECK_CKSUM)
874 free(datap, M_SEGMENT);
875 err1:
876 bp->b_flags |= B_AGE;
877 brelse(bp);
878
879 /* XXX should we update the serial number even for bad psegs? */
880 if ((flags & CHECK_UPDATE) && offset > 0 && fs->lfs_version > 1)
881 fs->lfs_serial = nextserial;
882 return offset;
883 }
884
885 /*
886 * Common code for mount and mountroot
887 * LFS specific
888 */
889 int
890 lfs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p)
891 {
892 struct dlfs *tdfs, *dfs, *adfs;
893 struct lfs *fs;
894 struct ufsmount *ump;
895 struct vnode *vp;
896 struct buf *bp, *abp;
897 struct partinfo dpart;
898 dev_t dev;
899 int error, i, ronly, secsize, fsbsize;
900 struct ucred *cred;
901 CLEANERINFO *cip;
902 SEGUSE *sup;
903 int flags, dirty, do_rollforward;
904 daddr_t offset, oldoffset, lastgoodpseg, sb_addr;
905 int sn, curseg;
906
907 cred = p ? p->p_ucred : NOCRED;
908
909 /*
910 * Flush out any old buffers remaining from a previous use.
911 */
912 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
913 error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
914 VOP_UNLOCK(devvp, 0);
915 if (error)
916 return (error);
917
918 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
919 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) != 0)
920 secsize = DEV_BSIZE;
921 else
922 secsize = dpart.disklab->d_secsize;
923
924 /* Don't free random space on error. */
925 bp = NULL;
926 abp = NULL;
927 ump = NULL;
928
929 sb_addr = LFS_LABELPAD / secsize;
930 while (1) {
931 /* Read in the superblock. */
932 error = bread(devvp, sb_addr, LFS_SBPAD, cred, &bp);
933 if (error)
934 goto out;
935 dfs = (struct dlfs *)bp->b_data;
936
937 /* Check the basics. */
938 if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize > MAXBSIZE ||
939 dfs->dlfs_version > LFS_VERSION ||
940 dfs->dlfs_bsize < sizeof(struct dlfs)) {
941 DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock sanity failed\n"));
942 error = EINVAL; /* XXX needs translation */
943 goto out;
944 }
945 if (dfs->dlfs_inodefmt > LFS_MAXINODEFMT) {
946 DLOG((DLOG_MOUNT, "lfs_mountfs: unknown inode format %d\n",
947 dfs->dlfs_inodefmt));
948 error = EINVAL;
949 goto out;
950 }
951
952 if (dfs->dlfs_version == 1)
953 fsbsize = secsize;
954 else {
955 fsbsize = 1 << (dfs->dlfs_bshift - dfs->dlfs_blktodb +
956 dfs->dlfs_fsbtodb);
957 /*
958 * Could be, if the frag size is large enough, that we
959 * don't have the "real" primary superblock. If that's
960 * the case, get the real one, and try again.
961 */
962 if (sb_addr != dfs->dlfs_sboffs[0] <<
963 dfs->dlfs_fsbtodb) {
964 DLOG((DLOG_MOUNT, "lfs_mountfs: sb daddr"
965 " 0x%llx is not right, trying 0x%llx\n",
966 (long long)sb_addr,
967 (long long)(dfs->dlfs_sboffs[0] <<
968 dfs->dlfs_fsbtodb)));
969 sb_addr = dfs->dlfs_sboffs[0] <<
970 dfs->dlfs_fsbtodb;
971 brelse(bp);
972 continue;
973 }
974 }
975 break;
976 }
977
978 /*
979 * Check the second superblock to see which is newer; then mount
980 * using the older of the two. This is necessary to ensure that
981 * the filesystem is valid if it was not unmounted cleanly.
982 */
983
984 if (dfs->dlfs_sboffs[1] &&
985 dfs->dlfs_sboffs[1] - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize)
986 {
987 error = bread(devvp, dfs->dlfs_sboffs[1] * (fsbsize / secsize),
988 LFS_SBPAD, cred, &abp);
989 if (error)
990 goto out;
991 adfs = (struct dlfs *)abp->b_data;
992
993 if (dfs->dlfs_version == 1) {
994 /* 1s resolution comparison */
995 if (adfs->dlfs_tstamp < dfs->dlfs_tstamp)
996 tdfs = adfs;
997 else
998 tdfs = dfs;
999 } else {
1000 /* monotonic infinite-resolution comparison */
1001 if (adfs->dlfs_serial < dfs->dlfs_serial)
1002 tdfs = adfs;
1003 else
1004 tdfs = dfs;
1005 }
1006
1007 /* Check the basics. */
1008 if (tdfs->dlfs_magic != LFS_MAGIC ||
1009 tdfs->dlfs_bsize > MAXBSIZE ||
1010 tdfs->dlfs_version > LFS_VERSION ||
1011 tdfs->dlfs_bsize < sizeof(struct dlfs)) {
1012 DLOG((DLOG_MOUNT, "lfs_mountfs: alt superblock"
1013 " sanity failed\n"));
1014 error = EINVAL; /* XXX needs translation */
1015 goto out;
1016 }
1017 } else {
1018 DLOG((DLOG_MOUNT, "lfs_mountfs: invalid alt superblock"
1019 " daddr=0x%x\n", dfs->dlfs_sboffs[1]));
1020 error = EINVAL;
1021 goto out;
1022 }
1023
1024 /* Allocate the mount structure, copy the superblock into it. */
1025 fs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK | M_ZERO);
1026 memcpy(&fs->lfs_dlfs, tdfs, sizeof(struct dlfs));
1027
1028 /* Compatibility */
1029 if (fs->lfs_version < 2) {
1030 fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
1031 fs->lfs_ibsize = fs->lfs_bsize;
1032 fs->lfs_start = fs->lfs_sboffs[0];
1033 fs->lfs_tstamp = fs->lfs_otstamp;
1034 fs->lfs_fsbtodb = 0;
1035 }
1036
1037 /*
1038 * If we aren't going to be able to write meaningfully to this
1039 * filesystem, and were not mounted readonly, bomb out now.
1040 */
1041 if (fsbtob(fs, LFS_NRESERVE(fs)) > LFS_MAX_BYTES && !ronly) {
1042 DLOG((DLOG_MOUNT, "lfs_mount: to mount this filesystem read/write,"
1043 " we need BUFPAGES >= %lld\n",
1044 (long long)((bufmem_hiwater / bufmem_lowater) *
1045 LFS_INVERSE_MAX_BYTES(
1046 fsbtob(fs, LFS_NRESERVE(fs))) >> PAGE_SHIFT)));
1047 free(fs, M_UFSMNT);
1048 error = EFBIG; /* XXX needs translation */
1049 goto out;
1050 }
1051
1052 /* Before rolling forward, lock so vget will sleep for other procs */
1053 fs->lfs_flags = LFS_NOTYET;
1054 fs->lfs_rfpid = p->p_pid;
1055
1056 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
1057 ump->um_lfs = fs;
1058 ump->um_ops = &lfs_ufsops;
1059 ump->um_fstype = UFS1;
1060 if (sizeof(struct lfs) < LFS_SBPAD) { /* XXX why? */
1061 bp->b_flags |= B_INVAL;
1062 abp->b_flags |= B_INVAL;
1063 }
1064 brelse(bp);
1065 bp = NULL;
1066 brelse(abp);
1067 abp = NULL;
1068
1069 /* Set up the I/O information */
1070 fs->lfs_devbsize = secsize;
1071 fs->lfs_iocount = 0;
1072 fs->lfs_diropwait = 0;
1073 fs->lfs_activesb = 0;
1074 fs->lfs_uinodes = 0;
1075 fs->lfs_ravail = 0;
1076 fs->lfs_favail = 0;
1077 fs->lfs_sbactive = 0;
1078
1079 /* Set up the ifile and lock aflags */
1080 fs->lfs_doifile = 0;
1081 fs->lfs_writer = 0;
1082 fs->lfs_dirops = 0;
1083 fs->lfs_nadirop = 0;
1084 fs->lfs_seglock = 0;
1085 fs->lfs_pdflush = 0;
1086 fs->lfs_sleepers = 0;
1087 fs->lfs_pages = 0;
1088 simple_lock_init(&fs->lfs_interlock);
1089 lockinit(&fs->lfs_fraglock, PINOD, "lfs_fraglock", 0, 0);
1090 lockinit(&fs->lfs_iflock, PINOD, "lfs_iflock", 0, 0);
1091
1092 /* Set the file system readonly/modify bits. */
1093 fs->lfs_ronly = ronly;
1094 if (ronly == 0)
1095 fs->lfs_fmod = 1;
1096
1097 /* Initialize the mount structure. */
1098 dev = devvp->v_rdev;
1099 mp->mnt_data = ump;
1100 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
1101 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_LFS);
1102 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
1103 mp->mnt_stat.f_namemax = LFS_MAXNAMLEN;
1104 mp->mnt_stat.f_iosize = fs->lfs_bsize;
1105 mp->mnt_flag |= MNT_LOCAL;
1106 mp->mnt_fs_bshift = fs->lfs_bshift;
1107 ump->um_flags = 0;
1108 ump->um_mountp = mp;
1109 ump->um_dev = dev;
1110 ump->um_devvp = devvp;
1111 ump->um_bptrtodb = fs->lfs_fsbtodb;
1112 ump->um_seqinc = fragstofsb(fs, fs->lfs_frag);
1113 ump->um_nindir = fs->lfs_nindir;
1114 ump->um_lognindir = ffs(fs->lfs_nindir) - 1;
1115 for (i = 0; i < MAXQUOTAS; i++)
1116 ump->um_quotas[i] = NULLVP;
1117 ump->um_maxsymlinklen = fs->lfs_maxsymlinklen;
1118 ump->um_dirblksiz = DIRBLKSIZ;
1119 ump->um_maxfilesize = fs->lfs_maxfilesize;
1120 if (ump->um_maxsymlinklen > 0)
1121 mp->mnt_iflag |= IMNT_DTYPE;
1122 devvp->v_specmountpoint = mp;
1123
1124 /* Set up reserved memory for pageout */
1125 lfs_setup_resblks(fs);
1126 /* Set up vdirop tailq */
1127 TAILQ_INIT(&fs->lfs_dchainhd);
1128 /* and paging tailq */
1129 TAILQ_INIT(&fs->lfs_pchainhd);
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 DLOG((DLOG_MOUNT, "lfs_mountfs: ifile vget failed, error=%d\n", error));
1139 goto out;
1140 }
1141 fs->lfs_ivnode = vp;
1142 VREF(vp);
1143
1144 /* Set up segment usage flags for the autocleaner. */
1145 fs->lfs_nactive = 0;
1146 fs->lfs_suflags = (u_int32_t **)malloc(2 * sizeof(u_int32_t *),
1147 M_SEGMENT, M_WAITOK);
1148 fs->lfs_suflags[0] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
1149 M_SEGMENT, M_WAITOK);
1150 fs->lfs_suflags[1] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
1151 M_SEGMENT, M_WAITOK);
1152 memset(fs->lfs_suflags[1], 0, fs->lfs_nseg * sizeof(u_int32_t));
1153 for (i = 0; i < fs->lfs_nseg; i++) {
1154 int changed;
1155
1156 LFS_SEGENTRY(sup, fs, i, bp);
1157 changed = 0;
1158 if (!ronly) {
1159 if (sup->su_nbytes == 0 &&
1160 !(sup->su_flags & SEGUSE_EMPTY)) {
1161 sup->su_flags |= SEGUSE_EMPTY;
1162 ++changed;
1163 } else if (!(sup->su_nbytes == 0) &&
1164 (sup->su_flags & SEGUSE_EMPTY)) {
1165 sup->su_flags &= ~SEGUSE_EMPTY;
1166 ++changed;
1167 }
1168 if (sup->su_flags & (SEGUSE_ACTIVE|SEGUSE_INVAL)) {
1169 sup->su_flags &= ~(SEGUSE_ACTIVE|SEGUSE_INVAL);
1170 ++changed;
1171 }
1172 }
1173 fs->lfs_suflags[0][i] = sup->su_flags;
1174 if (changed)
1175 LFS_WRITESEGENTRY(sup, fs, i, bp);
1176 else
1177 brelse(bp);
1178 }
1179
1180 /*
1181 * Roll forward.
1182 *
1183 * We don't roll forward for v1 filesystems, because
1184 * of the danger that the clock was turned back between the last
1185 * checkpoint and crash. This would roll forward garbage.
1186 *
1187 * v2 filesystems don't have this problem because they use a
1188 * monotonically increasing serial number instead of a timestamp.
1189 */
1190 do_rollforward = (!(fs->lfs_pflags & LFS_PF_CLEAN) &&
1191 lfs_do_rfw && fs->lfs_version > 1);
1192 if (do_rollforward) {
1193 u_int64_t nextserial;
1194 /*
1195 * Phase I: Find the address of the last good partial
1196 * segment that was written after the checkpoint. Mark
1197 * the segments in question dirty, so they won't be
1198 * reallocated.
1199 */
1200 lastgoodpseg = oldoffset = offset = fs->lfs_offset;
1201 flags = 0x0;
1202 DLOG((DLOG_RF, "LFS roll forward phase 1: start at offset 0x%"
1203 PRIx64 "\n", offset));
1204 LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
1205 if (!(sup->su_flags & SEGUSE_DIRTY))
1206 --fs->lfs_nclean;
1207 sup->su_flags |= SEGUSE_DIRTY;
1208 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, offset), bp);
1209 nextserial = fs->lfs_serial + 1;
1210 while ((offset = check_segsum(fs, offset, nextserial,
1211 cred, CHECK_CKSUM, &flags, p)) > 0) {
1212 nextserial++;
1213 if (sntod(fs, oldoffset) != sntod(fs, offset)) {
1214 LFS_SEGENTRY(sup, fs, dtosn(fs, oldoffset),
1215 bp);
1216 if (!(sup->su_flags & SEGUSE_DIRTY))
1217 --fs->lfs_nclean;
1218 sup->su_flags |= SEGUSE_DIRTY;
1219 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, oldoffset),
1220 bp);
1221 }
1222
1223 DLOG((DLOG_RF, "LFS roll forward phase 1: offset=0x%"
1224 PRIx64 "\n", offset));
1225 if (flags & SS_DIROP) {
1226 DLOG((DLOG_RF, "lfs_mountfs: dirops at 0x%"
1227 PRIx64 "\n", oldoffset));
1228 if (!(flags & SS_CONT))
1229 DLOG((DLOG_RF, "lfs_mountfs: dirops end "
1230 "at 0x%" PRIx64 "\n", oldoffset));
1231 }
1232 if (!(flags & SS_CONT))
1233 lastgoodpseg = offset;
1234 oldoffset = offset;
1235 }
1236 if (flags & SS_CONT) {
1237 DLOG((DLOG_RF, "LFS roll forward: warning: incomplete "
1238 "dirops discarded\n"));
1239 }
1240 DLOG((DLOG_RF, "LFS roll forward phase 1: completed: "
1241 "lastgoodpseg=0x%" PRIx64 "\n", lastgoodpseg));
1242 oldoffset = fs->lfs_offset;
1243 if (fs->lfs_offset != lastgoodpseg) {
1244 /* Don't overwrite what we're trying to preserve */
1245 offset = fs->lfs_offset;
1246 fs->lfs_offset = lastgoodpseg;
1247 fs->lfs_curseg = sntod(fs, dtosn(fs, fs->lfs_offset));
1248 for (sn = curseg = dtosn(fs, fs->lfs_curseg);;) {
1249 sn = (sn + 1) % fs->lfs_nseg;
1250 if (sn == curseg)
1251 panic("lfs_mountfs: no clean segments");
1252 LFS_SEGENTRY(sup, fs, sn, bp);
1253 dirty = (sup->su_flags & SEGUSE_DIRTY);
1254 brelse(bp);
1255 if (!dirty)
1256 break;
1257 }
1258 fs->lfs_nextseg = sntod(fs, sn);
1259
1260 /*
1261 * Phase II: Roll forward from the first superblock.
1262 */
1263 while (offset != lastgoodpseg) {
1264 DLOG((DLOG_RF, "LFS roll forward phase 2: 0x%"
1265 PRIx64 "\n", offset));
1266 offset = check_segsum(fs, offset,
1267 fs->lfs_serial + 1, cred, CHECK_UPDATE,
1268 NULL, p);
1269 }
1270
1271 /*
1272 * Finish: flush our changes to disk.
1273 */
1274 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
1275 DLOG((DLOG_RF, "lfs_mountfs: roll forward ",
1276 "recovered %lld blocks\n",
1277 (long long)(lastgoodpseg - oldoffset)));
1278 }
1279 DLOG((DLOG_RF, "LFS roll forward complete\n"));
1280 }
1281 /* If writing, sb is not clean; record in case of immediate crash */
1282 if (!fs->lfs_ronly) {
1283 fs->lfs_pflags &= ~LFS_PF_CLEAN;
1284 lfs_writesuper(fs, fs->lfs_sboffs[0]);
1285 lfs_writesuper(fs, fs->lfs_sboffs[1]);
1286 }
1287
1288 /* Allow vget now that roll-forward is complete */
1289 fs->lfs_flags &= ~(LFS_NOTYET);
1290 wakeup(&fs->lfs_flags);
1291
1292 /*
1293 * Initialize the ifile cleaner info with information from
1294 * the superblock.
1295 */
1296 LFS_CLEANERINFO(cip, fs, bp);
1297 cip->clean = fs->lfs_nclean;
1298 cip->dirty = fs->lfs_nseg - fs->lfs_nclean;
1299 cip->avail = fs->lfs_avail;
1300 cip->bfree = fs->lfs_bfree;
1301 (void) LFS_BWRITE_LOG(bp); /* Ifile */
1302
1303 /*
1304 * Mark the current segment as ACTIVE, since we're going to
1305 * be writing to it.
1306 */
1307 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp);
1308 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1309 fs->lfs_nactive++;
1310 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp); /* Ifile */
1311
1312 /* Now that roll-forward is done, unlock the Ifile */
1313 vput(vp);
1314
1315 /* Comment on ifile size if it is too large */
1316 warn_ifile_size(fs);
1317
1318 /* Start the pagedaemon-anticipating daemon */
1319 if (lfs_writer_daemon == 0 &&
1320 kthread_create1(lfs_writerd, NULL, NULL, "lfs_writer") != 0)
1321 panic("fork lfs_writer");
1322
1323 return (0);
1324
1325 out:
1326 if (bp)
1327 brelse(bp);
1328 if (abp)
1329 brelse(abp);
1330 if (ump) {
1331 free(ump->um_lfs, M_UFSMNT);
1332 free(ump, M_UFSMNT);
1333 mp->mnt_data = NULL;
1334 }
1335
1336 return (error);
1337 }
1338
1339 /*
1340 * unmount system call
1341 */
1342 int
1343 lfs_unmount(struct mount *mp, int mntflags, struct proc *p)
1344 {
1345 struct ufsmount *ump;
1346 struct lfs *fs;
1347 int error, flags, ronly;
1348 int s;
1349
1350 flags = 0;
1351 if (mntflags & MNT_FORCE)
1352 flags |= FORCECLOSE;
1353
1354 ump = VFSTOUFS(mp);
1355 fs = ump->um_lfs;
1356
1357 /* wake up the cleaner so it can die */
1358 wakeup(&fs->lfs_nextseg);
1359 wakeup(&lfs_allclean_wakeup);
1360 simple_lock(&fs->lfs_interlock);
1361 while (fs->lfs_sleepers)
1362 ltsleep(&fs->lfs_sleepers, PRIBIO + 1, "lfs_sleepers", 0,
1363 &fs->lfs_interlock);
1364 simple_unlock(&fs->lfs_interlock);
1365
1366 #ifdef QUOTA
1367 if (mp->mnt_flag & MNT_QUOTA) {
1368 int i;
1369 error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
1370 if (error)
1371 return (error);
1372 for (i = 0; i < MAXQUOTAS; i++) {
1373 if (ump->um_quotas[i] == NULLVP)
1374 continue;
1375 quotaoff(p, mp, i);
1376 }
1377 /*
1378 * Here we fall through to vflush again to ensure
1379 * that we have gotten rid of all the system vnodes.
1380 */
1381 }
1382 #endif
1383 if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
1384 return (error);
1385 if ((error = VFS_SYNC(mp, 1, p->p_ucred, p)) != 0)
1386 return (error);
1387 s = splbio();
1388 if (LIST_FIRST(&fs->lfs_ivnode->v_dirtyblkhd))
1389 panic("lfs_unmount: still dirty blocks on ifile vnode");
1390 splx(s);
1391
1392 /* Comment on ifile size if it has become too large */
1393 if (!(fs->lfs_flags & LFS_WARNED))
1394 warn_ifile_size(fs);
1395
1396 /* Explicitly write the superblock, to update serial and pflags */
1397 fs->lfs_pflags |= LFS_PF_CLEAN;
1398 lfs_writesuper(fs, fs->lfs_sboffs[0]);
1399 lfs_writesuper(fs, fs->lfs_sboffs[1]);
1400 simple_lock(&fs->lfs_interlock);
1401 while (fs->lfs_iocount)
1402 ltsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0,
1403 &fs->lfs_interlock);
1404 simple_unlock(&fs->lfs_interlock);
1405
1406 /* Finish with the Ifile, now that we're done with it */
1407 vrele(fs->lfs_ivnode);
1408 vgone(fs->lfs_ivnode);
1409
1410 ronly = !fs->lfs_ronly;
1411 if (ump->um_devvp->v_type != VBAD)
1412 ump->um_devvp->v_specmountpoint = NULL;
1413 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1414 error = VOP_CLOSE(ump->um_devvp,
1415 ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
1416 vput(ump->um_devvp);
1417
1418 /* Complain about page leakage */
1419 if (fs->lfs_pages > 0)
1420 printf("lfs_unmount: still claim %d pages (%d in subsystem)\n",
1421 fs->lfs_pages, lfs_subsys_pages);
1422
1423 /* Free per-mount data structures */
1424 free(fs->lfs_suflags[0], M_SEGMENT);
1425 free(fs->lfs_suflags[1], M_SEGMENT);
1426 free(fs->lfs_suflags, M_SEGMENT);
1427 lfs_free_resblks(fs);
1428 free(fs, M_UFSMNT);
1429 free(ump, M_UFSMNT);
1430
1431 mp->mnt_data = NULL;
1432 mp->mnt_flag &= ~MNT_LOCAL;
1433 return (error);
1434 }
1435
1436 /*
1437 * Get file system statistics.
1438 *
1439 * NB: We don't lock to access the superblock here, because it's not
1440 * really that important if we get it wrong.
1441 */
1442 int
1443 lfs_statvfs(struct mount *mp, struct statvfs *sbp, struct proc *p)
1444 {
1445 struct lfs *fs;
1446 struct ufsmount *ump;
1447
1448 ump = VFSTOUFS(mp);
1449 fs = ump->um_lfs;
1450 if (fs->lfs_magic != LFS_MAGIC)
1451 panic("lfs_statvfs: magic");
1452
1453 sbp->f_bsize = fs->lfs_bsize;
1454 sbp->f_frsize = fs->lfs_fsize;
1455 sbp->f_iosize = fs->lfs_bsize;
1456 sbp->f_blocks = fsbtofrags(fs, LFS_EST_NONMETA(fs));
1457
1458 sbp->f_bfree = fsbtofrags(fs, LFS_EST_BFREE(fs));
1459 KASSERT(sbp->f_bfree <= fs->lfs_dsize);
1460 if (sbp->f_bfree < 0)
1461 sbp->f_bfree = 0;
1462
1463 sbp->f_bresvd = fsbtofrags(fs, LFS_EST_RSVD(fs));
1464 if (sbp->f_bfree > sbp->f_bresvd)
1465 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1466 else
1467 sbp->f_bavail = 0;
1468
1469 sbp->f_files = fs->lfs_bfree / btofsb(fs, fs->lfs_ibsize) * INOPB(fs);
1470 sbp->f_ffree = sbp->f_files - fs->lfs_nfiles;
1471 sbp->f_favail = sbp->f_ffree;
1472 sbp->f_fresvd = 0;
1473 copy_statvfs_info(sbp, mp);
1474 return (0);
1475 }
1476
1477 /*
1478 * Go through the disk queues to initiate sandbagged IO;
1479 * go through the inodes to write those that have been modified;
1480 * initiate the writing of the super block if it has been modified.
1481 *
1482 * Note: we are always called with the filesystem marked `MPBUSY'.
1483 */
1484 int
1485 lfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
1486 {
1487 int error;
1488 struct lfs *fs;
1489
1490 fs = VFSTOUFS(mp)->um_lfs;
1491 if (fs->lfs_ronly)
1492 return 0;
1493 lfs_writer_enter(fs, "lfs_dirops");
1494
1495 /* All syncs must be checkpoints until roll-forward is implemented. */
1496 error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
1497 lfs_writer_leave(fs);
1498 #ifdef QUOTA
1499 qsync(mp);
1500 #endif
1501 return (error);
1502 }
1503
1504 extern struct lock ufs_hashlock;
1505
1506 /*
1507 * Look up an LFS dinode number to find its incore vnode. If not already
1508 * in core, read it in from the specified device. Return the inode locked.
1509 * Detection and handling of mount points must be done by the calling routine.
1510 */
1511 int
1512 lfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1513 {
1514 struct lfs *fs;
1515 struct ufs1_dinode *dip;
1516 struct inode *ip;
1517 struct buf *bp;
1518 struct ifile *ifp;
1519 struct vnode *vp;
1520 struct ufsmount *ump;
1521 daddr_t daddr;
1522 dev_t dev;
1523 int error, retries;
1524 struct timespec ts;
1525
1526 ump = VFSTOUFS(mp);
1527 dev = ump->um_dev;
1528 fs = ump->um_lfs;
1529
1530 /*
1531 * If the filesystem is not completely mounted yet, suspend
1532 * any access requests (wait for roll-forward to complete).
1533 */
1534 simple_lock(&fs->lfs_interlock);
1535 while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
1536 ltsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0,
1537 &fs->lfs_interlock);
1538 simple_unlock(&fs->lfs_interlock);
1539
1540 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1541 return (0);
1542
1543 if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
1544 *vpp = NULL;
1545 return (error);
1546 }
1547
1548 do {
1549 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
1550 ungetnewvnode(vp);
1551 return (0);
1552 }
1553 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
1554
1555 /* Translate the inode number to a disk address. */
1556 if (ino == LFS_IFILE_INUM)
1557 daddr = fs->lfs_idaddr;
1558 else {
1559 /* XXX bounds-check this too */
1560 LFS_IENTRY(ifp, fs, ino, bp);
1561 daddr = ifp->if_daddr;
1562 if (fs->lfs_version > 1) {
1563 ts.tv_sec = ifp->if_atime_sec;
1564 ts.tv_nsec = ifp->if_atime_nsec;
1565 }
1566
1567 brelse(bp);
1568 if (daddr == LFS_UNUSED_DADDR) {
1569 *vpp = NULLVP;
1570 ungetnewvnode(vp);
1571 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1572 return (ENOENT);
1573 }
1574 }
1575
1576 /* Allocate/init new vnode/inode. */
1577 lfs_vcreate(mp, ino, vp);
1578
1579 /*
1580 * Put it onto its hash chain and lock it so that other requests for
1581 * this inode will block if they arrive while we are sleeping waiting
1582 * for old data structures to be purged or for the contents of the
1583 * disk portion of this inode to be read.
1584 */
1585 ip = VTOI(vp);
1586 ufs_ihashins(ip);
1587 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1588
1589 /*
1590 * XXX
1591 * This may not need to be here, logically it should go down with
1592 * the i_devvp initialization.
1593 * Ask Kirk.
1594 */
1595 ip->i_lfs = ump->um_lfs;
1596
1597 /* Read in the disk contents for the inode, copy into the inode. */
1598 retries = 0;
1599 again:
1600 error = bread(ump->um_devvp, fsbtodb(fs, daddr),
1601 (fs->lfs_version == 1 ? fs->lfs_bsize : fs->lfs_ibsize),
1602 NOCRED, &bp);
1603 if (error) {
1604 /*
1605 * The inode does not contain anything useful, so it would
1606 * be misleading to leave it on its hash chain. With mode
1607 * still zero, it will be unlinked and returned to the free
1608 * list by vput().
1609 */
1610 vput(vp);
1611 brelse(bp);
1612 *vpp = NULL;
1613 return (error);
1614 }
1615
1616 dip = lfs_ifind(fs, ino, bp);
1617 if (dip == NULL) {
1618 /* Assume write has not completed yet; try again */
1619 bp->b_flags |= B_INVAL;
1620 brelse(bp);
1621 ++retries;
1622 if (retries > LFS_IFIND_RETRIES) {
1623 #ifdef DEBUG
1624 /* If the seglock is held look at the bpp to see
1625 what is there anyway */
1626 simple_lock(&fs->lfs_interlock);
1627 if (fs->lfs_seglock > 0) {
1628 struct buf **bpp;
1629 struct ufs1_dinode *dp;
1630 int i;
1631
1632 for (bpp = fs->lfs_sp->bpp;
1633 bpp != fs->lfs_sp->cbpp; ++bpp) {
1634 if ((*bpp)->b_vp == fs->lfs_ivnode &&
1635 bpp != fs->lfs_sp->bpp) {
1636 /* Inode block */
1637 printf("lfs_vget: block 0x%" PRIx64 ": ",
1638 (*bpp)->b_blkno);
1639 dp = (struct ufs1_dinode *)(*bpp)->b_data;
1640 for (i = 0; i < INOPB(fs); i++)
1641 if (dp[i].di_u.inumber)
1642 printf("%d ", dp[i].di_u.inumber);
1643 printf("\n");
1644 }
1645 }
1646 }
1647 simple_unlock(&fs->lfs_interlock);
1648 #endif /* DEBUG */
1649 panic("lfs_vget: dinode not found");
1650 }
1651 simple_lock(&fs->lfs_interlock);
1652 if (fs->lfs_iocount) {
1653 DLOG((DLOG_VNODE, "lfs_vget: dinode %d not found, retrying...\n", ino));
1654 (void)ltsleep(&fs->lfs_iocount, PRIBIO + 1,
1655 "lfs ifind", 1, &fs->lfs_interlock);
1656 } else
1657 retries = LFS_IFIND_RETRIES;
1658 simple_unlock(&fs->lfs_interlock);
1659 goto again;
1660 }
1661 *ip->i_din.ffs1_din = *dip;
1662 brelse(bp);
1663
1664 if (fs->lfs_version > 1) {
1665 ip->i_ffs1_atime = ts.tv_sec;
1666 ip->i_ffs1_atimensec = ts.tv_nsec;
1667 }
1668
1669 lfs_vinit(mp, &vp);
1670
1671 *vpp = vp;
1672
1673 KASSERT(VOP_ISLOCKED(vp));
1674
1675 return (0);
1676 }
1677
1678 /*
1679 * File handle to vnode
1680 */
1681 int
1682 lfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1683 {
1684 struct lfid *lfhp;
1685 struct buf *bp;
1686 IFILE *ifp;
1687 int32_t daddr;
1688 struct lfs *fs;
1689
1690 lfhp = (struct lfid *)fhp;
1691 if (lfhp->lfid_ino < LFS_IFILE_INUM)
1692 return ESTALE;
1693
1694 fs = VFSTOUFS(mp)->um_lfs;
1695 if (lfhp->lfid_ident != fs->lfs_ident)
1696 return ESTALE;
1697
1698 if (lfhp->lfid_ino >
1699 ((VTOI(fs->lfs_ivnode)->i_ffs1_size >> fs->lfs_bshift) -
1700 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb)
1701 return ESTALE;
1702
1703 if (ufs_ihashlookup(VFSTOUFS(mp)->um_dev, lfhp->lfid_ino) == NULLVP) {
1704 LFS_IENTRY(ifp, fs, lfhp->lfid_ino, bp);
1705 daddr = ifp->if_daddr;
1706 brelse(bp);
1707 if (daddr == LFS_UNUSED_DADDR)
1708 return ESTALE;
1709 }
1710
1711 return (ufs_fhtovp(mp, &lfhp->lfid_ufid, vpp));
1712 }
1713
1714 /*
1715 * Vnode pointer to File handle
1716 */
1717 /* ARGSUSED */
1718 int
1719 lfs_vptofh(struct vnode *vp, struct fid *fhp)
1720 {
1721 struct inode *ip;
1722 struct lfid *lfhp;
1723
1724 ip = VTOI(vp);
1725 lfhp = (struct lfid *)fhp;
1726 lfhp->lfid_len = sizeof(struct lfid);
1727 lfhp->lfid_ino = ip->i_number;
1728 lfhp->lfid_gen = ip->i_gen;
1729 lfhp->lfid_ident = ip->i_lfs->lfs_ident;
1730 return (0);
1731 }
1732
1733 static int
1734 sysctl_lfs_dostats(SYSCTLFN_ARGS)
1735 {
1736 extern struct lfs_stats lfs_stats;
1737 extern int lfs_dostats;
1738 int error;
1739
1740 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
1741 if (error || newp == NULL)
1742 return (error);
1743
1744 if (lfs_dostats == 0)
1745 memset(&lfs_stats, 0, sizeof(lfs_stats));
1746
1747 return (0);
1748 }
1749
1750 struct shortlong {
1751 const char *sname;
1752 const char *lname;
1753 };
1754
1755 SYSCTL_SETUP(sysctl_vfs_lfs_setup, "sysctl vfs.lfs subtree setup")
1756 {
1757 int i;
1758 extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead,
1759 lfs_fs_pagetrip;
1760 #ifdef DEBUG
1761 extern int lfs_debug_log_subsys[DLOG_MAX];
1762 struct shortlong dlog_names[DLOG_MAX] = { /* Must match lfs.h ! */
1763 { "rollforward", "Debug roll-forward code" },
1764 { "alloc", "Debug inode allocation and free list" },
1765 { "avail", "Debug space-available-now accounting" },
1766 { "flush", "Debug flush triggers" },
1767 { "lockedlist", "Debug locked list accounting" },
1768 { "vnode_verbose", "Verbose per-vnode-written debugging" },
1769 { "vnode", "Debug vnode use during segment write" },
1770 { "segment", "Debug segment writing" },
1771 { "seguse", "Debug segment used-bytes accounting" },
1772 { "cleaner", "Debug cleaning routines" },
1773 { "mount", "Debug mount/unmount routines" },
1774 { "pagecache", "Debug UBC interactions" },
1775 { "dirop", "Debug directory-operation accounting" },
1776 { "malloc", "Debug private malloc accounting" },
1777 };
1778 #endif /* DEBUG */
1779 struct shortlong stat_names[] = { /* Must match lfs.h! */
1780 { "segsused", "Number of new segments allocated" },
1781 { "psegwrites", "Number of partial-segment writes" },
1782 { "psyncwrites", "Number of synchronous partial-segment"
1783 " writes" },
1784 { "pcleanwrites", "Number of partial-segment writes by the"
1785 " cleaner" },
1786 { "blocktot", "Number of blocks written" },
1787 { "cleanblocks", "Number of blocks written by the cleaner" },
1788 { "ncheckpoints", "Number of checkpoints made" },
1789 { "nwrites", "Number of whole writes" },
1790 { "nsync_writes", "Number of synchronous writes" },
1791 { "wait_exceeded", "Number of times writer waited for"
1792 " cleaner" },
1793 { "write_exceeded", "Number of times writer invoked flush" },
1794 { "flush_invoked", "Number of times flush was invoked" },
1795 { "vflush_invoked", "Number of time vflush was called" },
1796 { "clean_inlocked", "Number of vnodes skipped for VXLOCK" },
1797 { "clean_vnlocked", "Number of vnodes skipped for vget failure" },
1798 { "segs_reclaimed", "Number of segments reclaimed" },
1799 };
1800
1801 sysctl_createv(clog, 0, NULL, NULL,
1802 CTLFLAG_PERMANENT,
1803 CTLTYPE_NODE, "vfs", NULL,
1804 NULL, 0, NULL, 0,
1805 CTL_VFS, CTL_EOL);
1806 sysctl_createv(clog, 0, NULL, NULL,
1807 CTLFLAG_PERMANENT,
1808 CTLTYPE_NODE, "lfs",
1809 SYSCTL_DESCR("Log-structured file system"),
1810 NULL, 0, NULL, 0,
1811 CTL_VFS, 5, CTL_EOL);
1812 /*
1813 * XXX the "5" above could be dynamic, thereby eliminating one
1814 * more instance of the "number to vfs" mapping problem, but
1815 * "5" is the order as taken from sys/mount.h
1816 */
1817
1818 sysctl_createv(clog, 0, NULL, NULL,
1819 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1820 CTLTYPE_INT, "flushindir", NULL,
1821 NULL, 0, &lfs_writeindir, 0,
1822 CTL_VFS, 5, LFS_WRITEINDIR, CTL_EOL);
1823 sysctl_createv(clog, 0, NULL, NULL,
1824 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1825 CTLTYPE_INT, "clean_vnhead", NULL,
1826 NULL, 0, &lfs_clean_vnhead, 0,
1827 CTL_VFS, 5, LFS_CLEAN_VNHEAD, CTL_EOL);
1828 sysctl_createv(clog, 0, NULL, NULL,
1829 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1830 CTLTYPE_INT, "dostats",
1831 SYSCTL_DESCR("Maintain statistics on LFS operations"),
1832 sysctl_lfs_dostats, 0, &lfs_dostats, 0,
1833 CTL_VFS, 5, LFS_DOSTATS, CTL_EOL);
1834 sysctl_createv(clog, 0, NULL, NULL,
1835 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1836 CTLTYPE_INT, "pagetrip",
1837 SYSCTL_DESCR("How many dirty pages in fs triggers"
1838 " a flush"),
1839 NULL, 0, &lfs_fs_pagetrip, 0,
1840 CTL_VFS, 5, LFS_FS_PAGETRIP, CTL_EOL);
1841 sysctl_createv(clog, 0, NULL, NULL,
1842 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1843 CTLTYPE_INT, "rfw",
1844 SYSCTL_DESCR("Use in-kernel roll-forward on mount"),
1845 NULL, 0, &lfs_do_rfw, 0,
1846 CTL_VFS, 5, LFS_DO_RFW, CTL_EOL);
1847
1848 sysctl_createv(clog, 0, NULL, NULL,
1849 CTLFLAG_PERMANENT,
1850 CTLTYPE_NODE, "stats",
1851 SYSCTL_DESCR("Debugging options"),
1852 NULL, 0, NULL, 0,
1853 CTL_VFS, 5, LFS_STATS, CTL_EOL);
1854 for (i = 0; i < sizeof(struct lfs_stats) / sizeof(u_int); i++) {
1855 sysctl_createv(clog, 0, NULL, NULL,
1856 CTLFLAG_PERMANENT|CTLFLAG_READONLY,
1857 CTLTYPE_INT, stat_names[i].sname,
1858 SYSCTL_DESCR(stat_names[i].lname),
1859 NULL, 0, &(((u_int *)&lfs_stats.segsused)[i]),
1860 0, CTL_VFS, 5, LFS_STATS, i, CTL_EOL);
1861 }
1862
1863 #ifdef DEBUG
1864 sysctl_createv(clog, 0, NULL, NULL,
1865 CTLFLAG_PERMANENT,
1866 CTLTYPE_NODE, "debug",
1867 SYSCTL_DESCR("Debugging options"),
1868 NULL, 0, NULL, 0,
1869 CTL_VFS, 5, LFS_DEBUGLOG, CTL_EOL);
1870 for (i = 0; i < DLOG_MAX; i++) {
1871 sysctl_createv(clog, 0, NULL, NULL,
1872 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1873 CTLTYPE_INT, dlog_names[i].sname,
1874 SYSCTL_DESCR(dlog_names[i].lname),
1875 NULL, 0, &(lfs_debug_log_subsys[i]), 0,
1876 CTL_VFS, 5, LFS_DEBUGLOG, i, CTL_EOL);
1877 }
1878 #endif
1879 }
1880
1881 /*
1882 * ufs_bmaparray callback function for writing.
1883 *
1884 * Since blocks will be written to the new segment anyway,
1885 * we don't care about current daddr of them.
1886 */
1887 static boolean_t
1888 lfs_issequential_hole(const struct ufsmount *ump,
1889 daddr_t daddr0, daddr_t daddr1)
1890 {
1891 daddr0 = (daddr_t)((int32_t)daddr0); /* XXX ondisk32 */
1892 daddr1 = (daddr_t)((int32_t)daddr1); /* XXX ondisk32 */
1893
1894 KASSERT(daddr0 == UNWRITTEN ||
1895 (0 <= daddr0 && daddr0 <= LFS_MAX_DADDR));
1896 KASSERT(daddr1 == UNWRITTEN ||
1897 (0 <= daddr1 && daddr1 <= LFS_MAX_DADDR));
1898
1899 /* NOTE: all we want to know here is 'hole or not'. */
1900 /* NOTE: UNASSIGNED is converted to 0 by ufs_bmaparray. */
1901
1902 /*
1903 * treat UNWRITTENs and all resident blocks as 'contiguous'
1904 */
1905 if (daddr0 != 0 && daddr1 != 0)
1906 return TRUE;
1907
1908 /*
1909 * both are in hole?
1910 */
1911 if (daddr0 == 0 && daddr1 == 0)
1912 return TRUE; /* all holes are 'contiguous' for us. */
1913
1914 return FALSE;
1915 }
1916
1917 /*
1918 * lfs_gop_write functions exactly like genfs_gop_write, except that
1919 * (1) it requires the seglock to be held by its caller, and sp->fip
1920 * to be properly initialized (it will return without re-initializing
1921 * sp->fip, and without calling lfs_writeseg).
1922 * (2) it uses the remaining space in the segment, rather than VOP_BMAP,
1923 * to determine how large a block it can write at once (though it does
1924 * still use VOP_BMAP to find holes in the file);
1925 * (3) it calls lfs_gatherblock instead of VOP_STRATEGY on its blocks
1926 * (leaving lfs_writeseg to deal with the cluster blocks, so we might
1927 * now have clusters of clusters, ick.)
1928 */
1929 static int
1930 lfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1931 {
1932 int i, s, error, run;
1933 int fs_bshift;
1934 vaddr_t kva;
1935 off_t eof, offset, startoffset = 0;
1936 size_t bytes, iobytes, skipbytes;
1937 daddr_t lbn, blkno;
1938 struct vm_page *pg;
1939 struct buf *mbp, *bp;
1940 struct vnode *devvp = VTOI(vp)->i_devvp;
1941 struct inode *ip = VTOI(vp);
1942 struct lfs *fs = ip->i_lfs;
1943 struct segment *sp = fs->lfs_sp;
1944 UVMHIST_FUNC("lfs_gop_write"); UVMHIST_CALLED(ubchist);
1945
1946 ASSERT_SEGLOCK(fs);
1947
1948 /* The Ifile lives in the buffer cache */
1949 KASSERT(vp != fs->lfs_ivnode);
1950
1951 /*
1952 * Sometimes things slip past the filters in lfs_putpages,
1953 * and the pagedaemon tries to write pages---problem is
1954 * that the pagedaemon never acquires the segment lock.
1955 *
1956 * Alternatively, pages that were clean when we called
1957 * genfs_putpages may have become dirty in the meantime. In this
1958 * case the segment header is not properly set up for blocks
1959 * to be added to it.
1960 *
1961 * Unbusy and unclean the pages, and put them on the ACTIVE
1962 * queue under the hypothesis that they couldn't have got here
1963 * unless they were modified *quite* recently.
1964 *
1965 * XXXUBC that last statement is an oversimplification of course.
1966 */
1967 if (!LFS_SEGLOCK_HELD(fs) ||
1968 (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) ||
1969 (pgs[0]->offset & fs->lfs_bmask) != 0) {
1970 goto tryagain;
1971 }
1972
1973 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1974 vp, pgs, npages, flags);
1975
1976 GOP_SIZE(vp, vp->v_size, &eof, GOP_SIZE_WRITE);
1977
1978 if (vp->v_type == VREG)
1979 fs_bshift = vp->v_mount->mnt_fs_bshift;
1980 else
1981 fs_bshift = DEV_BSHIFT;
1982 error = 0;
1983 pg = pgs[0];
1984 startoffset = pg->offset;
1985 if (startoffset >= eof) {
1986 goto tryagain;
1987 } else
1988 bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
1989 skipbytes = 0;
1990
1991 KASSERT(bytes != 0);
1992
1993 /* Swap PG_DELWRI for PG_PAGEOUT */
1994 for (i = 0; i < npages; i++)
1995 if (pgs[i]->flags & PG_DELWRI) {
1996 KASSERT(!(pgs[i]->flags & PG_PAGEOUT));
1997 pgs[i]->flags &= ~PG_DELWRI;
1998 pgs[i]->flags |= PG_PAGEOUT;
1999 uvmexp.paging++;
2000 uvm_lock_pageq();
2001 uvm_pageunwire(pgs[i]);
2002 uvm_unlock_pageq();
2003 }
2004
2005 /*
2006 * Check to make sure we're starting on a block boundary.
2007 * We'll check later to make sure we always write entire
2008 * blocks (or fragments).
2009 */
2010 if (startoffset & fs->lfs_bmask)
2011 printf("%" PRId64 " & %" PRId64 " = %" PRId64 "\n",
2012 startoffset, fs->lfs_bmask,
2013 startoffset & fs->lfs_bmask);
2014 KASSERT((startoffset & fs->lfs_bmask) == 0);
2015 if (bytes & fs->lfs_ffmask) {
2016 printf("lfs_gop_write: asked to write %ld bytes\n", (long)bytes);
2017 panic("lfs_gop_write: non-integer blocks");
2018 }
2019
2020 /*
2021 * We could deadlock here on pager_map with UVMPAGER_MAPIN_WAITOK.
2022 * If we would, write what we have and try again. If we don't
2023 * have anything to write, we'll have to sleep.
2024 */
2025 if ((kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
2026 (((SEGSUM *)(sp->segsum))->ss_nfinfo < 1 ?
2027 UVMPAGER_MAPIN_WAITOK : 0))) == 0x0) {
2028 int vers;
2029
2030 DLOG((DLOG_PAGE, "lfs_gop_write: forcing write\n"));
2031 #if 0
2032 " with nfinfo=%d at offset 0x%x\n",
2033 (int)((SEGSUM *)(sp->segsum))->ss_nfinfo,
2034 (unsigned)fs->lfs_offset));
2035 #endif
2036 if (sp->fip->fi_nblocks == 0) {
2037 /* Don't write zero-length finfos */
2038 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
2039 sp->sum_bytes_left += FINFOSIZE;
2040 } else
2041 lfs_updatemeta(sp);
2042
2043 vers = sp->fip->fi_version;
2044 (void) lfs_writeseg(fs, sp);
2045
2046 sp->fip->fi_version = vers;
2047 sp->fip->fi_ino = ip->i_number;
2048 /* Add the current file to the segment summary. */
2049 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
2050 sp->sum_bytes_left -= FINFOSIZE;
2051
2052 /*
2053 * Having given up all of the pager_map we were holding,
2054 * we can now wait for aiodoned to reclaim it for us
2055 * without fear of deadlock.
2056 */
2057 kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
2058 UVMPAGER_MAPIN_WAITOK);
2059 }
2060
2061 s = splbio();
2062 simple_lock(&global_v_numoutput_slock);
2063 vp->v_numoutput += 2; /* one for biodone, one for aiodone */
2064 simple_unlock(&global_v_numoutput_slock);
2065 mbp = pool_get(&bufpool, PR_WAITOK);
2066 splx(s);
2067
2068 memset(mbp, 0, sizeof(*bp));
2069 BUF_INIT(mbp);
2070 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
2071 vp, mbp, vp->v_numoutput, bytes);
2072 mbp->b_bufsize = npages << PAGE_SHIFT;
2073 mbp->b_data = (void *)kva;
2074 mbp->b_resid = mbp->b_bcount = bytes;
2075 mbp->b_flags = B_BUSY|B_WRITE|B_AGE|B_CALL;
2076 mbp->b_iodone = uvm_aio_biodone;
2077 mbp->b_vp = vp;
2078
2079 bp = NULL;
2080 for (offset = startoffset;
2081 bytes > 0;
2082 offset += iobytes, bytes -= iobytes) {
2083 lbn = offset >> fs_bshift;
2084 error = ufs_bmaparray(vp, lbn, &blkno, NULL, NULL, &run,
2085 lfs_issequential_hole);
2086 if (error) {
2087 UVMHIST_LOG(ubchist, "ufs_bmaparray() -> %d",
2088 error,0,0,0);
2089 skipbytes += bytes;
2090 bytes = 0;
2091 break;
2092 }
2093
2094 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
2095 bytes);
2096 if (blkno == (daddr_t)-1) {
2097 skipbytes += iobytes;
2098 continue;
2099 }
2100
2101 /*
2102 * Discover how much we can really pack into this buffer.
2103 */
2104 /* If no room in the current segment, finish it up */
2105 if (sp->sum_bytes_left < sizeof(int32_t) ||
2106 sp->seg_bytes_left < (1 << fs->lfs_bshift)) {
2107 int vers;
2108
2109 lfs_updatemeta(sp);
2110
2111 vers = sp->fip->fi_version;
2112 (void) lfs_writeseg(fs, sp);
2113
2114 sp->fip->fi_version = vers;
2115 sp->fip->fi_ino = ip->i_number;
2116 /* Add the current file to the segment summary. */
2117 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
2118 sp->sum_bytes_left -= FINFOSIZE;
2119 }
2120 /* Check both for space in segment and space in segsum */
2121 iobytes = MIN(iobytes, (sp->seg_bytes_left >> fs_bshift)
2122 << fs_bshift);
2123 iobytes = MIN(iobytes, (sp->sum_bytes_left / sizeof(int32_t))
2124 << fs_bshift);
2125 KASSERT(iobytes > 0);
2126
2127 /* if it's really one i/o, don't make a second buf */
2128 if (offset == startoffset && iobytes == bytes) {
2129 bp = mbp;
2130 /* correct overcount if there is no second buffer */
2131 s = splbio();
2132 simple_lock(&global_v_numoutput_slock);
2133 --vp->v_numoutput;
2134 simple_unlock(&global_v_numoutput_slock);
2135 splx(s);
2136 } else {
2137 s = splbio();
2138 bp = pool_get(&bufpool, PR_WAITOK);
2139 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
2140 vp, bp, vp->v_numoutput, 0);
2141 splx(s);
2142 memset(bp, 0, sizeof(*bp));
2143 BUF_INIT(bp);
2144 bp->b_data = (char *)kva +
2145 (vaddr_t)(offset - pg->offset);
2146 bp->b_resid = bp->b_bcount = iobytes;
2147 bp->b_flags = B_BUSY|B_WRITE|B_CALL;
2148 bp->b_iodone = uvm_aio_biodone1;
2149 }
2150
2151 /* XXX This is silly ... is this necessary? */
2152 bp->b_vp = NULL;
2153 s = splbio();
2154 bgetvp(vp, bp);
2155 splx(s);
2156
2157 bp->b_lblkno = lblkno(fs, offset);
2158 bp->b_private = mbp;
2159 if (devvp->v_type == VBLK) {
2160 bp->b_dev = devvp->v_rdev;
2161 }
2162 VOP_BWRITE(bp);
2163 while (lfs_gatherblock(sp, bp, NULL))
2164 continue;
2165 }
2166
2167 if (skipbytes) {
2168 UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
2169 s = splbio();
2170 if (error) {
2171 mbp->b_flags |= B_ERROR;
2172 mbp->b_error = error;
2173 }
2174 mbp->b_resid -= skipbytes;
2175 if (mbp->b_resid == 0) {
2176 biodone(mbp);
2177 }
2178 splx(s);
2179 }
2180 UVMHIST_LOG(ubchist, "returning 0", 0,0,0,0);
2181 return (0);
2182
2183 tryagain:
2184 /*
2185 * We can't write the pages, for whatever reason.
2186 * Clean up after ourselves, and make the caller try again.
2187 */
2188 simple_lock(&vp->v_interlock);
2189
2190 /* Tell why we're here, if we know */
2191 if (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE)
2192 DLOG((DLOG_PAGE, "lfs_gop_write: clean pages dirtied\n"));
2193 else if ((pgs[0]->offset & fs->lfs_bmask) != 0)
2194 DLOG((DLOG_PAGE, "lfs_gop_write: not on block boundary\n"));
2195 else if (startoffset >= eof)
2196 DLOG((DLOG_PAGE, "lfs_gop_write: ino %d start 0x%" PRIx64
2197 " eof 0x%" PRIx64 " npages=%d\n", VTOI(vp)->i_number,
2198 pgs[0]->offset, eof, npages));
2199 else
2200 DLOG((DLOG_PAGE, "lfs_gop_write: seglock not held\n"));
2201
2202 uvm_lock_pageq();
2203 for (i = 0; i < npages; i++) {
2204 pg = pgs[i];
2205
2206 if (pg->flags & PG_PAGEOUT)
2207 uvmexp.paging--;
2208 if (pg->flags & PG_DELWRI) {
2209 uvm_pageunwire(pg);
2210 }
2211 uvm_pageactivate(pg);
2212 pg->flags &= ~(PG_CLEAN|PG_DELWRI|PG_PAGEOUT|PG_RELEASED);
2213 DLOG((DLOG_PAGE, "pg[%d] = %p\n", i, pg));
2214 DLOG((DLOG_PAGE, "pg[%d]->flags = %x\n", i, pg->flags));
2215 DLOG((DLOG_PAGE, "pg[%d]->pqflags = %x\n", i, pg->pqflags));
2216 DLOG((DLOG_PAGE, "pg[%d]->uanon = %p\n", i, pg->uanon));
2217 DLOG((DLOG_PAGE, "pg[%d]->uobject = %p\n", i, pg->uobject));
2218 DLOG((DLOG_PAGE, "pg[%d]->wire_count = %d\n", i,
2219 pg->wire_count));
2220 DLOG((DLOG_PAGE, "pg[%d]->loan_count = %d\n", i,
2221 pg->loan_count));
2222 }
2223 /* uvm_pageunbusy takes care of PG_BUSY, PG_WANTED */
2224 uvm_page_unbusy(pgs, npages);
2225 uvm_unlock_pageq();
2226 simple_unlock(&vp->v_interlock);
2227 return EAGAIN;
2228 }
2229
2230 /*
2231 * finish vnode/inode initialization.
2232 * used by lfs_vget and lfs_fastvget.
2233 */
2234 void
2235 lfs_vinit(struct mount *mp, struct vnode **vpp)
2236 {
2237 struct vnode *vp = *vpp;
2238 struct inode *ip = VTOI(vp);
2239 struct ufsmount *ump = VFSTOUFS(mp);
2240 int i;
2241
2242 ip->i_mode = ip->i_ffs1_mode;
2243 ip->i_ffs_effnlink = ip->i_nlink = ip->i_ffs1_nlink;
2244 ip->i_lfs_osize = ip->i_size = ip->i_ffs1_size;
2245 ip->i_flags = ip->i_ffs1_flags;
2246 ip->i_gen = ip->i_ffs1_gen;
2247 ip->i_uid = ip->i_ffs1_uid;
2248 ip->i_gid = ip->i_ffs1_gid;
2249
2250 ip->i_lfs_effnblks = ip->i_ffs1_blocks;
2251
2252 /*
2253 * Initialize the vnode from the inode, check for aliases. In all
2254 * cases re-init ip, the underlying vnode/inode may have changed.
2255 */
2256 ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
2257 ip = VTOI(vp);
2258
2259 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
2260 if (vp->v_type != VLNK || ip->i_size >= ip->i_ump->um_maxsymlinklen) {
2261 struct lfs *fs = ump->um_lfs;
2262 #ifdef DEBUG
2263 for (i = (ip->i_size + fs->lfs_bsize - 1) >> fs->lfs_bshift;
2264 i < NDADDR; i++) {
2265 if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
2266 i == 0)
2267 continue;
2268 if (ip->i_ffs1_db[i] != 0) {
2269 inconsistent:
2270 lfs_dump_dinode(ip->i_din.ffs1_din);
2271 panic("inconsistent inode");
2272 }
2273 }
2274 for ( ; i < NDADDR + NIADDR; i++) {
2275 if (ip->i_ffs1_ib[i - NDADDR] != 0) {
2276 goto inconsistent;
2277 }
2278 }
2279 #endif /* DEBUG */
2280 for (i = 0; i < NDADDR; i++)
2281 if (ip->i_ffs1_db[i] != 0)
2282 ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
2283 }
2284
2285 #ifdef DIAGNOSTIC
2286 if (vp->v_type == VNON) {
2287 # ifdef DEBUG
2288 lfs_dump_dinode(ip->i_din.ffs1_din);
2289 # endif
2290 panic("lfs_vinit: ino %llu is type VNON! (ifmt=%o)\n",
2291 (unsigned long long)ip->i_number,
2292 (ip->i_mode & IFMT) >> 12);
2293 }
2294 #endif /* DIAGNOSTIC */
2295
2296 /*
2297 * Finish inode initialization now that aliasing has been resolved.
2298 */
2299
2300 ip->i_devvp = ump->um_devvp;
2301 VREF(ip->i_devvp);
2302 genfs_node_init(vp, &lfs_genfsops);
2303 uvm_vnp_setsize(vp, ip->i_size);
2304
2305 /* Initialize hiblk from file size */
2306 ip->i_lfs_hiblk = lblkno(ip->i_lfs, ip->i_size + ip->i_lfs->lfs_bsize - 1) - 1;
2307
2308 *vpp = vp;
2309 }
2310
2311 /*
2312 * Warn if the inode portion of the Ifile is too large to be contained
2313 * in the buffer cache, according to LFS_MAX_BUFS / LFS_MAX_BYTES.
2314 * XXX the estimates don't take multiple LFSs into account.
2315 */
2316 static void
2317 warn_ifile_size(struct lfs *fs)
2318 {
2319 KASSERT(LFS_MAX_BUFS > 0);
2320 KASSERT(LFS_MAX_BYTES > 0);
2321 if (((fs->lfs_ivnode->v_size >> fs->lfs_bshift) - fs->lfs_segtabsz) >
2322 LFS_MAX_BUFS) {
2323 simple_lock(&fs->lfs_interlock);
2324 fs->lfs_flags |= LFS_WARNED;
2325 simple_unlock(&fs->lfs_interlock);
2326 log(LOG_WARNING, "lfs_mountfs: inode part of ifile of length %"
2327 PRId64 " cannot fit in %d buffers\n",
2328 fs->lfs_ivnode->v_size -
2329 (fs->lfs_segtabsz << fs->lfs_bshift),
2330 LFS_MAX_BUFS);
2331 log(LOG_WARNING, "lfs_mountfs: please consider increasing NBUF"
2332 " to at least %" PRId64 "\n",
2333 LFS_INVERSE_MAX_BUFS((fs->lfs_ivnode->v_size >>
2334 fs->lfs_bshift) -
2335 fs->lfs_segtabsz));
2336 } else if ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) > LFS_MAX_BUFS) {
2337 /* Same thing but LOG_NOTICE */
2338 simple_lock(&fs->lfs_interlock);
2339 fs->lfs_flags |= LFS_WARNED;
2340 simple_unlock(&fs->lfs_interlock);
2341 log(LOG_NOTICE, "lfs_mountfs: entire ifile of length %"
2342 PRId64 " cannot fit in %d buffers\n",
2343 fs->lfs_ivnode->v_size, LFS_MAX_BUFS);
2344 log(LOG_NOTICE, "lfs_mountfs: please consider increasing NBUF"
2345 " to at least %" PRId64 "\n",
2346 LFS_INVERSE_MAX_BUFS(fs->lfs_ivnode->v_size >>
2347 fs->lfs_bshift));
2348 }
2349
2350 if (fs->lfs_ivnode->v_size - (fs->lfs_segtabsz << fs->lfs_bshift) >
2351 LFS_MAX_BYTES) {
2352 simple_lock(&fs->lfs_interlock);
2353 fs->lfs_flags |= LFS_WARNED;
2354 simple_unlock(&fs->lfs_interlock);
2355 log(LOG_WARNING, "lfs_mountfs: inode part of ifile of length %"
2356 PRId64 " cannot fit in %lu bytes\n",
2357 fs->lfs_ivnode->v_size - (fs->lfs_segtabsz <<
2358 fs->lfs_bshift),
2359 LFS_MAX_BYTES);
2360 log(LOG_WARNING, "lfs_mountfs: please consider increasing"
2361 " BUFPAGES to at least %" PRId64 "\n",
2362 LFS_INVERSE_MAX_BYTES(fs->lfs_ivnode->v_size -
2363 (fs->lfs_segtabsz <<
2364 fs->lfs_bshift)) >>
2365 PAGE_SHIFT);
2366 } else if(fs->lfs_ivnode->v_size > LFS_MAX_BYTES) {
2367 simple_lock(&fs->lfs_interlock);
2368 fs->lfs_flags |= LFS_WARNED;
2369 simple_unlock(&fs->lfs_interlock);
2370 log(LOG_NOTICE, "lfs_mountfs: entire ifile of length %" PRId64
2371 " cannot fit in %lu buffer bytes\n",
2372 fs->lfs_ivnode->v_size, LFS_MAX_BYTES);
2373 log(LOG_NOTICE, "lfs_mountfs: please consider increasing"
2374 " BUFPAGES to at least %" PRId64 "\n",
2375 LFS_INVERSE_MAX_BYTES(fs->lfs_ivnode->v_size -
2376 (fs->lfs_segtabsz <<
2377 fs->lfs_bshift)) >>
2378 PAGE_SHIFT);
2379 }
2380 }
2381
2382 /*
2383 * Resize the filesystem to contain the specified number of segments.
2384 */
2385 int
2386 lfs_resize_fs(struct lfs *fs, int newnsegs)
2387 {
2388 SEGUSE *sup;
2389 struct buf *bp, *obp;
2390 daddr_t olast, nlast, ilast, noff, start, end;
2391 struct vnode *ivp;
2392 struct inode *ip;
2393 int error, badnews, inc, oldnsegs;
2394 int sbbytes, csbbytes, gain, cgain;
2395 int i;
2396
2397 /* Only support v2 and up */
2398 if (fs->lfs_version < 2)
2399 return EOPNOTSUPP;
2400
2401 /* If we're doing nothing, do it fast */
2402 oldnsegs = fs->lfs_nseg;
2403 if (newnsegs == oldnsegs)
2404 return 0;
2405
2406 /* We always have to have two superblocks */
2407 if (newnsegs <= dtosn(fs, fs->lfs_sboffs[1]))
2408 return EFBIG;
2409
2410 ivp = fs->lfs_ivnode;
2411 ip = VTOI(ivp);
2412 error = 0;
2413
2414 /* Take the segment lock so no one else calls lfs_newseg() */
2415 lfs_seglock(fs, SEGM_PROT);
2416
2417 /*
2418 * Make sure the segments we're going to be losing, if any,
2419 * are in fact empty. We hold the seglock, so their status
2420 * cannot change underneath us. Count the superblocks we lose,
2421 * while we're at it.
2422 */
2423 sbbytes = csbbytes = 0;
2424 cgain = 0;
2425 for (i = newnsegs; i < oldnsegs; i++) {
2426 LFS_SEGENTRY(sup, fs, i, bp);
2427 badnews = sup->su_nbytes || !(sup->su_flags & SEGUSE_INVAL);
2428 if (sup->su_flags & SEGUSE_SUPERBLOCK)
2429 sbbytes += LFS_SBPAD;
2430 if (!(sup->su_flags & SEGUSE_DIRTY)) {
2431 ++cgain;
2432 if (sup->su_flags & SEGUSE_SUPERBLOCK)
2433 csbbytes += LFS_SBPAD;
2434 }
2435 brelse(bp);
2436 if (badnews) {
2437 error = EBUSY;
2438 goto out;
2439 }
2440 }
2441
2442 /* Note old and new segment table endpoints, and old ifile size */
2443 olast = fs->lfs_cleansz + fs->lfs_segtabsz;
2444 nlast = howmany(newnsegs, fs->lfs_sepb) + fs->lfs_cleansz;
2445 ilast = ivp->v_size >> fs->lfs_bshift;
2446 noff = nlast - olast;
2447
2448 /*
2449 * Make sure no one can use the Ifile while we change it around.
2450 * Even after taking the iflock we need to make sure no one still
2451 * is holding Ifile buffers, so we get each one, to drain them.
2452 * (XXX this could be done better.)
2453 */
2454 simple_lock(&fs->lfs_interlock);
2455 lockmgr(&fs->lfs_iflock, LK_EXCLUSIVE, &fs->lfs_interlock);
2456 simple_unlock(&fs->lfs_interlock);
2457 vn_lock(ivp, LK_EXCLUSIVE | LK_RETRY);
2458 for (i = 0; i < ilast; i++) {
2459 bread(ivp, i, fs->lfs_bsize, NOCRED, &bp);
2460 brelse(bp);
2461 }
2462
2463 /* Allocate new Ifile blocks */
2464 for (i = ilast; i < ilast + noff; i++) {
2465 if (VOP_BALLOC(ivp, i * fs->lfs_bsize, fs->lfs_bsize, NOCRED, 0,
2466 &bp) != 0)
2467 panic("balloc extending ifile");
2468 memset(bp->b_data, 0, fs->lfs_bsize);
2469 VOP_BWRITE(bp);
2470 }
2471
2472 /* Register new ifile size */
2473 ip->i_size += noff * fs->lfs_bsize;
2474 ip->i_ffs1_size = ip->i_size;
2475 uvm_vnp_setsize(ivp, ip->i_size);
2476
2477 /* Copy the inode table to its new position */
2478 if (noff != 0) {
2479 if (noff < 0) {
2480 start = nlast;
2481 end = ilast + noff;
2482 inc = 1;
2483 } else {
2484 start = ilast + noff - 1;
2485 end = nlast - 1;
2486 inc = -1;
2487 }
2488 for (i = start; i != end; i += inc) {
2489 if (bread(ivp, i, fs->lfs_bsize, NOCRED, &bp) != 0)
2490 panic("resize: bread dst blk failed");
2491 if (bread(ivp, i - noff, fs->lfs_bsize, NOCRED, &obp))
2492 panic("resize: bread src blk failed");
2493 memcpy(bp->b_data, obp->b_data, fs->lfs_bsize);
2494 VOP_BWRITE(bp);
2495 brelse(obp);
2496 }
2497 }
2498
2499 /* If we are expanding, write the new empty SEGUSE entries */
2500 if (newnsegs > oldnsegs) {
2501 for (i = oldnsegs; i < newnsegs; i++) {
2502 if ((error = bread(ivp, i / fs->lfs_sepb +
2503 fs->lfs_cleansz,
2504 fs->lfs_bsize, NOCRED, &bp)) != 0)
2505 panic("lfs: ifile read: %d", error);
2506 while ((i + 1) % fs->lfs_sepb && i < newnsegs) {
2507 sup = &((SEGUSE *)bp->b_data)[i % fs->lfs_sepb];
2508 memset(sup, 0, sizeof(*sup));
2509 i++;
2510 }
2511 VOP_BWRITE(bp);
2512 }
2513 }
2514
2515 /* Zero out unused superblock offsets */
2516 for (i = 2; i < LFS_MAXNUMSB; i++)
2517 if (dtosn(fs, fs->lfs_sboffs[i]) >= newnsegs)
2518 fs->lfs_sboffs[i] = 0x0;
2519
2520 /*
2521 * Correct superblock entries that depend on fs size.
2522 * The computations of these are as follows:
2523 *
2524 * size = segtod(fs, nseg)
2525 * dsize = segtod(fs, nseg - minfreeseg) - btofsb(#super * LFS_SBPAD)
2526 * bfree = dsize - btofsb(fs, bsize * nseg / 2) - blocks_actually_used
2527 * avail = segtod(fs, nclean) - btofsb(#clean_super * LFS_SBPAD)
2528 * + (segtod(fs, 1) - (offset - curseg))
2529 * - segtod(fs, minfreeseg - (minfreeseg / 2))
2530 *
2531 * XXX - we should probably adjust minfreeseg as well.
2532 */
2533 gain = (newnsegs - oldnsegs);
2534 fs->lfs_nseg = newnsegs;
2535 fs->lfs_segtabsz = nlast - fs->lfs_cleansz;
2536 fs->lfs_size += gain * btofsb(fs, fs->lfs_ssize);
2537 fs->lfs_dsize += gain * btofsb(fs, fs->lfs_ssize) - btofsb(fs, sbbytes);
2538 fs->lfs_bfree += gain * btofsb(fs, fs->lfs_ssize) - btofsb(fs, sbbytes)
2539 - gain * btofsb(fs, fs->lfs_bsize / 2);
2540 if (gain > 0) {
2541 fs->lfs_nclean += gain;
2542 fs->lfs_avail += gain * btofsb(fs, fs->lfs_ssize);
2543 } else {
2544 fs->lfs_nclean -= cgain;
2545 fs->lfs_avail -= cgain * btofsb(fs, fs->lfs_ssize) -
2546 btofsb(fs, csbbytes);
2547 }
2548
2549 /* Resize segment flag cache */
2550 fs->lfs_suflags[0] = (u_int32_t *)realloc(fs->lfs_suflags[0],
2551 fs->lfs_nseg * sizeof(u_int32_t),
2552 M_SEGMENT, M_WAITOK);
2553 fs->lfs_suflags[1] = (u_int32_t *)realloc(fs->lfs_suflags[0],
2554 fs->lfs_nseg * sizeof(u_int32_t),
2555 M_SEGMENT, M_WAITOK);
2556 for (i = oldnsegs; i < newnsegs; i++)
2557 fs->lfs_suflags[0][i] = fs->lfs_suflags[1][i] = 0x0;
2558
2559 /* Truncate Ifile if necessary */
2560 if (noff < 0)
2561 VOP_TRUNCATE(ivp, ivp->v_size + (noff << fs->lfs_bshift), 0,
2562 NOCRED, curproc);
2563
2564 /* Update cleaner info so the cleaner can die */
2565 bread(ivp, 0, fs->lfs_bsize, NOCRED, &bp);
2566 ((CLEANERINFO *)bp->b_data)->clean = fs->lfs_nclean;
2567 ((CLEANERINFO *)bp->b_data)->dirty = fs->lfs_nseg - fs->lfs_nclean;
2568 VOP_BWRITE(bp);
2569
2570 /* Let Ifile accesses proceed */
2571 VOP_UNLOCK(ivp, 0);
2572 simple_lock(&fs->lfs_interlock);
2573 lockmgr(&fs->lfs_iflock, LK_RELEASE, &fs->lfs_interlock);
2574 simple_unlock(&fs->lfs_interlock);
2575
2576 out:
2577 lfs_segunlock(fs);
2578 return error;
2579 }
2580