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