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