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