1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
26 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
27 */
28
29 #ifndef _FREEBSD_ZFS_SYS_ZNODE_IMPL_H
30 #define _FREEBSD_ZFS_SYS_ZNODE_IMPL_H
31
32 #ifdef _KERNEL
33 #include <sys/list.h>
34 #include <sys/dmu.h>
35 #include <sys/sa.h>
36 #include <sys/zfs_vfsops.h>
37 #include <sys/rrwlock.h>
38 #include <sys/zfs_sa.h>
39 #include <sys/zfs_stat.h>
40 #include <sys/zfs_rlock.h>
41 #include <sys/zfs_acl.h>
42 #include <sys/zil.h>
43 #include <sys/zfs_project.h>
44 #include <vm/vm_object.h>
45 #include <sys/uio.h>
46 #endif
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 /*
53 * Directory entry locks control access to directory entries.
54 * They are used to protect creates, deletes, and renames.
55 * Each directory znode has a mutex and a list of locked names.
56 */
57 #define ZNODE_OS_FIELDS \
58 struct zfsvfs *z_zfsvfs; \
59 struct vnode *z_vnode; \
60 char *z_cached_symlink; \
61 uint64_t z_uid; \
62 uint64_t z_gid; \
63 uint64_t z_gen; \
64 uint64_t z_atime[2]; \
65 uint64_t z_links;
66
67 #ifdef _KERNEL
68
69 #define ZFS_LINK_MAX UINT64_MAX
70
71 /*
72 * ZFS minor numbers can refer to either a control device instance or
73 * a zvol. Depending on the value of zss_type, zss_data points to either
74 * a zvol_state_t or a zfs_onexit_t.
75 */
76 enum zfs_soft_state_type {
77 ZSST_ZVOL,
78 ZSST_CTLDEV
79 };
80
81 typedef struct zfs_soft_state {
82 enum zfs_soft_state_type zss_type;
83 void *zss_data;
84 } zfs_soft_state_t;
85
86 /*
87 * Range locking rules
88 * --------------------
89 * 1. When truncating a file (zfs_create, zfs_setattr, zfs_space) the whole
90 * file range needs to be locked as RL_WRITER. Only then can the pages be
91 * freed etc and zp_size reset. zp_size must be set within range lock.
92 * 2. For writes and punching holes (zfs_write & zfs_space) just the range
93 * being written or freed needs to be locked as RL_WRITER.
94 * Multiple writes at the end of the file must coordinate zp_size updates
95 * to ensure data isn't lost. A compare and swap loop is currently used
96 * to ensure the file size is at least the offset last written.
97 * 3. For reads (zfs_read, zfs_get_data & zfs_putapage) just the range being
98 * read needs to be locked as RL_READER. A check against zp_size can then
99 * be made for reading beyond end of file.
100 */
101
102 /*
103 * Convert between znode pointers and vnode pointers
104 */
105 #define ZTOV(ZP) ((ZP)->z_vnode)
106 #define ZTOI(ZP) ((ZP)->z_vnode)
107 #define VTOZ(VP) ((struct znode *)(VP)->v_data)
108 #define VTOZ_SMR(VP) ((znode_t *)vn_load_v_data_smr(VP))
109 #define ITOZ(VP) ((struct znode *)(VP)->v_data)
110 #define zhold(zp) vhold(ZTOV((zp)))
111 #define zrele(zp) vrele(ZTOV((zp)))
112
113 #define ZTOZSB(zp) ((zp)->z_zfsvfs)
114 #define ITOZSB(vp) (VTOZ(vp)->z_zfsvfs)
115 #define ZTOTYPE(zp) (ZTOV(zp)->v_type)
116 #define ZTOGID(zp) ((zp)->z_gid)
117 #define ZTOUID(zp) ((zp)->z_uid)
118 #define ZTONLNK(zp) ((zp)->z_links)
119 #define Z_ISBLK(type) ((type) == VBLK)
120 #define Z_ISCHR(type) ((type) == VCHR)
121 #define Z_ISLNK(type) ((type) == VLNK)
122 #define Z_ISDIR(type) ((type) == VDIR)
123
124 #define zn_has_cached_data(zp, start, end) \
125 vn_has_cached_data(ZTOV(zp))
126 #define zn_flush_cached_data(zp, sync) vn_flush_cached_data(ZTOV(zp), sync)
127 #define zn_rlimit_fsize(size) zfs_rlimit_fsize(size)
128 #define zn_rlimit_fsize_uio(zp, uio) \
129 vn_rlimit_fsize(ZTOV(zp), GET_UIO_STRUCT(uio), zfs_uio_td(uio))
130
131 /* Called on entry to each ZFS vnode and vfs operation */
132 static inline int
zfs_enter(zfsvfs_t * zfsvfs,const char * tag)133 zfs_enter(zfsvfs_t *zfsvfs, const char *tag)
134 {
135 ZFS_TEARDOWN_ENTER_READ(zfsvfs, tag);
136 if (__predict_false((zfsvfs)->z_unmounted)) {
137 ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag);
138 return (SET_ERROR(EIO));
139 }
140 return (0);
141 }
142
143 /* Must be called before exiting the vop */
144 static inline void
zfs_exit(zfsvfs_t * zfsvfs,const char * tag)145 zfs_exit(zfsvfs_t *zfsvfs, const char *tag)
146 {
147 ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag);
148 }
149
150 /*
151 * Macros for dealing with dmu_buf_hold
152 */
153 #define ZFS_OBJ_HASH(obj_num) ((obj_num) & (ZFS_OBJ_MTX_SZ - 1))
154 #define ZFS_OBJ_MUTEX(zfsvfs, obj_num) \
155 (&(zfsvfs)->z_hold_mtx[ZFS_OBJ_HASH(obj_num)])
156 #define ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num) \
157 mutex_enter(ZFS_OBJ_MUTEX((zfsvfs), (obj_num)))
158 #define ZFS_OBJ_HOLD_TRYENTER(zfsvfs, obj_num) \
159 mutex_tryenter(ZFS_OBJ_MUTEX((zfsvfs), (obj_num)))
160 #define ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num) \
161 mutex_exit(ZFS_OBJ_MUTEX((zfsvfs), (obj_num)))
162
163 /* Encode ZFS stored time values from a struct timespec */
164 #define ZFS_TIME_ENCODE(tp, stmp) \
165 { \
166 (stmp)[0] = (uint64_t)(tp)->tv_sec; \
167 (stmp)[1] = (uint64_t)(tp)->tv_nsec; \
168 }
169
170 /* Decode ZFS stored time values to a struct timespec */
171 #define ZFS_TIME_DECODE(tp, stmp) \
172 { \
173 (tp)->tv_sec = (time_t)(stmp)[0]; \
174 (tp)->tv_nsec = (long)(stmp)[1]; \
175 }
176 #define ZFS_ACCESSTIME_STAMP(zfsvfs, zp) \
177 if ((zfsvfs)->z_atime && !((zfsvfs)->z_vfs->vfs_flag & VFS_RDONLY)) \
178 zfs_tstamp_update_setup_ext(zp, ACCESSED, NULL, NULL, B_FALSE);
179
180 extern void zfs_tstamp_update_setup_ext(struct znode *,
181 uint_t, uint64_t [2], uint64_t [2], boolean_t have_tx);
182 extern void zfs_znode_free(struct znode *);
183
184 extern zil_replay_func_t *const zfs_replay_vector[TX_MAX_TYPE];
185
186 extern int zfs_znode_parent_and_name(struct znode *zp, struct znode **dzpp,
187 char *buf, uint64_t buflen);
188
189 extern int zfs_rlimit_fsize(off_t fsize);
190
191 #endif /* _KERNEL */
192
193 #ifdef __cplusplus
194 }
195 #endif
196
197 #endif /* _FREEBSD_SYS_FS_ZFS_ZNODE_H */
198