xref: /illumos-gate/usr/src/uts/common/sys/fem.h (revision f48068addb8865f9338d23ffbe1043e369df37a1)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_FEM_H
27 #define	_SYS_FEM_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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
42 extern "C" {
43 #endif
44 
45 #ifdef	_KERNEL
46 
47 struct 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  */
89 typedef struct fem_arg femarg_t, fsemarg_t;
90 typedef struct fem fem_t;
91 typedef struct fsem    fsem_t;
92 
93 typedef int femop_t();
94 
95 /*
96  * The following enumerations specify the conditions
97  * under which a monitor (operation/argument combination)
98  * should be installed.  These are used when calling
99  * fem_install() and fsem_install()
100  */
101 typedef enum femhow {
102 	FORCE		= 0,	/* Force the installation of this monitor */
103 	OPUNIQ		= 1,	/* Install if operation set is unique */
104 	OPARGUNIQ	= 2	/* Install if op/arg combination is unique */
105 } femhow_t;
106 
107 struct fem_node {
108 	void		*fn_available;
109 	union {
110 		fem_t		*fem;
111 		vnodeops_t	*vnode;
112 		fsem_t		*fsem;
113 		vfsops_t	*vfs;
114 		void		*anon; /* anonymous, for updates */
115 	}		fn_op;
116 	void		(*fn_av_hold)(void *);	/* Hold for "fn_available" */
117 	void		(*fn_av_rele)(void *);	/* Release for "fn_available" */
118 };
119 
120 struct fem_arg {
121 	union {
122 		vnode_t	*vp,
123 			**vpp;
124 		vfs_t	*vfsp;
125 		void	*anon;
126 	}		fa_vnode;
127 	struct fem_node	*fa_fnode;
128 };
129 
130 
131 struct fem_list {
132 	uint_t	feml_refc;	/* reference count */
133 	int	feml_tos;	/* top of stack pointer(index) */
134 	int	feml_ssize;	/* stack size */
135 	int	feml_pad;	/* alignment */
136 	struct fem_node feml_nodes[1]; /* variable bounds */
137 };
138 
139 struct fem_head {
140 	kmutex_t	femh_lock;
141 	struct fem_list *femh_list;
142 };
143 
144 /*
145  * FEM_OPS defines all the FEM operations.  It is used to define
146  * the fem structure (below) and the fs_func_p union (vfs_opreg.h).
147  */
148 #define	FEM_OPS								\
149 	int (*femop_open)(femarg_t *vf, int mode, cred_t *cr);		\
150 	int (*femop_close)(femarg_t *vf, int flag, int count,		\
151 			offset_t offset, cred_t *cr);			\
152 	int (*femop_read)(femarg_t *vf, uio_t *uiop, int ioflag,	\
153 			cred_t *cr, struct caller_context *ct);		\
154 	int (*femop_write)(femarg_t *vf, uio_t *uiop, int ioflag,	\
155 			cred_t *cr, struct caller_context *ct);		\
156 	int (*femop_ioctl)(femarg_t *vf, int cmd, intptr_t arg,		\
157 			int flag, cred_t *cr, int *rvalp);		\
158 	int (*femop_setfl)(femarg_t *vf, int oflags, int nflags,	\
159 			cred_t *cr);					\
160 	int (*femop_getattr)(femarg_t *vf, vattr_t *vap, int flags,	\
161 			cred_t *cr);					\
162 	int (*femop_setattr)(femarg_t *vf, vattr_t *vap, int flags,	\
163 			cred_t *cr, caller_context_t *ct);		\
164 	int (*femop_access)(femarg_t *vf, int mode, int flags,		\
165 			cred_t *cr);					\
166 	int (*femop_lookup)(femarg_t *vf, char *nm, vnode_t **vpp,	\
167 			pathname_t *pnp, int flags, vnode_t *rdir,	\
168 			cred_t *cr);					\
169 	int (*femop_create)(femarg_t *vf, char *name, vattr_t *vap,	\
170 			vcexcl_t excl, int mode, vnode_t **vpp,		\
171 			cred_t *cr, int flag);				\
172 	int (*femop_remove)(femarg_t *vf, char *nm, cred_t *cr);	\
173 	int (*femop_link)(femarg_t *vf, vnode_t *svp, char *tnm,	\
174 			cred_t *cr);					\
175 	int (*femop_rename)(femarg_t *vf, char *snm, vnode_t *tdvp,	\
176 			char *tnm, cred_t *cr);				\
177 	int (*femop_mkdir)(femarg_t *vf, char *dirname, vattr_t *vap,	\
178 			vnode_t **vpp, cred_t *cr);			\
179 	int (*femop_rmdir)(femarg_t *vf, char *nm, vnode_t *cdir,	\
180 			cred_t *cr);					\
181 	int (*femop_readdir)(femarg_t *vf, uio_t *uiop, cred_t *cr,	\
182 			int *eofp);					\
183 	int (*femop_symlink)(femarg_t *vf, char *linkname,		\
184 			vattr_t *vap, char *target, cred_t *cr);	\
185 	int (*femop_readlink)(femarg_t *vf, uio_t *uiop, cred_t *cr);	\
186 	int (*femop_fsync)(femarg_t *vf, int syncflag, cred_t *cr);	\
187 	void (*femop_inactive)(femarg_t *vf, cred_t *cr);		\
188 	int (*femop_fid)(femarg_t *vf, fid_t *fidp);			\
189 	int (*femop_rwlock)(femarg_t *vf, int write_lock,		\
190 			caller_context_t *ct);				\
191 	void (*femop_rwunlock)(femarg_t *vf, int write_lock,		\
192 			caller_context_t *ct);				\
193 	int (*femop_seek)(femarg_t *vf, offset_t ooff,			\
194 			offset_t *noffp);				\
195 	int (*femop_cmp)(femarg_t *vf, vnode_t *vp2);			\
196 	int (*femop_frlock)(femarg_t *vf, int cmd, struct flock64 *bfp,	\
197 			int flag, offset_t offset,			\
198 			struct flk_callback *flk_cbp, cred_t *cr);	\
199 	int (*femop_space)(femarg_t *vf, int cmd, struct flock64 *bfp,	\
200 			int flag, offset_t offset, cred_t *cr,		\
201 			caller_context_t *ct);				\
202 	int (*femop_realvp)(femarg_t *vf, vnode_t **vpp);		\
203 	int (*femop_getpage)(femarg_t *vf, offset_t off, size_t len,	\
204 			uint_t *protp, struct page **plarr,		\
205 			size_t plsz, struct seg *seg, caddr_t addr,	\
206 			enum seg_rw rw,	cred_t *cr);			\
207 	int (*femop_putpage)(femarg_t *vf, offset_t off, size_t len,	\
208 			int flags, cred_t *cr);				\
209 	int (*femop_map)(femarg_t *vf, offset_t off, struct as *as,	\
210 			caddr_t *addrp, size_t len, uchar_t prot,	\
211 			uchar_t maxprot, uint_t flags, cred_t *cr);	\
212 	int (*femop_addmap)(femarg_t *vf, offset_t off, struct as *as,	\
213 			caddr_t addr, size_t len, uchar_t prot,		\
214 			uchar_t maxprot, uint_t flags, cred_t *cr);	\
215 	int (*femop_delmap)(femarg_t *vf, offset_t off, struct as *as,	\
216 			caddr_t addr, size_t len, uint_t prot,		\
217 			uint_t maxprot, uint_t flags, cred_t *cr);	\
218 	int (*femop_poll)(femarg_t *vf, short events, int anyyet,	\
219 			short *reventsp, struct pollhead **phpp);	\
220 	int (*femop_dump)(femarg_t *vf, caddr_t addr, int lbdn,		\
221 			int dblks); 					\
222 	int (*femop_pathconf)(femarg_t *vf, int cmd, ulong_t *valp,	\
223 			cred_t *cr);					\
224 	int (*femop_pageio)(femarg_t *vf, struct page *pp,		\
225 			u_offset_t io_off, size_t io_len, int flags,	\
226 			cred_t *cr);					\
227 	int (*femop_dumpctl)(femarg_t *vf, int action, int *blkp);	\
228 	void (*femop_dispose)(femarg_t *vf, struct page *pp, int flag,	\
229 			int dn, cred_t *cr);				\
230 	int (*femop_setsecattr)(femarg_t *vf, vsecattr_t *vsap,		\
231 			int flag, cred_t *cr);				\
232 	int (*femop_getsecattr)(femarg_t *vf, vsecattr_t *vsap,		\
233 			int flag, cred_t *cr);				\
234 	int (*femop_shrlock)(femarg_t *vf, int cmd,			\
235 			struct shrlock *shr, int flag, cred_t *cr);	\
236 	int (*femop_vnevent)(femarg_t *vf, vnevent_t vnevent,		\
237 			vnode_t *dvp, char *cname)
238 	/* NB: No ";" */
239 
240 struct fem {
241 	const char *name;
242 	const struct fs_operation_def *templ;
243 	FEM_OPS;	/* Signatures of all FEM operations (femops) */
244 };
245 
246 /*
247  * FSEM_OPS defines all the FSEM operations.  It is used to define
248  * the fsem structure (below) and the fs_func_p union (vfs_opreg.h).
249  */
250 #define	FSEM_OPS							\
251 	int (*fsemop_mount)(fsemarg_t *vf, vnode_t *mvp,		\
252 			struct mounta *uap, cred_t *cr);		\
253 	int (*fsemop_unmount)(fsemarg_t *vf, int flag, cred_t *cr);	\
254 	int (*fsemop_root)(fsemarg_t *vf, vnode_t **vpp);		\
255 	int (*fsemop_statvfs)(fsemarg_t *vf, statvfs64_t *sp);		\
256 	int (*fsemop_sync)(fsemarg_t *vf, short flag, cred_t *cr);	\
257 	int (*fsemop_vget)(fsemarg_t *vf, vnode_t **vpp, fid_t *fidp);	\
258 	int (*fsemop_mountroot)(fsemarg_t *vf,				\
259 			enum whymountroot reason);			\
260 	void (*fsemop_freevfs)(fsemarg_t *vf);				\
261 	int (*fsemop_vnstate)(fsemarg_t *vf, vnode_t *vp,		\
262 			vntrans_t nstate)		/* NB: No ";" */
263 
264 struct fsem {
265 	const char *name;
266 	const struct fs_operation_def *templ;
267 	FSEM_OPS;	/* Signatures of all FSEM operations (fsemops) */
268 };
269 
270 extern int vnext_open(femarg_t *vf, int mode, cred_t *cr);
271 extern int vnext_close(femarg_t *vf, int flag, int count, offset_t offset,
272 		cred_t *cr);
273 extern int vnext_read(femarg_t *vf, uio_t *uiop, int ioflag, cred_t *cr,
274 		struct caller_context *ct);
275 extern int vnext_write(femarg_t *vf, uio_t *uiop, int ioflag, cred_t *cr,
276 		struct caller_context *ct);
277 extern int vnext_ioctl(femarg_t *vf, int cmd, intptr_t arg, int flag,
278 		cred_t *cr, int *rvalp);
279 extern int vnext_setfl(femarg_t *vf, int oflags, int nflags, cred_t *cr);
280 extern int vnext_getattr(femarg_t *vf, vattr_t *vap, int flags, cred_t *cr);
281 extern int vnext_setattr(femarg_t *vf, vattr_t *vap, int flags, cred_t *cr,
282 		caller_context_t *ct);
283 extern int vnext_access(femarg_t *vf, int mode, int flags, cred_t *cr);
284 extern int vnext_lookup(femarg_t *vf, char *nm, vnode_t **vpp,
285 			pathname_t *pnp, int flags, vnode_t *rdir,
286 			cred_t *cr);
287 extern int vnext_create(femarg_t *vf, char *name, vattr_t *vap,
288 			vcexcl_t excl, int mode, vnode_t **vpp, cred_t *cr,
289 			int flag);
290 extern int vnext_remove(femarg_t *vf, char *nm, cred_t *cr);
291 extern int vnext_link(femarg_t *vf, vnode_t *svp, char *tnm, cred_t *cr);
292 extern int vnext_rename(femarg_t *vf, char *snm, vnode_t *tdvp, char *tnm,
293 			cred_t *cr);
294 extern int vnext_mkdir(femarg_t *vf, char *dirname, vattr_t *vap,
295 		vnode_t **vpp, cred_t *cr);
296 extern int vnext_rmdir(femarg_t *vf, char *nm, vnode_t *cdir, cred_t *cr);
297 extern int vnext_readdir(femarg_t *vf, uio_t *uiop, cred_t *cr, int *eofp);
298 extern int vnext_symlink(femarg_t *vf, char *linkname, vattr_t *vap,
299 			char *target, cred_t *cr);
300 extern int vnext_readlink(femarg_t *vf, uio_t *uiop, cred_t *cr);
301 extern int vnext_fsync(femarg_t *vf, int syncflag, cred_t *cr);
302 extern void vnext_inactive(femarg_t *vf, cred_t *cr);
303 extern int vnext_fid(femarg_t *vf, fid_t *fidp);
304 extern int vnext_rwlock(femarg_t *vf, int write_lock, caller_context_t *ct);
305 extern void vnext_rwunlock(femarg_t *vf, int write_lock, caller_context_t *ct);
306 extern int vnext_seek(femarg_t *vf, offset_t ooff, offset_t *noffp);
307 extern int vnext_cmp(femarg_t *vf, vnode_t *vp2);
308 extern int vnext_frlock(femarg_t *vf, int cmd, struct flock64 *bfp,
309 			int flag, offset_t offset,
310 			struct flk_callback *flk_cbp, cred_t *cr);
311 extern int vnext_space(femarg_t *vf, int cmd, struct flock64 *bfp,
312 			int flag, offset_t offset, cred_t *cr,
313 			caller_context_t *ct);
314 extern int vnext_realvp(femarg_t *vf, vnode_t **vpp);
315 extern int vnext_getpage(femarg_t *vf, offset_t off, size_t len,
316 			uint_t *protp, struct page **plarr, size_t plsz,
317 			struct seg *seg, caddr_t addr, enum seg_rw rw,
318 			cred_t *cr);
319 extern int vnext_putpage(femarg_t *vf, offset_t off, size_t len, int flags,
320 			cred_t *cr);
321 extern int vnext_map(femarg_t *vf, offset_t off, struct as *as,
322 		caddr_t *addrp, size_t len, uchar_t prot, uchar_t maxprot,
323 		uint_t flags, cred_t *cr);
324 extern int vnext_addmap(femarg_t *vf, offset_t off, struct as *as,
325 			caddr_t addr, size_t len, uchar_t prot,
326 			uchar_t maxprot, uint_t flags, cred_t *cr);
327 extern int vnext_delmap(femarg_t *vf, offset_t off, struct as *as,
328 			caddr_t addr, size_t len, uint_t prot,
329 			uint_t maxprot, uint_t flags, cred_t *cr);
330 extern int vnext_poll(femarg_t *vf, short events, int anyyet,
331 		short *reventsp, struct pollhead **phpp);
332 extern int vnext_dump(femarg_t *vf, caddr_t addr, int lbdn, int dblks);
333 extern int vnext_pathconf(femarg_t *vf, int cmd, ulong_t *valp, cred_t *cr);
334 extern int vnext_pageio(femarg_t *vf, struct page *pp, u_offset_t io_off,
335 			size_t io_len, int flags, cred_t *cr);
336 extern int vnext_dumpctl(femarg_t *vf, int action, int *blkp);
337 extern void vnext_dispose(femarg_t *vf, struct page *pp, int flag, int dn,
338 			cred_t *cr);
339 extern int vnext_setsecattr(femarg_t *vf, vsecattr_t *vsap, int flag,
340 			cred_t *cr);
341 extern int vnext_getsecattr(femarg_t *vf, vsecattr_t *vsap, int flag,
342 			cred_t *cr);
343 extern int vnext_shrlock(femarg_t *vf, int cmd, struct shrlock *shr,
344 			int flag, cred_t *cr);
345 extern int vnext_vnevent(femarg_t *vf, vnevent_t vevent, vnode_t *dvp,
346 			char *cname);
347 
348 extern int vfsnext_mount(fsemarg_t *vf, vnode_t *mvp, struct mounta *uap,
349 			cred_t *cr);
350 extern int vfsnext_unmount(fsemarg_t *vf, int flag, cred_t *cr);
351 extern int vfsnext_root(fsemarg_t *vf, vnode_t **vpp);
352 extern int vfsnext_statvfs(fsemarg_t *vf, statvfs64_t *sp);
353 extern int vfsnext_sync(fsemarg_t *vf, short flag, cred_t *cr);
354 extern int vfsnext_vget(fsemarg_t *vf, vnode_t **vpp, fid_t *fidp);
355 extern int vfsnext_mountroot(fsemarg_t *vf, enum whymountroot reason);
356 extern void vfsnext_freevfs(fsemarg_t *vf);
357 extern int vfsnext_vnstate(fsemarg_t *vf, vnode_t *vp, vntrans_t nstate);
358 
359 
360 extern void fem_init(void); /* called once, by startup */
361 
362 /* fem api */
363 extern int fem_create(char *name, const struct fs_operation_def *templ,
364 			fem_t **actual);
365 extern void fem_free(fem_t *fem);
366 extern int fem_install(struct vnode *v, fem_t *mon, void *arg, femhow_t how,
367 		void (*arg_hold)(void *), void (*arg_rele)(void *));
368 extern int fem_is_installed(struct vnode *v, fem_t *mon, void *arg);
369 extern int fem_uninstall(struct vnode *v, fem_t *mon, void *arg);
370 extern vnodeops_t *fem_getvnops(struct vnode *v);
371 extern void fem_setvnops(struct vnode *v, struct vnodeops *nops);
372 
373 
374 extern int fsem_create(char *name, const struct fs_operation_def *templ,
375 			fsem_t **actual);
376 extern void fsem_free(fsem_t *fsem);
377 extern int fsem_is_installed(struct vfs *v, fsem_t *mon, void *arg);
378 extern int fsem_install(struct vfs *v, fsem_t *mon, void *arg, femhow_t how,
379 		void (*arg_hold)(void *), void (*arg_rele)(void *));
380 extern int fsem_uninstall(struct vfs *v, fsem_t *mon, void *arg);
381 extern vfsops_t *fsem_getvfsops(struct vfs *v);
382 extern void fsem_setvfsops(struct vfs *v, struct vfsops *nops);
383 
384 #endif /* _KERNEL */
385 
386 #ifdef	__cplusplus
387 }
388 #endif
389 
390 #endif	/* _SYS_FEM_H */
391