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