1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>.
24  * All rights reserved.
25  */
26 
27 #ifndef	_SYS_FS_ZFS_VFSOPS_H
28 #define	_SYS_FS_ZFS_VFSOPS_H
29 
30 #if __FreeBSD_version >= 1300125
31 #define	TEARDOWN_RMS
32 #endif
33 
34 #if __FreeBSD_version >= 1300109
35 #define	TEARDOWN_INACTIVE_RMS
36 #endif
37 
38 #include <sys/dataset_kstats.h>
39 #include <sys/list.h>
40 #include <sys/vfs.h>
41 #include <sys/zil.h>
42 #include <sys/sa.h>
43 #include <sys/rrwlock.h>
44 #ifdef TEARDOWN_INACTIVE_RMS
45 #include <sys/rmlock.h>
46 #endif
47 #include <sys/zfs_ioctl.h>
48 
49 #ifdef	__cplusplus
50 extern "C" {
51 #endif
52 
53 #ifdef TEARDOWN_RMS
54 typedef struct rmslock zfs_teardown_lock_t;
55 #else
56 #define	zfs_teardown_lock_t		rrmlock_t
57 #endif
58 
59 #ifdef TEARDOWN_INACTIVE_RMS
60 typedef struct rmslock zfs_teardown_inactive_lock_t;
61 #else
62 #define	zfs_teardown_inactive_lock_t krwlock_t
63 #endif
64 
65 typedef struct zfsvfs zfsvfs_t;
66 struct znode;
67 
68 struct zfsvfs {
69 	vfs_t		*z_vfs;		/* generic fs struct */
70 	zfsvfs_t	*z_parent;	/* parent fs */
71 	objset_t	*z_os;		/* objset reference */
72 	uint64_t	z_flags;	/* super_block flags */
73 	uint64_t	z_root;		/* id of root znode */
74 	uint64_t	z_unlinkedobj;	/* id of unlinked zapobj */
75 	uint64_t	z_max_blksz;	/* maximum block size for files */
76 	uint64_t	z_fuid_obj;	/* fuid table object number */
77 	uint64_t	z_fuid_size;	/* fuid table size */
78 	avl_tree_t	z_fuid_idx;	/* fuid tree keyed by index */
79 	avl_tree_t	z_fuid_domain;	/* fuid tree keyed by domain */
80 	krwlock_t	z_fuid_lock;	/* fuid lock */
81 	boolean_t	z_fuid_loaded;	/* fuid tables are loaded */
82 	boolean_t	z_fuid_dirty;   /* need to sync fuid table ? */
83 	struct zfs_fuid_info	*z_fuid_replay; /* fuid info for replay */
84 	zilog_t		*z_log;		/* intent log pointer */
85 	uint_t		z_acl_type;	/* type of acl usable on this fs */
86 	uint_t		z_acl_mode;	/* acl chmod/mode behavior */
87 	uint_t		z_acl_inherit;	/* acl inheritance behavior */
88 	zfs_case_t	z_case;		/* case-sense */
89 	boolean_t	z_utf8;		/* utf8-only */
90 	int		z_norm;		/* normalization flags */
91 	boolean_t	z_atime;	/* enable atimes mount option */
92 	boolean_t	z_unmounted;	/* unmounted */
93 	zfs_teardown_lock_t z_teardown_lock;
94 	zfs_teardown_inactive_lock_t z_teardown_inactive_lock;
95 	list_t		z_all_znodes;	/* all vnodes in the fs */
96 	uint64_t	z_nr_znodes;	/* number of znodes in the fs */
97 	kmutex_t	z_znodes_lock;	/* lock for z_all_znodes */
98 	struct zfsctl_root	*z_ctldir;	/* .zfs directory pointer */
99 	boolean_t	z_show_ctldir;	/* expose .zfs in the root dir */
100 	boolean_t	z_issnap;	/* true if this is a snapshot */
101 	boolean_t	z_use_fuids;	/* version allows fuids */
102 	boolean_t	z_replay;	/* set during ZIL replay */
103 	boolean_t	z_use_sa;	/* version allow system attributes */
104 	boolean_t	z_xattr_sa;	/* allow xattrs to be stores as SA */
105 	boolean_t	z_use_namecache; /* make use of FreeBSD name cache */
106 	uint8_t		z_xattr;	/* xattr type in use */
107 	uint64_t	z_version;	/* ZPL version */
108 	uint64_t	z_shares_dir;	/* hidden shares dir */
109 	dataset_kstats_t	z_kstat;	/* fs kstats */
110 	kmutex_t	z_lock;
111 	uint64_t	z_userquota_obj;
112 	uint64_t	z_groupquota_obj;
113 	uint64_t	z_userobjquota_obj;
114 	uint64_t	z_groupobjquota_obj;
115 	uint64_t	z_projectquota_obj;
116 	uint64_t	z_projectobjquota_obj;
117 	uint64_t	z_replay_eof;	/* New end of file - replay only */
118 	sa_attr_type_t	*z_attr_table;	/* SA attr mapping->id */
119 #define	ZFS_OBJ_MTX_SZ	64
120 	kmutex_t	z_hold_mtx[ZFS_OBJ_MTX_SZ];	/* znode hold locks */
121 	struct task	z_unlinked_drain_task;
122 };
123 
124 #ifdef TEARDOWN_RMS
125 #define	ZFS_TEARDOWN_INIT(zfsvfs)		\
126 	rms_init(&(zfsvfs)->z_teardown_lock, "zfs teardown")
127 
128 #define	ZFS_TEARDOWN_DESTROY(zfsvfs)		\
129 	rms_destroy(&(zfsvfs)->z_teardown_lock)
130 
131 #define	ZFS_TEARDOWN_TRY_ENTER_READ(zfsvfs)	\
132 	rms_try_rlock(&(zfsvfs)->z_teardown_lock)
133 
134 #define	ZFS_TEARDOWN_ENTER_READ(zfsvfs, tag)	\
135 	rms_rlock(&(zfsvfs)->z_teardown_lock);
136 
137 #define	ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag)	\
138 	rms_runlock(&(zfsvfs)->z_teardown_lock)
139 
140 #define	ZFS_TEARDOWN_ENTER_WRITE(zfsvfs, tag)	\
141 	rms_wlock(&(zfsvfs)->z_teardown_lock)
142 
143 #define	ZFS_TEARDOWN_EXIT_WRITE(zfsvfs)		\
144 	rms_wunlock(&(zfsvfs)->z_teardown_lock)
145 
146 #define	ZFS_TEARDOWN_EXIT(zfsvfs, tag)		\
147 	rms_unlock(&(zfsvfs)->z_teardown_lock)
148 
149 #define	ZFS_TEARDOWN_READ_HELD(zfsvfs)		\
150 	rms_rowned(&(zfsvfs)->z_teardown_lock)
151 
152 #define	ZFS_TEARDOWN_WRITE_HELD(zfsvfs)		\
153 	rms_wowned(&(zfsvfs)->z_teardown_lock)
154 
155 #define	ZFS_TEARDOWN_HELD(zfsvfs)		\
156 	rms_owned_any(&(zfsvfs)->z_teardown_lock)
157 #else
158 #define	ZFS_TEARDOWN_INIT(zfsvfs)		\
159 	rrm_init(&(zfsvfs)->z_teardown_lock, B_FALSE)
160 
161 #define	ZFS_TEARDOWN_DESTROY(zfsvfs)		\
162 	rrm_destroy(&(zfsvfs)->z_teardown_lock)
163 
164 #define	ZFS_TEARDOWN_TRY_ENTER_READ(zfsvfs)	\
165 	rw_tryenter(&(zfsvfs)->z_teardown_lock, RW_READER)
166 
167 #define	ZFS_TEARDOWN_ENTER_READ(zfsvfs, tag)	\
168 	rrm_enter_read(&(zfsvfs)->z_teardown_lock, tag);
169 
170 #define	ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag)	\
171 	rrm_exit(&(zfsvfs)->z_teardown_lock, tag)
172 
173 #define	ZFS_TEARDOWN_ENTER_WRITE(zfsvfs, tag)	\
174 	rrm_enter(&(zfsvfs)->z_teardown_lock, RW_WRITER, tag)
175 
176 #define	ZFS_TEARDOWN_EXIT_WRITE(zfsvfs)		\
177 	rrm_exit(&(zfsvfs)->z_teardown_lock, tag)
178 
179 #define	ZFS_TEARDOWN_EXIT(zfsvfs, tag)		\
180 	rrm_exit(&(zfsvfs)->z_teardown_lock, tag)
181 
182 #define	ZFS_TEARDOWN_READ_HELD(zfsvfs)		\
183 	RRM_READ_HELD(&(zfsvfs)->z_teardown_lock)
184 
185 #define	ZFS_TEARDOWN_WRITE_HELD(zfsvfs)		\
186 	RRM_WRITE_HELD(&(zfsvfs)->z_teardown_lock)
187 
188 #define	ZFS_TEARDOWN_HELD(zfsvfs)		\
189 	RRM_LOCK_HELD(&(zfsvfs)->z_teardown_lock)
190 #endif
191 
192 #ifdef TEARDOWN_INACTIVE_RMS
193 #define	ZFS_TEARDOWN_INACTIVE_INIT(zfsvfs)		\
194 	rms_init(&(zfsvfs)->z_teardown_inactive_lock, "zfs teardown inactive")
195 
196 #define	ZFS_TEARDOWN_INACTIVE_DESTROY(zfsvfs)		\
197 	rms_destroy(&(zfsvfs)->z_teardown_inactive_lock)
198 
199 #define	ZFS_TEARDOWN_INACTIVE_TRY_ENTER_READ(zfsvfs)	\
200 	rms_try_rlock(&(zfsvfs)->z_teardown_inactive_lock)
201 
202 #define	ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs)	\
203 	rms_rlock(&(zfsvfs)->z_teardown_inactive_lock)
204 
205 #define	ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs)		\
206 	rms_runlock(&(zfsvfs)->z_teardown_inactive_lock)
207 
208 #define	ZFS_TEARDOWN_INACTIVE_ENTER_WRITE(zfsvfs)	\
209 	rms_wlock(&(zfsvfs)->z_teardown_inactive_lock)
210 
211 #define	ZFS_TEARDOWN_INACTIVE_EXIT_WRITE(zfsvfs)	\
212 	rms_wunlock(&(zfsvfs)->z_teardown_inactive_lock)
213 
214 #define	ZFS_TEARDOWN_INACTIVE_WRITE_HELD(zfsvfs)	\
215 	rms_wowned(&(zfsvfs)->z_teardown_inactive_lock)
216 #else
217 #define	ZFS_TEARDOWN_INACTIVE_INIT(zfsvfs)		\
218 	rw_init(&(zfsvfs)->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL)
219 
220 #define	ZFS_TEARDOWN_INACTIVE_DESTROY(zfsvfs)		\
221 	rw_destroy(&(zfsvfs)->z_teardown_inactive_lock)
222 
223 #define	ZFS_TEARDOWN_INACTIVE_TRY_ENTER_READ(zfsvfs)	\
224 	rw_tryenter(&(zfsvfs)->z_teardown_inactive_lock, RW_READER)
225 
226 #define	ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs)	\
227 	rw_enter(&(zfsvfs)->z_teardown_inactive_lock, RW_READER)
228 
229 #define	ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs)		\
230 	rw_exit(&(zfsvfs)->z_teardown_inactive_lock)
231 
232 #define	ZFS_TEARDOWN_INACTIVE_ENTER_WRITE(zfsvfs)	\
233 	rw_enter(&(zfsvfs)->z_teardown_inactive_lock, RW_WRITER)
234 
235 #define	ZFS_TEARDOWN_INACTIVE_EXIT_WRITE(zfsvfs)	\
236 	rw_exit(&(zfsvfs)->z_teardown_inactive_lock)
237 
238 #define	ZFS_TEARDOWN_INACTIVE_WRITE_HELD(zfsvfs)	\
239 	RW_WRITE_HELD(&(zfsvfs)->z_teardown_inactive_lock)
240 #endif
241 
242 #define	ZSB_XATTR	0x0001		/* Enable user xattrs */
243 /*
244  * Normal filesystems (those not under .zfs/snapshot) have a total
245  * file ID size limited to 12 bytes (including the length field) due to
246  * NFSv2 protocol's limitation of 32 bytes for a filehandle.  For historical
247  * reasons, this same limit is being imposed by the Solaris NFSv3 implementation
248  * (although the NFSv3 protocol actually permits a maximum of 64 bytes).  It
249  * is not possible to expand beyond 12 bytes without abandoning support
250  * of NFSv2.
251  *
252  * For normal filesystems, we partition up the available space as follows:
253  *	2 bytes		fid length (required)
254  *	6 bytes		object number (48 bits)
255  *	4 bytes		generation number (32 bits)
256  *
257  * We reserve only 48 bits for the object number, as this is the limit
258  * currently defined and imposed by the DMU.
259  */
260 typedef struct zfid_short {
261 	uint16_t	zf_len;
262 	uint8_t		zf_object[6];		/* obj[i] = obj >> (8 * i) */
263 	uint8_t		zf_gen[4];		/* gen[i] = gen >> (8 * i) */
264 } zfid_short_t;
265 
266 /*
267  * Filesystems under .zfs/snapshot have a total file ID size of 22[*] bytes
268  * (including the length field).  This makes files under .zfs/snapshot
269  * accessible by NFSv3 and NFSv4, but not NFSv2.
270  *
271  * For files under .zfs/snapshot, we partition up the available space
272  * as follows:
273  *	2 bytes		fid length (required)
274  *	6 bytes		object number (48 bits)
275  *	4 bytes		generation number (32 bits)
276  *	6 bytes		objset id (48 bits)
277  *	4 bytes[**]	currently just zero (32 bits)
278  *
279  * We reserve only 48 bits for the object number and objset id, as these are
280  * the limits currently defined and imposed by the DMU.
281  *
282  * [*] 20 bytes on FreeBSD to fit into the size of struct fid.
283  * [**] 2 bytes on FreeBSD for the above reason.
284  */
285 typedef struct zfid_long {
286 	zfid_short_t	z_fid;
287 	uint8_t		zf_setid[6];		/* obj[i] = obj >> (8 * i) */
288 	uint8_t		zf_setgen[2];		/* gen[i] = gen >> (8 * i) */
289 } zfid_long_t;
290 
291 #define	SHORT_FID_LEN	(sizeof (zfid_short_t) - sizeof (uint16_t))
292 #define	LONG_FID_LEN	(sizeof (zfid_long_t) - sizeof (uint16_t))
293 
294 extern uint_t zfs_fsyncer_key;
295 extern int zfs_super_owner;
296 
297 extern void zfs_init(void);
298 extern void zfs_fini(void);
299 
300 extern int zfs_suspend_fs(zfsvfs_t *zfsvfs);
301 extern int zfs_resume_fs(zfsvfs_t *zfsvfs, struct dsl_dataset *ds);
302 extern int zfs_end_fs(zfsvfs_t *zfsvfs, struct dsl_dataset *ds);
303 extern int zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers);
304 extern int zfsvfs_create(const char *name, boolean_t readonly, zfsvfs_t **zfvp);
305 extern int zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os);
306 extern void zfsvfs_free(zfsvfs_t *zfsvfs);
307 extern int zfs_check_global_label(const char *dsname, const char *hexsl);
308 extern boolean_t zfs_is_readonly(zfsvfs_t *zfsvfs);
309 extern int zfs_get_temporary_prop(struct dsl_dataset *ds, zfs_prop_t zfs_prop,
310     uint64_t *val, char *setpoint);
311 extern int zfs_busy(void);
312 
313 #ifdef	__cplusplus
314 }
315 #endif
316 
317 #endif	/* _SYS_FS_ZFS_VFSOPS_H */
318