lfs_vfsops.c revision 1.237 1 /* $NetBSD: lfs_vfsops.c,v 1.237 2007/07/09 21:11:34 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2007 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.237 2007/07/09 21:11:34 ad 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 bool lfs_issequential_hole(const struct ufsmount *,
120 daddr_t, daddr_t);
121
122 static int lfs_mountfs(struct vnode *, struct mount *, struct lwp *);
123
124 extern const struct vnodeopv_desc lfs_vnodeop_opv_desc;
125 extern const struct vnodeopv_desc lfs_specop_opv_desc;
126 extern const struct vnodeopv_desc lfs_fifoop_opv_desc;
127
128 pid_t lfs_writer_daemon = 0;
129 int lfs_do_flush = 0;
130 #ifdef LFS_KERNEL_RFW
131 int lfs_do_rfw = 0;
132 #endif
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 vfs_stdsuspendctl,
160 lfs_vnodeopv_descs,
161 0,
162 { NULL, NULL },
163 };
164 VFS_ATTACH(lfs_vfsops);
165
166 const struct genfs_ops lfs_genfsops = {
167 .gop_size = lfs_gop_size,
168 .gop_alloc = ufs_gop_alloc,
169 .gop_write = lfs_gop_write,
170 .gop_markupdate = ufs_gop_markupdate,
171 };
172
173 static const struct ufs_ops lfs_ufsops = {
174 .uo_itimes = NULL,
175 .uo_update = lfs_update,
176 .uo_truncate = lfs_truncate,
177 .uo_valloc = lfs_valloc,
178 .uo_vfree = lfs_vfree,
179 .uo_balloc = lfs_balloc,
180 };
181
182 /*
183 * XXX Same structure as FFS inodes? Should we share a common pool?
184 */
185 struct pool lfs_inode_pool;
186 struct pool lfs_dinode_pool;
187 struct pool lfs_inoext_pool;
188 struct pool lfs_lbnentry_pool;
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 fsflags;
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 fsflags = 0;
226 if ((fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
227 lfs_dirvcount > LFS_MAX_DIROP) &&
228 fs->lfs_dirops == 0)
229 fsflags |= SEGM_CKP;
230 if (fs->lfs_pdflush) {
231 DLOG((DLOG_FLUSH, "lfs_writerd: pdflush set\n"));
232 fs->lfs_pdflush = 0;
233 lfs_flush_fs(fs, fsflags);
234 simple_unlock(&fs->lfs_interlock);
235 } else if (!TAILQ_EMPTY(&fs->lfs_pchainhd)) {
236 DLOG((DLOG_FLUSH, "lfs_writerd: pchain non-empty\n"));
237 simple_unlock(&fs->lfs_interlock);
238 lfs_writer_enter(fs, "wrdirop");
239 lfs_flush_pchain(fs);
240 lfs_writer_leave(fs);
241 } else
242 simple_unlock(&fs->lfs_interlock);
243 }
244
245 simple_lock(&mountlist_slock);
246 nmp = CIRCLEQ_NEXT(mp, mnt_list);
247 vfs_unbusy(mp);
248 }
249 simple_unlock(&mountlist_slock);
250
251 /*
252 * If global state wants a flush, flush everything.
253 */
254 simple_lock(&lfs_subsys_lock);
255 loopcount = 0;
256 if (lfs_do_flush || locked_queue_count > LFS_MAX_BUFS ||
257 locked_queue_bytes > LFS_MAX_BYTES ||
258 lfs_subsys_pages > LFS_MAX_PAGES) {
259
260 if (lfs_do_flush) {
261 DLOG((DLOG_FLUSH, "daemon: lfs_do_flush\n"));
262 }
263 if (locked_queue_count > LFS_MAX_BUFS) {
264 DLOG((DLOG_FLUSH, "daemon: lqc = %d, max %d\n",
265 locked_queue_count, LFS_MAX_BUFS));
266 }
267 if (locked_queue_bytes > LFS_MAX_BYTES) {
268 DLOG((DLOG_FLUSH, "daemon: lqb = %ld, max %ld\n",
269 locked_queue_bytes, LFS_MAX_BYTES));
270 }
271 if (lfs_subsys_pages > LFS_MAX_PAGES) {
272 DLOG((DLOG_FLUSH, "daemon: lssp = %d, max %d\n",
273 lfs_subsys_pages, LFS_MAX_PAGES));
274 }
275
276 lfs_flush(NULL, SEGM_WRITERD, 0);
277 lfs_do_flush = 0;
278 }
279 }
280 /* NOTREACHED */
281 }
282
283 /*
284 * Initialize the filesystem, most work done by ufs_init.
285 */
286 void
287 lfs_init()
288 {
289
290 malloc_type_attach(M_SEGMENT);
291 pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
292 "lfsinopl", &pool_allocator_nointr, IPL_NONE);
293 pool_init(&lfs_dinode_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
294 "lfsdinopl", &pool_allocator_nointr, IPL_NONE);
295 pool_init(&lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0,
296 "lfsinoextpl", &pool_allocator_nointr, IPL_NONE);
297 pool_init(&lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0,
298 "lfslbnpool", &pool_allocator_nointr, IPL_NONE);
299 ufs_init();
300
301 #ifdef DEBUG
302 memset(lfs_log, 0, sizeof(lfs_log));
303 #endif
304 simple_lock_init(&lfs_subsys_lock);
305 }
306
307 void
308 lfs_reinit()
309 {
310 ufs_reinit();
311 }
312
313 void
314 lfs_done()
315 {
316
317 ufs_done();
318 pool_destroy(&lfs_inode_pool);
319 pool_destroy(&lfs_dinode_pool);
320 pool_destroy(&lfs_inoext_pool);
321 pool_destroy(&lfs_lbnentry_pool);
322 malloc_type_detach(M_SEGMENT);
323 }
324
325 /*
326 * Called by main() when ufs is going to be mounted as root.
327 */
328 int
329 lfs_mountroot()
330 {
331 extern struct vnode *rootvp;
332 struct mount *mp;
333 struct lwp *l = curlwp; /* XXX */
334 int error;
335
336 if (device_class(root_device) != DV_DISK)
337 return (ENODEV);
338
339 if (rootdev == NODEV)
340 return (ENODEV);
341 if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
342 vrele(rootvp);
343 return (error);
344 }
345 if ((error = lfs_mountfs(rootvp, mp, l))) {
346 mp->mnt_op->vfs_refcount--;
347 vfs_unbusy(mp);
348 free(mp, M_MOUNT);
349 return (error);
350 }
351 simple_lock(&mountlist_slock);
352 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
353 simple_unlock(&mountlist_slock);
354 (void)lfs_statvfs(mp, &mp->mnt_stat, l);
355 vfs_unbusy(mp);
356 setrootfstime((time_t)(VFSTOUFS(mp)->um_lfs->lfs_tstamp));
357 return (0);
358 }
359
360 /*
361 * VFS Operations.
362 *
363 * mount system call
364 */
365 int
366 lfs_mount(struct mount *mp, const char *path, void *data, struct nameidata *ndp, struct lwp *l)
367 {
368 struct vnode *devvp;
369 struct ufs_args args;
370 struct ufsmount *ump = NULL;
371 struct lfs *fs = NULL; /* LFS */
372 int error, update;
373 mode_t accessmode;
374
375 if (mp->mnt_flag & MNT_GETARGS) {
376 ump = VFSTOUFS(mp);
377 if (ump == NULL)
378 return EIO;
379 args.fspec = NULL;
380 return copyout(&args, data, sizeof(args));
381 }
382 error = copyin(data, &args, sizeof (struct ufs_args));
383 if (error)
384 return (error);
385
386 update = mp->mnt_flag & MNT_UPDATE;
387
388 /* Check arguments */
389 if (args.fspec != NULL) {
390 /*
391 * Look up the name and verify that it's sane.
392 */
393 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, l);
394 if ((error = namei(ndp)) != 0)
395 return (error);
396 devvp = ndp->ni_vp;
397
398 if (!update) {
399 /*
400 * Be sure this is a valid block device
401 */
402 if (devvp->v_type != VBLK)
403 error = ENOTBLK;
404 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
405 error = ENXIO;
406 } else {
407 /*
408 * Be sure we're still naming the same device
409 * used for our initial mount
410 */
411 ump = VFSTOUFS(mp);
412 if (devvp != ump->um_devvp)
413 error = EINVAL;
414 }
415 } else {
416 if (!update) {
417 /* New mounts must have a filename for the device */
418 return (EINVAL);
419 } else {
420 /* Use the extant mount */
421 ump = VFSTOUFS(mp);
422 devvp = ump->um_devvp;
423 vref(devvp);
424 }
425 }
426
427
428 /*
429 * If mount by non-root, then verify that user has necessary
430 * permissions on the device.
431 */
432 if (error == 0 && kauth_authorize_generic(l->l_cred,
433 KAUTH_GENERIC_ISSUSER, NULL) != 0) {
434 accessmode = VREAD;
435 if (update ?
436 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
437 (mp->mnt_flag & MNT_RDONLY) == 0)
438 accessmode |= VWRITE;
439 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
440 error = VOP_ACCESS(devvp, accessmode, l->l_cred, l);
441 VOP_UNLOCK(devvp, 0);
442 }
443
444 if (error) {
445 vrele(devvp);
446 return (error);
447 }
448
449 if (!update) {
450 int flags;
451
452 /*
453 * Disallow multiple mounts of the same device.
454 * Disallow mounting of a device that is currently in use
455 * (except for root, which might share swap device for
456 * miniroot).
457 */
458 error = vfs_mountedon(devvp);
459 if (error)
460 goto fail;
461 if (vcount(devvp) > 1 && devvp != rootvp) {
462 error = EBUSY;
463 goto fail;
464 }
465 if (mp->mnt_flag & MNT_RDONLY)
466 flags = FREAD;
467 else
468 flags = FREAD|FWRITE;
469 error = VOP_OPEN(devvp, flags, FSCRED, l);
470 if (error)
471 goto fail;
472 error = lfs_mountfs(devvp, mp, l); /* LFS */
473 if (error) {
474 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
475 (void)VOP_CLOSE(devvp, flags, NOCRED, l);
476 VOP_UNLOCK(devvp, 0);
477 goto fail;
478 }
479
480 ump = VFSTOUFS(mp);
481 fs = ump->um_lfs;
482 } else {
483 /*
484 * Update the mount.
485 */
486
487 /*
488 * The initial mount got a reference on this
489 * device, so drop the one obtained via
490 * namei(), above.
491 */
492 vrele(devvp);
493
494 ump = VFSTOUFS(mp);
495 fs = ump->um_lfs;
496 if (fs->lfs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
497 /*
498 * Changing from read-only to read/write.
499 * Note in the superblocks that we're writing.
500 */
501 fs->lfs_ronly = 0;
502 if (fs->lfs_pflags & LFS_PF_CLEAN) {
503 fs->lfs_pflags &= ~LFS_PF_CLEAN;
504 lfs_writesuper(fs, fs->lfs_sboffs[0]);
505 lfs_writesuper(fs, fs->lfs_sboffs[1]);
506 }
507 }
508 if (args.fspec == NULL)
509 return EINVAL;
510 }
511
512 error = set_statvfs_info(path, UIO_USERSPACE, args.fspec,
513 UIO_USERSPACE, mp, l);
514 if (error == 0)
515 (void)strncpy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname,
516 sizeof(fs->lfs_fsmnt));
517 return error;
518
519 fail:
520 vrele(devvp);
521 return (error);
522 }
523
524
525 /*
526 * Common code for mount and mountroot
527 * LFS specific
528 */
529 int
530 lfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
531 {
532 struct dlfs *tdfs, *dfs, *adfs;
533 struct lfs *fs;
534 struct ufsmount *ump;
535 struct vnode *vp;
536 struct buf *bp, *abp;
537 struct partinfo dpart;
538 dev_t dev;
539 int error, i, ronly, secsize, fsbsize;
540 kauth_cred_t cred;
541 CLEANERINFO *cip;
542 SEGUSE *sup;
543 daddr_t sb_addr;
544
545 cred = l ? l->l_cred : NOCRED;
546
547 /*
548 * Flush out any old buffers remaining from a previous use.
549 */
550 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
551 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
552 VOP_UNLOCK(devvp, 0);
553 if (error)
554 return (error);
555
556 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
557 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) != 0)
558 secsize = DEV_BSIZE;
559 else
560 secsize = dpart.disklab->d_secsize;
561
562 /* Don't free random space on error. */
563 bp = NULL;
564 abp = NULL;
565 ump = NULL;
566
567 sb_addr = LFS_LABELPAD / secsize;
568 while (1) {
569 /* Read in the superblock. */
570 error = bread(devvp, sb_addr, LFS_SBPAD, cred, &bp);
571 if (error)
572 goto out;
573 dfs = (struct dlfs *)bp->b_data;
574
575 /* Check the basics. */
576 if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize > MAXBSIZE ||
577 dfs->dlfs_version > LFS_VERSION ||
578 dfs->dlfs_bsize < sizeof(struct dlfs)) {
579 DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock sanity failed\n"));
580 error = EINVAL; /* XXX needs translation */
581 goto out;
582 }
583 if (dfs->dlfs_inodefmt > LFS_MAXINODEFMT) {
584 DLOG((DLOG_MOUNT, "lfs_mountfs: unknown inode format %d\n",
585 dfs->dlfs_inodefmt));
586 error = EINVAL;
587 goto out;
588 }
589
590 if (dfs->dlfs_version == 1)
591 fsbsize = secsize;
592 else {
593 fsbsize = 1 << (dfs->dlfs_bshift - dfs->dlfs_blktodb +
594 dfs->dlfs_fsbtodb);
595 /*
596 * Could be, if the frag size is large enough, that we
597 * don't have the "real" primary superblock. If that's
598 * the case, get the real one, and try again.
599 */
600 if (sb_addr != dfs->dlfs_sboffs[0] <<
601 dfs->dlfs_fsbtodb) {
602 DLOG((DLOG_MOUNT, "lfs_mountfs: sb daddr"
603 " 0x%llx is not right, trying 0x%llx\n",
604 (long long)sb_addr,
605 (long long)(dfs->dlfs_sboffs[0] <<
606 dfs->dlfs_fsbtodb)));
607 sb_addr = dfs->dlfs_sboffs[0] <<
608 dfs->dlfs_fsbtodb;
609 brelse(bp);
610 continue;
611 }
612 }
613 break;
614 }
615
616 /*
617 * Check the second superblock to see which is newer; then mount
618 * using the older of the two. This is necessary to ensure that
619 * the filesystem is valid if it was not unmounted cleanly.
620 */
621
622 if (dfs->dlfs_sboffs[1] &&
623 dfs->dlfs_sboffs[1] - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize)
624 {
625 error = bread(devvp, dfs->dlfs_sboffs[1] * (fsbsize / secsize),
626 LFS_SBPAD, cred, &abp);
627 if (error)
628 goto out;
629 adfs = (struct dlfs *)abp->b_data;
630
631 if (dfs->dlfs_version == 1) {
632 /* 1s resolution comparison */
633 if (adfs->dlfs_tstamp < dfs->dlfs_tstamp)
634 tdfs = adfs;
635 else
636 tdfs = dfs;
637 } else {
638 /* monotonic infinite-resolution comparison */
639 if (adfs->dlfs_serial < dfs->dlfs_serial)
640 tdfs = adfs;
641 else
642 tdfs = dfs;
643 }
644
645 /* Check the basics. */
646 if (tdfs->dlfs_magic != LFS_MAGIC ||
647 tdfs->dlfs_bsize > MAXBSIZE ||
648 tdfs->dlfs_version > LFS_VERSION ||
649 tdfs->dlfs_bsize < sizeof(struct dlfs)) {
650 DLOG((DLOG_MOUNT, "lfs_mountfs: alt superblock"
651 " sanity failed\n"));
652 error = EINVAL; /* XXX needs translation */
653 goto out;
654 }
655 } else {
656 DLOG((DLOG_MOUNT, "lfs_mountfs: invalid alt superblock"
657 " daddr=0x%x\n", dfs->dlfs_sboffs[1]));
658 error = EINVAL;
659 goto out;
660 }
661
662 /* Allocate the mount structure, copy the superblock into it. */
663 fs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK | M_ZERO);
664 memcpy(&fs->lfs_dlfs, tdfs, sizeof(struct dlfs));
665
666 /* Compatibility */
667 if (fs->lfs_version < 2) {
668 fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
669 fs->lfs_ibsize = fs->lfs_bsize;
670 fs->lfs_start = fs->lfs_sboffs[0];
671 fs->lfs_tstamp = fs->lfs_otstamp;
672 fs->lfs_fsbtodb = 0;
673 }
674 if (fs->lfs_resvseg == 0)
675 fs->lfs_resvseg = MIN(fs->lfs_minfreeseg - 1, \
676 MAX(MIN_RESV_SEGS, fs->lfs_minfreeseg / 2 + 1));
677
678 /*
679 * If we aren't going to be able to write meaningfully to this
680 * filesystem, and were not mounted readonly, bomb out now.
681 */
682 if (fsbtob(fs, LFS_NRESERVE(fs)) > LFS_MAX_BYTES && !ronly) {
683 DLOG((DLOG_MOUNT, "lfs_mount: to mount this filesystem read/write,"
684 " we need BUFPAGES >= %lld\n",
685 (long long)((bufmem_hiwater / bufmem_lowater) *
686 LFS_INVERSE_MAX_BYTES(
687 fsbtob(fs, LFS_NRESERVE(fs))) >> PAGE_SHIFT)));
688 free(fs, M_UFSMNT);
689 error = EFBIG; /* XXX needs translation */
690 goto out;
691 }
692
693 /* Before rolling forward, lock so vget will sleep for other procs */
694 if (l != NULL) {
695 fs->lfs_flags = LFS_NOTYET;
696 fs->lfs_rfpid = l->l_proc->p_pid;
697 }
698
699 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
700 ump->um_lfs = fs;
701 ump->um_ops = &lfs_ufsops;
702 ump->um_fstype = UFS1;
703 if (sizeof(struct lfs) < LFS_SBPAD) { /* XXX why? */
704 bp->b_flags |= B_INVAL;
705 abp->b_flags |= B_INVAL;
706 }
707 brelse(bp);
708 bp = NULL;
709 brelse(abp);
710 abp = NULL;
711
712 /* Set up the I/O information */
713 fs->lfs_devbsize = secsize;
714 fs->lfs_iocount = 0;
715 fs->lfs_diropwait = 0;
716 fs->lfs_activesb = 0;
717 fs->lfs_uinodes = 0;
718 fs->lfs_ravail = 0;
719 fs->lfs_favail = 0;
720 fs->lfs_sbactive = 0;
721
722 /* Set up the ifile and lock aflags */
723 fs->lfs_doifile = 0;
724 fs->lfs_writer = 0;
725 fs->lfs_dirops = 0;
726 fs->lfs_nadirop = 0;
727 fs->lfs_seglock = 0;
728 fs->lfs_pdflush = 0;
729 fs->lfs_sleepers = 0;
730 fs->lfs_pages = 0;
731 simple_lock_init(&fs->lfs_interlock);
732 rw_init(&fs->lfs_fraglock);
733 lockinit(&fs->lfs_iflock, PINOD, "lfs_iflock", 0, 0);
734 lockinit(&fs->lfs_stoplock, PINOD, "lfs_stoplock", 0, 0);
735
736 /* Set the file system readonly/modify bits. */
737 fs->lfs_ronly = ronly;
738 if (ronly == 0)
739 fs->lfs_fmod = 1;
740
741 /* Initialize the mount structure. */
742 dev = devvp->v_rdev;
743 mp->mnt_data = ump;
744 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
745 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_LFS);
746 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
747 mp->mnt_stat.f_namemax = LFS_MAXNAMLEN;
748 mp->mnt_stat.f_iosize = fs->lfs_bsize;
749 mp->mnt_flag |= MNT_LOCAL;
750 mp->mnt_fs_bshift = fs->lfs_bshift;
751 ump->um_flags = 0;
752 ump->um_mountp = mp;
753 ump->um_dev = dev;
754 ump->um_devvp = devvp;
755 ump->um_bptrtodb = fs->lfs_fsbtodb;
756 ump->um_seqinc = fragstofsb(fs, fs->lfs_frag);
757 ump->um_nindir = fs->lfs_nindir;
758 ump->um_lognindir = ffs(fs->lfs_nindir) - 1;
759 for (i = 0; i < MAXQUOTAS; i++)
760 ump->um_quotas[i] = NULLVP;
761 ump->um_maxsymlinklen = fs->lfs_maxsymlinklen;
762 ump->um_dirblksiz = DIRBLKSIZ;
763 ump->um_maxfilesize = fs->lfs_maxfilesize;
764 if (ump->um_maxsymlinklen > 0)
765 mp->mnt_iflag |= IMNT_DTYPE;
766 devvp->v_specmountpoint = mp;
767
768 /* Set up reserved memory for pageout */
769 lfs_setup_resblks(fs);
770 /* Set up vdirop tailq */
771 TAILQ_INIT(&fs->lfs_dchainhd);
772 /* and paging tailq */
773 TAILQ_INIT(&fs->lfs_pchainhd);
774 /* and delayed segment accounting for truncation list */
775 LIST_INIT(&fs->lfs_segdhd);
776
777 /*
778 * We use the ifile vnode for almost every operation. Instead of
779 * retrieving it from the hash table each time we retrieve it here,
780 * artificially increment the reference count and keep a pointer
781 * to it in the incore copy of the superblock.
782 */
783 if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0) {
784 DLOG((DLOG_MOUNT, "lfs_mountfs: ifile vget failed, error=%d\n", error));
785 goto out;
786 }
787 fs->lfs_ivnode = vp;
788 VREF(vp);
789
790 /* Set up inode bitmap and order free list */
791 lfs_order_freelist(fs);
792
793 /* Set up segment usage flags for the autocleaner. */
794 fs->lfs_nactive = 0;
795 fs->lfs_suflags = (u_int32_t **)malloc(2 * sizeof(u_int32_t *),
796 M_SEGMENT, M_WAITOK);
797 fs->lfs_suflags[0] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
798 M_SEGMENT, M_WAITOK);
799 fs->lfs_suflags[1] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
800 M_SEGMENT, M_WAITOK);
801 memset(fs->lfs_suflags[1], 0, fs->lfs_nseg * sizeof(u_int32_t));
802 for (i = 0; i < fs->lfs_nseg; i++) {
803 int changed;
804
805 LFS_SEGENTRY(sup, fs, i, bp);
806 changed = 0;
807 if (!ronly) {
808 if (sup->su_nbytes == 0 &&
809 !(sup->su_flags & SEGUSE_EMPTY)) {
810 sup->su_flags |= SEGUSE_EMPTY;
811 ++changed;
812 } else if (!(sup->su_nbytes == 0) &&
813 (sup->su_flags & SEGUSE_EMPTY)) {
814 sup->su_flags &= ~SEGUSE_EMPTY;
815 ++changed;
816 }
817 if (sup->su_flags & (SEGUSE_ACTIVE|SEGUSE_INVAL)) {
818 sup->su_flags &= ~(SEGUSE_ACTIVE|SEGUSE_INVAL);
819 ++changed;
820 }
821 }
822 fs->lfs_suflags[0][i] = sup->su_flags;
823 if (changed)
824 LFS_WRITESEGENTRY(sup, fs, i, bp);
825 else
826 brelse(bp);
827 }
828
829 #ifdef LFS_KERNEL_RFW
830 lfs_roll_forward(fs, mp, l);
831 #endif
832
833 /* If writing, sb is not clean; record in case of immediate crash */
834 if (!fs->lfs_ronly) {
835 fs->lfs_pflags &= ~LFS_PF_CLEAN;
836 lfs_writesuper(fs, fs->lfs_sboffs[0]);
837 lfs_writesuper(fs, fs->lfs_sboffs[1]);
838 }
839
840 /* Allow vget now that roll-forward is complete */
841 fs->lfs_flags &= ~(LFS_NOTYET);
842 wakeup(&fs->lfs_flags);
843
844 /*
845 * Initialize the ifile cleaner info with information from
846 * the superblock.
847 */
848 LFS_CLEANERINFO(cip, fs, bp);
849 cip->clean = fs->lfs_nclean;
850 cip->dirty = fs->lfs_nseg - fs->lfs_nclean;
851 cip->avail = fs->lfs_avail;
852 cip->bfree = fs->lfs_bfree;
853 (void) LFS_BWRITE_LOG(bp); /* Ifile */
854
855 /*
856 * Mark the current segment as ACTIVE, since we're going to
857 * be writing to it.
858 */
859 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp);
860 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
861 fs->lfs_nactive++;
862 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp); /* Ifile */
863
864 /* Now that roll-forward is done, unlock the Ifile */
865 vput(vp);
866
867 /* Start the pagedaemon-anticipating daemon */
868 if (lfs_writer_daemon == 0 &&
869 kthread_create(PRI_NONE, 0, NULL, lfs_writerd, NULL, NULL,
870 "lfs_writer") != 0)
871 panic("fork lfs_writer");
872
873 return (0);
874
875 out:
876 if (bp)
877 brelse(bp);
878 if (abp)
879 brelse(abp);
880 if (ump) {
881 free(ump->um_lfs, M_UFSMNT);
882 free(ump, M_UFSMNT);
883 mp->mnt_data = NULL;
884 }
885
886 return (error);
887 }
888
889 /*
890 * unmount system call
891 */
892 int
893 lfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
894 {
895 struct ufsmount *ump;
896 struct lfs *fs;
897 int error, flags, ronly;
898 int s;
899
900 flags = 0;
901 if (mntflags & MNT_FORCE)
902 flags |= FORCECLOSE;
903
904 ump = VFSTOUFS(mp);
905 fs = ump->um_lfs;
906
907 /* Two checkpoints */
908 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
909 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
910
911 /* wake up the cleaner so it can die */
912 lfs_wakeup_cleaner(fs);
913 simple_lock(&fs->lfs_interlock);
914 while (fs->lfs_sleepers)
915 ltsleep(&fs->lfs_sleepers, PRIBIO + 1, "lfs_sleepers", 0,
916 &fs->lfs_interlock);
917 simple_unlock(&fs->lfs_interlock);
918
919 #ifdef QUOTA
920 if (mp->mnt_flag & MNT_QUOTA) {
921 int i;
922 error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
923 if (error)
924 return (error);
925 for (i = 0; i < MAXQUOTAS; i++) {
926 if (ump->um_quotas[i] == NULLVP)
927 continue;
928 quotaoff(l, mp, i);
929 }
930 /*
931 * Here we fall through to vflush again to ensure
932 * that we have gotten rid of all the system vnodes.
933 */
934 }
935 #endif
936 if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
937 return (error);
938 if ((error = VFS_SYNC(mp, 1, l->l_cred, l)) != 0)
939 return (error);
940 s = splbio();
941 if (LIST_FIRST(&fs->lfs_ivnode->v_dirtyblkhd))
942 panic("lfs_unmount: still dirty blocks on ifile vnode");
943 splx(s);
944
945 /* Explicitly write the superblock, to update serial and pflags */
946 fs->lfs_pflags |= LFS_PF_CLEAN;
947 lfs_writesuper(fs, fs->lfs_sboffs[0]);
948 lfs_writesuper(fs, fs->lfs_sboffs[1]);
949 simple_lock(&fs->lfs_interlock);
950 while (fs->lfs_iocount)
951 ltsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0,
952 &fs->lfs_interlock);
953 simple_unlock(&fs->lfs_interlock);
954
955 /* Finish with the Ifile, now that we're done with it */
956 vrele(fs->lfs_ivnode);
957 vgone(fs->lfs_ivnode);
958
959 ronly = !fs->lfs_ronly;
960 if (ump->um_devvp->v_type != VBAD)
961 ump->um_devvp->v_specmountpoint = NULL;
962 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
963 error = VOP_CLOSE(ump->um_devvp,
964 ronly ? FREAD : FREAD|FWRITE, NOCRED, l);
965 vput(ump->um_devvp);
966
967 /* Complain about page leakage */
968 if (fs->lfs_pages > 0)
969 printf("lfs_unmount: still claim %d pages (%d in subsystem)\n",
970 fs->lfs_pages, lfs_subsys_pages);
971
972 /* Free per-mount data structures */
973 free(fs->lfs_ino_bitmap, M_SEGMENT);
974 free(fs->lfs_suflags[0], M_SEGMENT);
975 free(fs->lfs_suflags[1], M_SEGMENT);
976 free(fs->lfs_suflags, M_SEGMENT);
977 lfs_free_resblks(fs);
978 rw_destroy(&fs->lfs_fraglock);
979 free(fs, M_UFSMNT);
980 free(ump, M_UFSMNT);
981
982 mp->mnt_data = NULL;
983 mp->mnt_flag &= ~MNT_LOCAL;
984 return (error);
985 }
986
987 /*
988 * Get file system statistics.
989 *
990 * NB: We don't lock to access the superblock here, because it's not
991 * really that important if we get it wrong.
992 */
993 int
994 lfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
995 {
996 struct lfs *fs;
997 struct ufsmount *ump;
998
999 ump = VFSTOUFS(mp);
1000 fs = ump->um_lfs;
1001 if (fs->lfs_magic != LFS_MAGIC)
1002 panic("lfs_statvfs: magic");
1003
1004 sbp->f_bsize = fs->lfs_bsize;
1005 sbp->f_frsize = fs->lfs_fsize;
1006 sbp->f_iosize = fs->lfs_bsize;
1007 sbp->f_blocks = fsbtofrags(fs, LFS_EST_NONMETA(fs) - VTOI(fs->lfs_ivnode)->i_lfs_effnblks);
1008
1009 sbp->f_bfree = fsbtofrags(fs, LFS_EST_BFREE(fs));
1010 KASSERT(sbp->f_bfree <= fs->lfs_dsize);
1011 #if 0
1012 if (sbp->f_bfree < 0)
1013 sbp->f_bfree = 0;
1014 #endif
1015
1016 sbp->f_bresvd = fsbtofrags(fs, LFS_EST_RSVD(fs));
1017 if (sbp->f_bfree > sbp->f_bresvd)
1018 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1019 else
1020 sbp->f_bavail = 0;
1021
1022 sbp->f_files = fs->lfs_bfree / btofsb(fs, fs->lfs_ibsize) * INOPB(fs);
1023 sbp->f_ffree = sbp->f_files - fs->lfs_nfiles;
1024 sbp->f_favail = sbp->f_ffree;
1025 sbp->f_fresvd = 0;
1026 copy_statvfs_info(sbp, mp);
1027 return (0);
1028 }
1029
1030 /*
1031 * Go through the disk queues to initiate sandbagged IO;
1032 * go through the inodes to write those that have been modified;
1033 * initiate the writing of the super block if it has been modified.
1034 *
1035 * Note: we are always called with the filesystem marked `MPBUSY'.
1036 */
1037 int
1038 lfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred,
1039 struct lwp *l)
1040 {
1041 int error;
1042 struct lfs *fs;
1043
1044 fs = VFSTOUFS(mp)->um_lfs;
1045 if (fs->lfs_ronly)
1046 return 0;
1047
1048 /* Snapshots should not hose the syncer */
1049 /*
1050 * XXX Sync can block here anyway, since we don't have a very
1051 * XXX good idea of how much data is pending. If it's more
1052 * XXX than a segment and lfs_nextseg is close to the end of
1053 * XXX the log, we'll likely block.
1054 */
1055 simple_lock(&fs->lfs_interlock);
1056 if (fs->lfs_nowrap && fs->lfs_nextseg < fs->lfs_curseg) {
1057 simple_unlock(&fs->lfs_interlock);
1058 return 0;
1059 }
1060 simple_unlock(&fs->lfs_interlock);
1061
1062 lfs_writer_enter(fs, "lfs_dirops");
1063
1064 /* All syncs must be checkpoints until roll-forward is implemented. */
1065 DLOG((DLOG_FLUSH, "lfs_sync at 0x%x\n", fs->lfs_offset));
1066 error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
1067 lfs_writer_leave(fs);
1068 #ifdef QUOTA
1069 qsync(mp);
1070 #endif
1071 return (error);
1072 }
1073
1074 extern kmutex_t ufs_hashlock;
1075
1076 /*
1077 * Look up an LFS dinode number to find its incore vnode. If not already
1078 * in core, read it in from the specified device. Return the inode locked.
1079 * Detection and handling of mount points must be done by the calling routine.
1080 */
1081 int
1082 lfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1083 {
1084 struct lfs *fs;
1085 struct ufs1_dinode *dip;
1086 struct inode *ip;
1087 struct buf *bp;
1088 struct ifile *ifp;
1089 struct vnode *vp;
1090 struct ufsmount *ump;
1091 daddr_t daddr;
1092 dev_t dev;
1093 int error, retries;
1094 struct timespec ts;
1095
1096 memset(&ts, 0, sizeof ts); /* XXX gcc */
1097
1098 ump = VFSTOUFS(mp);
1099 dev = ump->um_dev;
1100 fs = ump->um_lfs;
1101
1102 /*
1103 * If the filesystem is not completely mounted yet, suspend
1104 * any access requests (wait for roll-forward to complete).
1105 */
1106 simple_lock(&fs->lfs_interlock);
1107 while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
1108 ltsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0,
1109 &fs->lfs_interlock);
1110 simple_unlock(&fs->lfs_interlock);
1111
1112 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1113 return (0);
1114
1115 if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
1116 *vpp = NULL;
1117 return (error);
1118 }
1119
1120 mutex_enter(&ufs_hashlock);
1121 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
1122 mutex_exit(&ufs_hashlock);
1123 ungetnewvnode(vp);
1124 return (0);
1125 }
1126
1127 /* Translate the inode number to a disk address. */
1128 if (ino == LFS_IFILE_INUM)
1129 daddr = fs->lfs_idaddr;
1130 else {
1131 /* XXX bounds-check this too */
1132 LFS_IENTRY(ifp, fs, ino, bp);
1133 daddr = ifp->if_daddr;
1134 if (fs->lfs_version > 1) {
1135 ts.tv_sec = ifp->if_atime_sec;
1136 ts.tv_nsec = ifp->if_atime_nsec;
1137 }
1138
1139 brelse(bp);
1140 if (daddr == LFS_UNUSED_DADDR) {
1141 *vpp = NULLVP;
1142 mutex_exit(&ufs_hashlock);
1143 ungetnewvnode(vp);
1144 return (ENOENT);
1145 }
1146 }
1147
1148 /* Allocate/init new vnode/inode. */
1149 lfs_vcreate(mp, ino, vp);
1150
1151 /*
1152 * Put it onto its hash chain and lock it so that other requests for
1153 * this inode will block if they arrive while we are sleeping waiting
1154 * for old data structures to be purged or for the contents of the
1155 * disk portion of this inode to be read.
1156 */
1157 ip = VTOI(vp);
1158 ufs_ihashins(ip);
1159 mutex_exit(&ufs_hashlock);
1160
1161 /*
1162 * XXX
1163 * This may not need to be here, logically it should go down with
1164 * the i_devvp initialization.
1165 * Ask Kirk.
1166 */
1167 ip->i_lfs = ump->um_lfs;
1168
1169 /* Read in the disk contents for the inode, copy into the inode. */
1170 retries = 0;
1171 again:
1172 error = bread(ump->um_devvp, fsbtodb(fs, daddr),
1173 (fs->lfs_version == 1 ? fs->lfs_bsize : fs->lfs_ibsize),
1174 NOCRED, &bp);
1175 if (error) {
1176 /*
1177 * The inode does not contain anything useful, so it would
1178 * be misleading to leave it on its hash chain. With mode
1179 * still zero, it will be unlinked and returned to the free
1180 * list by vput().
1181 */
1182 vput(vp);
1183 brelse(bp);
1184 *vpp = NULL;
1185 return (error);
1186 }
1187
1188 dip = lfs_ifind(fs, ino, bp);
1189 if (dip == NULL) {
1190 /* Assume write has not completed yet; try again */
1191 bp->b_flags |= B_INVAL;
1192 brelse(bp);
1193 ++retries;
1194 if (retries > LFS_IFIND_RETRIES) {
1195 #ifdef DEBUG
1196 /* If the seglock is held look at the bpp to see
1197 what is there anyway */
1198 simple_lock(&fs->lfs_interlock);
1199 if (fs->lfs_seglock > 0) {
1200 struct buf **bpp;
1201 struct ufs1_dinode *dp;
1202 int i;
1203
1204 for (bpp = fs->lfs_sp->bpp;
1205 bpp != fs->lfs_sp->cbpp; ++bpp) {
1206 if ((*bpp)->b_vp == fs->lfs_ivnode &&
1207 bpp != fs->lfs_sp->bpp) {
1208 /* Inode block */
1209 printf("lfs_vget: block 0x%" PRIx64 ": ",
1210 (*bpp)->b_blkno);
1211 dp = (struct ufs1_dinode *)(*bpp)->b_data;
1212 for (i = 0; i < INOPB(fs); i++)
1213 if (dp[i].di_u.inumber)
1214 printf("%d ", dp[i].di_u.inumber);
1215 printf("\n");
1216 }
1217 }
1218 }
1219 simple_unlock(&fs->lfs_interlock);
1220 #endif /* DEBUG */
1221 panic("lfs_vget: dinode not found");
1222 }
1223 simple_lock(&fs->lfs_interlock);
1224 if (fs->lfs_iocount) {
1225 DLOG((DLOG_VNODE, "lfs_vget: dinode %d not found, retrying...\n", ino));
1226 (void)ltsleep(&fs->lfs_iocount, PRIBIO + 1,
1227 "lfs ifind", 1, &fs->lfs_interlock);
1228 } else
1229 retries = LFS_IFIND_RETRIES;
1230 simple_unlock(&fs->lfs_interlock);
1231 goto again;
1232 }
1233 *ip->i_din.ffs1_din = *dip;
1234 brelse(bp);
1235
1236 if (fs->lfs_version > 1) {
1237 ip->i_ffs1_atime = ts.tv_sec;
1238 ip->i_ffs1_atimensec = ts.tv_nsec;
1239 }
1240
1241 lfs_vinit(mp, &vp);
1242
1243 *vpp = vp;
1244
1245 KASSERT(VOP_ISLOCKED(vp));
1246
1247 return (0);
1248 }
1249
1250 /*
1251 * File handle to vnode
1252 */
1253 int
1254 lfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1255 {
1256 struct lfid lfh;
1257 struct buf *bp;
1258 IFILE *ifp;
1259 int32_t daddr;
1260 struct lfs *fs;
1261
1262 if (fhp->fid_len != sizeof(struct lfid))
1263 return EINVAL;
1264
1265 memcpy(&lfh, fhp, sizeof(lfh));
1266 if (lfh.lfid_ino < LFS_IFILE_INUM)
1267 return ESTALE;
1268
1269 fs = VFSTOUFS(mp)->um_lfs;
1270 if (lfh.lfid_ident != fs->lfs_ident)
1271 return ESTALE;
1272
1273 if (lfh.lfid_ino >
1274 ((VTOI(fs->lfs_ivnode)->i_ffs1_size >> fs->lfs_bshift) -
1275 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb)
1276 return ESTALE;
1277
1278 if (ufs_ihashlookup(VFSTOUFS(mp)->um_dev, lfh.lfid_ino) == NULLVP) {
1279 LFS_IENTRY(ifp, fs, lfh.lfid_ino, bp);
1280 daddr = ifp->if_daddr;
1281 brelse(bp);
1282 if (daddr == LFS_UNUSED_DADDR)
1283 return ESTALE;
1284 }
1285
1286 return (ufs_fhtovp(mp, &lfh.lfid_ufid, vpp));
1287 }
1288
1289 /*
1290 * Vnode pointer to File handle
1291 */
1292 /* ARGSUSED */
1293 int
1294 lfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1295 {
1296 struct inode *ip;
1297 struct lfid lfh;
1298
1299 if (*fh_size < sizeof(struct lfid)) {
1300 *fh_size = sizeof(struct lfid);
1301 return E2BIG;
1302 }
1303 *fh_size = sizeof(struct lfid);
1304 ip = VTOI(vp);
1305 memset(&lfh, 0, sizeof(lfh));
1306 lfh.lfid_len = sizeof(struct lfid);
1307 lfh.lfid_ino = ip->i_number;
1308 lfh.lfid_gen = ip->i_gen;
1309 lfh.lfid_ident = ip->i_lfs->lfs_ident;
1310 memcpy(fhp, &lfh, sizeof(lfh));
1311 return (0);
1312 }
1313
1314 static int
1315 sysctl_lfs_dostats(SYSCTLFN_ARGS)
1316 {
1317 extern struct lfs_stats lfs_stats;
1318 extern int lfs_dostats;
1319 int error;
1320
1321 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
1322 if (error || newp == NULL)
1323 return (error);
1324
1325 if (lfs_dostats == 0)
1326 memset(&lfs_stats, 0, sizeof(lfs_stats));
1327
1328 return (0);
1329 }
1330
1331 struct shortlong {
1332 const char *sname;
1333 const char *lname;
1334 };
1335
1336 SYSCTL_SETUP(sysctl_vfs_lfs_setup, "sysctl vfs.lfs subtree setup")
1337 {
1338 int i;
1339 extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead,
1340 lfs_fs_pagetrip, lfs_ignore_lazy_sync;
1341 #ifdef DEBUG
1342 extern int lfs_debug_log_subsys[DLOG_MAX];
1343 struct shortlong dlog_names[DLOG_MAX] = { /* Must match lfs.h ! */
1344 { "rollforward", "Debug roll-forward code" },
1345 { "alloc", "Debug inode allocation and free list" },
1346 { "avail", "Debug space-available-now accounting" },
1347 { "flush", "Debug flush triggers" },
1348 { "lockedlist", "Debug locked list accounting" },
1349 { "vnode_verbose", "Verbose per-vnode-written debugging" },
1350 { "vnode", "Debug vnode use during segment write" },
1351 { "segment", "Debug segment writing" },
1352 { "seguse", "Debug segment used-bytes accounting" },
1353 { "cleaner", "Debug cleaning routines" },
1354 { "mount", "Debug mount/unmount routines" },
1355 { "pagecache", "Debug UBC interactions" },
1356 { "dirop", "Debug directory-operation accounting" },
1357 { "malloc", "Debug private malloc accounting" },
1358 };
1359 #endif /* DEBUG */
1360 struct shortlong stat_names[] = { /* Must match lfs.h! */
1361 { "segsused", "Number of new segments allocated" },
1362 { "psegwrites", "Number of partial-segment writes" },
1363 { "psyncwrites", "Number of synchronous partial-segment"
1364 " writes" },
1365 { "pcleanwrites", "Number of partial-segment writes by the"
1366 " cleaner" },
1367 { "blocktot", "Number of blocks written" },
1368 { "cleanblocks", "Number of blocks written by the cleaner" },
1369 { "ncheckpoints", "Number of checkpoints made" },
1370 { "nwrites", "Number of whole writes" },
1371 { "nsync_writes", "Number of synchronous writes" },
1372 { "wait_exceeded", "Number of times writer waited for"
1373 " cleaner" },
1374 { "write_exceeded", "Number of times writer invoked flush" },
1375 { "flush_invoked", "Number of times flush was invoked" },
1376 { "vflush_invoked", "Number of time vflush was called" },
1377 { "clean_inlocked", "Number of vnodes skipped for VXLOCK" },
1378 { "clean_vnlocked", "Number of vnodes skipped for vget failure" },
1379 { "segs_reclaimed", "Number of segments reclaimed" },
1380 };
1381
1382 sysctl_createv(clog, 0, NULL, NULL,
1383 CTLFLAG_PERMANENT,
1384 CTLTYPE_NODE, "vfs", NULL,
1385 NULL, 0, NULL, 0,
1386 CTL_VFS, CTL_EOL);
1387 sysctl_createv(clog, 0, NULL, NULL,
1388 CTLFLAG_PERMANENT,
1389 CTLTYPE_NODE, "lfs",
1390 SYSCTL_DESCR("Log-structured file system"),
1391 NULL, 0, NULL, 0,
1392 CTL_VFS, 5, CTL_EOL);
1393 /*
1394 * XXX the "5" above could be dynamic, thereby eliminating one
1395 * more instance of the "number to vfs" mapping problem, but
1396 * "5" is the order as taken from sys/mount.h
1397 */
1398
1399 sysctl_createv(clog, 0, NULL, NULL,
1400 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1401 CTLTYPE_INT, "flushindir", NULL,
1402 NULL, 0, &lfs_writeindir, 0,
1403 CTL_VFS, 5, LFS_WRITEINDIR, CTL_EOL);
1404 sysctl_createv(clog, 0, NULL, NULL,
1405 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1406 CTLTYPE_INT, "clean_vnhead", NULL,
1407 NULL, 0, &lfs_clean_vnhead, 0,
1408 CTL_VFS, 5, LFS_CLEAN_VNHEAD, CTL_EOL);
1409 sysctl_createv(clog, 0, NULL, NULL,
1410 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1411 CTLTYPE_INT, "dostats",
1412 SYSCTL_DESCR("Maintain statistics on LFS operations"),
1413 sysctl_lfs_dostats, 0, &lfs_dostats, 0,
1414 CTL_VFS, 5, LFS_DOSTATS, CTL_EOL);
1415 sysctl_createv(clog, 0, NULL, NULL,
1416 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1417 CTLTYPE_INT, "pagetrip",
1418 SYSCTL_DESCR("How many dirty pages in fs triggers"
1419 " a flush"),
1420 NULL, 0, &lfs_fs_pagetrip, 0,
1421 CTL_VFS, 5, LFS_FS_PAGETRIP, CTL_EOL);
1422 sysctl_createv(clog, 0, NULL, NULL,
1423 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1424 CTLTYPE_INT, "ignore_lazy_sync",
1425 SYSCTL_DESCR("Lazy Sync is ignored entirely"),
1426 NULL, 0, &lfs_ignore_lazy_sync, 0,
1427 CTL_VFS, 5, LFS_IGNORE_LAZY_SYNC, CTL_EOL);
1428 #ifdef LFS_KERNEL_RFW
1429 sysctl_createv(clog, 0, NULL, NULL,
1430 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1431 CTLTYPE_INT, "rfw",
1432 SYSCTL_DESCR("Use in-kernel roll-forward on mount"),
1433 NULL, 0, &lfs_do_rfw, 0,
1434 CTL_VFS, 5, LFS_DO_RFW, CTL_EOL);
1435 #endif
1436
1437 sysctl_createv(clog, 0, NULL, NULL,
1438 CTLFLAG_PERMANENT,
1439 CTLTYPE_NODE, "stats",
1440 SYSCTL_DESCR("Debugging options"),
1441 NULL, 0, NULL, 0,
1442 CTL_VFS, 5, LFS_STATS, CTL_EOL);
1443 for (i = 0; i < sizeof(struct lfs_stats) / sizeof(u_int); i++) {
1444 sysctl_createv(clog, 0, NULL, NULL,
1445 CTLFLAG_PERMANENT|CTLFLAG_READONLY,
1446 CTLTYPE_INT, stat_names[i].sname,
1447 SYSCTL_DESCR(stat_names[i].lname),
1448 NULL, 0, &(((u_int *)&lfs_stats.segsused)[i]),
1449 0, CTL_VFS, 5, LFS_STATS, i, CTL_EOL);
1450 }
1451
1452 #ifdef DEBUG
1453 sysctl_createv(clog, 0, NULL, NULL,
1454 CTLFLAG_PERMANENT,
1455 CTLTYPE_NODE, "debug",
1456 SYSCTL_DESCR("Debugging options"),
1457 NULL, 0, NULL, 0,
1458 CTL_VFS, 5, LFS_DEBUGLOG, CTL_EOL);
1459 for (i = 0; i < DLOG_MAX; i++) {
1460 sysctl_createv(clog, 0, NULL, NULL,
1461 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1462 CTLTYPE_INT, dlog_names[i].sname,
1463 SYSCTL_DESCR(dlog_names[i].lname),
1464 NULL, 0, &(lfs_debug_log_subsys[i]), 0,
1465 CTL_VFS, 5, LFS_DEBUGLOG, i, CTL_EOL);
1466 }
1467 #endif
1468 }
1469
1470 /*
1471 * ufs_bmaparray callback function for writing.
1472 *
1473 * Since blocks will be written to the new segment anyway,
1474 * we don't care about current daddr of them.
1475 */
1476 static bool
1477 lfs_issequential_hole(const struct ufsmount *ump,
1478 daddr_t daddr0, daddr_t daddr1)
1479 {
1480 daddr0 = (daddr_t)((int32_t)daddr0); /* XXX ondisk32 */
1481 daddr1 = (daddr_t)((int32_t)daddr1); /* XXX ondisk32 */
1482
1483 KASSERT(daddr0 == UNWRITTEN ||
1484 (0 <= daddr0 && daddr0 <= LFS_MAX_DADDR));
1485 KASSERT(daddr1 == UNWRITTEN ||
1486 (0 <= daddr1 && daddr1 <= LFS_MAX_DADDR));
1487
1488 /* NOTE: all we want to know here is 'hole or not'. */
1489 /* NOTE: UNASSIGNED is converted to 0 by ufs_bmaparray. */
1490
1491 /*
1492 * treat UNWRITTENs and all resident blocks as 'contiguous'
1493 */
1494 if (daddr0 != 0 && daddr1 != 0)
1495 return true;
1496
1497 /*
1498 * both are in hole?
1499 */
1500 if (daddr0 == 0 && daddr1 == 0)
1501 return true; /* all holes are 'contiguous' for us. */
1502
1503 return false;
1504 }
1505
1506 /*
1507 * lfs_gop_write functions exactly like genfs_gop_write, except that
1508 * (1) it requires the seglock to be held by its caller, and sp->fip
1509 * to be properly initialized (it will return without re-initializing
1510 * sp->fip, and without calling lfs_writeseg).
1511 * (2) it uses the remaining space in the segment, rather than VOP_BMAP,
1512 * to determine how large a block it can write at once (though it does
1513 * still use VOP_BMAP to find holes in the file);
1514 * (3) it calls lfs_gatherblock instead of VOP_STRATEGY on its blocks
1515 * (leaving lfs_writeseg to deal with the cluster blocks, so we might
1516 * now have clusters of clusters, ick.)
1517 */
1518 static int
1519 lfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
1520 int flags)
1521 {
1522 int i, s, error, run, haveeof = 0;
1523 int fs_bshift;
1524 vaddr_t kva;
1525 off_t eof, offset, startoffset = 0;
1526 size_t bytes, iobytes, skipbytes;
1527 daddr_t lbn, blkno;
1528 struct vm_page *pg;
1529 struct buf *mbp, *bp;
1530 struct vnode *devvp = VTOI(vp)->i_devvp;
1531 struct inode *ip = VTOI(vp);
1532 struct lfs *fs = ip->i_lfs;
1533 struct segment *sp = fs->lfs_sp;
1534 UVMHIST_FUNC("lfs_gop_write"); UVMHIST_CALLED(ubchist);
1535
1536 ASSERT_SEGLOCK(fs);
1537
1538 /* The Ifile lives in the buffer cache */
1539 KASSERT(vp != fs->lfs_ivnode);
1540
1541 /*
1542 * We don't want to fill the disk before the cleaner has a chance
1543 * to make room for us. If we're in danger of doing that, fail
1544 * with EAGAIN. The caller will have to notice this, unlock
1545 * so the cleaner can run, relock and try again.
1546 *
1547 * We must write everything, however, if our vnode is being
1548 * reclaimed.
1549 */
1550 if (LFS_STARVED_FOR_SEGS(fs) && vp != fs->lfs_flushvp)
1551 goto tryagain;
1552
1553 /*
1554 * Sometimes things slip past the filters in lfs_putpages,
1555 * and the pagedaemon tries to write pages---problem is
1556 * that the pagedaemon never acquires the segment lock.
1557 *
1558 * Alternatively, pages that were clean when we called
1559 * genfs_putpages may have become dirty in the meantime. In this
1560 * case the segment header is not properly set up for blocks
1561 * to be added to it.
1562 *
1563 * Unbusy and unclean the pages, and put them on the ACTIVE
1564 * queue under the hypothesis that they couldn't have got here
1565 * unless they were modified *quite* recently.
1566 *
1567 * XXXUBC that last statement is an oversimplification of course.
1568 */
1569 if (!LFS_SEGLOCK_HELD(fs) ||
1570 (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) ||
1571 (pgs[0]->offset & fs->lfs_bmask) != 0) {
1572 goto tryagain;
1573 }
1574
1575 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1576 vp, pgs, npages, flags);
1577
1578 GOP_SIZE(vp, vp->v_size, &eof, 0);
1579 haveeof = 1;
1580
1581 if (vp->v_type == VREG)
1582 fs_bshift = vp->v_mount->mnt_fs_bshift;
1583 else
1584 fs_bshift = DEV_BSHIFT;
1585 error = 0;
1586 pg = pgs[0];
1587 startoffset = pg->offset;
1588 KASSERT(eof >= 0);
1589
1590 if (startoffset >= eof) {
1591 goto tryagain;
1592 } else
1593 bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
1594 skipbytes = 0;
1595
1596 KASSERT(bytes != 0);
1597
1598 /* Swap PG_DELWRI for PG_PAGEOUT */
1599 for (i = 0; i < npages; i++) {
1600 if (pgs[i]->flags & PG_DELWRI) {
1601 KASSERT(!(pgs[i]->flags & PG_PAGEOUT));
1602 pgs[i]->flags &= ~PG_DELWRI;
1603 pgs[i]->flags |= PG_PAGEOUT;
1604 uvmexp.paging++;
1605 uvm_lock_pageq();
1606 uvm_pageunwire(pgs[i]);
1607 uvm_unlock_pageq();
1608 }
1609 }
1610
1611 /*
1612 * Check to make sure we're starting on a block boundary.
1613 * We'll check later to make sure we always write entire
1614 * blocks (or fragments).
1615 */
1616 if (startoffset & fs->lfs_bmask)
1617 printf("%" PRId64 " & %" PRId64 " = %" PRId64 "\n",
1618 startoffset, fs->lfs_bmask,
1619 startoffset & fs->lfs_bmask);
1620 KASSERT((startoffset & fs->lfs_bmask) == 0);
1621 if (bytes & fs->lfs_ffmask) {
1622 printf("lfs_gop_write: asked to write %ld bytes\n", (long)bytes);
1623 panic("lfs_gop_write: non-integer blocks");
1624 }
1625
1626 /*
1627 * We could deadlock here on pager_map with UVMPAGER_MAPIN_WAITOK.
1628 * If we would, write what we have and try again. If we don't
1629 * have anything to write, we'll have to sleep.
1630 */
1631 if ((kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
1632 (((SEGSUM *)(sp->segsum))->ss_nfinfo < 1 ?
1633 UVMPAGER_MAPIN_WAITOK : 0))) == 0x0) {
1634 DLOG((DLOG_PAGE, "lfs_gop_write: forcing write\n"));
1635 #if 0
1636 " with nfinfo=%d at offset 0x%x\n",
1637 (int)((SEGSUM *)(sp->segsum))->ss_nfinfo,
1638 (unsigned)fs->lfs_offset));
1639 #endif
1640 lfs_updatemeta(sp);
1641 lfs_release_finfo(fs);
1642 (void) lfs_writeseg(fs, sp);
1643
1644 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
1645
1646 /*
1647 * Having given up all of the pager_map we were holding,
1648 * we can now wait for aiodoned to reclaim it for us
1649 * without fear of deadlock.
1650 */
1651 kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
1652 UVMPAGER_MAPIN_WAITOK);
1653 }
1654
1655 s = splbio();
1656 simple_lock(&global_v_numoutput_slock);
1657 vp->v_numoutput += 2; /* one for biodone, one for aiodone */
1658 simple_unlock(&global_v_numoutput_slock);
1659 splx(s);
1660
1661 mbp = getiobuf();
1662 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
1663 vp, mbp, vp->v_numoutput, bytes);
1664 mbp->b_bufsize = npages << PAGE_SHIFT;
1665 mbp->b_data = (void *)kva;
1666 mbp->b_resid = mbp->b_bcount = bytes;
1667 mbp->b_flags = B_BUSY|B_WRITE|B_AGE|B_CALL;
1668 mbp->b_iodone = uvm_aio_biodone;
1669 mbp->b_vp = vp;
1670
1671 bp = NULL;
1672 for (offset = startoffset;
1673 bytes > 0;
1674 offset += iobytes, bytes -= iobytes) {
1675 lbn = offset >> fs_bshift;
1676 error = ufs_bmaparray(vp, lbn, &blkno, NULL, NULL, &run,
1677 lfs_issequential_hole);
1678 if (error) {
1679 UVMHIST_LOG(ubchist, "ufs_bmaparray() -> %d",
1680 error,0,0,0);
1681 skipbytes += bytes;
1682 bytes = 0;
1683 break;
1684 }
1685
1686 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
1687 bytes);
1688 if (blkno == (daddr_t)-1) {
1689 skipbytes += iobytes;
1690 continue;
1691 }
1692
1693 /*
1694 * Discover how much we can really pack into this buffer.
1695 */
1696 /* If no room in the current segment, finish it up */
1697 if (sp->sum_bytes_left < sizeof(int32_t) ||
1698 sp->seg_bytes_left < (1 << fs->lfs_bshift)) {
1699 int vers;
1700
1701 lfs_updatemeta(sp);
1702 vers = sp->fip->fi_version;
1703 lfs_release_finfo(fs);
1704 (void) lfs_writeseg(fs, sp);
1705
1706 lfs_acquire_finfo(fs, ip->i_number, vers);
1707 }
1708 /* Check both for space in segment and space in segsum */
1709 iobytes = MIN(iobytes, (sp->seg_bytes_left >> fs_bshift)
1710 << fs_bshift);
1711 iobytes = MIN(iobytes, (sp->sum_bytes_left / sizeof(int32_t))
1712 << fs_bshift);
1713 KASSERT(iobytes > 0);
1714
1715 /* if it's really one i/o, don't make a second buf */
1716 if (offset == startoffset && iobytes == bytes) {
1717 bp = mbp;
1718 /* correct overcount if there is no second buffer */
1719 s = splbio();
1720 simple_lock(&global_v_numoutput_slock);
1721 --vp->v_numoutput;
1722 simple_unlock(&global_v_numoutput_slock);
1723 splx(s);
1724 } else {
1725 bp = getiobuf();
1726 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1727 vp, bp, vp->v_numoutput, 0);
1728 bp->b_data = (char *)kva +
1729 (vaddr_t)(offset - pg->offset);
1730 bp->b_resid = bp->b_bcount = iobytes;
1731 bp->b_flags = B_BUSY|B_WRITE|B_CALL;
1732 bp->b_iodone = uvm_aio_biodone1;
1733 }
1734
1735 /* XXX This is silly ... is this necessary? */
1736 bp->b_vp = NULL;
1737 s = splbio();
1738 bgetvp(vp, bp);
1739 splx(s);
1740
1741 bp->b_lblkno = lblkno(fs, offset);
1742 bp->b_private = mbp;
1743 if (devvp->v_type == VBLK) {
1744 bp->b_dev = devvp->v_rdev;
1745 }
1746 VOP_BWRITE(bp);
1747 while (lfs_gatherblock(sp, bp, NULL))
1748 continue;
1749 }
1750
1751 if (skipbytes) {
1752 UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
1753 s = splbio();
1754 if (error) {
1755 mbp->b_flags |= B_ERROR;
1756 mbp->b_error = error;
1757 }
1758 mbp->b_resid -= skipbytes;
1759 if (mbp->b_resid == 0) {
1760 biodone(mbp);
1761 }
1762 splx(s);
1763 }
1764 UVMHIST_LOG(ubchist, "returning 0", 0,0,0,0);
1765 return (0);
1766
1767 tryagain:
1768 /*
1769 * We can't write the pages, for whatever reason.
1770 * Clean up after ourselves, and make the caller try again.
1771 */
1772 simple_lock(&vp->v_interlock);
1773
1774 /* Tell why we're here, if we know */
1775 if (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) {
1776 DLOG((DLOG_PAGE, "lfs_gop_write: clean pages dirtied\n"));
1777 } else if ((pgs[0]->offset & fs->lfs_bmask) != 0) {
1778 DLOG((DLOG_PAGE, "lfs_gop_write: not on block boundary\n"));
1779 } else if (haveeof && startoffset >= eof) {
1780 DLOG((DLOG_PAGE, "lfs_gop_write: ino %d start 0x%" PRIx64
1781 " eof 0x%" PRIx64 " npages=%d\n", VTOI(vp)->i_number,
1782 pgs[0]->offset, eof, npages));
1783 } else if (LFS_STARVED_FOR_SEGS(fs)) {
1784 DLOG((DLOG_PAGE, "lfs_gop_write: avail too low\n"));
1785 } else {
1786 DLOG((DLOG_PAGE, "lfs_gop_write: seglock not held\n"));
1787 }
1788
1789 uvm_lock_pageq();
1790 for (i = 0; i < npages; i++) {
1791 pg = pgs[i];
1792
1793 if (pg->flags & PG_PAGEOUT)
1794 uvmexp.paging--;
1795 if (pg->flags & PG_DELWRI) {
1796 uvm_pageunwire(pg);
1797 }
1798 uvm_pageactivate(pg);
1799 pg->flags &= ~(PG_CLEAN|PG_DELWRI|PG_PAGEOUT|PG_RELEASED);
1800 DLOG((DLOG_PAGE, "pg[%d] = %p (vp %p off %" PRIx64 ")\n", i, pg,
1801 vp, pg->offset));
1802 DLOG((DLOG_PAGE, "pg[%d]->flags = %x\n", i, pg->flags));
1803 DLOG((DLOG_PAGE, "pg[%d]->pqflags = %x\n", i, pg->pqflags));
1804 DLOG((DLOG_PAGE, "pg[%d]->uanon = %p\n", i, pg->uanon));
1805 DLOG((DLOG_PAGE, "pg[%d]->uobject = %p\n", i, pg->uobject));
1806 DLOG((DLOG_PAGE, "pg[%d]->wire_count = %d\n", i,
1807 pg->wire_count));
1808 DLOG((DLOG_PAGE, "pg[%d]->loan_count = %d\n", i,
1809 pg->loan_count));
1810 }
1811 /* uvm_pageunbusy takes care of PG_BUSY, PG_WANTED */
1812 uvm_page_unbusy(pgs, npages);
1813 uvm_unlock_pageq();
1814 simple_unlock(&vp->v_interlock);
1815 return EAGAIN;
1816 }
1817
1818 /*
1819 * finish vnode/inode initialization.
1820 * used by lfs_vget and lfs_fastvget.
1821 */
1822 void
1823 lfs_vinit(struct mount *mp, struct vnode **vpp)
1824 {
1825 struct vnode *vp = *vpp;
1826 struct inode *ip = VTOI(vp);
1827 struct ufsmount *ump = VFSTOUFS(mp);
1828 struct lfs *fs = ump->um_lfs;
1829 int i;
1830
1831 ip->i_mode = ip->i_ffs1_mode;
1832 ip->i_ffs_effnlink = ip->i_nlink = ip->i_ffs1_nlink;
1833 ip->i_lfs_osize = ip->i_size = ip->i_ffs1_size;
1834 ip->i_flags = ip->i_ffs1_flags;
1835 ip->i_gen = ip->i_ffs1_gen;
1836 ip->i_uid = ip->i_ffs1_uid;
1837 ip->i_gid = ip->i_ffs1_gid;
1838
1839 ip->i_lfs_effnblks = ip->i_ffs1_blocks;
1840 ip->i_lfs_odnlink = ip->i_ffs1_nlink;
1841
1842 /*
1843 * Initialize the vnode from the inode, check for aliases. In all
1844 * cases re-init ip, the underlying vnode/inode may have changed.
1845 */
1846 ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
1847 ip = VTOI(vp);
1848
1849 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
1850 if (vp->v_type != VLNK || ip->i_size >= ip->i_ump->um_maxsymlinklen) {
1851 #ifdef DEBUG
1852 for (i = (ip->i_size + fs->lfs_bsize - 1) >> fs->lfs_bshift;
1853 i < NDADDR; i++) {
1854 if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
1855 i == 0)
1856 continue;
1857 if (ip->i_ffs1_db[i] != 0) {
1858 inconsistent:
1859 lfs_dump_dinode(ip->i_din.ffs1_din);
1860 panic("inconsistent inode");
1861 }
1862 }
1863 for ( ; i < NDADDR + NIADDR; i++) {
1864 if (ip->i_ffs1_ib[i - NDADDR] != 0) {
1865 goto inconsistent;
1866 }
1867 }
1868 #endif /* DEBUG */
1869 for (i = 0; i < NDADDR; i++)
1870 if (ip->i_ffs1_db[i] != 0)
1871 ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
1872 }
1873
1874 #ifdef DIAGNOSTIC
1875 if (vp->v_type == VNON) {
1876 # ifdef DEBUG
1877 lfs_dump_dinode(ip->i_din.ffs1_din);
1878 # endif
1879 panic("lfs_vinit: ino %llu is type VNON! (ifmt=%o)\n",
1880 (unsigned long long)ip->i_number,
1881 (ip->i_mode & IFMT) >> 12);
1882 }
1883 #endif /* DIAGNOSTIC */
1884
1885 /*
1886 * Finish inode initialization now that aliasing has been resolved.
1887 */
1888
1889 ip->i_devvp = ump->um_devvp;
1890 VREF(ip->i_devvp);
1891 genfs_node_init(vp, &lfs_genfsops);
1892 uvm_vnp_setsize(vp, ip->i_size);
1893
1894 /* Initialize hiblk from file size */
1895 ip->i_lfs_hiblk = lblkno(ip->i_lfs, ip->i_size + ip->i_lfs->lfs_bsize - 1) - 1;
1896
1897 *vpp = vp;
1898 }
1899
1900 /*
1901 * Resize the filesystem to contain the specified number of segments.
1902 */
1903 int
1904 lfs_resize_fs(struct lfs *fs, int newnsegs)
1905 {
1906 SEGUSE *sup;
1907 struct buf *bp, *obp;
1908 daddr_t olast, nlast, ilast, noff, start, end;
1909 struct vnode *ivp;
1910 struct inode *ip;
1911 int error, badnews, inc, oldnsegs;
1912 int sbbytes, csbbytes, gain, cgain;
1913 int i;
1914
1915 /* Only support v2 and up */
1916 if (fs->lfs_version < 2)
1917 return EOPNOTSUPP;
1918
1919 /* If we're doing nothing, do it fast */
1920 oldnsegs = fs->lfs_nseg;
1921 if (newnsegs == oldnsegs)
1922 return 0;
1923
1924 /* We always have to have two superblocks */
1925 if (newnsegs <= dtosn(fs, fs->lfs_sboffs[1]))
1926 return EFBIG;
1927
1928 ivp = fs->lfs_ivnode;
1929 ip = VTOI(ivp);
1930 error = 0;
1931
1932 /* Take the segment lock so no one else calls lfs_newseg() */
1933 lfs_seglock(fs, SEGM_PROT);
1934
1935 /*
1936 * Make sure the segments we're going to be losing, if any,
1937 * are in fact empty. We hold the seglock, so their status
1938 * cannot change underneath us. Count the superblocks we lose,
1939 * while we're at it.
1940 */
1941 sbbytes = csbbytes = 0;
1942 cgain = 0;
1943 for (i = newnsegs; i < oldnsegs; i++) {
1944 LFS_SEGENTRY(sup, fs, i, bp);
1945 badnews = sup->su_nbytes || !(sup->su_flags & SEGUSE_INVAL);
1946 if (sup->su_flags & SEGUSE_SUPERBLOCK)
1947 sbbytes += LFS_SBPAD;
1948 if (!(sup->su_flags & SEGUSE_DIRTY)) {
1949 ++cgain;
1950 if (sup->su_flags & SEGUSE_SUPERBLOCK)
1951 csbbytes += LFS_SBPAD;
1952 }
1953 brelse(bp);
1954 if (badnews) {
1955 error = EBUSY;
1956 goto out;
1957 }
1958 }
1959
1960 /* Note old and new segment table endpoints, and old ifile size */
1961 olast = fs->lfs_cleansz + fs->lfs_segtabsz;
1962 nlast = howmany(newnsegs, fs->lfs_sepb) + fs->lfs_cleansz;
1963 ilast = ivp->v_size >> fs->lfs_bshift;
1964 noff = nlast - olast;
1965
1966 /*
1967 * Make sure no one can use the Ifile while we change it around.
1968 * Even after taking the iflock we need to make sure no one still
1969 * is holding Ifile buffers, so we get each one, to drain them.
1970 * (XXX this could be done better.)
1971 */
1972 simple_lock(&fs->lfs_interlock);
1973 lockmgr(&fs->lfs_iflock, LK_EXCLUSIVE, &fs->lfs_interlock);
1974 simple_unlock(&fs->lfs_interlock);
1975 vn_lock(ivp, LK_EXCLUSIVE | LK_RETRY);
1976 for (i = 0; i < ilast; i++) {
1977 bread(ivp, i, fs->lfs_bsize, NOCRED, &bp);
1978 brelse(bp);
1979 }
1980
1981 /* Allocate new Ifile blocks */
1982 for (i = ilast; i < ilast + noff; i++) {
1983 if (lfs_balloc(ivp, i * fs->lfs_bsize, fs->lfs_bsize, NOCRED, 0,
1984 &bp) != 0)
1985 panic("balloc extending ifile");
1986 memset(bp->b_data, 0, fs->lfs_bsize);
1987 VOP_BWRITE(bp);
1988 }
1989
1990 /* Register new ifile size */
1991 ip->i_size += noff * fs->lfs_bsize;
1992 ip->i_ffs1_size = ip->i_size;
1993 uvm_vnp_setsize(ivp, ip->i_size);
1994
1995 /* Copy the inode table to its new position */
1996 if (noff != 0) {
1997 if (noff < 0) {
1998 start = nlast;
1999 end = ilast + noff;
2000 inc = 1;
2001 } else {
2002 start = ilast + noff - 1;
2003 end = nlast - 1;
2004 inc = -1;
2005 }
2006 for (i = start; i != end; i += inc) {
2007 if (bread(ivp, i, fs->lfs_bsize, NOCRED, &bp) != 0)
2008 panic("resize: bread dst blk failed");
2009 if (bread(ivp, i - noff, fs->lfs_bsize, NOCRED, &obp))
2010 panic("resize: bread src blk failed");
2011 memcpy(bp->b_data, obp->b_data, fs->lfs_bsize);
2012 VOP_BWRITE(bp);
2013 brelse(obp);
2014 }
2015 }
2016
2017 /* If we are expanding, write the new empty SEGUSE entries */
2018 if (newnsegs > oldnsegs) {
2019 for (i = oldnsegs; i < newnsegs; i++) {
2020 if ((error = bread(ivp, i / fs->lfs_sepb +
2021 fs->lfs_cleansz,
2022 fs->lfs_bsize, NOCRED, &bp)) != 0)
2023 panic("lfs: ifile read: %d", error);
2024 while ((i + 1) % fs->lfs_sepb && i < newnsegs) {
2025 sup = &((SEGUSE *)bp->b_data)[i % fs->lfs_sepb];
2026 memset(sup, 0, sizeof(*sup));
2027 i++;
2028 }
2029 VOP_BWRITE(bp);
2030 }
2031 }
2032
2033 /* Zero out unused superblock offsets */
2034 for (i = 2; i < LFS_MAXNUMSB; i++)
2035 if (dtosn(fs, fs->lfs_sboffs[i]) >= newnsegs)
2036 fs->lfs_sboffs[i] = 0x0;
2037
2038 /*
2039 * Correct superblock entries that depend on fs size.
2040 * The computations of these are as follows:
2041 *
2042 * size = segtod(fs, nseg)
2043 * dsize = segtod(fs, nseg - minfreeseg) - btofsb(#super * LFS_SBPAD)
2044 * bfree = dsize - btofsb(fs, bsize * nseg / 2) - blocks_actually_used
2045 * avail = segtod(fs, nclean) - btofsb(#clean_super * LFS_SBPAD)
2046 * + (segtod(fs, 1) - (offset - curseg))
2047 * - segtod(fs, minfreeseg - (minfreeseg / 2))
2048 *
2049 * XXX - we should probably adjust minfreeseg as well.
2050 */
2051 gain = (newnsegs - oldnsegs);
2052 fs->lfs_nseg = newnsegs;
2053 fs->lfs_segtabsz = nlast - fs->lfs_cleansz;
2054 fs->lfs_size += gain * btofsb(fs, fs->lfs_ssize);
2055 fs->lfs_dsize += gain * btofsb(fs, fs->lfs_ssize) - btofsb(fs, sbbytes);
2056 fs->lfs_bfree += gain * btofsb(fs, fs->lfs_ssize) - btofsb(fs, sbbytes)
2057 - gain * btofsb(fs, fs->lfs_bsize / 2);
2058 if (gain > 0) {
2059 fs->lfs_nclean += gain;
2060 fs->lfs_avail += gain * btofsb(fs, fs->lfs_ssize);
2061 } else {
2062 fs->lfs_nclean -= cgain;
2063 fs->lfs_avail -= cgain * btofsb(fs, fs->lfs_ssize) -
2064 btofsb(fs, csbbytes);
2065 }
2066
2067 /* Resize segment flag cache */
2068 fs->lfs_suflags[0] = (u_int32_t *)realloc(fs->lfs_suflags[0],
2069 fs->lfs_nseg * sizeof(u_int32_t),
2070 M_SEGMENT, M_WAITOK);
2071 fs->lfs_suflags[1] = (u_int32_t *)realloc(fs->lfs_suflags[1],
2072 fs->lfs_nseg * sizeof(u_int32_t),
2073 M_SEGMENT, M_WAITOK);
2074 for (i = oldnsegs; i < newnsegs; i++)
2075 fs->lfs_suflags[0][i] = fs->lfs_suflags[1][i] = 0x0;
2076
2077 /* Truncate Ifile if necessary */
2078 if (noff < 0)
2079 lfs_truncate(ivp, ivp->v_size + (noff << fs->lfs_bshift), 0,
2080 NOCRED, curlwp);
2081
2082 /* Update cleaner info so the cleaner can die */
2083 bread(ivp, 0, fs->lfs_bsize, NOCRED, &bp);
2084 ((CLEANERINFO *)bp->b_data)->clean = fs->lfs_nclean;
2085 ((CLEANERINFO *)bp->b_data)->dirty = fs->lfs_nseg - fs->lfs_nclean;
2086 VOP_BWRITE(bp);
2087
2088 /* Let Ifile accesses proceed */
2089 VOP_UNLOCK(ivp, 0);
2090 simple_lock(&fs->lfs_interlock);
2091 lockmgr(&fs->lfs_iflock, LK_RELEASE, &fs->lfs_interlock);
2092 simple_unlock(&fs->lfs_interlock);
2093
2094 out:
2095 lfs_segunlock(fs);
2096 return error;
2097 }
2098