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