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 2010 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 *
25 * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
26 */
27
28#ifndef _SYS_FEM_H
29#define	_SYS_FEM_H
30
31#include <sys/types.h>
32#include <sys/mutex.h>
33#include <sys/pathname.h>
34#include <sys/uio.h>
35#include <sys/file.h>
36#include <sys/cred.h>
37#include <sys/vfs.h>
38#include <sys/vnode.h>
39
40
41#ifdef	__cplusplus
42extern "C" {
43#endif
44
45#if defined(_KERNEL) || defined(_FAKE_KERNEL)
46
47struct fs_operation_def;	/* from vfs.h */
48
49/*
50 * overview:
51 *
52 * fem - file event monitoring
53 *
54 * File Event Monitoring is a formalized mechanism to monitor events on a
55 * vnode or vfs by intercepting the vnode/vfs operations.  The framework enables
56 * the consumer to request event notifications for specified files and
57 * directories.  The consumers, which intercept the events, are responsible for
58 * delivering the event to the next interceptor or the terminal destination.
59 *
60 */
61
62/*
63 * protocol:
64 *
65 *  vnode ->  fem_head.
66 *	There can only be one fem_head for a vnode.
67 *	Once attached, the fem_head persists until explicitly detached
68 *	or the vnode expires.
69 *
70 * fem_head -> fem_list.
71 *	There can be many lists for a head, as each reconfiguration of
72 *	the list causes a new one to be created and initialized from the
73 *	old one.  For this reason, modules cannot assume that they can
74 *	reach thier list by vnode->fem_head->fem_list->list[n] == mod;
75 *
76 * fem_arg -> vnode, &vnode.
77 *	This relationship is established at the head of the call (ie. in
78 *	femhead_open()) where the fem_arg is allocated.  Intermediate nodes
79 *	have direct access to this.
80 *
81 * fem_arg -> fem_node
82 *	This relationship is established at the head of the call (ie. in
83 *	femhead_open()) where the fem_arg is allocated.  The fem_arg is
84 *	updated as intermediate nodes are invoked, however not as they
85 *	return.  For this reason, nodes which are interested in maintaining
86 *	context following a "next" should store a copy of the fem_available
87 *	field before invoking the 'next'.
88 */
89typedef struct fem_arg femarg_t, fsemarg_t;
90typedef struct fem fem_t;
91typedef struct fsem    fsem_t;
92
93typedef int femop_t();
94
95typedef void (*fem_func_t)(void *);
96
97/*
98 * The following enumerations specify the conditions
99 * under which a monitor (operation/argument combination)
100 * should be installed.  These are used when calling
101 * fem_install() and fsem_install()
102 */
103typedef enum femhow {
104	FORCE		= 0,	/* Force the installation of this monitor */
105	OPUNIQ		= 1,	/* Install if operation set is unique */
106	OPARGUNIQ	= 2	/* Install if op/arg combination is unique */
107} femhow_t;
108
109struct fem_node {
110	void		*fn_available;
111	union {
112		fem_t		*fem;
113		vnodeops_t	*vnode;
114		fsem_t		*fsem;
115		vfsops_t	*vfs;
116		void		*anon; /* anonymous, for updates */
117	}		fn_op;
118	void		(*fn_av_hold)(void *);	/* Hold for "fn_available" */
119	void		(*fn_av_rele)(void *);	/* Release for "fn_available" */
120};
121
122struct fem_arg {
123	union {
124		vnode_t	*vp,
125			**vpp;
126		vfs_t	*vfsp;
127		void	*anon;
128	}		fa_vnode;
129	struct fem_node	*fa_fnode;
130};
131
132
133struct fem_list {
134	uint_t	feml_refc;	/* reference count */
135	int	feml_tos;	/* top of stack pointer(index) */
136	int	feml_ssize;	/* stack size */
137	int	feml_pad;	/* alignment */
138	struct fem_node feml_nodes[1]; /* variable bounds */
139};
140
141struct fem_head {
142	kmutex_t	femh_lock;
143	struct fem_list *femh_list;
144};
145
146/*
147 * FEM_OPS defines all the FEM operations.  It is used to define
148 * the fem structure (below) and the fs_func_p union (vfs_opreg.h).
149 */
150#define	FEM_OPS								\
151	int (*femop_open)(femarg_t *vf, int mode, cred_t *cr,		\
152			caller_context_t *ct);				\
153	int (*femop_close)(femarg_t *vf, int flag, int count,		\
154			offset_t offset, cred_t *cr,			\
155			caller_context_t *ct);				\
156	int (*femop_read)(femarg_t *vf, uio_t *uiop, int ioflag,	\
157			cred_t *cr, caller_context_t *ct);		\
158	int (*femop_write)(femarg_t *vf, uio_t *uiop, int ioflag,	\
159			cred_t *cr, caller_context_t *ct);		\
160	int (*femop_ioctl)(femarg_t *vf, int cmd, intptr_t arg,		\
161			int flag, cred_t *cr, int *rvalp,		\
162			caller_context_t *ct);				\
163	int (*femop_setfl)(femarg_t *vf, int oflags, int nflags,	\
164			cred_t *cr, caller_context_t *ct);		\
165	int (*femop_getattr)(femarg_t *vf, vattr_t *vap, int flags,	\
166			cred_t *cr, caller_context_t *ct);		\
167	int (*femop_setattr)(femarg_t *vf, vattr_t *vap, int flags,	\
168			cred_t *cr, caller_context_t *ct);		\
169	int (*femop_access)(femarg_t *vf, int mode, int flags,		\
170			cred_t *cr, caller_context_t *ct);		\
171	int (*femop_lookup)(femarg_t *vf, char *nm, vnode_t **vpp,	\
172			pathname_t *pnp, int flags, vnode_t *rdir,	\
173			cred_t *cr, caller_context_t *ct,		\
174			int *direntflags, pathname_t *realpnp);		\
175	int (*femop_create)(femarg_t *vf, char *name, vattr_t *vap,	\
176			vcexcl_t excl, int mode, vnode_t **vpp,		\
177			cred_t *cr, int flag, caller_context_t *ct,	\
178			vsecattr_t *vsecp);				\
179	int (*femop_remove)(femarg_t *vf, char *nm, cred_t *cr,		\
180			caller_context_t *ct, int flags);		\
181	int (*femop_link)(femarg_t *vf, vnode_t *svp, char *tnm,	\
182			cred_t *cr, caller_context_t *ct, int flags);	\
183	int (*femop_rename)(femarg_t *vf, char *snm, vnode_t *tdvp,	\
184			char *tnm, cred_t *cr, caller_context_t *ct,	\
185			int flags);					\
186	int (*femop_mkdir)(femarg_t *vf, char *dirname, vattr_t *vap,	\
187			vnode_t **vpp, cred_t *cr,			\
188			caller_context_t *ct, int flags,		\
189			vsecattr_t *vsecp);				\
190	int (*femop_rmdir)(femarg_t *vf, char *nm, vnode_t *cdir,	\
191			cred_t *cr, caller_context_t *ct, int flags);	\
192	int (*femop_readdir)(femarg_t *vf, uio_t *uiop, cred_t *cr,	\
193			int *eofp, caller_context_t *ct, int flags);	\
194	int (*femop_symlink)(femarg_t *vf, char *linkname,		\
195			vattr_t *vap, char *target, cred_t *cr,		\
196			caller_context_t *ct, int flags);		\
197	int (*femop_readlink)(femarg_t *vf, uio_t *uiop, cred_t *cr,	\
198			caller_context_t *ct);				\
199	int (*femop_fsync)(femarg_t *vf, int syncflag, cred_t *cr,	\
200			caller_context_t *ct);				\
201	void (*femop_inactive)(femarg_t *vf, cred_t *cr,		\
202			caller_context_t *ct);				\
203	int (*femop_fid)(femarg_t *vf, fid_t *fidp,			\
204			caller_context_t *ct);				\
205	int (*femop_rwlock)(femarg_t *vf, int write_lock,		\
206			caller_context_t *ct);				\
207	void (*femop_rwunlock)(femarg_t *vf, int write_lock,		\
208			caller_context_t *ct);				\
209	int (*femop_seek)(femarg_t *vf, offset_t ooff,			\
210			offset_t *noffp, caller_context_t *ct);		\
211	int (*femop_cmp)(femarg_t *vf, vnode_t *vp2,			\
212			caller_context_t *ct);				\
213	int (*femop_frlock)(femarg_t *vf, int cmd, struct flock64 *bfp,	\
214			int flag, offset_t offset,			\
215			struct flk_callback *flk_cbp, cred_t *cr,	\
216			caller_context_t *ct);				\
217	int (*femop_space)(femarg_t *vf, int cmd, struct flock64 *bfp,	\
218			int flag, offset_t offset, cred_t *cr,		\
219			caller_context_t *ct);				\
220	int (*femop_realvp)(femarg_t *vf, vnode_t **vpp,		\
221			caller_context_t *ct);				\
222	int (*femop_getpage)(femarg_t *vf, offset_t off, size_t len,	\
223			uint_t *protp, struct page **plarr,		\
224			size_t plsz, struct seg *seg, caddr_t addr,	\
225			enum seg_rw rw,	cred_t *cr,			\
226			caller_context_t *ct);				\
227	int (*femop_putpage)(femarg_t *vf, offset_t off, size_t len,	\
228			int flags, cred_t *cr, caller_context_t *ct);	\
229	int (*femop_map)(femarg_t *vf, offset_t off, struct as *as,	\
230			caddr_t *addrp, size_t len, uchar_t prot,	\
231			uchar_t maxprot, uint_t flags, cred_t *cr,	\
232			caller_context_t *ct);				\
233	int (*femop_addmap)(femarg_t *vf, offset_t off, struct as *as,	\
234			caddr_t addr, size_t len, uchar_t prot,		\
235			uchar_t maxprot, uint_t flags, cred_t *cr,	\
236			caller_context_t *ct);				\
237	int (*femop_delmap)(femarg_t *vf, offset_t off, struct as *as,	\
238			caddr_t addr, size_t len, uint_t prot,		\
239			uint_t maxprot, uint_t flags, cred_t *cr,	\
240			caller_context_t *ct);				\
241	int (*femop_poll)(femarg_t *vf, short events, int anyyet,	\
242			short *reventsp, struct pollhead **phpp,	\
243			caller_context_t *ct);				\
244	int (*femop_dump)(femarg_t *vf, caddr_t addr, offset_t lbdn,	\
245			offset_t dblks, caller_context_t *ct);		\
246	int (*femop_pathconf)(femarg_t *vf, int cmd, ulong_t *valp,	\
247			cred_t *cr, caller_context_t *ct);		\
248	int (*femop_pageio)(femarg_t *vf, struct page *pp,		\
249			u_offset_t io_off, size_t io_len, int flags,	\
250			cred_t *cr, caller_context_t *ct);		\
251	int (*femop_dumpctl)(femarg_t *vf, int action, offset_t *blkp,	\
252			caller_context_t *ct);				\
253	void (*femop_dispose)(femarg_t *vf, struct page *pp, int flag,	\
254			int dn, cred_t *cr, caller_context_t *ct);	\
255	int (*femop_setsecattr)(femarg_t *vf, vsecattr_t *vsap,		\
256			int flag, cred_t *cr, caller_context_t *ct);	\
257	int (*femop_getsecattr)(femarg_t *vf, vsecattr_t *vsap,		\
258			int flag, cred_t *cr, caller_context_t *ct);	\
259	int (*femop_shrlock)(femarg_t *vf, int cmd,			\
260			struct shrlock *shr, int flag, cred_t *cr,	\
261			caller_context_t *ct);				\
262	int (*femop_vnevent)(femarg_t *vf, vnevent_t vnevent,		\
263			vnode_t *dvp, char *cname, 			\
264			caller_context_t *ct);				\
265	int (*femop_reqzcbuf)(femarg_t *vf, enum uio_rw ioflag,		\
266			xuio_t *xuio, cred_t *cr,			\
267			caller_context_t *ct);				\
268	int (*femop_retzcbuf)(femarg_t *vf, xuio_t *xuio, cred_t *cr,	\
269			caller_context_t *ct)
270	/* NB: No ";" */
271
272struct fem {
273	const char *name;
274	const struct fs_operation_def *templ;
275	FEM_OPS;	/* Signatures of all FEM operations (femops) */
276};
277
278/*
279 * FSEM_OPS defines all the FSEM operations.  It is used to define
280 * the fsem structure (below) and the fs_func_p union (vfs_opreg.h).
281 */
282#define	FSEM_OPS							\
283	int (*fsemop_mount)(fsemarg_t *vf, vnode_t *mvp,		\
284			struct mounta *uap, cred_t *cr);		\
285	int (*fsemop_unmount)(fsemarg_t *vf, int flag, cred_t *cr);	\
286	int (*fsemop_root)(fsemarg_t *vf, vnode_t **vpp);		\
287	int (*fsemop_statvfs)(fsemarg_t *vf, statvfs64_t *sp);		\
288	int (*fsemop_sync)(fsemarg_t *vf, short flag, cred_t *cr);	\
289	int (*fsemop_vget)(fsemarg_t *vf, vnode_t **vpp, fid_t *fidp);	\
290	int (*fsemop_mountroot)(fsemarg_t *vf,				\
291			enum whymountroot reason);			\
292	void (*fsemop_freevfs)(fsemarg_t *vf);				\
293	int (*fsemop_vnstate)(fsemarg_t *vf, vnode_t *vp,		\
294			vntrans_t nstate)		/* NB: No ";" */
295
296struct fsem {
297	const char *name;
298	const struct fs_operation_def *templ;
299	FSEM_OPS;	/* Signatures of all FSEM operations (fsemops) */
300};
301
302extern int vnext_open(femarg_t *vf, int mode, cred_t *cr,
303		caller_context_t *ct);
304extern int vnext_close(femarg_t *vf, int flag, int count, offset_t offset,
305		cred_t *cr, caller_context_t *ct);
306extern int vnext_read(femarg_t *vf, uio_t *uiop, int ioflag, cred_t *cr,
307		caller_context_t *ct);
308extern int vnext_write(femarg_t *vf, uio_t *uiop, int ioflag, cred_t *cr,
309		caller_context_t *ct);
310extern int vnext_ioctl(femarg_t *vf, int cmd, intptr_t arg, int flag,
311		cred_t *cr, int *rvalp, caller_context_t *ct);
312extern int vnext_setfl(femarg_t *vf, int oflags, int nflags, cred_t *cr,
313		caller_context_t *ct);
314extern int vnext_getattr(femarg_t *vf, vattr_t *vap, int flags, cred_t *cr,
315		caller_context_t *ct);
316extern int vnext_setattr(femarg_t *vf, vattr_t *vap, int flags, cred_t *cr,
317		caller_context_t *ct);
318extern int vnext_access(femarg_t *vf, int mode, int flags, cred_t *cr,
319		caller_context_t *ct);
320extern int vnext_lookup(femarg_t *vf, char *nm, vnode_t **vpp,
321			pathname_t *pnp, int flags, vnode_t *rdir,
322			cred_t *cr, caller_context_t *ct,
323			int *direntflags, pathname_t *realpnp);
324extern int vnext_create(femarg_t *vf, char *name, vattr_t *vap,
325			vcexcl_t excl, int mode, vnode_t **vpp, cred_t *cr,
326			int flag, caller_context_t *ct, vsecattr_t *vsecp);
327extern int vnext_remove(femarg_t *vf, char *nm, cred_t *cr,
328			caller_context_t *ct, int flags);
329extern int vnext_link(femarg_t *vf, vnode_t *svp, char *tnm, cred_t *cr,
330			caller_context_t *ct, int flags);
331extern int vnext_rename(femarg_t *vf, char *snm, vnode_t *tdvp, char *tnm,
332			cred_t *cr, caller_context_t *ct, int flags);
333extern int vnext_mkdir(femarg_t *vf, char *dirname, vattr_t *vap,
334			vnode_t **vpp, cred_t *cr, caller_context_t *ct,
335			int flags, vsecattr_t *vsecp);
336extern int vnext_rmdir(femarg_t *vf, char *nm, vnode_t *cdir, cred_t *cr,
337			caller_context_t *ct, int flags);
338extern int vnext_readdir(femarg_t *vf, uio_t *uiop, cred_t *cr, int *eofp,
339			caller_context_t *ct, int flags);
340extern int vnext_symlink(femarg_t *vf, char *linkname, vattr_t *vap,
341			char *target, cred_t *cr, caller_context_t *ct,
342			int flags);
343extern int vnext_readlink(femarg_t *vf, uio_t *uiop, cred_t *cr,
344			caller_context_t *ct);
345extern int vnext_fsync(femarg_t *vf, int syncflag, cred_t *cr,
346			caller_context_t *ct);
347extern void vnext_inactive(femarg_t *vf, cred_t *cr, caller_context_t *ct);
348extern int vnext_fid(femarg_t *vf, fid_t *fidp, caller_context_t *ct);
349extern int vnext_rwlock(femarg_t *vf, int write_lock, caller_context_t *ct);
350extern void vnext_rwunlock(femarg_t *vf, int write_lock, caller_context_t *ct);
351extern int vnext_seek(femarg_t *vf, offset_t ooff, offset_t *noffp,
352			caller_context_t *ct);
353extern int vnext_cmp(femarg_t *vf, vnode_t *vp2, caller_context_t *ct);
354extern int vnext_frlock(femarg_t *vf, int cmd, struct flock64 *bfp,
355			int flag, offset_t offset,
356			struct flk_callback *flk_cbp, cred_t *cr,
357			caller_context_t *ct);
358extern int vnext_space(femarg_t *vf, int cmd, struct flock64 *bfp,
359			int flag, offset_t offset, cred_t *cr,
360			caller_context_t *ct);
361extern int vnext_realvp(femarg_t *vf, vnode_t **vpp, caller_context_t *ct);
362extern int vnext_getpage(femarg_t *vf, offset_t off, size_t len,
363			uint_t *protp, struct page **plarr, size_t plsz,
364			struct seg *seg, caddr_t addr, enum seg_rw rw,
365			cred_t *cr, caller_context_t *ct);
366extern int vnext_putpage(femarg_t *vf, offset_t off, size_t len, int flags,
367			cred_t *cr, caller_context_t *ct);
368extern int vnext_map(femarg_t *vf, offset_t off, struct as *as,
369		caddr_t *addrp, size_t len, uchar_t prot, uchar_t maxprot,
370		uint_t flags, cred_t *cr, caller_context_t *ct);
371extern int vnext_addmap(femarg_t *vf, offset_t off, struct as *as,
372			caddr_t addr, size_t len, uchar_t prot,
373			uchar_t maxprot, uint_t flags, cred_t *cr,
374			caller_context_t *ct);
375extern int vnext_delmap(femarg_t *vf, offset_t off, struct as *as,
376			caddr_t addr, size_t len, uint_t prot,
377			uint_t maxprot, uint_t flags, cred_t *cr,
378			caller_context_t *ct);
379extern int vnext_poll(femarg_t *vf, short events, int anyyet,
380			short *reventsp, struct pollhead **phpp,
381			caller_context_t *ct);
382extern int vnext_dump(femarg_t *vf, caddr_t addr, offset_t lbdn,
383    offset_t dblks, caller_context_t *ct);
384extern int vnext_pathconf(femarg_t *vf, int cmd, ulong_t *valp, cred_t *cr,
385			caller_context_t *ct);
386extern int vnext_pageio(femarg_t *vf, struct page *pp, u_offset_t io_off,
387			size_t io_len, int flags, cred_t *cr,
388			caller_context_t *ct);
389extern int vnext_dumpctl(femarg_t *vf, int action, offset_t *blkp,
390			caller_context_t *ct);
391extern void vnext_dispose(femarg_t *vf, struct page *pp, int flag, int dn,
392			cred_t *cr, caller_context_t *ct);
393extern int vnext_setsecattr(femarg_t *vf, vsecattr_t *vsap, int flag,
394			cred_t *cr, caller_context_t *ct);
395extern int vnext_getsecattr(femarg_t *vf, vsecattr_t *vsap, int flag,
396			cred_t *cr, caller_context_t *ct);
397extern int vnext_shrlock(femarg_t *vf, int cmd, struct shrlock *shr,
398			int flag, cred_t *cr, caller_context_t *ct);
399extern int vnext_vnevent(femarg_t *vf, vnevent_t vevent, vnode_t *dvp,
400			char *cname, caller_context_t *ct);
401extern int vnext_reqzcbuf(femarg_t *vf, enum uio_rw ioflag, xuio_t *xuiop,
402			cred_t *cr, caller_context_t *ct);
403extern int vnext_retzcbuf(femarg_t *vf, xuio_t *xuiop, cred_t *cr,
404			caller_context_t *ct);
405
406extern int vfsnext_mount(fsemarg_t *vf, vnode_t *mvp, struct mounta *uap,
407			cred_t *cr);
408extern int vfsnext_unmount(fsemarg_t *vf, int flag, cred_t *cr);
409extern int vfsnext_root(fsemarg_t *vf, vnode_t **vpp);
410extern int vfsnext_statvfs(fsemarg_t *vf, statvfs64_t *sp);
411extern int vfsnext_sync(fsemarg_t *vf, short flag, cred_t *cr);
412extern int vfsnext_vget(fsemarg_t *vf, vnode_t **vpp, fid_t *fidp);
413extern int vfsnext_mountroot(fsemarg_t *vf, enum whymountroot reason);
414extern void vfsnext_freevfs(fsemarg_t *vf);
415extern int vfsnext_vnstate(fsemarg_t *vf, vnode_t *vp, vntrans_t nstate);
416
417
418extern void fem_init(void); /* called once, by startup */
419
420/* fem api */
421extern int fem_create(char *name, const struct fs_operation_def *templ,
422			fem_t **actual);
423extern void fem_free(fem_t *fem);
424extern int fem_install(struct vnode *v, fem_t *mon, void *arg, femhow_t how,
425		void (*arg_hold)(void *), void (*arg_rele)(void *));
426extern int fem_is_installed(struct vnode *v, fem_t *mon, void *arg);
427extern int fem_uninstall(struct vnode *v, fem_t *mon, void *arg);
428extern vnodeops_t *fem_getvnops(struct vnode *v);
429extern void fem_setvnops(struct vnode *v, struct vnodeops *nops);
430
431
432extern int fsem_create(char *name, const struct fs_operation_def *templ,
433			fsem_t **actual);
434extern void fsem_free(fsem_t *fsem);
435extern int fsem_is_installed(struct vfs *v, fsem_t *mon, void *arg);
436extern int fsem_install(struct vfs *v, fsem_t *mon, void *arg, femhow_t how,
437		void (*arg_hold)(void *), void (*arg_rele)(void *));
438extern int fsem_uninstall(struct vfs *v, fsem_t *mon, void *arg);
439extern vfsops_t *fsem_getvfsops(struct vfs *v);
440extern void fsem_setvfsops(struct vfs *v, struct vfsops *nops);
441
442#endif /* _KERNEL */
443
444#ifdef	__cplusplus
445}
446#endif
447
448#endif	/* _SYS_FEM_H */
449