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