2 * Copyright (c) 2004-2014 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>
64 #include <pexpert/pexpert.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
;
116 char proc_name
[(2 * MAXCOMLEN
) + 1];
119 // fs_event_watcher flags
120 #define WATCHER_DROPPED_EVENTS 0x0001
121 #define WATCHER_CLOSING 0x0002
122 #define WATCHER_WANTS_COMPACT_EVENTS 0x0004
123 #define WATCHER_WANTS_EXTENDED_INFO 0x0008
124 #define WATCHER_APPLE_SYSTEM_SERVICE 0x0010 // fseventsd, coreservicesd, mds, revisiond
126 #define MAX_WATCHERS 8
127 static fs_event_watcher
*watcher_table
[MAX_WATCHERS
];
129 #define DEFAULT_MAX_KFS_EVENTS 4096
130 static int max_kfs_events
= DEFAULT_MAX_KFS_EVENTS
;
132 // we allocate kfs_event structures out of this zone
133 static zone_t event_zone
;
134 static int fs_event_init
= 0;
137 // this array records whether anyone is interested in a
138 // particular type of event. if no one is, we bail out
139 // early from the event delivery
141 static int16_t fs_event_type_watchers
[FSE_MAX_EVENTS
];
143 // the device currently being unmounted:
144 static dev_t fsevent_unmount_dev
= 0;
145 // how many ACKs are still outstanding:
146 static int fsevent_unmount_ack_count
= 0;
148 static int watcher_add_event(fs_event_watcher
*watcher
, kfs_event
*kfse
);
149 static void fsevents_wakeup(fs_event_watcher
*watcher
);
154 static lck_grp_attr_t
* fsevent_group_attr
;
155 static lck_attr_t
* fsevent_lock_attr
;
156 static lck_grp_t
* fsevent_mutex_group
;
158 static lck_grp_t
* fsevent_rw_group
;
160 static lck_rw_t event_handling_lock
; // handles locking for event manipulation and recycling
161 static lck_mtx_t watch_table_lock
;
162 static lck_mtx_t event_buf_lock
;
163 static lck_mtx_t event_writer_lock
;
166 /* Explicitly declare qsort so compiler doesn't complain */
167 __private_extern__
void qsort(
171 int (*)(const void *, const void *));
174 is_ignored_directory(const char *path
) {
180 #define IS_TLD(x) strnstr(__DECONST(char *, path), x, MAXPATHLEN)
181 if (IS_TLD("/.Spotlight-V100/") ||
182 IS_TLD("/.MobileBackups/") ||
183 IS_TLD("/Backups.backupdb/")) {
192 fsevents_internal_init(void)
196 if (fs_event_init
++ != 0) {
200 for(i
=0; i
< FSE_MAX_EVENTS
; i
++) {
201 fs_event_type_watchers
[i
] = 0;
204 memset(watcher_table
, 0, sizeof(watcher_table
));
206 fsevent_lock_attr
= lck_attr_alloc_init();
207 fsevent_group_attr
= lck_grp_attr_alloc_init();
208 fsevent_mutex_group
= lck_grp_alloc_init("fsevent-mutex", fsevent_group_attr
);
209 fsevent_rw_group
= lck_grp_alloc_init("fsevent-rw", fsevent_group_attr
);
211 lck_mtx_init(&watch_table_lock
, fsevent_mutex_group
, fsevent_lock_attr
);
212 lck_mtx_init(&event_buf_lock
, fsevent_mutex_group
, fsevent_lock_attr
);
213 lck_mtx_init(&event_writer_lock
, fsevent_mutex_group
, fsevent_lock_attr
);
215 lck_rw_init(&event_handling_lock
, fsevent_rw_group
, fsevent_lock_attr
);
217 PE_get_default("kern.maxkfsevents", &max_kfs_events
, sizeof(max_kfs_events
));
219 event_zone
= zinit(sizeof(kfs_event
),
220 max_kfs_events
* sizeof(kfs_event
),
221 max_kfs_events
* sizeof(kfs_event
),
223 if (event_zone
== NULL
) {
224 printf("fsevents: failed to initialize the event zone.\n");
227 // mark the zone as exhaustible so that it will not
228 // ever grow beyond what we initially filled it with
229 zone_change(event_zone
, Z_EXHAUST
, TRUE
);
230 zone_change(event_zone
, Z_COLLECT
, FALSE
);
231 zone_change(event_zone
, Z_CALLERACCT
, FALSE
);
233 if (zfill(event_zone
, max_kfs_events
) < max_kfs_events
) {
234 printf("fsevents: failed to pre-fill the event zone.\n");
240 lock_watch_table(void)
242 lck_mtx_lock(&watch_table_lock
);
246 unlock_watch_table(void)
248 lck_mtx_unlock(&watch_table_lock
);
252 lock_fs_event_list(void)
254 lck_mtx_lock(&event_buf_lock
);
258 unlock_fs_event_list(void)
260 lck_mtx_unlock(&event_buf_lock
);
264 static void release_event_ref(kfs_event
*kfse
);
267 watcher_cares_about_dev(fs_event_watcher
*watcher
, dev_t dev
)
271 // if devices_not_to_watch is NULL then we care about all
272 // events from all devices
273 if (watcher
->devices_not_to_watch
== NULL
) {
277 for(i
=0; i
< watcher
->num_devices
; i
++) {
278 if (dev
== watcher
->devices_not_to_watch
[i
]) {
279 // found a match! that means we do not
280 // want events from this device.
285 // if we're here it's not in the devices_not_to_watch[]
286 // list so that means we do care about it
292 need_fsevent(int type
, vnode_t vp
)
294 if (type
>= 0 && type
< FSE_MAX_EVENTS
&& fs_event_type_watchers
[type
] == 0)
297 // events in /dev aren't really interesting...
298 if (vp
->v_tag
== VT_DEVFS
) {
306 #define is_throw_away(x) ((x) == FSE_STAT_CHANGED || (x) == FSE_CONTENT_MODIFIED)
309 // Ways that an event can be reused:
311 // "combined" events mean that there were two events for
312 // the same vnode or path and we're combining both events
313 // into a single event. The primary event gets a bit that
314 // marks it as having been combined. The secondary event
315 // is essentially dropped and the kfse structure reused.
317 // "collapsed" means that multiple events below a given
318 // directory are collapsed into a single event. in this
319 // case, the directory that we collapse into and all of
320 // its children must be re-scanned.
322 // "recycled" means that we're completely blowing away
323 // the event since there are other events that have info
324 // about the same vnode or path (and one of those other
325 // events will be marked as combined or collapsed as
328 #define KFSE_COMBINED 0x0001
329 #define KFSE_COLLAPSED 0x0002
330 #define KFSE_RECYCLED 0x0004
333 int num_parent_switch
= 0;
334 int num_recycled_rename
= 0;
336 static struct timeval last_print
;
339 // These variables are used to track coalescing multiple identical
340 // events for the same vnode/pathname. If we get the same event
341 // type and same vnode/pathname as the previous event, we just drop
342 // the event since it's superfluous. This improves some micro-
343 // benchmarks considerably and actually has a real-world impact on
344 // tests like a Finder copy where multiple stat-changed events can
347 static int last_event_type
=-1;
348 static void *last_ptr
=NULL
;
349 static char last_str
[MAXPATHLEN
];
350 static int last_nlen
=0;
351 static int last_vid
=-1;
352 static uint64_t last_coalesced_time
=0;
353 static void *last_event_ptr
=NULL
;
354 int last_coalesced
= 0;
355 static mach_timebase_info_data_t sTimebaseInfo
= { 0, 0 };
359 add_fsevent(int type
, vfs_context_t ctx
, ...)
361 struct proc
*p
= vfs_context_proc(ctx
);
362 int i
, arg_type
, ret
;
363 kfs_event
*kfse
, *kfse_dest
=NULL
, *cur
;
364 fs_event_watcher
*watcher
;
366 int error
= 0, did_alloc
=0;
368 uint64_t now
, elapsed
;
376 // ignore bogus event types..
377 if (type
< 0 || type
>= FSE_MAX_EVENTS
) {
381 // if no one cares about this type of event, bail out
382 if (fs_event_type_watchers
[type
] == 0) {
388 now
= mach_absolute_time();
390 // find a free event and snag it for our use
391 // NOTE: do not do anything that would block until
392 // the lock is dropped.
393 lock_fs_event_list();
396 // check if this event is identical to the previous one...
397 // (as long as it's not an event type that can never be the
398 // same as a previous event)
400 if (type
!= FSE_CREATE_FILE
&& type
!= FSE_DELETE
&& type
!= FSE_RENAME
&& type
!= FSE_EXCHANGE
&& type
!= FSE_CHOWN
&& type
!= FSE_DOCID_CHANGED
&& type
!= FSE_DOCID_CREATED
) {
402 int vid
=0, was_str
=0, nlen
=0;
404 for(arg_type
=va_arg(ap
, int32_t); arg_type
!= FSE_ARG_DONE
; arg_type
=va_arg(ap
, int32_t)) {
406 case FSE_ARG_VNODE
: {
407 ptr
= va_arg(ap
, void *);
408 vid
= vnode_vid((struct vnode
*)ptr
);
412 case FSE_ARG_STRING
: {
413 nlen
= va_arg(ap
, int32_t);
414 ptr
= va_arg(ap
, void *);
424 if ( sTimebaseInfo
.denom
== 0 ) {
425 (void) clock_timebase_info(&sTimebaseInfo
);
428 elapsed
= (now
- last_coalesced_time
);
429 if (sTimebaseInfo
.denom
!= sTimebaseInfo
.numer
) {
430 if (sTimebaseInfo
.denom
== 1) {
431 elapsed
*= sTimebaseInfo
.numer
;
433 // this could overflow... the worst that will happen is that we'll
434 // send (or not send) an extra event so I'm not going to worry about
435 // doing the math right like dtrace_abs_to_nano() does.
436 elapsed
= (elapsed
* sTimebaseInfo
.numer
) / (uint64_t)sTimebaseInfo
.denom
;
440 if (type
== last_event_type
441 && (elapsed
< 1000000000)
443 ((vid
&& vid
== last_vid
&& last_ptr
== ptr
)
445 (last_str
[0] && last_nlen
== nlen
&& ptr
&& strcmp(last_str
, ptr
) == 0))
449 unlock_fs_event_list();
456 strlcpy(last_str
, ptr
, sizeof(last_str
));
460 last_event_type
= type
;
461 last_coalesced_time
= now
;
467 kfse
= zalloc_noblock(event_zone
);
468 if (kfse
&& (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
)) {
469 kfse_dest
= zalloc_noblock(event_zone
);
470 if (kfse_dest
== NULL
) {
472 zfree(event_zone
, kfse
);
478 if (kfse
== NULL
) { // yikes! no free events
479 unlock_fs_event_list();
482 for(i
=0; i
< MAX_WATCHERS
; i
++) {
483 watcher
= watcher_table
[i
];
484 if (watcher
== NULL
) {
488 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
489 fsevents_wakeup(watcher
);
491 unlock_watch_table();
494 struct timeval current_tv
;
498 // only print a message at most once every 5 seconds
499 microuptime(¤t_tv
);
500 if ((current_tv
.tv_sec
- last_print
.tv_sec
) > 10) {
502 void *junkptr
=zalloc_noblock(event_zone
), *listhead
=kfse_list_head
.lh_first
;
504 printf("add_fsevent: event queue is full! dropping events (num dropped events: %d; num events outstanding: %d).\n", num_dropped
, num_events_outstanding
);
505 printf("add_fsevent: kfse_list head %p ; num_pending_rename %d\n", listhead
, num_pending_rename
);
506 printf("add_fsevent: zalloc sez: %p\n", junkptr
);
507 printf("add_fsevent: event_zone info: %d 0x%x\n", ((int *)event_zone
)[0], ((int *)event_zone
)[1]);
509 for(ii
=0; ii
< MAX_WATCHERS
; ii
++) {
510 if (watcher_table
[ii
] == NULL
) {
514 printf("add_fsevent: watcher %s %p: rd %4d wr %4d q_size %4d flags 0x%x\n",
515 watcher_table
[ii
]->proc_name
,
517 watcher_table
[ii
]->rd
, watcher_table
[ii
]->wr
,
518 watcher_table
[ii
]->eventq_size
, watcher_table
[ii
]->flags
);
520 unlock_watch_table();
522 last_print
= current_tv
;
524 zfree(event_zone
, junkptr
);
530 release_pathbuff(pathbuff
);
536 memset(kfse
, 0, sizeof(kfs_event
));
538 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse
->flags
);
540 last_event_ptr
= kfse
;
543 kfse
->pid
= p
->p_pid
;
544 if (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
) {
545 memset(kfse_dest
, 0, sizeof(kfs_event
));
546 kfse_dest
->refcount
= 1;
547 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse_dest
->flags
);
548 kfse_dest
->type
= type
;
549 kfse_dest
->pid
= p
->p_pid
;
550 kfse_dest
->abstime
= now
;
552 kfse
->dest
= kfse_dest
;
555 num_events_outstanding
++;
556 if (kfse
->type
== FSE_RENAME
) {
557 num_pending_rename
++;
559 LIST_INSERT_HEAD(&kfse_list_head
, kfse
, kevent_list
);
561 if (kfse
->refcount
< 1) {
562 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__
, kfse
->refcount
);
565 unlock_fs_event_list(); // at this point it's safe to unlock
568 // now process the arguments passed in and copy them into
574 if (type
== FSE_DOCID_CREATED
|| type
== FSE_DOCID_CHANGED
) {
578 // These events are special and not like the other events. They only
579 // have a dev_t, src inode #, dest inode #, and a doc-id. We use the
580 // fields that we can in the kfse but have to overlay the dest inode
581 // number and the doc-id on the other fields.
585 arg_type
= va_arg(ap
, int32_t);
586 if (arg_type
== FSE_ARG_DEV
) {
587 cur
->dev
= (dev_t
)(va_arg(ap
, dev_t
));
589 cur
->dev
= (dev_t
)0xbadc0de1;
592 // next the source inode #
593 arg_type
= va_arg(ap
, int32_t);
594 if (arg_type
== FSE_ARG_INO
) {
595 cur
->ino
= (ino64_t
)(va_arg(ap
, ino64_t
));
597 cur
->ino
= 0xbadc0de2;
600 // now the dest inode #
601 arg_type
= va_arg(ap
, int32_t);
602 if (arg_type
== FSE_ARG_INO
) {
603 val
= (ino64_t
)(va_arg(ap
, ino64_t
));
607 // overlay the dest inode number on the str/dest pointer fields
608 memcpy(&cur
->str
, &val
, sizeof(ino64_t
));
611 // and last the document-id
612 arg_type
= va_arg(ap
, int32_t);
613 if (arg_type
== FSE_ARG_INT32
) {
614 val
= (uint64_t)va_arg(ap
, uint32_t);
615 } else if (arg_type
== FSE_ARG_INT64
) {
616 val
= (uint64_t)va_arg(ap
, uint64_t);
621 // the docid is 64-bit and overlays the uid/gid fields
622 memcpy(&cur
->uid
, &val
, sizeof(uint64_t));
627 if (type
== FSE_UNMOUNT_PENDING
) {
630 arg_type
= va_arg(ap
, int32_t);
631 if (arg_type
== FSE_ARG_DEV
) {
632 cur
->dev
= (dev_t
)(va_arg(ap
, dev_t
));
634 cur
->dev
= (dev_t
)0xbadc0de1;
640 for(arg_type
=va_arg(ap
, int32_t); arg_type
!= FSE_ARG_DONE
; arg_type
=va_arg(ap
, int32_t))
643 case FSE_ARG_VNODE
: {
644 // this expands out into multiple arguments to the client
646 struct vnode_attr va
;
648 if (kfse
->str
!= NULL
) {
652 vp
= va_arg(ap
, struct vnode
*);
654 panic("add_fsevent: you can't pass me a NULL vnode ptr (type %d)!\n",
659 VATTR_WANTED(&va
, va_fsid
);
660 VATTR_WANTED(&va
, va_fileid
);
661 VATTR_WANTED(&va
, va_mode
);
662 VATTR_WANTED(&va
, va_uid
);
663 VATTR_WANTED(&va
, va_gid
);
664 VATTR_WANTED(&va
, va_nlink
);
665 if ((ret
= vnode_getattr(vp
, &va
, vfs_context_kernel())) != 0) {
666 // printf("add_fsevent: failed to getattr on vp %p (%d)\n", cur->fref.vp, ret);
672 cur
->dev
= dev
= (dev_t
)va
.va_fsid
;
673 cur
->ino
= (ino64_t
)va
.va_fileid
;
674 cur
->mode
= (int32_t)vnode_vttoif(vnode_vtype(vp
)) | va
.va_mode
;
675 cur
->uid
= va
.va_uid
;
676 cur
->gid
= va
.va_gid
;
677 if (vp
->v_flag
& VISHARDLINK
) {
678 cur
->mode
|= FSE_MODE_HLINK
;
679 if ((vp
->v_type
== VDIR
&& va
.va_dirlinkcount
== 0) || (vp
->v_type
== VREG
&& va
.va_nlink
== 0)) {
680 cur
->mode
|= FSE_MODE_LAST_HLINK
;
684 // if we haven't gotten the path yet, get it.
685 if (pathbuff
== NULL
) {
686 pathbuff
= get_pathbuff();
687 pathbuff_len
= MAXPATHLEN
;
690 if ((ret
= vn_getpath(vp
, pathbuff
, &pathbuff_len
)) != 0 || pathbuff
[0] == '\0') {
692 cur
->flags
|= KFSE_CONTAINS_DROPPED_EVENTS
;
695 if (vp
->v_parent
!= NULL
) {
697 } else if (vp
->v_mount
) {
698 strlcpy(pathbuff
, vp
->v_mount
->mnt_vfsstat
.f_mntonname
, MAXPATHLEN
);
708 pathbuff_len
= MAXPATHLEN
;
709 ret
= vn_getpath(vp
, pathbuff
, &pathbuff_len
);
710 } while (ret
== ENOSPC
);
712 if (ret
!= 0 || vp
== NULL
) {
719 // store the path by adding it to the global string table
720 cur
->len
= pathbuff_len
;
721 cur
->str
= vfs_addname(pathbuff
, pathbuff_len
, 0, 0);
722 if (cur
->str
== NULL
|| cur
->str
[0] == '\0') {
723 panic("add_fsevent: was not able to add path %s to event %p.\n", pathbuff
, cur
);
726 release_pathbuff(pathbuff
);
732 case FSE_ARG_FINFO
: {
735 fse
= va_arg(ap
, fse_info
*);
737 cur
->dev
= dev
= (dev_t
)fse
->dev
;
738 cur
->ino
= (ino64_t
)fse
->ino
;
739 cur
->mode
= (int32_t)fse
->mode
;
740 cur
->uid
= (uid_t
)fse
->uid
;
741 cur
->gid
= (uid_t
)fse
->gid
;
742 // if it's a hard-link and this is the last link, flag it
743 if ((fse
->mode
& FSE_MODE_HLINK
) && fse
->nlink
== 0) {
744 cur
->mode
|= FSE_MODE_LAST_HLINK
;
746 if (cur
->mode
& FSE_TRUNCATED_PATH
) {
747 cur
->flags
|= KFSE_CONTAINS_DROPPED_EVENTS
;
748 cur
->mode
&= ~FSE_TRUNCATED_PATH
;
754 if (kfse
->str
!= NULL
) {
758 cur
->len
= (int16_t)(va_arg(ap
, int32_t) & 0x7fff);
760 cur
->str
= vfs_addname(va_arg(ap
, char *), cur
->len
, 0, 0);
762 printf("add_fsevent: funny looking string length: %d\n", (int)cur
->len
);
764 cur
->str
= vfs_addname("/", cur
->len
, 0, 0);
766 if (cur
->str
[0] == 0) {
767 printf("add_fsevent: bogus looking string (len %d)\n", cur
->len
);
771 case FSE_ARG_INT32
: {
772 uint32_t ival
= (uint32_t)va_arg(ap
, int32_t);
773 kfse
->uid
= (ino64_t
)ival
;
778 printf("add_fsevent: unknown type %d\n", arg_type
);
779 // just skip one 32-bit word and hope we sync up...
780 (void)va_arg(ap
, int32_t);
786 OSBitAndAtomic16(~KFSE_BEING_CREATED
, &kfse
->flags
);
788 OSBitAndAtomic16(~KFSE_BEING_CREATED
, &kfse_dest
->flags
);
792 // now we have to go and let everyone know that
793 // is interested in this type of event
797 for(i
=0; i
< MAX_WATCHERS
; i
++) {
798 watcher
= watcher_table
[i
];
799 if (watcher
== NULL
) {
803 if ( type
< watcher
->num_events
804 && watcher
->event_list
[type
] == FSE_REPORT
805 && watcher_cares_about_dev(watcher
, dev
)) {
807 if (watcher_add_event(watcher
, kfse
) != 0) {
808 watcher
->num_dropped
++;
813 // if (kfse->refcount < 1) {
814 // panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__, kfse->refcount);
818 unlock_watch_table();
823 release_pathbuff(pathbuff
);
827 release_event_ref(kfse
);
834 release_event_ref(kfs_event
*kfse
)
837 kfs_event copy
, dest_copy
;
840 old_refcount
= OSAddAtomic(-1, &kfse
->refcount
);
841 if (old_refcount
> 1) {
845 lock_fs_event_list();
846 if (last_event_ptr
== kfse
) {
847 last_event_ptr
= NULL
;
848 last_event_type
= -1;
849 last_coalesced_time
= 0;
852 if (kfse
->refcount
< 0) {
853 panic("release_event_ref: bogus kfse refcount %d\n", kfse
->refcount
);
856 if (kfse
->refcount
> 0 || kfse
->type
== FSE_INVALID
) {
857 // This is very subtle. Either of these conditions can
858 // be true if an event got recycled while we were waiting
859 // on the fs_event_list lock or the event got recycled,
860 // delivered, _and_ free'd by someone else while we were
861 // waiting on the fs event list lock. In either case
862 // we need to just unlock the list and return without
863 // doing anything because if the refcount is > 0 then
864 // someone else will take care of free'ing it and when
865 // the kfse->type is invalid then someone else already
866 // has handled free'ing the event (while we were blocked
867 // on the event list lock).
869 unlock_fs_event_list();
874 // make a copy of this so we can free things without
875 // holding the fs_event_buf lock
878 if (kfse
->type
!= FSE_DOCID_CREATED
&& kfse
->type
!= FSE_DOCID_CHANGED
&& kfse
->dest
&& OSAddAtomic(-1, &kfse
->dest
->refcount
) == 1) {
879 dest_copy
= *kfse
->dest
;
881 dest_copy
.str
= NULL
;
883 dest_copy
.type
= FSE_INVALID
;
886 kfse
->pid
= kfse
->type
; // save this off for debugging...
887 kfse
->uid
= (uid_t
)(long)kfse
->str
; // save this off for debugging...
888 kfse
->gid
= (gid_t
)(long)current_thread();
890 kfse
->str
= (char *)0xdeadbeef; // XXXdbg - catch any cheaters...
892 if (dest_copy
.type
!= FSE_INVALID
) {
893 kfse
->dest
->str
= (char *)0xbadc0de; // XXXdbg - catch any cheaters...
894 kfse
->dest
->type
= FSE_INVALID
;
896 if (kfse
->dest
->kevent_list
.le_prev
!= NULL
) {
897 num_events_outstanding
--;
898 LIST_REMOVE(kfse
->dest
, kevent_list
);
899 memset(&kfse
->dest
->kevent_list
, 0xa5, sizeof(kfse
->dest
->kevent_list
));
902 zfree(event_zone
, kfse
->dest
);
905 // mark this fsevent as invalid
910 kfse
->type
= FSE_INVALID
;
912 if (kfse
->kevent_list
.le_prev
!= NULL
) {
913 num_events_outstanding
--;
914 if (otype
== FSE_RENAME
) {
915 num_pending_rename
--;
917 LIST_REMOVE(kfse
, kevent_list
);
918 memset(&kfse
->kevent_list
, 0, sizeof(kfse
->kevent_list
));
922 zfree(event_zone
, kfse
);
924 unlock_fs_event_list();
926 // if we have a pointer in the union
927 if (copy
.str
&& copy
.type
!= FSE_DOCID_CREATED
&& copy
.type
!= FSE_DOCID_CHANGED
) {
928 if (copy
.len
== 0) { // and it's not a string
929 panic("%s:%d: no more fref.vp!\n", __FILE__
, __LINE__
);
930 // vnode_rele_ext(copy.fref.vp, O_EVTONLY, 0);
931 } else { // else it's a string
932 vfs_removename(copy
.str
);
936 if (dest_copy
.type
!= FSE_INVALID
&& dest_copy
.str
) {
937 if (dest_copy
.len
== 0) {
938 panic("%s:%d: no more fref.vp!\n", __FILE__
, __LINE__
);
939 // vnode_rele_ext(dest_copy.fref.vp, O_EVTONLY, 0);
941 vfs_removename(dest_copy
.str
);
947 add_watcher(int8_t *event_list
, int32_t num_events
, int32_t eventq_size
, fs_event_watcher
**watcher_out
, void *fseh
)
950 fs_event_watcher
*watcher
;
952 if (eventq_size
<= 0 || eventq_size
> 100*max_kfs_events
) {
953 eventq_size
= max_kfs_events
;
956 // Note: the event_queue follows the fs_event_watcher struct
957 // in memory so we only have to do one allocation
960 sizeof(fs_event_watcher
) + eventq_size
* sizeof(kfs_event
*),
962 if (watcher
== NULL
) {
966 watcher
->event_list
= event_list
;
967 watcher
->num_events
= num_events
;
968 watcher
->devices_not_to_watch
= NULL
;
969 watcher
->num_devices
= 0;
971 watcher
->event_queue
= (kfs_event
**)&watcher
[1];
972 watcher
->eventq_size
= eventq_size
;
975 watcher
->blockers
= 0;
976 watcher
->num_readers
= 0;
977 watcher
->max_event_id
= 0;
978 watcher
->fseh
= fseh
;
979 watcher
->pid
= proc_selfpid();
980 proc_selfname(watcher
->proc_name
, sizeof(watcher
->proc_name
));
982 watcher
->num_dropped
= 0; // XXXdbg - debugging
984 if (!strncmp(watcher
->proc_name
, "fseventsd", sizeof(watcher
->proc_name
)) ||
985 !strncmp(watcher
->proc_name
, "coreservicesd", sizeof(watcher
->proc_name
)) ||
986 !strncmp(watcher
->proc_name
, "revisiond", sizeof(watcher
->proc_name
)) ||
987 !strncmp(watcher
->proc_name
, "mds", sizeof(watcher
->proc_name
))) {
988 watcher
->flags
|= WATCHER_APPLE_SYSTEM_SERVICE
;
990 printf("fsevents: watcher %s (pid: %d) - Using /dev/fsevents directly is unsupported. Migrate to FSEventsFramework\n",
991 watcher
->proc_name
, watcher
->pid
);
996 // find a slot for the new watcher
997 for(i
=0; i
< MAX_WATCHERS
; i
++) {
998 if (watcher_table
[i
] == NULL
) {
1000 watcher_table
[i
] = watcher
;
1005 if (i
>= MAX_WATCHERS
) {
1006 printf("fsevents: too many watchers!\n");
1007 unlock_watch_table();
1008 FREE(watcher
, M_TEMP
);
1012 // now update the global list of who's interested in
1013 // events of a particular type...
1014 for(i
=0; i
< num_events
; i
++) {
1015 if (event_list
[i
] != FSE_IGNORE
&& i
< FSE_MAX_EVENTS
) {
1016 fs_event_type_watchers
[i
]++;
1020 unlock_watch_table();
1022 *watcher_out
= watcher
;
1030 remove_watcher(fs_event_watcher
*target
)
1032 int i
, j
, counter
=0;
1033 fs_event_watcher
*watcher
;
1038 for(j
=0; j
< MAX_WATCHERS
; j
++) {
1039 watcher
= watcher_table
[j
];
1040 if (watcher
!= target
) {
1044 watcher_table
[j
] = NULL
;
1046 for(i
=0; i
< watcher
->num_events
; i
++) {
1047 if (watcher
->event_list
[i
] != FSE_IGNORE
&& i
< FSE_MAX_EVENTS
) {
1048 fs_event_type_watchers
[i
]--;
1052 if (watcher
->flags
& WATCHER_CLOSING
) {
1053 unlock_watch_table();
1057 // 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);
1058 watcher
->flags
|= WATCHER_CLOSING
;
1059 OSAddAtomic(1, &watcher
->num_readers
);
1061 unlock_watch_table();
1063 while (watcher
->num_readers
> 1 && counter
++ < 5000) {
1065 fsevents_wakeup(watcher
); // in case they're asleep
1066 unlock_watch_table();
1068 tsleep(watcher
, PRIBIO
, "fsevents-close", 1);
1070 if (counter
++ >= 5000) {
1071 // printf("fsevents: close: still have readers! (%d)\n", watcher->num_readers);
1072 panic("fsevents: close: still have readers! (%d)\n", watcher
->num_readers
);
1075 // drain the event_queue
1077 lck_rw_lock_exclusive(&event_handling_lock
);
1078 while(watcher
->rd
!= watcher
->wr
) {
1079 kfse
= watcher
->event_queue
[watcher
->rd
];
1080 watcher
->event_queue
[watcher
->rd
] = NULL
;
1081 watcher
->rd
= (watcher
->rd
+1) % watcher
->eventq_size
;
1083 if (kfse
!= NULL
&& kfse
->type
!= FSE_INVALID
&& kfse
->refcount
>= 1) {
1084 release_event_ref(kfse
);
1087 lck_rw_unlock_exclusive(&event_handling_lock
);
1089 if (watcher
->event_list
) {
1090 FREE(watcher
->event_list
, M_TEMP
);
1091 watcher
->event_list
= NULL
;
1093 if (watcher
->devices_not_to_watch
) {
1094 FREE(watcher
->devices_not_to_watch
, M_TEMP
);
1095 watcher
->devices_not_to_watch
= NULL
;
1097 FREE(watcher
, M_TEMP
);
1102 unlock_watch_table();
1106 #define EVENT_DELAY_IN_MS 10
1107 static thread_call_t event_delivery_timer
= NULL
;
1108 static int timer_set
= 0;
1112 delayed_event_delivery(__unused
void *param0
, __unused
void *param1
)
1118 for(i
=0; i
< MAX_WATCHERS
; i
++) {
1119 if (watcher_table
[i
] != NULL
&& watcher_table
[i
]->rd
!= watcher_table
[i
]->wr
) {
1120 fsevents_wakeup(watcher_table
[i
]);
1126 unlock_watch_table();
1131 // The watch table must be locked before calling this function.
1134 schedule_event_wakeup(void)
1138 if (event_delivery_timer
== NULL
) {
1139 event_delivery_timer
= thread_call_allocate((thread_call_func_t
)delayed_event_delivery
, NULL
);
1142 clock_interval_to_deadline(EVENT_DELAY_IN_MS
, 1000 * 1000, &deadline
);
1144 thread_call_enter_delayed(event_delivery_timer
, deadline
);
1150 #define MAX_NUM_PENDING 16
1153 // NOTE: the watch table must be locked before calling
1157 watcher_add_event(fs_event_watcher
*watcher
, kfs_event
*kfse
)
1159 if (kfse
->abstime
> watcher
->max_event_id
) {
1160 watcher
->max_event_id
= kfse
->abstime
;
1163 if (((watcher
->wr
+ 1) % watcher
->eventq_size
) == watcher
->rd
) {
1164 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
1165 fsevents_wakeup(watcher
);
1169 OSAddAtomic(1, &kfse
->refcount
);
1170 watcher
->event_queue
[watcher
->wr
] = kfse
;
1172 watcher
->wr
= (watcher
->wr
+ 1) % watcher
->eventq_size
;
1175 // wake up the watcher if there are more than MAX_NUM_PENDING events.
1176 // otherwise schedule a timer (if one isn't already set) which will
1177 // send any pending events if no more are received in the next
1178 // EVENT_DELAY_IN_MS milli-seconds.
1180 int32_t num_pending
= 0;
1181 if (watcher
->rd
< watcher
->wr
) {
1182 num_pending
= watcher
->wr
- watcher
->rd
;
1185 if (watcher
->rd
> watcher
->wr
) {
1186 num_pending
= watcher
->wr
+ watcher
->eventq_size
- watcher
->rd
;
1189 if (num_pending
> (watcher
->eventq_size
*3/4) && !(watcher
->flags
& WATCHER_APPLE_SYSTEM_SERVICE
)) {
1190 /* Non-Apple Service is falling behind, start dropping events for this process */
1191 lck_rw_lock_exclusive(&event_handling_lock
);
1192 while (watcher
->rd
!= watcher
->wr
) {
1193 kfse
= watcher
->event_queue
[watcher
->rd
];
1194 watcher
->event_queue
[watcher
->rd
] = NULL
;
1195 watcher
->rd
= (watcher
->rd
+1) % watcher
->eventq_size
;
1197 if (kfse
!= NULL
&& kfse
->type
!= FSE_INVALID
&& kfse
->refcount
>= 1) {
1198 release_event_ref(kfse
);
1201 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
1202 lck_rw_unlock_exclusive(&event_handling_lock
);
1204 printf("fsevents: watcher falling behind: %s (pid: %d) rd: %4d wr: %4d q_size: %4d flags: 0x%x\n",
1205 watcher
->proc_name
, watcher
->pid
, watcher
->rd
, watcher
->wr
,
1206 watcher
->eventq_size
, watcher
->flags
);
1208 fsevents_wakeup(watcher
);
1209 } else if (num_pending
> MAX_NUM_PENDING
) {
1210 fsevents_wakeup(watcher
);
1211 } else if (timer_set
== 0) {
1212 schedule_event_wakeup();
1219 fill_buff(uint16_t type
, int32_t size
, const void *data
,
1220 char *buff
, int32_t *_buff_idx
, int32_t buff_sz
,
1223 int32_t amt
, error
= 0, buff_idx
= *_buff_idx
;
1227 // the +1 on the size is to guarantee that the main data
1228 // copy loop will always copy at least 1 byte
1230 if ((buff_sz
- buff_idx
) <= (int)(2*sizeof(uint16_t) + 1)) {
1231 if (buff_idx
> uio_resid(uio
)) {
1236 error
= uiomove(buff
, buff_idx
, uio
);
1243 // copy out the header (type & size)
1244 memcpy(&buff
[buff_idx
], &type
, sizeof(uint16_t));
1245 buff_idx
+= sizeof(uint16_t);
1247 tmp
= size
& 0xffff;
1248 memcpy(&buff
[buff_idx
], &tmp
, sizeof(uint16_t));
1249 buff_idx
+= sizeof(uint16_t);
1251 // now copy the body of the data, flushing along the way
1252 // if the buffer fills up.
1255 amt
= (size
< (buff_sz
- buff_idx
)) ? size
: (buff_sz
- buff_idx
);
1256 memcpy(&buff
[buff_idx
], data
, amt
);
1260 data
= (const char *)data
+ amt
;
1261 if (size
> (buff_sz
- buff_idx
)) {
1262 if (buff_idx
> uio_resid(uio
)) {
1266 error
= uiomove(buff
, buff_idx
, uio
);
1273 if (amt
== 0) { // just in case...
1279 *_buff_idx
= buff_idx
;
1285 static int copy_out_kfse(fs_event_watcher
*watcher
, kfs_event
*kfse
, struct uio
*uio
) __attribute__((noinline
));
1288 copy_out_kfse(fs_event_watcher
*watcher
, kfs_event
*kfse
, struct uio
*uio
)
1297 if (kfse
->type
== FSE_INVALID
) {
1298 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
);
1301 if (kfse
->flags
& KFSE_BEING_CREATED
) {
1305 if (kfse
->type
== FSE_RENAME
&& kfse
->dest
== NULL
) {
1307 // This can happen if an event gets recycled but we had a
1308 // pointer to it in our event queue. The event is the
1309 // destination of a rename which we'll process separately
1310 // (that is, another kfse points to this one so it's ok
1311 // to skip this guy because we'll process it when we process
1317 if (watcher
->flags
& WATCHER_WANTS_EXTENDED_INFO
) {
1319 type
= (kfse
->type
& 0xfff);
1321 if (kfse
->flags
& KFSE_CONTAINS_DROPPED_EVENTS
) {
1322 type
|= (FSE_CONTAINS_DROPPED_EVENTS
<< FSE_FLAG_SHIFT
);
1323 } else if (kfse
->flags
& KFSE_COMBINED_EVENTS
) {
1324 type
|= (FSE_COMBINED_EVENTS
<< FSE_FLAG_SHIFT
);
1328 type
= (int32_t)kfse
->type
;
1331 // copy out the type of the event
1332 memcpy(evbuff
, &type
, sizeof(int32_t));
1333 evbuff_idx
+= sizeof(int32_t);
1335 // copy out the pid of the person that generated the event
1336 memcpy(&evbuff
[evbuff_idx
], &kfse
->pid
, sizeof(pid_t
));
1337 evbuff_idx
+= sizeof(pid_t
);
1343 if (kfse
->type
== FSE_DOCID_CHANGED
|| kfse
->type
== FSE_DOCID_CREATED
) {
1344 dev_t dev
= cur
->dev
;
1345 ino64_t ino
= cur
->ino
;
1348 error
= fill_buff(FSE_ARG_DEV
, sizeof(dev_t
), &dev
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1353 error
= fill_buff(FSE_ARG_INO
, sizeof(ino64_t
), &ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1358 memcpy(&ino
, &cur
->str
, sizeof(ino64_t
));
1359 error
= fill_buff(FSE_ARG_INO
, sizeof(ino64_t
), &ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1364 memcpy(&ival
, &cur
->uid
, sizeof(uint64_t)); // the docid gets stuffed into the ino field
1365 error
= fill_buff(FSE_ARG_INT64
, sizeof(uint64_t), &ival
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1373 if (kfse
->type
== FSE_UNMOUNT_PENDING
) {
1374 dev_t dev
= cur
->dev
;
1376 error
= fill_buff(FSE_ARG_DEV
, sizeof(dev_t
), &dev
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1384 if (cur
->str
== NULL
|| cur
->str
[0] == '\0') {
1385 printf("copy_out_kfse:2: empty/short path (%s)\n", cur
->str
);
1386 error
= fill_buff(FSE_ARG_STRING
, 2, "/", evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1388 error
= fill_buff(FSE_ARG_STRING
, cur
->len
, cur
->str
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1394 if (cur
->dev
== 0 && cur
->ino
== 0) {
1395 // this happens when a rename event happens and the
1396 // destination of the rename did not previously exist.
1397 // it thus has no other file info so skip copying out
1398 // the stuff below since it isn't initialized
1403 if (watcher
->flags
& WATCHER_WANTS_COMPACT_EVENTS
) {
1406 finfo_size
= sizeof(dev_t
) + sizeof(ino64_t
) + sizeof(int32_t) + sizeof(uid_t
) + sizeof(gid_t
);
1407 error
= fill_buff(FSE_ARG_FINFO
, finfo_size
, &cur
->ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1412 error
= fill_buff(FSE_ARG_DEV
, sizeof(dev_t
), &cur
->dev
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1417 error
= fill_buff(FSE_ARG_INO
, sizeof(ino64_t
), &cur
->ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1422 error
= fill_buff(FSE_ARG_MODE
, sizeof(int32_t), &cur
->mode
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1427 error
= fill_buff(FSE_ARG_UID
, sizeof(uid_t
), &cur
->uid
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1432 error
= fill_buff(FSE_ARG_GID
, sizeof(gid_t
), &cur
->gid
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1445 // very last thing: the time stamp
1446 error
= fill_buff(FSE_ARG_INT64
, sizeof(uint64_t), &cur
->abstime
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1451 // check if the FSE_ARG_DONE will fit
1452 if (sizeof(uint16_t) > sizeof(evbuff
) - evbuff_idx
) {
1453 if (evbuff_idx
> uio_resid(uio
)) {
1457 error
= uiomove(evbuff
, evbuff_idx
, uio
);
1464 tmp16
= FSE_ARG_DONE
;
1465 memcpy(&evbuff
[evbuff_idx
], &tmp16
, sizeof(uint16_t));
1466 evbuff_idx
+= sizeof(uint16_t);
1468 // flush any remaining data in the buffer (and hopefully
1469 // in most cases this is the only uiomove we'll do)
1470 if (evbuff_idx
> uio_resid(uio
)) {
1473 error
= uiomove(evbuff
, evbuff_idx
, uio
);
1484 fmod_watch(fs_event_watcher
*watcher
, struct uio
*uio
)
1487 user_ssize_t last_full_event_resid
;
1492 last_full_event_resid
= uio_resid(uio
);
1494 // need at least 2048 bytes of space (maxpathlen + 1 event buf)
1495 if (uio_resid(uio
) < 2048 || watcher
== NULL
) {
1499 if (watcher
->flags
& WATCHER_CLOSING
) {
1503 if (OSAddAtomic(1, &watcher
->num_readers
) != 0) {
1504 // don't allow multiple threads to read from the fd at the same time
1505 OSAddAtomic(-1, &watcher
->num_readers
);
1510 if (watcher
->rd
== watcher
->wr
) {
1511 if (watcher
->flags
& WATCHER_CLOSING
) {
1512 OSAddAtomic(-1, &watcher
->num_readers
);
1515 OSAddAtomic(1, &watcher
->blockers
);
1517 // there's nothing to do, go to sleep
1518 error
= tsleep((caddr_t
)watcher
, PUSER
|PCATCH
, "fsevents_empty", 0);
1520 OSAddAtomic(-1, &watcher
->blockers
);
1522 if (error
!= 0 || (watcher
->flags
& WATCHER_CLOSING
)) {
1523 OSAddAtomic(-1, &watcher
->num_readers
);
1528 // if we dropped events, return that as an event first
1529 if (watcher
->flags
& WATCHER_DROPPED_EVENTS
) {
1530 int32_t val
= FSE_EVENTS_DROPPED
;
1532 error
= uiomove((caddr_t
)&val
, sizeof(int32_t), uio
);
1534 val
= 0; // a fake pid
1535 error
= uiomove((caddr_t
)&val
, sizeof(int32_t), uio
);
1537 tmp16
= FSE_ARG_DONE
; // makes it a consistent msg
1538 error
= uiomove((caddr_t
)&tmp16
, sizeof(int16_t), uio
);
1540 last_full_event_resid
= uio_resid(uio
);
1544 OSAddAtomic(-1, &watcher
->num_readers
);
1548 watcher
->flags
&= ~WATCHER_DROPPED_EVENTS
;
1553 lck_rw_lock_shared(&event_handling_lock
);
1554 while (uio_resid(uio
) > 0 && watcher
->rd
!= watcher
->wr
) {
1555 if (watcher
->flags
& WATCHER_CLOSING
) {
1560 // check if the event is something of interest to us
1561 // (since it may have been recycled/reused and changed
1562 // its type or which device it is for)
1564 kfse
= watcher
->event_queue
[watcher
->rd
];
1565 if (!kfse
|| kfse
->type
== FSE_INVALID
|| kfse
->type
>= watcher
->num_events
|| kfse
->refcount
< 1) {
1569 if (watcher
->event_list
[kfse
->type
] == FSE_REPORT
&& watcher_cares_about_dev(watcher
, kfse
->dev
)) {
1571 if (!(watcher
->flags
& WATCHER_APPLE_SYSTEM_SERVICE
) && kfse
->type
!= FSE_DOCID_CREATED
&& kfse
->type
!= FSE_DOCID_CHANGED
&& is_ignored_directory(kfse
->str
)) {
1572 // If this is not an Apple System Service, skip specified directories
1579 if (last_event_ptr
== kfse
) {
1580 last_event_ptr
= NULL
;
1581 last_event_type
= -1;
1582 last_coalesced_time
= 0;
1584 error
= copy_out_kfse(watcher
, kfse
, uio
);
1586 // if an event won't fit or encountered an error while
1587 // we were copying it out, then backup to the last full
1588 // event and just bail out. if the error was ENOENT
1589 // then we can continue regular processing, otherwise
1590 // we should unlock things and return.
1591 uio_setresid(uio
, last_full_event_resid
);
1592 if (error
!= ENOENT
) {
1593 lck_rw_unlock_shared(&event_handling_lock
);
1599 last_full_event_resid
= uio_resid(uio
);
1603 watcher
->event_queue
[watcher
->rd
] = NULL
;
1604 watcher
->rd
= (watcher
->rd
+ 1) % watcher
->eventq_size
;
1606 release_event_ref(kfse
);
1608 lck_rw_unlock_shared(&event_handling_lock
);
1610 if (skipped
&& error
== 0) {
1615 OSAddAtomic(-1, &watcher
->num_readers
);
1622 // Shoo watchers away from a volume that's about to be unmounted
1623 // (so that it can be cleanly unmounted).
1626 fsevent_unmount(__unused
struct mount
*mp
, __unused vfs_context_t ctx
)
1632 // /dev/fsevents device code
1634 static int fsevents_installed
= 0;
1636 typedef struct fsevent_handle
{
1639 fs_event_watcher
*watcher
;
1640 struct klist knotes
;
1644 #define FSEH_CLOSING 0x0001
1647 fseventsf_read(struct fileproc
*fp
, struct uio
*uio
,
1648 __unused
int flags
, __unused vfs_context_t ctx
)
1650 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
1653 error
= fmod_watch(fseh
->watcher
, uio
);
1660 fseventsf_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1661 __unused
int flags
, __unused vfs_context_t ctx
)
1666 #pragma pack(push, 4)
1667 typedef struct fsevent_dev_filter_args32
{
1668 uint32_t num_devices
;
1669 user32_addr_t devices
;
1670 } fsevent_dev_filter_args32
;
1671 typedef struct fsevent_dev_filter_args64
{
1672 uint32_t num_devices
;
1673 user64_addr_t devices
;
1674 } fsevent_dev_filter_args64
;
1677 #define FSEVENTS_DEVICE_FILTER_32 _IOW('s', 100, fsevent_dev_filter_args32)
1678 #define FSEVENTS_DEVICE_FILTER_64 _IOW('s', 100, fsevent_dev_filter_args64)
1681 fseventsf_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
, vfs_context_t ctx
)
1683 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
1685 fsevent_dev_filter_args64
*devfilt_args
, _devfilt_args
;
1687 OSAddAtomic(1, &fseh
->active
);
1688 if (fseh
->flags
& FSEH_CLOSING
) {
1689 OSAddAtomic(-1, &fseh
->active
);
1698 case FSEVENTS_WANT_COMPACT_EVENTS
: {
1699 fseh
->watcher
->flags
|= WATCHER_WANTS_COMPACT_EVENTS
;
1703 case FSEVENTS_WANT_EXTENDED_INFO
: {
1704 fseh
->watcher
->flags
|= WATCHER_WANTS_EXTENDED_INFO
;
1708 case FSEVENTS_GET_CURRENT_ID
: {
1709 *(uint64_t *)data
= fseh
->watcher
->max_event_id
;
1714 case FSEVENTS_DEVICE_FILTER_32
: {
1715 if (proc_is64bit(vfs_context_proc(ctx
))) {
1719 fsevent_dev_filter_args32
*devfilt_args32
= (fsevent_dev_filter_args32
*)data
;
1721 devfilt_args
= &_devfilt_args
;
1722 memset(devfilt_args
, 0, sizeof(fsevent_dev_filter_args64
));
1723 devfilt_args
->num_devices
= devfilt_args32
->num_devices
;
1724 devfilt_args
->devices
= CAST_USER_ADDR_T(devfilt_args32
->devices
);
1725 goto handle_dev_filter
;
1728 case FSEVENTS_DEVICE_FILTER_64
:
1729 if (!proc_is64bit(vfs_context_proc(ctx
))) {
1733 devfilt_args
= (fsevent_dev_filter_args64
*)data
;
1737 int new_num_devices
;
1738 dev_t
*devices_not_to_watch
, *tmp
=NULL
;
1740 if (devfilt_args
->num_devices
> 256) {
1745 new_num_devices
= devfilt_args
->num_devices
;
1746 if (new_num_devices
== 0) {
1749 tmp
= fseh
->watcher
->devices_not_to_watch
;
1750 fseh
->watcher
->devices_not_to_watch
= NULL
;
1751 fseh
->watcher
->num_devices
= new_num_devices
;
1753 unlock_watch_table();
1760 MALLOC(devices_not_to_watch
, dev_t
*,
1761 new_num_devices
* sizeof(dev_t
),
1763 if (devices_not_to_watch
== NULL
) {
1768 ret
= copyin(devfilt_args
->devices
,
1769 (void *)devices_not_to_watch
,
1770 new_num_devices
* sizeof(dev_t
));
1772 FREE(devices_not_to_watch
, M_TEMP
);
1777 fseh
->watcher
->num_devices
= new_num_devices
;
1778 tmp
= fseh
->watcher
->devices_not_to_watch
;
1779 fseh
->watcher
->devices_not_to_watch
= devices_not_to_watch
;
1780 unlock_watch_table();
1789 case FSEVENTS_UNMOUNT_PENDING_ACK
: {
1791 dev_t dev
= *(dev_t
*)data
;
1792 if (fsevent_unmount_dev
== dev
) {
1793 if (--fsevent_unmount_ack_count
<= 0) {
1794 fsevent_unmount_dev
= 0;
1795 wakeup((caddr_t
)&fsevent_unmount_dev
);
1798 printf("unexpected unmount pending ack %d (%d)\n", dev
, fsevent_unmount_dev
);
1801 unlock_watch_table();
1810 OSAddAtomic(-1, &fseh
->active
);
1816 fseventsf_select(struct fileproc
*fp
, int which
, __unused
void *wql
, vfs_context_t ctx
)
1818 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
1821 if ((which
!= FREAD
) || (fseh
->watcher
->flags
& WATCHER_CLOSING
)) {
1826 // if there's nothing in the queue, we're not ready
1827 if (fseh
->watcher
->rd
!= fseh
->watcher
->wr
) {
1832 selrecord(vfs_context_proc(ctx
), &fseh
->si
, wql
);
1841 fseventsf_stat(__unused
struct fileproc
*fp
, __unused
struct stat
*sb
, __unused vfs_context_t ctx
)
1848 fseventsf_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1850 fsevent_handle
*fseh
= (struct fsevent_handle
*)fg
->fg_data
;
1851 fs_event_watcher
*watcher
;
1853 OSBitOrAtomic(FSEH_CLOSING
, &fseh
->flags
);
1854 while (OSAddAtomic(0, &fseh
->active
) > 0) {
1855 tsleep((caddr_t
)fseh
->watcher
, PRIBIO
, "fsevents-close", 1);
1858 watcher
= fseh
->watcher
;
1860 fseh
->watcher
= NULL
;
1862 remove_watcher(watcher
);
1869 filt_fsevent_detach(struct knote
*kn
)
1871 fsevent_handle
*fseh
= (struct fsevent_handle
*)kn
->kn_hook
;
1875 KNOTE_DETACH(&fseh
->knotes
, kn
);
1877 unlock_watch_table();
1881 * Determine whether this knote should be active
1883 * This is kind of subtle.
1884 * --First, notice if the vnode has been revoked: in so, override hint
1885 * --EVFILT_READ knotes are checked no matter what the hint is
1886 * --Other knotes activate based on hint.
1887 * --If hint is revoke, set special flags and activate
1890 filt_fsevent(struct knote
*kn
, long hint
)
1892 fsevent_handle
*fseh
= (struct fsevent_handle
*)kn
->kn_hook
;
1894 int32_t rd
, wr
, amt
;
1896 if (NOTE_REVOKE
== hint
) {
1897 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
1901 rd
= fseh
->watcher
->rd
;
1902 wr
= fseh
->watcher
->wr
;
1906 amt
= fseh
->watcher
->eventq_size
- (rd
- wr
);
1909 switch(kn
->kn_filter
) {
1913 if (kn
->kn_data
!= 0) {
1918 /* Check events this note matches against the hint */
1919 if (kn
->kn_sfflags
& hint
) {
1920 kn
->kn_fflags
|= hint
; /* Set which event occurred */
1922 if (kn
->kn_fflags
!= 0) {
1937 filt_fsevent_touch(struct knote
*kn
, struct kevent_internal_s
*kev
)
1943 /* accept new fflags/data as saved */
1944 kn
->kn_sfflags
= kev
->fflags
;
1945 kn
->kn_sdata
= kev
->data
;
1946 if ((kn
->kn_status
& KN_UDATA_SPECIFIC
) == 0)
1947 kn
->kn_udata
= kev
->udata
;
1949 /* restrict the current results to the (smaller?) set of new interest */
1951 * For compatibility with previous implementations, we leave kn_fflags
1952 * as they were before.
1954 //kn->kn_fflags &= kev->fflags;
1956 /* determine if the filter is now fired */
1957 res
= filt_fsevent(kn
, 0);
1959 unlock_watch_table();
1965 filt_fsevent_process(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
)
1967 #pragma unused(data)
1972 res
= filt_fsevent(kn
, 0);
1974 *kev
= kn
->kn_kevent
;
1975 if (kev
->flags
& EV_CLEAR
) {
1981 unlock_watch_table();
1985 struct filterops fsevent_filtops
= {
1988 .f_detach
= filt_fsevent_detach
,
1989 .f_event
= filt_fsevent
,
1990 .f_touch
= filt_fsevent_touch
,
1991 .f_process
= filt_fsevent_process
,
1995 fseventsf_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
, __unused vfs_context_t ctx
)
1997 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
2000 kn
->kn_hook
= (void*)fseh
;
2002 kn
->kn_filtid
= EVFILTID_FSEVENT
;
2006 KNOTE_ATTACH(&fseh
->knotes
, kn
);
2008 /* check to see if it is fired already */
2009 res
= filt_fsevent(kn
, 0);
2011 unlock_watch_table();
2018 fseventsf_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
2021 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
2023 fseh
->watcher
->flags
|= WATCHER_CLOSING
;
2025 // if there are people still waiting, sleep for 10ms to
2026 // let them clean up and get out of there. however we
2027 // also don't want to get stuck forever so if they don't
2028 // exit after 5 seconds we're tearing things down anyway.
2029 while(fseh
->watcher
->blockers
&& counter
++ < 500) {
2030 // issue wakeup in case anyone is blocked waiting for an event
2031 // do this each time we wakeup in case the blocker missed
2032 // the wakeup due to the unprotected test of WATCHER_CLOSING
2033 // and decision to tsleep in fmod_watch... this bit of
2034 // latency is a decent tradeoff against not having to
2035 // take and drop a lock in fmod_watch
2037 fsevents_wakeup(fseh
->watcher
);
2038 unlock_watch_table();
2040 tsleep((caddr_t
)fseh
->watcher
, PRIBIO
, "watcher-close", 1);
2048 fseventsopen(__unused dev_t dev
, __unused
int flag
, __unused
int mode
, __unused
struct proc
*p
)
2050 if (!kauth_cred_issuser(kauth_cred_get())) {
2058 fseventsclose(__unused dev_t dev
, __unused
int flag
, __unused
int mode
, __unused
struct proc
*p
)
2064 fseventsread(__unused dev_t dev
, __unused
struct uio
*uio
, __unused
int ioflag
)
2071 parse_buffer_and_add_events(const char *buffer
, int bufsize
, vfs_context_t ctx
, long *remainder
)
2073 const fse_info
*finfo
, *dest_finfo
;
2074 const char *path
, *ptr
, *dest_path
, *event_start
=buffer
;
2075 int path_len
, type
, dest_path_len
, err
= 0;
2079 while ((ptr
+sizeof(int)+sizeof(fse_info
)+1) < buffer
+bufsize
) {
2080 type
= *(const int *)ptr
;
2081 if (type
< 0 || type
>= FSE_MAX_EVENTS
) {
2088 finfo
= (const fse_info
*)ptr
;
2089 ptr
+= sizeof(fse_info
);
2092 while(ptr
< buffer
+bufsize
&& *ptr
!= '\0') {
2096 if (ptr
>= buffer
+bufsize
) {
2100 ptr
++; // advance over the trailing '\0'
2102 path_len
= ptr
- path
;
2104 if (type
!= FSE_RENAME
&& type
!= FSE_EXCHANGE
) {
2105 event_start
= ptr
; // record where the next event starts
2107 err
= add_fsevent(type
, ctx
, FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
, FSE_ARG_DONE
);
2115 // if we're here we have to slurp up the destination finfo
2116 // and path so that we can pass them to the add_fsevent()
2117 // call. basically it's a copy of the above code.
2119 dest_finfo
= (const fse_info
*)ptr
;
2120 ptr
+= sizeof(fse_info
);
2123 while(ptr
< buffer
+bufsize
&& *ptr
!= '\0') {
2127 if (ptr
>= buffer
+bufsize
) {
2131 ptr
++; // advance over the trailing '\0'
2132 event_start
= ptr
; // record where the next event starts
2134 dest_path_len
= ptr
- dest_path
;
2136 // If the destination inode number is non-zero, generate a rename
2137 // with both source and destination FSE_ARG_FINFO. Otherwise generate
2138 // a rename with only one FSE_ARG_FINFO. If you need to inject an
2139 // exchange with an inode of zero, just make that inode (and its path)
2140 // come in as the first one, not the second.
2142 if (dest_finfo
->ino
) {
2143 err
= add_fsevent(type
, ctx
,
2144 FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
,
2145 FSE_ARG_STRING
, dest_path_len
, dest_path
, FSE_ARG_FINFO
, dest_finfo
,
2148 err
= add_fsevent(type
, ctx
,
2149 FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
,
2150 FSE_ARG_STRING
, dest_path_len
, dest_path
,
2160 // if the last event wasn't complete, set the remainder
2161 // to be the last event start boundary.
2163 *remainder
= (long)((buffer
+bufsize
) - event_start
);
2170 // Note: this buffer size can not ever be less than
2171 // 2*MAXPATHLEN + 2*sizeof(fse_info) + sizeof(int)
2172 // because that is the max size for a single event.
2173 // I made it 4k to be a "nice" size. making it
2174 // smaller is not a good idea.
2176 #define WRITE_BUFFER_SIZE 4096
2177 char *write_buffer
=NULL
;
2180 fseventswrite(__unused dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
2183 vfs_context_t ctx
= vfs_context_current();
2184 long offset
=0, remainder
;
2186 lck_mtx_lock(&event_writer_lock
);
2188 if (write_buffer
== NULL
) {
2189 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&write_buffer
, WRITE_BUFFER_SIZE
, VM_KERN_MEMORY_FILE
)) {
2190 lck_mtx_unlock(&event_writer_lock
);
2196 // this loop copies in and processes the events written.
2197 // it takes care to copy in reasonable size chunks and
2198 // process them. if there is an event that spans a chunk
2199 // boundary we're careful to copy those bytes down to the
2200 // beginning of the buffer and read the next chunk in just
2203 while(uio_resid(uio
)) {
2204 if (uio_resid(uio
) > (WRITE_BUFFER_SIZE
-offset
)) {
2205 count
= WRITE_BUFFER_SIZE
- offset
;
2207 count
= uio_resid(uio
);
2210 error
= uiomove(write_buffer
+offset
, count
, uio
);
2215 // printf("fsevents: write: copied in %d bytes (offset: %ld)\n", count, offset);
2216 error
= parse_buffer_and_add_events(write_buffer
, offset
+count
, ctx
, &remainder
);
2222 // if there's any remainder, copy it down to the beginning
2223 // of the buffer so that it will get processed the next time
2224 // through the loop. note that the remainder always starts
2225 // at an event boundary.
2227 if (remainder
!= 0) {
2228 // printf("fsevents: write: an event spanned a %d byte boundary. remainder: %ld\n",
2229 // WRITE_BUFFER_SIZE, remainder);
2230 memmove(write_buffer
, (write_buffer
+count
+offset
) - remainder
, remainder
);
2237 lck_mtx_unlock(&event_writer_lock
);
2243 static const struct fileops fsevents_fops
= {
2244 .fo_type
= DTYPE_FSEVENTS
,
2245 .fo_read
= fseventsf_read
,
2246 .fo_write
= fseventsf_write
,
2247 .fo_ioctl
= fseventsf_ioctl
,
2248 .fo_select
= fseventsf_select
,
2249 .fo_close
= fseventsf_close
,
2250 .fo_kqfilter
= fseventsf_kqfilter
,
2251 .fo_drain
= fseventsf_drain
,
2254 typedef struct fsevent_clone_args32
{
2255 user32_addr_t event_list
;
2257 int32_t event_queue_depth
;
2259 } fsevent_clone_args32
;
2261 typedef struct fsevent_clone_args64
{
2262 user64_addr_t event_list
;
2264 int32_t event_queue_depth
;
2266 } fsevent_clone_args64
;
2268 #define FSEVENTS_CLONE_32 _IOW('s', 1, fsevent_clone_args32)
2269 #define FSEVENTS_CLONE_64 _IOW('s', 1, fsevent_clone_args64)
2272 fseventsioctl(__unused dev_t dev
, u_long cmd
, caddr_t data
, __unused
int flag
, struct proc
*p
)
2276 fsevent_handle
*fseh
= NULL
;
2277 fsevent_clone_args64
*fse_clone_args
, _fse_clone
;
2279 int is64bit
= proc_is64bit(p
);
2282 case FSEVENTS_CLONE_32
: {
2286 fsevent_clone_args32
*args32
= (fsevent_clone_args32
*)data
;
2288 fse_clone_args
= &_fse_clone
;
2289 memset(fse_clone_args
, 0, sizeof(fsevent_clone_args64
));
2291 fse_clone_args
->event_list
= CAST_USER_ADDR_T(args32
->event_list
);
2292 fse_clone_args
->num_events
= args32
->num_events
;
2293 fse_clone_args
->event_queue_depth
= args32
->event_queue_depth
;
2294 fse_clone_args
->fd
= CAST_USER_ADDR_T(args32
->fd
);
2298 case FSEVENTS_CLONE_64
:
2302 fse_clone_args
= (fsevent_clone_args64
*)data
;
2305 if (fse_clone_args
->num_events
< 0 || fse_clone_args
->num_events
> 4096) {
2309 MALLOC(fseh
, fsevent_handle
*, sizeof(fsevent_handle
),
2314 memset(fseh
, 0, sizeof(fsevent_handle
));
2316 klist_init(&fseh
->knotes
);
2318 MALLOC(event_list
, int8_t *,
2319 fse_clone_args
->num_events
* sizeof(int8_t),
2321 if (event_list
== NULL
) {
2326 error
= copyin(fse_clone_args
->event_list
,
2328 fse_clone_args
->num_events
* sizeof(int8_t));
2330 FREE(event_list
, M_TEMP
);
2335 error
= add_watcher(event_list
,
2336 fse_clone_args
->num_events
,
2337 fse_clone_args
->event_queue_depth
,
2341 FREE(event_list
, M_TEMP
);
2346 fseh
->watcher
->fseh
= fseh
;
2348 error
= falloc(p
, &f
, &fd
, vfs_context_current());
2350 remove_watcher(fseh
->watcher
);
2351 FREE(event_list
, M_TEMP
);
2356 f
->f_fglob
->fg_flag
= FREAD
| FWRITE
;
2357 f
->f_fglob
->fg_ops
= &fsevents_fops
;
2358 f
->f_fglob
->fg_data
= (caddr_t
) fseh
;
2360 error
= copyout((void *)&fd
, fse_clone_args
->fd
, sizeof(int32_t));
2365 procfdtbl_releasefd(p
, fd
, NULL
);
2366 fp_drop(p
, fd
, f
, 1);
2380 fsevents_wakeup(fs_event_watcher
*watcher
)
2382 selwakeup(&watcher
->fseh
->si
);
2383 KNOTE(&watcher
->fseh
->knotes
, NOTE_WRITE
|NOTE_NONE
);
2384 wakeup((caddr_t
)watcher
);
2389 * A struct describing which functions will get invoked for certain
2392 static struct cdevsw fsevents_cdevsw
=
2394 fseventsopen
, /* open */
2395 fseventsclose
, /* close */
2396 fseventsread
, /* read */
2397 fseventswrite
, /* write */
2398 fseventsioctl
, /* ioctl */
2399 (stop_fcn_t
*)&nulldev
, /* stop */
2400 (reset_fcn_t
*)&nulldev
, /* reset */
2402 eno_select
, /* select */
2403 eno_mmap
, /* mmap */
2404 eno_strat
, /* strategy */
2405 eno_getc
, /* getc */
2406 eno_putc
, /* putc */
2412 * Called to initialize our device,
2413 * and to register ourselves with devfs
2421 if (fsevents_installed
) {
2425 fsevents_installed
= 1;
2427 ret
= cdevsw_add(-1, &fsevents_cdevsw
);
2429 fsevents_installed
= 0;
2433 devfs_make_node(makedev (ret
, 0), DEVFS_CHAR
,
2434 UID_ROOT
, GID_WHEEL
, 0644, "fsevents", 0);
2436 fsevents_internal_init();
2445 MALLOC_ZONE(path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2450 release_pathbuff(char *path
)
2456 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
2460 get_fse_info(struct vnode
*vp
, fse_info
*fse
, __unused vfs_context_t ctx
)
2462 struct vnode_attr va
;
2465 VATTR_WANTED(&va
, va_fsid
);
2466 VATTR_WANTED(&va
, va_fileid
);
2467 VATTR_WANTED(&va
, va_mode
);
2468 VATTR_WANTED(&va
, va_uid
);
2469 VATTR_WANTED(&va
, va_gid
);
2470 if (vp
->v_flag
& VISHARDLINK
) {
2471 if (vp
->v_type
== VDIR
) {
2472 VATTR_WANTED(&va
, va_dirlinkcount
);
2474 VATTR_WANTED(&va
, va_nlink
);
2478 if (vnode_getattr(vp
, &va
, vfs_context_kernel()) != 0) {
2479 memset(fse
, 0, sizeof(fse_info
));
2483 return vnode_get_fse_info_from_vap(vp
, fse
, &va
);
2487 vnode_get_fse_info_from_vap(vnode_t vp
, fse_info
*fse
, struct vnode_attr
*vap
)
2489 fse
->ino
= (ino64_t
)vap
->va_fileid
;
2490 fse
->dev
= (dev_t
)vap
->va_fsid
;
2491 fse
->mode
= (int32_t)vnode_vttoif(vnode_vtype(vp
)) | vap
->va_mode
;
2492 fse
->uid
= (uid_t
)vap
->va_uid
;
2493 fse
->gid
= (gid_t
)vap
->va_gid
;
2494 if (vp
->v_flag
& VISHARDLINK
) {
2495 fse
->mode
|= FSE_MODE_HLINK
;
2496 if (vp
->v_type
== VDIR
) {
2497 fse
->nlink
= (uint64_t)vap
->va_dirlinkcount
;
2499 fse
->nlink
= (uint64_t)vap
->va_nlink
;
2507 create_fsevent_from_kevent(vnode_t vp
, uint32_t kevents
, struct vnode_attr
*vap
)
2509 int fsevent_type
=FSE_CONTENT_MODIFIED
, len
; // the default is the most pessimistic
2510 char pathbuf
[MAXPATHLEN
];
2514 if (kevents
& VNODE_EVENT_DELETE
) {
2515 fsevent_type
= FSE_DELETE
;
2516 } else if (kevents
& (VNODE_EVENT_EXTEND
|VNODE_EVENT_WRITE
)) {
2517 fsevent_type
= FSE_CONTENT_MODIFIED
;
2518 } else if (kevents
& VNODE_EVENT_LINK
) {
2519 fsevent_type
= FSE_CREATE_FILE
;
2520 } else if (kevents
& VNODE_EVENT_RENAME
) {
2521 fsevent_type
= FSE_CREATE_FILE
; // XXXdbg - should use FSE_RENAME but we don't have the destination info;
2522 } else if (kevents
& (VNODE_EVENT_FILE_CREATED
|VNODE_EVENT_FILE_REMOVED
|VNODE_EVENT_DIR_CREATED
|VNODE_EVENT_DIR_REMOVED
)) {
2523 fsevent_type
= FSE_STAT_CHANGED
; // XXXdbg - because vp is a dir and the thing created/removed lived inside it
2524 } else { // a catch all for VNODE_EVENT_PERMS, VNODE_EVENT_ATTRIB and anything else
2525 fsevent_type
= FSE_STAT_CHANGED
;
2528 // printf("convert_kevent: kevents 0x%x fsevent type 0x%x (for %s)\n", kevents, fsevent_type, vp->v_name ? vp->v_name : "(no-name)");
2530 fse
.dev
= vap
->va_fsid
;
2531 fse
.ino
= vap
->va_fileid
;
2532 fse
.mode
= vnode_vttoif(vnode_vtype(vp
)) | (uint32_t)vap
->va_mode
;
2533 if (vp
->v_flag
& VISHARDLINK
) {
2534 fse
.mode
|= FSE_MODE_HLINK
;
2535 if (vp
->v_type
== VDIR
) {
2536 fse
.nlink
= vap
->va_dirlinkcount
;
2538 fse
.nlink
= vap
->va_nlink
;
2542 if (vp
->v_type
== VDIR
) {
2543 fse
.mode
|= FSE_REMOTE_DIR_EVENT
;
2547 fse
.uid
= vap
->va_uid
;
2548 fse
.gid
= vap
->va_gid
;
2550 len
= sizeof(pathbuf
);
2551 if (vn_getpath(vp
, pathbuf
, &len
) == 0) {
2552 add_fsevent(fsevent_type
, vfs_context_current(), FSE_ARG_STRING
, len
, pathbuf
, FSE_ARG_FINFO
, &fse
, FSE_ARG_DONE
);
2557 #else /* CONFIG_FSE */
2559 #include <sys/fsevents.h>
2562 * The get_pathbuff and release_pathbuff routines are used in places not
2563 * related to fsevents, and it's a handy abstraction, so define trivial
2564 * versions that don't cache a pool of buffers. This way, we don't have
2565 * to conditionalize the callers, and they still get the advantage of the
2566 * pool of buffers if CONFIG_FSE is turned on.
2572 MALLOC_ZONE(path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2577 release_pathbuff(char *path
)
2579 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
2583 add_fsevent(__unused
int type
, __unused vfs_context_t ctx
, ...)
2588 int need_fsevent(__unused
int type
, __unused vnode_t vp
)
2593 #endif /* CONFIG_FSE */