zfs_znode.c revision 67bd71c6cc629bab3aa0d595c624a667f1574254
355b4669e025ff377602b6fc7caaf30dbc218371jacobs/*
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * CDDL HEADER START
355b4669e025ff377602b6fc7caaf30dbc218371jacobs *
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * The contents of this file are subject to the terms of the
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * Common Development and Distribution License (the "License").
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * You may not use this file except in compliance with the License.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs *
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * or http://www.opensolaris.org/os/licensing.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * See the License for the specific language governing permissions
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * and limitations under the License.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs *
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * When distributing Covered Code, include this CDDL HEADER in each
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * If applicable, add the following below this CDDL HEADER, with the
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * fields enclosed by brackets "[]" replaced with your own identifying
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * information: Portions Copyright [yyyy] [name of copyright owner]
355b4669e025ff377602b6fc7caaf30dbc218371jacobs *
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * CDDL HEADER END
355b4669e025ff377602b6fc7caaf30dbc218371jacobs */
355b4669e025ff377602b6fc7caaf30dbc218371jacobs/*
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
43b9c05035ac59f7f7a8e7827598db5a15f30ed3jacobs * Use is subject to license terms.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs */
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#pragma ident "%Z%%M% %I% %E% SMI"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/types.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/param.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/time.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/systm.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/sysmacros.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/resource.h>
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#include <sys/mntent.h>
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#include <sys/mkdev.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/vfs.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/vnode.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/file.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/kmem.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/cmn_err.h>
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#include <sys/errno.h>
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#include <sys/unistd.h>
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#include <sys/stat.h>
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#include <sys/mode.h>
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#include <sys/atomic.h>
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#include <vm/pvn.h>
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#include "fs/fs_subr.h"
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#include <sys/zfs_dir.h>
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#include <sys/zfs_acl.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/zfs_ioctl.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/zfs_znode.h>
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#include <sys/zfs_rlock.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/zap.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/dmu.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sys/fs/zfs.h>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobsstruct kmem_cache *znode_cache = NULL;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs/*ARGSUSED*/
355b4669e025ff377602b6fc7caaf30dbc218371jacobsstatic void
355b4669e025ff377602b6fc7caaf30dbc218371jacobsznode_pageout_func(dmu_buf_t *dbuf, void *user_ptr)
355b4669e025ff377602b6fc7caaf30dbc218371jacobs{
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs znode_t *zp = user_ptr;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vnode_t *vp = ZTOV(zp);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_enter(&zp->z_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (vp->v_count == 0) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_exit(&zp->z_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_invalid(vp);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfs_znode_free(zp);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs } else {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /* signal force unmount that this znode can be freed */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_dbuf = NULL;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_exit(&zp->z_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs}
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs/*ARGSUSED*/
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsstatic int
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobszfs_znode_cache_constructor(void *buf, void *cdrarg, int kmflags)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs znode_t *zp = buf;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_vnode = vn_alloc(KM_SLEEP);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_vnode->v_data = (caddr_t)zp;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs rw_init(&zp->z_map_lock, NULL, RW_DEFAULT, NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs avl_create(&zp->z_range_avl, zfs_range_compare,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs sizeof (rl_t), offsetof(rl_t, r_node));
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_dbuf_held = 0;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_dirlocks = 0;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (0);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs}
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs/*ARGSUSED*/
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsstatic void
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobszfs_znode_cache_destructor(void *buf, void *cdarg)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs znode_t *zp = buf;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT(zp->z_dirlocks == 0);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_destroy(&zp->z_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs rw_destroy(&zp->z_map_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs rw_destroy(&zp->z_parent_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_destroy(&zp->z_acl_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs avl_destroy(&zp->z_range_avl);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT(zp->z_dbuf_held == 0);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT(ZTOV(zp)->v_count == 0);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_free(ZTOV(zp));
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs}
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobsvoid
355b4669e025ff377602b6fc7caaf30dbc218371jacobszfs_znode_init(void)
355b4669e025ff377602b6fc7caaf30dbc218371jacobs{
355b4669e025ff377602b6fc7caaf30dbc218371jacobs /*
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * Initialize zcache
355b4669e025ff377602b6fc7caaf30dbc218371jacobs */
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ASSERT(znode_cache == NULL);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs znode_cache = kmem_cache_create("zfs_znode_cache",
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs sizeof (znode_t), 0, zfs_znode_cache_constructor,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs}
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsvoid
355b4669e025ff377602b6fc7caaf30dbc218371jacobszfs_znode_fini(void)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
355b4669e025ff377602b6fc7caaf30dbc218371jacobs /*
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * Cleanup vfs & vnode ops
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfs_remove_op_tables();
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Cleanup zcache
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (znode_cache)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs kmem_cache_destroy(znode_cache);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs znode_cache = NULL;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs}
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobsstruct vnodeops *zfs_dvnodeops;
355b4669e025ff377602b6fc7caaf30dbc218371jacobsstruct vnodeops *zfs_fvnodeops;
355b4669e025ff377602b6fc7caaf30dbc218371jacobsstruct vnodeops *zfs_symvnodeops;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsstruct vnodeops *zfs_xdvnodeops;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsstruct vnodeops *zfs_evnodeops;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsvoid
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobszfs_remove_op_tables()
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Remove vfs ops
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT(zfsfstype);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs (void) vfs_freevfsops_by_type(zfsfstype);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfsfstype = 0;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Remove vnode ops
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zfs_dvnodeops)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_freevnodeops(zfs_dvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zfs_fvnodeops)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_freevnodeops(zfs_fvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zfs_symvnodeops)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_freevnodeops(zfs_symvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zfs_xdvnodeops)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_freevnodeops(zfs_xdvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zfs_evnodeops)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_freevnodeops(zfs_evnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfs_dvnodeops = NULL;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfs_fvnodeops = NULL;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfs_symvnodeops = NULL;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfs_xdvnodeops = NULL;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfs_evnodeops = NULL;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs}
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsextern const fs_operation_def_t zfs_dvnodeops_template[];
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsextern const fs_operation_def_t zfs_fvnodeops_template[];
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsextern const fs_operation_def_t zfs_xdvnodeops_template[];
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsextern const fs_operation_def_t zfs_symvnodeops_template[];
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsextern const fs_operation_def_t zfs_evnodeops_template[];
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsint
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobszfs_create_op_tables()
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs int error;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * In this case we just return as the ops vectors are already set up.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zfs_dvnodeops)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (0);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs &zfs_dvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (error)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (error);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs &zfs_fvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (error)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (error);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs &zfs_symvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (error)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (error);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs &zfs_xdvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (error)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (error);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs &zfs_evnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (error);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs}
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs/*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * zfs_init_fs - Initialize the zfsvfs struct and the file system
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * incore "master" object. Verify version compatibility.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsint
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobszfs_init_fs(zfsvfs_t *zfsvfs, znode_t **zpp, cred_t *cr)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs extern int zfsfstype;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs objset_t *os = zfsvfs->z_os;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs uint64_t zoid;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs uint64_t version = ZPL_VERSION;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs int i, error;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dmu_object_info_t doi;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs uint64_t fsid_guid;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs *zpp = NULL;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * XXX - hack to auto-create the pool root filesystem at
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * the first attempted mount.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (dmu_object_info(os, MASTER_NODE_OBJ, &doi) == ENOENT) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dmu_tx_t *tx = dmu_tx_create(os);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* master */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* del queue */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); /* root node */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs error = dmu_tx_assign(tx, TXG_WAIT);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT3U(error, ==, 0);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfs_create_fs(os, cr, tx);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dmu_tx_commit(tx);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_OBJ, 8, 1,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs &version);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (error) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (error);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs } else if (version != ZPL_VERSION) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs (void) printf("Mismatched versions: File system "
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs "is version %lld on-disk format, which is "
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs "incompatible with this software version %lld!",
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs (u_longlong_t)version, ZPL_VERSION);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (ENOTSUP);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * The fsid is 64 bits, composed of an 8-bit fs type, which
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * separates our fsid from any other filesystem types, and a
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * 56-bit objset unique ID. The objset unique ID is unique to
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * all objsets open on this system, provided by unique_create().
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * The 8-bit fs type must be put in the low bits of fsid[1]
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * because that's where other Solaris filesystems put it.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs fsid_guid = dmu_objset_fsid_guid(os);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfsvfs->z_vfs->vfs_fsid.val[0] = fsid_guid;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfsvfs->z_vfs->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfsfstype & 0xFF;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &zoid);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (error)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (error);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT(zoid != 0);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfsvfs->z_root = zoid;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Create the per mount vop tables.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Initialize zget mutex's
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs error = zfs_zget(zfsvfs, zoid, zpp);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (error)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (error);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT3U((*zpp)->z_id, ==, zoid);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_DELETE_QUEUE, 8, 1, &zoid);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (error)
43b9c05035ac59f7f7a8e7827598db5a15f30ed3jacobs return (error);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfsvfs->z_dqueue = zoid;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Initialize delete head structure
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Thread(s) will be started/stopped via
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * readonly_changed_cb() depending
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * on whether this is rw/ro mount.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs list_create(&zfsvfs->z_delete_head.z_znodes,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs sizeof (znode_t), offsetof(znode_t, z_list_node));
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /* Mutex never destroyed. */
43b9c05035ac59f7f7a8e7827598db5a15f30ed3jacobs mutex_init(&zfsvfs->z_delete_head.z_mutex, NULL, MUTEX_DEFAULT, NULL);
43b9c05035ac59f7f7a8e7827598db5a15f30ed3jacobs
43b9c05035ac59f7f7a8e7827598db5a15f30ed3jacobs return (0);
43b9c05035ac59f7f7a8e7827598db5a15f30ed3jacobs}
43b9c05035ac59f7f7a8e7827598db5a15f30ed3jacobs
43b9c05035ac59f7f7a8e7827598db5a15f30ed3jacobs/*
43b9c05035ac59f7f7a8e7827598db5a15f30ed3jacobs * define a couple of values we need available
43b9c05035ac59f7f7a8e7827598db5a15f30ed3jacobs * for both 64 and 32 bit environments.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#ifndef NBITSMINOR64
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#define NBITSMINOR64 32
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#endif
43b9c05035ac59f7f7a8e7827598db5a15f30ed3jacobs#ifndef MAXMAJ64
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#define MAXMAJ64 0xffffffffUL
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#endif
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#ifndef MAXMIN64
43b9c05035ac59f7f7a8e7827598db5a15f30ed3jacobs#define MAXMIN64 0xffffffffUL
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#endif
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs/*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Create special expldev for ZFS private use.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Can't use standard expldev since it doesn't do
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * what we want. The standard expldev() takes a
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * dev32_t in LP64 and expands it to a long dev_t.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * We need an interface that takes a dev32_t in ILP32
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * and expands it to a long dev_t.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsstatic uint64_t
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobszfs_expldev(dev_t dev)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#ifndef _LP64
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (((uint64_t)major << NBITSMINOR64) |
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ((minor_t)dev & MAXMIN32));
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#else
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (dev);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#endif
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs}
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs/*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Special cmpldev for ZFS private use.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Can't use standard cmpldev since it takes
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * a long dev_t and compresses it to dev32_t in
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * LP64. We need to do a compaction of a long dev_t
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * to a dev32_t in ILP32.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsdev_t
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobszfs_cmpldev(uint64_t dev)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#ifndef _LP64
355b4669e025ff377602b6fc7caaf30dbc218371jacobs minor_t minor = (minor_t)dev & MAXMIN64;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (major > MAXMAJ32 || minor > MAXMIN32)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (NODEV32);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (((dev32_t)major << NBITSMINOR32) | minor);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#else
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (dev);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs#endif
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs}
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs/*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Construct a new znode/vnode and intialize.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs *
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * This does not do a call to dmu_set_user() that is
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * up to the caller to do, in case you don't want to
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * return the znode
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsstatic znode_t *
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobszfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, uint64_t obj_num, int blksz)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs znode_t *zp;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vnode_t *vp;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT(zp->z_dirlocks == NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_phys = db->db_data;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_zfsvfs = zfsvfs;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_reap = 0;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_atime_dirty = 0;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_dbuf_held = 0;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_mapcnt = 0;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_last_itx = 0;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_dbuf = db;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_id = obj_num;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_blksz = blksz;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_seq = 0x7A4653;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_sync_cnt = 0;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_enter(&zfsvfs->z_znodes_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs list_insert_tail(&zfsvfs->z_all_znodes, zp);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_exit(&zfsvfs->z_znodes_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vp = ZTOV(zp);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_reinit(vp);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vp->v_vfsp = zfsvfs->z_parent->z_vfs;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs switch (vp->v_type) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs case VDIR:
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zp->z_phys->zp_flags & ZFS_XATTR) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_setops(vp, zfs_xdvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vp->v_flag |= V_XATTRDIR;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs } else
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_setops(vp, zfs_dvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs break;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs case VBLK:
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs case VCHR:
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vp->v_rdev = zfs_cmpldev(zp->z_phys->zp_rdev);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*FALLTHROUGH*/
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs case VFIFO:
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs case VSOCK:
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs case VDOOR:
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_setops(vp, zfs_fvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs break;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs case VREG:
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vp->v_flag |= VMODSORT;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_setops(vp, zfs_fvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs break;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs case VLNK:
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_setops(vp, zfs_symvnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs break;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs default:
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_setops(vp, zfs_evnodeops);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs break;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (zp);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs}
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsstatic void
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobszfs_znode_dmu_init(znode_t *zp)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs znode_t *nzp;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfsvfs_t *zfsvfs = zp->z_zfsvfs;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dmu_buf_t *db = zp->z_dbuf;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_enter(&zp->z_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs nzp = dmu_buf_set_user(db, zp, &zp->z_phys, znode_pageout_func);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * there should be no
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * concurrent zgets on this object.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT3P(nzp, ==, NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Slap on VROOT if we are the root znode
d978af5cd9e717f0cb0742b6e940c8fdd836504djacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zp->z_id == zfsvfs->z_root) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZTOV(zp)->v_flag |= VROOT;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT(zp->z_dbuf_held == 0);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_dbuf_held = 1;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs VFS_HOLD(zfsvfs->z_vfs);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_exit(&zp->z_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vn_exists(ZTOV(zp));
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs}
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs/*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Create a new DMU object to hold a zfs znode.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs *
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * IN: dzp - parent directory for new znode
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * vap - file attributes for new znode
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * tx - dmu transaction id for zap operations
d978af5cd9e717f0cb0742b6e940c8fdd836504djacobs * cr - credentials of caller
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * flag - flags:
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * IS_ROOT_NODE - new object will be root
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * IS_XATTR - new object is an attribute
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * IS_REPLAY - intent log replay
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs *
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * OUT: oid - ID of created object
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs *
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsvoid
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobszfs_mknode(znode_t *dzp, vattr_t *vap, uint64_t *oid, dmu_tx_t *tx, cred_t *cr,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs uint_t flag, znode_t **zpp, int bonuslen)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dmu_buf_t *dbp;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs znode_phys_t *pzp;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs znode_t *zp;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs timestruc_t now;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs uint64_t gen;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs int err;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zfsvfs->z_assign >= TXG_INITIAL) { /* ZIL replay */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs *oid = vap->va_nodeid;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs flag |= IS_REPLAY;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs now = vap->va_ctime; /* see zfs_replay_create() */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs gen = vap->va_nblocks; /* ditto */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs } else {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs *oid = 0;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs gethrestime(&now);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs gen = dmu_tx_get_txg(tx);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * Create a new DMU object.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * There's currently no mechanism for pre-reading the blocks that will
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * be to needed allocate a new object, so we accept the small chance
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * that there will be an i/o error and we will fail one of the
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * assertions below.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (vap->va_type == VDIR) {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (flag & IS_REPLAY) {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs err = zap_create_claim(zfsvfs->z_os, *oid,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs DMU_OT_DIRECTORY_CONTENTS,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ASSERT3U(err, ==, 0);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs } else {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs *oid = zap_create(zfsvfs->z_os,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs DMU_OT_DIRECTORY_CONTENTS,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs } else {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (flag & IS_REPLAY) {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs err = dmu_object_claim(zfsvfs->z_os, *oid,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs DMU_OT_PLAIN_FILE_CONTENTS, 0,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ASSERT3U(err, ==, 0);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs } else {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs *oid = dmu_object_alloc(zfsvfs->z_os,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs DMU_OT_PLAIN_FILE_CONTENTS, 0,
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, *oid, NULL, &dbp));
355b4669e025ff377602b6fc7caaf30dbc218371jacobs dmu_buf_will_dirty(dbp, tx);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs /*
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * Initialize the znode physical data to zero.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ASSERT(dbp->db_size >= sizeof (znode_phys_t));
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs bzero(dbp->db_data, dbp->db_size);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs pzp = dbp->db_data;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * If this is the root, fix up the half-initialized parent pointer
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * to reference the just-allocated physical data area.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs */
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (flag & IS_ROOT_NODE) {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs dzp->z_phys = pzp;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dzp->z_id = *oid;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * If parent is an xattr, so am I.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (dzp->z_phys->zp_flags & ZFS_XATTR)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs flag |= IS_XATTR;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (vap->va_type == VBLK || vap->va_type == VCHR) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs pzp->zp_rdev = zfs_expldev(vap->va_rdev);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (vap->va_type == VDIR) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs pzp->zp_size = 2; /* contents ("." and "..") */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
d978af5cd9e717f0cb0742b6e940c8fdd836504djacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs pzp->zp_parent = dzp->z_id;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (flag & IS_XATTR)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs pzp->zp_flags |= ZFS_XATTR;
d978af5cd9e717f0cb0742b6e940c8fdd836504djacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs pzp->zp_gen = gen;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (vap->va_mask & AT_ATIME) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs } else {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_TIME_ENCODE(&now, pzp->zp_atime);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (vap->va_mask & AT_MTIME) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs } else {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp = zfs_znode_alloc(zfsvfs, dbp, *oid, 0);
d978af5cd9e717f0cb0742b6e940c8fdd836504djacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfs_perm_init(zp, dzp, flag, vap, tx, cr);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zpp) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs kmutex_t *hash_mtx = ZFS_OBJ_MUTEX(zp);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_enter(hash_mtx);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs zfs_znode_dmu_init(zp);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs mutex_exit(hash_mtx);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs *zpp = zp;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs } else {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ZTOV(zp)->v_count = 0;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs dmu_buf_rele(dbp, NULL);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs zfs_znode_free(zp);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs}
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobsint
355b4669e025ff377602b6fc7caaf30dbc218371jacobszfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
355b4669e025ff377602b6fc7caaf30dbc218371jacobs{
355b4669e025ff377602b6fc7caaf30dbc218371jacobs dmu_object_info_t doi;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs dmu_buf_t *db;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs znode_t *zp;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs int err;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs *zpp = NULL;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (err) {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs return (err);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs dmu_object_info_from_db(db, &doi);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (doi.doi_bonus_type != DMU_OT_ZNODE ||
355b4669e025ff377602b6fc7caaf30dbc218371jacobs doi.doi_bonus_size < sizeof (znode_phys_t)) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dmu_buf_rele(db, NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (EINVAL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT(db->db_object == obj_num);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ASSERT(db->db_offset == -1);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ASSERT(db->db_data != NULL);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs zp = dmu_buf_get_user(db);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zp != NULL) {
d978af5cd9e717f0cb0742b6e940c8fdd836504djacobs mutex_enter(&zp->z_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ASSERT3U(zp->z_id, ==, obj_num);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zp->z_reap) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dmu_buf_rele(db, NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_exit(&zp->z_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (ENOENT);
d978af5cd9e717f0cb0742b6e940c8fdd836504djacobs } else if (zp->z_dbuf_held) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dmu_buf_rele(db, NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs } else {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zp->z_dbuf_held = 1;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs VFS_HOLD(zfsvfs->z_vfs);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs VN_HOLD(ZTOV(zp));
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_exit(&zp->z_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs *zpp = zp;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (0);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs }
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs /*
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * Not found create new znode/vnode
355b4669e025ff377602b6fc7caaf30dbc218371jacobs */
355b4669e025ff377602b6fc7caaf30dbc218371jacobs zp = zfs_znode_alloc(zfsvfs, db, obj_num, doi.doi_data_block_size);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ASSERT3U(zp->z_id, ==, obj_num);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs zfs_znode_dmu_init(zp);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs *zpp = zp;
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs return (0);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs}
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsvoid
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobszfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
355b4669e025ff377602b6fc7caaf30dbc218371jacobs zfsvfs_t *zfsvfs = zp->z_zfsvfs;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs int error;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ZFS_OBJ_HOLD_ENTER(zfsvfs, zp->z_id);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (zp->z_phys->zp_acl.z_acl_extern_obj) {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs error = dmu_object_free(zfsvfs->z_os,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs zp->z_phys->zp_acl.z_acl_extern_obj, tx);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ASSERT3U(error, ==, 0);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs error = dmu_object_free(zfsvfs->z_os, zp->z_id, tx);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ASSERT3U(error, ==, 0);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs zp->z_dbuf_held = 0;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs dmu_buf_rele(zp->z_dbuf, NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs}
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobsvoid
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobszfs_zinactive(znode_t *zp)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs{
355b4669e025ff377602b6fc7caaf30dbc218371jacobs vnode_t *vp = ZTOV(zp);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs zfsvfs_t *zfsvfs = zp->z_zfsvfs;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs uint64_t z_id = zp->z_id;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ASSERT(zp->z_dbuf_held && zp->z_phys);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs /*
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * Don't allow a zfs_zget() while were trying to release this znode
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_enter(&zp->z_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_enter(&vp->v_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs vp->v_count--;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (vp->v_count > 0 || vn_has_cached_data(vp)) {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs /*
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * If the hold count is greater than zero, somebody has
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * obtained a new reference on this znode while we were
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * processing it here, so we are done. If we still have
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * mapped pages then we are also done, since we don't
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs * want to inactivate the znode until the pages get pushed.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs *
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * XXX - if vn_has_cached_data(vp) is true, but count == 0,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * this seems like it would leave the znode hanging with
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * no chance to go inactive...
355b4669e025ff377602b6fc7caaf30dbc218371jacobs */
355b4669e025ff377602b6fc7caaf30dbc218371jacobs mutex_exit(&vp->v_lock);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs mutex_exit(&zp->z_lock);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs return;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs mutex_exit(&vp->v_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /*
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * If this was the last reference to a file with no links,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * remove the file from the file system.
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zp->z_reap) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs mutex_exit(&zp->z_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs /* XATTR files are not put on the delete queue */
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs if (zp->z_phys->zp_flags & ZFS_XATTR) {
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs zfs_rmnode(zp);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs } else {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs mutex_enter(&zfsvfs->z_delete_head.z_mutex);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs list_insert_tail(&zfsvfs->z_delete_head.z_znodes, zp);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs zfsvfs->z_delete_head.z_znode_count++;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs cv_broadcast(&zfsvfs->z_delete_head.z_cv);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs mutex_exit(&zfsvfs->z_delete_head.z_mutex);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs VFS_RELE(zfsvfs->z_vfs);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs return;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ASSERT(zp->z_phys);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ASSERT(zp->z_dbuf_held);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs zp->z_dbuf_held = 0;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs mutex_exit(&zp->z_lock);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs dmu_buf_rele(zp->z_dbuf, NULL);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs VFS_RELE(zfsvfs->z_vfs);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs}
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobsvoid
355b4669e025ff377602b6fc7caaf30dbc218371jacobszfs_znode_free(znode_t *zp)
355b4669e025ff377602b6fc7caaf30dbc218371jacobs{
zfsvfs_t *zfsvfs = zp->z_zfsvfs;
mutex_enter(&zfsvfs->z_znodes_lock);
list_remove(&zfsvfs->z_all_znodes, zp);
mutex_exit(&zfsvfs->z_znodes_lock);
kmem_cache_free(znode_cache, zp);
}
void
zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
{
timestruc_t now;
ASSERT(MUTEX_HELD(&zp->z_lock));
gethrestime(&now);
if (tx) {
dmu_buf_will_dirty(zp->z_dbuf, tx);
zp->z_atime_dirty = 0;
zp->z_seq++;
} else {
zp->z_atime_dirty = 1;
}
if (flag & AT_ATIME)
ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
if (flag & AT_MTIME)
ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
if (flag & AT_CTIME)
ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
}
/*
* Update the requested znode timestamps with the current time.
* If we are in a transaction, then go ahead and mark the znode
* dirty in the transaction so the timestamps will go to disk.
* Otherwise, we will get pushed next time the znode is updated
* in a transaction, or when this znode eventually goes inactive.
*
* Why is this OK?
* 1 - Only the ACCESS time is ever updated outside of a transaction.
* 2 - Multiple consecutive updates will be collapsed into a single
* znode update by the transaction grouping semantics of the DMU.
*/
void
zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
{
mutex_enter(&zp->z_lock);
zfs_time_stamper_locked(zp, flag, tx);
mutex_exit(&zp->z_lock);
}
/*
* Grow the block size for a file.
*
* IN: zp - znode of file to free data in.
* size - requested block size
* tx - open transaction.
*
* NOTE: this function assumes that the znode is write locked.
*/
void
zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
{
int error;
u_longlong_t dummy;
if (size <= zp->z_blksz)
return;
/*
* If the file size is already greater than the current blocksize,
* we will not grow. If there is more than one block in a file,
* the blocksize cannot change.
*/
if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
return;
error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
size, 0, tx);
if (error == ENOTSUP)
return;
ASSERT3U(error, ==, 0);
/* What blocksize did we actually get? */
dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
}
/*
* This is a dummy interface used when pvn_vplist_dirty() should *not*
* be calling back into the fs for a putpage(). E.g.: when truncating
* a file, the pages being "thrown away* don't need to be written out.
*/
/* ARGSUSED */
static int
zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
int flags, cred_t *cr)
{
ASSERT(0);
return (0);
}
/*
* Free space in a file.
*
* IN: zp - znode of file to free data in.
* off - start of section to free.
* len - length of section to free (0 => to EOF).
* flag - current file open mode flags.
*
* RETURN: 0 if success
* error code if failure
*/
int
zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
{
vnode_t *vp = ZTOV(zp);
dmu_tx_t *tx;
zfsvfs_t *zfsvfs = zp->z_zfsvfs;
zilog_t *zilog = zfsvfs->z_log;
rl_t *rl;
uint64_t end = off + len;
uint64_t size, new_blksz;
int error;
if (ZTOV(zp)->v_type == VFIFO)
return (0);
/*
* If we will change zp_size then lock the whole file,
* otherwise just lock the range being freed.
*/
if (len == 0 || off + len > zp->z_phys->zp_size) {
rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
} else {
rl = zfs_range_lock(zp, off, len, RL_WRITER);
/* recheck, in case zp_size changed */
if (off + len > zp->z_phys->zp_size) {
/* lost race: file size changed, lock whole file */
zfs_range_unlock(rl);
rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
}
}
/*
* Nothing to do if file already at desired length.
*/
size = zp->z_phys->zp_size;
if (len == 0 && size == off) {
zfs_range_unlock(rl);
return (0);
}
/*
* Check for any locks in the region to be freed.
*/
if (MANDLOCK(vp, (mode_t)zp->z_phys->zp_mode)) {
uint64_t start = off;
uint64_t extent = len;
if (off > size) {
start = size;
extent += off - size;
} else if (len == 0) {
extent = size - off;
}
if (error = chklock(vp, FWRITE, start, extent, flag, NULL)) {
zfs_range_unlock(rl);
return (error);
}
}
tx = dmu_tx_create(zfsvfs->z_os);
dmu_tx_hold_bonus(tx, zp->z_id);
new_blksz = 0;
if (end > size &&
(!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
/*
* We are growing the file past the current block size.
*/
if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
ASSERT(!ISP2(zp->z_blksz));
new_blksz = MIN(end, SPA_MAXBLOCKSIZE);
} else {
new_blksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
}
dmu_tx_hold_write(tx, zp->z_id, 0, MIN(end, new_blksz));
} else if (off < size) {
/*
* If len == 0, we are truncating the file.
*/
dmu_tx_hold_free(tx, zp->z_id, off, len ? len : DMU_OBJECT_END);
}
error = dmu_tx_assign(tx, zfsvfs->z_assign);
if (error) {
if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
dmu_tx_wait(tx);
dmu_tx_abort(tx);
zfs_range_unlock(rl);
return (error);
}
if (new_blksz)
zfs_grow_blocksize(zp, new_blksz, tx);
if (end > size || len == 0)
zp->z_phys->zp_size = end;
if (off < size) {
objset_t *os = zfsvfs->z_os;
uint64_t rlen = len;
if (len == 0)
rlen = -1;
else if (end > size)
rlen = size - off;
VERIFY(0 == dmu_free_range(os, zp->z_id, off, rlen, tx));
}
if (log) {
zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
}
zfs_range_unlock(rl);
dmu_tx_commit(tx);
/*
* Clear any mapped pages in the truncated region. This has to
* happen outside of the transaction to avoid the possibility of
* a deadlock with someone trying to push a page that we are
* about to invalidate.
*/
rw_enter(&zp->z_map_lock, RW_WRITER);
if (off < size && vn_has_cached_data(vp)) {
page_t *pp;
uint64_t start = off & PAGEMASK;
int poff = off & PAGEOFFSET;
if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
/*
* We need to zero a partial page.
*/
pagezero(pp, poff, PAGESIZE - poff);
start += PAGESIZE;
page_unlock(pp);
}
error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
B_INVAL | B_TRUNC, NULL);
ASSERT(error == 0);
}
rw_exit(&zp->z_map_lock);
return (0);
}
void
zfs_create_fs(objset_t *os, cred_t *cr, dmu_tx_t *tx)
{
zfsvfs_t zfsvfs;
uint64_t moid, doid, roid = 0;
uint64_t version = ZPL_VERSION;
int error;
znode_t *rootzp = NULL;
vnode_t *vp;
vattr_t vattr;
/*
* First attempt to create master node.
*/
/*
* In an empty objset, there are no blocks to read and thus
* there can be no i/o errors (which we assert below).
*/
moid = MASTER_NODE_OBJ;
error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
DMU_OT_NONE, 0, tx);
ASSERT(error == 0);
/*
* Set starting attributes.
*/
error = zap_update(os, moid, ZPL_VERSION_OBJ, 8, 1, &version, tx);
ASSERT(error == 0);
/*
* Create a delete queue.
*/
doid = zap_create(os, DMU_OT_DELETE_QUEUE, DMU_OT_NONE, 0, tx);
error = zap_add(os, moid, ZFS_DELETE_QUEUE, 8, 1, &doid, tx);
ASSERT(error == 0);
/*
* Create root znode. Create minimal znode/vnode/zfsvfs
* to allow zfs_mknode to work.
*/
vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
vattr.va_type = VDIR;
vattr.va_mode = S_IFDIR|0755;
vattr.va_uid = 0;
vattr.va_gid = 3;
rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
rootzp->z_zfsvfs = &zfsvfs;
rootzp->z_reap = 0;
rootzp->z_atime_dirty = 0;
rootzp->z_dbuf_held = 0;
vp = ZTOV(rootzp);
vn_reinit(vp);
vp->v_type = VDIR;
bzero(&zfsvfs, sizeof (zfsvfs_t));
zfsvfs.z_os = os;
zfsvfs.z_assign = TXG_NOWAIT;
zfsvfs.z_parent = &zfsvfs;
mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
offsetof(znode_t, z_link_node));
zfs_mknode(rootzp, &vattr, &roid, tx, cr, IS_ROOT_NODE, NULL, 0);
ASSERT3U(rootzp->z_id, ==, roid);
error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &roid, tx);
ASSERT(error == 0);
ZTOV(rootzp)->v_count = 0;
kmem_cache_free(znode_cache, rootzp);
}