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