2 * Copyright (c) 2004-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/event.h> // for kqueue related stuff
32 #include <sys/fsevents.h>
35 #include <sys/namei.h>
36 #include <sys/filedesc.h>
37 #include <sys/kernel.h>
38 #include <sys/file_internal.h>
40 #include <sys/vnode_internal.h>
41 #include <sys/mount_internal.h>
42 #include <sys/proc_internal.h>
43 #include <sys/kauth.h>
45 #include <sys/malloc.h>
46 #include <sys/dirent.h>
48 #include <sys/sysctl.h>
50 #include <machine/cons.h>
51 #include <miscfs/specfs/specdev.h>
52 #include <miscfs/devfs/devfs.h>
53 #include <sys/filio.h>
54 #include <kern/locks.h>
55 #include <libkern/OSAtomic.h>
56 #include <kern/zalloc.h>
57 #include <mach/mach_time.h>
58 #include <kern/thread_call.h>
59 #include <kern/clock.h>
61 #include <security/audit/audit.h>
62 #include <bsm/audit_kevents.h>
66 typedef struct kfs_event
{
67 LIST_ENTRY(kfs_event
) kevent_list
;
68 int16_t type
; // type code of this event
69 u_int16_t flags
, // per-event flags
70 len
; // the length of the path in "str"
71 int32_t refcount
; // number of clients referencing this
72 pid_t pid
; // pid of the process that did the op
74 uint64_t abstime
; // when this event happened (mach_absolute_time())
83 struct kfs_event
*dest
; // if this is a two-file op
86 // flags for the flags field
87 #define KFSE_COMBINED_EVENTS 0x0001
88 #define KFSE_CONTAINS_DROPPED_EVENTS 0x0002
89 #define KFSE_RECYCLED_EVENT 0x0004
90 #define KFSE_BEING_CREATED 0x0008
92 LIST_HEAD(kfse_list
, kfs_event
) kfse_list_head
= LIST_HEAD_INITIALIZER(x
);
93 int num_events_outstanding
= 0;
94 int num_pending_rename
= 0;
97 struct fsevent_handle
;
99 typedef struct fs_event_watcher
{
100 int8_t *event_list
; // the events we're interested in
102 dev_t
*devices_not_to_watch
; // report events from devices not in this list
103 uint32_t num_devices
;
105 kfs_event
**event_queue
;
106 int32_t eventq_size
; // number of event pointers in queue
108 int32_t rd
; // read index into the event_queue
109 int32_t wr
; // write index into the event_queue
112 uint32_t num_dropped
;
113 uint64_t max_event_id
;
114 struct fsevent_handle
*fseh
;
117 // fs_event_watcher flags
118 #define WATCHER_DROPPED_EVENTS 0x0001
119 #define WATCHER_CLOSING 0x0002
120 #define WATCHER_WANTS_COMPACT_EVENTS 0x0004
121 #define WATCHER_WANTS_EXTENDED_INFO 0x0008
124 #define MAX_WATCHERS 8
125 static fs_event_watcher
*watcher_table
[MAX_WATCHERS
];
128 #define MAX_KFS_EVENTS 4096
130 // we allocate kfs_event structures out of this zone
131 static zone_t event_zone
;
132 static int fs_event_init
= 0;
135 // this array records whether anyone is interested in a
136 // particular type of event. if no one is, we bail out
137 // early from the event delivery
139 static int16_t fs_event_type_watchers
[FSE_MAX_EVENTS
];
141 static int watcher_add_event(fs_event_watcher
*watcher
, kfs_event
*kfse
);
142 static void fsevents_wakeup(fs_event_watcher
*watcher
);
147 static lck_grp_attr_t
* fsevent_group_attr
;
148 static lck_attr_t
* fsevent_lock_attr
;
149 static lck_grp_t
* fsevent_mutex_group
;
151 static lck_grp_t
* fsevent_rw_group
;
153 static lck_rw_t event_handling_lock
; // handles locking for event manipulation and recycling
154 static lck_mtx_t watch_table_lock
;
155 static lck_mtx_t event_buf_lock
;
156 static lck_mtx_t event_writer_lock
;
159 /* Explicitly declare qsort so compiler doesn't complain */
160 __private_extern__
void qsort(
164 int (*)(const void *, const void *));
168 fsevents_internal_init(void)
172 if (fs_event_init
++ != 0) {
176 for(i
=0; i
< FSE_MAX_EVENTS
; i
++) {
177 fs_event_type_watchers
[i
] = 0;
180 memset(watcher_table
, 0, sizeof(watcher_table
));
182 fsevent_lock_attr
= lck_attr_alloc_init();
183 fsevent_group_attr
= lck_grp_attr_alloc_init();
184 fsevent_mutex_group
= lck_grp_alloc_init("fsevent-mutex", fsevent_group_attr
);
185 fsevent_rw_group
= lck_grp_alloc_init("fsevent-rw", fsevent_group_attr
);
187 lck_mtx_init(&watch_table_lock
, fsevent_mutex_group
, fsevent_lock_attr
);
188 lck_mtx_init(&event_buf_lock
, fsevent_mutex_group
, fsevent_lock_attr
);
189 lck_mtx_init(&event_writer_lock
, fsevent_mutex_group
, fsevent_lock_attr
);
191 lck_rw_init(&event_handling_lock
, fsevent_rw_group
, fsevent_lock_attr
);
193 event_zone
= zinit(sizeof(kfs_event
),
194 MAX_KFS_EVENTS
* sizeof(kfs_event
),
195 MAX_KFS_EVENTS
* sizeof(kfs_event
),
197 if (event_zone
== NULL
) {
198 printf("fsevents: failed to initialize the event zone.\n");
201 if (zfill(event_zone
, MAX_KFS_EVENTS
) != MAX_KFS_EVENTS
) {
202 printf("fsevents: failed to pre-fill the event zone.\n");
205 // mark the zone as exhaustible so that it will not
206 // ever grow beyond what we initially filled it with
207 zone_change(event_zone
, Z_EXHAUST
, TRUE
);
208 zone_change(event_zone
, Z_COLLECT
, FALSE
);
212 lock_watch_table(void)
214 lck_mtx_lock(&watch_table_lock
);
218 unlock_watch_table(void)
220 lck_mtx_unlock(&watch_table_lock
);
224 lock_fs_event_list(void)
226 lck_mtx_lock(&event_buf_lock
);
230 unlock_fs_event_list(void)
232 lck_mtx_unlock(&event_buf_lock
);
236 static void release_event_ref(kfs_event
*kfse
);
239 watcher_cares_about_dev(fs_event_watcher
*watcher
, dev_t dev
)
243 // if devices_not_to_watch is NULL then we care about all
244 // events from all devices
245 if (watcher
->devices_not_to_watch
== NULL
) {
249 for(i
=0; i
< watcher
->num_devices
; i
++) {
250 if (dev
== watcher
->devices_not_to_watch
[i
]) {
251 // found a match! that means we do not
252 // want events from this device.
257 // if we're here it's not in the devices_not_to_watch[]
258 // list so that means we do care about it
264 need_fsevent(int type
, vnode_t vp
)
266 if (type
>= 0 && type
< FSE_MAX_EVENTS
&& fs_event_type_watchers
[type
] == 0)
269 // events in /dev aren't really interesting...
270 if (vp
->v_tag
== VT_DEVFS
) {
278 prefix_match_len(const char *str1
, const char *str2
)
282 while(*str1
&& *str2
&& *str1
== *str2
) {
288 if (*str1
== '\0' && *str2
== '\0') {
296 struct history_item
{
298 kfs_event
*oldest_kfse
;
303 compare_history_items(const void *_a
, const void *_b
)
305 const struct history_item
*a
= (const struct history_item
*)_a
;
306 const struct history_item
*b
= (const struct history_item
*)_b
;
308 // we want a descending order
309 return (b
->counter
- a
->counter
);
312 #define is_throw_away(x) ((x) == FSE_STAT_CHANGED || (x) == FSE_CONTENT_MODIFIED)
315 // Ways that an event can be reused:
317 // "combined" events mean that there were two events for
318 // the same vnode or path and we're combining both events
319 // into a single event. The primary event gets a bit that
320 // marks it as having been combined. The secondary event
321 // is essentially dropped and the kfse structure reused.
323 // "collapsed" means that multiple events below a given
324 // directory are collapsed into a single event. in this
325 // case, the directory that we collapse into and all of
326 // its children must be re-scanned.
328 // "recycled" means that we're completely blowing away
329 // the event since there are other events that have info
330 // about the same vnode or path (and one of those other
331 // events will be marked as combined or collapsed as
334 #define KFSE_COMBINED 0x0001
335 #define KFSE_COLLAPSED 0x0002
336 #define KFSE_RECYCLED 0x0004
339 int num_combined_events
= 0;
340 int num_added_to_parent
= 0;
341 int num_parent_switch
= 0;
342 int num_recycled_rename
= 0;
345 // NOTE: you must call lock_fs_event_list() before calling
349 find_an_event(const char *str
, int len
, kfs_event
*do_not_reuse
, int *reuse_type
, int *longest_match_len
)
351 kfs_event
*kfse
, *best_kfse
=NULL
;
353 // this seems to be enough to find most duplicate events for the same vnode
354 #define MAX_HISTORY 12
355 struct history_item history
[MAX_HISTORY
];
358 *longest_match_len
= 0;
361 memset(history
, 0, sizeof(history
));
364 // now walk the list of events and try to find the best match
365 // for this event. if we have a vnode, we look for an event
366 // that already references the vnode. if we don't find one
367 // we'll also take the parent of this vnode (in which case it
368 // will be marked as having dropped events within it).
370 // if we have a string we look for the longest match on the
374 LIST_FOREACH(kfse
, &kfse_list_head
, kevent_list
) {
378 // don't look at events that are still in the process of being
379 // created, have a null vnode ptr or rename/exchange events.
381 if ( (kfse
->flags
& KFSE_BEING_CREATED
) || kfse
->type
== FSE_RENAME
|| kfse
->type
== FSE_EXCHANGE
) {
387 if (kfse
->len
!= 0 && kfse
->str
!= NULL
) {
388 match_len
= prefix_match_len(str
, kfse
->str
);
389 if (match_len
> *longest_match_len
) {
391 *longest_match_len
= match_len
;
396 if (kfse
== do_not_reuse
) {
400 for(i
=0; i
< MAX_HISTORY
; i
++) {
401 if (history
[i
].kfse
== NULL
) {
406 // do a quick check to see if we've got two simple events
407 // that we can cheaply combine. if the event we're looking
408 // at and one of the events in the history table are for the
409 // same path then we'll just mark the newer event as combined
410 // and recyle the older event.
412 if (history
[i
].kfse
->str
== kfse
->str
) {
414 OSBitOrAtomic16(KFSE_COMBINED_EVENTS
, &kfse
->flags
);
415 *reuse_type
= KFSE_RECYCLED
;
416 history
[i
].kfse
->flags
|= KFSE_RECYCLED_EVENT
;
417 return history
[i
].kfse
;
421 if (i
< MAX_HISTORY
&& history
[i
].kfse
== NULL
) {
422 history
[i
].kfse
= kfse
;
423 history
[i
].counter
= 1;
424 } else if (i
>= MAX_HISTORY
) {
425 qsort(history
, MAX_HISTORY
, sizeof(struct history_item
), compare_history_items
);
427 // pluck off the lowest guy if he's only got a count of 1
428 if (history
[MAX_HISTORY
-1].counter
== 1) {
429 history
[MAX_HISTORY
-1].kfse
= kfse
;
435 if (str
!= NULL
&& best_kfse
) {
436 if (*longest_match_len
<= 1) {
437 // if the best match we had was "/" then basically we're toast...
438 *longest_match_len
= 0;
440 } else if (*longest_match_len
!= len
) {
441 OSBitOrAtomic16(KFSE_CONTAINS_DROPPED_EVENTS
, &best_kfse
->flags
);
442 *reuse_type
= KFSE_COLLAPSED
;
444 OSBitOrAtomic16(KFSE_COMBINED_EVENTS
, &best_kfse
->flags
);
445 *reuse_type
= KFSE_COMBINED
;
453 static struct timeval last_print
;
456 // These variables are used to track coalescing multiple identical
457 // events for the same vnode/pathname. If we get the same event
458 // type and same vnode/pathname as the previous event, we just drop
459 // the event since it's superfluous. This improves some micro-
460 // benchmarks considerably and actually has a real-world impact on
461 // tests like a Finder copy where multiple stat-changed events can
464 static int last_event_type
=-1;
465 static void *last_ptr
=NULL
;
466 static char last_str
[MAXPATHLEN
];
467 static int last_nlen
=0;
468 static int last_vid
=-1;
469 static uint64_t last_coalesced_time
=0;
470 static void *last_event_ptr
=NULL
;
471 int last_coalesced
= 0;
472 static mach_timebase_info_data_t sTimebaseInfo
= { 0, 0 };
476 add_fsevent(int type
, vfs_context_t ctx
, ...)
478 struct proc
*p
= vfs_context_proc(ctx
);
479 int i
, arg_type
, skip_init
=0, longest_match_len
, ret
;
480 kfs_event
*kfse
, *kfse_dest
=NULL
, *cur
;
481 fs_event_watcher
*watcher
;
483 int error
= 0, did_alloc
=0, need_event_unlock
= 0;
485 uint64_t now
, elapsed
;
494 // ignore bogus event types..
495 if (type
< 0 || type
>= FSE_MAX_EVENTS
) {
499 // if no one cares about this type of event, bail out
500 if (fs_event_type_watchers
[type
] == 0) {
506 now
= mach_absolute_time();
508 // find a free event and snag it for our use
509 // NOTE: do not do anything that would block until
510 // the lock is dropped.
511 lock_fs_event_list();
514 // check if this event is identical to the previous one...
515 // (as long as it's not an event type that can never be the
516 // same as a previous event)
518 if (type
!= FSE_CREATE_FILE
&& type
!= FSE_DELETE
&& type
!= FSE_RENAME
&& type
!= FSE_EXCHANGE
&& type
!= FSE_CHOWN
) {
520 int vid
=0, was_str
=0, nlen
=0;
522 for(arg_type
=va_arg(ap
, int32_t); arg_type
!= FSE_ARG_DONE
; arg_type
=va_arg(ap
, int32_t)) {
524 case FSE_ARG_VNODE
: {
525 ptr
= va_arg(ap
, void *);
526 vid
= vnode_vid((struct vnode
*)ptr
);
530 case FSE_ARG_STRING
: {
531 nlen
= va_arg(ap
, int32_t);
532 ptr
= va_arg(ap
, void *);
542 if ( sTimebaseInfo
.denom
== 0 ) {
543 (void) clock_timebase_info(&sTimebaseInfo
);
546 elapsed
= (now
- last_coalesced_time
);
547 if (sTimebaseInfo
.denom
!= sTimebaseInfo
.numer
) {
548 if (sTimebaseInfo
.denom
== 1) {
549 elapsed
*= sTimebaseInfo
.numer
;
551 // this could overflow... the worst that will happen is that we'll
552 // send (or not send) an extra event so I'm not going to worry about
553 // doing the math right like dtrace_abs_to_nano() does.
554 elapsed
= (elapsed
* sTimebaseInfo
.numer
) / (uint64_t)sTimebaseInfo
.denom
;
558 if (type
== last_event_type
559 && (elapsed
< 1000000000)
561 ((vid
&& vid
== last_vid
&& last_ptr
== ptr
)
563 (last_str
[0] && last_nlen
== nlen
&& ptr
&& strcmp(last_str
, ptr
) == 0))
567 unlock_fs_event_list();
574 strlcpy(last_str
, ptr
, sizeof(last_str
));
578 last_event_type
= type
;
579 last_coalesced_time
= now
;
585 kfse
= zalloc_noblock(event_zone
);
586 if (kfse
&& (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
)) {
587 kfse_dest
= zalloc_noblock(event_zone
);
588 if (kfse_dest
== NULL
) {
590 zfree(event_zone
, kfse
);
596 if (kfse
== NULL
) { // yikes! no free events
601 // Figure out what kind of reference we have to the
602 // file in this event. This helps us find an event
603 // to combine/collapse into to make room.
605 // If we have a rename or exchange event then we
606 // don't want to go through the normal path, we
607 // want to "steal" an event instead (which is what
608 // find_an_event() will do if str is null).
610 arg_type
= va_arg(ap
, int32_t);
611 if (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
) {
613 } else if (arg_type
== FSE_ARG_STRING
) {
614 len
= va_arg(ap
, int32_t);
615 str
= va_arg(ap
, char *);
616 } else if (arg_type
== FSE_ARG_VNODE
) {
619 vp
= va_arg(ap
, struct vnode
*);
620 pathbuff
= get_pathbuff();
621 pathbuff_len
= MAXPATHLEN
;
622 if (vn_getpath(vp
, pathbuff
, &pathbuff_len
) != 0 || pathbuff
[0] == '\0') {
623 release_pathbuff(pathbuff
);
632 // This will go through all events and find one that we
633 // can combine with (hopefully), or "collapse" into (i.e
634 // it has the same parent) or in the worst case we have
635 // to "recycle" an event which means that it will combine
636 // two other events and return us the now unused event.
637 // failing all that, find_an_event() could still return
638 // null and if it does then we have a catastrophic dropped
641 kfse
= find_an_event(str
, len
, NULL
, &reuse_type
, &longest_match_len
);
646 unlock_fs_event_list();
649 for(i
=0; i
< MAX_WATCHERS
; i
++) {
650 watcher
= watcher_table
[i
];
651 if (watcher
== NULL
) {
655 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
656 fsevents_wakeup(watcher
);
658 unlock_watch_table();
661 struct timeval current_tv
;
665 // only print a message at most once every 5 seconds
666 microuptime(¤t_tv
);
667 if ((current_tv
.tv_sec
- last_print
.tv_sec
) > 10) {
669 void *junkptr
=zalloc_noblock(event_zone
), *listhead
=kfse_list_head
.lh_first
;
671 printf("add_fsevent: event queue is full! dropping events (num dropped events: %d; num events outstanding: %d).\n", num_dropped
, num_events_outstanding
);
672 printf("add_fsevent: kfse_list head %p ; num_pending_rename %d\n", listhead
, num_pending_rename
);
673 printf("add_fsevent: zalloc sez: %p\n", junkptr
);
674 printf("add_fsevent: event_zone info: %d 0x%x\n", ((int *)event_zone
)[0], ((int *)event_zone
)[1]);
675 for(ii
=0; ii
< MAX_WATCHERS
; ii
++) {
676 if (watcher_table
[ii
] == NULL
) {
680 printf("add_fsevent: watcher %p: num dropped %d rd %4d wr %4d q_size %4d flags 0x%x\n",
681 watcher_table
[ii
], watcher_table
[ii
]->num_dropped
,
682 watcher_table
[ii
]->rd
, watcher_table
[ii
]->wr
,
683 watcher_table
[ii
]->eventq_size
, watcher_table
[ii
]->flags
);
686 last_print
= current_tv
;
688 zfree(event_zone
, junkptr
);
694 release_pathbuff(pathbuff
);
701 if ((type
== FSE_RENAME
|| type
== FSE_EXCHANGE
) && reuse_type
!= KFSE_RECYCLED
) {
702 panic("add_fsevent: type == %d but reuse type == %d!\n", type
, reuse_type
);
703 } else if ((kfse
->type
== FSE_RENAME
|| kfse
->type
== FSE_EXCHANGE
) && kfse
->dest
== NULL
) {
704 panic("add_fsevent: bogus kfse %p (type %d, but dest is NULL)\n", kfse
, kfse
->type
);
705 } else if (kfse
->type
== FSE_RENAME
|| kfse
->type
== FSE_EXCHANGE
) {
706 panic("add_fsevent: we should never re-use rename events (kfse %p reuse type %d)!\n", kfse
, reuse_type
);
709 if (reuse_type
== KFSE_COLLAPSED
) {
711 const char *tmp_ptr
, *new_str
;
714 // if we collapsed and have a string we have to chop off the
715 // tail component of the pathname to get the parent.
717 // NOTE: it is VERY IMPORTANT that we leave the trailing slash
718 // on the pathname. user-level code depends on this.
720 if (str
[0] == '\0' || longest_match_len
<= 1) {
721 printf("add_fsevent: strange state (str %s / longest_match_len %d)\n", str
, longest_match_len
);
722 if (longest_match_len
< 0) {
723 panic("add_fsevent: longest_match_len %d\n", longest_match_len
);
726 // chop off the tail component if it's not the
727 // first character...
728 if (longest_match_len
> 1) {
729 str
[longest_match_len
] = '\0';
730 } else if (longest_match_len
== 0) {
731 longest_match_len
= 1;
734 new_str
= vfs_addname(str
, longest_match_len
, 0, 0);
735 if (new_str
== NULL
|| new_str
[0] == '\0') {
736 panic("add_fsevent: longest match is strange (new_str %p).\n", new_str
);
739 lck_rw_lock_exclusive(&event_handling_lock
);
741 kfse
->len
= longest_match_len
;
749 lck_rw_unlock_exclusive(&event_handling_lock
);
751 vfs_removename(tmp_ptr
);
753 panic("add_fsevent: don't have a vnode or a string pointer (kfse %p)\n", kfse
);
757 if (reuse_type
== KFSE_RECYCLED
&& (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
)) {
759 // if we're recycling this kfse and we have a rename or
760 // exchange event then we need to also get an event for
764 // only happens if we allocated one but then failed
765 // for kfse_dest (and thus free'd the first one we
767 kfse_dest
= zalloc_noblock(event_zone
);
768 if (kfse_dest
!= NULL
) {
769 memset(kfse_dest
, 0, sizeof(kfs_event
));
770 kfse_dest
->refcount
= 1;
771 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse_dest
->flags
);
777 if (kfse_dest
== NULL
) {
778 int dest_reuse_type
, dest_match_len
;
780 kfse_dest
= find_an_event(NULL
, 0, kfse
, &dest_reuse_type
, &dest_match_len
);
782 if (kfse_dest
== NULL
) {
783 // nothing we can do... gotta bail out
787 if (dest_reuse_type
!= KFSE_RECYCLED
) {
788 panic("add_fsevent: type == %d but dest_reuse type == %d!\n", type
, dest_reuse_type
);
795 // Here we check for some fast-path cases so that we can
796 // jump over the normal initialization and just get on
797 // with delivering the event. These cases are when we're
798 // combining/collapsing an event and so basically there is
799 // no more work to do (aside from a little book-keeping)
801 if (str
&& kfse
->len
!= 0) {
803 OSAddAtomic(1, &kfse
->refcount
);
806 if (reuse_type
== KFSE_COMBINED
) {
807 num_combined_events
++;
808 } else if (reuse_type
== KFSE_COLLAPSED
) {
809 num_added_to_parent
++;
811 } else if (reuse_type
!= KFSE_RECYCLED
) {
812 panic("add_fsevent: I'm so confused! (reuse_type %d str %p kfse->len %d)\n",
813 reuse_type
, str
, kfse
->len
);
820 if (kfse
->refcount
< 1) {
821 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__
, kfse
->refcount
);
824 last_event_ptr
= kfse
;
825 unlock_fs_event_list();
826 goto normal_delivery
;
828 } else if (reuse_type
== KFSE_RECYCLED
|| reuse_type
== KFSE_COMBINED
) {
831 // If we're here we have to clear out the kfs_event(s)
832 // that we were given by find_an_event() and set it
833 // up to be re-filled in by the normal code path.
837 need_event_unlock
= 1;
838 lck_rw_lock_exclusive(&event_handling_lock
);
840 OSAddAtomic(1, &kfse
->refcount
);
842 if (kfse
->refcount
< 1) {
843 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__
, kfse
->refcount
);
846 if (kfse
->len
== 0) {
847 panic("%s:%d: no more fref.vp\n", __FILE__
, __LINE__
);
848 // vnode_rele_ext(kfse->fref.vp, O_EVTONLY, 0);
850 vfs_removename(kfse
->str
);
855 if (kfse
->kevent_list
.le_prev
!= NULL
) {
856 num_events_outstanding
--;
857 if (kfse
->type
== FSE_RENAME
) {
858 num_pending_rename
--;
860 LIST_REMOVE(kfse
, kevent_list
);
861 memset(&kfse
->kevent_list
, 0, sizeof(kfse
->kevent_list
));
864 kfse
->flags
= 0 | KFSE_RECYCLED_EVENT
;
867 OSAddAtomic(1, &kfse_dest
->refcount
);
868 kfse_dest
->flags
= 0 | KFSE_RECYCLED_EVENT
;
870 if (did_alloc
== 0) {
871 if (kfse_dest
->len
== 0) {
872 panic("%s:%d: no more fref.vp\n", __FILE__
, __LINE__
);
873 // vnode_rele_ext(kfse_dest->fref.vp, O_EVTONLY, 0);
875 vfs_removename(kfse_dest
->str
);
878 kfse_dest
->str
= NULL
;
880 if (kfse_dest
->kevent_list
.le_prev
!= NULL
) {
881 num_events_outstanding
--;
882 LIST_REMOVE(kfse_dest
, kevent_list
);
883 memset(&kfse_dest
->kevent_list
, 0, sizeof(kfse_dest
->kevent_list
));
886 if (kfse_dest
->dest
) {
887 panic("add_fsevent: should never recycle a rename event! kfse %p\n", kfse
);
892 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse
->flags
);
894 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse_dest
->flags
);
897 goto process_normally
;
901 if (reuse_type
!= 0) {
902 panic("fsevents: we have a reuse_type (%d) but are about to clear out kfse %p\n", reuse_type
, kfse
);
906 // we only want to do this for brand new events, not
907 // events which have been recycled.
909 memset(kfse
, 0, sizeof(kfs_event
));
911 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse
->flags
);
914 last_event_ptr
= kfse
;
917 kfse
->pid
= p
->p_pid
;
918 if (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
) {
919 if (need_event_unlock
== 0) {
920 memset(kfse_dest
, 0, sizeof(kfs_event
));
921 kfse_dest
->refcount
= 1;
922 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse_dest
->flags
);
924 kfse_dest
->type
= type
;
925 kfse_dest
->pid
= p
->p_pid
;
926 kfse_dest
->abstime
= now
;
928 kfse
->dest
= kfse_dest
;
931 num_events_outstanding
++;
932 if (kfse
->type
== FSE_RENAME
) {
933 num_pending_rename
++;
935 LIST_INSERT_HEAD(&kfse_list_head
, kfse
, kevent_list
);
937 if (kfse
->refcount
< 1) {
938 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__
, kfse
->refcount
);
941 unlock_fs_event_list(); // at this point it's safe to unlock
944 // now process the arguments passed in and copy them into
947 if (need_event_unlock
== 0) {
948 lck_rw_lock_shared(&event_handling_lock
);
952 for(arg_type
=va_arg(ap
, int32_t); arg_type
!= FSE_ARG_DONE
; arg_type
=va_arg(ap
, int32_t))
955 case FSE_ARG_VNODE
: {
956 // this expands out into multiple arguments to the client
958 struct vnode_attr va
;
960 if (kfse
->str
!= NULL
) {
964 vp
= va_arg(ap
, struct vnode
*);
966 panic("add_fsevent: you can't pass me a NULL vnode ptr (type %d)!\n",
971 VATTR_WANTED(&va
, va_fsid
);
972 VATTR_WANTED(&va
, va_fileid
);
973 VATTR_WANTED(&va
, va_mode
);
974 VATTR_WANTED(&va
, va_uid
);
975 VATTR_WANTED(&va
, va_gid
);
976 if ((ret
= vnode_getattr(vp
, &va
, vfs_context_kernel())) != 0) {
977 // printf("add_fsevent: failed to getattr on vp %p (%d)\n", cur->fref.vp, ret);
980 if (need_event_unlock
== 0) {
981 // then we only grabbed it shared
982 lck_rw_unlock_shared(&event_handling_lock
);
987 cur
->dev
= dev
= (dev_t
)va
.va_fsid
;
988 cur
->ino
= (ino64_t
)va
.va_fileid
;
989 cur
->mode
= (int32_t)vnode_vttoif(vnode_vtype(vp
)) | va
.va_mode
;
990 cur
->uid
= va
.va_uid
;
991 cur
->gid
= va
.va_gid
;
993 // if we haven't gotten the path yet, get it.
994 if (pathbuff
== NULL
) {
995 pathbuff
= get_pathbuff();
996 pathbuff_len
= MAXPATHLEN
;
999 if ((ret
= vn_getpath(vp
, pathbuff
, &pathbuff_len
)) != 0 || pathbuff
[0] == '\0') {
1000 struct vnode
*orig_vp
= vp
;
1002 if (ret
!= ENOSPC
) {
1003 printf("add_fsevent: unable to get path for vp %p (%s; ret %d; type %d)\n",
1004 vp
, vp
->v_name
? vp
->v_name
: "-UNKNOWN-FILE", ret
, type
);
1007 cur
->flags
|= KFSE_CONTAINS_DROPPED_EVENTS
;
1010 if (vp
->v_parent
!= NULL
) {
1012 } else if (vp
->v_mount
) {
1013 strlcpy(pathbuff
, vp
->v_mount
->mnt_vfsstat
.f_mntonname
, MAXPATHLEN
);
1023 pathbuff_len
= MAXPATHLEN
;
1024 ret
= vn_getpath(vp
, pathbuff
, &pathbuff_len
);
1025 } while (ret
== ENOSPC
);
1027 if (ret
!= 0 || vp
== NULL
) {
1028 printf("add_fsevent: unabled to get a path for vp %p. dropping the event.\n", orig_vp
);
1030 if (need_event_unlock
== 0) {
1031 // then we only grabbed it shared
1032 lck_rw_unlock_shared(&event_handling_lock
);
1039 // store the path by adding it to the global string table
1040 cur
->len
= pathbuff_len
;
1041 cur
->str
= vfs_addname(pathbuff
, pathbuff_len
, 0, 0);
1042 if (cur
->str
== NULL
|| cur
->str
[0] == '\0') {
1043 panic("add_fsevent: was not able to add path %s to event %p.\n", pathbuff
, cur
);
1046 release_pathbuff(pathbuff
);
1052 case FSE_ARG_FINFO
: {
1055 fse
= va_arg(ap
, fse_info
*);
1057 cur
->dev
= dev
= (dev_t
)fse
->dev
;
1058 cur
->ino
= (ino64_t
)fse
->ino
;
1059 cur
->mode
= (int32_t)fse
->mode
;
1060 cur
->uid
= (uid_t
)fse
->uid
;
1061 cur
->gid
= (uid_t
)fse
->gid
;
1062 // if it's a hard-link and this is the last link, flag it
1063 if ((fse
->mode
& FSE_MODE_HLINK
) && fse
->nlink
== 0) {
1064 cur
->mode
|= FSE_MODE_LAST_HLINK
;
1066 if (cur
->mode
& FSE_TRUNCATED_PATH
) {
1067 cur
->flags
|= KFSE_CONTAINS_DROPPED_EVENTS
;
1068 cur
->mode
&= ~FSE_TRUNCATED_PATH
;
1073 case FSE_ARG_STRING
:
1074 if (kfse
->str
!= NULL
) {
1078 cur
->len
= (int16_t)(va_arg(ap
, int32_t) & 0x7fff);
1079 if (cur
->len
>= 1) {
1080 cur
->str
= vfs_addname(va_arg(ap
, char *), cur
->len
, 0, 0);
1082 printf("add_fsevent: funny looking string length: %d\n", (int)cur
->len
);
1084 cur
->str
= vfs_addname("/", cur
->len
, 0, 0);
1086 if (cur
->str
[0] == 0) {
1087 printf("add_fsevent: bogus looking string (len %d)\n", cur
->len
);
1092 printf("add_fsevent: unknown type %d\n", arg_type
);
1093 // just skip one 32-bit word and hope we sync up...
1094 (void)va_arg(ap
, int32_t);
1099 OSBitAndAtomic16(~KFSE_BEING_CREATED
, &kfse
->flags
);
1101 OSBitAndAtomic16(~KFSE_BEING_CREATED
, &kfse_dest
->flags
);
1104 if (need_event_unlock
== 0) {
1105 // then we only grabbed it shared
1106 lck_rw_unlock_shared(&event_handling_lock
);
1110 // unlock this here so we don't hold it across the
1111 // event delivery loop.
1112 if (need_event_unlock
) {
1113 lck_rw_unlock_exclusive(&event_handling_lock
);
1114 need_event_unlock
= 0;
1118 // now we have to go and let everyone know that
1119 // is interested in this type of event
1123 for(i
=0; i
< MAX_WATCHERS
; i
++) {
1124 watcher
= watcher_table
[i
];
1125 if (watcher
== NULL
) {
1129 if ( watcher
->event_list
[type
] == FSE_REPORT
1130 && watcher_cares_about_dev(watcher
, dev
)) {
1132 if (watcher_add_event(watcher
, kfse
) != 0) {
1133 watcher
->num_dropped
++;
1137 if (kfse
->refcount
< 1) {
1138 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__
, kfse
->refcount
);
1142 unlock_watch_table();
1145 // have to check if this needs to be unlocked (in
1146 // case we came here from an error handling path)
1147 if (need_event_unlock
) {
1148 lck_rw_unlock_exclusive(&event_handling_lock
);
1149 need_event_unlock
= 0;
1153 release_pathbuff(pathbuff
);
1157 release_event_ref(kfse
);
1164 release_event_ref(kfs_event
*kfse
)
1167 kfs_event copy
, dest_copy
;
1170 old_refcount
= OSAddAtomic(-1, &kfse
->refcount
);
1171 if (old_refcount
> 1) {
1175 lock_fs_event_list();
1176 if (last_event_ptr
== kfse
) {
1177 last_event_ptr
= NULL
;
1178 last_event_type
= -1;
1179 last_coalesced_time
= 0;
1182 if (kfse
->refcount
< 0) {
1183 panic("release_event_ref: bogus kfse refcount %d\n", kfse
->refcount
);
1186 if (kfse
->refcount
> 0 || kfse
->type
== FSE_INVALID
) {
1187 // This is very subtle. Either of these conditions can
1188 // be true if an event got recycled while we were waiting
1189 // on the fs_event_list lock or the event got recycled,
1190 // delivered, _and_ free'd by someone else while we were
1191 // waiting on the fs event list lock. In either case
1192 // we need to just unlock the list and return without
1193 // doing anything because if the refcount is > 0 then
1194 // someone else will take care of free'ing it and when
1195 // the kfse->type is invalid then someone else already
1196 // has handled free'ing the event (while we were blocked
1197 // on the event list lock).
1199 unlock_fs_event_list();
1204 // make a copy of this so we can free things without
1205 // holding the fs_event_buf lock
1208 if (kfse
->dest
&& OSAddAtomic(-1, &kfse
->dest
->refcount
) == 1) {
1209 dest_copy
= *kfse
->dest
;
1211 dest_copy
.str
= NULL
;
1213 dest_copy
.type
= FSE_INVALID
;
1216 kfse
->pid
= kfse
->type
; // save this off for debugging...
1217 kfse
->uid
= (uid_t
)(long)kfse
->str
; // save this off for debugging...
1218 kfse
->gid
= (gid_t
)(long)current_thread();
1220 kfse
->str
= (char *)0xdeadbeef; // XXXdbg - catch any cheaters...
1222 if (dest_copy
.type
!= FSE_INVALID
) {
1223 kfse
->dest
->str
= (char *)0xbadc0de; // XXXdbg - catch any cheaters...
1224 kfse
->dest
->type
= FSE_INVALID
;
1226 if (kfse
->dest
->kevent_list
.le_prev
!= NULL
) {
1227 num_events_outstanding
--;
1228 LIST_REMOVE(kfse
->dest
, kevent_list
);
1229 memset(&kfse
->dest
->kevent_list
, 0xa5, sizeof(kfse
->dest
->kevent_list
));
1232 zfree(event_zone
, kfse
->dest
);
1235 // mark this fsevent as invalid
1240 kfse
->type
= FSE_INVALID
;
1242 if (kfse
->kevent_list
.le_prev
!= NULL
) {
1243 num_events_outstanding
--;
1244 if (otype
== FSE_RENAME
) {
1245 num_pending_rename
--;
1247 LIST_REMOVE(kfse
, kevent_list
);
1248 memset(&kfse
->kevent_list
, 0, sizeof(kfse
->kevent_list
));
1252 zfree(event_zone
, kfse
);
1254 unlock_fs_event_list();
1256 // if we have a pointer in the union
1258 if (copy
.len
== 0) { // and it's not a string
1259 panic("%s:%d: no more fref.vp!\n", __FILE__
, __LINE__
);
1260 // vnode_rele_ext(copy.fref.vp, O_EVTONLY, 0);
1261 } else { // else it's a string
1262 vfs_removename(copy
.str
);
1266 if (dest_copy
.type
!= FSE_INVALID
&& dest_copy
.str
) {
1267 if (dest_copy
.len
== 0) {
1268 panic("%s:%d: no more fref.vp!\n", __FILE__
, __LINE__
);
1269 // vnode_rele_ext(dest_copy.fref.vp, O_EVTONLY, 0);
1271 vfs_removename(dest_copy
.str
);
1278 add_watcher(int8_t *event_list
, int32_t num_events
, int32_t eventq_size
, fs_event_watcher
**watcher_out
)
1281 fs_event_watcher
*watcher
;
1283 if (eventq_size
<= 0 || eventq_size
> 100*MAX_KFS_EVENTS
) {
1284 eventq_size
= MAX_KFS_EVENTS
;
1287 // Note: the event_queue follows the fs_event_watcher struct
1288 // in memory so we only have to do one allocation
1291 sizeof(fs_event_watcher
) + eventq_size
* sizeof(kfs_event
*),
1293 if (watcher
== NULL
) {
1297 watcher
->event_list
= event_list
;
1298 watcher
->num_events
= num_events
;
1299 watcher
->devices_not_to_watch
= NULL
;
1300 watcher
->num_devices
= 0;
1302 watcher
->event_queue
= (kfs_event
**)&watcher
[1];
1303 watcher
->eventq_size
= eventq_size
;
1306 watcher
->blockers
= 0;
1307 watcher
->num_readers
= 0;
1308 watcher
->max_event_id
= 0;
1309 watcher
->fseh
= NULL
;
1311 watcher
->num_dropped
= 0; // XXXdbg - debugging
1315 // now update the global list of who's interested in
1316 // events of a particular type...
1317 for(i
=0; i
< num_events
; i
++) {
1318 if (event_list
[i
] != FSE_IGNORE
&& i
< FSE_MAX_EVENTS
) {
1319 fs_event_type_watchers
[i
]++;
1323 for(i
=0; i
< MAX_WATCHERS
; i
++) {
1324 if (watcher_table
[i
] == NULL
) {
1326 watcher_table
[i
] = watcher
;
1331 if (i
> MAX_WATCHERS
) {
1332 printf("fsevents: too many watchers!\n");
1333 unlock_watch_table();
1337 unlock_watch_table();
1339 *watcher_out
= watcher
;
1347 remove_watcher(fs_event_watcher
*target
)
1349 int i
, j
, counter
=0;
1350 fs_event_watcher
*watcher
;
1355 for(j
=0; j
< MAX_WATCHERS
; j
++) {
1356 watcher
= watcher_table
[j
];
1357 if (watcher
!= target
) {
1361 watcher_table
[j
] = NULL
;
1363 for(i
=0; i
< watcher
->num_events
; i
++) {
1364 if (watcher
->event_list
[i
] != FSE_IGNORE
&& i
< FSE_MAX_EVENTS
) {
1365 fs_event_type_watchers
[i
]--;
1369 if (watcher
->flags
& WATCHER_CLOSING
) {
1370 unlock_watch_table();
1374 // printf("fsevents: removing watcher %p (rd %d wr %d num_readers %d flags 0x%x)\n", watcher, watcher->rd, watcher->wr, watcher->num_readers, watcher->flags);
1375 watcher
->flags
|= WATCHER_CLOSING
;
1376 OSAddAtomic(1, &watcher
->num_readers
);
1378 unlock_watch_table();
1380 while (watcher
->num_readers
> 1 && counter
++ < 5000) {
1381 fsevents_wakeup(watcher
); // in case they're asleep
1383 tsleep(watcher
, PRIBIO
, "fsevents-close", 1);
1385 if (counter
++ >= 5000) {
1386 // printf("fsevents: close: still have readers! (%d)\n", watcher->num_readers);
1387 panic("fsevents: close: still have readers! (%d)\n", watcher
->num_readers
);
1390 // drain the event_queue
1391 while(watcher
->rd
!= watcher
->wr
) {
1392 lck_rw_lock_shared(&event_handling_lock
);
1394 kfse
= watcher
->event_queue
[watcher
->rd
];
1395 if (kfse
->type
== FSE_INVALID
|| kfse
->refcount
< 1) {
1396 panic("remove_watcher: bogus kfse %p during cleanup (type %d refcount %d rd %d wr %d)\n", kfse
, kfse
->type
, kfse
->refcount
, watcher
->rd
, watcher
->wr
);
1399 lck_rw_unlock_shared(&event_handling_lock
);
1401 watcher
->rd
= (watcher
->rd
+1) % watcher
->eventq_size
;
1404 release_event_ref(kfse
);
1408 if (watcher
->event_list
) {
1409 FREE(watcher
->event_list
, M_TEMP
);
1410 watcher
->event_list
= NULL
;
1412 if (watcher
->devices_not_to_watch
) {
1413 FREE(watcher
->devices_not_to_watch
, M_TEMP
);
1414 watcher
->devices_not_to_watch
= NULL
;
1416 FREE(watcher
, M_TEMP
);
1421 unlock_watch_table();
1425 #define EVENT_DELAY_IN_MS 10
1426 static thread_call_t event_delivery_timer
= NULL
;
1427 static int timer_set
= 0;
1431 delayed_event_delivery(__unused
void *param0
, __unused
void *param1
)
1437 for(i
=0; i
< MAX_WATCHERS
; i
++) {
1438 if (watcher_table
[i
] != NULL
&& watcher_table
[i
]->rd
!= watcher_table
[i
]->wr
) {
1439 fsevents_wakeup(watcher_table
[i
]);
1445 unlock_watch_table();
1450 // The watch table must be locked before calling this function.
1453 schedule_event_wakeup(void)
1457 if (event_delivery_timer
== NULL
) {
1458 event_delivery_timer
= thread_call_allocate((thread_call_func_t
)delayed_event_delivery
, NULL
);
1461 clock_interval_to_deadline(EVENT_DELAY_IN_MS
, 1000 * 1000, &deadline
);
1463 thread_call_enter_delayed(event_delivery_timer
, deadline
);
1469 #define MAX_NUM_PENDING 16
1472 // NOTE: the watch table must be locked before calling
1476 watcher_add_event(fs_event_watcher
*watcher
, kfs_event
*kfse
)
1478 if (kfse
->abstime
> watcher
->max_event_id
) {
1479 watcher
->max_event_id
= kfse
->abstime
;
1482 if (((watcher
->wr
+ 1) % watcher
->eventq_size
) == watcher
->rd
) {
1483 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
1484 fsevents_wakeup(watcher
);
1488 OSAddAtomic(1, &kfse
->refcount
);
1489 watcher
->event_queue
[watcher
->wr
] = kfse
;
1491 watcher
->wr
= (watcher
->wr
+ 1) % watcher
->eventq_size
;
1494 // wake up the watcher if there are more than MAX_NUM_PENDING events.
1495 // otherwise schedule a timer (if one isn't already set) which will
1496 // send any pending events if no more are received in the next
1497 // EVENT_DELAY_IN_MS milli-seconds.
1499 if ( (watcher
->rd
< watcher
->wr
&& (watcher
->wr
- watcher
->rd
) > MAX_NUM_PENDING
)
1500 || (watcher
->rd
> watcher
->wr
&& (watcher
->wr
+ watcher
->eventq_size
- watcher
->rd
) > MAX_NUM_PENDING
)) {
1502 fsevents_wakeup(watcher
);
1504 } else if (timer_set
== 0) {
1506 schedule_event_wakeup();
1513 fill_buff(uint16_t type
, int32_t size
, const void *data
,
1514 char *buff
, int32_t *_buff_idx
, int32_t buff_sz
,
1517 int32_t amt
, error
= 0, buff_idx
= *_buff_idx
;
1521 // the +1 on the size is to guarantee that the main data
1522 // copy loop will always copy at least 1 byte
1524 if ((buff_sz
- buff_idx
) <= (int)(2*sizeof(uint16_t) + 1)) {
1525 if (buff_idx
> uio_resid(uio
)) {
1530 error
= uiomove(buff
, buff_idx
, uio
);
1537 // copy out the header (type & size)
1538 memcpy(&buff
[buff_idx
], &type
, sizeof(uint16_t));
1539 buff_idx
+= sizeof(uint16_t);
1541 tmp
= size
& 0xffff;
1542 memcpy(&buff
[buff_idx
], &tmp
, sizeof(uint16_t));
1543 buff_idx
+= sizeof(uint16_t);
1545 // now copy the body of the data, flushing along the way
1546 // if the buffer fills up.
1549 amt
= (size
< (buff_sz
- buff_idx
)) ? size
: (buff_sz
- buff_idx
);
1550 memcpy(&buff
[buff_idx
], data
, amt
);
1554 data
= (const char *)data
+ amt
;
1555 if (size
> (buff_sz
- buff_idx
)) {
1556 if (buff_idx
> uio_resid(uio
)) {
1560 error
= uiomove(buff
, buff_idx
, uio
);
1567 if (amt
== 0) { // just in case...
1573 *_buff_idx
= buff_idx
;
1579 static int copy_out_kfse(fs_event_watcher
*watcher
, kfs_event
*kfse
, struct uio
*uio
) __attribute__((noinline
));
1582 copy_out_kfse(fs_event_watcher
*watcher
, kfs_event
*kfse
, struct uio
*uio
)
1591 if (kfse
->type
== FSE_INVALID
) {
1592 panic("fsevents: copy_out_kfse: asked to copy out an invalid event (kfse %p, refcount %d fref ptr %p)\n", kfse
, kfse
->refcount
, kfse
->str
);
1595 if (kfse
->flags
& KFSE_BEING_CREATED
) {
1599 if (kfse
->type
== FSE_RENAME
&& kfse
->dest
== NULL
) {
1601 // This can happen if an event gets recycled but we had a
1602 // pointer to it in our event queue. The event is the
1603 // destination of a rename which we'll process separately
1604 // (that is, another kfse points to this one so it's ok
1605 // to skip this guy because we'll process it when we process
1611 if (watcher
->flags
& WATCHER_WANTS_EXTENDED_INFO
) {
1613 type
= (kfse
->type
& 0xfff);
1615 if (kfse
->flags
& KFSE_CONTAINS_DROPPED_EVENTS
) {
1616 type
|= (FSE_CONTAINS_DROPPED_EVENTS
<< FSE_FLAG_SHIFT
);
1617 } else if (kfse
->flags
& KFSE_COMBINED_EVENTS
) {
1618 type
|= (FSE_COMBINED_EVENTS
<< FSE_FLAG_SHIFT
);
1622 type
= (int32_t)kfse
->type
;
1625 // copy out the type of the event
1626 memcpy(evbuff
, &type
, sizeof(int32_t));
1627 evbuff_idx
+= sizeof(int32_t);
1629 // copy out the pid of the person that generated the event
1630 memcpy(&evbuff
[evbuff_idx
], &kfse
->pid
, sizeof(pid_t
));
1631 evbuff_idx
+= sizeof(pid_t
);
1637 if (cur
->str
== NULL
|| cur
->str
[0] == '\0') {
1638 printf("copy_out_kfse:2: empty/short path (%s)\n", cur
->str
);
1639 error
= fill_buff(FSE_ARG_STRING
, 2, "/", evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1641 error
= fill_buff(FSE_ARG_STRING
, cur
->len
, cur
->str
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1647 if (cur
->dev
== 0 && cur
->ino
== 0) {
1648 // this happens when a rename event happens and the
1649 // destination of the rename did not previously exist.
1650 // it thus has no other file info so skip copying out
1651 // the stuff below since it isn't initialized
1656 if (watcher
->flags
& WATCHER_WANTS_COMPACT_EVENTS
) {
1659 finfo_size
= sizeof(dev_t
) + sizeof(ino64_t
) + sizeof(int32_t) + sizeof(uid_t
) + sizeof(gid_t
);
1660 error
= fill_buff(FSE_ARG_FINFO
, finfo_size
, &cur
->ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1667 error
= fill_buff(FSE_ARG_DEV
, sizeof(dev_t
), &cur
->dev
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1672 ino
= (ino_t
)cur
->ino
;
1673 error
= fill_buff(FSE_ARG_INO
, sizeof(ino_t
), &ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1678 error
= fill_buff(FSE_ARG_MODE
, sizeof(int32_t), &cur
->mode
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1683 error
= fill_buff(FSE_ARG_UID
, sizeof(uid_t
), &cur
->uid
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1688 error
= fill_buff(FSE_ARG_GID
, sizeof(gid_t
), &cur
->gid
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1701 // very last thing: the time stamp
1702 error
= fill_buff(FSE_ARG_INT64
, sizeof(uint64_t), &cur
->abstime
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1707 // check if the FSE_ARG_DONE will fit
1708 if (sizeof(uint16_t) > sizeof(evbuff
) - evbuff_idx
) {
1709 if (evbuff_idx
> uio_resid(uio
)) {
1713 error
= uiomove(evbuff
, evbuff_idx
, uio
);
1720 tmp16
= FSE_ARG_DONE
;
1721 memcpy(&evbuff
[evbuff_idx
], &tmp16
, sizeof(uint16_t));
1722 evbuff_idx
+= sizeof(uint16_t);
1724 // flush any remaining data in the buffer (and hopefully
1725 // in most cases this is the only uiomove we'll do)
1726 if (evbuff_idx
> uio_resid(uio
)) {
1729 error
= uiomove(evbuff
, evbuff_idx
, uio
);
1740 fmod_watch(fs_event_watcher
*watcher
, struct uio
*uio
)
1743 user_ssize_t last_full_event_resid
;
1747 last_full_event_resid
= uio_resid(uio
);
1749 // need at least 2048 bytes of space (maxpathlen + 1 event buf)
1750 if (uio_resid(uio
) < 2048 || watcher
== NULL
) {
1754 if (watcher
->flags
& WATCHER_CLOSING
) {
1758 if (OSAddAtomic(1, &watcher
->num_readers
) != 0) {
1759 // don't allow multiple threads to read from the fd at the same time
1760 OSAddAtomic(-1, &watcher
->num_readers
);
1764 if (watcher
->rd
== watcher
->wr
) {
1765 if (watcher
->flags
& WATCHER_CLOSING
) {
1766 OSAddAtomic(-1, &watcher
->num_readers
);
1769 OSAddAtomic(1, &watcher
->blockers
);
1771 // there's nothing to do, go to sleep
1772 error
= tsleep((caddr_t
)watcher
, PUSER
|PCATCH
, "fsevents_empty", 0);
1774 OSAddAtomic(-1, &watcher
->blockers
);
1776 if (error
!= 0 || (watcher
->flags
& WATCHER_CLOSING
)) {
1777 OSAddAtomic(-1, &watcher
->num_readers
);
1782 // if we dropped events, return that as an event first
1783 if (watcher
->flags
& WATCHER_DROPPED_EVENTS
) {
1784 int32_t val
= FSE_EVENTS_DROPPED
;
1786 error
= uiomove((caddr_t
)&val
, sizeof(int32_t), uio
);
1788 val
= 0; // a fake pid
1789 error
= uiomove((caddr_t
)&val
, sizeof(int32_t), uio
);
1791 tmp16
= FSE_ARG_DONE
; // makes it a consistent msg
1792 error
= uiomove((caddr_t
)&tmp16
, sizeof(int16_t), uio
);
1794 last_full_event_resid
= uio_resid(uio
);
1798 OSAddAtomic(-1, &watcher
->num_readers
);
1802 watcher
->flags
&= ~WATCHER_DROPPED_EVENTS
;
1805 while (uio_resid(uio
) > 0 && watcher
->rd
!= watcher
->wr
) {
1806 if (watcher
->flags
& WATCHER_CLOSING
) {
1811 // check if the event is something of interest to us
1812 // (since it may have been recycled/reused and changed
1813 // its type or which device it is for)
1815 lck_rw_lock_shared(&event_handling_lock
);
1817 kfse
= watcher
->event_queue
[watcher
->rd
];
1818 if (kfse
->type
== FSE_INVALID
|| kfse
->refcount
< 1) {
1819 panic("fmod_watch: someone left me a bogus kfse %p (type %d refcount %d rd %d wr %d)\n", kfse
, kfse
->type
, kfse
->refcount
, watcher
->rd
, watcher
->wr
);
1822 if (watcher
->event_list
[kfse
->type
] == FSE_REPORT
&& watcher_cares_about_dev(watcher
, kfse
->dev
)) {
1824 error
= copy_out_kfse(watcher
, kfse
, uio
);
1826 // if an event won't fit or encountered an error while
1827 // we were copying it out, then backup to the last full
1828 // event and just bail out. if the error was ENOENT
1829 // then we can continue regular processing, otherwise
1830 // we should unlock things and return.
1831 uio_setresid(uio
, last_full_event_resid
);
1832 if (error
!= ENOENT
) {
1833 lck_rw_unlock_shared(&event_handling_lock
);
1839 last_full_event_resid
= uio_resid(uio
);
1842 lck_rw_unlock_shared(&event_handling_lock
);
1844 watcher
->rd
= (watcher
->rd
+ 1) % watcher
->eventq_size
;
1847 if (kfse
->type
== FSE_INVALID
|| kfse
->refcount
< 1) {
1848 panic("fmod_watch:2: my kfse became bogus! kfse %p (type %d refcount %d rd %d wr %d)\n", kfse
, kfse
->type
, kfse
->refcount
, watcher
->rd
, watcher
->wr
);
1851 release_event_ref(kfse
);
1855 OSAddAtomic(-1, &watcher
->num_readers
);
1861 // release any references we might have on vnodes which are
1862 // the mount point passed to us (so that it can be cleanly
1865 // since we don't want to lose the events we'll convert the
1866 // vnode refs to full paths.
1869 fsevent_unmount(__unused
struct mount
*mp
)
1871 // we no longer maintain pointers to vnodes so
1872 // there is nothing to do...
1877 // /dev/fsevents device code
1879 static int fsevents_installed
= 0;
1881 typedef struct fsevent_handle
{
1884 fs_event_watcher
*watcher
;
1885 struct klist knotes
;
1889 #define FSEH_CLOSING 0x0001
1892 fseventsf_read(struct fileproc
*fp
, struct uio
*uio
,
1893 __unused
int flags
, __unused vfs_context_t ctx
)
1895 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
1898 error
= fmod_watch(fseh
->watcher
, uio
);
1905 fseventsf_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1906 __unused
int flags
, __unused vfs_context_t ctx
)
1911 #pragma pack(push, 4)
1912 typedef struct ext_fsevent_dev_filter_args
{
1913 uint32_t num_devices
;
1914 user_addr_t devices
;
1915 } ext_fsevent_dev_filter_args
;
1918 typedef struct old_fsevent_dev_filter_args
{
1919 uint32_t num_devices
;
1921 } old_fsevent_dev_filter_args
;
1923 #define OLD_FSEVENTS_DEVICE_FILTER _IOW('s', 100, old_fsevent_dev_filter_args)
1924 #define NEW_FSEVENTS_DEVICE_FILTER _IOW('s', 100, ext_fsevent_dev_filter_args)
1927 /* need this in spite of the padding due to alignment of devices */
1928 typedef struct fsevent_dev_filter_args32
{
1929 uint32_t num_devices
;
1932 } fsevent_dev_filter_args32
;
1936 fseventsf_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
, vfs_context_t ctx
)
1938 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
1940 ext_fsevent_dev_filter_args
*devfilt_args
, _devfilt_args
;
1942 if (proc_is64bit(vfs_context_proc(ctx
))) {
1943 devfilt_args
= (ext_fsevent_dev_filter_args
*)data
;
1944 } else if (cmd
== OLD_FSEVENTS_DEVICE_FILTER
) {
1945 old_fsevent_dev_filter_args
*udev_filt_args
= (old_fsevent_dev_filter_args
*)data
;
1947 devfilt_args
= &_devfilt_args
;
1948 memset(devfilt_args
, 0, sizeof(ext_fsevent_dev_filter_args
));
1950 devfilt_args
->num_devices
= udev_filt_args
->num_devices
;
1951 devfilt_args
->devices
= CAST_USER_ADDR_T(udev_filt_args
->devices
);
1954 fsevent_dev_filter_args32
*udev_filt_args
= (fsevent_dev_filter_args32
*)data
;
1956 fsevent_dev_filter_args
*udev_filt_args
= (fsevent_dev_filter_args
*)data
;
1959 devfilt_args
= &_devfilt_args
;
1960 memset(devfilt_args
, 0, sizeof(ext_fsevent_dev_filter_args
));
1962 devfilt_args
->num_devices
= udev_filt_args
->num_devices
;
1963 devfilt_args
->devices
= CAST_USER_ADDR_T(udev_filt_args
->devices
);
1966 OSAddAtomic(1, &fseh
->active
);
1967 if (fseh
->flags
& FSEH_CLOSING
) {
1968 OSAddAtomic(-1, &fseh
->active
);
1977 case FSEVENTS_WANT_COMPACT_EVENTS
: {
1978 fseh
->watcher
->flags
|= WATCHER_WANTS_COMPACT_EVENTS
;
1982 case FSEVENTS_WANT_EXTENDED_INFO
: {
1983 fseh
->watcher
->flags
|= WATCHER_WANTS_EXTENDED_INFO
;
1987 case FSEVENTS_GET_CURRENT_ID
: {
1988 *(uint64_t *)data
= fseh
->watcher
->max_event_id
;
1993 case OLD_FSEVENTS_DEVICE_FILTER
:
1994 case NEW_FSEVENTS_DEVICE_FILTER
: {
1995 int new_num_devices
;
1996 dev_t
*devices_not_to_watch
, *tmp
=NULL
;
1998 if (devfilt_args
->num_devices
> 256) {
2003 new_num_devices
= devfilt_args
->num_devices
;
2004 if (new_num_devices
== 0) {
2005 tmp
= fseh
->watcher
->devices_not_to_watch
;
2008 fseh
->watcher
->devices_not_to_watch
= NULL
;
2009 fseh
->watcher
->num_devices
= new_num_devices
;
2010 unlock_watch_table();
2018 MALLOC(devices_not_to_watch
, dev_t
*,
2019 new_num_devices
* sizeof(dev_t
),
2021 if (devices_not_to_watch
== NULL
) {
2026 ret
= copyin(devfilt_args
->devices
,
2027 (void *)devices_not_to_watch
,
2028 new_num_devices
* sizeof(dev_t
));
2030 FREE(devices_not_to_watch
, M_TEMP
);
2035 fseh
->watcher
->num_devices
= new_num_devices
;
2036 tmp
= fseh
->watcher
->devices_not_to_watch
;
2037 fseh
->watcher
->devices_not_to_watch
= devices_not_to_watch
;
2038 unlock_watch_table();
2052 OSAddAtomic(-1, &fseh
->active
);
2058 fseventsf_select(struct fileproc
*fp
, int which
, __unused
void *wql
, vfs_context_t ctx
)
2060 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
2063 if ((which
!= FREAD
) || (fseh
->watcher
->flags
& WATCHER_CLOSING
)) {
2068 // if there's nothing in the queue, we're not ready
2069 if (fseh
->watcher
->rd
!= fseh
->watcher
->wr
) {
2074 selrecord(vfs_context_proc(ctx
), &fseh
->si
, wql
);
2083 fseventsf_stat(__unused
struct fileproc
*fp
, __unused
struct stat
*sb
, __unused vfs_context_t ctx
)
2090 fseventsf_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
2092 fsevent_handle
*fseh
= (struct fsevent_handle
*)fg
->fg_data
;
2093 fs_event_watcher
*watcher
;
2095 OSBitOrAtomic(FSEH_CLOSING
, &fseh
->flags
);
2096 while (OSAddAtomic(0, &fseh
->active
) > 0) {
2097 tsleep((caddr_t
)fseh
->watcher
, PRIBIO
, "fsevents-close", 1);
2100 watcher
= fseh
->watcher
;
2102 fseh
->watcher
= NULL
;
2104 remove_watcher(watcher
);
2111 filt_fsevent_detach(struct knote
*kn
)
2113 fsevent_handle
*fseh
= (struct fsevent_handle
*)kn
->kn_hook
;
2117 KNOTE_DETACH(&fseh
->knotes
, kn
);
2119 unlock_watch_table();
2123 * Determine whether this knote should be active
2125 * This is kind of subtle.
2126 * --First, notice if the vnode has been revoked: in so, override hint
2127 * --EVFILT_READ knotes are checked no matter what the hint is
2128 * --Other knotes activate based on hint.
2129 * --If hint is revoke, set special flags and activate
2132 filt_fsevent(struct knote
*kn
, long hint
)
2134 fsevent_handle
*fseh
= (struct fsevent_handle
*)kn
->kn_hook
;
2136 int32_t rd
, wr
, amt
;
2138 if (NOTE_REVOKE
== hint
) {
2139 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
2143 rd
= fseh
->watcher
->rd
;
2144 wr
= fseh
->watcher
->wr
;
2148 amt
= fseh
->watcher
->eventq_size
- (rd
- wr
);
2151 switch(kn
->kn_filter
) {
2155 if (kn
->kn_data
!= 0) {
2160 /* Check events this note matches against the hint */
2161 if (kn
->kn_sfflags
& hint
) {
2162 kn
->kn_fflags
|= hint
; /* Set which event occurred */
2164 if (kn
->kn_fflags
!= 0) {
2178 struct filterops fsevent_filtops
= {
2181 .f_detach
= filt_fsevent_detach
,
2182 .f_event
= filt_fsevent
2186 fseventsf_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
, __unused vfs_context_t ctx
)
2188 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
2190 kn
->kn_hook
= (void*)fseh
;
2192 kn
->kn_fop
= &fsevent_filtops
;
2196 KNOTE_ATTACH(&fseh
->knotes
, kn
);
2198 unlock_watch_table();
2204 fseventsf_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
2207 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
2209 fseh
->watcher
->flags
|= WATCHER_CLOSING
;
2211 // if there are people still waiting, sleep for 10ms to
2212 // let them clean up and get out of there. however we
2213 // also don't want to get stuck forever so if they don't
2214 // exit after 5 seconds we're tearing things down anyway.
2215 while(fseh
->watcher
->blockers
&& counter
++ < 500) {
2216 // issue wakeup in case anyone is blocked waiting for an event
2217 // do this each time we wakeup in case the blocker missed
2218 // the wakeup due to the unprotected test of WATCHER_CLOSING
2219 // and decision to tsleep in fmod_watch... this bit of
2220 // latency is a decent tradeoff against not having to
2221 // take and drop a lock in fmod_watch
2222 fsevents_wakeup(fseh
->watcher
);
2224 tsleep((caddr_t
)fseh
->watcher
, PRIBIO
, "watcher-close", 1);
2232 fseventsopen(__unused dev_t dev
, __unused
int flag
, __unused
int mode
, __unused
struct proc
*p
)
2242 fseventsclose(__unused dev_t dev
, __unused
int flag
, __unused
int mode
, __unused
struct proc
*p
)
2248 fseventsread(__unused dev_t dev
, __unused
struct uio
*uio
, __unused
int ioflag
)
2255 parse_buffer_and_add_events(const char *buffer
, int bufsize
, vfs_context_t ctx
, long *remainder
)
2257 const fse_info
*finfo
, *dest_finfo
;
2258 const char *path
, *ptr
, *dest_path
, *event_start
=buffer
;
2259 int path_len
, type
, dest_path_len
, err
= 0;
2263 while ((ptr
+sizeof(int)+sizeof(fse_info
)+1) < buffer
+bufsize
) {
2264 type
= *(const int *)ptr
;
2265 if (type
< 0 || type
>= FSE_MAX_EVENTS
) {
2272 finfo
= (const fse_info
*)ptr
;
2273 ptr
+= sizeof(fse_info
);
2276 while(ptr
< buffer
+bufsize
&& *ptr
!= '\0') {
2280 if (ptr
>= buffer
+bufsize
) {
2284 ptr
++; // advance over the trailing '\0'
2286 path_len
= ptr
- path
;
2288 if (type
!= FSE_RENAME
&& type
!= FSE_EXCHANGE
) {
2289 event_start
= ptr
; // record where the next event starts
2291 err
= add_fsevent(type
, ctx
, FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
, FSE_ARG_DONE
);
2299 // if we're here we have to slurp up the destination finfo
2300 // and path so that we can pass them to the add_fsevent()
2301 // call. basically it's a copy of the above code.
2303 dest_finfo
= (const fse_info
*)ptr
;
2304 ptr
+= sizeof(fse_info
);
2307 while(ptr
< buffer
+bufsize
&& *ptr
!= '\0') {
2311 if (ptr
>= buffer
+bufsize
) {
2315 ptr
++; // advance over the trailing '\0'
2316 event_start
= ptr
; // record where the next event starts
2318 dest_path_len
= ptr
- dest_path
;
2320 // If the destination inode number is non-zero, generate a rename
2321 // with both source and destination FSE_ARG_FINFO. Otherwise generate
2322 // a rename with only one FSE_ARG_FINFO. If you need to inject an
2323 // exchange with an inode of zero, just make that inode (and its path)
2324 // come in as the first one, not the second.
2326 if (dest_finfo
->ino
) {
2327 err
= add_fsevent(type
, ctx
,
2328 FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
,
2329 FSE_ARG_STRING
, dest_path_len
, dest_path
, FSE_ARG_FINFO
, dest_finfo
,
2332 err
= add_fsevent(type
, ctx
,
2333 FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
,
2334 FSE_ARG_STRING
, dest_path_len
, dest_path
,
2344 // if the last event wasn't complete, set the remainder
2345 // to be the last event start boundary.
2347 *remainder
= (long)((buffer
+bufsize
) - event_start
);
2354 // Note: this buffer size can not ever be less than
2355 // 2*MAXPATHLEN + 2*sizeof(fse_info) + sizeof(int)
2356 // because that is the max size for a single event.
2357 // I made it 4k to be a "nice" size. making it
2358 // smaller is not a good idea.
2360 #define WRITE_BUFFER_SIZE 4096
2361 char *write_buffer
=NULL
;
2364 fseventswrite(__unused dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
2367 vfs_context_t ctx
= vfs_context_current();
2368 long offset
=0, remainder
;
2370 lck_mtx_lock(&event_writer_lock
);
2372 if (write_buffer
== NULL
) {
2373 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&write_buffer
, WRITE_BUFFER_SIZE
)) {
2374 lck_mtx_unlock(&event_writer_lock
);
2380 // this loop copies in and processes the events written.
2381 // it takes care to copy in reasonable size chunks and
2382 // process them. if there is an event that spans a chunk
2383 // boundary we're careful to copy those bytes down to the
2384 // beginning of the buffer and read the next chunk in just
2387 while(uio_resid(uio
)) {
2388 if (uio_resid(uio
) > (WRITE_BUFFER_SIZE
-offset
)) {
2389 count
= WRITE_BUFFER_SIZE
- offset
;
2391 count
= uio_resid(uio
);
2394 error
= uiomove(write_buffer
+offset
, count
, uio
);
2399 // printf("fsevents: write: copied in %d bytes (offset: %ld)\n", count, offset);
2400 error
= parse_buffer_and_add_events(write_buffer
, offset
+count
, ctx
, &remainder
);
2406 // if there's any remainder, copy it down to the beginning
2407 // of the buffer so that it will get processed the next time
2408 // through the loop. note that the remainder always starts
2409 // at an event boundary.
2411 if (remainder
!= 0) {
2412 // printf("fsevents: write: an event spanned a %d byte boundary. remainder: %ld\n",
2413 // WRITE_BUFFER_SIZE, remainder);
2414 memmove(write_buffer
, (write_buffer
+count
+offset
) - remainder
, remainder
);
2421 lck_mtx_unlock(&event_writer_lock
);
2427 static struct fileops fsevents_fops
= {
2437 typedef struct ext_fsevent_clone_args
{
2438 user_addr_t event_list
;
2440 int32_t event_queue_depth
;
2442 } ext_fsevent_clone_args
;
2444 typedef struct old_fsevent_clone_args
{
2445 uint32_t event_list
;
2447 int32_t event_queue_depth
;
2449 } old_fsevent_clone_args
;
2451 #define OLD_FSEVENTS_CLONE _IOW('s', 1, old_fsevent_clone_args)
2454 fseventsioctl(__unused dev_t dev
, u_long cmd
, caddr_t data
, __unused
int flag
, struct proc
*p
)
2458 fsevent_handle
*fseh
= NULL
;
2459 ext_fsevent_clone_args
*fse_clone_args
, _fse_clone
;
2461 int is64bit
= proc_is64bit(p
);
2464 case OLD_FSEVENTS_CLONE
: {
2465 old_fsevent_clone_args
*old_args
= (old_fsevent_clone_args
*)data
;
2467 fse_clone_args
= &_fse_clone
;
2468 memset(fse_clone_args
, 0, sizeof(ext_fsevent_clone_args
));
2470 fse_clone_args
->event_list
= CAST_USER_ADDR_T(old_args
->event_list
);
2471 fse_clone_args
->num_events
= old_args
->num_events
;
2472 fse_clone_args
->event_queue_depth
= old_args
->event_queue_depth
;
2473 fse_clone_args
->fd
= CAST_USER_ADDR_T(old_args
->fd
);
2477 case FSEVENTS_CLONE
:
2479 fse_clone_args
= (ext_fsevent_clone_args
*)data
;
2481 fsevent_clone_args
*ufse_clone
= (fsevent_clone_args
*)data
;
2483 fse_clone_args
= &_fse_clone
;
2484 memset(fse_clone_args
, 0, sizeof(ext_fsevent_clone_args
));
2486 fse_clone_args
->event_list
= CAST_USER_ADDR_T(ufse_clone
->event_list
);
2487 fse_clone_args
->num_events
= ufse_clone
->num_events
;
2488 fse_clone_args
->event_queue_depth
= ufse_clone
->event_queue_depth
;
2489 fse_clone_args
->fd
= CAST_USER_ADDR_T(ufse_clone
->fd
);
2493 if (fse_clone_args
->num_events
< 0 || fse_clone_args
->num_events
> 4096) {
2497 MALLOC(fseh
, fsevent_handle
*, sizeof(fsevent_handle
),
2502 memset(fseh
, 0, sizeof(fsevent_handle
));
2504 klist_init(&fseh
->knotes
);
2506 MALLOC(event_list
, int8_t *,
2507 fse_clone_args
->num_events
* sizeof(int8_t),
2509 if (event_list
== NULL
) {
2514 error
= copyin(fse_clone_args
->event_list
,
2516 fse_clone_args
->num_events
* sizeof(int8_t));
2518 FREE(event_list
, M_TEMP
);
2523 error
= add_watcher(event_list
,
2524 fse_clone_args
->num_events
,
2525 fse_clone_args
->event_queue_depth
,
2528 FREE(event_list
, M_TEMP
);
2533 // connect up the watcher with this fsevent_handle
2534 fseh
->watcher
->fseh
= fseh
;
2536 error
= falloc(p
, &f
, &fd
, vfs_context_current());
2538 FREE(event_list
, M_TEMP
);
2543 f
->f_fglob
->fg_flag
= FREAD
| FWRITE
;
2544 f
->f_fglob
->fg_type
= DTYPE_FSEVENTS
;
2545 f
->f_fglob
->fg_ops
= &fsevents_fops
;
2546 f
->f_fglob
->fg_data
= (caddr_t
) fseh
;
2548 error
= copyout((void *)&fd
, fse_clone_args
->fd
, sizeof(int32_t));
2553 procfdtbl_releasefd(p
, fd
, NULL
);
2554 fp_drop(p
, fd
, f
, 1);
2568 fsevents_wakeup(fs_event_watcher
*watcher
)
2570 selwakeup(&watcher
->fseh
->si
);
2571 KNOTE(&watcher
->fseh
->knotes
, NOTE_WRITE
|NOTE_NONE
);
2572 wakeup((caddr_t
)watcher
);
2577 * A struct describing which functions will get invoked for certain
2580 static struct cdevsw fsevents_cdevsw
=
2582 fseventsopen
, /* open */
2583 fseventsclose
, /* close */
2584 fseventsread
, /* read */
2585 fseventswrite
, /* write */
2586 fseventsioctl
, /* ioctl */
2587 (stop_fcn_t
*)&nulldev
, /* stop */
2588 (reset_fcn_t
*)&nulldev
, /* reset */
2590 eno_select
, /* select */
2591 eno_mmap
, /* mmap */
2592 eno_strat
, /* strategy */
2593 eno_getc
, /* getc */
2594 eno_putc
, /* putc */
2600 * Called to initialize our device,
2601 * and to register ourselves with devfs
2609 if (fsevents_installed
) {
2613 fsevents_installed
= 1;
2615 ret
= cdevsw_add(-1, &fsevents_cdevsw
);
2617 fsevents_installed
= 0;
2621 devfs_make_node(makedev (ret
, 0), DEVFS_CHAR
,
2622 UID_ROOT
, GID_WHEEL
, 0644, "fsevents", 0);
2624 fsevents_internal_init();
2633 MALLOC_ZONE(path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2638 release_pathbuff(char *path
)
2644 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
2648 get_fse_info(struct vnode
*vp
, fse_info
*fse
, __unused vfs_context_t ctx
)
2650 struct vnode_attr va
;
2653 VATTR_WANTED(&va
, va_fsid
);
2654 VATTR_WANTED(&va
, va_fileid
);
2655 VATTR_WANTED(&va
, va_mode
);
2656 VATTR_WANTED(&va
, va_uid
);
2657 VATTR_WANTED(&va
, va_gid
);
2658 if (vp
->v_flag
& VISHARDLINK
) {
2659 if (vp
->v_type
== VDIR
) {
2660 VATTR_WANTED(&va
, va_dirlinkcount
);
2662 VATTR_WANTED(&va
, va_nlink
);
2666 if (vnode_getattr(vp
, &va
, vfs_context_kernel()) != 0) {
2667 memset(fse
, 0, sizeof(fse_info
));
2671 fse
->ino
= (ino64_t
)va
.va_fileid
;
2672 fse
->dev
= (dev_t
)va
.va_fsid
;
2673 fse
->mode
= (int32_t)vnode_vttoif(vnode_vtype(vp
)) | va
.va_mode
;
2674 fse
->uid
= (uid_t
)va
.va_uid
;
2675 fse
->gid
= (gid_t
)va
.va_gid
;
2676 if (vp
->v_flag
& VISHARDLINK
) {
2677 fse
->mode
|= FSE_MODE_HLINK
;
2678 if (vp
->v_type
== VDIR
) {
2679 fse
->nlink
= (uint64_t)va
.va_dirlinkcount
;
2681 fse
->nlink
= (uint64_t)va
.va_nlink
;
2689 create_fsevent_from_kevent(vnode_t vp
, uint32_t kevents
, struct vnode_attr
*vap
)
2691 int fsevent_type
=FSE_CONTENT_MODIFIED
, len
; // the default is the most pessimistic
2692 char pathbuf
[MAXPATHLEN
];
2696 if (kevents
& VNODE_EVENT_DELETE
) {
2697 fsevent_type
= FSE_DELETE
;
2698 } else if (kevents
& (VNODE_EVENT_EXTEND
|VNODE_EVENT_WRITE
)) {
2699 fsevent_type
= FSE_CONTENT_MODIFIED
;
2700 } else if (kevents
& VNODE_EVENT_LINK
) {
2701 fsevent_type
= FSE_CREATE_FILE
;
2702 } else if (kevents
& VNODE_EVENT_RENAME
) {
2703 fsevent_type
= FSE_CREATE_FILE
; // XXXdbg - should use FSE_RENAME but we don't have the destination info;
2704 } else if (kevents
& (VNODE_EVENT_FILE_CREATED
|VNODE_EVENT_FILE_REMOVED
|VNODE_EVENT_DIR_CREATED
|VNODE_EVENT_DIR_REMOVED
)) {
2705 fsevent_type
= FSE_STAT_CHANGED
; // XXXdbg - because vp is a dir and the thing created/removed lived inside it
2706 } else { // a catch all for VNODE_EVENT_PERMS, VNODE_EVENT_ATTRIB and anything else
2707 fsevent_type
= FSE_STAT_CHANGED
;
2710 // printf("convert_kevent: kevents 0x%x fsevent type 0x%x (for %s)\n", kevents, fsevent_type, vp->v_name ? vp->v_name : "(no-name)");
2712 fse
.dev
= vap
->va_fsid
;
2713 fse
.ino
= vap
->va_fileid
;
2714 fse
.mode
= vnode_vttoif(vnode_vtype(vp
)) | (uint32_t)vap
->va_mode
;
2715 if (vp
->v_flag
& VISHARDLINK
) {
2716 fse
.mode
|= FSE_MODE_HLINK
;
2717 if (vp
->v_type
== VDIR
) {
2718 fse
.nlink
= vap
->va_dirlinkcount
;
2720 fse
.nlink
= vap
->va_nlink
;
2724 if (vp
->v_type
== VDIR
) {
2725 fse
.mode
|= FSE_REMOTE_DIR_EVENT
;
2729 fse
.uid
= vap
->va_uid
;
2730 fse
.gid
= vap
->va_gid
;
2732 len
= sizeof(pathbuf
);
2733 if (vn_getpath(vp
, pathbuf
, &len
) == 0) {
2734 add_fsevent(fsevent_type
, vfs_context_current(), FSE_ARG_STRING
, len
, pathbuf
, FSE_ARG_FINFO
, &fse
, FSE_ARG_DONE
);
2739 #else /* CONFIG_FSE */
2741 * The get_pathbuff and release_pathbuff routines are used in places not
2742 * related to fsevents, and it's a handy abstraction, so define trivial
2743 * versions that don't cache a pool of buffers. This way, we don't have
2744 * to conditionalize the callers, and they still get the advantage of the
2745 * pool of buffers if CONFIG_FSE is turned on.
2751 MALLOC_ZONE(path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2756 release_pathbuff(char *path
)
2758 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
2760 #endif /* CONFIG_FSE */