1 /* 2 * Copyright (c) 2011-2014 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression) 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/nlookup.h> 39 #include <sys/vnode.h> 40 #include <sys/mount.h> 41 #include <sys/fcntl.h> 42 #include <sys/buf.h> 43 #include <sys/uuid.h> 44 #include <sys/vfsops.h> 45 #include <sys/sysctl.h> 46 #include <sys/socket.h> 47 #include <sys/objcache.h> 48 49 #include <sys/proc.h> 50 #include <sys/namei.h> 51 #include <sys/mountctl.h> 52 #include <sys/dirent.h> 53 #include <sys/uio.h> 54 55 #include <sys/mutex.h> 56 #include <sys/mutex2.h> 57 58 #include "hammer2.h" 59 #include "hammer2_disk.h" 60 #include "hammer2_mount.h" 61 62 #include "hammer2.h" 63 #include "hammer2_lz4.h" 64 65 #include "zlib/hammer2_zlib.h" 66 67 #define REPORT_REFS_ERRORS 1 /* XXX remove me */ 68 69 MALLOC_DEFINE(M_OBJCACHE, "objcache", "Object Cache"); 70 71 struct hammer2_sync_info { 72 hammer2_trans_t trans; 73 int error; 74 int waitfor; 75 }; 76 77 TAILQ_HEAD(hammer2_mntlist, hammer2_mount); 78 TAILQ_HEAD(hammer2_pfslist, hammer2_pfsmount); 79 static struct hammer2_mntlist hammer2_mntlist; 80 static struct hammer2_pfslist hammer2_pfslist; 81 static struct lock hammer2_mntlk; 82 83 int hammer2_debug; 84 int hammer2_cluster_enable = 1; 85 int hammer2_hardlink_enable = 1; 86 int hammer2_flush_pipe = 100; 87 int hammer2_synchronous_flush = 1; 88 int hammer2_dio_count; 89 long hammer2_limit_dirty_chains; 90 long hammer2_iod_file_read; 91 long hammer2_iod_meta_read; 92 long hammer2_iod_indr_read; 93 long hammer2_iod_fmap_read; 94 long hammer2_iod_volu_read; 95 long hammer2_iod_file_write; 96 long hammer2_iod_meta_write; 97 long hammer2_iod_indr_write; 98 long hammer2_iod_fmap_write; 99 long hammer2_iod_volu_write; 100 long hammer2_ioa_file_read; 101 long hammer2_ioa_meta_read; 102 long hammer2_ioa_indr_read; 103 long hammer2_ioa_fmap_read; 104 long hammer2_ioa_volu_read; 105 long hammer2_ioa_fmap_write; 106 long hammer2_ioa_file_write; 107 long hammer2_ioa_meta_write; 108 long hammer2_ioa_indr_write; 109 long hammer2_ioa_volu_write; 110 111 MALLOC_DECLARE(C_BUFFER); 112 MALLOC_DEFINE(C_BUFFER, "compbuffer", "Buffer used for compression."); 113 114 MALLOC_DECLARE(D_BUFFER); 115 MALLOC_DEFINE(D_BUFFER, "decompbuffer", "Buffer used for decompression."); 116 117 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem"); 118 119 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW, 120 &hammer2_debug, 0, ""); 121 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW, 122 &hammer2_cluster_enable, 0, ""); 123 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW, 124 &hammer2_hardlink_enable, 0, ""); 125 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW, 126 &hammer2_flush_pipe, 0, ""); 127 SYSCTL_INT(_vfs_hammer2, OID_AUTO, synchronous_flush, CTLFLAG_RW, 128 &hammer2_synchronous_flush, 0, ""); 129 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW, 130 &hammer2_limit_dirty_chains, 0, ""); 131 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD, 132 &hammer2_dio_count, 0, ""); 133 134 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW, 135 &hammer2_iod_file_read, 0, ""); 136 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW, 137 &hammer2_iod_meta_read, 0, ""); 138 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW, 139 &hammer2_iod_indr_read, 0, ""); 140 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW, 141 &hammer2_iod_fmap_read, 0, ""); 142 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW, 143 &hammer2_iod_volu_read, 0, ""); 144 145 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW, 146 &hammer2_iod_file_write, 0, ""); 147 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW, 148 &hammer2_iod_meta_write, 0, ""); 149 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW, 150 &hammer2_iod_indr_write, 0, ""); 151 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW, 152 &hammer2_iod_fmap_write, 0, ""); 153 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW, 154 &hammer2_iod_volu_write, 0, ""); 155 156 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW, 157 &hammer2_ioa_file_read, 0, ""); 158 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW, 159 &hammer2_ioa_meta_read, 0, ""); 160 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW, 161 &hammer2_ioa_indr_read, 0, ""); 162 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW, 163 &hammer2_ioa_fmap_read, 0, ""); 164 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW, 165 &hammer2_ioa_volu_read, 0, ""); 166 167 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW, 168 &hammer2_ioa_file_write, 0, ""); 169 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW, 170 &hammer2_ioa_meta_write, 0, ""); 171 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW, 172 &hammer2_ioa_indr_write, 0, ""); 173 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW, 174 &hammer2_ioa_fmap_write, 0, ""); 175 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW, 176 &hammer2_ioa_volu_write, 0, ""); 177 178 static int hammer2_vfs_init(struct vfsconf *conf); 179 static int hammer2_vfs_uninit(struct vfsconf *vfsp); 180 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data, 181 struct ucred *cred); 182 static int hammer2_remount(hammer2_mount_t *, struct mount *, char *, 183 struct vnode *, struct ucred *); 184 static int hammer2_recovery(hammer2_mount_t *hmp); 185 static int hammer2_vfs_unmount(struct mount *mp, int mntflags); 186 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp); 187 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, 188 struct ucred *cred); 189 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, 190 struct ucred *cred); 191 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp, 192 ino_t ino, struct vnode **vpp); 193 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 194 struct fid *fhp, struct vnode **vpp); 195 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp); 196 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 197 int *exflagsp, struct ucred **credanonp); 198 199 static int hammer2_install_volume_header(hammer2_mount_t *hmp); 200 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data); 201 202 static void hammer2_write_thread(void *arg); 203 204 static void hammer2_vfs_unmount_hmp1(struct mount *mp, hammer2_mount_t *hmp); 205 static void hammer2_vfs_unmount_hmp2(struct mount *mp, hammer2_mount_t *hmp); 206 207 /* 208 * Functions for compression in threads, 209 * from hammer2_vnops.c 210 */ 211 static void hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans, 212 hammer2_inode_t *ip, 213 const hammer2_inode_data_t *ripdata, 214 hammer2_cluster_t *cparent, 215 hammer2_key_t lbase, int ioflag, int pblksize, 216 int *errorp); 217 static void hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans, 218 hammer2_inode_t *ip, 219 const hammer2_inode_data_t *ripdata, 220 hammer2_cluster_t *cparent, 221 hammer2_key_t lbase, int ioflag, 222 int pblksize, int *errorp, 223 int comp_algo, int check_algo); 224 static void hammer2_zero_check_and_write(struct buf *bp, 225 hammer2_trans_t *trans, hammer2_inode_t *ip, 226 const hammer2_inode_data_t *ripdata, 227 hammer2_cluster_t *cparent, 228 hammer2_key_t lbase, 229 int ioflag, int pblksize, int *errorp, 230 int check_algo); 231 static int test_block_zeros(const char *buf, size_t bytes); 232 static void zero_write(struct buf *bp, hammer2_trans_t *trans, 233 hammer2_inode_t *ip, 234 const hammer2_inode_data_t *ripdata, 235 hammer2_cluster_t *cparent, 236 hammer2_key_t lbase, 237 int *errorp); 238 static void hammer2_write_bp(hammer2_cluster_t *cluster, struct buf *bp, 239 int ioflag, int pblksize, int *errorp, 240 int check_algo); 241 242 static int hammer2_rcvdmsg(kdmsg_msg_t *msg); 243 static void hammer2_autodmsg(kdmsg_msg_t *msg); 244 static int hammer2_lnk_span_reply(kdmsg_state_t *state, kdmsg_msg_t *msg); 245 246 247 /* 248 * HAMMER2 vfs operations. 249 */ 250 static struct vfsops hammer2_vfsops = { 251 .vfs_init = hammer2_vfs_init, 252 .vfs_uninit = hammer2_vfs_uninit, 253 .vfs_sync = hammer2_vfs_sync, 254 .vfs_mount = hammer2_vfs_mount, 255 .vfs_unmount = hammer2_vfs_unmount, 256 .vfs_root = hammer2_vfs_root, 257 .vfs_statfs = hammer2_vfs_statfs, 258 .vfs_statvfs = hammer2_vfs_statvfs, 259 .vfs_vget = hammer2_vfs_vget, 260 .vfs_vptofh = hammer2_vfs_vptofh, 261 .vfs_fhtovp = hammer2_vfs_fhtovp, 262 .vfs_checkexp = hammer2_vfs_checkexp 263 }; 264 265 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", ""); 266 267 VFS_SET(hammer2_vfsops, hammer2, 0); 268 MODULE_VERSION(hammer2, 1); 269 270 static 271 int 272 hammer2_vfs_init(struct vfsconf *conf) 273 { 274 static struct objcache_malloc_args margs_read; 275 static struct objcache_malloc_args margs_write; 276 277 int error; 278 279 error = 0; 280 281 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref)) 282 error = EINVAL; 283 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data)) 284 error = EINVAL; 285 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data)) 286 error = EINVAL; 287 288 if (error) 289 kprintf("HAMMER2 structure size mismatch; cannot continue.\n"); 290 291 margs_read.objsize = 65536; 292 margs_read.mtype = D_BUFFER; 293 294 margs_write.objsize = 32768; 295 margs_write.mtype = C_BUFFER; 296 297 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc, 298 0, 1, NULL, NULL, NULL, objcache_malloc_alloc, 299 objcache_malloc_free, &margs_read); 300 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc, 301 0, 1, NULL, NULL, NULL, objcache_malloc_alloc, 302 objcache_malloc_free, &margs_write); 303 304 lockinit(&hammer2_mntlk, "mntlk", 0, 0); 305 TAILQ_INIT(&hammer2_mntlist); 306 TAILQ_INIT(&hammer2_pfslist); 307 308 hammer2_limit_dirty_chains = desiredvnodes / 10; 309 310 hammer2_trans_manage_init(); 311 312 return (error); 313 } 314 315 static 316 int 317 hammer2_vfs_uninit(struct vfsconf *vfsp __unused) 318 { 319 objcache_destroy(cache_buffer_read); 320 objcache_destroy(cache_buffer_write); 321 return 0; 322 } 323 324 /* 325 * Core PFS allocator. Used to allocate the pmp structure for PFS cluster 326 * mounts and the spmp structure for media (hmp) structures. 327 */ 328 static hammer2_pfsmount_t * 329 hammer2_pfsalloc(const hammer2_inode_data_t *ripdata, hammer2_tid_t alloc_tid) 330 { 331 hammer2_pfsmount_t *pmp; 332 333 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO); 334 kmalloc_create(&pmp->minode, "HAMMER2-inodes"); 335 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg"); 336 lockinit(&pmp->lock, "pfslk", 0, 0); 337 spin_init(&pmp->inum_spin, "hm2pfsalloc_inum"); 338 RB_INIT(&pmp->inum_tree); 339 TAILQ_INIT(&pmp->unlinkq); 340 spin_init(&pmp->list_spin, "hm2pfsalloc_list"); 341 342 pmp->alloc_tid = alloc_tid + 1; /* our first media transaction id */ 343 pmp->flush_tid = pmp->alloc_tid; 344 if (ripdata) { 345 pmp->inode_tid = ripdata->pfs_inum + 1; 346 pmp->pfs_clid = ripdata->pfs_clid; 347 } 348 mtx_init(&pmp->wthread_mtx); 349 bioq_init(&pmp->wthread_bioq); 350 351 return pmp; 352 } 353 354 /* 355 * Mount or remount HAMMER2 fileystem from physical media 356 * 357 * mountroot 358 * mp mount point structure 359 * path NULL 360 * data <unused> 361 * cred <unused> 362 * 363 * mount 364 * mp mount point structure 365 * path path to mount point 366 * data pointer to argument structure in user space 367 * volume volume path (device@LABEL form) 368 * hflags user mount flags 369 * cred user credentials 370 * 371 * RETURNS: 0 Success 372 * !0 error number 373 */ 374 static 375 int 376 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data, 377 struct ucred *cred) 378 { 379 struct hammer2_mount_info info; 380 hammer2_pfsmount_t *pmp; 381 hammer2_pfsmount_t *spmp; 382 hammer2_mount_t *hmp; 383 hammer2_key_t key_next; 384 hammer2_key_t key_dummy; 385 hammer2_key_t lhc; 386 struct vnode *devvp; 387 struct nlookupdata nd; 388 hammer2_chain_t *parent; 389 hammer2_chain_t *rchain; 390 hammer2_cluster_t *cluster; 391 hammer2_cluster_t *cparent; 392 const hammer2_inode_data_t *ripdata; 393 hammer2_blockref_t bref; 394 struct file *fp; 395 char devstr[MNAMELEN]; 396 size_t size; 397 size_t done; 398 char *dev; 399 char *label; 400 int ronly = 1; 401 int error; 402 int cache_index; 403 int ddflag; 404 int i; 405 406 hmp = NULL; 407 pmp = NULL; 408 dev = NULL; 409 label = NULL; 410 devvp = NULL; 411 cache_index = -1; 412 413 kprintf("hammer2_mount\n"); 414 415 if (path == NULL) { 416 /* 417 * Root mount 418 */ 419 bzero(&info, sizeof(info)); 420 info.cluster_fd = -1; 421 return (EOPNOTSUPP); 422 } else { 423 /* 424 * Non-root mount or updating a mount 425 */ 426 error = copyin(data, &info, sizeof(info)); 427 if (error) 428 return (error); 429 430 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done); 431 if (error) 432 return (error); 433 434 /* Extract device and label */ 435 dev = devstr; 436 label = strchr(devstr, '@'); 437 if (label == NULL || 438 ((label + 1) - dev) > done) { 439 return (EINVAL); 440 } 441 *label = '\0'; 442 label++; 443 if (*label == '\0') 444 return (EINVAL); 445 446 if (mp->mnt_flag & MNT_UPDATE) { 447 /* Update mount */ 448 /* HAMMER2 implements NFS export via mountctl */ 449 pmp = MPTOPMP(mp); 450 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { 451 hmp = pmp->iroot->cluster.array[i]->hmp; 452 devvp = hmp->devvp; 453 error = hammer2_remount(hmp, mp, path, 454 devvp, cred); 455 if (error) 456 break; 457 } 458 /*hammer2_inode_install_hidden(pmp);*/ 459 460 return error; 461 } 462 } 463 464 /* 465 * HMP device mount 466 * 467 * Lookup name and verify it refers to a block device. 468 */ 469 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW); 470 if (error == 0) 471 error = nlookup(&nd); 472 if (error == 0) 473 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp); 474 nlookup_done(&nd); 475 476 if (error == 0) { 477 if (vn_isdisk(devvp, &error)) 478 error = vfs_mountedon(devvp); 479 } 480 481 /* 482 * Determine if the device has already been mounted. After this 483 * check hmp will be non-NULL if we are doing the second or more 484 * hammer2 mounts from the same device. 485 */ 486 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE); 487 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) { 488 if (hmp->devvp == devvp) 489 break; 490 } 491 492 /* 493 * Open the device if this isn't a secondary mount and construct 494 * the H2 device mount (hmp). 495 */ 496 if (hmp == NULL) { 497 hammer2_chain_t *schain; 498 hammer2_xid_t xid; 499 500 if (error == 0 && vcount(devvp) > 0) 501 error = EBUSY; 502 503 /* 504 * Now open the device 505 */ 506 if (error == 0) { 507 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0); 508 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 509 error = vinvalbuf(devvp, V_SAVE, 0, 0); 510 if (error == 0) { 511 error = VOP_OPEN(devvp, 512 ronly ? FREAD : FREAD | FWRITE, 513 FSCRED, NULL); 514 } 515 vn_unlock(devvp); 516 } 517 if (error && devvp) { 518 vrele(devvp); 519 devvp = NULL; 520 } 521 if (error) { 522 lockmgr(&hammer2_mntlk, LK_RELEASE); 523 return error; 524 } 525 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO); 526 hmp->ronly = ronly; 527 hmp->devvp = devvp; 528 kmalloc_create(&hmp->mchain, "HAMMER2-chains"); 529 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry); 530 RB_INIT(&hmp->iotree); 531 spin_init(&hmp->io_spin, "hm2mount_io"); 532 spin_init(&hmp->list_spin, "hm2mount_list"); 533 TAILQ_INIT(&hmp->flushq); 534 535 lockinit(&hmp->vollk, "h2vol", 0, 0); 536 537 /* 538 * vchain setup. vchain.data is embedded. 539 * vchain.refs is initialized and will never drop to 0. 540 * 541 * NOTE! voldata is not yet loaded. 542 */ 543 hmp->vchain.hmp = hmp; 544 hmp->vchain.refs = 1; 545 hmp->vchain.data = (void *)&hmp->voldata; 546 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME; 547 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX; 548 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid; 549 550 hammer2_chain_core_alloc(NULL, &hmp->vchain); 551 /* hmp->vchain.u.xxx is left NULL */ 552 553 /* 554 * fchain setup. fchain.data is embedded. 555 * fchain.refs is initialized and will never drop to 0. 556 * 557 * The data is not used but needs to be initialized to 558 * pass assertion muster. We use this chain primarily 559 * as a placeholder for the freemap's top-level RBTREE 560 * so it does not interfere with the volume's topology 561 * RBTREE. 562 */ 563 hmp->fchain.hmp = hmp; 564 hmp->fchain.refs = 1; 565 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset; 566 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP; 567 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX; 568 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid; 569 hmp->fchain.bref.methods = 570 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) | 571 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE); 572 573 hammer2_chain_core_alloc(NULL, &hmp->fchain); 574 /* hmp->fchain.u.xxx is left NULL */ 575 576 /* 577 * Install the volume header and initialize fields from 578 * voldata. 579 */ 580 error = hammer2_install_volume_header(hmp); 581 if (error) { 582 ++hmp->pmp_count; 583 hammer2_vfs_unmount_hmp1(mp, hmp); 584 hammer2_vfs_unmount_hmp2(mp, hmp); 585 lockmgr(&hammer2_mntlk, LK_RELEASE); 586 hammer2_vfs_unmount(mp, MNT_FORCE); 587 return error; 588 } 589 590 /* 591 * Really important to get these right or flush will get 592 * confused. 593 */ 594 hmp->spmp = hammer2_pfsalloc(NULL, hmp->voldata.mirror_tid); 595 kprintf("alloc spmp %p tid %016jx\n", 596 hmp->spmp, hmp->voldata.mirror_tid); 597 spmp = hmp->spmp; 598 spmp->inode_tid = 1; 599 600 xid = 0; 601 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid; 602 hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid; 603 hmp->vchain.pmp = spmp; 604 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid; 605 hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid; 606 hmp->fchain.pmp = spmp; 607 608 /* 609 * First locate the super-root inode, which is key 0 610 * relative to the volume header's blockset. 611 * 612 * Then locate the root inode by scanning the directory keyspace 613 * represented by the label. 614 */ 615 parent = hammer2_chain_lookup_init(&hmp->vchain, 0); 616 schain = hammer2_chain_lookup(&parent, &key_dummy, 617 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 618 &cache_index, 0, &ddflag); 619 hammer2_chain_lookup_done(parent); 620 if (schain == NULL) { 621 kprintf("hammer2_mount: invalid super-root\n"); 622 ++hmp->pmp_count; 623 hammer2_vfs_unmount_hmp1(mp, hmp); 624 hammer2_vfs_unmount_hmp2(mp, hmp); 625 lockmgr(&hammer2_mntlk, LK_RELEASE); 626 hammer2_vfs_unmount(mp, MNT_FORCE); 627 return EINVAL; 628 } 629 630 /* 631 * Sanity-check schain's pmp, finish initializing spmp. 632 */ 633 ripdata = &hammer2_chain_rdata(schain)->ipdata; 634 KKASSERT(schain->pmp == spmp); 635 spmp->pfs_clid = ripdata->pfs_clid; 636 637 /* 638 * NOTE: inode_get sucks up schain's lock. 639 */ 640 cluster = hammer2_cluster_from_chain(schain); 641 spmp->iroot = hammer2_inode_get(spmp, NULL, cluster); 642 spmp->spmp_hmp = hmp; 643 hammer2_inode_ref(spmp->iroot); 644 hammer2_inode_unlock_ex(spmp->iroot, cluster); 645 schain = NULL; 646 /* leave spmp->iroot with one ref */ 647 648 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 649 error = hammer2_recovery(hmp); 650 /* XXX do something with error */ 651 } 652 ++hmp->pmp_count; 653 654 /* 655 * XXX RDONLY stuff is totally broken FIXME XXX 656 * 657 * Automatic LNK_CONN 658 * Automatic handling of received LNK_SPAN 659 * Automatic handling of received LNK_CIRC 660 * No automatic LNK_SPAN generation - we do this ourselves 661 * No automatic LNK_CIRC generation - we do this ourselves 662 */ 663 kdmsg_iocom_init(&hmp->iocom, hmp, 664 KDMSG_IOCOMF_AUTOCONN | 665 KDMSG_IOCOMF_AUTORXSPAN, 666 hmp->mchain, hammer2_rcvdmsg); 667 668 /* 669 * Ref the cluster management messaging descriptor. The mount 670 * program deals with the other end of the communications pipe. 671 */ 672 fp = holdfp(curproc->p_fd, info.cluster_fd, -1); 673 if (fp) { 674 hammer2_cluster_reconnect(hmp, fp); 675 } else { 676 kprintf("hammer2_mount: bad cluster_fd!\n"); 677 } 678 } else { 679 spmp = hmp->spmp; 680 ++hmp->pmp_count; 681 } 682 683 /* 684 * Lookup mount point under the media-localized super-root. 685 * 686 * cluster->pmp will incorrectly point to spmp and must be fixed 687 * up later on. 688 */ 689 cparent = hammer2_inode_lock_ex(spmp->iroot); 690 lhc = hammer2_dirhash(label, strlen(label)); 691 cluster = hammer2_cluster_lookup(cparent, &key_next, 692 lhc, lhc + HAMMER2_DIRHASH_LOMASK, 693 0, &ddflag); 694 while (cluster) { 695 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE && 696 strcmp(label, 697 hammer2_cluster_rdata(cluster)->ipdata.filename) == 0) { 698 break; 699 } 700 cluster = hammer2_cluster_next(cparent, cluster, &key_next, 701 key_next, 702 lhc + HAMMER2_DIRHASH_LOMASK, 0); 703 } 704 hammer2_inode_unlock_ex(spmp->iroot, cparent); 705 706 if (cluster == NULL) { 707 kprintf("hammer2_mount: PFS label not found\n"); 708 hammer2_vfs_unmount_hmp1(mp, hmp); 709 hammer2_vfs_unmount_hmp2(mp, hmp); 710 lockmgr(&hammer2_mntlk, LK_RELEASE); 711 hammer2_vfs_unmount(mp, MNT_FORCE); 712 return EINVAL; 713 } 714 715 for (i = 0; i < cluster->nchains; ++i) { 716 rchain = cluster->array[i]; 717 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) { 718 kprintf("hammer2_mount: PFS label already mounted!\n"); 719 hammer2_cluster_unlock(cluster); 720 hammer2_vfs_unmount_hmp1(mp, hmp); 721 hammer2_vfs_unmount_hmp2(mp, hmp); 722 lockmgr(&hammer2_mntlk, LK_RELEASE); 723 hammer2_vfs_unmount(mp, MNT_FORCE); 724 return EBUSY; 725 } 726 KKASSERT(rchain->pmp == NULL); 727 #if 0 728 if (rchain->flags & HAMMER2_CHAIN_RECYCLE) { 729 kprintf("hammer2_mount: PFS label is recycling\n"); 730 hammer2_cluster_unlock(cluster); 731 hammer2_vfs_unmount_hmp1(mp, hmp); 732 hammer2_vfs_unmount_hmp2(mp, hmp); 733 lockmgr(&hammer2_mntlk, LK_RELEASE); 734 hammer2_vfs_unmount(mp, MNT_FORCE); 735 return EBUSY; 736 } 737 #endif 738 } 739 740 /* 741 * Check to see if the cluster id is already mounted at the mount 742 * point. If it is, add us to the cluster. 743 */ 744 ripdata = &hammer2_cluster_rdata(cluster)->ipdata; 745 hammer2_cluster_bref(cluster, &bref); 746 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) { 747 if (pmp->spmp_hmp == NULL && 748 bcmp(&pmp->pfs_clid, &ripdata->pfs_clid, 749 sizeof(pmp->pfs_clid)) == 0) { 750 break; 751 } 752 } 753 754 if (pmp) { 755 int i; 756 int j; 757 758 hammer2_inode_ref(pmp->iroot); 759 ccms_thread_lock(&pmp->iroot->topo_cst, CCMS_STATE_EXCLUSIVE); 760 761 if (pmp->iroot->cluster.nchains + cluster->nchains > 762 HAMMER2_MAXCLUSTER) { 763 kprintf("hammer2_mount: cluster full!\n"); 764 765 ccms_thread_unlock(&pmp->iroot->topo_cst); 766 hammer2_inode_drop(pmp->iroot); 767 768 hammer2_cluster_unlock(cluster); 769 hammer2_vfs_unmount_hmp1(mp, hmp); 770 hammer2_vfs_unmount_hmp2(mp, hmp); 771 lockmgr(&hammer2_mntlk, LK_RELEASE); 772 hammer2_vfs_unmount(mp, MNT_FORCE); 773 return EBUSY; 774 } 775 kprintf("hammer2_vfs_mount: Adding pfs to existing cluster\n"); 776 j = pmp->iroot->cluster.nchains; 777 for (i = 0; i < cluster->nchains; ++i) { 778 rchain = cluster->array[i]; 779 KKASSERT(rchain->pmp == NULL); 780 rchain->pmp = pmp; 781 hammer2_chain_ref(cluster->array[i]); 782 pmp->iroot->cluster.array[j] = cluster->array[i]; 783 ++j; 784 } 785 pmp->iroot->cluster.nchains = j; 786 ccms_thread_unlock(&pmp->iroot->topo_cst); 787 hammer2_inode_drop(pmp->iroot); 788 hammer2_cluster_unlock(cluster); 789 lockmgr(&hammer2_mntlk, LK_RELEASE); 790 791 kprintf("ok\n"); 792 hammer2_inode_install_hidden(pmp); 793 794 return ERANGE; 795 } 796 797 /* 798 * Block device opened successfully, finish initializing the 799 * mount structure. 800 * 801 * From this point on we have to call hammer2_unmount() on failure. 802 */ 803 pmp = hammer2_pfsalloc(ripdata, bref.mirror_tid); 804 kprintf("PMP mirror_tid is %016jx\n", bref.mirror_tid); 805 for (i = 0; i < cluster->nchains; ++i) { 806 rchain = cluster->array[i]; 807 KKASSERT(rchain->pmp == NULL); 808 rchain->pmp = pmp; 809 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED); 810 } 811 cluster->pmp = pmp; 812 813 ccms_domain_init(&pmp->ccms_dom); 814 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry); 815 lockmgr(&hammer2_mntlk, LK_RELEASE); 816 817 kprintf("hammer2_mount hmp=%p pmp=%p pmpcnt=%d\n", 818 hmp, pmp, hmp->pmp_count); 819 820 mp->mnt_flag = MNT_LOCAL; 821 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */ 822 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */ 823 824 /* 825 * required mount structure initializations 826 */ 827 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE; 828 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE; 829 830 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE; 831 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE; 832 833 /* 834 * Optional fields 835 */ 836 mp->mnt_iosize_max = MAXPHYS; 837 mp->mnt_data = (qaddr_t)pmp; 838 pmp->mp = mp; 839 840 /* 841 * After this point hammer2_vfs_unmount() has visibility on hmp 842 * and manual hmp1/hmp2 calls are not needed on fatal errors. 843 */ 844 pmp->iroot = hammer2_inode_get(pmp, NULL, cluster); 845 hammer2_inode_ref(pmp->iroot); /* ref for pmp->iroot */ 846 hammer2_inode_unlock_ex(pmp->iroot, cluster); 847 848 /* 849 * The logical file buffer bio write thread handles things 850 * like physical block assignment and compression. 851 * 852 * (only applicable to pfs mounts, not applicable to spmp) 853 */ 854 pmp->wthread_destroy = 0; 855 lwkt_create(hammer2_write_thread, pmp, 856 &pmp->wthread_td, NULL, 0, -1, "hwrite-%s", label); 857 858 /* 859 * With the cluster operational install ihidden. 860 * (only applicable to pfs mounts, not applicable to spmp) 861 */ 862 hammer2_inode_install_hidden(pmp); 863 864 /* 865 * Finish setup 866 */ 867 vfs_getnewfsid(mp); 868 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops); 869 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops); 870 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops); 871 872 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size); 873 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 874 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname)); 875 copyinstr(path, mp->mnt_stat.f_mntonname, 876 sizeof(mp->mnt_stat.f_mntonname) - 1, 877 &size); 878 879 /* 880 * Initial statfs to prime mnt_stat. 881 */ 882 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred); 883 884 return 0; 885 } 886 887 /* 888 * Handle bioq for strategy write 889 */ 890 static 891 void 892 hammer2_write_thread(void *arg) 893 { 894 hammer2_pfsmount_t *pmp; 895 struct bio *bio; 896 struct buf *bp; 897 hammer2_trans_t trans; 898 struct vnode *vp; 899 hammer2_inode_t *ip; 900 hammer2_cluster_t *cparent; 901 hammer2_inode_data_t *wipdata; 902 hammer2_key_t lbase; 903 int lblksize; 904 int pblksize; 905 int error; 906 907 pmp = arg; 908 909 mtx_lock(&pmp->wthread_mtx); 910 while (pmp->wthread_destroy == 0) { 911 if (bioq_first(&pmp->wthread_bioq) == NULL) { 912 mtxsleep(&pmp->wthread_bioq, &pmp->wthread_mtx, 913 0, "h2bioqw", 0); 914 } 915 cparent = NULL; 916 917 hammer2_trans_init(&trans, pmp, HAMMER2_TRANS_BUFCACHE); 918 919 while ((bio = bioq_takefirst(&pmp->wthread_bioq)) != NULL) { 920 /* 921 * dummy bio for synchronization. The transaction 922 * must be reinitialized. 923 */ 924 if (bio->bio_buf == NULL) { 925 bio->bio_flags |= BIO_DONE; 926 wakeup(bio); 927 hammer2_trans_done(&trans); 928 hammer2_trans_init(&trans, pmp, 929 HAMMER2_TRANS_BUFCACHE); 930 continue; 931 } 932 933 /* 934 * else normal bio processing 935 */ 936 mtx_unlock(&pmp->wthread_mtx); 937 938 hammer2_lwinprog_drop(pmp); 939 940 error = 0; 941 bp = bio->bio_buf; 942 vp = bp->b_vp; 943 ip = VTOI(vp); 944 945 /* 946 * Inode is modified, flush size and mtime changes 947 * to ensure that the file size remains consistent 948 * with the buffers being flushed. 949 * 950 * NOTE: The inode_fsync() call only flushes the 951 * inode's meta-data state, it doesn't try 952 * to flush underlying buffers or chains. 953 */ 954 cparent = hammer2_inode_lock_ex(ip); 955 if (ip->flags & (HAMMER2_INODE_RESIZED | 956 HAMMER2_INODE_MTIME)) { 957 hammer2_inode_fsync(&trans, ip, cparent); 958 } 959 wipdata = hammer2_cluster_modify_ip(&trans, ip, 960 cparent, 0); 961 lblksize = hammer2_calc_logical(ip, bio->bio_offset, 962 &lbase, NULL); 963 pblksize = hammer2_calc_physical(ip, wipdata, lbase); 964 hammer2_write_file_core(bp, &trans, ip, wipdata, 965 cparent, 966 lbase, IO_ASYNC, 967 pblksize, &error); 968 hammer2_cluster_modsync(cparent); 969 hammer2_inode_unlock_ex(ip, cparent); 970 if (error) { 971 kprintf("hammer2: error in buffer write\n"); 972 bp->b_flags |= B_ERROR; 973 bp->b_error = EIO; 974 } 975 biodone(bio); 976 mtx_lock(&pmp->wthread_mtx); 977 } 978 hammer2_trans_done(&trans); 979 } 980 pmp->wthread_destroy = -1; 981 wakeup(&pmp->wthread_destroy); 982 983 mtx_unlock(&pmp->wthread_mtx); 984 } 985 986 void 987 hammer2_bioq_sync(hammer2_pfsmount_t *pmp) 988 { 989 struct bio sync_bio; 990 991 bzero(&sync_bio, sizeof(sync_bio)); /* dummy with no bio_buf */ 992 mtx_lock(&pmp->wthread_mtx); 993 if (pmp->wthread_destroy == 0 && 994 TAILQ_FIRST(&pmp->wthread_bioq.queue)) { 995 bioq_insert_tail(&pmp->wthread_bioq, &sync_bio); 996 while ((sync_bio.bio_flags & BIO_DONE) == 0) 997 mtxsleep(&sync_bio, &pmp->wthread_mtx, 0, "h2bioq", 0); 998 } 999 mtx_unlock(&pmp->wthread_mtx); 1000 } 1001 1002 /* 1003 * Return a chain suitable for I/O, creating the chain if necessary 1004 * and assigning its physical block. 1005 */ 1006 static 1007 hammer2_cluster_t * 1008 hammer2_assign_physical(hammer2_trans_t *trans, 1009 hammer2_inode_t *ip, hammer2_cluster_t *cparent, 1010 hammer2_key_t lbase, int pblksize, int *errorp) 1011 { 1012 hammer2_cluster_t *cluster; 1013 hammer2_cluster_t *dparent; 1014 hammer2_key_t key_dummy; 1015 int pradix = hammer2_getradix(pblksize); 1016 int ddflag; 1017 1018 /* 1019 * Locate the chain associated with lbase, return a locked chain. 1020 * However, do not instantiate any data reference (which utilizes a 1021 * device buffer) because we will be using direct IO via the 1022 * logical buffer cache buffer. 1023 */ 1024 *errorp = 0; 1025 KKASSERT(pblksize >= HAMMER2_ALLOC_MIN); 1026 retry: 1027 dparent = hammer2_cluster_lookup_init(cparent, 0); 1028 cluster = hammer2_cluster_lookup(dparent, &key_dummy, 1029 lbase, lbase, 1030 HAMMER2_LOOKUP_NODATA, &ddflag); 1031 1032 if (cluster == NULL) { 1033 /* 1034 * We found a hole, create a new chain entry. 1035 * 1036 * NOTE: DATA chains are created without device backing 1037 * store (nor do we want any). 1038 */ 1039 *errorp = hammer2_cluster_create(trans, dparent, &cluster, 1040 lbase, HAMMER2_PBUFRADIX, 1041 HAMMER2_BREF_TYPE_DATA, 1042 pblksize, 0); 1043 if (cluster == NULL) { 1044 hammer2_cluster_lookup_done(dparent); 1045 panic("hammer2_cluster_create: par=%p error=%d\n", 1046 dparent->focus, *errorp); 1047 goto retry; 1048 } 1049 /*ip->delta_dcount += pblksize;*/ 1050 } else { 1051 switch (hammer2_cluster_type(cluster)) { 1052 case HAMMER2_BREF_TYPE_INODE: 1053 /* 1054 * The data is embedded in the inode. The 1055 * caller is responsible for marking the inode 1056 * modified and copying the data to the embedded 1057 * area. 1058 */ 1059 break; 1060 case HAMMER2_BREF_TYPE_DATA: 1061 if (hammer2_cluster_need_resize(cluster, pblksize)) { 1062 hammer2_cluster_resize(trans, ip, 1063 dparent, cluster, 1064 pradix, 1065 HAMMER2_MODIFY_OPTDATA); 1066 } 1067 1068 /* 1069 * DATA buffers must be marked modified whether the 1070 * data is in a logical buffer or not. We also have 1071 * to make this call to fixup the chain data pointers 1072 * after resizing in case this is an encrypted or 1073 * compressed buffer. 1074 */ 1075 hammer2_cluster_modify(trans, cluster, 1076 HAMMER2_MODIFY_OPTDATA); 1077 break; 1078 default: 1079 panic("hammer2_assign_physical: bad type"); 1080 /* NOT REACHED */ 1081 break; 1082 } 1083 } 1084 1085 /* 1086 * Cleanup. If cluster wound up being the inode itself, i.e. 1087 * the DIRECTDATA case for offset 0, then we need to update cparent. 1088 * The caller expects cparent to not become stale. 1089 */ 1090 hammer2_cluster_lookup_done(dparent); 1091 /* dparent = NULL; safety */ 1092 if (cluster && ddflag) 1093 hammer2_cluster_replace_locked(cparent, cluster); 1094 return (cluster); 1095 } 1096 1097 /* 1098 * bio queued from hammer2_vnops.c. 1099 * 1100 * The core write function which determines which path to take 1101 * depending on compression settings. We also have to locate the 1102 * related clusters so we can calculate and set the check data for 1103 * the blockref. 1104 */ 1105 static 1106 void 1107 hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans, 1108 hammer2_inode_t *ip, 1109 const hammer2_inode_data_t *ripdata, 1110 hammer2_cluster_t *cparent, 1111 hammer2_key_t lbase, int ioflag, int pblksize, 1112 int *errorp) 1113 { 1114 hammer2_cluster_t *cluster; 1115 1116 switch(HAMMER2_DEC_ALGO(ripdata->comp_algo)) { 1117 case HAMMER2_COMP_NONE: 1118 /* 1119 * We have to assign physical storage to the buffer 1120 * we intend to dirty or write now to avoid deadlocks 1121 * in the strategy code later. 1122 * 1123 * This can return NOOFFSET for inode-embedded data. 1124 * The strategy code will take care of it in that case. 1125 */ 1126 cluster = hammer2_assign_physical(trans, ip, cparent, 1127 lbase, pblksize, 1128 errorp); 1129 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp, 1130 ripdata->check_algo); 1131 if (cluster) 1132 hammer2_cluster_unlock(cluster); 1133 break; 1134 case HAMMER2_COMP_AUTOZERO: 1135 /* 1136 * Check for zero-fill only 1137 */ 1138 hammer2_zero_check_and_write(bp, trans, ip, 1139 ripdata, cparent, lbase, 1140 ioflag, pblksize, errorp, 1141 ripdata->check_algo); 1142 break; 1143 case HAMMER2_COMP_LZ4: 1144 case HAMMER2_COMP_ZLIB: 1145 default: 1146 /* 1147 * Check for zero-fill and attempt compression. 1148 */ 1149 hammer2_compress_and_write(bp, trans, ip, 1150 ripdata, cparent, 1151 lbase, ioflag, 1152 pblksize, errorp, 1153 ripdata->comp_algo, 1154 ripdata->check_algo); 1155 break; 1156 } 1157 } 1158 1159 /* 1160 * Generic function that will perform the compression in compression 1161 * write path. The compression algorithm is determined by the settings 1162 * obtained from inode. 1163 */ 1164 static 1165 void 1166 hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans, 1167 hammer2_inode_t *ip, const hammer2_inode_data_t *ripdata, 1168 hammer2_cluster_t *cparent, 1169 hammer2_key_t lbase, int ioflag, int pblksize, 1170 int *errorp, int comp_algo, int check_algo) 1171 { 1172 hammer2_cluster_t *cluster; 1173 hammer2_chain_t *chain; 1174 int comp_size; 1175 int comp_block_size; 1176 int i; 1177 char *comp_buffer; 1178 1179 if (test_block_zeros(bp->b_data, pblksize)) { 1180 zero_write(bp, trans, ip, ripdata, cparent, lbase, errorp); 1181 return; 1182 } 1183 1184 comp_size = 0; 1185 comp_buffer = NULL; 1186 1187 KKASSERT(pblksize / 2 <= 32768); 1188 1189 if (ip->comp_heuristic < 8 || (ip->comp_heuristic & 7) == 0) { 1190 z_stream strm_compress; 1191 int comp_level; 1192 int ret; 1193 1194 switch(HAMMER2_DEC_ALGO(comp_algo)) { 1195 case HAMMER2_COMP_LZ4: 1196 comp_buffer = objcache_get(cache_buffer_write, 1197 M_INTWAIT); 1198 comp_size = LZ4_compress_limitedOutput( 1199 bp->b_data, 1200 &comp_buffer[sizeof(int)], 1201 pblksize, 1202 pblksize / 2 - sizeof(int)); 1203 /* 1204 * We need to prefix with the size, LZ4 1205 * doesn't do it for us. Add the related 1206 * overhead. 1207 */ 1208 *(int *)comp_buffer = comp_size; 1209 if (comp_size) 1210 comp_size += sizeof(int); 1211 break; 1212 case HAMMER2_COMP_ZLIB: 1213 comp_level = HAMMER2_DEC_LEVEL(comp_algo); 1214 if (comp_level == 0) 1215 comp_level = 6; /* default zlib compression */ 1216 else if (comp_level < 6) 1217 comp_level = 6; 1218 else if (comp_level > 9) 1219 comp_level = 9; 1220 ret = deflateInit(&strm_compress, comp_level); 1221 if (ret != Z_OK) { 1222 kprintf("HAMMER2 ZLIB: fatal error " 1223 "on deflateInit.\n"); 1224 } 1225 1226 comp_buffer = objcache_get(cache_buffer_write, 1227 M_INTWAIT); 1228 strm_compress.next_in = bp->b_data; 1229 strm_compress.avail_in = pblksize; 1230 strm_compress.next_out = comp_buffer; 1231 strm_compress.avail_out = pblksize / 2; 1232 ret = deflate(&strm_compress, Z_FINISH); 1233 if (ret == Z_STREAM_END) { 1234 comp_size = pblksize / 2 - 1235 strm_compress.avail_out; 1236 } else { 1237 comp_size = 0; 1238 } 1239 ret = deflateEnd(&strm_compress); 1240 break; 1241 default: 1242 kprintf("Error: Unknown compression method.\n"); 1243 kprintf("Comp_method = %d.\n", comp_algo); 1244 break; 1245 } 1246 } 1247 1248 if (comp_size == 0) { 1249 /* 1250 * compression failed or turned off 1251 */ 1252 comp_block_size = pblksize; /* safety */ 1253 if (++ip->comp_heuristic > 128) 1254 ip->comp_heuristic = 8; 1255 } else { 1256 /* 1257 * compression succeeded 1258 */ 1259 ip->comp_heuristic = 0; 1260 if (comp_size <= 1024) { 1261 comp_block_size = 1024; 1262 } else if (comp_size <= 2048) { 1263 comp_block_size = 2048; 1264 } else if (comp_size <= 4096) { 1265 comp_block_size = 4096; 1266 } else if (comp_size <= 8192) { 1267 comp_block_size = 8192; 1268 } else if (comp_size <= 16384) { 1269 comp_block_size = 16384; 1270 } else if (comp_size <= 32768) { 1271 comp_block_size = 32768; 1272 } else { 1273 panic("hammer2: WRITE PATH: " 1274 "Weird comp_size value."); 1275 /* NOT REACHED */ 1276 comp_block_size = pblksize; 1277 } 1278 } 1279 1280 cluster = hammer2_assign_physical(trans, ip, cparent, 1281 lbase, comp_block_size, 1282 errorp); 1283 ripdata = NULL; 1284 1285 if (*errorp) { 1286 kprintf("WRITE PATH: An error occurred while " 1287 "assigning physical space.\n"); 1288 KKASSERT(cluster == NULL); 1289 goto done; 1290 } 1291 1292 for (i = 0; i < cluster->nchains; ++i) { 1293 hammer2_inode_data_t *wipdata; 1294 hammer2_io_t *dio; 1295 char *bdata; 1296 1297 chain = cluster->array[i]; /* XXX */ 1298 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED); 1299 1300 switch(chain->bref.type) { 1301 case HAMMER2_BREF_TYPE_INODE: 1302 wipdata = &hammer2_chain_wdata(chain)->ipdata; 1303 KKASSERT(wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA); 1304 KKASSERT(bp->b_loffset == 0); 1305 bcopy(bp->b_data, wipdata->u.data, 1306 HAMMER2_EMBEDDED_BYTES); 1307 break; 1308 case HAMMER2_BREF_TYPE_DATA: 1309 /* 1310 * Optimize out the read-before-write 1311 * if possible. 1312 */ 1313 *errorp = hammer2_io_newnz(chain->hmp, 1314 chain->bref.data_off, 1315 chain->bytes, 1316 &dio); 1317 if (*errorp) { 1318 hammer2_io_brelse(&dio); 1319 kprintf("hammer2: WRITE PATH: " 1320 "dbp bread error\n"); 1321 break; 1322 } 1323 bdata = hammer2_io_data(dio, chain->bref.data_off); 1324 1325 /* 1326 * When loading the block make sure we don't 1327 * leave garbage after the compressed data. 1328 */ 1329 if (comp_size) { 1330 chain->bref.methods = 1331 HAMMER2_ENC_COMP(comp_algo) + 1332 HAMMER2_ENC_CHECK(check_algo); 1333 bcopy(comp_buffer, bdata, comp_size); 1334 if (comp_size != comp_block_size) { 1335 bzero(bdata + comp_size, 1336 comp_block_size - comp_size); 1337 } 1338 } else { 1339 chain->bref.methods = 1340 HAMMER2_ENC_COMP( 1341 HAMMER2_COMP_NONE) + 1342 HAMMER2_ENC_CHECK(check_algo); 1343 bcopy(bp->b_data, bdata, pblksize); 1344 } 1345 1346 /* 1347 * The flush code doesn't calculate check codes for 1348 * file data (doing so can result in excessive I/O), 1349 * so we do it here. 1350 */ 1351 hammer2_chain_setcheck(chain, bdata); 1352 1353 /* 1354 * Device buffer is now valid, chain is no longer in 1355 * the initial state. 1356 * 1357 * (No blockref table worries with file data) 1358 */ 1359 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL); 1360 1361 /* Now write the related bdp. */ 1362 if (ioflag & IO_SYNC) { 1363 /* 1364 * Synchronous I/O requested. 1365 */ 1366 hammer2_io_bwrite(&dio); 1367 /* 1368 } else if ((ioflag & IO_DIRECT) && 1369 loff + n == pblksize) { 1370 hammer2_io_bdwrite(&dio); 1371 */ 1372 } else if (ioflag & IO_ASYNC) { 1373 hammer2_io_bawrite(&dio); 1374 } else { 1375 hammer2_io_bdwrite(&dio); 1376 } 1377 break; 1378 default: 1379 panic("hammer2_write_bp: bad chain type %d\n", 1380 chain->bref.type); 1381 /* NOT REACHED */ 1382 break; 1383 } 1384 } 1385 done: 1386 if (cluster) 1387 hammer2_cluster_unlock(cluster); 1388 if (comp_buffer) 1389 objcache_put(cache_buffer_write, comp_buffer); 1390 } 1391 1392 /* 1393 * Function that performs zero-checking and writing without compression, 1394 * it corresponds to default zero-checking path. 1395 */ 1396 static 1397 void 1398 hammer2_zero_check_and_write(struct buf *bp, hammer2_trans_t *trans, 1399 hammer2_inode_t *ip, const hammer2_inode_data_t *ripdata, 1400 hammer2_cluster_t *cparent, 1401 hammer2_key_t lbase, int ioflag, int pblksize, int *errorp, 1402 int check_algo) 1403 { 1404 hammer2_cluster_t *cluster; 1405 1406 if (test_block_zeros(bp->b_data, pblksize)) { 1407 zero_write(bp, trans, ip, ripdata, cparent, lbase, errorp); 1408 } else { 1409 cluster = hammer2_assign_physical(trans, ip, cparent, 1410 lbase, pblksize, errorp); 1411 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp, 1412 check_algo); 1413 if (cluster) 1414 hammer2_cluster_unlock(cluster); 1415 } 1416 } 1417 1418 /* 1419 * A function to test whether a block of data contains only zeros, 1420 * returns TRUE (non-zero) if the block is all zeros. 1421 */ 1422 static 1423 int 1424 test_block_zeros(const char *buf, size_t bytes) 1425 { 1426 size_t i; 1427 1428 for (i = 0; i < bytes; i += sizeof(long)) { 1429 if (*(const long *)(buf + i) != 0) 1430 return (0); 1431 } 1432 return (1); 1433 } 1434 1435 /* 1436 * Function to "write" a block that contains only zeros. 1437 */ 1438 static 1439 void 1440 zero_write(struct buf *bp, hammer2_trans_t *trans, 1441 hammer2_inode_t *ip, const hammer2_inode_data_t *ripdata, 1442 hammer2_cluster_t *cparent, 1443 hammer2_key_t lbase, int *errorp __unused) 1444 { 1445 hammer2_cluster_t *cluster; 1446 hammer2_media_data_t *data; 1447 hammer2_key_t key_dummy; 1448 int ddflag; 1449 1450 cparent = hammer2_cluster_lookup_init(cparent, 0); 1451 cluster = hammer2_cluster_lookup(cparent, &key_dummy, lbase, lbase, 1452 HAMMER2_LOOKUP_NODATA, &ddflag); 1453 if (cluster) { 1454 data = hammer2_cluster_wdata(cluster); 1455 1456 if (ddflag) { 1457 KKASSERT(cluster->focus->flags & 1458 HAMMER2_CHAIN_MODIFIED); 1459 bzero(data->ipdata.u.data, HAMMER2_EMBEDDED_BYTES); 1460 hammer2_cluster_modsync(cluster); 1461 } else { 1462 hammer2_cluster_delete(trans, cparent, cluster, 1463 HAMMER2_DELETE_PERMANENT); 1464 } 1465 hammer2_cluster_unlock(cluster); 1466 } 1467 hammer2_cluster_lookup_done(cparent); 1468 } 1469 1470 /* 1471 * Function to write the data as it is, without performing any sort of 1472 * compression. This function is used in path without compression and 1473 * default zero-checking path. 1474 */ 1475 static 1476 void 1477 hammer2_write_bp(hammer2_cluster_t *cluster, struct buf *bp, int ioflag, 1478 int pblksize, int *errorp, int check_algo) 1479 { 1480 hammer2_chain_t *chain; 1481 hammer2_inode_data_t *wipdata; 1482 hammer2_io_t *dio; 1483 char *bdata; 1484 int error; 1485 int i; 1486 1487 error = 0; /* XXX TODO below */ 1488 1489 for (i = 0; i < cluster->nchains; ++i) { 1490 chain = cluster->array[i]; /* XXX */ 1491 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED); 1492 1493 switch(chain->bref.type) { 1494 case HAMMER2_BREF_TYPE_INODE: 1495 wipdata = &hammer2_chain_wdata(chain)->ipdata; 1496 KKASSERT(wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA); 1497 KKASSERT(bp->b_loffset == 0); 1498 bcopy(bp->b_data, wipdata->u.data, 1499 HAMMER2_EMBEDDED_BYTES); 1500 error = 0; 1501 break; 1502 case HAMMER2_BREF_TYPE_DATA: 1503 error = hammer2_io_newnz(chain->hmp, 1504 chain->bref.data_off, 1505 chain->bytes, &dio); 1506 if (error) { 1507 hammer2_io_bqrelse(&dio); 1508 kprintf("hammer2: WRITE PATH: " 1509 "dbp bread error\n"); 1510 break; 1511 } 1512 bdata = hammer2_io_data(dio, chain->bref.data_off); 1513 1514 chain->bref.methods = HAMMER2_ENC_COMP( 1515 HAMMER2_COMP_NONE) + 1516 HAMMER2_ENC_CHECK(check_algo); 1517 bcopy(bp->b_data, bdata, chain->bytes); 1518 1519 /* 1520 * The flush code doesn't calculate check codes for 1521 * file data (doing so can result in excessive I/O), 1522 * so we do it here. 1523 */ 1524 hammer2_chain_setcheck(chain, bdata); 1525 1526 /* 1527 * Device buffer is now valid, chain is no longer in 1528 * the initial state. 1529 * 1530 * (No blockref table worries with file data) 1531 */ 1532 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL); 1533 1534 if (ioflag & IO_SYNC) { 1535 /* 1536 * Synchronous I/O requested. 1537 */ 1538 hammer2_io_bwrite(&dio); 1539 /* 1540 } else if ((ioflag & IO_DIRECT) && 1541 loff + n == pblksize) { 1542 hammer2_io_bdwrite(&dio); 1543 */ 1544 } else if (ioflag & IO_ASYNC) { 1545 hammer2_io_bawrite(&dio); 1546 } else { 1547 hammer2_io_bdwrite(&dio); 1548 } 1549 break; 1550 default: 1551 panic("hammer2_write_bp: bad chain type %d\n", 1552 chain->bref.type); 1553 /* NOT REACHED */ 1554 error = 0; 1555 break; 1556 } 1557 KKASSERT(error == 0); /* XXX TODO */ 1558 } 1559 *errorp = error; 1560 } 1561 1562 static 1563 int 1564 hammer2_remount(hammer2_mount_t *hmp, struct mount *mp, char *path, 1565 struct vnode *devvp, struct ucred *cred) 1566 { 1567 int error; 1568 1569 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 1570 error = hammer2_recovery(hmp); 1571 } else { 1572 error = 0; 1573 } 1574 return error; 1575 } 1576 1577 static 1578 int 1579 hammer2_vfs_unmount(struct mount *mp, int mntflags) 1580 { 1581 hammer2_pfsmount_t *pmp; 1582 hammer2_mount_t *hmp; 1583 hammer2_chain_t *rchain; 1584 hammer2_cluster_t *cluster; 1585 int flags; 1586 int error = 0; 1587 int i; 1588 1589 pmp = MPTOPMP(mp); 1590 1591 if (pmp == NULL) 1592 return(0); 1593 1594 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE); 1595 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry); 1596 1597 /* 1598 * If mount initialization proceeded far enough we must flush 1599 * its vnodes. 1600 */ 1601 if (mntflags & MNT_FORCE) 1602 flags = FORCECLOSE; 1603 else 1604 flags = 0; 1605 if (pmp->iroot) { 1606 error = vflush(mp, 0, flags); 1607 if (error) 1608 goto failed; 1609 } 1610 1611 ccms_domain_uninit(&pmp->ccms_dom); 1612 1613 if (pmp->wthread_td) { 1614 mtx_lock(&pmp->wthread_mtx); 1615 pmp->wthread_destroy = 1; 1616 wakeup(&pmp->wthread_bioq); 1617 while (pmp->wthread_destroy != -1) { 1618 mtxsleep(&pmp->wthread_destroy, 1619 &pmp->wthread_mtx, 0, 1620 "umount-sleep", 0); 1621 } 1622 mtx_unlock(&pmp->wthread_mtx); 1623 pmp->wthread_td = NULL; 1624 } 1625 1626 /* 1627 * Cleanup our reference on ihidden. 1628 */ 1629 if (pmp->ihidden) { 1630 hammer2_inode_drop(pmp->ihidden); 1631 pmp->ihidden = NULL; 1632 } 1633 1634 /* 1635 * Cleanup our reference on iroot. iroot is (should) not be needed 1636 * by the flush code. 1637 */ 1638 if (pmp->iroot) { 1639 cluster = &pmp->iroot->cluster; 1640 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) { 1641 rchain = pmp->iroot->cluster.array[i]; 1642 if (rchain == NULL) 1643 continue; 1644 hmp = rchain->hmp; 1645 hammer2_vfs_unmount_hmp1(mp, hmp); 1646 1647 atomic_clear_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED); 1648 #if REPORT_REFS_ERRORS 1649 if (rchain->refs != 1) 1650 kprintf("PMP->RCHAIN %p REFS WRONG %d\n", 1651 rchain, rchain->refs); 1652 #else 1653 KKASSERT(rchain->refs == 1); 1654 #endif 1655 hammer2_chain_drop(rchain); 1656 cluster->array[i] = NULL; 1657 hammer2_vfs_unmount_hmp2(mp, hmp); 1658 } 1659 cluster->focus = NULL; 1660 1661 #if REPORT_REFS_ERRORS 1662 if (pmp->iroot->refs != 1) 1663 kprintf("PMP->IROOT %p REFS WRONG %d\n", 1664 pmp->iroot, pmp->iroot->refs); 1665 #else 1666 KKASSERT(pmp->iroot->refs == 1); 1667 #endif 1668 /* ref for pmp->iroot */ 1669 hammer2_inode_drop(pmp->iroot); 1670 pmp->iroot = NULL; 1671 } 1672 1673 pmp->mp = NULL; 1674 mp->mnt_data = NULL; 1675 1676 kmalloc_destroy(&pmp->mmsg); 1677 kmalloc_destroy(&pmp->minode); 1678 1679 kfree(pmp, M_HAMMER2); 1680 error = 0; 1681 1682 failed: 1683 lockmgr(&hammer2_mntlk, LK_RELEASE); 1684 1685 return (error); 1686 } 1687 1688 static 1689 void 1690 hammer2_vfs_unmount_hmp1(struct mount *mp, hammer2_mount_t *hmp) 1691 { 1692 hammer2_mount_exlock(hmp); 1693 --hmp->pmp_count; 1694 1695 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count); 1696 1697 kdmsg_iocom_uninit(&hmp->iocom); /* XXX chain depend deadlck? */ 1698 1699 /* 1700 * Cycle the volume data lock as a safety (probably not needed any 1701 * more). To ensure everything is out we need to flush at least 1702 * three times. (1) The running of the unlinkq can dirty the 1703 * filesystem, (2) A normal flush can dirty the freemap, and 1704 * (3) ensure that the freemap is fully synchronized. 1705 * 1706 * The next mount's recovery scan can clean everything up but we want 1707 * to leave the filesystem in a 100% clean state on a normal unmount. 1708 */ 1709 hammer2_voldata_lock(hmp); 1710 hammer2_voldata_unlock(hmp); 1711 if (mp->mnt_data) { 1712 hammer2_vfs_sync(mp, MNT_WAIT); 1713 hammer2_vfs_sync(mp, MNT_WAIT); 1714 hammer2_vfs_sync(mp, MNT_WAIT); 1715 } 1716 1717 if (hmp->pmp_count == 0) { 1718 if ((hmp->vchain.flags | hmp->fchain.flags) & 1719 HAMMER2_CHAIN_FLUSH_MASK) { 1720 kprintf("hammer2_unmount: chains left over " 1721 "after final sync\n"); 1722 kprintf(" vchain %08x\n", hmp->vchain.flags); 1723 kprintf(" fchain %08x\n", hmp->fchain.flags); 1724 1725 if (hammer2_debug & 0x0010) 1726 Debugger("entered debugger"); 1727 } 1728 } 1729 } 1730 1731 static 1732 void 1733 hammer2_vfs_unmount_hmp2(struct mount *mp, hammer2_mount_t *hmp) 1734 { 1735 hammer2_pfsmount_t *spmp; 1736 struct vnode *devvp; 1737 int dumpcnt; 1738 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0); 1739 1740 /* 1741 * If no PFS's left drop the master hammer2_mount for the 1742 * device. 1743 */ 1744 if (hmp->pmp_count == 0) { 1745 /* 1746 * Clean up SPMP and the super-root inode 1747 */ 1748 spmp = hmp->spmp; 1749 if (spmp) { 1750 if (spmp->iroot) { 1751 hammer2_inode_drop(spmp->iroot); 1752 spmp->iroot = NULL; 1753 } 1754 hmp->spmp = NULL; 1755 kmalloc_destroy(&spmp->mmsg); 1756 kmalloc_destroy(&spmp->minode); 1757 kfree(spmp, M_HAMMER2); 1758 } 1759 1760 /* 1761 * Finish up with the device vnode 1762 */ 1763 if ((devvp = hmp->devvp) != NULL) { 1764 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1765 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0); 1766 hmp->devvp = NULL; 1767 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL); 1768 vn_unlock(devvp); 1769 vrele(devvp); 1770 devvp = NULL; 1771 } 1772 1773 /* 1774 * Clear vchain/fchain flags that might prevent final cleanup 1775 * of these chains. 1776 */ 1777 if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) { 1778 atomic_clear_int(&hmp->vchain.flags, 1779 HAMMER2_CHAIN_MODIFIED); 1780 hammer2_pfs_memory_wakeup(hmp->vchain.pmp); 1781 hammer2_chain_drop(&hmp->vchain); 1782 } 1783 if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) { 1784 atomic_clear_int(&hmp->vchain.flags, 1785 HAMMER2_CHAIN_UPDATE); 1786 hammer2_chain_drop(&hmp->vchain); 1787 } 1788 1789 if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) { 1790 atomic_clear_int(&hmp->fchain.flags, 1791 HAMMER2_CHAIN_MODIFIED); 1792 hammer2_pfs_memory_wakeup(hmp->fchain.pmp); 1793 hammer2_chain_drop(&hmp->fchain); 1794 } 1795 if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) { 1796 atomic_clear_int(&hmp->fchain.flags, 1797 HAMMER2_CHAIN_UPDATE); 1798 hammer2_chain_drop(&hmp->fchain); 1799 } 1800 1801 /* 1802 * Final drop of embedded freemap root chain to 1803 * clean up fchain.core (fchain structure is not 1804 * flagged ALLOCATED so it is cleaned out and then 1805 * left to rot). 1806 */ 1807 hammer2_chain_drop(&hmp->fchain); 1808 1809 /* 1810 * Final drop of embedded volume root chain to clean 1811 * up vchain.core (vchain structure is not flagged 1812 * ALLOCATED so it is cleaned out and then left to 1813 * rot). 1814 */ 1815 dumpcnt = 50; 1816 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v'); 1817 dumpcnt = 50; 1818 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f'); 1819 hammer2_mount_unlock(hmp); 1820 hammer2_chain_drop(&hmp->vchain); 1821 1822 hammer2_io_cleanup(hmp, &hmp->iotree); 1823 if (hmp->iofree_count) { 1824 kprintf("io_cleanup: %d I/O's left hanging\n", 1825 hmp->iofree_count); 1826 } 1827 1828 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry); 1829 kmalloc_destroy(&hmp->mchain); 1830 kfree(hmp, M_HAMMER2); 1831 } else { 1832 hammer2_mount_unlock(hmp); 1833 } 1834 } 1835 1836 static 1837 int 1838 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp, 1839 ino_t ino, struct vnode **vpp) 1840 { 1841 kprintf("hammer2_vget\n"); 1842 return (EOPNOTSUPP); 1843 } 1844 1845 static 1846 int 1847 hammer2_vfs_root(struct mount *mp, struct vnode **vpp) 1848 { 1849 hammer2_pfsmount_t *pmp; 1850 hammer2_cluster_t *cparent; 1851 int error; 1852 struct vnode *vp; 1853 1854 pmp = MPTOPMP(mp); 1855 if (pmp->iroot == NULL) { 1856 *vpp = NULL; 1857 error = EINVAL; 1858 } else { 1859 cparent = hammer2_inode_lock_sh(pmp->iroot); 1860 vp = hammer2_igetv(pmp->iroot, cparent, &error); 1861 hammer2_inode_unlock_sh(pmp->iroot, cparent); 1862 *vpp = vp; 1863 if (vp == NULL) 1864 kprintf("vnodefail\n"); 1865 } 1866 1867 return (error); 1868 } 1869 1870 /* 1871 * Filesystem status 1872 * 1873 * XXX incorporate ipdata->inode_quota and data_quota 1874 */ 1875 static 1876 int 1877 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 1878 { 1879 hammer2_pfsmount_t *pmp; 1880 hammer2_mount_t *hmp; 1881 1882 pmp = MPTOPMP(mp); 1883 KKASSERT(pmp->iroot->cluster.nchains >= 1); 1884 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */ 1885 1886 mp->mnt_stat.f_files = pmp->inode_count; 1887 mp->mnt_stat.f_ffree = 0; 1888 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE; 1889 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE; 1890 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree; 1891 1892 *sbp = mp->mnt_stat; 1893 return (0); 1894 } 1895 1896 static 1897 int 1898 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred) 1899 { 1900 hammer2_pfsmount_t *pmp; 1901 hammer2_mount_t *hmp; 1902 1903 pmp = MPTOPMP(mp); 1904 KKASSERT(pmp->iroot->cluster.nchains >= 1); 1905 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */ 1906 1907 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE; 1908 mp->mnt_vstat.f_files = pmp->inode_count; 1909 mp->mnt_vstat.f_ffree = 0; 1910 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE; 1911 mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE; 1912 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree; 1913 1914 *sbp = mp->mnt_vstat; 1915 return (0); 1916 } 1917 1918 /* 1919 * Mount-time recovery (RW mounts) 1920 * 1921 * Updates to the free block table are allowed to lag flushes by one 1922 * transaction. In case of a crash, then on a fresh mount we must do an 1923 * incremental scan of the last committed transaction id and make sure that 1924 * all related blocks have been marked allocated. 1925 * 1926 * The super-root topology and each PFS has its own transaction id domain, 1927 * so we must track PFS boundary transitions. 1928 */ 1929 struct hammer2_recovery_elm { 1930 TAILQ_ENTRY(hammer2_recovery_elm) entry; 1931 hammer2_chain_t *chain; 1932 hammer2_tid_t sync_tid; 1933 }; 1934 1935 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm); 1936 1937 struct hammer2_recovery_info { 1938 struct hammer2_recovery_list list; 1939 int depth; 1940 }; 1941 1942 static int hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_mount_t *hmp, 1943 hammer2_chain_t *parent, 1944 struct hammer2_recovery_info *info, 1945 hammer2_tid_t sync_tid); 1946 1947 #define HAMMER2_RECOVERY_MAXDEPTH 10 1948 1949 static 1950 int 1951 hammer2_recovery(hammer2_mount_t *hmp) 1952 { 1953 hammer2_trans_t trans; 1954 struct hammer2_recovery_info info; 1955 struct hammer2_recovery_elm *elm; 1956 hammer2_chain_t *parent; 1957 hammer2_tid_t sync_tid; 1958 int error; 1959 int cumulative_error = 0; 1960 1961 hammer2_trans_init(&trans, hmp->spmp, 0); 1962 1963 sync_tid = 0; 1964 TAILQ_INIT(&info.list); 1965 info.depth = 0; 1966 parent = hammer2_chain_lookup_init(&hmp->vchain, 0); 1967 cumulative_error = hammer2_recovery_scan(&trans, hmp, parent, 1968 &info, sync_tid); 1969 hammer2_chain_lookup_done(parent); 1970 1971 while ((elm = TAILQ_FIRST(&info.list)) != NULL) { 1972 TAILQ_REMOVE(&info.list, elm, entry); 1973 parent = elm->chain; 1974 sync_tid = elm->sync_tid; 1975 kfree(elm, M_HAMMER2); 1976 1977 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS | 1978 HAMMER2_RESOLVE_NOREF); 1979 error = hammer2_recovery_scan(&trans, hmp, parent, 1980 &info, sync_tid); 1981 hammer2_chain_unlock(parent); 1982 if (error) 1983 cumulative_error = error; 1984 } 1985 hammer2_trans_done(&trans); 1986 1987 return cumulative_error; 1988 } 1989 1990 static 1991 int 1992 hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_mount_t *hmp, 1993 hammer2_chain_t *parent, 1994 struct hammer2_recovery_info *info, 1995 hammer2_tid_t sync_tid) 1996 { 1997 const hammer2_inode_data_t *ripdata; 1998 hammer2_chain_t *chain; 1999 int cache_index; 2000 int cumulative_error = 0; 2001 int pfs_boundary = 0; 2002 int error; 2003 2004 /* 2005 * Adjust freemap to ensure that the block(s) are marked allocated. 2006 */ 2007 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) { 2008 hammer2_freemap_adjust(trans, hmp, &parent->bref, 2009 HAMMER2_FREEMAP_DORECOVER); 2010 } 2011 2012 /* 2013 * Check type for recursive scan 2014 */ 2015 switch(parent->bref.type) { 2016 case HAMMER2_BREF_TYPE_VOLUME: 2017 /* data already instantiated */ 2018 break; 2019 case HAMMER2_BREF_TYPE_INODE: 2020 /* 2021 * Must instantiate data for DIRECTDATA test and also 2022 * for recursion. 2023 */ 2024 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 2025 ripdata = &hammer2_chain_rdata(parent)->ipdata; 2026 if (ripdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) { 2027 /* not applicable to recovery scan */ 2028 hammer2_chain_unlock(parent); 2029 return 0; 2030 } 2031 if ((ripdata->op_flags & HAMMER2_OPFLAG_PFSROOT) && 2032 info->depth != 0) { 2033 pfs_boundary = 1; 2034 sync_tid = parent->bref.mirror_tid - 1; 2035 } 2036 hammer2_chain_unlock(parent); 2037 break; 2038 case HAMMER2_BREF_TYPE_INDIRECT: 2039 /* 2040 * Must instantiate data for recursion 2041 */ 2042 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 2043 hammer2_chain_unlock(parent); 2044 break; 2045 case HAMMER2_BREF_TYPE_DATA: 2046 case HAMMER2_BREF_TYPE_FREEMAP: 2047 case HAMMER2_BREF_TYPE_FREEMAP_NODE: 2048 case HAMMER2_BREF_TYPE_FREEMAP_LEAF: 2049 /* not applicable to recovery scan */ 2050 return 0; 2051 break; 2052 default: 2053 return EDOM; 2054 } 2055 2056 /* 2057 * Defer operation if depth limit reached or if we are crossing a 2058 * PFS boundary. 2059 */ 2060 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH || pfs_boundary) { 2061 struct hammer2_recovery_elm *elm; 2062 2063 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK); 2064 elm->chain = parent; 2065 elm->sync_tid = sync_tid; 2066 hammer2_chain_ref(parent); 2067 TAILQ_INSERT_TAIL(&info->list, elm, entry); 2068 /* unlocked by caller */ 2069 2070 return(0); 2071 } 2072 2073 2074 /* 2075 * Recursive scan of the last flushed transaction only. We are 2076 * doing this without pmp assignments so don't leave the chains 2077 * hanging around after we are done with them. 2078 */ 2079 cache_index = 0; 2080 chain = hammer2_chain_scan(parent, NULL, &cache_index, 2081 HAMMER2_LOOKUP_NODATA); 2082 while (chain) { 2083 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE); 2084 if (chain->bref.mirror_tid >= sync_tid) { 2085 ++info->depth; 2086 error = hammer2_recovery_scan(trans, hmp, chain, 2087 info, sync_tid); 2088 --info->depth; 2089 if (error) 2090 cumulative_error = error; 2091 } 2092 chain = hammer2_chain_scan(parent, chain, &cache_index, 2093 HAMMER2_LOOKUP_NODATA); 2094 } 2095 2096 return cumulative_error; 2097 } 2098 2099 /* 2100 * Sync the entire filesystem; this is called from the filesystem syncer 2101 * process periodically and whenever a user calls sync(1) on the hammer 2102 * mountpoint. 2103 * 2104 * Currently is actually called from the syncer! \o/ 2105 * 2106 * This task will have to snapshot the state of the dirty inode chain. 2107 * From that, it will have to make sure all of the inodes on the dirty 2108 * chain have IO initiated. We make sure that io is initiated for the root 2109 * block. 2110 * 2111 * If waitfor is set, we wait for media to acknowledge the new rootblock. 2112 * 2113 * THINKS: side A vs side B, to have sync not stall all I/O? 2114 */ 2115 int 2116 hammer2_vfs_sync(struct mount *mp, int waitfor) 2117 { 2118 struct hammer2_sync_info info; 2119 hammer2_inode_t *iroot; 2120 hammer2_chain_t *chain; 2121 hammer2_chain_t *parent; 2122 hammer2_pfsmount_t *pmp; 2123 hammer2_mount_t *hmp; 2124 int flags; 2125 int error; 2126 int total_error; 2127 int force_fchain; 2128 int i; 2129 int j; 2130 2131 pmp = MPTOPMP(mp); 2132 iroot = pmp->iroot; 2133 KKASSERT(iroot); 2134 KKASSERT(iroot->pmp == pmp); 2135 2136 /* 2137 * We can't acquire locks on existing vnodes while in a transaction 2138 * without risking a deadlock. This assumes that vfsync() can be 2139 * called without the vnode locked (which it can in DragonFly). 2140 * Otherwise we'd have to implement a multi-pass or flag the lock 2141 * failures and retry. 2142 * 2143 * The reclamation code interlocks with the sync list's token 2144 * (by removing the vnode from the scan list) before unlocking 2145 * the inode, giving us time to ref the inode. 2146 */ 2147 /*flags = VMSC_GETVP;*/ 2148 flags = 0; 2149 if (waitfor & MNT_LAZY) 2150 flags |= VMSC_ONEPASS; 2151 2152 /* 2153 * Start our flush transaction. This does not return until all 2154 * concurrent transactions have completed and will prevent any 2155 * new transactions from running concurrently, except for the 2156 * buffer cache transactions. 2157 * 2158 * For efficiency do an async pass before making sure with a 2159 * synchronous pass on all related buffer cache buffers. It 2160 * should theoretically not be possible for any new file buffers 2161 * to be instantiated during this sequence. 2162 */ 2163 hammer2_trans_init(&info.trans, pmp, HAMMER2_TRANS_ISFLUSH | 2164 HAMMER2_TRANS_PREFLUSH); 2165 hammer2_run_unlinkq(&info.trans, pmp); 2166 2167 info.error = 0; 2168 info.waitfor = MNT_NOWAIT; 2169 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info); 2170 info.waitfor = MNT_WAIT; 2171 vsyncscan(mp, flags, hammer2_sync_scan2, &info); 2172 2173 /* 2174 * Clear PREFLUSH. This prevents (or asserts on) any new logical 2175 * buffer cache flushes which occur during the flush. Device buffers 2176 * are not affected. 2177 */ 2178 2179 #if 0 2180 if (info.error == 0 && (waitfor & MNT_WAIT)) { 2181 info.waitfor = waitfor; 2182 vsyncscan(mp, flags, hammer2_sync_scan2, &info); 2183 2184 } 2185 #endif 2186 hammer2_bioq_sync(info.trans.pmp); 2187 atomic_clear_int(&info.trans.flags, HAMMER2_TRANS_PREFLUSH); 2188 2189 total_error = 0; 2190 2191 /* 2192 * Flush all storage elements making up the cluster 2193 * 2194 * We must also flush any deleted siblings because the super-root 2195 * flush won't do it for us. They all must be staged or the 2196 * super-root flush will not be able to update its block table 2197 * properly. 2198 * 2199 * XXX currently done serially instead of concurrently 2200 */ 2201 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) { 2202 chain = iroot->cluster.array[i]; 2203 if (chain) { 2204 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS); 2205 hammer2_flush(&info.trans, chain); 2206 hammer2_chain_unlock(chain); 2207 } 2208 } 2209 #if 0 2210 hammer2_trans_done(&info.trans); 2211 #endif 2212 2213 /* 2214 * Flush all volume roots to synchronize PFS flushes with the 2215 * storage media. Use a super-root transaction for each one. 2216 * 2217 * The flush code will detect super-root -> pfs-root chain 2218 * transitions using the last pfs-root flush. 2219 */ 2220 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) { 2221 chain = iroot->cluster.array[i]; 2222 if (chain == NULL) 2223 continue; 2224 2225 hmp = chain->hmp; 2226 2227 /* 2228 * We only have to flush each hmp once 2229 */ 2230 for (j = i - 1; j >= 0; --j) { 2231 if (iroot->cluster.array[j] && 2232 iroot->cluster.array[j]->hmp == hmp) 2233 break; 2234 } 2235 if (j >= 0) 2236 continue; 2237 hammer2_trans_spmp(&info.trans, hmp->spmp); 2238 2239 /* 2240 * Force an update of the XID from the PFS root to the 2241 * topology root. We couldn't do this from the PFS 2242 * transaction because a SPMP transaction is needed. 2243 * This does not modify blocks, instead what it does is 2244 * allow the flush code to find the transition point and 2245 * then update on the way back up. 2246 */ 2247 parent = chain->parent; 2248 KKASSERT(chain->pmp != parent->pmp); 2249 hammer2_chain_setflush(&info.trans, parent); 2250 2251 /* 2252 * Media mounts have two 'roots', vchain for the topology 2253 * and fchain for the free block table. Flush both. 2254 * 2255 * Note that the topology and free block table are handled 2256 * independently, so the free block table can wind up being 2257 * ahead of the topology. We depend on the bulk free scan 2258 * code to deal with any loose ends. 2259 */ 2260 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS); 2261 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS); 2262 if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) { 2263 /* 2264 * This will also modify vchain as a side effect, 2265 * mark vchain as modified now. 2266 */ 2267 hammer2_voldata_modify(hmp); 2268 chain = &hmp->fchain; 2269 hammer2_flush(&info.trans, chain); 2270 KKASSERT(chain == &hmp->fchain); 2271 } 2272 hammer2_chain_unlock(&hmp->fchain); 2273 hammer2_chain_unlock(&hmp->vchain); 2274 2275 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS); 2276 if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) { 2277 chain = &hmp->vchain; 2278 hammer2_flush(&info.trans, chain); 2279 KKASSERT(chain == &hmp->vchain); 2280 force_fchain = 1; 2281 } else { 2282 force_fchain = 0; 2283 } 2284 hammer2_chain_unlock(&hmp->vchain); 2285 2286 #if 0 2287 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS); 2288 if ((hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) || 2289 force_fchain) { 2290 /* this will also modify vchain as a side effect */ 2291 chain = &hmp->fchain; 2292 hammer2_flush(&info.trans, chain); 2293 KKASSERT(chain == &hmp->fchain); 2294 } 2295 hammer2_chain_unlock(&hmp->fchain); 2296 #endif 2297 2298 error = 0; 2299 2300 /* 2301 * We can't safely flush the volume header until we have 2302 * flushed any device buffers which have built up. 2303 * 2304 * XXX this isn't being incremental 2305 */ 2306 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY); 2307 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0); 2308 vn_unlock(hmp->devvp); 2309 2310 /* 2311 * The flush code sets CHAIN_VOLUMESYNC to indicate that the 2312 * volume header needs synchronization via hmp->volsync. 2313 * 2314 * XXX synchronize the flag & data with only this flush XXX 2315 */ 2316 if (error == 0 && 2317 (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) { 2318 struct buf *bp; 2319 2320 /* 2321 * Synchronize the disk before flushing the volume 2322 * header. 2323 */ 2324 bp = getpbuf(NULL); 2325 bp->b_bio1.bio_offset = 0; 2326 bp->b_bufsize = 0; 2327 bp->b_bcount = 0; 2328 bp->b_cmd = BUF_CMD_FLUSH; 2329 bp->b_bio1.bio_done = biodone_sync; 2330 bp->b_bio1.bio_flags |= BIO_SYNC; 2331 vn_strategy(hmp->devvp, &bp->b_bio1); 2332 biowait(&bp->b_bio1, "h2vol"); 2333 relpbuf(bp, NULL); 2334 2335 /* 2336 * Then we can safely flush the version of the 2337 * volume header synchronized by the flush code. 2338 */ 2339 i = hmp->volhdrno + 1; 2340 if (i >= HAMMER2_NUM_VOLHDRS) 2341 i = 0; 2342 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE > 2343 hmp->volsync.volu_size) { 2344 i = 0; 2345 } 2346 kprintf("sync volhdr %d %jd\n", 2347 i, (intmax_t)hmp->volsync.volu_size); 2348 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64, 2349 HAMMER2_PBUFSIZE, 0, 0); 2350 atomic_clear_int(&hmp->vchain.flags, 2351 HAMMER2_CHAIN_VOLUMESYNC); 2352 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE); 2353 bawrite(bp); 2354 hmp->volhdrno = i; 2355 } 2356 if (error) 2357 total_error = error; 2358 2359 #if 0 2360 hammer2_trans_done(&info.trans); 2361 #endif 2362 } 2363 hammer2_trans_done(&info.trans); 2364 2365 return (total_error); 2366 } 2367 2368 /* 2369 * Sync passes. 2370 */ 2371 static int 2372 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data) 2373 { 2374 struct hammer2_sync_info *info = data; 2375 hammer2_inode_t *ip; 2376 int error; 2377 2378 /* 2379 * 2380 */ 2381 ip = VTOI(vp); 2382 if (ip == NULL) 2383 return(0); 2384 if (vp->v_type == VNON || vp->v_type == VBAD) { 2385 vclrisdirty(vp); 2386 return(0); 2387 } 2388 if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 && 2389 RB_EMPTY(&vp->v_rbdirty_tree)) { 2390 vclrisdirty(vp); 2391 return(0); 2392 } 2393 2394 /* 2395 * VOP_FSYNC will start a new transaction so replicate some code 2396 * here to do it inline (see hammer2_vop_fsync()). 2397 * 2398 * WARNING: The vfsync interacts with the buffer cache and might 2399 * block, we can't hold the inode lock at that time. 2400 * However, we MUST ref ip before blocking to ensure that 2401 * it isn't ripped out from under us (since we do not 2402 * hold a lock on the vnode). 2403 */ 2404 hammer2_inode_ref(ip); 2405 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED); 2406 if (vp) 2407 vfsync(vp, MNT_NOWAIT, 1, NULL, NULL); 2408 2409 hammer2_inode_drop(ip); 2410 #if 1 2411 error = 0; 2412 if (error) 2413 info->error = error; 2414 #endif 2415 return(0); 2416 } 2417 2418 static 2419 int 2420 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp) 2421 { 2422 return (0); 2423 } 2424 2425 static 2426 int 2427 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 2428 struct fid *fhp, struct vnode **vpp) 2429 { 2430 return (0); 2431 } 2432 2433 static 2434 int 2435 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 2436 int *exflagsp, struct ucred **credanonp) 2437 { 2438 return (0); 2439 } 2440 2441 /* 2442 * Support code for hammer2_mount(). Read, verify, and install the volume 2443 * header into the HMP 2444 * 2445 * XXX read four volhdrs and use the one with the highest TID whos CRC 2446 * matches. 2447 * 2448 * XXX check iCRCs. 2449 * 2450 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to 2451 * nonexistant locations. 2452 * 2453 * XXX Record selected volhdr and ring updates to each of 4 volhdrs 2454 */ 2455 static 2456 int 2457 hammer2_install_volume_header(hammer2_mount_t *hmp) 2458 { 2459 hammer2_volume_data_t *vd; 2460 struct buf *bp; 2461 hammer2_crc32_t crc0, crc, bcrc0, bcrc; 2462 int error_reported; 2463 int error; 2464 int valid; 2465 int i; 2466 2467 error_reported = 0; 2468 error = 0; 2469 valid = 0; 2470 bp = NULL; 2471 2472 /* 2473 * There are up to 4 copies of the volume header (syncs iterate 2474 * between them so there is no single master). We don't trust the 2475 * volu_size field so we don't know precisely how large the filesystem 2476 * is, so depend on the OS to return an error if we go beyond the 2477 * block device's EOF. 2478 */ 2479 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) { 2480 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64, 2481 HAMMER2_VOLUME_BYTES, &bp); 2482 if (error) { 2483 brelse(bp); 2484 bp = NULL; 2485 continue; 2486 } 2487 2488 vd = (struct hammer2_volume_data *) bp->b_data; 2489 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) && 2490 (vd->magic != HAMMER2_VOLUME_ID_ABO)) { 2491 brelse(bp); 2492 bp = NULL; 2493 continue; 2494 } 2495 2496 if (vd->magic == HAMMER2_VOLUME_ID_ABO) { 2497 /* XXX: Reversed-endianness filesystem */ 2498 kprintf("hammer2: reverse-endian filesystem detected"); 2499 brelse(bp); 2500 bp = NULL; 2501 continue; 2502 } 2503 2504 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0]; 2505 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF, 2506 HAMMER2_VOLUME_ICRC0_SIZE); 2507 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1]; 2508 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF, 2509 HAMMER2_VOLUME_ICRC1_SIZE); 2510 if ((crc0 != crc) || (bcrc0 != bcrc)) { 2511 kprintf("hammer2 volume header crc " 2512 "mismatch copy #%d %08x/%08x\n", 2513 i, crc0, crc); 2514 error_reported = 1; 2515 brelse(bp); 2516 bp = NULL; 2517 continue; 2518 } 2519 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) { 2520 valid = 1; 2521 hmp->voldata = *vd; 2522 hmp->volhdrno = i; 2523 } 2524 brelse(bp); 2525 bp = NULL; 2526 } 2527 if (valid) { 2528 hmp->volsync = hmp->voldata; 2529 error = 0; 2530 if (error_reported || bootverbose || 1) { /* 1/DEBUG */ 2531 kprintf("hammer2: using volume header #%d\n", 2532 hmp->volhdrno); 2533 } 2534 } else { 2535 error = EINVAL; 2536 kprintf("hammer2: no valid volume headers found!\n"); 2537 } 2538 return (error); 2539 } 2540 2541 /* 2542 * Reconnect using the passed file pointer. The caller must ref the 2543 * fp for us. 2544 */ 2545 void 2546 hammer2_cluster_reconnect(hammer2_mount_t *hmp, struct file *fp) 2547 { 2548 size_t name_len; 2549 const char *name = "disk-volume"; 2550 2551 /* 2552 * Closes old comm descriptor, kills threads, cleans up 2553 * states, then installs the new descriptor and creates 2554 * new threads. 2555 */ 2556 kdmsg_iocom_reconnect(&hmp->iocom, fp, "hammer2"); 2557 2558 /* 2559 * Setup LNK_CONN fields for autoinitiated state machine. We 2560 * will use SPANs to advertise multiple PFSs so only pass the 2561 * fsid and HAMMER2_PFSTYPE_SUPROOT for the AUTOCONN. 2562 * 2563 * We are not initiating a LNK_SPAN so we do not have to set-up 2564 * iocom.auto_lnk_span. 2565 */ 2566 bzero(&hmp->iocom.auto_lnk_conn.pfs_clid, 2567 sizeof(hmp->iocom.auto_lnk_conn.pfs_clid)); 2568 hmp->iocom.auto_lnk_conn.pfs_fsid = hmp->voldata.fsid; 2569 hmp->iocom.auto_lnk_conn.pfs_type = HAMMER2_PFSTYPE_SUPROOT; 2570 hmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1; 2571 #if 0 2572 hmp->iocom.auto_lnk_conn.peer_type = hmp->voldata.peer_type; 2573 #endif 2574 hmp->iocom.auto_lnk_conn.peer_type = DMSG_PEER_HAMMER2; 2575 2576 /* 2577 * Filter adjustment. Clients do not need visibility into other 2578 * clients (otherwise millions of clients would present a serious 2579 * problem). The fs_label also serves to restrict the namespace. 2580 */ 2581 hmp->iocom.auto_lnk_conn.peer_mask = 1LLU << DMSG_PEER_HAMMER2; 2582 hmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1; 2583 2584 #if 0 2585 switch (ipdata->pfs_type) { 2586 case DMSG_PFSTYPE_CLIENT: 2587 hmp->iocom.auto_lnk_conn.peer_mask &= 2588 ~(1LLU << DMSG_PFSTYPE_CLIENT); 2589 break; 2590 default: 2591 break; 2592 } 2593 #endif 2594 2595 name_len = strlen(name); 2596 if (name_len >= sizeof(hmp->iocom.auto_lnk_conn.fs_label)) 2597 name_len = sizeof(hmp->iocom.auto_lnk_conn.fs_label) - 1; 2598 bcopy(name, hmp->iocom.auto_lnk_conn.fs_label, name_len); 2599 hmp->iocom.auto_lnk_conn.fs_label[name_len] = 0; 2600 2601 kdmsg_iocom_autoinitiate(&hmp->iocom, hammer2_autodmsg); 2602 } 2603 2604 static int 2605 hammer2_rcvdmsg(kdmsg_msg_t *msg) 2606 { 2607 kprintf("RCVMSG %08x\n", msg->tcmd); 2608 2609 switch(msg->tcmd) { 2610 case DMSG_DBG_SHELL: 2611 /* 2612 * (non-transaction) 2613 * Execute shell command (not supported atm) 2614 */ 2615 kdmsg_msg_result(msg, DMSG_ERR_NOSUPP); 2616 break; 2617 case DMSG_DBG_SHELL | DMSGF_REPLY: 2618 /* 2619 * (non-transaction) 2620 */ 2621 if (msg->aux_data) { 2622 msg->aux_data[msg->aux_size - 1] = 0; 2623 kprintf("HAMMER2 DBG: %s\n", msg->aux_data); 2624 } 2625 break; 2626 default: 2627 /* 2628 * Unsupported message received. We only need to 2629 * reply if it's a transaction in order to close our end. 2630 * Ignore any one-way messages or any further messages 2631 * associated with the transaction. 2632 * 2633 * NOTE: This case also includes DMSG_LNK_ERROR messages 2634 * which might be one-way, replying to those would 2635 * cause an infinite ping-pong. 2636 */ 2637 if (msg->any.head.cmd & DMSGF_CREATE) 2638 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 2639 break; 2640 } 2641 return(0); 2642 } 2643 2644 /* 2645 * This function is called after KDMSG has automatically handled processing 2646 * of a LNK layer message (typically CONN, SPAN, or CIRC). 2647 * 2648 * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which 2649 * advertises all available hammer2 super-root volumes. 2650 */ 2651 static void hammer2_update_spans(hammer2_mount_t *hmp, kdmsg_state_t *state); 2652 2653 static void 2654 hammer2_autodmsg(kdmsg_msg_t *msg) 2655 { 2656 hammer2_mount_t *hmp = msg->state->iocom->handle; 2657 int copyid; 2658 2659 kprintf("RCAMSG %08x\n", msg->tcmd); 2660 2661 switch(msg->tcmd) { 2662 case DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY: 2663 case DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_DELETE | DMSGF_REPLY: 2664 if (msg->any.head.cmd & DMSGF_CREATE) { 2665 kprintf("HAMMER2: VOLDATA DUMP\n"); 2666 2667 /* 2668 * Dump the configuration stored in the volume header. 2669 * This will typically be import/export access rights, 2670 * master encryption keys (encrypted), etc. 2671 */ 2672 hammer2_voldata_lock(hmp); 2673 copyid = 0; 2674 while (copyid < HAMMER2_COPYID_COUNT) { 2675 if (hmp->voldata.copyinfo[copyid].copyid) 2676 hammer2_volconf_update(hmp, copyid); 2677 ++copyid; 2678 } 2679 hammer2_voldata_unlock(hmp); 2680 2681 kprintf("HAMMER2: INITIATE SPANs\n"); 2682 hammer2_update_spans(hmp, msg->state); 2683 } 2684 if ((msg->any.head.cmd & DMSGF_DELETE) && 2685 msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) { 2686 kprintf("HAMMER2: CONN WAS TERMINATED\n"); 2687 } 2688 break; 2689 default: 2690 break; 2691 } 2692 } 2693 2694 /* 2695 * Update LNK_SPAN state 2696 */ 2697 static void 2698 hammer2_update_spans(hammer2_mount_t *hmp, kdmsg_state_t *state) 2699 { 2700 const hammer2_inode_data_t *ripdata; 2701 hammer2_cluster_t *cparent; 2702 hammer2_cluster_t *cluster; 2703 hammer2_pfsmount_t *spmp; 2704 hammer2_key_t key_next; 2705 kdmsg_msg_t *rmsg; 2706 size_t name_len; 2707 int ddflag; 2708 2709 /* 2710 * Lookup mount point under the media-localized super-root. 2711 * 2712 * cluster->pmp will incorrectly point to spmp and must be fixed 2713 * up later on. 2714 */ 2715 spmp = hmp->spmp; 2716 cparent = hammer2_inode_lock_ex(spmp->iroot); 2717 cluster = hammer2_cluster_lookup(cparent, &key_next, 2718 HAMMER2_KEY_MIN, 2719 HAMMER2_KEY_MAX, 2720 0, &ddflag); 2721 while (cluster) { 2722 if (hammer2_cluster_type(cluster) != HAMMER2_BREF_TYPE_INODE) 2723 continue; 2724 ripdata = &hammer2_cluster_rdata(cluster)->ipdata; 2725 kprintf("UPDATE SPANS: %s\n", ripdata->filename); 2726 2727 rmsg = kdmsg_msg_alloc(state, DMSG_LNK_SPAN | DMSGF_CREATE, 2728 hammer2_lnk_span_reply, NULL); 2729 rmsg->any.lnk_span.pfs_clid = ripdata->pfs_clid; 2730 rmsg->any.lnk_span.pfs_fsid = ripdata->pfs_fsid; 2731 rmsg->any.lnk_span.pfs_type = ripdata->pfs_type; 2732 rmsg->any.lnk_span.peer_type = DMSG_PEER_HAMMER2; 2733 rmsg->any.lnk_span.proto_version = DMSG_SPAN_PROTO_1; 2734 name_len = ripdata->name_len; 2735 if (name_len >= sizeof(rmsg->any.lnk_span.fs_label)) 2736 name_len = sizeof(rmsg->any.lnk_span.fs_label) - 1; 2737 bcopy(ripdata->filename, rmsg->any.lnk_span.fs_label, name_len); 2738 2739 kdmsg_msg_write(rmsg); 2740 2741 cluster = hammer2_cluster_next(cparent, cluster, 2742 &key_next, 2743 key_next, 2744 HAMMER2_KEY_MAX, 2745 0); 2746 } 2747 hammer2_inode_unlock_ex(spmp->iroot, cparent); 2748 } 2749 2750 static 2751 int 2752 hammer2_lnk_span_reply(kdmsg_state_t *state, kdmsg_msg_t *msg) 2753 { 2754 if ((state->txcmd & DMSGF_DELETE) == 0 && 2755 (msg->any.head.cmd & DMSGF_DELETE)) { 2756 kdmsg_msg_reply(msg, 0); 2757 } 2758 return 0; 2759 } 2760 2761 /* 2762 * Volume configuration updates are passed onto the userland service 2763 * daemon via the open LNK_CONN transaction. 2764 */ 2765 void 2766 hammer2_volconf_update(hammer2_mount_t *hmp, int index) 2767 { 2768 kdmsg_msg_t *msg; 2769 2770 /* XXX interlock against connection state termination */ 2771 kprintf("volconf update %p\n", hmp->iocom.conn_state); 2772 if (hmp->iocom.conn_state) { 2773 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n"); 2774 msg = kdmsg_msg_alloc(hmp->iocom.conn_state, 2775 DMSG_LNK_HAMMER2_VOLCONF, 2776 NULL, NULL); 2777 H2_LNK_VOLCONF(msg)->copy = hmp->voldata.copyinfo[index]; 2778 H2_LNK_VOLCONF(msg)->mediaid = hmp->voldata.fsid; 2779 H2_LNK_VOLCONF(msg)->index = index; 2780 kdmsg_msg_write(msg); 2781 } 2782 } 2783 2784 /* 2785 * This handles hysteresis on regular file flushes. Because the BIOs are 2786 * routed to a thread it is possible for an excessive number to build up 2787 * and cause long front-end stalls long before the runningbuffspace limit 2788 * is hit, so we implement hammer2_flush_pipe to control the 2789 * hysteresis. 2790 * 2791 * This is a particular problem when compression is used. 2792 */ 2793 void 2794 hammer2_lwinprog_ref(hammer2_pfsmount_t *pmp) 2795 { 2796 atomic_add_int(&pmp->count_lwinprog, 1); 2797 } 2798 2799 void 2800 hammer2_lwinprog_drop(hammer2_pfsmount_t *pmp) 2801 { 2802 int lwinprog; 2803 2804 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1); 2805 if ((lwinprog & HAMMER2_LWINPROG_WAITING) && 2806 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) { 2807 atomic_clear_int(&pmp->count_lwinprog, 2808 HAMMER2_LWINPROG_WAITING); 2809 wakeup(&pmp->count_lwinprog); 2810 } 2811 } 2812 2813 void 2814 hammer2_lwinprog_wait(hammer2_pfsmount_t *pmp) 2815 { 2816 int lwinprog; 2817 2818 for (;;) { 2819 lwinprog = pmp->count_lwinprog; 2820 cpu_ccfence(); 2821 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe) 2822 break; 2823 tsleep_interlock(&pmp->count_lwinprog, 0); 2824 atomic_set_int(&pmp->count_lwinprog, HAMMER2_LWINPROG_WAITING); 2825 lwinprog = pmp->count_lwinprog; 2826 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe) 2827 break; 2828 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz); 2829 } 2830 } 2831 2832 /* 2833 * Manage excessive memory resource use for chain and related 2834 * structures. 2835 */ 2836 void 2837 hammer2_pfs_memory_wait(hammer2_pfsmount_t *pmp) 2838 { 2839 uint32_t waiting; 2840 uint32_t count; 2841 uint32_t limit; 2842 #if 0 2843 static int zzticks; 2844 #endif 2845 2846 /* 2847 * Atomic check condition and wait. Also do an early speedup of 2848 * the syncer to try to avoid hitting the wait. 2849 */ 2850 for (;;) { 2851 waiting = pmp->inmem_dirty_chains; 2852 cpu_ccfence(); 2853 count = waiting & HAMMER2_DIRTYCHAIN_MASK; 2854 2855 limit = pmp->mp->mnt_nvnodelistsize / 10; 2856 if (limit < hammer2_limit_dirty_chains) 2857 limit = hammer2_limit_dirty_chains; 2858 if (limit < 1000) 2859 limit = 1000; 2860 2861 #if 0 2862 if ((int)(ticks - zzticks) > hz) { 2863 zzticks = ticks; 2864 kprintf("count %ld %ld\n", count, limit); 2865 } 2866 #endif 2867 2868 /* 2869 * Block if there are too many dirty chains present, wait 2870 * for the flush to clean some out. 2871 */ 2872 if (count > limit) { 2873 tsleep_interlock(&pmp->inmem_dirty_chains, 0); 2874 if (atomic_cmpset_int(&pmp->inmem_dirty_chains, 2875 waiting, 2876 waiting | HAMMER2_DIRTYCHAIN_WAITING)) { 2877 speedup_syncer(pmp->mp); 2878 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED, 2879 "chnmem", hz); 2880 } 2881 continue; /* loop on success or fail */ 2882 } 2883 2884 /* 2885 * Try to start an early flush before we are forced to block. 2886 */ 2887 if (count > limit * 7 / 10) 2888 speedup_syncer(pmp->mp); 2889 break; 2890 } 2891 } 2892 2893 void 2894 hammer2_pfs_memory_inc(hammer2_pfsmount_t *pmp) 2895 { 2896 if (pmp) { 2897 atomic_add_int(&pmp->inmem_dirty_chains, 1); 2898 } 2899 } 2900 2901 void 2902 hammer2_pfs_memory_wakeup(hammer2_pfsmount_t *pmp) 2903 { 2904 uint32_t waiting; 2905 2906 if (pmp == NULL) 2907 return; 2908 2909 for (;;) { 2910 waiting = pmp->inmem_dirty_chains; 2911 cpu_ccfence(); 2912 if (atomic_cmpset_int(&pmp->inmem_dirty_chains, 2913 waiting, 2914 (waiting - 1) & 2915 ~HAMMER2_DIRTYCHAIN_WAITING)) { 2916 break; 2917 } 2918 } 2919 2920 if (waiting & HAMMER2_DIRTYCHAIN_WAITING) 2921 wakeup(&pmp->inmem_dirty_chains); 2922 } 2923 2924 /* 2925 * Debugging 2926 */ 2927 void 2928 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx) 2929 { 2930 hammer2_chain_t *scan; 2931 hammer2_chain_t *parent; 2932 2933 --*countp; 2934 if (*countp == 0) { 2935 kprintf("%*.*s...\n", tab, tab, ""); 2936 return; 2937 } 2938 if (*countp < 0) 2939 return; 2940 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n", 2941 tab, tab, "", pfx, 2942 chain, chain->bref.type, 2943 chain->bref.key, chain->bref.keybits, 2944 chain->bref.mirror_tid); 2945 2946 kprintf("%*.*s [%08x] (%s) refs=%d\n", 2947 tab, tab, "", 2948 chain->flags, 2949 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE && 2950 chain->data) ? (char *)chain->data->ipdata.filename : "?"), 2951 chain->refs); 2952 2953 kprintf("%*.*s core [%08x]", 2954 tab, tab, "", 2955 chain->core.flags); 2956 2957 parent = chain->parent; 2958 if (parent) 2959 kprintf("\n%*.*s p=%p [pflags %08x prefs %d", 2960 tab, tab, "", 2961 parent, parent->flags, parent->refs); 2962 if (RB_EMPTY(&chain->core.rbtree)) { 2963 kprintf("\n"); 2964 } else { 2965 kprintf(" {\n"); 2966 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree) 2967 hammer2_dump_chain(scan, tab + 4, countp, 'a'); 2968 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data) 2969 kprintf("%*.*s}(%s)\n", tab, tab, "", 2970 chain->data->ipdata.filename); 2971 else 2972 kprintf("%*.*s}\n", tab, tab, ""); 2973 } 2974 } 2975