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