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