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