vnode.h revision 285391
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: head/sys/cddl/compat/opensolaris/sys/vnode.h 285391 2015-07-11 16:22:48Z mjg $
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	  !vm_object_cache_is_empty((vp)->v_object)))
80#define	vn_exists(vp)		do { } while (0)
81#define	vn_invalid(vp)		do { } while (0)
82#define	vn_renamepath(tdvp, svp, tnm, lentnm)	do { } while (0)
83#define	vn_free(vp)		do { } while (0)
84#define	vn_matchops(vp, vops)	((vp)->v_op == &(vops))
85
86#define	VN_HOLD(v)	vref(v)
87#define	VN_RELE(v)	vrele(v)
88#define	VN_URELE(v)	vput(v)
89
90#define	VOP_REALVP(vp, vpp, ct)	(*(vpp) = (vp), 0)
91
92#define	vnevent_create(vp, ct)			do { } while (0)
93#define	vnevent_link(vp, ct)			do { } while (0)
94#define	vnevent_remove(vp, dvp, name, ct)	do { } while (0)
95#define	vnevent_rmdir(vp, dvp, name, ct)	do { } while (0)
96#define	vnevent_rename_src(vp, dvp, name, ct)	do { } while (0)
97#define	vnevent_rename_dest(vp, dvp, name, ct)	do { } while (0)
98#define	vnevent_rename_dest_dir(vp, ct)		do { } while (0)
99
100#define	specvp(vp, rdev, type, cr)	(VN_HOLD(vp), (vp))
101#define	MANDMODE(mode)		(0)
102#define	MANDLOCK(vp, mode)	(0)
103#define	chklock(vp, op, offset, size, mode, ct)	(0)
104#define	cleanlocks(vp, pid, foo)	do { } while (0)
105#define	cleanshares(vp, pid)		do { } while (0)
106
107/*
108 * We will use va_spare is place of Solaris' va_mask.
109 * This field is initialized in zfs_setattr().
110 */
111#define	va_mask		va_spare
112/* TODO: va_fileid is shorter than va_nodeid !!! */
113#define	va_nodeid	va_fileid
114/* TODO: This field needs conversion! */
115#define	va_nblocks	va_bytes
116#define	va_blksize	va_blocksize
117#define	va_seq		va_gen
118
119#define	MAXOFFSET_T	OFF_MAX
120#define	EXCL		0
121
122#define	ACCESSED		(AT_ATIME)
123#define	STATE_CHANGED		(AT_CTIME)
124#define	CONTENT_MODIFIED	(AT_MTIME | AT_CTIME)
125
126static __inline void
127vattr_init_mask(vattr_t *vap)
128{
129
130	vap->va_mask = 0;
131
132	if (vap->va_type != VNON)
133		vap->va_mask |= AT_TYPE;
134	if (vap->va_uid != (uid_t)VNOVAL)
135		vap->va_mask |= AT_UID;
136	if (vap->va_gid != (gid_t)VNOVAL)
137		vap->va_mask |= AT_GID;
138	if (vap->va_size != (u_quad_t)VNOVAL)
139		vap->va_mask |= AT_SIZE;
140	if (vap->va_atime.tv_sec != VNOVAL)
141		vap->va_mask |= AT_ATIME;
142	if (vap->va_mtime.tv_sec != VNOVAL)
143		vap->va_mask |= AT_MTIME;
144	if (vap->va_mode != (u_short)VNOVAL)
145		vap->va_mask |= AT_MODE;
146	if (vap->va_flags != VNOVAL)
147		vap->va_mask |= AT_XVATTR;
148}
149
150#define	FCREAT		O_CREAT
151#define	FTRUNC		O_TRUNC
152#define	FEXCL		O_EXCL
153#define	FDSYNC		FFSYNC
154#define	FRSYNC		FFSYNC
155#define	FSYNC		FFSYNC
156#define	FOFFMAX		0x00
157#define	FIGNORECASE	0x00
158
159static __inline int
160vn_openat(char *pnamep, enum uio_seg seg, int filemode, int createmode,
161    vnode_t **vpp, enum create crwhy, mode_t umask, struct vnode *startvp,
162    int fd)
163{
164	struct thread *td = curthread;
165	struct nameidata nd;
166	int error, operation;
167
168	ASSERT(seg == UIO_SYSSPACE);
169	if ((filemode & FCREAT) != 0) {
170		ASSERT(filemode == (FWRITE | FCREAT | FTRUNC | FOFFMAX));
171		ASSERT(crwhy == CRCREAT);
172		operation = CREATE;
173	} else {
174		ASSERT(filemode == (FREAD | FOFFMAX) ||
175		    filemode == (FREAD | FWRITE | FOFFMAX));
176		ASSERT(crwhy == 0);
177		operation = LOOKUP;
178	}
179	ASSERT(umask == 0);
180
181	pwd_ensure_dirs();
182
183	if (startvp != NULL)
184		vref(startvp);
185	NDINIT_ATVP(&nd, operation, 0, UIO_SYSSPACE, pnamep, startvp, td);
186	filemode |= O_NOFOLLOW;
187	error = vn_open_cred(&nd, &filemode, createmode, 0, td->td_ucred, NULL);
188	NDFREE(&nd, NDF_ONLY_PNBUF);
189	if (error == 0) {
190		/* We just unlock so we hold a reference. */
191		VOP_UNLOCK(nd.ni_vp, 0);
192		*vpp = nd.ni_vp;
193	}
194	return (error);
195}
196
197static __inline int
198zfs_vn_open(char *pnamep, enum uio_seg seg, int filemode, int createmode,
199    vnode_t **vpp, enum create crwhy, mode_t umask)
200{
201
202	return (vn_openat(pnamep, seg, filemode, createmode, vpp, crwhy,
203	    umask, NULL, -1));
204}
205#define	vn_open(pnamep, seg, filemode, createmode, vpp, crwhy, umask)	\
206	zfs_vn_open((pnamep), (seg), (filemode), (createmode), (vpp), (crwhy), (umask))
207
208#define	RLIM64_INFINITY	0
209static __inline int
210zfs_vn_rdwr(enum uio_rw rw, vnode_t *vp, caddr_t base, ssize_t len,
211    offset_t offset, enum uio_seg seg, int ioflag, int ulimit, cred_t *cr,
212    ssize_t *residp)
213{
214	struct thread *td = curthread;
215	int error;
216	ssize_t resid;
217
218	ASSERT(ioflag == 0);
219	ASSERT(ulimit == RLIM64_INFINITY);
220
221	if (rw == UIO_WRITE) {
222		ioflag = IO_SYNC;
223	} else {
224		ioflag = IO_DIRECT;
225	}
226	error = vn_rdwr(rw, vp, base, len, offset, seg, ioflag, cr, NOCRED,
227	    &resid, td);
228	if (residp != NULL)
229		*residp = (ssize_t)resid;
230	return (error);
231}
232#define	vn_rdwr(rw, vp, base, len, offset, seg, ioflag, ulimit, cr, residp) \
233	zfs_vn_rdwr((rw), (vp), (base), (len), (offset), (seg), (ioflag), (ulimit), (cr), (residp))
234
235static __inline int
236zfs_vop_fsync(vnode_t *vp, int flag, cred_t *cr)
237{
238	struct mount *mp;
239	int error;
240
241	ASSERT(flag == FSYNC);
242
243	if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
244		goto drop;
245	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
246	error = VOP_FSYNC(vp, MNT_WAIT, curthread);
247	VOP_UNLOCK(vp, 0);
248	vn_finished_write(mp);
249drop:
250	return (error);
251}
252#define	VOP_FSYNC(vp, flag, cr, ct)	zfs_vop_fsync((vp), (flag), (cr))
253
254static __inline int
255zfs_vop_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
256{
257	int error;
258
259	ASSERT(count == 1);
260	ASSERT(offset == 0);
261
262	error = vn_close(vp, flag, cr, curthread);
263	return (error);
264}
265#define	VOP_CLOSE(vp, oflags, count, offset, cr, ct)			\
266	zfs_vop_close((vp), (oflags), (count), (offset), (cr))
267
268static __inline int
269vn_rename(char *from, char *to, enum uio_seg seg)
270{
271
272	ASSERT(seg == UIO_SYSSPACE);
273
274	return (kern_renameat(curthread, AT_FDCWD, from, AT_FDCWD, to, seg));
275}
276
277static __inline int
278vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag)
279{
280
281	ASSERT(seg == UIO_SYSSPACE);
282	ASSERT(dirflag == RMFILE);
283
284	return (kern_unlinkat(curthread, AT_FDCWD, fnamep, seg, 0));
285}
286
287#endif	/* _KERNEL */
288
289#endif	/* _OPENSOLARIS_SYS_VNODE_H_ */
290