mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2025-05-23 23:14:59 +03:00

### Background Various admin operations will be invoked by some userspace task, but the work will be done on a separate kernel thread at a later time. Snapshots are an example, which are triggered through zfs_ioc_snapshot() -> dsl_dataset_snapshot(), but the actual work is from a task dispatched to dp_sync_taskq. Many such tasks end up in dsl_enforce_ds_ss_limits(), where various limits and permissions are enforced. Among other things, it is necessary to ensure that the invoking task (that is, the user) has permission to do things. We can't simply check if the running task has permission; it is a privileged kernel thread, which can do anything. However, in the general case it's not safe to simply query the task for its permissions at the check time, as the task may not exist any more, or its permissions may have changed since it was first invoked. So instead, we capture the permissions by saving CRED() in the user task, and then using it for the check through the secpolicy_* functions. ### Current implementation The current code calls CRED() to get the credential, which gets a pointer to the cred_t inside the current task and passes it to the worker task. However, it doesn't take a reference to the cred_t, and so expects that it won't change, and that the task continues to exist. In practice that is always the case, because we don't let the calling task return from the kernel until the work is done. For Linux, we also take a reference to the current task, because the Linux credential APIs for the most part do not check an arbitrary credential, but rather, query what a task can do. See secpolicy_zfs_proc(). Again, we don't take a reference on the task, just a pointer to it. ### Changes We change to calling crhold() on the task credential, and crfree() when we're done with it. This ensures it stays alive and unchanged for the duration of the call. On the Linux side, we change the main policy checking function priv_policy_ns() to use override_creds()/revert_creds() if necessary to make the provided credential active in the current task, allowing the standard task-permission APIs to do the needed check. Since the task pointer is no longer required, this lets us entirely remove secpolicy_zfs_proc() and the need to carry a task pointer around as well. Sponsored-by: https://despairlabs.com/sponsor/ Signed-off-by: Rob Norris <robn@despairlabs.com> Reviewed-by: Pavel Snajdr <snajpa@snajpa.net> Reviewed-by: Alexander Motin <mav@FreeBSD.org> Reviewed-by: Kyle Evans <kevans@FreeBSD.org> Reviewed-by: Tony Hutter <hutter2@llnl.gov>
233 lines
8.0 KiB
C
233 lines
8.0 KiB
C
// SPDX-License-Identifier: CDDL-1.0
|
|
/*
|
|
* CDDL HEADER START
|
|
*
|
|
* The contents of this file are subject to the terms of the
|
|
* Common Development and Distribution License (the "License").
|
|
* You may not use this file except in compliance with the License.
|
|
*
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
* or https://opensource.org/licenses/CDDL-1.0.
|
|
* See the License for the specific language governing permissions
|
|
* and limitations under the License.
|
|
*
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
*
|
|
* CDDL HEADER END
|
|
*/
|
|
/*
|
|
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
|
|
* Copyright (c) 2012, 2018 by Delphix. All rights reserved.
|
|
* Copyright (c) 2014, Joyent, Inc. All rights reserved.
|
|
* Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
|
|
*/
|
|
|
|
#ifndef _SYS_DSL_DIR_H
|
|
#define _SYS_DSL_DIR_H
|
|
|
|
#include <sys/dmu.h>
|
|
#include <sys/dsl_deadlist.h>
|
|
#include <sys/dsl_pool.h>
|
|
#include <sys/dsl_synctask.h>
|
|
#include <sys/zfs_refcount.h>
|
|
#include <sys/zfs_context.h>
|
|
#include <sys/dsl_crypt.h>
|
|
#include <sys/bplist.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
struct dsl_dataset;
|
|
struct zthr;
|
|
/*
|
|
* DD_FIELD_* are strings that are used in the "extensified" dsl_dir zap object.
|
|
* They should be of the format <reverse-dns>:<field>.
|
|
*/
|
|
|
|
#define DD_FIELD_FILESYSTEM_COUNT "com.joyent:filesystem_count"
|
|
#define DD_FIELD_SNAPSHOT_COUNT "com.joyent:snapshot_count"
|
|
#define DD_FIELD_CRYPTO_KEY_OBJ "com.datto:crypto_key_obj"
|
|
#define DD_FIELD_LIVELIST "com.delphix:livelist"
|
|
#define DD_FIELD_SNAPSHOTS_CHANGED "com.ixsystems:snapshots_changed"
|
|
|
|
typedef enum dd_used {
|
|
DD_USED_HEAD,
|
|
DD_USED_SNAP,
|
|
DD_USED_CHILD,
|
|
DD_USED_CHILD_RSRV,
|
|
DD_USED_REFRSRV,
|
|
DD_USED_NUM
|
|
} dd_used_t;
|
|
|
|
#define DD_FLAG_USED_BREAKDOWN (1<<0)
|
|
|
|
typedef struct dsl_dir_phys {
|
|
uint64_t dd_creation_time; /* not actually used */
|
|
uint64_t dd_head_dataset_obj;
|
|
uint64_t dd_parent_obj;
|
|
uint64_t dd_origin_obj;
|
|
uint64_t dd_child_dir_zapobj;
|
|
/*
|
|
* how much space our children are accounting for; for leaf
|
|
* datasets, == physical space used by fs + snaps
|
|
*/
|
|
uint64_t dd_used_bytes;
|
|
uint64_t dd_compressed_bytes;
|
|
uint64_t dd_uncompressed_bytes;
|
|
/* Administrative quota setting */
|
|
uint64_t dd_quota;
|
|
/* Administrative reservation setting */
|
|
uint64_t dd_reserved;
|
|
uint64_t dd_props_zapobj;
|
|
uint64_t dd_deleg_zapobj; /* dataset delegation permissions */
|
|
uint64_t dd_flags;
|
|
uint64_t dd_used_breakdown[DD_USED_NUM];
|
|
uint64_t dd_clones; /* dsl_dir objects */
|
|
uint64_t dd_pad[13]; /* pad out to 256 bytes for good measure */
|
|
} dsl_dir_phys_t;
|
|
|
|
struct dsl_dir {
|
|
dmu_buf_user_t dd_dbu;
|
|
|
|
/* These are immutable; no lock needed: */
|
|
uint64_t dd_object;
|
|
uint64_t dd_crypto_obj;
|
|
dsl_pool_t *dd_pool;
|
|
|
|
/* Stable until user eviction; no lock needed: */
|
|
dmu_buf_t *dd_dbuf;
|
|
|
|
/* protected by lock on pool's dp_dirty_dirs list */
|
|
txg_node_t dd_dirty_link;
|
|
|
|
/* protected by dp_config_rwlock */
|
|
dsl_dir_t *dd_parent;
|
|
|
|
/* Protected by dd_lock */
|
|
kmutex_t dd_lock;
|
|
list_t dd_props; /* list of dsl_prop_record_t's */
|
|
inode_timespec_t dd_snap_cmtime; /* last snapshot namespace change */
|
|
uint64_t dd_origin_txg;
|
|
|
|
/* gross estimate of space used by in-flight tx's */
|
|
uint64_t dd_tempreserved[TXG_SIZE];
|
|
/* amount of space we expect to write; == amount of dirty data */
|
|
uint64_t dd_space_towrite[TXG_SIZE];
|
|
|
|
dsl_deadlist_t dd_livelist;
|
|
bplist_t dd_pending_frees;
|
|
bplist_t dd_pending_allocs;
|
|
|
|
kmutex_t dd_activity_lock;
|
|
kcondvar_t dd_activity_cv;
|
|
boolean_t dd_activity_cancelled;
|
|
uint64_t dd_activity_waiters;
|
|
|
|
/* protected by dd_lock; keep at end of struct for better locality */
|
|
char dd_myname[ZFS_MAX_DATASET_NAME_LEN];
|
|
};
|
|
|
|
static inline dsl_dir_phys_t *
|
|
dsl_dir_phys(dsl_dir_t *dd)
|
|
{
|
|
return (dd->dd_dbuf->db_data);
|
|
}
|
|
|
|
void dsl_dir_rele(dsl_dir_t *dd, const void *tag);
|
|
void dsl_dir_async_rele(dsl_dir_t *dd, const void *tag);
|
|
int dsl_dir_hold(dsl_pool_t *dp, const char *name, const void *tag,
|
|
dsl_dir_t **, const char **tail);
|
|
int dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
|
|
const char *tail, const void *tag, dsl_dir_t **);
|
|
void dsl_dir_name(dsl_dir_t *dd, char *buf);
|
|
int dsl_dir_namelen(dsl_dir_t *dd);
|
|
uint64_t dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds,
|
|
const char *name, dmu_tx_t *tx);
|
|
|
|
uint64_t dsl_dir_get_used(dsl_dir_t *dd);
|
|
uint64_t dsl_dir_get_compressed(dsl_dir_t *dd);
|
|
uint64_t dsl_dir_get_quota(dsl_dir_t *dd);
|
|
uint64_t dsl_dir_get_reservation(dsl_dir_t *dd);
|
|
uint64_t dsl_dir_get_compressratio(dsl_dir_t *dd);
|
|
uint64_t dsl_dir_get_logicalused(dsl_dir_t *dd);
|
|
uint64_t dsl_dir_get_usedsnap(dsl_dir_t *dd);
|
|
uint64_t dsl_dir_get_usedds(dsl_dir_t *dd);
|
|
uint64_t dsl_dir_get_usedrefreserv(dsl_dir_t *dd);
|
|
uint64_t dsl_dir_get_usedchild(dsl_dir_t *dd);
|
|
void dsl_dir_get_origin(dsl_dir_t *dd, char *buf);
|
|
int dsl_dir_get_filesystem_count(dsl_dir_t *dd, uint64_t *count);
|
|
int dsl_dir_get_snapshot_count(dsl_dir_t *dd, uint64_t *count);
|
|
|
|
void dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv);
|
|
uint64_t dsl_dir_space_available(dsl_dir_t *dd,
|
|
dsl_dir_t *ancestor, int64_t delta, int ondiskonly);
|
|
void dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx);
|
|
void dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx);
|
|
int dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t mem,
|
|
uint64_t asize, boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx);
|
|
void dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx);
|
|
void dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx);
|
|
void dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
|
|
int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx);
|
|
void dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
|
|
dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx);
|
|
void dsl_dir_diduse_transfer_space(dsl_dir_t *dd, int64_t used,
|
|
int64_t compressed, int64_t uncompressed, int64_t tonew,
|
|
dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx);
|
|
int dsl_dir_set_quota(const char *ddname, zprop_source_t source,
|
|
uint64_t quota);
|
|
int dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
|
|
uint64_t reservation);
|
|
int dsl_dir_activate_fs_ss_limit(const char *);
|
|
int dsl_fs_ss_limit_check(dsl_dir_t *, uint64_t, zfs_prop_t, dsl_dir_t *,
|
|
cred_t *);
|
|
void dsl_fs_ss_count_adjust(dsl_dir_t *, int64_t, const char *, dmu_tx_t *);
|
|
int dsl_dir_rename(const char *oldname, const char *newname);
|
|
int dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
|
|
uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *);
|
|
boolean_t dsl_dir_is_clone(dsl_dir_t *dd);
|
|
void dsl_dir_new_refreservation(dsl_dir_t *dd, struct dsl_dataset *ds,
|
|
uint64_t reservation, cred_t *cr, dmu_tx_t *tx);
|
|
void dsl_dir_snap_cmtime_update(dsl_dir_t *dd, dmu_tx_t *tx);
|
|
inode_timespec_t dsl_dir_snap_cmtime(dsl_dir_t *dd);
|
|
void dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value,
|
|
dmu_tx_t *tx);
|
|
void dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx);
|
|
boolean_t dsl_dir_is_zapified(dsl_dir_t *dd);
|
|
int dsl_dir_livelist_open(dsl_dir_t *dd, uint64_t obj);
|
|
void dsl_dir_livelist_close(dsl_dir_t *dd);
|
|
void dsl_dir_remove_livelist(dsl_dir_t *dd, dmu_tx_t *tx, boolean_t total);
|
|
int dsl_dir_wait(dsl_dir_t *dd, dsl_dataset_t *ds, zfs_wait_activity_t activity,
|
|
boolean_t *waited);
|
|
void dsl_dir_cancel_waiters(dsl_dir_t *dd);
|
|
|
|
/* internal reserved dir name */
|
|
#define MOS_DIR_NAME "$MOS"
|
|
#define ORIGIN_DIR_NAME "$ORIGIN"
|
|
#define FREE_DIR_NAME "$FREE"
|
|
#define LEAK_DIR_NAME "$LEAK"
|
|
|
|
#ifdef ZFS_DEBUG
|
|
#define dprintf_dd(dd, fmt, ...) do { \
|
|
if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
|
|
char *__ds_name = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); \
|
|
dsl_dir_name(dd, __ds_name); \
|
|
dprintf("dd=%s " fmt, __ds_name, __VA_ARGS__); \
|
|
kmem_free(__ds_name, ZFS_MAX_DATASET_NAME_LEN); \
|
|
} \
|
|
} while (0)
|
|
#else
|
|
#define dprintf_dd(dd, fmt, ...)
|
|
#endif
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* _SYS_DSL_DIR_H */
|