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