]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/os/freebsd/zfs/sys/zfs_znode_impl.h
Fix lseek(SEEK_DATA/SEEK_HOLE) mmap consistency
[FreeBSD/FreeBSD.git] / include / os / freebsd / zfs / sys / zfs_znode_impl.h
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) 2012, 2015 by Delphix. All rights reserved.
24  * Copyright (c) 2014 Integros [integros.com]
25  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
26  */
27
28 #ifndef _FREEBSD_ZFS_SYS_ZNODE_IMPL_H
29 #define _FREEBSD_ZFS_SYS_ZNODE_IMPL_H
30
31 #include <sys/list.h>
32 #include <sys/dmu.h>
33 #include <sys/sa.h>
34 #include <sys/zfs_vfsops.h>
35 #include <sys/rrwlock.h>
36 #include <sys/zfs_sa.h>
37 #include <sys/zfs_stat.h>
38 #include <sys/zfs_rlock.h>
39 #include <sys/zfs_acl.h>
40 #include <sys/zil.h>
41 #include <sys/zfs_project.h>
42 #include <vm/vm_object.h>
43 #include <sys/uio.h>
44
45 #ifdef  __cplusplus
46 extern "C" {
47 #endif
48
49 /*
50  * Directory entry locks control access to directory entries.
51  * They are used to protect creates, deletes, and renames.
52  * Each directory znode has a mutex and a list of locked names.
53  */
54 #define ZNODE_OS_FIELDS                 \
55         struct zfsvfs   *z_zfsvfs;      \
56         vnode_t         *z_vnode;       \
57         char            *z_cached_symlink;      \
58         uint64_t                z_uid;          \
59         uint64_t                z_gid;          \
60         uint64_t                z_gen;          \
61         uint64_t                z_atime[2];     \
62         uint64_t                z_links;
63
64 #define ZFS_LINK_MAX    UINT64_MAX
65
66 /*
67  * ZFS minor numbers can refer to either a control device instance or
68  * a zvol. Depending on the value of zss_type, zss_data points to either
69  * a zvol_state_t or a zfs_onexit_t.
70  */
71 enum zfs_soft_state_type {
72         ZSST_ZVOL,
73         ZSST_CTLDEV
74 };
75
76 typedef struct zfs_soft_state {
77         enum zfs_soft_state_type zss_type;
78         void *zss_data;
79 } zfs_soft_state_t;
80
81 extern minor_t zfsdev_minor_alloc(void);
82
83 /*
84  * Range locking rules
85  * --------------------
86  * 1. When truncating a file (zfs_create, zfs_setattr, zfs_space) the whole
87  *    file range needs to be locked as RL_WRITER. Only then can the pages be
88  *    freed etc and zp_size reset. zp_size must be set within range lock.
89  * 2. For writes and punching holes (zfs_write & zfs_space) just the range
90  *    being written or freed needs to be locked as RL_WRITER.
91  *    Multiple writes at the end of the file must coordinate zp_size updates
92  *    to ensure data isn't lost. A compare and swap loop is currently used
93  *    to ensure the file size is at least the offset last written.
94  * 3. For reads (zfs_read, zfs_get_data & zfs_putapage) just the range being
95  *    read needs to be locked as RL_READER. A check against zp_size can then
96  *    be made for reading beyond end of file.
97  */
98
99 /*
100  * Convert between znode pointers and vnode pointers
101  */
102 #define ZTOV(ZP)        ((ZP)->z_vnode)
103 #define ZTOI(ZP)        ((ZP)->z_vnode)
104 #define VTOZ(VP)        ((struct znode *)(VP)->v_data)
105 #define VTOZ_SMR(VP)    ((znode_t *)vn_load_v_data_smr(VP))
106 #define ITOZ(VP)        ((struct znode *)(VP)->v_data)
107 #define zhold(zp)       vhold(ZTOV((zp)))
108 #define zrele(zp)       vrele(ZTOV((zp)))
109
110 #define ZTOZSB(zp) ((zp)->z_zfsvfs)
111 #define ITOZSB(vp) (VTOZ(vp)->z_zfsvfs)
112 #define ZTOTYPE(zp)     (ZTOV(zp)->v_type)
113 #define ZTOGID(zp) ((zp)->z_gid)
114 #define ZTOUID(zp) ((zp)->z_uid)
115 #define ZTONLNK(zp) ((zp)->z_links)
116 #define Z_ISBLK(type) ((type) == VBLK)
117 #define Z_ISCHR(type) ((type) == VCHR)
118 #define Z_ISLNK(type) ((type) == VLNK)
119 #define Z_ISDIR(type) ((type) == VDIR)
120
121 #define zn_has_cached_data(zp)          vn_has_cached_data(ZTOV(zp))
122 #define zn_flush_cached_data(zp, sync)  vn_flush_cached_data(ZTOV(zp), sync)
123 #define zn_rlimit_fsize(zp, uio) \
124     vn_rlimit_fsize(ZTOV(zp), GET_UIO_STRUCT(uio), zfs_uio_td(uio))
125
126 /* Called on entry to each ZFS vnode and vfs operation  */
127 #define ZFS_ENTER(zfsvfs) \
128         { \
129                 ZFS_TEARDOWN_ENTER_READ((zfsvfs), FTAG); \
130                 if (__predict_false((zfsvfs)->z_unmounted)) { \
131                         ZFS_TEARDOWN_EXIT_READ(zfsvfs, FTAG); \
132                         return (EIO); \
133                 } \
134         }
135
136 /* Must be called before exiting the vop */
137 #define ZFS_EXIT(zfsvfs) ZFS_TEARDOWN_EXIT_READ(zfsvfs, FTAG)
138
139 /* Verifies the znode is valid */
140 #define ZFS_VERIFY_ZP(zp) \
141         if (__predict_false((zp)->z_sa_hdl == NULL)) { \
142                 ZFS_EXIT((zp)->z_zfsvfs); \
143                 return (EIO); \
144         } \
145
146 /*
147  * Macros for dealing with dmu_buf_hold
148  */
149 #define ZFS_OBJ_HASH(obj_num)   ((obj_num) & (ZFS_OBJ_MTX_SZ - 1))
150 #define ZFS_OBJ_MUTEX(zfsvfs, obj_num)  \
151         (&(zfsvfs)->z_hold_mtx[ZFS_OBJ_HASH(obj_num)])
152 #define ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num) \
153         mutex_enter(ZFS_OBJ_MUTEX((zfsvfs), (obj_num)))
154 #define ZFS_OBJ_HOLD_TRYENTER(zfsvfs, obj_num) \
155         mutex_tryenter(ZFS_OBJ_MUTEX((zfsvfs), (obj_num)))
156 #define ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num) \
157         mutex_exit(ZFS_OBJ_MUTEX((zfsvfs), (obj_num)))
158
159 /* Encode ZFS stored time values from a struct timespec */
160 #define ZFS_TIME_ENCODE(tp, stmp)               \
161 {                                               \
162         (stmp)[0] = (uint64_t)(tp)->tv_sec;     \
163         (stmp)[1] = (uint64_t)(tp)->tv_nsec;    \
164 }
165
166 /* Decode ZFS stored time values to a struct timespec */
167 #define ZFS_TIME_DECODE(tp, stmp)               \
168 {                                               \
169         (tp)->tv_sec = (time_t)(stmp)[0];               \
170         (tp)->tv_nsec = (long)(stmp)[1];                \
171 }
172 #define ZFS_ACCESSTIME_STAMP(zfsvfs, zp) \
173         if ((zfsvfs)->z_atime && !((zfsvfs)->z_vfs->vfs_flag & VFS_RDONLY)) \
174                 zfs_tstamp_update_setup_ext(zp, ACCESSED, NULL, NULL, B_FALSE);
175
176 extern void     zfs_tstamp_update_setup_ext(struct znode *,
177     uint_t, uint64_t [2], uint64_t [2], boolean_t have_tx);
178 extern void zfs_znode_free(struct znode *);
179
180 extern zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE];
181 extern int zfsfstype;
182
183 extern int zfs_znode_parent_and_name(struct znode *zp, struct znode **dzpp,
184     char *buf);
185 #ifdef  __cplusplus
186 }
187 #endif
188
189 #endif  /* _FREEBSD_SYS_FS_ZFS_ZNODE_H */