1 /* 2 * Copyright (c) 2007-2008 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 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/nlookup.h> 36 #include <sys/fcntl.h> 37 #include <sys/sysctl.h> 38 39 #include "hammer.h" 40 41 /* 42 * NOTE! Global statistics may not be MPSAFE so HAMMER never uses them 43 * in conditionals. 44 */ 45 int hammer_supported_version = HAMMER_VOL_VERSION_DEFAULT; 46 int hammer_debug_io; 47 int hammer_debug_general; 48 int hammer_debug_debug = 1; /* medium-error panics */ 49 int hammer_debug_inode; 50 int hammer_debug_locks; 51 int hammer_debug_btree; 52 int hammer_debug_tid; 53 int hammer_debug_recover; /* -1 will disable, +1 will force */ 54 int hammer_debug_recover_faults; 55 int hammer_debug_critical; /* non-zero enter debugger on error */ 56 int hammer_cluster_enable = 1; /* enable read clustering by default */ 57 int hammer_live_dedup = 0; 58 int hammer_tdmux_ticks; 59 int hammer_count_fsyncs; 60 int hammer_count_inodes; 61 int hammer_count_iqueued; 62 int hammer_count_reclaims; 63 int hammer_count_records; 64 int hammer_count_record_datas; 65 int hammer_count_volumes; 66 int hammer_count_buffers; 67 int hammer_count_nodes; 68 int64_t hammer_count_extra_space_used; 69 int64_t hammer_stats_btree_lookups; 70 int64_t hammer_stats_btree_searches; 71 int64_t hammer_stats_btree_inserts; 72 int64_t hammer_stats_btree_deletes; 73 int64_t hammer_stats_btree_elements; 74 int64_t hammer_stats_btree_splits; 75 int64_t hammer_stats_btree_iterations; 76 int64_t hammer_stats_btree_root_iterations; 77 int64_t hammer_stats_record_iterations; 78 79 int64_t hammer_stats_file_read; 80 int64_t hammer_stats_file_write; 81 int64_t hammer_stats_file_iopsr; 82 int64_t hammer_stats_file_iopsw; 83 int64_t hammer_stats_disk_read; 84 int64_t hammer_stats_disk_write; 85 int64_t hammer_stats_inode_flushes; 86 int64_t hammer_stats_commits; 87 int64_t hammer_stats_undo; 88 int64_t hammer_stats_redo; 89 90 long hammer_count_dirtybufspace; /* global */ 91 int hammer_count_refedbufs; /* global */ 92 int hammer_count_reservations; 93 long hammer_count_io_running_read; 94 long hammer_count_io_running_write; 95 int hammer_count_io_locked; 96 long hammer_limit_dirtybufspace; /* per-mount */ 97 int hammer_limit_recs; /* as a whole XXX */ 98 int hammer_limit_inode_recs = 2048; /* per inode */ 99 int hammer_limit_reclaims; 100 int hammer_live_dedup_cache_size = DEDUP_CACHE_SIZE; 101 int hammer_limit_redo = 4096 * 1024; /* per inode */ 102 int hammer_autoflush = 500; /* auto flush (typ on reclaim) */ 103 int hammer_bio_count; 104 int hammer_verify_zone; 105 int hammer_verify_data = 1; 106 int hammer_write_mode; 107 int hammer_double_buffer; 108 int hammer_btree_full_undo = 1; 109 int hammer_yield_check = 16; 110 int hammer_fsync_mode = 3; 111 int64_t hammer_contention_count; 112 int64_t hammer_zone_limit; 113 114 /* 115 * Live dedup debug counters (sysctls are writable so that counters 116 * can be reset from userspace). 117 */ 118 int64_t hammer_live_dedup_vnode_bcmps = 0; 119 int64_t hammer_live_dedup_device_bcmps = 0; 120 int64_t hammer_live_dedup_findblk_failures = 0; 121 int64_t hammer_live_dedup_bmap_saves = 0; 122 123 124 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem"); 125 126 SYSCTL_INT(_vfs_hammer, OID_AUTO, supported_version, CTLFLAG_RD, 127 &hammer_supported_version, 0, ""); 128 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW, 129 &hammer_debug_general, 0, ""); 130 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW, 131 &hammer_debug_io, 0, ""); 132 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW, 133 &hammer_debug_debug, 0, ""); 134 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW, 135 &hammer_debug_inode, 0, ""); 136 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW, 137 &hammer_debug_locks, 0, ""); 138 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW, 139 &hammer_debug_btree, 0, ""); 140 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW, 141 &hammer_debug_tid, 0, ""); 142 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW, 143 &hammer_debug_recover, 0, ""); 144 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW, 145 &hammer_debug_recover_faults, 0, ""); 146 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_critical, CTLFLAG_RW, 147 &hammer_debug_critical, 0, ""); 148 SYSCTL_INT(_vfs_hammer, OID_AUTO, cluster_enable, CTLFLAG_RW, 149 &hammer_cluster_enable, 0, ""); 150 /* 151 * 0 - live dedup is disabled 152 * 1 - dedup cache is populated on reads only 153 * 2 - dedup cache is populated on both reads and writes 154 * 155 * LIVE_DEDUP IS DISABLED PERMANENTLY! This feature appears to cause 156 * blockmap corruption over time so we've turned it off permanently. 157 */ 158 SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup, CTLFLAG_RD, 159 &hammer_live_dedup, 0, "Enable live dedup (experimental)"); 160 SYSCTL_INT(_vfs_hammer, OID_AUTO, tdmux_ticks, CTLFLAG_RW, 161 &hammer_tdmux_ticks, 0, "Hammer tdmux ticks"); 162 163 SYSCTL_LONG(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW, 164 &hammer_limit_dirtybufspace, 0, ""); 165 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW, 166 &hammer_limit_recs, 0, ""); 167 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_inode_recs, CTLFLAG_RW, 168 &hammer_limit_inode_recs, 0, ""); 169 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_reclaims, CTLFLAG_RW, 170 &hammer_limit_reclaims, 0, ""); 171 SYSCTL_INT(_vfs_hammer, OID_AUTO, live_dedup_cache_size, CTLFLAG_RW, 172 &hammer_live_dedup_cache_size, 0, 173 "Number of cache entries"); 174 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_redo, CTLFLAG_RW, 175 &hammer_limit_redo, 0, ""); 176 177 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_fsyncs, CTLFLAG_RD, 178 &hammer_count_fsyncs, 0, ""); 179 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD, 180 &hammer_count_inodes, 0, ""); 181 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD, 182 &hammer_count_iqueued, 0, ""); 183 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaims, CTLFLAG_RD, 184 &hammer_count_reclaims, 0, ""); 185 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD, 186 &hammer_count_records, 0, ""); 187 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD, 188 &hammer_count_record_datas, 0, ""); 189 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD, 190 &hammer_count_volumes, 0, ""); 191 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD, 192 &hammer_count_buffers, 0, ""); 193 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD, 194 &hammer_count_nodes, 0, ""); 195 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, count_extra_space_used, CTLFLAG_RD, 196 &hammer_count_extra_space_used, 0, ""); 197 198 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD, 199 &hammer_stats_btree_searches, 0, ""); 200 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD, 201 &hammer_stats_btree_lookups, 0, ""); 202 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD, 203 &hammer_stats_btree_inserts, 0, ""); 204 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD, 205 &hammer_stats_btree_deletes, 0, ""); 206 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD, 207 &hammer_stats_btree_elements, 0, ""); 208 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD, 209 &hammer_stats_btree_splits, 0, ""); 210 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD, 211 &hammer_stats_btree_iterations, 0, ""); 212 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_root_iterations, CTLFLAG_RD, 213 &hammer_stats_btree_root_iterations, 0, ""); 214 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD, 215 &hammer_stats_record_iterations, 0, ""); 216 217 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_read, CTLFLAG_RD, 218 &hammer_stats_file_read, 0, ""); 219 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_write, CTLFLAG_RD, 220 &hammer_stats_file_write, 0, ""); 221 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsr, CTLFLAG_RD, 222 &hammer_stats_file_iopsr, 0, ""); 223 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsw, CTLFLAG_RD, 224 &hammer_stats_file_iopsw, 0, ""); 225 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_read, CTLFLAG_RD, 226 &hammer_stats_disk_read, 0, ""); 227 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_write, CTLFLAG_RD, 228 &hammer_stats_disk_write, 0, ""); 229 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_inode_flushes, CTLFLAG_RD, 230 &hammer_stats_inode_flushes, 0, ""); 231 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_commits, CTLFLAG_RD, 232 &hammer_stats_commits, 0, ""); 233 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_undo, CTLFLAG_RD, 234 &hammer_stats_undo, 0, ""); 235 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_redo, CTLFLAG_RD, 236 &hammer_stats_redo, 0, ""); 237 238 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_vnode_bcmps, CTLFLAG_RW, 239 &hammer_live_dedup_vnode_bcmps, 0, 240 "successful vnode buffer comparisons"); 241 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_device_bcmps, CTLFLAG_RW, 242 &hammer_live_dedup_device_bcmps, 0, 243 "successful device buffer comparisons"); 244 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_findblk_failures, CTLFLAG_RW, 245 &hammer_live_dedup_findblk_failures, 0, 246 "block lookup failures for comparison"); 247 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, live_dedup_bmap_saves, CTLFLAG_RW, 248 &hammer_live_dedup_bmap_saves, 0, 249 "useful physical block lookups"); 250 251 SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD, 252 &hammer_count_dirtybufspace, 0, ""); 253 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD, 254 &hammer_count_refedbufs, 0, ""); 255 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD, 256 &hammer_count_reservations, 0, ""); 257 SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD, 258 &hammer_count_io_running_read, 0, ""); 259 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD, 260 &hammer_count_io_locked, 0, ""); 261 SYSCTL_LONG(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD, 262 &hammer_count_io_running_write, 0, ""); 263 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW, 264 &hammer_zone_limit, 0, ""); 265 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW, 266 &hammer_contention_count, 0, ""); 267 SYSCTL_INT(_vfs_hammer, OID_AUTO, autoflush, CTLFLAG_RW, 268 &hammer_autoflush, 0, ""); 269 SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW, 270 &hammer_verify_zone, 0, ""); 271 SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_data, CTLFLAG_RW, 272 &hammer_verify_data, 0, ""); 273 SYSCTL_INT(_vfs_hammer, OID_AUTO, write_mode, CTLFLAG_RW, 274 &hammer_write_mode, 0, ""); 275 SYSCTL_INT(_vfs_hammer, OID_AUTO, double_buffer, CTLFLAG_RW, 276 &hammer_double_buffer, 0, ""); 277 SYSCTL_INT(_vfs_hammer, OID_AUTO, btree_full_undo, CTLFLAG_RW, 278 &hammer_btree_full_undo, 0, ""); 279 SYSCTL_INT(_vfs_hammer, OID_AUTO, yield_check, CTLFLAG_RW, 280 &hammer_yield_check, 0, ""); 281 SYSCTL_INT(_vfs_hammer, OID_AUTO, fsync_mode, CTLFLAG_RW, 282 &hammer_fsync_mode, 0, ""); 283 284 /* KTR_INFO_MASTER(hammer); */ 285 286 /* 287 * VFS ABI 288 */ 289 static void hammer_free_hmp(struct mount *mp); 290 291 static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data, 292 struct ucred *cred); 293 static int hammer_vfs_unmount(struct mount *mp, int mntflags); 294 static int hammer_vfs_root(struct mount *mp, struct vnode **vpp); 295 static int hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, 296 struct ucred *cred); 297 static int hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, 298 struct ucred *cred); 299 static int hammer_vfs_sync(struct mount *mp, int waitfor); 300 static int hammer_vfs_vget(struct mount *mp, struct vnode *dvp, 301 ino_t ino, struct vnode **vpp); 302 static int hammer_vfs_init(struct vfsconf *conf); 303 static int hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 304 struct fid *fhp, struct vnode **vpp); 305 static int hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp); 306 static int hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 307 int *exflagsp, struct ucred **credanonp); 308 309 310 static struct vfsops hammer_vfsops = { 311 .vfs_mount = hammer_vfs_mount, 312 .vfs_unmount = hammer_vfs_unmount, 313 .vfs_root = hammer_vfs_root, 314 .vfs_statfs = hammer_vfs_statfs, 315 .vfs_statvfs = hammer_vfs_statvfs, 316 .vfs_sync = hammer_vfs_sync, 317 .vfs_vget = hammer_vfs_vget, 318 .vfs_init = hammer_vfs_init, 319 .vfs_vptofh = hammer_vfs_vptofh, 320 .vfs_fhtovp = hammer_vfs_fhtovp, 321 .vfs_checkexp = hammer_vfs_checkexp 322 }; 323 324 MALLOC_DEFINE(M_HAMMER, "HAMMER-mount", ""); 325 326 VFS_SET(hammer_vfsops, hammer, 0); 327 MODULE_VERSION(hammer, 1); 328 329 static int 330 hammer_vfs_init(struct vfsconf *conf) 331 { 332 long n; 333 334 /* 335 * Wait up to this long for an exclusive deadlock to clear 336 * before acquiring a new shared lock on the ip. The deadlock 337 * may have occured on a b-tree node related to the ip. 338 */ 339 if (hammer_tdmux_ticks == 0) 340 hammer_tdmux_ticks = hz / 5; 341 342 /* 343 * Autosize, but be careful because a hammer filesystem's 344 * reserve is partially calculated based on dirtybufspace, 345 * so we simply cannot allow it to get too large. 346 */ 347 if (hammer_limit_recs == 0) { 348 n = nbuf * 25; 349 if (n > kmalloc_limit(M_HAMMER) / 512) 350 n = kmalloc_limit(M_HAMMER) / 512; 351 if (n > 2 * 1024 * 1024) 352 n = 2 * 1024 * 1024; 353 hammer_limit_recs = (int)n; 354 } 355 if (hammer_limit_dirtybufspace == 0) { 356 hammer_limit_dirtybufspace = hidirtybufspace / 2; 357 if (hammer_limit_dirtybufspace < 1L * 1024 * 1024) 358 hammer_limit_dirtybufspace = 1024L * 1024; 359 if (hammer_limit_dirtybufspace > 1024L * 1024 * 1024) 360 hammer_limit_dirtybufspace = 1024L * 1024 * 1024; 361 } 362 363 /* 364 * The hammer_inode structure detaches from the vnode on reclaim. 365 * This limits the number of inodes in this state to prevent a 366 * memory pool blowout. 367 */ 368 if (hammer_limit_reclaims == 0) 369 hammer_limit_reclaims = desiredvnodes / 10; 370 371 return(0); 372 } 373 374 static int 375 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data, 376 struct ucred *cred) 377 { 378 struct hammer_mount_info info; 379 hammer_mount_t hmp; 380 hammer_volume_t rootvol; 381 struct vnode *rootvp; 382 struct vnode *devvp = NULL; 383 const char *upath; /* volume name in userspace */ 384 char *path; /* volume name in system space */ 385 int error; 386 int i; 387 int master_id; 388 int nvolumes; 389 char *next_volume_ptr = NULL; 390 391 /* 392 * Accept hammer_mount_info. mntpt is NULL for root mounts at boot. 393 */ 394 if (mntpt == NULL) { 395 bzero(&info, sizeof(info)); 396 info.asof = 0; 397 info.hflags = 0; 398 info.nvolumes = 1; 399 400 next_volume_ptr = mp->mnt_stat.f_mntfromname; 401 402 /* Count number of volumes separated by ':' */ 403 for (char *p = next_volume_ptr; *p != '\0'; ++p) { 404 if (*p == ':') { 405 ++info.nvolumes; 406 } 407 } 408 409 mp->mnt_flag &= ~MNT_RDONLY; /* mount R/W */ 410 } else { 411 if ((error = copyin(data, &info, sizeof(info))) != 0) 412 return (error); 413 } 414 415 /* 416 * updating or new mount 417 */ 418 if (mp->mnt_flag & MNT_UPDATE) { 419 hmp = (void *)mp->mnt_data; 420 KKASSERT(hmp != NULL); 421 } else { 422 if (info.nvolumes <= 0 || info.nvolumes > HAMMER_MAX_VOLUMES) 423 return (EINVAL); 424 hmp = NULL; 425 } 426 427 /* 428 * master-id validation. The master id may not be changed by a 429 * mount update. 430 */ 431 if (info.hflags & HMNT_MASTERID) { 432 if (hmp && hmp->master_id != info.master_id) { 433 kprintf("HAMMER: cannot change master id " 434 "with mount update\n"); 435 return(EINVAL); 436 } 437 master_id = info.master_id; 438 if (master_id < -1 || master_id >= HAMMER_MAX_MASTERS) 439 return (EINVAL); 440 } else { 441 if (hmp) 442 master_id = hmp->master_id; 443 else 444 master_id = 0; 445 } 446 447 /* 448 * Internal mount data structure 449 */ 450 if (hmp == NULL) { 451 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO); 452 mp->mnt_data = (qaddr_t)hmp; 453 hmp->mp = mp; 454 455 /* 456 * Make sure kmalloc type limits are set appropriately. 457 * 458 * Our inode kmalloc group is sized based on maxvnodes 459 * (controlled by the system, not us). 460 */ 461 kmalloc_create(&hmp->m_misc, "HAMMER-others"); 462 kmalloc_create(&hmp->m_inodes, "HAMMER-inodes"); 463 464 kmalloc_raise_limit(hmp->m_inodes, 0); /* unlimited */ 465 466 hmp->root_btree_beg.localization = 0x00000000U; 467 hmp->root_btree_beg.obj_id = -0x8000000000000000LL; 468 hmp->root_btree_beg.key = -0x8000000000000000LL; 469 hmp->root_btree_beg.create_tid = 1; 470 hmp->root_btree_beg.delete_tid = 1; 471 hmp->root_btree_beg.rec_type = 0; 472 hmp->root_btree_beg.obj_type = 0; 473 hmp->root_btree_beg.btype = HAMMER_BTREE_TYPE_NONE; 474 475 hmp->root_btree_end.localization = 0xFFFFFFFFU; 476 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL; 477 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL; 478 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL; 479 hmp->root_btree_end.delete_tid = 0; /* special case */ 480 hmp->root_btree_end.rec_type = 0xFFFFU; 481 hmp->root_btree_end.obj_type = 0; 482 hmp->root_btree_end.btype = HAMMER_BTREE_TYPE_NONE; 483 484 hmp->krate.freq = 1; /* maximum reporting rate (hz) */ 485 hmp->krate.count = -16; /* initial burst */ 486 hmp->kdiag.freq = 1; /* maximum reporting rate (hz) */ 487 hmp->kdiag.count = -16; /* initial burst */ 488 489 hmp->sync_lock.refs = 1; 490 hmp->free_lock.refs = 1; 491 hmp->undo_lock.refs = 1; 492 hmp->blkmap_lock.refs = 1; 493 hmp->snapshot_lock.refs = 1; 494 hmp->volume_lock.refs = 1; 495 496 TAILQ_INIT(&hmp->delay_list); 497 TAILQ_INIT(&hmp->flush_group_list); 498 TAILQ_INIT(&hmp->objid_cache_list); 499 TAILQ_INIT(&hmp->undo_lru_list); 500 TAILQ_INIT(&hmp->reclaim_list); 501 502 RB_INIT(&hmp->rb_dedup_crc_root); 503 RB_INIT(&hmp->rb_dedup_off_root); 504 TAILQ_INIT(&hmp->dedup_lru_list); 505 } 506 hmp->hflags &= ~HMNT_USERFLAGS; 507 hmp->hflags |= info.hflags & HMNT_USERFLAGS; 508 509 hmp->master_id = master_id; 510 511 if (info.asof) { 512 mp->mnt_flag |= MNT_RDONLY; 513 hmp->asof = info.asof; 514 } else { 515 hmp->asof = HAMMER_MAX_TID; 516 } 517 518 hmp->volume_to_remove = -1; 519 520 /* 521 * Re-open read-write if originally read-only, or vise-versa. 522 * 523 * When going from read-only to read-write execute the stage2 524 * recovery if it has not already been run. 525 */ 526 if (mp->mnt_flag & MNT_UPDATE) { 527 lwkt_gettoken(&hmp->fs_token); 528 error = 0; 529 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 530 kprintf("HAMMER: read-only -> read-write\n"); 531 hmp->ronly = 0; 532 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 533 hammer_adjust_volume_mode, NULL); 534 rootvol = hammer_get_root_volume(hmp, &error); 535 if (rootvol) { 536 hammer_recover_flush_buffers(hmp, rootvol, 1); 537 error = hammer_recover_stage2(hmp, rootvol); 538 bcopy(rootvol->ondisk->vol0_blockmap, 539 hmp->blockmap, 540 sizeof(hmp->blockmap)); 541 hammer_rel_volume(rootvol, 0); 542 } 543 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 544 hammer_reload_inode, NULL); 545 /* kernel clears MNT_RDONLY */ 546 } else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { 547 kprintf("HAMMER: read-write -> read-only\n"); 548 hmp->ronly = 1; /* messy */ 549 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 550 hammer_reload_inode, NULL); 551 hmp->ronly = 0; 552 hammer_flusher_sync(hmp); 553 hammer_flusher_sync(hmp); 554 hammer_flusher_sync(hmp); 555 hmp->ronly = 1; 556 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 557 hammer_adjust_volume_mode, NULL); 558 } 559 lwkt_reltoken(&hmp->fs_token); 560 return(error); 561 } 562 563 RB_INIT(&hmp->rb_vols_root); 564 RB_INIT(&hmp->rb_inos_root); 565 RB_INIT(&hmp->rb_redo_root); 566 RB_INIT(&hmp->rb_nods_root); 567 RB_INIT(&hmp->rb_undo_root); 568 RB_INIT(&hmp->rb_resv_root); 569 RB_INIT(&hmp->rb_bufs_root); 570 RB_INIT(&hmp->rb_pfsm_root); 571 572 hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0); 573 574 RB_INIT(&hmp->volu_root); 575 RB_INIT(&hmp->undo_root); 576 RB_INIT(&hmp->data_root); 577 RB_INIT(&hmp->meta_root); 578 RB_INIT(&hmp->lose_root); 579 TAILQ_INIT(&hmp->iorun_list); 580 581 lwkt_token_init(&hmp->fs_token, "hammerfs"); 582 lwkt_token_init(&hmp->io_token, "hammerio"); 583 584 lwkt_gettoken(&hmp->fs_token); 585 586 /* 587 * Load volumes 588 */ 589 path = objcache_get(namei_oc, M_WAITOK); 590 hmp->nvolumes = -1; 591 for (i = 0; i < info.nvolumes; ++i) { 592 if (mntpt == NULL) { 593 /* 594 * Root mount. 595 */ 596 KKASSERT(next_volume_ptr != NULL); 597 strcpy(path, ""); 598 if (*next_volume_ptr != '/') { 599 /* relative path */ 600 strcpy(path, "/dev/"); 601 } 602 int k; 603 for (k = strlen(path); k < MAXPATHLEN-1; ++k) { 604 if (*next_volume_ptr == '\0') { 605 break; 606 } else if (*next_volume_ptr == ':') { 607 ++next_volume_ptr; 608 break; 609 } else { 610 path[k] = *next_volume_ptr; 611 ++next_volume_ptr; 612 } 613 } 614 path[k] = '\0'; 615 616 error = 0; 617 cdev_t dev = kgetdiskbyname(path); 618 error = bdevvp(dev, &devvp); 619 if (error) { 620 kprintf("hammer_mount: can't find devvp\n"); 621 } 622 } else { 623 error = copyin(&info.volumes[i], &upath, 624 sizeof(char *)); 625 if (error == 0) 626 error = copyinstr(upath, path, 627 MAXPATHLEN, NULL); 628 } 629 if (error == 0) 630 error = hammer_install_volume(hmp, path, devvp, NULL); 631 if (error) 632 break; 633 } 634 objcache_put(namei_oc, path); 635 636 /* 637 * Make sure we found a root volume 638 */ 639 if (hmp->rootvol == NULL) { 640 kprintf("hammer_mount: No root volume found!\n"); 641 error = EINVAL; 642 goto failed; 643 } 644 645 /* 646 * Check that all required volumes are available 647 */ 648 if (error == 0 && hammer_mountcheck_volumes(hmp)) { 649 kprintf("hammer_mount: Missing volumes, cannot mount!\n"); 650 error = EINVAL; 651 goto failed; 652 } 653 654 /* 655 * Other errors 656 */ 657 if (error) { 658 kprintf("hammer_mount: Failed to load volumes!\n"); 659 goto failed; 660 } 661 662 nvolumes = hammer_get_installed_volumes(hmp); 663 if (hmp->nvolumes != nvolumes) { 664 kprintf("hammer_mount: volume header says %d volumes, " 665 "but %d installed\n", 666 hmp->nvolumes, nvolumes); 667 error = EINVAL; 668 goto failed; 669 } 670 671 /* 672 * No errors, setup enough of the mount point so we can lookup the 673 * root vnode. 674 */ 675 mp->mnt_iosize_max = MAXPHYS; 676 mp->mnt_kern_flag |= MNTK_FSMID; 677 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */ 678 679 /* 680 * MPSAFE code. Note that VOPs and VFSops which are not MPSAFE 681 * will acquire a per-mount token prior to entry and release it 682 * on return. 683 */ 684 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; 685 686 /* 687 * note: f_iosize is used by vnode_pager_haspage() when constructing 688 * its VOP_BMAP call. 689 */ 690 mp->mnt_stat.f_iosize = HAMMER_BUFSIZE; 691 mp->mnt_stat.f_bsize = HAMMER_BUFSIZE; 692 693 mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE; 694 mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE; 695 696 mp->mnt_maxsymlinklen = 255; 697 mp->mnt_flag |= MNT_LOCAL; 698 699 vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops); 700 vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops); 701 vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops); 702 703 /* 704 * The root volume's ondisk pointer is only valid if we hold a 705 * reference to it. 706 */ 707 rootvol = hammer_get_root_volume(hmp, &error); 708 if (error) 709 goto failed; 710 711 /* 712 * Perform any necessary UNDO operations. The recovery code does 713 * call hammer_undo_lookup() so we have to pre-cache the blockmap, 714 * and then re-copy it again after recovery is complete. 715 * 716 * If this is a read-only mount the UNDO information is retained 717 * in memory in the form of dirty buffer cache buffers, and not 718 * written back to the media. 719 */ 720 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap, 721 sizeof(hmp->blockmap)); 722 723 /* 724 * Check filesystem version 725 */ 726 hmp->version = rootvol->ondisk->vol_version; 727 if (hmp->version < HAMMER_VOL_VERSION_MIN || 728 hmp->version > HAMMER_VOL_VERSION_MAX) { 729 kprintf("HAMMER: mount unsupported fs version %d\n", 730 hmp->version); 731 error = ERANGE; 732 goto done; 733 } 734 735 /* 736 * The undo_rec_limit limits the size of flush groups to avoid 737 * blowing out the UNDO FIFO. This calculation is typically in 738 * the tens of thousands and is designed primarily when small 739 * HAMMER filesystems are created. 740 */ 741 hmp->undo_rec_limit = hammer_undo_max(hmp) / 8192 + 100; 742 if (hammer_debug_general & 0x0001) 743 kprintf("HAMMER: undo_rec_limit %d\n", hmp->undo_rec_limit); 744 745 /* 746 * NOTE: Recover stage1 not only handles meta-data recovery, it 747 * also sets hmp->undo_seqno for HAMMER VERSION 4+ filesystems. 748 */ 749 error = hammer_recover_stage1(hmp, rootvol); 750 if (error) { 751 kprintf("Failed to recover HAMMER filesystem on mount\n"); 752 goto done; 753 } 754 755 /* 756 * Finish setup now that we have a good root volume. 757 * 758 * The top 16 bits of fsid.val[1] is a pfs id. 759 */ 760 ksnprintf(mp->mnt_stat.f_mntfromname, 761 sizeof(mp->mnt_stat.f_mntfromname), "%s", 762 rootvol->ondisk->vol_name); 763 mp->mnt_stat.f_fsid.val[0] = 764 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8); 765 mp->mnt_stat.f_fsid.val[1] = 766 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8); 767 mp->mnt_stat.f_fsid.val[1] &= 0x0000FFFF; 768 769 mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid; 770 mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid, 771 sizeof(mp->mnt_vstat.f_fsid_uuid)); 772 773 /* 774 * Certain often-modified fields in the root volume are cached in 775 * the hammer_mount structure so we do not have to generate lots 776 * of little UNDO structures for them. 777 * 778 * Recopy after recovery. This also has the side effect of 779 * setting our cached undo FIFO's first_offset, which serves to 780 * placemark the FIFO start for the NEXT flush cycle while the 781 * on-disk first_offset represents the LAST flush cycle. 782 */ 783 hmp->next_tid = rootvol->ondisk->vol0_next_tid; 784 hmp->flush_tid1 = hmp->next_tid; 785 hmp->flush_tid2 = hmp->next_tid; 786 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap, 787 sizeof(hmp->blockmap)); 788 hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks; 789 790 hammer_flusher_create(hmp); 791 792 /* 793 * Locate the root directory with an obj_id of 1. 794 */ 795 error = hammer_vfs_vget(mp, NULL, HAMMER_OBJID_ROOT, &rootvp); 796 if (error) 797 goto done; 798 vput(rootvp); 799 if (hmp->ronly == 0) 800 error = hammer_recover_stage2(hmp, rootvol); 801 802 /* 803 * If the stage2 recovery fails be sure to clean out all cached 804 * vnodes before throwing away the mount structure or bad things 805 * will happen. 806 */ 807 if (error) 808 vflush(mp, 0, 0); 809 810 done: 811 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 812 /* New mount */ 813 814 /* Populate info for mount point (NULL pad)*/ 815 bzero(mp->mnt_stat.f_mntonname, MNAMELEN); 816 size_t size; 817 if (mntpt) { 818 copyinstr(mntpt, mp->mnt_stat.f_mntonname, 819 MNAMELEN -1, &size); 820 } else { /* Root mount */ 821 mp->mnt_stat.f_mntonname[0] = '/'; 822 } 823 } 824 (void)VFS_STATFS(mp, &mp->mnt_stat, cred); 825 hammer_rel_volume(rootvol, 0); 826 failed: 827 /* 828 * Cleanup and return. 829 */ 830 if (error) { 831 /* called with fs_token held */ 832 hammer_free_hmp(mp); 833 } else { 834 lwkt_reltoken(&hmp->fs_token); 835 } 836 return (error); 837 } 838 839 static int 840 hammer_vfs_unmount(struct mount *mp, int mntflags) 841 { 842 hammer_mount_t hmp = (void *)mp->mnt_data; 843 int flags; 844 int error; 845 846 /* 847 * Clean out the vnodes 848 */ 849 lwkt_gettoken(&hmp->fs_token); 850 flags = 0; 851 if (mntflags & MNT_FORCE) 852 flags |= FORCECLOSE; 853 error = vflush(mp, 0, flags); 854 855 /* 856 * Clean up the internal mount structure and related entities. This 857 * may issue I/O. 858 */ 859 if (error == 0) { 860 /* called with fs_token held */ 861 hammer_free_hmp(mp); 862 } else { 863 lwkt_reltoken(&hmp->fs_token); 864 } 865 return(error); 866 } 867 868 /* 869 * Clean up the internal mount structure and disassociate it from the mount. 870 * This may issue I/O. 871 * 872 * Called with fs_token held. 873 */ 874 static void 875 hammer_free_hmp(struct mount *mp) 876 { 877 hammer_mount_t hmp = (void *)mp->mnt_data; 878 hammer_flush_group_t flg; 879 880 /* 881 * Flush anything dirty. This won't even run if the 882 * filesystem errored-out. 883 */ 884 hammer_flush_dirty(hmp, 30); 885 886 /* 887 * If the mount had a critical error we have to destroy any 888 * remaining inodes before we can finish cleaning up the flusher. 889 */ 890 if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) { 891 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 892 hammer_destroy_inode_callback, NULL); 893 } 894 895 /* 896 * There shouldn't be any inodes left now and any left over 897 * flush groups should now be empty. 898 */ 899 KKASSERT(RB_EMPTY(&hmp->rb_inos_root)); 900 while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) { 901 TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry); 902 KKASSERT(RB_EMPTY(&flg->flush_tree)); 903 if (flg->refs) { 904 kprintf("HAMMER: Warning, flush_group %p was " 905 "not empty on umount!\n", flg); 906 } 907 kfree(flg, hmp->m_misc); 908 } 909 910 /* 911 * We can finally destroy the flusher 912 */ 913 hammer_flusher_destroy(hmp); 914 915 /* 916 * We may have held recovered buffers due to a read-only mount. 917 * These must be discarded. 918 */ 919 if (hmp->ronly) 920 hammer_recover_flush_buffers(hmp, NULL, -1); 921 922 /* 923 * Unload buffers and then volumes 924 */ 925 RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL, 926 hammer_unload_buffer, NULL); 927 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 928 hammer_unload_volume, NULL); 929 930 mp->mnt_data = NULL; 931 mp->mnt_flag &= ~MNT_LOCAL; 932 hmp->mp = NULL; 933 hammer_destroy_objid_cache(hmp); 934 hammer_destroy_dedup_cache(hmp); 935 if (hmp->dedup_free_cache != NULL) { 936 kfree(hmp->dedup_free_cache, hmp->m_misc); 937 hmp->dedup_free_cache = NULL; 938 } 939 kmalloc_destroy(&hmp->m_misc); 940 kmalloc_destroy(&hmp->m_inodes); 941 lwkt_reltoken(&hmp->fs_token); 942 kfree(hmp, M_HAMMER); 943 } 944 945 /* 946 * Report critical errors. ip may be NULL. 947 */ 948 void 949 hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip, 950 int error, const char *msg) 951 { 952 hmp->flags |= HAMMER_MOUNT_CRITICAL_ERROR; 953 954 krateprintf(&hmp->krate, 955 "HAMMER(%s): Critical error inode=%jd error=%d %s\n", 956 hmp->mp->mnt_stat.f_mntfromname, 957 (intmax_t)(ip ? ip->obj_id : -1), 958 error, msg); 959 960 if (hmp->ronly == 0) { 961 hmp->ronly = 2; /* special errored read-only mode */ 962 hmp->mp->mnt_flag |= MNT_RDONLY; 963 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 964 hammer_adjust_volume_mode, NULL); 965 kprintf("HAMMER(%s): Forcing read-only mode\n", 966 hmp->mp->mnt_stat.f_mntfromname); 967 } 968 hmp->error = error; 969 if (hammer_debug_critical) 970 Debugger("Entering debugger"); 971 } 972 973 974 /* 975 * Obtain a vnode for the specified inode number. An exclusively locked 976 * vnode is returned. 977 */ 978 int 979 hammer_vfs_vget(struct mount *mp, struct vnode *dvp, 980 ino_t ino, struct vnode **vpp) 981 { 982 struct hammer_transaction trans; 983 struct hammer_mount *hmp = (void *)mp->mnt_data; 984 struct hammer_inode *ip; 985 int error; 986 u_int32_t localization; 987 988 lwkt_gettoken(&hmp->fs_token); 989 hammer_simple_transaction(&trans, hmp); 990 991 /* 992 * If a directory vnode is supplied (mainly NFS) then we can acquire 993 * the PFS domain from it. Otherwise we would only be able to vget 994 * inodes in the root PFS. 995 */ 996 if (dvp) { 997 localization = HAMMER_DEF_LOCALIZATION + 998 VTOI(dvp)->obj_localization; 999 } else { 1000 localization = HAMMER_DEF_LOCALIZATION; 1001 } 1002 1003 /* 1004 * Lookup the requested HAMMER inode. The structure must be 1005 * left unlocked while we manipulate the related vnode to avoid 1006 * a deadlock. 1007 */ 1008 ip = hammer_get_inode(&trans, NULL, ino, 1009 hmp->asof, localization, 1010 0, &error); 1011 if (ip == NULL) { 1012 *vpp = NULL; 1013 } else { 1014 error = hammer_get_vnode(ip, vpp); 1015 hammer_rel_inode(ip, 0); 1016 } 1017 hammer_done_transaction(&trans); 1018 lwkt_reltoken(&hmp->fs_token); 1019 return (error); 1020 } 1021 1022 /* 1023 * Return the root vnode for the filesystem. 1024 * 1025 * HAMMER stores the root vnode in the hammer_mount structure so 1026 * getting it is easy. 1027 */ 1028 static int 1029 hammer_vfs_root(struct mount *mp, struct vnode **vpp) 1030 { 1031 int error; 1032 1033 error = hammer_vfs_vget(mp, NULL, HAMMER_OBJID_ROOT, vpp); 1034 return (error); 1035 } 1036 1037 static int 1038 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 1039 { 1040 struct hammer_mount *hmp = (void *)mp->mnt_data; 1041 hammer_volume_t volume; 1042 hammer_volume_ondisk_t ondisk; 1043 int error; 1044 int64_t bfree; 1045 int64_t breserved; 1046 1047 lwkt_gettoken(&hmp->fs_token); 1048 volume = hammer_get_root_volume(hmp, &error); 1049 if (error) { 1050 lwkt_reltoken(&hmp->fs_token); 1051 return(error); 1052 } 1053 ondisk = volume->ondisk; 1054 1055 /* 1056 * Basic stats 1057 */ 1058 _hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved); 1059 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes; 1060 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE; 1061 hammer_rel_volume(volume, 0); 1062 1063 mp->mnt_stat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE; 1064 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree; 1065 if (mp->mnt_stat.f_files < 0) 1066 mp->mnt_stat.f_files = 0; 1067 1068 *sbp = mp->mnt_stat; 1069 lwkt_reltoken(&hmp->fs_token); 1070 return(0); 1071 } 1072 1073 static int 1074 hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred) 1075 { 1076 struct hammer_mount *hmp = (void *)mp->mnt_data; 1077 hammer_volume_t volume; 1078 hammer_volume_ondisk_t ondisk; 1079 int error; 1080 int64_t bfree; 1081 int64_t breserved; 1082 1083 lwkt_gettoken(&hmp->fs_token); 1084 volume = hammer_get_root_volume(hmp, &error); 1085 if (error) { 1086 lwkt_reltoken(&hmp->fs_token); 1087 return(error); 1088 } 1089 ondisk = volume->ondisk; 1090 1091 /* 1092 * Basic stats 1093 */ 1094 _hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved); 1095 mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes; 1096 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_BIGBLOCK_SIZE; 1097 hammer_rel_volume(volume, 0); 1098 1099 mp->mnt_vstat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE; 1100 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree; 1101 if (mp->mnt_vstat.f_files < 0) 1102 mp->mnt_vstat.f_files = 0; 1103 *sbp = mp->mnt_vstat; 1104 lwkt_reltoken(&hmp->fs_token); 1105 return(0); 1106 } 1107 1108 /* 1109 * Sync the filesystem. Currently we have to run it twice, the second 1110 * one will advance the undo start index to the end index, so if a crash 1111 * occurs no undos will be run on mount. 1112 * 1113 * We do not sync the filesystem if we are called from a panic. If we did 1114 * we might end up blowing up a sync that was already in progress. 1115 */ 1116 static int 1117 hammer_vfs_sync(struct mount *mp, int waitfor) 1118 { 1119 struct hammer_mount *hmp = (void *)mp->mnt_data; 1120 int error; 1121 1122 lwkt_gettoken(&hmp->fs_token); 1123 if (panicstr == NULL) { 1124 error = hammer_sync_hmp(hmp, waitfor); 1125 } else { 1126 error = EIO; 1127 } 1128 lwkt_reltoken(&hmp->fs_token); 1129 return (error); 1130 } 1131 1132 /* 1133 * Convert a vnode to a file handle. 1134 * 1135 * Accesses read-only fields on already-referenced structures so 1136 * no token is needed. 1137 */ 1138 static int 1139 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp) 1140 { 1141 hammer_inode_t ip; 1142 1143 KKASSERT(MAXFIDSZ >= 16); 1144 ip = VTOI(vp); 1145 fhp->fid_len = offsetof(struct fid, fid_data[16]); 1146 fhp->fid_ext = ip->obj_localization >> 16; 1147 bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id)); 1148 bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof)); 1149 return(0); 1150 } 1151 1152 1153 /* 1154 * Convert a file handle back to a vnode. 1155 * 1156 * Use rootvp to enforce PFS isolation when a PFS is exported via a 1157 * null mount. 1158 */ 1159 static int 1160 hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp, 1161 struct fid *fhp, struct vnode **vpp) 1162 { 1163 hammer_mount_t hmp = (void *)mp->mnt_data; 1164 struct hammer_transaction trans; 1165 struct hammer_inode *ip; 1166 struct hammer_inode_info info; 1167 int error; 1168 u_int32_t localization; 1169 1170 bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id)); 1171 bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof)); 1172 if (rootvp) 1173 localization = VTOI(rootvp)->obj_localization; 1174 else 1175 localization = (u_int32_t)fhp->fid_ext << 16; 1176 1177 lwkt_gettoken(&hmp->fs_token); 1178 hammer_simple_transaction(&trans, hmp); 1179 1180 /* 1181 * Get/allocate the hammer_inode structure. The structure must be 1182 * unlocked while we manipulate the related vnode to avoid a 1183 * deadlock. 1184 */ 1185 ip = hammer_get_inode(&trans, NULL, info.obj_id, 1186 info.obj_asof, localization, 0, &error); 1187 if (ip) { 1188 error = hammer_get_vnode(ip, vpp); 1189 hammer_rel_inode(ip, 0); 1190 } else { 1191 *vpp = NULL; 1192 } 1193 hammer_done_transaction(&trans); 1194 lwkt_reltoken(&hmp->fs_token); 1195 return (error); 1196 } 1197 1198 static int 1199 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 1200 int *exflagsp, struct ucred **credanonp) 1201 { 1202 hammer_mount_t hmp = (void *)mp->mnt_data; 1203 struct netcred *np; 1204 int error; 1205 1206 lwkt_gettoken(&hmp->fs_token); 1207 np = vfs_export_lookup(mp, &hmp->export, nam); 1208 if (np) { 1209 *exflagsp = np->netc_exflags; 1210 *credanonp = &np->netc_anon; 1211 error = 0; 1212 } else { 1213 error = EACCES; 1214 } 1215 lwkt_reltoken(&hmp->fs_token); 1216 return (error); 1217 1218 } 1219 1220 int 1221 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export) 1222 { 1223 hammer_mount_t hmp = (void *)mp->mnt_data; 1224 int error; 1225 1226 lwkt_gettoken(&hmp->fs_token); 1227 1228 switch(op) { 1229 case MOUNTCTL_SET_EXPORT: 1230 error = vfs_export(mp, &hmp->export, export); 1231 break; 1232 default: 1233 error = EOPNOTSUPP; 1234 break; 1235 } 1236 lwkt_reltoken(&hmp->fs_token); 1237 1238 return(error); 1239 } 1240 1241