xref: /onnv-gate/usr/src/uts/common/fs/portfs/port_fop.c (revision 4813:8eaf4b355227)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * File Events Notification
30  * ------------------------
31  *
32  * The File Events Notification facility provides file and directory change
33  * notification. It is implemented as an event source(PORT_SOURCE_FILE)
34  * under the Event Ports framework. Therefore the API is an extension to
35  * the Event Ports API.
36  *
37  * It uses the FEM (File Events Monitoring) framework to intercept
38  * operations on the files & directories and generate appropriate events.
39  *
40  * It provides event notification in accordance with what an application
41  * can find out by stat`ing the file and comparing time stamps. The various
42  * system calls that update the file's access, modification, and change
43  * time stamps are documented in the man page section 2.
44  *
45  * It is non intrusive. That is, having an active file event watch on a file
46  * or directory will not prevent it from being removed or renamed or block an
47  * unmount operation of the file system where the watched file or directory
48  * resides.
49  *
50  *
51  * Interface:
52  * ----------
53  *
54  *   The object for this event source is of type 'struct file_obj *'
55  *
56  *   The file that needs to be monitored is specified in 'fo_name'.
57  *   The time stamps collected by a stat(2) call are passed in fo_atime,
58  *   fo_mtime, fo_ctime. At the time a file events watch is registered, the
59  *   time stamps passed in are compared with the current time stamps of the
60  *   file. If it has changed, relavant events are sent immediately. If the time
61  *   stamps are all '0', they will not be compared.
62  *
63  *
64  * The events are delivered to an event port. A port is created using
65  * port_create().
66  *
67  * To register a file events watch on a file or directory.
68  *
69  *   port_associate(int port, PORT_SOURCE_FILE, (uintptr_t)&fobj, events, user)
70  *
71  *   'user' is the user pointer to be returned with the event.
72  *
73  * To de-register a file events watch,
74  *
75  *   port_dissociate(int port, PORT_SOURCE_FILE, (uintptr_t)&fobj)
76  *
77  * The events are collected using the port_get()/port_getn() interface. The
78  * event source will be PORT_SOURCE_FILE.
79  *
80  * After an event is delivered, the file events watch gets de-activated. To
81  * receive the next event, the process will have to re-register the watch and
82  * activate it by calling port_associate() again. This behavior is intentional
83  * and support proper multi threaded programming when using file events
84  * notification API.
85  *
86  *
87  * Implementation overview:
88  * ------------------------
89  *
90  * Each file events watch is represented by 'portfop_t' in the kernel. A
91  * cache(portfop_cache_t) of these file portfop_t's are maintained per event
92  * port by this source. The object here is the pointer to the file_obj
93  * structure. The portfop_t's are hashed in using the object pointer. Therefore
94  * it is possible to have multiple file event watches on a file by the same
95  * process by using different object structure(file_obj_t) and hence can
96  * receive multiple event notification for a file. These watches can be for
97  * different event types.
98  *
99  * The cached entries of these file objects are retained, even after delivering
100  * an event makring them inactive, for performance reason. The assumption
101  * is that the process would come back and re-register the file to receive
102  * further events. When there are more then 'port_fop_maxpfps' watches per file
103  * it will attempt to free the oldest inactive watch.
104  *
105  * In case the event that is being delivered is an exception event, the cached
106  * entries get removed. An exception event on a file or directory means its
107  * identity got changed(rename to/from, delete, mounted over, file system
108  * unmount).
109  *
110  * If the event port gets closed, all the associated file event watches will be
111  * removed and discarded.
112  *
113  *
114  * Data structures:
115  * ----------------
116  *
117  * The list of file event watches per file are managed by the data structure
118  * portfop_vp_t. The first time a file events watch is registered for a file,
119  * the portfop_vp_t is installed on the vnode_t's member v_fopdata. This gets
120  * removed and freed only when the vnode becomes inactive. The FEM hooks are
121  * also installed when the first watch is registered on a file. The FEM hooks
122  * get un-installed when all the watches are removed.
123  *
124  * Each file events watch is represented by the structure portfop_t. They
125  * get added to a list of portfop_t's on the vnode(portfop_vp_t). After
126  * delivering an event, the portfop_t is marked inactive but retained. It is
127  * moved to the end of the list. All the active portfop_t's are maintained at
128  * the beginning. In case of exception events, the portfop_t will be removed
129  * and discarded.
130  *
131  * To intercept unmount operations, FSEM hooks are added to the file system
132  * under which files are being watched. A hash table('portfop_vfs_hash_t') of
133  * active file systems is maintained. Each file system that has active watches
134  * is represented by 'portfop_vfs_t' and is added to the hash table.
135  * The vnode's 'portfop_vp_t' structure is added to the list of files(vnodes)
136  * being watched on the portfop_vfs_t structure.
137  *
138  *
139  * File system support:
140  * -------------------
141  *
142  * The file systems implementation has to provide vnode event notifications
143  * (vnevents) in order to support watching any files on that file system.
144  * The vnode events(vnevents) are notifications provided by the file system
145  * for name based file operations like rename, remove etc, which do not go
146  * thru the VOP_** interfaces. If the file system does not implement vnode
147  * notifications, watching for file events on such file systems is not
148  * supported. The vnode event notifications support is determined by the call
149  * vnevent_support(vp) (VOP_VNEVENT(vp, VE_SUPPORT)), which the file system
150  * has to implement.
151  *
152  *
153  * Locking order:
154  * --------------
155  *
156  * A file(vnode) can have file event watches registered by different processes.
157  * There is one portfop_t per watch registered. These are on the vnode's list
158  * protected by the mutex 'pvp_mutex' in 'portfop_vp_t'. The portfop_t's are
159  * also on the per port cache. The cache is protected by the pfc_lock of
160  * portfop_cache_t. The lock order here is 'pfc_lock' -> 'pvp_mutex'.
161  *
162  */
163 
164 #include <sys/types.h>
165 #include <sys/systm.h>
166 #include <sys/stat.h>
167 #include <sys/errno.h>
168 #include <sys/kmem.h>
169 #include <sys/sysmacros.h>
170 #include <sys/debug.h>
171 #include <sys/vnode.h>
172 #include <sys/poll_impl.h>
173 #include <sys/port_impl.h>
174 #include <sys/fem.h>
175 #include <sys/vfs_opreg.h>
176 #include <sys/atomic.h>
177 
178 /*
179  * For special case support of /etc/mnttab
180  */
181 extern struct vnode *mntdummyvp;
182 extern int mntfstype;
183 
184 #define	PORTFOP_PVFSH(vfsp)	(&portvfs_hash[PORTFOP_PVFSHASH(vfsp)])
185 portfop_vfs_hash_t	 portvfs_hash[PORTFOP_PVFSHASH_SZ];
186 
187 /*
188  * Inactive file event watches(portfop_t) are retained on the vnode's list
189  * for performance reason. If the applications re-registers the file, the
190  * inactive entry is made active and moved up the list.
191  *
192  * If there are greater then the following number of watches on a vnode,
193  * it will attempt to discard an oldest inactive watch(pfp) at the time
194  * a new watch is being registerd and when events get delivered. We
195  * do this to avoid accumulating inactive watches on a file.
196  */
197 int	port_fop_maxpfps = 20;
198 
199 /* local functions */
200 static int	port_fop_callback(void *, int *, pid_t, int, void *);
201 
202 static void	port_pcache_insert(portfop_cache_t *, portfop_t *);
203 static void	port_pcache_delete(portfop_cache_t *, portfop_t *);
204 static void	port_close_fop(void *arg, int port, pid_t pid, int lastclose);
205 
206 /*
207  * port fop functions that will be the fem hooks.
208  */
209 static int port_fop_open(femarg_t *vf, int mode, cred_t *cr);
210 static int port_fop_read(femarg_t *vf, uio_t *uiop, int ioflag, cred_t *cr,
211 		struct caller_context *ct);
212 static int port_fop_write(femarg_t *vf, uio_t *uiop, int ioflag, cred_t *cr,
213 		caller_context_t *ct);
214 static int port_fop_map(femarg_t *vf, offset_t off, struct as *as,
215 		caddr_t *addrp, size_t len, uchar_t prot, uchar_t maxport,
216 		uint_t flags, cred_t *cr);
217 static int port_fop_setattr(femarg_t *vf, vattr_t *vap, int flags, cred_t *cr,
218 		caller_context_t *ct);
219 static int port_fop_create(femarg_t *vf, char *name, vattr_t *vap,
220 			vcexcl_t excl, int mode, vnode_t **vpp, cred_t *cr,
221 			int flag);
222 static int port_fop_remove(femarg_t *vf, char *nm, cred_t *cr);
223 static int port_fop_link(femarg_t *vf, vnode_t *svp, char *tnm, cred_t *cr);
224 static int port_fop_rename(femarg_t *vf, char *snm, vnode_t *tdvp, char *tnm,
225 			cred_t *cr);
226 static int port_fop_mkdir(femarg_t *vf, char *dirname, vattr_t *vap,
227 		vnode_t **vpp, cred_t *cr);
228 static int port_fop_rmdir(femarg_t *vf, char *nm, vnode_t *cdir, cred_t *cr);
229 static int port_fop_readdir(femarg_t *vf, uio_t *uiop, cred_t *cr, int *eofp);
230 static int port_fop_symlink(femarg_t *vf, char *linkname, vattr_t *vap,
231 			char *target, cred_t *cr);
232 static int port_fop_setsecattr(femarg_t *vf, vsecattr_t *vsap, int flag,
233 			cred_t *cr);
234 static int port_fop_vnevent(femarg_t *vf, vnevent_t vnevent, vnode_t *dvp,
235 			char *cname);
236 
237 static int port_fop_unmount(fsemarg_t *vf, int flag, cred_t *cr);
238 
239 
240 /*
241  * Fem hooks.
242  */
243 const fs_operation_def_t	port_vnodesrc_template[] = {
244 	VOPNAME_OPEN,		{ .femop_open = port_fop_open },
245 	VOPNAME_READ,		{ .femop_read = port_fop_read },
246 	VOPNAME_WRITE,		{ .femop_write = port_fop_write },
247 	VOPNAME_MAP,		{ .femop_map = port_fop_map },
248 	VOPNAME_SETATTR, 	{ .femop_setattr = port_fop_setattr },
249 	VOPNAME_CREATE,		{ .femop_create = port_fop_create },
250 	VOPNAME_REMOVE,		{ .femop_remove = port_fop_remove },
251 	VOPNAME_LINK,		{ .femop_link = port_fop_link },
252 	VOPNAME_RENAME,		{ .femop_rename = port_fop_rename },
253 	VOPNAME_MKDIR,		{ .femop_mkdir = port_fop_mkdir },
254 	VOPNAME_RMDIR,		{ .femop_rmdir = port_fop_rmdir },
255 	VOPNAME_READDIR,	{ .femop_readdir = port_fop_readdir },
256 	VOPNAME_SYMLINK,	{ .femop_symlink = port_fop_symlink },
257 	VOPNAME_SETSECATTR, 	{ .femop_setsecattr = port_fop_setsecattr },
258 	VOPNAME_VNEVENT,	{ .femop_vnevent = port_fop_vnevent },
259 	NULL,	NULL
260 };
261 
262 /*
263  * Fsem - vfs ops hooks
264  */
265 const fs_operation_def_t	port_vfssrc_template[] = {
266 	VFSNAME_UNMOUNT, 	{ .fsemop_unmount = port_fop_unmount },
267 	NULL,	NULL
268 };
269 
270 fem_t *fop_femop;
271 fsem_t *fop_fsemop;
272 
273 static fem_t *
274 port_fop_femop()
275 {
276 	fem_t *femp;
277 	if (fop_femop != NULL)
278 		return (fop_femop);
279 	if (fem_create("portfop_fem",
280 	    (const struct fs_operation_def *)port_vnodesrc_template,
281 	    (fem_t **)&femp)) {
282 		return (NULL);
283 	}
284 	if (casptr(&fop_femop, NULL, femp) != NULL) {
285 		/*
286 		 * some other thread beat us to it.
287 		 */
288 		fem_free(femp);
289 	}
290 	return (fop_femop);
291 }
292 
293 static fsem_t *
294 port_fop_fsemop()
295 {
296 	fsem_t *fsemp;
297 	if (fop_fsemop != NULL)
298 		return (fop_fsemop);
299 	if (fsem_create("portfop_fsem", port_vfssrc_template, &fsemp)) {
300 		return (NULL);
301 	}
302 	if (casptr(&fop_fsemop, NULL, fsemp) != NULL) {
303 		/*
304 		 * some other thread beat us to it.
305 		 */
306 		fsem_free(fsemp);
307 	}
308 	return (fop_fsemop);
309 }
310 
311 /*
312  * port_fop_callback()
313  * - PORT_CALLBACK_DEFAULT
314  *	The file event will be delivered to the application.
315  * - PORT_CALLBACK_DISSOCIATE
316  *	The object will be dissociated from  the port.
317  * - PORT_CALLBACK_CLOSE
318  *	The object will be dissociated from the port because the port
319  *	is being closed.
320  */
321 /* ARGSUSED */
322 static int
323 port_fop_callback(void *arg, int *events, pid_t pid, int flag, void *evp)
324 {
325 	portfop_t	*pfp = (portfop_t *)arg;
326 	port_kevent_t	*pkevp = (port_kevent_t *)evp;
327 	int		error = 0;
328 
329 	ASSERT((events != NULL));
330 	if (flag == PORT_CALLBACK_DEFAULT) {
331 		if (curproc->p_pid != pid) {
332 				return (EACCES); /* deny delivery of events */
333 		}
334 
335 		*events = pkevp->portkev_events;
336 		pkevp->portkev_events = 0;
337 		if (pfp != NULL) {
338 			pfp->pfop_flags &= ~PORT_FOP_KEV_ONQ;
339 		}
340 	}
341 	return (error);
342 }
343 
344 /*
345  * Inserts a portfop_t into the port sources cache's.
346  */
347 static void
348 port_pcache_insert(portfop_cache_t *pfcp, portfop_t *pfp)
349 {
350 	portfop_t	**bucket;
351 
352 	ASSERT(MUTEX_HELD(&pfcp->pfc_lock));
353 	bucket = PORT_FOP_BUCKET(pfcp, pfp->pfop_object);
354 	pfp->pfop_hashnext = *bucket;
355 	*bucket = pfp;
356 	pfcp->pfc_objcount++;
357 }
358 
359 /*
360  * Remove the pfp from the port source cache.
361  */
362 static void
363 port_pcache_delete(portfop_cache_t *pfcp, portfop_t *pfp)
364 {
365 	portfop_t	*lpdp;
366 	portfop_t	*cpdp;
367 	portfop_t	**bucket;
368 
369 	bucket = PORT_FOP_BUCKET(pfcp, pfp->pfop_object);
370 	cpdp = *bucket;
371 	if (pfp == cpdp) {
372 		*bucket = pfp->pfop_hashnext;
373 	} else {
374 		while (cpdp != NULL) {
375 			lpdp = cpdp;
376 			cpdp = cpdp->pfop_hashnext;
377 			if (cpdp == pfp) {
378 				/* portfop struct found */
379 				lpdp->pfop_hashnext = pfp->pfop_hashnext;
380 				break;
381 			}
382 		}
383 	}
384 	pfcp->pfc_objcount--;
385 }
386 
387 /*
388  * The vnode's(portfop_vp_t) pfp list management. The 'pvp_mutex' is held
389  * when these routines are called.
390  *
391  * The 'pvp_lpfop' member points to the oldest inactive entry on the list.
392  * It is used to discard the oldtest inactive pfp if the number of entries
393  * exceed the limit.
394  */
395 static void
396 port_fop_listinsert(portfop_vp_t *pvp, portfop_t *pfp, int where)
397 {
398 	if (where == 1) {
399 		list_insert_head(&pvp->pvp_pfoplist, (void *)pfp);
400 	} else {
401 		list_insert_tail(&pvp->pvp_pfoplist, (void *)pfp);
402 	}
403 	if (pvp->pvp_lpfop == NULL) {
404 		pvp->pvp_lpfop = pfp;
405 	}
406 	pvp->pvp_cnt++;
407 }
408 
409 static void
410 port_fop_listinsert_head(portfop_vp_t *pvp, portfop_t *pfp)
411 {
412 	port_fop_listinsert(pvp, pfp, 1);
413 }
414 
415 static void
416 port_fop_listinsert_tail(portfop_vp_t *pvp, portfop_t *pfp)
417 {
418 	/*
419 	 * We point lpfop to an inactive one, if it was initially pointing
420 	 * to an active one. Insert to the tail is done only when a pfp goes
421 	 * inactive.
422 	 */
423 	if (pvp->pvp_lpfop && pvp->pvp_lpfop->pfop_flags & PORT_FOP_ACTIVE) {
424 		pvp->pvp_lpfop = pfp;
425 	}
426 	port_fop_listinsert(pvp, pfp, 0);
427 }
428 
429 static void
430 port_fop_listremove(portfop_vp_t *pvp, portfop_t *pfp)
431 {
432 	if (pvp->pvp_lpfop == pfp) {
433 		pvp->pvp_lpfop = list_next(&pvp->pvp_pfoplist, (void *)pfp);
434 	}
435 
436 	list_remove(&pvp->pvp_pfoplist, (void *)pfp);
437 
438 	pvp->pvp_cnt--;
439 	if (pvp->pvp_cnt && pvp->pvp_lpfop == NULL) {
440 		pvp->pvp_lpfop = list_head(&pvp->pvp_pfoplist);
441 	}
442 }
443 
444 static void
445 port_fop_listmove(portfop_vp_t *pvp, list_t *tlist)
446 {
447 	list_move_tail(tlist, &pvp->pvp_pfoplist);
448 	pvp->pvp_lpfop = NULL;
449 	pvp->pvp_cnt = 0;
450 }
451 
452 /*
453  * Remove a portfop_t from the port cache hash table and discard it.
454  * It is called only when pfp is not on the vnode's list. Otherwise,
455  * port_remove_fop() is called.
456  */
457 void
458 port_pcache_remove_fop(portfop_cache_t *pfcp, portfop_t *pfp)
459 {
460 	port_kevent_t	*pkevp;
461 
462 
463 	ASSERT(MUTEX_HELD(&pfcp->pfc_lock));
464 
465 	pkevp = pfp->pfop_pev;
466 	pfp->pfop_pev = NULL;
467 
468 	if (pkevp != NULL) {
469 		(void) port_remove_done_event(pkevp);
470 		port_free_event_local(pkevp, 0);
471 	}
472 
473 	port_pcache_delete(pfcp, pfp);
474 
475 	if (pfp->pfop_cname != NULL)
476 		kmem_free(pfp->pfop_cname, pfp->pfop_clen + 1);
477 	kmem_free(pfp, sizeof (portfop_t));
478 	if (pfcp->pfc_objcount == 0)
479 		cv_signal(&pfcp->pfc_lclosecv);
480 }
481 
482 /*
483  * if we have too many watches on the vnode, attempt to discard an
484  * inactive one.
485  */
486 static void
487 port_fop_trimpfplist(vnode_t *vp)
488 {
489 	portfop_vp_t *pvp;
490 	portfop_t *pfp = NULL;
491 	portfop_cache_t *pfcp;
492 
493 	/*
494 	 * Due to a reference the vnode cannot disappear, v_fopdata should
495 	 * not change.
496 	 */
497 	if ((pvp = vp->v_fopdata) != NULL &&
498 	    pvp->pvp_cnt > port_fop_maxpfps) {
499 		mutex_enter(&pvp->pvp_mutex);
500 		pfp = pvp->pvp_lpfop;
501 		pfcp = pfp->pfop_pcache;
502 		/*
503 		 * only if we can get the cache lock, we need to
504 		 * do this due to reverse lock order and some thread
505 		 * that may be trying to reactivate this entry.
506 		 */
507 		if (mutex_tryenter(&pfcp->pfc_lock)) {
508 			if (pfp && !(pfp->pfop_flags & PORT_FOP_ACTIVE) &&
509 			    !(pfp->pfop_flags & PORT_FOP_KEV_ONQ)) {
510 				port_fop_listremove(pvp, pfp);
511 				pfp->pfop_flags |= PORT_FOP_REMOVING;
512 			} else {
513 				mutex_exit(&pfcp->pfc_lock);
514 				pfp = NULL;
515 			}
516 		} else {
517 			pfp = NULL;
518 		}
519 		mutex_exit(&pvp->pvp_mutex);
520 
521 		/*
522 		 * discard pfp if any.
523 		 */
524 		if (pfp != NULL) {
525 			port_pcache_remove_fop(pfcp, pfp);
526 			mutex_exit(&pfcp->pfc_lock);
527 		}
528 	}
529 }
530 
531 void
532 port_fop_femuninstall(vnode_t *vp)
533 {
534 	portfop_vp_t	*pvp;
535 	vfs_t		*vfsp;
536 	portfop_vfs_t *pvfsp;
537 	portfop_vfs_hash_t	*pvfsh;
538 	kmutex_t	*mtx;
539 
540 	/*
541 	 * if list is empty, uninstall fem.
542 	 */
543 	pvp = vp->v_fopdata;
544 	ASSERT(MUTEX_HELD(&pvp->pvp_mutex));
545 
546 	/*
547 	 * make sure the list is empty.
548 	 */
549 	if (!list_head(&pvp->pvp_pfoplist)) {
550 
551 		/*
552 		 * we could possibly uninstall the fem hooks when
553 		 * the vnode becomes inactive and the v_fopdata is
554 		 * free. But the hooks get triggered uncessarily
555 		 * even though there are no active watches. So, we
556 		 * uninstall it here.
557 		 */
558 		(void) fem_uninstall(vp, (fem_t *)pvp->pvp_femp, vp);
559 		pvp->pvp_femp = NULL;
560 		mutex_exit(&pvp->pvp_mutex);
561 
562 
563 		/*
564 		 * If we uinstalled fem means no process is watching this
565 		 * vnode, remove it from the vfs's list of watched vnodes.
566 		 */
567 		pvfsp = pvp->pvp_pvfsp;
568 		vfsp = vp->v_vfsp;
569 		pvfsh = PORTFOP_PVFSH(vfsp);
570 		mtx = &pvfsh->pvfshash_mutex;
571 		mutex_enter(mtx);
572 		/*
573 		 * If unmount is in progress, that thread will remove and
574 		 * release the vnode from the vfs's list, just leave.
575 		 */
576 		if (!pvfsp->pvfs_unmount) {
577 			list_remove(&pvfsp->pvfs_pvplist, pvp);
578 			mutex_exit(mtx);
579 			VN_RELE(vp);
580 		} else {
581 			mutex_exit(mtx);
582 		}
583 	} else {
584 		mutex_exit(&pvp->pvp_mutex);
585 	}
586 }
587 
588 /*
589  * Remove pfp from the vnode's watch list and the cache and discard it.
590  * If it is the last pfp on the vnode's list, the fem hooks get uninstalled.
591  * Returns 1 if removed successfully.
592  *
593  * The *active is set to indicate if the pfp was still active(no events had
594  * been posted, or the posted event had not been collected yet and it was
595  * able to remove it from the port's queue).
596  */
597 int
598 port_remove_fop(portfop_t *pfp, portfop_cache_t *pfcp, int cleanup,
599     int *active)
600 {
601 	vnode_t		*vp;
602 	portfop_vp_t	*pvp;
603 	int	tactive = 0;
604 
605 	ASSERT(MUTEX_HELD(&pfcp->pfc_lock));
606 	vp = pfp->pfop_vp;
607 	pvp = vp->v_fopdata;
608 	mutex_enter(&pvp->pvp_mutex);
609 
610 	/*
611 	 * if not cleanup, remove it only if the pfp is still active and
612 	 * is not being removed by some other thread.
613 	 */
614 	if (!cleanup && (!(pfp->pfop_flags & PORT_FOP_ACTIVE) ||
615 	    pfp->pfop_flags & PORT_FOP_REMOVING)) {
616 		mutex_exit(&pvp->pvp_mutex);
617 		return (0);
618 	}
619 
620 	/*
621 	 * mark it inactive.
622 	 */
623 	if (pfp->pfop_flags & PORT_FOP_ACTIVE) {
624 		pfp->pfop_flags &= ~PORT_FOP_ACTIVE;
625 		tactive = 1;
626 	}
627 
628 	/*
629 	 * Check if the pfp is still on the vnode's list. This can
630 	 * happen if port_fop_excep() is in the process of removing it.
631 	 * In case of cleanup, just mark this pfp as inactive so that no
632 	 * new events (VNEVENT) will be delivered, and remove it from the
633 	 * event queue if it was already queued. Since the cache lock is
634 	 * held, the pfp will not disappear, even though it is being
635 	 * removed.
636 	 */
637 	if (pfp->pfop_flags & PORT_FOP_REMOVING) {
638 		mutex_exit(&pvp->pvp_mutex);
639 		if (!tactive && port_remove_done_event(pfp->pfop_pev)) {
640 			pfp->pfop_flags &= ~PORT_FOP_KEV_ONQ;
641 			tactive = 1;
642 		}
643 		if (active) {
644 			*active = tactive;
645 		}
646 		return (1);
647 	}
648 
649 	/*
650 	 * if we find an event on the queue and removed it, then this
651 	 * association is considered active.
652 	 */
653 	if (!tactive && port_remove_done_event(pfp->pfop_pev)) {
654 		pfp->pfop_flags &= ~PORT_FOP_KEV_ONQ;
655 		tactive = 1;
656 	}
657 
658 	if (active) {
659 		*active = tactive;
660 	}
661 	pvp = (portfop_vp_t *)vp->v_fopdata;
662 
663 	/*
664 	 * remove pfp from the vnode's list
665 	 */
666 	port_fop_listremove(pvp, pfp);
667 
668 	/*
669 	 * If no more associations on the vnode, uninstall fem hooks.
670 	 * The pvp mutex will be released in this routine.
671 	 */
672 	port_fop_femuninstall(vp);
673 	port_pcache_remove_fop(pfcp, pfp);
674 	return (1);
675 }
676 
677 /*
678  * This routine returns a pointer to a cached portfop entry, or NULL if it
679  * does not find it in the hash table. The object pointer is used as index.
680  * The entries are hashed by the object's address. We need to match the pid
681  * as the evet port can be shared between processes. The file events
682  * watches are per process only.
683  */
684 portfop_t *
685 port_cache_lookup_fop(portfop_cache_t *pfcp, pid_t pid, uintptr_t obj)
686 {
687 	portfop_t	*pfp = NULL;
688 	portfop_t	**bucket;
689 
690 	ASSERT(MUTEX_HELD(&pfcp->pfc_lock));
691 	bucket = PORT_FOP_BUCKET(pfcp, obj);
692 	pfp = *bucket;
693 	while (pfp != NULL) {
694 		if (pfp->pfop_object == obj && pfp->pfop_pid == pid)
695 			break;
696 		pfp = pfp->pfop_hashnext;
697 	}
698 	return (pfp);
699 }
700 
701 /*
702  * Given the file name, get the vnode and also the directory vnode
703  * On return, the vnodes are held (VN_HOLD). The caller has to VN_RELE
704  * the vnode(s).
705  */
706 int
707 port_fop_getdvp(void *objptr, vnode_t **vp, vnode_t **dvp,
708 	char **cname, int *len, int follow)
709 {
710 	int error = 0;
711 	struct pathname pn;
712 	char *fname;
713 
714 	if (get_udatamodel() == DATAMODEL_NATIVE) {
715 		fname = ((file_obj_t *)objptr)->fo_name;
716 #ifdef  _SYSCALL32_IMPL
717 	} else {
718 		fname = (caddr_t)(uintptr_t)((file_obj32_t *)objptr)->fo_name;
719 #endif	/* _SYSCALL32_IMPL */
720 	}
721 
722 	/*
723 	 * lookuppn may fail with EINVAL, if dvp is  non-null(like when
724 	 * looking for "."). So call again with dvp = NULL.
725 	 */
726 	if ((error = pn_get(fname, UIO_USERSPACE, &pn)) != 0) {
727 		return (error);
728 	}
729 
730 	error = lookuppn(&pn, NULL, follow, dvp, vp);
731 	if (error == EINVAL) {
732 		pn_free(&pn);
733 		if ((error = pn_get(fname, UIO_USERSPACE, &pn)) != 0) {
734 			return (error);
735 		}
736 		error = lookuppn(&pn, NULL, follow, NULL, vp);
737 		if (dvp != NULL) {
738 			*dvp = NULL;
739 		}
740 	}
741 
742 	if (error == 0 && cname != NULL && len != NULL) {
743 		pn_setlast(&pn);
744 		*len = pn.pn_pathlen;
745 		*cname = kmem_alloc(*len + 1, KM_SLEEP);
746 		(void) strcpy(*cname, pn.pn_path);
747 	} else {
748 		if (cname != NULL && len != NULL) {
749 			*cname = NULL;
750 			*len = 0;
751 		}
752 	}
753 
754 	pn_free(&pn);
755 	return (error);
756 }
757 
758 port_source_t *
759 port_getsrc(port_t *pp, int source)
760 {
761 	port_source_t *pse;
762 	int	lock = 0;
763 	/*
764 	 * get the port source structure.
765 	 */
766 	if (!MUTEX_HELD(&pp->port_queue.portq_source_mutex)) {
767 		mutex_enter(&pp->port_queue.portq_source_mutex);
768 		lock = 1;
769 	}
770 
771 	pse = pp->port_queue.portq_scache[PORT_SHASH(source)];
772 	for (; pse != NULL; pse = pse->portsrc_next) {
773 		if (pse->portsrc_source == source)
774 			break;
775 	}
776 
777 	if (lock) {
778 		mutex_exit(&pp->port_queue.portq_source_mutex);
779 	}
780 	return (pse);
781 }
782 
783 
784 /*
785  * compare time stamps and generate an event if it has changed.
786  */
787 static void
788 port_check_timestamp(vnode_t *vp, portfop_t *pfp, void *objptr)
789 {
790 	vattr_t		vatt;
791 	portfop_vp_t	*pvp = vp->v_fopdata;
792 	int		events = 0;
793 	port_kevent_t	*pkevp;
794 	file_obj_t	*fobj;
795 
796 	if (!(pfp->pfop_flags & PORT_FOP_ACTIVE)) {
797 		/*
798 		 * some event got delivered, don't bother with
799 		 * checking the timestamps.
800 		 */
801 		return;
802 	}
803 
804 	/*
805 	 * If time stamps is specified, get attributes and compare. This
806 	 * needs to be done after registering. We should check if any
807 	 * timestamps have been specified before getting attr XXX.
808 	 */
809 	vatt.va_mask = AT_ATIME|AT_MTIME|AT_CTIME;
810 	if (get_udatamodel() == DATAMODEL_NATIVE) {
811 		fobj = (file_obj_t *)objptr;
812 		if (fobj->fo_atime.tv_sec || fobj->fo_atime.tv_nsec ||
813 		    fobj->fo_mtime.tv_sec || fobj->fo_mtime.tv_nsec ||
814 		    fobj->fo_ctime.tv_sec || fobj->fo_ctime.tv_nsec) {
815 			if (VOP_GETATTR(vp, &vatt, 0, CRED())) {
816 				return;
817 			}
818 		} else {
819 			/*
820 			 * timestamp not specified, all 0's,
821 			 */
822 			return;
823 		}
824 #ifdef  _SYSCALL32_IMPL
825 	} else {
826 		file_obj32_t	*fobj32;
827 		fobj32 = (file_obj32_t *)objptr;
828 		if (fobj32->fo_atime.tv_sec || fobj32->fo_atime.tv_nsec ||
829 		    fobj32->fo_mtime.tv_sec || fobj32->fo_mtime.tv_nsec ||
830 		    fobj32->fo_ctime.tv_sec || fobj32->fo_ctime.tv_nsec) {
831 			if (VOP_GETATTR(vp, &vatt, 0, CRED())) {
832 				return;
833 			}
834 		} else {
835 			/*
836 			 * timestamp not specified, all 0.
837 			 */
838 			return;
839 		}
840 #endif /* _SYSCALL32_IMPL */
841 	}
842 
843 	mutex_enter(&pvp->pvp_mutex);
844 	/*
845 	 * The pfp cannot dissappear as the port cache lock is held.
846 	 * While the pvp_mutex is held, no events will get delivered.
847 	 */
848 	if (pfp->pfop_flags & PORT_FOP_ACTIVE &&
849 	    !(pfp->pfop_flags & PORT_FOP_REMOVING)) {
850 		if (get_udatamodel() == DATAMODEL_NATIVE) {
851 			fobj = (file_obj_t *)objptr;
852 			if (pfp->pfop_events & FILE_ACCESS &&
853 			    (fobj->fo_atime.tv_sec || fobj->fo_atime.tv_nsec) &&
854 			    (vatt.va_atime.tv_sec != fobj->fo_atime.tv_sec ||
855 			    vatt.va_atime.tv_nsec != fobj->fo_atime.tv_nsec))
856 				events |= FILE_ACCESS;
857 
858 			if (pfp->pfop_events & FILE_MODIFIED &&
859 			    (fobj->fo_mtime.tv_sec || fobj->fo_mtime.tv_nsec) &&
860 			    (vatt.va_mtime.tv_sec != fobj->fo_mtime.tv_sec ||
861 			    vatt.va_mtime.tv_nsec != fobj->fo_mtime.tv_nsec))
862 				events |= FILE_MODIFIED;
863 
864 			if (pfp->pfop_events & FILE_ATTRIB &&
865 			    (fobj->fo_ctime.tv_sec || fobj->fo_ctime.tv_nsec) &&
866 			    (vatt.va_ctime.tv_sec != fobj->fo_ctime.tv_sec ||
867 			    vatt.va_ctime.tv_nsec != fobj->fo_ctime.tv_nsec))
868 				events |= FILE_ATTRIB;
869 #ifdef  _SYSCALL32_IMPL
870 		} else {
871 			file_obj32_t	*fobj32;
872 			fobj32 = (file_obj32_t *)objptr;
873 			if (pfp->pfop_events & FILE_ACCESS &&
874 			    (fobj32->fo_atime.tv_sec ||
875 			    fobj32->fo_atime.tv_nsec) &&
876 			    (vatt.va_atime.tv_sec != fobj32->fo_atime.tv_sec ||
877 			    vatt.va_atime.tv_nsec != fobj32->fo_atime.tv_nsec))
878 				events |= FILE_ACCESS;
879 
880 			if (pfp->pfop_events & FILE_MODIFIED &&
881 			    (fobj32->fo_mtime.tv_sec ||
882 			    fobj32->fo_mtime.tv_nsec) &&
883 			    (vatt.va_mtime.tv_sec != fobj32->fo_mtime.tv_sec ||
884 			    vatt.va_mtime.tv_nsec != fobj32->fo_mtime.tv_nsec))
885 				events |= FILE_MODIFIED;
886 
887 			if (pfp->pfop_events & FILE_ATTRIB &&
888 			    (fobj32->fo_ctime.tv_sec ||
889 			    fobj32->fo_ctime.tv_nsec) &&
890 			    (vatt.va_ctime.tv_sec != fobj32->fo_ctime.tv_sec ||
891 			    vatt.va_ctime.tv_nsec != fobj32->fo_ctime.tv_nsec))
892 				events |= FILE_ATTRIB;
893 #endif /* _SYSCALL32_IMPL */
894 		}
895 
896 		/*
897 		 * No events to deliver
898 		 */
899 		if (events == 0) {
900 			mutex_exit(&pvp->pvp_mutex);
901 			return;
902 		}
903 
904 		/*
905 		 * Deliver the event now.
906 		 */
907 		pkevp = pfp->pfop_pev;
908 		pfp->pfop_flags &= ~PORT_FOP_ACTIVE;
909 		pkevp->portkev_events |= events;
910 		/*
911 		 * Move it to the tail as active once are in the
912 		 * begining of the list.
913 		 */
914 		port_fop_listremove(pvp, pfp);
915 		port_fop_listinsert_tail(pvp, pfp);
916 		port_send_event(pkevp);
917 		pfp->pfop_flags |= PORT_FOP_KEV_ONQ;
918 	}
919 	mutex_exit(&pvp->pvp_mutex);
920 }
921 
922 /*
923  * Add the event source to the port and return the port source cache pointer.
924  */
925 int
926 port_fop_associate_source(portfop_cache_t **pfcpp, port_t *pp, int source)
927 {
928 	portfop_cache_t *pfcp;
929 	port_source_t	*pse;
930 	int		error;
931 
932 	/*
933 	 * associate PORT_SOURCE_FILE source with the port, if it is
934 	 * not associated yet. Note the PORT_SOURCE_FILE source is
935 	 * associated once and will not be dissociated.
936 	 */
937 	if ((pse = port_getsrc(pp, PORT_SOURCE_FILE)) == NULL) {
938 		if (error = port_associate_ksource(pp->port_fd, source,
939 		    &pse, port_close_fop, pp, NULL)) {
940 			*pfcpp = NULL;
941 			return (error);
942 		}
943 	}
944 
945 	/*
946 	 * Get the portfop cache pointer.
947 	 */
948 	if ((pfcp = pse->portsrc_data) == NULL) {
949 		/*
950 		 * This is the first time that a file is being associated,
951 		 * create the portfop cache.
952 		 */
953 		pfcp = kmem_zalloc(sizeof (portfop_cache_t), KM_SLEEP);
954 		mutex_enter(&pp->port_queue.portq_source_mutex);
955 		if (pse->portsrc_data == NULL) {
956 			pse->portsrc_data = pfcp;
957 			mutex_exit(&pp->port_queue.portq_source_mutex);
958 		} else {
959 			/*
960 			 * someone else created the port cache, free
961 			 * what we just now allocated.
962 			 */
963 			mutex_exit(&pp->port_queue.portq_source_mutex);
964 			kmem_free(pfcp, sizeof (portfop_cache_t));
965 			pfcp = pse->portsrc_data;
966 		}
967 	}
968 	*pfcpp = pfcp;
969 	return (0);
970 }
971 
972 /*
973  * Add the given pvp on the file system's list of vnodes watched.
974  */
975 int
976 port_fop_pvfsadd(portfop_vp_t *pvp)
977 {
978 	int error = 0;
979 	vnode_t	*vp = pvp->pvp_vp;
980 	portfop_vfs_hash_t *pvfsh;
981 	portfop_vfs_t	 *pvfsp;
982 	fsem_t		*fsemp;
983 
984 	pvfsh = PORTFOP_PVFSH(vp->v_vfsp);
985 	mutex_enter(&pvfsh->pvfshash_mutex);
986 	for (pvfsp = pvfsh->pvfshash_pvfsp; pvfsp &&
987 	    pvfsp->pvfs != vp->v_vfsp; pvfsp = pvfsp->pvfs_next)
988 		;
989 
990 	if (!pvfsp) {
991 		if ((fsemp = port_fop_fsemop()) != NULL) {
992 			if ((error = fsem_install(vp->v_vfsp, fsemp,
993 			    vp->v_vfsp, OPUNIQ, NULL, NULL))) {
994 				mutex_exit(&pvfsh->pvfshash_mutex);
995 				return (error);
996 			}
997 		} else {
998 			mutex_exit(&pvfsh->pvfshash_mutex);
999 			return (EINVAL);
1000 		}
1001 		pvfsp = kmem_zalloc(sizeof (portfop_vfs_t), KM_SLEEP);
1002 		pvfsp->pvfs = vp->v_vfsp;
1003 		list_create(&(pvfsp->pvfs_pvplist), sizeof (portfop_vp_t),
1004 		    offsetof(portfop_vp_t, pvp_pvfsnode));
1005 		pvfsp->pvfs_fsemp = fsemp;
1006 		pvfsp->pvfs_next = pvfsh->pvfshash_pvfsp;
1007 		pvfsh->pvfshash_pvfsp = pvfsp;
1008 	}
1009 
1010 	/*
1011 	 * check if an unmount is in progress.
1012 	 */
1013 	if (!pvfsp->pvfs_unmount) {
1014 		/*
1015 		 * insert the pvp on list.
1016 		 */
1017 		pvp->pvp_pvfsp = pvfsp;
1018 		list_insert_head(&pvfsp->pvfs_pvplist, (void *)pvp);
1019 	} else {
1020 		error = EINVAL;
1021 	}
1022 	mutex_exit(&pvfsh->pvfshash_mutex);
1023 	return (error);
1024 }
1025 
1026 /*
1027  * Installs the portfop_vp_t data structure on the
1028  * vnode. The 'pvp_femp == NULL' indicates it is not
1029  * active. The fem hooks have to be installed.
1030  * The portfop_vp_t is only freed when the vnode gets freed.
1031  */
1032 void
1033 port_install_fopdata(vnode_t *vp)
1034 {
1035 	portfop_vp_t *npvp;
1036 
1037 	npvp = kmem_zalloc(sizeof (*npvp), KM_SLEEP);
1038 	mutex_init(&npvp->pvp_mutex, NULL, MUTEX_DEFAULT, NULL);
1039 	list_create(&npvp->pvp_pfoplist, sizeof (portfop_t),
1040 	    offsetof(portfop_t, pfop_node));
1041 	npvp->pvp_vp = vp;
1042 	/*
1043 	 * If v_fopdata is not null, some other thread beat us to it.
1044 	 */
1045 	if (casptr(&vp->v_fopdata, NULL, npvp) != NULL) {
1046 		mutex_destroy(&npvp->pvp_mutex);
1047 		list_destroy(&npvp->pvp_pfoplist);
1048 		kmem_free(npvp, sizeof (*npvp));
1049 	}
1050 }
1051 
1052 
1053 /*
1054  * Allocate and add a portfop_t to the per port cache. Also add the portfop_t
1055  * to the vnode's list. The association is identified by the object pointer
1056  * address and pid.
1057  */
1058 int
1059 port_pfp_setup(portfop_t **pfpp, port_t *pp, vnode_t *vp, portfop_cache_t *pfcp,
1060 	uintptr_t object, int events, void *user, char *cname, int clen,
1061 	vnode_t *dvp)
1062 {
1063 	portfop_t	*pfp = NULL;
1064 	port_kevent_t	*pkevp;
1065 	fem_t		*femp;
1066 	int		error = 0;
1067 	portfop_vp_t	*pvp;
1068 
1069 
1070 	/*
1071 	 * The port cache mutex is held.
1072 	 */
1073 	*pfpp  = NULL;
1074 
1075 
1076 	/*
1077 	 * At this point the fem monitor is installed.
1078 	 * Allocate a port event structure per vnode association.
1079 	 */
1080 	if (pfp == NULL) {
1081 		if (error = port_alloc_event_local(pp, PORT_SOURCE_FILE,
1082 		    PORT_ALLOC_CACHED, &pkevp)) {
1083 			return (error);
1084 		}
1085 		pfp = kmem_zalloc(sizeof (portfop_t), KM_SLEEP);
1086 		pfp->pfop_pev = pkevp;
1087 	}
1088 
1089 	pfp->pfop_vp = vp;
1090 	pfp->pfop_pid = curproc->p_pid;
1091 	pfp->pfop_pcache = pfcp;
1092 	pfp->pfop_pp = pp;
1093 	pfp->pfop_flags |= PORT_FOP_ACTIVE;
1094 	pfp->pfop_cname = cname;
1095 	pfp->pfop_clen = clen;
1096 	pfp->pfop_dvp = dvp;
1097 	pfp->pfop_object = object;
1098 
1099 	pkevp->portkev_callback = port_fop_callback;
1100 	pkevp->portkev_arg = pfp;
1101 	pkevp->portkev_object = object;
1102 	pkevp->portkev_user = user;
1103 	pkevp->portkev_events = 0;
1104 
1105 	port_pcache_insert(pfcp, pfp);
1106 
1107 	/*
1108 	 * Register a new file events monitor for this file(vnode), if not
1109 	 * done already.
1110 	 */
1111 	if ((pvp = vp->v_fopdata) == NULL) {
1112 		port_install_fopdata(vp);
1113 		pvp = vp->v_fopdata;
1114 	}
1115 
1116 	mutex_enter(&pvp->pvp_mutex);
1117 	/*
1118 	 * if the vnode does not have the file events hooks, install it.
1119 	 */
1120 	if (pvp->pvp_femp == NULL) {
1121 		if ((femp = port_fop_femop()) != NULL) {
1122 			if (!(error = fem_install(pfp->pfop_vp, femp,
1123 			    (void *)vp, OPUNIQ, NULL, NULL))) {
1124 				pvp->pvp_femp = femp;
1125 				/*
1126 				 * add fsem_t hooks to the vfsp and add pvp to
1127 				 * the list of vnodes for this vfs.
1128 				 */
1129 				if (!(error = port_fop_pvfsadd(pvp))) {
1130 					/*
1131 					 * Hold a reference to the vnode since
1132 					 * we successfully installed the hooks.
1133 					 */
1134 					VN_HOLD(vp);
1135 				} else {
1136 					(void) fem_uninstall(vp, femp, vp);
1137 					pvp->pvp_femp = NULL;
1138 				}
1139 			}
1140 		} else {
1141 			error = EINVAL;
1142 		}
1143 	}
1144 
1145 	if (error) {
1146 		/*
1147 		 * pkevp will get freed here.
1148 		 */
1149 		port_pcache_remove_fop(pfcp, pfp);
1150 		mutex_exit(&pvp->pvp_mutex);
1151 		return (error);
1152 	}
1153 
1154 	/*
1155 	 * insert the pfp on the vnode's list. After this
1156 	 * events can get delivered.
1157 	 */
1158 	pfp->pfop_events = events;
1159 	port_fop_listinsert_head(pvp, pfp);
1160 
1161 	mutex_exit(&pvp->pvp_mutex);
1162 	*pfpp = pfp;
1163 	return (0);
1164 }
1165 
1166 vnode_t *
1167 port_resolve_vp(vnode_t *vp)
1168 {
1169 	vnode_t *rvp;
1170 	/*
1171 	 * special case /etc/mnttab, the only mntfs type
1172 	 * file that can exist.
1173 	 */
1174 	if (mntdummyvp && vp->v_vfsp->vfs_fstype == mntfstype) {
1175 		VN_RELE(vp);
1176 		vp = mntdummyvp;
1177 		VN_HOLD(mntdummyvp);
1178 	}
1179 
1180 	/*
1181 	 * This should take care of lofs mounted fs systems and nfs4
1182 	 * hardlinks.
1183 	 */
1184 	if ((VOP_REALVP(vp, &rvp) == 0) && vp != rvp) {
1185 		VN_HOLD(rvp);
1186 		VN_RELE(vp);
1187 		vp = rvp;
1188 	}
1189 	return (vp);
1190 }
1191 
1192 /*
1193  * Register a file events watch on the given file associated to the port *pp.
1194  *
1195  * The association is identified by the object pointer and the pid.
1196  * The events argument contains the events to be monitored for.
1197  */
1198 int
1199 port_associate_fop(port_t *pp, int source, uintptr_t object, int events,
1200     void *user)
1201 {
1202 	portfop_cache_t	*pfcp;
1203 	vnode_t		*vp, *dvp;
1204 	portfop_t	*pfp;
1205 	int		error = 0;
1206 	file_obj_t	fobj;
1207 	void		*objptr;
1208 	char		*cname;
1209 	int		clen;
1210 	int		removing = 0;
1211 	int		follow;
1212 
1213 	/*
1214 	 * check that events specified are valid.
1215 	 */
1216 	if ((events & ~FILE_EVENTS_MASK) != 0)
1217 		return (EINVAL);
1218 
1219 	if (get_udatamodel() == DATAMODEL_NATIVE) {
1220 		if (copyin((void *)object, &fobj, sizeof (file_obj_t)))
1221 			return (EFAULT);
1222 		objptr = (void *)&fobj;
1223 #ifdef  _SYSCALL32_IMPL
1224 	} else {
1225 		file_obj32_t	fobj32;
1226 		if (copyin((void *)object, &fobj32, sizeof (file_obj32_t)))
1227 			return (EFAULT);
1228 		objptr = (void *)&fobj32;
1229 #endif  /* _SYSCALL32_IMPL */
1230 	}
1231 
1232 	vp = dvp = NULL;
1233 
1234 	/*
1235 	 * findout if we need to follow symbolic links.
1236 	 */
1237 	follow = !(events & FILE_NOFOLLOW);
1238 	events = events & ~FILE_NOFOLLOW;
1239 
1240 	/*
1241 	 * lookup and find the vnode and its directory vnode of the given
1242 	 * file.
1243 	 */
1244 	if ((error = port_fop_getdvp(objptr, &vp, &dvp, &cname, &clen,
1245 	    follow)) != 0) {
1246 		return (error);
1247 	}
1248 
1249 	if (dvp != NULL) {
1250 		dvp = port_resolve_vp(dvp);
1251 		VN_RELE(dvp);
1252 	}
1253 
1254 	/*
1255 	 * Not found
1256 	 */
1257 	if (vp == NULL) {
1258 		error = ENOENT;
1259 		goto errout;
1260 	}
1261 
1262 	vp = port_resolve_vp(vp);
1263 
1264 
1265 	if (vp != NULL && vnevent_support(vp)) {
1266 		error = ENOTSUP;
1267 		goto errout;
1268 	}
1269 
1270 	/*
1271 	 * Associate this source to the port and get the per port
1272 	 * fop cache pointer. If the source is already associated, it
1273 	 * will just return the cache pointer.
1274 	 */
1275 	if (error = port_fop_associate_source(&pfcp, pp, source)) {
1276 		goto errout;
1277 	}
1278 
1279 	/*
1280 	 * Check if there is an existing association of this file.
1281 	 */
1282 	mutex_enter(&pfcp->pfc_lock);
1283 	pfp = port_cache_lookup_fop(pfcp, curproc->p_pid, object);
1284 
1285 	/*
1286 	 * if it is not the same vnode, just discard it.
1287 	 */
1288 	if (pfp != NULL && (pfp->pfop_vp != vp || pfp->pfop_dvp != dvp)) {
1289 		(void) port_remove_fop(pfp, pfcp, 1, NULL);
1290 		pfp = NULL;
1291 	}
1292 
1293 	if (pfp == NULL) {
1294 		/*
1295 		 * Add a new association, save the file name and the
1296 		 * directory vnode pointer.
1297 		 */
1298 		if (error = port_pfp_setup(&pfp, pp, vp, pfcp, object,
1299 		    events, user, cname, clen, dvp)) {
1300 			mutex_exit(&pfcp->pfc_lock);
1301 			goto errout;
1302 		}
1303 
1304 		/*
1305 		 * File name used, so make sure we don't free it.
1306 		 */
1307 		cname = NULL;
1308 
1309 		/*
1310 		 * We need to check if the file was removed after the
1311 		 * the lookup and before the fem hooks where added. If
1312 		 * so, return error. The vnode will still exist as we have
1313 		 * a hold on it.
1314 		 */
1315 		if (pfp->pfop_flags & PORT_FOP_ACTIVE &&
1316 		    !(pfp->pfop_flags & PORT_FOP_REMOVING)) {
1317 			vnode_t *tvp;
1318 			int error;
1319 
1320 			tvp = NULL;
1321 			if ((error = port_fop_getdvp(objptr, &tvp, NULL,
1322 			    NULL, NULL, follow)) == 0) {
1323 				if (tvp != NULL) {
1324 					tvp = port_resolve_vp(tvp);
1325 				}
1326 			}
1327 			if (error || tvp == NULL || tvp != vp) {
1328 
1329 				/*
1330 				 * remove the pfp and fem hooks, if pfp still
1331 				 * active and it is not being removed from
1332 				 * the vnode list. This is checked in
1333 				 * port_remove_fop with the vnode lock held.
1334 				 */
1335 				if (port_remove_fop(pfp, pfcp, 0, NULL)) {
1336 					/*
1337 					 * the pfp was removed, means no
1338 					 * events where queued. Report the
1339 					 * error now.
1340 					 */
1341 					error = EINVAL;
1342 					if (tvp != NULL)
1343 						VN_RELE(tvp);
1344 					mutex_exit(&pfcp->pfc_lock);
1345 					goto errout;
1346 				}
1347 			} else {
1348 				VN_RELE(tvp);
1349 			}
1350 		}
1351 	} else {
1352 		portfop_vp_t	*pvp = vp->v_fopdata;
1353 
1354 		/*
1355 		 * Re-association of the object.
1356 		 */
1357 		mutex_enter(&pvp->pvp_mutex);
1358 
1359 		/*
1360 		 * remove any queued up event.
1361 		 */
1362 		if (port_remove_done_event(pfp->pfop_pev)) {
1363 			pfp->pfop_flags &= ~PORT_FOP_KEV_ONQ;
1364 		}
1365 
1366 		/*
1367 		 * set new events to watch.
1368 		 */
1369 		pfp->pfop_events = events;
1370 
1371 		/*
1372 		 * check if this pfp is being removed. Port_fop_excep()
1373 		 * will deliver an exception event.
1374 		 */
1375 		if (pfp->pfop_flags & PORT_FOP_REMOVING) {
1376 			removing  = 1;
1377 		}
1378 
1379 		/*
1380 		 * If not active, mark it active even if it is being
1381 		 * removed. Then it can send an exception event.
1382 		 *
1383 		 * Move it to the head, as the active ones are only
1384 		 * in the begining. If removing, the pfp will be on
1385 		 * a temporary list, no need to move it to the front
1386 		 * all the entries will be processed.
1387 		 */
1388 		if (!(pfp->pfop_flags & PORT_FOP_ACTIVE)) {
1389 			pfp->pfop_flags |= PORT_FOP_ACTIVE;
1390 			if (!removing) {
1391 				pvp = (portfop_vp_t *)vp->v_fopdata;
1392 				port_fop_listremove(pvp, pfp);
1393 				port_fop_listinsert_head(pvp, pfp);
1394 			}
1395 		}
1396 		mutex_exit(&pvp->pvp_mutex);
1397 	}
1398 
1399 
1400 	/*
1401 	 * compare time stamps and deliver events. The pfp cannot
1402 	 * dissappear since we are holding the cache lock.
1403 	 */
1404 	if (!removing && vp->v_type != VFIFO) {
1405 		port_check_timestamp(vp, pfp, objptr);
1406 	}
1407 
1408 	mutex_exit(&pfcp->pfc_lock);
1409 	error = 0;
1410 
1411 	/*
1412 	 *  If we have too many watches on the vnode, discard an
1413 	 *  inactive watch.
1414 	 */
1415 	port_fop_trimpfplist(vp);
1416 
1417 errout:
1418 	/*
1419 	 * Release the hold acquired due to the lookup operation.
1420 	 */
1421 	if (vp != NULL)
1422 		VN_RELE(vp);
1423 
1424 	/*
1425 	 * copied file name not used, free it.
1426 	 */
1427 	if (cname != NULL) {
1428 		kmem_free(cname, clen + 1);
1429 	}
1430 	return (error);
1431 }
1432 
1433 
1434 /*
1435  * The port_dissociate_fop() function dissociates the file object
1436  * from the event port and removes any events that are already on the queue.
1437  * Only the owner of the association is allowed to dissociate the file from
1438  * the port. Returns  success (0) if it was found and removed. Otherwise
1439  * ENOENT.
1440  */
1441 int
1442 port_dissociate_fop(port_t *pp, uintptr_t object)
1443 {
1444 	portfop_cache_t	*pfcp;
1445 	portfop_t	*pfp;
1446 	port_source_t	*pse;
1447 	int		active = 0;
1448 
1449 	pse = port_getsrc(pp, PORT_SOURCE_FILE);
1450 
1451 	/*
1452 	 * if this source is not associated or if there is no
1453 	 * cache, nothing to do just return.
1454 	 */
1455 	if (pse == NULL ||
1456 	    (pfcp = (portfop_cache_t *)pse->portsrc_data) == NULL)
1457 		return (EINVAL);
1458 
1459 	/*
1460 	 * Check if this object is on the cache. Only the owner pid
1461 	 * is allowed to dissociate.
1462 	 */
1463 	mutex_enter(&pfcp->pfc_lock);
1464 	pfp = port_cache_lookup_fop(pfcp, curproc->p_pid, object);
1465 	if (pfp == NULL) {
1466 		mutex_exit(&pfcp->pfc_lock);
1467 		return (ENOENT);
1468 	}
1469 
1470 	/*
1471 	 * If this was the last association, it will release
1472 	 * the hold on the vnode. There is a race condition where
1473 	 * the the pfp is being removed due to an exception event
1474 	 * in port_fop_sendevent()->port_fop_excep() and port_remove_fop().
1475 	 * Since port source cache lock is held, port_fop_excep() cannot
1476 	 * complete. And the vnode itself will not dissapear as long pfp's
1477 	 * have a reference.
1478 	 */
1479 	(void) port_remove_fop(pfp, pfcp, 1, &active);
1480 	mutex_exit(&pfcp->pfc_lock);
1481 	return (active ? 0 : ENOENT);
1482 }
1483 
1484 
1485 /*
1486  * port_close() calls this function to request the PORT_SOURCE_FILE source
1487  * to remove/free all resources allocated and associated with the port.
1488  */
1489 
1490 /* ARGSUSED */
1491 static void
1492 port_close_fop(void *arg, int port, pid_t pid, int lastclose)
1493 {
1494 	port_t		*pp = arg;
1495 	portfop_cache_t	*pfcp;
1496 	portfop_t	**hashtbl;
1497 	portfop_t	*pfp;
1498 	portfop_t	*pfpnext;
1499 	int		index;
1500 	port_source_t	*pse;
1501 
1502 
1503 	pse = port_getsrc(pp, PORT_SOURCE_FILE);
1504 
1505 	/*
1506 	 * No source or no cache, nothing to do.
1507 	 */
1508 	if (pse == NULL ||
1509 	    (pfcp = (portfop_cache_t *)pse->portsrc_data) == NULL)
1510 		return;
1511 	/*
1512 	 * Scan the cache and free all allocated portfop_t and port_kevent_t
1513 	 * structures of this pid.
1514 	 */
1515 	mutex_enter(&pfcp->pfc_lock);
1516 	hashtbl = (portfop_t **)pfcp->pfc_hash;
1517 	for (index = 0; index < PORTFOP_HASHSIZE; index++) {
1518 		for (pfp = hashtbl[index]; pfp != NULL; pfp = pfpnext) {
1519 			pfpnext = pfp->pfop_hashnext;
1520 			if (pid == pfp->pfop_pid) {
1521 				(void) port_remove_fop(pfp, pfcp, 1, NULL);
1522 			}
1523 		}
1524 	}
1525 
1526 	/*
1527 	 * Due to a race between port_close_fop() and port_fop()
1528 	 * trying to remove the pfp's from the port's cache, it is
1529 	 * possible that some pfp's are still in the process of being
1530 	 * freed so we wait.
1531 	 */
1532 	while (lastclose && pfcp->pfc_objcount) {
1533 		(void) cv_wait_sig(&pfcp->pfc_lclosecv, &pfcp->pfc_lock);
1534 	}
1535 	mutex_exit(&pfcp->pfc_lock);
1536 	/*
1537 	 * last close, free the cache.
1538 	 */
1539 	if (lastclose) {
1540 		ASSERT(pfcp->pfc_objcount == 0);
1541 		pse->portsrc_data = NULL;
1542 		kmem_free(pfcp, sizeof (portfop_cache_t));
1543 	}
1544 }
1545 
1546 /*
1547  * Given the list of associations(watches), it will send exception events,
1548  * if still active, and discard them. The exception events are handled
1549  * seperately because, the pfp needs to be removed from the port cache and
1550  * freed as the vnode's identity is changing or being removed. To remove
1551  * the pfp from the port's cache, we need to hold the cache lock (pfc_lock).
1552  * The lock order is pfc_lock -> pvp_mutex(vnode's) mutex and that is why
1553  * the cache's lock cannot be acquired in port_fop_sendevent().
1554  */
1555 static void
1556 port_fop_excep(list_t *tlist, int op)
1557 {
1558 	portfop_t	*pfp;
1559 	portfop_cache_t *pfcp;
1560 	port_t	*pp;
1561 	port_kevent_t	*pkevp;
1562 	int		error = 0;
1563 
1564 	while (pfp = (portfop_t *)list_head(tlist)) {
1565 		int removed = 0;
1566 		/*
1567 		 * remove from the temp list. Since PORT_FOP_REMOVING is
1568 		 * set, no other thread should attempt to perform a
1569 		 * list_remove on this pfp.
1570 		 */
1571 		list_remove(tlist, pfp);
1572 
1573 		pfcp = pfp->pfop_pcache;
1574 		mutex_enter(&pfcp->pfc_lock);
1575 
1576 		/*
1577 		 * Remove the event from the port queue if it was queued up.
1578 		 * No need to clear the PORT_FOP_KEV_ONQ flag as this pfp is
1579 		 * no longer on the vnode's list.
1580 		 */
1581 		if ((pfp->pfop_flags & PORT_FOP_KEV_ONQ)) {
1582 			removed = port_remove_done_event(pfp->pfop_pev);
1583 		}
1584 
1585 		/*
1586 		 * If still active or the event was queued up and
1587 		 * had not been collected yet, send an EXCEPTION event.
1588 		 */
1589 		if (pfp->pfop_flags & (PORT_FOP_ACTIVE) || removed) {
1590 			pp = pfp->pfop_pp;
1591 			/*
1592 			 * Allocate a port_kevent_t non cached to send this
1593 			 * event since we will be de-registering.
1594 			 * The port_kevent_t cannot be pointing back to the
1595 			 * pfp anymore.
1596 			 */
1597 			pfp->pfop_flags &= ~PORT_FOP_ACTIVE;
1598 			error = port_alloc_event_local(pp, PORT_SOURCE_FILE,
1599 			    PORT_ALLOC_DEFAULT, &pkevp);
1600 			if (!error) {
1601 
1602 				pkevp->portkev_callback = port_fop_callback;
1603 				pkevp->portkev_arg = NULL;
1604 				pkevp->portkev_object =
1605 				    pfp->pfop_pev->portkev_object;
1606 				pkevp->portkev_user =
1607 				    pfp->pfop_pev->portkev_user;
1608 				/*
1609 				 * Copy the pid of the watching process.
1610 				 */
1611 				pkevp->portkev_pid =
1612 				    pfp->pfop_pev->portkev_pid;
1613 				pkevp->portkev_events = op;
1614 				port_send_event(pkevp);
1615 			}
1616 		}
1617 		/*
1618 		 * At this point the pfp has been removed from the vnode's
1619 		 * list its cached port_kevent_t is not on the done queue.
1620 		 * Remove the pfp and free it from the cache.
1621 		 */
1622 		port_pcache_remove_fop(pfcp, pfp);
1623 		mutex_exit(&pfcp->pfc_lock);
1624 	}
1625 }
1626 
1627 /*
1628  * Send the file events to all of the processes watching this
1629  * vnode. In case of hard links, the directory vnode pointer and
1630  * the file name are compared. If the names match, then the specified
1631  * event is sent or else, the FILE_ATTRIB event is sent, This is the
1632  * documented behavior.
1633  */
1634 void
1635 port_fop_sendevent(vnode_t *vp, int events, vnode_t *dvp, char *cname)
1636 {
1637 	port_kevent_t	*pkevp;
1638 	portfop_t	*pfp, *npfp;
1639 	portfop_vp_t	*pvp;
1640 	list_t		tmplist;
1641 	int		removeall = 0;
1642 
1643 	pvp = (portfop_vp_t *)vp->v_fopdata;
1644 	mutex_enter(&pvp->pvp_mutex);
1645 
1646 	/*
1647 	 * Check if the list is empty.
1648 	 *
1649 	 * All entries have been removed by some other thread.
1650 	 * The vnode may be still active and we got called,
1651 	 * but some other thread is in the process of removing the hooks.
1652 	 */
1653 	if (!list_head(&pvp->pvp_pfoplist)) {
1654 		mutex_exit(&pvp->pvp_mutex);
1655 		return;
1656 	}
1657 
1658 	if ((events & (FILE_EXCEPTION))) {
1659 		/*
1660 		 * If it is an event for which we are going to remove
1661 		 * the watches so just move it a temporary list and
1662 		 * release this vnode.
1663 		 */
1664 		list_create(&tmplist, sizeof (portfop_t),
1665 		    offsetof(portfop_t, pfop_node));
1666 
1667 		/*
1668 		 * If it is an UNMOUNT, MOUNTEDOVER or no file name has been
1669 		 * passed for an exception event, all associations need to be
1670 		 * removed.
1671 		 */
1672 		if (dvp == NULL || cname == NULL) {
1673 			removeall = 1;
1674 		}
1675 	}
1676 
1677 	if (!removeall) {
1678 		/*
1679 		 * All the active ones are in the begining of the list.
1680 		 */
1681 		for (pfp = (portfop_t *)list_head(&pvp->pvp_pfoplist);
1682 		    pfp && pfp->pfop_flags & PORT_FOP_ACTIVE; pfp = npfp) {
1683 			int levents = events;
1684 
1685 			npfp = list_next(&pvp->pvp_pfoplist, pfp);
1686 			/*
1687 			 * Hard links case - If the file is being
1688 			 * removed/renamed, and the name matches
1689 			 * the watched file, then it is an EXCEPTION
1690 			 * event or else it will be just a FILE_ATTRIB.
1691 			 */
1692 			if ((events & (FILE_EXCEPTION))) {
1693 				ASSERT(dvp != NULL && cname != NULL);
1694 				if (pfp->pfop_dvp == NULL ||
1695 				    (pfp->pfop_dvp == dvp &&
1696 				    (strcmp(cname, pfp->pfop_cname) == 0))) {
1697 					/*
1698 					 * It is an exception event, move it
1699 					 * to temp list and process it later.
1700 					 * Note we don't set the pfp->pfop_vp
1701 					 * to NULL even thought it has been
1702 					 * removed from the vnode's list. This
1703 					 * pointer is referenced in
1704 					 * port_remove_fop(). The vnode it
1705 					 * self cannot dissapear until this
1706 					 * pfp gets removed and freed.
1707 					 */
1708 					port_fop_listremove(pvp, pfp);
1709 					list_insert_tail(&tmplist, (void *)pfp);
1710 					pfp->pfop_flags  |= PORT_FOP_REMOVING;
1711 					continue;
1712 				} else {
1713 					levents = FILE_ATTRIB;
1714 				}
1715 
1716 			}
1717 
1718 			if (pfp->pfop_events & levents) {
1719 				/*
1720 				 * deactivate and move it to the tail.
1721 				 * If the pfp was active, it cannot be
1722 				 * on the port's done queue.
1723 				 */
1724 				pfp->pfop_flags &= ~PORT_FOP_ACTIVE;
1725 				port_fop_listremove(pvp, pfp);
1726 				port_fop_listinsert_tail(pvp, pfp);
1727 
1728 				pkevp = pfp->pfop_pev;
1729 				pkevp->portkev_events |=
1730 				    (levents & pfp->pfop_events);
1731 				port_send_event(pkevp);
1732 				pfp->pfop_flags |= PORT_FOP_KEV_ONQ;
1733 			}
1734 		}
1735 	}
1736 
1737 
1738 	if ((events & (FILE_EXCEPTION))) {
1739 		if (!removeall) {
1740 			/*
1741 			 * Check the inactive associations and remove them if
1742 			 * the file name matches.
1743 			 */
1744 			for (; pfp; pfp = npfp) {
1745 				npfp = list_next(&pvp->pvp_pfoplist, pfp);
1746 				if (dvp == NULL || cname == NULL ||
1747 				    pfp->pfop_dvp == NULL ||
1748 				    (pfp->pfop_dvp == dvp &&
1749 				    (strcmp(cname, pfp->pfop_cname) == 0))) {
1750 					port_fop_listremove(pvp, pfp);
1751 					list_insert_tail(&tmplist, (void *)pfp);
1752 					pfp->pfop_flags  |= PORT_FOP_REMOVING;
1753 				}
1754 			}
1755 		} else {
1756 			/*
1757 			 * Can be optimized to avoid two pass over this list
1758 			 * by having a flag in the vnode's portfop_vp_t
1759 			 * structure to indicate that it is going away,
1760 			 * Or keep the list short by reusing inactive watches.
1761 			 */
1762 			port_fop_listmove(pvp, &tmplist);
1763 			for (pfp = (portfop_t *)list_head(&tmplist);
1764 			    pfp; pfp = list_next(&tmplist, pfp)) {
1765 				pfp->pfop_flags |= PORT_FOP_REMOVING;
1766 			}
1767 		}
1768 
1769 		/*
1770 		 * Uninstall the fem hooks if there are no more associations.
1771 		 * This will release the pvp mutex.
1772 		 *
1773 		 * Even thought all entries may have been removed,
1774 		 * the vnode itself cannot disappear as there will be a
1775 		 * hold on it due to this call to port_fop_sendevent. This is
1776 		 * important to syncronize with a port_dissociate_fop() call
1777 		 * that may be attempting to remove an object from the vnode's.
1778 		 */
1779 		port_fop_femuninstall(vp);
1780 
1781 		/*
1782 		 * Send exception events and discard the watch entries.
1783 		 */
1784 		port_fop_excep(&tmplist, events);
1785 		list_destroy(&tmplist);
1786 
1787 	} else {
1788 		mutex_exit(&pvp->pvp_mutex);
1789 
1790 		/*
1791 		 * contain the list size.
1792 		 */
1793 		port_fop_trimpfplist(vp);
1794 	}
1795 }
1796 
1797 /*
1798  * Given the file operation, map it to the events types and send.
1799  */
1800 void
1801 port_fop(vnode_t *vp, int op, int retval)
1802 {
1803 	int event = 0;
1804 	/*
1805 	 * deliver events only if the operation was successful.
1806 	 */
1807 	if (retval)
1808 		return;
1809 
1810 	/*
1811 	 * These events occuring on the watched file.
1812 	 */
1813 	if (op & FOP_MODIFIED_MASK) {
1814 		event  = FILE_MODIFIED;
1815 	}
1816 	if (op & FOP_ACCESS_MASK) {
1817 		event  |= FILE_ACCESS;
1818 	}
1819 	if (op & FOP_ATTRIB_MASK) {
1820 		event  |= FILE_ATTRIB;
1821 	}
1822 
1823 	if (event) {
1824 		port_fop_sendevent(vp, 	event, NULL, NULL);
1825 	}
1826 }
1827 
1828 /*
1829  * ----- the unmount filesystem op(fsem) hook.
1830  */
1831 int
1832 port_fop_unmount(fsemarg_t *vf, int flag, cred_t *cr)
1833 {
1834 	vfs_t	*vfsp = (vfs_t *)vf->fa_fnode->fn_available;
1835 	kmutex_t	*mtx;
1836 	portfop_vfs_t	*pvfsp, **ppvfsp;
1837 	portfop_vp_t	*pvp;
1838 	int error;
1839 
1840 	mtx = &(portvfs_hash[PORTFOP_PVFSHASH(vfsp)].pvfshash_mutex);
1841 	ppvfsp = &(portvfs_hash[PORTFOP_PVFSHASH(vfsp)].pvfshash_pvfsp);
1842 	pvfsp = NULL;
1843 	mutex_enter(mtx);
1844 	/*
1845 	 * since this fsem hook is triggered, tit has to be on
1846 	 * the hash list.
1847 	 */
1848 	for (pvfsp = *ppvfsp; pvfsp->pvfs != vfsp; pvfsp = pvfsp->pvfs_next)
1849 	;
1850 
1851 	/*
1852 	 * Indicate that the unmount is in process. Don't remove it yet.
1853 	 * The underlying filesystem unmount routine sets the VFS_UNMOUNTED
1854 	 * flag on the vfs_t structure. But we call the filesystem unmount
1855 	 * routine after removing all the file watches for this filesystem,
1856 	 * otherwise the unmount will fail due to active vnodes.
1857 	 * Meanwhile setting pvfsp->unmount = 1 will prevent any thread
1858 	 * attempting to add a file watch.
1859 	 */
1860 	pvfsp->pvfs_unmount = 1;
1861 	mutex_exit(mtx);
1862 
1863 	/*
1864 	 * uninstall the fsem hooks.
1865 	 */
1866 	(void) fsem_uninstall(vfsp, (fsem_t *)pvfsp->pvfs_fsemp, vfsp);
1867 
1868 	while (pvp = list_head(&pvfsp->pvfs_pvplist)) {
1869 		list_remove(&pvfsp->pvfs_pvplist, pvp);
1870 		/*
1871 		 * This should send an UNMOUNTED event to all the
1872 		 * watched vnode of this filesystem and uninstall
1873 		 * the fem hooks. We release the hold on the vnode here
1874 		 * because port_fop_femuninstall() will not do it if
1875 		 * unmount is in process.
1876 		 */
1877 		port_fop_sendevent(pvp->pvp_vp, UNMOUNTED, NULL, NULL);
1878 		VN_RELE(pvp->pvp_vp);
1879 	}
1880 
1881 	error = vfsnext_unmount(vf, flag, cr);
1882 
1883 	/*
1884 	 * we free the pvfsp after the unmount has been completed.
1885 	 */
1886 	mutex_enter(mtx);
1887 	for (; *ppvfsp && (*ppvfsp)->pvfs != vfsp;
1888 	    ppvfsp = &(*ppvfsp)->pvfs_next)
1889 	;
1890 
1891 	/*
1892 	 * remove and free it.
1893 	 */
1894 	ASSERT(list_head(&pvfsp->pvfs_pvplist) == NULL);
1895 	if (*ppvfsp) {
1896 		pvfsp = *ppvfsp;
1897 		*ppvfsp = pvfsp->pvfs_next;
1898 	}
1899 	mutex_exit(mtx);
1900 	kmem_free(pvfsp, sizeof (portfop_vfs_t));
1901 	return (error);
1902 }
1903 
1904 /*
1905  * ------------------------------file op hooks--------------------------
1906  * The O_TRUNC operation is caught with the VOP_SETATTR(AT_SIZE) call.
1907  */
1908 static int
1909 port_fop_open(femarg_t *vf, int mode, cred_t *cr)
1910 {
1911 	int		retval;
1912 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
1913 
1914 	retval = vnext_open(vf, mode, cr);
1915 	port_fop(vp, FOP_FILE_OPEN, retval);
1916 	return (retval);
1917 }
1918 
1919 static int
1920 port_fop_write(femarg_t *vf, struct uio *uiop, int ioflag, struct cred *cr,
1921     caller_context_t *ct)
1922 {
1923 	int		retval;
1924 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
1925 
1926 	retval =  vnext_write(vf, uiop, ioflag, cr, ct);
1927 	port_fop(vp, FOP_FILE_WRITE, retval);
1928 	return (retval);
1929 }
1930 
1931 static int
1932 port_fop_map(femarg_t *vf, offset_t off, struct as *as, caddr_t *addrp,
1933     size_t len, uchar_t prot, uchar_t maxport, uint_t flags, cred_t *cr)
1934 {
1935 	int		retval;
1936 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
1937 
1938 	retval =  vnext_map(vf, off, as, addrp, len, prot, maxport, flags, cr);
1939 	port_fop(vp, FOP_FILE_MAP, retval);
1940 	return (retval);
1941 }
1942 
1943 static int
1944 port_fop_read(femarg_t *vf, struct uio *uiop, int ioflag, struct cred *cr,
1945     caller_context_t *ct)
1946 {
1947 	int		retval;
1948 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
1949 
1950 	retval =  vnext_read(vf, uiop, ioflag, cr, ct);
1951 	port_fop(vp, FOP_FILE_READ, retval);
1952 	return (retval);
1953 }
1954 
1955 
1956 /*
1957  * AT_SIZE - is for the open(O_TRUNC) case.
1958  */
1959 int
1960 port_fop_setattr(femarg_t *vf, vattr_t *vap, int flags, cred_t *cr,
1961     caller_context_t *ct)
1962 {
1963 	int		retval;
1964 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
1965 	int		events = 0;
1966 
1967 	retval = vnext_setattr(vf, vap, flags, cr, ct);
1968 	if (vap->va_mask & (AT_SIZE|AT_MTIME)) {
1969 		events |= FOP_FILE_SETATTR_MTIME;
1970 	}
1971 	if (vap->va_mask & AT_ATIME) {
1972 		events |= FOP_FILE_SETATTR_ATIME;
1973 	}
1974 	if (vap->va_mask & (AT_SIZE|AT_CTIME)) {
1975 		events |= FOP_FILE_SETATTR_CTIME;
1976 	}
1977 
1978 	port_fop(vp, events, retval);
1979 	return (retval);
1980 }
1981 
1982 int
1983 port_fop_create(femarg_t *vf, char *name, vattr_t *vap, vcexcl_t excl,
1984 	int mode, vnode_t **vpp, cred_t *cr, int flag)
1985 {
1986 	int		retval, got = 1;
1987 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
1988 	vattr_t		vatt, vatt1;
1989 
1990 	/*
1991 	 * If the file already exists, then there will be no change
1992 	 * to the directory. Therefore, we need to compare the
1993 	 * modification time of the directory to determine if the
1994 	 * file was actually created.
1995 	 */
1996 	if (VOP_GETATTR(vp, &vatt, 0, CRED())) {
1997 		got = 0;
1998 	}
1999 	retval = vnext_create(vf, name, vap, excl, mode, vpp, cr, flag);
2000 
2001 	if (got && !VOP_GETATTR(vp, &vatt1, 0, CRED())) {
2002 		if ((vatt1.va_mtime.tv_sec > vatt.va_mtime.tv_sec ||
2003 		    (vatt1.va_mtime.tv_sec = vatt.va_mtime.tv_sec &&
2004 		    vatt1.va_mtime.tv_nsec > vatt.va_mtime.tv_nsec))) {
2005 			/*
2006 			 * File was created.
2007 			 */
2008 			port_fop(vp, FOP_FILE_CREATE, retval);
2009 		}
2010 	}
2011 	return (retval);
2012 }
2013 
2014 int
2015 port_fop_remove(femarg_t *vf, char *nm, cred_t *cr)
2016 {
2017 	int		retval;
2018 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
2019 
2020 	retval = vnext_remove(vf, nm, cr);
2021 	port_fop(vp, FOP_FILE_REMOVE, retval);
2022 	return (retval);
2023 }
2024 
2025 int
2026 port_fop_link(femarg_t *vf, vnode_t *svp, char *tnm, cred_t *cr)
2027 {
2028 	int		retval;
2029 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
2030 
2031 	retval = vnext_link(vf, svp, tnm, cr);
2032 	port_fop(vp, FOP_FILE_LINK, retval);
2033 	return (retval);
2034 }
2035 
2036 /*
2037  * Rename operation is allowed only when from and to directories are
2038  * on the same filesystem. This is checked in vn_rename().
2039  * The target directory is notified thru a VNEVENT by the filesystem
2040  * if the source dir != target dir.
2041  */
2042 int
2043 port_fop_rename(femarg_t *vf, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr)
2044 {
2045 	int		retval;
2046 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
2047 
2048 	retval = vnext_rename(vf, snm, tdvp, tnm, cr);
2049 	port_fop(vp, FOP_FILE_RENAMESRC, retval);
2050 	return (retval);
2051 }
2052 
2053 int
2054 port_fop_mkdir(femarg_t *vf, char *dirname, vattr_t *vap, vnode_t **vpp,
2055 	cred_t *cr)
2056 {
2057 	int		retval;
2058 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
2059 
2060 	retval = vnext_mkdir(vf, dirname, vap, vpp, cr);
2061 	port_fop(vp, FOP_FILE_MKDIR, retval);
2062 	return (retval);
2063 }
2064 
2065 int
2066 port_fop_rmdir(femarg_t *vf, char *nm, vnode_t *cdir, cred_t *cr)
2067 {
2068 	int		retval;
2069 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
2070 
2071 	retval = vnext_rmdir(vf, nm, cdir, cr);
2072 	port_fop(vp, FOP_FILE_RMDIR, retval);
2073 	return (retval);
2074 }
2075 
2076 int
2077 port_fop_readdir(femarg_t *vf, uio_t *uiop, cred_t *cr, int *eofp)
2078 {
2079 	int		retval;
2080 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
2081 
2082 	retval = vnext_readdir(vf, uiop, cr, eofp);
2083 	port_fop(vp, FOP_FILE_READDIR, retval);
2084 	return (retval);
2085 }
2086 
2087 int
2088 port_fop_symlink(femarg_t *vf, char *linkname, vattr_t *vap, char *target,
2089 	cred_t *cr)
2090 {
2091 	int		retval;
2092 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
2093 
2094 	retval = vnext_symlink(vf, linkname, vap, target, cr);
2095 	port_fop(vp, FOP_FILE_SYMLINK, retval);
2096 	return (retval);
2097 }
2098 
2099 /*
2100  * acl, facl call this.
2101  */
2102 int
2103 port_fop_setsecattr(femarg_t *vf, vsecattr_t *vsap, int flags, cred_t *cr)
2104 {
2105 	int	retval;
2106 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
2107 	retval = vnext_setsecattr(vf, vsap, flags, cr);
2108 	port_fop(vp, FOP_FILE_SETSECATTR, retval);
2109 	return (retval);
2110 }
2111 
2112 /*
2113  * these are events on the watched file/directory
2114  */
2115 int
2116 port_fop_vnevent(femarg_t *vf, vnevent_t vnevent, vnode_t *dvp, char *name)
2117 {
2118 	vnode_t		*vp = (vnode_t *)vf->fa_fnode->fn_available;
2119 
2120 
2121 	switch (vnevent) {
2122 	case	VE_RENAME_SRC:
2123 			port_fop_sendevent(vp, FILE_RENAME_FROM, dvp, name);
2124 		break;
2125 	case	VE_RENAME_DEST:
2126 			port_fop_sendevent(vp, FILE_RENAME_TO, dvp, name);
2127 		break;
2128 	case	VE_REMOVE:
2129 			port_fop_sendevent(vp, FILE_DELETE, dvp, name);
2130 		break;
2131 	case	VE_RMDIR:
2132 			port_fop_sendevent(vp, FILE_DELETE, dvp, name);
2133 		break;
2134 	case	VE_CREATE:
2135 			port_fop_sendevent(vp, FILE_MODIFIED|FILE_ATTRIB,
2136 			    NULL, NULL);
2137 		break;
2138 	case	VE_LINK:
2139 			port_fop_sendevent(vp, FILE_ATTRIB, NULL, NULL);
2140 		break;
2141 
2142 	case	VE_RENAME_DEST_DIR:
2143 			port_fop_sendevent(vp, FILE_MODIFIED|FILE_ATTRIB,
2144 			    NULL, NULL);
2145 		break;
2146 
2147 	case	VE_MOUNTEDOVER:
2148 			port_fop_sendevent(vp, MOUNTEDOVER, NULL, NULL);
2149 		break;
2150 	default:
2151 		break;
2152 	}
2153 	return (vnext_vnevent(vf, vnevent, dvp, name));
2154 }
2155