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