zfs_fuid.c revision 3b12c289fe048d2e1698e22811cbfd6d3b3ed96e
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include <sys/zfs_context.h>
2N/A#include <sys/sunddi.h>
2N/A#include <sys/dmu.h>
2N/A#include <sys/avl.h>
2N/A#include <sys/zap.h>
2N/A#include <sys/refcount.h>
2N/A#include <sys/nvpair.h>
2N/A#ifdef _KERNEL
2N/A#include <sys/kidmap.h>
2N/A#include <sys/sid.h>
2N/A#include <sys/zfs_vfsops.h>
2N/A#include <sys/zfs_znode.h>
2N/A#endif
2N/A#include <sys/zfs_fuid.h>
2N/A
2N/A/*
2N/A * FUID Domain table(s).
2N/A *
2N/A * The FUID table is stored as a packed nvlist of an array
2N/A * of nvlists which contain an index, domain string and offset
2N/A *
2N/A * During file system initialization the nvlist(s) are read and
2N/A * two AVL trees are created. One tree is keyed by the index number
2N/A * and the other by the domain string. Nodes are never removed from
2N/A * trees, but new entries may be added. If a new entry is added then
2N/A * the zfsvfs->z_fuid_dirty flag is set to true and the caller will then
2N/A * be responsible for calling zfs_fuid_sync() to sync the changes to disk.
2N/A *
2N/A */
2N/A
2N/A#define FUID_IDX "fuid_idx"
2N/A#define FUID_DOMAIN "fuid_domain"
2N/A#define FUID_OFFSET "fuid_offset"
2N/A#define FUID_NVP_ARRAY "fuid_nvlist"
2N/A
2N/Atypedef struct fuid_domain {
2N/A avl_node_t f_domnode;
2N/A avl_node_t f_idxnode;
2N/A ksiddomain_t *f_ksid;
2N/A uint64_t f_idx;
2N/A} fuid_domain_t;
2N/A
2N/Astatic char *nulldomain = "";
2N/A
2N/A/*
2N/A * Compare two indexes.
2N/A */
2N/Astatic int
2N/Aidx_compare(const void *arg1, const void *arg2)
2N/A{
2N/A const fuid_domain_t *node1 = arg1;
2N/A const fuid_domain_t *node2 = arg2;
2N/A
2N/A if (node1->f_idx < node2->f_idx)
2N/A return (-1);
2N/A else if (node1->f_idx > node2->f_idx)
2N/A return (1);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Compare two domain strings.
2N/A */
2N/Astatic int
2N/Adomain_compare(const void *arg1, const void *arg2)
2N/A{
2N/A const fuid_domain_t *node1 = arg1;
2N/A const fuid_domain_t *node2 = arg2;
2N/A int val;
2N/A
2N/A val = strcmp(node1->f_ksid->kd_name, node2->f_ksid->kd_name);
2N/A if (val == 0)
2N/A return (0);
2N/A return (val > 0 ? 1 : -1);
2N/A}
2N/A
2N/Avoid
2N/Azfs_fuid_avl_tree_create(avl_tree_t *idx_tree, avl_tree_t *domain_tree)
2N/A{
2N/A avl_create(idx_tree, idx_compare,
2N/A sizeof (fuid_domain_t), offsetof(fuid_domain_t, f_idxnode));
2N/A avl_create(domain_tree, domain_compare,
2N/A sizeof (fuid_domain_t), offsetof(fuid_domain_t, f_domnode));
2N/A}
2N/A
2N/A/*
2N/A * load initial fuid domain and idx trees. This function is used by
2N/A * both the kernel and zdb.
2N/A */
2N/Auint64_t
2N/Azfs_fuid_table_load(objset_t *os, uint64_t fuid_obj, avl_tree_t *idx_tree,
2N/A avl_tree_t *domain_tree)
2N/A{
2N/A dmu_buf_t *db;
2N/A uint64_t fuid_size;
2N/A
2N/A ASSERT(fuid_obj != 0);
2N/A VERIFY(0 == dmu_bonus_hold(os, fuid_obj,
2N/A FTAG, &db));
2N/A fuid_size = *(uint64_t *)db->db_data;
2N/A dmu_buf_rele(db, FTAG);
2N/A
2N/A if (fuid_size) {
2N/A nvlist_t **fuidnvp;
2N/A nvlist_t *nvp = NULL;
2N/A uint_t count;
2N/A char *packed;
2N/A int i;
2N/A
2N/A packed = kmem_alloc(fuid_size, KM_SLEEP);
2N/A VERIFY(dmu_read(os, fuid_obj, 0,
2N/A fuid_size, packed, DMU_READ_PREFETCH) == 0);
2N/A VERIFY(nvlist_unpack(packed, fuid_size,
2N/A &nvp, 0) == 0);
2N/A VERIFY(nvlist_lookup_nvlist_array(nvp, FUID_NVP_ARRAY,
2N/A &fuidnvp, &count) == 0);
2N/A
2N/A for (i = 0; i != count; i++) {
2N/A fuid_domain_t *domnode;
2N/A char *domain;
2N/A uint64_t idx;
2N/A
2N/A VERIFY(nvlist_lookup_string(fuidnvp[i], FUID_DOMAIN,
2N/A &domain) == 0);
2N/A VERIFY(nvlist_lookup_uint64(fuidnvp[i], FUID_IDX,
2N/A &idx) == 0);
2N/A
2N/A domnode = kmem_alloc(sizeof (fuid_domain_t), KM_SLEEP);
2N/A
2N/A domnode->f_idx = idx;
2N/A domnode->f_ksid = ksid_lookupdomain(domain);
2N/A avl_add(idx_tree, domnode);
2N/A avl_add(domain_tree, domnode);
2N/A }
2N/A nvlist_free(nvp);
2N/A kmem_free(packed, fuid_size);
2N/A }
2N/A return (fuid_size);
2N/A}
2N/A
2N/Avoid
2N/Azfs_fuid_table_destroy(avl_tree_t *idx_tree, avl_tree_t *domain_tree)
2N/A{
2N/A fuid_domain_t *domnode;
2N/A void *cookie;
2N/A
2N/A cookie = NULL;
2N/A while (domnode = avl_destroy_nodes(domain_tree, &cookie))
2N/A ksiddomain_rele(domnode->f_ksid);
2N/A
2N/A avl_destroy(domain_tree);
2N/A cookie = NULL;
2N/A while (domnode = avl_destroy_nodes(idx_tree, &cookie))
2N/A kmem_free(domnode, sizeof (fuid_domain_t));
2N/A avl_destroy(idx_tree);
2N/A}
2N/A
2N/Achar *
2N/Azfs_fuid_idx_domain(avl_tree_t *idx_tree, uint32_t idx)
2N/A{
2N/A fuid_domain_t searchnode, *findnode;
2N/A avl_index_t loc;
2N/A
2N/A searchnode.f_idx = idx;
2N/A
2N/A findnode = avl_find(idx_tree, &searchnode, &loc);
2N/A
2N/A return (findnode ? findnode->f_ksid->kd_name : nulldomain);
2N/A}
2N/A
2N/A#ifdef _KERNEL
2N/A/*
2N/A * Load the fuid table(s) into memory.
2N/A */
2N/Astatic void
2N/Azfs_fuid_init(zfsvfs_t *zfsvfs)
2N/A{
2N/A rw_enter(&zfsvfs->z_fuid_lock, RW_WRITER);
2N/A
2N/A if (zfsvfs->z_fuid_loaded) {
2N/A rw_exit(&zfsvfs->z_fuid_lock);
2N/A return;
2N/A }
2N/A
2N/A zfs_fuid_avl_tree_create(&zfsvfs->z_fuid_idx, &zfsvfs->z_fuid_domain);
2N/A
2N/A (void) zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
2N/A ZFS_FUID_TABLES, 8, 1, &zfsvfs->z_fuid_obj);
2N/A if (zfsvfs->z_fuid_obj != 0) {
2N/A zfsvfs->z_fuid_size = zfs_fuid_table_load(zfsvfs->z_os,
2N/A zfsvfs->z_fuid_obj, &zfsvfs->z_fuid_idx,
2N/A &zfsvfs->z_fuid_domain);
2N/A }
2N/A
2N/A zfsvfs->z_fuid_loaded = B_TRUE;
2N/A rw_exit(&zfsvfs->z_fuid_lock);
2N/A}
2N/A
2N/A/*
2N/A * sync out AVL trees to persistent storage.
2N/A */
2N/Avoid
2N/Azfs_fuid_sync(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
2N/A{
2N/A nvlist_t *nvp;
2N/A nvlist_t **fuids;
2N/A size_t nvsize = 0;
2N/A char *packed;
2N/A dmu_buf_t *db;
2N/A fuid_domain_t *domnode;
2N/A int numnodes;
2N/A int i;
2N/A
2N/A if (!zfsvfs->z_fuid_dirty) {
2N/A return;
2N/A }
2N/A
2N/A rw_enter(&zfsvfs->z_fuid_lock, RW_WRITER);
2N/A
2N/A /*
2N/A * First see if table needs to be created?
2N/A */
2N/A if (zfsvfs->z_fuid_obj == 0) {
2N/A zfsvfs->z_fuid_obj = dmu_object_alloc(zfsvfs->z_os,
2N/A DMU_OT_FUID, 1 << 14, DMU_OT_FUID_SIZE,
2N/A sizeof (uint64_t), tx);
2N/A VERIFY(zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
2N/A ZFS_FUID_TABLES, sizeof (uint64_t), 1,
2N/A &zfsvfs->z_fuid_obj, tx) == 0);
2N/A }
2N/A
2N/A VERIFY(nvlist_alloc(&nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2N/A
2N/A numnodes = avl_numnodes(&zfsvfs->z_fuid_idx);
2N/A fuids = kmem_alloc(numnodes * sizeof (void *), KM_SLEEP);
2N/A for (i = 0, domnode = avl_first(&zfsvfs->z_fuid_domain); domnode; i++,
2N/A domnode = AVL_NEXT(&zfsvfs->z_fuid_domain, domnode)) {
2N/A VERIFY(nvlist_alloc(&fuids[i], NV_UNIQUE_NAME, KM_SLEEP) == 0);
2N/A VERIFY(nvlist_add_uint64(fuids[i], FUID_IDX,
2N/A domnode->f_idx) == 0);
2N/A VERIFY(nvlist_add_uint64(fuids[i], FUID_OFFSET, 0) == 0);
2N/A VERIFY(nvlist_add_string(fuids[i], FUID_DOMAIN,
2N/A domnode->f_ksid->kd_name) == 0);
2N/A }
2N/A VERIFY(nvlist_add_nvlist_array(nvp, FUID_NVP_ARRAY,
2N/A fuids, numnodes) == 0);
2N/A for (i = 0; i != numnodes; i++)
2N/A nvlist_free(fuids[i]);
2N/A kmem_free(fuids, numnodes * sizeof (void *));
2N/A VERIFY(nvlist_size(nvp, &nvsize, NV_ENCODE_XDR) == 0);
2N/A packed = kmem_alloc(nvsize, KM_SLEEP);
2N/A VERIFY(nvlist_pack(nvp, &packed, &nvsize,
2N/A NV_ENCODE_XDR, KM_SLEEP) == 0);
2N/A nvlist_free(nvp);
2N/A zfsvfs->z_fuid_size = nvsize;
2N/A dmu_write(zfsvfs->z_os, zfsvfs->z_fuid_obj, 0,
2N/A zfsvfs->z_fuid_size, packed, tx);
2N/A kmem_free(packed, zfsvfs->z_fuid_size);
2N/A VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, zfsvfs->z_fuid_obj,
2N/A FTAG, &db));
2N/A dmu_buf_will_dirty(db, tx);
2N/A *(uint64_t *)db->db_data = zfsvfs->z_fuid_size;
2N/A dmu_buf_rele(db, FTAG);
2N/A
2N/A zfsvfs->z_fuid_dirty = B_FALSE;
2N/A rw_exit(&zfsvfs->z_fuid_lock);
2N/A}
2N/A
2N/A/*
2N/A * Query domain table for a given domain.
2N/A *
2N/A * If domain isn't found and addok is set, it is added to AVL trees and
2N/A * the zfsvfs->z_fuid_dirty flag will be set to TRUE. It will then be
2N/A * necessary for the caller or another thread to detect the dirty table
2N/A * and sync out the changes.
2N/A */
2N/Aint
2N/Azfs_fuid_find_by_domain(zfsvfs_t *zfsvfs, const char *domain,
2N/A char **retdomain, boolean_t addok)
2N/A{
2N/A fuid_domain_t searchnode, *findnode;
2N/A avl_index_t loc;
2N/A krw_t rw = RW_READER;
2N/A
2N/A /*
2N/A * If the dummy "nobody" domain then return an index of 0
2N/A * to cause the created FUID to be a standard POSIX id
2N/A * for the user nobody.
2N/A */
2N/A if (domain[0] == '\0') {
2N/A if (retdomain)
2N/A *retdomain = nulldomain;
2N/A return (0);
2N/A }
2N/A
2N/A searchnode.f_ksid = ksid_lookupdomain(domain);
2N/A if (retdomain)
2N/A *retdomain = searchnode.f_ksid->kd_name;
2N/A if (!zfsvfs->z_fuid_loaded)
2N/A zfs_fuid_init(zfsvfs);
2N/A
2N/Aretry:
2N/A rw_enter(&zfsvfs->z_fuid_lock, rw);
2N/A findnode = avl_find(&zfsvfs->z_fuid_domain, &searchnode, &loc);
2N/A
2N/A if (findnode) {
2N/A rw_exit(&zfsvfs->z_fuid_lock);
2N/A ksiddomain_rele(searchnode.f_ksid);
2N/A return (findnode->f_idx);
2N/A } else if (addok) {
2N/A fuid_domain_t *domnode;
2N/A uint64_t retidx;
2N/A
2N/A if (rw == RW_READER && !rw_tryupgrade(&zfsvfs->z_fuid_lock)) {
2N/A rw_exit(&zfsvfs->z_fuid_lock);
2N/A rw = RW_WRITER;
2N/A goto retry;
2N/A }
2N/A
2N/A domnode = kmem_alloc(sizeof (fuid_domain_t), KM_SLEEP);
2N/A domnode->f_ksid = searchnode.f_ksid;
2N/A
2N/A retidx = domnode->f_idx = avl_numnodes(&zfsvfs->z_fuid_idx) + 1;
2N/A
2N/A avl_add(&zfsvfs->z_fuid_domain, domnode);
2N/A avl_add(&zfsvfs->z_fuid_idx, domnode);
2N/A zfsvfs->z_fuid_dirty = B_TRUE;
2N/A rw_exit(&zfsvfs->z_fuid_lock);
2N/A return (retidx);
2N/A } else {
2N/A rw_exit(&zfsvfs->z_fuid_lock);
2N/A return (-1);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Query domain table by index, returning domain string
2N/A *
2N/A * Returns a pointer from an avl node of the domain string.
2N/A *
2N/A */
2N/Aconst char *
2N/Azfs_fuid_find_by_idx(zfsvfs_t *zfsvfs, uint32_t idx)
2N/A{
2N/A char *domain;
2N/A
2N/A if (idx == 0 || !zfsvfs->z_use_fuids)
2N/A return (NULL);
2N/A
2N/A if (!zfsvfs->z_fuid_loaded)
2N/A zfs_fuid_init(zfsvfs);
2N/A
2N/A rw_enter(&zfsvfs->z_fuid_lock, RW_READER);
2N/A
2N/A if (zfsvfs->z_fuid_obj)
2N/A domain = zfs_fuid_idx_domain(&zfsvfs->z_fuid_idx, idx);
2N/A else
2N/A domain = nulldomain;
2N/A rw_exit(&zfsvfs->z_fuid_lock);
2N/A
2N/A ASSERT(domain);
2N/A return (domain);
2N/A}
2N/A
2N/Avoid
2N/Azfs_fuid_map_ids(znode_t *zp, cred_t *cr, uid_t *uidp, uid_t *gidp)
2N/A{
2N/A *uidp = zfs_fuid_map_id(zp->z_zfsvfs, zp->z_phys->zp_uid,
2N/A cr, ZFS_OWNER);
2N/A *gidp = zfs_fuid_map_id(zp->z_zfsvfs, zp->z_phys->zp_gid,
2N/A cr, ZFS_GROUP);
2N/A}
2N/A
2N/Auid_t
2N/Azfs_fuid_map_id(zfsvfs_t *zfsvfs, uint64_t fuid,
2N/A cred_t *cr, zfs_fuid_type_t type)
2N/A{
2N/A uint32_t index = FUID_INDEX(fuid);
2N/A const char *domain;
2N/A uid_t id;
2N/A
2N/A if (index == 0)
2N/A return (fuid);
2N/A
2N/A domain = zfs_fuid_find_by_idx(zfsvfs, index);
2N/A ASSERT(domain != NULL);
2N/A
2N/A if (type == ZFS_OWNER || type == ZFS_ACE_USER) {
2N/A (void) kidmap_getuidbysid(crgetzone(cr), domain,
2N/A FUID_RID(fuid), &id);
2N/A } else {
2N/A (void) kidmap_getgidbysid(crgetzone(cr), domain,
2N/A FUID_RID(fuid), &id);
2N/A }
2N/A return (id);
2N/A}
2N/A
2N/A/*
2N/A * Add a FUID node to the list of fuid's being created for this
2N/A * ACL
2N/A *
2N/A * If ACL has multiple domains, then keep only one copy of each unique
2N/A * domain.
2N/A */
2N/Astatic void
2N/Azfs_fuid_node_add(zfs_fuid_info_t **fuidpp, const char *domain, uint32_t rid,
2N/A uint64_t idx, uint64_t id, zfs_fuid_type_t type)
2N/A{
2N/A zfs_fuid_t *fuid;
2N/A zfs_fuid_domain_t *fuid_domain;
2N/A zfs_fuid_info_t *fuidp;
2N/A uint64_t fuididx;
2N/A boolean_t found = B_FALSE;
2N/A
2N/A if (*fuidpp == NULL)
2N/A *fuidpp = zfs_fuid_info_alloc();
2N/A
2N/A fuidp = *fuidpp;
2N/A /*
2N/A * First find fuid domain index in linked list
2N/A *
2N/A * If one isn't found then create an entry.
2N/A */
2N/A
2N/A for (fuididx = 1, fuid_domain = list_head(&fuidp->z_domains);
2N/A fuid_domain; fuid_domain = list_next(&fuidp->z_domains,
2N/A fuid_domain), fuididx++) {
2N/A if (idx == fuid_domain->z_domidx) {
2N/A found = B_TRUE;
2N/A break;
2N/A }
2N/A }
2N/A
2N/A if (!found) {
2N/A fuid_domain = kmem_alloc(sizeof (zfs_fuid_domain_t), KM_SLEEP);
2N/A fuid_domain->z_domain = domain;
2N/A fuid_domain->z_domidx = idx;
2N/A list_insert_tail(&fuidp->z_domains, fuid_domain);
2N/A fuidp->z_domain_str_sz += strlen(domain) + 1;
2N/A fuidp->z_domain_cnt++;
2N/A }
2N/A
2N/A if (type == ZFS_ACE_USER || type == ZFS_ACE_GROUP) {
2N/A
2N/A /*
2N/A * Now allocate fuid entry and add it on the end of the list
2N/A */
2N/A
2N/A fuid = kmem_alloc(sizeof (zfs_fuid_t), KM_SLEEP);
2N/A fuid->z_id = id;
2N/A fuid->z_domidx = idx;
2N/A fuid->z_logfuid = FUID_ENCODE(fuididx, rid);
2N/A
2N/A list_insert_tail(&fuidp->z_fuids, fuid);
2N/A fuidp->z_fuid_cnt++;
2N/A } else {
2N/A if (type == ZFS_OWNER)
2N/A fuidp->z_fuid_owner = FUID_ENCODE(fuididx, rid);
2N/A else
2N/A fuidp->z_fuid_group = FUID_ENCODE(fuididx, rid);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Create a file system FUID, based on information in the users cred
2N/A */
2N/Auint64_t
2N/Azfs_fuid_create_cred(zfsvfs_t *zfsvfs, zfs_fuid_type_t type,
2N/A cred_t *cr, zfs_fuid_info_t **fuidp)
2N/A{
2N/A uint64_t idx;
2N/A ksid_t *ksid;
2N/A uint32_t rid;
2N/A char *kdomain;
2N/A const char *domain;
2N/A uid_t id;
2N/A
2N/A VERIFY(type == ZFS_OWNER || type == ZFS_GROUP);
2N/A
2N/A ksid = crgetsid(cr, (type == ZFS_OWNER) ? KSID_OWNER : KSID_GROUP);
2N/A if (ksid) {
2N/A id = ksid_getid(ksid);
2N/A } else {
2N/A if (type == ZFS_OWNER)
2N/A id = crgetuid(cr);
2N/A else
2N/A id = crgetgid(cr);
2N/A }
2N/A
2N/A if (!zfsvfs->z_use_fuids || (!IS_EPHEMERAL(id)))
2N/A return ((uint64_t)id);
2N/A
2N/A rid = ksid_getrid(ksid);
2N/A domain = ksid_getdomain(ksid);
2N/A
2N/A idx = zfs_fuid_find_by_domain(zfsvfs, domain, &kdomain, B_TRUE);
2N/A
2N/A zfs_fuid_node_add(fuidp, kdomain, rid, idx, id, type);
2N/A
2N/A return (FUID_ENCODE(idx, rid));
2N/A}
2N/A
2N/A/*
2N/A * Create a file system FUID for an ACL ace
2N/A * or a chown/chgrp of the file.
2N/A * This is similar to zfs_fuid_create_cred, except that
2N/A * we can't find the domain + rid information in the
2N/A * cred. Instead we have to query Winchester for the
2N/A * domain and rid.
2N/A *
2N/A * During replay operations the domain+rid information is
2N/A * found in the zfs_fuid_info_t that the replay code has
2N/A * attached to the zfsvfs of the file system.
2N/A */
2N/Auint64_t
2N/Azfs_fuid_create(zfsvfs_t *zfsvfs, uint64_t id, cred_t *cr,
2N/A zfs_fuid_type_t type, zfs_fuid_info_t **fuidpp)
2N/A{
2N/A const char *domain;
2N/A char *kdomain;
2N/A uint32_t fuid_idx = FUID_INDEX(id);
2N/A uint32_t rid;
2N/A idmap_stat status;
2N/A uint64_t idx;
2N/A zfs_fuid_t *zfuid = NULL;
2N/A zfs_fuid_info_t *fuidp;
2N/A
2N/A /*
2N/A * If POSIX ID, or entry is already a FUID then
2N/A * just return the id
2N/A *
2N/A * We may also be handed an already FUID'ized id via
2N/A * chmod.
2N/A */
2N/A
2N/A if (!zfsvfs->z_use_fuids || !IS_EPHEMERAL(id) || fuid_idx != 0)
2N/A return (id);
2N/A
2N/A if (zfsvfs->z_replay) {
2N/A fuidp = zfsvfs->z_fuid_replay;
2N/A
2N/A /*
2N/A * If we are passed an ephemeral id, but no
2N/A * fuid_info was logged then return NOBODY.
2N/A * This is most likely a result of idmap service
2N/A * not being available.
2N/A */
2N/A if (fuidp == NULL)
2N/A return (UID_NOBODY);
2N/A
2N/A switch (type) {
2N/A case ZFS_ACE_USER:
2N/A case ZFS_ACE_GROUP:
2N/A zfuid = list_head(&fuidp->z_fuids);
2N/A rid = FUID_RID(zfuid->z_logfuid);
2N/A idx = FUID_INDEX(zfuid->z_logfuid);
2N/A break;
2N/A case ZFS_OWNER:
2N/A rid = FUID_RID(fuidp->z_fuid_owner);
2N/A idx = FUID_INDEX(fuidp->z_fuid_owner);
2N/A break;
2N/A case ZFS_GROUP:
2N/A rid = FUID_RID(fuidp->z_fuid_group);
2N/A idx = FUID_INDEX(fuidp->z_fuid_group);
2N/A break;
2N/A };
2N/A domain = fuidp->z_domain_table[idx -1];
2N/A } else {
2N/A if (type == ZFS_OWNER || type == ZFS_ACE_USER)
2N/A status = kidmap_getsidbyuid(crgetzone(cr), id,
2N/A &domain, &rid);
2N/A else
2N/A status = kidmap_getsidbygid(crgetzone(cr), id,
2N/A &domain, &rid);
2N/A
2N/A if (status != 0) {
2N/A /*
2N/A * When returning nobody we will need to
2N/A * make a dummy fuid table entry for logging
2N/A * purposes.
2N/A */
2N/A rid = UID_NOBODY;
2N/A domain = nulldomain;
2N/A }
2N/A }
2N/A
2N/A idx = zfs_fuid_find_by_domain(zfsvfs, domain, &kdomain, B_TRUE);
2N/A
2N/A if (!zfsvfs->z_replay)
2N/A zfs_fuid_node_add(fuidpp, kdomain,
2N/A rid, idx, id, type);
2N/A else if (zfuid != NULL) {
2N/A list_remove(&fuidp->z_fuids, zfuid);
2N/A kmem_free(zfuid, sizeof (zfs_fuid_t));
2N/A }
2N/A return (FUID_ENCODE(idx, rid));
2N/A}
2N/A
2N/Avoid
2N/Azfs_fuid_destroy(zfsvfs_t *zfsvfs)
2N/A{
2N/A rw_enter(&zfsvfs->z_fuid_lock, RW_WRITER);
2N/A if (!zfsvfs->z_fuid_loaded) {
2N/A rw_exit(&zfsvfs->z_fuid_lock);
2N/A return;
2N/A }
2N/A zfs_fuid_table_destroy(&zfsvfs->z_fuid_idx, &zfsvfs->z_fuid_domain);
2N/A rw_exit(&zfsvfs->z_fuid_lock);
2N/A}
2N/A
2N/A/*
2N/A * Allocate zfs_fuid_info for tracking FUIDs created during
2N/A * zfs_mknode, VOP_SETATTR() or VOP_SETSECATTR()
2N/A */
2N/Azfs_fuid_info_t *
2N/Azfs_fuid_info_alloc(void)
2N/A{
2N/A zfs_fuid_info_t *fuidp;
2N/A
2N/A fuidp = kmem_zalloc(sizeof (zfs_fuid_info_t), KM_SLEEP);
2N/A list_create(&fuidp->z_domains, sizeof (zfs_fuid_domain_t),
2N/A offsetof(zfs_fuid_domain_t, z_next));
2N/A list_create(&fuidp->z_fuids, sizeof (zfs_fuid_t),
2N/A offsetof(zfs_fuid_t, z_next));
2N/A return (fuidp);
2N/A}
2N/A
2N/A/*
2N/A * Release all memory associated with zfs_fuid_info_t
2N/A */
2N/Avoid
2N/Azfs_fuid_info_free(zfs_fuid_info_t *fuidp)
2N/A{
2N/A zfs_fuid_t *zfuid;
2N/A zfs_fuid_domain_t *zdomain;
2N/A
2N/A while ((zfuid = list_head(&fuidp->z_fuids)) != NULL) {
2N/A list_remove(&fuidp->z_fuids, zfuid);
2N/A kmem_free(zfuid, sizeof (zfs_fuid_t));
2N/A }
2N/A
2N/A if (fuidp->z_domain_table != NULL)
2N/A kmem_free(fuidp->z_domain_table,
2N/A (sizeof (char **)) * fuidp->z_domain_cnt);
2N/A
2N/A while ((zdomain = list_head(&fuidp->z_domains)) != NULL) {
2N/A list_remove(&fuidp->z_domains, zdomain);
2N/A kmem_free(zdomain, sizeof (zfs_fuid_domain_t));
2N/A }
2N/A
2N/A kmem_free(fuidp, sizeof (zfs_fuid_info_t));
2N/A}
2N/A
2N/A/*
2N/A * Check to see if id is a groupmember. If cred
2N/A * has ksid info then sidlist is checked first
2N/A * and if still not found then POSIX groups are checked
2N/A *
2N/A * Will use a straight FUID compare when possible.
2N/A */
2N/Aboolean_t
2N/Azfs_groupmember(zfsvfs_t *zfsvfs, uint64_t id, cred_t *cr)
2N/A{
2N/A ksid_t *ksid = crgetsid(cr, KSID_GROUP);
2N/A ksidlist_t *ksidlist = crgetsidlist(cr);
2N/A uid_t gid;
2N/A
2N/A if (ksid && ksidlist) {
2N/A int i;
2N/A ksid_t *ksid_groups;
2N/A uint32_t idx = FUID_INDEX(id);
2N/A uint32_t rid = FUID_RID(id);
2N/A
2N/A ksid_groups = ksidlist->ksl_sids;
2N/A
2N/A for (i = 0; i != ksidlist->ksl_nsid; i++) {
2N/A if (idx == 0) {
2N/A if (id != IDMAP_WK_CREATOR_GROUP_GID &&
2N/A id == ksid_groups[i].ks_id) {
2N/A return (B_TRUE);
2N/A }
2N/A } else {
2N/A const char *domain;
2N/A
2N/A domain = zfs_fuid_find_by_idx(zfsvfs, idx);
2N/A ASSERT(domain != NULL);
2N/A
2N/A if (strcmp(domain,
2N/A IDMAP_WK_CREATOR_SID_AUTHORITY) == 0)
2N/A return (B_FALSE);
2N/A
2N/A if ((strcmp(domain,
2N/A ksid_groups[i].ks_domain->kd_name) == 0) &&
2N/A rid == ksid_groups[i].ks_rid)
2N/A return (B_TRUE);
2N/A }
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Not found in ksidlist, check posix groups
2N/A */
2N/A gid = zfs_fuid_map_id(zfsvfs, id, cr, ZFS_GROUP);
2N/A return (groupmember(gid, cr));
2N/A}
2N/A
2N/Avoid
2N/Azfs_fuid_txhold(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
2N/A{
2N/A if (zfsvfs->z_fuid_obj == 0) {
2N/A dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
2N/A dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2N/A FUID_SIZE_ESTIMATE(zfsvfs));
2N/A dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
2N/A } else {
2N/A dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
2N/A dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
2N/A FUID_SIZE_ESTIMATE(zfsvfs));
2N/A }
2N/A}
2N/A#endif
2N/A