1 /* 2 * Copyright (c) 2011-2018 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression) 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/nlookup.h> 39 #include <sys/vnode.h> 40 #include <sys/mount.h> 41 #include <sys/fcntl.h> 42 #include <sys/buf.h> 43 #include <sys/uuid.h> 44 #include <sys/vfsops.h> 45 #include <sys/sysctl.h> 46 #include <sys/socket.h> 47 #include <sys/objcache.h> 48 49 #include <sys/proc.h> 50 #include <sys/mountctl.h> 51 #include <sys/dirent.h> 52 #include <sys/uio.h> 53 54 #include "hammer2.h" 55 #include "hammer2_disk.h" 56 #include "hammer2_mount.h" 57 #include "hammer2_lz4.h" 58 59 #include "zlib/hammer2_zlib.h" 60 61 #define REPORT_REFS_ERRORS 1 /* XXX remove me */ 62 63 MALLOC_DEFINE(M_OBJCACHE, "objcache", "Object Cache"); 64 65 struct hammer2_sync_info { 66 int error; 67 int waitfor; 68 int pass; 69 }; 70 71 TAILQ_HEAD(hammer2_mntlist, hammer2_dev); 72 static struct hammer2_mntlist hammer2_mntlist; 73 74 struct hammer2_pfslist hammer2_pfslist; 75 struct hammer2_pfslist hammer2_spmplist; 76 struct lock hammer2_mntlk; 77 78 int hammer2_supported_version = HAMMER2_VOL_VERSION_DEFAULT; 79 int hammer2_debug; 80 int hammer2_xopgroups; 81 long hammer2_debug_inode; 82 int hammer2_cluster_meta_read = 1; /* physical read-ahead */ 83 int hammer2_cluster_data_read = 4; /* physical read-ahead */ 84 int hammer2_cluster_write = 0; /* physical write clustering */ 85 int hammer2_dedup_enable = 1; 86 int hammer2_always_compress = 0; /* always try to compress */ 87 int hammer2_flush_pipe = 100; 88 int hammer2_dio_count; 89 int hammer2_dio_limit = 256; 90 int hammer2_bulkfree_tps = 5000; 91 int hammer2_worker_rmask = 3; 92 long hammer2_chain_allocs; 93 long hammer2_limit_dirty_chains; 94 long hammer2_limit_dirty_inodes; 95 long hammer2_count_modified_chains; 96 long hammer2_iod_file_read; 97 long hammer2_iod_meta_read; 98 long hammer2_iod_indr_read; 99 long hammer2_iod_fmap_read; 100 long hammer2_iod_volu_read; 101 long hammer2_iod_file_write; 102 long hammer2_iod_file_wembed; 103 long hammer2_iod_file_wzero; 104 long hammer2_iod_file_wdedup; 105 long hammer2_iod_meta_write; 106 long hammer2_iod_indr_write; 107 long hammer2_iod_fmap_write; 108 long hammer2_iod_volu_write; 109 static long hammer2_iod_inode_creates; 110 static long hammer2_iod_inode_deletes; 111 112 long hammer2_process_icrc32; 113 long hammer2_process_xxhash64; 114 115 MALLOC_DECLARE(M_HAMMER2_CBUFFER); 116 MALLOC_DEFINE(M_HAMMER2_CBUFFER, "HAMMER2-compbuffer", 117 "Buffer used for compression."); 118 119 MALLOC_DECLARE(M_HAMMER2_DEBUFFER); 120 MALLOC_DEFINE(M_HAMMER2_DEBUFFER, "HAMMER2-decompbuffer", 121 "Buffer used for decompression."); 122 123 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem"); 124 125 SYSCTL_INT(_vfs_hammer2, OID_AUTO, supported_version, CTLFLAG_RD, 126 &hammer2_supported_version, 0, ""); 127 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW, 128 &hammer2_debug, 0, ""); 129 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, debug_inode, CTLFLAG_RW, 130 &hammer2_debug_inode, 0, ""); 131 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_meta_read, CTLFLAG_RW, 132 &hammer2_cluster_meta_read, 0, ""); 133 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_data_read, CTLFLAG_RW, 134 &hammer2_cluster_data_read, 0, ""); 135 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_write, CTLFLAG_RW, 136 &hammer2_cluster_write, 0, ""); 137 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dedup_enable, CTLFLAG_RW, 138 &hammer2_dedup_enable, 0, ""); 139 SYSCTL_INT(_vfs_hammer2, OID_AUTO, always_compress, CTLFLAG_RW, 140 &hammer2_always_compress, 0, ""); 141 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW, 142 &hammer2_flush_pipe, 0, ""); 143 SYSCTL_INT(_vfs_hammer2, OID_AUTO, worker_rmask, CTLFLAG_RW, 144 &hammer2_worker_rmask, 0, ""); 145 SYSCTL_INT(_vfs_hammer2, OID_AUTO, bulkfree_tps, CTLFLAG_RW, 146 &hammer2_bulkfree_tps, 0, ""); 147 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, chain_allocs, CTLFLAG_RW, 148 &hammer2_chain_allocs, 0, ""); 149 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW, 150 &hammer2_limit_dirty_chains, 0, ""); 151 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_inodes, CTLFLAG_RW, 152 &hammer2_limit_dirty_inodes, 0, ""); 153 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, count_modified_chains, CTLFLAG_RW, 154 &hammer2_count_modified_chains, 0, ""); 155 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD, 156 &hammer2_dio_count, 0, ""); 157 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_limit, CTLFLAG_RW, 158 &hammer2_dio_limit, 0, ""); 159 160 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW, 161 &hammer2_iod_file_read, 0, ""); 162 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW, 163 &hammer2_iod_meta_read, 0, ""); 164 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW, 165 &hammer2_iod_indr_read, 0, ""); 166 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW, 167 &hammer2_iod_fmap_read, 0, ""); 168 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW, 169 &hammer2_iod_volu_read, 0, ""); 170 171 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW, 172 &hammer2_iod_file_write, 0, ""); 173 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wembed, CTLFLAG_RW, 174 &hammer2_iod_file_wembed, 0, ""); 175 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wzero, CTLFLAG_RW, 176 &hammer2_iod_file_wzero, 0, ""); 177 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wdedup, CTLFLAG_RW, 178 &hammer2_iod_file_wdedup, 0, ""); 179 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW, 180 &hammer2_iod_meta_write, 0, ""); 181 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW, 182 &hammer2_iod_indr_write, 0, ""); 183 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW, 184 &hammer2_iod_fmap_write, 0, ""); 185 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW, 186 &hammer2_iod_volu_write, 0, ""); 187 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_inode_creates, CTLFLAG_RW, 188 &hammer2_iod_inode_creates, 0, ""); 189 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_inode_deletes, CTLFLAG_RW, 190 &hammer2_iod_inode_deletes, 0, ""); 191 192 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, process_icrc32, CTLFLAG_RW, 193 &hammer2_process_icrc32, 0, ""); 194 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, process_xxhash64, CTLFLAG_RW, 195 &hammer2_process_xxhash64, 0, ""); 196 197 static int hammer2_vfs_init(struct vfsconf *conf); 198 static int hammer2_vfs_uninit(struct vfsconf *vfsp); 199 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data, 200 struct ucred *cred); 201 static int hammer2_remount(hammer2_dev_t *, struct mount *, char *, 202 struct ucred *); 203 static int hammer2_recovery(hammer2_dev_t *hmp); 204 static int hammer2_vfs_unmount(struct mount *mp, int mntflags); 205 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp); 206 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, 207 struct ucred *cred); 208 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, 209 struct ucred *cred); 210 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 211 struct fid *fhp, struct vnode **vpp); 212 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp); 213 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 214 int *exflagsp, struct ucred **credanonp); 215 static int hammer2_vfs_modifying(struct mount *mp); 216 217 static void hammer2_update_pmps(hammer2_dev_t *hmp); 218 219 static void hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp); 220 static void hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, 221 hammer2_dev_t *hmp); 222 static int hammer2_fixup_pfses(hammer2_dev_t *hmp); 223 224 /* 225 * HAMMER2 vfs operations. 226 */ 227 static struct vfsops hammer2_vfsops = { 228 .vfs_flags = 0, 229 .vfs_init = hammer2_vfs_init, 230 .vfs_uninit = hammer2_vfs_uninit, 231 .vfs_sync = hammer2_vfs_sync, 232 .vfs_mount = hammer2_vfs_mount, 233 .vfs_unmount = hammer2_vfs_unmount, 234 .vfs_root = hammer2_vfs_root, 235 .vfs_statfs = hammer2_vfs_statfs, 236 .vfs_statvfs = hammer2_vfs_statvfs, 237 .vfs_vget = hammer2_vfs_vget, 238 .vfs_vptofh = hammer2_vfs_vptofh, 239 .vfs_fhtovp = hammer2_vfs_fhtovp, 240 .vfs_checkexp = hammer2_vfs_checkexp, 241 .vfs_modifying = hammer2_vfs_modifying 242 }; 243 244 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", ""); 245 246 VFS_SET(hammer2_vfsops, hammer2, VFCF_MPSAFE); 247 MODULE_VERSION(hammer2, 1); 248 249 static 250 int 251 hammer2_vfs_init(struct vfsconf *conf) 252 { 253 static struct objcache_malloc_args margs_read; 254 static struct objcache_malloc_args margs_write; 255 static struct objcache_malloc_args margs_vop; 256 257 int error; 258 259 error = 0; 260 kmalloc_raise_limit(M_HAMMER2, 0); /* unlimited */ 261 262 /* 263 * hammer2_xopgroups must be even and is most optimal if 264 * 2 x ncpus so strategy functions can be queued to the same 265 * cpu. 266 */ 267 hammer2_xopgroups = HAMMER2_XOPGROUPS_MIN; 268 if (hammer2_xopgroups < ncpus * 2) 269 hammer2_xopgroups = ncpus * 2; 270 271 /* 272 * A large DIO cache is needed to retain dedup enablement masks. 273 * The bulkfree code clears related masks as part of the disk block 274 * recycling algorithm, preventing it from being used for a later 275 * dedup. 276 * 277 * NOTE: A large buffer cache can actually interfere with dedup 278 * operation because we dedup based on media physical buffers 279 * and not logical buffers. Try to make the DIO case large 280 * enough to avoid this problem, but also cap it. 281 */ 282 hammer2_dio_limit = nbuf * 2; 283 if (hammer2_dio_limit > 100000) 284 hammer2_dio_limit = 100000; 285 286 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref)) 287 error = EINVAL; 288 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data)) 289 error = EINVAL; 290 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data)) 291 error = EINVAL; 292 293 if (error) 294 kprintf("HAMMER2 structure size mismatch; cannot continue.\n"); 295 296 margs_read.objsize = 65536; 297 margs_read.mtype = M_HAMMER2_DEBUFFER; 298 299 margs_write.objsize = 32768; 300 margs_write.mtype = M_HAMMER2_CBUFFER; 301 302 margs_vop.objsize = sizeof(hammer2_xop_t); 303 margs_vop.mtype = M_HAMMER2; 304 305 /* 306 * Note thaht for the XOPS cache we want backing store allocations 307 * to use M_ZERO. This is not allowed in objcache_get() (to avoid 308 * confusion), so use the backing store function that does it. This 309 * means that initial XOPS objects are zerod but REUSED objects are 310 * not. So we are responsible for cleaning the object up sufficiently 311 * for our needs before objcache_put()ing it back (typically just the 312 * FIFO indices). 313 */ 314 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc, 315 0, 1, NULL, NULL, NULL, 316 objcache_malloc_alloc, 317 objcache_malloc_free, 318 &margs_read); 319 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc, 320 0, 1, NULL, NULL, NULL, 321 objcache_malloc_alloc, 322 objcache_malloc_free, 323 &margs_write); 324 cache_xops = objcache_create(margs_vop.mtype->ks_shortdesc, 325 0, 1, NULL, NULL, NULL, 326 objcache_malloc_alloc_zero, 327 objcache_malloc_free, 328 &margs_vop); 329 330 331 lockinit(&hammer2_mntlk, "mntlk", 0, 0); 332 TAILQ_INIT(&hammer2_mntlist); 333 TAILQ_INIT(&hammer2_pfslist); 334 TAILQ_INIT(&hammer2_spmplist); 335 336 hammer2_limit_dirty_chains = maxvnodes / 10; 337 if (hammer2_limit_dirty_chains > HAMMER2_LIMIT_DIRTY_CHAINS) 338 hammer2_limit_dirty_chains = HAMMER2_LIMIT_DIRTY_CHAINS; 339 if (hammer2_limit_dirty_chains < 1000) 340 hammer2_limit_dirty_chains = 1000; 341 342 hammer2_limit_dirty_inodes = maxvnodes / 25; 343 if (hammer2_limit_dirty_inodes < 100) 344 hammer2_limit_dirty_inodes = 100; 345 if (hammer2_limit_dirty_inodes > HAMMER2_LIMIT_DIRTY_INODES) 346 hammer2_limit_dirty_inodes = HAMMER2_LIMIT_DIRTY_INODES; 347 348 return (error); 349 } 350 351 static 352 int 353 hammer2_vfs_uninit(struct vfsconf *vfsp __unused) 354 { 355 objcache_destroy(cache_buffer_read); 356 objcache_destroy(cache_buffer_write); 357 objcache_destroy(cache_xops); 358 return 0; 359 } 360 361 /* 362 * Core PFS allocator. Used to allocate or reference the pmp structure 363 * for PFS cluster mounts and the spmp structure for media (hmp) structures. 364 * The pmp can be passed in or loaded by this function using the chain and 365 * inode data. 366 * 367 * pmp->modify_tid tracks new modify_tid transaction ids for front-end 368 * transactions. Note that synchronization does not use this field. 369 * (typically frontend operations and synchronization cannot run on the 370 * same PFS node at the same time). 371 * 372 * XXX check locking 373 */ 374 hammer2_pfs_t * 375 hammer2_pfsalloc(hammer2_chain_t *chain, 376 const hammer2_inode_data_t *ripdata, 377 hammer2_tid_t modify_tid, hammer2_dev_t *force_local) 378 { 379 hammer2_pfs_t *pmp; 380 hammer2_inode_t *iroot; 381 int count; 382 int i; 383 int j; 384 385 pmp = NULL; 386 387 /* 388 * Locate or create the PFS based on the cluster id. If ripdata 389 * is NULL this is a spmp which is unique and is always allocated. 390 * 391 * If the device is mounted in local mode all PFSs are considered 392 * independent and not part of any cluster (for debugging only). 393 */ 394 if (ripdata) { 395 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) { 396 if (force_local != pmp->force_local) 397 continue; 398 if (force_local == NULL && 399 bcmp(&pmp->pfs_clid, &ripdata->meta.pfs_clid, 400 sizeof(pmp->pfs_clid)) == 0) { 401 break; 402 } else if (force_local && pmp->pfs_names[0] && 403 strcmp(pmp->pfs_names[0], ripdata->filename) == 0) { 404 break; 405 } 406 } 407 } 408 409 if (pmp == NULL) { 410 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO); 411 pmp->force_local = force_local; 412 hammer2_trans_manage_init(pmp); 413 kmalloc_create(&pmp->minode, "HAMMER2-inodes"); 414 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg"); 415 lockinit(&pmp->lock, "pfslk", 0, 0); 416 lockinit(&pmp->lock_nlink, "h2nlink", 0, 0); 417 spin_init(&pmp->inum_spin, "hm2pfsalloc_inum"); 418 spin_init(&pmp->xop_spin, "h2xop"); 419 spin_init(&pmp->lru_spin, "h2lru"); 420 RB_INIT(&pmp->inum_tree); 421 TAILQ_INIT(&pmp->syncq); 422 TAILQ_INIT(&pmp->depq); 423 TAILQ_INIT(&pmp->lru_list); 424 spin_init(&pmp->list_spin, "h2pfsalloc_list"); 425 426 /* 427 * Save the last media transaction id for the flusher. Set 428 * initial 429 */ 430 if (ripdata) { 431 pmp->pfs_clid = ripdata->meta.pfs_clid; 432 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry); 433 } else { 434 pmp->flags |= HAMMER2_PMPF_SPMP; 435 TAILQ_INSERT_TAIL(&hammer2_spmplist, pmp, mntentry); 436 } 437 438 /* 439 * The synchronization thread may start too early, make 440 * sure it stays frozen until we are ready to let it go. 441 * XXX 442 */ 443 /* 444 pmp->primary_thr.flags = HAMMER2_THREAD_FROZEN | 445 HAMMER2_THREAD_REMASTER; 446 */ 447 } 448 449 /* 450 * Create the PFS's root inode and any missing XOP helper threads. 451 */ 452 if ((iroot = pmp->iroot) == NULL) { 453 iroot = hammer2_inode_get(pmp, NULL, 1, -1); 454 if (ripdata) 455 iroot->meta = ripdata->meta; 456 pmp->iroot = iroot; 457 hammer2_inode_ref(iroot); 458 hammer2_inode_unlock(iroot); 459 } 460 461 /* 462 * Stop here if no chain is passed in. 463 */ 464 if (chain == NULL) 465 goto done; 466 467 /* 468 * When a chain is passed in we must add it to the PFS's root 469 * inode, update pmp->pfs_types[], and update the syncronization 470 * threads. 471 * 472 * When forcing local mode, mark the PFS as a MASTER regardless. 473 * 474 * At the moment empty spots can develop due to removals or failures. 475 * Ultimately we want to re-fill these spots but doing so might 476 * confused running code. XXX 477 */ 478 hammer2_inode_ref(iroot); 479 hammer2_mtx_ex(&iroot->lock); 480 j = iroot->cluster.nchains; 481 482 if (j == HAMMER2_MAXCLUSTER) { 483 kprintf("hammer2_pfsalloc: cluster full!\n"); 484 /* XXX fatal error? */ 485 } else { 486 KKASSERT(chain->pmp == NULL); 487 chain->pmp = pmp; 488 hammer2_chain_ref(chain); 489 iroot->cluster.array[j].chain = chain; 490 if (force_local) 491 pmp->pfs_types[j] = HAMMER2_PFSTYPE_MASTER; 492 else 493 pmp->pfs_types[j] = ripdata->meta.pfs_type; 494 pmp->pfs_names[j] = kstrdup(ripdata->filename, M_HAMMER2); 495 pmp->pfs_hmps[j] = chain->hmp; 496 hammer2_spin_ex(&pmp->inum_spin); 497 pmp->pfs_iroot_blocksets[j] = chain->data->ipdata.u.blockset; 498 hammer2_spin_unex(&pmp->inum_spin); 499 500 /* 501 * If the PFS is already mounted we must account 502 * for the mount_count here. 503 */ 504 if (pmp->mp) 505 ++chain->hmp->mount_count; 506 507 /* 508 * May have to fixup dirty chain tracking. Previous 509 * pmp was NULL so nothing to undo. 510 */ 511 if (chain->flags & HAMMER2_CHAIN_MODIFIED) 512 hammer2_pfs_memory_inc(pmp); 513 ++j; 514 } 515 iroot->cluster.nchains = j; 516 517 /* 518 * Update nmasters from any PFS inode which is part of the cluster. 519 * It is possible that this will result in a value which is too 520 * high. MASTER PFSs are authoritative for pfs_nmasters and will 521 * override this value later on. 522 * 523 * (This informs us of masters that might not currently be 524 * discoverable by this mount). 525 */ 526 if (ripdata && pmp->pfs_nmasters < ripdata->meta.pfs_nmasters) { 527 pmp->pfs_nmasters = ripdata->meta.pfs_nmasters; 528 } 529 530 /* 531 * Count visible masters. Masters are usually added with 532 * ripdata->meta.pfs_nmasters set to 1. This detects when there 533 * are more (XXX and must update the master inodes). 534 */ 535 count = 0; 536 for (i = 0; i < iroot->cluster.nchains; ++i) { 537 if (pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) 538 ++count; 539 } 540 if (pmp->pfs_nmasters < count) 541 pmp->pfs_nmasters = count; 542 543 /* 544 * Create missing synchronization and support threads. 545 * 546 * Single-node masters (including snapshots) have nothing to 547 * synchronize and do not require this thread. 548 * 549 * Multi-node masters or any number of soft masters, slaves, copy, 550 * or other PFS types need the thread. 551 * 552 * Each thread is responsible for its particular cluster index. 553 * We use independent threads so stalls or mismatches related to 554 * any given target do not affect other targets. 555 */ 556 for (i = 0; i < iroot->cluster.nchains; ++i) { 557 /* 558 * Single-node masters (including snapshots) have nothing 559 * to synchronize and will make direct xops support calls, 560 * thus they do not require this thread. 561 * 562 * Note that there can be thousands of snapshots. We do not 563 * want to create thousands of threads. 564 */ 565 if (pmp->pfs_nmasters <= 1 && 566 pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) { 567 continue; 568 } 569 570 /* 571 * Sync support thread 572 */ 573 if (pmp->sync_thrs[i].td == NULL) { 574 hammer2_thr_create(&pmp->sync_thrs[i], pmp, NULL, 575 "h2nod", i, -1, 576 hammer2_primary_sync_thread); 577 } 578 } 579 580 /* 581 * Create missing Xop threads 582 * 583 * NOTE: We create helper threads for all mounted PFSs or any 584 * PFSs with 2+ nodes (so the sync thread can update them, 585 * even if not mounted). 586 */ 587 if (pmp->mp || iroot->cluster.nchains >= 2) 588 hammer2_xop_helper_create(pmp); 589 590 hammer2_mtx_unlock(&iroot->lock); 591 hammer2_inode_drop(iroot); 592 done: 593 return pmp; 594 } 595 596 /* 597 * Deallocate an element of a probed PFS. If destroying and this is a 598 * MASTER, adjust nmasters. 599 * 600 * This function does not physically destroy the PFS element in its device 601 * under the super-root (see hammer2_ioctl_pfs_delete()). 602 */ 603 void 604 hammer2_pfsdealloc(hammer2_pfs_t *pmp, int clindex, int destroying) 605 { 606 hammer2_inode_t *iroot; 607 hammer2_chain_t *chain; 608 int j; 609 610 /* 611 * Cleanup our reference on iroot. iroot is (should) not be needed 612 * by the flush code. 613 */ 614 iroot = pmp->iroot; 615 if (iroot) { 616 /* 617 * Stop synchronizing 618 * 619 * XXX flush after acquiring the iroot lock. 620 * XXX clean out the cluster index from all inode structures. 621 */ 622 hammer2_thr_delete(&pmp->sync_thrs[clindex]); 623 624 /* 625 * Remove the cluster index from the group. If destroying 626 * the PFS and this is a master, adjust pfs_nmasters. 627 */ 628 hammer2_mtx_ex(&iroot->lock); 629 chain = iroot->cluster.array[clindex].chain; 630 iroot->cluster.array[clindex].chain = NULL; 631 632 switch(pmp->pfs_types[clindex]) { 633 case HAMMER2_PFSTYPE_MASTER: 634 if (destroying && pmp->pfs_nmasters > 0) 635 --pmp->pfs_nmasters; 636 /* XXX adjust ripdata->meta.pfs_nmasters */ 637 break; 638 default: 639 break; 640 } 641 pmp->pfs_types[clindex] = HAMMER2_PFSTYPE_NONE; 642 643 hammer2_mtx_unlock(&iroot->lock); 644 645 /* 646 * Release the chain. 647 */ 648 if (chain) { 649 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE); 650 hammer2_chain_drop(chain); 651 } 652 653 /* 654 * Terminate all XOP threads for the cluster index. 655 */ 656 if (pmp->xop_groups) { 657 for (j = 0; j < hammer2_xopgroups; ++j) { 658 hammer2_thr_delete( 659 &pmp->xop_groups[j].thrs[clindex]); 660 } 661 } 662 } 663 } 664 665 /* 666 * Destroy a PFS, typically only occurs after the last mount on a device 667 * has gone away. 668 */ 669 static void 670 hammer2_pfsfree(hammer2_pfs_t *pmp) 671 { 672 hammer2_inode_t *iroot; 673 hammer2_chain_t *chain; 674 int chains_still_present = 0; 675 int i; 676 int j; 677 678 /* 679 * Cleanup our reference on iroot. iroot is (should) not be needed 680 * by the flush code. 681 */ 682 if (pmp->flags & HAMMER2_PMPF_SPMP) 683 TAILQ_REMOVE(&hammer2_spmplist, pmp, mntentry); 684 else 685 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry); 686 687 /* 688 * Cleanup chains remaining on LRU list. 689 */ 690 hammer2_spin_ex(&pmp->lru_spin); 691 while ((chain = TAILQ_FIRST(&pmp->lru_list)) != NULL) { 692 KKASSERT(chain->flags & HAMMER2_CHAIN_ONLRU); 693 atomic_add_int(&pmp->lru_count, -1); 694 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU); 695 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node); 696 hammer2_chain_ref(chain); 697 hammer2_spin_unex(&pmp->lru_spin); 698 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE); 699 hammer2_chain_drop(chain); 700 hammer2_spin_ex(&pmp->lru_spin); 701 } 702 hammer2_spin_unex(&pmp->lru_spin); 703 704 /* 705 * Clean up iroot 706 */ 707 iroot = pmp->iroot; 708 if (iroot) { 709 for (i = 0; i < iroot->cluster.nchains; ++i) { 710 hammer2_thr_delete(&pmp->sync_thrs[i]); 711 if (pmp->xop_groups) { 712 for (j = 0; j < hammer2_xopgroups; ++j) 713 hammer2_thr_delete( 714 &pmp->xop_groups[j].thrs[i]); 715 } 716 chain = iroot->cluster.array[i].chain; 717 if (chain && !RB_EMPTY(&chain->core.rbtree)) { 718 kprintf("hammer2: Warning pmp %p still " 719 "has active chains\n", pmp); 720 chains_still_present = 1; 721 } 722 } 723 #if REPORT_REFS_ERRORS 724 if (iroot->refs != 1) 725 kprintf("PMP->IROOT %p REFS WRONG %d\n", 726 iroot, iroot->refs); 727 #else 728 KKASSERT(iroot->refs == 1); 729 #endif 730 /* ref for iroot */ 731 hammer2_inode_drop(iroot); 732 pmp->iroot = NULL; 733 } 734 735 /* 736 * Free remaining pmp resources 737 */ 738 if (chains_still_present) { 739 kprintf("hammer2: cannot free pmp %p, still in use\n", pmp); 740 } else { 741 kmalloc_destroy(&pmp->mmsg); 742 kmalloc_destroy(&pmp->minode); 743 kfree(pmp, M_HAMMER2); 744 } 745 } 746 747 /* 748 * Remove all references to hmp from the pfs list. Any PFS which becomes 749 * empty is terminated and freed. 750 * 751 * XXX inefficient. 752 */ 753 static void 754 hammer2_pfsfree_scan(hammer2_dev_t *hmp, int which) 755 { 756 hammer2_pfs_t *pmp; 757 hammer2_inode_t *iroot; 758 hammer2_chain_t *rchain; 759 int i; 760 int j; 761 struct hammer2_pfslist *wlist; 762 763 if (which == 0) 764 wlist = &hammer2_pfslist; 765 else 766 wlist = &hammer2_spmplist; 767 again: 768 TAILQ_FOREACH(pmp, wlist, mntentry) { 769 if ((iroot = pmp->iroot) == NULL) 770 continue; 771 772 /* 773 * Determine if this PFS is affected. If it is we must 774 * freeze all management threads and lock its iroot. 775 * 776 * Freezing a management thread forces it idle, operations 777 * in-progress will be aborted and it will have to start 778 * over again when unfrozen, or exit if told to exit. 779 */ 780 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 781 if (pmp->pfs_hmps[i] == hmp) 782 break; 783 } 784 if (i == HAMMER2_MAXCLUSTER) 785 continue; 786 787 hammer2_vfs_sync_pmp(pmp, MNT_WAIT); 788 789 /* 790 * Make sure all synchronization threads are locked 791 * down. 792 */ 793 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 794 if (pmp->pfs_hmps[i] == NULL) 795 continue; 796 hammer2_thr_freeze_async(&pmp->sync_thrs[i]); 797 if (pmp->xop_groups) { 798 for (j = 0; j < hammer2_xopgroups; ++j) { 799 hammer2_thr_freeze_async( 800 &pmp->xop_groups[j].thrs[i]); 801 } 802 } 803 } 804 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 805 if (pmp->pfs_hmps[i] == NULL) 806 continue; 807 hammer2_thr_freeze(&pmp->sync_thrs[i]); 808 if (pmp->xop_groups) { 809 for (j = 0; j < hammer2_xopgroups; ++j) { 810 hammer2_thr_freeze( 811 &pmp->xop_groups[j].thrs[i]); 812 } 813 } 814 } 815 816 /* 817 * Lock the inode and clean out matching chains. 818 * Note that we cannot use hammer2_inode_lock_*() 819 * here because that would attempt to validate the 820 * cluster that we are in the middle of ripping 821 * apart. 822 * 823 * WARNING! We are working directly on the inodes 824 * embedded cluster. 825 */ 826 hammer2_mtx_ex(&iroot->lock); 827 828 /* 829 * Remove the chain from matching elements of the PFS. 830 */ 831 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 832 if (pmp->pfs_hmps[i] != hmp) 833 continue; 834 hammer2_thr_delete(&pmp->sync_thrs[i]); 835 if (pmp->xop_groups) { 836 for (j = 0; j < hammer2_xopgroups; ++j) { 837 hammer2_thr_delete( 838 &pmp->xop_groups[j].thrs[i]); 839 } 840 } 841 rchain = iroot->cluster.array[i].chain; 842 iroot->cluster.array[i].chain = NULL; 843 pmp->pfs_types[i] = 0; 844 if (pmp->pfs_names[i]) { 845 kfree(pmp->pfs_names[i], M_HAMMER2); 846 pmp->pfs_names[i] = NULL; 847 } 848 if (rchain) { 849 hammer2_chain_drop(rchain); 850 /* focus hint */ 851 if (iroot->cluster.focus == rchain) 852 iroot->cluster.focus = NULL; 853 } 854 pmp->pfs_hmps[i] = NULL; 855 } 856 hammer2_mtx_unlock(&iroot->lock); 857 858 /* 859 * Cleanup trailing chains. Gaps may remain. 860 */ 861 for (i = HAMMER2_MAXCLUSTER - 1; i >= 0; --i) { 862 if (pmp->pfs_hmps[i]) 863 break; 864 } 865 iroot->cluster.nchains = i + 1; 866 867 /* 868 * If the PMP has no elements remaining we can destroy it. 869 * (this will transition management threads from frozen->exit). 870 */ 871 if (iroot->cluster.nchains == 0) { 872 /* 873 * If this was the hmp's spmp, we need to clean 874 * a little more stuff out. 875 */ 876 if (hmp->spmp == pmp) { 877 hmp->spmp = NULL; 878 hmp->vchain.pmp = NULL; 879 hmp->fchain.pmp = NULL; 880 } 881 882 /* 883 * Free the pmp and restart the loop 884 */ 885 KKASSERT(TAILQ_EMPTY(&pmp->syncq)); 886 KKASSERT(TAILQ_EMPTY(&pmp->depq)); 887 hammer2_pfsfree(pmp); 888 goto again; 889 } 890 891 /* 892 * If elements still remain we need to set the REMASTER 893 * flag and unfreeze it. 894 */ 895 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 896 if (pmp->pfs_hmps[i] == NULL) 897 continue; 898 hammer2_thr_remaster(&pmp->sync_thrs[i]); 899 hammer2_thr_unfreeze(&pmp->sync_thrs[i]); 900 if (pmp->xop_groups) { 901 for (j = 0; j < hammer2_xopgroups; ++j) { 902 hammer2_thr_remaster( 903 &pmp->xop_groups[j].thrs[i]); 904 hammer2_thr_unfreeze( 905 &pmp->xop_groups[j].thrs[i]); 906 } 907 } 908 } 909 } 910 } 911 912 /* 913 * Mount or remount HAMMER2 fileystem from physical media 914 * 915 * mountroot 916 * mp mount point structure 917 * path NULL 918 * data <unused> 919 * cred <unused> 920 * 921 * mount 922 * mp mount point structure 923 * path path to mount point 924 * data pointer to argument structure in user space 925 * volume volume path (device@LABEL form) 926 * hflags user mount flags 927 * cred user credentials 928 * 929 * RETURNS: 0 Success 930 * !0 error number 931 */ 932 static 933 int 934 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data, 935 struct ucred *cred) 936 { 937 struct hammer2_mount_info info; 938 hammer2_pfs_t *pmp; 939 hammer2_pfs_t *spmp; 940 hammer2_dev_t *hmp, *hmp_tmp; 941 hammer2_dev_t *force_local; 942 hammer2_key_t key_next; 943 hammer2_key_t key_dummy; 944 hammer2_key_t lhc; 945 hammer2_chain_t *parent; 946 hammer2_chain_t *chain; 947 const hammer2_inode_data_t *ripdata; 948 hammer2_blockref_t bref; 949 hammer2_devvp_list_t devvpl; 950 hammer2_devvp_t *e, *e_tmp; 951 struct file *fp; 952 char devstr[MNAMELEN]; 953 size_t size; 954 size_t done; 955 char *dev; 956 char *label; 957 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0); 958 int error; 959 int i; 960 961 hmp = NULL; 962 pmp = NULL; 963 dev = NULL; 964 label = NULL; 965 bzero(&info, sizeof(info)); 966 967 if (path) { 968 /* 969 * Non-root mount or updating a mount 970 */ 971 error = copyin(data, &info, sizeof(info)); 972 if (error) 973 return (error); 974 } 975 976 if (mp->mnt_flag & MNT_UPDATE) { 977 /* 978 * Update mount. Note that pmp->iroot->cluster is 979 * an inode-embedded cluster and thus cannot be 980 * directly locked. 981 * 982 * XXX HAMMER2 needs to implement NFS export via 983 * mountctl. 984 */ 985 hammer2_cluster_t *cluster; 986 987 pmp = MPTOPMP(mp); 988 pmp->hflags = info.hflags; 989 cluster = &pmp->iroot->cluster; 990 for (i = 0; i < cluster->nchains; ++i) { 991 if (cluster->array[i].chain == NULL) 992 continue; 993 hmp = cluster->array[i].chain->hmp; 994 error = hammer2_remount(hmp, mp, path, cred); 995 if (error) 996 break; 997 } 998 999 return error; 1000 } 1001 1002 if (path == NULL) { 1003 /* 1004 * Root mount 1005 */ 1006 info.cluster_fd = -1; 1007 ksnprintf(devstr, sizeof(devstr), "%s", 1008 mp->mnt_stat.f_mntfromname); 1009 done = strlen(devstr) + 1; 1010 kprintf("hammer2_mount: root devstr=\"%s\"\n", devstr); 1011 } else { 1012 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done); 1013 if (error) 1014 return (error); 1015 kprintf("hammer2_mount: devstr=\"%s\"\n", devstr); 1016 } 1017 1018 /* 1019 * Extract device and label, automatically mount @BOOT, @ROOT, or @DATA 1020 * if no label specified, based on the partition id. Error out if no 1021 * label or device (with partition id) is specified. This is strictly 1022 * a convenience to match the default label created by newfs_hammer2, 1023 * our preference is that a label always be specified. 1024 * 1025 * NOTE: We allow 'mount @LABEL <blah>'... that is, a mount command 1026 * that does not specify a device, as long as some H2 label 1027 * has already been mounted from that device. This makes 1028 * mounting snapshots a lot easier. 1029 */ 1030 dev = devstr; 1031 label = strchr(devstr, '@'); 1032 if (label && ((label + 1) - dev) > done) { 1033 kprintf("hammer2_mount: bad label %s/%zd\n", devstr, done); 1034 return (EINVAL); 1035 } 1036 if (label == NULL || label[1] == 0) { 1037 char slice; 1038 1039 if (label == NULL) 1040 label = devstr + strlen(devstr); 1041 else 1042 *label = '\0'; /* clean up trailing @ */ 1043 1044 slice = label[-1]; 1045 switch(slice) { 1046 case 'a': 1047 label = "BOOT"; 1048 break; 1049 case 'd': 1050 label = "ROOT"; 1051 break; 1052 default: 1053 label = "DATA"; 1054 break; 1055 } 1056 } else { 1057 *label = '\0'; 1058 label++; 1059 } 1060 1061 kprintf("hammer2_mount: dev=\"%s\" label=\"%s\" rdonly=%d\n", 1062 dev, label, ronly); 1063 1064 /* 1065 * Initialize all device vnodes. 1066 */ 1067 TAILQ_INIT(&devvpl); 1068 error = hammer2_init_devvp(dev, path == NULL, &devvpl); 1069 if (error) { 1070 kprintf("hammer2: failed to initialize devvp in %s\n", dev); 1071 hammer2_cleanup_devvp(&devvpl); 1072 return error; 1073 } 1074 1075 /* 1076 * Determine if the device has already been mounted. After this 1077 * check hmp will be non-NULL if we are doing the second or more 1078 * hammer2 mounts from the same device. 1079 */ 1080 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE); 1081 if (!TAILQ_EMPTY(&devvpl)) { 1082 /* 1083 * Match the device. Due to the way devfs works, 1084 * we may not be able to directly match the vnode pointer, 1085 * so also check to see if the underlying device matches. 1086 */ 1087 TAILQ_FOREACH(hmp_tmp, &hammer2_mntlist, mntentry) { 1088 TAILQ_FOREACH(e_tmp, &hmp_tmp->devvpl, entry) { 1089 int devvp_found = 0; 1090 TAILQ_FOREACH(e, &devvpl, entry) { 1091 KKASSERT(e->devvp); 1092 if (e_tmp->devvp == e->devvp) 1093 devvp_found = 1; 1094 if (e_tmp->devvp->v_rdev && 1095 e_tmp->devvp->v_rdev == e->devvp->v_rdev) 1096 devvp_found = 1; 1097 } 1098 if (!devvp_found) 1099 goto next_hmp; 1100 } 1101 hmp = hmp_tmp; 1102 kprintf("hammer2_mount: hmp=%p matched\n", hmp); 1103 break; 1104 next_hmp: 1105 continue; 1106 } 1107 1108 /* 1109 * If no match this may be a fresh H2 mount, make sure 1110 * the device is not mounted on anything else. 1111 */ 1112 if (hmp == NULL) { 1113 TAILQ_FOREACH(e, &devvpl, entry) { 1114 struct vnode *devvp = e->devvp; 1115 KKASSERT(devvp); 1116 error = vfs_mountedon(devvp); 1117 if (error) { 1118 kprintf("hammer2_mount: %s mounted %d\n", 1119 e->path, error); 1120 hammer2_cleanup_devvp(&devvpl); 1121 lockmgr(&hammer2_mntlk, LK_RELEASE); 1122 return error; 1123 } 1124 } 1125 } 1126 } else { 1127 /* 1128 * Match the label to a pmp already probed. 1129 */ 1130 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) { 1131 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { 1132 if (pmp->pfs_names[i] && 1133 strcmp(pmp->pfs_names[i], label) == 0) { 1134 hmp = pmp->pfs_hmps[i]; 1135 break; 1136 } 1137 } 1138 if (hmp) 1139 break; 1140 } 1141 if (hmp == NULL) { 1142 kprintf("hammer2_mount: PFS label \"%s\" not found\n", 1143 label); 1144 hammer2_cleanup_devvp(&devvpl); 1145 lockmgr(&hammer2_mntlk, LK_RELEASE); 1146 return ENOENT; 1147 } 1148 } 1149 1150 /* 1151 * Open the device if this isn't a secondary mount and construct 1152 * the H2 device mount (hmp). 1153 */ 1154 if (hmp == NULL) { 1155 hammer2_chain_t *schain; 1156 hammer2_xid_t xid; 1157 hammer2_xop_head_t xop; 1158 1159 /* 1160 * Now open the device 1161 */ 1162 KKASSERT(!TAILQ_EMPTY(&devvpl)); 1163 if (error == 0) { 1164 error = hammer2_open_devvp(&devvpl, ronly); 1165 if (error) { 1166 hammer2_close_devvp(&devvpl, ronly); 1167 hammer2_cleanup_devvp(&devvpl); 1168 lockmgr(&hammer2_mntlk, LK_RELEASE); 1169 return error; 1170 } 1171 } 1172 1173 /* 1174 * Construct volumes and link with device vnodes. 1175 */ 1176 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO); 1177 hmp->devvp = NULL; 1178 error = hammer2_init_volumes(mp, &devvpl, hmp->volumes, 1179 &hmp->voldata, &hmp->devvp); 1180 if (error) { 1181 hammer2_close_devvp(&devvpl, ronly); 1182 hammer2_cleanup_devvp(&devvpl); 1183 lockmgr(&hammer2_mntlk, LK_RELEASE); 1184 kfree(hmp, M_HAMMER2); 1185 return error; 1186 } 1187 if (!hmp->devvp) { 1188 kprintf("hammer2: failed to initialize root volume\n"); 1189 hammer2_unmount_helper(mp, NULL, hmp); 1190 lockmgr(&hammer2_mntlk, LK_RELEASE); 1191 hammer2_vfs_unmount(mp, MNT_FORCE); 1192 return EINVAL; 1193 } 1194 1195 ksnprintf(hmp->devrepname, sizeof(hmp->devrepname), "%s", dev); 1196 hmp->ronly = ronly; 1197 hmp->hflags = info.hflags & HMNT2_DEVFLAGS; 1198 kmalloc_create(&hmp->mchain, "HAMMER2-chains"); 1199 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry); 1200 RB_INIT(&hmp->iotree); 1201 spin_init(&hmp->io_spin, "h2mount_io"); 1202 spin_init(&hmp->list_spin, "h2mount_list"); 1203 1204 lockinit(&hmp->vollk, "h2vol", 0, 0); 1205 lockinit(&hmp->bulklk, "h2bulk", 0, 0); 1206 lockinit(&hmp->bflock, "h2bflk", 0, 0); 1207 1208 /* 1209 * vchain setup. vchain.data is embedded. 1210 * vchain.refs is initialized and will never drop to 0. 1211 * 1212 * NOTE! voldata is not yet loaded. 1213 */ 1214 hmp->vchain.hmp = hmp; 1215 hmp->vchain.refs = 1; 1216 hmp->vchain.data = (void *)&hmp->voldata; 1217 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME; 1218 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX; 1219 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid; 1220 hammer2_chain_core_init(&hmp->vchain); 1221 1222 /* 1223 * fchain setup. fchain.data is embedded. 1224 * fchain.refs is initialized and will never drop to 0. 1225 * 1226 * The data is not used but needs to be initialized to 1227 * pass assertion muster. We use this chain primarily 1228 * as a placeholder for the freemap's top-level radix tree 1229 * so it does not interfere with the volume's topology 1230 * radix tree. 1231 */ 1232 hmp->fchain.hmp = hmp; 1233 hmp->fchain.refs = 1; 1234 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset; 1235 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP; 1236 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX; 1237 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid; 1238 hmp->fchain.bref.methods = 1239 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) | 1240 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE); 1241 hammer2_chain_core_init(&hmp->fchain); 1242 1243 /* 1244 * Initialize volume header related fields. 1245 */ 1246 KKASSERT(hmp->voldata.magic == HAMMER2_VOLUME_ID_HBO || 1247 hmp->voldata.magic == HAMMER2_VOLUME_ID_ABO); 1248 hmp->volhdrno = error; 1249 hmp->volsync = hmp->voldata; 1250 hmp->free_reserved = hmp->voldata.allocator_size / 20; 1251 /* 1252 * Must use hmp instead of volume header for these two 1253 * in order to handle volume versions transparently. 1254 */ 1255 if (hmp->voldata.version >= HAMMER2_VOL_VERSION_MULTI_VOLUMES) { 1256 hmp->nvolumes = hmp->voldata.nvolumes; 1257 hmp->total_size = hmp->voldata.total_size; 1258 } else { 1259 hmp->nvolumes = 1; 1260 hmp->total_size = hmp->voldata.volu_size; 1261 } 1262 KKASSERT(hmp->nvolumes > 0); 1263 1264 /* 1265 * Move devvpl entries to hmp. 1266 */ 1267 TAILQ_INIT(&hmp->devvpl); 1268 while ((e = TAILQ_FIRST(&devvpl)) != NULL) { 1269 TAILQ_REMOVE(&devvpl, e, entry); 1270 TAILQ_INSERT_TAIL(&hmp->devvpl, e, entry); 1271 } 1272 KKASSERT(TAILQ_EMPTY(&devvpl)); 1273 KKASSERT(!TAILQ_EMPTY(&hmp->devvpl)); 1274 1275 /* 1276 * Really important to get these right or the flush and 1277 * teardown code will get confused. 1278 */ 1279 hmp->spmp = hammer2_pfsalloc(NULL, NULL, 0, NULL); 1280 spmp = hmp->spmp; 1281 spmp->pfs_hmps[0] = hmp; 1282 1283 /* 1284 * Dummy-up vchain and fchain's modify_tid. mirror_tid 1285 * is inherited from the volume header. 1286 */ 1287 xid = 0; 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 * Mount 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((void *)(intptr_t)mp->mnt_data == 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(&hmp->mchain); 1907 kfree(hmp, M_HAMMER2); 1908 } 1909 1910 int 1911 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp, 1912 ino_t ino, struct vnode **vpp) 1913 { 1914 hammer2_xop_lookup_t *xop; 1915 hammer2_pfs_t *pmp; 1916 hammer2_inode_t *ip; 1917 hammer2_tid_t inum; 1918 int error; 1919 1920 inum = (hammer2_tid_t)ino & HAMMER2_DIRHASH_USERMSK; 1921 1922 error = 0; 1923 pmp = MPTOPMP(mp); 1924 1925 /* 1926 * Easy if we already have it cached 1927 */ 1928 ip = hammer2_inode_lookup(pmp, inum); 1929 if (ip) { 1930 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED); 1931 *vpp = hammer2_igetv(ip, &error); 1932 hammer2_inode_unlock(ip); 1933 hammer2_inode_drop(ip); /* from lookup */ 1934 1935 return error; 1936 } 1937 1938 /* 1939 * Otherwise we have to find the inode 1940 */ 1941 xop = hammer2_xop_alloc(pmp->iroot, 0); 1942 xop->lhc = inum; 1943 hammer2_xop_start(&xop->head, &hammer2_lookup_desc); 1944 error = hammer2_xop_collect(&xop->head, 0); 1945 1946 if (error == 0) 1947 ip = hammer2_inode_get(pmp, &xop->head, -1, -1); 1948 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1949 1950 if (ip) { 1951 *vpp = hammer2_igetv(ip, &error); 1952 hammer2_inode_unlock(ip); 1953 } else { 1954 *vpp = NULL; 1955 error = ENOENT; 1956 } 1957 return (error); 1958 } 1959 1960 static 1961 int 1962 hammer2_vfs_root(struct mount *mp, struct vnode **vpp) 1963 { 1964 hammer2_pfs_t *pmp; 1965 struct vnode *vp; 1966 int error; 1967 1968 pmp = MPTOPMP(mp); 1969 if (pmp->iroot == NULL) { 1970 kprintf("hammer2 (%s): no root inode\n", 1971 mp->mnt_stat.f_mntfromname); 1972 *vpp = NULL; 1973 return EINVAL; 1974 } 1975 1976 error = 0; 1977 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED); 1978 1979 while (pmp->inode_tid == 0) { 1980 hammer2_xop_ipcluster_t *xop; 1981 const hammer2_inode_meta_t *meta; 1982 1983 xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING); 1984 hammer2_xop_start(&xop->head, &hammer2_ipcluster_desc); 1985 error = hammer2_xop_collect(&xop->head, 0); 1986 1987 if (error == 0) { 1988 meta = &hammer2_xop_gdata(&xop->head)->ipdata.meta; 1989 pmp->iroot->meta = *meta; 1990 pmp->inode_tid = meta->pfs_inum + 1; 1991 hammer2_xop_pdata(&xop->head); 1992 /* meta invalid */ 1993 1994 if (pmp->inode_tid < HAMMER2_INODE_START) 1995 pmp->inode_tid = HAMMER2_INODE_START; 1996 pmp->modify_tid = 1997 xop->head.cluster.focus->bref.modify_tid + 1; 1998 #if 0 1999 kprintf("PFS: Starting inode %jd\n", 2000 (intmax_t)pmp->inode_tid); 2001 kprintf("PMP focus good set nextino=%ld mod=%016jx\n", 2002 pmp->inode_tid, pmp->modify_tid); 2003 #endif 2004 wakeup(&pmp->iroot); 2005 2006 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 2007 2008 /* 2009 * Prime the mount info. 2010 */ 2011 hammer2_vfs_statfs(mp, &mp->mnt_stat, NULL); 2012 break; 2013 } 2014 2015 /* 2016 * Loop, try again 2017 */ 2018 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 2019 hammer2_inode_unlock(pmp->iroot); 2020 error = tsleep(&pmp->iroot, PCATCH, "h2root", hz); 2021 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED); 2022 if (error == EINTR) 2023 break; 2024 } 2025 2026 if (error) { 2027 hammer2_inode_unlock(pmp->iroot); 2028 *vpp = NULL; 2029 } else { 2030 vp = hammer2_igetv(pmp->iroot, &error); 2031 hammer2_inode_unlock(pmp->iroot); 2032 *vpp = vp; 2033 } 2034 2035 return (error); 2036 } 2037 2038 /* 2039 * Filesystem status 2040 * 2041 * XXX incorporate ipdata->meta.inode_quota and data_quota 2042 */ 2043 static 2044 int 2045 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 2046 { 2047 hammer2_pfs_t *pmp; 2048 hammer2_dev_t *hmp; 2049 hammer2_blockref_t bref; 2050 struct statfs tmp; 2051 int i; 2052 2053 /* 2054 * NOTE: iroot might not have validated the cluster yet. 2055 */ 2056 pmp = MPTOPMP(mp); 2057 2058 bzero(&tmp, sizeof(tmp)); 2059 2060 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { 2061 hmp = pmp->pfs_hmps[i]; 2062 if (hmp == NULL) 2063 continue; 2064 if (pmp->iroot->cluster.array[i].chain) 2065 bref = pmp->iroot->cluster.array[i].chain->bref; 2066 else 2067 bzero(&bref, sizeof(bref)); 2068 2069 tmp.f_files = bref.embed.stats.inode_count; 2070 tmp.f_ffree = 0; 2071 tmp.f_blocks = hmp->voldata.allocator_size / 2072 mp->mnt_vstat.f_bsize; 2073 tmp.f_bfree = hmp->voldata.allocator_free / 2074 mp->mnt_vstat.f_bsize; 2075 tmp.f_bavail = tmp.f_bfree; 2076 2077 if (cred && cred->cr_uid != 0) { 2078 uint64_t adj; 2079 2080 /* 5% */ 2081 adj = hmp->free_reserved / mp->mnt_vstat.f_bsize; 2082 tmp.f_blocks -= adj; 2083 tmp.f_bfree -= adj; 2084 tmp.f_bavail -= adj; 2085 } 2086 2087 mp->mnt_stat.f_blocks = tmp.f_blocks; 2088 mp->mnt_stat.f_bfree = tmp.f_bfree; 2089 mp->mnt_stat.f_bavail = tmp.f_bavail; 2090 mp->mnt_stat.f_files = tmp.f_files; 2091 mp->mnt_stat.f_ffree = tmp.f_ffree; 2092 2093 *sbp = mp->mnt_stat; 2094 } 2095 return (0); 2096 } 2097 2098 static 2099 int 2100 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred) 2101 { 2102 hammer2_pfs_t *pmp; 2103 hammer2_dev_t *hmp; 2104 hammer2_blockref_t bref; 2105 struct statvfs tmp; 2106 int i; 2107 2108 /* 2109 * NOTE: iroot might not have validated the cluster yet. 2110 */ 2111 pmp = MPTOPMP(mp); 2112 bzero(&tmp, sizeof(tmp)); 2113 2114 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { 2115 hmp = pmp->pfs_hmps[i]; 2116 if (hmp == NULL) 2117 continue; 2118 if (pmp->iroot->cluster.array[i].chain) 2119 bref = pmp->iroot->cluster.array[i].chain->bref; 2120 else 2121 bzero(&bref, sizeof(bref)); 2122 2123 tmp.f_files = bref.embed.stats.inode_count; 2124 tmp.f_ffree = 0; 2125 tmp.f_blocks = hmp->voldata.allocator_size / 2126 mp->mnt_vstat.f_bsize; 2127 tmp.f_bfree = hmp->voldata.allocator_free / 2128 mp->mnt_vstat.f_bsize; 2129 tmp.f_bavail = tmp.f_bfree; 2130 2131 if (cred && cred->cr_uid != 0) { 2132 uint64_t adj; 2133 2134 /* 5% */ 2135 adj = hmp->free_reserved / mp->mnt_vstat.f_bsize; 2136 tmp.f_blocks -= adj; 2137 tmp.f_bfree -= adj; 2138 tmp.f_bavail -= adj; 2139 } 2140 2141 mp->mnt_vstat.f_blocks = tmp.f_blocks; 2142 mp->mnt_vstat.f_bfree = tmp.f_bfree; 2143 mp->mnt_vstat.f_bavail = tmp.f_bavail; 2144 mp->mnt_vstat.f_files = tmp.f_files; 2145 mp->mnt_vstat.f_ffree = tmp.f_ffree; 2146 2147 *sbp = mp->mnt_vstat; 2148 } 2149 return (0); 2150 } 2151 2152 /* 2153 * Mount-time recovery (RW mounts) 2154 * 2155 * Updates to the free block table are allowed to lag flushes by one 2156 * transaction. In case of a crash, then on a fresh mount we must do an 2157 * incremental scan of the last committed transaction id and make sure that 2158 * all related blocks have been marked allocated. 2159 */ 2160 struct hammer2_recovery_elm { 2161 TAILQ_ENTRY(hammer2_recovery_elm) entry; 2162 hammer2_chain_t *chain; 2163 hammer2_tid_t sync_tid; 2164 }; 2165 2166 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm); 2167 2168 struct hammer2_recovery_info { 2169 struct hammer2_recovery_list list; 2170 hammer2_tid_t mtid; 2171 int depth; 2172 }; 2173 2174 static int hammer2_recovery_scan(hammer2_dev_t *hmp, 2175 hammer2_chain_t *parent, 2176 struct hammer2_recovery_info *info, 2177 hammer2_tid_t sync_tid); 2178 2179 #define HAMMER2_RECOVERY_MAXDEPTH 10 2180 2181 static 2182 int 2183 hammer2_recovery(hammer2_dev_t *hmp) 2184 { 2185 struct hammer2_recovery_info info; 2186 struct hammer2_recovery_elm *elm; 2187 hammer2_chain_t *parent; 2188 hammer2_tid_t sync_tid; 2189 hammer2_tid_t mirror_tid; 2190 int error; 2191 2192 hammer2_trans_init(hmp->spmp, 0); 2193 2194 sync_tid = hmp->voldata.freemap_tid; 2195 mirror_tid = hmp->voldata.mirror_tid; 2196 2197 kprintf("hammer2_mount: \"%s\": ", hmp->devrepname); 2198 if (sync_tid >= mirror_tid) { 2199 kprintf("no recovery needed\n"); 2200 } else { 2201 kprintf("freemap recovery %016jx-%016jx\n", 2202 sync_tid + 1, mirror_tid); 2203 } 2204 2205 TAILQ_INIT(&info.list); 2206 info.depth = 0; 2207 parent = hammer2_chain_lookup_init(&hmp->vchain, 0); 2208 error = hammer2_recovery_scan(hmp, parent, &info, sync_tid); 2209 hammer2_chain_lookup_done(parent); 2210 2211 while ((elm = TAILQ_FIRST(&info.list)) != NULL) { 2212 TAILQ_REMOVE(&info.list, elm, entry); 2213 parent = elm->chain; 2214 sync_tid = elm->sync_tid; 2215 kfree(elm, M_HAMMER2); 2216 2217 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 2218 error |= hammer2_recovery_scan(hmp, parent, &info, 2219 hmp->voldata.freemap_tid); 2220 hammer2_chain_unlock(parent); 2221 hammer2_chain_drop(parent); /* drop elm->chain ref */ 2222 } 2223 2224 hammer2_trans_done(hmp->spmp, 0); 2225 2226 return error; 2227 } 2228 2229 static 2230 int 2231 hammer2_recovery_scan(hammer2_dev_t *hmp, hammer2_chain_t *parent, 2232 struct hammer2_recovery_info *info, 2233 hammer2_tid_t sync_tid) 2234 { 2235 const hammer2_inode_data_t *ripdata; 2236 hammer2_chain_t *chain; 2237 hammer2_blockref_t bref; 2238 int tmp_error; 2239 int rup_error; 2240 int error; 2241 int first; 2242 2243 /* 2244 * Adjust freemap to ensure that the block(s) are marked allocated. 2245 */ 2246 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) { 2247 hammer2_freemap_adjust(hmp, &parent->bref, 2248 HAMMER2_FREEMAP_DORECOVER); 2249 } 2250 2251 /* 2252 * Check type for recursive scan 2253 */ 2254 switch(parent->bref.type) { 2255 case HAMMER2_BREF_TYPE_VOLUME: 2256 /* data already instantiated */ 2257 break; 2258 case HAMMER2_BREF_TYPE_INODE: 2259 /* 2260 * Must instantiate data for DIRECTDATA test and also 2261 * for recursion. 2262 */ 2263 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 2264 ripdata = &parent->data->ipdata; 2265 if (ripdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) { 2266 /* not applicable to recovery scan */ 2267 hammer2_chain_unlock(parent); 2268 return 0; 2269 } 2270 hammer2_chain_unlock(parent); 2271 break; 2272 case HAMMER2_BREF_TYPE_INDIRECT: 2273 /* 2274 * Must instantiate data for recursion 2275 */ 2276 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 2277 hammer2_chain_unlock(parent); 2278 break; 2279 case HAMMER2_BREF_TYPE_DIRENT: 2280 case HAMMER2_BREF_TYPE_DATA: 2281 case HAMMER2_BREF_TYPE_FREEMAP: 2282 case HAMMER2_BREF_TYPE_FREEMAP_NODE: 2283 case HAMMER2_BREF_TYPE_FREEMAP_LEAF: 2284 /* not applicable to recovery scan */ 2285 return 0; 2286 break; 2287 default: 2288 return HAMMER2_ERROR_BADBREF; 2289 } 2290 2291 /* 2292 * Defer operation if depth limit reached. 2293 */ 2294 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH) { 2295 struct hammer2_recovery_elm *elm; 2296 2297 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK); 2298 elm->chain = parent; 2299 elm->sync_tid = sync_tid; 2300 hammer2_chain_ref(parent); 2301 TAILQ_INSERT_TAIL(&info->list, elm, entry); 2302 /* unlocked by caller */ 2303 2304 return(0); 2305 } 2306 2307 2308 /* 2309 * Recursive scan of the last flushed transaction only. We are 2310 * doing this without pmp assignments so don't leave the chains 2311 * hanging around after we are done with them. 2312 * 2313 * error Cumulative error this level only 2314 * rup_error Cumulative error for recursion 2315 * tmp_error Specific non-cumulative recursion error 2316 */ 2317 chain = NULL; 2318 first = 1; 2319 rup_error = 0; 2320 error = 0; 2321 2322 for (;;) { 2323 error |= hammer2_chain_scan(parent, &chain, &bref, 2324 &first, 2325 HAMMER2_LOOKUP_NODATA); 2326 2327 /* 2328 * Problem during scan or EOF 2329 */ 2330 if (error) 2331 break; 2332 2333 /* 2334 * If this is a leaf 2335 */ 2336 if (chain == NULL) { 2337 if (bref.mirror_tid > sync_tid) { 2338 hammer2_freemap_adjust(hmp, &bref, 2339 HAMMER2_FREEMAP_DORECOVER); 2340 } 2341 continue; 2342 } 2343 2344 /* 2345 * This may or may not be a recursive node. 2346 */ 2347 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE); 2348 if (bref.mirror_tid > sync_tid) { 2349 ++info->depth; 2350 tmp_error = hammer2_recovery_scan(hmp, chain, 2351 info, sync_tid); 2352 --info->depth; 2353 } else { 2354 tmp_error = 0; 2355 } 2356 2357 /* 2358 * Flush the recovery at the PFS boundary to stage it for 2359 * the final flush of the super-root topology. 2360 */ 2361 if (tmp_error == 0 && 2362 (bref.flags & HAMMER2_BREF_FLAG_PFSROOT) && 2363 (chain->flags & HAMMER2_CHAIN_ONFLUSH)) { 2364 hammer2_flush(chain, HAMMER2_FLUSH_TOP | 2365 HAMMER2_FLUSH_ALL); 2366 } 2367 rup_error |= tmp_error; 2368 } 2369 return ((error | rup_error) & ~HAMMER2_ERROR_EOF); 2370 } 2371 2372 /* 2373 * This fixes up an error introduced in earlier H2 implementations where 2374 * moving a PFS inode into an indirect block wound up causing the 2375 * HAMMER2_BREF_FLAG_PFSROOT flag in the bref to get cleared. 2376 */ 2377 static 2378 int 2379 hammer2_fixup_pfses(hammer2_dev_t *hmp) 2380 { 2381 const hammer2_inode_data_t *ripdata; 2382 hammer2_chain_t *parent; 2383 hammer2_chain_t *chain; 2384 hammer2_key_t key_next; 2385 hammer2_pfs_t *spmp; 2386 int error; 2387 2388 error = 0; 2389 2390 /* 2391 * Lookup mount point under the media-localized super-root. 2392 * 2393 * cluster->pmp will incorrectly point to spmp and must be fixed 2394 * up later on. 2395 */ 2396 spmp = hmp->spmp; 2397 hammer2_inode_lock(spmp->iroot, 0); 2398 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS); 2399 chain = hammer2_chain_lookup(&parent, &key_next, 2400 HAMMER2_KEY_MIN, HAMMER2_KEY_MAX, 2401 &error, 0); 2402 while (chain) { 2403 if (chain->bref.type != HAMMER2_BREF_TYPE_INODE) 2404 continue; 2405 if (chain->error) { 2406 kprintf("I/O error scanning PFS labels\n"); 2407 error |= chain->error; 2408 } else if ((chain->bref.flags & 2409 HAMMER2_BREF_FLAG_PFSROOT) == 0) { 2410 int error2; 2411 2412 ripdata = &chain->data->ipdata; 2413 hammer2_trans_init(hmp->spmp, 0); 2414 error2 = hammer2_chain_modify(chain, 2415 chain->bref.modify_tid, 2416 0, 0); 2417 if (error2 == 0) { 2418 kprintf("hammer2: Correct mis-flagged PFS %s\n", 2419 ripdata->filename); 2420 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT; 2421 } else { 2422 error |= error2; 2423 } 2424 hammer2_flush(chain, HAMMER2_FLUSH_TOP | 2425 HAMMER2_FLUSH_ALL); 2426 hammer2_trans_done(hmp->spmp, 0); 2427 } 2428 chain = hammer2_chain_next(&parent, chain, &key_next, 2429 key_next, HAMMER2_KEY_MAX, 2430 &error, 0); 2431 } 2432 if (parent) { 2433 hammer2_chain_unlock(parent); 2434 hammer2_chain_drop(parent); 2435 } 2436 hammer2_inode_unlock(spmp->iroot); 2437 2438 return error; 2439 } 2440 2441 /* 2442 * Sync a mount point; this is called periodically on a per-mount basis from 2443 * the filesystem syncer, and whenever a user issues a sync. 2444 */ 2445 int 2446 hammer2_vfs_sync(struct mount *mp, int waitfor) 2447 { 2448 int error; 2449 2450 error = hammer2_vfs_sync_pmp(MPTOPMP(mp), waitfor); 2451 2452 return error; 2453 } 2454 2455 /* 2456 * Because frontend operations lock vnodes before we get a chance to 2457 * lock the related inode, we can't just acquire a vnode lock without 2458 * risking a deadlock. The frontend may be holding a vnode lock while 2459 * also blocked on our SYNCQ flag while trying to get the inode lock. 2460 * 2461 * To deal with this situation we can check the vnode lock situation 2462 * after locking the inode and perform a work-around. 2463 */ 2464 int 2465 hammer2_vfs_sync_pmp(hammer2_pfs_t *pmp, int waitfor) 2466 { 2467 struct mount *mp; 2468 /*hammer2_xop_flush_t *xop;*/ 2469 /*struct hammer2_sync_info info;*/ 2470 hammer2_inode_t *ip; 2471 hammer2_depend_t *depend; 2472 hammer2_depend_t *depend_next; 2473 struct vnode *vp; 2474 uint32_t pass2; 2475 int error; 2476 int wakecount; 2477 int dorestart; 2478 2479 mp = pmp->mp; 2480 2481 /* 2482 * Move all inodes on sideq to syncq. This will clear sideq. 2483 * This should represent all flushable inodes. These inodes 2484 * will already have refs due to being on syncq or sideq. We 2485 * must do this all at once with the spinlock held to ensure that 2486 * all inode dependencies are part of the same flush. 2487 * 2488 * We should be able to do this asynchronously from frontend 2489 * operations because we will be locking the inodes later on 2490 * to actually flush them, and that will partition any frontend 2491 * op using the same inode. Either it has already locked the 2492 * inode and we will block, or it has not yet locked the inode 2493 * and it will block until we are finished flushing that inode. 2494 * 2495 * When restarting, only move the inodes flagged as PASS2 from 2496 * SIDEQ to SYNCQ. PASS2 propagation by inode_lock4() and 2497 * inode_depend() are atomic with the spin-lock. 2498 */ 2499 hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH); 2500 #ifdef HAMMER2_DEBUG_SYNC 2501 kprintf("FILESYSTEM SYNC BOUNDARY\n"); 2502 #endif 2503 dorestart = 0; 2504 2505 /* 2506 * Move inodes from depq to syncq, releasing the related 2507 * depend structures. 2508 */ 2509 restart: 2510 #ifdef HAMMER2_DEBUG_SYNC 2511 kprintf("FILESYSTEM SYNC RESTART (%d)\n", dorestart); 2512 #endif 2513 hammer2_trans_setflags(pmp, 0/*HAMMER2_TRANS_COPYQ*/); 2514 hammer2_trans_clearflags(pmp, HAMMER2_TRANS_RESCAN); 2515 2516 /* 2517 * Move inodes from depq to syncq. When restarting, only depq's 2518 * marked pass2 are moved. 2519 */ 2520 hammer2_spin_ex(&pmp->list_spin); 2521 depend_next = TAILQ_FIRST(&pmp->depq); 2522 wakecount = 0; 2523 2524 while ((depend = depend_next) != NULL) { 2525 depend_next = TAILQ_NEXT(depend, entry); 2526 if (dorestart && depend->pass2 == 0) 2527 continue; 2528 TAILQ_FOREACH(ip, &depend->sideq, entry) { 2529 KKASSERT(ip->flags & HAMMER2_INODE_SIDEQ); 2530 atomic_set_int(&ip->flags, HAMMER2_INODE_SYNCQ); 2531 atomic_clear_int(&ip->flags, HAMMER2_INODE_SIDEQ); 2532 ip->depend = NULL; 2533 } 2534 2535 /* 2536 * NOTE: pmp->sideq_count includes both sideq and syncq 2537 */ 2538 TAILQ_CONCAT(&pmp->syncq, &depend->sideq, entry); 2539 2540 depend->count = 0; 2541 depend->pass2 = 0; 2542 TAILQ_REMOVE(&pmp->depq, depend, entry); 2543 } 2544 2545 hammer2_spin_unex(&pmp->list_spin); 2546 hammer2_trans_clearflags(pmp, /*HAMMER2_TRANS_COPYQ |*/ 2547 HAMMER2_TRANS_WAITING); 2548 dorestart = 0; 2549 2550 /* 2551 * sideq_count may have dropped enough to allow us to unstall 2552 * the frontend. 2553 */ 2554 hammer2_pfs_memory_wakeup(pmp, 0); 2555 2556 /* 2557 * Now run through all inodes on syncq. 2558 * 2559 * Flush transactions only interlock with other flush transactions. 2560 * Any conflicting frontend operations will block on the inode, but 2561 * may hold a vnode lock while doing so. 2562 */ 2563 hammer2_spin_ex(&pmp->list_spin); 2564 while ((ip = TAILQ_FIRST(&pmp->syncq)) != NULL) { 2565 /* 2566 * Remove the inode from the SYNCQ, transfer the syncq ref 2567 * to us. We must clear SYNCQ to allow any potential 2568 * front-end deadlock to proceed. We must set PASS2 so 2569 * the dependency code knows what to do. 2570 */ 2571 pass2 = ip->flags; 2572 cpu_ccfence(); 2573 if (atomic_cmpset_int(&ip->flags, 2574 pass2, 2575 (pass2 & ~(HAMMER2_INODE_SYNCQ | 2576 HAMMER2_INODE_SYNCQ_WAKEUP)) | 2577 HAMMER2_INODE_SYNCQ_PASS2) == 0) { 2578 continue; 2579 } 2580 TAILQ_REMOVE(&pmp->syncq, ip, entry); 2581 --pmp->sideq_count; 2582 hammer2_spin_unex(&pmp->list_spin); 2583 2584 /* 2585 * Tickle anyone waiting on ip->flags or the hysteresis 2586 * on the dirty inode count. 2587 */ 2588 if (pass2 & HAMMER2_INODE_SYNCQ_WAKEUP) 2589 wakeup(&ip->flags); 2590 if (++wakecount >= hammer2_limit_dirty_inodes / 20 + 1) { 2591 wakecount = 0; 2592 hammer2_pfs_memory_wakeup(pmp, 0); 2593 } 2594 2595 /* 2596 * Relock the inode, and we inherit a ref from the above. 2597 * We will check for a race after we acquire the vnode. 2598 */ 2599 hammer2_mtx_ex(&ip->lock); 2600 2601 /* 2602 * We need the vp in order to vfsync() dirty buffers, so if 2603 * one isn't attached we can skip it. 2604 * 2605 * Ordering the inode lock and then the vnode lock has the 2606 * potential to deadlock. If we had left SYNCQ set that could 2607 * also deadlock us against the frontend even if we don't hold 2608 * any locks, but the latter is not a problem now since we 2609 * cleared it. igetv will temporarily release the inode lock 2610 * in a safe manner to work-around the deadlock. 2611 * 2612 * Unfortunately it is still possible to deadlock when the 2613 * frontend obtains multiple inode locks, because all the 2614 * related vnodes are already locked (nor can the vnode locks 2615 * be released and reacquired without messing up RECLAIM and 2616 * INACTIVE sequencing). 2617 * 2618 * The solution for now is to move the vp back onto SIDEQ 2619 * and set dorestart, which will restart the flush after we 2620 * exhaust the current SYNCQ. Note that additional 2621 * dependencies may build up, so we definitely need to move 2622 * the whole SIDEQ back to SYNCQ when we restart. 2623 */ 2624 vp = ip->vp; 2625 if (vp) { 2626 if (vget(vp, LK_EXCLUSIVE|LK_NOWAIT)) { 2627 /* 2628 * Failed to get the vnode, requeue the inode 2629 * (PASS2 is already set so it will be found 2630 * again on the restart). 2631 * 2632 * Then unlock, possibly sleep, and retry 2633 * later. We sleep if PASS2 was *previously* 2634 * set, before we set it again above. 2635 */ 2636 vp = NULL; 2637 dorestart = 1; 2638 #ifdef HAMMER2_DEBUG_SYNC 2639 kprintf("inum %ld (sync delayed by vnode)\n", 2640 (long)ip->meta.inum); 2641 #endif 2642 hammer2_inode_delayed_sideq(ip); 2643 2644 hammer2_mtx_unlock(&ip->lock); 2645 hammer2_inode_drop(ip); 2646 2647 if (pass2 & HAMMER2_INODE_SYNCQ_PASS2) { 2648 tsleep(&dorestart, 0, "h2syndel", 2); 2649 } 2650 hammer2_spin_ex(&pmp->list_spin); 2651 continue; 2652 } 2653 } else { 2654 vp = NULL; 2655 } 2656 2657 /* 2658 * If the inode wound up on a SIDEQ again it will already be 2659 * prepped for another PASS2. In this situation if we flush 2660 * it now we will just wind up flushing it again in the same 2661 * syncer run, so we might as well not flush it now. 2662 */ 2663 if (ip->flags & HAMMER2_INODE_SIDEQ) { 2664 hammer2_mtx_unlock(&ip->lock); 2665 hammer2_inode_drop(ip); 2666 if (vp) 2667 vput(vp); 2668 dorestart = 1; 2669 hammer2_spin_ex(&pmp->list_spin); 2670 continue; 2671 } 2672 2673 /* 2674 * Ok we have the inode exclusively locked and if vp is 2675 * not NULL that will also be exclusively locked. Do the 2676 * meat of the flush. 2677 * 2678 * vp token needed for v_rbdirty_tree check / vclrisdirty 2679 * sequencing. Though we hold the vnode exclusively so 2680 * we shouldn't need to hold the token also in this case. 2681 */ 2682 if (vp) { 2683 vfsync(vp, MNT_WAIT, 1, NULL, NULL); 2684 bio_track_wait(&vp->v_track_write, 0, 0); /* XXX */ 2685 } 2686 2687 /* 2688 * If the inode has not yet been inserted into the tree 2689 * we must do so. Then sync and flush it. The flush should 2690 * update the parent. 2691 */ 2692 if (ip->flags & HAMMER2_INODE_DELETING) { 2693 #ifdef HAMMER2_DEBUG_SYNC 2694 kprintf("inum %ld destroy\n", (long)ip->meta.inum); 2695 #endif 2696 hammer2_inode_chain_des(ip); 2697 atomic_add_long(&hammer2_iod_inode_deletes, 1); 2698 } else if (ip->flags & HAMMER2_INODE_CREATING) { 2699 #ifdef HAMMER2_DEBUG_SYNC 2700 kprintf("inum %ld insert\n", (long)ip->meta.inum); 2701 #endif 2702 hammer2_inode_chain_ins(ip); 2703 atomic_add_long(&hammer2_iod_inode_creates, 1); 2704 } 2705 #ifdef HAMMER2_DEBUG_SYNC 2706 kprintf("inum %ld chain-sync\n", (long)ip->meta.inum); 2707 #endif 2708 2709 /* 2710 * Because I kinda messed up the design and index the inodes 2711 * under the root inode, along side the directory entries, 2712 * we can't flush the inode index under the iroot until the 2713 * end. If we do it now we might miss effects created by 2714 * other inodes on the SYNCQ. 2715 * 2716 * Do a normal (non-FSSYNC) flush instead, which allows the 2717 * vnode code to work the same. We don't want to force iroot 2718 * back onto the SIDEQ, and we also don't want the flush code 2719 * to update pfs_iroot_blocksets until the final flush later. 2720 * 2721 * XXX at the moment this will likely result in a double-flush 2722 * of the iroot chain. 2723 */ 2724 hammer2_inode_chain_sync(ip); 2725 if (ip == pmp->iroot) { 2726 hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP); 2727 } else { 2728 hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP | 2729 HAMMER2_XOP_FSSYNC); 2730 } 2731 if (vp) { 2732 lwkt_gettoken(&vp->v_token); 2733 if ((ip->flags & (HAMMER2_INODE_MODIFIED | 2734 HAMMER2_INODE_RESIZED | 2735 HAMMER2_INODE_DIRTYDATA)) == 0 && 2736 RB_EMPTY(&vp->v_rbdirty_tree) && 2737 !bio_track_active(&vp->v_track_write)) { 2738 vclrisdirty(vp); 2739 } else { 2740 hammer2_inode_delayed_sideq(ip); 2741 } 2742 lwkt_reltoken(&vp->v_token); 2743 vput(vp); 2744 vp = NULL; /* safety */ 2745 } 2746 atomic_clear_int(&ip->flags, HAMMER2_INODE_SYNCQ_PASS2); 2747 hammer2_inode_unlock(ip); /* unlock+drop */ 2748 /* ip pointer invalid */ 2749 2750 /* 2751 * If the inode got dirted after we dropped our locks, 2752 * it will have already been moved back to the SIDEQ. 2753 */ 2754 hammer2_spin_ex(&pmp->list_spin); 2755 } 2756 hammer2_spin_unex(&pmp->list_spin); 2757 hammer2_pfs_memory_wakeup(pmp, 0); 2758 2759 if (dorestart || (pmp->trans.flags & HAMMER2_TRANS_RESCAN)) { 2760 #ifdef HAMMER2_DEBUG_SYNC 2761 kprintf("FILESYSTEM SYNC STAGE 1 RESTART\n"); 2762 /*tsleep(&dorestart, 0, "h2STG1-R", hz*20);*/ 2763 #endif 2764 dorestart = 1; 2765 goto restart; 2766 } 2767 #ifdef HAMMER2_DEBUG_SYNC 2768 kprintf("FILESYSTEM SYNC STAGE 2 BEGIN\n"); 2769 /*tsleep(&dorestart, 0, "h2STG2", hz*20);*/ 2770 #endif 2771 2772 /* 2773 * We have to flush the PFS root last, even if it does not appear to 2774 * be dirty, because all the inodes in the PFS are indexed under it. 2775 * The normal flushing of iroot above would only occur if directory 2776 * entries under the root were changed. 2777 * 2778 * Specifying VOLHDR will cause an additionl flush of hmp->spmp 2779 * for the media making up the cluster. 2780 */ 2781 if ((ip = pmp->iroot) != NULL) { 2782 hammer2_inode_ref(ip); 2783 hammer2_mtx_ex(&ip->lock); 2784 hammer2_inode_chain_sync(ip); 2785 hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP | 2786 HAMMER2_XOP_FSSYNC | 2787 HAMMER2_XOP_VOLHDR); 2788 hammer2_inode_unlock(ip); /* unlock+drop */ 2789 } 2790 #ifdef HAMMER2_DEBUG_SYNC 2791 kprintf("FILESYSTEM SYNC STAGE 2 DONE\n"); 2792 #endif 2793 2794 /* 2795 * device bioq sync 2796 */ 2797 hammer2_bioq_sync(pmp); 2798 2799 #if 0 2800 /* 2801 * Generally speaking we now want to flush the media topology from 2802 * the iroot through to the inodes. The flush stops at any inode 2803 * boundary, which allows the frontend to continue running concurrent 2804 * modifying operations on inodes (including kernel flushes of 2805 * buffers) without interfering with the main sync. 2806 * 2807 * Use the XOP interface to concurrently flush all nodes to 2808 * synchronize the PFSROOT subtopology to the media. A standard 2809 * end-of-scan ENOENT error indicates cluster sufficiency. 2810 * 2811 * Note that this flush will not be visible on crash recovery until 2812 * we flush the super-root topology in the next loop. 2813 * 2814 * XXX For now wait for all flushes to complete. 2815 */ 2816 if (mp && (ip = pmp->iroot) != NULL) { 2817 /* 2818 * If unmounting try to flush everything including any 2819 * sub-trees under inodes, just in case there is dangling 2820 * modified data, as a safety. Otherwise just flush up to 2821 * the inodes in this stage. 2822 */ 2823 kprintf("MP & IROOT\n"); 2824 #ifdef HAMMER2_DEBUG_SYNC 2825 kprintf("FILESYSTEM SYNC STAGE 3 IROOT BEGIN\n"); 2826 #endif 2827 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 2828 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING | 2829 HAMMER2_XOP_VOLHDR | 2830 HAMMER2_XOP_FSSYNC | 2831 HAMMER2_XOP_INODE_STOP); 2832 } else { 2833 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING | 2834 HAMMER2_XOP_INODE_STOP | 2835 HAMMER2_XOP_VOLHDR | 2836 HAMMER2_XOP_FSSYNC | 2837 HAMMER2_XOP_INODE_STOP); 2838 } 2839 hammer2_xop_start(&xop->head, &hammer2_inode_flush_desc); 2840 error = hammer2_xop_collect(&xop->head, 2841 HAMMER2_XOP_COLLECT_WAITALL); 2842 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 2843 #ifdef HAMMER2_DEBUG_SYNC 2844 kprintf("FILESYSTEM SYNC STAGE 3 IROOT END\n"); 2845 #endif 2846 if (error == HAMMER2_ERROR_ENOENT) 2847 error = 0; 2848 else 2849 error = hammer2_error_to_errno(error); 2850 } else { 2851 error = 0; 2852 } 2853 #endif 2854 error = 0; /* XXX */ 2855 hammer2_trans_done(pmp, HAMMER2_TRANS_ISFLUSH); 2856 2857 return (error); 2858 } 2859 2860 static 2861 int 2862 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp) 2863 { 2864 hammer2_inode_t *ip; 2865 2866 KKASSERT(MAXFIDSZ >= 16); 2867 ip = VTOI(vp); 2868 fhp->fid_len = offsetof(struct fid, fid_data[16]); 2869 fhp->fid_ext = 0; 2870 ((hammer2_tid_t *)fhp->fid_data)[0] = ip->meta.inum; 2871 ((hammer2_tid_t *)fhp->fid_data)[1] = 0; 2872 2873 return 0; 2874 } 2875 2876 static 2877 int 2878 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 2879 struct fid *fhp, struct vnode **vpp) 2880 { 2881 hammer2_pfs_t *pmp; 2882 hammer2_tid_t inum; 2883 int error; 2884 2885 pmp = MPTOPMP(mp); 2886 inum = ((hammer2_tid_t *)fhp->fid_data)[0] & HAMMER2_DIRHASH_USERMSK; 2887 if (vpp) { 2888 if (inum == 1) 2889 error = hammer2_vfs_root(mp, vpp); 2890 else 2891 error = hammer2_vfs_vget(mp, NULL, inum, vpp); 2892 } else { 2893 error = 0; 2894 } 2895 if (error) 2896 kprintf("fhtovp: %016jx -> %p, %d\n", inum, *vpp, error); 2897 return error; 2898 } 2899 2900 static 2901 int 2902 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 2903 int *exflagsp, struct ucred **credanonp) 2904 { 2905 hammer2_pfs_t *pmp; 2906 struct netcred *np; 2907 int error; 2908 2909 pmp = MPTOPMP(mp); 2910 np = vfs_export_lookup(mp, &pmp->export, nam); 2911 if (np) { 2912 *exflagsp = np->netc_exflags; 2913 *credanonp = &np->netc_anon; 2914 error = 0; 2915 } else { 2916 error = EACCES; 2917 } 2918 return error; 2919 } 2920 2921 /* 2922 * This handles hysteresis on regular file flushes. Because the BIOs are 2923 * routed to a thread it is possible for an excessive number to build up 2924 * and cause long front-end stalls long before the runningbuffspace limit 2925 * is hit, so we implement hammer2_flush_pipe to control the 2926 * hysteresis. 2927 * 2928 * This is a particular problem when compression is used. 2929 */ 2930 void 2931 hammer2_lwinprog_ref(hammer2_pfs_t *pmp) 2932 { 2933 atomic_add_int(&pmp->count_lwinprog, 1); 2934 } 2935 2936 void 2937 hammer2_lwinprog_drop(hammer2_pfs_t *pmp) 2938 { 2939 int lwinprog; 2940 2941 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1); 2942 if ((lwinprog & HAMMER2_LWINPROG_WAITING) && 2943 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) { 2944 atomic_clear_int(&pmp->count_lwinprog, 2945 HAMMER2_LWINPROG_WAITING); 2946 wakeup(&pmp->count_lwinprog); 2947 } 2948 if ((lwinprog & HAMMER2_LWINPROG_WAITING0) && 2949 (lwinprog & HAMMER2_LWINPROG_MASK) <= 0) { 2950 atomic_clear_int(&pmp->count_lwinprog, 2951 HAMMER2_LWINPROG_WAITING0); 2952 wakeup(&pmp->count_lwinprog); 2953 } 2954 } 2955 2956 void 2957 hammer2_lwinprog_wait(hammer2_pfs_t *pmp, int flush_pipe) 2958 { 2959 int lwinprog; 2960 int lwflag = (flush_pipe) ? HAMMER2_LWINPROG_WAITING : 2961 HAMMER2_LWINPROG_WAITING0; 2962 2963 for (;;) { 2964 lwinprog = pmp->count_lwinprog; 2965 cpu_ccfence(); 2966 if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe) 2967 break; 2968 tsleep_interlock(&pmp->count_lwinprog, 0); 2969 atomic_set_int(&pmp->count_lwinprog, lwflag); 2970 lwinprog = pmp->count_lwinprog; 2971 if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe) 2972 break; 2973 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz); 2974 } 2975 } 2976 2977 /* 2978 * It is possible for an excessive number of dirty chains or dirty inodes 2979 * to build up. When this occurs we start an asynchronous filesystem sync. 2980 * If the level continues to build up, we stall, waiting for it to drop, 2981 * with some hysteresis. 2982 * 2983 * This relies on the kernel calling hammer2_vfs_modifying() prior to 2984 * obtaining any vnode locks before making a modifying VOP call. 2985 */ 2986 static int 2987 hammer2_vfs_modifying(struct mount *mp) 2988 { 2989 if (mp->mnt_flag & MNT_RDONLY) 2990 return EROFS; 2991 hammer2_pfs_memory_wait(MPTOPMP(mp)); 2992 2993 return 0; 2994 } 2995 2996 /* 2997 * Initiate an asynchronous filesystem sync and, with hysteresis, 2998 * stall if the internal data structure count becomes too bloated. 2999 */ 3000 void 3001 hammer2_pfs_memory_wait(hammer2_pfs_t *pmp) 3002 { 3003 uint32_t waiting; 3004 int pcatch; 3005 int error; 3006 3007 if (pmp == NULL || pmp->mp == NULL) 3008 return; 3009 3010 for (;;) { 3011 waiting = pmp->inmem_dirty_chains & HAMMER2_DIRTYCHAIN_MASK; 3012 cpu_ccfence(); 3013 3014 /* 3015 * Start the syncer running at 1/2 the limit 3016 */ 3017 if (waiting > hammer2_limit_dirty_chains / 2 || 3018 pmp->sideq_count > hammer2_limit_dirty_inodes / 2) { 3019 trigger_syncer(pmp->mp); 3020 } 3021 3022 /* 3023 * Stall at the limit waiting for the counts to drop. 3024 * This code will typically be woken up once the count 3025 * drops below 3/4 the limit, or in one second. 3026 */ 3027 if (waiting < hammer2_limit_dirty_chains && 3028 pmp->sideq_count < hammer2_limit_dirty_inodes) { 3029 break; 3030 } 3031 3032 pcatch = curthread->td_proc ? PCATCH : 0; 3033 3034 tsleep_interlock(&pmp->inmem_dirty_chains, pcatch); 3035 atomic_set_int(&pmp->inmem_dirty_chains, 3036 HAMMER2_DIRTYCHAIN_WAITING); 3037 if (waiting < hammer2_limit_dirty_chains && 3038 pmp->sideq_count < hammer2_limit_dirty_inodes) { 3039 break; 3040 } 3041 trigger_syncer(pmp->mp); 3042 error = tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED | pcatch, 3043 "h2memw", hz); 3044 if (error == ERESTART) 3045 break; 3046 } 3047 } 3048 3049 /* 3050 * Wake up any stalled frontend ops waiting, with hysteresis, using 3051 * 2/3 of the limit. 3052 */ 3053 void 3054 hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp, int count) 3055 { 3056 uint32_t waiting; 3057 3058 if (pmp) { 3059 waiting = atomic_fetchadd_int(&pmp->inmem_dirty_chains, count); 3060 /* don't need --waiting to test flag */ 3061 3062 if ((waiting & HAMMER2_DIRTYCHAIN_WAITING) && 3063 (pmp->inmem_dirty_chains & HAMMER2_DIRTYCHAIN_MASK) <= 3064 hammer2_limit_dirty_chains * 2 / 3 && 3065 pmp->sideq_count <= hammer2_limit_dirty_inodes * 2 / 3) { 3066 atomic_clear_int(&pmp->inmem_dirty_chains, 3067 HAMMER2_DIRTYCHAIN_WAITING); 3068 wakeup(&pmp->inmem_dirty_chains); 3069 } 3070 } 3071 } 3072 3073 void 3074 hammer2_pfs_memory_inc(hammer2_pfs_t *pmp) 3075 { 3076 if (pmp) { 3077 atomic_add_int(&pmp->inmem_dirty_chains, 1); 3078 } 3079 } 3080 3081 /* 3082 * Volume header data locks 3083 */ 3084 void 3085 hammer2_voldata_lock(hammer2_dev_t *hmp) 3086 { 3087 lockmgr(&hmp->vollk, LK_EXCLUSIVE); 3088 } 3089 3090 void 3091 hammer2_voldata_unlock(hammer2_dev_t *hmp) 3092 { 3093 lockmgr(&hmp->vollk, LK_RELEASE); 3094 } 3095 3096 void 3097 hammer2_voldata_modify(hammer2_dev_t *hmp) 3098 { 3099 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) { 3100 atomic_add_long(&hammer2_count_modified_chains, 1); 3101 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED); 3102 hammer2_pfs_memory_inc(hmp->vchain.pmp); 3103 } 3104 } 3105 3106 /* 3107 * Returns 0 if the filesystem has tons of free space 3108 * Returns 1 if the filesystem has less than 10% remaining 3109 * Returns 2 if the filesystem has less than 2%/5% (user/root) remaining. 3110 */ 3111 int 3112 hammer2_vfs_enospace(hammer2_inode_t *ip, off_t bytes, struct ucred *cred) 3113 { 3114 hammer2_pfs_t *pmp; 3115 hammer2_dev_t *hmp; 3116 hammer2_off_t free_reserved; 3117 hammer2_off_t free_nominal; 3118 int i; 3119 3120 pmp = ip->pmp; 3121 3122 if (pmp->free_ticks == 0 || pmp->free_ticks != ticks) { 3123 free_reserved = HAMMER2_SEGSIZE; 3124 free_nominal = 0x7FFFFFFFFFFFFFFFLLU; 3125 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { 3126 hmp = pmp->pfs_hmps[i]; 3127 if (hmp == NULL) 3128 continue; 3129 if (pmp->pfs_types[i] != HAMMER2_PFSTYPE_MASTER && 3130 pmp->pfs_types[i] != HAMMER2_PFSTYPE_SOFT_MASTER) 3131 continue; 3132 3133 if (free_nominal > hmp->voldata.allocator_free) 3134 free_nominal = hmp->voldata.allocator_free; 3135 if (free_reserved < hmp->free_reserved) 3136 free_reserved = hmp->free_reserved; 3137 } 3138 3139 /* 3140 * SMP races ok 3141 */ 3142 pmp->free_reserved = free_reserved; 3143 pmp->free_nominal = free_nominal; 3144 pmp->free_ticks = ticks; 3145 } else { 3146 free_reserved = pmp->free_reserved; 3147 free_nominal = pmp->free_nominal; 3148 } 3149 if (cred && cred->cr_uid != 0) { 3150 if ((int64_t)(free_nominal - bytes) < 3151 (int64_t)free_reserved) { 3152 return 2; 3153 } 3154 } else { 3155 if ((int64_t)(free_nominal - bytes) < 3156 (int64_t)free_reserved / 2) { 3157 return 2; 3158 } 3159 } 3160 if ((int64_t)(free_nominal - bytes) < (int64_t)free_reserved * 2) 3161 return 1; 3162 return 0; 3163 } 3164 3165 /* 3166 * Debugging 3167 */ 3168 void 3169 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int bi, int *countp, 3170 char pfx, u_int flags) 3171 { 3172 hammer2_chain_t *scan; 3173 hammer2_chain_t *parent; 3174 3175 --*countp; 3176 if (*countp == 0) { 3177 kprintf("%*.*s...\n", tab, tab, ""); 3178 return; 3179 } 3180 if (*countp < 0) 3181 return; 3182 kprintf("%*.*s%c-chain %p %s.%-3d %016jx %016jx/%-2d mir=%016jx\n", 3183 tab, tab, "", pfx, chain, 3184 hammer2_bref_type_str(chain->bref.type), bi, 3185 chain->bref.data_off, chain->bref.key, chain->bref.keybits, 3186 chain->bref.mirror_tid); 3187 3188 kprintf("%*.*s [%08x] (%s) refs=%d", 3189 tab, tab, "", 3190 chain->flags, 3191 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE && 3192 chain->data) ? (char *)chain->data->ipdata.filename : "?"), 3193 chain->refs); 3194 3195 parent = chain->parent; 3196 if (parent) 3197 kprintf("\n%*.*s p=%p [pflags %08x prefs %d]", 3198 tab, tab, "", 3199 parent, parent->flags, parent->refs); 3200 if (RB_EMPTY(&chain->core.rbtree)) { 3201 kprintf("\n"); 3202 } else { 3203 int bi = 0; 3204 kprintf(" {\n"); 3205 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree) { 3206 if ((scan->flags & flags) || flags == (u_int)-1) { 3207 hammer2_dump_chain(scan, tab + 4, bi, countp, 3208 'a', flags); 3209 } 3210 bi++; 3211 } 3212 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data) 3213 kprintf("%*.*s}(%s)\n", tab, tab, "", 3214 chain->data->ipdata.filename); 3215 else 3216 kprintf("%*.*s}\n", tab, tab, ""); 3217 } 3218 } 3219