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