lfs_vfsops.c revision 1.387 1 /* $NetBSD: lfs_vfsops.c,v 1.387 2025/09/17 04:37:47 perseant 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.387 2025/09/17 04:37:47 perseant Exp $");
65
66 #if defined(_KERNEL_OPT)
67 #include "opt_lfs.h"
68 #include "opt_quota.h"
69 #include "opt_uvmhist.h"
70 #endif
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/namei.h>
75 #include <sys/proc.h>
76 #include <sys/kernel.h>
77 #include <sys/vnode.h>
78 #include <sys/mount.h>
79 #include <sys/kthread.h>
80 #include <sys/buf.h>
81 #include <sys/device.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 <sys/sysctl.h>
91 #include <sys/conf.h>
92 #include <sys/kauth.h>
93 #include <sys/module.h>
94 #include <sys/syscallvar.h>
95 #include <sys/syscall.h>
96 #include <sys/syscallargs.h>
97
98 #include <miscfs/specfs/specdev.h>
99
100 #include <ufs/lfs/ulfs_quotacommon.h>
101 #include <ufs/lfs/ulfs_inode.h>
102 #include <ufs/lfs/ulfsmount.h>
103 #include <ufs/lfs/ulfs_bswap.h>
104 #include <ufs/lfs/ulfs_extern.h>
105
106 #ifdef UVMHIST
107 #include <uvm/uvm.h>
108 #endif
109 #include <uvm/uvm_extern.h>
110 #include <uvm/uvm_object.h>
111 #include <uvm/uvm_page.h>
112 #include <uvm/uvm_stat.h>
113
114 #include <ufs/lfs/lfs.h>
115 #include <ufs/lfs/lfs_accessors.h>
116 #include <ufs/lfs/lfs_kernel.h>
117 #include <ufs/lfs/lfs_extern.h>
118
119 #include <miscfs/genfs/genfs.h>
120 #include <miscfs/genfs/genfs_node.h>
121
122 MODULE(MODULE_CLASS_VFS, lfs, NULL);
123
124 static int lfs_gop_write(struct vnode *, struct vm_page **, int, int);
125 static int lfs_mountfs(struct vnode *, struct mount *, struct lwp *);
126 static int lfs_flushfiles(struct mount *, int);
127
128 extern const struct vnodeopv_desc lfs_vnodeop_opv_desc;
129 extern const struct vnodeopv_desc lfs_specop_opv_desc;
130 extern const struct vnodeopv_desc lfs_fifoop_opv_desc;
131
132 extern int locked_queue_rcount;
133 extern long locked_queue_rbytes;
134
135 struct lwp * lfs_writer_daemon = NULL;
136 kcondvar_t lfs_writerd_cv;
137
138 struct workqueue *lfs_cluster_wq = NULL;
139 struct workqueue *lfs_super_wq = NULL;
140
141 int lfs_do_flush = 0;
142 int lfs_do_rfw = 1;
143
144 const struct vnodeopv_desc * const lfs_vnodeopv_descs[] = {
145 &lfs_vnodeop_opv_desc,
146 &lfs_specop_opv_desc,
147 &lfs_fifoop_opv_desc,
148 NULL,
149 };
150
151 struct vfsops lfs_vfsops = {
152 .vfs_name = MOUNT_LFS,
153 .vfs_min_mount_data = sizeof (struct ulfs_args),
154 .vfs_mount = lfs_mount,
155 .vfs_start = ulfs_start,
156 .vfs_unmount = lfs_unmount,
157 .vfs_root = ulfs_root,
158 .vfs_quotactl = ulfs_quotactl,
159 .vfs_statvfs = lfs_statvfs,
160 .vfs_sync = lfs_sync,
161 .vfs_vget = lfs_vget,
162 .vfs_loadvnode = lfs_loadvnode,
163 .vfs_newvnode = lfs_newvnode,
164 .vfs_fhtovp = lfs_fhtovp,
165 .vfs_vptofh = lfs_vptofh,
166 .vfs_init = lfs_init,
167 .vfs_reinit = lfs_reinit,
168 .vfs_done = lfs_done,
169 .vfs_mountroot = lfs_mountroot,
170 .vfs_snapshot = (void *)eopnotsupp,
171 .vfs_extattrctl = lfs_extattrctl,
172 .vfs_suspendctl = genfs_suspendctl,
173 .vfs_renamelock_enter = genfs_renamelock_enter,
174 .vfs_renamelock_exit = genfs_renamelock_exit,
175 .vfs_fsync = (void *)eopnotsupp,
176 .vfs_opv_descs = lfs_vnodeopv_descs
177 };
178
179 const struct genfs_ops lfs_genfsops = {
180 .gop_size = lfs_gop_size,
181 .gop_alloc = ulfs_gop_alloc,
182 .gop_write = lfs_gop_write,
183 .gop_markupdate = ulfs_gop_markupdate,
184 .gop_putrange = genfs_gop_putrange,
185 };
186
187 struct shortlong {
188 const char *sname;
189 const char *lname;
190 };
191
192 static int
193 sysctl_lfs_dostats(SYSCTLFN_ARGS)
194 {
195 extern struct lfs_stats lfs_stats;
196 extern int lfs_dostats;
197 int error;
198
199 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
200 if (error || newp == NULL)
201 return (error);
202
203 if (lfs_dostats == 0)
204 memset(&lfs_stats, 0, sizeof(lfs_stats));
205
206 return (0);
207 }
208
209 SYSCTL_SETUP(lfs_sysctl_setup, "lfs sysctl")
210 {
211 int i;
212 extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead,
213 lfs_fs_pagetrip, lfs_ignore_lazy_sync;
214 #ifdef DEBUG
215 extern int lfs_debug_log_subsys[DLOG_MAX];
216 struct shortlong dlog_names[DLOG_MAX] = { /* Must match lfs.h ! */
217 { "rollforward", "Debug roll-forward code" },
218 { "alloc", "Debug inode allocation and free list" },
219 { "avail", "Debug space-available-now accounting" },
220 { "flush", "Debug flush triggers" },
221 { "lockedlist", "Debug locked list accounting" },
222 { "vnode_verbose", "Verbose per-vnode-written debugging" },
223 { "vnode", "Debug vnode use during segment write" },
224 { "segment", "Debug segment writing" },
225 { "seguse", "Debug segment used-bytes accounting" },
226 { "cleaner", "Debug cleaning routines" },
227 { "mount", "Debug mount/unmount routines" },
228 { "pagecache", "Debug UBC interactions" },
229 { "dirop", "Debug directory-operation accounting" },
230 { "malloc", "Debug private malloc accounting" },
231 };
232 #endif /* DEBUG */
233 struct shortlong stat_names[] = { /* Must match lfs.h! */
234 { "segsused", "Number of new segments allocated" },
235 { "psegwrites", "Number of partial-segment writes" },
236 { "psyncwrites", "Number of synchronous partial-segment"
237 " writes" },
238 { "pcleanwrites", "Number of partial-segment writes by the"
239 " cleaner" },
240 { "blocktot", "Number of blocks written" },
241 { "cleanblocks", "Number of blocks written by the cleaner" },
242 { "ncheckpoints", "Number of checkpoints made" },
243 { "nwrites", "Number of whole writes" },
244 { "nsync_writes", "Number of synchronous writes" },
245 { "wait_exceeded", "Number of times writer waited for"
246 " cleaner" },
247 { "write_exceeded", "Number of times writer invoked flush" },
248 { "flush_invoked", "Number of times flush was invoked" },
249 { "vflush_invoked", "Number of time vflush was called" },
250 { "clean_inlocked", "Number of vnodes skipped for being dead" },
251 { "clean_vnlocked", "Number of vnodes skipped for vget failure" },
252 { "segs_reclaimed", "Number of segments reclaimed" },
253 };
254
255 sysctl_createv(clog, 0, NULL, NULL,
256 CTLFLAG_PERMANENT,
257 CTLTYPE_NODE, "lfs",
258 SYSCTL_DESCR("Log-structured file system"),
259 NULL, 0, NULL, 0,
260 CTL_VFS, 5, CTL_EOL);
261 /*
262 * XXX the "5" above could be dynamic, thereby eliminating one
263 * more instance of the "number to vfs" mapping problem, but
264 * "5" is the order as taken from sys/mount.h
265 */
266
267 sysctl_createv(clog, 0, NULL, NULL,
268 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
269 CTLTYPE_INT, "flushindir", NULL,
270 NULL, 0, &lfs_writeindir, 0,
271 CTL_VFS, 5, LFS_WRITEINDIR, CTL_EOL);
272 sysctl_createv(clog, 0, NULL, NULL,
273 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
274 CTLTYPE_INT, "clean_vnhead", NULL,
275 NULL, 0, &lfs_clean_vnhead, 0,
276 CTL_VFS, 5, LFS_CLEAN_VNHEAD, CTL_EOL);
277 sysctl_createv(clog, 0, NULL, NULL,
278 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
279 CTLTYPE_INT, "dostats",
280 SYSCTL_DESCR("Maintain statistics on LFS operations"),
281 sysctl_lfs_dostats, 0, &lfs_dostats, 0,
282 CTL_VFS, 5, LFS_DOSTATS, CTL_EOL);
283 sysctl_createv(clog, 0, NULL, NULL,
284 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
285 CTLTYPE_INT, "pagetrip",
286 SYSCTL_DESCR("How many dirty pages in fs triggers"
287 " a flush"),
288 NULL, 0, &lfs_fs_pagetrip, 0,
289 CTL_VFS, 5, LFS_FS_PAGETRIP, CTL_EOL);
290 sysctl_createv(clog, 0, NULL, NULL,
291 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
292 CTLTYPE_INT, "ignore_lazy_sync",
293 SYSCTL_DESCR("Lazy Sync is ignored entirely"),
294 NULL, 0, &lfs_ignore_lazy_sync, 0,
295 CTL_VFS, 5, LFS_IGNORE_LAZY_SYNC, CTL_EOL);
296 sysctl_createv(clog, 0, NULL, NULL,
297 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
298 CTLTYPE_INT, "rfw",
299 SYSCTL_DESCR("Use in-kernel roll-forward on mount"),
300 NULL, 0, &lfs_do_rfw, 0,
301 CTL_VFS, 5, LFS_DO_RFW, CTL_EOL);
302 sysctl_createv(clog, 0, NULL, NULL,
303 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
304 CTLTYPE_INT, "rfw_limit",
305 SYSCTL_DESCR("Maximum number of partial segments"
306 " to roll forward"),
307 NULL, 0, &lfs_rfw_max_psegs, 0,
308 CTL_VFS, 5, LFS_RFW_LIMIT, CTL_EOL);
309
310 sysctl_createv(clog, 0, NULL, NULL,
311 CTLFLAG_PERMANENT,
312 CTLTYPE_NODE, "stats",
313 SYSCTL_DESCR("Statistics"),
314 NULL, 0, NULL, 0,
315 CTL_VFS, 5, LFS_STATS, CTL_EOL);
316 for (i = 0; i < sizeof(struct lfs_stats) / sizeof(u_int); i++) {
317 sysctl_createv(clog, 0, NULL, NULL,
318 CTLFLAG_PERMANENT|CTLFLAG_READONLY,
319 CTLTYPE_INT, stat_names[i].sname,
320 SYSCTL_DESCR(stat_names[i].lname),
321 NULL, 0, &(((u_int *)&lfs_stats.segsused)[i]),
322 0, CTL_VFS, 5, LFS_STATS, i, CTL_EOL);
323 }
324
325 #ifdef DEBUG
326 sysctl_createv(clog, 0, NULL, NULL,
327 CTLFLAG_PERMANENT,
328 CTLTYPE_NODE, "debug",
329 SYSCTL_DESCR("Debug options"),
330 NULL, 0, NULL, 0,
331 CTL_VFS, 5, LFS_DEBUG, CTL_EOL);
332 sysctl_createv(clog, 0, NULL, NULL,
333 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
334 CTLTYPE_INT, "freelist",
335 SYSCTL_DESCR("Track consistency of inode freelist"),
336 NULL, 0, &lfs_do_check_freelist, 0,
337 CTL_VFS, 5, LFS_DEBUG, LFS_DEBUG_FREELIST, CTL_EOL);
338 sysctl_createv(clog, 0, NULL, NULL,
339 CTLFLAG_PERMANENT,
340 CTLTYPE_NODE, "log",
341 SYSCTL_DESCR("Verbose logging"),
342 NULL, 0, NULL, 0,
343 CTL_VFS, 5, LFS_DEBUG, LFS_DEBUG_LOG, CTL_EOL);
344 for (i = 0; i < DLOG_MAX; i++) {
345 sysctl_createv(clog, 0, NULL, NULL,
346 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
347 CTLTYPE_INT, dlog_names[i].sname,
348 SYSCTL_DESCR(dlog_names[i].lname),
349 NULL, 0, &(lfs_debug_log_subsys[i]), 0,
350 CTL_VFS, 5, LFS_DEBUG, LFS_DEBUG_LOG, i, CTL_EOL);
351 }
352 #endif
353 }
354
355 /* old cleaner syscall interface. see VOP_FCNTL() */
356 static const struct syscall_package lfs_syscalls[] = {
357 { SYS_lfs_bmapv, 0, (sy_call_t *)sys_lfs_bmapv },
358 { SYS_lfs_markv, 0, (sy_call_t *)sys_lfs_markv },
359 { SYS___lfs_segwait50, 0, (sy_call_t *)sys___lfs_segwait50 },
360 { SYS_lfs_segclean, 0, (sy_call_t *)sys_lfs_segclean },
361 { 0, 0, NULL },
362 };
363
364 static int
365 lfs_modcmd(modcmd_t cmd, void *arg)
366 {
367 int error;
368
369 switch (cmd) {
370 case MODULE_CMD_INIT:
371 error = syscall_establish(NULL, lfs_syscalls);
372 if (error)
373 return error;
374 error = vfs_attach(&lfs_vfsops);
375 if (error != 0) {
376 syscall_disestablish(NULL, lfs_syscalls);
377 break;
378 }
379 cv_init(&lfs_allclean_wakeup, "segment");
380 break;
381 case MODULE_CMD_FINI:
382 error = vfs_detach(&lfs_vfsops);
383 if (error != 0)
384 break;
385 syscall_disestablish(NULL, lfs_syscalls);
386 cv_destroy(&lfs_allclean_wakeup);
387 break;
388 default:
389 error = ENOTTY;
390 break;
391 }
392
393 return (error);
394 }
395
396 /*
397 * XXX Same structure as FFS inodes? Should we share a common pool?
398 */
399 struct pool lfs_inode_pool;
400 struct pool lfs_dinode_pool;
401 struct pool lfs_inoext_pool;
402 struct pool lfs_lbnentry_pool;
403
404 /*
405 * The writer daemon. UVM keeps track of how many dirty pages we are holding
406 * in lfs_subsys_pages; the daemon flushes the filesystem when this value
407 * crosses the (user-defined) threshold LFS_MAX_PAGES.
408 */
409 static void
410 lfs_writerd(void *arg)
411 {
412 mount_iterator_t *iter;
413 struct mount *mp;
414 struct lfs *fs;
415 struct vfsops *vfs = NULL;
416 int fsflags;
417 int lfsc;
418 int wrote_something = 0;
419
420 mutex_enter(&lfs_lock);
421 KASSERTMSG(lfs_writer_daemon == NULL, "more than one LFS writer daemon");
422 lfs_writer_daemon = curlwp;
423 mutex_exit(&lfs_lock);
424
425 /* Take an extra reference to the LFS vfsops. */
426 vfs = vfs_getopsbyname(MOUNT_LFS);
427
428 mutex_enter(&lfs_lock);
429 for (;;) {
430 KASSERT(mutex_owned(&lfs_lock));
431 if (wrote_something == 0)
432 cv_timedwait(&lfs_writerd_cv, &lfs_lock, hz/10 + 1);
433 KASSERT(mutex_owned(&lfs_lock));
434 wrote_something = 0;
435
436 /*
437 * If global state wants a flush, flush everything.
438 */
439 if (lfs_do_flush || locked_queue_count + locked_queue_rcount > LFS_MAX_BUFS ||
440 locked_queue_bytes + locked_queue_rbytes > LFS_MAX_BYTES ||
441 lfs_subsys_pages > LFS_MAX_PAGES) {
442
443 if (lfs_do_flush) {
444 DLOG((DLOG_FLUSH, "lfs_writerd: lfs_do_flush\n"));
445 }
446 if (locked_queue_count + locked_queue_rcount > LFS_MAX_BUFS) {
447 DLOG((DLOG_FLUSH, "lfs_writerd: lqc+lqrc = %d, max %d\n",
448 locked_queue_count, LFS_MAX_BUFS));
449 }
450 if (locked_queue_bytes + locked_queue_rbytes > LFS_MAX_BYTES) {
451 DLOG((DLOG_FLUSH, "lfs_writerd: lqb + lqrb = %ld, max %ld\n",
452 locked_queue_bytes, LFS_MAX_BYTES));
453 }
454 if (lfs_subsys_pages > LFS_MAX_PAGES) {
455 DLOG((DLOG_FLUSH, "lfs_writerd: lssp = %d, max %d\n",
456 lfs_subsys_pages, LFS_MAX_PAGES));
457 }
458
459 lfs_flush(NULL, SEGM_WRITERD, 0);
460 lfs_do_flush = 0;
461 KASSERT(mutex_owned(&lfs_lock));
462 continue;
463 }
464 KASSERT(mutex_owned(&lfs_lock));
465 mutex_exit(&lfs_lock);
466
467 /*
468 * Look through the list of LFSs to see if any of them
469 * have requested pageouts.
470 */
471 mountlist_iterator_init(&iter);
472 lfsc = 0;
473 while ((mp = mountlist_iterator_next(iter)) != NULL) {
474 KASSERT(!mutex_owned(&lfs_lock));
475 if (strncmp(mp->mnt_stat.f_fstypename, MOUNT_LFS,
476 sizeof(mp->mnt_stat.f_fstypename)) == 0) {
477 ++lfsc;
478 fs = VFSTOULFS(mp)->um_lfs;
479 daddr_t ooffset = 0;
480 fsflags = SEGM_SINGLE;
481
482 mutex_enter(&lfs_lock);
483 ooffset = lfs_sb_getoffset(fs);
484
485 if (lfs_sb_getnextseg(fs) < lfs_sb_getcurseg(fs) && fs->lfs_nowrap) {
486 /* Don't try to write if we're suspended */
487 mutex_exit(&lfs_lock);
488 continue;
489 }
490 if (LFS_STARVED_FOR_SEGS(fs)) {
491 mutex_exit(&lfs_lock);
492
493 DLOG((DLOG_FLUSH, "lfs_writerd: need cleaning before writing possible\n"));
494 lfs_wakeup_cleaner(fs);
495 continue;
496 }
497
498 if ((fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
499 lfs_dirvcount > LFS_MAX_DIROP) &&
500 fs->lfs_dirops == 0) {
501 fsflags &= ~SEGM_SINGLE;
502 fsflags |= SEGM_CKP;
503 DLOG((DLOG_FLUSH, "lfs_writerd: checkpoint\n"));
504 lfs_flush_fs(fs, fsflags);
505 } else if (fs->lfs_pdflush) {
506 DLOG((DLOG_FLUSH, "lfs_writerd: pdflush set\n"));
507 lfs_flush_fs(fs, fsflags);
508 } else if (!TAILQ_EMPTY(&fs->lfs_pchainhd)) {
509 DLOG((DLOG_FLUSH, "lfs_writerd: pchain non-empty\n"));
510 mutex_exit(&lfs_lock);
511 lfs_writer_enter(fs, "wrdirop");
512 lfs_flush_pchain(fs);
513 lfs_writer_leave(fs);
514 mutex_enter(&lfs_lock);
515 }
516 if (lfs_sb_getoffset(fs) != ooffset)
517 ++wrote_something;
518 mutex_exit(&lfs_lock);
519 }
520 KASSERT(!mutex_owned(&lfs_lock));
521 }
522 if (lfsc == 0) {
523 mutex_enter(&lfs_lock);
524 lfs_writer_daemon = NULL;
525 mutex_exit(&lfs_lock);
526 mountlist_iterator_destroy(iter);
527 break;
528 }
529 mountlist_iterator_destroy(iter);
530
531 mutex_enter(&lfs_lock);
532 }
533 KASSERT(!mutex_owned(&lfs_lock));
534
535 /* Give up our extra reference so the module can be unloaded. */
536 mutex_enter(&vfs_list_lock);
537 if (vfs != NULL)
538 vfs->vfs_refcount--;
539 mutex_exit(&vfs_list_lock);
540
541 /* Done! */
542 kthread_exit(0);
543 }
544
545 /*
546 * Initialize the filesystem, most work done by ulfs_init.
547 */
548 void
549 lfs_init(void)
550 {
551
552 /*
553 * XXX: should we use separate pools for 32-bit and 64-bit
554 * dinodes?
555 */
556 malloc_type_attach(M_SEGMENT);
557 pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
558 "lfsinopl", &pool_allocator_nointr, IPL_NONE);
559 pool_init(&lfs_dinode_pool, sizeof(union lfs_dinode), 0, 0, 0,
560 "lfsdinopl", &pool_allocator_nointr, IPL_NONE);
561 pool_init(&lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0,
562 "lfsinoextpl", &pool_allocator_nointr, IPL_NONE);
563 pool_init(&lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0,
564 "lfslbnpool", &pool_allocator_nointr, IPL_NONE);
565 ulfs_init();
566
567 #ifdef DEBUG
568 memset(lfs_log, 0, sizeof(lfs_log));
569 #endif
570 mutex_init(&lfs_lock, MUTEX_DEFAULT, IPL_NONE);
571 cv_init(&lfs_writerd_cv, "lfswrite");
572 cv_init(&locked_queue_cv, "lfsbuf");
573 cv_init(&lfs_writing_cv, "lfsflush");
574 workqueue_create(&lfs_cluster_wq, "lfscwq", lfs_cluster_work, NULL,
575 PRI_BIO, IPL_BIO, WQ_MPSAFE);
576 workqueue_create(&lfs_super_wq, "lfsswq", lfs_super_work, NULL,
577 PRI_BIO, IPL_BIO, WQ_MPSAFE);
578 }
579
580 void
581 lfs_reinit(void)
582 {
583 ulfs_reinit();
584 }
585
586 void
587 lfs_done(void)
588 {
589 ulfs_done();
590 mutex_destroy(&lfs_lock);
591 cv_destroy(&lfs_writerd_cv);
592 cv_destroy(&locked_queue_cv);
593 cv_destroy(&lfs_writing_cv);
594 workqueue_destroy(lfs_cluster_wq);
595 workqueue_destroy(lfs_super_wq);
596 pool_destroy(&lfs_inode_pool);
597 pool_destroy(&lfs_dinode_pool);
598 pool_destroy(&lfs_inoext_pool);
599 pool_destroy(&lfs_lbnentry_pool);
600 malloc_type_detach(M_SEGMENT);
601 }
602
603 /*
604 * Called by main() when ulfs is going to be mounted as root.
605 */
606 int
607 lfs_mountroot(void)
608 {
609 extern struct vnode *rootvp;
610 struct lfs *fs = NULL; /* LFS */
611 struct mount *mp;
612 struct lwp *l = curlwp;
613 struct ulfsmount *ump;
614 int error;
615
616 if (device_class(root_device) != DV_DISK)
617 return (ENODEV);
618
619 if (rootdev == NODEV)
620 return (ENODEV);
621 if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
622 vrele(rootvp);
623 return (error);
624 }
625 if ((error = lfs_mountfs(rootvp, mp, l))) {
626 vfs_unbusy(mp);
627 vfs_rele(mp);
628 return (error);
629 }
630 mountlist_append(mp);
631 ump = VFSTOULFS(mp);
632 fs = ump->um_lfs;
633 lfs_sb_setfsmnt(fs, mp->mnt_stat.f_mntonname);
634 (void)lfs_statvfs(mp, &mp->mnt_stat);
635 vfs_unbusy(mp);
636 setrootfstime((time_t)lfs_sb_gettstamp(VFSTOULFS(mp)->um_lfs));
637 return (0);
638 }
639
640 /*
641 * VFS Operations.
642 *
643 * mount system call
644 */
645 int
646 lfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
647 {
648 struct lwp *l = curlwp;
649 struct vnode *devvp;
650 struct ulfs_args *args = data;
651 struct ulfsmount *ump = NULL;
652 struct lfs *fs = NULL; /* LFS */
653 int error = 0, update;
654 mode_t accessmode;
655
656 if (args == NULL)
657 return EINVAL;
658 if (*data_len < sizeof *args)
659 return EINVAL;
660
661 if (mp->mnt_flag & MNT_GETARGS) {
662 ump = VFSTOULFS(mp);
663 if (ump == NULL)
664 return EIO;
665 args->fspec = NULL;
666 *data_len = sizeof *args;
667 return 0;
668 }
669
670 update = mp->mnt_flag & MNT_UPDATE;
671
672 /* Check arguments */
673 if (args->fspec != NULL) {
674 /*
675 * Look up the name and verify that it's sane.
676 */
677 error = namei_simple_user(args->fspec,
678 NSM_FOLLOW_NOEMULROOT, &devvp);
679 if (error != 0)
680 return (error);
681
682 if (!update) {
683 /*
684 * Be sure this is a valid block device
685 */
686 if (devvp->v_type != VBLK)
687 error = ENOTBLK;
688 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
689 error = ENXIO;
690 } else {
691 /*
692 * Be sure we're still naming the same device
693 * used for our initial mount
694 *
695 * XXX dholland 20151010: if namei gives us a
696 * different vnode for the same device,
697 * wouldn't it be better to use it going
698 * forward rather than ignore it in favor of
699 * the old one?
700 */
701 ump = VFSTOULFS(mp);
702 fs = ump->um_lfs;
703 if (devvp != fs->lfs_devvp) {
704 if (devvp->v_rdev != fs->lfs_devvp->v_rdev)
705 error = EINVAL;
706 else {
707 vrele(devvp);
708 devvp = fs->lfs_devvp;
709 vref(devvp);
710 }
711 }
712 }
713 } else {
714 if (!update) {
715 /* New mounts must have a filename for the device */
716 return (EINVAL);
717 } else {
718 /* Use the extant mount */
719 ump = VFSTOULFS(mp);
720 fs = ump->um_lfs;
721 devvp = fs->lfs_devvp;
722 vref(devvp);
723 }
724 }
725
726
727 /*
728 * If mount by non-root, then verify that user has necessary
729 * permissions on the device.
730 */
731 if (error == 0) {
732 accessmode = VREAD;
733 if (update ?
734 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
735 (mp->mnt_flag & MNT_RDONLY) == 0)
736 accessmode |= VWRITE;
737 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
738 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
739 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
740 KAUTH_ARG(accessmode));
741 VOP_UNLOCK(devvp);
742 }
743
744 if (error) {
745 vrele(devvp);
746 return (error);
747 }
748
749 if (!update) {
750 int flags;
751
752 if (mp->mnt_flag & MNT_RDONLY)
753 flags = FREAD;
754 else
755 flags = FREAD|FWRITE;
756 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
757 error = VOP_OPEN(devvp, flags, FSCRED);
758 VOP_UNLOCK(devvp);
759 if (error)
760 goto fail;
761 error = lfs_mountfs(devvp, mp, l); /* LFS */
762 if (error) {
763 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
764 (void)VOP_CLOSE(devvp, flags, NOCRED);
765 VOP_UNLOCK(devvp);
766 goto fail;
767 }
768
769 ump = VFSTOULFS(mp);
770 fs = ump->um_lfs;
771 } else {
772 /*
773 * Update the mount.
774 */
775
776 /*
777 * The initial mount got a reference on this
778 * device, so drop the one obtained via
779 * namei(), above.
780 */
781 vrele(devvp);
782
783 ump = VFSTOULFS(mp);
784 fs = ump->um_lfs;
785
786 if (!fs->lfs_ronly && (mp->mnt_iflag & IMNT_WANTRDONLY)) {
787 /*
788 * Changing from read/write to read-only.
789 */
790 int flags = WRITECLOSE;
791 if (mp->mnt_flag & MNT_FORCE)
792 flags |= FORCECLOSE;
793 error = lfs_flushfiles(mp, flags);
794 if (error)
795 return error;
796 fs->lfs_ronly = 1;
797 } else if (fs->lfs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
798 /*
799 * Changing from read-only to read/write.
800 * Note in the superblocks that we're writing.
801 */
802
803 /* XXX: quotas should have been on even if readonly */
804 if (fs->lfs_use_quota2) {
805 #ifdef LFS_QUOTA2
806 error = lfs_quota2_mount(mp);
807 #else
808 uprintf("%s: no kernel support for this "
809 "filesystem's quotas\n",
810 mp->mnt_stat.f_mntonname);
811 if (mp->mnt_flag & MNT_FORCE) {
812 uprintf("%s: mounting anyway; "
813 "fsck afterwards\n",
814 mp->mnt_stat.f_mntonname);
815 } else {
816 error = EINVAL;
817 }
818 #endif
819 if (error) {
820 return error;
821 }
822 }
823
824 fs->lfs_ronly = 0;
825 if (lfs_sb_getpflags(fs) & LFS_PF_CLEAN) {
826 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) & ~LFS_PF_CLEAN);
827 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0));
828 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1));
829 }
830 }
831
832 if (args->fspec == NULL)
833 return 0;
834 }
835
836 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
837 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
838 if (error == 0)
839 lfs_sb_setfsmnt(fs, mp->mnt_stat.f_mntonname);
840 return error;
841
842 fail:
843 vrele(devvp);
844 return (error);
845 }
846
847 /*
848 * Helper for mountfs. Note that the fs pointer may be a dummy one
849 * pointing into a superblock buffer. (Which is gross; see below.)
850 */
851 static int
852 lfs_checkmagic(struct lfs *fs)
853 {
854 switch (fs->lfs_dlfs_u.u_32.dlfs_magic) {
855 case LFS_MAGIC:
856 fs->lfs_is64 = false;
857 fs->lfs_dobyteswap = false;
858 break;
859 case LFS64_MAGIC:
860 fs->lfs_is64 = true;
861 fs->lfs_dobyteswap = false;
862 break;
863 #ifdef LFS_EI
864 case LFS_MAGIC_SWAPPED:
865 fs->lfs_is64 = false;
866 fs->lfs_dobyteswap = true;
867 break;
868 case LFS64_MAGIC_SWAPPED:
869 fs->lfs_is64 = true;
870 fs->lfs_dobyteswap = true;
871 break;
872 #endif
873 default:
874 /* XXX needs translation */
875 return EINVAL;
876 }
877 return 0;
878 }
879
880 /*
881 * Common code for mount and mountroot
882 * LFS specific
883 */
884 int
885 lfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
886 {
887 struct lfs *primarysb, *altsb, *thesb;
888 struct buf *primarybuf, *altbuf;
889 struct lfs *fs;
890 struct ulfsmount *ump;
891 struct vnode *vp;
892 dev_t dev;
893 int error, i, ronly, fsbsize;
894 kauth_cred_t cred;
895 CLEANERINFO *cip;
896 SEGUSE *sup;
897 daddr_t sb_addr;
898 ino_t *orphan;
899 size_t norphan;
900
901 cred = l ? l->l_cred : NOCRED;
902
903 /* The superblock is supposed to be 512 bytes. */
904 __CTASSERT(sizeof(struct dlfs) == DEV_BSIZE);
905
906 /*
907 * Flush out any old buffers remaining from a previous use.
908 */
909 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
910 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
911 VOP_UNLOCK(devvp);
912 if (error)
913 return (error);
914
915 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
916
917 /* Don't free random space on error. */
918 primarybuf = NULL;
919 altbuf = NULL;
920 ump = NULL;
921
922 sb_addr = LFS_LABELPAD / DEV_BSIZE;
923 while (1) {
924 /*
925 * Read in the superblock.
926 *
927 * Note that because LFS_SBPAD is substantially larger
928 * (8K) than the actual on-disk superblock (512 bytes)
929 * the buffer contains enough space to be used as a
930 * whole struct lfs (in-memory superblock) - we do this
931 * only so we can set and use the is64 and dobyteswap
932 * members. XXX this is gross and the logic here should
933 * be reworked.
934 */
935 error = bread(devvp, sb_addr, LFS_SBPAD, 0, &primarybuf);
936 if (error)
937 goto out;
938 primarysb = (struct lfs *)primarybuf->b_data;
939
940 /* Check the basics. */
941 error = lfs_checkmagic(primarysb);
942 if (error) {
943 DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock wrong magic\n"));
944 goto out;
945 }
946 if (lfs_sb_getbsize(primarysb) > MAXBSIZE ||
947 lfs_sb_getversion(primarysb) > LFS_VERSION ||
948 lfs_sb_getbsize(primarysb) < sizeof(struct dlfs)) {
949 DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock sanity failed\n"));
950 /* XXX needs translation */
951 error = EINVAL;
952 goto out;
953 }
954 if (lfs_sb_getinodefmt(primarysb) > LFS_MAXINODEFMT) {
955 DLOG((DLOG_MOUNT, "lfs_mountfs: unknown inode format %d\n",
956 lfs_sb_getinodefmt(primarysb)));
957 error = EINVAL;
958 goto out;
959 }
960
961 if (lfs_sb_getversion(primarysb) == 1)
962 fsbsize = DEV_BSIZE;
963 else {
964 fsbsize = 1 << lfs_sb_getffshift(primarysb);
965 /*
966 * Could be, if the frag size is large enough, that we
967 * don't have the "real" primary superblock. If that's
968 * the case, get the real one, and try again.
969 */
970 if (sb_addr != (lfs_sb_getsboff(primarysb, 0) << (lfs_sb_getffshift(primarysb) - DEV_BSHIFT))) {
971 DLOG((DLOG_MOUNT, "lfs_mountfs: sb daddr"
972 " 0x%llx is not right, trying 0x%llx\n",
973 (long long)sb_addr,
974 (long long)(lfs_sb_getsboff(primarysb, 0) << (lfs_sb_getffshift(primarysb) - DEV_BSHIFT))));
975 sb_addr = lfs_sb_getsboff(primarysb, 0) << (lfs_sb_getffshift(primarysb) - DEV_BSHIFT);
976 brelse(primarybuf, BC_INVAL);
977 continue;
978 }
979 }
980 break;
981 }
982
983 /*
984 * Check the second superblock to see which is newer; then mount
985 * using the older of the two. This is necessary to ensure that
986 * the filesystem is valid if it was not unmounted cleanly.
987 */
988
989 if (lfs_sb_getsboff(primarysb, 1) &&
990 lfs_sb_getsboff(primarysb, 1) - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize)
991 {
992 error = bread(devvp, lfs_sb_getsboff(primarysb, 1) * (fsbsize / DEV_BSIZE),
993 LFS_SBPAD, 0, &altbuf);
994 if (error)
995 goto out;
996 altsb = (struct lfs *)altbuf->b_data;
997
998 /*
999 * Note: this used to do the sanity check only if the
1000 * timestamp/serial comparison required use of altsb;
1001 * this way is less tolerant, but if altsb is corrupted
1002 * enough that the magic number, version, and blocksize
1003 * are bogus, why would the timestamp or serial fields
1004 * mean anything either? If this kind of thing happens,
1005 * you need to fsck anyway.
1006 */
1007
1008 error = lfs_checkmagic(altsb);
1009 if (error)
1010 goto out;
1011
1012 /* Check the basics. */
1013 if (lfs_sb_getbsize(altsb) > MAXBSIZE ||
1014 lfs_sb_getversion(altsb) > LFS_VERSION ||
1015 lfs_sb_getbsize(altsb) < sizeof(struct dlfs)) {
1016 DLOG((DLOG_MOUNT, "lfs_mountfs: alt superblock"
1017 " sanity failed\n"));
1018 error = EINVAL; /* XXX needs translation */
1019 goto out;
1020 }
1021
1022 if (lfs_sb_getversion(primarysb) == 1) {
1023 /* 1s resolution comparison */
1024 if (lfs_sb_gettstamp(altsb) < lfs_sb_gettstamp(primarysb))
1025 thesb = altsb;
1026 else
1027 thesb = primarysb;
1028 } else {
1029 /* monotonic infinite-resolution comparison */
1030 if (lfs_sb_getserial(altsb) < lfs_sb_getserial(primarysb))
1031 thesb = altsb;
1032 else
1033 thesb = primarysb;
1034 }
1035 } else {
1036 DLOG((DLOG_MOUNT, "lfs_mountfs: invalid alt superblock location"
1037 " daddr=0x%x\n", lfs_sb_getsboff(primarysb, 1)));
1038 error = EINVAL;
1039 goto out;
1040 }
1041
1042 /*
1043 * Allocate the mount structure, copy the superblock into it.
1044 * Note that the 32-bit and 64-bit superblocks are the same size.
1045 */
1046 fs = kmem_zalloc(sizeof(struct lfs), KM_SLEEP);
1047 memcpy(&fs->lfs_dlfs_u.u_32, &thesb->lfs_dlfs_u.u_32,
1048 sizeof(struct dlfs));
1049 fs->lfs_is64 = thesb->lfs_is64;
1050 fs->lfs_dobyteswap = thesb->lfs_dobyteswap;
1051 fs->lfs_hasolddirfmt = false; /* set for real below */
1052
1053 /* Compatibility */
1054 if (lfs_sb_getversion(fs) < 2) {
1055 lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE);
1056 lfs_sb_setibsize(fs, lfs_sb_getbsize(fs));
1057 lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0));
1058 lfs_sb_settstamp(fs, lfs_sb_getotstamp(fs));
1059 lfs_sb_setfsbtodb(fs, 0);
1060 }
1061 if (lfs_sb_getresvseg(fs) == 0)
1062 lfs_sb_setresvseg(fs, MIN(lfs_sb_getminfreeseg(fs) - 1, \
1063 MAX(MIN_RESV_SEGS, lfs_sb_getminfreeseg(fs) / 2 + 1)));
1064
1065 /*
1066 * If we aren't going to be able to write meaningfully to this
1067 * filesystem, and were not mounted readonly, bomb out now.
1068 */
1069 if (lfs_fsbtob(fs, LFS_NRESERVE(fs)) > LFS_MAX_BYTES && !ronly) {
1070 DLOG((DLOG_MOUNT, "lfs_mount: to mount this filesystem read/write,"
1071 " we need BUFPAGES >= %lld\n",
1072 (long long)((bufmem_hiwater / bufmem_lowater) *
1073 LFS_INVERSE_MAX_BYTES(
1074 lfs_fsbtob(fs, LFS_NRESERVE(fs))) >> PAGE_SHIFT)));
1075 kmem_free(fs, sizeof(struct lfs));
1076 error = EFBIG; /* XXX needs translation */
1077 goto out;
1078 }
1079
1080 /* Before rolling forward, lock so vget will sleep for other procs */
1081 if (l != NULL) {
1082 fs->lfs_flags = LFS_NOTYET;
1083 fs->lfs_rfpid = l->l_proc->p_pid;
1084 }
1085
1086 ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
1087 ump->um_lfs = fs;
1088 ump->um_fstype = fs->lfs_is64 ? ULFS2 : ULFS1;
1089 /* ump->um_cleaner_thread = NULL; */
1090 brelse(primarybuf, BC_INVAL);
1091 brelse(altbuf, BC_INVAL);
1092 primarybuf = NULL;
1093 altbuf = NULL;
1094
1095
1096 /* Set up the I/O information */
1097 fs->lfs_devbsize = DEV_BSIZE;
1098 fs->lfs_iocount = 0;
1099 fs->lfs_diropwait = 0;
1100 fs->lfs_activesb = 0;
1101 lfs_sb_setuinodes(fs, 0);
1102 fs->lfs_ravail = 0;
1103 fs->lfs_favail = 0;
1104 fs->lfs_sbactive = 0;
1105
1106 /* Set up the ifile and lock aflags */
1107 fs->lfs_doifile = 0;
1108 fs->lfs_writer = 0;
1109 fs->lfs_dirops = 0;
1110 fs->lfs_nadirop = 0;
1111 fs->lfs_seglock = 0;
1112 fs->lfs_pdflush = 0;
1113 fs->lfs_sleepers = 0;
1114 fs->lfs_pages = 0;
1115 rw_init(&fs->lfs_fraglock);
1116 rw_init(&fs->lfs_iflock);
1117 cv_init(&fs->lfs_sleeperscv, "lfs_slp");
1118 cv_init(&fs->lfs_diropscv, "lfs_dirop");
1119 cv_init(&fs->lfs_stopcv, "lfsstop");
1120 cv_init(&fs->lfs_nextsegsleep, "segment");
1121
1122 /* Set the file system readonly/modify bits. */
1123 fs->lfs_ronly = ronly;
1124 if (ronly == 0)
1125 fs->lfs_fmod = 1;
1126
1127 /* Device we're using */
1128 dev = devvp->v_rdev;
1129 fs->lfs_dev = dev;
1130 fs->lfs_devvp = devvp;
1131
1132 /* ulfs-level information */
1133 fs->um_flags = 0;
1134 fs->um_bptrtodb = lfs_sb_getffshift(fs) - DEV_BSHIFT;
1135 fs->um_seqinc = lfs_sb_getfrag(fs);
1136 fs->um_nindir = lfs_sb_getnindir(fs);
1137 fs->um_lognindir = ffs(lfs_sb_getnindir(fs)) - 1;
1138 fs->um_maxsymlinklen = lfs_sb_getmaxsymlinklen(fs);
1139 fs->um_dirblksiz = LFS_DIRBLKSIZ;
1140 fs->um_maxfilesize = lfs_sb_getmaxfilesize(fs);
1141
1142 /* quota stuff */
1143 /* XXX: these need to come from the on-disk superblock to be used */
1144 fs->lfs_use_quota2 = 0;
1145 fs->lfs_quota_magic = 0;
1146 fs->lfs_quota_flags = 0;
1147 fs->lfs_quotaino[0] = 0;
1148 fs->lfs_quotaino[1] = 0;
1149
1150 /* Initialize the mount structure. */
1151 mp->mnt_data = ump;
1152 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
1153 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_LFS);
1154 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
1155 mp->mnt_stat.f_namemax = LFS_MAXNAMLEN;
1156 mp->mnt_stat.f_iosize = lfs_sb_getbsize(fs);
1157 mp->mnt_flag |= MNT_LOCAL;
1158 mp->mnt_iflag |= IMNT_SHRLOOKUP;
1159 mp->mnt_fs_bshift = lfs_sb_getbshift(fs);
1160 mp->mnt_iflag |= IMNT_CAN_RWTORO;
1161 if (fs->um_maxsymlinklen > 0)
1162 mp->mnt_iflag |= IMNT_DTYPE;
1163 else
1164 fs->lfs_hasolddirfmt = true;
1165
1166 ump->um_mountp = mp;
1167 for (i = 0; i < ULFS_MAXQUOTAS; i++)
1168 ump->um_quotas[i] = NULLVP;
1169 spec_node_setmountedfs(devvp, mp);
1170
1171 /* Set up reserved memory for pageout */
1172 lfs_setup_resblks(fs);
1173 /* Set up vdirop tailq */
1174 TAILQ_INIT(&fs->lfs_dchainhd);
1175 /* and paging tailq */
1176 TAILQ_INIT(&fs->lfs_pchainhd);
1177 /* and delayed segment accounting for truncation list */
1178 LIST_INIT(&fs->lfs_segdhd);
1179
1180 /*
1181 * We use the ifile vnode for almost every operation. Instead of
1182 * retrieving it from the hash table each time we retrieve it here,
1183 * artificially increment the reference count and keep a pointer
1184 * to it in the incore copy of the superblock.
1185 */
1186 if ((error = VFS_VGET(mp, LFS_IFILE_INUM, LK_EXCLUSIVE, &vp)) != 0) {
1187 DLOG((DLOG_MOUNT, "lfs_mountfs: ifile vget failed, error=%d\n", error));
1188 goto out;
1189 }
1190 fs->lfs_ivnode = vp;
1191 vref(vp);
1192
1193 /* Set up inode bitmap, order free list, and gather orphans. */
1194 lfs_order_freelist(fs, &orphan, &norphan);
1195
1196 /* Set up segment usage flags for the autocleaner. */
1197 fs->lfs_nactive = 0;
1198 fs->lfs_suflags = malloc(2 * sizeof(u_int32_t *),
1199 M_SEGMENT, M_WAITOK);
1200 fs->lfs_suflags[0] = malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t),
1201 M_SEGMENT, M_WAITOK);
1202 fs->lfs_suflags[1] = malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t),
1203 M_SEGMENT, M_WAITOK);
1204 memset(fs->lfs_suflags[1], 0, lfs_sb_getnseg(fs) * sizeof(u_int32_t));
1205 for (i = 0; i < lfs_sb_getnseg(fs); i++) {
1206 int changed;
1207 struct buf *bp;
1208
1209 LFS_SEGENTRY(sup, fs, i, bp);
1210 changed = 0;
1211 if (!ronly) {
1212 if (sup->su_nbytes == 0 &&
1213 !(sup->su_flags & SEGUSE_EMPTY)) {
1214 sup->su_flags |= SEGUSE_EMPTY;
1215 ++changed;
1216 } else if (!(sup->su_nbytes == 0) &&
1217 (sup->su_flags & SEGUSE_EMPTY)) {
1218 sup->su_flags &= ~SEGUSE_EMPTY;
1219 ++changed;
1220 }
1221 if (sup->su_flags & (SEGUSE_ACTIVE|SEGUSE_INVAL)) {
1222 sup->su_flags &= ~(SEGUSE_ACTIVE|SEGUSE_INVAL);
1223 ++changed;
1224 }
1225 }
1226 fs->lfs_suflags[0][i] = sup->su_flags;
1227 if (changed)
1228 LFS_WRITESEGENTRY(sup, fs, i, bp);
1229 else
1230 brelse(bp, 0);
1231 }
1232
1233 /* Free the orphans we discovered while ordering the freelist. */
1234 lfs_free_orphans(fs, orphan, norphan);
1235
1236 if (!ronly) {
1237 /* Roll forward */
1238 lfs_roll_forward(fs, mp, l);
1239 lfs_reset_avail(fs);
1240 }
1241 fs->lfs_rfpid = 0;
1242
1243 /*
1244 * XXX: if the fs has quotas, quotas should be on even if
1245 * readonly. Otherwise you can't query the quota info!
1246 * However, that's not how the quota2 code got written and I
1247 * don't know if it'll behave itself if enabled while
1248 * readonly, so for now use the same enable logic as ffs.
1249 *
1250 * XXX: also, if you use the -f behavior allowed here (and
1251 * equivalently above for remount) it will corrupt the fs. It
1252 * ought not to allow that. It should allow mounting readonly
1253 * if there are quotas and the kernel doesn't have the quota
1254 * code, but only readonly.
1255 *
1256 * XXX: and if you use the -f behavior allowed here it will
1257 * likely crash at unmount time (or remount time) because we
1258 * think quotas are active.
1259 *
1260 * Although none of this applies until there's a way to set
1261 * lfs_use_quota2 and have quotas in the fs at all.
1262 */
1263 if (!ronly && fs->lfs_use_quota2) {
1264 #ifdef LFS_QUOTA2
1265 error = lfs_quota2_mount(mp);
1266 #else
1267 uprintf("%s: no kernel support for this filesystem's quotas\n",
1268 mp->mnt_stat.f_mntonname);
1269 if (mp->mnt_flag & MNT_FORCE) {
1270 uprintf("%s: mounting anyway; fsck afterwards\n",
1271 mp->mnt_stat.f_mntonname);
1272 } else {
1273 error = EINVAL;
1274 }
1275 #endif
1276 if (error) {
1277 /* XXX XXX must clean up the stuff immediately above */
1278 printf("lfs_mountfs: sorry, leaking some memory\n");
1279 goto out;
1280 }
1281 }
1282
1283 /* If writing, sb is not clean; record in case of immediate crash */
1284 if (!fs->lfs_ronly) {
1285 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) & ~LFS_PF_CLEAN);
1286 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0));
1287 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1));
1288 }
1289
1290 /* Allow vget now that roll-forward is complete */
1291 fs->lfs_flags &= ~(LFS_NOTYET);
1292 wakeup(&fs->lfs_flags);
1293
1294 /*
1295 * Initialize the ifile cleaner info with information from
1296 * the superblock.
1297 */
1298 {
1299 struct buf *bp;
1300
1301 LFS_CLEANERINFO(cip, fs, bp);
1302 lfs_ci_setclean(fs, cip, lfs_sb_getnclean(fs));
1303 lfs_ci_setdirty(fs, cip, lfs_sb_getnseg(fs) - lfs_sb_getnclean(fs));
1304 lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs));
1305 lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs));
1306 (void) LFS_BWRITE_LOG(bp); /* Ifile */
1307 }
1308
1309 /*
1310 * Mark the current segment as ACTIVE, since we're going to
1311 * be writing to it.
1312 */
1313 {
1314 struct buf *bp;
1315
1316 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getoffset(fs)), bp);
1317 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1318 fs->lfs_nactive++;
1319 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getoffset(fs)), bp); /* Ifile */
1320 }
1321
1322 /* Now that roll-forward is done, unlock the Ifile */
1323 vput(vp);
1324
1325 /* Start the pagedaemon-anticipating daemon */
1326 mutex_enter(&lfs_lock);
1327 if (lfs_writer_daemon == NULL &&
1328 kthread_create(PRI_BIO, 0, NULL,
1329 lfs_writerd, NULL, NULL, "lfs_writer") != 0)
1330 panic("fork lfs_writer");
1331 mutex_exit(&lfs_lock);
1332
1333 printf("WARNING: the log-structured file system is experimental\n"
1334 "WARNING: it may cause system crashes and/or corrupt data\n");
1335
1336 return (0);
1337
1338 out:
1339 if (primarybuf)
1340 brelse(primarybuf, BC_INVAL);
1341 if (altbuf)
1342 brelse(altbuf, BC_INVAL);
1343 if (ump) {
1344 kmem_free(ump->um_lfs, sizeof(struct lfs));
1345 kmem_free(ump, sizeof(*ump));
1346 mp->mnt_data = NULL;
1347 }
1348
1349 return (error);
1350 }
1351
1352 void
1353 lfs_reset_avail(struct lfs *fs)
1354 {
1355 daddr_t avail;
1356 int sn, nclean, nsb, labelcorrect, curr, mfs;
1357 SEGUSE *sup;
1358 struct buf *bp;
1359
1360 KASSERT(!fs->lfs_ronly);
1361
1362 avail = nclean = nsb = labelcorrect = 0;
1363 for (sn = 0; sn < lfs_sb_getnseg(fs); ++sn) {
1364 LFS_SEGENTRY(sup, fs, sn, bp);
1365
1366 /* Count all clean segments and the remainder of this one */
1367 if (!(sup->su_flags & SEGUSE_DIRTY)) {
1368 ++nclean;
1369 avail += lfs_segtod(fs, 1);
1370
1371 /* Correct for label and superblock, if present */
1372 if (sup->su_flags & SEGUSE_SUPERBLOCK)
1373 ++nsb;
1374 if (sn == 0 && lfs_sb_getversion(fs) > 1
1375 && lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
1376 labelcorrect = lfs_btofsb(fs, LFS_LABELPAD)
1377 - lfs_sb_gets0addr(fs);
1378 }
1379
1380 brelse(bp, 0);
1381 }
1382
1383 /* Also may be available bytes in current seg */
1384 sn = lfs_dtosn(fs, lfs_sb_getoffset(fs));
1385 curr = lfs_sntod(fs, sn + 1) - lfs_sb_getoffset(fs);
1386 /* But do not count minfreesegs */
1387 mfs = lfs_segtod(fs, (lfs_sb_getminfreeseg(fs) -
1388 (lfs_sb_getminfreeseg(fs) / 2)));
1389
1390 avail = nclean * lfs_segtod(fs, 1);
1391 avail -= nsb * lfs_btofsb(fs, LFS_SBPAD);
1392 avail -= labelcorrect;
1393 avail += curr;
1394 avail -= mfs;
1395
1396 DLOG((DLOG_AVAIL, "avail := %jd*%jd-%jd*%jd-%jd+%jd-%jd=%jd\n",
1397 (intmax_t)nclean,
1398 (intmax_t)lfs_segtod(fs, 1),
1399 (intmax_t)nsb,
1400 (intmax_t)lfs_btofsb(fs, LFS_SBPAD),
1401 (intmax_t)labelcorrect,
1402 (intmax_t)curr,
1403 (intmax_t)mfs,
1404 (intmax_t)avail));
1405
1406 lfs_sb_setavail(fs, avail);
1407 lfs_sb_setnclean(fs, nclean);
1408 }
1409
1410 /*
1411 * unmount system call
1412 */
1413 int
1414 lfs_unmount(struct mount *mp, int mntflags)
1415 {
1416 struct ulfsmount *ump;
1417 struct lfs *fs;
1418 int error, ronly;
1419
1420 ump = VFSTOULFS(mp);
1421 fs = ump->um_lfs;
1422
1423 error = lfs_flushfiles(mp, mntflags & MNT_FORCE ? FORCECLOSE : 0);
1424 if (error)
1425 return error;
1426
1427 DEBUG_CHECK_FREELIST(fs);
1428
1429 /* Finish with the Ifile, now that we're done with it */
1430 vgone(fs->lfs_ivnode);
1431
1432 ronly = !fs->lfs_ronly;
1433 if (fs->lfs_devvp->v_type != VBAD)
1434 spec_node_setmountedfs(fs->lfs_devvp, NULL);
1435 vn_lock(fs->lfs_devvp, LK_EXCLUSIVE | LK_RETRY);
1436 error = VOP_CLOSE(fs->lfs_devvp,
1437 ronly ? FREAD : FREAD|FWRITE, NOCRED);
1438 vput(fs->lfs_devvp);
1439
1440 /* Complain about page leakage */
1441 if (fs->lfs_pages > 0)
1442 printf("lfs_unmount: still claim %d pages (%d in subsystem)\n",
1443 fs->lfs_pages, lfs_subsys_pages);
1444
1445 /* Free per-mount data structures */
1446 free(fs->lfs_ino_bitmap, M_SEGMENT);
1447 free(fs->lfs_suflags[0], M_SEGMENT);
1448 free(fs->lfs_suflags[1], M_SEGMENT);
1449 free(fs->lfs_suflags, M_SEGMENT);
1450 lfs_free_resblks(fs);
1451 cv_destroy(&fs->lfs_sleeperscv);
1452 cv_destroy(&fs->lfs_diropscv);
1453 cv_destroy(&fs->lfs_stopcv);
1454 cv_destroy(&fs->lfs_nextsegsleep);
1455
1456 rw_destroy(&fs->lfs_fraglock);
1457 rw_destroy(&fs->lfs_iflock);
1458
1459 kmem_free(fs, sizeof(struct lfs));
1460 kmem_free(ump, sizeof(*ump));
1461
1462 mp->mnt_data = NULL;
1463 mp->mnt_flag &= ~MNT_LOCAL;
1464 return (error);
1465 }
1466
1467 static int
1468 lfs_flushfiles(struct mount *mp, int flags)
1469 {
1470 struct lwp *l = curlwp;
1471 struct ulfsmount *ump;
1472 struct lfs *fs;
1473 struct vnode *vp;
1474 int error;
1475
1476 ump = VFSTOULFS(mp);
1477 fs = ump->um_lfs;
1478
1479 /* Two checkpoints */
1480 if (!fs->lfs_ronly) {
1481 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
1482 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
1483 }
1484
1485 /* wake up the cleaner so it can die */
1486 /* XXX: shouldn't this be *after* the error cases below? */
1487 lfs_wakeup_cleaner(fs);
1488 mutex_enter(&lfs_lock);
1489 while (fs->lfs_sleepers)
1490 cv_wait(&fs->lfs_sleeperscv, &lfs_lock);
1491 mutex_exit(&lfs_lock);
1492
1493 #ifdef LFS_EXTATTR
1494 if (ump->um_fstype == ULFS1) {
1495 if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED) {
1496 ulfs_extattr_stop(mp, curlwp);
1497 }
1498 if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED) {
1499 ulfs_extattr_uepm_destroy(&ump->um_extattr);
1500 mp->mnt_flag &= ~MNT_EXTATTR;
1501 }
1502 }
1503 #endif
1504 #ifdef LFS_QUOTA
1505 if ((error = lfsquota1_umount(mp, flags)) != 0)
1506 return (error);
1507 #endif
1508 #ifdef LFS_QUOTA2
1509 if ((error = lfsquota2_umount(mp, flags)) != 0)
1510 return (error);
1511 #endif
1512 if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
1513 return (error);
1514 if ((error = VFS_SYNC(mp, 1, l->l_cred)) != 0)
1515 return (error);
1516 vp = fs->lfs_ivnode;
1517 mutex_enter(vp->v_interlock);
1518 if (LIST_FIRST(&vp->v_dirtyblkhd))
1519 panic("lfs_unmount: still dirty blocks on ifile vnode");
1520 mutex_exit(vp->v_interlock);
1521
1522 /* Explicitly write the superblock, to update serial and pflags */
1523 if (!fs->lfs_ronly) {
1524 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) | LFS_PF_CLEAN);
1525 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0));
1526 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1));
1527 }
1528 mutex_enter(&lfs_lock);
1529 while (fs->lfs_iocount)
1530 mtsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0,
1531 &lfs_lock);
1532 mutex_exit(&lfs_lock);
1533
1534 return 0;
1535 }
1536
1537 /*
1538 * Get file system statistics.
1539 *
1540 * NB: We don't lock to access the superblock here, because it's not
1541 * really that important if we get it wrong.
1542 */
1543 int
1544 lfs_statvfs(struct mount *mp, struct statvfs *sbp)
1545 {
1546 struct lfs *fs;
1547 struct ulfsmount *ump;
1548
1549 ump = VFSTOULFS(mp);
1550 fs = ump->um_lfs;
1551
1552 sbp->f_bsize = lfs_sb_getbsize(fs);
1553 sbp->f_frsize = lfs_sb_getfsize(fs);
1554 sbp->f_iosize = lfs_sb_getbsize(fs);
1555 sbp->f_blocks = LFS_EST_NONMETA(fs) - VTOI(fs->lfs_ivnode)->i_lfs_effnblks;
1556
1557 sbp->f_bfree = LFS_EST_BFREE(fs);
1558 /*
1559 * XXX this should be lfs_sb_getsize (measured in frags)
1560 * rather than dsize (measured in diskblocks). However,
1561 * getsize needs a format version check (for version 1 it
1562 * needs to be blockstofrags'd) so for the moment I'm going to
1563 * leave this... it won't fire wrongly as frags are at least
1564 * as big as diskblocks.
1565 */
1566 KASSERT(sbp->f_bfree <= lfs_sb_getdsize(fs));
1567 #if 0
1568 if (sbp->f_bfree < 0)
1569 sbp->f_bfree = 0;
1570 #endif
1571
1572 sbp->f_bresvd = LFS_EST_RSVD(fs);
1573 if (sbp->f_bfree > sbp->f_bresvd)
1574 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1575 else
1576 sbp->f_bavail = 0;
1577
1578 /* XXX: huh? - dholland 20150728 */
1579 sbp->f_files = lfs_sb_getbfree(fs) / lfs_btofsb(fs, lfs_sb_getibsize(fs))
1580 * LFS_INOPB(fs);
1581 sbp->f_ffree = sbp->f_files - lfs_sb_getnfiles(fs);
1582 sbp->f_favail = sbp->f_ffree;
1583 sbp->f_fresvd = 0;
1584 copy_statvfs_info(sbp, mp);
1585 return (0);
1586 }
1587
1588 /*
1589 * Go through the disk queues to initiate sandbagged IO;
1590 * go through the inodes to write those that have been modified;
1591 * initiate the writing of the super block if it has been modified.
1592 */
1593 int
1594 lfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
1595 {
1596 int error;
1597 struct lfs *fs;
1598
1599 fs = VFSTOULFS(mp)->um_lfs;
1600 if (fs->lfs_ronly)
1601 return 0;
1602
1603 /* Snapshots should not hose the syncer */
1604 /*
1605 * XXX Sync can block here anyway, since we don't have a very
1606 * XXX good idea of how much data is pending. If it's more
1607 * XXX than a segment and lfs_nextseg is close to the end of
1608 * XXX the log, we'll likely block.
1609 */
1610 mutex_enter(&lfs_lock);
1611 if (fs->lfs_nowrap && lfs_sb_getnextseg(fs) < lfs_sb_getcurseg(fs)) {
1612 mutex_exit(&lfs_lock);
1613 return 0;
1614 }
1615 mutex_exit(&lfs_lock);
1616
1617 lfs_writer_enter(fs, "lfs_dirops");
1618
1619 /* All syncs must be checkpoints until roll-forward is implemented. */
1620 DLOG((DLOG_FLUSH, "lfs_sync at 0x%jx\n",
1621 (uintmax_t)lfs_sb_getoffset(fs)));
1622 error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
1623 lfs_writer_leave(fs);
1624 #ifdef LFS_QUOTA
1625 lfs_qsync(mp);
1626 #endif
1627 return (error);
1628 }
1629
1630 /*
1631 * Look up an LFS dinode number to find its incore vnode. If not already
1632 * in core, read it in from the specified device. Return the inode locked.
1633 * Detection and handling of mount points must be done by the calling routine.
1634 */
1635 int
1636 lfs_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
1637 {
1638 int error;
1639
1640 error = vcache_get(mp, &ino, sizeof(ino), vpp);
1641 if (error)
1642 return error;
1643 error = vn_lock(*vpp, lktype);
1644 if (error) {
1645 vrele(*vpp);
1646 *vpp = NULL;
1647 return error;
1648 }
1649
1650 return 0;
1651 }
1652
1653 /*
1654 * Create a new vnode/inode pair and initialize what fields we can.
1655 */
1656 static void
1657 lfs_init_vnode(struct ulfsmount *ump, ino_t ino, struct vnode *vp)
1658 {
1659 struct lfs *fs = ump->um_lfs;
1660 struct inode *ip;
1661 union lfs_dinode *dp;
1662
1663 ASSERT_NO_SEGLOCK(fs);
1664
1665 /* Initialize the inode. */
1666 ip = pool_get(&lfs_inode_pool, PR_WAITOK);
1667 memset(ip, 0, sizeof(*ip));
1668 dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
1669 memset(dp, 0, sizeof(*dp));
1670 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
1671 memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
1672 ip->i_din = dp;
1673 ip->i_ump = ump;
1674 ip->i_vnode = vp;
1675 ip->i_dev = fs->lfs_dev;
1676 lfs_dino_setinumber(fs, dp, ino);
1677 ip->i_number = ino;
1678 ip->i_lfs = fs;
1679 ip->i_lfs_effnblks = 0;
1680 SPLAY_INIT(&ip->i_lfs_lbtree);
1681 ip->i_lfs_nbtree = 0;
1682 LIST_INIT(&ip->i_lfs_segdhd);
1683
1684 vp->v_tag = VT_LFS;
1685 vp->v_op = lfs_vnodeop_p;
1686 vp->v_data = ip;
1687 }
1688
1689 /*
1690 * Undo lfs_init_vnode().
1691 */
1692 static void
1693 lfs_deinit_vnode(struct ulfsmount *ump, struct vnode *vp)
1694 {
1695 struct inode *ip = VTOI(vp);
1696
1697 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1698 pool_put(&lfs_dinode_pool, ip->i_din);
1699 pool_put(&lfs_inode_pool, ip);
1700 vp->v_data = NULL;
1701 }
1702
1703 /*
1704 * Read an inode from disk and initialize this vnode / inode pair.
1705 * Caller assures no other thread will try to load this inode.
1706 */
1707 int
1708 lfs_loadvnode(struct mount *mp, struct vnode *vp,
1709 const void *key, size_t key_len, const void **new_key)
1710 {
1711 struct lfs *fs;
1712 union lfs_dinode *dip;
1713 struct inode *ip;
1714 struct buf *bp;
1715 IFILE *ifp;
1716 struct ulfsmount *ump;
1717 ino_t ino;
1718 daddr_t daddr;
1719 int error, retries;
1720 struct timespec ts;
1721
1722 KASSERT(key_len == sizeof(ino));
1723 memcpy(&ino, key, key_len);
1724
1725 memset(&ts, 0, sizeof ts); /* XXX gcc */
1726
1727 ump = VFSTOULFS(mp);
1728 fs = ump->um_lfs;
1729
1730 /*
1731 * If the filesystem is not completely mounted yet, suspend
1732 * any access requests (wait for roll-forward to complete).
1733 */
1734 mutex_enter(&lfs_lock);
1735 while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
1736 mtsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0,
1737 &lfs_lock);
1738 mutex_exit(&lfs_lock);
1739
1740 /* Translate the inode number to a disk address. */
1741 if (ino == LFS_IFILE_INUM)
1742 daddr = lfs_sb_getidaddr(fs);
1743 else {
1744 /* XXX bounds-check this too */
1745 LFS_IENTRY(ifp, fs, ino, bp);
1746 daddr = lfs_if_getdaddr(fs, ifp);
1747 if (lfs_sb_getversion(fs) > 1) {
1748 ts.tv_sec = lfs_if_getatime_sec(fs, ifp);
1749 ts.tv_nsec = lfs_if_getatime_nsec(fs, ifp);
1750 }
1751
1752 brelse(bp, 0);
1753 if (DADDR_IS_BAD(daddr))
1754 return (ENOENT);
1755 }
1756
1757 /* Allocate/init new vnode/inode. */
1758 lfs_init_vnode(ump, ino, vp);
1759 ip = VTOI(vp);
1760
1761 /* If the cleaner supplied the inode, use it. */
1762 if (curlwp == fs->lfs_cleaner_thread && fs->lfs_cleaner_hint != NULL &&
1763 fs->lfs_cleaner_hint->bi_lbn == LFS_UNUSED_LBN) {
1764 dip = fs->lfs_cleaner_hint->bi_bp;
1765 if (fs->lfs_is64) {
1766 error = copyin(dip, &ip->i_din->u_64,
1767 sizeof(struct lfs64_dinode));
1768 } else {
1769 error = copyin(dip, &ip->i_din->u_32,
1770 sizeof(struct lfs32_dinode));
1771 }
1772 if (error) {
1773 lfs_deinit_vnode(ump, vp);
1774 return error;
1775 }
1776 KASSERT(ip->i_number == ino);
1777 goto out;
1778 }
1779
1780 /* Read in the disk contents for the inode, copy into the inode. */
1781 retries = 0;
1782 again:
1783 error = bread(fs->lfs_devvp, LFS_FSBTODB(fs, daddr),
1784 (lfs_sb_getversion(fs) == 1 ? lfs_sb_getbsize(fs) : lfs_sb_getibsize(fs)),
1785 0, &bp);
1786 if (error) {
1787 lfs_deinit_vnode(ump, vp);
1788 return error;
1789 }
1790
1791 dip = lfs_ifind(fs, ino, bp);
1792 if (dip == NULL) {
1793 /* Assume write has not completed yet; try again */
1794 brelse(bp, BC_INVAL);
1795 ++retries;
1796 if (retries <= LFS_IFIND_RETRIES) {
1797 mutex_enter(&lfs_lock);
1798 if (fs->lfs_iocount) {
1799 DLOG((DLOG_VNODE,
1800 "%s: dinode %d not found, retrying...\n",
1801 __func__, ino));
1802 (void)mtsleep(&fs->lfs_iocount, PRIBIO + 1,
1803 "lfs ifind", 1, &lfs_lock);
1804 } else
1805 retries = LFS_IFIND_RETRIES;
1806 mutex_exit(&lfs_lock);
1807 goto again;
1808 }
1809 #ifdef DEBUG
1810 /* If the seglock is held look at the bpp to see
1811 what is there anyway */
1812 mutex_enter(&lfs_lock);
1813 if (fs->lfs_seglock > 0) {
1814 struct buf **bpp;
1815 union lfs_dinode *dp;
1816 int i;
1817
1818 for (bpp = fs->lfs_sp->bpp;
1819 bpp != fs->lfs_sp->cbpp; ++bpp) {
1820 if ((*bpp)->b_vp == fs->lfs_ivnode &&
1821 bpp != fs->lfs_sp->bpp) {
1822 /* Inode block */
1823 printf("%s: block 0x%" PRIx64 ": ",
1824 __func__, (*bpp)->b_blkno);
1825 for (i = 0; i < LFS_INOPB(fs); i++) {
1826 dp = DINO_IN_BLOCK(fs,
1827 (*bpp)->b_data, i);
1828 if (lfs_dino_getinumber(fs, dp))
1829 printf("%ju ",
1830 (uintmax_t)lfs_dino_getinumber(fs, dp));
1831 }
1832 printf("\n");
1833 }
1834 }
1835 }
1836 mutex_exit(&lfs_lock);
1837 #endif /* DEBUG */
1838 panic("lfs_loadvnode: dinode not found");
1839 }
1840 lfs_copy_dinode(fs, ip->i_din, dip);
1841 brelse(bp, 0);
1842
1843 out:
1844 if (lfs_sb_getversion(fs) > 1) {
1845 lfs_dino_setatime(fs, ip->i_din, ts.tv_sec);
1846 lfs_dino_setatimensec(fs, ip->i_din, ts.tv_nsec);
1847 }
1848
1849 lfs_vinit(mp, &vp);
1850
1851 *new_key = &ip->i_number;
1852 return 0;
1853 }
1854
1855 /*
1856 * Create a new inode and initialize this vnode / inode pair.
1857 */
1858 int
1859 lfs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp,
1860 struct vattr *vap, kauth_cred_t cred, void *extra,
1861 size_t *key_len, const void **new_key)
1862 {
1863 ino_t ino;
1864 struct inode *ip;
1865 struct ulfsmount *ump;
1866 struct lfs *fs;
1867 int error, mode, gen;
1868
1869 ump = VFSTOULFS(mp);
1870 fs = ump->um_lfs;
1871
1872 KASSERT(dvp != NULL || vap->va_fileid > 0);
1873 KASSERT((fs->lfs_flags & LFS_NOTYET) ||
1874 (dvp != NULL && dvp->v_mount == mp));
1875 KASSERT(vap->va_type != VNON);
1876
1877 *key_len = sizeof(ino);
1878 mode = MAKEIMODE(vap->va_type, vap->va_mode);
1879
1880 /*
1881 * Allocate fresh inode. With "dvp == NULL" take the inode number
1882 * and version from "vap".
1883 */
1884 if (dvp == NULL) {
1885 ino = vap->va_fileid;
1886 gen = vap->va_gen;
1887 error = lfs_valloc_fixed(fs, ino, gen);
1888 } else {
1889 error = lfs_valloc(dvp, mode, cred, &ino, &gen);
1890 }
1891 if (error)
1892 return error;
1893
1894 /* Attach inode to vnode. */
1895 lfs_init_vnode(ump, ino, vp);
1896 ip = VTOI(vp);
1897
1898 mutex_enter(&lfs_lock);
1899 LFS_SET_UINO(ip, IN_CHANGE);
1900 mutex_exit(&lfs_lock);
1901
1902 /* Note no blocks yet */
1903 ip->i_lfs_hiblk = -1;
1904
1905 /* Set a new generation number for this inode. */
1906 ip->i_gen = gen;
1907 lfs_dino_setgen(fs, ip->i_din, gen);
1908
1909 memset(ip->i_lfs_fragsize, 0,
1910 ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
1911
1912 /* Set uid / gid. */
1913 if (cred == NOCRED || cred == FSCRED) {
1914 ip->i_gid = 0;
1915 ip->i_uid = 0;
1916 } else {
1917 ip->i_gid = VTOI(dvp)->i_gid;
1918 ip->i_uid = kauth_cred_geteuid(cred);
1919 }
1920 DIP_ASSIGN(ip, gid, ip->i_gid);
1921 DIP_ASSIGN(ip, uid, ip->i_uid);
1922
1923 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
1924 error = lfs_chkiq(ip, 1, cred, 0);
1925 if (error) {
1926 lfs_vfree(dvp, ino, mode);
1927 lfs_deinit_vnode(ump, vp);
1928
1929 return error;
1930 }
1931 #endif
1932
1933 /* Set type and finalize. */
1934 ip->i_flags = 0;
1935 DIP_ASSIGN(ip, flags, 0);
1936 ip->i_mode = mode;
1937 DIP_ASSIGN(ip, mode, mode);
1938 if (vap->va_rdev != VNOVAL) {
1939 /*
1940 * Want to be able to use this to make badblock
1941 * inodes, so don't truncate the dev number.
1942 */
1943 // XXX clean this up
1944 if (ump->um_fstype == ULFS1)
1945 ip->i_din->u_32.di_rdev = ulfs_rw32(vap->va_rdev,
1946 ULFS_MPNEEDSWAP(fs));
1947 else
1948 ip->i_din->u_64.di_rdev = ulfs_rw64(vap->va_rdev,
1949 ULFS_MPNEEDSWAP(fs));
1950 }
1951 lfs_vinit(mp, &vp);
1952
1953 *new_key = &ip->i_number;
1954 return 0;
1955 }
1956
1957 /*
1958 * File handle to vnode
1959 */
1960 int
1961 lfs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp)
1962 {
1963 struct lfid lfh;
1964 struct lfs *fs;
1965
1966 if (fhp->fid_len != sizeof(struct lfid))
1967 return EINVAL;
1968
1969 memcpy(&lfh, fhp, sizeof(lfh));
1970 if (lfh.lfid_ino < LFS_IFILE_INUM)
1971 return ESTALE;
1972
1973 fs = VFSTOULFS(mp)->um_lfs;
1974 if (lfh.lfid_ident != lfs_sb_getident(fs))
1975 return ESTALE;
1976
1977 if (lfh.lfid_ino >
1978 ((lfs_dino_getsize(fs, VTOI(fs->lfs_ivnode)->i_din) >> lfs_sb_getbshift(fs)) -
1979 lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs))
1980 return ESTALE;
1981
1982 return (ulfs_fhtovp(mp, &lfh.lfid_ufid, lktype, vpp));
1983 }
1984
1985 /*
1986 * Vnode pointer to File handle
1987 */
1988 /* ARGSUSED */
1989 int
1990 lfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1991 {
1992 struct inode *ip;
1993 struct lfid lfh;
1994
1995 if (*fh_size < sizeof(struct lfid)) {
1996 *fh_size = sizeof(struct lfid);
1997 return E2BIG;
1998 }
1999 *fh_size = sizeof(struct lfid);
2000 ip = VTOI(vp);
2001 memset(&lfh, 0, sizeof(lfh));
2002 lfh.lfid_len = sizeof(struct lfid);
2003 lfh.lfid_ino = ip->i_number;
2004 lfh.lfid_gen = ip->i_gen;
2005 lfh.lfid_ident = lfs_sb_getident(ip->i_lfs);
2006 memcpy(fhp, &lfh, sizeof(lfh));
2007 return (0);
2008 }
2009
2010 /*
2011 * ulfs_bmaparray callback function for writing.
2012 *
2013 * Since blocks will be written to the new segment anyway,
2014 * we don't care about current daddr of them.
2015 */
2016 static bool
2017 lfs_issequential_hole(const struct lfs *fs,
2018 daddr_t daddr0, daddr_t daddr1)
2019 {
2020 (void)fs; /* not used */
2021
2022 KASSERT(daddr0 == UNWRITTEN ||
2023 (0 <= daddr0 && daddr0 <= LFS_MAX_DADDR(fs)));
2024 KASSERT(daddr1 == UNWRITTEN ||
2025 (0 <= daddr1 && daddr1 <= LFS_MAX_DADDR(fs)));
2026
2027 /* NOTE: all we want to know here is 'hole or not'. */
2028 /* NOTE: UNASSIGNED is converted to 0 by ulfs_bmaparray. */
2029
2030 /*
2031 * treat UNWRITTENs and all resident blocks as 'contiguous'
2032 */
2033 if (daddr0 != 0 && daddr1 != 0)
2034 return true;
2035
2036 /*
2037 * both are in hole?
2038 */
2039 if (daddr0 == 0 && daddr1 == 0)
2040 return true; /* all holes are 'contiguous' for us. */
2041
2042 return false;
2043 }
2044
2045 /*
2046 * lfs_gop_write functions exactly like genfs_gop_write, except that
2047 * (1) it requires the seglock to be held by its caller, and sp->fip
2048 * to be properly initialized (it will return without re-initializing
2049 * sp->fip, and without calling lfs_writeseg).
2050 * (2) it uses the remaining space in the segment, rather than VOP_BMAP,
2051 * to determine how large a block it can write at once (though it does
2052 * still use VOP_BMAP to find holes in the file);
2053 * (3) it calls lfs_gatherblock instead of VOP_STRATEGY on its blocks
2054 * (leaving lfs_writeseg to deal with the cluster blocks, so we might
2055 * now have clusters of clusters, ick.)
2056 */
2057 static int
2058 lfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
2059 int flags)
2060 {
2061 int i, error, run, haveeof = 0;
2062 int fs_bshift;
2063 vaddr_t kva;
2064 off_t eof, offset, startoffset = 0;
2065 size_t bytes, iobytes, skipbytes;
2066 bool async = (flags & PGO_SYNCIO) == 0;
2067 daddr_t lbn, blkno;
2068 struct vm_page *pg;
2069 struct buf *mbp, *bp;
2070 struct vnode *devvp = VTOI(vp)->i_devvp;
2071 struct inode *ip = VTOI(vp);
2072 struct lfs *fs = ip->i_lfs;
2073 struct segment *sp = fs->lfs_sp;
2074 SEGSUM *ssp;
2075 UVMHIST_FUNC("lfs_gop_write"); UVMHIST_CALLED(ubchist);
2076 const char * failreason = NULL;
2077
2078 ASSERT_SEGLOCK(fs);
2079
2080 /* The Ifile lives in the buffer cache */
2081 KASSERT(vp != fs->lfs_ivnode);
2082
2083 /*
2084 * We don't want to fill the disk before the cleaner has a chance
2085 * to make room for us. If we're in danger of doing that, fail
2086 * with EAGAIN. The caller will have to notice this, unlock
2087 * so the cleaner can run, relock and try again.
2088 *
2089 * We must write everything, however, if our vnode is being
2090 * reclaimed.
2091 */
2092 mutex_enter(vp->v_interlock);
2093 if (LFS_STARVED_FOR_SEGS(fs) && vdead_check(vp, VDEAD_NOWAIT) == 0) {
2094 mutex_exit(vp->v_interlock);
2095 failreason = "Starved for segs and not flushing vp";
2096 goto tryagain;
2097 }
2098 mutex_exit(vp->v_interlock);
2099
2100 /*
2101 * Sometimes things slip past the filters in lfs_putpages,
2102 * and the pagedaemon tries to write pages---problem is
2103 * that the pagedaemon never acquires the segment lock.
2104 *
2105 * Alternatively, pages that were clean when we called
2106 * genfs_putpages may have become dirty in the meantime. In this
2107 * case the segment header is not properly set up for blocks
2108 * to be added to it.
2109 *
2110 * Unbusy and unclean the pages, and put them on the ACTIVE
2111 * queue under the hypothesis that they couldn't have got here
2112 * unless they were modified *quite* recently.
2113 *
2114 * XXXUBC that last statement is an oversimplification of course.
2115 */
2116 if (!LFS_SEGLOCK_HELD(fs)) {
2117 failreason = "Seglock not held";
2118 goto tryagain;
2119 }
2120 if (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) {
2121 failreason = "Inode with no_gop_write";
2122 goto tryagain;
2123 }
2124 if ((pgs[0]->offset & lfs_sb_getbmask(fs)) != 0) {
2125 failreason = "Bad page offset";
2126 goto tryagain;
2127 }
2128
2129 UVMHIST_LOG(ubchist, "vp %#jx pgs %#jx npages %jd flags 0x%jx",
2130 (uintptr_t)vp, (uintptr_t)pgs, npages, flags);
2131
2132 GOP_SIZE(vp, vp->v_size, &eof, 0);
2133 haveeof = 1;
2134
2135 if (vp->v_type == VREG)
2136 fs_bshift = vp->v_mount->mnt_fs_bshift;
2137 else
2138 fs_bshift = DEV_BSHIFT;
2139 error = 0;
2140 pg = pgs[0];
2141 startoffset = pg->offset;
2142 KASSERT(eof >= 0);
2143
2144 if (startoffset >= eof) {
2145 failreason = "Offset beyond EOF";
2146 goto tryagain;
2147 } else
2148 bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
2149 skipbytes = 0;
2150
2151 KASSERT(bytes != 0);
2152
2153 /* Swap PG_DELWRI for PG_PAGEOUT */
2154 for (i = 0; i < npages; i++) {
2155 if (pgs[i]->flags & PG_DELWRI) {
2156 KASSERT(!(pgs[i]->flags & PG_PAGEOUT));
2157 pgs[i]->flags &= ~PG_DELWRI;
2158 pgs[i]->flags |= PG_PAGEOUT;
2159 uvm_pageout_start(1);
2160 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
2161 uvm_pagelock(pgs[i]);
2162 uvm_pageunwire(pgs[i]);
2163 uvm_pageunlock(pgs[i]);
2164 rw_exit(vp->v_uobj.vmobjlock);
2165 }
2166 }
2167
2168 /*
2169 * Check to make sure we're starting on a block boundary.
2170 * We'll check later to make sure we always write entire
2171 * blocks (or fragments).
2172 */
2173 if (startoffset & lfs_sb_getbmask(fs))
2174 printf("%" PRId64 " & %" PRIu64 " = %" PRId64 "\n",
2175 startoffset, lfs_sb_getbmask(fs),
2176 startoffset & lfs_sb_getbmask(fs));
2177 KASSERT((startoffset & lfs_sb_getbmask(fs)) == 0);
2178 if (bytes & lfs_sb_getffmask(fs)) {
2179 printf("lfs_gop_write: asked to write %ld bytes\n", (long)bytes);
2180 panic("lfs_gop_write: non-integer blocks");
2181 }
2182
2183 /*
2184 * We could deadlock here on pager_map with UVMPAGER_MAPIN_WAITOK.
2185 * If we would, write what we have and try again. If we don't
2186 * have anything to write, we'll have to sleep.
2187 */
2188 ssp = (SEGSUM *)sp->segsum;
2189 if ((kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
2190 (lfs_ss_getnfinfo(fs, ssp) < 1 ?
2191 UVMPAGER_MAPIN_WAITOK : 0))) == 0x0) {
2192 DLOG((DLOG_PAGE, "lfs_gop_write: forcing write\n"));
2193 #if 0
2194 " with nfinfo=%d at offset 0x%jx\n",
2195 (int)lfs_ss_getnfinfo(fs, ssp),
2196 (uintmax_t)lfs_sb_getoffset(fs)));
2197 #endif
2198 lfs_updatemeta(sp);
2199 lfs_release_finfo(fs);
2200 (void) lfs_writeseg(fs, sp);
2201
2202 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
2203
2204 /*
2205 * Having given up all of the pager_map we were holding,
2206 * we can now wait for aiodoned to reclaim it for us
2207 * without fear of deadlock.
2208 */
2209 kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
2210 UVMPAGER_MAPIN_WAITOK);
2211 }
2212
2213 mbp = getiobuf(NULL, true);
2214 UVMHIST_LOG(ubchist, "vp %#jx mbp %#jx num now %jd bytes 0x%jx",
2215 (uintptr_t)vp, (uintptr_t)mbp, vp->v_numoutput, bytes);
2216 mbp->b_bufsize = npages << PAGE_SHIFT;
2217 mbp->b_data = (void *)kva;
2218 mbp->b_resid = mbp->b_bcount = bytes;
2219 mbp->b_cflags |= BC_BUSY|BC_AGE;
2220 mbp->b_iodone = uvm_aio_aiodone;
2221
2222 bp = NULL;
2223 for (offset = startoffset;
2224 bytes > 0;
2225 offset += iobytes, bytes -= iobytes) {
2226 lbn = offset >> fs_bshift;
2227 error = ulfs_bmaparray(vp, lbn, &blkno, NULL, NULL, &run,
2228 lfs_issequential_hole);
2229 if (error) {
2230 UVMHIST_LOG(ubchist, "ulfs_bmaparray() -> %jd",
2231 error,0,0,0);
2232 skipbytes += bytes;
2233 bytes = 0;
2234 break;
2235 }
2236
2237 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
2238 bytes);
2239 if (blkno == (daddr_t)-1) {
2240 skipbytes += iobytes;
2241 continue;
2242 }
2243
2244 /*
2245 * Discover how much we can really pack into this buffer.
2246 */
2247 /* If no room in the current segment, finish it up */
2248 if (sp->sum_bytes_left < sizeof(int32_t) ||
2249 sp->seg_bytes_left < (1 << lfs_sb_getbshift(fs))) {
2250 int vers;
2251
2252 lfs_updatemeta(sp);
2253 vers = lfs_fi_getversion(fs, sp->fip);
2254 lfs_release_finfo(fs);
2255 (void) lfs_writeseg(fs, sp);
2256
2257 lfs_acquire_finfo(fs, ip->i_number, vers);
2258 }
2259 /* Check both for space in segment and space in segsum */
2260 iobytes = MIN(iobytes, (sp->seg_bytes_left >> fs_bshift)
2261 << fs_bshift);
2262 iobytes = MIN(iobytes, (sp->sum_bytes_left / sizeof(int32_t))
2263 << fs_bshift);
2264 KASSERT(iobytes > 0);
2265
2266 /* if it's really one i/o, don't make a second buf */
2267 if (offset == startoffset && iobytes == bytes) {
2268 bp = mbp;
2269 /*
2270 * All the LFS output is done by the segwriter. It
2271 * will increment numoutput by one for all the bufs it
2272 * receives. However this buffer needs one extra to
2273 * account for aiodone.
2274 */
2275 mutex_enter(vp->v_interlock);
2276 vp->v_numoutput++;
2277 mutex_exit(vp->v_interlock);
2278 } else {
2279 bp = getiobuf(NULL, true);
2280 UVMHIST_LOG(ubchist, "vp %#jx bp %#jx num now %jd",
2281 (uintptr_t)vp, (uintptr_t)bp, vp->v_numoutput, 0);
2282 nestiobuf_setup(mbp, bp, offset - pg->offset, iobytes);
2283 /*
2284 * LFS doesn't like async I/O here, dies with
2285 * an assert in lfs_bwrite(). Is that assert
2286 * valid? I retained non-async behaviour when
2287 * converted this to use nestiobuf --pooka
2288 */
2289 bp->b_flags &= ~B_ASYNC;
2290 }
2291
2292 /* XXX This is silly ... is this necessary? */
2293 mutex_enter(&bufcache_lock);
2294 mutex_enter(vp->v_interlock);
2295 bgetvp(vp, bp);
2296 mutex_exit(vp->v_interlock);
2297 mutex_exit(&bufcache_lock);
2298
2299 bp->b_lblkno = lfs_lblkno(fs, offset);
2300 bp->b_private = mbp;
2301 if (devvp->v_type == VBLK) {
2302 bp->b_dev = devvp->v_rdev;
2303 }
2304 VOP_BWRITE(bp->b_vp, bp);
2305 while (lfs_gatherblock(sp, bp, NULL))
2306 continue;
2307 }
2308
2309 nestiobuf_done(mbp, skipbytes, error);
2310 if (skipbytes) {
2311 UVMHIST_LOG(ubchist, "skipbytes %jd", skipbytes, 0,0,0);
2312 }
2313 UVMHIST_LOG(ubchist, "returning 0", 0,0,0,0);
2314
2315 if (!async) {
2316 /* Start a segment write. */
2317 UVMHIST_LOG(ubchist, "flushing", 0,0,0,0);
2318 mutex_enter(&lfs_lock);
2319 lfs_flush(fs, 0, 1);
2320 mutex_exit(&lfs_lock);
2321 }
2322
2323 if ((sp->seg_flags & SEGM_SINGLE) && lfs_sb_getcurseg(fs) != fs->lfs_startseg)
2324 return EAGAIN;
2325
2326 return (0);
2327
2328 tryagain:
2329 /*
2330 * We can't write the pages, for whatever reason.
2331 * Clean up after ourselves, and make the caller try again.
2332 */
2333
2334 /* Tell why we're here, if we know */
2335 if (failreason != NULL) {
2336 DLOG((DLOG_PAGE, "lfs_gop_write: %s\n", failreason));
2337 }
2338 if (haveeof && startoffset >= eof) {
2339 DLOG((DLOG_PAGE, "lfs_gop_write: ino %d start 0x%" PRIx64
2340 " eof 0x%" PRIx64 " npages=%d\n", VTOI(vp)->i_number,
2341 pgs[0]->offset, eof, npages));
2342 }
2343
2344 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
2345 for (i = 0; i < npages; i++) {
2346 pg = pgs[i];
2347
2348 if (pg->flags & PG_PAGEOUT)
2349 uvm_pageout_done(1);
2350 uvm_pagelock(pg);
2351 if (pg->flags & PG_DELWRI) {
2352 uvm_pageunwire(pg);
2353 }
2354 uvm_pageactivate(pg);
2355 uvm_pageunlock(pg);
2356 pg->flags &= ~(PG_DELWRI|PG_PAGEOUT|PG_RELEASED);
2357 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
2358 DLOG((DLOG_PAGE, "pg[%d] = %p (vp %p off %" PRIx64 ")\n", i, pg,
2359 vp, pg->offset));
2360 DLOG((DLOG_PAGE, "pg[%d]->flags = %x\n", i, pg->flags));
2361 DLOG((DLOG_PAGE, "pg[%d]->pqflags = %x\n", i, pg->pqflags));
2362 DLOG((DLOG_PAGE, "pg[%d]->uanon = %p\n", i, pg->uanon));
2363 DLOG((DLOG_PAGE, "pg[%d]->uobject = %p\n", i, pg->uobject));
2364 DLOG((DLOG_PAGE, "pg[%d]->wire_count = %d\n", i,
2365 pg->wire_count));
2366 DLOG((DLOG_PAGE, "pg[%d]->loan_count = %d\n", i,
2367 pg->loan_count));
2368 }
2369 uvm_page_unbusy(pgs, npages);
2370 rw_exit(vp->v_uobj.vmobjlock);
2371 return EAGAIN;
2372 }
2373
2374 /*
2375 * finish vnode/inode initialization.
2376 * used by lfs_vget.
2377 */
2378 void
2379 lfs_vinit(struct mount *mp, struct vnode **vpp)
2380 {
2381 struct vnode *vp = *vpp;
2382 struct inode *ip = VTOI(vp);
2383 struct ulfsmount *ump = VFSTOULFS(mp);
2384 struct lfs *fs = ump->um_lfs;
2385 int i;
2386
2387 ip->i_mode = lfs_dino_getmode(fs, ip->i_din);
2388 ip->i_nlink = lfs_dino_getnlink(fs, ip->i_din);
2389 ip->i_lfs_osize = ip->i_size = lfs_dino_getsize(fs, ip->i_din);
2390 ip->i_flags = lfs_dino_getflags(fs, ip->i_din);
2391 ip->i_gen = lfs_dino_getgen(fs, ip->i_din);
2392 ip->i_uid = lfs_dino_getuid(fs, ip->i_din);
2393 ip->i_gid = lfs_dino_getgid(fs, ip->i_din);
2394
2395 ip->i_lfs_effnblks = lfs_dino_getblocks(fs, ip->i_din);
2396 ip->i_lfs_odnlink = lfs_dino_getnlink(fs, ip->i_din);
2397
2398 /*
2399 * Initialize the vnode from the inode, check for aliases. In all
2400 * cases re-init ip, the underlying vnode/inode may have changed.
2401 */
2402 ulfs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
2403 ip = VTOI(vp);
2404
2405 memset(ip->i_lfs_fragsize, 0, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
2406 if (vp->v_type != VLNK || ip->i_size >= ip->i_lfs->um_maxsymlinklen) {
2407 #ifdef DEBUG
2408 for (i = (ip->i_size + lfs_sb_getbsize(fs) - 1) >> lfs_sb_getbshift(fs);
2409 i < ULFS_NDADDR; i++) {
2410 if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
2411 i == 0)
2412 continue;
2413 if (lfs_dino_getdb(fs, ip->i_din, i) != 0) {
2414 lfs_dump_dinode(fs, ip->i_din);
2415 panic("inconsistent inode (direct)");
2416 }
2417 }
2418 for ( ; i < ULFS_NDADDR + ULFS_NIADDR; i++) {
2419 if (lfs_dino_getib(fs, ip->i_din, i - ULFS_NDADDR) != 0) {
2420 lfs_dump_dinode(fs, ip->i_din);
2421 panic("inconsistent inode (indirect)");
2422 }
2423 }
2424 #endif /* DEBUG */
2425 for (i = 0; i < ULFS_NDADDR; i++)
2426 if (lfs_dino_getdb(fs, ip->i_din, i) != 0)
2427 ip->i_lfs_fragsize[i] = lfs_blksize(fs, ip, i);
2428 }
2429
2430 KASSERTMSG((vp->v_type != VNON),
2431 "lfs_vinit: ino %llu is type VNON! (ifmt=%o)\n",
2432 (unsigned long long)ip->i_number,
2433 (ip->i_mode & LFS_IFMT) >> 12);
2434
2435 /*
2436 * Finish inode initialization now that aliasing has been resolved.
2437 */
2438
2439 ip->i_devvp = fs->lfs_devvp;
2440 vref(ip->i_devvp);
2441 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
2442 ulfsquota_init(ip);
2443 #endif
2444 genfs_node_init(vp, &lfs_genfsops);
2445 uvm_vnp_setsize(vp, ip->i_size);
2446
2447 /* Initialize hiblk from file size */
2448 ip->i_lfs_hiblk = lfs_lblkno(ip->i_lfs, ip->i_size + lfs_sb_getbsize(ip->i_lfs) - 1) - 1;
2449
2450 *vpp = vp;
2451 }
2452
2453 /*
2454 * Resize the filesystem to contain the specified number of segments.
2455 */
2456 int
2457 lfs_resize_fs(struct lfs *fs, int newnsegs)
2458 {
2459 SEGUSE *sup;
2460 CLEANERINFO *cip;
2461 struct buf *bp, *obp;
2462 daddr_t olast, nlast, ilast, noff, start, end;
2463 struct vnode *ivp;
2464 struct inode *ip;
2465 int error, badnews, inc, oldnsegs;
2466 int sbbytes, csbbytes, gain, cgain;
2467 int i;
2468
2469 /* Only support v2 and up */
2470 if (lfs_sb_getversion(fs) < 2)
2471 return EOPNOTSUPP;
2472
2473 /* If we're doing nothing, do it fast */
2474 oldnsegs = lfs_sb_getnseg(fs);
2475 if (newnsegs == oldnsegs)
2476 return 0;
2477
2478 /* We always have to have two superblocks */
2479 if (newnsegs <= lfs_dtosn(fs, lfs_sb_getsboff(fs, 1)))
2480 /* XXX this error code is rather nonsense */
2481 return EFBIG;
2482
2483 ivp = fs->lfs_ivnode;
2484 ip = VTOI(ivp);
2485 error = 0;
2486
2487 /* Take the segment lock so no one else calls lfs_newseg() */
2488 lfs_seglock(fs, SEGM_PROT);
2489
2490 /*
2491 * Make sure the segments we're going to be losing, if any,
2492 * are in fact empty. We hold the seglock, so their status
2493 * cannot change underneath us. Count the superblocks we lose,
2494 * while we're at it.
2495 */
2496 sbbytes = csbbytes = 0;
2497 cgain = 0;
2498 for (i = newnsegs; i < oldnsegs; i++) {
2499 LFS_SEGENTRY(sup, fs, i, bp);
2500 badnews = sup->su_nbytes || !(sup->su_flags & SEGUSE_INVAL);
2501 if (sup->su_flags & SEGUSE_SUPERBLOCK)
2502 sbbytes += LFS_SBPAD;
2503 if (!(sup->su_flags & SEGUSE_DIRTY)) {
2504 ++cgain;
2505 if (sup->su_flags & SEGUSE_SUPERBLOCK)
2506 csbbytes += LFS_SBPAD;
2507 }
2508 brelse(bp, 0);
2509 if (badnews) {
2510 error = EBUSY;
2511 goto out;
2512 }
2513 }
2514
2515 /* Note old and new segment table endpoints, and old ifile size */
2516 olast = lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs);
2517 nlast = howmany(newnsegs, lfs_sb_getsepb(fs)) + lfs_sb_getcleansz(fs);
2518 ilast = ivp->v_size >> lfs_sb_getbshift(fs);
2519 noff = nlast - olast;
2520
2521 /*
2522 * Make sure no one can use the Ifile while we change it around.
2523 * Even after taking the iflock we need to make sure no one still
2524 * is holding Ifile buffers, so we get each one, to drain them.
2525 * (XXX this could be done better.)
2526 */
2527 rw_enter(&fs->lfs_iflock, RW_WRITER);
2528 for (i = 0; i < ilast; i++) {
2529 /* XXX what to do if bread fails? */
2530 bread(ivp, i, lfs_sb_getbsize(fs), 0, &bp);
2531 brelse(bp, 0);
2532 }
2533
2534 /* Allocate new Ifile blocks */
2535 for (i = ilast; i < ilast + noff; i++) {
2536 if (lfs_balloc(ivp, i * lfs_sb_getbsize(fs), lfs_sb_getbsize(fs), NOCRED, 0,
2537 &bp) != 0)
2538 panic("balloc extending ifile");
2539 memset(bp->b_data, 0, lfs_sb_getbsize(fs));
2540 VOP_BWRITE(bp->b_vp, bp);
2541 }
2542
2543 /* Register new ifile size */
2544 ip->i_size += noff * lfs_sb_getbsize(fs);
2545 lfs_dino_setsize(fs, ip->i_din, ip->i_size);
2546 uvm_vnp_setsize(ivp, ip->i_size);
2547
2548 /* Copy the inode table to its new position */
2549 if (noff != 0) {
2550 if (noff < 0) {
2551 start = nlast;
2552 end = ilast + noff;
2553 inc = 1;
2554 } else {
2555 start = ilast + noff - 1;
2556 end = nlast - 1;
2557 inc = -1;
2558 }
2559 for (i = start; i != end; i += inc) {
2560 if (bread(ivp, i, lfs_sb_getbsize(fs),
2561 B_MODIFY, &bp) != 0)
2562 panic("resize: bread dst blk failed");
2563 if (bread(ivp, i - noff, lfs_sb_getbsize(fs),
2564 0, &obp))
2565 panic("resize: bread src blk failed");
2566 memcpy(bp->b_data, obp->b_data, lfs_sb_getbsize(fs));
2567 VOP_BWRITE(bp->b_vp, bp);
2568 brelse(obp, 0);
2569 }
2570 }
2571
2572 /* If we are expanding, write the new empty SEGUSE entries */
2573 if (newnsegs > oldnsegs) {
2574 for (i = oldnsegs; i < newnsegs; i++) {
2575 if ((error = bread(ivp, i / lfs_sb_getsepb(fs) +
2576 lfs_sb_getcleansz(fs), lfs_sb_getbsize(fs),
2577 B_MODIFY, &bp)) != 0)
2578 panic("lfs: ifile read: %d", error);
2579 while ((i + 1) % lfs_sb_getsepb(fs) && i < newnsegs) {
2580 sup = &((SEGUSE *)bp->b_data)[i % lfs_sb_getsepb(fs)];
2581 memset(sup, 0, sizeof(*sup));
2582 i++;
2583 }
2584 VOP_BWRITE(bp->b_vp, bp);
2585 }
2586 }
2587
2588 /* Zero out unused superblock offsets */
2589 for (i = 2; i < LFS_MAXNUMSB; i++)
2590 if (lfs_dtosn(fs, lfs_sb_getsboff(fs, i)) >= newnsegs)
2591 lfs_sb_setsboff(fs, i, 0x0);
2592
2593 /*
2594 * Correct superblock entries that depend on fs size.
2595 * The computations of these are as follows:
2596 *
2597 * size = lfs_segtod(fs, nseg)
2598 * dsize = lfs_segtod(fs, nseg - minfreeseg) - lfs_btofsb(#super * LFS_SBPAD)
2599 * bfree = dsize - lfs_btofsb(fs, bsize * nseg / 2) - blocks_actually_used
2600 * avail = lfs_segtod(fs, nclean) - lfs_btofsb(#clean_super * LFS_SBPAD)
2601 * + (lfs_segtod(fs, 1) - (offset - curseg))
2602 * - lfs_segtod(fs, minfreeseg - (minfreeseg / 2))
2603 *
2604 * XXX - we should probably adjust minfreeseg as well.
2605 */
2606 gain = (newnsegs - oldnsegs);
2607 lfs_sb_setnseg(fs, newnsegs);
2608 lfs_sb_setsegtabsz(fs, nlast - lfs_sb_getcleansz(fs));
2609 lfs_sb_addsize(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)));
2610 lfs_sb_adddsize(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)) - lfs_btofsb(fs, sbbytes));
2611 lfs_sb_addbfree(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)) - lfs_btofsb(fs, sbbytes)
2612 - gain * lfs_btofsb(fs, lfs_sb_getbsize(fs) / 2));
2613 if (gain > 0) {
2614 lfs_sb_addnclean(fs, gain);
2615 lfs_sb_addavail(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)));
2616 } else {
2617 lfs_sb_subnclean(fs, cgain);
2618 lfs_sb_subavail(fs, cgain * lfs_btofsb(fs, lfs_sb_getssize(fs)) -
2619 lfs_btofsb(fs, csbbytes));
2620 }
2621
2622 /* Resize segment flag cache */
2623 fs->lfs_suflags[0] = realloc(fs->lfs_suflags[0],
2624 lfs_sb_getnseg(fs) * sizeof(u_int32_t), M_SEGMENT, M_WAITOK);
2625 fs->lfs_suflags[1] = realloc(fs->lfs_suflags[1],
2626 lfs_sb_getnseg(fs) * sizeof(u_int32_t), M_SEGMENT, M_WAITOK);
2627 for (i = oldnsegs; i < newnsegs; i++)
2628 fs->lfs_suflags[0][i] = fs->lfs_suflags[1][i] = 0x0;
2629
2630 /* Truncate Ifile if necessary */
2631 if (noff < 0)
2632 lfs_truncate(ivp, ivp->v_size + (noff << lfs_sb_getbshift(fs)), 0,
2633 NOCRED);
2634
2635 /* Update cleaner info so the cleaner can die */
2636 /* XXX what to do if bread fails? */
2637 bread(ivp, 0, lfs_sb_getbsize(fs), B_MODIFY, &bp);
2638 cip = bp->b_data;
2639 lfs_ci_setclean(fs, cip, lfs_sb_getnclean(fs));
2640 lfs_ci_setdirty(fs, cip, lfs_sb_getnseg(fs) - lfs_sb_getnclean(fs));
2641 VOP_BWRITE(bp->b_vp, bp);
2642
2643 /* Let Ifile accesses proceed */
2644 rw_exit(&fs->lfs_iflock);
2645
2646 out:
2647 lfs_segunlock(fs);
2648 return error;
2649 }
2650
2651 /*
2652 * Extended attribute dispatch
2653 */
2654 int
2655 lfs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
2656 int attrnamespace, const char *attrname)
2657 {
2658 #ifdef LFS_EXTATTR
2659 struct ulfsmount *ump;
2660
2661 ump = VFSTOULFS(mp);
2662 if (ump->um_fstype == ULFS1) {
2663 return ulfs_extattrctl(mp, cmd, vp, attrnamespace, attrname);
2664 }
2665 #endif
2666 return vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname);
2667 }
2668