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