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