1 /* 2 * Copyright (c) 2011-2015 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression) 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/nlookup.h> 39 #include <sys/vnode.h> 40 #include <sys/mount.h> 41 #include <sys/fcntl.h> 42 #include <sys/buf.h> 43 #include <sys/uuid.h> 44 #include <sys/vfsops.h> 45 #include <sys/sysctl.h> 46 #include <sys/socket.h> 47 #include <sys/objcache.h> 48 49 #include <sys/proc.h> 50 #include <sys/namei.h> 51 #include <sys/mountctl.h> 52 #include <sys/dirent.h> 53 #include <sys/uio.h> 54 55 #include <sys/mutex.h> 56 #include <sys/mutex2.h> 57 58 #include "hammer2.h" 59 #include "hammer2_disk.h" 60 #include "hammer2_mount.h" 61 #include "hammer2_lz4.h" 62 63 #include "zlib/hammer2_zlib.h" 64 65 #define REPORT_REFS_ERRORS 1 /* XXX remove me */ 66 67 MALLOC_DEFINE(M_OBJCACHE, "objcache", "Object Cache"); 68 69 struct hammer2_sync_info { 70 int error; 71 int waitfor; 72 }; 73 74 TAILQ_HEAD(hammer2_mntlist, hammer2_dev); 75 TAILQ_HEAD(hammer2_pfslist, hammer2_pfs); 76 static struct hammer2_mntlist hammer2_mntlist; 77 static struct hammer2_pfslist hammer2_pfslist; 78 static struct lock hammer2_mntlk; 79 80 int hammer2_debug; 81 int hammer2_cluster_enable = 1; 82 int hammer2_hardlink_enable = 1; 83 int hammer2_flush_pipe = 100; 84 int hammer2_synchronous_flush = 1; 85 int hammer2_dio_count; 86 long hammer2_limit_dirty_chains; 87 long hammer2_iod_file_read; 88 long hammer2_iod_meta_read; 89 long hammer2_iod_indr_read; 90 long hammer2_iod_fmap_read; 91 long hammer2_iod_volu_read; 92 long hammer2_iod_file_write; 93 long hammer2_iod_meta_write; 94 long hammer2_iod_indr_write; 95 long hammer2_iod_fmap_write; 96 long hammer2_iod_volu_write; 97 long hammer2_ioa_file_read; 98 long hammer2_ioa_meta_read; 99 long hammer2_ioa_indr_read; 100 long hammer2_ioa_fmap_read; 101 long hammer2_ioa_volu_read; 102 long hammer2_ioa_fmap_write; 103 long hammer2_ioa_file_write; 104 long hammer2_ioa_meta_write; 105 long hammer2_ioa_indr_write; 106 long hammer2_ioa_volu_write; 107 108 MALLOC_DECLARE(M_HAMMER2_CBUFFER); 109 MALLOC_DEFINE(M_HAMMER2_CBUFFER, "HAMMER2-compbuffer", 110 "Buffer used for compression."); 111 112 MALLOC_DECLARE(M_HAMMER2_DEBUFFER); 113 MALLOC_DEFINE(M_HAMMER2_DEBUFFER, "HAMMER2-decompbuffer", 114 "Buffer used for decompression."); 115 116 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem"); 117 118 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW, 119 &hammer2_debug, 0, ""); 120 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW, 121 &hammer2_cluster_enable, 0, ""); 122 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW, 123 &hammer2_hardlink_enable, 0, ""); 124 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW, 125 &hammer2_flush_pipe, 0, ""); 126 SYSCTL_INT(_vfs_hammer2, OID_AUTO, synchronous_flush, CTLFLAG_RW, 127 &hammer2_synchronous_flush, 0, ""); 128 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW, 129 &hammer2_limit_dirty_chains, 0, ""); 130 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD, 131 &hammer2_dio_count, 0, ""); 132 133 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW, 134 &hammer2_iod_file_read, 0, ""); 135 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW, 136 &hammer2_iod_meta_read, 0, ""); 137 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW, 138 &hammer2_iod_indr_read, 0, ""); 139 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW, 140 &hammer2_iod_fmap_read, 0, ""); 141 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW, 142 &hammer2_iod_volu_read, 0, ""); 143 144 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW, 145 &hammer2_iod_file_write, 0, ""); 146 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW, 147 &hammer2_iod_meta_write, 0, ""); 148 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW, 149 &hammer2_iod_indr_write, 0, ""); 150 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW, 151 &hammer2_iod_fmap_write, 0, ""); 152 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW, 153 &hammer2_iod_volu_write, 0, ""); 154 155 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW, 156 &hammer2_ioa_file_read, 0, ""); 157 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW, 158 &hammer2_ioa_meta_read, 0, ""); 159 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW, 160 &hammer2_ioa_indr_read, 0, ""); 161 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW, 162 &hammer2_ioa_fmap_read, 0, ""); 163 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW, 164 &hammer2_ioa_volu_read, 0, ""); 165 166 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW, 167 &hammer2_ioa_file_write, 0, ""); 168 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW, 169 &hammer2_ioa_meta_write, 0, ""); 170 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW, 171 &hammer2_ioa_indr_write, 0, ""); 172 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW, 173 &hammer2_ioa_fmap_write, 0, ""); 174 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW, 175 &hammer2_ioa_volu_write, 0, ""); 176 177 static int hammer2_vfs_init(struct vfsconf *conf); 178 static int hammer2_vfs_uninit(struct vfsconf *vfsp); 179 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data, 180 struct ucred *cred); 181 static int hammer2_remount(hammer2_dev_t *, struct mount *, char *, 182 struct vnode *, struct ucred *); 183 static int hammer2_recovery(hammer2_dev_t *hmp); 184 static int hammer2_vfs_unmount(struct mount *mp, int mntflags); 185 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp); 186 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, 187 struct ucred *cred); 188 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, 189 struct ucred *cred); 190 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp, 191 ino_t ino, struct vnode **vpp); 192 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 193 struct fid *fhp, struct vnode **vpp); 194 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp); 195 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 196 int *exflagsp, struct ucred **credanonp); 197 198 static int hammer2_install_volume_header(hammer2_dev_t *hmp); 199 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data); 200 201 static void hammer2_update_pmps(hammer2_dev_t *hmp); 202 203 static void hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp); 204 static void hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, 205 hammer2_dev_t *hmp); 206 207 /* 208 * HAMMER2 vfs operations. 209 */ 210 static struct vfsops hammer2_vfsops = { 211 .vfs_init = hammer2_vfs_init, 212 .vfs_uninit = hammer2_vfs_uninit, 213 .vfs_sync = hammer2_vfs_sync, 214 .vfs_mount = hammer2_vfs_mount, 215 .vfs_unmount = hammer2_vfs_unmount, 216 .vfs_root = hammer2_vfs_root, 217 .vfs_statfs = hammer2_vfs_statfs, 218 .vfs_statvfs = hammer2_vfs_statvfs, 219 .vfs_vget = hammer2_vfs_vget, 220 .vfs_vptofh = hammer2_vfs_vptofh, 221 .vfs_fhtovp = hammer2_vfs_fhtovp, 222 .vfs_checkexp = hammer2_vfs_checkexp 223 }; 224 225 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", ""); 226 227 VFS_SET(hammer2_vfsops, hammer2, 0); 228 MODULE_VERSION(hammer2, 1); 229 230 static 231 int 232 hammer2_vfs_init(struct vfsconf *conf) 233 { 234 static struct objcache_malloc_args margs_read; 235 static struct objcache_malloc_args margs_write; 236 static struct objcache_malloc_args margs_vop; 237 238 int error; 239 240 error = 0; 241 242 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref)) 243 error = EINVAL; 244 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data)) 245 error = EINVAL; 246 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data)) 247 error = EINVAL; 248 249 if (error) 250 kprintf("HAMMER2 structure size mismatch; cannot continue.\n"); 251 252 margs_read.objsize = 65536; 253 margs_read.mtype = M_HAMMER2_DEBUFFER; 254 255 margs_write.objsize = 32768; 256 margs_write.mtype = M_HAMMER2_CBUFFER; 257 258 margs_vop.objsize = sizeof(hammer2_xop_t); 259 margs_vop.mtype = M_HAMMER2; 260 261 /* 262 * Note thaht for the XOPS cache we want backing store allocations 263 * to use M_ZERO. This is not allowed in objcache_get() (to avoid 264 * confusion), so use the backing store function that does it. This 265 * means that initial XOPS objects are zerod but REUSED objects are 266 * not. So we are responsible for cleaning the object up sufficiently 267 * for our needs before objcache_put()ing it back (typically just the 268 * FIFO indices). 269 */ 270 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc, 271 0, 1, NULL, NULL, NULL, 272 objcache_malloc_alloc, 273 objcache_malloc_free, 274 &margs_read); 275 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc, 276 0, 1, NULL, NULL, NULL, 277 objcache_malloc_alloc, 278 objcache_malloc_free, 279 &margs_write); 280 cache_xops = objcache_create(margs_vop.mtype->ks_shortdesc, 281 0, 1, NULL, NULL, NULL, 282 objcache_malloc_alloc_zero, 283 objcache_malloc_free, 284 &margs_vop); 285 286 287 lockinit(&hammer2_mntlk, "mntlk", 0, 0); 288 TAILQ_INIT(&hammer2_mntlist); 289 TAILQ_INIT(&hammer2_pfslist); 290 291 hammer2_limit_dirty_chains = desiredvnodes / 10; 292 293 return (error); 294 } 295 296 static 297 int 298 hammer2_vfs_uninit(struct vfsconf *vfsp __unused) 299 { 300 objcache_destroy(cache_buffer_read); 301 objcache_destroy(cache_buffer_write); 302 objcache_destroy(cache_xops); 303 return 0; 304 } 305 306 /* 307 * Core PFS allocator. Used to allocate the pmp structure for PFS cluster 308 * mounts and the spmp structure for media (hmp) structures. 309 * 310 * pmp->modify_tid tracks new modify_tid transaction ids for front-end 311 * transactions. Note that synchronization does not use this field. 312 * (typically frontend operations and synchronization cannot run on the 313 * same PFS node at the same time). 314 * 315 * XXX check locking 316 */ 317 hammer2_pfs_t * 318 hammer2_pfsalloc(hammer2_chain_t *chain, const hammer2_inode_data_t *ripdata, 319 hammer2_tid_t modify_tid) 320 { 321 hammer2_inode_t *iroot; 322 hammer2_pfs_t *pmp; 323 int count; 324 int i; 325 int j; 326 327 /* 328 * Locate or create the PFS based on the cluster id. If ripdata 329 * is NULL this is a spmp which is unique and is always allocated. 330 */ 331 if (ripdata) { 332 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) { 333 if (bcmp(&pmp->pfs_clid, &ripdata->meta.pfs_clid, 334 sizeof(pmp->pfs_clid)) == 0) { 335 break; 336 } 337 } 338 } else { 339 pmp = NULL; 340 } 341 342 if (pmp == NULL) { 343 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO); 344 hammer2_trans_manage_init(pmp); 345 kmalloc_create(&pmp->minode, "HAMMER2-inodes"); 346 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg"); 347 lockinit(&pmp->lock, "pfslk", 0, 0); 348 spin_init(&pmp->inum_spin, "hm2pfsalloc_inum"); 349 spin_init(&pmp->xop_spin, "h2xop"); 350 RB_INIT(&pmp->inum_tree); 351 TAILQ_INIT(&pmp->unlinkq); 352 spin_init(&pmp->list_spin, "hm2pfsalloc_list"); 353 354 /* 355 * Distribute backend operations to threads 356 */ 357 for (j = 0; j < HAMMER2_MAXCLUSTER; ++j) 358 TAILQ_INIT(&pmp->xopq[j]); 359 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) 360 hammer2_xop_group_init(pmp, &pmp->xop_groups[j]); 361 362 /* 363 * Save the last media transaction id for the flusher. Set 364 * initial 365 */ 366 if (ripdata) 367 pmp->pfs_clid = ripdata->meta.pfs_clid; 368 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry); 369 370 /* 371 * The synchronization thread may start too early, make 372 * sure it stays frozen until we are ready to let it go. 373 * XXX 374 */ 375 /* 376 pmp->primary_thr.flags = HAMMER2_THREAD_FROZEN | 377 HAMMER2_THREAD_REMASTER; 378 */ 379 } 380 381 /* 382 * Create the PFS's root inode. 383 */ 384 if ((iroot = pmp->iroot) == NULL) { 385 iroot = hammer2_inode_get(pmp, NULL, NULL, -1); 386 pmp->iroot = iroot; 387 hammer2_inode_ref(iroot); 388 hammer2_inode_unlock(iroot); 389 } 390 391 /* 392 * Stop here if no chain is passed in. 393 */ 394 if (chain == NULL) 395 goto done; 396 397 /* 398 * When a chain is passed in we must add it to the PFS's root 399 * inode, update pmp->pfs_types[], and update the syncronization 400 * threads. 401 * 402 * At the moment empty spots can develop due to removals or failures. 403 * Ultimately we want to re-fill these spots but doing so might 404 * confused running code. XXX 405 */ 406 hammer2_inode_ref(iroot); 407 hammer2_mtx_ex(&iroot->lock); 408 j = iroot->cluster.nchains; 409 410 kprintf("add PFS to pmp %p[%d]\n", pmp, j); 411 412 if (j == HAMMER2_MAXCLUSTER) { 413 kprintf("hammer2_mount: cluster full!\n"); 414 /* XXX fatal error? */ 415 } else { 416 KKASSERT(chain->pmp == NULL); 417 chain->pmp = pmp; 418 hammer2_chain_ref(chain); 419 iroot->cluster.array[j].chain = chain; 420 pmp->pfs_types[j] = ripdata->meta.pfs_type; 421 pmp->pfs_names[j] = kstrdup(ripdata->filename, M_HAMMER2); 422 pmp->pfs_hmps[j] = chain->hmp; 423 424 /* 425 * If the PFS is already mounted we must account 426 * for the mount_count here. 427 */ 428 if (pmp->mp) 429 ++chain->hmp->mount_count; 430 431 /* 432 * May have to fixup dirty chain tracking. Previous 433 * pmp was NULL so nothing to undo. 434 */ 435 if (chain->flags & HAMMER2_CHAIN_MODIFIED) 436 hammer2_pfs_memory_inc(pmp); 437 ++j; 438 } 439 iroot->cluster.nchains = j; 440 441 /* 442 * Update nmasters from any PFS inode which is part of the cluster. 443 * It is possible that this will result in a value which is too 444 * high. MASTER PFSs are authoritative for pfs_nmasters and will 445 * override this value later on. 446 * 447 * (This informs us of masters that might not currently be 448 * discoverable by this mount). 449 */ 450 if (ripdata && pmp->pfs_nmasters < ripdata->meta.pfs_nmasters) { 451 pmp->pfs_nmasters = ripdata->meta.pfs_nmasters; 452 } 453 454 /* 455 * Count visible masters. Masters are usually added with 456 * ripdata->meta.pfs_nmasters set to 1. This detects when there 457 * are more (XXX and must update the master inodes). 458 */ 459 count = 0; 460 for (i = 0; i < iroot->cluster.nchains; ++i) { 461 if (pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) 462 ++count; 463 } 464 if (pmp->pfs_nmasters < count) 465 pmp->pfs_nmasters = count; 466 467 /* 468 * Create missing synchronization and support threads. 469 * 470 * Single-node masters (including snapshots) have nothing to 471 * synchronize and do not require this thread. 472 * 473 * Multi-node masters or any number of soft masters, slaves, copy, 474 * or other PFS types need the thread. 475 * 476 * Each thread is responsible for its particular cluster index. 477 * We use independent threads so stalls or mismatches related to 478 * any given target do not affect other targets. 479 */ 480 for (i = 0; i < iroot->cluster.nchains; ++i) { 481 /* 482 * Single-node masters (including snapshots) have nothing 483 * to synchronize and will make direct xops support calls, 484 * thus they do not require this thread. 485 * 486 * Note that there can be thousands of snapshots. We do not 487 * want to create thousands of threads. 488 */ 489 if (pmp->pfs_nmasters <= 1 && 490 pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) { 491 continue; 492 } 493 494 /* 495 * Sync support thread 496 */ 497 if (pmp->sync_thrs[i].td == NULL) { 498 hammer2_thr_create(&pmp->sync_thrs[i], pmp, 499 "h2nod", i, -1, 500 hammer2_primary_sync_thread); 501 } 502 } 503 504 /* 505 * Create missing Xop threads 506 */ 507 if (pmp->mp) 508 hammer2_xop_helper_create(pmp); 509 510 hammer2_mtx_unlock(&iroot->lock); 511 hammer2_inode_drop(iroot); 512 done: 513 return pmp; 514 } 515 516 /* 517 * Destroy a PFS, typically only occurs after the last mount on a device 518 * has gone away. 519 */ 520 static void 521 hammer2_pfsfree(hammer2_pfs_t *pmp) 522 { 523 hammer2_inode_t *iroot; 524 int i; 525 int j; 526 527 /* 528 * Cleanup our reference on iroot. iroot is (should) not be needed 529 * by the flush code. 530 */ 531 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry); 532 533 iroot = pmp->iroot; 534 if (iroot) { 535 for (i = 0; i < iroot->cluster.nchains; ++i) { 536 hammer2_thr_delete(&pmp->sync_thrs[i]); 537 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) 538 hammer2_thr_delete(&pmp->xop_groups[j].thrs[i]); 539 } 540 #if REPORT_REFS_ERRORS 541 if (pmp->iroot->refs != 1) 542 kprintf("PMP->IROOT %p REFS WRONG %d\n", 543 pmp->iroot, pmp->iroot->refs); 544 #else 545 KKASSERT(pmp->iroot->refs == 1); 546 #endif 547 /* ref for pmp->iroot */ 548 hammer2_inode_drop(pmp->iroot); 549 pmp->iroot = NULL; 550 } 551 552 kmalloc_destroy(&pmp->mmsg); 553 kmalloc_destroy(&pmp->minode); 554 555 kfree(pmp, M_HAMMER2); 556 } 557 558 /* 559 * Remove all references to hmp from the pfs list. Any PFS which becomes 560 * empty is terminated and freed. 561 * 562 * XXX inefficient. 563 */ 564 static void 565 hammer2_pfsfree_scan(hammer2_dev_t *hmp) 566 { 567 hammer2_pfs_t *pmp; 568 hammer2_inode_t *iroot; 569 hammer2_chain_t *rchain; 570 int didfreeze; 571 int i; 572 int j; 573 574 again: 575 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) { 576 if ((iroot = pmp->iroot) == NULL) 577 continue; 578 if (hmp->spmp == pmp) { 579 kprintf("unmount hmp %p remove spmp %p\n", 580 hmp, pmp); 581 hmp->spmp = NULL; 582 } 583 584 /* 585 * Determine if this PFS is affected. If it is we must 586 * freeze all management threads and lock its iroot. 587 * 588 * Freezing a management thread forces it idle, operations 589 * in-progress will be aborted and it will have to start 590 * over again when unfrozen, or exit if told to exit. 591 */ 592 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 593 if (pmp->pfs_hmps[i] == hmp) 594 break; 595 } 596 if (i != HAMMER2_MAXCLUSTER) { 597 /* 598 * Make sure all synchronization threads are locked 599 * down. 600 */ 601 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 602 if (pmp->pfs_hmps[i] == NULL) 603 continue; 604 hammer2_thr_freeze_async(&pmp->sync_thrs[i]); 605 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) { 606 hammer2_thr_freeze_async( 607 &pmp->xop_groups[j].thrs[i]); 608 } 609 } 610 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 611 if (pmp->pfs_hmps[i] == NULL) 612 continue; 613 hammer2_thr_freeze(&pmp->sync_thrs[i]); 614 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) { 615 hammer2_thr_freeze( 616 &pmp->xop_groups[j].thrs[i]); 617 } 618 } 619 620 /* 621 * Lock the inode and clean out matching chains. 622 * Note that we cannot use hammer2_inode_lock_*() 623 * here because that would attempt to validate the 624 * cluster that we are in the middle of ripping 625 * apart. 626 * 627 * WARNING! We are working directly on the inodes 628 * embedded cluster. 629 */ 630 hammer2_mtx_ex(&iroot->lock); 631 632 /* 633 * Remove the chain from matching elements of the PFS. 634 */ 635 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 636 if (pmp->pfs_hmps[i] != hmp) 637 continue; 638 hammer2_thr_delete(&pmp->sync_thrs[i]); 639 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) { 640 hammer2_thr_delete( 641 &pmp->xop_groups[j].thrs[i]); 642 } 643 rchain = iroot->cluster.array[i].chain; 644 iroot->cluster.array[i].chain = NULL; 645 pmp->pfs_types[i] = 0; 646 if (pmp->pfs_names[i]) { 647 kfree(pmp->pfs_names[i], M_HAMMER2); 648 pmp->pfs_names[i] = NULL; 649 } 650 if (rchain) { 651 hammer2_chain_drop(rchain); 652 /* focus hint */ 653 if (iroot->cluster.focus == rchain) 654 iroot->cluster.focus = NULL; 655 } 656 pmp->pfs_hmps[i] = NULL; 657 } 658 hammer2_mtx_unlock(&iroot->lock); 659 didfreeze = 1; /* remaster, unfreeze down below */ 660 } else { 661 didfreeze = 0; 662 } 663 664 /* 665 * Cleanup trailing chains. Gaps may remain. 666 */ 667 for (i = HAMMER2_MAXCLUSTER - 1; i >= 0; --i) { 668 if (pmp->pfs_hmps[i]) 669 break; 670 } 671 iroot->cluster.nchains = i + 1; 672 673 /* 674 * If the PMP has no elements remaining we can destroy it. 675 * (this will transition management threads from frozen->exit). 676 */ 677 if (iroot->cluster.nchains == 0) { 678 kprintf("unmount hmp %p last ref to PMP=%p\n", 679 hmp, pmp); 680 hammer2_pfsfree(pmp); 681 goto again; 682 } 683 684 /* 685 * If elements still remain we need to set the REMASTER 686 * flag and unfreeze it. 687 */ 688 if (didfreeze) { 689 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 690 if (pmp->pfs_hmps[i] == NULL) 691 continue; 692 hammer2_thr_remaster(&pmp->sync_thrs[i]); 693 hammer2_thr_unfreeze(&pmp->sync_thrs[i]); 694 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) { 695 hammer2_thr_remaster( 696 &pmp->xop_groups[j].thrs[i]); 697 hammer2_thr_unfreeze( 698 &pmp->xop_groups[j].thrs[i]); 699 } 700 } 701 } 702 } 703 } 704 705 /* 706 * Mount or remount HAMMER2 fileystem from physical media 707 * 708 * mountroot 709 * mp mount point structure 710 * path NULL 711 * data <unused> 712 * cred <unused> 713 * 714 * mount 715 * mp mount point structure 716 * path path to mount point 717 * data pointer to argument structure in user space 718 * volume volume path (device@LABEL form) 719 * hflags user mount flags 720 * cred user credentials 721 * 722 * RETURNS: 0 Success 723 * !0 error number 724 */ 725 static 726 int 727 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data, 728 struct ucred *cred) 729 { 730 struct hammer2_mount_info info; 731 hammer2_pfs_t *pmp; 732 hammer2_pfs_t *spmp; 733 hammer2_dev_t *hmp; 734 hammer2_key_t key_next; 735 hammer2_key_t key_dummy; 736 hammer2_key_t lhc; 737 struct vnode *devvp; 738 struct nlookupdata nd; 739 hammer2_chain_t *parent; 740 hammer2_chain_t *chain; 741 hammer2_cluster_t *cluster; 742 const hammer2_inode_data_t *ripdata; 743 hammer2_blockref_t bref; 744 struct file *fp; 745 char devstr[MNAMELEN]; 746 size_t size; 747 size_t done; 748 char *dev; 749 char *label; 750 int ronly = 1; 751 int error; 752 int cache_index; 753 int i; 754 755 hmp = NULL; 756 pmp = NULL; 757 dev = NULL; 758 label = NULL; 759 devvp = NULL; 760 cache_index = -1; 761 762 kprintf("hammer2_mount\n"); 763 764 if (path == NULL) { 765 /* 766 * Root mount 767 */ 768 bzero(&info, sizeof(info)); 769 info.cluster_fd = -1; 770 ksnprintf(devstr, sizeof(devstr), "%s", 771 mp->mnt_stat.f_mntfromname); 772 kprintf("hammer2_mount: root '%s'\n", devstr); 773 } else { 774 /* 775 * Non-root mount or updating a mount 776 */ 777 error = copyin(data, &info, sizeof(info)); 778 if (error) 779 return (error); 780 781 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done); 782 if (error) 783 return (error); 784 } 785 786 /* Extract device and label */ 787 dev = devstr; 788 label = strchr(devstr, '@'); 789 if (label == NULL || 790 ((label + 1) - dev) > done) { 791 return (EINVAL); 792 } 793 *label = '\0'; 794 label++; 795 if (*label == '\0') 796 return (EINVAL); 797 798 if (mp->mnt_flag & MNT_UPDATE) { 799 /* 800 * Update mount. Note that pmp->iroot->cluster is 801 * an inode-embedded cluster and thus cannot be 802 * directly locked. 803 * 804 * XXX HAMMER2 needs to implement NFS export via 805 * mountctl. 806 */ 807 pmp = MPTOPMP(mp); 808 cluster = &pmp->iroot->cluster; 809 for (i = 0; i < cluster->nchains; ++i) { 810 if (cluster->array[i].chain == NULL) 811 continue; 812 hmp = cluster->array[i].chain->hmp; 813 devvp = hmp->devvp; 814 error = hammer2_remount(hmp, mp, path, 815 devvp, cred); 816 if (error) 817 break; 818 } 819 820 return error; 821 } 822 823 /* 824 * HMP device mount 825 * 826 * Lookup name and verify it refers to a block device. 827 */ 828 if (path) { 829 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW); 830 if (error == 0) 831 error = nlookup(&nd); 832 if (error == 0) 833 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp); 834 nlookup_done(&nd); 835 } else { 836 /* root mount */ 837 cdev_t cdev = kgetdiskbyname(dev); 838 error = bdevvp(cdev, &devvp); 839 if (error) 840 kprintf("hammer2: cannot find '%s'\n", dev); 841 } 842 843 if (error == 0) { 844 if (vn_isdisk(devvp, &error)) 845 error = vfs_mountedon(devvp); 846 } 847 848 /* 849 * Determine if the device has already been mounted. After this 850 * check hmp will be non-NULL if we are doing the second or more 851 * hammer2 mounts from the same device. 852 */ 853 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE); 854 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) { 855 if (hmp->devvp == devvp) 856 break; 857 } 858 859 /* 860 * Open the device if this isn't a secondary mount and construct 861 * the H2 device mount (hmp). 862 */ 863 if (hmp == NULL) { 864 hammer2_chain_t *schain; 865 hammer2_xid_t xid; 866 867 if (error == 0 && vcount(devvp) > 0) 868 error = EBUSY; 869 870 /* 871 * Now open the device 872 */ 873 if (error == 0) { 874 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0); 875 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 876 error = vinvalbuf(devvp, V_SAVE, 0, 0); 877 if (error == 0) { 878 error = VOP_OPEN(devvp, 879 ronly ? FREAD : FREAD | FWRITE, 880 FSCRED, NULL); 881 } 882 vn_unlock(devvp); 883 } 884 if (error && devvp) { 885 vrele(devvp); 886 devvp = NULL; 887 } 888 if (error) { 889 lockmgr(&hammer2_mntlk, LK_RELEASE); 890 return error; 891 } 892 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO); 893 ksnprintf(hmp->devrepname, sizeof(hmp->devrepname), "%s", dev); 894 hmp->ronly = ronly; 895 hmp->devvp = devvp; 896 kmalloc_create(&hmp->mchain, "HAMMER2-chains"); 897 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry); 898 RB_INIT(&hmp->iotree); 899 spin_init(&hmp->io_spin, "hm2mount_io"); 900 spin_init(&hmp->list_spin, "hm2mount_list"); 901 TAILQ_INIT(&hmp->flushq); 902 903 lockinit(&hmp->vollk, "h2vol", 0, 0); 904 lockinit(&hmp->bulklk, "h2bulk", 0, 0); 905 906 /* 907 * vchain setup. vchain.data is embedded. 908 * vchain.refs is initialized and will never drop to 0. 909 * 910 * NOTE! voldata is not yet loaded. 911 */ 912 hmp->vchain.hmp = hmp; 913 hmp->vchain.refs = 1; 914 hmp->vchain.data = (void *)&hmp->voldata; 915 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME; 916 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX; 917 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid; 918 919 hammer2_chain_core_init(&hmp->vchain); 920 /* hmp->vchain.u.xxx is left NULL */ 921 922 /* 923 * fchain setup. fchain.data is embedded. 924 * fchain.refs is initialized and will never drop to 0. 925 * 926 * The data is not used but needs to be initialized to 927 * pass assertion muster. We use this chain primarily 928 * as a placeholder for the freemap's top-level RBTREE 929 * so it does not interfere with the volume's topology 930 * RBTREE. 931 */ 932 hmp->fchain.hmp = hmp; 933 hmp->fchain.refs = 1; 934 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset; 935 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP; 936 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX; 937 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid; 938 hmp->fchain.bref.methods = 939 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) | 940 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE); 941 942 hammer2_chain_core_init(&hmp->fchain); 943 /* hmp->fchain.u.xxx is left NULL */ 944 945 /* 946 * Install the volume header and initialize fields from 947 * voldata. 948 */ 949 error = hammer2_install_volume_header(hmp); 950 if (error) { 951 hammer2_unmount_helper(mp, NULL, hmp); 952 lockmgr(&hammer2_mntlk, LK_RELEASE); 953 hammer2_vfs_unmount(mp, MNT_FORCE); 954 return error; 955 } 956 957 /* 958 * Really important to get these right or flush will get 959 * confused. 960 */ 961 hmp->spmp = hammer2_pfsalloc(NULL, NULL, 0); 962 kprintf("alloc spmp %p tid %016jx\n", 963 hmp->spmp, hmp->voldata.mirror_tid); 964 spmp = hmp->spmp; 965 966 /* 967 * Dummy-up vchain and fchain's modify_tid. mirror_tid 968 * is inherited from the volume header. 969 */ 970 xid = 0; 971 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid; 972 hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid; 973 hmp->vchain.pmp = spmp; 974 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid; 975 hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid; 976 hmp->fchain.pmp = spmp; 977 978 /* 979 * First locate the super-root inode, which is key 0 980 * relative to the volume header's blockset. 981 * 982 * Then locate the root inode by scanning the directory keyspace 983 * represented by the label. 984 */ 985 parent = hammer2_chain_lookup_init(&hmp->vchain, 0); 986 schain = hammer2_chain_lookup(&parent, &key_dummy, 987 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 988 &cache_index, 0); 989 hammer2_chain_lookup_done(parent); 990 if (schain == NULL) { 991 kprintf("hammer2_mount: invalid super-root\n"); 992 hammer2_unmount_helper(mp, NULL, hmp); 993 lockmgr(&hammer2_mntlk, LK_RELEASE); 994 hammer2_vfs_unmount(mp, MNT_FORCE); 995 return EINVAL; 996 } 997 if (schain->error) { 998 kprintf("hammer2_mount: error %s reading super-root\n", 999 hammer2_error_str(schain->error)); 1000 hammer2_chain_unlock(schain); 1001 hammer2_chain_drop(schain); 1002 schain = NULL; 1003 hammer2_unmount_helper(mp, NULL, hmp); 1004 lockmgr(&hammer2_mntlk, LK_RELEASE); 1005 hammer2_vfs_unmount(mp, MNT_FORCE); 1006 return EINVAL; 1007 } 1008 1009 /* 1010 * The super-root always uses an inode_tid of 1 when 1011 * creating PFSs. 1012 */ 1013 spmp->inode_tid = 1; 1014 spmp->modify_tid = schain->bref.modify_tid + 1; 1015 1016 /* 1017 * Sanity-check schain's pmp and finish initialization. 1018 * Any chain belonging to the super-root topology should 1019 * have a NULL pmp (not even set to spmp). 1020 */ 1021 ripdata = &hammer2_chain_rdata(schain)->ipdata; 1022 KKASSERT(schain->pmp == NULL); 1023 spmp->pfs_clid = ripdata->meta.pfs_clid; 1024 1025 /* 1026 * Replace the dummy spmp->iroot with a real one. It's 1027 * easier to just do a wholesale replacement than to try 1028 * to update the chain and fixup the iroot fields. 1029 * 1030 * The returned inode is locked with the supplied cluster. 1031 */ 1032 cluster = hammer2_cluster_from_chain(schain); 1033 hammer2_inode_drop(spmp->iroot); 1034 spmp->iroot = NULL; 1035 spmp->iroot = hammer2_inode_get(spmp, NULL, cluster, -1); 1036 spmp->spmp_hmp = hmp; 1037 spmp->pfs_types[0] = ripdata->meta.pfs_type; 1038 spmp->pfs_hmps[0] = hmp; 1039 hammer2_inode_ref(spmp->iroot); 1040 hammer2_inode_unlock(spmp->iroot); 1041 hammer2_cluster_unlock(cluster); 1042 hammer2_cluster_drop(cluster); 1043 schain = NULL; 1044 /* leave spmp->iroot with one ref */ 1045 1046 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 1047 error = hammer2_recovery(hmp); 1048 /* XXX do something with error */ 1049 } 1050 hammer2_update_pmps(hmp); 1051 hammer2_iocom_init(hmp); 1052 1053 /* 1054 * Ref the cluster management messaging descriptor. The mount 1055 * program deals with the other end of the communications pipe. 1056 * 1057 * Root mounts typically do not supply one. 1058 */ 1059 if (info.cluster_fd >= 0) { 1060 fp = holdfp(curproc->p_fd, info.cluster_fd, -1); 1061 if (fp) { 1062 hammer2_cluster_reconnect(hmp, fp); 1063 } else { 1064 kprintf("hammer2_mount: bad cluster_fd!\n"); 1065 } 1066 } 1067 } else { 1068 spmp = hmp->spmp; 1069 } 1070 1071 /* 1072 * Lookup the mount point under the media-localized super-root. 1073 * Scanning hammer2_pfslist doesn't help us because it represents 1074 * PFS cluster ids which can aggregate several named PFSs together. 1075 * 1076 * cluster->pmp will incorrectly point to spmp and must be fixed 1077 * up later on. 1078 */ 1079 hammer2_inode_lock(spmp->iroot, 0); 1080 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS); 1081 lhc = hammer2_dirhash(label, strlen(label)); 1082 chain = hammer2_chain_lookup(&parent, &key_next, 1083 lhc, lhc + HAMMER2_DIRHASH_LOMASK, 1084 &cache_index, 0); 1085 while (chain) { 1086 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && 1087 strcmp(label, chain->data->ipdata.filename) == 0) { 1088 break; 1089 } 1090 chain = hammer2_chain_next(&parent, chain, &key_next, 1091 key_next, 1092 lhc + HAMMER2_DIRHASH_LOMASK, 1093 &cache_index, 0); 1094 } 1095 if (parent) { 1096 hammer2_chain_unlock(parent); 1097 hammer2_chain_drop(parent); 1098 } 1099 hammer2_inode_unlock(spmp->iroot); 1100 1101 /* 1102 * PFS could not be found? 1103 */ 1104 if (chain == NULL) { 1105 kprintf("hammer2_mount: PFS label not found\n"); 1106 hammer2_unmount_helper(mp, NULL, hmp); 1107 lockmgr(&hammer2_mntlk, LK_RELEASE); 1108 hammer2_vfs_unmount(mp, MNT_FORCE); 1109 1110 return EINVAL; 1111 } 1112 1113 /* 1114 * Acquire the pmp structure (it should have already been allocated 1115 * via hammer2_update_pmps() so do not pass cluster in to add to 1116 * available chains). 1117 * 1118 * Check if the cluster has already been mounted. A cluster can 1119 * only be mounted once, use null mounts to mount additional copies. 1120 */ 1121 ripdata = &chain->data->ipdata; 1122 bref = chain->bref; 1123 pmp = hammer2_pfsalloc(NULL, ripdata, bref.modify_tid); 1124 hammer2_chain_unlock(chain); 1125 hammer2_chain_drop(chain); 1126 1127 if (pmp->mp) { 1128 kprintf("hammer2_mount: PFS already mounted!\n"); 1129 hammer2_unmount_helper(mp, NULL, hmp); 1130 lockmgr(&hammer2_mntlk, LK_RELEASE); 1131 hammer2_vfs_unmount(mp, MNT_FORCE); 1132 1133 return EBUSY; 1134 } 1135 1136 /* 1137 * Finish the mount 1138 */ 1139 kprintf("hammer2_mount hmp=%p pmp=%p\n", hmp, pmp); 1140 1141 mp->mnt_flag = MNT_LOCAL; 1142 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */ 1143 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */ 1144 1145 /* 1146 * required mount structure initializations 1147 */ 1148 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE; 1149 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE; 1150 1151 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE; 1152 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE; 1153 1154 /* 1155 * Optional fields 1156 */ 1157 mp->mnt_iosize_max = MAXPHYS; 1158 1159 /* 1160 * Connect up mount pointers. 1161 */ 1162 hammer2_mount_helper(mp, pmp); 1163 1164 lockmgr(&hammer2_mntlk, LK_RELEASE); 1165 1166 /* 1167 * Finish setup 1168 */ 1169 vfs_getnewfsid(mp); 1170 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops); 1171 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops); 1172 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops); 1173 1174 if (path) { 1175 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, 1176 MNAMELEN - 1, &size); 1177 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 1178 } /* else root mount, already in there */ 1179 1180 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname)); 1181 if (path) { 1182 copyinstr(path, mp->mnt_stat.f_mntonname, 1183 sizeof(mp->mnt_stat.f_mntonname) - 1, 1184 &size); 1185 } else { 1186 /* root mount */ 1187 mp->mnt_stat.f_mntonname[0] = '/'; 1188 } 1189 1190 /* 1191 * Initial statfs to prime mnt_stat. 1192 */ 1193 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred); 1194 1195 return 0; 1196 } 1197 1198 /* 1199 * Scan PFSs under the super-root and create hammer2_pfs structures. 1200 */ 1201 static 1202 void 1203 hammer2_update_pmps(hammer2_dev_t *hmp) 1204 { 1205 const hammer2_inode_data_t *ripdata; 1206 hammer2_chain_t *parent; 1207 hammer2_chain_t *chain; 1208 hammer2_blockref_t bref; 1209 hammer2_pfs_t *spmp; 1210 hammer2_pfs_t *pmp; 1211 hammer2_key_t key_next; 1212 int cache_index = -1; 1213 1214 /* 1215 * Lookup mount point under the media-localized super-root. 1216 * 1217 * cluster->pmp will incorrectly point to spmp and must be fixed 1218 * up later on. 1219 */ 1220 spmp = hmp->spmp; 1221 hammer2_inode_lock(spmp->iroot, 0); 1222 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS); 1223 chain = hammer2_chain_lookup(&parent, &key_next, 1224 HAMMER2_KEY_MIN, HAMMER2_KEY_MAX, 1225 &cache_index, 0); 1226 while (chain) { 1227 if (chain->bref.type != HAMMER2_BREF_TYPE_INODE) 1228 continue; 1229 ripdata = &chain->data->ipdata; 1230 bref = chain->bref; 1231 kprintf("ADD LOCAL PFS: %s\n", ripdata->filename); 1232 1233 pmp = hammer2_pfsalloc(chain, ripdata, bref.modify_tid); 1234 chain = hammer2_chain_next(&parent, chain, &key_next, 1235 key_next, HAMMER2_KEY_MAX, 1236 &cache_index, 0); 1237 } 1238 if (parent) { 1239 hammer2_chain_unlock(parent); 1240 hammer2_chain_drop(parent); 1241 } 1242 hammer2_inode_unlock(spmp->iroot); 1243 } 1244 1245 static 1246 int 1247 hammer2_remount(hammer2_dev_t *hmp, struct mount *mp, char *path __unused, 1248 struct vnode *devvp, struct ucred *cred) 1249 { 1250 int error; 1251 1252 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 1253 error = hammer2_recovery(hmp); 1254 } else { 1255 error = 0; 1256 } 1257 return error; 1258 } 1259 1260 static 1261 int 1262 hammer2_vfs_unmount(struct mount *mp, int mntflags) 1263 { 1264 hammer2_pfs_t *pmp; 1265 int flags; 1266 int error = 0; 1267 1268 pmp = MPTOPMP(mp); 1269 1270 if (pmp == NULL) 1271 return(0); 1272 1273 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE); 1274 1275 /* 1276 * If mount initialization proceeded far enough we must flush 1277 * its vnodes and sync the underlying mount points. Three syncs 1278 * are required to fully flush the filesystem (freemap updates lag 1279 * by one flush, and one extra for safety). 1280 */ 1281 if (mntflags & MNT_FORCE) 1282 flags = FORCECLOSE; 1283 else 1284 flags = 0; 1285 if (pmp->iroot) { 1286 error = vflush(mp, 0, flags); 1287 if (error) 1288 goto failed; 1289 hammer2_vfs_sync(mp, MNT_WAIT); 1290 hammer2_vfs_sync(mp, MNT_WAIT); 1291 hammer2_vfs_sync(mp, MNT_WAIT); 1292 } 1293 1294 /* 1295 * Cleanup the frontend support XOPS threads 1296 */ 1297 hammer2_xop_helper_cleanup(pmp); 1298 1299 /* 1300 * Cleanup our reference on ihidden. 1301 */ 1302 if (pmp->ihidden) { 1303 hammer2_inode_drop(pmp->ihidden); 1304 pmp->ihidden = NULL; 1305 } 1306 if (pmp->mp) 1307 hammer2_unmount_helper(mp, pmp, NULL); 1308 1309 error = 0; 1310 failed: 1311 lockmgr(&hammer2_mntlk, LK_RELEASE); 1312 1313 return (error); 1314 } 1315 1316 /* 1317 * Mount helper, hook the system mount into our PFS. 1318 * The mount lock is held. 1319 * 1320 * We must bump the mount_count on related devices for any 1321 * mounted PFSs. 1322 */ 1323 static 1324 void 1325 hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp) 1326 { 1327 hammer2_cluster_t *cluster; 1328 hammer2_chain_t *rchain; 1329 int i; 1330 1331 mp->mnt_data = (qaddr_t)pmp; 1332 pmp->mp = mp; 1333 1334 /* 1335 * After pmp->mp is set we have to adjust hmp->mount_count. 1336 */ 1337 cluster = &pmp->iroot->cluster; 1338 for (i = 0; i < cluster->nchains; ++i) { 1339 rchain = cluster->array[i].chain; 1340 if (rchain == NULL) 1341 continue; 1342 ++rchain->hmp->mount_count; 1343 kprintf("hammer2_mount hmp=%p ++mount_count=%d\n", 1344 rchain->hmp, rchain->hmp->mount_count); 1345 } 1346 1347 /* 1348 * Create missing Xop threads 1349 */ 1350 hammer2_xop_helper_create(pmp); 1351 } 1352 1353 /* 1354 * Mount helper, unhook the system mount from our PFS. 1355 * The mount lock is held. 1356 * 1357 * If hmp is supplied a mount responsible for being the first to open 1358 * the block device failed and the block device and all PFSs using the 1359 * block device must be cleaned up. 1360 * 1361 * If pmp is supplied multiple devices might be backing the PFS and each 1362 * must be disconnect. This might not be the last PFS using some of the 1363 * underlying devices. Also, we have to adjust our hmp->mount_count 1364 * accounting for the devices backing the pmp which is now undergoing an 1365 * unmount. 1366 */ 1367 static 1368 void 1369 hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, hammer2_dev_t *hmp) 1370 { 1371 hammer2_cluster_t *cluster; 1372 hammer2_chain_t *rchain; 1373 struct vnode *devvp; 1374 int dumpcnt; 1375 int ronly = 0; 1376 int i; 1377 1378 /* 1379 * If no device supplied this is a high-level unmount and we have to 1380 * to disconnect the mount, adjust mount_count, and locate devices 1381 * that might now have no mounts. 1382 */ 1383 if (pmp) { 1384 KKASSERT(hmp == NULL); 1385 KKASSERT((void *)(intptr_t)mp->mnt_data == pmp); 1386 pmp->mp = NULL; 1387 mp->mnt_data = NULL; 1388 1389 /* 1390 * After pmp->mp is cleared we have to account for 1391 * mount_count. 1392 */ 1393 cluster = &pmp->iroot->cluster; 1394 for (i = 0; i < cluster->nchains; ++i) { 1395 rchain = cluster->array[i].chain; 1396 if (rchain == NULL) 1397 continue; 1398 --rchain->hmp->mount_count; 1399 kprintf("hammer2_unmount hmp=%p --mount_count=%d\n", 1400 rchain->hmp, rchain->hmp->mount_count); 1401 /* scrapping hmp now may invalidate the pmp */ 1402 } 1403 again: 1404 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) { 1405 if (hmp->mount_count == 0) { 1406 hammer2_unmount_helper(NULL, NULL, hmp); 1407 goto again; 1408 } 1409 } 1410 return; 1411 } 1412 1413 /* 1414 * Try to terminate the block device. We can't terminate it if 1415 * there are still PFSs referencing it. 1416 */ 1417 kprintf("hammer2_unmount hmp=%p mount_count=%d\n", 1418 hmp, hmp->mount_count); 1419 if (hmp->mount_count) 1420 return; 1421 1422 hammer2_pfsfree_scan(hmp); 1423 hammer2_dev_exlock(hmp); /* XXX order */ 1424 1425 /* 1426 * Cycle the volume data lock as a safety (probably not needed any 1427 * more). To ensure everything is out we need to flush at least 1428 * three times. (1) The running of the unlinkq can dirty the 1429 * filesystem, (2) A normal flush can dirty the freemap, and 1430 * (3) ensure that the freemap is fully synchronized. 1431 * 1432 * The next mount's recovery scan can clean everything up but we want 1433 * to leave the filesystem in a 100% clean state on a normal unmount. 1434 */ 1435 #if 0 1436 hammer2_voldata_lock(hmp); 1437 hammer2_voldata_unlock(hmp); 1438 #endif 1439 hammer2_iocom_uninit(hmp); 1440 1441 if ((hmp->vchain.flags | hmp->fchain.flags) & 1442 HAMMER2_CHAIN_FLUSH_MASK) { 1443 kprintf("hammer2_unmount: chains left over " 1444 "after final sync\n"); 1445 kprintf(" vchain %08x\n", hmp->vchain.flags); 1446 kprintf(" fchain %08x\n", hmp->fchain.flags); 1447 1448 if (hammer2_debug & 0x0010) 1449 Debugger("entered debugger"); 1450 } 1451 1452 KKASSERT(hmp->spmp == NULL); 1453 1454 /* 1455 * Finish up with the device vnode 1456 */ 1457 if ((devvp = hmp->devvp) != NULL) { 1458 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1459 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0); 1460 hmp->devvp = NULL; 1461 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL); 1462 vn_unlock(devvp); 1463 vrele(devvp); 1464 devvp = NULL; 1465 } 1466 1467 /* 1468 * Clear vchain/fchain flags that might prevent final cleanup 1469 * of these chains. 1470 */ 1471 if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) { 1472 atomic_clear_int(&hmp->vchain.flags, 1473 HAMMER2_CHAIN_MODIFIED); 1474 hammer2_pfs_memory_wakeup(hmp->vchain.pmp); 1475 hammer2_chain_drop(&hmp->vchain); 1476 } 1477 if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) { 1478 atomic_clear_int(&hmp->vchain.flags, 1479 HAMMER2_CHAIN_UPDATE); 1480 hammer2_chain_drop(&hmp->vchain); 1481 } 1482 1483 if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) { 1484 atomic_clear_int(&hmp->fchain.flags, 1485 HAMMER2_CHAIN_MODIFIED); 1486 hammer2_pfs_memory_wakeup(hmp->fchain.pmp); 1487 hammer2_chain_drop(&hmp->fchain); 1488 } 1489 if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) { 1490 atomic_clear_int(&hmp->fchain.flags, 1491 HAMMER2_CHAIN_UPDATE); 1492 hammer2_chain_drop(&hmp->fchain); 1493 } 1494 1495 /* 1496 * Final drop of embedded freemap root chain to 1497 * clean up fchain.core (fchain structure is not 1498 * flagged ALLOCATED so it is cleaned out and then 1499 * left to rot). 1500 */ 1501 hammer2_chain_drop(&hmp->fchain); 1502 1503 /* 1504 * Final drop of embedded volume root chain to clean 1505 * up vchain.core (vchain structure is not flagged 1506 * ALLOCATED so it is cleaned out and then left to 1507 * rot). 1508 */ 1509 dumpcnt = 50; 1510 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v'); 1511 dumpcnt = 50; 1512 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f'); 1513 hammer2_dev_unlock(hmp); 1514 hammer2_chain_drop(&hmp->vchain); 1515 1516 hammer2_io_cleanup(hmp, &hmp->iotree); 1517 if (hmp->iofree_count) { 1518 kprintf("io_cleanup: %d I/O's left hanging\n", 1519 hmp->iofree_count); 1520 } 1521 1522 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry); 1523 kmalloc_destroy(&hmp->mchain); 1524 kfree(hmp, M_HAMMER2); 1525 } 1526 1527 static 1528 int 1529 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp, 1530 ino_t ino, struct vnode **vpp) 1531 { 1532 kprintf("hammer2_vget\n"); 1533 return (EOPNOTSUPP); 1534 } 1535 1536 static 1537 int 1538 hammer2_vfs_root(struct mount *mp, struct vnode **vpp) 1539 { 1540 hammer2_pfs_t *pmp; 1541 int error; 1542 struct vnode *vp; 1543 1544 pmp = MPTOPMP(mp); 1545 if (pmp->iroot == NULL) { 1546 *vpp = NULL; 1547 return EINVAL; 1548 } 1549 1550 error = 0; 1551 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED); 1552 1553 while (pmp->inode_tid == 0) { 1554 hammer2_xop_ipcluster_t *xop; 1555 hammer2_inode_meta_t *meta; 1556 1557 xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING); 1558 hammer2_xop_start(&xop->head, hammer2_xop_ipcluster); 1559 error = hammer2_xop_collect(&xop->head, 0); 1560 1561 if (error == 0) { 1562 meta = &xop->head.cluster.focus->data->ipdata.meta; 1563 pmp->iroot->meta = *meta; 1564 pmp->inode_tid = meta->pfs_inum + 1; 1565 if (pmp->inode_tid < HAMMER2_INODE_START) 1566 pmp->inode_tid = HAMMER2_INODE_START; 1567 pmp->modify_tid = 1568 xop->head.cluster.focus->bref.modify_tid + 1; 1569 kprintf("PFS: Starting inode %jd\n", 1570 (intmax_t)pmp->inode_tid); 1571 kprintf("PMP focus good set nextino=%ld mod=%016jx\n", 1572 pmp->inode_tid, pmp->modify_tid); 1573 wakeup(&pmp->iroot); 1574 1575 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1576 1577 /* 1578 * Prime the mount info. 1579 */ 1580 hammer2_vfs_statfs(mp, &mp->mnt_stat, NULL); 1581 1582 /* 1583 * With the cluster operational, check for and 1584 * install ihidden if needed. The install_hidden 1585 * code needs to get a transaction so we must unlock 1586 * iroot around it. 1587 * 1588 * This is only applicable PFS mounts, there is no 1589 * hidden directory in the spmp. 1590 */ 1591 hammer2_inode_unlock(pmp->iroot); 1592 hammer2_inode_install_hidden(pmp); 1593 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED); 1594 1595 break; 1596 } 1597 1598 /* 1599 * Loop, try again 1600 */ 1601 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1602 hammer2_inode_unlock(pmp->iroot); 1603 error = tsleep(&pmp->iroot, PCATCH, "h2root", hz); 1604 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED); 1605 if (error == EINTR) 1606 break; 1607 } 1608 1609 if (error) { 1610 hammer2_inode_unlock(pmp->iroot); 1611 *vpp = NULL; 1612 } else { 1613 vp = hammer2_igetv(pmp->iroot, &error); 1614 hammer2_inode_unlock(pmp->iroot); 1615 *vpp = vp; 1616 } 1617 1618 return (error); 1619 } 1620 1621 /* 1622 * Filesystem status 1623 * 1624 * XXX incorporate ipdata->meta.inode_quota and data_quota 1625 */ 1626 static 1627 int 1628 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 1629 { 1630 hammer2_pfs_t *pmp; 1631 hammer2_dev_t *hmp; 1632 hammer2_blockref_t bref; 1633 int i; 1634 1635 /* 1636 * NOTE: iroot might not have validated the cluster yet. 1637 */ 1638 pmp = MPTOPMP(mp); 1639 1640 mp->mnt_stat.f_files = 0; 1641 mp->mnt_stat.f_ffree = 0; 1642 mp->mnt_stat.f_blocks = 0; 1643 mp->mnt_stat.f_bfree = 0; 1644 mp->mnt_stat.f_bavail = 0; 1645 1646 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { 1647 hmp = pmp->pfs_hmps[i]; 1648 if (hmp == NULL) 1649 continue; 1650 if (pmp->iroot->cluster.array[i].chain) 1651 bref = pmp->iroot->cluster.array[i].chain->bref; 1652 else 1653 bzero(&bref, sizeof(bref)); 1654 1655 mp->mnt_stat.f_files = bref.inode_count; 1656 mp->mnt_stat.f_ffree = 0; 1657 mp->mnt_stat.f_blocks = (bref.data_count + 1658 hmp->voldata.allocator_free) / 1659 mp->mnt_vstat.f_bsize; 1660 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free / 1661 mp->mnt_vstat.f_bsize; 1662 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree; 1663 1664 *sbp = mp->mnt_stat; 1665 } 1666 return (0); 1667 } 1668 1669 static 1670 int 1671 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred) 1672 { 1673 hammer2_pfs_t *pmp; 1674 hammer2_dev_t *hmp; 1675 hammer2_blockref_t bref; 1676 int i; 1677 1678 /* 1679 * NOTE: iroot might not have validated the cluster yet. 1680 */ 1681 pmp = MPTOPMP(mp); 1682 1683 mp->mnt_vstat.f_bsize = 0; 1684 mp->mnt_vstat.f_files = 0; 1685 mp->mnt_vstat.f_ffree = 0; 1686 mp->mnt_vstat.f_blocks = 0; 1687 mp->mnt_vstat.f_bfree = 0; 1688 mp->mnt_vstat.f_bavail = 0; 1689 1690 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { 1691 hmp = pmp->pfs_hmps[i]; 1692 if (hmp == NULL) 1693 continue; 1694 if (pmp->iroot->cluster.array[i].chain) 1695 bref = pmp->iroot->cluster.array[i].chain->bref; 1696 else 1697 bzero(&bref, sizeof(bref)); 1698 1699 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE; 1700 mp->mnt_vstat.f_files = bref.inode_count; 1701 mp->mnt_vstat.f_ffree = 0; 1702 mp->mnt_vstat.f_blocks = (bref.data_count + 1703 hmp->voldata.allocator_free) / 1704 mp->mnt_vstat.f_bsize; 1705 mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free / 1706 mp->mnt_vstat.f_bsize; 1707 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree; 1708 1709 *sbp = mp->mnt_vstat; 1710 } 1711 return (0); 1712 } 1713 1714 /* 1715 * Mount-time recovery (RW mounts) 1716 * 1717 * Updates to the free block table are allowed to lag flushes by one 1718 * transaction. In case of a crash, then on a fresh mount we must do an 1719 * incremental scan of the last committed transaction id and make sure that 1720 * all related blocks have been marked allocated. 1721 * 1722 * The super-root topology and each PFS has its own transaction id domain, 1723 * so we must track PFS boundary transitions. 1724 */ 1725 struct hammer2_recovery_elm { 1726 TAILQ_ENTRY(hammer2_recovery_elm) entry; 1727 hammer2_chain_t *chain; 1728 hammer2_tid_t sync_tid; 1729 }; 1730 1731 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm); 1732 1733 struct hammer2_recovery_info { 1734 struct hammer2_recovery_list list; 1735 hammer2_tid_t mtid; 1736 int depth; 1737 }; 1738 1739 static int hammer2_recovery_scan(hammer2_dev_t *hmp, 1740 hammer2_chain_t *parent, 1741 struct hammer2_recovery_info *info, 1742 hammer2_tid_t sync_tid); 1743 1744 #define HAMMER2_RECOVERY_MAXDEPTH 10 1745 1746 static 1747 int 1748 hammer2_recovery(hammer2_dev_t *hmp) 1749 { 1750 struct hammer2_recovery_info info; 1751 struct hammer2_recovery_elm *elm; 1752 hammer2_chain_t *parent; 1753 hammer2_tid_t sync_tid; 1754 hammer2_tid_t mirror_tid; 1755 int error; 1756 int cumulative_error = 0; 1757 1758 hammer2_trans_init(hmp->spmp, 0); 1759 1760 sync_tid = hmp->voldata.freemap_tid; 1761 mirror_tid = hmp->voldata.mirror_tid; 1762 1763 kprintf("hammer2 mount \"%s\": ", hmp->devrepname); 1764 if (sync_tid >= mirror_tid) { 1765 kprintf(" no recovery needed\n"); 1766 } else { 1767 kprintf(" freemap recovery %016jx-%016jx\n", 1768 sync_tid + 1, mirror_tid); 1769 } 1770 1771 TAILQ_INIT(&info.list); 1772 info.depth = 0; 1773 parent = hammer2_chain_lookup_init(&hmp->vchain, 0); 1774 cumulative_error = hammer2_recovery_scan(hmp, parent, 1775 &info, sync_tid); 1776 hammer2_chain_lookup_done(parent); 1777 1778 while ((elm = TAILQ_FIRST(&info.list)) != NULL) { 1779 TAILQ_REMOVE(&info.list, elm, entry); 1780 parent = elm->chain; 1781 sync_tid = elm->sync_tid; 1782 kfree(elm, M_HAMMER2); 1783 1784 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 1785 error = hammer2_recovery_scan(hmp, parent, 1786 &info, hmp->voldata.freemap_tid); 1787 hammer2_chain_unlock(parent); 1788 hammer2_chain_drop(parent); /* drop elm->chain ref */ 1789 if (error) 1790 cumulative_error = error; 1791 } 1792 hammer2_trans_done(hmp->spmp); 1793 1794 return cumulative_error; 1795 } 1796 1797 static 1798 int 1799 hammer2_recovery_scan(hammer2_dev_t *hmp, hammer2_chain_t *parent, 1800 struct hammer2_recovery_info *info, 1801 hammer2_tid_t sync_tid) 1802 { 1803 const hammer2_inode_data_t *ripdata; 1804 hammer2_chain_t *chain; 1805 int cache_index; 1806 int cumulative_error = 0; 1807 int error; 1808 1809 /* 1810 * Adjust freemap to ensure that the block(s) are marked allocated. 1811 */ 1812 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) { 1813 hammer2_freemap_adjust(hmp, &parent->bref, 1814 HAMMER2_FREEMAP_DORECOVER); 1815 } 1816 1817 /* 1818 * Check type for recursive scan 1819 */ 1820 switch(parent->bref.type) { 1821 case HAMMER2_BREF_TYPE_VOLUME: 1822 /* data already instantiated */ 1823 break; 1824 case HAMMER2_BREF_TYPE_INODE: 1825 /* 1826 * Must instantiate data for DIRECTDATA test and also 1827 * for recursion. 1828 */ 1829 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 1830 ripdata = &hammer2_chain_rdata(parent)->ipdata; 1831 if (ripdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) { 1832 /* not applicable to recovery scan */ 1833 hammer2_chain_unlock(parent); 1834 return 0; 1835 } 1836 hammer2_chain_unlock(parent); 1837 break; 1838 case HAMMER2_BREF_TYPE_INDIRECT: 1839 /* 1840 * Must instantiate data for recursion 1841 */ 1842 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 1843 hammer2_chain_unlock(parent); 1844 break; 1845 case HAMMER2_BREF_TYPE_DATA: 1846 case HAMMER2_BREF_TYPE_FREEMAP: 1847 case HAMMER2_BREF_TYPE_FREEMAP_NODE: 1848 case HAMMER2_BREF_TYPE_FREEMAP_LEAF: 1849 /* not applicable to recovery scan */ 1850 return 0; 1851 break; 1852 default: 1853 return EDOM; 1854 } 1855 1856 /* 1857 * Defer operation if depth limit reached or if we are crossing a 1858 * PFS boundary. 1859 */ 1860 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH) { 1861 struct hammer2_recovery_elm *elm; 1862 1863 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK); 1864 elm->chain = parent; 1865 elm->sync_tid = sync_tid; 1866 hammer2_chain_ref(parent); 1867 TAILQ_INSERT_TAIL(&info->list, elm, entry); 1868 /* unlocked by caller */ 1869 1870 return(0); 1871 } 1872 1873 1874 /* 1875 * Recursive scan of the last flushed transaction only. We are 1876 * doing this without pmp assignments so don't leave the chains 1877 * hanging around after we are done with them. 1878 */ 1879 cache_index = 0; 1880 chain = hammer2_chain_scan(parent, NULL, &cache_index, 1881 HAMMER2_LOOKUP_NODATA); 1882 while (chain) { 1883 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE); 1884 if (chain->bref.mirror_tid > sync_tid) { 1885 ++info->depth; 1886 error = hammer2_recovery_scan(hmp, chain, 1887 info, sync_tid); 1888 --info->depth; 1889 if (error) 1890 cumulative_error = error; 1891 } 1892 1893 /* 1894 * Flush the recovery at the PFS boundary to stage it for 1895 * the final flush of the super-root topology. 1896 */ 1897 if ((chain->bref.flags & HAMMER2_BREF_FLAG_PFSROOT) && 1898 (chain->flags & HAMMER2_CHAIN_ONFLUSH)) { 1899 hammer2_flush(chain, HAMMER2_FLUSH_TOP); 1900 } 1901 chain = hammer2_chain_scan(parent, chain, &cache_index, 1902 HAMMER2_LOOKUP_NODATA); 1903 } 1904 1905 return cumulative_error; 1906 } 1907 1908 /* 1909 * Sync a mount point; this is called on a per-mount basis from the 1910 * filesystem syncer process periodically and whenever a user issues 1911 * a sync. 1912 */ 1913 int 1914 hammer2_vfs_sync(struct mount *mp, int waitfor) 1915 { 1916 hammer2_xop_flush_t *xop; 1917 struct hammer2_sync_info info; 1918 hammer2_inode_t *iroot; 1919 hammer2_pfs_t *pmp; 1920 int flags; 1921 int error; 1922 1923 pmp = MPTOPMP(mp); 1924 iroot = pmp->iroot; 1925 KKASSERT(iroot); 1926 KKASSERT(iroot->pmp == pmp); 1927 1928 /* 1929 * We can't acquire locks on existing vnodes while in a transaction 1930 * without risking a deadlock. This assumes that vfsync() can be 1931 * called without the vnode locked (which it can in DragonFly). 1932 * Otherwise we'd have to implement a multi-pass or flag the lock 1933 * failures and retry. 1934 * 1935 * The reclamation code interlocks with the sync list's token 1936 * (by removing the vnode from the scan list) before unlocking 1937 * the inode, giving us time to ref the inode. 1938 */ 1939 /*flags = VMSC_GETVP;*/ 1940 flags = 0; 1941 if (waitfor & MNT_LAZY) 1942 flags |= VMSC_ONEPASS; 1943 1944 #if 0 1945 /* 1946 * Preflush the vnodes using a normal transaction before interlocking 1947 * with a flush transaction. 1948 */ 1949 hammer2_trans_init(pmp, 0); 1950 info.error = 0; 1951 info.waitfor = MNT_NOWAIT; 1952 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info); 1953 hammer2_trans_done(pmp); 1954 #endif 1955 1956 /* 1957 * Start our flush transaction. This does not return until all 1958 * concurrent transactions have completed and will prevent any 1959 * new transactions from running concurrently, except for the 1960 * buffer cache transactions. 1961 * 1962 * For efficiency do an async pass before making sure with a 1963 * synchronous pass on all related buffer cache buffers. It 1964 * should theoretically not be possible for any new file buffers 1965 * to be instantiated during this sequence. 1966 */ 1967 hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH | 1968 HAMMER2_TRANS_PREFLUSH); 1969 hammer2_inode_run_unlinkq(pmp); 1970 1971 info.error = 0; 1972 info.waitfor = MNT_NOWAIT; 1973 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info); 1974 info.waitfor = MNT_WAIT; 1975 vsyncscan(mp, flags, hammer2_sync_scan2, &info); 1976 1977 /* 1978 * Clear PREFLUSH. This prevents (or asserts on) any new logical 1979 * buffer cache flushes which occur during the flush. Device buffers 1980 * are not affected. 1981 */ 1982 hammer2_bioq_sync(pmp); 1983 hammer2_trans_clear_preflush(pmp); 1984 1985 /* 1986 * Use the XOP interface to concurrently flush all nodes to 1987 * synchronize the PFSROOT subtopology to the media. A standard 1988 * end-of-scan ENOENT error indicates cluster sufficiency. 1989 * 1990 * Note that this flush will not be visible on crash recovery until 1991 * we flush the super-root topology in the next loop. 1992 * 1993 * XXX For now wait for all flushes to complete. 1994 */ 1995 if (iroot) { 1996 xop = hammer2_xop_alloc(iroot, HAMMER2_XOP_MODIFYING); 1997 hammer2_xop_start(&xop->head, hammer2_inode_xop_flush); 1998 error = hammer2_xop_collect(&xop->head, 1999 HAMMER2_XOP_COLLECT_WAITALL); 2000 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 2001 if (error == ENOENT) 2002 error = 0; 2003 } else { 2004 error = 0; 2005 } 2006 hammer2_trans_done(pmp); 2007 2008 return (error); 2009 } 2010 2011 /* 2012 * Sync passes. 2013 * 2014 * Note that we ignore the tranasction mtid we got above. Instead, 2015 * each vfsync below will ultimately get its own via TRANS_BUFCACHE 2016 * transactions. 2017 */ 2018 static int 2019 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data) 2020 { 2021 struct hammer2_sync_info *info = data; 2022 hammer2_inode_t *ip; 2023 int error; 2024 2025 /* 2026 * Degenerate cases. Note that ip == NULL typically means the 2027 * syncer vnode itself and we don't want to vclrisdirty() in that 2028 * situation. 2029 */ 2030 ip = VTOI(vp); 2031 if (ip == NULL) { 2032 return(0); 2033 } 2034 if (vp->v_type == VNON || vp->v_type == VBAD) { 2035 vclrisdirty(vp); 2036 return(0); 2037 } 2038 2039 /* 2040 * VOP_FSYNC will start a new transaction so replicate some code 2041 * here to do it inline (see hammer2_vop_fsync()). 2042 * 2043 * WARNING: The vfsync interacts with the buffer cache and might 2044 * block, we can't hold the inode lock at that time. 2045 * However, we MUST ref ip before blocking to ensure that 2046 * it isn't ripped out from under us (since we do not 2047 * hold a lock on the vnode). 2048 */ 2049 hammer2_inode_ref(ip); 2050 if ((ip->flags & HAMMER2_INODE_MODIFIED) || 2051 !RB_EMPTY(&vp->v_rbdirty_tree)) { 2052 vfsync(vp, info->waitfor, 1, NULL, NULL); 2053 if (ip->flags & (HAMMER2_INODE_RESIZED | 2054 HAMMER2_INODE_MODIFIED)) { 2055 hammer2_inode_lock(ip, 0); 2056 hammer2_inode_chain_sync(ip); 2057 hammer2_inode_unlock(ip); 2058 } 2059 } 2060 if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 && 2061 RB_EMPTY(&vp->v_rbdirty_tree)) { 2062 vclrisdirty(vp); 2063 } 2064 2065 hammer2_inode_drop(ip); 2066 #if 1 2067 error = 0; 2068 if (error) 2069 info->error = error; 2070 #endif 2071 return(0); 2072 } 2073 2074 static 2075 int 2076 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp) 2077 { 2078 return (0); 2079 } 2080 2081 static 2082 int 2083 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 2084 struct fid *fhp, struct vnode **vpp) 2085 { 2086 return (0); 2087 } 2088 2089 static 2090 int 2091 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 2092 int *exflagsp, struct ucred **credanonp) 2093 { 2094 return (0); 2095 } 2096 2097 /* 2098 * Support code for hammer2_vfs_mount(). Read, verify, and install the volume 2099 * header into the HMP 2100 * 2101 * XXX read four volhdrs and use the one with the highest TID whos CRC 2102 * matches. 2103 * 2104 * XXX check iCRCs. 2105 * 2106 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to 2107 * nonexistant locations. 2108 * 2109 * XXX Record selected volhdr and ring updates to each of 4 volhdrs 2110 */ 2111 static 2112 int 2113 hammer2_install_volume_header(hammer2_dev_t *hmp) 2114 { 2115 hammer2_volume_data_t *vd; 2116 struct buf *bp; 2117 hammer2_crc32_t crc0, crc, bcrc0, bcrc; 2118 int error_reported; 2119 int error; 2120 int valid; 2121 int i; 2122 2123 error_reported = 0; 2124 error = 0; 2125 valid = 0; 2126 bp = NULL; 2127 2128 /* 2129 * There are up to 4 copies of the volume header (syncs iterate 2130 * between them so there is no single master). We don't trust the 2131 * volu_size field so we don't know precisely how large the filesystem 2132 * is, so depend on the OS to return an error if we go beyond the 2133 * block device's EOF. 2134 */ 2135 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) { 2136 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64, 2137 HAMMER2_VOLUME_BYTES, &bp); 2138 if (error) { 2139 brelse(bp); 2140 bp = NULL; 2141 continue; 2142 } 2143 2144 vd = (struct hammer2_volume_data *) bp->b_data; 2145 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) && 2146 (vd->magic != HAMMER2_VOLUME_ID_ABO)) { 2147 brelse(bp); 2148 bp = NULL; 2149 continue; 2150 } 2151 2152 if (vd->magic == HAMMER2_VOLUME_ID_ABO) { 2153 /* XXX: Reversed-endianness filesystem */ 2154 kprintf("hammer2: reverse-endian filesystem detected"); 2155 brelse(bp); 2156 bp = NULL; 2157 continue; 2158 } 2159 2160 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0]; 2161 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF, 2162 HAMMER2_VOLUME_ICRC0_SIZE); 2163 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1]; 2164 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF, 2165 HAMMER2_VOLUME_ICRC1_SIZE); 2166 if ((crc0 != crc) || (bcrc0 != bcrc)) { 2167 kprintf("hammer2 volume header crc " 2168 "mismatch copy #%d %08x/%08x\n", 2169 i, crc0, crc); 2170 error_reported = 1; 2171 brelse(bp); 2172 bp = NULL; 2173 continue; 2174 } 2175 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) { 2176 valid = 1; 2177 hmp->voldata = *vd; 2178 hmp->volhdrno = i; 2179 } 2180 brelse(bp); 2181 bp = NULL; 2182 } 2183 if (valid) { 2184 hmp->volsync = hmp->voldata; 2185 error = 0; 2186 if (error_reported || bootverbose || 1) { /* 1/DEBUG */ 2187 kprintf("hammer2: using volume header #%d\n", 2188 hmp->volhdrno); 2189 } 2190 } else { 2191 error = EINVAL; 2192 kprintf("hammer2: no valid volume headers found!\n"); 2193 } 2194 return (error); 2195 } 2196 2197 /* 2198 * This handles hysteresis on regular file flushes. Because the BIOs are 2199 * routed to a thread it is possible for an excessive number to build up 2200 * and cause long front-end stalls long before the runningbuffspace limit 2201 * is hit, so we implement hammer2_flush_pipe to control the 2202 * hysteresis. 2203 * 2204 * This is a particular problem when compression is used. 2205 */ 2206 void 2207 hammer2_lwinprog_ref(hammer2_pfs_t *pmp) 2208 { 2209 atomic_add_int(&pmp->count_lwinprog, 1); 2210 } 2211 2212 void 2213 hammer2_lwinprog_drop(hammer2_pfs_t *pmp) 2214 { 2215 int lwinprog; 2216 2217 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1); 2218 if ((lwinprog & HAMMER2_LWINPROG_WAITING) && 2219 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) { 2220 atomic_clear_int(&pmp->count_lwinprog, 2221 HAMMER2_LWINPROG_WAITING); 2222 wakeup(&pmp->count_lwinprog); 2223 } 2224 if ((lwinprog & HAMMER2_LWINPROG_WAITING0) && 2225 (lwinprog & HAMMER2_LWINPROG_MASK) <= 0) { 2226 atomic_clear_int(&pmp->count_lwinprog, 2227 HAMMER2_LWINPROG_WAITING0); 2228 wakeup(&pmp->count_lwinprog); 2229 } 2230 } 2231 2232 void 2233 hammer2_lwinprog_wait(hammer2_pfs_t *pmp, int flush_pipe) 2234 { 2235 int lwinprog; 2236 int lwflag = (flush_pipe) ? HAMMER2_LWINPROG_WAITING : 2237 HAMMER2_LWINPROG_WAITING0; 2238 2239 for (;;) { 2240 lwinprog = pmp->count_lwinprog; 2241 cpu_ccfence(); 2242 if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe) 2243 break; 2244 tsleep_interlock(&pmp->count_lwinprog, 0); 2245 atomic_set_int(&pmp->count_lwinprog, lwflag); 2246 lwinprog = pmp->count_lwinprog; 2247 if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe) 2248 break; 2249 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz); 2250 } 2251 } 2252 2253 /* 2254 * Manage excessive memory resource use for chain and related 2255 * structures. 2256 */ 2257 void 2258 hammer2_pfs_memory_wait(hammer2_pfs_t *pmp) 2259 { 2260 uint32_t waiting; 2261 uint32_t count; 2262 uint32_t limit; 2263 #if 0 2264 static int zzticks; 2265 #endif 2266 2267 /* 2268 * Atomic check condition and wait. Also do an early speedup of 2269 * the syncer to try to avoid hitting the wait. 2270 */ 2271 for (;;) { 2272 waiting = pmp->inmem_dirty_chains; 2273 cpu_ccfence(); 2274 count = waiting & HAMMER2_DIRTYCHAIN_MASK; 2275 2276 limit = pmp->mp->mnt_nvnodelistsize / 10; 2277 if (limit < hammer2_limit_dirty_chains) 2278 limit = hammer2_limit_dirty_chains; 2279 if (limit < 1000) 2280 limit = 1000; 2281 2282 #if 0 2283 if ((int)(ticks - zzticks) > hz) { 2284 zzticks = ticks; 2285 kprintf("count %ld %ld\n", count, limit); 2286 } 2287 #endif 2288 2289 /* 2290 * Block if there are too many dirty chains present, wait 2291 * for the flush to clean some out. 2292 */ 2293 if (count > limit) { 2294 tsleep_interlock(&pmp->inmem_dirty_chains, 0); 2295 if (atomic_cmpset_int(&pmp->inmem_dirty_chains, 2296 waiting, 2297 waiting | HAMMER2_DIRTYCHAIN_WAITING)) { 2298 speedup_syncer(pmp->mp); 2299 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED, 2300 "chnmem", hz); 2301 } 2302 continue; /* loop on success or fail */ 2303 } 2304 2305 /* 2306 * Try to start an early flush before we are forced to block. 2307 */ 2308 if (count > limit * 7 / 10) 2309 speedup_syncer(pmp->mp); 2310 break; 2311 } 2312 } 2313 2314 void 2315 hammer2_pfs_memory_inc(hammer2_pfs_t *pmp) 2316 { 2317 if (pmp) { 2318 atomic_add_int(&pmp->inmem_dirty_chains, 1); 2319 } 2320 } 2321 2322 void 2323 hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp) 2324 { 2325 uint32_t waiting; 2326 2327 if (pmp == NULL) 2328 return; 2329 2330 for (;;) { 2331 waiting = pmp->inmem_dirty_chains; 2332 cpu_ccfence(); 2333 if (atomic_cmpset_int(&pmp->inmem_dirty_chains, 2334 waiting, 2335 (waiting - 1) & 2336 ~HAMMER2_DIRTYCHAIN_WAITING)) { 2337 break; 2338 } 2339 } 2340 2341 if (waiting & HAMMER2_DIRTYCHAIN_WAITING) 2342 wakeup(&pmp->inmem_dirty_chains); 2343 } 2344 2345 /* 2346 * Debugging 2347 */ 2348 void 2349 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx) 2350 { 2351 hammer2_chain_t *scan; 2352 hammer2_chain_t *parent; 2353 2354 --*countp; 2355 if (*countp == 0) { 2356 kprintf("%*.*s...\n", tab, tab, ""); 2357 return; 2358 } 2359 if (*countp < 0) 2360 return; 2361 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n", 2362 tab, tab, "", pfx, 2363 chain, chain->bref.type, 2364 chain->bref.key, chain->bref.keybits, 2365 chain->bref.mirror_tid); 2366 2367 kprintf("%*.*s [%08x] (%s) refs=%d", 2368 tab, tab, "", 2369 chain->flags, 2370 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE && 2371 chain->data) ? (char *)chain->data->ipdata.filename : "?"), 2372 chain->refs); 2373 2374 parent = chain->parent; 2375 if (parent) 2376 kprintf("\n%*.*s p=%p [pflags %08x prefs %d", 2377 tab, tab, "", 2378 parent, parent->flags, parent->refs); 2379 if (RB_EMPTY(&chain->core.rbtree)) { 2380 kprintf("\n"); 2381 } else { 2382 kprintf(" {\n"); 2383 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree) 2384 hammer2_dump_chain(scan, tab + 4, countp, 'a'); 2385 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data) 2386 kprintf("%*.*s}(%s)\n", tab, tab, "", 2387 chain->data->ipdata.filename); 2388 else 2389 kprintf("%*.*s}\n", tab, tab, ""); 2390 } 2391 } 2392