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