lfs_vfsops.c revision 1.311 1 /* $NetBSD: lfs_vfsops.c,v 1.311 2013/07/28 01:22:55 dholland 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.311 2013/07/28 01:22:55 dholland 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_kernel.h>
113 #include <ufs/lfs/lfs_extern.h>
114
115 #include <miscfs/genfs/genfs.h>
116 #include <miscfs/genfs/genfs_node.h>
117
118 MODULE(MODULE_CLASS_VFS, lfs, NULL);
119
120 static int lfs_gop_write(struct vnode *, struct vm_page **, int, int);
121 static int lfs_mountfs(struct vnode *, struct mount *, struct lwp *);
122
123 static struct sysctllog *lfs_sysctl_log;
124
125 extern const struct vnodeopv_desc lfs_vnodeop_opv_desc;
126 extern const struct vnodeopv_desc lfs_specop_opv_desc;
127 extern const struct vnodeopv_desc lfs_fifoop_opv_desc;
128
129 pid_t lfs_writer_daemon = 0;
130 lwpid_t lfs_writer_lid = 0;
131 int lfs_do_flush = 0;
132 #ifdef LFS_KERNEL_RFW
133 int lfs_do_rfw = 0;
134 #endif
135
136 const struct vnodeopv_desc * const lfs_vnodeopv_descs[] = {
137 &lfs_vnodeop_opv_desc,
138 &lfs_specop_opv_desc,
139 &lfs_fifoop_opv_desc,
140 NULL,
141 };
142
143 struct vfsops lfs_vfsops = {
144 MOUNT_LFS,
145 sizeof (struct ulfs_args),
146 lfs_mount,
147 ulfs_start,
148 lfs_unmount,
149 ulfs_root,
150 ulfs_quotactl,
151 lfs_statvfs,
152 lfs_sync,
153 lfs_vget,
154 lfs_fhtovp,
155 lfs_vptofh,
156 lfs_init,
157 lfs_reinit,
158 lfs_done,
159 lfs_mountroot,
160 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
161 vfs_stdextattrctl,
162 (void *)eopnotsupp, /* vfs_suspendctl */
163 genfs_renamelock_enter,
164 genfs_renamelock_exit,
165 (void *)eopnotsupp,
166 lfs_vnodeopv_descs,
167 0,
168 { NULL, NULL },
169 };
170
171 const struct genfs_ops lfs_genfsops = {
172 .gop_size = lfs_gop_size,
173 .gop_alloc = ulfs_gop_alloc,
174 .gop_write = lfs_gop_write,
175 .gop_markupdate = ulfs_gop_markupdate,
176 };
177
178 struct shortlong {
179 const char *sname;
180 const char *lname;
181 };
182
183 static int
184 sysctl_lfs_dostats(SYSCTLFN_ARGS)
185 {
186 extern struct lfs_stats lfs_stats;
187 extern int lfs_dostats;
188 int error;
189
190 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
191 if (error || newp == NULL)
192 return (error);
193
194 if (lfs_dostats == 0)
195 memset(&lfs_stats, 0, sizeof(lfs_stats));
196
197 return (0);
198 }
199
200 static void
201 lfs_sysctl_setup(struct sysctllog **clog)
202 {
203 int i;
204 extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead,
205 lfs_fs_pagetrip, lfs_ignore_lazy_sync;
206 #ifdef DEBUG
207 extern int lfs_debug_log_subsys[DLOG_MAX];
208 struct shortlong dlog_names[DLOG_MAX] = { /* Must match lfs.h ! */
209 { "rollforward", "Debug roll-forward code" },
210 { "alloc", "Debug inode allocation and free list" },
211 { "avail", "Debug space-available-now accounting" },
212 { "flush", "Debug flush triggers" },
213 { "lockedlist", "Debug locked list accounting" },
214 { "vnode_verbose", "Verbose per-vnode-written debugging" },
215 { "vnode", "Debug vnode use during segment write" },
216 { "segment", "Debug segment writing" },
217 { "seguse", "Debug segment used-bytes accounting" },
218 { "cleaner", "Debug cleaning routines" },
219 { "mount", "Debug mount/unmount routines" },
220 { "pagecache", "Debug UBC interactions" },
221 { "dirop", "Debug directory-operation accounting" },
222 { "malloc", "Debug private malloc accounting" },
223 };
224 #endif /* DEBUG */
225 struct shortlong stat_names[] = { /* Must match lfs.h! */
226 { "segsused", "Number of new segments allocated" },
227 { "psegwrites", "Number of partial-segment writes" },
228 { "psyncwrites", "Number of synchronous partial-segment"
229 " writes" },
230 { "pcleanwrites", "Number of partial-segment writes by the"
231 " cleaner" },
232 { "blocktot", "Number of blocks written" },
233 { "cleanblocks", "Number of blocks written by the cleaner" },
234 { "ncheckpoints", "Number of checkpoints made" },
235 { "nwrites", "Number of whole writes" },
236 { "nsync_writes", "Number of synchronous writes" },
237 { "wait_exceeded", "Number of times writer waited for"
238 " cleaner" },
239 { "write_exceeded", "Number of times writer invoked flush" },
240 { "flush_invoked", "Number of times flush was invoked" },
241 { "vflush_invoked", "Number of time vflush was called" },
242 { "clean_inlocked", "Number of vnodes skipped for VI_XLOCK" },
243 { "clean_vnlocked", "Number of vnodes skipped for vget failure" },
244 { "segs_reclaimed", "Number of segments reclaimed" },
245 };
246
247 sysctl_createv(clog, 0, NULL, NULL,
248 CTLFLAG_PERMANENT,
249 CTLTYPE_NODE, "vfs", NULL,
250 NULL, 0, NULL, 0,
251 CTL_VFS, CTL_EOL);
252 sysctl_createv(clog, 0, NULL, NULL,
253 CTLFLAG_PERMANENT,
254 CTLTYPE_NODE, "lfs",
255 SYSCTL_DESCR("Log-structured file system"),
256 NULL, 0, NULL, 0,
257 CTL_VFS, 5, CTL_EOL);
258 /*
259 * XXX the "5" above could be dynamic, thereby eliminating one
260 * more instance of the "number to vfs" mapping problem, but
261 * "5" is the order as taken from sys/mount.h
262 */
263
264 sysctl_createv(clog, 0, NULL, NULL,
265 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
266 CTLTYPE_INT, "flushindir", NULL,
267 NULL, 0, &lfs_writeindir, 0,
268 CTL_VFS, 5, LFS_WRITEINDIR, CTL_EOL);
269 sysctl_createv(clog, 0, NULL, NULL,
270 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
271 CTLTYPE_INT, "clean_vnhead", NULL,
272 NULL, 0, &lfs_clean_vnhead, 0,
273 CTL_VFS, 5, LFS_CLEAN_VNHEAD, CTL_EOL);
274 sysctl_createv(clog, 0, NULL, NULL,
275 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
276 CTLTYPE_INT, "dostats",
277 SYSCTL_DESCR("Maintain statistics on LFS operations"),
278 sysctl_lfs_dostats, 0, &lfs_dostats, 0,
279 CTL_VFS, 5, LFS_DOSTATS, CTL_EOL);
280 sysctl_createv(clog, 0, NULL, NULL,
281 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
282 CTLTYPE_INT, "pagetrip",
283 SYSCTL_DESCR("How many dirty pages in fs triggers"
284 " a flush"),
285 NULL, 0, &lfs_fs_pagetrip, 0,
286 CTL_VFS, 5, LFS_FS_PAGETRIP, CTL_EOL);
287 sysctl_createv(clog, 0, NULL, NULL,
288 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
289 CTLTYPE_INT, "ignore_lazy_sync",
290 SYSCTL_DESCR("Lazy Sync is ignored entirely"),
291 NULL, 0, &lfs_ignore_lazy_sync, 0,
292 CTL_VFS, 5, LFS_IGNORE_LAZY_SYNC, CTL_EOL);
293 #ifdef LFS_KERNEL_RFW
294 sysctl_createv(clog, 0, NULL, NULL,
295 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
296 CTLTYPE_INT, "rfw",
297 SYSCTL_DESCR("Use in-kernel roll-forward on mount"),
298 NULL, 0, &lfs_do_rfw, 0,
299 CTL_VFS, 5, LFS_DO_RFW, CTL_EOL);
300 #endif
301
302 sysctl_createv(clog, 0, NULL, NULL,
303 CTLFLAG_PERMANENT,
304 CTLTYPE_NODE, "stats",
305 SYSCTL_DESCR("Debugging options"),
306 NULL, 0, NULL, 0,
307 CTL_VFS, 5, LFS_STATS, CTL_EOL);
308 for (i = 0; i < sizeof(struct lfs_stats) / sizeof(u_int); i++) {
309 sysctl_createv(clog, 0, NULL, NULL,
310 CTLFLAG_PERMANENT|CTLFLAG_READONLY,
311 CTLTYPE_INT, stat_names[i].sname,
312 SYSCTL_DESCR(stat_names[i].lname),
313 NULL, 0, &(((u_int *)&lfs_stats.segsused)[i]),
314 0, CTL_VFS, 5, LFS_STATS, i, CTL_EOL);
315 }
316
317 #ifdef DEBUG
318 sysctl_createv(clog, 0, NULL, NULL,
319 CTLFLAG_PERMANENT,
320 CTLTYPE_NODE, "debug",
321 SYSCTL_DESCR("Debugging options"),
322 NULL, 0, NULL, 0,
323 CTL_VFS, 5, LFS_DEBUGLOG, CTL_EOL);
324 for (i = 0; i < DLOG_MAX; i++) {
325 sysctl_createv(clog, 0, NULL, NULL,
326 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
327 CTLTYPE_INT, dlog_names[i].sname,
328 SYSCTL_DESCR(dlog_names[i].lname),
329 NULL, 0, &(lfs_debug_log_subsys[i]), 0,
330 CTL_VFS, 5, LFS_DEBUGLOG, i, CTL_EOL);
331 }
332 #endif
333 }
334
335 /* old cleaner syscall interface. see VOP_FCNTL() */
336 static const struct syscall_package lfs_syscalls[] = {
337 { SYS_lfs_bmapv, 0, (sy_call_t *)sys_lfs_bmapv },
338 { SYS_lfs_markv, 0, (sy_call_t *)sys_lfs_markv },
339 { SYS_lfs_segclean, 0, (sy_call_t *)sys___lfs_segwait50 },
340 { 0, 0, NULL },
341 };
342
343 static int
344 lfs_modcmd(modcmd_t cmd, void *arg)
345 {
346 int error;
347
348 switch (cmd) {
349 case MODULE_CMD_INIT:
350 error = syscall_establish(NULL, lfs_syscalls);
351 if (error)
352 return error;
353 error = vfs_attach(&lfs_vfsops);
354 if (error != 0) {
355 syscall_disestablish(NULL, lfs_syscalls);
356 break;
357 }
358 lfs_sysctl_setup(&lfs_sysctl_log);
359 break;
360 case MODULE_CMD_FINI:
361 error = vfs_detach(&lfs_vfsops);
362 if (error != 0)
363 break;
364 syscall_disestablish(NULL, lfs_syscalls);
365 sysctl_teardown(&lfs_sysctl_log);
366 break;
367 default:
368 error = ENOTTY;
369 break;
370 }
371
372 return (error);
373 }
374
375 /*
376 * XXX Same structure as FFS inodes? Should we share a common pool?
377 */
378 struct pool lfs_inode_pool;
379 struct pool lfs_dinode_pool;
380 struct pool lfs_inoext_pool;
381 struct pool lfs_lbnentry_pool;
382
383 /*
384 * The writer daemon. UVM keeps track of how many dirty pages we are holding
385 * in lfs_subsys_pages; the daemon flushes the filesystem when this value
386 * crosses the (user-defined) threshhold LFS_MAX_PAGES.
387 */
388 static void
389 lfs_writerd(void *arg)
390 {
391 struct mount *mp, *nmp;
392 struct lfs *fs;
393 struct vfsops *vfs = NULL;
394 int fsflags;
395 int loopcount;
396 int skipc;
397 int lfsc;
398 int wrote_something = 0;
399
400 mutex_enter(&lfs_lock);
401 lfs_writer_daemon = curproc->p_pid;
402 lfs_writer_lid = curlwp->l_lid;
403 mutex_exit(&lfs_lock);
404
405 /* Take an extra reference to the LFS vfsops. */
406 vfs = vfs_getopsbyname(MOUNT_LFS);
407
408 mutex_enter(&lfs_lock);
409 for (;;) {
410 KASSERT(mutex_owned(&lfs_lock));
411 if (wrote_something == 0)
412 mtsleep(&lfs_writer_daemon, PVM, "lfswriter", hz/10 + 1,
413 &lfs_lock);
414
415 KASSERT(mutex_owned(&lfs_lock));
416 loopcount = 0;
417 wrote_something = 0;
418
419 /*
420 * If global state wants a flush, flush everything.
421 */
422 if (lfs_do_flush || locked_queue_count > LFS_MAX_BUFS ||
423 locked_queue_bytes > LFS_MAX_BYTES ||
424 lfs_subsys_pages > LFS_MAX_PAGES) {
425
426 if (lfs_do_flush) {
427 DLOG((DLOG_FLUSH, "lfs_writerd: lfs_do_flush\n"));
428 }
429 if (locked_queue_count > LFS_MAX_BUFS) {
430 DLOG((DLOG_FLUSH, "lfs_writerd: lqc = %d, max %d\n",
431 locked_queue_count, LFS_MAX_BUFS));
432 }
433 if (locked_queue_bytes > LFS_MAX_BYTES) {
434 DLOG((DLOG_FLUSH, "lfs_writerd: lqb = %ld, max %ld\n",
435 locked_queue_bytes, LFS_MAX_BYTES));
436 }
437 if (lfs_subsys_pages > LFS_MAX_PAGES) {
438 DLOG((DLOG_FLUSH, "lfs_writerd: lssp = %d, max %d\n",
439 lfs_subsys_pages, LFS_MAX_PAGES));
440 }
441
442 lfs_flush(NULL, SEGM_WRITERD, 0);
443 lfs_do_flush = 0;
444 KASSERT(mutex_owned(&lfs_lock));
445 continue;
446 }
447 KASSERT(mutex_owned(&lfs_lock));
448 mutex_exit(&lfs_lock);
449
450 /*
451 * Look through the list of LFSs to see if any of them
452 * have requested pageouts.
453 */
454 mutex_enter(&mountlist_lock);
455 lfsc = 0;
456 skipc = 0;
457 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
458 mp = nmp) {
459 if (vfs_busy(mp, &nmp)) {
460 ++skipc;
461 continue;
462 }
463 KASSERT(!mutex_owned(&lfs_lock));
464 if (strncmp(mp->mnt_stat.f_fstypename, MOUNT_LFS,
465 sizeof(mp->mnt_stat.f_fstypename)) == 0) {
466 ++lfsc;
467 fs = VFSTOULFS(mp)->um_lfs;
468 int32_t ooffset = 0;
469 fsflags = SEGM_SINGLE;
470
471 mutex_enter(&lfs_lock);
472 ooffset = fs->lfs_offset;
473
474 if (fs->lfs_nextseg < fs->lfs_curseg && fs->lfs_nowrap) {
475 /* Don't try to write if we're suspended */
476 mutex_exit(&lfs_lock);
477 vfs_unbusy(mp, false, &nmp);
478 continue;
479 }
480 if (LFS_STARVED_FOR_SEGS(fs)) {
481 mutex_exit(&lfs_lock);
482
483 DLOG((DLOG_FLUSH, "lfs_writerd: need cleaning before writing possible\n"));
484 lfs_wakeup_cleaner(fs);
485 vfs_unbusy(mp, false, &nmp);
486 continue;
487 }
488
489 if ((fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
490 lfs_dirvcount > LFS_MAX_DIROP) &&
491 fs->lfs_dirops == 0) {
492 fsflags &= ~SEGM_SINGLE;
493 fsflags |= SEGM_CKP;
494 DLOG((DLOG_FLUSH, "lfs_writerd: checkpoint\n"));
495 lfs_flush_fs(fs, fsflags);
496 } else if (fs->lfs_pdflush) {
497 DLOG((DLOG_FLUSH, "lfs_writerd: pdflush set\n"));
498 lfs_flush_fs(fs, fsflags);
499 } else if (!TAILQ_EMPTY(&fs->lfs_pchainhd)) {
500 DLOG((DLOG_FLUSH, "lfs_writerd: pchain non-empty\n"));
501 mutex_exit(&lfs_lock);
502 lfs_writer_enter(fs, "wrdirop");
503 lfs_flush_pchain(fs);
504 lfs_writer_leave(fs);
505 mutex_enter(&lfs_lock);
506 }
507 if (fs->lfs_offset != ooffset)
508 ++wrote_something;
509 mutex_exit(&lfs_lock);
510 }
511 KASSERT(!mutex_owned(&lfs_lock));
512 vfs_unbusy(mp, false, &nmp);
513 }
514 if (lfsc + skipc == 0) {
515 mutex_enter(&lfs_lock);
516 lfs_writer_daemon = 0;
517 lfs_writer_lid = 0;
518 mutex_exit(&lfs_lock);
519 mutex_exit(&mountlist_lock);
520 break;
521 }
522 mutex_exit(&mountlist_lock);
523
524 mutex_enter(&lfs_lock);
525 }
526 KASSERT(!mutex_owned(&lfs_lock));
527 KASSERT(!mutex_owned(&mountlist_lock));
528
529 /* Give up our extra reference so the module can be unloaded. */
530 mutex_enter(&vfs_list_lock);
531 if (vfs != NULL)
532 vfs->vfs_refcount--;
533 mutex_exit(&vfs_list_lock);
534
535 /* Done! */
536 kthread_exit(0);
537 }
538
539 /*
540 * Initialize the filesystem, most work done by ulfs_init.
541 */
542 void
543 lfs_init(void)
544 {
545
546 malloc_type_attach(M_SEGMENT);
547 pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
548 "lfsinopl", &pool_allocator_nointr, IPL_NONE);
549 pool_init(&lfs_dinode_pool, sizeof(struct ulfs1_dinode), 0, 0, 0,
550 "lfsdinopl", &pool_allocator_nointr, IPL_NONE);
551 pool_init(&lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0,
552 "lfsinoextpl", &pool_allocator_nointr, IPL_NONE);
553 pool_init(&lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0,
554 "lfslbnpool", &pool_allocator_nointr, IPL_NONE);
555 ulfs_init();
556
557 #ifdef DEBUG
558 memset(lfs_log, 0, sizeof(lfs_log));
559 #endif
560 mutex_init(&lfs_lock, MUTEX_DEFAULT, IPL_NONE);
561 cv_init(&locked_queue_cv, "lfsbuf");
562 cv_init(&lfs_writing_cv, "lfsflush");
563 }
564
565 void
566 lfs_reinit(void)
567 {
568 ulfs_reinit();
569 }
570
571 void
572 lfs_done(void)
573 {
574 ulfs_done();
575 mutex_destroy(&lfs_lock);
576 cv_destroy(&locked_queue_cv);
577 cv_destroy(&lfs_writing_cv);
578 pool_destroy(&lfs_inode_pool);
579 pool_destroy(&lfs_dinode_pool);
580 pool_destroy(&lfs_inoext_pool);
581 pool_destroy(&lfs_lbnentry_pool);
582 malloc_type_detach(M_SEGMENT);
583 }
584
585 /*
586 * Called by main() when ulfs is going to be mounted as root.
587 */
588 int
589 lfs_mountroot(void)
590 {
591 extern struct vnode *rootvp;
592 struct lfs *fs = NULL; /* LFS */
593 struct mount *mp;
594 struct lwp *l = curlwp;
595 struct ulfsmount *ump;
596 int error;
597
598 if (device_class(root_device) != DV_DISK)
599 return (ENODEV);
600
601 if (rootdev == NODEV)
602 return (ENODEV);
603 if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
604 vrele(rootvp);
605 return (error);
606 }
607 if ((error = lfs_mountfs(rootvp, mp, l))) {
608 vfs_unbusy(mp, false, NULL);
609 vfs_destroy(mp);
610 return (error);
611 }
612 mutex_enter(&mountlist_lock);
613 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
614 mutex_exit(&mountlist_lock);
615 ump = VFSTOULFS(mp);
616 fs = ump->um_lfs;
617 memset(fs->lfs_fsmnt, 0, sizeof(fs->lfs_fsmnt));
618 (void)copystr(mp->mnt_stat.f_mntonname, fs->lfs_fsmnt, MNAMELEN - 1, 0);
619 (void)lfs_statvfs(mp, &mp->mnt_stat);
620 vfs_unbusy(mp, false, NULL);
621 setrootfstime((time_t)(VFSTOULFS(mp)->um_lfs->lfs_tstamp));
622 return (0);
623 }
624
625 /*
626 * VFS Operations.
627 *
628 * mount system call
629 */
630 int
631 lfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
632 {
633 struct lwp *l = curlwp;
634 struct vnode *devvp;
635 struct ulfs_args *args = data;
636 struct ulfsmount *ump = NULL;
637 struct lfs *fs = NULL; /* LFS */
638 int error = 0, update;
639 mode_t accessmode;
640
641 if (*data_len < sizeof *args)
642 return EINVAL;
643
644 if (mp->mnt_flag & MNT_GETARGS) {
645 ump = VFSTOULFS(mp);
646 if (ump == NULL)
647 return EIO;
648 args->fspec = NULL;
649 *data_len = sizeof *args;
650 return 0;
651 }
652
653 update = mp->mnt_flag & MNT_UPDATE;
654
655 /* Check arguments */
656 if (args->fspec != NULL) {
657 /*
658 * Look up the name and verify that it's sane.
659 */
660 error = namei_simple_user(args->fspec,
661 NSM_FOLLOW_NOEMULROOT, &devvp);
662 if (error != 0)
663 return (error);
664
665 if (!update) {
666 /*
667 * Be sure this is a valid block device
668 */
669 if (devvp->v_type != VBLK)
670 error = ENOTBLK;
671 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
672 error = ENXIO;
673 } else {
674 /*
675 * Be sure we're still naming the same device
676 * used for our initial mount
677 */
678 ump = VFSTOULFS(mp);
679 if (devvp != ump->um_devvp) {
680 if (devvp->v_rdev != ump->um_devvp->v_rdev)
681 error = EINVAL;
682 else {
683 vrele(devvp);
684 devvp = ump->um_devvp;
685 vref(devvp);
686 }
687 }
688 }
689 } else {
690 if (!update) {
691 /* New mounts must have a filename for the device */
692 return (EINVAL);
693 } else {
694 /* Use the extant mount */
695 ump = VFSTOULFS(mp);
696 devvp = ump->um_devvp;
697 vref(devvp);
698 }
699 }
700
701
702 /*
703 * If mount by non-root, then verify that user has necessary
704 * permissions on the device.
705 */
706 if (error == 0) {
707 accessmode = VREAD;
708 if (update ?
709 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
710 (mp->mnt_flag & MNT_RDONLY) == 0)
711 accessmode |= VWRITE;
712 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
713 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
714 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
715 KAUTH_ARG(accessmode));
716 VOP_UNLOCK(devvp);
717 }
718
719 if (error) {
720 vrele(devvp);
721 return (error);
722 }
723
724 if (!update) {
725 int flags;
726
727 if (mp->mnt_flag & MNT_RDONLY)
728 flags = FREAD;
729 else
730 flags = FREAD|FWRITE;
731 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
732 error = VOP_OPEN(devvp, flags, FSCRED);
733 VOP_UNLOCK(devvp);
734 if (error)
735 goto fail;
736 error = lfs_mountfs(devvp, mp, l); /* LFS */
737 if (error) {
738 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
739 (void)VOP_CLOSE(devvp, flags, NOCRED);
740 VOP_UNLOCK(devvp);
741 goto fail;
742 }
743
744 ump = VFSTOULFS(mp);
745 fs = ump->um_lfs;
746 } else {
747 /*
748 * Update the mount.
749 */
750
751 /*
752 * The initial mount got a reference on this
753 * device, so drop the one obtained via
754 * namei(), above.
755 */
756 vrele(devvp);
757
758 ump = VFSTOULFS(mp);
759 fs = ump->um_lfs;
760 if (fs->lfs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
761 /*
762 * Changing from read-only to read/write.
763 * Note in the superblocks that we're writing.
764 */
765 fs->lfs_ronly = 0;
766 if (fs->lfs_pflags & LFS_PF_CLEAN) {
767 fs->lfs_pflags &= ~LFS_PF_CLEAN;
768 lfs_writesuper(fs, fs->lfs_sboffs[0]);
769 lfs_writesuper(fs, fs->lfs_sboffs[1]);
770 }
771 }
772 if (args->fspec == NULL)
773 return EINVAL;
774 }
775
776 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
777 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
778 if (error == 0)
779 (void)strncpy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname,
780 sizeof(fs->lfs_fsmnt));
781 return error;
782
783 fail:
784 vrele(devvp);
785 return (error);
786 }
787
788
789 /*
790 * Common code for mount and mountroot
791 * LFS specific
792 */
793 int
794 lfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
795 {
796 struct dlfs *tdfs, *dfs, *adfs;
797 struct lfs *fs;
798 struct ulfsmount *ump;
799 struct vnode *vp;
800 struct buf *bp, *abp;
801 dev_t dev;
802 int error, i, ronly, fsbsize;
803 kauth_cred_t cred;
804 CLEANERINFO *cip;
805 SEGUSE *sup;
806 daddr_t sb_addr;
807
808 cred = l ? l->l_cred : NOCRED;
809
810 /*
811 * Flush out any old buffers remaining from a previous use.
812 */
813 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
814 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
815 VOP_UNLOCK(devvp);
816 if (error)
817 return (error);
818
819 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
820
821 /* Don't free random space on error. */
822 bp = NULL;
823 abp = NULL;
824 ump = NULL;
825
826 sb_addr = LFS_LABELPAD / DEV_BSIZE;
827 while (1) {
828 /* Read in the superblock. */
829 error = bread(devvp, sb_addr, LFS_SBPAD, cred, 0, &bp);
830 if (error)
831 goto out;
832 dfs = (struct dlfs *)bp->b_data;
833
834 /* Check the basics. */
835 if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize > MAXBSIZE ||
836 dfs->dlfs_version > LFS_VERSION ||
837 dfs->dlfs_bsize < sizeof(struct dlfs)) {
838 DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock sanity failed\n"));
839 error = EINVAL; /* XXX needs translation */
840 goto out;
841 }
842 if (dfs->dlfs_inodefmt > LFS_MAXINODEFMT) {
843 DLOG((DLOG_MOUNT, "lfs_mountfs: unknown inode format %d\n",
844 dfs->dlfs_inodefmt));
845 error = EINVAL;
846 goto out;
847 }
848
849 if (dfs->dlfs_version == 1)
850 fsbsize = DEV_BSIZE;
851 else {
852 fsbsize = 1 << dfs->dlfs_ffshift;
853 /*
854 * Could be, if the frag size is large enough, that we
855 * don't have the "real" primary superblock. If that's
856 * the case, get the real one, and try again.
857 */
858 if (sb_addr != (dfs->dlfs_sboffs[0] << (dfs->dlfs_ffshift - DEV_BSHIFT))) {
859 DLOG((DLOG_MOUNT, "lfs_mountfs: sb daddr"
860 " 0x%llx is not right, trying 0x%llx\n",
861 (long long)sb_addr,
862 (long long)(dfs->dlfs_sboffs[0] << (dfs->dlfs_ffshift - DEV_BSHIFT))));
863 sb_addr = dfs->dlfs_sboffs[0] << (dfs->dlfs_ffshift - DEV_BSHIFT);
864 brelse(bp, 0);
865 continue;
866 }
867 }
868 break;
869 }
870
871 /*
872 * Check the second superblock to see which is newer; then mount
873 * using the older of the two. This is necessary to ensure that
874 * the filesystem is valid if it was not unmounted cleanly.
875 */
876
877 if (dfs->dlfs_sboffs[1] &&
878 dfs->dlfs_sboffs[1] - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize)
879 {
880 error = bread(devvp, dfs->dlfs_sboffs[1] * (fsbsize / DEV_BSIZE),
881 LFS_SBPAD, cred, 0, &abp);
882 if (error)
883 goto out;
884 adfs = (struct dlfs *)abp->b_data;
885
886 if (dfs->dlfs_version == 1) {
887 /* 1s resolution comparison */
888 if (adfs->dlfs_tstamp < dfs->dlfs_tstamp)
889 tdfs = adfs;
890 else
891 tdfs = dfs;
892 } else {
893 /* monotonic infinite-resolution comparison */
894 if (adfs->dlfs_serial < dfs->dlfs_serial)
895 tdfs = adfs;
896 else
897 tdfs = dfs;
898 }
899
900 /* Check the basics. */
901 if (tdfs->dlfs_magic != LFS_MAGIC ||
902 tdfs->dlfs_bsize > MAXBSIZE ||
903 tdfs->dlfs_version > LFS_VERSION ||
904 tdfs->dlfs_bsize < sizeof(struct dlfs)) {
905 DLOG((DLOG_MOUNT, "lfs_mountfs: alt superblock"
906 " sanity failed\n"));
907 error = EINVAL; /* XXX needs translation */
908 goto out;
909 }
910 } else {
911 DLOG((DLOG_MOUNT, "lfs_mountfs: invalid alt superblock"
912 " daddr=0x%x\n", dfs->dlfs_sboffs[1]));
913 error = EINVAL;
914 goto out;
915 }
916
917 /* Allocate the mount structure, copy the superblock into it. */
918 fs = kmem_zalloc(sizeof(struct lfs), KM_SLEEP);
919 memcpy(&fs->lfs_dlfs, tdfs, sizeof(struct dlfs));
920
921 /* Compatibility */
922 if (fs->lfs_version < 2) {
923 fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
924 fs->lfs_ibsize = fs->lfs_bsize;
925 fs->lfs_start = fs->lfs_sboffs[0];
926 fs->lfs_tstamp = fs->lfs_otstamp;
927 fs->lfs_fsbtodb = 0;
928 }
929 if (fs->lfs_resvseg == 0)
930 fs->lfs_resvseg = MIN(fs->lfs_minfreeseg - 1, \
931 MAX(MIN_RESV_SEGS, fs->lfs_minfreeseg / 2 + 1));
932
933 /*
934 * If we aren't going to be able to write meaningfully to this
935 * filesystem, and were not mounted readonly, bomb out now.
936 */
937 if (lfs_fsbtob(fs, LFS_NRESERVE(fs)) > LFS_MAX_BYTES && !ronly) {
938 DLOG((DLOG_MOUNT, "lfs_mount: to mount this filesystem read/write,"
939 " we need BUFPAGES >= %lld\n",
940 (long long)((bufmem_hiwater / bufmem_lowater) *
941 LFS_INVERSE_MAX_BYTES(
942 lfs_fsbtob(fs, LFS_NRESERVE(fs))) >> PAGE_SHIFT)));
943 kmem_free(fs, sizeof(struct lfs));
944 error = EFBIG; /* XXX needs translation */
945 goto out;
946 }
947
948 /* Before rolling forward, lock so vget will sleep for other procs */
949 if (l != NULL) {
950 fs->lfs_flags = LFS_NOTYET;
951 fs->lfs_rfpid = l->l_proc->p_pid;
952 }
953
954 ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
955 ump->um_lfs = fs;
956 ump->um_fstype = ULFS1;
957 if (sizeof(struct lfs) < LFS_SBPAD) { /* XXX why? */
958 brelse(bp, BC_INVAL);
959 brelse(abp, BC_INVAL);
960 } else {
961 brelse(bp, 0);
962 brelse(abp, 0);
963 }
964 bp = NULL;
965 abp = NULL;
966
967
968 /* Set up the I/O information */
969 fs->lfs_devbsize = DEV_BSIZE;
970 fs->lfs_iocount = 0;
971 fs->lfs_diropwait = 0;
972 fs->lfs_activesb = 0;
973 fs->lfs_uinodes = 0;
974 fs->lfs_ravail = 0;
975 fs->lfs_favail = 0;
976 fs->lfs_sbactive = 0;
977
978 /* Set up the ifile and lock aflags */
979 fs->lfs_doifile = 0;
980 fs->lfs_writer = 0;
981 fs->lfs_dirops = 0;
982 fs->lfs_nadirop = 0;
983 fs->lfs_seglock = 0;
984 fs->lfs_pdflush = 0;
985 fs->lfs_sleepers = 0;
986 fs->lfs_pages = 0;
987 rw_init(&fs->lfs_fraglock);
988 rw_init(&fs->lfs_iflock);
989 cv_init(&fs->lfs_stopcv, "lfsstop");
990
991 /* Set the file system readonly/modify bits. */
992 fs->lfs_ronly = ronly;
993 if (ronly == 0)
994 fs->lfs_fmod = 1;
995
996 /* ulfs-level information */
997 fs->um_flags = 0;
998 fs->um_bptrtodb = fs->lfs_ffshift - DEV_BSHIFT;
999 fs->um_seqinc = fs->lfs_frag;
1000 fs->um_nindir = fs->lfs_nindir;
1001 fs->um_lognindir = ffs(fs->lfs_nindir) - 1;
1002 fs->um_maxsymlinklen = fs->lfs_maxsymlinklen;
1003 fs->um_dirblksiz = LFS_DIRBLKSIZ;
1004 fs->um_maxfilesize = fs->lfs_maxfilesize;
1005
1006 /* quota stuff */
1007 /* XXX: these need to come from the on-disk superblock to be used */
1008 fs->lfs_use_quota2 = 0;
1009 fs->lfs_quota_magic = 0;
1010 fs->lfs_quota_flags = 0;
1011 fs->lfs_quotaino[0] = 0;
1012 fs->lfs_quotaino[1] = 0;
1013
1014 /* Initialize the mount structure. */
1015 dev = devvp->v_rdev;
1016 mp->mnt_data = ump;
1017 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
1018 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_LFS);
1019 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
1020 mp->mnt_stat.f_namemax = LFS_MAXNAMLEN;
1021 mp->mnt_stat.f_iosize = fs->lfs_bsize;
1022 mp->mnt_flag |= MNT_LOCAL;
1023 mp->mnt_fs_bshift = fs->lfs_bshift;
1024 if (fs->um_maxsymlinklen > 0)
1025 mp->mnt_iflag |= IMNT_DTYPE;
1026
1027 ump->um_mountp = mp;
1028 ump->um_dev = dev;
1029 ump->um_devvp = devvp;
1030 for (i = 0; i < ULFS_MAXQUOTAS; i++)
1031 ump->um_quotas[i] = NULLVP;
1032 devvp->v_specmountpoint = mp;
1033
1034 /* Set up reserved memory for pageout */
1035 lfs_setup_resblks(fs);
1036 /* Set up vdirop tailq */
1037 TAILQ_INIT(&fs->lfs_dchainhd);
1038 /* and paging tailq */
1039 TAILQ_INIT(&fs->lfs_pchainhd);
1040 /* and delayed segment accounting for truncation list */
1041 LIST_INIT(&fs->lfs_segdhd);
1042
1043 /*
1044 * We use the ifile vnode for almost every operation. Instead of
1045 * retrieving it from the hash table each time we retrieve it here,
1046 * artificially increment the reference count and keep a pointer
1047 * to it in the incore copy of the superblock.
1048 */
1049 if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0) {
1050 DLOG((DLOG_MOUNT, "lfs_mountfs: ifile vget failed, error=%d\n", error));
1051 goto out;
1052 }
1053 fs->lfs_ivnode = vp;
1054 vref(vp);
1055
1056 /* Set up inode bitmap and order free list */
1057 lfs_order_freelist(fs);
1058
1059 /* Set up segment usage flags for the autocleaner. */
1060 fs->lfs_nactive = 0;
1061 fs->lfs_suflags = (u_int32_t **)malloc(2 * sizeof(u_int32_t *),
1062 M_SEGMENT, M_WAITOK);
1063 fs->lfs_suflags[0] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
1064 M_SEGMENT, M_WAITOK);
1065 fs->lfs_suflags[1] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
1066 M_SEGMENT, M_WAITOK);
1067 memset(fs->lfs_suflags[1], 0, fs->lfs_nseg * sizeof(u_int32_t));
1068 for (i = 0; i < fs->lfs_nseg; i++) {
1069 int changed;
1070
1071 LFS_SEGENTRY(sup, fs, i, bp);
1072 changed = 0;
1073 if (!ronly) {
1074 if (sup->su_nbytes == 0 &&
1075 !(sup->su_flags & SEGUSE_EMPTY)) {
1076 sup->su_flags |= SEGUSE_EMPTY;
1077 ++changed;
1078 } else if (!(sup->su_nbytes == 0) &&
1079 (sup->su_flags & SEGUSE_EMPTY)) {
1080 sup->su_flags &= ~SEGUSE_EMPTY;
1081 ++changed;
1082 }
1083 if (sup->su_flags & (SEGUSE_ACTIVE|SEGUSE_INVAL)) {
1084 sup->su_flags &= ~(SEGUSE_ACTIVE|SEGUSE_INVAL);
1085 ++changed;
1086 }
1087 }
1088 fs->lfs_suflags[0][i] = sup->su_flags;
1089 if (changed)
1090 LFS_WRITESEGENTRY(sup, fs, i, bp);
1091 else
1092 brelse(bp, 0);
1093 }
1094
1095 #ifdef LFS_KERNEL_RFW
1096 lfs_roll_forward(fs, mp, l);
1097 #endif
1098
1099 /* If writing, sb is not clean; record in case of immediate crash */
1100 if (!fs->lfs_ronly) {
1101 fs->lfs_pflags &= ~LFS_PF_CLEAN;
1102 lfs_writesuper(fs, fs->lfs_sboffs[0]);
1103 lfs_writesuper(fs, fs->lfs_sboffs[1]);
1104 }
1105
1106 /* Allow vget now that roll-forward is complete */
1107 fs->lfs_flags &= ~(LFS_NOTYET);
1108 wakeup(&fs->lfs_flags);
1109
1110 /*
1111 * Initialize the ifile cleaner info with information from
1112 * the superblock.
1113 */
1114 LFS_CLEANERINFO(cip, fs, bp);
1115 cip->clean = fs->lfs_nclean;
1116 cip->dirty = fs->lfs_nseg - fs->lfs_nclean;
1117 cip->avail = fs->lfs_avail;
1118 cip->bfree = fs->lfs_bfree;
1119 (void) LFS_BWRITE_LOG(bp); /* Ifile */
1120
1121 /*
1122 * Mark the current segment as ACTIVE, since we're going to
1123 * be writing to it.
1124 */
1125 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, fs->lfs_offset), bp);
1126 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1127 fs->lfs_nactive++;
1128 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, fs->lfs_offset), bp); /* Ifile */
1129
1130 /* Now that roll-forward is done, unlock the Ifile */
1131 vput(vp);
1132
1133 /* Start the pagedaemon-anticipating daemon */
1134 mutex_enter(&lfs_lock);
1135 if (lfs_writer_daemon == 0 && lfs_writer_lid == 0 &&
1136 kthread_create(PRI_BIO, 0, NULL,
1137 lfs_writerd, NULL, NULL, "lfs_writer") != 0)
1138 panic("fork lfs_writer");
1139 mutex_exit(&lfs_lock);
1140
1141 printf("WARNING: the log-structured file system is experimental\n"
1142 "WARNING: it may cause system crashes and/or corrupt data\n");
1143
1144 return (0);
1145
1146 out:
1147 if (bp)
1148 brelse(bp, 0);
1149 if (abp)
1150 brelse(abp, 0);
1151 if (ump) {
1152 kmem_free(ump->um_lfs, sizeof(struct lfs));
1153 kmem_free(ump, sizeof(*ump));
1154 mp->mnt_data = NULL;
1155 }
1156
1157 return (error);
1158 }
1159
1160 /*
1161 * unmount system call
1162 */
1163 int
1164 lfs_unmount(struct mount *mp, int mntflags)
1165 {
1166 struct lwp *l = curlwp;
1167 struct ulfsmount *ump;
1168 struct lfs *fs;
1169 int error, flags, ronly;
1170 vnode_t *vp;
1171
1172 flags = 0;
1173 if (mntflags & MNT_FORCE)
1174 flags |= FORCECLOSE;
1175
1176 ump = VFSTOULFS(mp);
1177 fs = ump->um_lfs;
1178
1179 /* Two checkpoints */
1180 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
1181 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
1182
1183 /* wake up the cleaner so it can die */
1184 lfs_wakeup_cleaner(fs);
1185 mutex_enter(&lfs_lock);
1186 while (fs->lfs_sleepers)
1187 mtsleep(&fs->lfs_sleepers, PRIBIO + 1, "lfs_sleepers", 0,
1188 &lfs_lock);
1189 mutex_exit(&lfs_lock);
1190
1191 #ifdef LFS_QUOTA
1192 if ((error = lfsquota1_umount(mp, flags)) != 0)
1193 return (error);
1194 #endif
1195 if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
1196 return (error);
1197 if ((error = VFS_SYNC(mp, 1, l->l_cred)) != 0)
1198 return (error);
1199 vp = fs->lfs_ivnode;
1200 mutex_enter(vp->v_interlock);
1201 if (LIST_FIRST(&vp->v_dirtyblkhd))
1202 panic("lfs_unmount: still dirty blocks on ifile vnode");
1203 mutex_exit(vp->v_interlock);
1204
1205 /* Explicitly write the superblock, to update serial and pflags */
1206 fs->lfs_pflags |= LFS_PF_CLEAN;
1207 lfs_writesuper(fs, fs->lfs_sboffs[0]);
1208 lfs_writesuper(fs, fs->lfs_sboffs[1]);
1209 mutex_enter(&lfs_lock);
1210 while (fs->lfs_iocount)
1211 mtsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0,
1212 &lfs_lock);
1213 mutex_exit(&lfs_lock);
1214
1215 /* Finish with the Ifile, now that we're done with it */
1216 vgone(fs->lfs_ivnode);
1217
1218 ronly = !fs->lfs_ronly;
1219 if (ump->um_devvp->v_type != VBAD)
1220 ump->um_devvp->v_specmountpoint = NULL;
1221 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1222 error = VOP_CLOSE(ump->um_devvp,
1223 ronly ? FREAD : FREAD|FWRITE, NOCRED);
1224 vput(ump->um_devvp);
1225
1226 /* Complain about page leakage */
1227 if (fs->lfs_pages > 0)
1228 printf("lfs_unmount: still claim %d pages (%d in subsystem)\n",
1229 fs->lfs_pages, lfs_subsys_pages);
1230
1231 /* Free per-mount data structures */
1232 free(fs->lfs_ino_bitmap, M_SEGMENT);
1233 free(fs->lfs_suflags[0], M_SEGMENT);
1234 free(fs->lfs_suflags[1], M_SEGMENT);
1235 free(fs->lfs_suflags, M_SEGMENT);
1236 lfs_free_resblks(fs);
1237 cv_destroy(&fs->lfs_stopcv);
1238 rw_destroy(&fs->lfs_fraglock);
1239 rw_destroy(&fs->lfs_iflock);
1240
1241 kmem_free(fs, sizeof(struct lfs));
1242 kmem_free(ump, sizeof(*ump));
1243
1244 mp->mnt_data = NULL;
1245 mp->mnt_flag &= ~MNT_LOCAL;
1246 return (error);
1247 }
1248
1249 /*
1250 * Get file system statistics.
1251 *
1252 * NB: We don't lock to access the superblock here, because it's not
1253 * really that important if we get it wrong.
1254 */
1255 int
1256 lfs_statvfs(struct mount *mp, struct statvfs *sbp)
1257 {
1258 struct lfs *fs;
1259 struct ulfsmount *ump;
1260
1261 ump = VFSTOULFS(mp);
1262 fs = ump->um_lfs;
1263 if (fs->lfs_magic != LFS_MAGIC)
1264 panic("lfs_statvfs: magic");
1265
1266 sbp->f_bsize = fs->lfs_bsize;
1267 sbp->f_frsize = fs->lfs_fsize;
1268 sbp->f_iosize = fs->lfs_bsize;
1269 sbp->f_blocks = LFS_EST_NONMETA(fs) - VTOI(fs->lfs_ivnode)->i_lfs_effnblks;
1270
1271 sbp->f_bfree = LFS_EST_BFREE(fs);
1272 KASSERT(sbp->f_bfree <= fs->lfs_dsize);
1273 #if 0
1274 if (sbp->f_bfree < 0)
1275 sbp->f_bfree = 0;
1276 #endif
1277
1278 sbp->f_bresvd = LFS_EST_RSVD(fs);
1279 if (sbp->f_bfree > sbp->f_bresvd)
1280 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1281 else
1282 sbp->f_bavail = 0;
1283
1284 sbp->f_files = fs->lfs_bfree / lfs_btofsb(fs, fs->lfs_ibsize)
1285 * LFS_INOPB(fs);
1286 sbp->f_ffree = sbp->f_files - fs->lfs_nfiles;
1287 sbp->f_favail = sbp->f_ffree;
1288 sbp->f_fresvd = 0;
1289 copy_statvfs_info(sbp, mp);
1290 return (0);
1291 }
1292
1293 /*
1294 * Go through the disk queues to initiate sandbagged IO;
1295 * go through the inodes to write those that have been modified;
1296 * initiate the writing of the super block if it has been modified.
1297 *
1298 * Note: we are always called with the filesystem marked `MPBUSY'.
1299 */
1300 int
1301 lfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
1302 {
1303 int error;
1304 struct lfs *fs;
1305
1306 fs = VFSTOULFS(mp)->um_lfs;
1307 if (fs->lfs_ronly)
1308 return 0;
1309
1310 /* Snapshots should not hose the syncer */
1311 /*
1312 * XXX Sync can block here anyway, since we don't have a very
1313 * XXX good idea of how much data is pending. If it's more
1314 * XXX than a segment and lfs_nextseg is close to the end of
1315 * XXX the log, we'll likely block.
1316 */
1317 mutex_enter(&lfs_lock);
1318 if (fs->lfs_nowrap && fs->lfs_nextseg < fs->lfs_curseg) {
1319 mutex_exit(&lfs_lock);
1320 return 0;
1321 }
1322 mutex_exit(&lfs_lock);
1323
1324 lfs_writer_enter(fs, "lfs_dirops");
1325
1326 /* All syncs must be checkpoints until roll-forward is implemented. */
1327 DLOG((DLOG_FLUSH, "lfs_sync at 0x%x\n", fs->lfs_offset));
1328 error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
1329 lfs_writer_leave(fs);
1330 #ifdef LFS_QUOTA
1331 lfs_qsync(mp);
1332 #endif
1333 return (error);
1334 }
1335
1336 /*
1337 * Look up an LFS dinode number to find its incore vnode. If not already
1338 * in core, read it in from the specified device. Return the inode locked.
1339 * Detection and handling of mount points must be done by the calling routine.
1340 */
1341 int
1342 lfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1343 {
1344 struct lfs *fs;
1345 struct ulfs1_dinode *dip;
1346 struct inode *ip;
1347 struct buf *bp;
1348 struct ifile *ifp;
1349 struct vnode *vp;
1350 struct ulfsmount *ump;
1351 daddr_t daddr;
1352 dev_t dev;
1353 int error, retries;
1354 struct timespec ts;
1355
1356 memset(&ts, 0, sizeof ts); /* XXX gcc */
1357
1358 ump = VFSTOULFS(mp);
1359 dev = ump->um_dev;
1360 fs = ump->um_lfs;
1361
1362 /*
1363 * If the filesystem is not completely mounted yet, suspend
1364 * any access requests (wait for roll-forward to complete).
1365 */
1366 mutex_enter(&lfs_lock);
1367 while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
1368 mtsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0,
1369 &lfs_lock);
1370 mutex_exit(&lfs_lock);
1371
1372 retry:
1373 if ((*vpp = ulfs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1374 return (0);
1375
1376 error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, NULL, &vp);
1377 if (error) {
1378 *vpp = NULL;
1379 return (error);
1380 }
1381
1382 mutex_enter(&ulfs_hashlock);
1383 if (ulfs_ihashget(dev, ino, 0) != NULL) {
1384 mutex_exit(&ulfs_hashlock);
1385 ungetnewvnode(vp);
1386 goto retry;
1387 }
1388
1389 /* Translate the inode number to a disk address. */
1390 if (ino == LFS_IFILE_INUM)
1391 daddr = fs->lfs_idaddr;
1392 else {
1393 /* XXX bounds-check this too */
1394 LFS_IENTRY(ifp, fs, ino, bp);
1395 daddr = ifp->if_daddr;
1396 if (fs->lfs_version > 1) {
1397 ts.tv_sec = ifp->if_atime_sec;
1398 ts.tv_nsec = ifp->if_atime_nsec;
1399 }
1400
1401 brelse(bp, 0);
1402 if (daddr == LFS_UNUSED_DADDR) {
1403 *vpp = NULLVP;
1404 mutex_exit(&ulfs_hashlock);
1405 ungetnewvnode(vp);
1406 return (ENOENT);
1407 }
1408 }
1409
1410 /* Allocate/init new vnode/inode. */
1411 lfs_vcreate(mp, ino, vp);
1412
1413 /*
1414 * Put it onto its hash chain and lock it so that other requests for
1415 * this inode will block if they arrive while we are sleeping waiting
1416 * for old data structures to be purged or for the contents of the
1417 * disk portion of this inode to be read.
1418 */
1419 ip = VTOI(vp);
1420 ulfs_ihashins(ip);
1421 mutex_exit(&ulfs_hashlock);
1422
1423 /*
1424 * XXX
1425 * This may not need to be here, logically it should go down with
1426 * the i_devvp initialization.
1427 * Ask Kirk.
1428 */
1429 ip->i_lfs = ump->um_lfs;
1430
1431 /* Read in the disk contents for the inode, copy into the inode. */
1432 retries = 0;
1433 again:
1434 error = bread(ump->um_devvp, LFS_FSBTODB(fs, daddr),
1435 (fs->lfs_version == 1 ? fs->lfs_bsize : fs->lfs_ibsize),
1436 NOCRED, 0, &bp);
1437 if (error) {
1438 /*
1439 * The inode does not contain anything useful, so it would
1440 * be misleading to leave it on its hash chain. With mode
1441 * still zero, it will be unlinked and returned to the free
1442 * list by vput().
1443 */
1444 vput(vp);
1445 *vpp = NULL;
1446 return (error);
1447 }
1448
1449 dip = lfs_ifind(fs, ino, bp);
1450 if (dip == NULL) {
1451 /* Assume write has not completed yet; try again */
1452 brelse(bp, BC_INVAL);
1453 ++retries;
1454 if (retries > LFS_IFIND_RETRIES) {
1455 #ifdef DEBUG
1456 /* If the seglock is held look at the bpp to see
1457 what is there anyway */
1458 mutex_enter(&lfs_lock);
1459 if (fs->lfs_seglock > 0) {
1460 struct buf **bpp;
1461 struct ulfs1_dinode *dp;
1462 int i;
1463
1464 for (bpp = fs->lfs_sp->bpp;
1465 bpp != fs->lfs_sp->cbpp; ++bpp) {
1466 if ((*bpp)->b_vp == fs->lfs_ivnode &&
1467 bpp != fs->lfs_sp->bpp) {
1468 /* Inode block */
1469 printf("lfs_vget: block 0x%" PRIx64 ": ",
1470 (*bpp)->b_blkno);
1471 dp = (struct ulfs1_dinode *)(*bpp)->b_data;
1472 for (i = 0; i < LFS_INOPB(fs); i++)
1473 if (dp[i].di_inumber)
1474 printf("%d ", dp[i].di_inumber);
1475 printf("\n");
1476 }
1477 }
1478 }
1479 mutex_exit(&lfs_lock);
1480 #endif /* DEBUG */
1481 panic("lfs_vget: dinode not found");
1482 }
1483 mutex_enter(&lfs_lock);
1484 if (fs->lfs_iocount) {
1485 DLOG((DLOG_VNODE, "lfs_vget: dinode %d not found, retrying...\n", ino));
1486 (void)mtsleep(&fs->lfs_iocount, PRIBIO + 1,
1487 "lfs ifind", 1, &lfs_lock);
1488 } else
1489 retries = LFS_IFIND_RETRIES;
1490 mutex_exit(&lfs_lock);
1491 goto again;
1492 }
1493 *ip->i_din.ffs1_din = *dip;
1494 brelse(bp, 0);
1495
1496 if (fs->lfs_version > 1) {
1497 ip->i_ffs1_atime = ts.tv_sec;
1498 ip->i_ffs1_atimensec = ts.tv_nsec;
1499 }
1500
1501 lfs_vinit(mp, &vp);
1502
1503 *vpp = vp;
1504
1505 KASSERT(VOP_ISLOCKED(vp));
1506
1507 return (0);
1508 }
1509
1510 /*
1511 * File handle to vnode
1512 */
1513 int
1514 lfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1515 {
1516 struct lfid lfh;
1517 struct buf *bp;
1518 IFILE *ifp;
1519 int32_t daddr;
1520 struct lfs *fs;
1521 vnode_t *vp;
1522
1523 if (fhp->fid_len != sizeof(struct lfid))
1524 return EINVAL;
1525
1526 memcpy(&lfh, fhp, sizeof(lfh));
1527 if (lfh.lfid_ino < LFS_IFILE_INUM)
1528 return ESTALE;
1529
1530 fs = VFSTOULFS(mp)->um_lfs;
1531 if (lfh.lfid_ident != fs->lfs_ident)
1532 return ESTALE;
1533
1534 if (lfh.lfid_ino >
1535 ((VTOI(fs->lfs_ivnode)->i_ffs1_size >> fs->lfs_bshift) -
1536 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb)
1537 return ESTALE;
1538
1539 mutex_enter(&ulfs_ihash_lock);
1540 vp = ulfs_ihashlookup(VFSTOULFS(mp)->um_dev, lfh.lfid_ino);
1541 mutex_exit(&ulfs_ihash_lock);
1542 if (vp == NULL) {
1543 LFS_IENTRY(ifp, fs, lfh.lfid_ino, bp);
1544 daddr = ifp->if_daddr;
1545 brelse(bp, 0);
1546 if (daddr == LFS_UNUSED_DADDR)
1547 return ESTALE;
1548 }
1549
1550 return (ulfs_fhtovp(mp, &lfh.lfid_ufid, vpp));
1551 }
1552
1553 /*
1554 * Vnode pointer to File handle
1555 */
1556 /* ARGSUSED */
1557 int
1558 lfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1559 {
1560 struct inode *ip;
1561 struct lfid lfh;
1562
1563 if (*fh_size < sizeof(struct lfid)) {
1564 *fh_size = sizeof(struct lfid);
1565 return E2BIG;
1566 }
1567 *fh_size = sizeof(struct lfid);
1568 ip = VTOI(vp);
1569 memset(&lfh, 0, sizeof(lfh));
1570 lfh.lfid_len = sizeof(struct lfid);
1571 lfh.lfid_ino = ip->i_number;
1572 lfh.lfid_gen = ip->i_gen;
1573 lfh.lfid_ident = ip->i_lfs->lfs_ident;
1574 memcpy(fhp, &lfh, sizeof(lfh));
1575 return (0);
1576 }
1577
1578 /*
1579 * ulfs_bmaparray callback function for writing.
1580 *
1581 * Since blocks will be written to the new segment anyway,
1582 * we don't care about current daddr of them.
1583 */
1584 static bool
1585 lfs_issequential_hole(const struct lfs *fs,
1586 daddr_t daddr0, daddr_t daddr1)
1587 {
1588 (void)fs; /* not used */
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_lfs->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