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