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 https://opensource.org/licenses/CDDL-1.0.
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
47extern "C" {
48#endif
49
50#if defined(HAVE_FILEMAP_RANGE_HAS_PAGE)
51#define	ZNODE_OS_FIELDS			\
52	inode_timespec_t z_btime; /* creation/birth time (cached) */ \
53	struct inode	z_inode;
54#else
55#define	ZNODE_OS_FIELDS			\
56	inode_timespec_t z_btime; /* creation/birth time (cached) */ \
57	struct inode	z_inode;                                     \
58	boolean_t	z_is_mapped;    /* we are mmap'ed */
59#endif
60
61/*
62 * Convert between znode pointers and inode pointers
63 */
64#define	ZTOI(znode)	(&((znode)->z_inode))
65#define	ITOZ(inode)	(container_of((inode), znode_t, z_inode))
66#define	ZTOZSB(znode)	((zfsvfs_t *)(ZTOI(znode)->i_sb->s_fs_info))
67#define	ITOZSB(inode)	((zfsvfs_t *)((inode)->i_sb->s_fs_info))
68
69#define	ZTOTYPE(zp)	(ZTOI(zp)->i_mode)
70#define	ZTOGID(zp) (ZTOI(zp)->i_gid)
71#define	ZTOUID(zp) (ZTOI(zp)->i_uid)
72#define	ZTONLNK(zp) (ZTOI(zp)->i_nlink)
73
74#define	Z_ISBLK(type) S_ISBLK(type)
75#define	Z_ISCHR(type) S_ISCHR(type)
76#define	Z_ISLNK(type) S_ISLNK(type)
77#define	Z_ISDEV(type)	(S_ISCHR(type) || S_ISBLK(type) || S_ISFIFO(type))
78#define	Z_ISDIR(type)	S_ISDIR(type)
79
80#if defined(HAVE_FILEMAP_RANGE_HAS_PAGE)
81#define	zn_has_cached_data(zp, start, end) \
82	filemap_range_has_page(ZTOI(zp)->i_mapping, start, end)
83#else
84#define	zn_has_cached_data(zp, start, end) \
85	((zp)->z_is_mapped)
86#endif
87
88#define	zn_flush_cached_data(zp, sync)	write_inode_now(ZTOI(zp), sync)
89#define	zn_rlimit_fsize(size)		(0)
90#define	zn_rlimit_fsize_uio(zp, uio)	(0)
91
92/*
93 * zhold() wraps igrab() on Linux, and igrab() may fail when the
94 * inode is in the process of being deleted.  As zhold() must only be
95 * called when a ref already exists - so the inode cannot be
96 * mid-deletion - we VERIFY() this.
97 */
98#define	zhold(zp)	VERIFY3P(igrab(ZTOI((zp))), !=, NULL)
99#define	zrele(zp)	iput(ZTOI((zp)))
100
101/* Called on entry to each ZFS inode and vfs operation. */
102static inline int
103zfs_enter(zfsvfs_t *zfsvfs, const char *tag)
104{
105	ZFS_TEARDOWN_ENTER_READ(zfsvfs, tag);
106	if (unlikely(zfsvfs->z_unmounted)) {
107		ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag);
108		return (SET_ERROR(EIO));
109	}
110	return (0);
111}
112
113/* Must be called before exiting the operation. */
114static inline void
115zfs_exit(zfsvfs_t *zfsvfs, const char *tag)
116{
117	zfs_exit_fs(zfsvfs);
118	ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag);
119}
120
121static inline int
122zpl_enter(zfsvfs_t *zfsvfs, const char *tag)
123{
124	return (-zfs_enter(zfsvfs, tag));
125}
126
127static inline void
128zpl_exit(zfsvfs_t *zfsvfs, const char *tag)
129{
130	ZFS_TEARDOWN_EXIT_READ(zfsvfs, tag);
131}
132
133/* zfs_verify_zp and zfs_enter_verify_zp are defined in zfs_znode.h */
134#define	zpl_verify_zp(zp)	(-zfs_verify_zp(zp))
135#define	zpl_enter_verify_zp(zfsvfs, zp, tag)	\
136	(-zfs_enter_verify_zp(zfsvfs, zp, tag))
137
138/*
139 * Macros for dealing with dmu_buf_hold
140 */
141#define	ZFS_OBJ_MTX_SZ		64
142#define	ZFS_OBJ_MTX_MAX		(1024 * 1024)
143#define	ZFS_OBJ_HASH(zfsvfs, obj)	((obj) & ((zfsvfs->z_hold_size) - 1))
144
145extern unsigned int zfs_object_mutex_size;
146
147/*
148 * Encode ZFS stored time values from a struct timespec / struct timespec64.
149 */
150#define	ZFS_TIME_ENCODE(tp, stmp)		\
151do {						\
152	(stmp)[0] = (uint64_t)(tp)->tv_sec;	\
153	(stmp)[1] = (uint64_t)(tp)->tv_nsec;	\
154} while (0)
155
156#if defined(HAVE_INODE_TIMESPEC64_TIMES)
157/*
158 * Decode ZFS stored time values to a struct timespec64
159 * 4.18 and newer kernels.
160 */
161#define	ZFS_TIME_DECODE(tp, stmp)		\
162do {						\
163	(tp)->tv_sec = (time64_t)(stmp)[0];	\
164	(tp)->tv_nsec = (long)(stmp)[1];	\
165} while (0)
166#else
167/*
168 * Decode ZFS stored time values to a struct timespec
169 * 4.17 and older kernels.
170 */
171#define	ZFS_TIME_DECODE(tp, stmp)		\
172do {						\
173	(tp)->tv_sec = (time_t)(stmp)[0];	\
174	(tp)->tv_nsec = (long)(stmp)[1];	\
175} while (0)
176#endif /* HAVE_INODE_TIMESPEC64_TIMES */
177
178#define	ZFS_ACCESSTIME_STAMP(zfsvfs, zp)
179
180struct znode;
181
182extern int	zfs_sync(struct super_block *, int, cred_t *);
183extern int	zfs_inode_alloc(struct super_block *, struct inode **ip);
184extern void	zfs_inode_destroy(struct inode *);
185extern void	zfs_mark_inode_dirty(struct inode *);
186extern boolean_t zfs_relatime_need_update(const struct inode *);
187
188#if defined(HAVE_UIO_RW)
189extern caddr_t zfs_map_page(page_t *, enum seg_rw);
190extern void zfs_unmap_page(page_t *, caddr_t);
191#endif /* HAVE_UIO_RW */
192
193extern zil_replay_func_t *const zfs_replay_vector[TX_MAX_TYPE];
194
195#ifdef	__cplusplus
196}
197#endif
198
199#endif	/* _SYS_ZFS_ZNODE_IMPL_H */
200