1427e5fc6SMatthew Dillon /* 2b84de5afSMatthew Dillon * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved. 3427e5fc6SMatthew Dillon * 4427e5fc6SMatthew Dillon * This code is derived from software contributed to The DragonFly Project 5427e5fc6SMatthew Dillon * by Matthew Dillon <dillon@backplane.com> 6427e5fc6SMatthew Dillon * 7427e5fc6SMatthew Dillon * Redistribution and use in source and binary forms, with or without 8427e5fc6SMatthew Dillon * modification, are permitted provided that the following conditions 9427e5fc6SMatthew Dillon * are met: 10427e5fc6SMatthew Dillon * 11427e5fc6SMatthew Dillon * 1. Redistributions of source code must retain the above copyright 12427e5fc6SMatthew Dillon * notice, this list of conditions and the following disclaimer. 13427e5fc6SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright 14427e5fc6SMatthew Dillon * notice, this list of conditions and the following disclaimer in 15427e5fc6SMatthew Dillon * the documentation and/or other materials provided with the 16427e5fc6SMatthew Dillon * distribution. 17427e5fc6SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its 18427e5fc6SMatthew Dillon * contributors may be used to endorse or promote products derived 19427e5fc6SMatthew Dillon * from this software without specific, prior written permission. 20427e5fc6SMatthew Dillon * 21427e5fc6SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22427e5fc6SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23427e5fc6SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24427e5fc6SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25427e5fc6SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26427e5fc6SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27427e5fc6SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28427e5fc6SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29427e5fc6SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30427e5fc6SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31427e5fc6SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32427e5fc6SMatthew Dillon * SUCH DAMAGE. 33427e5fc6SMatthew Dillon * 34*c9b9e29dSMatthew Dillon * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.33 2008/05/04 09:06:45 dillon Exp $ 35427e5fc6SMatthew Dillon */ 36427e5fc6SMatthew Dillon 37427e5fc6SMatthew Dillon #include <sys/param.h> 38427e5fc6SMatthew Dillon #include <sys/systm.h> 39427e5fc6SMatthew Dillon #include <sys/kernel.h> 40427e5fc6SMatthew Dillon #include <sys/vnode.h> 41427e5fc6SMatthew Dillon #include <sys/mount.h> 42427e5fc6SMatthew Dillon #include <sys/malloc.h> 43427e5fc6SMatthew Dillon #include <sys/nlookup.h> 44427e5fc6SMatthew Dillon #include <sys/fcntl.h> 45b3deaf57SMatthew Dillon #include <sys/sysctl.h> 46427e5fc6SMatthew Dillon #include <sys/buf.h> 47427e5fc6SMatthew Dillon #include <sys/buf2.h> 48427e5fc6SMatthew Dillon #include "hammer.h" 49427e5fc6SMatthew Dillon 50d5ef456eSMatthew Dillon int hammer_debug_general; 51e8599db1SMatthew Dillon int hammer_debug_inode; 527d683b0fSMatthew Dillon int hammer_debug_locks; 53b3deaf57SMatthew Dillon int hammer_debug_btree; 54d113fda1SMatthew Dillon int hammer_debug_tid; 5546fe7ae1SMatthew Dillon int hammer_debug_recover; /* -1 will disable, +1 will force */ 5646fe7ae1SMatthew Dillon int hammer_debug_recover_faults; 57b3deaf57SMatthew Dillon int hammer_count_inodes; 58b3deaf57SMatthew Dillon int hammer_count_records; 59b3deaf57SMatthew Dillon int hammer_count_record_datas; 60b3deaf57SMatthew Dillon int hammer_count_volumes; 61b3deaf57SMatthew Dillon int hammer_count_buffers; 62b3deaf57SMatthew Dillon int hammer_count_nodes; 639480ff55SMatthew Dillon int hammer_count_dirtybufs; /* global */ 649480ff55SMatthew Dillon int hammer_limit_dirtybufs = 100; /* per-mount */ 651f07f686SMatthew Dillon int hammer_bio_count; 667d683b0fSMatthew Dillon int64_t hammer_contention_count; 67f03c9cf4SMatthew Dillon int64_t hammer_zone_limit; 68b3deaf57SMatthew Dillon 69b3deaf57SMatthew Dillon SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem"); 70d5ef456eSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW, 71d5ef456eSMatthew Dillon &hammer_debug_general, 0, ""); 72e8599db1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW, 73e8599db1SMatthew Dillon &hammer_debug_inode, 0, ""); 747d683b0fSMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW, 757d683b0fSMatthew Dillon &hammer_debug_locks, 0, ""); 76b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW, 77b3deaf57SMatthew Dillon &hammer_debug_btree, 0, ""); 78d113fda1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW, 79d113fda1SMatthew Dillon &hammer_debug_tid, 0, ""); 80b33e2cc0SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW, 81b33e2cc0SMatthew Dillon &hammer_debug_recover, 0, ""); 8246fe7ae1SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW, 8346fe7ae1SMatthew Dillon &hammer_debug_recover_faults, 0, ""); 849480ff55SMatthew Dillon 859480ff55SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_dirtybufs, CTLFLAG_RW, 869480ff55SMatthew Dillon &hammer_limit_dirtybufs, 0, ""); 879480ff55SMatthew Dillon 88b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD, 89b3deaf57SMatthew Dillon &hammer_count_inodes, 0, ""); 90b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD, 91b3deaf57SMatthew Dillon &hammer_count_records, 0, ""); 92b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD, 93b3deaf57SMatthew Dillon &hammer_count_record_datas, 0, ""); 94b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD, 95b3deaf57SMatthew Dillon &hammer_count_volumes, 0, ""); 96b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD, 97b3deaf57SMatthew Dillon &hammer_count_buffers, 0, ""); 98b3deaf57SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD, 99b3deaf57SMatthew Dillon &hammer_count_nodes, 0, ""); 1009480ff55SMatthew Dillon SYSCTL_INT(_vfs_hammer, OID_AUTO, count_dirtybufs, CTLFLAG_RD, 1019480ff55SMatthew Dillon &hammer_count_dirtybufs, 0, ""); 102f03c9cf4SMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW, 103f03c9cf4SMatthew Dillon &hammer_zone_limit, 0, ""); 1047d683b0fSMatthew Dillon SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW, 1057d683b0fSMatthew Dillon &hammer_contention_count, 0, ""); 106b3deaf57SMatthew Dillon 107427e5fc6SMatthew Dillon /* 108427e5fc6SMatthew Dillon * VFS ABI 109427e5fc6SMatthew Dillon */ 110427e5fc6SMatthew Dillon static void hammer_free_hmp(struct mount *mp); 111427e5fc6SMatthew Dillon 112427e5fc6SMatthew Dillon static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data, 113427e5fc6SMatthew Dillon struct ucred *cred); 114427e5fc6SMatthew Dillon static int hammer_vfs_unmount(struct mount *mp, int mntflags); 115427e5fc6SMatthew Dillon static int hammer_vfs_root(struct mount *mp, struct vnode **vpp); 116427e5fc6SMatthew Dillon static int hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, 117427e5fc6SMatthew Dillon struct ucred *cred); 118427e5fc6SMatthew Dillon static int hammer_vfs_sync(struct mount *mp, int waitfor); 119513ca7d7SMatthew Dillon static int hammer_vfs_vget(struct mount *mp, ino_t ino, 120513ca7d7SMatthew Dillon struct vnode **vpp); 121427e5fc6SMatthew Dillon static int hammer_vfs_init(struct vfsconf *conf); 122513ca7d7SMatthew Dillon static int hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, 123513ca7d7SMatthew Dillon struct vnode **vpp); 124513ca7d7SMatthew Dillon static int hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp); 125513ca7d7SMatthew Dillon static int hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 126513ca7d7SMatthew Dillon int *exflagsp, struct ucred **credanonp); 127513ca7d7SMatthew Dillon 128427e5fc6SMatthew Dillon 129427e5fc6SMatthew Dillon static struct vfsops hammer_vfsops = { 130427e5fc6SMatthew Dillon .vfs_mount = hammer_vfs_mount, 131427e5fc6SMatthew Dillon .vfs_unmount = hammer_vfs_unmount, 132427e5fc6SMatthew Dillon .vfs_root = hammer_vfs_root, 133427e5fc6SMatthew Dillon .vfs_statfs = hammer_vfs_statfs, 134427e5fc6SMatthew Dillon .vfs_sync = hammer_vfs_sync, 135427e5fc6SMatthew Dillon .vfs_vget = hammer_vfs_vget, 136513ca7d7SMatthew Dillon .vfs_init = hammer_vfs_init, 137513ca7d7SMatthew Dillon .vfs_vptofh = hammer_vfs_vptofh, 138513ca7d7SMatthew Dillon .vfs_fhtovp = hammer_vfs_fhtovp, 139513ca7d7SMatthew Dillon .vfs_checkexp = hammer_vfs_checkexp 140427e5fc6SMatthew Dillon }; 141427e5fc6SMatthew Dillon 142427e5fc6SMatthew Dillon MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount"); 143427e5fc6SMatthew Dillon 144427e5fc6SMatthew Dillon VFS_SET(hammer_vfsops, hammer, 0); 145427e5fc6SMatthew Dillon MODULE_VERSION(hammer, 1); 146427e5fc6SMatthew Dillon 147427e5fc6SMatthew Dillon static int 148427e5fc6SMatthew Dillon hammer_vfs_init(struct vfsconf *conf) 149427e5fc6SMatthew Dillon { 15047197d71SMatthew Dillon /*hammer_init_alist_config();*/ 151427e5fc6SMatthew Dillon return(0); 152427e5fc6SMatthew Dillon } 153427e5fc6SMatthew Dillon 154427e5fc6SMatthew Dillon static int 155427e5fc6SMatthew Dillon hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data, 156427e5fc6SMatthew Dillon struct ucred *cred) 157427e5fc6SMatthew Dillon { 158427e5fc6SMatthew Dillon struct hammer_mount_info info; 159a89aec1bSMatthew Dillon hammer_mount_t hmp; 160a89aec1bSMatthew Dillon hammer_volume_t rootvol; 16127ea2398SMatthew Dillon struct vnode *rootvp; 162427e5fc6SMatthew Dillon const char *upath; /* volume name in userspace */ 163427e5fc6SMatthew Dillon char *path; /* volume name in system space */ 164427e5fc6SMatthew Dillon int error; 165427e5fc6SMatthew Dillon int i; 166427e5fc6SMatthew Dillon 167427e5fc6SMatthew Dillon if ((error = copyin(data, &info, sizeof(info))) != 0) 168427e5fc6SMatthew Dillon return (error); 169427e5fc6SMatthew Dillon if (info.nvolumes <= 0 || info.nvolumes >= 32768) 170427e5fc6SMatthew Dillon return (EINVAL); 171427e5fc6SMatthew Dillon 172427e5fc6SMatthew Dillon /* 173427e5fc6SMatthew Dillon * Interal mount data structure 174427e5fc6SMatthew Dillon */ 175195c19a1SMatthew Dillon if (mp->mnt_flag & MNT_UPDATE) { 176195c19a1SMatthew Dillon hmp = (void *)mp->mnt_data; 177195c19a1SMatthew Dillon KKASSERT(hmp != NULL); 178195c19a1SMatthew Dillon } else { 179427e5fc6SMatthew Dillon hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO); 180427e5fc6SMatthew Dillon mp->mnt_data = (qaddr_t)hmp; 181427e5fc6SMatthew Dillon hmp->mp = mp; 182195c19a1SMatthew Dillon hmp->zbuf = kmalloc(HAMMER_BUFSIZE, M_HAMMER, M_WAITOK|M_ZERO); 183195c19a1SMatthew Dillon hmp->namekey_iterator = mycpu->gd_time_seconds; 18446fe7ae1SMatthew Dillon /*TAILQ_INIT(&hmp->recycle_list);*/ 18547197d71SMatthew Dillon 18647197d71SMatthew Dillon hmp->root_btree_beg.obj_id = -0x8000000000000000LL; 18747197d71SMatthew Dillon hmp->root_btree_beg.key = -0x8000000000000000LL; 18847197d71SMatthew Dillon hmp->root_btree_beg.create_tid = 1; 18947197d71SMatthew Dillon hmp->root_btree_beg.delete_tid = 1; 19047197d71SMatthew Dillon hmp->root_btree_beg.rec_type = 0; 19147197d71SMatthew Dillon hmp->root_btree_beg.obj_type = 0; 19247197d71SMatthew Dillon 19347197d71SMatthew Dillon hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL; 19447197d71SMatthew Dillon hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL; 19547197d71SMatthew Dillon hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL; 19647197d71SMatthew Dillon hmp->root_btree_end.delete_tid = 0; /* special case */ 19747197d71SMatthew Dillon hmp->root_btree_end.rec_type = 0xFFFFU; 19847197d71SMatthew Dillon hmp->root_btree_end.obj_type = 0; 19940043e7fSMatthew Dillon lockinit(&hmp->blockmap_lock, "blkmap", 0, 0); 200f03c9cf4SMatthew Dillon 2019480ff55SMatthew Dillon hmp->sync_lock.refs = 1; 202*c9b9e29dSMatthew Dillon hmp->free_lock.refs = 1; 2039480ff55SMatthew Dillon 204059819e3SMatthew Dillon TAILQ_INIT(&hmp->flush_list); 2050729c8c8SMatthew Dillon TAILQ_INIT(&hmp->objid_cache_list); 206e8599db1SMatthew Dillon TAILQ_INIT(&hmp->undo_lru_list); 207059819e3SMatthew Dillon 2080729c8c8SMatthew Dillon /* 2090729c8c8SMatthew Dillon * Set default zone limits. This value can be reduced 2100729c8c8SMatthew Dillon * further by the zone limit specified in the root volume. 2110729c8c8SMatthew Dillon * 2120729c8c8SMatthew Dillon * The sysctl can force a small zone limit for debugging 2130729c8c8SMatthew Dillon * purposes. 2140729c8c8SMatthew Dillon */ 215f03c9cf4SMatthew Dillon for (i = 0; i < HAMMER_MAX_ZONES; ++i) { 216f03c9cf4SMatthew Dillon hmp->zone_limits[i] = 217f03c9cf4SMatthew Dillon HAMMER_ZONE_ENCODE(i, HAMMER_ZONE_LIMIT); 2180729c8c8SMatthew Dillon 219f03c9cf4SMatthew Dillon if (hammer_zone_limit) { 220f03c9cf4SMatthew Dillon hmp->zone_limits[i] = 221f03c9cf4SMatthew Dillon HAMMER_ZONE_ENCODE(i, hammer_zone_limit); 222f03c9cf4SMatthew Dillon } 223bf686dbeSMatthew Dillon hammer_init_holes(hmp, &hmp->holes[i]); 224f03c9cf4SMatthew Dillon } 225195c19a1SMatthew Dillon } 226195c19a1SMatthew Dillon hmp->hflags = info.hflags; 2277f7c1f84SMatthew Dillon if (info.asof) { 2287f7c1f84SMatthew Dillon mp->mnt_flag |= MNT_RDONLY; 2297f7c1f84SMatthew Dillon hmp->asof = info.asof; 2307f7c1f84SMatthew Dillon } else { 2317f7c1f84SMatthew Dillon hmp->asof = HAMMER_MAX_TID; 2327f7c1f84SMatthew Dillon } 233195c19a1SMatthew Dillon 234195c19a1SMatthew Dillon /* 235195c19a1SMatthew Dillon * Re-open read-write if originally read-only, or vise-versa XXX 236195c19a1SMatthew Dillon */ 237195c19a1SMatthew Dillon if (mp->mnt_flag & MNT_UPDATE) { 238195c19a1SMatthew Dillon if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { 239195c19a1SMatthew Dillon kprintf("HAMMER read-write -> read-only XXX\n"); 240195c19a1SMatthew Dillon hmp->ronly = 1; 241195c19a1SMatthew Dillon } else if (hmp->ronly && (mp->mnt_flag & MNT_RDONLY) == 0) { 242195c19a1SMatthew Dillon kprintf("HAMMER read-only -> read-write XXX\n"); 243195c19a1SMatthew Dillon hmp->ronly = 0; 244195c19a1SMatthew Dillon } 245195c19a1SMatthew Dillon return(0); 246195c19a1SMatthew Dillon } 247195c19a1SMatthew Dillon 248427e5fc6SMatthew Dillon RB_INIT(&hmp->rb_vols_root); 249427e5fc6SMatthew Dillon RB_INIT(&hmp->rb_inos_root); 25040043e7fSMatthew Dillon RB_INIT(&hmp->rb_nods_root); 251e8599db1SMatthew Dillon RB_INIT(&hmp->rb_undo_root); 252195c19a1SMatthew Dillon hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0); 253427e5fc6SMatthew Dillon 25410a5d1baSMatthew Dillon TAILQ_INIT(&hmp->volu_list); 25510a5d1baSMatthew Dillon TAILQ_INIT(&hmp->undo_list); 25610a5d1baSMatthew Dillon TAILQ_INIT(&hmp->data_list); 25710a5d1baSMatthew Dillon TAILQ_INIT(&hmp->meta_list); 25810a5d1baSMatthew Dillon TAILQ_INIT(&hmp->lose_list); 25910a5d1baSMatthew Dillon 260427e5fc6SMatthew Dillon /* 261427e5fc6SMatthew Dillon * Load volumes 262427e5fc6SMatthew Dillon */ 263427e5fc6SMatthew Dillon path = objcache_get(namei_oc, M_WAITOK); 264d26d0ae9SMatthew Dillon hmp->nvolumes = info.nvolumes; 265427e5fc6SMatthew Dillon for (i = 0; i < info.nvolumes; ++i) { 266427e5fc6SMatthew Dillon error = copyin(&info.volumes[i], &upath, sizeof(char *)); 267427e5fc6SMatthew Dillon if (error == 0) 268427e5fc6SMatthew Dillon error = copyinstr(upath, path, MAXPATHLEN, NULL); 269427e5fc6SMatthew Dillon if (error == 0) 2708cd0a023SMatthew Dillon error = hammer_install_volume(hmp, path); 271427e5fc6SMatthew Dillon if (error) 272427e5fc6SMatthew Dillon break; 273427e5fc6SMatthew Dillon } 274427e5fc6SMatthew Dillon objcache_put(namei_oc, path); 275427e5fc6SMatthew Dillon 276427e5fc6SMatthew Dillon /* 277427e5fc6SMatthew Dillon * Make sure we found a root volume 278427e5fc6SMatthew Dillon */ 279427e5fc6SMatthew Dillon if (error == 0 && hmp->rootvol == NULL) { 280427e5fc6SMatthew Dillon kprintf("hammer_mount: No root volume found!\n"); 281427e5fc6SMatthew Dillon error = EINVAL; 282427e5fc6SMatthew Dillon } 283427e5fc6SMatthew Dillon if (error) { 284427e5fc6SMatthew Dillon hammer_free_hmp(mp); 285427e5fc6SMatthew Dillon return (error); 286427e5fc6SMatthew Dillon } 287427e5fc6SMatthew Dillon 288427e5fc6SMatthew Dillon /* 28927ea2398SMatthew Dillon * No errors, setup enough of the mount point so we can lookup the 29027ea2398SMatthew Dillon * root vnode. 291427e5fc6SMatthew Dillon */ 292427e5fc6SMatthew Dillon mp->mnt_iosize_max = MAXPHYS; 293427e5fc6SMatthew Dillon mp->mnt_kern_flag |= MNTK_FSMID; 294c0ade690SMatthew Dillon 295c0ade690SMatthew Dillon /* 296c0ade690SMatthew Dillon * note: f_iosize is used by vnode_pager_haspage() when constructing 297c0ade690SMatthew Dillon * its VOP_BMAP call. 298c0ade690SMatthew Dillon */ 299c0ade690SMatthew Dillon mp->mnt_stat.f_iosize = HAMMER_BUFSIZE; 300fbc6e32aSMatthew Dillon mp->mnt_stat.f_bsize = HAMMER_BUFSIZE; 301427e5fc6SMatthew Dillon mp->mnt_maxsymlinklen = 255; 302427e5fc6SMatthew Dillon mp->mnt_flag |= MNT_LOCAL; 303427e5fc6SMatthew Dillon 304427e5fc6SMatthew Dillon vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops); 3057a04d74fSMatthew Dillon vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops); 3067a04d74fSMatthew Dillon vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops); 30727ea2398SMatthew Dillon 30827ea2398SMatthew Dillon /* 309a89aec1bSMatthew Dillon * The root volume's ondisk pointer is only valid if we hold a 310a89aec1bSMatthew Dillon * reference to it. 311a89aec1bSMatthew Dillon */ 312a89aec1bSMatthew Dillon rootvol = hammer_get_root_volume(hmp, &error); 313a89aec1bSMatthew Dillon if (error) 314f90dde4cSMatthew Dillon goto failed; 315f90dde4cSMatthew Dillon 316f90dde4cSMatthew Dillon /* 3170729c8c8SMatthew Dillon * Perform any necessary UNDO operations. The recover code does 3180729c8c8SMatthew Dillon * call hammer_undo_lookup() so we have to pre-cache the blockmap, 3190729c8c8SMatthew Dillon * and then re-copy it again after recovery is complete. 320*c9b9e29dSMatthew Dillon * 321*c9b9e29dSMatthew Dillon * The recover code will load hmp->flusher_undo_start. 322f90dde4cSMatthew Dillon */ 3230729c8c8SMatthew Dillon bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap, 3240729c8c8SMatthew Dillon sizeof(hmp->blockmap)); 325*c9b9e29dSMatthew Dillon 326f90dde4cSMatthew Dillon error = hammer_recover(hmp, rootvol); 327f90dde4cSMatthew Dillon if (error) { 328f90dde4cSMatthew Dillon kprintf("Failed to recover HAMMER filesystem on mount\n"); 329a89aec1bSMatthew Dillon goto done; 330f90dde4cSMatthew Dillon } 331f90dde4cSMatthew Dillon 332f90dde4cSMatthew Dillon /* 333f90dde4cSMatthew Dillon * Finish setup now that we have a good root volume 334f90dde4cSMatthew Dillon */ 335a89aec1bSMatthew Dillon ksnprintf(mp->mnt_stat.f_mntfromname, 336a89aec1bSMatthew Dillon sizeof(mp->mnt_stat.f_mntfromname), "%s", 337a89aec1bSMatthew Dillon rootvol->ondisk->vol_name); 338513ca7d7SMatthew Dillon mp->mnt_stat.f_fsid.val[0] = 339513ca7d7SMatthew Dillon crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8); 340513ca7d7SMatthew Dillon mp->mnt_stat.f_fsid.val[1] = 341513ca7d7SMatthew Dillon crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8); 342b84de5afSMatthew Dillon 3430729c8c8SMatthew Dillon /* 3440729c8c8SMatthew Dillon * Certain often-modified fields in the root volume are cached in 3450729c8c8SMatthew Dillon * the hammer_mount structure so we do not have to generate lots 3460729c8c8SMatthew Dillon * of little UNDO structures for them. 347*c9b9e29dSMatthew Dillon * 348*c9b9e29dSMatthew Dillon * Recopy after recovery. 3490729c8c8SMatthew Dillon */ 350b84de5afSMatthew Dillon hmp->next_tid = rootvol->ondisk->vol0_next_tid; 3510729c8c8SMatthew Dillon bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap, 3520729c8c8SMatthew Dillon sizeof(hmp->blockmap)); 3530729c8c8SMatthew Dillon 3540729c8c8SMatthew Dillon /* 3550729c8c8SMatthew Dillon * Use the zone limit set by newfs_hammer, or the zone limit set by 3560729c8c8SMatthew Dillon * sysctl (for debugging), whichever is smaller. 3570729c8c8SMatthew Dillon */ 3580729c8c8SMatthew Dillon if (rootvol->ondisk->vol0_zone_limit) { 3590729c8c8SMatthew Dillon hammer_off_t vol0_zone_limit; 3600729c8c8SMatthew Dillon 3610729c8c8SMatthew Dillon vol0_zone_limit = rootvol->ondisk->vol0_zone_limit; 3620729c8c8SMatthew Dillon for (i = 0; i < HAMMER_MAX_ZONES; ++i) { 3630729c8c8SMatthew Dillon if (hmp->zone_limits[i] > vol0_zone_limit) 3640729c8c8SMatthew Dillon hmp->zone_limits[i] = vol0_zone_limit; 3650729c8c8SMatthew Dillon } 3660729c8c8SMatthew Dillon } 367b84de5afSMatthew Dillon 368059819e3SMatthew Dillon hammer_flusher_create(hmp); 369059819e3SMatthew Dillon 370a89aec1bSMatthew Dillon /* 37127ea2398SMatthew Dillon * Locate the root directory using the root cluster's B-Tree as a 37227ea2398SMatthew Dillon * starting point. The root directory uses an obj_id of 1. 37327ea2398SMatthew Dillon * 37427ea2398SMatthew Dillon * FUTURE: Leave the root directory cached referenced but unlocked 37527ea2398SMatthew Dillon * in hmp->rootvp (need to flush it on unmount). 37627ea2398SMatthew Dillon */ 37727ea2398SMatthew Dillon error = hammer_vfs_vget(mp, 1, &rootvp); 378a89aec1bSMatthew Dillon if (error) 379a89aec1bSMatthew Dillon goto done; 38027ea2398SMatthew Dillon vput(rootvp); 38127ea2398SMatthew Dillon /*vn_unlock(hmp->rootvp);*/ 38227ea2398SMatthew Dillon 383a89aec1bSMatthew Dillon done: 384f90dde4cSMatthew Dillon hammer_rel_volume(rootvol, 0); 385f90dde4cSMatthew Dillon failed: 38627ea2398SMatthew Dillon /* 38727ea2398SMatthew Dillon * Cleanup and return. 38827ea2398SMatthew Dillon */ 38927ea2398SMatthew Dillon if (error) 39027ea2398SMatthew Dillon hammer_free_hmp(mp); 391427e5fc6SMatthew Dillon return (error); 392427e5fc6SMatthew Dillon } 393427e5fc6SMatthew Dillon 394427e5fc6SMatthew Dillon static int 395427e5fc6SMatthew Dillon hammer_vfs_unmount(struct mount *mp, int mntflags) 396427e5fc6SMatthew Dillon { 397427e5fc6SMatthew Dillon #if 0 398427e5fc6SMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 399427e5fc6SMatthew Dillon #endif 400427e5fc6SMatthew Dillon int flags; 40166325755SMatthew Dillon int error; 40227ea2398SMatthew Dillon 40327ea2398SMatthew Dillon /* 404427e5fc6SMatthew Dillon * Clean out the vnodes 405427e5fc6SMatthew Dillon */ 40666325755SMatthew Dillon flags = 0; 40766325755SMatthew Dillon if (mntflags & MNT_FORCE) 40866325755SMatthew Dillon flags |= FORCECLOSE; 40966325755SMatthew Dillon if ((error = vflush(mp, 0, flags)) != 0) 41066325755SMatthew Dillon return (error); 411427e5fc6SMatthew Dillon 412427e5fc6SMatthew Dillon /* 413427e5fc6SMatthew Dillon * Clean up the internal mount structure and related entities. This 414427e5fc6SMatthew Dillon * may issue I/O. 415427e5fc6SMatthew Dillon */ 416427e5fc6SMatthew Dillon hammer_free_hmp(mp); 417427e5fc6SMatthew Dillon return(0); 418427e5fc6SMatthew Dillon } 419427e5fc6SMatthew Dillon 420427e5fc6SMatthew Dillon /* 421427e5fc6SMatthew Dillon * Clean up the internal mount structure and disassociate it from the mount. 422427e5fc6SMatthew Dillon * This may issue I/O. 423427e5fc6SMatthew Dillon */ 424427e5fc6SMatthew Dillon static void 425427e5fc6SMatthew Dillon hammer_free_hmp(struct mount *mp) 426427e5fc6SMatthew Dillon { 427427e5fc6SMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 428bf686dbeSMatthew Dillon int i; 429427e5fc6SMatthew Dillon 43027ea2398SMatthew Dillon #if 0 431427e5fc6SMatthew Dillon /* 432427e5fc6SMatthew Dillon * Clean up the root vnode 433427e5fc6SMatthew Dillon */ 434427e5fc6SMatthew Dillon if (hmp->rootvp) { 435427e5fc6SMatthew Dillon vrele(hmp->rootvp); 436427e5fc6SMatthew Dillon hmp->rootvp = NULL; 437427e5fc6SMatthew Dillon } 43827ea2398SMatthew Dillon #endif 439059819e3SMatthew Dillon hammer_flusher_sync(hmp); 440b84de5afSMatthew Dillon hammer_flusher_sync(hmp); 441059819e3SMatthew Dillon hammer_flusher_destroy(hmp); 442427e5fc6SMatthew Dillon 443b84de5afSMatthew Dillon KKASSERT(RB_EMPTY(&hmp->rb_inos_root)); 444b84de5afSMatthew Dillon 445b84de5afSMatthew Dillon #if 0 446427e5fc6SMatthew Dillon /* 447427e5fc6SMatthew Dillon * Unload & flush inodes 448b84de5afSMatthew Dillon * 449b84de5afSMatthew Dillon * XXX illegal to call this from here, it can only be done from 450b84de5afSMatthew Dillon * the flusher. 451427e5fc6SMatthew Dillon */ 452427e5fc6SMatthew Dillon RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL, 453d113fda1SMatthew Dillon hammer_unload_inode, (void *)MNT_WAIT); 454427e5fc6SMatthew Dillon 455427e5fc6SMatthew Dillon /* 456427e5fc6SMatthew Dillon * Unload & flush volumes 457427e5fc6SMatthew Dillon */ 458b84de5afSMatthew Dillon #endif 459b84de5afSMatthew Dillon /* 460b84de5afSMatthew Dillon * Unload the volumes 461b84de5afSMatthew Dillon */ 462427e5fc6SMatthew Dillon RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL, 463427e5fc6SMatthew Dillon hammer_unload_volume, NULL); 464427e5fc6SMatthew Dillon 465427e5fc6SMatthew Dillon mp->mnt_data = NULL; 46666325755SMatthew Dillon mp->mnt_flag &= ~MNT_LOCAL; 467427e5fc6SMatthew Dillon hmp->mp = NULL; 4680729c8c8SMatthew Dillon hammer_destroy_objid_cache(hmp); 46966325755SMatthew Dillon kfree(hmp->zbuf, M_HAMMER); 47040043e7fSMatthew Dillon lockuninit(&hmp->blockmap_lock); 471bf686dbeSMatthew Dillon 472bf686dbeSMatthew Dillon for (i = 0; i < HAMMER_MAX_ZONES; ++i) 473bf686dbeSMatthew Dillon hammer_free_holes(hmp, &hmp->holes[i]); 474bf686dbeSMatthew Dillon 475427e5fc6SMatthew Dillon kfree(hmp, M_HAMMER); 476427e5fc6SMatthew Dillon } 477427e5fc6SMatthew Dillon 478427e5fc6SMatthew Dillon /* 479513ca7d7SMatthew Dillon * Obtain a vnode for the specified inode number. An exclusively locked 480513ca7d7SMatthew Dillon * vnode is returned. 481513ca7d7SMatthew Dillon */ 482513ca7d7SMatthew Dillon int 483513ca7d7SMatthew Dillon hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 484513ca7d7SMatthew Dillon { 48536f82b23SMatthew Dillon struct hammer_transaction trans; 486513ca7d7SMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 487513ca7d7SMatthew Dillon struct hammer_inode *ip; 488513ca7d7SMatthew Dillon int error; 489513ca7d7SMatthew Dillon 49036f82b23SMatthew Dillon hammer_simple_transaction(&trans, hmp); 49136f82b23SMatthew Dillon 492513ca7d7SMatthew Dillon /* 49336f82b23SMatthew Dillon * Lookup the requested HAMMER inode. The structure must be 49436f82b23SMatthew Dillon * left unlocked while we manipulate the related vnode to avoid 49536f82b23SMatthew Dillon * a deadlock. 496513ca7d7SMatthew Dillon */ 49736f82b23SMatthew Dillon ip = hammer_get_inode(&trans, NULL, ino, hmp->asof, 0, &error); 498513ca7d7SMatthew Dillon if (ip == NULL) { 499513ca7d7SMatthew Dillon *vpp = NULL; 500513ca7d7SMatthew Dillon return(error); 501513ca7d7SMatthew Dillon } 502e8599db1SMatthew Dillon error = hammer_get_vnode(ip, vpp); 503513ca7d7SMatthew Dillon hammer_rel_inode(ip, 0); 504b84de5afSMatthew Dillon hammer_done_transaction(&trans); 505513ca7d7SMatthew Dillon return (error); 506513ca7d7SMatthew Dillon } 507513ca7d7SMatthew Dillon 508513ca7d7SMatthew Dillon /* 509427e5fc6SMatthew Dillon * Return the root vnode for the filesystem. 510427e5fc6SMatthew Dillon * 511427e5fc6SMatthew Dillon * HAMMER stores the root vnode in the hammer_mount structure so 512427e5fc6SMatthew Dillon * getting it is easy. 513427e5fc6SMatthew Dillon */ 514427e5fc6SMatthew Dillon static int 515427e5fc6SMatthew Dillon hammer_vfs_root(struct mount *mp, struct vnode **vpp) 516427e5fc6SMatthew Dillon { 51747197d71SMatthew Dillon #if 0 518427e5fc6SMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 51947197d71SMatthew Dillon #endif 52027ea2398SMatthew Dillon int error; 521427e5fc6SMatthew Dillon 52227ea2398SMatthew Dillon error = hammer_vfs_vget(mp, 1, vpp); 52327ea2398SMatthew Dillon return (error); 524427e5fc6SMatthew Dillon } 525427e5fc6SMatthew Dillon 526427e5fc6SMatthew Dillon static int 527427e5fc6SMatthew Dillon hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 528427e5fc6SMatthew Dillon { 529fbc6e32aSMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 530fbc6e32aSMatthew Dillon hammer_volume_t volume; 531fbc6e32aSMatthew Dillon hammer_volume_ondisk_t ondisk; 532fbc6e32aSMatthew Dillon int error; 53347197d71SMatthew Dillon int64_t bfree; 534fbc6e32aSMatthew Dillon 535fbc6e32aSMatthew Dillon volume = hammer_get_root_volume(hmp, &error); 536fbc6e32aSMatthew Dillon if (error) 537fbc6e32aSMatthew Dillon return(error); 538fbc6e32aSMatthew Dillon ondisk = volume->ondisk; 539fbc6e32aSMatthew Dillon 54047197d71SMatthew Dillon /* 54147197d71SMatthew Dillon * Basic stats 54247197d71SMatthew Dillon */ 543fbc6e32aSMatthew Dillon mp->mnt_stat.f_files = ondisk->vol0_stat_inodes; 544c3be93f2SMatthew Dillon bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE; 54540043e7fSMatthew Dillon hammer_rel_volume(volume, 0); 54647197d71SMatthew Dillon 54747197d71SMatthew Dillon mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE; 54847197d71SMatthew Dillon mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree; 549fbc6e32aSMatthew Dillon if (mp->mnt_stat.f_files < 0) 550fbc6e32aSMatthew Dillon mp->mnt_stat.f_files = 0; 551fbc6e32aSMatthew Dillon 55227ea2398SMatthew Dillon *sbp = mp->mnt_stat; 55327ea2398SMatthew Dillon return(0); 554427e5fc6SMatthew Dillon } 555427e5fc6SMatthew Dillon 5560729c8c8SMatthew Dillon /* 5570729c8c8SMatthew Dillon * Sync the filesystem. Currently we have to run it twice, the second 5580729c8c8SMatthew Dillon * one will advance the undo start index to the end index, so if a crash 5590729c8c8SMatthew Dillon * occurs no undos will be run on mount. 5600729c8c8SMatthew Dillon */ 561427e5fc6SMatthew Dillon static int 562427e5fc6SMatthew Dillon hammer_vfs_sync(struct mount *mp, int waitfor) 563427e5fc6SMatthew Dillon { 564fbc6e32aSMatthew Dillon struct hammer_mount *hmp = (void *)mp->mnt_data; 5650729c8c8SMatthew Dillon int error; 5660729c8c8SMatthew Dillon 5670729c8c8SMatthew Dillon error = hammer_sync_hmp(hmp, waitfor); 5680729c8c8SMatthew Dillon if (error == 0) 5690729c8c8SMatthew Dillon error = hammer_sync_hmp(hmp, waitfor); 5700729c8c8SMatthew Dillon return (error); 571427e5fc6SMatthew Dillon } 572427e5fc6SMatthew Dillon 573513ca7d7SMatthew Dillon /* 574513ca7d7SMatthew Dillon * Convert a vnode to a file handle. 575513ca7d7SMatthew Dillon */ 576513ca7d7SMatthew Dillon static int 577513ca7d7SMatthew Dillon hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp) 578513ca7d7SMatthew Dillon { 579513ca7d7SMatthew Dillon hammer_inode_t ip; 580513ca7d7SMatthew Dillon 581513ca7d7SMatthew Dillon KKASSERT(MAXFIDSZ >= 16); 582513ca7d7SMatthew Dillon ip = VTOI(vp); 583513ca7d7SMatthew Dillon fhp->fid_len = offsetof(struct fid, fid_data[16]); 584513ca7d7SMatthew Dillon fhp->fid_reserved = 0; 585513ca7d7SMatthew Dillon bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id)); 586513ca7d7SMatthew Dillon bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof)); 587513ca7d7SMatthew Dillon return(0); 588513ca7d7SMatthew Dillon } 589513ca7d7SMatthew Dillon 590513ca7d7SMatthew Dillon 591513ca7d7SMatthew Dillon /* 592513ca7d7SMatthew Dillon * Convert a file handle back to a vnode. 593513ca7d7SMatthew Dillon */ 594513ca7d7SMatthew Dillon static int 595513ca7d7SMatthew Dillon hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 596513ca7d7SMatthew Dillon { 59736f82b23SMatthew Dillon struct hammer_transaction trans; 598513ca7d7SMatthew Dillon struct hammer_inode *ip; 599513ca7d7SMatthew Dillon struct hammer_inode_info info; 600513ca7d7SMatthew Dillon int error; 601513ca7d7SMatthew Dillon 602513ca7d7SMatthew Dillon bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id)); 603513ca7d7SMatthew Dillon bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof)); 604513ca7d7SMatthew Dillon 60536f82b23SMatthew Dillon hammer_simple_transaction(&trans, (void *)mp->mnt_data); 60636f82b23SMatthew Dillon 607513ca7d7SMatthew Dillon /* 608513ca7d7SMatthew Dillon * Get/allocate the hammer_inode structure. The structure must be 609513ca7d7SMatthew Dillon * unlocked while we manipulate the related vnode to avoid a 610513ca7d7SMatthew Dillon * deadlock. 611513ca7d7SMatthew Dillon */ 61236f82b23SMatthew Dillon ip = hammer_get_inode(&trans, NULL, info.obj_id, info.obj_asof, 61336f82b23SMatthew Dillon 0, &error); 614513ca7d7SMatthew Dillon if (ip == NULL) { 615513ca7d7SMatthew Dillon *vpp = NULL; 616513ca7d7SMatthew Dillon return(error); 617513ca7d7SMatthew Dillon } 618e8599db1SMatthew Dillon error = hammer_get_vnode(ip, vpp); 619513ca7d7SMatthew Dillon hammer_rel_inode(ip, 0); 620b84de5afSMatthew Dillon hammer_done_transaction(&trans); 621513ca7d7SMatthew Dillon return (error); 622513ca7d7SMatthew Dillon } 623513ca7d7SMatthew Dillon 624513ca7d7SMatthew Dillon static int 625513ca7d7SMatthew Dillon hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam, 626513ca7d7SMatthew Dillon int *exflagsp, struct ucred **credanonp) 627513ca7d7SMatthew Dillon { 628513ca7d7SMatthew Dillon hammer_mount_t hmp = (void *)mp->mnt_data; 629513ca7d7SMatthew Dillon struct netcred *np; 630513ca7d7SMatthew Dillon int error; 631513ca7d7SMatthew Dillon 632513ca7d7SMatthew Dillon np = vfs_export_lookup(mp, &hmp->export, nam); 633513ca7d7SMatthew Dillon if (np) { 634513ca7d7SMatthew Dillon *exflagsp = np->netc_exflags; 635513ca7d7SMatthew Dillon *credanonp = &np->netc_anon; 636513ca7d7SMatthew Dillon error = 0; 637513ca7d7SMatthew Dillon } else { 638513ca7d7SMatthew Dillon error = EACCES; 639513ca7d7SMatthew Dillon } 640513ca7d7SMatthew Dillon return (error); 641513ca7d7SMatthew Dillon 642513ca7d7SMatthew Dillon } 643513ca7d7SMatthew Dillon 644513ca7d7SMatthew Dillon int 645513ca7d7SMatthew Dillon hammer_vfs_export(struct mount *mp, int op, const struct export_args *export) 646513ca7d7SMatthew Dillon { 647513ca7d7SMatthew Dillon hammer_mount_t hmp = (void *)mp->mnt_data; 648513ca7d7SMatthew Dillon int error; 649513ca7d7SMatthew Dillon 650513ca7d7SMatthew Dillon switch(op) { 651513ca7d7SMatthew Dillon case MOUNTCTL_SET_EXPORT: 652513ca7d7SMatthew Dillon error = vfs_export(mp, &hmp->export, export); 653513ca7d7SMatthew Dillon break; 654513ca7d7SMatthew Dillon default: 655513ca7d7SMatthew Dillon error = EOPNOTSUPP; 656513ca7d7SMatthew Dillon break; 657513ca7d7SMatthew Dillon } 658513ca7d7SMatthew Dillon return(error); 659513ca7d7SMatthew Dillon } 660513ca7d7SMatthew Dillon 661