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.59 2008/07/03 04:24:51 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 ksnprintf(mp->mnt_stat.f_mntfromname, 455 sizeof(mp->mnt_stat.f_mntfromname), "%s", 456 rootvol->ondisk->vol_name); 457 mp->mnt_stat.f_fsid.val[0] = 458 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8); 459 mp->mnt_stat.f_fsid.val[1] = 460 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8); 461 462 mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid; 463 mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid, 464 sizeof(mp->mnt_vstat.f_fsid_uuid)); 465 466 /* 467 * Certain often-modified fields in the root volume are cached in 468 * the hammer_mount structure so we do not have to generate lots 469 * of little UNDO structures for them. 470 * 471 * Recopy after recovery. This also has the side effect of 472 * setting our cached undo FIFO's first_offset, which serves to 473 * placemark the FIFO start for the NEXT flush cycle while the 474 * on-disk first_offset represents the LAST flush cycle. 475 */ 476 hmp->next_tid = rootvol->ondisk->vol0_next_tid; 477 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap, 478 sizeof(hmp->blockmap)); 479 hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks; 480 481 hammer_flusher_create(hmp); 482 483 /* 484 * Locate the root directory using the root cluster's B-Tree as a 485 * starting point. The root directory uses an obj_id of 1. 486 * 487 * FUTURE: Leave the root directory cached referenced but unlocked 488 * in hmp->rootvp (need to flush it on unmount). 489 */ 490 error = hammer_vfs_vget(mp, 1, &rootvp); 491 if (error) 492 goto done; 493 vput(rootvp); 494 /*vn_unlock(hmp->rootvp);*/ 495 496 done: 497 hammer_rel_volume(rootvol, 0); 498 failed: 499 /* 500 * Cleanup and return. 501 */ 502 if (error) 503 hammer_free_hmp(mp); 504 return (error); 505 } 506 507 static int 508 hammer_vfs_unmount(struct mount *mp, int mntflags) 509 { 510 #if 0 511 struct hammer_mount *hmp = (void *)mp->mnt_data; 512 #endif 513 int flags; 514 int error; 515 516 /* 517 * Clean out the vnodes 518 */ 519 flags = 0; 520 if (mntflags & MNT_FORCE) 521 flags |= FORCECLOSE; 522 if ((error = vflush(mp, 0, flags)) != 0) 523 return (error); 524 525 /* 526 * Clean up the internal mount structure and related entities. This 527 * may issue I/O. 528 */ 529 hammer_free_hmp(mp); 530 return(0); 531 } 532 533 /* 534 * Clean up the internal mount structure and disassociate it from the mount. 535 * This may issue I/O. 536 */ 537 static void 538 hammer_free_hmp(struct mount *mp) 539 { 540 struct hammer_mount *hmp = (void *)mp->mnt_data; 541 542 #if 0 543 /* 544 * Clean up the root vnode 545 */ 546 if (hmp->rootvp) { 547 vrele(hmp->rootvp); 548 hmp->rootvp = NULL; 549 } 550 #endif 551 hammer_flusher_sync(hmp); 552 hammer_flusher_sync(hmp); 553 hammer_flusher_destroy(hmp); 554 555 KKASSERT(RB_EMPTY(&hmp->rb_inos_root)); 556 557 #if 0 558 /* 559 * Unload & flush inodes 560 * 561 * XXX illegal to call this from here, it can only be done from 562 * the flusher. 563 */ 564 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 565 hammer_unload_inode, (void *)MNT_WAIT); 566 567 /* 568 * Unload & flush volumes 569 */ 570 #endif 571 /* 572 * Unload buffers and then volumes 573 */ 574 RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL, 575 hammer_unload_buffer, NULL); 576 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 577 hammer_unload_volume, NULL); 578 579 mp->mnt_data = NULL; 580 mp->mnt_flag &= ~MNT_LOCAL; 581 hmp->mp = NULL; 582 hammer_destroy_objid_cache(hmp); 583 kfree(hmp, M_HAMMER); 584 } 585 586 /* 587 * Obtain a vnode for the specified inode number. An exclusively locked 588 * vnode is returned. 589 */ 590 int 591 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 592 { 593 struct hammer_transaction trans; 594 struct hammer_mount *hmp = (void *)mp->mnt_data; 595 struct hammer_inode *ip; 596 int error; 597 598 hammer_simple_transaction(&trans, hmp); 599 600 /* 601 * Lookup the requested HAMMER inode. The structure must be 602 * left unlocked while we manipulate the related vnode to avoid 603 * a deadlock. 604 */ 605 ip = hammer_get_inode(&trans, NULL, ino, 606 hmp->asof, HAMMER_DEF_LOCALIZATION, 607 0, &error); 608 if (ip == NULL) { 609 *vpp = NULL; 610 return(error); 611 } 612 error = hammer_get_vnode(ip, vpp); 613 hammer_rel_inode(ip, 0); 614 hammer_done_transaction(&trans); 615 return (error); 616 } 617 618 /* 619 * Return the root vnode for the filesystem. 620 * 621 * HAMMER stores the root vnode in the hammer_mount structure so 622 * getting it is easy. 623 */ 624 static int 625 hammer_vfs_root(struct mount *mp, struct vnode **vpp) 626 { 627 #if 0 628 struct hammer_mount *hmp = (void *)mp->mnt_data; 629 #endif 630 int error; 631 632 error = hammer_vfs_vget(mp, 1, vpp); 633 return (error); 634 } 635 636 static int 637 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 638 { 639 struct hammer_mount *hmp = (void *)mp->mnt_data; 640 hammer_volume_t volume; 641 hammer_volume_ondisk_t ondisk; 642 int error; 643 int64_t bfree; 644 645 volume = hammer_get_root_volume(hmp, &error); 646 if (error) 647 return(error); 648 ondisk = volume->ondisk; 649 650 /* 651 * Basic stats 652 */ 653 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes; 654 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE; 655 hammer_rel_volume(volume, 0); 656 657 mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE; 658 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree; 659 if (mp->mnt_stat.f_files < 0) 660 mp->mnt_stat.f_files = 0; 661 662 *sbp = mp->mnt_stat; 663 return(0); 664 } 665 666 static int 667 hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred) 668 { 669 struct hammer_mount *hmp = (void *)mp->mnt_data; 670 hammer_volume_t volume; 671 hammer_volume_ondisk_t ondisk; 672 int error; 673 int64_t bfree; 674 675 volume = hammer_get_root_volume(hmp, &error); 676 if (error) 677 return(error); 678 ondisk = volume->ondisk; 679 680 /* 681 * Basic stats 682 */ 683 mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes; 684 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE; 685 hammer_rel_volume(volume, 0); 686 687 mp->mnt_vstat.f_bfree = bfree / HAMMER_BUFSIZE; 688 mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree; 689 if (mp->mnt_vstat.f_files < 0) 690 mp->mnt_vstat.f_files = 0; 691 *sbp = mp->mnt_vstat; 692 return(0); 693 } 694 695 /* 696 * Sync the filesystem. Currently we have to run it twice, the second 697 * one will advance the undo start index to the end index, so if a crash 698 * occurs no undos will be run on mount. 699 * 700 * We do not sync the filesystem if we are called from a panic. If we did 701 * we might end up blowing up a sync that was already in progress. 702 */ 703 static int 704 hammer_vfs_sync(struct mount *mp, int waitfor) 705 { 706 struct hammer_mount *hmp = (void *)mp->mnt_data; 707 int error; 708 709 if (panicstr == NULL) { 710 error = hammer_sync_hmp(hmp, waitfor); 711 if (error == 0) 712 error = hammer_sync_hmp(hmp, waitfor); 713 } else { 714 error = EIO; 715 } 716 return (error); 717 } 718 719 /* 720 * Convert a vnode to a file handle. 721 */ 722 static int 723 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp) 724 { 725 hammer_inode_t ip; 726 727 KKASSERT(MAXFIDSZ >= 16); 728 ip = VTOI(vp); 729 fhp->fid_len = offsetof(struct fid, fid_data[16]); 730 fhp->fid_reserved = 0; 731 bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id)); 732 bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof)); 733 return(0); 734 } 735 736 737 /* 738 * Convert a file handle back to a vnode. 739 */ 740 static int 741 hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 742 { 743 struct hammer_transaction trans; 744 struct hammer_inode *ip; 745 struct hammer_inode_info info; 746 int error; 747 748 bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id)); 749 bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof)); 750 751 hammer_simple_transaction(&trans, (void *)mp->mnt_data); 752 753 /* 754 * Get/allocate the hammer_inode structure. The structure must be 755 * unlocked while we manipulate the related vnode to avoid a 756 * deadlock. 757 */ 758 ip = hammer_get_inode(&trans, NULL, info.obj_id, 759 info.obj_asof, HAMMER_DEF_LOCALIZATION, 760 0, &error); 761 if (ip == NULL) { 762 *vpp = NULL; 763 return(error); 764 } 765 error = hammer_get_vnode(ip, vpp); 766 hammer_rel_inode(ip, 0); 767 hammer_done_transaction(&trans); 768 return (error); 769 } 770 771 static int 772 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 773 int *exflagsp, struct ucred **credanonp) 774 { 775 hammer_mount_t hmp = (void *)mp->mnt_data; 776 struct netcred *np; 777 int error; 778 779 np = vfs_export_lookup(mp, &hmp->export, nam); 780 if (np) { 781 *exflagsp = np->netc_exflags; 782 *credanonp = &np->netc_anon; 783 error = 0; 784 } else { 785 error = EACCES; 786 } 787 return (error); 788 789 } 790 791 int 792 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export) 793 { 794 hammer_mount_t hmp = (void *)mp->mnt_data; 795 int error; 796 797 switch(op) { 798 case MOUNTCTL_SET_EXPORT: 799 error = vfs_export(mp, &hmp->export, export); 800 break; 801 default: 802 error = EOPNOTSUPP; 803 break; 804 } 805 return(error); 806 } 807 808