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