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