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