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