1 /* $NetBSD: lfs_vfsops.c,v 1.399 2026/01/05 05:02:47 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.399 2026/01/05 05:02:47 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, hz/10 + 1); 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; 917 kauth_cred_t cred; 918 CLEANERINFO *cip; 919 SEGUSE *sup; 920 daddr_t sb_addr; 921 ino_t *orphan; 922 size_t norphan; 923 924 cred = l ? l->l_cred : NOCRED; 925 926 /* The superblock is supposed to be 512 bytes. */ 927 __CTASSERT(sizeof(struct dlfs) == DEV_BSIZE); 928 929 /* 930 * Flush out any old buffers remaining from a previous use. 931 */ 932 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 933 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0); 934 VOP_UNLOCK(devvp); 935 if (error) 936 return (error); 937 938 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 939 940 /* Don't free random space on error. */ 941 primarybuf = NULL; 942 altbuf = NULL; 943 ump = NULL; 944 945 sb_addr = LFS_LABELPAD / DEV_BSIZE; 946 while (1) { 947 /* 948 * Read in the superblock. 949 * 950 * Note that because LFS_SBPAD is substantially larger 951 * (8K) than the actual on-disk superblock (512 bytes) 952 * the buffer contains enough space to be used as a 953 * whole struct lfs (in-memory superblock) - we do this 954 * only so we can set and use the is64 and dobyteswap 955 * members. XXX this is gross and the logic here should 956 * be reworked. 957 */ 958 error = bread(devvp, sb_addr, LFS_SBPAD, 0, &primarybuf); 959 if (error) 960 goto out; 961 primarysb = (struct lfs *)primarybuf->b_data; 962 963 /* Check the basics. */ 964 error = lfs_checkmagic(primarysb); 965 if (error) { 966 DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock wrong magic\n")); 967 goto out; 968 } 969 if (lfs_sb_getbsize(primarysb) > MAXBSIZE || 970 lfs_sb_getversion(primarysb) > LFS_VERSION || 971 lfs_sb_getbsize(primarysb) < sizeof(struct dlfs)) { 972 DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock sanity failed\n")); 973 /* XXX needs translation */ 974 error = EINVAL; 975 goto out; 976 } 977 if (lfs_sb_getinodefmt(primarysb) > LFS_MAXINODEFMT) { 978 DLOG((DLOG_MOUNT, "lfs_mountfs: unknown inode format %d\n", 979 lfs_sb_getinodefmt(primarysb))); 980 error = EINVAL; 981 goto out; 982 } 983 984 if (lfs_sb_getversion(primarysb) == 1) 985 fsbsize = DEV_BSIZE; 986 else { 987 fsbsize = 1 << lfs_sb_getffshift(primarysb); 988 /* 989 * Could be, if the frag size is large enough, that we 990 * don't have the "real" primary superblock. If that's 991 * the case, get the real one, and try again. 992 */ 993 if (sb_addr != (lfs_sb_getsboff(primarysb, 0) << (lfs_sb_getffshift(primarysb) - DEV_BSHIFT))) { 994 DLOG((DLOG_MOUNT, "lfs_mountfs: sb daddr" 995 " 0x%llx is not right, trying 0x%llx\n", 996 (long long)sb_addr, 997 (long long)(lfs_sb_getsboff(primarysb, 0) << (lfs_sb_getffshift(primarysb) - DEV_BSHIFT)))); 998 sb_addr = lfs_sb_getsboff(primarysb, 0) << (lfs_sb_getffshift(primarysb) - DEV_BSHIFT); 999 brelse(primarybuf, BC_INVAL); 1000 continue; 1001 } 1002 } 1003 break; 1004 } 1005 1006 /* 1007 * Check the second superblock to see which is newer; then mount 1008 * using the older of the two. This is necessary to ensure that 1009 * the filesystem is valid if it was not unmounted cleanly. 1010 */ 1011 1012 if (lfs_sb_getsboff(primarysb, 1) && 1013 lfs_sb_getsboff(primarysb, 1) - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize) 1014 { 1015 error = bread(devvp, lfs_sb_getsboff(primarysb, 1) * (fsbsize / DEV_BSIZE), 1016 LFS_SBPAD, 0, &altbuf); 1017 if (error) 1018 goto out; 1019 altsb = (struct lfs *)altbuf->b_data; 1020 1021 /* 1022 * Note: this used to do the sanity check only if the 1023 * timestamp/serial comparison required use of altsb; 1024 * this way is less tolerant, but if altsb is corrupted 1025 * enough that the magic number, version, and blocksize 1026 * are bogus, why would the timestamp or serial fields 1027 * mean anything either? If this kind of thing happens, 1028 * you need to fsck anyway. 1029 */ 1030 1031 error = lfs_checkmagic(altsb); 1032 if (error) 1033 goto out; 1034 1035 /* Check the basics. */ 1036 if (lfs_sb_getbsize(altsb) > MAXBSIZE || 1037 lfs_sb_getversion(altsb) > LFS_VERSION || 1038 lfs_sb_getbsize(altsb) < sizeof(struct dlfs)) { 1039 DLOG((DLOG_MOUNT, "lfs_mountfs: alt superblock" 1040 " sanity failed\n")); 1041 error = EINVAL; /* XXX needs translation */ 1042 goto out; 1043 } 1044 1045 if (lfs_sb_getversion(primarysb) == 1) { 1046 /* 1s resolution comparison */ 1047 if (lfs_sb_gettstamp(altsb) < lfs_sb_gettstamp(primarysb)) 1048 thesb = altsb; 1049 else 1050 thesb = primarysb; 1051 } else { 1052 /* monotonic infinite-resolution comparison */ 1053 if (lfs_sb_getserial(altsb) < lfs_sb_getserial(primarysb)) 1054 thesb = altsb; 1055 else 1056 thesb = primarysb; 1057 } 1058 } else { 1059 DLOG((DLOG_MOUNT, "lfs_mountfs: invalid alt superblock location" 1060 " daddr=0x%x\n", lfs_sb_getsboff(primarysb, 1))); 1061 error = EINVAL; 1062 goto out; 1063 } 1064 1065 /* 1066 * Allocate the mount structure, copy the superblock into it. 1067 * Note that the 32-bit and 64-bit superblocks are the same size. 1068 */ 1069 fs = kmem_zalloc(sizeof(struct lfs), KM_SLEEP); 1070 memcpy(&fs->lfs_dlfs_u.u_32, &thesb->lfs_dlfs_u.u_32, 1071 sizeof(struct dlfs)); 1072 fs->lfs_is64 = thesb->lfs_is64; 1073 fs->lfs_dobyteswap = thesb->lfs_dobyteswap; 1074 fs->lfs_hasolddirfmt = false; /* set for real below */ 1075 1076 /* Compatibility */ 1077 if (lfs_sb_getversion(fs) < 2) { 1078 lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE); 1079 lfs_sb_setibsize(fs, lfs_sb_getbsize(fs)); 1080 lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0)); 1081 lfs_sb_settstamp(fs, lfs_sb_getotstamp(fs)); 1082 lfs_sb_setfsbtodb(fs, 0); 1083 } 1084 if (lfs_sb_getresvseg(fs) == 0) 1085 lfs_sb_setresvseg(fs, MIN(lfs_sb_getminfreeseg(fs) - 1, \ 1086 MAX(MIN_RESV_SEGS, lfs_sb_getminfreeseg(fs) / 2 + 1))); 1087 1088 /* 1089 * If we aren't going to be able to write meaningfully to this 1090 * filesystem, and were not mounted readonly, bomb out now. 1091 */ 1092 if (lfs_fsbtob(fs, LFS_NRESERVE(fs)) > LFS_MAX_BYTES && !ronly) { 1093 DLOG((DLOG_MOUNT, "lfs_mount: to mount this filesystem read/write," 1094 " we need BUFPAGES >= %lld\n", 1095 (long long)((bufmem_hiwater / bufmem_lowater) * 1096 LFS_INVERSE_MAX_BYTES( 1097 lfs_fsbtob(fs, LFS_NRESERVE(fs))) >> PAGE_SHIFT))); 1098 kmem_free(fs, sizeof(struct lfs)); 1099 error = EFBIG; /* XXX needs translation */ 1100 goto out; 1101 } 1102 1103 /* Before rolling forward, lock so vget will sleep for other procs */ 1104 if (l != NULL) { 1105 fs->lfs_flags = LFS_NOTYET; 1106 fs->lfs_rfpid = l->l_proc->p_pid; 1107 } 1108 1109 ump = kmem_zalloc(sizeof(*ump), KM_SLEEP); 1110 ump->um_lfs = fs; 1111 ump->um_fstype = fs->lfs_is64 ? ULFS2 : ULFS1; 1112 /* ump->um_cleaner_thread = NULL; */ 1113 brelse(primarybuf, BC_INVAL); 1114 brelse(altbuf, BC_INVAL); 1115 primarybuf = NULL; 1116 altbuf = NULL; 1117 1118 1119 /* Set up the I/O information */ 1120 fs->lfs_devbsize = DEV_BSIZE; 1121 fs->lfs_iocount = 0; 1122 fs->lfs_diropwait = 0; 1123 fs->lfs_activesb = 0; 1124 lfs_sb_setuinodes(fs, 0); 1125 fs->lfs_ravail = 0; 1126 fs->lfs_favail = 0; 1127 fs->lfs_sbactive = 0; 1128 1129 /* Set up the ifile and lock aflags */ 1130 fs->lfs_doifile = 0; 1131 fs->lfs_writer = 0; 1132 fs->lfs_dirops = 0; 1133 fs->lfs_nadirop = 0; 1134 fs->lfs_prelock = 0; 1135 fs->lfs_seglock = 0; 1136 fs->lfs_cleanlock = 0; 1137 fs->lfs_pdflush = 0; 1138 fs->lfs_sleepers = 0; 1139 fs->lfs_pages = 0; 1140 fs->lfs_prelocklwp = NULL; 1141 rw_init(&fs->lfs_fraglock); 1142 rw_init(&fs->lfs_iflock); 1143 cv_init(&fs->lfs_sleeperscv, "lfs_slp"); 1144 cv_init(&fs->lfs_diropscv, "lfs_dirop"); 1145 cv_init(&fs->lfs_stopcv, "lfsstop"); 1146 cv_init(&fs->lfs_nextsegsleep, "segment"); 1147 cv_init(&fs->lfs_cleanercv, "cleancv"); 1148 cv_init(&fs->lfs_prelockcv, "prelockcv"); 1149 cv_init(&fs->lfs_cleanquitcv, "cleanquit"); 1150 1151 /* Set the file system readonly/modify bits. */ 1152 fs->lfs_ronly = ronly; 1153 if (ronly == 0) 1154 fs->lfs_fmod = 1; 1155 1156 /* Device we're using */ 1157 dev = devvp->v_rdev; 1158 fs->lfs_dev = dev; 1159 fs->lfs_devvp = devvp; 1160 1161 /* ulfs-level information */ 1162 fs->um_flags = 0; 1163 fs->um_bptrtodb = lfs_sb_getffshift(fs) - DEV_BSHIFT; 1164 fs->um_seqinc = lfs_sb_getfrag(fs); 1165 fs->um_nindir = lfs_sb_getnindir(fs); 1166 fs->um_lognindir = ffs(lfs_sb_getnindir(fs)) - 1; 1167 fs->um_maxsymlinklen = lfs_sb_getmaxsymlinklen(fs); 1168 fs->um_dirblksiz = LFS_DIRBLKSIZ; 1169 fs->um_maxfilesize = lfs_sb_getmaxfilesize(fs); 1170 1171 /* quota stuff */ 1172 /* XXX: these need to come from the on-disk superblock to be used */ 1173 fs->lfs_use_quota2 = 0; 1174 fs->lfs_quota_magic = 0; 1175 fs->lfs_quota_flags = 0; 1176 fs->lfs_quotaino[0] = 0; 1177 fs->lfs_quotaino[1] = 0; 1178 1179 /* Initialize the mount structure. */ 1180 mp->mnt_data = ump; 1181 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev; 1182 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_LFS); 1183 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 1184 mp->mnt_stat.f_namemax = LFS_MAXNAMLEN; 1185 mp->mnt_stat.f_iosize = lfs_sb_getbsize(fs); 1186 mp->mnt_flag |= MNT_LOCAL; 1187 mp->mnt_iflag |= IMNT_SHRLOOKUP; 1188 mp->mnt_fs_bshift = lfs_sb_getbshift(fs); 1189 mp->mnt_iflag |= IMNT_CAN_RWTORO; 1190 if (fs->um_maxsymlinklen > 0) 1191 mp->mnt_iflag |= IMNT_DTYPE; 1192 else 1193 fs->lfs_hasolddirfmt = true; 1194 1195 ump->um_mountp = mp; 1196 for (i = 0; i < ULFS_MAXQUOTAS; i++) 1197 ump->um_quotas[i] = NULLVP; 1198 spec_node_setmountedfs(devvp, mp); 1199 1200 /* Set up reserved memory for pageout */ 1201 lfs_setup_resblks(fs); 1202 /* Set up vdirop tailq */ 1203 TAILQ_INIT(&fs->lfs_dchainhd); 1204 /* and cleaning vnode tailq */ 1205 TAILQ_INIT(&fs->lfs_cleanhd); 1206 /* and paging tailq */ 1207 TAILQ_INIT(&fs->lfs_pchainhd); 1208 /* and delayed segment accounting for truncation list */ 1209 rb_tree_init(&fs->lfs_segdhd, &lfs_rbtree_ops); 1210 1211 /* 1212 * We use the ifile vnode for almost every operation. Instead of 1213 * retrieving it from the hash table each time we retrieve it here, 1214 * artificially increment the reference count and keep a pointer 1215 * to it in the incore copy of the superblock. 1216 */ 1217 if ((error = VFS_VGET(mp, LFS_IFILE_INUM, LK_EXCLUSIVE, &vp)) != 0) { 1218 DLOG((DLOG_MOUNT, "lfs_mountfs: ifile vget failed, error=%d\n", error)); 1219 goto out; 1220 } 1221 fs->lfs_ivnode = vp; 1222 vref(vp); 1223 1224 /* Set up inode bitmap, order free list, and gather orphans. */ 1225 lfs_order_freelist(fs, &orphan, &norphan); 1226 1227 /* Set up segment usage flags for the autocleaner. */ 1228 fs->lfs_nactive = 0; 1229 for (i = 0; i < lfs_sb_getnseg(fs); i++) { 1230 int changed; 1231 struct buf *bp; 1232 1233 LFS_SEGENTRY(sup, fs, i, bp); 1234 changed = 0; 1235 if (!ronly) { 1236 uint32_t nflags = sup->su_flags; 1237 nflags &= ~(SEGUSE_ACTIVE | SEGUSE_EMPTY 1238 | SEGUSE_READY | SEGUSE_INVAL 1239 | SEGUSE_ERROR); 1240 if (sup->su_nbytes == 0) { 1241 if (sup->su_flags & SEGUSE_DIRTY) 1242 nflags |= SEGUSE_EMPTY | SEGUSE_READY; 1243 } else { 1244 nflags &= ~(SEGUSE_EMPTY | SEGUSE_READY); 1245 } 1246 if (nflags != sup->su_flags) { 1247 sup->su_flags = nflags; 1248 ++changed; 1249 } 1250 } 1251 if (changed) 1252 LFS_WRITESEGENTRY(sup, fs, i, bp); 1253 else 1254 brelse(bp, 0); 1255 } 1256 /* Set lastcleaned to an invalid value */ 1257 fs->lfs_lastcleaned = (uint32_t)-1; 1258 1259 /* Free the orphans we discovered while ordering the freelist. */ 1260 lfs_free_orphans(fs, orphan, norphan); 1261 1262 if (!ronly) { 1263 /* Roll forward */ 1264 lfs_roll_forward(fs, mp, l); 1265 lfs_reset_avail(fs); 1266 } 1267 fs->lfs_rfpid = 0; 1268 1269 /* 1270 * XXX: if the fs has quotas, quotas should be on even if 1271 * readonly. Otherwise you can't query the quota info! 1272 * However, that's not how the quota2 code got written and I 1273 * don't know if it'll behave itself if enabled while 1274 * readonly, so for now use the same enable logic as ffs. 1275 * 1276 * XXX: also, if you use the -f behavior allowed here (and 1277 * equivalently above for remount) it will corrupt the fs. It 1278 * ought not to allow that. It should allow mounting readonly 1279 * if there are quotas and the kernel doesn't have the quota 1280 * code, but only readonly. 1281 * 1282 * XXX: and if you use the -f behavior allowed here it will 1283 * likely crash at unmount time (or remount time) because we 1284 * think quotas are active. 1285 * 1286 * Although none of this applies until there's a way to set 1287 * lfs_use_quota2 and have quotas in the fs at all. 1288 */ 1289 if (!ronly && fs->lfs_use_quota2) { 1290 #ifdef LFS_QUOTA2 1291 error = lfs_quota2_mount(mp); 1292 #else 1293 uprintf("%s: no kernel support for this filesystem's quotas\n", 1294 mp->mnt_stat.f_mntonname); 1295 if (mp->mnt_flag & MNT_FORCE) { 1296 uprintf("%s: mounting anyway; fsck afterwards\n", 1297 mp->mnt_stat.f_mntonname); 1298 } else { 1299 error = EINVAL; 1300 } 1301 #endif 1302 if (error) { 1303 /* XXX XXX must clean up the stuff immediately above */ 1304 printf("lfs_mountfs: sorry, leaking some memory\n"); 1305 goto out; 1306 } 1307 } 1308 1309 /* If writing, sb is not clean; record in case of immediate crash */ 1310 if (!fs->lfs_ronly) { 1311 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) & ~LFS_PF_CLEAN); 1312 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0)); 1313 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1)); 1314 } 1315 1316 /* Allow vget now that roll-forward is complete */ 1317 fs->lfs_flags &= ~(LFS_NOTYET); 1318 wakeup(&fs->lfs_flags); 1319 1320 /* 1321 * Initialize the ifile cleaner info with information from 1322 * the superblock. 1323 */ 1324 { 1325 struct buf *bp; 1326 1327 LFS_CLEANERINFO(cip, fs, bp); 1328 lfs_ci_setclean(fs, cip, lfs_sb_getnclean(fs)); 1329 lfs_ci_setdirty(fs, cip, lfs_sb_getnseg(fs) - lfs_sb_getnclean(fs)); 1330 lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs)); 1331 lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs)); 1332 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 1333 } 1334 1335 /* 1336 * Mark the current segment as ACTIVE, since we're going to 1337 * be writing to it. 1338 */ 1339 { 1340 struct buf *bp; 1341 1342 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getoffset(fs)), bp); 1343 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE; 1344 fs->lfs_nactive++; 1345 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getoffset(fs)), bp); /* Ifile */ 1346 } 1347 1348 /* Now that roll-forward is done, unlock the Ifile */ 1349 vput(vp); 1350 1351 /* Start the pagedaemon-anticipating daemon */ 1352 mutex_enter(&lfs_lock); 1353 if (lfs_writer_daemon == NULL && 1354 kthread_create(PRI_BIO, 0, NULL, 1355 lfs_writerd, NULL, NULL, "lfs_writer") != 0) 1356 panic("fork lfs_writer"); 1357 mutex_exit(&lfs_lock); 1358 1359 printf("WARNING: the log-structured file system is experimental\n" 1360 "WARNING: it may cause system crashes and/or corrupt data\n"); 1361 1362 return (0); 1363 1364 out: 1365 if (primarybuf) 1366 brelse(primarybuf, BC_INVAL); 1367 if (altbuf) 1368 brelse(altbuf, BC_INVAL); 1369 if (ump) { 1370 kmem_free(ump->um_lfs, sizeof(struct lfs)); 1371 kmem_free(ump, sizeof(*ump)); 1372 mp->mnt_data = NULL; 1373 } 1374 1375 return (error); 1376 } 1377 1378 void 1379 lfs_reset_avail(struct lfs *fs) 1380 { 1381 daddr_t avail; 1382 int sn, nclean, nsb, labelcorrect, curr, mfs; 1383 SEGUSE *sup; 1384 struct buf *bp; 1385 1386 KASSERT(!fs->lfs_ronly); 1387 1388 avail = nclean = nsb = labelcorrect = 0; 1389 for (sn = 0; sn < lfs_sb_getnseg(fs); ++sn) { 1390 LFS_SEGENTRY(sup, fs, sn, bp); 1391 1392 /* Count all clean segments and the remainder of this one */ 1393 if (!(sup->su_flags & SEGUSE_DIRTY)) { 1394 ++nclean; 1395 avail += lfs_segtod(fs, 1); 1396 1397 /* Correct for label and superblock, if present */ 1398 if (sup->su_flags & SEGUSE_SUPERBLOCK) 1399 ++nsb; 1400 if (sn == 0 && lfs_sb_getversion(fs) > 1 1401 && lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD)) 1402 labelcorrect = lfs_btofsb(fs, LFS_LABELPAD) 1403 - lfs_sb_gets0addr(fs); 1404 } 1405 1406 brelse(bp, 0); 1407 } 1408 1409 /* Also may be available bytes in current seg */ 1410 sn = lfs_dtosn(fs, lfs_sb_getoffset(fs)); 1411 curr = lfs_sntod(fs, sn + 1) - lfs_sb_getoffset(fs); 1412 /* But do not count minfreesegs */ 1413 mfs = lfs_segtod(fs, (lfs_sb_getminfreeseg(fs) - 1414 (lfs_sb_getminfreeseg(fs) / 2))); 1415 1416 avail = nclean * lfs_segtod(fs, 1); 1417 avail -= nsb * lfs_btofsb(fs, LFS_SBPAD); 1418 avail -= labelcorrect; 1419 avail += curr; 1420 avail -= mfs; 1421 1422 DLOG((DLOG_AVAIL, "avail := %jd*%jd-%jd*%jd-%jd+%jd-%jd=%jd\n", 1423 (intmax_t)nclean, 1424 (intmax_t)lfs_segtod(fs, 1), 1425 (intmax_t)nsb, 1426 (intmax_t)lfs_btofsb(fs, LFS_SBPAD), 1427 (intmax_t)labelcorrect, 1428 (intmax_t)curr, 1429 (intmax_t)mfs, 1430 (intmax_t)avail)); 1431 1432 lfs_sb_setavail(fs, avail); 1433 lfs_sb_setnclean(fs, nclean); 1434 } 1435 1436 /* 1437 * unmount system call 1438 */ 1439 int 1440 lfs_unmount(struct mount *mp, int mntflags) 1441 { 1442 struct ulfsmount *ump; 1443 struct lfs *fs; 1444 int error, ronly; 1445 1446 ump = VFSTOULFS(mp); 1447 fs = ump->um_lfs; 1448 1449 error = lfs_flushfiles(mp, mntflags & MNT_FORCE ? FORCECLOSE : 0); 1450 if (error) 1451 return error; 1452 1453 DEBUG_CHECK_FREELIST(fs); 1454 1455 /* Check for dirty blocks on ifile */ 1456 KASSERT(LIST_FIRST(&fs->lfs_ivnode->v_dirtyblkhd) == NULL); 1457 1458 /* Finish with the Ifile, now that we're done with it */ 1459 vgone(fs->lfs_ivnode); 1460 1461 ronly = !fs->lfs_ronly; 1462 if (fs->lfs_devvp->v_type != VBAD) 1463 spec_node_setmountedfs(fs->lfs_devvp, NULL); 1464 vn_lock(fs->lfs_devvp, LK_EXCLUSIVE | LK_RETRY); 1465 error = VOP_CLOSE(fs->lfs_devvp, 1466 ronly ? FREAD : FREAD|FWRITE, NOCRED); 1467 vput(fs->lfs_devvp); 1468 1469 /* Complain about page leakage */ 1470 if (fs->lfs_pages > 0) 1471 printf("lfs_unmount: still claim %d pages (%d in subsystem)\n", 1472 fs->lfs_pages, lfs_subsys_pages); 1473 1474 /* 1475 * Make doubly sure we have no outstanding I/O before we 1476 * call lfs_free_resblks(), to avoid a busy pool panic. 1477 */ 1478 mutex_enter(&lfs_lock); 1479 while (fs->lfs_iocount) 1480 mtsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount2", 0, 1481 &lfs_lock); 1482 mutex_exit(&lfs_lock); 1483 1484 /* Free per-mount data structures */ 1485 free(fs->lfs_ino_bitmap, M_SEGMENT); 1486 lfs_free_resblks(fs); 1487 cv_destroy(&fs->lfs_sleeperscv); 1488 cv_destroy(&fs->lfs_diropscv); 1489 cv_destroy(&fs->lfs_stopcv); 1490 cv_destroy(&fs->lfs_nextsegsleep); 1491 1492 rw_destroy(&fs->lfs_fraglock); 1493 rw_destroy(&fs->lfs_iflock); 1494 1495 kmem_free(fs, sizeof(struct lfs)); 1496 kmem_free(ump, sizeof(*ump)); 1497 1498 mp->mnt_data = NULL; 1499 mp->mnt_flag &= ~MNT_LOCAL; 1500 return (error); 1501 } 1502 1503 static int 1504 lfs_flushfiles(struct mount *mp, int flags) 1505 { 1506 struct lwp *l = curlwp; 1507 struct ulfsmount *ump; 1508 struct lfs *fs; 1509 struct vnode *vp; 1510 int error; 1511 1512 ump = VFSTOULFS(mp); 1513 fs = ump->um_lfs; 1514 1515 /* Two checkpoints */ 1516 if (!fs->lfs_ronly) { 1517 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC); 1518 lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC); 1519 } 1520 1521 /* wake up the cleaner so it can die */ 1522 /* XXX: shouldn't this be *after* the error cases below? */ 1523 lfs_wakeup_cleaner(fs); 1524 mutex_enter(&lfs_lock); 1525 while (fs->lfs_sleepers) 1526 cv_wait(&fs->lfs_sleeperscv, &lfs_lock); 1527 mutex_exit(&lfs_lock); 1528 1529 #ifdef LFS_EXTATTR 1530 if (ump->um_fstype == ULFS1) { 1531 if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED) { 1532 ulfs_extattr_stop(mp, curlwp); 1533 } 1534 if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED) { 1535 ulfs_extattr_uepm_destroy(&ump->um_extattr); 1536 mp->mnt_flag &= ~MNT_EXTATTR; 1537 } 1538 } 1539 #endif 1540 #ifdef LFS_QUOTA 1541 if ((error = lfsquota1_umount(mp, flags)) != 0) 1542 return (error); 1543 #endif 1544 #ifdef LFS_QUOTA2 1545 if ((error = lfsquota2_umount(mp, flags)) != 0) 1546 return (error); 1547 #endif 1548 if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0) 1549 return (error); 1550 if ((error = VFS_SYNC(mp, MNT_WAIT, l->l_cred)) != 0) 1551 return (error); 1552 vp = fs->lfs_ivnode; 1553 mutex_enter(vp->v_interlock); 1554 if (LIST_FIRST(&vp->v_dirtyblkhd)) 1555 panic("lfs_flushfiles: still dirty blocks on ifile vnode"); 1556 mutex_exit(vp->v_interlock); 1557 1558 /* Explicitly write the superblock, to update serial and pflags */ 1559 if (!fs->lfs_ronly) { 1560 lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) | LFS_PF_CLEAN); 1561 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0)); 1562 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1)); 1563 } 1564 mutex_enter(&lfs_lock); 1565 while (fs->lfs_iocount) 1566 mtsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0, 1567 &lfs_lock); 1568 mutex_exit(&lfs_lock); 1569 1570 return 0; 1571 } 1572 1573 /* 1574 * Get file system statistics. 1575 * 1576 * NB: We don't lock to access the superblock here, because it's not 1577 * really that important if we get it wrong. 1578 */ 1579 int 1580 lfs_statvfs(struct mount *mp, struct statvfs *sbp) 1581 { 1582 struct lfs *fs; 1583 struct ulfsmount *ump; 1584 1585 ump = VFSTOULFS(mp); 1586 fs = ump->um_lfs; 1587 1588 sbp->f_bsize = lfs_sb_getbsize(fs); 1589 sbp->f_frsize = lfs_sb_getfsize(fs); 1590 sbp->f_iosize = lfs_sb_getbsize(fs); 1591 sbp->f_blocks = LFS_EST_NONMETA(fs) - VTOI(fs->lfs_ivnode)->i_lfs_effnblks; 1592 1593 sbp->f_bfree = LFS_EST_BFREE(fs); 1594 /* 1595 * XXX this should be lfs_sb_getsize (measured in frags) 1596 * rather than dsize (measured in diskblocks). However, 1597 * getsize needs a format version check (for version 1 it 1598 * needs to be blockstofrags'd) so for the moment I'm going to 1599 * leave this... it won't fire wrongly as frags are at least 1600 * as big as diskblocks. 1601 */ 1602 KASSERT(sbp->f_bfree <= lfs_sb_getdsize(fs)); 1603 #if 0 1604 if (sbp->f_bfree < 0) 1605 sbp->f_bfree = 0; 1606 #endif 1607 1608 sbp->f_bresvd = LFS_EST_RSVD(fs); 1609 if (sbp->f_bfree > sbp->f_bresvd) 1610 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd; 1611 else 1612 sbp->f_bavail = 0; 1613 1614 /* XXX: huh? - dholland 20150728 */ 1615 sbp->f_files = lfs_sb_getbfree(fs) / lfs_btofsb(fs, lfs_sb_getibsize(fs)) 1616 * LFS_INOPB(fs); 1617 sbp->f_ffree = sbp->f_files - lfs_sb_getnfiles(fs); 1618 sbp->f_favail = sbp->f_ffree; 1619 sbp->f_fresvd = 0; 1620 copy_statvfs_info(sbp, mp); 1621 return (0); 1622 } 1623 1624 /* 1625 * Go through the disk queues to initiate sandbagged IO; 1626 * go through the inodes to write those that have been modified; 1627 * initiate the writing of the super block if it has been modified. 1628 */ 1629 int 1630 lfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred) 1631 { 1632 int error, segflags; 1633 struct lfs *fs; 1634 1635 fs = VFSTOULFS(mp)->um_lfs; 1636 if (fs->lfs_ronly) 1637 return 0; 1638 1639 /* Snapshots should not hose the syncer */ 1640 /* 1641 * XXX Sync can block here anyway, since we don't have a very 1642 * XXX good idea of how much data is pending. If it's more 1643 * XXX than a segment and lfs_nextseg is close to the end of 1644 * XXX the log, we'll likely block. 1645 */ 1646 mutex_enter(&lfs_lock); 1647 if (fs->lfs_nowrap && lfs_sb_getnextseg(fs) < lfs_sb_getcurseg(fs)) { 1648 mutex_exit(&lfs_lock); 1649 return 0; 1650 } 1651 mutex_exit(&lfs_lock); 1652 1653 lfs_writer_enter(fs, "lfs_wsync"); 1654 1655 DLOG((DLOG_FLUSH, "lfs_sync waitfor=%x at 0x%jx\n", waitfor, 1656 (uintmax_t)lfs_sb_getoffset(fs))); 1657 1658 segflags = 0; 1659 if (waitfor == MNT_LAZY) 1660 segflags = 0; 1661 else if (waitfor == MNT_NOWAIT) 1662 segflags = SEGM_CKP; 1663 else /* MNT_WAIT, or unknown value */ 1664 segflags = SEGM_SYNC | SEGM_CKP; 1665 1666 error = lfs_segwrite(mp, segflags); 1667 lfs_writer_leave(fs); 1668 #ifdef LFS_QUOTA 1669 lfs_qsync(mp); 1670 #endif 1671 return (error); 1672 } 1673 1674 /* 1675 * Look up an LFS dinode number to find its incore vnode. If not already 1676 * in core, read it in from the specified device. Return the inode locked. 1677 * Detection and handling of mount points must be done by the calling routine. 1678 */ 1679 int 1680 lfs_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp) 1681 { 1682 int error; 1683 1684 error = vcache_get(mp, &ino, sizeof(ino), vpp); 1685 if (error) 1686 return error; 1687 error = vn_lock(*vpp, lktype); 1688 if (error) { 1689 vrele(*vpp); 1690 *vpp = NULL; 1691 return error; 1692 } 1693 1694 return 0; 1695 } 1696 1697 static int 1698 compare_nodes_sd(void *context, const void *v1, const void *v2) 1699 { 1700 const struct segdelta *sd1, *sd2; 1701 1702 sd1 = (const struct segdelta *)v1; 1703 sd2 = (const struct segdelta *)v2; 1704 return sd1->segnum - sd2->segnum; 1705 } 1706 1707 static int 1708 compare_key_sd(void *context, const void *nv, const void *kv) 1709 { 1710 const struct segdelta *sd; 1711 long key; 1712 1713 sd = (const struct segdelta *)nv; 1714 key = *(const long *)kv; 1715 return sd->segnum - key; 1716 } 1717 1718 /* 1719 * Create a new vnode/inode pair and initialize what fields we can. 1720 */ 1721 static void 1722 lfs_init_vnode(struct ulfsmount *ump, ino_t ino, struct vnode *vp) 1723 { 1724 struct lfs *fs = ump->um_lfs; 1725 struct inode *ip; 1726 union lfs_dinode *dp; 1727 1728 ASSERT_NO_SEGLOCK(fs); 1729 1730 /* Initialize the inode. */ 1731 ip = pool_get(&lfs_inode_pool, PR_WAITOK); 1732 memset(ip, 0, sizeof(*ip)); 1733 dp = pool_get(&lfs_dinode_pool, PR_WAITOK); 1734 memset(dp, 0, sizeof(*dp)); 1735 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK); 1736 memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs)); 1737 ip->i_din = dp; 1738 ip->i_ump = ump; 1739 ip->i_vnode = vp; 1740 ip->i_dev = fs->lfs_dev; 1741 lfs_dino_setinumber(fs, dp, ino); 1742 ip->i_number = ino; 1743 ip->i_lfs = fs; 1744 ip->i_lfs_effnblks = 0; 1745 SPLAY_INIT(&ip->i_lfs_lbtree); 1746 ip->i_lfs_nbtree = 0; 1747 rb_tree_init(&ip->i_lfs_segdhd, &lfs_rbtree_ops); 1748 1749 vp->v_tag = VT_LFS; 1750 vp->v_op = lfs_vnodeop_p; 1751 vp->v_data = ip; 1752 } 1753 1754 /* 1755 * Undo lfs_init_vnode(). 1756 */ 1757 static void 1758 lfs_deinit_vnode(struct ulfsmount *ump, struct vnode *vp) 1759 { 1760 struct inode *ip = VTOI(vp); 1761 1762 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs); 1763 pool_put(&lfs_dinode_pool, ip->i_din); 1764 pool_put(&lfs_inode_pool, ip); 1765 vp->v_data = NULL; 1766 } 1767 1768 /* 1769 * Read an inode from disk and initialize this vnode / inode pair. 1770 * Caller assures no other thread will try to load this inode. 1771 */ 1772 int 1773 lfs_loadvnode(struct mount *mp, struct vnode *vp, 1774 const void *key, size_t key_len, const void **new_key) 1775 { 1776 struct lfs *fs; 1777 union lfs_dinode *dip; 1778 struct inode *ip; 1779 struct buf *bp; 1780 IFILE *ifp; 1781 struct ulfsmount *ump; 1782 ino_t ino; 1783 daddr_t daddr; 1784 int error, retries; 1785 struct timespec ts; 1786 1787 KASSERT(key_len == sizeof(ino)); 1788 memcpy(&ino, key, key_len); 1789 1790 memset(&ts, 0, sizeof ts); /* XXX gcc */ 1791 1792 ump = VFSTOULFS(mp); 1793 fs = ump->um_lfs; 1794 1795 /* 1796 * If the filesystem is not completely mounted yet, suspend 1797 * any access requests (wait for roll-forward to complete). 1798 */ 1799 mutex_enter(&lfs_lock); 1800 while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid) 1801 mtsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0, 1802 &lfs_lock); 1803 mutex_exit(&lfs_lock); 1804 1805 KASSERT(ino >= LFS_IFILE_INUM); 1806 /* LFS_ASSERT_MAXINO(fs, ino); */ 1807 1808 /* Translate the inode number to a disk address. */ 1809 if (ino == LFS_IFILE_INUM) 1810 daddr = lfs_sb_getidaddr(fs); 1811 else { 1812 LFS_IENTRY(ifp, fs, ino, bp); 1813 daddr = lfs_if_getdaddr(fs, ifp); 1814 if (lfs_sb_getversion(fs) > 1) { 1815 ts.tv_sec = lfs_if_getatime_sec(fs, ifp); 1816 ts.tv_nsec = lfs_if_getatime_nsec(fs, ifp); 1817 } 1818 1819 brelse(bp, 0); 1820 if (DADDR_IS_BAD(daddr)) 1821 return (ENOENT); 1822 } 1823 1824 /* Allocate/init new vnode/inode. */ 1825 lfs_init_vnode(ump, ino, vp); 1826 ip = VTOI(vp); 1827 1828 /* If the cleaner supplied the inode, use it. */ 1829 if (curlwp == fs->lfs_cleaner_thread && fs->lfs_cleaner_hint != NULL && 1830 fs->lfs_cleaner_hint->bi_lbn == LFS_UNUSED_LBN) { 1831 dip = fs->lfs_cleaner_hint->bi_bp; 1832 if (fs->lfs_is64) { 1833 error = copyin(dip, &ip->i_din->u_64, 1834 sizeof(struct lfs64_dinode)); 1835 } else { 1836 error = copyin(dip, &ip->i_din->u_32, 1837 sizeof(struct lfs32_dinode)); 1838 } 1839 if (error) { 1840 lfs_deinit_vnode(ump, vp); 1841 return error; 1842 } 1843 KASSERT(ip->i_number == ino); 1844 goto out; 1845 } 1846 1847 /* Read in the disk contents for the inode, copy into the inode. */ 1848 retries = 0; 1849 again: 1850 error = bread(fs->lfs_devvp, LFS_FSBTODB(fs, daddr), 1851 (lfs_sb_getversion(fs) == 1 ? lfs_sb_getbsize(fs) : lfs_sb_getibsize(fs)), 1852 0, &bp); 1853 if (error) { 1854 lfs_deinit_vnode(ump, vp); 1855 return error; 1856 } 1857 1858 dip = lfs_ifind(fs, ino, bp); 1859 if (dip == NULL) { 1860 /* Assume write has not completed yet; try again */ 1861 brelse(bp, BC_INVAL); 1862 ++retries; 1863 if (retries <= LFS_IFIND_RETRIES) { 1864 mutex_enter(&lfs_lock); 1865 if (fs->lfs_iocount) { 1866 DLOG((DLOG_VNODE, 1867 "%s: dinode %d not found, retrying...\n", 1868 __func__, ino)); 1869 (void)mtsleep(&fs->lfs_iocount, PRIBIO + 1, 1870 "lfs ifind", 1, &lfs_lock); 1871 } else 1872 retries = LFS_IFIND_RETRIES; 1873 mutex_exit(&lfs_lock); 1874 goto again; 1875 } 1876 #ifdef DEBUG 1877 /* If the seglock is held look at the bpp to see 1878 what is there anyway */ 1879 mutex_enter(&lfs_lock); 1880 if (fs->lfs_seglock > 0) { 1881 struct buf **bpp; 1882 union lfs_dinode *dp; 1883 int i; 1884 1885 for (bpp = fs->lfs_sp->bpp; 1886 bpp != fs->lfs_sp->cbpp; ++bpp) { 1887 if ((*bpp)->b_vp == fs->lfs_ivnode && 1888 bpp != fs->lfs_sp->bpp) { 1889 /* Inode block */ 1890 printf("%s: block 0x%" PRIx64 ": ", 1891 __func__, (*bpp)->b_blkno); 1892 for (i = 0; i < LFS_INOPB(fs); i++) { 1893 dp = DINO_IN_BLOCK(fs, 1894 (*bpp)->b_data, i); 1895 if (lfs_dino_getinumber(fs, dp)) 1896 printf("%ju ", 1897 (uintmax_t)lfs_dino_getinumber(fs, dp)); 1898 } 1899 printf("\n"); 1900 } 1901 } 1902 } 1903 mutex_exit(&lfs_lock); 1904 #endif /* DEBUG */ 1905 panic("lfs_loadvnode: dinode not found"); 1906 } 1907 lfs_copy_dinode(fs, ip->i_din, dip); 1908 brelse(bp, 0); 1909 1910 out: 1911 if (lfs_sb_getversion(fs) > 1) { 1912 lfs_dino_setatime(fs, ip->i_din, ts.tv_sec); 1913 lfs_dino_setatimensec(fs, ip->i_din, ts.tv_nsec); 1914 } 1915 1916 lfs_vinit(mp, &vp); 1917 1918 *new_key = &ip->i_number; 1919 return 0; 1920 } 1921 1922 /* 1923 * Create a new inode and initialize this vnode / inode pair. 1924 */ 1925 int 1926 lfs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp, 1927 struct vattr *vap, kauth_cred_t cred, void *extra, 1928 size_t *key_len, const void **new_key) 1929 { 1930 ino_t ino; 1931 struct inode *ip; 1932 struct ulfsmount *ump; 1933 struct lfs *fs; 1934 int error, mode, gen; 1935 1936 ump = VFSTOULFS(mp); 1937 fs = ump->um_lfs; 1938 1939 KASSERT(dvp != NULL || vap->va_fileid > 0); 1940 KASSERT((fs->lfs_flags & LFS_NOTYET) || 1941 (dvp != NULL && dvp->v_mount == mp)); 1942 KASSERT(vap->va_type != VNON); 1943 1944 *key_len = sizeof(ino); 1945 mode = MAKEIMODE(vap->va_type, vap->va_mode); 1946 1947 /* 1948 * Allocate fresh inode. With "dvp == NULL" take the inode number 1949 * and version from "vap". 1950 */ 1951 if (dvp == NULL) { 1952 ino = vap->va_fileid; 1953 gen = vap->va_gen; 1954 error = lfs_valloc_fixed(fs, ino, gen); 1955 } else { 1956 error = lfs_valloc(dvp, mode, cred, &ino, &gen); 1957 } 1958 if (error) 1959 return error; 1960 1961 /* Attach inode to vnode. */ 1962 lfs_init_vnode(ump, ino, vp); 1963 ip = VTOI(vp); 1964 1965 mutex_enter(&lfs_lock); 1966 LFS_SET_UINO(ip, IN_CHANGE); 1967 mutex_exit(&lfs_lock); 1968 1969 /* Note no blocks yet */ 1970 ip->i_lfs_hiblk = -1; 1971 1972 /* Set a new generation number for this inode. */ 1973 ip->i_gen = gen; 1974 lfs_dino_setgen(fs, ip->i_din, gen); 1975 1976 memset(ip->i_lfs_fragsize, 0, 1977 ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize)); 1978 1979 /* Set uid / gid. */ 1980 if (cred == NOCRED || cred == FSCRED) { 1981 ip->i_gid = 0; 1982 ip->i_uid = 0; 1983 } else { 1984 ip->i_gid = VTOI(dvp)->i_gid; 1985 ip->i_uid = kauth_cred_geteuid(cred); 1986 } 1987 DIP_ASSIGN(ip, gid, ip->i_gid); 1988 DIP_ASSIGN(ip, uid, ip->i_uid); 1989 1990 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2) 1991 error = lfs_chkiq(ip, 1, cred, 0); 1992 if (error) { 1993 lfs_vfree(dvp, ino, mode); 1994 lfs_deinit_vnode(ump, vp); 1995 1996 return error; 1997 } 1998 #endif 1999 2000 /* Set type and finalize. */ 2001 ip->i_flags = 0; 2002 DIP_ASSIGN(ip, flags, 0); 2003 ip->i_mode = mode; 2004 DIP_ASSIGN(ip, mode, mode); 2005 if (vap->va_rdev != VNOVAL) { 2006 /* 2007 * Want to be able to use this to make badblock 2008 * inodes, so don't truncate the dev number. 2009 */ 2010 // XXX clean this up 2011 if (ump->um_fstype == ULFS1) 2012 ip->i_din->u_32.di_rdev = ulfs_rw32(vap->va_rdev, 2013 ULFS_MPNEEDSWAP(fs)); 2014 else 2015 ip->i_din->u_64.di_rdev = ulfs_rw64(vap->va_rdev, 2016 ULFS_MPNEEDSWAP(fs)); 2017 } 2018 lfs_vinit(mp, &vp); 2019 2020 *new_key = &ip->i_number; 2021 return 0; 2022 } 2023 2024 /* 2025 * File handle to vnode 2026 */ 2027 int 2028 lfs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp) 2029 { 2030 struct lfid lfh; 2031 struct lfs *fs; 2032 2033 if (fhp->fid_len != sizeof(struct lfid)) 2034 return EINVAL; 2035 2036 memcpy(&lfh, fhp, sizeof(lfh)); 2037 if (lfh.lfid_ino < LFS_IFILE_INUM) 2038 return ESTALE; 2039 2040 fs = VFSTOULFS(mp)->um_lfs; 2041 if (lfh.lfid_ident != lfs_sb_getident(fs)) 2042 return ESTALE; 2043 2044 if (lfh.lfid_ino > 2045 ((lfs_dino_getsize(fs, VTOI(fs->lfs_ivnode)->i_din) >> lfs_sb_getbshift(fs)) - 2046 lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs)) 2047 return ESTALE; 2048 2049 return (ulfs_fhtovp(mp, &lfh.lfid_ufid, lktype, vpp)); 2050 } 2051 2052 /* 2053 * Vnode pointer to File handle 2054 */ 2055 /* ARGSUSED */ 2056 int 2057 lfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size) 2058 { 2059 struct inode *ip; 2060 struct lfid lfh; 2061 2062 if (*fh_size < sizeof(struct lfid)) { 2063 *fh_size = sizeof(struct lfid); 2064 return E2BIG; 2065 } 2066 *fh_size = sizeof(struct lfid); 2067 ip = VTOI(vp); 2068 memset(&lfh, 0, sizeof(lfh)); 2069 lfh.lfid_len = sizeof(struct lfid); 2070 lfh.lfid_ino = ip->i_number; 2071 lfh.lfid_gen = ip->i_gen; 2072 lfh.lfid_ident = lfs_sb_getident(ip->i_lfs); 2073 memcpy(fhp, &lfh, sizeof(lfh)); 2074 return (0); 2075 } 2076 2077 /* 2078 * ulfs_bmaparray callback function for writing. 2079 * 2080 * Since blocks will be written to the new segment anyway, 2081 * we don't care about current daddr of them. 2082 */ 2083 static bool 2084 lfs_issequential_hole(const struct lfs *fs, 2085 daddr_t daddr0, daddr_t daddr1) 2086 { 2087 (void)fs; /* not used */ 2088 2089 KASSERT(daddr0 == UNWRITTEN || 2090 (0 <= daddr0 && daddr0 <= LFS_MAX_DADDR(fs))); 2091 KASSERT(daddr1 == UNWRITTEN || 2092 (0 <= daddr1 && daddr1 <= LFS_MAX_DADDR(fs))); 2093 2094 /* NOTE: all we want to know here is 'hole or not'. */ 2095 /* NOTE: UNASSIGNED is converted to 0 by ulfs_bmaparray. */ 2096 2097 /* 2098 * treat UNWRITTENs and all resident blocks as 'contiguous' 2099 */ 2100 if (daddr0 != 0 && daddr1 != 0) 2101 return true; 2102 2103 /* 2104 * both are in hole? 2105 */ 2106 if (daddr0 == 0 && daddr1 == 0) 2107 return true; /* all holes are 'contiguous' for us. */ 2108 2109 return false; 2110 } 2111 2112 /* 2113 * lfs_gop_write functions exactly like genfs_gop_write, except that 2114 * (1) it requires the seglock to be held by its caller, and sp->fip 2115 * to be properly initialized (it will return without re-initializing 2116 * sp->fip, and without calling lfs_writeseg). 2117 * (2) it uses the remaining space in the segment, rather than VOP_BMAP, 2118 * to determine how large a block it can write at once (though it does 2119 * still use VOP_BMAP to find holes in the file); 2120 * (3) it calls lfs_gatherblock instead of VOP_STRATEGY on its blocks 2121 * (leaving lfs_writeseg to deal with the cluster blocks, so we might 2122 * now have clusters of clusters, ick.) 2123 */ 2124 static int 2125 lfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, 2126 int flags) 2127 { 2128 int i, error, run, haveeof = 0; 2129 int fs_bshift; 2130 vaddr_t kva; 2131 off_t eof, offset, startoffset = 0; 2132 size_t bytes, iobytes, skipbytes; 2133 bool async = (flags & PGO_SYNCIO) == 0; 2134 daddr_t lbn, blkno; 2135 struct vm_page *pg; 2136 struct buf *mbp, *bp; 2137 struct vnode *devvp = VTOI(vp)->i_devvp; 2138 struct inode *ip = VTOI(vp); 2139 struct lfs *fs = ip->i_lfs; 2140 struct segment *sp = fs->lfs_sp; 2141 SEGSUM *ssp; 2142 UVMHIST_FUNC("lfs_gop_write"); UVMHIST_CALLED(ubchist); 2143 const char * failreason = NULL; 2144 2145 ASSERT_SEGLOCK(fs); 2146 2147 /* The Ifile lives in the buffer cache */ 2148 KASSERT(vp != fs->lfs_ivnode); 2149 2150 /* 2151 * We don't want to fill the disk before the cleaner has a chance 2152 * to make room for us. If we're in danger of doing that, fail 2153 * with EAGAIN. The caller will have to notice this, unlock 2154 * so the cleaner can run, relock and try again. 2155 * 2156 * We must write everything, however, if our vnode is being 2157 * reclaimed. 2158 */ 2159 mutex_enter(vp->v_interlock); 2160 if (LFS_STARVED_FOR_SEGS(fs) && vdead_check(vp, VDEAD_NOWAIT) == 0) { 2161 mutex_exit(vp->v_interlock); 2162 failreason = "Starved for segs and not flushing vp"; 2163 goto tryagain; 2164 } 2165 mutex_exit(vp->v_interlock); 2166 2167 /* 2168 * Sometimes things slip past the filters in lfs_putpages, 2169 * and the pagedaemon tries to write pages---problem is 2170 * that the pagedaemon never acquires the segment lock. 2171 * 2172 * Alternatively, pages that were clean when we called 2173 * genfs_putpages may have become dirty in the meantime. In this 2174 * case the segment header is not properly set up for blocks 2175 * to be added to it. 2176 * 2177 * Unbusy and unclean the pages, and put them on the ACTIVE 2178 * queue under the hypothesis that they couldn't have got here 2179 * unless they were modified *quite* recently. 2180 * 2181 * XXXUBC that last statement is an oversimplification of course. 2182 */ 2183 if (!LFS_SEGLOCK_HELD(fs)) { 2184 failreason = "Seglock not held"; 2185 goto tryagain; 2186 } 2187 if (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) { 2188 failreason = "Inode with no_gop_write"; 2189 goto tryagain; 2190 } 2191 if ((pgs[0]->offset & lfs_sb_getbmask(fs)) != 0) { 2192 failreason = "Bad page offset"; 2193 goto tryagain; 2194 } 2195 2196 UVMHIST_LOG(ubchist, "vp %#jx pgs %#jx npages %jd flags 0x%jx", 2197 (uintptr_t)vp, (uintptr_t)pgs, npages, flags); 2198 2199 GOP_SIZE(vp, vp->v_size, &eof, 0); 2200 haveeof = 1; 2201 2202 if (vp->v_type == VREG) 2203 fs_bshift = vp->v_mount->mnt_fs_bshift; 2204 else 2205 fs_bshift = DEV_BSHIFT; 2206 error = 0; 2207 pg = pgs[0]; 2208 startoffset = pg->offset; 2209 KASSERT(eof >= 0); 2210 2211 if (startoffset >= eof) { 2212 failreason = "Offset beyond EOF"; 2213 goto tryagain; 2214 } else 2215 bytes = MIN(npages << PAGE_SHIFT, eof - startoffset); 2216 skipbytes = 0; 2217 2218 KASSERT(bytes != 0); 2219 2220 /* Swap PG_DELWRI for PG_PAGEOUT */ 2221 for (i = 0; i < npages; i++) { 2222 if (pgs[i]->flags & PG_DELWRI) { 2223 KASSERT(!(pgs[i]->flags & PG_PAGEOUT)); 2224 pgs[i]->flags &= ~PG_DELWRI; 2225 pgs[i]->flags |= PG_PAGEOUT; 2226 uvm_pageout_start(1); 2227 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER); 2228 uvm_pagelock(pgs[i]); 2229 uvm_pageunwire(pgs[i]); 2230 uvm_pageunlock(pgs[i]); 2231 rw_exit(vp->v_uobj.vmobjlock); 2232 } 2233 } 2234 2235 /* 2236 * Check to make sure we're starting on a block boundary. 2237 * We'll check later to make sure we always write entire 2238 * blocks (or fragments). 2239 */ 2240 if (startoffset & lfs_sb_getbmask(fs)) 2241 printf("%" PRId64 " & %" PRIu64 " = %" PRId64 "\n", 2242 startoffset, lfs_sb_getbmask(fs), 2243 startoffset & lfs_sb_getbmask(fs)); 2244 KASSERT((startoffset & lfs_sb_getbmask(fs)) == 0); 2245 if (bytes & lfs_sb_getffmask(fs)) { 2246 printf("lfs_gop_write: asked to write %ld bytes\n", (long)bytes); 2247 panic("lfs_gop_write: non-integer blocks"); 2248 } 2249 2250 /* 2251 * We could deadlock here on pager_map with UVMPAGER_MAPIN_WAITOK. 2252 * If we would, write what we have and try again. If we don't 2253 * have anything to write, we'll have to sleep. 2254 */ 2255 ssp = (SEGSUM *)sp->segsum; 2256 if ((kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE | 2257 (lfs_ss_getnfinfo(fs, ssp) < 1 ? 2258 UVMPAGER_MAPIN_WAITOK : 0))) == 0x0) { 2259 DLOG((DLOG_PAGE, "lfs_gop_write: forcing write\n")); 2260 lfs_updatemeta(sp); 2261 lfs_release_finfo(fs); 2262 (void) lfs_writeseg(fs, sp); 2263 2264 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen); 2265 2266 /* 2267 * Having given up all of the pager_map we were holding, 2268 * we can now wait for aiodoned to reclaim it for us 2269 * without fear of deadlock. 2270 */ 2271 kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE | 2272 UVMPAGER_MAPIN_WAITOK); 2273 } 2274 2275 mbp = getiobuf(NULL, true); 2276 UVMHIST_LOG(ubchist, "vp %#jx mbp %#jx num now %jd bytes 0x%jx", 2277 (uintptr_t)vp, (uintptr_t)mbp, vp->v_numoutput, bytes); 2278 mbp->b_bufsize = npages << PAGE_SHIFT; 2279 mbp->b_data = (void *)kva; 2280 mbp->b_resid = mbp->b_bcount = bytes; 2281 mbp->b_cflags |= BC_BUSY|BC_AGE; 2282 mbp->b_iodone = uvm_aio_aiodone; 2283 2284 bp = NULL; 2285 for (offset = startoffset; 2286 bytes > 0; 2287 offset += iobytes, bytes -= iobytes) { 2288 lbn = offset >> fs_bshift; 2289 error = ulfs_bmaparray(vp, lbn, &blkno, NULL, NULL, &run, 2290 lfs_issequential_hole); 2291 if (error) { 2292 UVMHIST_LOG(ubchist, "ulfs_bmaparray() -> %jd", 2293 error,0,0,0); 2294 skipbytes += bytes; 2295 bytes = 0; 2296 break; 2297 } 2298 2299 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset, 2300 bytes); 2301 if (blkno == (daddr_t)-1) { 2302 skipbytes += iobytes; 2303 continue; 2304 } 2305 2306 /* 2307 * Discover how much we can really pack into this buffer. 2308 */ 2309 /* If no room in the current segment, finish it up */ 2310 if (sp->sum_bytes_left < sizeof(int32_t) || 2311 sp->seg_bytes_left < (1 << lfs_sb_getbshift(fs))) { 2312 int vers; 2313 2314 lfs_updatemeta(sp); 2315 vers = lfs_fi_getversion(fs, sp->fip); 2316 lfs_release_finfo(fs); 2317 (void) lfs_writeseg(fs, sp); 2318 2319 lfs_acquire_finfo(fs, ip->i_number, vers); 2320 } 2321 /* Check both for space in segment and space in segsum */ 2322 iobytes = MIN(iobytes, (sp->seg_bytes_left >> fs_bshift) 2323 << fs_bshift); 2324 iobytes = MIN(iobytes, (sp->sum_bytes_left / sizeof(int32_t)) 2325 << fs_bshift); 2326 KASSERT(iobytes > 0); 2327 2328 /* if it's really one i/o, don't make a second buf */ 2329 if (offset == startoffset && iobytes == bytes) { 2330 bp = mbp; 2331 /* 2332 * All the LFS output is done by the segwriter. It 2333 * will increment numoutput by one for all the bufs it 2334 * receives. However this buffer needs one extra to 2335 * account for aiodone. 2336 */ 2337 mutex_enter(vp->v_interlock); 2338 vp->v_numoutput++; 2339 mutex_exit(vp->v_interlock); 2340 } else { 2341 bp = getiobuf(NULL, true); 2342 UVMHIST_LOG(ubchist, "vp %#jx bp %#jx num now %jd", 2343 (uintptr_t)vp, (uintptr_t)bp, vp->v_numoutput, 0); 2344 nestiobuf_setup(mbp, bp, offset - pg->offset, iobytes); 2345 /* 2346 * LFS doesn't like async I/O here, dies with 2347 * an assert in lfs_bwrite(). Is that assert 2348 * valid? I retained non-async behaviour when 2349 * converted this to use nestiobuf --pooka 2350 */ 2351 bp->b_flags &= ~B_ASYNC; 2352 } 2353 2354 /* XXX This is silly ... is this necessary? */ 2355 mutex_enter(&bufcache_lock); 2356 mutex_enter(vp->v_interlock); 2357 bgetvp(vp, bp); 2358 mutex_exit(vp->v_interlock); 2359 mutex_exit(&bufcache_lock); 2360 2361 bp->b_lblkno = lfs_lblkno(fs, offset); 2362 bp->b_private = mbp; 2363 if (devvp->v_type == VBLK) { 2364 bp->b_dev = devvp->v_rdev; 2365 } 2366 VOP_BWRITE(bp->b_vp, bp); 2367 while (lfs_gatherblock(sp, bp, NULL)) 2368 continue; 2369 } 2370 2371 nestiobuf_done(mbp, skipbytes, error); 2372 if (skipbytes) { 2373 UVMHIST_LOG(ubchist, "skipbytes %jd", skipbytes, 0,0,0); 2374 } 2375 UVMHIST_LOG(ubchist, "returning 0", 0,0,0,0); 2376 2377 if (!async) { 2378 /* Start a segment write. */ 2379 UVMHIST_LOG(ubchist, "flushing", 0,0,0,0); 2380 mutex_enter(&lfs_lock); 2381 lfs_flush(fs, 0, 1); 2382 mutex_exit(&lfs_lock); 2383 } 2384 2385 if ((sp->seg_flags & SEGM_SINGLE) && lfs_sb_getcurseg(fs) != fs->lfs_startseg) 2386 return EAGAIN; 2387 2388 return (0); 2389 2390 tryagain: 2391 /* 2392 * We can't write the pages, for whatever reason. 2393 * Clean up after ourselves, and make the caller try again. 2394 */ 2395 2396 /* Tell why we're here, if we know */ 2397 if (failreason != NULL) { 2398 DLOG((DLOG_PAGE, "lfs_gop_write: %s\n", failreason)); 2399 } 2400 if (haveeof && startoffset >= eof) { 2401 DLOG((DLOG_PAGE, "lfs_gop_write: ino %d start 0x%" PRIx64 2402 " eof 0x%" PRIx64 " npages=%d\n", VTOI(vp)->i_number, 2403 pgs[0]->offset, eof, npages)); 2404 } 2405 2406 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER); 2407 for (i = 0; i < npages; i++) { 2408 pg = pgs[i]; 2409 2410 if (pg->flags & PG_PAGEOUT) 2411 uvm_pageout_done(1); 2412 uvm_pagelock(pg); 2413 if (pg->flags & PG_DELWRI) { 2414 uvm_pageunwire(pg); 2415 } 2416 uvm_pageactivate(pg); 2417 uvm_pageunlock(pg); 2418 pg->flags &= ~(PG_DELWRI|PG_PAGEOUT|PG_RELEASED); 2419 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY); 2420 DLOG((DLOG_PAGE, "pg[%d] = %p (vp %p off %" PRIx64 ")\n", i, pg, 2421 vp, pg->offset)); 2422 DLOG((DLOG_PAGE, "pg[%d]->flags = %x\n", i, pg->flags)); 2423 DLOG((DLOG_PAGE, "pg[%d]->pqflags = %x\n", i, pg->pqflags)); 2424 DLOG((DLOG_PAGE, "pg[%d]->uanon = %p\n", i, pg->uanon)); 2425 DLOG((DLOG_PAGE, "pg[%d]->uobject = %p\n", i, pg->uobject)); 2426 DLOG((DLOG_PAGE, "pg[%d]->wire_count = %d\n", i, 2427 pg->wire_count)); 2428 DLOG((DLOG_PAGE, "pg[%d]->loan_count = %d\n", i, 2429 pg->loan_count)); 2430 } 2431 uvm_page_unbusy(pgs, npages); 2432 rw_exit(vp->v_uobj.vmobjlock); 2433 return EAGAIN; 2434 } 2435 2436 /* 2437 * finish vnode/inode initialization. 2438 * used by lfs_vget. 2439 */ 2440 void 2441 lfs_vinit(struct mount *mp, struct vnode **vpp) 2442 { 2443 struct vnode *vp = *vpp; 2444 struct inode *ip = VTOI(vp); 2445 struct ulfsmount *ump = VFSTOULFS(mp); 2446 struct lfs *fs = ump->um_lfs; 2447 int i; 2448 2449 ip->i_mode = lfs_dino_getmode(fs, ip->i_din); 2450 ip->i_nlink = lfs_dino_getnlink(fs, ip->i_din); 2451 ip->i_lfs_osize = ip->i_size = lfs_dino_getsize(fs, ip->i_din); 2452 ip->i_flags = lfs_dino_getflags(fs, ip->i_din); 2453 ip->i_gen = lfs_dino_getgen(fs, ip->i_din); 2454 ip->i_uid = lfs_dino_getuid(fs, ip->i_din); 2455 ip->i_gid = lfs_dino_getgid(fs, ip->i_din); 2456 2457 ip->i_lfs_effnblks = lfs_dino_getblocks(fs, ip->i_din); 2458 ip->i_lfs_odnlink = lfs_dino_getnlink(fs, ip->i_din); 2459 2460 /* 2461 * Initialize the vnode from the inode, check for aliases. In all 2462 * cases re-init ip, the underlying vnode/inode may have changed. 2463 */ 2464 ulfs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp); 2465 ip = VTOI(vp); 2466 2467 memset(ip->i_lfs_fragsize, 0, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize)); 2468 if (vp->v_type != VLNK || ip->i_size >= ip->i_lfs->um_maxsymlinklen) { 2469 #ifdef DEBUG 2470 for (i = (ip->i_size + lfs_sb_getbsize(fs) - 1) >> lfs_sb_getbshift(fs); 2471 i < ULFS_NDADDR; i++) { 2472 if ((vp->v_type == VBLK || vp->v_type == VCHR) && 2473 i == 0) 2474 continue; 2475 if (lfs_dino_getdb(fs, ip->i_din, i) != 0) { 2476 lfs_dump_dinode(fs, ip->i_din); 2477 panic("inconsistent inode (direct)"); 2478 } 2479 } 2480 for ( ; i < ULFS_NDADDR + ULFS_NIADDR; i++) { 2481 if (lfs_dino_getib(fs, ip->i_din, i - ULFS_NDADDR) != 0) { 2482 lfs_dump_dinode(fs, ip->i_din); 2483 panic("inconsistent inode (indirect)"); 2484 } 2485 } 2486 #endif /* DEBUG */ 2487 for (i = 0; i < ULFS_NDADDR; i++) 2488 if (lfs_dino_getdb(fs, ip->i_din, i) != 0) 2489 ip->i_lfs_fragsize[i] = lfs_blksize(fs, ip, i); 2490 } 2491 2492 KASSERTMSG((vp->v_type != VNON), 2493 "lfs_vinit: ino %llu is type VNON! (ifmt=%o)\n", 2494 (unsigned long long)ip->i_number, 2495 (ip->i_mode & LFS_IFMT) >> 12); 2496 2497 /* 2498 * Finish inode initialization now that aliasing has been resolved. 2499 */ 2500 2501 ip->i_devvp = fs->lfs_devvp; 2502 vref(ip->i_devvp); 2503 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2) 2504 ulfsquota_init(ip); 2505 #endif 2506 genfs_node_init(vp, &lfs_genfsops); 2507 uvm_vnp_setsize(vp, ip->i_size); 2508 2509 /* Initialize hiblk from file size */ 2510 ip->i_lfs_hiblk = lfs_lblkno(ip->i_lfs, ip->i_size + lfs_sb_getbsize(ip->i_lfs) - 1) - 1; 2511 2512 *vpp = vp; 2513 } 2514 2515 /* 2516 * Resize the filesystem to contain the specified number of segments. 2517 */ 2518 int 2519 lfs_resize_fs(struct lfs *fs, int newnsegs) 2520 { 2521 SEGUSE *sup; 2522 CLEANERINFO *cip; 2523 struct buf *bp, *obp; 2524 daddr_t olast, nlast, ilast, noff, start, end, dirtysums; 2525 struct vnode *ivp; 2526 struct inode *ip; 2527 int error, badnews, inc, oldnsegs; 2528 int sbbytes, csbbytes, gain, cgain; 2529 int i, nexti; 2530 2531 /* Only support v2 and up */ 2532 if (lfs_sb_getversion(fs) < 2) 2533 return EOPNOTSUPP; 2534 2535 /* If we're doing nothing, do it fast */ 2536 oldnsegs = lfs_sb_getnseg(fs); 2537 if (newnsegs == oldnsegs) 2538 return 0; 2539 2540 /* We always have to have two superblocks */ 2541 if (newnsegs <= lfs_dtosn(fs, lfs_sb_getsboff(fs, 1))) 2542 return EINVAL; 2543 2544 ivp = fs->lfs_ivnode; 2545 ip = VTOI(ivp); 2546 error = 0; 2547 2548 /* Take the segment lock */ 2549 lfs_seglock(fs, SEGM_CKP); 2550 2551 /* If we're shrinking, clean out the upper segments */ 2552 if (newnsegs < oldnsegs) { 2553 lfs_rewind(fs, newnsegs); 2554 for (i = newnsegs; i < oldnsegs; i++) { 2555 uint32_t bytes; 2556 LFS_SEGENTRY(sup, fs, i, bp); 2557 bytes = sup->su_nbytes; 2558 brelse(bp, 0); 2559 if (bytes > 0) { 2560 if ((error = lfs_rewrite_segment(fs, i, NULL, NOCRED, curlwp)) != 0) 2561 goto out; 2562 } 2563 2564 lfs_invalidate(fs, i); 2565 } 2566 } 2567 2568 /* 2569 * Make sure the segments we're going to be losing, if any, 2570 * are in fact empty. We hold the seglock, so their status 2571 * cannot change underneath us. Count the superblocks we lose, 2572 * while we're at it. 2573 */ 2574 sbbytes = csbbytes = cgain = dirtysums = 0; 2575 for (i = newnsegs; i < oldnsegs; i++) { 2576 LFS_SEGENTRY(sup, fs, i, bp); 2577 badnews = sup->su_nbytes || !(sup->su_flags & SEGUSE_INVAL); 2578 if (sup->su_flags & SEGUSE_SUPERBLOCK) 2579 sbbytes += LFS_SBPAD; 2580 if (!(sup->su_flags & SEGUSE_DIRTY)) { 2581 ++cgain; 2582 if (sup->su_flags & SEGUSE_SUPERBLOCK) 2583 csbbytes += LFS_SBPAD; 2584 } else 2585 dirtysums += sup->su_nsums; 2586 brelse(bp, 0); 2587 if (badnews) { 2588 error = EBUSY; 2589 goto out; 2590 } 2591 } 2592 2593 /* Note old and new segment table endpoints, and old ifile size */ 2594 olast = lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs); 2595 nlast = howmany(newnsegs, lfs_sb_getsepb(fs)) + lfs_sb_getcleansz(fs); 2596 ilast = ivp->v_size >> lfs_sb_getbshift(fs); 2597 noff = nlast - olast; 2598 2599 /* 2600 * Make sure no one can use the Ifile while we change it around. 2601 * Even after taking the iflock we need to make sure no one still 2602 * is holding Ifile buffers, so we get each one, to drain them. 2603 * (XXX this could be done better.) 2604 */ 2605 rw_enter(&fs->lfs_iflock, RW_WRITER); 2606 for (i = 0; i < ilast; i++) { 2607 /* XXX what to do if bread fails? */ 2608 bread(ivp, i, lfs_sb_getbsize(fs), 0, &bp); 2609 brelse(bp, 0); 2610 } 2611 2612 /* Allocate new Ifile blocks */ 2613 for (i = ilast; i < ilast + noff; i++) { 2614 if (lfs_balloc(ivp, i * lfs_sb_getbsize(fs), 2615 lfs_sb_getbsize(fs), NOCRED, 0, 2616 &bp) != 0) 2617 panic("balloc extending ifile"); 2618 memset(bp->b_data, 0, lfs_sb_getbsize(fs)); 2619 VOP_BWRITE(bp->b_vp, bp); 2620 } 2621 2622 /* 2623 * If we are expanding, increase the Ifile size now 2624 * so we can allocate blocks. If we are shrinking 2625 * we need to wait until we have moved the data. 2626 */ 2627 if (noff > 0) { 2628 ip->i_size += noff * lfs_sb_getbsize(fs); 2629 lfs_dino_setsize(fs, ip->i_din, ip->i_size); 2630 uvm_vnp_setsize(ivp, ip->i_size); 2631 } 2632 2633 /* Copy the inode table to its new position */ 2634 if (noff != 0) { 2635 if (noff < 0) { 2636 start = nlast; 2637 end = ilast + noff; 2638 inc = 1; 2639 } else { 2640 start = ilast + noff - 1; 2641 end = nlast - 1; 2642 inc = -1; 2643 } 2644 for (i = start; i != end; i += inc) { 2645 if (bread(ivp, i, lfs_sb_getbsize(fs), 2646 B_MODIFY, &bp) != 0) 2647 panic("resize: bread dst blk failed"); 2648 if (bread(ivp, i - noff, lfs_sb_getbsize(fs), 2649 0, &obp)) 2650 panic("resize: bread src blk failed"); 2651 memcpy(bp->b_data, obp->b_data, lfs_sb_getbsize(fs)); 2652 VOP_BWRITE(bp->b_vp, bp); 2653 brelse(obp, 0); 2654 } 2655 } 2656 2657 /* If we are expanding, write the new empty SEGUSE entries */ 2658 for (i = oldnsegs, nexti = 0; i < newnsegs; i = nexti) { 2659 nexti = roundup(i + 1, lfs_sb_getsepb(fs)); 2660 if (nexti > newnsegs) 2661 nexti = newnsegs; 2662 2663 if ((error = bread(ivp, i / lfs_sb_getsepb(fs) + 2664 lfs_sb_getcleansz(fs), 2665 lfs_sb_getbsize(fs), 2666 (i > oldnsegs ? B_MODIFY : 0), &bp)) != 0) 2667 panic("lfs: ifile read: %d", error); 2668 sup = &((SEGUSE *)bp->b_data)[i % lfs_sb_getsepb(fs)]; 2669 memset(sup, 0, (nexti - i) * sizeof(SEGUSE)); 2670 VOP_BWRITE(bp->b_vp, bp); 2671 } 2672 2673 /* Zero out unused superblock offsets */ 2674 for (i = 2; i < LFS_MAXNUMSB; i++) 2675 if (lfs_dtosn(fs, lfs_sb_getsboff(fs, i)) >= newnsegs) 2676 lfs_sb_setsboff(fs, i, 0x0); 2677 2678 /* 2679 * Correct superblock entries that depend on fs size. 2680 * The computations of these are as follows: 2681 * 2682 * size = lfs_segtod(fs, nseg) 2683 * dsize = lfs_segtod(fs, nseg - minfreeseg) - lfs_btofsb(#super * LFS_SBPAD) 2684 * bfree = dsize - lfs_btofsb(fs, bsize * nseg / 2) - blocks_actually_used 2685 * avail = lfs_segtod(fs, nclean) - lfs_btofsb(#clean_super * LFS_SBPAD) 2686 * + (lfs_segtod(fs, 1) - (offset - curseg)) 2687 * - lfs_segtod(fs, minfreeseg - (minfreeseg / 2)) 2688 * 2689 * XXX - we should probably adjust minfreeseg as well. 2690 */ 2691 gain = (newnsegs - oldnsegs); 2692 lfs_sb_setnseg(fs, newnsegs); 2693 lfs_sb_setsegtabsz(fs, nlast - lfs_sb_getcleansz(fs)); 2694 lfs_sb_addsize(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs))); 2695 lfs_sb_adddsize(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)) 2696 + lfs_btofsb(fs, sbbytes)); 2697 lfs_sb_addbfree(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)) 2698 + lfs_btofsb(fs, sbbytes) 2699 #if 0 /* This doesn't match fsck_lfs */ 2700 - gain * lfs_btofsb(fs, lfs_sb_getbsize(fs) / 2) 2701 + dirtysums * lfs_btofsb(fs, lfs_sb_getsumsize(fs)) 2702 #endif /* 0 */ 2703 ); 2704 if (gain > 0) { 2705 lfs_sb_addnclean(fs, gain); 2706 lfs_sb_addavail(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs))); 2707 } else { 2708 lfs_sb_subnclean(fs, cgain); 2709 lfs_sb_subavail(fs, cgain * lfs_btofsb(fs, lfs_sb_getssize(fs)) - 2710 lfs_btofsb(fs, csbbytes)); 2711 } 2712 2713 /* Truncate Ifile if necessary */ 2714 if (noff < 0) { 2715 vn_lock(ivp, LK_EXCLUSIVE); 2716 lfs_truncate(ivp, ivp->v_size + (noff << lfs_sb_getbshift(fs)), 0, 2717 NOCRED); 2718 VOP_UNLOCK(ivp); 2719 } 2720 2721 /* Update cleaner info so the cleaner can die */ 2722 /* XXX what to do if bread fails? */ 2723 bread(ivp, 0, lfs_sb_getbsize(fs), B_MODIFY, &bp); 2724 cip = bp->b_data; 2725 lfs_ci_setclean(fs, cip, lfs_sb_getnclean(fs)); 2726 lfs_ci_setdirty(fs, cip, lfs_sb_getnseg(fs) - lfs_sb_getnclean(fs)); 2727 VOP_BWRITE(bp->b_vp, bp); 2728 2729 /* Let Ifile accesses proceed */ 2730 rw_exit(&fs->lfs_iflock); 2731 2732 out: 2733 lfs_segunlock(fs); 2734 return error; 2735 } 2736 2737 /* 2738 * Extended attribute dispatch 2739 */ 2740 int 2741 lfs_extattrctl(struct mount *mp, int cmd, struct vnode *vp, 2742 int attrnamespace, const char *attrname) 2743 { 2744 #ifdef LFS_EXTATTR 2745 struct ulfsmount *ump; 2746 2747 ump = VFSTOULFS(mp); 2748 if (ump->um_fstype == ULFS1) { 2749 return ulfs_extattrctl(mp, cmd, vp, attrnamespace, attrname); 2750 } 2751 #endif 2752 return vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname); 2753 } 2754