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