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