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