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