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, 2018 by Delphix. All rights reserved.
24  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
25  */
26 
27 #ifndef	_SYS_ZFS_ZNODE_IMPL_H
28 #define	_SYS_ZFS_ZNODE_IMPL_H
29 
30 #ifndef _KERNEL
31 #error "no user serviceable parts within"
32 #endif
33 
34 #include <sys/isa_defs.h>
35 #include <sys/types32.h>
36 #include <sys/list.h>
37 #include <sys/dmu.h>
38 #include <sys/sa.h>
39 #include <sys/time.h>
40 #include <sys/zfs_vfsops.h>
41 #include <sys/rrwlock.h>
42 #include <sys/zfs_sa.h>
43 #include <sys/zfs_stat.h>
44 #include <sys/zfs_rlock.h>
45 
46 #ifdef	__cplusplus
47 extern "C" {
48 #endif
49 
50 #define	ZNODE_OS_FIELDS			\
51 	inode_timespec_t z_btime; /* creation/birth time (cached) */ \
52 	struct inode	z_inode;
53 
54 /*
55  * Convert between znode pointers and inode pointers
56  */
57 #define	ZTOI(znode)	(&((znode)->z_inode))
58 #define	ITOZ(inode)	(container_of((inode), znode_t, z_inode))
59 #define	ZTOZSB(znode)	((zfsvfs_t *)(ZTOI(znode)->i_sb->s_fs_info))
60 #define	ITOZSB(inode)	((zfsvfs_t *)((inode)->i_sb->s_fs_info))
61 
62 #define	ZTOTYPE(zp)	(ZTOI(zp)->i_mode)
63 #define	ZTOGID(zp) (ZTOI(zp)->i_gid)
64 #define	ZTOUID(zp) (ZTOI(zp)->i_uid)
65 #define	ZTONLNK(zp) (ZTOI(zp)->i_nlink)
66 
67 #define	Z_ISBLK(type) S_ISBLK(type)
68 #define	Z_ISCHR(type) S_ISCHR(type)
69 #define	Z_ISLNK(type) S_ISLNK(type)
70 #define	Z_ISDEV(type)	(S_ISCHR(type) || S_ISBLK(type) || S_ISFIFO(type))
71 #define	Z_ISDIR(type)	S_ISDIR(type)
72 
73 #define	zn_has_cached_data(zp)		((zp)->z_is_mapped)
74 #define	zn_flush_cached_data(zp, sync)	write_inode_now(ZTOI(zp), sync)
75 #define	zn_rlimit_fsize(zp, uio)	(0)
76 
77 /*
78  * zhold() wraps igrab() on Linux, and igrab() may fail when the
79  * inode is in the process of being deleted.  As zhold() must only be
80  * called when a ref already exists - so the inode cannot be
81  * mid-deletion - we VERIFY() this.
82  */
83 #define	zhold(zp)	VERIFY3P(igrab(ZTOI((zp))), !=, NULL)
84 #define	zrele(zp)	iput(ZTOI((zp)))
85 
86 /* Called on entry to each ZFS inode and vfs operation. */
87 #define	ZFS_ENTER_ERROR(zfsvfs, error)				\
88 do {								\
89 	ZFS_TEARDOWN_ENTER_READ(zfsvfs, FTAG);			\
90 	if (unlikely((zfsvfs)->z_unmounted)) {			\
91 		ZFS_TEARDOWN_EXIT_READ(zfsvfs, FTAG);		\
92 		return (error);					\
93 	}							\
94 } while (0)
95 #define	ZFS_ENTER(zfsvfs)	ZFS_ENTER_ERROR(zfsvfs, EIO)
96 #define	ZPL_ENTER(zfsvfs)	ZFS_ENTER_ERROR(zfsvfs, -EIO)
97 
98 /* Must be called before exiting the operation. */
99 #define	ZFS_EXIT(zfsvfs)					\
100 do {								\
101 	zfs_exit_fs(zfsvfs);					\
102 	ZFS_TEARDOWN_EXIT_READ(zfsvfs, FTAG);			\
103 } while (0)
104 
105 #define	ZPL_EXIT(zfsvfs)					\
106 do {								\
107 	rrm_exit(&(zfsvfs)->z_teardown_lock, FTAG);		\
108 } while (0)
109 
110 /* Verifies the znode is valid. */
111 #define	ZFS_VERIFY_ZP_ERROR(zp, error)				\
112 do {								\
113 	if (unlikely((zp)->z_sa_hdl == NULL)) {			\
114 		ZFS_EXIT(ZTOZSB(zp));				\
115 		return (error);					\
116 	}							\
117 } while (0)
118 #define	ZFS_VERIFY_ZP(zp)	ZFS_VERIFY_ZP_ERROR(zp, EIO)
119 #define	ZPL_VERIFY_ZP(zp)	ZFS_VERIFY_ZP_ERROR(zp, -EIO)
120 
121 /*
122  * Macros for dealing with dmu_buf_hold
123  */
124 #define	ZFS_OBJ_MTX_SZ		64
125 #define	ZFS_OBJ_MTX_MAX		(1024 * 1024)
126 #define	ZFS_OBJ_HASH(zfsvfs, obj)	((obj) & ((zfsvfs->z_hold_size) - 1))
127 
128 extern unsigned int zfs_object_mutex_size;
129 
130 /*
131  * Encode ZFS stored time values from a struct timespec / struct timespec64.
132  */
133 #define	ZFS_TIME_ENCODE(tp, stmp)		\
134 do {						\
135 	(stmp)[0] = (uint64_t)(tp)->tv_sec;	\
136 	(stmp)[1] = (uint64_t)(tp)->tv_nsec;	\
137 } while (0)
138 
139 #if defined(HAVE_INODE_TIMESPEC64_TIMES)
140 /*
141  * Decode ZFS stored time values to a struct timespec64
142  * 4.18 and newer kernels.
143  */
144 #define	ZFS_TIME_DECODE(tp, stmp)		\
145 do {						\
146 	(tp)->tv_sec = (time64_t)(stmp)[0];	\
147 	(tp)->tv_nsec = (long)(stmp)[1];	\
148 } while (0)
149 #else
150 /*
151  * Decode ZFS stored time values to a struct timespec
152  * 4.17 and older kernels.
153  */
154 #define	ZFS_TIME_DECODE(tp, stmp)		\
155 do {						\
156 	(tp)->tv_sec = (time_t)(stmp)[0];	\
157 	(tp)->tv_nsec = (long)(stmp)[1];	\
158 } while (0)
159 #endif /* HAVE_INODE_TIMESPEC64_TIMES */
160 
161 #define	ZFS_ACCESSTIME_STAMP(zfsvfs, zp)
162 
163 struct znode;
164 
165 extern int	zfs_sync(struct super_block *, int, cred_t *);
166 extern int	zfs_inode_alloc(struct super_block *, struct inode **ip);
167 extern void	zfs_inode_destroy(struct inode *);
168 extern void	zfs_mark_inode_dirty(struct inode *);
169 extern boolean_t zfs_relatime_need_update(const struct inode *);
170 
171 #if defined(HAVE_UIO_RW)
172 extern caddr_t zfs_map_page(page_t *, enum seg_rw);
173 extern void zfs_unmap_page(page_t *, caddr_t);
174 #endif /* HAVE_UIO_RW */
175 
176 extern zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE];
177 extern int zfsfstype;
178 
179 #ifdef	__cplusplus
180 }
181 #endif
182 
183 #endif	/* _SYS_ZFS_ZNODE_IMPL_H */
184