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