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