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