1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23260763Savg * Copyright (c) 2013 by Delphix. All rights reserved.
24265744Sdelphij * Copyright (c) 2014, Joyent, Inc. All rights reserved.
25288549Smav * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26168404Spjd */
27168404Spjd
28168404Spjd#ifndef	_SYS_DSL_DIR_H
29168404Spjd#define	_SYS_DSL_DIR_H
30168404Spjd
31168404Spjd#include <sys/dmu.h>
32168404Spjd#include <sys/dsl_pool.h>
33168404Spjd#include <sys/dsl_synctask.h>
34168404Spjd#include <sys/refcount.h>
35168404Spjd#include <sys/zfs_context.h>
36168404Spjd
37168404Spjd#ifdef	__cplusplus
38168404Spjdextern "C" {
39168404Spjd#endif
40168404Spjd
41168404Spjdstruct dsl_dataset;
42168404Spjd
43265744Sdelphij/*
44265744Sdelphij * DD_FIELD_* are strings that are used in the "extensified" dsl_dir zap object.
45265744Sdelphij * They should be of the format <reverse-dns>:<field>.
46265744Sdelphij */
47265744Sdelphij
48265744Sdelphij#define	DD_FIELD_FILESYSTEM_COUNT	"com.joyent:filesystem_count"
49265744Sdelphij#define	DD_FIELD_SNAPSHOT_COUNT		"com.joyent:snapshot_count"
50265744Sdelphij
51185029Spjdtypedef enum dd_used {
52185029Spjd	DD_USED_HEAD,
53185029Spjd	DD_USED_SNAP,
54185029Spjd	DD_USED_CHILD,
55185029Spjd	DD_USED_CHILD_RSRV,
56185029Spjd	DD_USED_REFRSRV,
57185029Spjd	DD_USED_NUM
58185029Spjd} dd_used_t;
59185029Spjd
60185029Spjd#define	DD_FLAG_USED_BREAKDOWN (1<<0)
61185029Spjd
62168404Spjdtypedef struct dsl_dir_phys {
63168404Spjd	uint64_t dd_creation_time; /* not actually used */
64168404Spjd	uint64_t dd_head_dataset_obj;
65168404Spjd	uint64_t dd_parent_obj;
66185029Spjd	uint64_t dd_origin_obj;
67168404Spjd	uint64_t dd_child_dir_zapobj;
68168404Spjd	/*
69168404Spjd	 * how much space our children are accounting for; for leaf
70168404Spjd	 * datasets, == physical space used by fs + snaps
71168404Spjd	 */
72168404Spjd	uint64_t dd_used_bytes;
73168404Spjd	uint64_t dd_compressed_bytes;
74168404Spjd	uint64_t dd_uncompressed_bytes;
75168404Spjd	/* Administrative quota setting */
76168404Spjd	uint64_t dd_quota;
77168404Spjd	/* Administrative reservation setting */
78168404Spjd	uint64_t dd_reserved;
79168404Spjd	uint64_t dd_props_zapobj;
80185029Spjd	uint64_t dd_deleg_zapobj; /* dataset delegation permissions */
81185029Spjd	uint64_t dd_flags;
82185029Spjd	uint64_t dd_used_breakdown[DD_USED_NUM];
83219089Spjd	uint64_t dd_clones; /* dsl_dir objects */
84219089Spjd	uint64_t dd_pad[13]; /* pad out to 256 bytes for good measure */
85168404Spjd} dsl_dir_phys_t;
86168404Spjd
87168404Spjdstruct dsl_dir {
88288549Smav	dmu_buf_user_t dd_dbu;
89288549Smav
90168404Spjd	/* These are immutable; no lock needed: */
91168404Spjd	uint64_t dd_object;
92168404Spjd	dsl_pool_t *dd_pool;
93168404Spjd
94277585Sdelphij	/* Stable until user eviction; no lock needed: */
95277585Sdelphij	dmu_buf_t *dd_dbuf;
96277585Sdelphij
97168404Spjd	/* protected by lock on pool's dp_dirty_dirs list */
98168404Spjd	txg_node_t dd_dirty_link;
99168404Spjd
100168404Spjd	/* protected by dp_config_rwlock */
101168404Spjd	dsl_dir_t *dd_parent;
102168404Spjd
103168404Spjd	/* Protected by dd_lock */
104168404Spjd	kmutex_t dd_lock;
105289100Sdelphij	list_t dd_props; /* list of dsl_prop_record_t's */
106219089Spjd	timestruc_t dd_snap_cmtime; /* last time snapshot namespace changed */
107219089Spjd	uint64_t dd_origin_txg;
108168404Spjd
109168404Spjd	/* gross estimate of space used by in-flight tx's */
110168404Spjd	uint64_t dd_tempreserved[TXG_SIZE];
111168404Spjd	/* amount of space we expect to write; == amount of dirty data */
112168404Spjd	int64_t dd_space_towrite[TXG_SIZE];
113168404Spjd
114168404Spjd	/* protected by dd_lock; keep at end of struct for better locality */
115307122Smav	char dd_myname[ZFS_MAX_DATASET_NAME_LEN];
116168404Spjd};
117168404Spjd
118277585Sdelphijinline dsl_dir_phys_t *
119277585Sdelphijdsl_dir_phys(dsl_dir_t *dd)
120277585Sdelphij{
121277585Sdelphij	return (dd->dd_dbuf->db_data);
122277585Sdelphij}
123277585Sdelphij
124248571Smmvoid dsl_dir_rele(dsl_dir_t *dd, void *tag);
125288549Smavvoid dsl_dir_async_rele(dsl_dir_t *dd, void *tag);
126248571Smmint dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
127248571Smm    dsl_dir_t **, const char **tail);
128248571Smmint dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
129168404Spjd    const char *tail, void *tag, dsl_dir_t **);
130168404Spjdvoid dsl_dir_name(dsl_dir_t *dd, char *buf);
131168498Spjdint dsl_dir_namelen(dsl_dir_t *dd);
132185029Spjduint64_t dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds,
133185029Spjd    const char *name, dmu_tx_t *tx);
134168404Spjdvoid dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv);
135168404Spjduint64_t dsl_dir_space_available(dsl_dir_t *dd,
136168404Spjd    dsl_dir_t *ancestor, int64_t delta, int ondiskonly);
137168404Spjdvoid dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx);
138168404Spjdvoid dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx);
139168404Spjdint dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t mem,
140185029Spjd    uint64_t asize, uint64_t fsize, uint64_t usize, void **tr_cookiep,
141185029Spjd    dmu_tx_t *tx);
142168404Spjdvoid dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx);
143168404Spjdvoid dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx);
144185029Spjdvoid dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
145168404Spjd    int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx);
146185029Spjdvoid dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
147185029Spjd    dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx);
148219089Spjdint dsl_dir_set_quota(const char *ddname, zprop_source_t source,
149219089Spjd    uint64_t quota);
150219089Spjdint dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
151219089Spjd    uint64_t reservation);
152265744Sdelphijint dsl_dir_activate_fs_ss_limit(const char *);
153265744Sdelphijint dsl_fs_ss_limit_check(dsl_dir_t *, uint64_t, zfs_prop_t, dsl_dir_t *,
154265744Sdelphij    cred_t *);
155265744Sdelphijvoid dsl_fs_ss_count_adjust(dsl_dir_t *, int64_t, const char *, dmu_tx_t *);
156248571Smmint dsl_dir_rename(const char *oldname, const char *newname);
157265744Sdelphijint dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
158265744Sdelphij    uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *);
159185029Spjdboolean_t dsl_dir_is_clone(dsl_dir_t *dd);
160185029Spjdvoid dsl_dir_new_refreservation(dsl_dir_t *dd, struct dsl_dataset *ds,
161185029Spjd    uint64_t reservation, cred_t *cr, dmu_tx_t *tx);
162219089Spjdvoid dsl_dir_snap_cmtime_update(dsl_dir_t *dd);
163219089Spjdtimestruc_t dsl_dir_snap_cmtime(dsl_dir_t *dd);
164248571Smmvoid dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value,
165248571Smm    dmu_tx_t *tx);
166265744Sdelphijvoid dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx);
167265744Sdelphijboolean_t dsl_dir_is_zapified(dsl_dir_t *dd);
168168404Spjd
169168404Spjd/* internal reserved dir name */
170168404Spjd#define	MOS_DIR_NAME "$MOS"
171185029Spjd#define	ORIGIN_DIR_NAME "$ORIGIN"
172219089Spjd#define	XLATION_DIR_NAME "$XLATION"
173219089Spjd#define	FREE_DIR_NAME "$FREE"
174268650Sdelphij#define	LEAK_DIR_NAME "$LEAK"
175168404Spjd
176168404Spjd#ifdef ZFS_DEBUG
177168404Spjd#define	dprintf_dd(dd, fmt, ...) do { \
178168404Spjd	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
179307122Smav	char *__ds_name = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); \
180168404Spjd	dsl_dir_name(dd, __ds_name); \
181168404Spjd	dprintf("dd=%s " fmt, __ds_name, __VA_ARGS__); \
182307122Smav	kmem_free(__ds_name, ZFS_MAX_DATASET_NAME_LEN); \
183168404Spjd	} \
184168404Spjd_NOTE(CONSTCOND) } while (0)
185168404Spjd#else
186168404Spjd#define	dprintf_dd(dd, fmt, ...)
187168404Spjd#endif
188168404Spjd
189168404Spjd#ifdef	__cplusplus
190168404Spjd}
191168404Spjd#endif
192168404Spjd
193168404Spjd#endif /* _SYS_DSL_DIR_H */
194