1 /* 2 * Copyright (c) 2011-2018 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 int pass; 73 }; 74 75 TAILQ_HEAD(hammer2_mntlist, hammer2_dev); 76 static struct hammer2_mntlist hammer2_mntlist; 77 78 struct hammer2_pfslist hammer2_pfslist; 79 struct hammer2_pfslist hammer2_spmplist; 80 struct lock hammer2_mntlk; 81 82 int hammer2_supported_version = HAMMER2_VOL_VERSION_DEFAULT; 83 int hammer2_debug; 84 int hammer2_cluster_meta_read = 1; /* physical read-ahead */ 85 int hammer2_cluster_data_read = 4; /* physical read-ahead */ 86 int hammer2_cluster_write = 0; /* physical write clustering */ 87 int hammer2_dedup_enable = 1; 88 int hammer2_always_compress = 0; /* always try to compress */ 89 int hammer2_inval_enable = 0; 90 int hammer2_flush_pipe = 100; 91 int hammer2_dio_count; 92 int hammer2_dio_limit = 256; 93 int hammer2_bulkfree_tps = 5000; 94 long hammer2_chain_allocs; 95 long hammer2_chain_frees; 96 long hammer2_limit_dirty_chains; 97 long hammer2_limit_dirty_inodes; 98 long hammer2_count_modified_chains; 99 long hammer2_iod_invals; 100 long hammer2_iod_file_read; 101 long hammer2_iod_meta_read; 102 long hammer2_iod_indr_read; 103 long hammer2_iod_fmap_read; 104 long hammer2_iod_volu_read; 105 long hammer2_iod_file_write; 106 long hammer2_iod_file_wembed; 107 long hammer2_iod_file_wzero; 108 long hammer2_iod_file_wdedup; 109 long hammer2_iod_meta_write; 110 long hammer2_iod_indr_write; 111 long hammer2_iod_fmap_write; 112 long hammer2_iod_volu_write; 113 114 MALLOC_DECLARE(M_HAMMER2_CBUFFER); 115 MALLOC_DEFINE(M_HAMMER2_CBUFFER, "HAMMER2-compbuffer", 116 "Buffer used for compression."); 117 118 MALLOC_DECLARE(M_HAMMER2_DEBUFFER); 119 MALLOC_DEFINE(M_HAMMER2_DEBUFFER, "HAMMER2-decompbuffer", 120 "Buffer used for decompression."); 121 122 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem"); 123 124 SYSCTL_INT(_vfs_hammer2, OID_AUTO, supported_version, CTLFLAG_RD, 125 &hammer2_supported_version, 0, ""); 126 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW, 127 &hammer2_debug, 0, ""); 128 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_meta_read, CTLFLAG_RW, 129 &hammer2_cluster_meta_read, 0, ""); 130 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_data_read, CTLFLAG_RW, 131 &hammer2_cluster_data_read, 0, ""); 132 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_write, CTLFLAG_RW, 133 &hammer2_cluster_write, 0, ""); 134 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dedup_enable, CTLFLAG_RW, 135 &hammer2_dedup_enable, 0, ""); 136 SYSCTL_INT(_vfs_hammer2, OID_AUTO, always_compress, CTLFLAG_RW, 137 &hammer2_always_compress, 0, ""); 138 SYSCTL_INT(_vfs_hammer2, OID_AUTO, inval_enable, CTLFLAG_RW, 139 &hammer2_inval_enable, 0, ""); 140 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW, 141 &hammer2_flush_pipe, 0, ""); 142 SYSCTL_INT(_vfs_hammer2, OID_AUTO, bulkfree_tps, CTLFLAG_RW, 143 &hammer2_bulkfree_tps, 0, ""); 144 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, chain_allocs, CTLFLAG_RW, 145 &hammer2_chain_allocs, 0, ""); 146 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, chain_frees, CTLFLAG_RW, 147 &hammer2_chain_frees, 0, ""); 148 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW, 149 &hammer2_limit_dirty_chains, 0, ""); 150 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_inodes, CTLFLAG_RW, 151 &hammer2_limit_dirty_inodes, 0, ""); 152 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, count_modified_chains, CTLFLAG_RW, 153 &hammer2_count_modified_chains, 0, ""); 154 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD, 155 &hammer2_dio_count, 0, ""); 156 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_limit, CTLFLAG_RW, 157 &hammer2_dio_limit, 0, ""); 158 159 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_invals, CTLFLAG_RW, 160 &hammer2_iod_invals, 0, ""); 161 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW, 162 &hammer2_iod_file_read, 0, ""); 163 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW, 164 &hammer2_iod_meta_read, 0, ""); 165 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW, 166 &hammer2_iod_indr_read, 0, ""); 167 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW, 168 &hammer2_iod_fmap_read, 0, ""); 169 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW, 170 &hammer2_iod_volu_read, 0, ""); 171 172 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW, 173 &hammer2_iod_file_write, 0, ""); 174 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wembed, CTLFLAG_RW, 175 &hammer2_iod_file_wembed, 0, ""); 176 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wzero, CTLFLAG_RW, 177 &hammer2_iod_file_wzero, 0, ""); 178 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wdedup, CTLFLAG_RW, 179 &hammer2_iod_file_wdedup, 0, ""); 180 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW, 181 &hammer2_iod_meta_write, 0, ""); 182 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW, 183 &hammer2_iod_indr_write, 0, ""); 184 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW, 185 &hammer2_iod_fmap_write, 0, ""); 186 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW, 187 &hammer2_iod_volu_write, 0, ""); 188 189 long hammer2_process_icrc32; 190 long hammer2_process_xxhash64; 191 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, process_icrc32, CTLFLAG_RW, 192 &hammer2_process_icrc32, 0, ""); 193 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, process_xxhash64, CTLFLAG_RW, 194 &hammer2_process_xxhash64, 0, ""); 195 196 static int hammer2_vfs_init(struct vfsconf *conf); 197 static int hammer2_vfs_uninit(struct vfsconf *vfsp); 198 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data, 199 struct ucred *cred); 200 static int hammer2_remount(hammer2_dev_t *, struct mount *, char *, 201 struct vnode *, struct ucred *); 202 static int hammer2_recovery(hammer2_dev_t *hmp); 203 static int hammer2_vfs_unmount(struct mount *mp, int mntflags); 204 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp); 205 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, 206 struct ucred *cred); 207 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, 208 struct ucred *cred); 209 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 210 struct fid *fhp, struct vnode **vpp); 211 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp); 212 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 213 int *exflagsp, struct ucred **credanonp); 214 215 static int hammer2_install_volume_header(hammer2_dev_t *hmp); 216 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data); 217 218 static void hammer2_update_pmps(hammer2_dev_t *hmp); 219 220 static void hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp); 221 static void hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, 222 hammer2_dev_t *hmp); 223 static int hammer2_fixup_pfses(hammer2_dev_t *hmp); 224 225 /* 226 * HAMMER2 vfs operations. 227 */ 228 static struct vfsops hammer2_vfsops = { 229 .vfs_init = hammer2_vfs_init, 230 .vfs_uninit = hammer2_vfs_uninit, 231 .vfs_sync = hammer2_vfs_sync, 232 .vfs_mount = hammer2_vfs_mount, 233 .vfs_unmount = hammer2_vfs_unmount, 234 .vfs_root = hammer2_vfs_root, 235 .vfs_statfs = hammer2_vfs_statfs, 236 .vfs_statvfs = hammer2_vfs_statvfs, 237 .vfs_vget = hammer2_vfs_vget, 238 .vfs_vptofh = hammer2_vfs_vptofh, 239 .vfs_fhtovp = hammer2_vfs_fhtovp, 240 .vfs_checkexp = hammer2_vfs_checkexp 241 }; 242 243 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", ""); 244 245 VFS_SET(hammer2_vfsops, hammer2, VFCF_MPSAFE); 246 MODULE_VERSION(hammer2, 1); 247 248 static 249 int 250 hammer2_vfs_init(struct vfsconf *conf) 251 { 252 static struct objcache_malloc_args margs_read; 253 static struct objcache_malloc_args margs_write; 254 static struct objcache_malloc_args margs_vop; 255 256 int error; 257 258 error = 0; 259 260 /* 261 * A large DIO cache is needed to retain dedup enablement masks. 262 * The bulkfree code clears related masks as part of the disk block 263 * recycling algorithm, preventing it from being used for a later 264 * dedup. 265 * 266 * NOTE: A large buffer cache can actually interfere with dedup 267 * operation because we dedup based on media physical buffers 268 * and not logical buffers. Try to make the DIO case large 269 * enough to avoid this problem, but also cap it. 270 */ 271 hammer2_dio_limit = nbuf * 2; 272 if (hammer2_dio_limit > 100000) 273 hammer2_dio_limit = 100000; 274 275 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref)) 276 error = EINVAL; 277 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data)) 278 error = EINVAL; 279 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data)) 280 error = EINVAL; 281 282 if (error) 283 kprintf("HAMMER2 structure size mismatch; cannot continue.\n"); 284 285 margs_read.objsize = 65536; 286 margs_read.mtype = M_HAMMER2_DEBUFFER; 287 288 margs_write.objsize = 32768; 289 margs_write.mtype = M_HAMMER2_CBUFFER; 290 291 margs_vop.objsize = sizeof(hammer2_xop_t); 292 margs_vop.mtype = M_HAMMER2; 293 294 /* 295 * Note thaht for the XOPS cache we want backing store allocations 296 * to use M_ZERO. This is not allowed in objcache_get() (to avoid 297 * confusion), so use the backing store function that does it. This 298 * means that initial XOPS objects are zerod but REUSED objects are 299 * not. So we are responsible for cleaning the object up sufficiently 300 * for our needs before objcache_put()ing it back (typically just the 301 * FIFO indices). 302 */ 303 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc, 304 0, 1, NULL, NULL, NULL, 305 objcache_malloc_alloc, 306 objcache_malloc_free, 307 &margs_read); 308 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc, 309 0, 1, NULL, NULL, NULL, 310 objcache_malloc_alloc, 311 objcache_malloc_free, 312 &margs_write); 313 cache_xops = objcache_create(margs_vop.mtype->ks_shortdesc, 314 0, 1, NULL, NULL, NULL, 315 objcache_malloc_alloc_zero, 316 objcache_malloc_free, 317 &margs_vop); 318 319 320 lockinit(&hammer2_mntlk, "mntlk", 0, 0); 321 TAILQ_INIT(&hammer2_mntlist); 322 TAILQ_INIT(&hammer2_pfslist); 323 TAILQ_INIT(&hammer2_spmplist); 324 325 hammer2_limit_dirty_chains = maxvnodes / 10; 326 if (hammer2_limit_dirty_chains > HAMMER2_LIMIT_DIRTY_CHAINS) 327 hammer2_limit_dirty_chains = HAMMER2_LIMIT_DIRTY_CHAINS; 328 329 hammer2_limit_dirty_inodes = maxvnodes / 100; 330 if (hammer2_limit_dirty_inodes < 100) 331 hammer2_limit_dirty_inodes = 100; 332 if (hammer2_limit_dirty_inodes > HAMMER2_LIMIT_DIRTY_INODES) 333 hammer2_limit_dirty_inodes = HAMMER2_LIMIT_DIRTY_INODES; 334 335 return (error); 336 } 337 338 static 339 int 340 hammer2_vfs_uninit(struct vfsconf *vfsp __unused) 341 { 342 objcache_destroy(cache_buffer_read); 343 objcache_destroy(cache_buffer_write); 344 objcache_destroy(cache_xops); 345 return 0; 346 } 347 348 /* 349 * Core PFS allocator. Used to allocate or reference the pmp structure 350 * for PFS cluster mounts and the spmp structure for media (hmp) structures. 351 * The pmp can be passed in or loaded by this function using the chain and 352 * inode data. 353 * 354 * pmp->modify_tid tracks new modify_tid transaction ids for front-end 355 * transactions. Note that synchronization does not use this field. 356 * (typically frontend operations and synchronization cannot run on the 357 * same PFS node at the same time). 358 * 359 * XXX check locking 360 */ 361 hammer2_pfs_t * 362 hammer2_pfsalloc(hammer2_chain_t *chain, 363 const hammer2_inode_data_t *ripdata, 364 hammer2_tid_t modify_tid, hammer2_dev_t *force_local) 365 { 366 hammer2_pfs_t *pmp; 367 hammer2_inode_t *iroot; 368 int count; 369 int i; 370 int j; 371 372 pmp = NULL; 373 374 /* 375 * Locate or create the PFS based on the cluster id. If ripdata 376 * is NULL this is a spmp which is unique and is always allocated. 377 * 378 * If the device is mounted in local mode all PFSs are considered 379 * independent and not part of any cluster (for debugging only). 380 */ 381 if (ripdata) { 382 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) { 383 if (force_local != pmp->force_local) 384 continue; 385 if (force_local == NULL && 386 bcmp(&pmp->pfs_clid, &ripdata->meta.pfs_clid, 387 sizeof(pmp->pfs_clid)) == 0) { 388 break; 389 } else if (force_local && pmp->pfs_names[0] && 390 strcmp(pmp->pfs_names[0], ripdata->filename) == 0) { 391 break; 392 } 393 } 394 } 395 396 if (pmp == NULL) { 397 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO); 398 pmp->force_local = force_local; 399 hammer2_trans_manage_init(pmp); 400 kmalloc_create(&pmp->minode, "HAMMER2-inodes"); 401 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg"); 402 lockinit(&pmp->lock, "pfslk", 0, 0); 403 lockinit(&pmp->lock_nlink, "h2nlink", 0, 0); 404 spin_init(&pmp->inum_spin, "hm2pfsalloc_inum"); 405 spin_init(&pmp->xop_spin, "h2xop"); 406 spin_init(&pmp->lru_spin, "h2lru"); 407 RB_INIT(&pmp->inum_tree); 408 TAILQ_INIT(&pmp->sideq); 409 TAILQ_INIT(&pmp->lru_list); 410 spin_init(&pmp->list_spin, "hm2pfsalloc_list"); 411 412 /* 413 * Distribute backend operations to threads 414 */ 415 for (i = 0; i < HAMMER2_XOPGROUPS; ++i) 416 hammer2_xop_group_init(pmp, &pmp->xop_groups[i]); 417 418 /* 419 * Save the last media transaction id for the flusher. Set 420 * initial 421 */ 422 if (ripdata) { 423 pmp->pfs_clid = ripdata->meta.pfs_clid; 424 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry); 425 } else { 426 pmp->flags |= HAMMER2_PMPF_SPMP; 427 TAILQ_INSERT_TAIL(&hammer2_spmplist, pmp, mntentry); 428 } 429 430 /* 431 * The synchronization thread may start too early, make 432 * sure it stays frozen until we are ready to let it go. 433 * XXX 434 */ 435 /* 436 pmp->primary_thr.flags = HAMMER2_THREAD_FROZEN | 437 HAMMER2_THREAD_REMASTER; 438 */ 439 } 440 441 /* 442 * Create the PFS's root inode and any missing XOP helper threads. 443 */ 444 if ((iroot = pmp->iroot) == NULL) { 445 iroot = hammer2_inode_get(pmp, NULL, NULL, -1); 446 if (ripdata) 447 iroot->meta = ripdata->meta; 448 pmp->iroot = iroot; 449 hammer2_inode_ref(iroot); 450 hammer2_inode_unlock(iroot); 451 } 452 453 /* 454 * Stop here if no chain is passed in. 455 */ 456 if (chain == NULL) 457 goto done; 458 459 /* 460 * When a chain is passed in we must add it to the PFS's root 461 * inode, update pmp->pfs_types[], and update the syncronization 462 * threads. 463 * 464 * When forcing local mode, mark the PFS as a MASTER regardless. 465 * 466 * At the moment empty spots can develop due to removals or failures. 467 * Ultimately we want to re-fill these spots but doing so might 468 * confused running code. XXX 469 */ 470 hammer2_inode_ref(iroot); 471 hammer2_mtx_ex(&iroot->lock); 472 j = iroot->cluster.nchains; 473 474 if (j == HAMMER2_MAXCLUSTER) { 475 kprintf("hammer2_mount: cluster full!\n"); 476 /* XXX fatal error? */ 477 } else { 478 KKASSERT(chain->pmp == NULL); 479 chain->pmp = pmp; 480 hammer2_chain_ref(chain); 481 iroot->cluster.array[j].chain = chain; 482 if (force_local) 483 pmp->pfs_types[j] = HAMMER2_PFSTYPE_MASTER; 484 else 485 pmp->pfs_types[j] = ripdata->meta.pfs_type; 486 pmp->pfs_names[j] = kstrdup(ripdata->filename, M_HAMMER2); 487 pmp->pfs_hmps[j] = chain->hmp; 488 489 /* 490 * If the PFS is already mounted we must account 491 * for the mount_count here. 492 */ 493 if (pmp->mp) 494 ++chain->hmp->mount_count; 495 496 /* 497 * May have to fixup dirty chain tracking. Previous 498 * pmp was NULL so nothing to undo. 499 */ 500 if (chain->flags & HAMMER2_CHAIN_MODIFIED) 501 hammer2_pfs_memory_inc(pmp); 502 ++j; 503 } 504 iroot->cluster.nchains = j; 505 506 /* 507 * Update nmasters from any PFS inode which is part of the cluster. 508 * It is possible that this will result in a value which is too 509 * high. MASTER PFSs are authoritative for pfs_nmasters and will 510 * override this value later on. 511 * 512 * (This informs us of masters that might not currently be 513 * discoverable by this mount). 514 */ 515 if (ripdata && pmp->pfs_nmasters < ripdata->meta.pfs_nmasters) { 516 pmp->pfs_nmasters = ripdata->meta.pfs_nmasters; 517 } 518 519 /* 520 * Count visible masters. Masters are usually added with 521 * ripdata->meta.pfs_nmasters set to 1. This detects when there 522 * are more (XXX and must update the master inodes). 523 */ 524 count = 0; 525 for (i = 0; i < iroot->cluster.nchains; ++i) { 526 if (pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) 527 ++count; 528 } 529 if (pmp->pfs_nmasters < count) 530 pmp->pfs_nmasters = count; 531 532 /* 533 * Create missing synchronization and support threads. 534 * 535 * Single-node masters (including snapshots) have nothing to 536 * synchronize and do not require this thread. 537 * 538 * Multi-node masters or any number of soft masters, slaves, copy, 539 * or other PFS types need the thread. 540 * 541 * Each thread is responsible for its particular cluster index. 542 * We use independent threads so stalls or mismatches related to 543 * any given target do not affect other targets. 544 */ 545 for (i = 0; i < iroot->cluster.nchains; ++i) { 546 /* 547 * Single-node masters (including snapshots) have nothing 548 * to synchronize and will make direct xops support calls, 549 * thus they do not require this thread. 550 * 551 * Note that there can be thousands of snapshots. We do not 552 * want to create thousands of threads. 553 */ 554 if (pmp->pfs_nmasters <= 1 && 555 pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) { 556 continue; 557 } 558 559 /* 560 * Sync support thread 561 */ 562 if (pmp->sync_thrs[i].td == NULL) { 563 hammer2_thr_create(&pmp->sync_thrs[i], pmp, NULL, 564 "h2nod", i, -1, 565 hammer2_primary_sync_thread); 566 } 567 } 568 569 /* 570 * Create missing Xop threads 571 * 572 * NOTE: We create helper threads for all mounted PFSs or any 573 * PFSs with 2+ nodes (so the sync thread can update them, 574 * even if not mounted). 575 */ 576 if (pmp->mp || iroot->cluster.nchains >= 2) 577 hammer2_xop_helper_create(pmp); 578 579 hammer2_mtx_unlock(&iroot->lock); 580 hammer2_inode_drop(iroot); 581 done: 582 return pmp; 583 } 584 585 /* 586 * Deallocate an element of a probed PFS. If destroying and this is a 587 * MASTER, adjust nmasters. 588 * 589 * This function does not physically destroy the PFS element in its device 590 * under the super-root (see hammer2_ioctl_pfs_delete()). 591 */ 592 void 593 hammer2_pfsdealloc(hammer2_pfs_t *pmp, int clindex, int destroying) 594 { 595 hammer2_inode_t *iroot; 596 hammer2_chain_t *chain; 597 int j; 598 599 /* 600 * Cleanup our reference on iroot. iroot is (should) not be needed 601 * by the flush code. 602 */ 603 iroot = pmp->iroot; 604 if (iroot) { 605 /* 606 * Stop synchronizing 607 * 608 * XXX flush after acquiring the iroot lock. 609 * XXX clean out the cluster index from all inode structures. 610 */ 611 hammer2_thr_delete(&pmp->sync_thrs[clindex]); 612 613 /* 614 * Remove the cluster index from the group. If destroying 615 * the PFS and this is a master, adjust pfs_nmasters. 616 */ 617 hammer2_mtx_ex(&iroot->lock); 618 chain = iroot->cluster.array[clindex].chain; 619 iroot->cluster.array[clindex].chain = NULL; 620 621 switch(pmp->pfs_types[clindex]) { 622 case HAMMER2_PFSTYPE_MASTER: 623 if (destroying && pmp->pfs_nmasters > 0) 624 --pmp->pfs_nmasters; 625 /* XXX adjust ripdata->meta.pfs_nmasters */ 626 break; 627 default: 628 break; 629 } 630 pmp->pfs_types[clindex] = HAMMER2_PFSTYPE_NONE; 631 632 hammer2_mtx_unlock(&iroot->lock); 633 634 /* 635 * Release the chain. 636 */ 637 if (chain) { 638 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE); 639 hammer2_chain_drop(chain); 640 } 641 642 /* 643 * Terminate all XOP threads for the cluster index. 644 */ 645 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) 646 hammer2_thr_delete(&pmp->xop_groups[j].thrs[clindex]); 647 } 648 } 649 650 /* 651 * Destroy a PFS, typically only occurs after the last mount on a device 652 * has gone away. 653 */ 654 static void 655 hammer2_pfsfree(hammer2_pfs_t *pmp) 656 { 657 hammer2_inode_t *iroot; 658 hammer2_chain_t *chain; 659 int i; 660 int j; 661 662 /* 663 * Cleanup our reference on iroot. iroot is (should) not be needed 664 * by the flush code. 665 */ 666 if (pmp->flags & HAMMER2_PMPF_SPMP) 667 TAILQ_REMOVE(&hammer2_spmplist, pmp, mntentry); 668 else 669 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry); 670 671 iroot = pmp->iroot; 672 if (iroot) { 673 for (i = 0; i < iroot->cluster.nchains; ++i) { 674 hammer2_thr_delete(&pmp->sync_thrs[i]); 675 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) 676 hammer2_thr_delete(&pmp->xop_groups[j].thrs[i]); 677 } 678 #if REPORT_REFS_ERRORS 679 if (pmp->iroot->refs != 1) 680 kprintf("PMP->IROOT %p REFS WRONG %d\n", 681 pmp->iroot, pmp->iroot->refs); 682 #else 683 KKASSERT(pmp->iroot->refs == 1); 684 #endif 685 /* ref for pmp->iroot */ 686 hammer2_inode_drop(pmp->iroot); 687 pmp->iroot = NULL; 688 } 689 690 /* 691 * Cleanup chains remaining on LRU list. 692 */ 693 hammer2_spin_ex(&pmp->lru_spin); 694 while ((chain = TAILQ_FIRST(&pmp->lru_list)) != NULL) { 695 KKASSERT(chain->flags & HAMMER2_CHAIN_ONLRU); 696 atomic_add_int(&pmp->lru_count, -1); 697 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU); 698 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node); 699 hammer2_chain_ref(chain); 700 hammer2_spin_unex(&pmp->lru_spin); 701 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE); 702 hammer2_chain_drop(chain); 703 hammer2_spin_ex(&pmp->lru_spin); 704 } 705 hammer2_spin_unex(&pmp->lru_spin); 706 707 /* 708 * Free remaining pmp resources 709 */ 710 kmalloc_destroy(&pmp->mmsg); 711 kmalloc_destroy(&pmp->minode); 712 713 kfree(pmp, M_HAMMER2); 714 } 715 716 /* 717 * Remove all references to hmp from the pfs list. Any PFS which becomes 718 * empty is terminated and freed. 719 * 720 * XXX inefficient. 721 */ 722 static void 723 hammer2_pfsfree_scan(hammer2_dev_t *hmp, int which) 724 { 725 hammer2_pfs_t *pmp; 726 hammer2_inode_t *iroot; 727 hammer2_chain_t *rchain; 728 int didfreeze; 729 int i; 730 int j; 731 struct hammer2_pfslist *wlist; 732 733 if (which == 0) 734 wlist = &hammer2_pfslist; 735 else 736 wlist = &hammer2_spmplist; 737 again: 738 TAILQ_FOREACH(pmp, wlist, mntentry) { 739 if ((iroot = pmp->iroot) == NULL) 740 continue; 741 hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH); 742 hammer2_inode_run_sideq(pmp, 1); 743 hammer2_bioq_sync(pmp); 744 hammer2_trans_done(pmp, 0); 745 746 /* 747 * Determine if this PFS is affected. If it is we must 748 * freeze all management threads and lock its iroot. 749 * 750 * Freezing a management thread forces it idle, operations 751 * in-progress will be aborted and it will have to start 752 * over again when unfrozen, or exit if told to exit. 753 */ 754 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 755 if (pmp->pfs_hmps[i] == hmp) 756 break; 757 } 758 if (i != HAMMER2_MAXCLUSTER) { 759 /* 760 * Make sure all synchronization threads are locked 761 * down. 762 */ 763 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 764 if (pmp->pfs_hmps[i] == NULL) 765 continue; 766 hammer2_thr_freeze_async(&pmp->sync_thrs[i]); 767 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) { 768 hammer2_thr_freeze_async( 769 &pmp->xop_groups[j].thrs[i]); 770 } 771 } 772 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 773 if (pmp->pfs_hmps[i] == NULL) 774 continue; 775 hammer2_thr_freeze(&pmp->sync_thrs[i]); 776 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) { 777 hammer2_thr_freeze( 778 &pmp->xop_groups[j].thrs[i]); 779 } 780 } 781 782 /* 783 * Lock the inode and clean out matching chains. 784 * Note that we cannot use hammer2_inode_lock_*() 785 * here because that would attempt to validate the 786 * cluster that we are in the middle of ripping 787 * apart. 788 * 789 * WARNING! We are working directly on the inodes 790 * embedded cluster. 791 */ 792 hammer2_mtx_ex(&iroot->lock); 793 794 /* 795 * Remove the chain from matching elements of the PFS. 796 */ 797 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 798 if (pmp->pfs_hmps[i] != hmp) 799 continue; 800 hammer2_thr_delete(&pmp->sync_thrs[i]); 801 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) { 802 hammer2_thr_delete( 803 &pmp->xop_groups[j].thrs[i]); 804 } 805 rchain = iroot->cluster.array[i].chain; 806 iroot->cluster.array[i].chain = NULL; 807 pmp->pfs_types[i] = 0; 808 if (pmp->pfs_names[i]) { 809 kfree(pmp->pfs_names[i], M_HAMMER2); 810 pmp->pfs_names[i] = NULL; 811 } 812 if (rchain) { 813 hammer2_chain_drop(rchain); 814 /* focus hint */ 815 if (iroot->cluster.focus == rchain) 816 iroot->cluster.focus = NULL; 817 } 818 pmp->pfs_hmps[i] = NULL; 819 } 820 hammer2_mtx_unlock(&iroot->lock); 821 didfreeze = 1; /* remaster, unfreeze down below */ 822 } else { 823 didfreeze = 0; 824 } 825 826 /* 827 * Cleanup trailing chains. Gaps may remain. 828 */ 829 for (i = HAMMER2_MAXCLUSTER - 1; i >= 0; --i) { 830 if (pmp->pfs_hmps[i]) 831 break; 832 } 833 iroot->cluster.nchains = i + 1; 834 835 /* 836 * If the PMP has no elements remaining we can destroy it. 837 * (this will transition management threads from frozen->exit). 838 */ 839 if (iroot->cluster.nchains == 0) { 840 /* 841 * If this was the hmp's spmp, we need to clean 842 * a little more stuff out. 843 */ 844 if (hmp->spmp == pmp) { 845 hmp->spmp = NULL; 846 hmp->vchain.pmp = NULL; 847 hmp->fchain.pmp = NULL; 848 } 849 850 /* 851 * Free the pmp and restart the loop 852 */ 853 hammer2_pfsfree(pmp); 854 goto again; 855 } 856 857 /* 858 * If elements still remain we need to set the REMASTER 859 * flag and unfreeze it. 860 */ 861 if (didfreeze) { 862 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 863 if (pmp->pfs_hmps[i] == NULL) 864 continue; 865 hammer2_thr_remaster(&pmp->sync_thrs[i]); 866 hammer2_thr_unfreeze(&pmp->sync_thrs[i]); 867 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) { 868 hammer2_thr_remaster( 869 &pmp->xop_groups[j].thrs[i]); 870 hammer2_thr_unfreeze( 871 &pmp->xop_groups[j].thrs[i]); 872 } 873 } 874 } 875 } 876 } 877 878 /* 879 * Mount or remount HAMMER2 fileystem from physical media 880 * 881 * mountroot 882 * mp mount point structure 883 * path NULL 884 * data <unused> 885 * cred <unused> 886 * 887 * mount 888 * mp mount point structure 889 * path path to mount point 890 * data pointer to argument structure in user space 891 * volume volume path (device@LABEL form) 892 * hflags user mount flags 893 * cred user credentials 894 * 895 * RETURNS: 0 Success 896 * !0 error number 897 */ 898 static 899 int 900 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data, 901 struct ucred *cred) 902 { 903 struct hammer2_mount_info info; 904 hammer2_pfs_t *pmp; 905 hammer2_pfs_t *spmp; 906 hammer2_dev_t *hmp; 907 hammer2_dev_t *force_local; 908 hammer2_key_t key_next; 909 hammer2_key_t key_dummy; 910 hammer2_key_t lhc; 911 struct vnode *devvp; 912 struct nlookupdata nd; 913 hammer2_chain_t *parent; 914 hammer2_chain_t *chain; 915 const hammer2_inode_data_t *ripdata; 916 hammer2_blockref_t bref; 917 struct file *fp; 918 char devstr[MNAMELEN]; 919 size_t size; 920 size_t done; 921 char *dev; 922 char *label; 923 int ronly = 1; 924 int error; 925 int i; 926 927 hmp = NULL; 928 pmp = NULL; 929 dev = NULL; 930 label = NULL; 931 devvp = NULL; 932 933 if (path == NULL) { 934 /* 935 * Root mount 936 */ 937 bzero(&info, sizeof(info)); 938 info.cluster_fd = -1; 939 ksnprintf(devstr, sizeof(devstr), "%s", 940 mp->mnt_stat.f_mntfromname); 941 kprintf("hammer2_mount: root '%s'\n", devstr); 942 done = strlen(devstr) + 1; 943 } else { 944 /* 945 * Non-root mount or updating a mount 946 */ 947 error = copyin(data, &info, sizeof(info)); 948 if (error) 949 return (error); 950 951 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done); 952 if (error) 953 return (error); 954 kprintf("hammer2_mount: '%s'\n", devstr); 955 } 956 957 /* 958 * Extract device and label, automatically mount @BOOT, @ROOT, or @DATA 959 * if no label specified, based on the partition id. Error out if no 960 * label or device (with partition id) is specified. This is strictly 961 * a convenience to match the default label created by newfs_hammer2, 962 * our preference is that a label always be specified. 963 * 964 * NOTE: We allow 'mount @LABEL <blah>'... that is, a mount command 965 * that does not specify a device, as long as some H2 label 966 * has already been mounted from that device. This makes 967 * mounting snapshots a lot easier. 968 */ 969 dev = devstr; 970 label = strchr(devstr, '@'); 971 if (label && ((label + 1) - dev) > done) { 972 kprintf("hammer2: mount: bad label %s/%zd\n", 973 devstr, done); 974 return (EINVAL); 975 } 976 if (label == NULL || label[1] == 0) { 977 char slice; 978 979 if (label == NULL) 980 label = devstr + strlen(devstr); 981 else 982 *label = '\0'; /* clean up trailing @ */ 983 984 slice = label[-1]; 985 switch(slice) { 986 case 'a': 987 label = "BOOT"; 988 break; 989 case 'd': 990 label = "ROOT"; 991 break; 992 default: 993 label = "DATA"; 994 break; 995 } 996 } else { 997 *label = '\0'; 998 label++; 999 } 1000 1001 kprintf("hammer2_mount: dev=\"%s\" label=\"%s\" rdonly=%d\n", 1002 dev, label, (mp->mnt_flag & MNT_RDONLY)); 1003 1004 if (mp->mnt_flag & MNT_UPDATE) { 1005 /* 1006 * Update mount. Note that pmp->iroot->cluster is 1007 * an inode-embedded cluster and thus cannot be 1008 * directly locked. 1009 * 1010 * XXX HAMMER2 needs to implement NFS export via 1011 * mountctl. 1012 */ 1013 hammer2_cluster_t *cluster; 1014 1015 pmp = MPTOPMP(mp); 1016 pmp->hflags = info.hflags; 1017 cluster = &pmp->iroot->cluster; 1018 for (i = 0; i < cluster->nchains; ++i) { 1019 if (cluster->array[i].chain == NULL) 1020 continue; 1021 hmp = cluster->array[i].chain->hmp; 1022 devvp = hmp->devvp; 1023 error = hammer2_remount(hmp, mp, path, 1024 devvp, cred); 1025 if (error) 1026 break; 1027 } 1028 1029 return error; 1030 } 1031 1032 /* 1033 * HMP device mount 1034 * 1035 * If a path is specified and dev is not an empty string, lookup the 1036 * name and verify that it referes to a block device. 1037 * 1038 * If a path is specified and dev is an empty string we fall through 1039 * and locate the label in the hmp search. 1040 */ 1041 if (path && *dev != 0) { 1042 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW); 1043 if (error == 0) 1044 error = nlookup(&nd); 1045 if (error == 0) 1046 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp); 1047 nlookup_done(&nd); 1048 } else if (path == NULL) { 1049 /* root mount */ 1050 cdev_t cdev = kgetdiskbyname(dev); 1051 error = bdevvp(cdev, &devvp); 1052 if (error) 1053 kprintf("hammer2: cannot find '%s'\n", dev); 1054 } else { 1055 /* 1056 * We will locate the hmp using the label in the hmp loop. 1057 */ 1058 error = 0; 1059 } 1060 1061 /* 1062 * Make sure its a block device. Do not check to see if it is 1063 * already mounted until we determine that its a fresh H2 device. 1064 */ 1065 if (error == 0 && devvp) { 1066 vn_isdisk(devvp, &error); 1067 } 1068 1069 /* 1070 * Determine if the device has already been mounted. After this 1071 * check hmp will be non-NULL if we are doing the second or more 1072 * hammer2 mounts from the same device. 1073 */ 1074 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE); 1075 if (devvp) { 1076 /* 1077 * Match the device. Due to the way devfs works, 1078 * we may not be able to directly match the vnode pointer, 1079 * so also check to see if the underlying device matches. 1080 */ 1081 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) { 1082 if (hmp->devvp == devvp) 1083 break; 1084 if (devvp->v_rdev && 1085 hmp->devvp->v_rdev == devvp->v_rdev) { 1086 break; 1087 } 1088 } 1089 1090 /* 1091 * If no match this may be a fresh H2 mount, make sure 1092 * the device is not mounted on anything else. 1093 */ 1094 if (hmp == NULL) 1095 error = vfs_mountedon(devvp); 1096 } else if (error == 0) { 1097 /* 1098 * Match the label to a pmp already probed. 1099 */ 1100 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) { 1101 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 1102 if (pmp->pfs_names[i] && 1103 strcmp(pmp->pfs_names[i], label) == 0) { 1104 hmp = pmp->pfs_hmps[i]; 1105 break; 1106 } 1107 } 1108 if (hmp) 1109 break; 1110 } 1111 if (hmp == NULL) 1112 error = ENOENT; 1113 } 1114 1115 /* 1116 * Open the device if this isn't a secondary mount and construct 1117 * the H2 device mount (hmp). 1118 */ 1119 if (hmp == NULL) { 1120 hammer2_chain_t *schain; 1121 hammer2_xid_t xid; 1122 hammer2_xop_head_t xop; 1123 1124 if (error == 0 && vcount(devvp) > 0) { 1125 kprintf("Primary device already has references\n"); 1126 error = EBUSY; 1127 } 1128 1129 /* 1130 * Now open the device 1131 */ 1132 if (error == 0) { 1133 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0); 1134 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1135 error = vinvalbuf(devvp, V_SAVE, 0, 0); 1136 if (error == 0) { 1137 error = VOP_OPEN(devvp, 1138 (ronly ? FREAD : FREAD | FWRITE), 1139 FSCRED, NULL); 1140 } 1141 vn_unlock(devvp); 1142 } 1143 if (error && devvp) { 1144 vrele(devvp); 1145 devvp = NULL; 1146 } 1147 if (error) { 1148 lockmgr(&hammer2_mntlk, LK_RELEASE); 1149 return error; 1150 } 1151 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO); 1152 ksnprintf(hmp->devrepname, sizeof(hmp->devrepname), "%s", dev); 1153 hmp->ronly = ronly; 1154 hmp->devvp = devvp; 1155 hmp->hflags = info.hflags & HMNT2_DEVFLAGS; 1156 kmalloc_create(&hmp->mchain, "HAMMER2-chains"); 1157 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry); 1158 RB_INIT(&hmp->iotree); 1159 spin_init(&hmp->io_spin, "hm2mount_io"); 1160 spin_init(&hmp->list_spin, "hm2mount_list"); 1161 TAILQ_INIT(&hmp->flushq); 1162 1163 lockinit(&hmp->vollk, "h2vol", 0, 0); 1164 lockinit(&hmp->bulklk, "h2bulk", 0, 0); 1165 lockinit(&hmp->bflock, "h2bflk", 0, 0); 1166 1167 /* 1168 * vchain setup. vchain.data is embedded. 1169 * vchain.refs is initialized and will never drop to 0. 1170 * 1171 * NOTE! voldata is not yet loaded. 1172 */ 1173 hmp->vchain.hmp = hmp; 1174 hmp->vchain.refs = 1; 1175 hmp->vchain.data = (void *)&hmp->voldata; 1176 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME; 1177 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX; 1178 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid; 1179 1180 hammer2_chain_core_init(&hmp->vchain); 1181 /* hmp->vchain.u.xxx is left NULL */ 1182 1183 /* 1184 * fchain setup. fchain.data is embedded. 1185 * fchain.refs is initialized and will never drop to 0. 1186 * 1187 * The data is not used but needs to be initialized to 1188 * pass assertion muster. We use this chain primarily 1189 * as a placeholder for the freemap's top-level RBTREE 1190 * so it does not interfere with the volume's topology 1191 * RBTREE. 1192 */ 1193 hmp->fchain.hmp = hmp; 1194 hmp->fchain.refs = 1; 1195 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset; 1196 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP; 1197 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX; 1198 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid; 1199 hmp->fchain.bref.methods = 1200 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) | 1201 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE); 1202 1203 hammer2_chain_core_init(&hmp->fchain); 1204 /* hmp->fchain.u.xxx is left NULL */ 1205 1206 /* 1207 * Install the volume header and initialize fields from 1208 * voldata. 1209 */ 1210 error = hammer2_install_volume_header(hmp); 1211 if (error) { 1212 hammer2_unmount_helper(mp, NULL, hmp); 1213 lockmgr(&hammer2_mntlk, LK_RELEASE); 1214 hammer2_vfs_unmount(mp, MNT_FORCE); 1215 return error; 1216 } 1217 1218 /* 1219 * Really important to get these right or flush will get 1220 * confused. 1221 */ 1222 hmp->spmp = hammer2_pfsalloc(NULL, NULL, 0, NULL); 1223 spmp = hmp->spmp; 1224 1225 /* 1226 * Dummy-up vchain and fchain's modify_tid. mirror_tid 1227 * is inherited from the volume header. 1228 */ 1229 xid = 0; 1230 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid; 1231 hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid; 1232 hmp->vchain.pmp = spmp; 1233 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid; 1234 hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid; 1235 hmp->fchain.pmp = spmp; 1236 1237 /* 1238 * First locate the super-root inode, which is key 0 1239 * relative to the volume header's blockset. 1240 * 1241 * Then locate the root inode by scanning the directory keyspace 1242 * represented by the label. 1243 */ 1244 parent = hammer2_chain_lookup_init(&hmp->vchain, 0); 1245 schain = hammer2_chain_lookup(&parent, &key_dummy, 1246 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 1247 &error, 0); 1248 hammer2_chain_lookup_done(parent); 1249 if (schain == NULL) { 1250 kprintf("hammer2_mount: invalid super-root\n"); 1251 hammer2_unmount_helper(mp, NULL, hmp); 1252 lockmgr(&hammer2_mntlk, LK_RELEASE); 1253 hammer2_vfs_unmount(mp, MNT_FORCE); 1254 return EINVAL; 1255 } 1256 if (schain->error) { 1257 kprintf("hammer2_mount: error %s reading super-root\n", 1258 hammer2_error_str(schain->error)); 1259 hammer2_chain_unlock(schain); 1260 hammer2_chain_drop(schain); 1261 schain = NULL; 1262 hammer2_unmount_helper(mp, NULL, hmp); 1263 lockmgr(&hammer2_mntlk, LK_RELEASE); 1264 hammer2_vfs_unmount(mp, MNT_FORCE); 1265 return EINVAL; 1266 } 1267 1268 /* 1269 * The super-root always uses an inode_tid of 1 when 1270 * creating PFSs. 1271 */ 1272 spmp->inode_tid = 1; 1273 spmp->modify_tid = schain->bref.modify_tid + 1; 1274 1275 /* 1276 * Sanity-check schain's pmp and finish initialization. 1277 * Any chain belonging to the super-root topology should 1278 * have a NULL pmp (not even set to spmp). 1279 */ 1280 ripdata = &hammer2_chain_rdata(schain)->ipdata; 1281 KKASSERT(schain->pmp == NULL); 1282 spmp->pfs_clid = ripdata->meta.pfs_clid; 1283 1284 /* 1285 * Replace the dummy spmp->iroot with a real one. It's 1286 * easier to just do a wholesale replacement than to try 1287 * to update the chain and fixup the iroot fields. 1288 * 1289 * The returned inode is locked with the supplied cluster. 1290 */ 1291 hammer2_dummy_xop_from_chain(&xop, schain); 1292 hammer2_inode_drop(spmp->iroot); 1293 spmp->iroot = NULL; 1294 spmp->iroot = hammer2_inode_get(spmp, NULL, &xop, -1); 1295 spmp->spmp_hmp = hmp; 1296 spmp->pfs_types[0] = ripdata->meta.pfs_type; 1297 spmp->pfs_hmps[0] = hmp; 1298 hammer2_inode_ref(spmp->iroot); 1299 hammer2_inode_unlock(spmp->iroot); 1300 hammer2_cluster_unlock(&xop.cluster); 1301 hammer2_chain_drop(schain); 1302 /* do not call hammer2_cluster_drop() on an embedded cluster */ 1303 schain = NULL; /* now invalid */ 1304 /* leave spmp->iroot with one ref */ 1305 1306 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 1307 error = hammer2_recovery(hmp); 1308 if (error == 0) 1309 error |= hammer2_fixup_pfses(hmp); 1310 /* XXX do something with error */ 1311 } 1312 hammer2_update_pmps(hmp); 1313 hammer2_iocom_init(hmp); 1314 hammer2_bulkfree_init(hmp); 1315 1316 /* 1317 * Ref the cluster management messaging descriptor. The mount 1318 * program deals with the other end of the communications pipe. 1319 * 1320 * Root mounts typically do not supply one. 1321 */ 1322 if (info.cluster_fd >= 0) { 1323 fp = holdfp(curthread, info.cluster_fd, -1); 1324 if (fp) { 1325 hammer2_cluster_reconnect(hmp, fp); 1326 } else { 1327 kprintf("hammer2_mount: bad cluster_fd!\n"); 1328 } 1329 } 1330 } else { 1331 spmp = hmp->spmp; 1332 if (info.hflags & HMNT2_DEVFLAGS) { 1333 kprintf("hammer2: Warning: mount flags pertaining " 1334 "to the whole device may only be specified " 1335 "on the first mount of the device: %08x\n", 1336 info.hflags & HMNT2_DEVFLAGS); 1337 } 1338 } 1339 1340 /* 1341 * Force local mount (disassociate all PFSs from their clusters). 1342 * Used primarily for debugging. 1343 */ 1344 force_local = (hmp->hflags & HMNT2_LOCAL) ? hmp : NULL; 1345 1346 /* 1347 * Lookup the mount point under the media-localized super-root. 1348 * Scanning hammer2_pfslist doesn't help us because it represents 1349 * PFS cluster ids which can aggregate several named PFSs together. 1350 * 1351 * cluster->pmp will incorrectly point to spmp and must be fixed 1352 * up later on. 1353 */ 1354 hammer2_inode_lock(spmp->iroot, 0); 1355 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS); 1356 lhc = hammer2_dirhash(label, strlen(label)); 1357 chain = hammer2_chain_lookup(&parent, &key_next, 1358 lhc, lhc + HAMMER2_DIRHASH_LOMASK, 1359 &error, 0); 1360 while (chain) { 1361 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && 1362 strcmp(label, chain->data->ipdata.filename) == 0) { 1363 break; 1364 } 1365 chain = hammer2_chain_next(&parent, chain, &key_next, 1366 key_next, 1367 lhc + HAMMER2_DIRHASH_LOMASK, 1368 &error, 0); 1369 } 1370 if (parent) { 1371 hammer2_chain_unlock(parent); 1372 hammer2_chain_drop(parent); 1373 } 1374 hammer2_inode_unlock(spmp->iroot); 1375 1376 /* 1377 * PFS could not be found? 1378 */ 1379 if (chain == NULL) { 1380 if (error) 1381 kprintf("hammer2_mount: PFS label I/O error\n"); 1382 else 1383 kprintf("hammer2_mount: PFS label not found\n"); 1384 hammer2_unmount_helper(mp, NULL, hmp); 1385 lockmgr(&hammer2_mntlk, LK_RELEASE); 1386 hammer2_vfs_unmount(mp, MNT_FORCE); 1387 1388 return EINVAL; 1389 } 1390 1391 /* 1392 * Acquire the pmp structure (it should have already been allocated 1393 * via hammer2_update_pmps() so do not pass cluster in to add to 1394 * available chains). 1395 * 1396 * Check if the cluster has already been mounted. A cluster can 1397 * only be mounted once, use null mounts to mount additional copies. 1398 */ 1399 if (chain->error) { 1400 kprintf("hammer2_mount: PFS label I/O error\n"); 1401 } else { 1402 ripdata = &chain->data->ipdata; 1403 bref = chain->bref; 1404 pmp = hammer2_pfsalloc(NULL, ripdata, 1405 bref.modify_tid, force_local); 1406 } 1407 hammer2_chain_unlock(chain); 1408 hammer2_chain_drop(chain); 1409 1410 /* 1411 * Finish the mount 1412 */ 1413 kprintf("hammer2_mount hmp=%p pmp=%p\n", hmp, pmp); 1414 1415 if (pmp->mp) { 1416 kprintf("hammer2_mount: PFS already mounted!\n"); 1417 hammer2_unmount_helper(mp, NULL, hmp); 1418 lockmgr(&hammer2_mntlk, LK_RELEASE); 1419 hammer2_vfs_unmount(mp, MNT_FORCE); 1420 1421 return EBUSY; 1422 } 1423 1424 pmp->hflags = info.hflags; 1425 mp->mnt_flag |= MNT_LOCAL; 1426 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */ 1427 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */ 1428 1429 /* 1430 * required mount structure initializations 1431 */ 1432 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE; 1433 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE; 1434 1435 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE; 1436 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE; 1437 1438 /* 1439 * Optional fields 1440 */ 1441 mp->mnt_iosize_max = MAXPHYS; 1442 1443 /* 1444 * Connect up mount pointers. 1445 */ 1446 hammer2_mount_helper(mp, pmp); 1447 1448 lockmgr(&hammer2_mntlk, LK_RELEASE); 1449 1450 /* 1451 * Finish setup 1452 */ 1453 vfs_getnewfsid(mp); 1454 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops); 1455 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops); 1456 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops); 1457 1458 if (path) { 1459 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, 1460 MNAMELEN - 1, &size); 1461 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 1462 } /* else root mount, already in there */ 1463 1464 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname)); 1465 if (path) { 1466 copyinstr(path, mp->mnt_stat.f_mntonname, 1467 sizeof(mp->mnt_stat.f_mntonname) - 1, 1468 &size); 1469 } else { 1470 /* root mount */ 1471 mp->mnt_stat.f_mntonname[0] = '/'; 1472 } 1473 1474 /* 1475 * Initial statfs to prime mnt_stat. 1476 */ 1477 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred); 1478 1479 return 0; 1480 } 1481 1482 /* 1483 * Scan PFSs under the super-root and create hammer2_pfs structures. 1484 */ 1485 static 1486 void 1487 hammer2_update_pmps(hammer2_dev_t *hmp) 1488 { 1489 const hammer2_inode_data_t *ripdata; 1490 hammer2_chain_t *parent; 1491 hammer2_chain_t *chain; 1492 hammer2_blockref_t bref; 1493 hammer2_dev_t *force_local; 1494 hammer2_pfs_t *spmp; 1495 hammer2_pfs_t *pmp; 1496 hammer2_key_t key_next; 1497 int error; 1498 1499 /* 1500 * Force local mount (disassociate all PFSs from their clusters). 1501 * Used primarily for debugging. 1502 */ 1503 force_local = (hmp->hflags & HMNT2_LOCAL) ? hmp : NULL; 1504 1505 /* 1506 * Lookup mount point under the media-localized super-root. 1507 * 1508 * cluster->pmp will incorrectly point to spmp and must be fixed 1509 * up later on. 1510 */ 1511 spmp = hmp->spmp; 1512 hammer2_inode_lock(spmp->iroot, 0); 1513 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS); 1514 chain = hammer2_chain_lookup(&parent, &key_next, 1515 HAMMER2_KEY_MIN, HAMMER2_KEY_MAX, 1516 &error, 0); 1517 while (chain) { 1518 if (chain->bref.type != HAMMER2_BREF_TYPE_INODE) 1519 continue; 1520 if (chain->error) { 1521 kprintf("I/O error scanning PFS labels\n"); 1522 } else { 1523 ripdata = &chain->data->ipdata; 1524 bref = chain->bref; 1525 1526 pmp = hammer2_pfsalloc(chain, ripdata, 1527 bref.modify_tid, force_local); 1528 } 1529 chain = hammer2_chain_next(&parent, chain, &key_next, 1530 key_next, HAMMER2_KEY_MAX, 1531 &error, 0); 1532 } 1533 if (parent) { 1534 hammer2_chain_unlock(parent); 1535 hammer2_chain_drop(parent); 1536 } 1537 hammer2_inode_unlock(spmp->iroot); 1538 } 1539 1540 static 1541 int 1542 hammer2_remount(hammer2_dev_t *hmp, struct mount *mp, char *path __unused, 1543 struct vnode *devvp, struct ucred *cred) 1544 { 1545 int error; 1546 1547 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 1548 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1549 VOP_OPEN(devvp, FREAD | FWRITE, FSCRED, NULL); 1550 vn_unlock(devvp); 1551 error = hammer2_recovery(hmp); 1552 if (error == 0) 1553 error |= hammer2_fixup_pfses(hmp); 1554 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1555 if (error == 0) { 1556 VOP_CLOSE(devvp, FREAD, NULL); 1557 hmp->ronly = 0; 1558 } else { 1559 VOP_CLOSE(devvp, FREAD | FWRITE, NULL); 1560 } 1561 vn_unlock(devvp); 1562 } else { 1563 error = 0; 1564 } 1565 return error; 1566 } 1567 1568 static 1569 int 1570 hammer2_vfs_unmount(struct mount *mp, int mntflags) 1571 { 1572 hammer2_pfs_t *pmp; 1573 int flags; 1574 int error = 0; 1575 1576 pmp = MPTOPMP(mp); 1577 1578 if (pmp == NULL) 1579 return(0); 1580 1581 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE); 1582 1583 /* 1584 * If mount initialization proceeded far enough we must flush 1585 * its vnodes and sync the underlying mount points. Three syncs 1586 * are required to fully flush the filesystem (freemap updates lag 1587 * by one flush, and one extra for safety). 1588 */ 1589 if (mntflags & MNT_FORCE) 1590 flags = FORCECLOSE; 1591 else 1592 flags = 0; 1593 if (pmp->iroot) { 1594 error = vflush(mp, 0, flags); 1595 if (error) 1596 goto failed; 1597 hammer2_vfs_sync(mp, MNT_WAIT); 1598 hammer2_vfs_sync(mp, MNT_WAIT); 1599 hammer2_vfs_sync(mp, MNT_WAIT); 1600 } 1601 1602 /* 1603 * Cleanup the frontend support XOPS threads 1604 */ 1605 hammer2_xop_helper_cleanup(pmp); 1606 1607 if (pmp->mp) 1608 hammer2_unmount_helper(mp, pmp, NULL); 1609 1610 error = 0; 1611 failed: 1612 lockmgr(&hammer2_mntlk, LK_RELEASE); 1613 1614 return (error); 1615 } 1616 1617 /* 1618 * Mount helper, hook the system mount into our PFS. 1619 * The mount lock is held. 1620 * 1621 * We must bump the mount_count on related devices for any 1622 * mounted PFSs. 1623 */ 1624 static 1625 void 1626 hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp) 1627 { 1628 hammer2_cluster_t *cluster; 1629 hammer2_chain_t *rchain; 1630 int i; 1631 1632 mp->mnt_data = (qaddr_t)pmp; 1633 pmp->mp = mp; 1634 1635 /* 1636 * After pmp->mp is set we have to adjust hmp->mount_count. 1637 */ 1638 cluster = &pmp->iroot->cluster; 1639 for (i = 0; i < cluster->nchains; ++i) { 1640 rchain = cluster->array[i].chain; 1641 if (rchain == NULL) 1642 continue; 1643 ++rchain->hmp->mount_count; 1644 } 1645 1646 /* 1647 * Create missing Xop threads 1648 */ 1649 hammer2_xop_helper_create(pmp); 1650 } 1651 1652 /* 1653 * Mount helper, unhook the system mount from our PFS. 1654 * The mount lock is held. 1655 * 1656 * If hmp is supplied a mount responsible for being the first to open 1657 * the block device failed and the block device and all PFSs using the 1658 * block device must be cleaned up. 1659 * 1660 * If pmp is supplied multiple devices might be backing the PFS and each 1661 * must be disconnected. This might not be the last PFS using some of the 1662 * underlying devices. Also, we have to adjust our hmp->mount_count 1663 * accounting for the devices backing the pmp which is now undergoing an 1664 * unmount. 1665 */ 1666 static 1667 void 1668 hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, hammer2_dev_t *hmp) 1669 { 1670 hammer2_cluster_t *cluster; 1671 hammer2_chain_t *rchain; 1672 struct vnode *devvp; 1673 int dumpcnt; 1674 int ronly; 1675 int i; 1676 1677 /* 1678 * If no device supplied this is a high-level unmount and we have to 1679 * to disconnect the mount, adjust mount_count, and locate devices 1680 * that might now have no mounts. 1681 */ 1682 if (pmp) { 1683 KKASSERT(hmp == NULL); 1684 KKASSERT((void *)(intptr_t)mp->mnt_data == pmp); 1685 pmp->mp = NULL; 1686 mp->mnt_data = NULL; 1687 1688 /* 1689 * After pmp->mp is cleared we have to account for 1690 * mount_count. 1691 */ 1692 cluster = &pmp->iroot->cluster; 1693 for (i = 0; i < cluster->nchains; ++i) { 1694 rchain = cluster->array[i].chain; 1695 if (rchain == NULL) 1696 continue; 1697 --rchain->hmp->mount_count; 1698 /* scrapping hmp now may invalidate the pmp */ 1699 } 1700 again: 1701 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) { 1702 if (hmp->mount_count == 0) { 1703 hammer2_unmount_helper(NULL, NULL, hmp); 1704 goto again; 1705 } 1706 } 1707 return; 1708 } 1709 1710 /* 1711 * Try to terminate the block device. We can't terminate it if 1712 * there are still PFSs referencing it. 1713 */ 1714 if (hmp->mount_count) 1715 return; 1716 1717 /* 1718 * Decomission the network before we start messing with the 1719 * device and PFS. 1720 */ 1721 hammer2_iocom_uninit(hmp); 1722 1723 hammer2_bulkfree_uninit(hmp); 1724 hammer2_pfsfree_scan(hmp, 0); 1725 hammer2_dev_exlock(hmp); /* XXX order */ 1726 1727 /* 1728 * Cycle the volume data lock as a safety (probably not needed any 1729 * more). To ensure everything is out we need to flush at least 1730 * three times. (1) The running of the sideq can dirty the 1731 * filesystem, (2) A normal flush can dirty the freemap, and 1732 * (3) ensure that the freemap is fully synchronized. 1733 * 1734 * The next mount's recovery scan can clean everything up but we want 1735 * to leave the filesystem in a 100% clean state on a normal unmount. 1736 */ 1737 #if 0 1738 hammer2_voldata_lock(hmp); 1739 hammer2_voldata_unlock(hmp); 1740 #endif 1741 1742 /* 1743 * Flush whatever is left. Unmounted but modified PFS's might still 1744 * have some dirty chains on them. 1745 */ 1746 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS); 1747 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS); 1748 1749 if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) { 1750 hammer2_voldata_modify(hmp); 1751 hammer2_flush(&hmp->fchain, HAMMER2_FLUSH_TOP | 1752 HAMMER2_FLUSH_ALL); 1753 } 1754 hammer2_chain_unlock(&hmp->fchain); 1755 1756 if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) { 1757 hammer2_flush(&hmp->vchain, HAMMER2_FLUSH_TOP | 1758 HAMMER2_FLUSH_ALL); 1759 } 1760 hammer2_chain_unlock(&hmp->vchain); 1761 1762 if ((hmp->vchain.flags | hmp->fchain.flags) & 1763 HAMMER2_CHAIN_FLUSH_MASK) { 1764 kprintf("hammer2_unmount: chains left over " 1765 "after final sync\n"); 1766 kprintf(" vchain %08x\n", hmp->vchain.flags); 1767 kprintf(" fchain %08x\n", hmp->fchain.flags); 1768 1769 if (hammer2_debug & 0x0010) 1770 Debugger("entered debugger"); 1771 } 1772 1773 hammer2_pfsfree_scan(hmp, 1); 1774 1775 KKASSERT(hmp->spmp == NULL); 1776 1777 /* 1778 * Finish up with the device vnode 1779 */ 1780 if ((devvp = hmp->devvp) != NULL) { 1781 ronly = hmp->ronly; 1782 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1783 kprintf("hammer2_unmount(A): devvp %s rbdirty %p ronly=%d\n", 1784 hmp->devrepname, RB_ROOT(&devvp->v_rbdirty_tree), 1785 ronly); 1786 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0); 1787 kprintf("hammer2_unmount(B): devvp %s rbdirty %p\n", 1788 hmp->devrepname, RB_ROOT(&devvp->v_rbdirty_tree)); 1789 hmp->devvp = NULL; 1790 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL); 1791 vn_unlock(devvp); 1792 vrele(devvp); 1793 devvp = NULL; 1794 } 1795 1796 /* 1797 * Clear vchain/fchain flags that might prevent final cleanup 1798 * of these chains. 1799 */ 1800 if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) { 1801 atomic_add_long(&hammer2_count_modified_chains, -1); 1802 atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED); 1803 hammer2_pfs_memory_wakeup(hmp->vchain.pmp); 1804 } 1805 if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) { 1806 atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_UPDATE); 1807 } 1808 1809 if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) { 1810 atomic_add_long(&hammer2_count_modified_chains, -1); 1811 atomic_clear_int(&hmp->fchain.flags, HAMMER2_CHAIN_MODIFIED); 1812 hammer2_pfs_memory_wakeup(hmp->fchain.pmp); 1813 } 1814 if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) { 1815 atomic_clear_int(&hmp->fchain.flags, HAMMER2_CHAIN_UPDATE); 1816 } 1817 1818 /* 1819 * Final drop of embedded freemap root chain to 1820 * clean up fchain.core (fchain structure is not 1821 * flagged ALLOCATED so it is cleaned out and then 1822 * left to rot). 1823 */ 1824 hammer2_chain_drop(&hmp->fchain); 1825 1826 /* 1827 * Final drop of embedded volume root chain to clean 1828 * up vchain.core (vchain structure is not flagged 1829 * ALLOCATED so it is cleaned out and then left to 1830 * rot). 1831 */ 1832 dumpcnt = 50; 1833 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v', (u_int)-1); 1834 dumpcnt = 50; 1835 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f', (u_int)-1); 1836 hammer2_dev_unlock(hmp); 1837 hammer2_chain_drop(&hmp->vchain); 1838 1839 hammer2_io_cleanup(hmp, &hmp->iotree); 1840 if (hmp->iofree_count) { 1841 kprintf("io_cleanup: %d I/O's left hanging\n", 1842 hmp->iofree_count); 1843 } 1844 1845 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry); 1846 kmalloc_destroy(&hmp->mchain); 1847 kfree(hmp, M_HAMMER2); 1848 } 1849 1850 int 1851 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp, 1852 ino_t ino, struct vnode **vpp) 1853 { 1854 hammer2_xop_lookup_t *xop; 1855 hammer2_pfs_t *pmp; 1856 hammer2_inode_t *ip; 1857 hammer2_tid_t inum; 1858 int error; 1859 1860 inum = (hammer2_tid_t)ino & HAMMER2_DIRHASH_USERMSK; 1861 1862 error = 0; 1863 pmp = MPTOPMP(mp); 1864 1865 /* 1866 * Easy if we already have it cached 1867 */ 1868 ip = hammer2_inode_lookup(pmp, inum); 1869 if (ip) { 1870 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED); 1871 *vpp = hammer2_igetv(ip, &error); 1872 hammer2_inode_unlock(ip); 1873 hammer2_inode_drop(ip); /* from lookup */ 1874 1875 return error; 1876 } 1877 1878 /* 1879 * Otherwise we have to find the inode 1880 */ 1881 xop = hammer2_xop_alloc(pmp->iroot, 0); 1882 xop->lhc = inum; 1883 hammer2_xop_start(&xop->head, &hammer2_lookup_desc); 1884 error = hammer2_xop_collect(&xop->head, 0); 1885 1886 if (error == 0) 1887 ip = hammer2_inode_get(pmp, NULL, &xop->head, -1); 1888 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1889 1890 if (ip) { 1891 *vpp = hammer2_igetv(ip, &error); 1892 hammer2_inode_unlock(ip); 1893 } else { 1894 *vpp = NULL; 1895 error = ENOENT; 1896 } 1897 return (error); 1898 } 1899 1900 static 1901 int 1902 hammer2_vfs_root(struct mount *mp, struct vnode **vpp) 1903 { 1904 hammer2_pfs_t *pmp; 1905 struct vnode *vp; 1906 int error; 1907 1908 pmp = MPTOPMP(mp); 1909 if (pmp->iroot == NULL) { 1910 kprintf("hammer2 (%s): no root inode\n", 1911 mp->mnt_stat.f_mntfromname); 1912 *vpp = NULL; 1913 return EINVAL; 1914 } 1915 1916 error = 0; 1917 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED); 1918 1919 while (pmp->inode_tid == 0) { 1920 hammer2_xop_ipcluster_t *xop; 1921 const hammer2_inode_meta_t *meta; 1922 1923 xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING); 1924 hammer2_xop_start(&xop->head, &hammer2_ipcluster_desc); 1925 error = hammer2_xop_collect(&xop->head, 0); 1926 1927 if (error == 0) { 1928 meta = &hammer2_xop_gdata(&xop->head)->ipdata.meta; 1929 pmp->iroot->meta = *meta; 1930 pmp->inode_tid = meta->pfs_inum + 1; 1931 hammer2_xop_pdata(&xop->head); 1932 /* meta invalid */ 1933 1934 if (pmp->inode_tid < HAMMER2_INODE_START) 1935 pmp->inode_tid = HAMMER2_INODE_START; 1936 pmp->modify_tid = 1937 xop->head.cluster.focus->bref.modify_tid + 1; 1938 #if 0 1939 kprintf("PFS: Starting inode %jd\n", 1940 (intmax_t)pmp->inode_tid); 1941 kprintf("PMP focus good set nextino=%ld mod=%016jx\n", 1942 pmp->inode_tid, pmp->modify_tid); 1943 #endif 1944 wakeup(&pmp->iroot); 1945 1946 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1947 1948 /* 1949 * Prime the mount info. 1950 */ 1951 hammer2_vfs_statfs(mp, &mp->mnt_stat, NULL); 1952 break; 1953 } 1954 1955 /* 1956 * Loop, try again 1957 */ 1958 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1959 hammer2_inode_unlock(pmp->iroot); 1960 error = tsleep(&pmp->iroot, PCATCH, "h2root", hz); 1961 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED); 1962 if (error == EINTR) 1963 break; 1964 } 1965 1966 if (error) { 1967 hammer2_inode_unlock(pmp->iroot); 1968 *vpp = NULL; 1969 } else { 1970 vp = hammer2_igetv(pmp->iroot, &error); 1971 hammer2_inode_unlock(pmp->iroot); 1972 *vpp = vp; 1973 } 1974 1975 return (error); 1976 } 1977 1978 /* 1979 * Filesystem status 1980 * 1981 * XXX incorporate ipdata->meta.inode_quota and data_quota 1982 */ 1983 static 1984 int 1985 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 1986 { 1987 hammer2_pfs_t *pmp; 1988 hammer2_dev_t *hmp; 1989 hammer2_blockref_t bref; 1990 struct statfs tmp; 1991 int i; 1992 1993 /* 1994 * NOTE: iroot might not have validated the cluster yet. 1995 */ 1996 pmp = MPTOPMP(mp); 1997 1998 bzero(&tmp, sizeof(tmp)); 1999 2000 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { 2001 hmp = pmp->pfs_hmps[i]; 2002 if (hmp == NULL) 2003 continue; 2004 if (pmp->iroot->cluster.array[i].chain) 2005 bref = pmp->iroot->cluster.array[i].chain->bref; 2006 else 2007 bzero(&bref, sizeof(bref)); 2008 2009 tmp.f_files = bref.embed.stats.inode_count; 2010 tmp.f_ffree = 0; 2011 tmp.f_blocks = hmp->voldata.allocator_size / 2012 mp->mnt_vstat.f_bsize; 2013 tmp.f_bfree = hmp->voldata.allocator_free / 2014 mp->mnt_vstat.f_bsize; 2015 tmp.f_bavail = tmp.f_bfree; 2016 2017 if (cred && cred->cr_uid != 0) { 2018 uint64_t adj; 2019 2020 /* 5% */ 2021 adj = hmp->free_reserved / mp->mnt_vstat.f_bsize; 2022 tmp.f_blocks -= adj; 2023 tmp.f_bfree -= adj; 2024 tmp.f_bavail -= adj; 2025 } 2026 2027 mp->mnt_stat.f_blocks = tmp.f_blocks; 2028 mp->mnt_stat.f_bfree = tmp.f_bfree; 2029 mp->mnt_stat.f_bavail = tmp.f_bavail; 2030 mp->mnt_stat.f_files = tmp.f_files; 2031 mp->mnt_stat.f_ffree = tmp.f_ffree; 2032 2033 *sbp = mp->mnt_stat; 2034 } 2035 return (0); 2036 } 2037 2038 static 2039 int 2040 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred) 2041 { 2042 hammer2_pfs_t *pmp; 2043 hammer2_dev_t *hmp; 2044 hammer2_blockref_t bref; 2045 struct statvfs tmp; 2046 int i; 2047 2048 /* 2049 * NOTE: iroot might not have validated the cluster yet. 2050 */ 2051 pmp = MPTOPMP(mp); 2052 bzero(&tmp, sizeof(tmp)); 2053 2054 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { 2055 hmp = pmp->pfs_hmps[i]; 2056 if (hmp == NULL) 2057 continue; 2058 if (pmp->iroot->cluster.array[i].chain) 2059 bref = pmp->iroot->cluster.array[i].chain->bref; 2060 else 2061 bzero(&bref, sizeof(bref)); 2062 2063 tmp.f_files = bref.embed.stats.inode_count; 2064 tmp.f_ffree = 0; 2065 tmp.f_blocks = hmp->voldata.allocator_size / 2066 mp->mnt_vstat.f_bsize; 2067 tmp.f_bfree = hmp->voldata.allocator_free / 2068 mp->mnt_vstat.f_bsize; 2069 tmp.f_bavail = tmp.f_bfree; 2070 2071 if (cred && cred->cr_uid != 0) { 2072 uint64_t adj; 2073 2074 /* 5% */ 2075 adj = hmp->free_reserved / mp->mnt_vstat.f_bsize; 2076 tmp.f_blocks -= adj; 2077 tmp.f_bfree -= adj; 2078 tmp.f_bavail -= adj; 2079 } 2080 2081 mp->mnt_vstat.f_blocks = tmp.f_blocks; 2082 mp->mnt_vstat.f_bfree = tmp.f_bfree; 2083 mp->mnt_vstat.f_bavail = tmp.f_bavail; 2084 mp->mnt_vstat.f_files = tmp.f_files; 2085 mp->mnt_vstat.f_ffree = tmp.f_ffree; 2086 2087 *sbp = mp->mnt_vstat; 2088 } 2089 return (0); 2090 } 2091 2092 /* 2093 * Mount-time recovery (RW mounts) 2094 * 2095 * Updates to the free block table are allowed to lag flushes by one 2096 * transaction. In case of a crash, then on a fresh mount we must do an 2097 * incremental scan of the last committed transaction id and make sure that 2098 * all related blocks have been marked allocated. 2099 * 2100 * The super-root topology and each PFS has its own transaction id domain, 2101 * so we must track PFS boundary transitions. 2102 */ 2103 struct hammer2_recovery_elm { 2104 TAILQ_ENTRY(hammer2_recovery_elm) entry; 2105 hammer2_chain_t *chain; 2106 hammer2_tid_t sync_tid; 2107 }; 2108 2109 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm); 2110 2111 struct hammer2_recovery_info { 2112 struct hammer2_recovery_list list; 2113 hammer2_tid_t mtid; 2114 int depth; 2115 }; 2116 2117 static int hammer2_recovery_scan(hammer2_dev_t *hmp, 2118 hammer2_chain_t *parent, 2119 struct hammer2_recovery_info *info, 2120 hammer2_tid_t sync_tid); 2121 2122 #define HAMMER2_RECOVERY_MAXDEPTH 10 2123 2124 static 2125 int 2126 hammer2_recovery(hammer2_dev_t *hmp) 2127 { 2128 struct hammer2_recovery_info info; 2129 struct hammer2_recovery_elm *elm; 2130 hammer2_chain_t *parent; 2131 hammer2_tid_t sync_tid; 2132 hammer2_tid_t mirror_tid; 2133 int error; 2134 2135 hammer2_trans_init(hmp->spmp, 0); 2136 2137 sync_tid = hmp->voldata.freemap_tid; 2138 mirror_tid = hmp->voldata.mirror_tid; 2139 2140 kprintf("hammer2 mount \"%s\": ", hmp->devrepname); 2141 if (sync_tid >= mirror_tid) { 2142 kprintf(" no recovery needed\n"); 2143 } else { 2144 kprintf(" freemap recovery %016jx-%016jx\n", 2145 sync_tid + 1, mirror_tid); 2146 } 2147 2148 TAILQ_INIT(&info.list); 2149 info.depth = 0; 2150 parent = hammer2_chain_lookup_init(&hmp->vchain, 0); 2151 error = hammer2_recovery_scan(hmp, parent, &info, sync_tid); 2152 hammer2_chain_lookup_done(parent); 2153 2154 while ((elm = TAILQ_FIRST(&info.list)) != NULL) { 2155 TAILQ_REMOVE(&info.list, elm, entry); 2156 parent = elm->chain; 2157 sync_tid = elm->sync_tid; 2158 kfree(elm, M_HAMMER2); 2159 2160 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 2161 error |= hammer2_recovery_scan(hmp, parent, &info, 2162 hmp->voldata.freemap_tid); 2163 hammer2_chain_unlock(parent); 2164 hammer2_chain_drop(parent); /* drop elm->chain ref */ 2165 } 2166 2167 hammer2_trans_done(hmp->spmp, 0); 2168 2169 return error; 2170 } 2171 2172 static 2173 int 2174 hammer2_recovery_scan(hammer2_dev_t *hmp, hammer2_chain_t *parent, 2175 struct hammer2_recovery_info *info, 2176 hammer2_tid_t sync_tid) 2177 { 2178 const hammer2_inode_data_t *ripdata; 2179 hammer2_chain_t *chain; 2180 hammer2_blockref_t bref; 2181 int tmp_error; 2182 int rup_error; 2183 int error; 2184 int first; 2185 2186 /* 2187 * Adjust freemap to ensure that the block(s) are marked allocated. 2188 */ 2189 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) { 2190 hammer2_freemap_adjust(hmp, &parent->bref, 2191 HAMMER2_FREEMAP_DORECOVER); 2192 } 2193 2194 /* 2195 * Check type for recursive scan 2196 */ 2197 switch(parent->bref.type) { 2198 case HAMMER2_BREF_TYPE_VOLUME: 2199 /* data already instantiated */ 2200 break; 2201 case HAMMER2_BREF_TYPE_INODE: 2202 /* 2203 * Must instantiate data for DIRECTDATA test and also 2204 * for recursion. 2205 */ 2206 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 2207 ripdata = &hammer2_chain_rdata(parent)->ipdata; 2208 if (ripdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) { 2209 /* not applicable to recovery scan */ 2210 hammer2_chain_unlock(parent); 2211 return 0; 2212 } 2213 hammer2_chain_unlock(parent); 2214 break; 2215 case HAMMER2_BREF_TYPE_INDIRECT: 2216 /* 2217 * Must instantiate data for recursion 2218 */ 2219 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 2220 hammer2_chain_unlock(parent); 2221 break; 2222 case HAMMER2_BREF_TYPE_DIRENT: 2223 case HAMMER2_BREF_TYPE_DATA: 2224 case HAMMER2_BREF_TYPE_FREEMAP: 2225 case HAMMER2_BREF_TYPE_FREEMAP_NODE: 2226 case HAMMER2_BREF_TYPE_FREEMAP_LEAF: 2227 /* not applicable to recovery scan */ 2228 return 0; 2229 break; 2230 default: 2231 return HAMMER2_ERROR_BADBREF; 2232 } 2233 2234 /* 2235 * Defer operation if depth limit reached or if we are crossing a 2236 * PFS boundary. 2237 */ 2238 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH) { 2239 struct hammer2_recovery_elm *elm; 2240 2241 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK); 2242 elm->chain = parent; 2243 elm->sync_tid = sync_tid; 2244 hammer2_chain_ref(parent); 2245 TAILQ_INSERT_TAIL(&info->list, elm, entry); 2246 /* unlocked by caller */ 2247 2248 return(0); 2249 } 2250 2251 2252 /* 2253 * Recursive scan of the last flushed transaction only. We are 2254 * doing this without pmp assignments so don't leave the chains 2255 * hanging around after we are done with them. 2256 * 2257 * error Cumulative error this level only 2258 * rup_error Cumulative error for recursion 2259 * tmp_error Specific non-cumulative recursion error 2260 */ 2261 chain = NULL; 2262 first = 1; 2263 rup_error = 0; 2264 error = 0; 2265 2266 for (;;) { 2267 error |= hammer2_chain_scan(parent, &chain, &bref, 2268 &first, 2269 HAMMER2_LOOKUP_NODATA); 2270 2271 /* 2272 * Problem during scan or EOF 2273 */ 2274 if (error) 2275 break; 2276 2277 /* 2278 * If this is a leaf 2279 */ 2280 if (chain == NULL) { 2281 if (bref.mirror_tid > sync_tid) { 2282 hammer2_freemap_adjust(hmp, &bref, 2283 HAMMER2_FREEMAP_DORECOVER); 2284 } 2285 continue; 2286 } 2287 2288 /* 2289 * This may or may not be a recursive node. 2290 */ 2291 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE); 2292 if (bref.mirror_tid > sync_tid) { 2293 ++info->depth; 2294 tmp_error = hammer2_recovery_scan(hmp, chain, 2295 info, sync_tid); 2296 --info->depth; 2297 } else { 2298 tmp_error = 0; 2299 } 2300 2301 /* 2302 * Flush the recovery at the PFS boundary to stage it for 2303 * the final flush of the super-root topology. 2304 */ 2305 if (tmp_error == 0 && 2306 (bref.flags & HAMMER2_BREF_FLAG_PFSROOT) && 2307 (chain->flags & HAMMER2_CHAIN_ONFLUSH)) { 2308 hammer2_flush(chain, HAMMER2_FLUSH_TOP | 2309 HAMMER2_FLUSH_ALL); 2310 } 2311 rup_error |= tmp_error; 2312 } 2313 return ((error | rup_error) & ~HAMMER2_ERROR_EOF); 2314 } 2315 2316 /* 2317 * This fixes up an error introduced in earlier H2 implementations where 2318 * moving a PFS inode into an indirect block wound up causing the 2319 * HAMMER2_BREF_FLAG_PFSROOT flag in the bref to get cleared. 2320 */ 2321 static 2322 int 2323 hammer2_fixup_pfses(hammer2_dev_t *hmp) 2324 { 2325 const hammer2_inode_data_t *ripdata; 2326 hammer2_chain_t *parent; 2327 hammer2_chain_t *chain; 2328 hammer2_key_t key_next; 2329 hammer2_pfs_t *spmp; 2330 int error; 2331 2332 error = 0; 2333 2334 /* 2335 * Lookup mount point under the media-localized super-root. 2336 * 2337 * cluster->pmp will incorrectly point to spmp and must be fixed 2338 * up later on. 2339 */ 2340 spmp = hmp->spmp; 2341 hammer2_inode_lock(spmp->iroot, 0); 2342 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS); 2343 chain = hammer2_chain_lookup(&parent, &key_next, 2344 HAMMER2_KEY_MIN, HAMMER2_KEY_MAX, 2345 &error, 0); 2346 while (chain) { 2347 if (chain->bref.type != HAMMER2_BREF_TYPE_INODE) 2348 continue; 2349 if (chain->error) { 2350 kprintf("I/O error scanning PFS labels\n"); 2351 error |= chain->error; 2352 } else if ((chain->bref.flags & 2353 HAMMER2_BREF_FLAG_PFSROOT) == 0) { 2354 int error2; 2355 2356 ripdata = &chain->data->ipdata; 2357 hammer2_trans_init(hmp->spmp, 0); 2358 error2 = hammer2_chain_modify(chain, 2359 chain->bref.modify_tid, 2360 0, 0); 2361 if (error2 == 0) { 2362 kprintf("hammer2: Correct mis-flagged PFS %s\n", 2363 ripdata->filename); 2364 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT; 2365 } else { 2366 error |= error2; 2367 } 2368 hammer2_flush(chain, HAMMER2_FLUSH_TOP | 2369 HAMMER2_FLUSH_ALL); 2370 hammer2_trans_done(hmp->spmp, 0); 2371 } 2372 chain = hammer2_chain_next(&parent, chain, &key_next, 2373 key_next, HAMMER2_KEY_MAX, 2374 &error, 0); 2375 } 2376 if (parent) { 2377 hammer2_chain_unlock(parent); 2378 hammer2_chain_drop(parent); 2379 } 2380 hammer2_inode_unlock(spmp->iroot); 2381 2382 return error; 2383 } 2384 2385 /* 2386 * Sync a mount point; this is called periodically on a per-mount basis from 2387 * the filesystem syncer, and whenever a user issues a sync. 2388 */ 2389 int 2390 hammer2_vfs_sync(struct mount *mp, int waitfor) 2391 { 2392 hammer2_xop_flush_t *xop; 2393 struct hammer2_sync_info info; 2394 hammer2_inode_t *iroot; 2395 hammer2_pfs_t *pmp; 2396 int flags; 2397 int error; 2398 2399 pmp = MPTOPMP(mp); 2400 iroot = pmp->iroot; 2401 KKASSERT(iroot); 2402 KKASSERT(iroot->pmp == pmp); 2403 2404 /* 2405 * We can't acquire locks on existing vnodes while in a transaction 2406 * without risking a deadlock. This assumes that vfsync() can be 2407 * called without the vnode locked (which it can in DragonFly). 2408 * Otherwise we'd have to implement a multi-pass or flag the lock 2409 * failures and retry. 2410 * 2411 * The reclamation code interlocks with the sync list's token 2412 * (by removing the vnode from the scan list) before unlocking 2413 * the inode, giving us time to ref the inode. 2414 */ 2415 /*flags = VMSC_GETVP;*/ 2416 flags = 0; 2417 if (waitfor & MNT_LAZY) 2418 flags |= VMSC_ONEPASS; 2419 2420 /* 2421 * Flush vnodes individually using a normal transaction to avoid 2422 * stalling any concurrent operations. This will flush the related 2423 * buffer cache buffers and inodes to the media. 2424 * 2425 * For efficiency do an async pass before making sure with a 2426 * synchronous pass on all related buffer cache buffers. 2427 */ 2428 hammer2_trans_init(pmp, 0); 2429 2430 info.error = 0; 2431 2432 info.waitfor = MNT_NOWAIT; 2433 info.pass = 1; 2434 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info); 2435 2436 /* 2437 * Now do two passes making sure we get everything. The first pass 2438 * vfsync()s dirty vnodes. The second pass waits for their I/O's 2439 * to finish and cleans up the dirty flag on the vnode. 2440 */ 2441 info.pass = 1; 2442 info.waitfor = MNT_WAIT; 2443 vsyncscan(mp, flags, hammer2_sync_scan2, &info); 2444 2445 info.pass = 2; 2446 info.waitfor = MNT_WAIT; 2447 vsyncscan(mp, flags, hammer2_sync_scan2, &info); 2448 2449 /* 2450 * We must also run the sideq to handle any disconnected inodes 2451 * as the vnode scan will not see these. 2452 */ 2453 hammer2_inode_run_sideq(pmp, 1); 2454 hammer2_trans_done(pmp, 0); 2455 2456 /* 2457 * Start our flush transaction and flush the root topology down to 2458 * the inodes, but not the inodes themselves (which we already flushed 2459 * above). Any concurrent activity effecting inode contents will not 2460 * 2461 * The flush sequence will 2462 * 2463 * NOTE! It is still possible for the paging code to push pages 2464 * out via a UIO_NOCOPY hammer2_vop_write() during the main 2465 * flush. 2466 */ 2467 hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH); 2468 2469 /* 2470 * sync dirty vnodes again while in the flush transaction. This is 2471 * currently an expensive shim to makre sure the logical topology is 2472 * completely consistent before we flush the volume header. 2473 */ 2474 info.pass = 1; 2475 info.waitfor = MNT_WAIT; 2476 vsyncscan(mp, flags, hammer2_sync_scan2, &info); 2477 2478 info.pass = 2; 2479 info.waitfor = MNT_WAIT; 2480 vsyncscan(mp, flags, hammer2_sync_scan2, &info); 2481 2482 /* 2483 * Use the XOP interface to concurrently flush all nodes to 2484 * synchronize the PFSROOT subtopology to the media. A standard 2485 * end-of-scan ENOENT error indicates cluster sufficiency. 2486 * 2487 * Note that this flush will not be visible on crash recovery until 2488 * we flush the super-root topology in the next loop. 2489 * 2490 * XXX For now wait for all flushes to complete. 2491 */ 2492 if (iroot) { 2493 /* 2494 * If unmounting try to flush everything including any 2495 * sub-trees under inodes, just in case there is dangling 2496 * modified data, as a safety. Otherwise just flush up to 2497 * the inodes in this stage. 2498 */ 2499 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 2500 xop = hammer2_xop_alloc(iroot, HAMMER2_XOP_MODIFYING | 2501 HAMMER2_XOP_VOLHDR); 2502 } else { 2503 xop = hammer2_xop_alloc(iroot, HAMMER2_XOP_MODIFYING | 2504 HAMMER2_XOP_INODE_STOP | 2505 HAMMER2_XOP_VOLHDR); 2506 } 2507 hammer2_xop_start(&xop->head, &hammer2_inode_flush_desc); 2508 error = hammer2_xop_collect(&xop->head, 2509 HAMMER2_XOP_COLLECT_WAITALL); 2510 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 2511 if (error == HAMMER2_ERROR_ENOENT) 2512 error = 0; 2513 else 2514 error = hammer2_error_to_errno(error); 2515 } else { 2516 error = 0; 2517 } 2518 hammer2_trans_done(pmp, 0); 2519 2520 return (error); 2521 } 2522 2523 /* 2524 * Sync passes. 2525 * 2526 * Note that we ignore the tranasction mtid we got above. Instead, 2527 * each vfsync below will ultimately get its own via TRANS_BUFCACHE 2528 * transactions. 2529 * 2530 * WARNING! The frontend might be waiting on chnmem (limit_dirty_chains) 2531 * while holding a vnode locked. When this situation occurs we cannot 2532 * safely test whether it is ok to clear the dirty bit on the vnode. 2533 * However, we can still flush the inode's topology. 2534 */ 2535 static int 2536 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data) 2537 { 2538 struct hammer2_sync_info *info = data; 2539 hammer2_inode_t *ip; 2540 int error; 2541 2542 /* 2543 * Degenerate cases. Note that ip == NULL typically means the 2544 * syncer vnode itself and we don't want to vclrisdirty() in that 2545 * situation. 2546 */ 2547 ip = VTOI(vp); 2548 if (ip == NULL) { 2549 return(0); 2550 } 2551 if (vp->v_type == VNON || vp->v_type == VBAD) { 2552 vclrisdirty(vp); 2553 return(0); 2554 } 2555 2556 /* 2557 * Synchronize the buffer cche and inode meta-data to the backing 2558 * chain topology. 2559 * 2560 * vfsync is not necessarily synchronous, so it is best NOT to try 2561 * to flush the backing topology to media at this point. 2562 */ 2563 hammer2_inode_ref(ip); 2564 if ((ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MODIFIED)) || 2565 !RB_EMPTY(&vp->v_rbdirty_tree)) { 2566 if (info->pass == 1) 2567 vfsync(vp, info->waitfor, 1, NULL, NULL); 2568 else 2569 bio_track_wait(&vp->v_track_write, 0, 0); 2570 } 2571 if (info->pass == 2 && (vp->v_flag & VISDIRTY)) { 2572 /* 2573 * v_token is needed to interlock v_rbdirty_tree. 2574 */ 2575 lwkt_gettoken(&vp->v_token); 2576 hammer2_inode_lock(ip, 0); 2577 hammer2_inode_chain_sync(ip); 2578 hammer2_inode_chain_flush(ip); 2579 if ((ip->flags & (HAMMER2_INODE_MODIFIED | 2580 HAMMER2_INODE_RESIZED | 2581 HAMMER2_INODE_DIRTYDATA)) == 0 && 2582 RB_EMPTY(&vp->v_rbdirty_tree) && 2583 !bio_track_active(&vp->v_track_write)) { 2584 vclrisdirty(vp); 2585 } 2586 hammer2_inode_unlock(ip); 2587 lwkt_reltoken(&vp->v_token); 2588 } 2589 hammer2_inode_drop(ip); 2590 #if 1 2591 error = 0; 2592 if (error) 2593 info->error = error; 2594 #endif 2595 return(0); 2596 } 2597 2598 static 2599 int 2600 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp) 2601 { 2602 hammer2_inode_t *ip; 2603 2604 KKASSERT(MAXFIDSZ >= 16); 2605 ip = VTOI(vp); 2606 fhp->fid_len = offsetof(struct fid, fid_data[16]); 2607 fhp->fid_ext = 0; 2608 ((hammer2_tid_t *)fhp->fid_data)[0] = ip->meta.inum; 2609 ((hammer2_tid_t *)fhp->fid_data)[1] = 0; 2610 2611 return 0; 2612 } 2613 2614 static 2615 int 2616 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 2617 struct fid *fhp, struct vnode **vpp) 2618 { 2619 hammer2_pfs_t *pmp; 2620 hammer2_tid_t inum; 2621 int error; 2622 2623 pmp = MPTOPMP(mp); 2624 inum = ((hammer2_tid_t *)fhp->fid_data)[0] & HAMMER2_DIRHASH_USERMSK; 2625 if (vpp) { 2626 if (inum == 1) 2627 error = hammer2_vfs_root(mp, vpp); 2628 else 2629 error = hammer2_vfs_vget(mp, NULL, inum, vpp); 2630 } else { 2631 error = 0; 2632 } 2633 if (error) 2634 kprintf("fhtovp: %016jx -> %p, %d\n", inum, *vpp, error); 2635 return error; 2636 } 2637 2638 static 2639 int 2640 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 2641 int *exflagsp, struct ucred **credanonp) 2642 { 2643 hammer2_pfs_t *pmp; 2644 struct netcred *np; 2645 int error; 2646 2647 pmp = MPTOPMP(mp); 2648 np = vfs_export_lookup(mp, &pmp->export, nam); 2649 if (np) { 2650 *exflagsp = np->netc_exflags; 2651 *credanonp = &np->netc_anon; 2652 error = 0; 2653 } else { 2654 error = EACCES; 2655 } 2656 return error; 2657 } 2658 2659 /* 2660 * Support code for hammer2_vfs_mount(). Read, verify, and install the volume 2661 * header into the HMP 2662 * 2663 * XXX read four volhdrs and use the one with the highest TID whos CRC 2664 * matches. 2665 * 2666 * XXX check iCRCs. 2667 * 2668 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to 2669 * nonexistant locations. 2670 * 2671 * XXX Record selected volhdr and ring updates to each of 4 volhdrs 2672 */ 2673 static 2674 int 2675 hammer2_install_volume_header(hammer2_dev_t *hmp) 2676 { 2677 hammer2_volume_data_t *vd; 2678 struct buf *bp; 2679 hammer2_crc32_t crc0, crc, bcrc0, bcrc; 2680 int error_reported; 2681 int error; 2682 int valid; 2683 int i; 2684 2685 error_reported = 0; 2686 error = 0; 2687 valid = 0; 2688 bp = NULL; 2689 2690 /* 2691 * There are up to 4 copies of the volume header (syncs iterate 2692 * between them so there is no single master). We don't trust the 2693 * volu_size field so we don't know precisely how large the filesystem 2694 * is, so depend on the OS to return an error if we go beyond the 2695 * block device's EOF. 2696 */ 2697 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) { 2698 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64, 2699 HAMMER2_VOLUME_BYTES, &bp); 2700 if (error) { 2701 brelse(bp); 2702 bp = NULL; 2703 continue; 2704 } 2705 2706 vd = (struct hammer2_volume_data *) bp->b_data; 2707 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) && 2708 (vd->magic != HAMMER2_VOLUME_ID_ABO)) { 2709 brelse(bp); 2710 bp = NULL; 2711 continue; 2712 } 2713 2714 if (vd->magic == HAMMER2_VOLUME_ID_ABO) { 2715 /* XXX: Reversed-endianness filesystem */ 2716 kprintf("hammer2: reverse-endian filesystem detected"); 2717 brelse(bp); 2718 bp = NULL; 2719 continue; 2720 } 2721 2722 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0]; 2723 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF, 2724 HAMMER2_VOLUME_ICRC0_SIZE); 2725 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1]; 2726 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF, 2727 HAMMER2_VOLUME_ICRC1_SIZE); 2728 if ((crc0 != crc) || (bcrc0 != bcrc)) { 2729 kprintf("hammer2 volume header crc " 2730 "mismatch copy #%d %08x/%08x\n", 2731 i, crc0, crc); 2732 error_reported = 1; 2733 brelse(bp); 2734 bp = NULL; 2735 continue; 2736 } 2737 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) { 2738 valid = 1; 2739 hmp->voldata = *vd; 2740 hmp->volhdrno = i; 2741 } 2742 brelse(bp); 2743 bp = NULL; 2744 } 2745 if (valid) { 2746 hmp->volsync = hmp->voldata; 2747 hmp->free_reserved = hmp->voldata.allocator_size / 20; 2748 error = 0; 2749 if (error_reported || bootverbose || 1) { /* 1/DEBUG */ 2750 kprintf("hammer2: using volume header #%d\n", 2751 hmp->volhdrno); 2752 } 2753 } else { 2754 error = EINVAL; 2755 kprintf("hammer2: no valid volume headers found!\n"); 2756 } 2757 return (error); 2758 } 2759 2760 /* 2761 * This handles hysteresis on regular file flushes. Because the BIOs are 2762 * routed to a thread it is possible for an excessive number to build up 2763 * and cause long front-end stalls long before the runningbuffspace limit 2764 * is hit, so we implement hammer2_flush_pipe to control the 2765 * hysteresis. 2766 * 2767 * This is a particular problem when compression is used. 2768 */ 2769 void 2770 hammer2_lwinprog_ref(hammer2_pfs_t *pmp) 2771 { 2772 atomic_add_int(&pmp->count_lwinprog, 1); 2773 } 2774 2775 void 2776 hammer2_lwinprog_drop(hammer2_pfs_t *pmp) 2777 { 2778 int lwinprog; 2779 2780 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1); 2781 if ((lwinprog & HAMMER2_LWINPROG_WAITING) && 2782 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) { 2783 atomic_clear_int(&pmp->count_lwinprog, 2784 HAMMER2_LWINPROG_WAITING); 2785 wakeup(&pmp->count_lwinprog); 2786 } 2787 if ((lwinprog & HAMMER2_LWINPROG_WAITING0) && 2788 (lwinprog & HAMMER2_LWINPROG_MASK) <= 0) { 2789 atomic_clear_int(&pmp->count_lwinprog, 2790 HAMMER2_LWINPROG_WAITING0); 2791 wakeup(&pmp->count_lwinprog); 2792 } 2793 } 2794 2795 void 2796 hammer2_lwinprog_wait(hammer2_pfs_t *pmp, int flush_pipe) 2797 { 2798 int lwinprog; 2799 int lwflag = (flush_pipe) ? HAMMER2_LWINPROG_WAITING : 2800 HAMMER2_LWINPROG_WAITING0; 2801 2802 for (;;) { 2803 lwinprog = pmp->count_lwinprog; 2804 cpu_ccfence(); 2805 if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe) 2806 break; 2807 tsleep_interlock(&pmp->count_lwinprog, 0); 2808 atomic_set_int(&pmp->count_lwinprog, lwflag); 2809 lwinprog = pmp->count_lwinprog; 2810 if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe) 2811 break; 2812 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz); 2813 } 2814 } 2815 2816 /* 2817 * Attempt to proactively fsync dirty vnodes if we have too many. This 2818 * solves an issue where the kernel syncer thread can get seriously behind 2819 * when multiple user processes/threads are furiously modifying inodes. 2820 * This situation can occur on slow storage and is only limited by 2821 * kern.maxvnodes without the moderation code below. It is made worse 2822 * when the device buffers underlying the modified inodes (which are clean) 2823 * get evicted before the flush can occur, forcing a re-read. 2824 * 2825 * We do not want sysads to feel that they have to torpedo kern.maxvnodes 2826 * to solve this problem, so we implement vfs.hammer2.limit_dirty_inodes 2827 * (per-mount-basis) and default it to something reasonable. 2828 */ 2829 static void 2830 hammer2_pfs_moderate(hammer2_inode_t *ip, int always_moderate) 2831 { 2832 hammer2_pfs_t *pmp = ip->pmp; 2833 struct mount *mp = pmp->mp; 2834 2835 if (mp && vn_syncer_count(mp) > hammer2_limit_dirty_inodes) { 2836 vn_syncer_one(mp); 2837 } 2838 } 2839 2840 /* 2841 * Manage excessive memory resource use for chain and related 2842 * structures. 2843 * 2844 * Called without any inode locks or transaction locks. VNodes 2845 * might be locked by the kernel in the call stack. 2846 */ 2847 void 2848 hammer2_pfs_memory_wait(hammer2_inode_t *ip, int always_moderate) 2849 { 2850 hammer2_pfs_t *pmp = ip->pmp; 2851 uint32_t waiting; 2852 uint32_t count; 2853 uint32_t limit; 2854 #if 0 2855 static int zzticks; 2856 #endif 2857 2858 /* 2859 * Moderate the number of dirty inodes 2860 */ 2861 hammer2_pfs_moderate(ip, always_moderate); 2862 2863 /* 2864 * Atomic check condition and wait. Also do an early speedup of 2865 * the syncer to try to avoid hitting the wait. 2866 */ 2867 for (;;) { 2868 waiting = pmp->inmem_dirty_chains; 2869 cpu_ccfence(); 2870 count = waiting & HAMMER2_DIRTYCHAIN_MASK; 2871 2872 limit = pmp->mp->mnt_nvnodelistsize / 10; 2873 if (limit < hammer2_limit_dirty_chains) 2874 limit = hammer2_limit_dirty_chains; 2875 if (limit < 1000) 2876 limit = 1000; 2877 2878 #if 0 2879 if ((int)(ticks - zzticks) > hz) { 2880 zzticks = ticks; 2881 kprintf("count %ld %ld\n", count, limit); 2882 } 2883 #endif 2884 2885 /* 2886 * Block if there are too many dirty chains present, wait 2887 * for the flush to clean some out. 2888 */ 2889 if (count > limit) { 2890 hammer2_pfs_moderate(ip, always_moderate); 2891 tsleep_interlock(&pmp->inmem_dirty_chains, 0); 2892 if (atomic_cmpset_int(&pmp->inmem_dirty_chains, 2893 waiting, 2894 waiting | HAMMER2_DIRTYCHAIN_WAITING)) { 2895 if (ticks != pmp->speedup_ticks) { 2896 pmp->speedup_ticks = ticks; 2897 speedup_syncer(pmp->mp); 2898 } 2899 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED, 2900 "chnmem", hz); 2901 } 2902 continue; /* loop on success or fail */ 2903 } 2904 2905 /* 2906 * Try to start an early flush before we are forced to block. 2907 */ 2908 if (count > limit * 5 / 10 && 2909 ticks != pmp->speedup_ticks) { 2910 pmp->speedup_ticks = ticks; 2911 speedup_syncer(pmp->mp); 2912 } 2913 break; 2914 } 2915 } 2916 2917 void 2918 hammer2_pfs_memory_inc(hammer2_pfs_t *pmp) 2919 { 2920 if (pmp) { 2921 atomic_add_int(&pmp->inmem_dirty_chains, 1); 2922 } 2923 } 2924 2925 void 2926 hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp) 2927 { 2928 uint32_t waiting; 2929 2930 if (pmp) { 2931 waiting = atomic_fetchadd_int(&pmp->inmem_dirty_chains, -1); 2932 /* don't need --waiting to test flag */ 2933 if (waiting & HAMMER2_DIRTYCHAIN_WAITING) { 2934 atomic_clear_int(&pmp->inmem_dirty_chains, 2935 HAMMER2_DIRTYCHAIN_WAITING); 2936 wakeup(&pmp->inmem_dirty_chains); 2937 } 2938 } 2939 } 2940 2941 /* 2942 * Returns 0 if the filesystem has tons of free space 2943 * Returns 1 if the filesystem has less than 10% remaining 2944 * Returns 2 if the filesystem has less than 2%/5% (user/root) remaining. 2945 */ 2946 int 2947 hammer2_vfs_enospace(hammer2_inode_t *ip, off_t bytes, struct ucred *cred) 2948 { 2949 hammer2_pfs_t *pmp; 2950 hammer2_dev_t *hmp; 2951 hammer2_off_t free_reserved; 2952 hammer2_off_t free_nominal; 2953 int i; 2954 2955 pmp = ip->pmp; 2956 2957 if (pmp->free_ticks == 0 || pmp->free_ticks != ticks) { 2958 free_reserved = HAMMER2_SEGSIZE; 2959 free_nominal = 0x7FFFFFFFFFFFFFFFLLU; 2960 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { 2961 hmp = pmp->pfs_hmps[i]; 2962 if (hmp == NULL) 2963 continue; 2964 if (pmp->pfs_types[i] != HAMMER2_PFSTYPE_MASTER && 2965 pmp->pfs_types[i] != HAMMER2_PFSTYPE_SOFT_MASTER) 2966 continue; 2967 2968 if (free_nominal > hmp->voldata.allocator_free) 2969 free_nominal = hmp->voldata.allocator_free; 2970 if (free_reserved < hmp->free_reserved) 2971 free_reserved = hmp->free_reserved; 2972 } 2973 2974 /* 2975 * SMP races ok 2976 */ 2977 pmp->free_reserved = free_reserved; 2978 pmp->free_nominal = free_nominal; 2979 pmp->free_ticks = ticks; 2980 } else { 2981 free_reserved = pmp->free_reserved; 2982 free_nominal = pmp->free_nominal; 2983 } 2984 if (cred && cred->cr_uid != 0) { 2985 if ((int64_t)(free_nominal - bytes) < 2986 (int64_t)free_reserved) { 2987 return 2; 2988 } 2989 } else { 2990 if ((int64_t)(free_nominal - bytes) < 2991 (int64_t)free_reserved / 2) { 2992 return 2; 2993 } 2994 } 2995 if ((int64_t)(free_nominal - bytes) < (int64_t)free_reserved * 2) 2996 return 1; 2997 return 0; 2998 } 2999 3000 /* 3001 * Debugging 3002 */ 3003 void 3004 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx, 3005 u_int flags) 3006 { 3007 hammer2_chain_t *scan; 3008 hammer2_chain_t *parent; 3009 3010 --*countp; 3011 if (*countp == 0) { 3012 kprintf("%*.*s...\n", tab, tab, ""); 3013 return; 3014 } 3015 if (*countp < 0) 3016 return; 3017 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n", 3018 tab, tab, "", pfx, 3019 chain, chain->bref.type, 3020 chain->bref.key, chain->bref.keybits, 3021 chain->bref.mirror_tid); 3022 3023 kprintf("%*.*s [%08x] (%s) refs=%d", 3024 tab, tab, "", 3025 chain->flags, 3026 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE && 3027 chain->data) ? (char *)chain->data->ipdata.filename : "?"), 3028 chain->refs); 3029 3030 parent = chain->parent; 3031 if (parent) 3032 kprintf("\n%*.*s p=%p [pflags %08x prefs %d", 3033 tab, tab, "", 3034 parent, parent->flags, parent->refs); 3035 if (RB_EMPTY(&chain->core.rbtree)) { 3036 kprintf("\n"); 3037 } else { 3038 kprintf(" {\n"); 3039 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree) { 3040 if ((scan->flags & flags) || flags == (u_int)-1) { 3041 hammer2_dump_chain(scan, tab + 4, countp, 'a', 3042 flags); 3043 } 3044 } 3045 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data) 3046 kprintf("%*.*s}(%s)\n", tab, tab, "", 3047 chain->data->ipdata.filename); 3048 else 3049 kprintf("%*.*s}\n", tab, tab, ""); 3050 } 3051 } 3052