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 * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.60 2008/07/07 00:24:31 dillon Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/vnode.h> 41 #include <sys/mount.h> 42 #include <sys/malloc.h> 43 #include <sys/nlookup.h> 44 #include <sys/fcntl.h> 45 #include <sys/sysctl.h> 46 #include <sys/buf.h> 47 #include <sys/buf2.h> 48 #include "hammer.h" 49 50 int hammer_debug_io; 51 int hammer_debug_general; 52 int hammer_debug_debug; 53 int hammer_debug_inode; 54 int hammer_debug_locks; 55 int hammer_debug_btree; 56 int hammer_debug_tid; 57 int hammer_debug_recover; /* -1 will disable, +1 will force */ 58 int hammer_debug_recover_faults; 59 int hammer_debug_cluster_enable = 1; /* enable read clustering by default */ 60 int hammer_count_inodes; 61 int hammer_count_iqueued; 62 int hammer_count_reclaiming; 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_record_iterations; 77 int hammer_count_dirtybufspace; /* global */ 78 int hammer_count_refedbufs; /* global */ 79 int hammer_count_reservations; 80 int hammer_count_io_running_read; 81 int hammer_count_io_running_write; 82 int hammer_count_io_locked; 83 int hammer_limit_dirtybufspace; /* per-mount */ 84 int hammer_limit_recs; /* as a whole XXX */ 85 int hammer_limit_iqueued; /* per-mount */ 86 int hammer_bio_count; 87 int hammer_verify_zone; 88 int hammer_write_mode; 89 int64_t hammer_contention_count; 90 int64_t hammer_zone_limit; 91 92 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem"); 93 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW, 94 &hammer_debug_general, 0, ""); 95 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW, 96 &hammer_debug_io, 0, ""); 97 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW, 98 &hammer_debug_debug, 0, ""); 99 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW, 100 &hammer_debug_inode, 0, ""); 101 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW, 102 &hammer_debug_locks, 0, ""); 103 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW, 104 &hammer_debug_btree, 0, ""); 105 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW, 106 &hammer_debug_tid, 0, ""); 107 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW, 108 &hammer_debug_recover, 0, ""); 109 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW, 110 &hammer_debug_recover_faults, 0, ""); 111 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_cluster_enable, CTLFLAG_RW, 112 &hammer_debug_cluster_enable, 0, ""); 113 114 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW, 115 &hammer_limit_dirtybufspace, 0, ""); 116 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW, 117 &hammer_limit_recs, 0, ""); 118 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_iqueued, CTLFLAG_RW, 119 &hammer_limit_iqueued, 0, ""); 120 121 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD, 122 &hammer_count_inodes, 0, ""); 123 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD, 124 &hammer_count_iqueued, 0, ""); 125 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaiming, CTLFLAG_RD, 126 &hammer_count_reclaiming, 0, ""); 127 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD, 128 &hammer_count_records, 0, ""); 129 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD, 130 &hammer_count_record_datas, 0, ""); 131 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD, 132 &hammer_count_volumes, 0, ""); 133 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD, 134 &hammer_count_buffers, 0, ""); 135 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD, 136 &hammer_count_nodes, 0, ""); 137 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, count_extra_space_used, CTLFLAG_RD, 138 &hammer_count_extra_space_used, 0, ""); 139 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD, 140 &hammer_stats_btree_searches, 0, ""); 141 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD, 142 &hammer_stats_btree_lookups, 0, ""); 143 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD, 144 &hammer_stats_btree_inserts, 0, ""); 145 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD, 146 &hammer_stats_btree_deletes, 0, ""); 147 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD, 148 &hammer_stats_btree_elements, 0, ""); 149 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD, 150 &hammer_stats_btree_splits, 0, ""); 151 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD, 152 &hammer_stats_btree_iterations, 0, ""); 153 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD, 154 &hammer_stats_record_iterations, 0, ""); 155 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD, 156 &hammer_count_dirtybufspace, 0, ""); 157 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD, 158 &hammer_count_refedbufs, 0, ""); 159 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD, 160 &hammer_count_reservations, 0, ""); 161 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD, 162 &hammer_count_io_running_read, 0, ""); 163 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD, 164 &hammer_count_io_locked, 0, ""); 165 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD, 166 &hammer_count_io_running_write, 0, ""); 167 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW, 168 &hammer_zone_limit, 0, ""); 169 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW, 170 &hammer_contention_count, 0, ""); 171 SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW, 172 &hammer_verify_zone, 0, ""); 173 SYSCTL_INT(_vfs_hammer, OID_AUTO, write_mode, CTLFLAG_RW, 174 &hammer_write_mode, 0, ""); 175 176 KTR_INFO_MASTER(hammer); 177 178 /* 179 * VFS ABI 180 */ 181 static void hammer_free_hmp(struct mount *mp); 182 183 static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data, 184 struct ucred *cred); 185 static int hammer_vfs_unmount(struct mount *mp, int mntflags); 186 static int hammer_vfs_root(struct mount *mp, struct vnode **vpp); 187 static int hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, 188 struct ucred *cred); 189 static int hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, 190 struct ucred *cred); 191 static int hammer_vfs_sync(struct mount *mp, int waitfor); 192 static int hammer_vfs_vget(struct mount *mp, ino_t ino, 193 struct vnode **vpp); 194 static int hammer_vfs_init(struct vfsconf *conf); 195 static int hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, 196 struct vnode **vpp); 197 static int hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp); 198 static int hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 199 int *exflagsp, struct ucred **credanonp); 200 201 202 static struct vfsops hammer_vfsops = { 203 .vfs_mount = hammer_vfs_mount, 204 .vfs_unmount = hammer_vfs_unmount, 205 .vfs_root = hammer_vfs_root, 206 .vfs_statfs = hammer_vfs_statfs, 207 .vfs_statvfs = hammer_vfs_statvfs, 208 .vfs_sync = hammer_vfs_sync, 209 .vfs_vget = hammer_vfs_vget, 210 .vfs_init = hammer_vfs_init, 211 .vfs_vptofh = hammer_vfs_vptofh, 212 .vfs_fhtovp = hammer_vfs_fhtovp, 213 .vfs_checkexp = hammer_vfs_checkexp 214 }; 215 216 MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount"); 217 218 VFS_SET(hammer_vfsops, hammer, 0); 219 MODULE_VERSION(hammer, 1); 220 221 static int 222 hammer_vfs_init(struct vfsconf *conf) 223 { 224 int n; 225 226 if (hammer_limit_recs == 0) { 227 hammer_limit_recs = nbuf * 25; 228 n = kmalloc_limit(M_HAMMER) / 512; 229 if (hammer_limit_recs > n) 230 hammer_limit_recs = n; 231 } 232 if (hammer_limit_dirtybufspace == 0) { 233 hammer_limit_dirtybufspace = hidirtybufspace / 2; 234 if (hammer_limit_dirtybufspace < 100) 235 hammer_limit_dirtybufspace = 100; 236 } 237 if (hammer_limit_iqueued == 0) 238 hammer_limit_iqueued = desiredvnodes / 5; 239 return(0); 240 } 241 242 static int 243 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data, 244 struct ucred *cred) 245 { 246 struct hammer_mount_info info; 247 hammer_mount_t hmp; 248 hammer_volume_t rootvol; 249 struct vnode *rootvp; 250 const char *upath; /* volume name in userspace */ 251 char *path; /* volume name in system space */ 252 int error; 253 int i; 254 255 if ((error = copyin(data, &info, sizeof(info))) != 0) 256 return (error); 257 if ((mp->mnt_flag & MNT_UPDATE) == 0) { 258 if (info.nvolumes <= 0 || info.nvolumes >= 32768) 259 return (EINVAL); 260 } 261 if ((info.hflags & HMNT_MASTERID) && 262 (info.masterid < -1 || info.masterid >= HAMMER_MAX_MASTERS)) { 263 return (EINVAL); 264 } 265 266 /* 267 * Interal mount data structure 268 */ 269 if (mp->mnt_flag & MNT_UPDATE) { 270 hmp = (void *)mp->mnt_data; 271 KKASSERT(hmp != NULL); 272 } else { 273 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO); 274 mp->mnt_data = (qaddr_t)hmp; 275 hmp->mp = mp; 276 hmp->namekey_iterator = mycpu->gd_time_seconds; 277 /*TAILQ_INIT(&hmp->recycle_list);*/ 278 279 hmp->root_btree_beg.localization = 0x00000000U; 280 hmp->root_btree_beg.obj_id = -0x8000000000000000LL; 281 hmp->root_btree_beg.key = -0x8000000000000000LL; 282 hmp->root_btree_beg.create_tid = 1; 283 hmp->root_btree_beg.delete_tid = 1; 284 hmp->root_btree_beg.rec_type = 0; 285 hmp->root_btree_beg.obj_type = 0; 286 287 hmp->root_btree_end.localization = 0xFFFFFFFFU; 288 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL; 289 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL; 290 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL; 291 hmp->root_btree_end.delete_tid = 0; /* special case */ 292 hmp->root_btree_end.rec_type = 0xFFFFU; 293 hmp->root_btree_end.obj_type = 0; 294 295 hmp->sync_lock.refs = 1; 296 hmp->free_lock.refs = 1; 297 hmp->undo_lock.refs = 1; 298 hmp->blkmap_lock.refs = 1; 299 300 TAILQ_INIT(&hmp->flush_list); 301 TAILQ_INIT(&hmp->delay_list); 302 TAILQ_INIT(&hmp->objid_cache_list); 303 TAILQ_INIT(&hmp->undo_lru_list); 304 TAILQ_INIT(&hmp->reclaim_list); 305 } 306 hmp->hflags &= ~HMNT_USERFLAGS; 307 hmp->hflags |= info.hflags & HMNT_USERFLAGS; 308 if (info.hflags & HMNT_MASTERID) 309 hmp->masterid = info.masterid; 310 else 311 hmp->masterid = -1; 312 if (info.asof) { 313 kprintf("ASOF\n"); 314 mp->mnt_flag |= MNT_RDONLY; 315 hmp->asof = info.asof; 316 } else { 317 hmp->asof = HAMMER_MAX_TID; 318 } 319 320 /* 321 * Re-open read-write if originally read-only, or vise-versa. 322 */ 323 if (mp->mnt_flag & MNT_UPDATE) { 324 error = 0; 325 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 326 kprintf("HAMMER read-only -> read-write\n"); 327 hmp->ronly = 0; 328 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 329 hammer_adjust_volume_mode, NULL); 330 rootvol = hammer_get_root_volume(hmp, &error); 331 if (rootvol) { 332 hammer_recover_flush_buffers(hmp, rootvol, 1); 333 bcopy(rootvol->ondisk->vol0_blockmap, 334 hmp->blockmap, 335 sizeof(hmp->blockmap)); 336 hammer_rel_volume(rootvol, 0); 337 } 338 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 339 hammer_reload_inode, NULL); 340 /* kernel clears MNT_RDONLY */ 341 } else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { 342 kprintf("HAMMER read-write -> read-only\n"); 343 hmp->ronly = 1; /* messy */ 344 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 345 hammer_reload_inode, NULL); 346 hmp->ronly = 0; 347 hammer_flusher_sync(hmp); 348 hammer_flusher_sync(hmp); 349 hammer_flusher_sync(hmp); 350 hmp->ronly = 1; 351 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 352 hammer_adjust_volume_mode, NULL); 353 } 354 return(error); 355 } 356 357 RB_INIT(&hmp->rb_vols_root); 358 RB_INIT(&hmp->rb_inos_root); 359 RB_INIT(&hmp->rb_nods_root); 360 RB_INIT(&hmp->rb_undo_root); 361 RB_INIT(&hmp->rb_resv_root); 362 RB_INIT(&hmp->rb_bufs_root); 363 RB_INIT(&hmp->rb_pfsm_root); 364 365 hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0); 366 367 TAILQ_INIT(&hmp->volu_list); 368 TAILQ_INIT(&hmp->undo_list); 369 TAILQ_INIT(&hmp->data_list); 370 TAILQ_INIT(&hmp->meta_list); 371 TAILQ_INIT(&hmp->lose_list); 372 373 /* 374 * Load volumes 375 */ 376 path = objcache_get(namei_oc, M_WAITOK); 377 hmp->nvolumes = info.nvolumes; 378 for (i = 0; i < info.nvolumes; ++i) { 379 error = copyin(&info.volumes[i], &upath, sizeof(char *)); 380 if (error == 0) 381 error = copyinstr(upath, path, MAXPATHLEN, NULL); 382 if (error == 0) 383 error = hammer_install_volume(hmp, path); 384 if (error) 385 break; 386 } 387 objcache_put(namei_oc, path); 388 389 /* 390 * Make sure we found a root volume 391 */ 392 if (error == 0 && hmp->rootvol == NULL) { 393 kprintf("hammer_mount: No root volume found!\n"); 394 error = EINVAL; 395 } 396 if (error) { 397 hammer_free_hmp(mp); 398 return (error); 399 } 400 401 /* 402 * No errors, setup enough of the mount point so we can lookup the 403 * root vnode. 404 */ 405 mp->mnt_iosize_max = MAXPHYS; 406 mp->mnt_kern_flag |= MNTK_FSMID; 407 408 /* 409 * note: f_iosize is used by vnode_pager_haspage() when constructing 410 * its VOP_BMAP call. 411 */ 412 mp->mnt_stat.f_iosize = HAMMER_BUFSIZE; 413 mp->mnt_stat.f_bsize = HAMMER_BUFSIZE; 414 415 mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE; 416 mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE; 417 418 mp->mnt_maxsymlinklen = 255; 419 mp->mnt_flag |= MNT_LOCAL; 420 421 vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops); 422 vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops); 423 vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops); 424 425 /* 426 * The root volume's ondisk pointer is only valid if we hold a 427 * reference to it. 428 */ 429 rootvol = hammer_get_root_volume(hmp, &error); 430 if (error) 431 goto failed; 432 433 /* 434 * Perform any necessary UNDO operations. The recovery code does 435 * call hammer_undo_lookup() so we have to pre-cache the blockmap, 436 * and then re-copy it again after recovery is complete. 437 * 438 * If this is a read-only mount the UNDO information is retained 439 * in memory in the form of dirty buffer cache buffers, and not 440 * written back to the media. 441 */ 442 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap, 443 sizeof(hmp->blockmap)); 444 445 error = hammer_recover(hmp, rootvol); 446 if (error) { 447 kprintf("Failed to recover HAMMER filesystem on mount\n"); 448 goto done; 449 } 450 451 /* 452 * Finish setup now that we have a good root volume. 453 * 454 * The top 16 bits of fsid.val[1] is a pfs id. 455 */ 456 ksnprintf(mp->mnt_stat.f_mntfromname, 457 sizeof(mp->mnt_stat.f_mntfromname), "%s", 458 rootvol->ondisk->vol_name); 459 mp->mnt_stat.f_fsid.val[0] = 460 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8); 461 mp->mnt_stat.f_fsid.val[1] = 462 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8); 463 mp->mnt_stat.f_fsid.val[1] &= 0x0000FFFF; 464 465 mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid; 466 mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid, 467 sizeof(mp->mnt_vstat.f_fsid_uuid)); 468 469 /* 470 * Certain often-modified fields in the root volume are cached in 471 * the hammer_mount structure so we do not have to generate lots 472 * of little UNDO structures for them. 473 * 474 * Recopy after recovery. This also has the side effect of 475 * setting our cached undo FIFO's first_offset, which serves to 476 * placemark the FIFO start for the NEXT flush cycle while the 477 * on-disk first_offset represents the LAST flush cycle. 478 */ 479 hmp->next_tid = rootvol->ondisk->vol0_next_tid; 480 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap, 481 sizeof(hmp->blockmap)); 482 hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks; 483 484 hammer_flusher_create(hmp); 485 486 /* 487 * Locate the root directory using the root cluster's B-Tree as a 488 * starting point. The root directory uses an obj_id of 1. 489 * 490 * FUTURE: Leave the root directory cached referenced but unlocked 491 * in hmp->rootvp (need to flush it on unmount). 492 */ 493 error = hammer_vfs_vget(mp, 1, &rootvp); 494 if (error) 495 goto done; 496 vput(rootvp); 497 /*vn_unlock(hmp->rootvp);*/ 498 499 done: 500 hammer_rel_volume(rootvol, 0); 501 failed: 502 /* 503 * Cleanup and return. 504 */ 505 if (error) 506 hammer_free_hmp(mp); 507 return (error); 508 } 509 510 static int 511 hammer_vfs_unmount(struct mount *mp, int mntflags) 512 { 513 #if 0 514 struct hammer_mount *hmp = (void *)mp->mnt_data; 515 #endif 516 int flags; 517 int error; 518 519 /* 520 * Clean out the vnodes 521 */ 522 flags = 0; 523 if (mntflags & MNT_FORCE) 524 flags |= FORCECLOSE; 525 if ((error = vflush(mp, 0, flags)) != 0) 526 return (error); 527 528 /* 529 * Clean up the internal mount structure and related entities. This 530 * may issue I/O. 531 */ 532 hammer_free_hmp(mp); 533 return(0); 534 } 535 536 /* 537 * Clean up the internal mount structure and disassociate it from the mount. 538 * This may issue I/O. 539 */ 540 static void 541 hammer_free_hmp(struct mount *mp) 542 { 543 struct hammer_mount *hmp = (void *)mp->mnt_data; 544 545 #if 0 546 /* 547 * Clean up the root vnode 548 */ 549 if (hmp->rootvp) { 550 vrele(hmp->rootvp); 551 hmp->rootvp = NULL; 552 } 553 #endif 554 hammer_flusher_sync(hmp); 555 hammer_flusher_sync(hmp); 556 hammer_flusher_destroy(hmp); 557 558 KKASSERT(RB_EMPTY(&hmp->rb_inos_root)); 559 560 #if 0 561 /* 562 * Unload & flush inodes 563 * 564 * XXX illegal to call this from here, it can only be done from 565 * the flusher. 566 */ 567 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 568 hammer_unload_inode, (void *)MNT_WAIT); 569 570 /* 571 * Unload & flush volumes 572 */ 573 #endif 574 /* 575 * Unload buffers and then volumes 576 */ 577 RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL, 578 hammer_unload_buffer, NULL); 579 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 580 hammer_unload_volume, NULL); 581 582 mp->mnt_data = NULL; 583 mp->mnt_flag &= ~MNT_LOCAL; 584 hmp->mp = NULL; 585 hammer_destroy_objid_cache(hmp); 586 kfree(hmp, M_HAMMER); 587 } 588 589 /* 590 * Obtain a vnode for the specified inode number. An exclusively locked 591 * vnode is returned. 592 */ 593 int 594 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 595 { 596 struct hammer_transaction trans; 597 struct hammer_mount *hmp = (void *)mp->mnt_data; 598 struct hammer_inode *ip; 599 int error; 600 601 hammer_simple_transaction(&trans, hmp); 602 603 /* 604 * Lookup the requested HAMMER inode. The structure must be 605 * left unlocked while we manipulate the related vnode to avoid 606 * a deadlock. 607 */ 608 ip = hammer_get_inode(&trans, NULL, ino, 609 hmp->asof, HAMMER_DEF_LOCALIZATION, 610 0, &error); 611 if (ip == NULL) { 612 *vpp = NULL; 613 return(error); 614 } 615 error = hammer_get_vnode(ip, vpp); 616 hammer_rel_inode(ip, 0); 617 hammer_done_transaction(&trans); 618 return (error); 619 } 620 621 /* 622 * Return the root vnode for the filesystem. 623 * 624 * HAMMER stores the root vnode in the hammer_mount structure so 625 * getting it is easy. 626 */ 627 static int 628 hammer_vfs_root(struct mount *mp, struct vnode **vpp) 629 { 630 #if 0 631 struct hammer_mount *hmp = (void *)mp->mnt_data; 632 #endif 633 int error; 634 635 error = hammer_vfs_vget(mp, 1, vpp); 636 return (error); 637 } 638 639 static int 640 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 641 { 642 struct hammer_mount *hmp = (void *)mp->mnt_data; 643 hammer_volume_t volume; 644 hammer_volume_ondisk_t ondisk; 645 int error; 646 int64_t bfree; 647 648 volume = hammer_get_root_volume(hmp, &error); 649 if (error) 650 return(error); 651 ondisk = volume->ondisk; 652 653 /* 654 * Basic stats 655 */ 656 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes; 657 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE; 658 hammer_rel_volume(volume, 0); 659 660 mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE; 661 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree; 662 if (mp->mnt_stat.f_files < 0) 663 mp->mnt_stat.f_files = 0; 664 665 *sbp = mp->mnt_stat; 666 return(0); 667 } 668 669 static int 670 hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred) 671 { 672 struct hammer_mount *hmp = (void *)mp->mnt_data; 673 hammer_volume_t volume; 674 hammer_volume_ondisk_t ondisk; 675 int error; 676 int64_t bfree; 677 678 volume = hammer_get_root_volume(hmp, &error); 679 if (error) 680 return(error); 681 ondisk = volume->ondisk; 682 683 /* 684 * Basic stats 685 */ 686 mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes; 687 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE; 688 hammer_rel_volume(volume, 0); 689 690 mp->mnt_vstat.f_bfree = bfree / HAMMER_BUFSIZE; 691 mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree; 692 if (mp->mnt_vstat.f_files < 0) 693 mp->mnt_vstat.f_files = 0; 694 *sbp = mp->mnt_vstat; 695 return(0); 696 } 697 698 /* 699 * Sync the filesystem. Currently we have to run it twice, the second 700 * one will advance the undo start index to the end index, so if a crash 701 * occurs no undos will be run on mount. 702 * 703 * We do not sync the filesystem if we are called from a panic. If we did 704 * we might end up blowing up a sync that was already in progress. 705 */ 706 static int 707 hammer_vfs_sync(struct mount *mp, int waitfor) 708 { 709 struct hammer_mount *hmp = (void *)mp->mnt_data; 710 int error; 711 712 if (panicstr == NULL) { 713 error = hammer_sync_hmp(hmp, waitfor); 714 if (error == 0) 715 error = hammer_sync_hmp(hmp, waitfor); 716 } else { 717 error = EIO; 718 } 719 return (error); 720 } 721 722 /* 723 * Convert a vnode to a file handle. 724 */ 725 static int 726 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp) 727 { 728 hammer_inode_t ip; 729 730 KKASSERT(MAXFIDSZ >= 16); 731 ip = VTOI(vp); 732 fhp->fid_len = offsetof(struct fid, fid_data[16]); 733 fhp->fid_ext = ip->obj_localization >> 16; 734 bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id)); 735 bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof)); 736 return(0); 737 } 738 739 740 /* 741 * Convert a file handle back to a vnode. 742 */ 743 static int 744 hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 745 { 746 struct hammer_transaction trans; 747 struct hammer_inode *ip; 748 struct hammer_inode_info info; 749 int error; 750 u_int32_t localization; 751 752 bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id)); 753 bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof)); 754 localization = (u_int32_t)fhp->fid_ext << 16; 755 756 hammer_simple_transaction(&trans, (void *)mp->mnt_data); 757 758 /* 759 * Get/allocate the hammer_inode structure. The structure must be 760 * unlocked while we manipulate the related vnode to avoid a 761 * deadlock. 762 */ 763 kprintf("localization %08x\n", localization); 764 ip = hammer_get_inode(&trans, NULL, info.obj_id, 765 info.obj_asof, localization, 0, &error); 766 if (ip == NULL) { 767 *vpp = NULL; 768 return(error); 769 } 770 error = hammer_get_vnode(ip, vpp); 771 hammer_rel_inode(ip, 0); 772 hammer_done_transaction(&trans); 773 return (error); 774 } 775 776 static int 777 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 778 int *exflagsp, struct ucred **credanonp) 779 { 780 hammer_mount_t hmp = (void *)mp->mnt_data; 781 struct netcred *np; 782 int error; 783 784 np = vfs_export_lookup(mp, &hmp->export, nam); 785 if (np) { 786 *exflagsp = np->netc_exflags; 787 *credanonp = &np->netc_anon; 788 error = 0; 789 } else { 790 error = EACCES; 791 } 792 return (error); 793 794 } 795 796 int 797 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export) 798 { 799 hammer_mount_t hmp = (void *)mp->mnt_data; 800 int error; 801 802 switch(op) { 803 case MOUNTCTL_SET_EXPORT: 804 error = vfs_export(mp, &hmp->export, export); 805 break; 806 default: 807 error = EOPNOTSUPP; 808 break; 809 } 810 return(error); 811 } 812 813