1/*-
2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/11/sys/cddl/compat/opensolaris/sys/vnode.h 318716 2017-05-23 07:27:30Z markj $
27 */
28
29#ifndef _OPENSOLARIS_SYS_VNODE_H_
30#define	_OPENSOLARIS_SYS_VNODE_H_
31
32#ifdef _KERNEL
33
34struct vnode;
35struct vattr;
36
37typedef	struct vnode	vnode_t;
38typedef	struct vattr	vattr_t;
39typedef enum vtype vtype_t;
40
41#include <sys/namei.h>
42enum symfollow { NO_FOLLOW = NOFOLLOW };
43
44#include <sys/proc.h>
45#include_next <sys/vnode.h>
46#include <sys/mount.h>
47#include <sys/cred.h>
48#include <sys/fcntl.h>
49#include <sys/file.h>
50#include <sys/filedesc.h>
51#include <sys/syscallsubr.h>
52
53typedef	struct vop_vector	vnodeops_t;
54#define	VOP_FID		VOP_VPTOFH
55#define	vop_fid		vop_vptofh
56#define	vop_fid_args	vop_vptofh_args
57#define	a_fid		a_fhp
58
59#define	IS_XATTRDIR(dvp)	(0)
60
61#define	v_count	v_usecount
62
63#define	V_APPEND	VAPPEND
64
65#define	rootvfs		(rootvnode == NULL ? NULL : rootvnode->v_mount)
66
67static __inline int
68vn_is_readonly(vnode_t *vp)
69{
70	return (vp->v_mount->mnt_flag & MNT_RDONLY);
71}
72#define	vn_vfswlock(vp)		(0)
73#define	vn_vfsunlock(vp)	do { } while (0)
74#define	vn_ismntpt(vp)		((vp)->v_type == VDIR && (vp)->v_mountedhere != NULL)
75#define	vn_mountedvfs(vp)	((vp)->v_mountedhere)
76#define	vn_has_cached_data(vp)	\
77	((vp)->v_object != NULL && \
78	 (vp)->v_object->resident_page_count > 0)
79#define	vn_exists(vp)		do { } while (0)
80#define	vn_invalid(vp)		do { } while (0)
81#define	vn_renamepath(tdvp, svp, tnm, lentnm)	do { } while (0)
82#define	vn_free(vp)		do { } while (0)
83#define	vn_matchops(vp, vops)	((vp)->v_op == &(vops))
84
85#define	VN_HOLD(v)	vref(v)
86#define	VN_RELE(v)	vrele(v)
87#define	VN_URELE(v)	vput(v)
88
89#define	vnevent_create(vp, ct)			do { } while (0)
90#define	vnevent_link(vp, ct)			do { } while (0)
91#define	vnevent_remove(vp, dvp, name, ct)	do { } while (0)
92#define	vnevent_rmdir(vp, dvp, name, ct)	do { } while (0)
93#define	vnevent_rename_src(vp, dvp, name, ct)	do { } while (0)
94#define	vnevent_rename_dest(vp, dvp, name, ct)	do { } while (0)
95#define	vnevent_rename_dest_dir(vp, ct)		do { } while (0)
96
97#define	specvp(vp, rdev, type, cr)	(VN_HOLD(vp), (vp))
98#define	MANDMODE(mode)		(0)
99#define	MANDLOCK(vp, mode)	(0)
100#define	chklock(vp, op, offset, size, mode, ct)	(0)
101#define	cleanlocks(vp, pid, foo)	do { } while (0)
102#define	cleanshares(vp, pid)		do { } while (0)
103
104/*
105 * We will use va_spare is place of Solaris' va_mask.
106 * This field is initialized in zfs_setattr().
107 */
108#define	va_mask		va_spare
109/* TODO: va_fileid is shorter than va_nodeid !!! */
110#define	va_nodeid	va_fileid
111/* TODO: This field needs conversion! */
112#define	va_nblocks	va_bytes
113#define	va_blksize	va_blocksize
114#define	va_seq		va_gen
115
116#define	MAXOFFSET_T	OFF_MAX
117#define	EXCL		0
118
119#define	ACCESSED		(AT_ATIME)
120#define	STATE_CHANGED		(AT_CTIME)
121#define	CONTENT_MODIFIED	(AT_MTIME | AT_CTIME)
122
123static __inline void
124vattr_init_mask(vattr_t *vap)
125{
126
127	vap->va_mask = 0;
128
129	if (vap->va_type != VNON)
130		vap->va_mask |= AT_TYPE;
131	if (vap->va_uid != (uid_t)VNOVAL)
132		vap->va_mask |= AT_UID;
133	if (vap->va_gid != (gid_t)VNOVAL)
134		vap->va_mask |= AT_GID;
135	if (vap->va_size != (u_quad_t)VNOVAL)
136		vap->va_mask |= AT_SIZE;
137	if (vap->va_atime.tv_sec != VNOVAL)
138		vap->va_mask |= AT_ATIME;
139	if (vap->va_mtime.tv_sec != VNOVAL)
140		vap->va_mask |= AT_MTIME;
141	if (vap->va_mode != (u_short)VNOVAL)
142		vap->va_mask |= AT_MODE;
143	if (vap->va_flags != VNOVAL)
144		vap->va_mask |= AT_XVATTR;
145}
146
147#define	FCREAT		O_CREAT
148#define	FTRUNC		O_TRUNC
149#define	FEXCL		O_EXCL
150#define	FDSYNC		FFSYNC
151#define	FRSYNC		FFSYNC
152#define	FSYNC		FFSYNC
153#define	FOFFMAX		0x00
154#define	FIGNORECASE	0x00
155
156static __inline int
157vn_openat(char *pnamep, enum uio_seg seg, int filemode, int createmode,
158    vnode_t **vpp, enum create crwhy, mode_t umask, struct vnode *startvp,
159    int fd)
160{
161	struct thread *td = curthread;
162	struct nameidata nd;
163	int error, operation;
164
165	ASSERT(seg == UIO_SYSSPACE);
166	if ((filemode & FCREAT) != 0) {
167		ASSERT(filemode == (FWRITE | FCREAT | FTRUNC | FOFFMAX));
168		ASSERT(crwhy == CRCREAT);
169		operation = CREATE;
170	} else {
171		ASSERT(filemode == (FREAD | FOFFMAX) ||
172		    filemode == (FREAD | FWRITE | FOFFMAX));
173		ASSERT(crwhy == 0);
174		operation = LOOKUP;
175	}
176	ASSERT(umask == 0);
177
178	pwd_ensure_dirs();
179
180	if (startvp != NULL)
181		vref(startvp);
182	NDINIT_ATVP(&nd, operation, 0, UIO_SYSSPACE, pnamep, startvp, td);
183	filemode |= O_NOFOLLOW;
184	error = vn_open_cred(&nd, &filemode, createmode, 0, td->td_ucred, NULL);
185	NDFREE(&nd, NDF_ONLY_PNBUF);
186	if (error == 0) {
187		/* We just unlock so we hold a reference. */
188		VOP_UNLOCK(nd.ni_vp, 0);
189		*vpp = nd.ni_vp;
190	}
191	return (error);
192}
193
194static __inline int
195zfs_vn_open(char *pnamep, enum uio_seg seg, int filemode, int createmode,
196    vnode_t **vpp, enum create crwhy, mode_t umask)
197{
198
199	return (vn_openat(pnamep, seg, filemode, createmode, vpp, crwhy,
200	    umask, NULL, -1));
201}
202#define	vn_open(pnamep, seg, filemode, createmode, vpp, crwhy, umask)	\
203	zfs_vn_open((pnamep), (seg), (filemode), (createmode), (vpp), (crwhy), (umask))
204
205#define	RLIM64_INFINITY	0
206static __inline int
207zfs_vn_rdwr(enum uio_rw rw, vnode_t *vp, caddr_t base, ssize_t len,
208    offset_t offset, enum uio_seg seg, int ioflag, int ulimit, cred_t *cr,
209    ssize_t *residp)
210{
211	struct thread *td = curthread;
212	int error;
213	ssize_t resid;
214
215	ASSERT(ioflag == 0);
216	ASSERT(ulimit == RLIM64_INFINITY);
217
218	if (rw == UIO_WRITE) {
219		ioflag = IO_SYNC;
220	} else {
221		ioflag = IO_DIRECT;
222	}
223	error = vn_rdwr(rw, vp, base, len, offset, seg, ioflag, cr, NOCRED,
224	    &resid, td);
225	if (residp != NULL)
226		*residp = (ssize_t)resid;
227	return (error);
228}
229#define	vn_rdwr(rw, vp, base, len, offset, seg, ioflag, ulimit, cr, residp) \
230	zfs_vn_rdwr((rw), (vp), (base), (len), (offset), (seg), (ioflag), (ulimit), (cr), (residp))
231
232static __inline int
233zfs_vop_fsync(vnode_t *vp, int flag, cred_t *cr)
234{
235	struct mount *mp;
236	int error;
237
238	ASSERT(flag == FSYNC);
239
240	if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
241		goto drop;
242	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
243	error = VOP_FSYNC(vp, MNT_WAIT, curthread);
244	VOP_UNLOCK(vp, 0);
245	vn_finished_write(mp);
246drop:
247	return (error);
248}
249#define	VOP_FSYNC(vp, flag, cr, ct)	zfs_vop_fsync((vp), (flag), (cr))
250
251static __inline int
252zfs_vop_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
253{
254	int error;
255
256	ASSERT(count == 1);
257	ASSERT(offset == 0);
258
259	error = vn_close(vp, flag, cr, curthread);
260	return (error);
261}
262#define	VOP_CLOSE(vp, oflags, count, offset, cr, ct)			\
263	zfs_vop_close((vp), (oflags), (count), (offset), (cr))
264
265static __inline int
266vn_rename(char *from, char *to, enum uio_seg seg)
267{
268
269	ASSERT(seg == UIO_SYSSPACE);
270
271	return (kern_renameat(curthread, AT_FDCWD, from, AT_FDCWD, to, seg));
272}
273
274static __inline int
275vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag)
276{
277
278	ASSERT(seg == UIO_SYSSPACE);
279	ASSERT(dirflag == RMFILE);
280
281	return (kern_unlinkat(curthread, AT_FDCWD, fnamep, seg, 0));
282}
283
284#endif	/* _KERNEL */
285
286#endif	/* _OPENSOLARIS_SYS_VNODE_H_ */
287