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 // mark the zone as exhaustible so that it will not
202 // ever grow beyond what we initially filled it with
203 zone_change(event_zone
, Z_EXHAUST
, TRUE
);
204 zone_change(event_zone
, Z_COLLECT
, FALSE
);
205 zone_change(event_zone
, Z_CALLERACCT
, FALSE
);
207 if (zfill(event_zone
, MAX_KFS_EVENTS
) < MAX_KFS_EVENTS
) {
208 printf("fsevents: failed to pre-fill the event zone.\n");
214 lock_watch_table(void)
216 lck_mtx_lock(&watch_table_lock
);
220 unlock_watch_table(void)
222 lck_mtx_unlock(&watch_table_lock
);
226 lock_fs_event_list(void)
228 lck_mtx_lock(&event_buf_lock
);
232 unlock_fs_event_list(void)
234 lck_mtx_unlock(&event_buf_lock
);
238 static void release_event_ref(kfs_event
*kfse
);
241 watcher_cares_about_dev(fs_event_watcher
*watcher
, dev_t dev
)
245 // if devices_not_to_watch is NULL then we care about all
246 // events from all devices
247 if (watcher
->devices_not_to_watch
== NULL
) {
251 for(i
=0; i
< watcher
->num_devices
; i
++) {
252 if (dev
== watcher
->devices_not_to_watch
[i
]) {
253 // found a match! that means we do not
254 // want events from this device.
259 // if we're here it's not in the devices_not_to_watch[]
260 // list so that means we do care about it
266 need_fsevent(int type
, vnode_t vp
)
268 if (type
>= 0 && type
< FSE_MAX_EVENTS
&& fs_event_type_watchers
[type
] == 0)
271 // events in /dev aren't really interesting...
272 if (vp
->v_tag
== VT_DEVFS
) {
280 prefix_match_len(const char *str1
, const char *str2
)
284 while(*str1
&& *str2
&& *str1
== *str2
) {
290 if (*str1
== '\0' && *str2
== '\0') {
298 struct history_item
{
300 kfs_event
*oldest_kfse
;
305 compare_history_items(const void *_a
, const void *_b
)
307 const struct history_item
*a
= (const struct history_item
*)_a
;
308 const struct history_item
*b
= (const struct history_item
*)_b
;
310 // we want a descending order
311 return (b
->counter
- a
->counter
);
314 #define is_throw_away(x) ((x) == FSE_STAT_CHANGED || (x) == FSE_CONTENT_MODIFIED)
317 // Ways that an event can be reused:
319 // "combined" events mean that there were two events for
320 // the same vnode or path and we're combining both events
321 // into a single event. The primary event gets a bit that
322 // marks it as having been combined. The secondary event
323 // is essentially dropped and the kfse structure reused.
325 // "collapsed" means that multiple events below a given
326 // directory are collapsed into a single event. in this
327 // case, the directory that we collapse into and all of
328 // its children must be re-scanned.
330 // "recycled" means that we're completely blowing away
331 // the event since there are other events that have info
332 // about the same vnode or path (and one of those other
333 // events will be marked as combined or collapsed as
336 #define KFSE_COMBINED 0x0001
337 #define KFSE_COLLAPSED 0x0002
338 #define KFSE_RECYCLED 0x0004
341 int num_combined_events
= 0;
342 int num_added_to_parent
= 0;
343 int num_parent_switch
= 0;
344 int num_recycled_rename
= 0;
347 // NOTE: you must call lock_fs_event_list() before calling
351 find_an_event(const char *str
, int len
, kfs_event
*do_not_reuse
, int *reuse_type
, int *longest_match_len
)
353 kfs_event
*kfse
, *best_kfse
=NULL
;
355 // this seems to be enough to find most duplicate events for the same vnode
356 #define MAX_HISTORY 12
357 struct history_item history
[MAX_HISTORY
];
360 *longest_match_len
= 0;
363 memset(history
, 0, sizeof(history
));
366 // now walk the list of events and try to find the best match
367 // for this event. if we have a vnode, we look for an event
368 // that already references the vnode. if we don't find one
369 // we'll also take the parent of this vnode (in which case it
370 // will be marked as having dropped events within it).
372 // if we have a string we look for the longest match on the
376 LIST_FOREACH(kfse
, &kfse_list_head
, kevent_list
) {
380 // don't look at events that are still in the process of being
381 // created, have a null vnode ptr or rename/exchange events.
383 if ( (kfse
->flags
& KFSE_BEING_CREATED
) || kfse
->type
== FSE_RENAME
|| kfse
->type
== FSE_EXCHANGE
) {
389 if (kfse
->len
!= 0 && kfse
->str
!= NULL
) {
390 match_len
= prefix_match_len(str
, kfse
->str
);
391 if (match_len
> *longest_match_len
) {
393 *longest_match_len
= match_len
;
398 if (kfse
== do_not_reuse
) {
402 for(i
=0; i
< MAX_HISTORY
; i
++) {
403 if (history
[i
].kfse
== NULL
) {
408 // do a quick check to see if we've got two simple events
409 // that we can cheaply combine. if the event we're looking
410 // at and one of the events in the history table are for the
411 // same path then we'll just mark the newer event as combined
412 // and recyle the older event.
414 if (history
[i
].kfse
->str
== kfse
->str
) {
416 OSBitOrAtomic16(KFSE_COMBINED_EVENTS
, &kfse
->flags
);
417 *reuse_type
= KFSE_RECYCLED
;
418 history
[i
].kfse
->flags
|= KFSE_RECYCLED_EVENT
;
419 return history
[i
].kfse
;
423 if (i
< MAX_HISTORY
&& history
[i
].kfse
== NULL
) {
424 history
[i
].kfse
= kfse
;
425 history
[i
].counter
= 1;
426 } else if (i
>= MAX_HISTORY
) {
427 qsort(history
, MAX_HISTORY
, sizeof(struct history_item
), compare_history_items
);
429 // pluck off the lowest guy if he's only got a count of 1
430 if (history
[MAX_HISTORY
-1].counter
== 1) {
431 history
[MAX_HISTORY
-1].kfse
= kfse
;
437 if (str
!= NULL
&& best_kfse
) {
438 if (*longest_match_len
<= 1) {
439 // if the best match we had was "/" then basically we're toast...
440 *longest_match_len
= 0;
442 } else if (*longest_match_len
!= len
) {
443 OSBitOrAtomic16(KFSE_CONTAINS_DROPPED_EVENTS
, &best_kfse
->flags
);
444 *reuse_type
= KFSE_COLLAPSED
;
446 OSBitOrAtomic16(KFSE_COMBINED_EVENTS
, &best_kfse
->flags
);
447 *reuse_type
= KFSE_COMBINED
;
455 static struct timeval last_print
;
458 // These variables are used to track coalescing multiple identical
459 // events for the same vnode/pathname. If we get the same event
460 // type and same vnode/pathname as the previous event, we just drop
461 // the event since it's superfluous. This improves some micro-
462 // benchmarks considerably and actually has a real-world impact on
463 // tests like a Finder copy where multiple stat-changed events can
466 static int last_event_type
=-1;
467 static void *last_ptr
=NULL
;
468 static char last_str
[MAXPATHLEN
];
469 static int last_nlen
=0;
470 static int last_vid
=-1;
471 static uint64_t last_coalesced_time
=0;
472 static void *last_event_ptr
=NULL
;
473 int last_coalesced
= 0;
474 static mach_timebase_info_data_t sTimebaseInfo
= { 0, 0 };
478 add_fsevent(int type
, vfs_context_t ctx
, ...)
480 struct proc
*p
= vfs_context_proc(ctx
);
481 int i
, arg_type
, skip_init
=0, longest_match_len
, ret
;
482 kfs_event
*kfse
, *kfse_dest
=NULL
, *cur
;
483 fs_event_watcher
*watcher
;
485 int error
= 0, did_alloc
=0, need_event_unlock
= 0;
487 uint64_t now
, elapsed
;
496 // ignore bogus event types..
497 if (type
< 0 || type
>= FSE_MAX_EVENTS
) {
501 // if no one cares about this type of event, bail out
502 if (fs_event_type_watchers
[type
] == 0) {
508 now
= mach_absolute_time();
510 // find a free event and snag it for our use
511 // NOTE: do not do anything that would block until
512 // the lock is dropped.
513 lock_fs_event_list();
516 // check if this event is identical to the previous one...
517 // (as long as it's not an event type that can never be the
518 // same as a previous event)
520 if (type
!= FSE_CREATE_FILE
&& type
!= FSE_DELETE
&& type
!= FSE_RENAME
&& type
!= FSE_EXCHANGE
&& type
!= FSE_CHOWN
) {
522 int vid
=0, was_str
=0, nlen
=0;
524 for(arg_type
=va_arg(ap
, int32_t); arg_type
!= FSE_ARG_DONE
; arg_type
=va_arg(ap
, int32_t)) {
526 case FSE_ARG_VNODE
: {
527 ptr
= va_arg(ap
, void *);
528 vid
= vnode_vid((struct vnode
*)ptr
);
532 case FSE_ARG_STRING
: {
533 nlen
= va_arg(ap
, int32_t);
534 ptr
= va_arg(ap
, void *);
544 if ( sTimebaseInfo
.denom
== 0 ) {
545 (void) clock_timebase_info(&sTimebaseInfo
);
548 elapsed
= (now
- last_coalesced_time
);
549 if (sTimebaseInfo
.denom
!= sTimebaseInfo
.numer
) {
550 if (sTimebaseInfo
.denom
== 1) {
551 elapsed
*= sTimebaseInfo
.numer
;
553 // this could overflow... the worst that will happen is that we'll
554 // send (or not send) an extra event so I'm not going to worry about
555 // doing the math right like dtrace_abs_to_nano() does.
556 elapsed
= (elapsed
* sTimebaseInfo
.numer
) / (uint64_t)sTimebaseInfo
.denom
;
560 if (type
== last_event_type
561 && (elapsed
< 1000000000)
563 ((vid
&& vid
== last_vid
&& last_ptr
== ptr
)
565 (last_str
[0] && last_nlen
== nlen
&& ptr
&& strcmp(last_str
, ptr
) == 0))
569 unlock_fs_event_list();
576 strlcpy(last_str
, ptr
, sizeof(last_str
));
580 last_event_type
= type
;
581 last_coalesced_time
= now
;
587 kfse
= zalloc_noblock(event_zone
);
588 if (kfse
&& (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
)) {
589 kfse_dest
= zalloc_noblock(event_zone
);
590 if (kfse_dest
== NULL
) {
592 zfree(event_zone
, kfse
);
598 if (kfse
== NULL
) { // yikes! no free events
603 // Figure out what kind of reference we have to the
604 // file in this event. This helps us find an event
605 // to combine/collapse into to make room.
607 // If we have a rename or exchange event then we
608 // don't want to go through the normal path, we
609 // want to "steal" an event instead (which is what
610 // find_an_event() will do if str is null).
612 arg_type
= va_arg(ap
, int32_t);
613 if (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
) {
615 } else if (arg_type
== FSE_ARG_STRING
) {
616 len
= va_arg(ap
, int32_t);
617 str
= va_arg(ap
, char *);
618 } else if (arg_type
== FSE_ARG_VNODE
) {
621 vp
= va_arg(ap
, struct vnode
*);
622 pathbuff
= get_pathbuff();
623 pathbuff_len
= MAXPATHLEN
;
624 if (vn_getpath(vp
, pathbuff
, &pathbuff_len
) != 0 || pathbuff
[0] == '\0') {
625 release_pathbuff(pathbuff
);
634 // This will go through all events and find one that we
635 // can combine with (hopefully), or "collapse" into (i.e
636 // it has the same parent) or in the worst case we have
637 // to "recycle" an event which means that it will combine
638 // two other events and return us the now unused event.
639 // failing all that, find_an_event() could still return
640 // null and if it does then we have a catastrophic dropped
643 kfse
= find_an_event(str
, len
, NULL
, &reuse_type
, &longest_match_len
);
648 unlock_fs_event_list();
651 for(i
=0; i
< MAX_WATCHERS
; i
++) {
652 watcher
= watcher_table
[i
];
653 if (watcher
== NULL
) {
657 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
658 fsevents_wakeup(watcher
);
660 unlock_watch_table();
663 struct timeval current_tv
;
667 // only print a message at most once every 5 seconds
668 microuptime(¤t_tv
);
669 if ((current_tv
.tv_sec
- last_print
.tv_sec
) > 10) {
671 void *junkptr
=zalloc_noblock(event_zone
), *listhead
=kfse_list_head
.lh_first
;
673 printf("add_fsevent: event queue is full! dropping events (num dropped events: %d; num events outstanding: %d).\n", num_dropped
, num_events_outstanding
);
674 printf("add_fsevent: kfse_list head %p ; num_pending_rename %d\n", listhead
, num_pending_rename
);
675 printf("add_fsevent: zalloc sez: %p\n", junkptr
);
676 printf("add_fsevent: event_zone info: %d 0x%x\n", ((int *)event_zone
)[0], ((int *)event_zone
)[1]);
677 for(ii
=0; ii
< MAX_WATCHERS
; ii
++) {
678 if (watcher_table
[ii
] == NULL
) {
682 printf("add_fsevent: watcher %p: num dropped %d rd %4d wr %4d q_size %4d flags 0x%x\n",
683 watcher_table
[ii
], watcher_table
[ii
]->num_dropped
,
684 watcher_table
[ii
]->rd
, watcher_table
[ii
]->wr
,
685 watcher_table
[ii
]->eventq_size
, watcher_table
[ii
]->flags
);
688 last_print
= current_tv
;
690 zfree(event_zone
, junkptr
);
696 release_pathbuff(pathbuff
);
703 if ((type
== FSE_RENAME
|| type
== FSE_EXCHANGE
) && reuse_type
!= KFSE_RECYCLED
) {
704 panic("add_fsevent: type == %d but reuse type == %d!\n", type
, reuse_type
);
705 } else if ((kfse
->type
== FSE_RENAME
|| kfse
->type
== FSE_EXCHANGE
) && kfse
->dest
== NULL
) {
706 panic("add_fsevent: bogus kfse %p (type %d, but dest is NULL)\n", kfse
, kfse
->type
);
707 } else if (kfse
->type
== FSE_RENAME
|| kfse
->type
== FSE_EXCHANGE
) {
708 panic("add_fsevent: we should never re-use rename events (kfse %p reuse type %d)!\n", kfse
, reuse_type
);
711 if (reuse_type
== KFSE_COLLAPSED
) {
713 const char *tmp_ptr
, *new_str
;
716 // if we collapsed and have a string we have to chop off the
717 // tail component of the pathname to get the parent.
719 // NOTE: it is VERY IMPORTANT that we leave the trailing slash
720 // on the pathname. user-level code depends on this.
722 if (str
[0] == '\0' || longest_match_len
<= 1) {
723 printf("add_fsevent: strange state (str %s / longest_match_len %d)\n", str
, longest_match_len
);
724 if (longest_match_len
< 0) {
725 panic("add_fsevent: longest_match_len %d\n", longest_match_len
);
728 // chop off the tail component if it's not the
729 // first character...
730 if (longest_match_len
> 1) {
731 str
[longest_match_len
] = '\0';
732 } else if (longest_match_len
== 0) {
733 longest_match_len
= 1;
736 new_str
= vfs_addname(str
, longest_match_len
, 0, 0);
737 if (new_str
== NULL
|| new_str
[0] == '\0') {
738 panic("add_fsevent: longest match is strange (new_str %p).\n", new_str
);
741 lck_rw_lock_exclusive(&event_handling_lock
);
743 kfse
->len
= longest_match_len
;
751 lck_rw_unlock_exclusive(&event_handling_lock
);
753 vfs_removename(tmp_ptr
);
755 panic("add_fsevent: don't have a vnode or a string pointer (kfse %p)\n", kfse
);
759 if (reuse_type
== KFSE_RECYCLED
&& (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
)) {
761 // if we're recycling this kfse and we have a rename or
762 // exchange event then we need to also get an event for
766 // only happens if we allocated one but then failed
767 // for kfse_dest (and thus free'd the first one we
769 kfse_dest
= zalloc_noblock(event_zone
);
770 if (kfse_dest
!= NULL
) {
771 memset(kfse_dest
, 0, sizeof(kfs_event
));
772 kfse_dest
->refcount
= 1;
773 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse_dest
->flags
);
779 if (kfse_dest
== NULL
) {
780 int dest_reuse_type
, dest_match_len
;
782 kfse_dest
= find_an_event(NULL
, 0, kfse
, &dest_reuse_type
, &dest_match_len
);
784 if (kfse_dest
== NULL
) {
785 // nothing we can do... gotta bail out
789 if (dest_reuse_type
!= KFSE_RECYCLED
) {
790 panic("add_fsevent: type == %d but dest_reuse type == %d!\n", type
, dest_reuse_type
);
797 // Here we check for some fast-path cases so that we can
798 // jump over the normal initialization and just get on
799 // with delivering the event. These cases are when we're
800 // combining/collapsing an event and so basically there is
801 // no more work to do (aside from a little book-keeping)
803 if (str
&& kfse
->len
!= 0) {
805 OSAddAtomic(1, &kfse
->refcount
);
808 if (reuse_type
== KFSE_COMBINED
) {
809 num_combined_events
++;
810 } else if (reuse_type
== KFSE_COLLAPSED
) {
811 num_added_to_parent
++;
813 } else if (reuse_type
!= KFSE_RECYCLED
) {
814 panic("add_fsevent: I'm so confused! (reuse_type %d str %p kfse->len %d)\n",
815 reuse_type
, str
, kfse
->len
);
822 if (kfse
->refcount
< 1) {
823 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__
, kfse
->refcount
);
826 last_event_ptr
= kfse
;
827 unlock_fs_event_list();
828 goto normal_delivery
;
830 } else if (reuse_type
== KFSE_RECYCLED
|| reuse_type
== KFSE_COMBINED
) {
833 // If we're here we have to clear out the kfs_event(s)
834 // that we were given by find_an_event() and set it
835 // up to be re-filled in by the normal code path.
839 need_event_unlock
= 1;
840 lck_rw_lock_exclusive(&event_handling_lock
);
842 OSAddAtomic(1, &kfse
->refcount
);
844 if (kfse
->refcount
< 1) {
845 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__
, kfse
->refcount
);
848 if (kfse
->len
== 0) {
849 panic("%s:%d: no more fref.vp\n", __FILE__
, __LINE__
);
850 // vnode_rele_ext(kfse->fref.vp, O_EVTONLY, 0);
852 vfs_removename(kfse
->str
);
857 if (kfse
->kevent_list
.le_prev
!= NULL
) {
858 num_events_outstanding
--;
859 if (kfse
->type
== FSE_RENAME
) {
860 num_pending_rename
--;
862 LIST_REMOVE(kfse
, kevent_list
);
863 memset(&kfse
->kevent_list
, 0, sizeof(kfse
->kevent_list
));
866 kfse
->flags
= 0 | KFSE_RECYCLED_EVENT
;
869 OSAddAtomic(1, &kfse_dest
->refcount
);
870 kfse_dest
->flags
= 0 | KFSE_RECYCLED_EVENT
;
872 if (did_alloc
== 0) {
873 if (kfse_dest
->len
== 0) {
874 panic("%s:%d: no more fref.vp\n", __FILE__
, __LINE__
);
875 // vnode_rele_ext(kfse_dest->fref.vp, O_EVTONLY, 0);
877 vfs_removename(kfse_dest
->str
);
880 kfse_dest
->str
= NULL
;
882 if (kfse_dest
->kevent_list
.le_prev
!= NULL
) {
883 num_events_outstanding
--;
884 LIST_REMOVE(kfse_dest
, kevent_list
);
885 memset(&kfse_dest
->kevent_list
, 0, sizeof(kfse_dest
->kevent_list
));
888 if (kfse_dest
->dest
) {
889 panic("add_fsevent: should never recycle a rename event! kfse %p\n", kfse
);
894 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse
->flags
);
896 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse_dest
->flags
);
899 goto process_normally
;
903 if (reuse_type
!= 0) {
904 panic("fsevents: we have a reuse_type (%d) but are about to clear out kfse %p\n", reuse_type
, kfse
);
908 // we only want to do this for brand new events, not
909 // events which have been recycled.
911 memset(kfse
, 0, sizeof(kfs_event
));
913 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse
->flags
);
916 last_event_ptr
= kfse
;
919 kfse
->pid
= p
->p_pid
;
920 if (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
) {
921 if (need_event_unlock
== 0) {
922 memset(kfse_dest
, 0, sizeof(kfs_event
));
923 kfse_dest
->refcount
= 1;
924 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse_dest
->flags
);
926 kfse_dest
->type
= type
;
927 kfse_dest
->pid
= p
->p_pid
;
928 kfse_dest
->abstime
= now
;
930 kfse
->dest
= kfse_dest
;
933 num_events_outstanding
++;
934 if (kfse
->type
== FSE_RENAME
) {
935 num_pending_rename
++;
937 LIST_INSERT_HEAD(&kfse_list_head
, kfse
, kevent_list
);
939 if (kfse
->refcount
< 1) {
940 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__
, kfse
->refcount
);
943 unlock_fs_event_list(); // at this point it's safe to unlock
946 // now process the arguments passed in and copy them into
949 if (need_event_unlock
== 0) {
950 lck_rw_lock_shared(&event_handling_lock
);
954 for(arg_type
=va_arg(ap
, int32_t); arg_type
!= FSE_ARG_DONE
; arg_type
=va_arg(ap
, int32_t))
957 case FSE_ARG_VNODE
: {
958 // this expands out into multiple arguments to the client
960 struct vnode_attr va
;
962 if (kfse
->str
!= NULL
) {
966 vp
= va_arg(ap
, struct vnode
*);
968 panic("add_fsevent: you can't pass me a NULL vnode ptr (type %d)!\n",
973 VATTR_WANTED(&va
, va_fsid
);
974 VATTR_WANTED(&va
, va_fileid
);
975 VATTR_WANTED(&va
, va_mode
);
976 VATTR_WANTED(&va
, va_uid
);
977 VATTR_WANTED(&va
, va_gid
);
978 if ((ret
= vnode_getattr(vp
, &va
, vfs_context_kernel())) != 0) {
979 // printf("add_fsevent: failed to getattr on vp %p (%d)\n", cur->fref.vp, ret);
982 if (need_event_unlock
== 0) {
983 // then we only grabbed it shared
984 lck_rw_unlock_shared(&event_handling_lock
);
989 cur
->dev
= dev
= (dev_t
)va
.va_fsid
;
990 cur
->ino
= (ino64_t
)va
.va_fileid
;
991 cur
->mode
= (int32_t)vnode_vttoif(vnode_vtype(vp
)) | va
.va_mode
;
992 cur
->uid
= va
.va_uid
;
993 cur
->gid
= va
.va_gid
;
995 // if we haven't gotten the path yet, get it.
996 if (pathbuff
== NULL
) {
997 pathbuff
= get_pathbuff();
998 pathbuff_len
= MAXPATHLEN
;
1001 if ((ret
= vn_getpath(vp
, pathbuff
, &pathbuff_len
)) != 0 || pathbuff
[0] == '\0') {
1002 struct vnode
*orig_vp
= vp
;
1004 if (ret
!= ENOSPC
) {
1005 printf("add_fsevent: unable to get path for vp %p (%s; ret %d; type %d)\n",
1006 vp
, vp
->v_name
? vp
->v_name
: "-UNKNOWN-FILE", ret
, type
);
1009 cur
->flags
|= KFSE_CONTAINS_DROPPED_EVENTS
;
1012 if (vp
->v_parent
!= NULL
) {
1014 } else if (vp
->v_mount
) {
1015 strlcpy(pathbuff
, vp
->v_mount
->mnt_vfsstat
.f_mntonname
, MAXPATHLEN
);
1025 pathbuff_len
= MAXPATHLEN
;
1026 ret
= vn_getpath(vp
, pathbuff
, &pathbuff_len
);
1027 } while (ret
== ENOSPC
);
1029 if (ret
!= 0 || vp
== NULL
) {
1030 printf("add_fsevent: unabled to get a path for vp %p. dropping the event.\n", orig_vp
);
1032 if (need_event_unlock
== 0) {
1033 // then we only grabbed it shared
1034 lck_rw_unlock_shared(&event_handling_lock
);
1041 // store the path by adding it to the global string table
1042 cur
->len
= pathbuff_len
;
1043 cur
->str
= vfs_addname(pathbuff
, pathbuff_len
, 0, 0);
1044 if (cur
->str
== NULL
|| cur
->str
[0] == '\0') {
1045 panic("add_fsevent: was not able to add path %s to event %p.\n", pathbuff
, cur
);
1048 release_pathbuff(pathbuff
);
1054 case FSE_ARG_FINFO
: {
1057 fse
= va_arg(ap
, fse_info
*);
1059 cur
->dev
= dev
= (dev_t
)fse
->dev
;
1060 cur
->ino
= (ino64_t
)fse
->ino
;
1061 cur
->mode
= (int32_t)fse
->mode
;
1062 cur
->uid
= (uid_t
)fse
->uid
;
1063 cur
->gid
= (uid_t
)fse
->gid
;
1064 // if it's a hard-link and this is the last link, flag it
1065 if ((fse
->mode
& FSE_MODE_HLINK
) && fse
->nlink
== 0) {
1066 cur
->mode
|= FSE_MODE_LAST_HLINK
;
1068 if (cur
->mode
& FSE_TRUNCATED_PATH
) {
1069 cur
->flags
|= KFSE_CONTAINS_DROPPED_EVENTS
;
1070 cur
->mode
&= ~FSE_TRUNCATED_PATH
;
1075 case FSE_ARG_STRING
:
1076 if (kfse
->str
!= NULL
) {
1080 cur
->len
= (int16_t)(va_arg(ap
, int32_t) & 0x7fff);
1081 if (cur
->len
>= 1) {
1082 cur
->str
= vfs_addname(va_arg(ap
, char *), cur
->len
, 0, 0);
1084 printf("add_fsevent: funny looking string length: %d\n", (int)cur
->len
);
1086 cur
->str
= vfs_addname("/", cur
->len
, 0, 0);
1088 if (cur
->str
[0] == 0) {
1089 printf("add_fsevent: bogus looking string (len %d)\n", cur
->len
);
1094 printf("add_fsevent: unknown type %d\n", arg_type
);
1095 // just skip one 32-bit word and hope we sync up...
1096 (void)va_arg(ap
, int32_t);
1101 OSBitAndAtomic16(~KFSE_BEING_CREATED
, &kfse
->flags
);
1103 OSBitAndAtomic16(~KFSE_BEING_CREATED
, &kfse_dest
->flags
);
1106 if (need_event_unlock
== 0) {
1107 // then we only grabbed it shared
1108 lck_rw_unlock_shared(&event_handling_lock
);
1112 // unlock this here so we don't hold it across the
1113 // event delivery loop.
1114 if (need_event_unlock
) {
1115 lck_rw_unlock_exclusive(&event_handling_lock
);
1116 need_event_unlock
= 0;
1120 // now we have to go and let everyone know that
1121 // is interested in this type of event
1125 for(i
=0; i
< MAX_WATCHERS
; i
++) {
1126 watcher
= watcher_table
[i
];
1127 if (watcher
== NULL
) {
1131 if ( watcher
->event_list
[type
] == FSE_REPORT
1132 && watcher_cares_about_dev(watcher
, dev
)) {
1134 if (watcher_add_event(watcher
, kfse
) != 0) {
1135 watcher
->num_dropped
++;
1139 if (kfse
->refcount
< 1) {
1140 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__
, kfse
->refcount
);
1144 unlock_watch_table();
1147 // have to check if this needs to be unlocked (in
1148 // case we came here from an error handling path)
1149 if (need_event_unlock
) {
1150 lck_rw_unlock_exclusive(&event_handling_lock
);
1151 need_event_unlock
= 0;
1155 release_pathbuff(pathbuff
);
1159 release_event_ref(kfse
);
1166 release_event_ref(kfs_event
*kfse
)
1169 kfs_event copy
, dest_copy
;
1172 old_refcount
= OSAddAtomic(-1, &kfse
->refcount
);
1173 if (old_refcount
> 1) {
1177 lock_fs_event_list();
1178 if (last_event_ptr
== kfse
) {
1179 last_event_ptr
= NULL
;
1180 last_event_type
= -1;
1181 last_coalesced_time
= 0;
1184 if (kfse
->refcount
< 0) {
1185 panic("release_event_ref: bogus kfse refcount %d\n", kfse
->refcount
);
1188 if (kfse
->refcount
> 0 || kfse
->type
== FSE_INVALID
) {
1189 // This is very subtle. Either of these conditions can
1190 // be true if an event got recycled while we were waiting
1191 // on the fs_event_list lock or the event got recycled,
1192 // delivered, _and_ free'd by someone else while we were
1193 // waiting on the fs event list lock. In either case
1194 // we need to just unlock the list and return without
1195 // doing anything because if the refcount is > 0 then
1196 // someone else will take care of free'ing it and when
1197 // the kfse->type is invalid then someone else already
1198 // has handled free'ing the event (while we were blocked
1199 // on the event list lock).
1201 unlock_fs_event_list();
1206 // make a copy of this so we can free things without
1207 // holding the fs_event_buf lock
1210 if (kfse
->dest
&& OSAddAtomic(-1, &kfse
->dest
->refcount
) == 1) {
1211 dest_copy
= *kfse
->dest
;
1213 dest_copy
.str
= NULL
;
1215 dest_copy
.type
= FSE_INVALID
;
1218 kfse
->pid
= kfse
->type
; // save this off for debugging...
1219 kfse
->uid
= (uid_t
)(long)kfse
->str
; // save this off for debugging...
1220 kfse
->gid
= (gid_t
)(long)current_thread();
1222 kfse
->str
= (char *)0xdeadbeef; // XXXdbg - catch any cheaters...
1224 if (dest_copy
.type
!= FSE_INVALID
) {
1225 kfse
->dest
->str
= (char *)0xbadc0de; // XXXdbg - catch any cheaters...
1226 kfse
->dest
->type
= FSE_INVALID
;
1228 if (kfse
->dest
->kevent_list
.le_prev
!= NULL
) {
1229 num_events_outstanding
--;
1230 LIST_REMOVE(kfse
->dest
, kevent_list
);
1231 memset(&kfse
->dest
->kevent_list
, 0xa5, sizeof(kfse
->dest
->kevent_list
));
1234 zfree(event_zone
, kfse
->dest
);
1237 // mark this fsevent as invalid
1242 kfse
->type
= FSE_INVALID
;
1244 if (kfse
->kevent_list
.le_prev
!= NULL
) {
1245 num_events_outstanding
--;
1246 if (otype
== FSE_RENAME
) {
1247 num_pending_rename
--;
1249 LIST_REMOVE(kfse
, kevent_list
);
1250 memset(&kfse
->kevent_list
, 0, sizeof(kfse
->kevent_list
));
1254 zfree(event_zone
, kfse
);
1256 unlock_fs_event_list();
1258 // if we have a pointer in the union
1260 if (copy
.len
== 0) { // and it's not a string
1261 panic("%s:%d: no more fref.vp!\n", __FILE__
, __LINE__
);
1262 // vnode_rele_ext(copy.fref.vp, O_EVTONLY, 0);
1263 } else { // else it's a string
1264 vfs_removename(copy
.str
);
1268 if (dest_copy
.type
!= FSE_INVALID
&& dest_copy
.str
) {
1269 if (dest_copy
.len
== 0) {
1270 panic("%s:%d: no more fref.vp!\n", __FILE__
, __LINE__
);
1271 // vnode_rele_ext(dest_copy.fref.vp, O_EVTONLY, 0);
1273 vfs_removename(dest_copy
.str
);
1280 add_watcher(int8_t *event_list
, int32_t num_events
, int32_t eventq_size
, fs_event_watcher
**watcher_out
)
1283 fs_event_watcher
*watcher
;
1285 if (eventq_size
<= 0 || eventq_size
> 100*MAX_KFS_EVENTS
) {
1286 eventq_size
= MAX_KFS_EVENTS
;
1289 // Note: the event_queue follows the fs_event_watcher struct
1290 // in memory so we only have to do one allocation
1293 sizeof(fs_event_watcher
) + eventq_size
* sizeof(kfs_event
*),
1295 if (watcher
== NULL
) {
1299 watcher
->event_list
= event_list
;
1300 watcher
->num_events
= num_events
;
1301 watcher
->devices_not_to_watch
= NULL
;
1302 watcher
->num_devices
= 0;
1304 watcher
->event_queue
= (kfs_event
**)&watcher
[1];
1305 watcher
->eventq_size
= eventq_size
;
1308 watcher
->blockers
= 0;
1309 watcher
->num_readers
= 0;
1310 watcher
->max_event_id
= 0;
1311 watcher
->fseh
= NULL
;
1313 watcher
->num_dropped
= 0; // XXXdbg - debugging
1317 // now update the global list of who's interested in
1318 // events of a particular type...
1319 for(i
=0; i
< num_events
; i
++) {
1320 if (event_list
[i
] != FSE_IGNORE
&& i
< FSE_MAX_EVENTS
) {
1321 fs_event_type_watchers
[i
]++;
1325 for(i
=0; i
< MAX_WATCHERS
; i
++) {
1326 if (watcher_table
[i
] == NULL
) {
1328 watcher_table
[i
] = watcher
;
1333 if (i
> MAX_WATCHERS
) {
1334 printf("fsevents: too many watchers!\n");
1335 unlock_watch_table();
1339 unlock_watch_table();
1341 *watcher_out
= watcher
;
1349 remove_watcher(fs_event_watcher
*target
)
1351 int i
, j
, counter
=0;
1352 fs_event_watcher
*watcher
;
1357 for(j
=0; j
< MAX_WATCHERS
; j
++) {
1358 watcher
= watcher_table
[j
];
1359 if (watcher
!= target
) {
1363 watcher_table
[j
] = NULL
;
1365 for(i
=0; i
< watcher
->num_events
; i
++) {
1366 if (watcher
->event_list
[i
] != FSE_IGNORE
&& i
< FSE_MAX_EVENTS
) {
1367 fs_event_type_watchers
[i
]--;
1371 if (watcher
->flags
& WATCHER_CLOSING
) {
1372 unlock_watch_table();
1376 // 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);
1377 watcher
->flags
|= WATCHER_CLOSING
;
1378 OSAddAtomic(1, &watcher
->num_readers
);
1380 unlock_watch_table();
1382 while (watcher
->num_readers
> 1 && counter
++ < 5000) {
1383 fsevents_wakeup(watcher
); // in case they're asleep
1385 tsleep(watcher
, PRIBIO
, "fsevents-close", 1);
1387 if (counter
++ >= 5000) {
1388 // printf("fsevents: close: still have readers! (%d)\n", watcher->num_readers);
1389 panic("fsevents: close: still have readers! (%d)\n", watcher
->num_readers
);
1392 // drain the event_queue
1393 while(watcher
->rd
!= watcher
->wr
) {
1394 lck_rw_lock_shared(&event_handling_lock
);
1396 kfse
= watcher
->event_queue
[watcher
->rd
];
1397 if (kfse
->type
== FSE_INVALID
|| kfse
->refcount
< 1) {
1398 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
);
1401 lck_rw_unlock_shared(&event_handling_lock
);
1403 watcher
->rd
= (watcher
->rd
+1) % watcher
->eventq_size
;
1406 release_event_ref(kfse
);
1410 if (watcher
->event_list
) {
1411 FREE(watcher
->event_list
, M_TEMP
);
1412 watcher
->event_list
= NULL
;
1414 if (watcher
->devices_not_to_watch
) {
1415 FREE(watcher
->devices_not_to_watch
, M_TEMP
);
1416 watcher
->devices_not_to_watch
= NULL
;
1418 FREE(watcher
, M_TEMP
);
1423 unlock_watch_table();
1427 #define EVENT_DELAY_IN_MS 10
1428 static thread_call_t event_delivery_timer
= NULL
;
1429 static int timer_set
= 0;
1433 delayed_event_delivery(__unused
void *param0
, __unused
void *param1
)
1439 for(i
=0; i
< MAX_WATCHERS
; i
++) {
1440 if (watcher_table
[i
] != NULL
&& watcher_table
[i
]->rd
!= watcher_table
[i
]->wr
) {
1441 fsevents_wakeup(watcher_table
[i
]);
1447 unlock_watch_table();
1452 // The watch table must be locked before calling this function.
1455 schedule_event_wakeup(void)
1459 if (event_delivery_timer
== NULL
) {
1460 event_delivery_timer
= thread_call_allocate((thread_call_func_t
)delayed_event_delivery
, NULL
);
1463 clock_interval_to_deadline(EVENT_DELAY_IN_MS
, 1000 * 1000, &deadline
);
1465 thread_call_enter_delayed(event_delivery_timer
, deadline
);
1471 #define MAX_NUM_PENDING 16
1474 // NOTE: the watch table must be locked before calling
1478 watcher_add_event(fs_event_watcher
*watcher
, kfs_event
*kfse
)
1480 if (kfse
->abstime
> watcher
->max_event_id
) {
1481 watcher
->max_event_id
= kfse
->abstime
;
1484 if (((watcher
->wr
+ 1) % watcher
->eventq_size
) == watcher
->rd
) {
1485 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
1486 fsevents_wakeup(watcher
);
1490 OSAddAtomic(1, &kfse
->refcount
);
1491 watcher
->event_queue
[watcher
->wr
] = kfse
;
1493 watcher
->wr
= (watcher
->wr
+ 1) % watcher
->eventq_size
;
1496 // wake up the watcher if there are more than MAX_NUM_PENDING events.
1497 // otherwise schedule a timer (if one isn't already set) which will
1498 // send any pending events if no more are received in the next
1499 // EVENT_DELAY_IN_MS milli-seconds.
1501 if ( (watcher
->rd
< watcher
->wr
&& (watcher
->wr
- watcher
->rd
) > MAX_NUM_PENDING
)
1502 || (watcher
->rd
> watcher
->wr
&& (watcher
->wr
+ watcher
->eventq_size
- watcher
->rd
) > MAX_NUM_PENDING
)) {
1504 fsevents_wakeup(watcher
);
1506 } else if (timer_set
== 0) {
1508 schedule_event_wakeup();
1515 fill_buff(uint16_t type
, int32_t size
, const void *data
,
1516 char *buff
, int32_t *_buff_idx
, int32_t buff_sz
,
1519 int32_t amt
, error
= 0, buff_idx
= *_buff_idx
;
1523 // the +1 on the size is to guarantee that the main data
1524 // copy loop will always copy at least 1 byte
1526 if ((buff_sz
- buff_idx
) <= (int)(2*sizeof(uint16_t) + 1)) {
1527 if (buff_idx
> uio_resid(uio
)) {
1532 error
= uiomove(buff
, buff_idx
, uio
);
1539 // copy out the header (type & size)
1540 memcpy(&buff
[buff_idx
], &type
, sizeof(uint16_t));
1541 buff_idx
+= sizeof(uint16_t);
1543 tmp
= size
& 0xffff;
1544 memcpy(&buff
[buff_idx
], &tmp
, sizeof(uint16_t));
1545 buff_idx
+= sizeof(uint16_t);
1547 // now copy the body of the data, flushing along the way
1548 // if the buffer fills up.
1551 amt
= (size
< (buff_sz
- buff_idx
)) ? size
: (buff_sz
- buff_idx
);
1552 memcpy(&buff
[buff_idx
], data
, amt
);
1556 data
= (const char *)data
+ amt
;
1557 if (size
> (buff_sz
- buff_idx
)) {
1558 if (buff_idx
> uio_resid(uio
)) {
1562 error
= uiomove(buff
, buff_idx
, uio
);
1569 if (amt
== 0) { // just in case...
1575 *_buff_idx
= buff_idx
;
1581 static int copy_out_kfse(fs_event_watcher
*watcher
, kfs_event
*kfse
, struct uio
*uio
) __attribute__((noinline
));
1584 copy_out_kfse(fs_event_watcher
*watcher
, kfs_event
*kfse
, struct uio
*uio
)
1593 if (kfse
->type
== FSE_INVALID
) {
1594 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
);
1597 if (kfse
->flags
& KFSE_BEING_CREATED
) {
1601 if (kfse
->type
== FSE_RENAME
&& kfse
->dest
== NULL
) {
1603 // This can happen if an event gets recycled but we had a
1604 // pointer to it in our event queue. The event is the
1605 // destination of a rename which we'll process separately
1606 // (that is, another kfse points to this one so it's ok
1607 // to skip this guy because we'll process it when we process
1613 if (watcher
->flags
& WATCHER_WANTS_EXTENDED_INFO
) {
1615 type
= (kfse
->type
& 0xfff);
1617 if (kfse
->flags
& KFSE_CONTAINS_DROPPED_EVENTS
) {
1618 type
|= (FSE_CONTAINS_DROPPED_EVENTS
<< FSE_FLAG_SHIFT
);
1619 } else if (kfse
->flags
& KFSE_COMBINED_EVENTS
) {
1620 type
|= (FSE_COMBINED_EVENTS
<< FSE_FLAG_SHIFT
);
1624 type
= (int32_t)kfse
->type
;
1627 // copy out the type of the event
1628 memcpy(evbuff
, &type
, sizeof(int32_t));
1629 evbuff_idx
+= sizeof(int32_t);
1631 // copy out the pid of the person that generated the event
1632 memcpy(&evbuff
[evbuff_idx
], &kfse
->pid
, sizeof(pid_t
));
1633 evbuff_idx
+= sizeof(pid_t
);
1639 if (cur
->str
== NULL
|| cur
->str
[0] == '\0') {
1640 printf("copy_out_kfse:2: empty/short path (%s)\n", cur
->str
);
1641 error
= fill_buff(FSE_ARG_STRING
, 2, "/", evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1643 error
= fill_buff(FSE_ARG_STRING
, cur
->len
, cur
->str
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1649 if (cur
->dev
== 0 && cur
->ino
== 0) {
1650 // this happens when a rename event happens and the
1651 // destination of the rename did not previously exist.
1652 // it thus has no other file info so skip copying out
1653 // the stuff below since it isn't initialized
1658 if (watcher
->flags
& WATCHER_WANTS_COMPACT_EVENTS
) {
1661 finfo_size
= sizeof(dev_t
) + sizeof(ino64_t
) + sizeof(int32_t) + sizeof(uid_t
) + sizeof(gid_t
);
1662 error
= fill_buff(FSE_ARG_FINFO
, finfo_size
, &cur
->ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1669 error
= fill_buff(FSE_ARG_DEV
, sizeof(dev_t
), &cur
->dev
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1674 ino
= (ino_t
)cur
->ino
;
1675 error
= fill_buff(FSE_ARG_INO
, sizeof(ino_t
), &ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1680 error
= fill_buff(FSE_ARG_MODE
, sizeof(int32_t), &cur
->mode
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1685 error
= fill_buff(FSE_ARG_UID
, sizeof(uid_t
), &cur
->uid
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1690 error
= fill_buff(FSE_ARG_GID
, sizeof(gid_t
), &cur
->gid
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1703 // very last thing: the time stamp
1704 error
= fill_buff(FSE_ARG_INT64
, sizeof(uint64_t), &cur
->abstime
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1709 // check if the FSE_ARG_DONE will fit
1710 if (sizeof(uint16_t) > sizeof(evbuff
) - evbuff_idx
) {
1711 if (evbuff_idx
> uio_resid(uio
)) {
1715 error
= uiomove(evbuff
, evbuff_idx
, uio
);
1722 tmp16
= FSE_ARG_DONE
;
1723 memcpy(&evbuff
[evbuff_idx
], &tmp16
, sizeof(uint16_t));
1724 evbuff_idx
+= sizeof(uint16_t);
1726 // flush any remaining data in the buffer (and hopefully
1727 // in most cases this is the only uiomove we'll do)
1728 if (evbuff_idx
> uio_resid(uio
)) {
1731 error
= uiomove(evbuff
, evbuff_idx
, uio
);
1742 fmod_watch(fs_event_watcher
*watcher
, struct uio
*uio
)
1745 user_ssize_t last_full_event_resid
;
1749 last_full_event_resid
= uio_resid(uio
);
1751 // need at least 2048 bytes of space (maxpathlen + 1 event buf)
1752 if (uio_resid(uio
) < 2048 || watcher
== NULL
) {
1756 if (watcher
->flags
& WATCHER_CLOSING
) {
1760 if (OSAddAtomic(1, &watcher
->num_readers
) != 0) {
1761 // don't allow multiple threads to read from the fd at the same time
1762 OSAddAtomic(-1, &watcher
->num_readers
);
1766 if (watcher
->rd
== watcher
->wr
) {
1767 if (watcher
->flags
& WATCHER_CLOSING
) {
1768 OSAddAtomic(-1, &watcher
->num_readers
);
1771 OSAddAtomic(1, &watcher
->blockers
);
1773 // there's nothing to do, go to sleep
1774 error
= tsleep((caddr_t
)watcher
, PUSER
|PCATCH
, "fsevents_empty", 0);
1776 OSAddAtomic(-1, &watcher
->blockers
);
1778 if (error
!= 0 || (watcher
->flags
& WATCHER_CLOSING
)) {
1779 OSAddAtomic(-1, &watcher
->num_readers
);
1784 // if we dropped events, return that as an event first
1785 if (watcher
->flags
& WATCHER_DROPPED_EVENTS
) {
1786 int32_t val
= FSE_EVENTS_DROPPED
;
1788 error
= uiomove((caddr_t
)&val
, sizeof(int32_t), uio
);
1790 val
= 0; // a fake pid
1791 error
= uiomove((caddr_t
)&val
, sizeof(int32_t), uio
);
1793 tmp16
= FSE_ARG_DONE
; // makes it a consistent msg
1794 error
= uiomove((caddr_t
)&tmp16
, sizeof(int16_t), uio
);
1796 last_full_event_resid
= uio_resid(uio
);
1800 OSAddAtomic(-1, &watcher
->num_readers
);
1804 watcher
->flags
&= ~WATCHER_DROPPED_EVENTS
;
1807 while (uio_resid(uio
) > 0 && watcher
->rd
!= watcher
->wr
) {
1808 if (watcher
->flags
& WATCHER_CLOSING
) {
1813 // check if the event is something of interest to us
1814 // (since it may have been recycled/reused and changed
1815 // its type or which device it is for)
1817 lck_rw_lock_shared(&event_handling_lock
);
1819 kfse
= watcher
->event_queue
[watcher
->rd
];
1820 if (kfse
->type
== FSE_INVALID
|| kfse
->refcount
< 1) {
1821 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
);
1824 if (watcher
->event_list
[kfse
->type
] == FSE_REPORT
&& watcher_cares_about_dev(watcher
, kfse
->dev
)) {
1826 if (last_event_ptr
== kfse
) {
1827 last_event_ptr
= NULL
;
1828 last_event_type
= -1;
1829 last_coalesced_time
= 0;
1831 error
= copy_out_kfse(watcher
, kfse
, uio
);
1833 // if an event won't fit or encountered an error while
1834 // we were copying it out, then backup to the last full
1835 // event and just bail out. if the error was ENOENT
1836 // then we can continue regular processing, otherwise
1837 // we should unlock things and return.
1838 uio_setresid(uio
, last_full_event_resid
);
1839 if (error
!= ENOENT
) {
1840 lck_rw_unlock_shared(&event_handling_lock
);
1846 last_full_event_resid
= uio_resid(uio
);
1849 lck_rw_unlock_shared(&event_handling_lock
);
1851 watcher
->rd
= (watcher
->rd
+ 1) % watcher
->eventq_size
;
1854 if (kfse
->type
== FSE_INVALID
|| kfse
->refcount
< 1) {
1855 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
);
1858 release_event_ref(kfse
);
1862 OSAddAtomic(-1, &watcher
->num_readers
);
1868 // release any references we might have on vnodes which are
1869 // the mount point passed to us (so that it can be cleanly
1872 // since we don't want to lose the events we'll convert the
1873 // vnode refs to full paths.
1876 fsevent_unmount(__unused
struct mount
*mp
)
1878 // we no longer maintain pointers to vnodes so
1879 // there is nothing to do...
1884 // /dev/fsevents device code
1886 static int fsevents_installed
= 0;
1888 typedef struct fsevent_handle
{
1891 fs_event_watcher
*watcher
;
1892 struct klist knotes
;
1896 #define FSEH_CLOSING 0x0001
1899 fseventsf_read(struct fileproc
*fp
, struct uio
*uio
,
1900 __unused
int flags
, __unused vfs_context_t ctx
)
1902 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
1905 error
= fmod_watch(fseh
->watcher
, uio
);
1912 fseventsf_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1913 __unused
int flags
, __unused vfs_context_t ctx
)
1918 #pragma pack(push, 4)
1919 typedef struct ext_fsevent_dev_filter_args
{
1920 uint32_t num_devices
;
1921 user_addr_t devices
;
1922 } ext_fsevent_dev_filter_args
;
1925 typedef struct old_fsevent_dev_filter_args
{
1926 uint32_t num_devices
;
1928 } old_fsevent_dev_filter_args
;
1930 #define OLD_FSEVENTS_DEVICE_FILTER _IOW('s', 100, old_fsevent_dev_filter_args)
1931 #define NEW_FSEVENTS_DEVICE_FILTER _IOW('s', 100, ext_fsevent_dev_filter_args)
1934 /* need this in spite of the padding due to alignment of devices */
1935 typedef struct fsevent_dev_filter_args32
{
1936 uint32_t num_devices
;
1939 } fsevent_dev_filter_args32
;
1943 fseventsf_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
, vfs_context_t ctx
)
1945 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
1947 ext_fsevent_dev_filter_args
*devfilt_args
, _devfilt_args
;
1949 if (proc_is64bit(vfs_context_proc(ctx
))) {
1950 devfilt_args
= (ext_fsevent_dev_filter_args
*)data
;
1951 } else if (cmd
== OLD_FSEVENTS_DEVICE_FILTER
) {
1952 old_fsevent_dev_filter_args
*udev_filt_args
= (old_fsevent_dev_filter_args
*)data
;
1954 devfilt_args
= &_devfilt_args
;
1955 memset(devfilt_args
, 0, sizeof(ext_fsevent_dev_filter_args
));
1957 devfilt_args
->num_devices
= udev_filt_args
->num_devices
;
1958 devfilt_args
->devices
= CAST_USER_ADDR_T(udev_filt_args
->devices
);
1961 fsevent_dev_filter_args32
*udev_filt_args
= (fsevent_dev_filter_args32
*)data
;
1963 fsevent_dev_filter_args
*udev_filt_args
= (fsevent_dev_filter_args
*)data
;
1966 devfilt_args
= &_devfilt_args
;
1967 memset(devfilt_args
, 0, sizeof(ext_fsevent_dev_filter_args
));
1969 devfilt_args
->num_devices
= udev_filt_args
->num_devices
;
1970 devfilt_args
->devices
= CAST_USER_ADDR_T(udev_filt_args
->devices
);
1973 OSAddAtomic(1, &fseh
->active
);
1974 if (fseh
->flags
& FSEH_CLOSING
) {
1975 OSAddAtomic(-1, &fseh
->active
);
1984 case FSEVENTS_WANT_COMPACT_EVENTS
: {
1985 fseh
->watcher
->flags
|= WATCHER_WANTS_COMPACT_EVENTS
;
1989 case FSEVENTS_WANT_EXTENDED_INFO
: {
1990 fseh
->watcher
->flags
|= WATCHER_WANTS_EXTENDED_INFO
;
1994 case FSEVENTS_GET_CURRENT_ID
: {
1995 *(uint64_t *)data
= fseh
->watcher
->max_event_id
;
2000 case OLD_FSEVENTS_DEVICE_FILTER
:
2001 case NEW_FSEVENTS_DEVICE_FILTER
: {
2002 int new_num_devices
;
2003 dev_t
*devices_not_to_watch
, *tmp
=NULL
;
2005 if (devfilt_args
->num_devices
> 256) {
2010 new_num_devices
= devfilt_args
->num_devices
;
2011 if (new_num_devices
== 0) {
2012 tmp
= fseh
->watcher
->devices_not_to_watch
;
2015 fseh
->watcher
->devices_not_to_watch
= NULL
;
2016 fseh
->watcher
->num_devices
= new_num_devices
;
2017 unlock_watch_table();
2025 MALLOC(devices_not_to_watch
, dev_t
*,
2026 new_num_devices
* sizeof(dev_t
),
2028 if (devices_not_to_watch
== NULL
) {
2033 ret
= copyin(devfilt_args
->devices
,
2034 (void *)devices_not_to_watch
,
2035 new_num_devices
* sizeof(dev_t
));
2037 FREE(devices_not_to_watch
, M_TEMP
);
2042 fseh
->watcher
->num_devices
= new_num_devices
;
2043 tmp
= fseh
->watcher
->devices_not_to_watch
;
2044 fseh
->watcher
->devices_not_to_watch
= devices_not_to_watch
;
2045 unlock_watch_table();
2059 OSAddAtomic(-1, &fseh
->active
);
2065 fseventsf_select(struct fileproc
*fp
, int which
, __unused
void *wql
, vfs_context_t ctx
)
2067 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
2070 if ((which
!= FREAD
) || (fseh
->watcher
->flags
& WATCHER_CLOSING
)) {
2075 // if there's nothing in the queue, we're not ready
2076 if (fseh
->watcher
->rd
!= fseh
->watcher
->wr
) {
2081 selrecord(vfs_context_proc(ctx
), &fseh
->si
, wql
);
2090 fseventsf_stat(__unused
struct fileproc
*fp
, __unused
struct stat
*sb
, __unused vfs_context_t ctx
)
2097 fseventsf_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
2099 fsevent_handle
*fseh
= (struct fsevent_handle
*)fg
->fg_data
;
2100 fs_event_watcher
*watcher
;
2102 OSBitOrAtomic(FSEH_CLOSING
, &fseh
->flags
);
2103 while (OSAddAtomic(0, &fseh
->active
) > 0) {
2104 tsleep((caddr_t
)fseh
->watcher
, PRIBIO
, "fsevents-close", 1);
2107 watcher
= fseh
->watcher
;
2109 fseh
->watcher
= NULL
;
2111 remove_watcher(watcher
);
2118 filt_fsevent_detach(struct knote
*kn
)
2120 fsevent_handle
*fseh
= (struct fsevent_handle
*)kn
->kn_hook
;
2124 KNOTE_DETACH(&fseh
->knotes
, kn
);
2126 unlock_watch_table();
2130 * Determine whether this knote should be active
2132 * This is kind of subtle.
2133 * --First, notice if the vnode has been revoked: in so, override hint
2134 * --EVFILT_READ knotes are checked no matter what the hint is
2135 * --Other knotes activate based on hint.
2136 * --If hint is revoke, set special flags and activate
2139 filt_fsevent(struct knote
*kn
, long hint
)
2141 fsevent_handle
*fseh
= (struct fsevent_handle
*)kn
->kn_hook
;
2143 int32_t rd
, wr
, amt
;
2145 if (NOTE_REVOKE
== hint
) {
2146 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
2150 rd
= fseh
->watcher
->rd
;
2151 wr
= fseh
->watcher
->wr
;
2155 amt
= fseh
->watcher
->eventq_size
- (rd
- wr
);
2158 switch(kn
->kn_filter
) {
2162 if (kn
->kn_data
!= 0) {
2167 /* Check events this note matches against the hint */
2168 if (kn
->kn_sfflags
& hint
) {
2169 kn
->kn_fflags
|= hint
; /* Set which event occurred */
2171 if (kn
->kn_fflags
!= 0) {
2185 struct filterops fsevent_filtops
= {
2188 .f_detach
= filt_fsevent_detach
,
2189 .f_event
= filt_fsevent
2193 fseventsf_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
, __unused vfs_context_t ctx
)
2195 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
2197 kn
->kn_hook
= (void*)fseh
;
2199 kn
->kn_fop
= &fsevent_filtops
;
2203 KNOTE_ATTACH(&fseh
->knotes
, kn
);
2205 unlock_watch_table();
2211 fseventsf_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
2214 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
2216 fseh
->watcher
->flags
|= WATCHER_CLOSING
;
2218 // if there are people still waiting, sleep for 10ms to
2219 // let them clean up and get out of there. however we
2220 // also don't want to get stuck forever so if they don't
2221 // exit after 5 seconds we're tearing things down anyway.
2222 while(fseh
->watcher
->blockers
&& counter
++ < 500) {
2223 // issue wakeup in case anyone is blocked waiting for an event
2224 // do this each time we wakeup in case the blocker missed
2225 // the wakeup due to the unprotected test of WATCHER_CLOSING
2226 // and decision to tsleep in fmod_watch... this bit of
2227 // latency is a decent tradeoff against not having to
2228 // take and drop a lock in fmod_watch
2229 fsevents_wakeup(fseh
->watcher
);
2231 tsleep((caddr_t
)fseh
->watcher
, PRIBIO
, "watcher-close", 1);
2239 fseventsopen(__unused dev_t dev
, __unused
int flag
, __unused
int mode
, __unused
struct proc
*p
)
2249 fseventsclose(__unused dev_t dev
, __unused
int flag
, __unused
int mode
, __unused
struct proc
*p
)
2255 fseventsread(__unused dev_t dev
, __unused
struct uio
*uio
, __unused
int ioflag
)
2262 parse_buffer_and_add_events(const char *buffer
, int bufsize
, vfs_context_t ctx
, long *remainder
)
2264 const fse_info
*finfo
, *dest_finfo
;
2265 const char *path
, *ptr
, *dest_path
, *event_start
=buffer
;
2266 int path_len
, type
, dest_path_len
, err
= 0;
2270 while ((ptr
+sizeof(int)+sizeof(fse_info
)+1) < buffer
+bufsize
) {
2271 type
= *(const int *)ptr
;
2272 if (type
< 0 || type
>= FSE_MAX_EVENTS
) {
2279 finfo
= (const fse_info
*)ptr
;
2280 ptr
+= sizeof(fse_info
);
2283 while(ptr
< buffer
+bufsize
&& *ptr
!= '\0') {
2287 if (ptr
>= buffer
+bufsize
) {
2291 ptr
++; // advance over the trailing '\0'
2293 path_len
= ptr
- path
;
2295 if (type
!= FSE_RENAME
&& type
!= FSE_EXCHANGE
) {
2296 event_start
= ptr
; // record where the next event starts
2298 err
= add_fsevent(type
, ctx
, FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
, FSE_ARG_DONE
);
2306 // if we're here we have to slurp up the destination finfo
2307 // and path so that we can pass them to the add_fsevent()
2308 // call. basically it's a copy of the above code.
2310 dest_finfo
= (const fse_info
*)ptr
;
2311 ptr
+= sizeof(fse_info
);
2314 while(ptr
< buffer
+bufsize
&& *ptr
!= '\0') {
2318 if (ptr
>= buffer
+bufsize
) {
2322 ptr
++; // advance over the trailing '\0'
2323 event_start
= ptr
; // record where the next event starts
2325 dest_path_len
= ptr
- dest_path
;
2327 // If the destination inode number is non-zero, generate a rename
2328 // with both source and destination FSE_ARG_FINFO. Otherwise generate
2329 // a rename with only one FSE_ARG_FINFO. If you need to inject an
2330 // exchange with an inode of zero, just make that inode (and its path)
2331 // come in as the first one, not the second.
2333 if (dest_finfo
->ino
) {
2334 err
= add_fsevent(type
, ctx
,
2335 FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
,
2336 FSE_ARG_STRING
, dest_path_len
, dest_path
, FSE_ARG_FINFO
, dest_finfo
,
2339 err
= add_fsevent(type
, ctx
,
2340 FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
,
2341 FSE_ARG_STRING
, dest_path_len
, dest_path
,
2351 // if the last event wasn't complete, set the remainder
2352 // to be the last event start boundary.
2354 *remainder
= (long)((buffer
+bufsize
) - event_start
);
2361 // Note: this buffer size can not ever be less than
2362 // 2*MAXPATHLEN + 2*sizeof(fse_info) + sizeof(int)
2363 // because that is the max size for a single event.
2364 // I made it 4k to be a "nice" size. making it
2365 // smaller is not a good idea.
2367 #define WRITE_BUFFER_SIZE 4096
2368 char *write_buffer
=NULL
;
2371 fseventswrite(__unused dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
2374 vfs_context_t ctx
= vfs_context_current();
2375 long offset
=0, remainder
;
2377 lck_mtx_lock(&event_writer_lock
);
2379 if (write_buffer
== NULL
) {
2380 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&write_buffer
, WRITE_BUFFER_SIZE
)) {
2381 lck_mtx_unlock(&event_writer_lock
);
2387 // this loop copies in and processes the events written.
2388 // it takes care to copy in reasonable size chunks and
2389 // process them. if there is an event that spans a chunk
2390 // boundary we're careful to copy those bytes down to the
2391 // beginning of the buffer and read the next chunk in just
2394 while(uio_resid(uio
)) {
2395 if (uio_resid(uio
) > (WRITE_BUFFER_SIZE
-offset
)) {
2396 count
= WRITE_BUFFER_SIZE
- offset
;
2398 count
= uio_resid(uio
);
2401 error
= uiomove(write_buffer
+offset
, count
, uio
);
2406 // printf("fsevents: write: copied in %d bytes (offset: %ld)\n", count, offset);
2407 error
= parse_buffer_and_add_events(write_buffer
, offset
+count
, ctx
, &remainder
);
2413 // if there's any remainder, copy it down to the beginning
2414 // of the buffer so that it will get processed the next time
2415 // through the loop. note that the remainder always starts
2416 // at an event boundary.
2418 if (remainder
!= 0) {
2419 // printf("fsevents: write: an event spanned a %d byte boundary. remainder: %ld\n",
2420 // WRITE_BUFFER_SIZE, remainder);
2421 memmove(write_buffer
, (write_buffer
+count
+offset
) - remainder
, remainder
);
2428 lck_mtx_unlock(&event_writer_lock
);
2434 static struct fileops fsevents_fops
= {
2444 typedef struct ext_fsevent_clone_args
{
2445 user_addr_t event_list
;
2447 int32_t event_queue_depth
;
2449 } ext_fsevent_clone_args
;
2451 typedef struct old_fsevent_clone_args
{
2452 uint32_t event_list
;
2454 int32_t event_queue_depth
;
2456 } old_fsevent_clone_args
;
2458 #define OLD_FSEVENTS_CLONE _IOW('s', 1, old_fsevent_clone_args)
2461 fseventsioctl(__unused dev_t dev
, u_long cmd
, caddr_t data
, __unused
int flag
, struct proc
*p
)
2465 fsevent_handle
*fseh
= NULL
;
2466 ext_fsevent_clone_args
*fse_clone_args
, _fse_clone
;
2468 int is64bit
= proc_is64bit(p
);
2471 case OLD_FSEVENTS_CLONE
: {
2472 old_fsevent_clone_args
*old_args
= (old_fsevent_clone_args
*)data
;
2474 fse_clone_args
= &_fse_clone
;
2475 memset(fse_clone_args
, 0, sizeof(ext_fsevent_clone_args
));
2477 fse_clone_args
->event_list
= CAST_USER_ADDR_T(old_args
->event_list
);
2478 fse_clone_args
->num_events
= old_args
->num_events
;
2479 fse_clone_args
->event_queue_depth
= old_args
->event_queue_depth
;
2480 fse_clone_args
->fd
= CAST_USER_ADDR_T(old_args
->fd
);
2484 case FSEVENTS_CLONE
:
2486 fse_clone_args
= (ext_fsevent_clone_args
*)data
;
2488 fsevent_clone_args
*ufse_clone
= (fsevent_clone_args
*)data
;
2490 fse_clone_args
= &_fse_clone
;
2491 memset(fse_clone_args
, 0, sizeof(ext_fsevent_clone_args
));
2493 fse_clone_args
->event_list
= CAST_USER_ADDR_T(ufse_clone
->event_list
);
2494 fse_clone_args
->num_events
= ufse_clone
->num_events
;
2495 fse_clone_args
->event_queue_depth
= ufse_clone
->event_queue_depth
;
2496 fse_clone_args
->fd
= CAST_USER_ADDR_T(ufse_clone
->fd
);
2500 if (fse_clone_args
->num_events
< 0 || fse_clone_args
->num_events
> 4096) {
2504 MALLOC(fseh
, fsevent_handle
*, sizeof(fsevent_handle
),
2509 memset(fseh
, 0, sizeof(fsevent_handle
));
2511 klist_init(&fseh
->knotes
);
2513 MALLOC(event_list
, int8_t *,
2514 fse_clone_args
->num_events
* sizeof(int8_t),
2516 if (event_list
== NULL
) {
2521 error
= copyin(fse_clone_args
->event_list
,
2523 fse_clone_args
->num_events
* sizeof(int8_t));
2525 FREE(event_list
, M_TEMP
);
2530 error
= add_watcher(event_list
,
2531 fse_clone_args
->num_events
,
2532 fse_clone_args
->event_queue_depth
,
2535 FREE(event_list
, M_TEMP
);
2540 // connect up the watcher with this fsevent_handle
2541 fseh
->watcher
->fseh
= fseh
;
2543 error
= falloc(p
, &f
, &fd
, vfs_context_current());
2545 FREE(event_list
, M_TEMP
);
2550 f
->f_fglob
->fg_flag
= FREAD
| FWRITE
;
2551 f
->f_fglob
->fg_type
= DTYPE_FSEVENTS
;
2552 f
->f_fglob
->fg_ops
= &fsevents_fops
;
2553 f
->f_fglob
->fg_data
= (caddr_t
) fseh
;
2555 error
= copyout((void *)&fd
, fse_clone_args
->fd
, sizeof(int32_t));
2560 procfdtbl_releasefd(p
, fd
, NULL
);
2561 fp_drop(p
, fd
, f
, 1);
2575 fsevents_wakeup(fs_event_watcher
*watcher
)
2577 selwakeup(&watcher
->fseh
->si
);
2578 KNOTE(&watcher
->fseh
->knotes
, NOTE_WRITE
|NOTE_NONE
);
2579 wakeup((caddr_t
)watcher
);
2584 * A struct describing which functions will get invoked for certain
2587 static struct cdevsw fsevents_cdevsw
=
2589 fseventsopen
, /* open */
2590 fseventsclose
, /* close */
2591 fseventsread
, /* read */
2592 fseventswrite
, /* write */
2593 fseventsioctl
, /* ioctl */
2594 (stop_fcn_t
*)&nulldev
, /* stop */
2595 (reset_fcn_t
*)&nulldev
, /* reset */
2597 eno_select
, /* select */
2598 eno_mmap
, /* mmap */
2599 eno_strat
, /* strategy */
2600 eno_getc
, /* getc */
2601 eno_putc
, /* putc */
2607 * Called to initialize our device,
2608 * and to register ourselves with devfs
2616 if (fsevents_installed
) {
2620 fsevents_installed
= 1;
2622 ret
= cdevsw_add(-1, &fsevents_cdevsw
);
2624 fsevents_installed
= 0;
2628 devfs_make_node(makedev (ret
, 0), DEVFS_CHAR
,
2629 UID_ROOT
, GID_WHEEL
, 0644, "fsevents", 0);
2631 fsevents_internal_init();
2640 MALLOC_ZONE(path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2645 release_pathbuff(char *path
)
2651 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
2655 get_fse_info(struct vnode
*vp
, fse_info
*fse
, __unused vfs_context_t ctx
)
2657 struct vnode_attr va
;
2660 VATTR_WANTED(&va
, va_fsid
);
2661 VATTR_WANTED(&va
, va_fileid
);
2662 VATTR_WANTED(&va
, va_mode
);
2663 VATTR_WANTED(&va
, va_uid
);
2664 VATTR_WANTED(&va
, va_gid
);
2665 if (vp
->v_flag
& VISHARDLINK
) {
2666 if (vp
->v_type
== VDIR
) {
2667 VATTR_WANTED(&va
, va_dirlinkcount
);
2669 VATTR_WANTED(&va
, va_nlink
);
2673 if (vnode_getattr(vp
, &va
, vfs_context_kernel()) != 0) {
2674 memset(fse
, 0, sizeof(fse_info
));
2678 return vnode_get_fse_info_from_vap(vp
, fse
, &va
);
2682 vnode_get_fse_info_from_vap(vnode_t vp
, fse_info
*fse
, struct vnode_attr
*vap
)
2684 fse
->ino
= (ino64_t
)vap
->va_fileid
;
2685 fse
->dev
= (dev_t
)vap
->va_fsid
;
2686 fse
->mode
= (int32_t)vnode_vttoif(vnode_vtype(vp
)) | vap
->va_mode
;
2687 fse
->uid
= (uid_t
)vap
->va_uid
;
2688 fse
->gid
= (gid_t
)vap
->va_gid
;
2689 if (vp
->v_flag
& VISHARDLINK
) {
2690 fse
->mode
|= FSE_MODE_HLINK
;
2691 if (vp
->v_type
== VDIR
) {
2692 fse
->nlink
= (uint64_t)vap
->va_dirlinkcount
;
2694 fse
->nlink
= (uint64_t)vap
->va_nlink
;
2702 create_fsevent_from_kevent(vnode_t vp
, uint32_t kevents
, struct vnode_attr
*vap
)
2704 int fsevent_type
=FSE_CONTENT_MODIFIED
, len
; // the default is the most pessimistic
2705 char pathbuf
[MAXPATHLEN
];
2709 if (kevents
& VNODE_EVENT_DELETE
) {
2710 fsevent_type
= FSE_DELETE
;
2711 } else if (kevents
& (VNODE_EVENT_EXTEND
|VNODE_EVENT_WRITE
)) {
2712 fsevent_type
= FSE_CONTENT_MODIFIED
;
2713 } else if (kevents
& VNODE_EVENT_LINK
) {
2714 fsevent_type
= FSE_CREATE_FILE
;
2715 } else if (kevents
& VNODE_EVENT_RENAME
) {
2716 fsevent_type
= FSE_CREATE_FILE
; // XXXdbg - should use FSE_RENAME but we don't have the destination info;
2717 } else if (kevents
& (VNODE_EVENT_FILE_CREATED
|VNODE_EVENT_FILE_REMOVED
|VNODE_EVENT_DIR_CREATED
|VNODE_EVENT_DIR_REMOVED
)) {
2718 fsevent_type
= FSE_STAT_CHANGED
; // XXXdbg - because vp is a dir and the thing created/removed lived inside it
2719 } else { // a catch all for VNODE_EVENT_PERMS, VNODE_EVENT_ATTRIB and anything else
2720 fsevent_type
= FSE_STAT_CHANGED
;
2723 // printf("convert_kevent: kevents 0x%x fsevent type 0x%x (for %s)\n", kevents, fsevent_type, vp->v_name ? vp->v_name : "(no-name)");
2725 fse
.dev
= vap
->va_fsid
;
2726 fse
.ino
= vap
->va_fileid
;
2727 fse
.mode
= vnode_vttoif(vnode_vtype(vp
)) | (uint32_t)vap
->va_mode
;
2728 if (vp
->v_flag
& VISHARDLINK
) {
2729 fse
.mode
|= FSE_MODE_HLINK
;
2730 if (vp
->v_type
== VDIR
) {
2731 fse
.nlink
= vap
->va_dirlinkcount
;
2733 fse
.nlink
= vap
->va_nlink
;
2737 if (vp
->v_type
== VDIR
) {
2738 fse
.mode
|= FSE_REMOTE_DIR_EVENT
;
2742 fse
.uid
= vap
->va_uid
;
2743 fse
.gid
= vap
->va_gid
;
2745 len
= sizeof(pathbuf
);
2746 if (vn_getpath(vp
, pathbuf
, &len
) == 0) {
2747 add_fsevent(fsevent_type
, vfs_context_current(), FSE_ARG_STRING
, len
, pathbuf
, FSE_ARG_FINFO
, &fse
, FSE_ARG_DONE
);
2752 #else /* CONFIG_FSE */
2754 * The get_pathbuff and release_pathbuff routines are used in places not
2755 * related to fsevents, and it's a handy abstraction, so define trivial
2756 * versions that don't cache a pool of buffers. This way, we don't have
2757 * to conditionalize the callers, and they still get the advantage of the
2758 * pool of buffers if CONFIG_FSE is turned on.
2764 MALLOC_ZONE(path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2769 release_pathbuff(char *path
)
2771 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
2773 #endif /* CONFIG_FSE */