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