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