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