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