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