lfs_vfsops.c revision 1.379 1 /* $NetBSD: lfs_vfsops.c,v 1.379 2020/08/04 03:00:47 riastradh 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.379 2020/08/04 03:00:47 riastradh 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_vflag |= VV_LOCKSWORK;
1596 vp->v_data = ip;
1597 }
1598
1599 /*
1600 * Undo lfs_init_vnode().
1601 */
1602 static void
1603 lfs_deinit_vnode(struct ulfsmount *ump, struct vnode *vp)
1604 {
1605 struct inode *ip = VTOI(vp);
1606
1607 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1608 pool_put(&lfs_dinode_pool, ip->i_din);
1609 pool_put(&lfs_inode_pool, ip);
1610 vp->v_data = NULL;
1611 }
1612
1613 /*
1614 * Read an inode from disk and initialize this vnode / inode pair.
1615 * Caller assures no other thread will try to load this inode.
1616 */
1617 int
1618 lfs_loadvnode(struct mount *mp, struct vnode *vp,
1619 const void *key, size_t key_len, const void **new_key)
1620 {
1621 struct lfs *fs;
1622 union lfs_dinode *dip;
1623 struct inode *ip;
1624 struct buf *bp;
1625 IFILE *ifp;
1626 struct ulfsmount *ump;
1627 ino_t ino;
1628 daddr_t daddr;
1629 int error, retries;
1630 struct timespec ts;
1631
1632 KASSERT(key_len == sizeof(ino));
1633 memcpy(&ino, key, key_len);
1634
1635 memset(&ts, 0, sizeof ts); /* XXX gcc */
1636
1637 ump = VFSTOULFS(mp);
1638 fs = ump->um_lfs;
1639
1640 /*
1641 * If the filesystem is not completely mounted yet, suspend
1642 * any access requests (wait for roll-forward to complete).
1643 */
1644 mutex_enter(&lfs_lock);
1645 while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
1646 mtsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0,
1647 &lfs_lock);
1648 mutex_exit(&lfs_lock);
1649
1650 /* Translate the inode number to a disk address. */
1651 if (ino == LFS_IFILE_INUM)
1652 daddr = lfs_sb_getidaddr(fs);
1653 else {
1654 /* XXX bounds-check this too */
1655 LFS_IENTRY(ifp, fs, ino, bp);
1656 daddr = lfs_if_getdaddr(fs, ifp);
1657 if (lfs_sb_getversion(fs) > 1) {
1658 ts.tv_sec = lfs_if_getatime_sec(fs, ifp);
1659 ts.tv_nsec = lfs_if_getatime_nsec(fs, ifp);
1660 }
1661
1662 brelse(bp, 0);
1663 if (daddr == LFS_UNUSED_DADDR)
1664 return (ENOENT);
1665 }
1666
1667 /* Allocate/init new vnode/inode. */
1668 lfs_init_vnode(ump, ino, vp);
1669 ip = VTOI(vp);
1670
1671 /* If the cleaner supplied the inode, use it. */
1672 if (curlwp == fs->lfs_cleaner_thread && fs->lfs_cleaner_hint != NULL &&
1673 fs->lfs_cleaner_hint->bi_lbn == LFS_UNUSED_LBN) {
1674 dip = fs->lfs_cleaner_hint->bi_bp;
1675 if (fs->lfs_is64) {
1676 error = copyin(dip, &ip->i_din->u_64,
1677 sizeof(struct lfs64_dinode));
1678 } else {
1679 error = copyin(dip, &ip->i_din->u_32,
1680 sizeof(struct lfs32_dinode));
1681 }
1682 if (error) {
1683 lfs_deinit_vnode(ump, vp);
1684 return error;
1685 }
1686 KASSERT(ip->i_number == ino);
1687 goto out;
1688 }
1689
1690 /* Read in the disk contents for the inode, copy into the inode. */
1691 retries = 0;
1692 again:
1693 error = bread(fs->lfs_devvp, LFS_FSBTODB(fs, daddr),
1694 (lfs_sb_getversion(fs) == 1 ? lfs_sb_getbsize(fs) : lfs_sb_getibsize(fs)),
1695 0, &bp);
1696 if (error) {
1697 lfs_deinit_vnode(ump, vp);
1698 return error;
1699 }
1700
1701 dip = lfs_ifind(fs, ino, bp);
1702 if (dip == NULL) {
1703 /* Assume write has not completed yet; try again */
1704 brelse(bp, BC_INVAL);
1705 ++retries;
1706 if (retries <= LFS_IFIND_RETRIES) {
1707 mutex_enter(&lfs_lock);
1708 if (fs->lfs_iocount) {
1709 DLOG((DLOG_VNODE,
1710 "%s: dinode %d not found, retrying...\n",
1711 __func__, ino));
1712 (void)mtsleep(&fs->lfs_iocount, PRIBIO + 1,
1713 "lfs ifind", 1, &lfs_lock);
1714 } else
1715 retries = LFS_IFIND_RETRIES;
1716 mutex_exit(&lfs_lock);
1717 goto again;
1718 }
1719 #ifdef DEBUG
1720 /* If the seglock is held look at the bpp to see
1721 what is there anyway */
1722 mutex_enter(&lfs_lock);
1723 if (fs->lfs_seglock > 0) {
1724 struct buf **bpp;
1725 union lfs_dinode *dp;
1726 int i;
1727
1728 for (bpp = fs->lfs_sp->bpp;
1729 bpp != fs->lfs_sp->cbpp; ++bpp) {
1730 if ((*bpp)->b_vp == fs->lfs_ivnode &&
1731 bpp != fs->lfs_sp->bpp) {
1732 /* Inode block */
1733 printf("%s: block 0x%" PRIx64 ": ",
1734 __func__, (*bpp)->b_blkno);
1735 for (i = 0; i < LFS_INOPB(fs); i++) {
1736 dp = DINO_IN_BLOCK(fs,
1737 (*bpp)->b_data, i);
1738 if (lfs_dino_getinumber(fs, dp))
1739 printf("%ju ",
1740 (uintmax_t)lfs_dino_getinumber(fs, dp));
1741 }
1742 printf("\n");
1743 }
1744 }
1745 }
1746 mutex_exit(&lfs_lock);
1747 #endif /* DEBUG */
1748 panic("lfs_loadvnode: dinode not found");
1749 }
1750 lfs_copy_dinode(fs, ip->i_din, dip);
1751 brelse(bp, 0);
1752
1753 out:
1754 if (lfs_sb_getversion(fs) > 1) {
1755 lfs_dino_setatime(fs, ip->i_din, ts.tv_sec);
1756 lfs_dino_setatimensec(fs, ip->i_din, ts.tv_nsec);
1757 }
1758
1759 lfs_vinit(mp, &vp);
1760
1761 *new_key = &ip->i_number;
1762 return 0;
1763 }
1764
1765 /*
1766 * Create a new inode and initialize this vnode / inode pair.
1767 */
1768 int
1769 lfs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp,
1770 struct vattr *vap, kauth_cred_t cred, void *extra,
1771 size_t *key_len, const void **new_key)
1772 {
1773 ino_t ino;
1774 struct inode *ip;
1775 struct ulfsmount *ump;
1776 struct lfs *fs;
1777 int error, mode, gen;
1778
1779 KASSERT(dvp != NULL || vap->va_fileid > 0);
1780 KASSERT(dvp != NULL && dvp->v_mount == mp);
1781 KASSERT(vap->va_type != VNON);
1782
1783 *key_len = sizeof(ino);
1784 ump = VFSTOULFS(mp);
1785 fs = ump->um_lfs;
1786 mode = MAKEIMODE(vap->va_type, vap->va_mode);
1787
1788 /*
1789 * Allocate fresh inode. With "dvp == NULL" take the inode number
1790 * and version from "vap".
1791 */
1792 if (dvp == NULL) {
1793 ino = vap->va_fileid;
1794 gen = vap->va_gen;
1795 error = lfs_valloc_fixed(fs, ino, gen);
1796 } else {
1797 error = lfs_valloc(dvp, mode, cred, &ino, &gen);
1798 }
1799 if (error)
1800 return error;
1801
1802 /* Attach inode to vnode. */
1803 lfs_init_vnode(ump, ino, vp);
1804 ip = VTOI(vp);
1805
1806 mutex_enter(&lfs_lock);
1807 LFS_SET_UINO(ip, IN_CHANGE);
1808 mutex_exit(&lfs_lock);
1809
1810 /* Note no blocks yet */
1811 ip->i_lfs_hiblk = -1;
1812
1813 /* Set a new generation number for this inode. */
1814 ip->i_gen = gen;
1815 lfs_dino_setgen(fs, ip->i_din, gen);
1816
1817 memset(ip->i_lfs_fragsize, 0,
1818 ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
1819
1820 /* Set uid / gid. */
1821 if (cred == NOCRED || cred == FSCRED) {
1822 ip->i_gid = 0;
1823 ip->i_uid = 0;
1824 } else {
1825 ip->i_gid = VTOI(dvp)->i_gid;
1826 ip->i_uid = kauth_cred_geteuid(cred);
1827 }
1828 DIP_ASSIGN(ip, gid, ip->i_gid);
1829 DIP_ASSIGN(ip, uid, ip->i_uid);
1830
1831 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
1832 error = lfs_chkiq(ip, 1, cred, 0);
1833 if (error) {
1834 lfs_vfree(dvp, ino, mode);
1835 lfs_deinit_vnode(ump, vp);
1836
1837 return error;
1838 }
1839 #endif
1840
1841 /* Set type and finalize. */
1842 ip->i_flags = 0;
1843 DIP_ASSIGN(ip, flags, 0);
1844 ip->i_mode = mode;
1845 DIP_ASSIGN(ip, mode, mode);
1846 if (vap->va_rdev != VNOVAL) {
1847 /*
1848 * Want to be able to use this to make badblock
1849 * inodes, so don't truncate the dev number.
1850 */
1851 // XXX clean this up
1852 if (ump->um_fstype == ULFS1)
1853 ip->i_din->u_32.di_rdev = ulfs_rw32(vap->va_rdev,
1854 ULFS_MPNEEDSWAP(fs));
1855 else
1856 ip->i_din->u_64.di_rdev = ulfs_rw64(vap->va_rdev,
1857 ULFS_MPNEEDSWAP(fs));
1858 }
1859 lfs_vinit(mp, &vp);
1860
1861 *new_key = &ip->i_number;
1862 return 0;
1863 }
1864
1865 /*
1866 * File handle to vnode
1867 */
1868 int
1869 lfs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp)
1870 {
1871 struct lfid lfh;
1872 struct lfs *fs;
1873
1874 if (fhp->fid_len != sizeof(struct lfid))
1875 return EINVAL;
1876
1877 memcpy(&lfh, fhp, sizeof(lfh));
1878 if (lfh.lfid_ino < LFS_IFILE_INUM)
1879 return ESTALE;
1880
1881 fs = VFSTOULFS(mp)->um_lfs;
1882 if (lfh.lfid_ident != lfs_sb_getident(fs))
1883 return ESTALE;
1884
1885 if (lfh.lfid_ino >
1886 ((lfs_dino_getsize(fs, VTOI(fs->lfs_ivnode)->i_din) >> lfs_sb_getbshift(fs)) -
1887 lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs))
1888 return ESTALE;
1889
1890 return (ulfs_fhtovp(mp, &lfh.lfid_ufid, lktype, vpp));
1891 }
1892
1893 /*
1894 * Vnode pointer to File handle
1895 */
1896 /* ARGSUSED */
1897 int
1898 lfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1899 {
1900 struct inode *ip;
1901 struct lfid lfh;
1902
1903 if (*fh_size < sizeof(struct lfid)) {
1904 *fh_size = sizeof(struct lfid);
1905 return E2BIG;
1906 }
1907 *fh_size = sizeof(struct lfid);
1908 ip = VTOI(vp);
1909 memset(&lfh, 0, sizeof(lfh));
1910 lfh.lfid_len = sizeof(struct lfid);
1911 lfh.lfid_ino = ip->i_number;
1912 lfh.lfid_gen = ip->i_gen;
1913 lfh.lfid_ident = lfs_sb_getident(ip->i_lfs);
1914 memcpy(fhp, &lfh, sizeof(lfh));
1915 return (0);
1916 }
1917
1918 /*
1919 * ulfs_bmaparray callback function for writing.
1920 *
1921 * Since blocks will be written to the new segment anyway,
1922 * we don't care about current daddr of them.
1923 */
1924 static bool
1925 lfs_issequential_hole(const struct lfs *fs,
1926 daddr_t daddr0, daddr_t daddr1)
1927 {
1928 (void)fs; /* not used */
1929
1930 KASSERT(daddr0 == UNWRITTEN ||
1931 (0 <= daddr0 && daddr0 <= LFS_MAX_DADDR(fs)));
1932 KASSERT(daddr1 == UNWRITTEN ||
1933 (0 <= daddr1 && daddr1 <= LFS_MAX_DADDR(fs)));
1934
1935 /* NOTE: all we want to know here is 'hole or not'. */
1936 /* NOTE: UNASSIGNED is converted to 0 by ulfs_bmaparray. */
1937
1938 /*
1939 * treat UNWRITTENs and all resident blocks as 'contiguous'
1940 */
1941 if (daddr0 != 0 && daddr1 != 0)
1942 return true;
1943
1944 /*
1945 * both are in hole?
1946 */
1947 if (daddr0 == 0 && daddr1 == 0)
1948 return true; /* all holes are 'contiguous' for us. */
1949
1950 return false;
1951 }
1952
1953 /*
1954 * lfs_gop_write functions exactly like genfs_gop_write, except that
1955 * (1) it requires the seglock to be held by its caller, and sp->fip
1956 * to be properly initialized (it will return without re-initializing
1957 * sp->fip, and without calling lfs_writeseg).
1958 * (2) it uses the remaining space in the segment, rather than VOP_BMAP,
1959 * to determine how large a block it can write at once (though it does
1960 * still use VOP_BMAP to find holes in the file);
1961 * (3) it calls lfs_gatherblock instead of VOP_STRATEGY on its blocks
1962 * (leaving lfs_writeseg to deal with the cluster blocks, so we might
1963 * now have clusters of clusters, ick.)
1964 */
1965 static int
1966 lfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
1967 int flags)
1968 {
1969 int i, error, run, haveeof = 0;
1970 int fs_bshift;
1971 vaddr_t kva;
1972 off_t eof, offset, startoffset = 0;
1973 size_t bytes, iobytes, skipbytes;
1974 bool async = (flags & PGO_SYNCIO) == 0;
1975 daddr_t lbn, blkno;
1976 struct vm_page *pg;
1977 struct buf *mbp, *bp;
1978 struct vnode *devvp = VTOI(vp)->i_devvp;
1979 struct inode *ip = VTOI(vp);
1980 struct lfs *fs = ip->i_lfs;
1981 struct segment *sp = fs->lfs_sp;
1982 SEGSUM *ssp;
1983 UVMHIST_FUNC("lfs_gop_write"); UVMHIST_CALLED(ubchist);
1984 const char * failreason = NULL;
1985
1986 ASSERT_SEGLOCK(fs);
1987
1988 /* The Ifile lives in the buffer cache */
1989 KASSERT(vp != fs->lfs_ivnode);
1990
1991 /*
1992 * We don't want to fill the disk before the cleaner has a chance
1993 * to make room for us. If we're in danger of doing that, fail
1994 * with EAGAIN. The caller will have to notice this, unlock
1995 * so the cleaner can run, relock and try again.
1996 *
1997 * We must write everything, however, if our vnode is being
1998 * reclaimed.
1999 */
2000 mutex_enter(vp->v_interlock);
2001 if (LFS_STARVED_FOR_SEGS(fs) && vdead_check(vp, VDEAD_NOWAIT) == 0) {
2002 mutex_exit(vp->v_interlock);
2003 failreason = "Starved for segs and not flushing vp";
2004 goto tryagain;
2005 }
2006 mutex_exit(vp->v_interlock);
2007
2008 /*
2009 * Sometimes things slip past the filters in lfs_putpages,
2010 * and the pagedaemon tries to write pages---problem is
2011 * that the pagedaemon never acquires the segment lock.
2012 *
2013 * Alternatively, pages that were clean when we called
2014 * genfs_putpages may have become dirty in the meantime. In this
2015 * case the segment header is not properly set up for blocks
2016 * to be added to it.
2017 *
2018 * Unbusy and unclean the pages, and put them on the ACTIVE
2019 * queue under the hypothesis that they couldn't have got here
2020 * unless they were modified *quite* recently.
2021 *
2022 * XXXUBC that last statement is an oversimplification of course.
2023 */
2024 if (!LFS_SEGLOCK_HELD(fs)) {
2025 failreason = "Seglock not held";
2026 goto tryagain;
2027 }
2028 if (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) {
2029 failreason = "Inode with no_gop_write";
2030 goto tryagain;
2031 }
2032 if ((pgs[0]->offset & lfs_sb_getbmask(fs)) != 0) {
2033 failreason = "Bad page offset";
2034 goto tryagain;
2035 }
2036
2037 UVMHIST_LOG(ubchist, "vp %#jx pgs %#jx npages %jd flags 0x%jx",
2038 (uintptr_t)vp, (uintptr_t)pgs, npages, flags);
2039
2040 GOP_SIZE(vp, vp->v_size, &eof, 0);
2041 haveeof = 1;
2042
2043 if (vp->v_type == VREG)
2044 fs_bshift = vp->v_mount->mnt_fs_bshift;
2045 else
2046 fs_bshift = DEV_BSHIFT;
2047 error = 0;
2048 pg = pgs[0];
2049 startoffset = pg->offset;
2050 KASSERT(eof >= 0);
2051
2052 if (startoffset >= eof) {
2053 failreason = "Offset beyond EOF";
2054 goto tryagain;
2055 } else
2056 bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
2057 skipbytes = 0;
2058
2059 KASSERT(bytes != 0);
2060
2061 /* Swap PG_DELWRI for PG_PAGEOUT */
2062 for (i = 0; i < npages; i++) {
2063 if (pgs[i]->flags & PG_DELWRI) {
2064 KASSERT(!(pgs[i]->flags & PG_PAGEOUT));
2065 pgs[i]->flags &= ~PG_DELWRI;
2066 pgs[i]->flags |= PG_PAGEOUT;
2067 uvm_pageout_start(1);
2068 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
2069 uvm_pagelock(pgs[i]);
2070 uvm_pageunwire(pgs[i]);
2071 uvm_pageunlock(pgs[i]);
2072 rw_exit(vp->v_uobj.vmobjlock);
2073 }
2074 }
2075
2076 /*
2077 * Check to make sure we're starting on a block boundary.
2078 * We'll check later to make sure we always write entire
2079 * blocks (or fragments).
2080 */
2081 if (startoffset & lfs_sb_getbmask(fs))
2082 printf("%" PRId64 " & %" PRIu64 " = %" PRId64 "\n",
2083 startoffset, lfs_sb_getbmask(fs),
2084 startoffset & lfs_sb_getbmask(fs));
2085 KASSERT((startoffset & lfs_sb_getbmask(fs)) == 0);
2086 if (bytes & lfs_sb_getffmask(fs)) {
2087 printf("lfs_gop_write: asked to write %ld bytes\n", (long)bytes);
2088 panic("lfs_gop_write: non-integer blocks");
2089 }
2090
2091 /*
2092 * We could deadlock here on pager_map with UVMPAGER_MAPIN_WAITOK.
2093 * If we would, write what we have and try again. If we don't
2094 * have anything to write, we'll have to sleep.
2095 */
2096 ssp = (SEGSUM *)sp->segsum;
2097 if ((kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
2098 (lfs_ss_getnfinfo(fs, ssp) < 1 ?
2099 UVMPAGER_MAPIN_WAITOK : 0))) == 0x0) {
2100 DLOG((DLOG_PAGE, "lfs_gop_write: forcing write\n"));
2101 #if 0
2102 " with nfinfo=%d at offset 0x%jx\n",
2103 (int)lfs_ss_getnfinfo(fs, ssp),
2104 (uintmax_t)lfs_sb_getoffset(fs)));
2105 #endif
2106 lfs_updatemeta(sp);
2107 lfs_release_finfo(fs);
2108 (void) lfs_writeseg(fs, sp);
2109
2110 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
2111
2112 /*
2113 * Having given up all of the pager_map we were holding,
2114 * we can now wait for aiodoned to reclaim it for us
2115 * without fear of deadlock.
2116 */
2117 kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
2118 UVMPAGER_MAPIN_WAITOK);
2119 }
2120
2121 mbp = getiobuf(NULL, true);
2122 UVMHIST_LOG(ubchist, "vp %#jx mbp %#jx num now %jd bytes 0x%jx",
2123 (uintptr_t)vp, (uintptr_t)mbp, vp->v_numoutput, bytes);
2124 mbp->b_bufsize = npages << PAGE_SHIFT;
2125 mbp->b_data = (void *)kva;
2126 mbp->b_resid = mbp->b_bcount = bytes;
2127 mbp->b_cflags |= BC_BUSY|BC_AGE;
2128 mbp->b_iodone = uvm_aio_aiodone;
2129
2130 bp = NULL;
2131 for (offset = startoffset;
2132 bytes > 0;
2133 offset += iobytes, bytes -= iobytes) {
2134 lbn = offset >> fs_bshift;
2135 error = ulfs_bmaparray(vp, lbn, &blkno, NULL, NULL, &run,
2136 lfs_issequential_hole);
2137 if (error) {
2138 UVMHIST_LOG(ubchist, "ulfs_bmaparray() -> %jd",
2139 error,0,0,0);
2140 skipbytes += bytes;
2141 bytes = 0;
2142 break;
2143 }
2144
2145 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
2146 bytes);
2147 if (blkno == (daddr_t)-1) {
2148 skipbytes += iobytes;
2149 continue;
2150 }
2151
2152 /*
2153 * Discover how much we can really pack into this buffer.
2154 */
2155 /* If no room in the current segment, finish it up */
2156 if (sp->sum_bytes_left < sizeof(int32_t) ||
2157 sp->seg_bytes_left < (1 << lfs_sb_getbshift(fs))) {
2158 int vers;
2159
2160 lfs_updatemeta(sp);
2161 vers = lfs_fi_getversion(fs, sp->fip);
2162 lfs_release_finfo(fs);
2163 (void) lfs_writeseg(fs, sp);
2164
2165 lfs_acquire_finfo(fs, ip->i_number, vers);
2166 }
2167 /* Check both for space in segment and space in segsum */
2168 iobytes = MIN(iobytes, (sp->seg_bytes_left >> fs_bshift)
2169 << fs_bshift);
2170 iobytes = MIN(iobytes, (sp->sum_bytes_left / sizeof(int32_t))
2171 << fs_bshift);
2172 KASSERT(iobytes > 0);
2173
2174 /* if it's really one i/o, don't make a second buf */
2175 if (offset == startoffset && iobytes == bytes) {
2176 bp = mbp;
2177 /*
2178 * All the LFS output is done by the segwriter. It
2179 * will increment numoutput by one for all the bufs it
2180 * receives. However this buffer needs one extra to
2181 * account for aiodone.
2182 */
2183 mutex_enter(vp->v_interlock);
2184 vp->v_numoutput++;
2185 mutex_exit(vp->v_interlock);
2186 } else {
2187 bp = getiobuf(NULL, true);
2188 UVMHIST_LOG(ubchist, "vp %#jx bp %#jx num now %jd",
2189 (uintptr_t)vp, (uintptr_t)bp, vp->v_numoutput, 0);
2190 nestiobuf_setup(mbp, bp, offset - pg->offset, iobytes);
2191 /*
2192 * LFS doesn't like async I/O here, dies with
2193 * an assert in lfs_bwrite(). Is that assert
2194 * valid? I retained non-async behaviour when
2195 * converted this to use nestiobuf --pooka
2196 */
2197 bp->b_flags &= ~B_ASYNC;
2198 }
2199
2200 /* XXX This is silly ... is this necessary? */
2201 mutex_enter(&bufcache_lock);
2202 mutex_enter(vp->v_interlock);
2203 bgetvp(vp, bp);
2204 mutex_exit(vp->v_interlock);
2205 mutex_exit(&bufcache_lock);
2206
2207 bp->b_lblkno = lfs_lblkno(fs, offset);
2208 bp->b_private = mbp;
2209 if (devvp->v_type == VBLK) {
2210 bp->b_dev = devvp->v_rdev;
2211 }
2212 VOP_BWRITE(bp->b_vp, bp);
2213 while (lfs_gatherblock(sp, bp, NULL))
2214 continue;
2215 }
2216
2217 nestiobuf_done(mbp, skipbytes, error);
2218 if (skipbytes) {
2219 UVMHIST_LOG(ubchist, "skipbytes %jd", skipbytes, 0,0,0);
2220 }
2221 UVMHIST_LOG(ubchist, "returning 0", 0,0,0,0);
2222
2223 if (!async) {
2224 /* Start a segment write. */
2225 UVMHIST_LOG(ubchist, "flushing", 0,0,0,0);
2226 mutex_enter(&lfs_lock);
2227 lfs_flush(fs, 0, 1);
2228 mutex_exit(&lfs_lock);
2229 }
2230
2231 if ((sp->seg_flags & SEGM_SINGLE) && lfs_sb_getcurseg(fs) != fs->lfs_startseg)
2232 return EAGAIN;
2233
2234 return (0);
2235
2236 tryagain:
2237 /*
2238 * We can't write the pages, for whatever reason.
2239 * Clean up after ourselves, and make the caller try again.
2240 */
2241 mutex_enter(vp->v_interlock);
2242
2243 /* Tell why we're here, if we know */
2244 if (failreason != NULL) {
2245 DLOG((DLOG_PAGE, "lfs_gop_write: %s\n", failreason));
2246 }
2247 if (haveeof && startoffset >= eof) {
2248 DLOG((DLOG_PAGE, "lfs_gop_write: ino %d start 0x%" PRIx64
2249 " eof 0x%" PRIx64 " npages=%d\n", VTOI(vp)->i_number,
2250 pgs[0]->offset, eof, npages));
2251 }
2252
2253 for (i = 0; i < npages; i++) {
2254 pg = pgs[i];
2255
2256 if (pg->flags & PG_PAGEOUT)
2257 uvm_pageout_done(1);
2258 uvm_pagelock(pg);
2259 if (pg->flags & PG_DELWRI) {
2260 uvm_pageunwire(pg);
2261 }
2262 uvm_pageactivate(pg);
2263 uvm_pageunlock(pg);
2264 pg->flags &= ~(PG_DELWRI|PG_PAGEOUT|PG_RELEASED);
2265 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
2266 DLOG((DLOG_PAGE, "pg[%d] = %p (vp %p off %" PRIx64 ")\n", i, pg,
2267 vp, pg->offset));
2268 DLOG((DLOG_PAGE, "pg[%d]->flags = %x\n", i, pg->flags));
2269 DLOG((DLOG_PAGE, "pg[%d]->pqflags = %x\n", i, pg->pqflags));
2270 DLOG((DLOG_PAGE, "pg[%d]->uanon = %p\n", i, pg->uanon));
2271 DLOG((DLOG_PAGE, "pg[%d]->uobject = %p\n", i, pg->uobject));
2272 DLOG((DLOG_PAGE, "pg[%d]->wire_count = %d\n", i,
2273 pg->wire_count));
2274 DLOG((DLOG_PAGE, "pg[%d]->loan_count = %d\n", i,
2275 pg->loan_count));
2276 }
2277 uvm_page_unbusy(pgs, npages);
2278 mutex_exit(vp->v_interlock);
2279 return EAGAIN;
2280 }
2281
2282 /*
2283 * finish vnode/inode initialization.
2284 * used by lfs_vget.
2285 */
2286 void
2287 lfs_vinit(struct mount *mp, struct vnode **vpp)
2288 {
2289 struct vnode *vp = *vpp;
2290 struct inode *ip = VTOI(vp);
2291 struct ulfsmount *ump = VFSTOULFS(mp);
2292 struct lfs *fs = ump->um_lfs;
2293 int i;
2294
2295 ip->i_mode = lfs_dino_getmode(fs, ip->i_din);
2296 ip->i_nlink = lfs_dino_getnlink(fs, ip->i_din);
2297 ip->i_lfs_osize = ip->i_size = lfs_dino_getsize(fs, ip->i_din);
2298 ip->i_flags = lfs_dino_getflags(fs, ip->i_din);
2299 ip->i_gen = lfs_dino_getgen(fs, ip->i_din);
2300 ip->i_uid = lfs_dino_getuid(fs, ip->i_din);
2301 ip->i_gid = lfs_dino_getgid(fs, ip->i_din);
2302
2303 ip->i_lfs_effnblks = lfs_dino_getblocks(fs, ip->i_din);
2304 ip->i_lfs_odnlink = lfs_dino_getnlink(fs, ip->i_din);
2305
2306 /*
2307 * Initialize the vnode from the inode, check for aliases. In all
2308 * cases re-init ip, the underlying vnode/inode may have changed.
2309 */
2310 ulfs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
2311 ip = VTOI(vp);
2312
2313 memset(ip->i_lfs_fragsize, 0, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
2314 if (vp->v_type != VLNK || ip->i_size >= ip->i_lfs->um_maxsymlinklen) {
2315 #ifdef DEBUG
2316 for (i = (ip->i_size + lfs_sb_getbsize(fs) - 1) >> lfs_sb_getbshift(fs);
2317 i < ULFS_NDADDR; i++) {
2318 if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
2319 i == 0)
2320 continue;
2321 if (lfs_dino_getdb(fs, ip->i_din, i) != 0) {
2322 lfs_dump_dinode(fs, ip->i_din);
2323 panic("inconsistent inode (direct)");
2324 }
2325 }
2326 for ( ; i < ULFS_NDADDR + ULFS_NIADDR; i++) {
2327 if (lfs_dino_getib(fs, ip->i_din, i - ULFS_NDADDR) != 0) {
2328 lfs_dump_dinode(fs, ip->i_din);
2329 panic("inconsistent inode (indirect)");
2330 }
2331 }
2332 #endif /* DEBUG */
2333 for (i = 0; i < ULFS_NDADDR; i++)
2334 if (lfs_dino_getdb(fs, ip->i_din, i) != 0)
2335 ip->i_lfs_fragsize[i] = lfs_blksize(fs, ip, i);
2336 }
2337
2338 KASSERTMSG((vp->v_type != VNON),
2339 "lfs_vinit: ino %llu is type VNON! (ifmt=%o)\n",
2340 (unsigned long long)ip->i_number,
2341 (ip->i_mode & LFS_IFMT) >> 12);
2342
2343 /*
2344 * Finish inode initialization now that aliasing has been resolved.
2345 */
2346
2347 ip->i_devvp = fs->lfs_devvp;
2348 vref(ip->i_devvp);
2349 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
2350 ulfsquota_init(ip);
2351 #endif
2352 genfs_node_init(vp, &lfs_genfsops);
2353 uvm_vnp_setsize(vp, ip->i_size);
2354
2355 /* Initialize hiblk from file size */
2356 ip->i_lfs_hiblk = lfs_lblkno(ip->i_lfs, ip->i_size + lfs_sb_getbsize(ip->i_lfs) - 1) - 1;
2357
2358 *vpp = vp;
2359 }
2360
2361 /*
2362 * Resize the filesystem to contain the specified number of segments.
2363 */
2364 int
2365 lfs_resize_fs(struct lfs *fs, int newnsegs)
2366 {
2367 SEGUSE *sup;
2368 CLEANERINFO *cip;
2369 struct buf *bp, *obp;
2370 daddr_t olast, nlast, ilast, noff, start, end;
2371 struct vnode *ivp;
2372 struct inode *ip;
2373 int error, badnews, inc, oldnsegs;
2374 int sbbytes, csbbytes, gain, cgain;
2375 int i;
2376
2377 /* Only support v2 and up */
2378 if (lfs_sb_getversion(fs) < 2)
2379 return EOPNOTSUPP;
2380
2381 /* If we're doing nothing, do it fast */
2382 oldnsegs = lfs_sb_getnseg(fs);
2383 if (newnsegs == oldnsegs)
2384 return 0;
2385
2386 /* We always have to have two superblocks */
2387 if (newnsegs <= lfs_dtosn(fs, lfs_sb_getsboff(fs, 1)))
2388 /* XXX this error code is rather nonsense */
2389 return EFBIG;
2390
2391 ivp = fs->lfs_ivnode;
2392 ip = VTOI(ivp);
2393 error = 0;
2394
2395 /* Take the segment lock so no one else calls lfs_newseg() */
2396 lfs_seglock(fs, SEGM_PROT);
2397
2398 /*
2399 * Make sure the segments we're going to be losing, if any,
2400 * are in fact empty. We hold the seglock, so their status
2401 * cannot change underneath us. Count the superblocks we lose,
2402 * while we're at it.
2403 */
2404 sbbytes = csbbytes = 0;
2405 cgain = 0;
2406 for (i = newnsegs; i < oldnsegs; i++) {
2407 LFS_SEGENTRY(sup, fs, i, bp);
2408 badnews = sup->su_nbytes || !(sup->su_flags & SEGUSE_INVAL);
2409 if (sup->su_flags & SEGUSE_SUPERBLOCK)
2410 sbbytes += LFS_SBPAD;
2411 if (!(sup->su_flags & SEGUSE_DIRTY)) {
2412 ++cgain;
2413 if (sup->su_flags & SEGUSE_SUPERBLOCK)
2414 csbbytes += LFS_SBPAD;
2415 }
2416 brelse(bp, 0);
2417 if (badnews) {
2418 error = EBUSY;
2419 goto out;
2420 }
2421 }
2422
2423 /* Note old and new segment table endpoints, and old ifile size */
2424 olast = lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs);
2425 nlast = howmany(newnsegs, lfs_sb_getsepb(fs)) + lfs_sb_getcleansz(fs);
2426 ilast = ivp->v_size >> lfs_sb_getbshift(fs);
2427 noff = nlast - olast;
2428
2429 /*
2430 * Make sure no one can use the Ifile while we change it around.
2431 * Even after taking the iflock we need to make sure no one still
2432 * is holding Ifile buffers, so we get each one, to drain them.
2433 * (XXX this could be done better.)
2434 */
2435 rw_enter(&fs->lfs_iflock, RW_WRITER);
2436 for (i = 0; i < ilast; i++) {
2437 /* XXX what to do if bread fails? */
2438 bread(ivp, i, lfs_sb_getbsize(fs), 0, &bp);
2439 brelse(bp, 0);
2440 }
2441
2442 /* Allocate new Ifile blocks */
2443 for (i = ilast; i < ilast + noff; i++) {
2444 if (lfs_balloc(ivp, i * lfs_sb_getbsize(fs), lfs_sb_getbsize(fs), NOCRED, 0,
2445 &bp) != 0)
2446 panic("balloc extending ifile");
2447 memset(bp->b_data, 0, lfs_sb_getbsize(fs));
2448 VOP_BWRITE(bp->b_vp, bp);
2449 }
2450
2451 /* Register new ifile size */
2452 ip->i_size += noff * lfs_sb_getbsize(fs);
2453 lfs_dino_setsize(fs, ip->i_din, ip->i_size);
2454 uvm_vnp_setsize(ivp, ip->i_size);
2455
2456 /* Copy the inode table to its new position */
2457 if (noff != 0) {
2458 if (noff < 0) {
2459 start = nlast;
2460 end = ilast + noff;
2461 inc = 1;
2462 } else {
2463 start = ilast + noff - 1;
2464 end = nlast - 1;
2465 inc = -1;
2466 }
2467 for (i = start; i != end; i += inc) {
2468 if (bread(ivp, i, lfs_sb_getbsize(fs),
2469 B_MODIFY, &bp) != 0)
2470 panic("resize: bread dst blk failed");
2471 if (bread(ivp, i - noff, lfs_sb_getbsize(fs),
2472 0, &obp))
2473 panic("resize: bread src blk failed");
2474 memcpy(bp->b_data, obp->b_data, lfs_sb_getbsize(fs));
2475 VOP_BWRITE(bp->b_vp, bp);
2476 brelse(obp, 0);
2477 }
2478 }
2479
2480 /* If we are expanding, write the new empty SEGUSE entries */
2481 if (newnsegs > oldnsegs) {
2482 for (i = oldnsegs; i < newnsegs; i++) {
2483 if ((error = bread(ivp, i / lfs_sb_getsepb(fs) +
2484 lfs_sb_getcleansz(fs), lfs_sb_getbsize(fs),
2485 B_MODIFY, &bp)) != 0)
2486 panic("lfs: ifile read: %d", error);
2487 while ((i + 1) % lfs_sb_getsepb(fs) && i < newnsegs) {
2488 sup = &((SEGUSE *)bp->b_data)[i % lfs_sb_getsepb(fs)];
2489 memset(sup, 0, sizeof(*sup));
2490 i++;
2491 }
2492 VOP_BWRITE(bp->b_vp, bp);
2493 }
2494 }
2495
2496 /* Zero out unused superblock offsets */
2497 for (i = 2; i < LFS_MAXNUMSB; i++)
2498 if (lfs_dtosn(fs, lfs_sb_getsboff(fs, i)) >= newnsegs)
2499 lfs_sb_setsboff(fs, i, 0x0);
2500
2501 /*
2502 * Correct superblock entries that depend on fs size.
2503 * The computations of these are as follows:
2504 *
2505 * size = lfs_segtod(fs, nseg)
2506 * dsize = lfs_segtod(fs, nseg - minfreeseg) - lfs_btofsb(#super * LFS_SBPAD)
2507 * bfree = dsize - lfs_btofsb(fs, bsize * nseg / 2) - blocks_actually_used
2508 * avail = lfs_segtod(fs, nclean) - lfs_btofsb(#clean_super * LFS_SBPAD)
2509 * + (lfs_segtod(fs, 1) - (offset - curseg))
2510 * - lfs_segtod(fs, minfreeseg - (minfreeseg / 2))
2511 *
2512 * XXX - we should probably adjust minfreeseg as well.
2513 */
2514 gain = (newnsegs - oldnsegs);
2515 lfs_sb_setnseg(fs, newnsegs);
2516 lfs_sb_setsegtabsz(fs, nlast - lfs_sb_getcleansz(fs));
2517 lfs_sb_addsize(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)));
2518 lfs_sb_adddsize(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)) - lfs_btofsb(fs, sbbytes));
2519 lfs_sb_addbfree(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)) - lfs_btofsb(fs, sbbytes)
2520 - gain * lfs_btofsb(fs, lfs_sb_getbsize(fs) / 2));
2521 if (gain > 0) {
2522 lfs_sb_addnclean(fs, gain);
2523 lfs_sb_addavail(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)));
2524 } else {
2525 lfs_sb_subnclean(fs, cgain);
2526 lfs_sb_subavail(fs, cgain * lfs_btofsb(fs, lfs_sb_getssize(fs)) -
2527 lfs_btofsb(fs, csbbytes));
2528 }
2529
2530 /* Resize segment flag cache */
2531 fs->lfs_suflags[0] = realloc(fs->lfs_suflags[0],
2532 lfs_sb_getnseg(fs) * sizeof(u_int32_t), M_SEGMENT, M_WAITOK);
2533 fs->lfs_suflags[1] = realloc(fs->lfs_suflags[1],
2534 lfs_sb_getnseg(fs) * sizeof(u_int32_t), M_SEGMENT, M_WAITOK);
2535 for (i = oldnsegs; i < newnsegs; i++)
2536 fs->lfs_suflags[0][i] = fs->lfs_suflags[1][i] = 0x0;
2537
2538 /* Truncate Ifile if necessary */
2539 if (noff < 0)
2540 lfs_truncate(ivp, ivp->v_size + (noff << lfs_sb_getbshift(fs)), 0,
2541 NOCRED);
2542
2543 /* Update cleaner info so the cleaner can die */
2544 /* XXX what to do if bread fails? */
2545 bread(ivp, 0, lfs_sb_getbsize(fs), B_MODIFY, &bp);
2546 cip = bp->b_data;
2547 lfs_ci_setclean(fs, cip, lfs_sb_getnclean(fs));
2548 lfs_ci_setdirty(fs, cip, lfs_sb_getnseg(fs) - lfs_sb_getnclean(fs));
2549 VOP_BWRITE(bp->b_vp, bp);
2550
2551 /* Let Ifile accesses proceed */
2552 rw_exit(&fs->lfs_iflock);
2553
2554 out:
2555 lfs_segunlock(fs);
2556 return error;
2557 }
2558
2559 /*
2560 * Extended attribute dispatch
2561 */
2562 int
2563 lfs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
2564 int attrnamespace, const char *attrname)
2565 {
2566 #ifdef LFS_EXTATTR
2567 struct ulfsmount *ump;
2568
2569 ump = VFSTOULFS(mp);
2570 if (ump->um_fstype == ULFS1) {
2571 return ulfs_extattrctl(mp, cmd, vp, attrnamespace, attrname);
2572 }
2573 #endif
2574 return vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname);
2575 }
2576