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
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 static int watcher_add_event(fs_event_watcher
*watcher
, kfs_event
*kfse
);
144 static void fsevents_wakeup(fs_event_watcher
*watcher
);
149 static lck_grp_attr_t
* fsevent_group_attr
;
150 static lck_attr_t
* fsevent_lock_attr
;
151 static lck_grp_t
* fsevent_mutex_group
;
153 static lck_grp_t
* fsevent_rw_group
;
155 static lck_rw_t event_handling_lock
; // handles locking for event manipulation and recycling
156 static lck_mtx_t watch_table_lock
;
157 static lck_mtx_t event_buf_lock
;
158 static lck_mtx_t event_writer_lock
;
161 /* Explicitly declare qsort so compiler doesn't complain */
162 __private_extern__
void qsort(
166 int (*)(const void *, const void *));
169 is_ignored_directory(const char *path
) {
175 #define IS_TLD(x) strnstr((char *) path, x, MAXPATHLEN)
176 if (IS_TLD("/.Spotlight-V100/") ||
177 IS_TLD("/.MobileBackups/") ||
178 IS_TLD("/Backups.backupdb/")) {
187 fsevents_internal_init(void)
191 if (fs_event_init
++ != 0) {
195 for(i
=0; i
< FSE_MAX_EVENTS
; i
++) {
196 fs_event_type_watchers
[i
] = 0;
199 memset(watcher_table
, 0, sizeof(watcher_table
));
201 fsevent_lock_attr
= lck_attr_alloc_init();
202 fsevent_group_attr
= lck_grp_attr_alloc_init();
203 fsevent_mutex_group
= lck_grp_alloc_init("fsevent-mutex", fsevent_group_attr
);
204 fsevent_rw_group
= lck_grp_alloc_init("fsevent-rw", fsevent_group_attr
);
206 lck_mtx_init(&watch_table_lock
, fsevent_mutex_group
, fsevent_lock_attr
);
207 lck_mtx_init(&event_buf_lock
, fsevent_mutex_group
, fsevent_lock_attr
);
208 lck_mtx_init(&event_writer_lock
, fsevent_mutex_group
, fsevent_lock_attr
);
210 lck_rw_init(&event_handling_lock
, fsevent_rw_group
, fsevent_lock_attr
);
212 PE_get_default("kern.maxkfsevents", &max_kfs_events
, sizeof(max_kfs_events
));
214 event_zone
= zinit(sizeof(kfs_event
),
215 max_kfs_events
* sizeof(kfs_event
),
216 max_kfs_events
* sizeof(kfs_event
),
218 if (event_zone
== NULL
) {
219 printf("fsevents: failed to initialize the event zone.\n");
222 // mark the zone as exhaustible so that it will not
223 // ever grow beyond what we initially filled it with
224 zone_change(event_zone
, Z_EXHAUST
, TRUE
);
225 zone_change(event_zone
, Z_COLLECT
, FALSE
);
226 zone_change(event_zone
, Z_CALLERACCT
, FALSE
);
228 if (zfill(event_zone
, max_kfs_events
) < max_kfs_events
) {
229 printf("fsevents: failed to pre-fill the event zone.\n");
235 lock_watch_table(void)
237 lck_mtx_lock(&watch_table_lock
);
241 unlock_watch_table(void)
243 lck_mtx_unlock(&watch_table_lock
);
247 lock_fs_event_list(void)
249 lck_mtx_lock(&event_buf_lock
);
253 unlock_fs_event_list(void)
255 lck_mtx_unlock(&event_buf_lock
);
259 static void release_event_ref(kfs_event
*kfse
);
262 watcher_cares_about_dev(fs_event_watcher
*watcher
, dev_t dev
)
266 // if devices_not_to_watch is NULL then we care about all
267 // events from all devices
268 if (watcher
->devices_not_to_watch
== NULL
) {
272 for(i
=0; i
< watcher
->num_devices
; i
++) {
273 if (dev
== watcher
->devices_not_to_watch
[i
]) {
274 // found a match! that means we do not
275 // want events from this device.
280 // if we're here it's not in the devices_not_to_watch[]
281 // list so that means we do care about it
287 need_fsevent(int type
, vnode_t vp
)
289 if (type
>= 0 && type
< FSE_MAX_EVENTS
&& fs_event_type_watchers
[type
] == 0)
292 // events in /dev aren't really interesting...
293 if (vp
->v_tag
== VT_DEVFS
) {
301 #define is_throw_away(x) ((x) == FSE_STAT_CHANGED || (x) == FSE_CONTENT_MODIFIED)
304 // Ways that an event can be reused:
306 // "combined" events mean that there were two events for
307 // the same vnode or path and we're combining both events
308 // into a single event. The primary event gets a bit that
309 // marks it as having been combined. The secondary event
310 // is essentially dropped and the kfse structure reused.
312 // "collapsed" means that multiple events below a given
313 // directory are collapsed into a single event. in this
314 // case, the directory that we collapse into and all of
315 // its children must be re-scanned.
317 // "recycled" means that we're completely blowing away
318 // the event since there are other events that have info
319 // about the same vnode or path (and one of those other
320 // events will be marked as combined or collapsed as
323 #define KFSE_COMBINED 0x0001
324 #define KFSE_COLLAPSED 0x0002
325 #define KFSE_RECYCLED 0x0004
328 int num_parent_switch
= 0;
329 int num_recycled_rename
= 0;
331 static struct timeval last_print
;
334 // These variables are used to track coalescing multiple identical
335 // events for the same vnode/pathname. If we get the same event
336 // type and same vnode/pathname as the previous event, we just drop
337 // the event since it's superfluous. This improves some micro-
338 // benchmarks considerably and actually has a real-world impact on
339 // tests like a Finder copy where multiple stat-changed events can
342 static int last_event_type
=-1;
343 static void *last_ptr
=NULL
;
344 static char last_str
[MAXPATHLEN
];
345 static int last_nlen
=0;
346 static int last_vid
=-1;
347 static uint64_t last_coalesced_time
=0;
348 static void *last_event_ptr
=NULL
;
349 int last_coalesced
= 0;
350 static mach_timebase_info_data_t sTimebaseInfo
= { 0, 0 };
354 add_fsevent(int type
, vfs_context_t ctx
, ...)
356 struct proc
*p
= vfs_context_proc(ctx
);
357 int i
, arg_type
, ret
;
358 kfs_event
*kfse
, *kfse_dest
=NULL
, *cur
;
359 fs_event_watcher
*watcher
;
361 int error
= 0, did_alloc
=0;
363 uint64_t now
, elapsed
;
371 // ignore bogus event types..
372 if (type
< 0 || type
>= FSE_MAX_EVENTS
) {
376 // if no one cares about this type of event, bail out
377 if (fs_event_type_watchers
[type
] == 0) {
383 now
= mach_absolute_time();
385 // find a free event and snag it for our use
386 // NOTE: do not do anything that would block until
387 // the lock is dropped.
388 lock_fs_event_list();
391 // check if this event is identical to the previous one...
392 // (as long as it's not an event type that can never be the
393 // same as a previous event)
395 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
) {
397 int vid
=0, was_str
=0, nlen
=0;
399 for(arg_type
=va_arg(ap
, int32_t); arg_type
!= FSE_ARG_DONE
; arg_type
=va_arg(ap
, int32_t)) {
401 case FSE_ARG_VNODE
: {
402 ptr
= va_arg(ap
, void *);
403 vid
= vnode_vid((struct vnode
*)ptr
);
407 case FSE_ARG_STRING
: {
408 nlen
= va_arg(ap
, int32_t);
409 ptr
= va_arg(ap
, void *);
419 if ( sTimebaseInfo
.denom
== 0 ) {
420 (void) clock_timebase_info(&sTimebaseInfo
);
423 elapsed
= (now
- last_coalesced_time
);
424 if (sTimebaseInfo
.denom
!= sTimebaseInfo
.numer
) {
425 if (sTimebaseInfo
.denom
== 1) {
426 elapsed
*= sTimebaseInfo
.numer
;
428 // this could overflow... the worst that will happen is that we'll
429 // send (or not send) an extra event so I'm not going to worry about
430 // doing the math right like dtrace_abs_to_nano() does.
431 elapsed
= (elapsed
* sTimebaseInfo
.numer
) / (uint64_t)sTimebaseInfo
.denom
;
435 if (type
== last_event_type
436 && (elapsed
< 1000000000)
438 ((vid
&& vid
== last_vid
&& last_ptr
== ptr
)
440 (last_str
[0] && last_nlen
== nlen
&& ptr
&& strcmp(last_str
, ptr
) == 0))
444 unlock_fs_event_list();
451 strlcpy(last_str
, ptr
, sizeof(last_str
));
455 last_event_type
= type
;
456 last_coalesced_time
= now
;
462 kfse
= zalloc_noblock(event_zone
);
463 if (kfse
&& (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
)) {
464 kfse_dest
= zalloc_noblock(event_zone
);
465 if (kfse_dest
== NULL
) {
467 zfree(event_zone
, kfse
);
473 if (kfse
== NULL
) { // yikes! no free events
474 unlock_fs_event_list();
477 for(i
=0; i
< MAX_WATCHERS
; i
++) {
478 watcher
= watcher_table
[i
];
479 if (watcher
== NULL
) {
483 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
484 fsevents_wakeup(watcher
);
486 unlock_watch_table();
489 struct timeval current_tv
;
493 // only print a message at most once every 5 seconds
494 microuptime(¤t_tv
);
495 if ((current_tv
.tv_sec
- last_print
.tv_sec
) > 10) {
497 void *junkptr
=zalloc_noblock(event_zone
), *listhead
=kfse_list_head
.lh_first
;
499 printf("add_fsevent: event queue is full! dropping events (num dropped events: %d; num events outstanding: %d).\n", num_dropped
, num_events_outstanding
);
500 printf("add_fsevent: kfse_list head %p ; num_pending_rename %d\n", listhead
, num_pending_rename
);
501 printf("add_fsevent: zalloc sez: %p\n", junkptr
);
502 printf("add_fsevent: event_zone info: %d 0x%x\n", ((int *)event_zone
)[0], ((int *)event_zone
)[1]);
504 for(ii
=0; ii
< MAX_WATCHERS
; ii
++) {
505 if (watcher_table
[ii
] == NULL
) {
509 printf("add_fsevent: watcher %s %p: rd %4d wr %4d q_size %4d flags 0x%x\n",
510 watcher_table
[ii
]->proc_name
,
512 watcher_table
[ii
]->rd
, watcher_table
[ii
]->wr
,
513 watcher_table
[ii
]->eventq_size
, watcher_table
[ii
]->flags
);
515 unlock_watch_table();
517 last_print
= current_tv
;
519 zfree(event_zone
, junkptr
);
525 release_pathbuff(pathbuff
);
531 memset(kfse
, 0, sizeof(kfs_event
));
533 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse
->flags
);
535 last_event_ptr
= kfse
;
538 kfse
->pid
= p
->p_pid
;
539 if (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
) {
540 memset(kfse_dest
, 0, sizeof(kfs_event
));
541 kfse_dest
->refcount
= 1;
542 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse_dest
->flags
);
543 kfse_dest
->type
= type
;
544 kfse_dest
->pid
= p
->p_pid
;
545 kfse_dest
->abstime
= now
;
547 kfse
->dest
= kfse_dest
;
550 num_events_outstanding
++;
551 if (kfse
->type
== FSE_RENAME
) {
552 num_pending_rename
++;
554 LIST_INSERT_HEAD(&kfse_list_head
, kfse
, kevent_list
);
556 if (kfse
->refcount
< 1) {
557 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__
, kfse
->refcount
);
560 unlock_fs_event_list(); // at this point it's safe to unlock
563 // now process the arguments passed in and copy them into
569 if (type
== FSE_DOCID_CREATED
|| type
== FSE_DOCID_CHANGED
) {
573 // These events are special and not like the other events. They only
574 // have a dev_t, src inode #, dest inode #, and a doc-id. We use the
575 // fields that we can in the kfse but have to overlay the dest inode
576 // number and the doc-id on the other fields.
580 arg_type
= va_arg(ap
, int32_t);
581 if (arg_type
== FSE_ARG_DEV
) {
582 cur
->dev
= (dev_t
)(va_arg(ap
, dev_t
));
584 cur
->dev
= (dev_t
)0xbadc0de1;
587 // next the source inode #
588 arg_type
= va_arg(ap
, int32_t);
589 if (arg_type
== FSE_ARG_INO
) {
590 cur
->ino
= (ino64_t
)(va_arg(ap
, ino64_t
));
592 cur
->ino
= 0xbadc0de2;
595 // now the dest inode #
596 arg_type
= va_arg(ap
, int32_t);
597 if (arg_type
== FSE_ARG_INO
) {
598 val
= (ino64_t
)(va_arg(ap
, ino64_t
));
602 // overlay the dest inode number on the str/dest pointer fields
603 memcpy(&cur
->str
, &val
, sizeof(ino64_t
));
606 // and last the document-id
607 arg_type
= va_arg(ap
, int32_t);
608 if (arg_type
== FSE_ARG_INT32
) {
609 val
= (uint64_t)va_arg(ap
, uint32_t);
610 } else if (arg_type
== FSE_ARG_INT64
) {
611 val
= (uint64_t)va_arg(ap
, uint64_t);
616 // the docid is 64-bit and overlays the uid/gid fields
617 memcpy(&cur
->uid
, &val
, sizeof(uint64_t));
622 for(arg_type
=va_arg(ap
, int32_t); arg_type
!= FSE_ARG_DONE
; arg_type
=va_arg(ap
, int32_t))
625 case FSE_ARG_VNODE
: {
626 // this expands out into multiple arguments to the client
628 struct vnode_attr va
;
630 if (kfse
->str
!= NULL
) {
634 vp
= va_arg(ap
, struct vnode
*);
636 panic("add_fsevent: you can't pass me a NULL vnode ptr (type %d)!\n",
641 VATTR_WANTED(&va
, va_fsid
);
642 VATTR_WANTED(&va
, va_fileid
);
643 VATTR_WANTED(&va
, va_mode
);
644 VATTR_WANTED(&va
, va_uid
);
645 VATTR_WANTED(&va
, va_gid
);
646 VATTR_WANTED(&va
, va_nlink
);
647 if ((ret
= vnode_getattr(vp
, &va
, vfs_context_kernel())) != 0) {
648 // printf("add_fsevent: failed to getattr on vp %p (%d)\n", cur->fref.vp, ret);
654 cur
->dev
= dev
= (dev_t
)va
.va_fsid
;
655 cur
->ino
= (ino64_t
)va
.va_fileid
;
656 cur
->mode
= (int32_t)vnode_vttoif(vnode_vtype(vp
)) | va
.va_mode
;
657 cur
->uid
= va
.va_uid
;
658 cur
->gid
= va
.va_gid
;
659 if (vp
->v_flag
& VISHARDLINK
) {
660 cur
->mode
|= FSE_MODE_HLINK
;
661 if ((vp
->v_type
== VDIR
&& va
.va_dirlinkcount
== 0) || (vp
->v_type
== VREG
&& va
.va_nlink
== 0)) {
662 cur
->mode
|= FSE_MODE_LAST_HLINK
;
666 // if we haven't gotten the path yet, get it.
667 if (pathbuff
== NULL
) {
668 pathbuff
= get_pathbuff();
669 pathbuff_len
= MAXPATHLEN
;
672 if ((ret
= vn_getpath(vp
, pathbuff
, &pathbuff_len
)) != 0 || pathbuff
[0] == '\0') {
674 cur
->flags
|= KFSE_CONTAINS_DROPPED_EVENTS
;
677 if (vp
->v_parent
!= NULL
) {
679 } else if (vp
->v_mount
) {
680 strlcpy(pathbuff
, vp
->v_mount
->mnt_vfsstat
.f_mntonname
, MAXPATHLEN
);
690 pathbuff_len
= MAXPATHLEN
;
691 ret
= vn_getpath(vp
, pathbuff
, &pathbuff_len
);
692 } while (ret
== ENOSPC
);
694 if (ret
!= 0 || vp
== NULL
) {
701 // store the path by adding it to the global string table
702 cur
->len
= pathbuff_len
;
703 cur
->str
= vfs_addname(pathbuff
, pathbuff_len
, 0, 0);
704 if (cur
->str
== NULL
|| cur
->str
[0] == '\0') {
705 panic("add_fsevent: was not able to add path %s to event %p.\n", pathbuff
, cur
);
708 release_pathbuff(pathbuff
);
714 case FSE_ARG_FINFO
: {
717 fse
= va_arg(ap
, fse_info
*);
719 cur
->dev
= dev
= (dev_t
)fse
->dev
;
720 cur
->ino
= (ino64_t
)fse
->ino
;
721 cur
->mode
= (int32_t)fse
->mode
;
722 cur
->uid
= (uid_t
)fse
->uid
;
723 cur
->gid
= (uid_t
)fse
->gid
;
724 // if it's a hard-link and this is the last link, flag it
725 if ((fse
->mode
& FSE_MODE_HLINK
) && fse
->nlink
== 0) {
726 cur
->mode
|= FSE_MODE_LAST_HLINK
;
728 if (cur
->mode
& FSE_TRUNCATED_PATH
) {
729 cur
->flags
|= KFSE_CONTAINS_DROPPED_EVENTS
;
730 cur
->mode
&= ~FSE_TRUNCATED_PATH
;
736 if (kfse
->str
!= NULL
) {
740 cur
->len
= (int16_t)(va_arg(ap
, int32_t) & 0x7fff);
742 cur
->str
= vfs_addname(va_arg(ap
, char *), cur
->len
, 0, 0);
744 printf("add_fsevent: funny looking string length: %d\n", (int)cur
->len
);
746 cur
->str
= vfs_addname("/", cur
->len
, 0, 0);
748 if (cur
->str
[0] == 0) {
749 printf("add_fsevent: bogus looking string (len %d)\n", cur
->len
);
753 case FSE_ARG_INT32
: {
754 uint32_t ival
= (uint32_t)va_arg(ap
, int32_t);
755 kfse
->uid
= (ino64_t
)ival
;
760 printf("add_fsevent: unknown type %d\n", arg_type
);
761 // just skip one 32-bit word and hope we sync up...
762 (void)va_arg(ap
, int32_t);
768 OSBitAndAtomic16(~KFSE_BEING_CREATED
, &kfse
->flags
);
770 OSBitAndAtomic16(~KFSE_BEING_CREATED
, &kfse_dest
->flags
);
774 // now we have to go and let everyone know that
775 // is interested in this type of event
779 for(i
=0; i
< MAX_WATCHERS
; i
++) {
780 watcher
= watcher_table
[i
];
781 if (watcher
== NULL
) {
785 if ( watcher
->event_list
[type
] == FSE_REPORT
786 && watcher_cares_about_dev(watcher
, dev
)) {
788 if (watcher_add_event(watcher
, kfse
) != 0) {
789 watcher
->num_dropped
++;
794 // if (kfse->refcount < 1) {
795 // panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__, kfse->refcount);
799 unlock_watch_table();
804 release_pathbuff(pathbuff
);
808 release_event_ref(kfse
);
815 release_event_ref(kfs_event
*kfse
)
818 kfs_event copy
, dest_copy
;
821 old_refcount
= OSAddAtomic(-1, &kfse
->refcount
);
822 if (old_refcount
> 1) {
826 lock_fs_event_list();
827 if (last_event_ptr
== kfse
) {
828 last_event_ptr
= NULL
;
829 last_event_type
= -1;
830 last_coalesced_time
= 0;
833 if (kfse
->refcount
< 0) {
834 panic("release_event_ref: bogus kfse refcount %d\n", kfse
->refcount
);
837 if (kfse
->refcount
> 0 || kfse
->type
== FSE_INVALID
) {
838 // This is very subtle. Either of these conditions can
839 // be true if an event got recycled while we were waiting
840 // on the fs_event_list lock or the event got recycled,
841 // delivered, _and_ free'd by someone else while we were
842 // waiting on the fs event list lock. In either case
843 // we need to just unlock the list and return without
844 // doing anything because if the refcount is > 0 then
845 // someone else will take care of free'ing it and when
846 // the kfse->type is invalid then someone else already
847 // has handled free'ing the event (while we were blocked
848 // on the event list lock).
850 unlock_fs_event_list();
855 // make a copy of this so we can free things without
856 // holding the fs_event_buf lock
859 if (kfse
->dest
&& OSAddAtomic(-1, &kfse
->dest
->refcount
) == 1) {
860 dest_copy
= *kfse
->dest
;
862 dest_copy
.str
= NULL
;
864 dest_copy
.type
= FSE_INVALID
;
867 kfse
->pid
= kfse
->type
; // save this off for debugging...
868 kfse
->uid
= (uid_t
)(long)kfse
->str
; // save this off for debugging...
869 kfse
->gid
= (gid_t
)(long)current_thread();
871 kfse
->str
= (char *)0xdeadbeef; // XXXdbg - catch any cheaters...
873 if (dest_copy
.type
!= FSE_INVALID
) {
874 kfse
->dest
->str
= (char *)0xbadc0de; // XXXdbg - catch any cheaters...
875 kfse
->dest
->type
= FSE_INVALID
;
877 if (kfse
->dest
->kevent_list
.le_prev
!= NULL
) {
878 num_events_outstanding
--;
879 LIST_REMOVE(kfse
->dest
, kevent_list
);
880 memset(&kfse
->dest
->kevent_list
, 0xa5, sizeof(kfse
->dest
->kevent_list
));
883 zfree(event_zone
, kfse
->dest
);
886 // mark this fsevent as invalid
891 kfse
->type
= FSE_INVALID
;
893 if (kfse
->kevent_list
.le_prev
!= NULL
) {
894 num_events_outstanding
--;
895 if (otype
== FSE_RENAME
) {
896 num_pending_rename
--;
898 LIST_REMOVE(kfse
, kevent_list
);
899 memset(&kfse
->kevent_list
, 0, sizeof(kfse
->kevent_list
));
903 zfree(event_zone
, kfse
);
905 unlock_fs_event_list();
907 // if we have a pointer in the union
908 if (copy
.str
&& copy
.type
!= FSE_DOCID_CHANGED
) {
909 if (copy
.len
== 0) { // and it's not a string
910 panic("%s:%d: no more fref.vp!\n", __FILE__
, __LINE__
);
911 // vnode_rele_ext(copy.fref.vp, O_EVTONLY, 0);
912 } else { // else it's a string
913 vfs_removename(copy
.str
);
917 if (dest_copy
.type
!= FSE_INVALID
&& dest_copy
.str
) {
918 if (dest_copy
.len
== 0) {
919 panic("%s:%d: no more fref.vp!\n", __FILE__
, __LINE__
);
920 // vnode_rele_ext(dest_copy.fref.vp, O_EVTONLY, 0);
922 vfs_removename(dest_copy
.str
);
928 add_watcher(int8_t *event_list
, int32_t num_events
, int32_t eventq_size
, fs_event_watcher
**watcher_out
, void *fseh
)
931 fs_event_watcher
*watcher
;
933 if (eventq_size
<= 0 || eventq_size
> 100*max_kfs_events
) {
934 eventq_size
= max_kfs_events
;
937 // Note: the event_queue follows the fs_event_watcher struct
938 // in memory so we only have to do one allocation
941 sizeof(fs_event_watcher
) + eventq_size
* sizeof(kfs_event
*),
943 if (watcher
== NULL
) {
947 watcher
->event_list
= event_list
;
948 watcher
->num_events
= num_events
;
949 watcher
->devices_not_to_watch
= NULL
;
950 watcher
->num_devices
= 0;
952 watcher
->event_queue
= (kfs_event
**)&watcher
[1];
953 watcher
->eventq_size
= eventq_size
;
956 watcher
->blockers
= 0;
957 watcher
->num_readers
= 0;
958 watcher
->max_event_id
= 0;
959 watcher
->fseh
= fseh
;
960 watcher
->pid
= proc_selfpid();
961 proc_selfname(watcher
->proc_name
, sizeof(watcher
->proc_name
));
963 watcher
->num_dropped
= 0; // XXXdbg - debugging
965 if (!strncmp(watcher
->proc_name
, "fseventsd", sizeof(watcher
->proc_name
)) ||
966 !strncmp(watcher
->proc_name
, "coreservicesd", sizeof(watcher
->proc_name
)) ||
967 !strncmp(watcher
->proc_name
, "mds", sizeof(watcher
->proc_name
))) {
968 watcher
->flags
|= WATCHER_APPLE_SYSTEM_SERVICE
;
970 printf("fsevents: watcher %s (pid: %d) - Using /dev/fsevents directly is unsupported. Migrate to FSEventsFramework\n",
971 watcher
->proc_name
, watcher
->pid
);
976 // find a slot for the new watcher
977 for(i
=0; i
< MAX_WATCHERS
; i
++) {
978 if (watcher_table
[i
] == NULL
) {
980 watcher_table
[i
] = watcher
;
985 if (i
>= MAX_WATCHERS
) {
986 printf("fsevents: too many watchers!\n");
987 unlock_watch_table();
988 FREE(watcher
, M_TEMP
);
992 // now update the global list of who's interested in
993 // events of a particular type...
994 for(i
=0; i
< num_events
; i
++) {
995 if (event_list
[i
] != FSE_IGNORE
&& i
< FSE_MAX_EVENTS
) {
996 fs_event_type_watchers
[i
]++;
1000 unlock_watch_table();
1002 *watcher_out
= watcher
;
1010 remove_watcher(fs_event_watcher
*target
)
1012 int i
, j
, counter
=0;
1013 fs_event_watcher
*watcher
;
1018 for(j
=0; j
< MAX_WATCHERS
; j
++) {
1019 watcher
= watcher_table
[j
];
1020 if (watcher
!= target
) {
1024 watcher_table
[j
] = NULL
;
1026 for(i
=0; i
< watcher
->num_events
; i
++) {
1027 if (watcher
->event_list
[i
] != FSE_IGNORE
&& i
< FSE_MAX_EVENTS
) {
1028 fs_event_type_watchers
[i
]--;
1032 if (watcher
->flags
& WATCHER_CLOSING
) {
1033 unlock_watch_table();
1037 // 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);
1038 watcher
->flags
|= WATCHER_CLOSING
;
1039 OSAddAtomic(1, &watcher
->num_readers
);
1041 unlock_watch_table();
1043 while (watcher
->num_readers
> 1 && counter
++ < 5000) {
1045 fsevents_wakeup(watcher
); // in case they're asleep
1046 unlock_watch_table();
1048 tsleep(watcher
, PRIBIO
, "fsevents-close", 1);
1050 if (counter
++ >= 5000) {
1051 // printf("fsevents: close: still have readers! (%d)\n", watcher->num_readers);
1052 panic("fsevents: close: still have readers! (%d)\n", watcher
->num_readers
);
1055 // drain the event_queue
1057 lck_rw_lock_exclusive(&event_handling_lock
);
1058 while(watcher
->rd
!= watcher
->wr
) {
1059 kfse
= watcher
->event_queue
[watcher
->rd
];
1060 watcher
->event_queue
[watcher
->rd
] = NULL
;
1061 watcher
->rd
= (watcher
->rd
+1) % watcher
->eventq_size
;
1063 if (kfse
!= NULL
&& kfse
->type
!= FSE_INVALID
&& kfse
->refcount
>= 1) {
1064 release_event_ref(kfse
);
1067 lck_rw_unlock_exclusive(&event_handling_lock
);
1069 if (watcher
->event_list
) {
1070 FREE(watcher
->event_list
, M_TEMP
);
1071 watcher
->event_list
= NULL
;
1073 if (watcher
->devices_not_to_watch
) {
1074 FREE(watcher
->devices_not_to_watch
, M_TEMP
);
1075 watcher
->devices_not_to_watch
= NULL
;
1077 FREE(watcher
, M_TEMP
);
1082 unlock_watch_table();
1086 #define EVENT_DELAY_IN_MS 10
1087 static thread_call_t event_delivery_timer
= NULL
;
1088 static int timer_set
= 0;
1092 delayed_event_delivery(__unused
void *param0
, __unused
void *param1
)
1098 for(i
=0; i
< MAX_WATCHERS
; i
++) {
1099 if (watcher_table
[i
] != NULL
&& watcher_table
[i
]->rd
!= watcher_table
[i
]->wr
) {
1100 fsevents_wakeup(watcher_table
[i
]);
1106 unlock_watch_table();
1111 // The watch table must be locked before calling this function.
1114 schedule_event_wakeup(void)
1118 if (event_delivery_timer
== NULL
) {
1119 event_delivery_timer
= thread_call_allocate((thread_call_func_t
)delayed_event_delivery
, NULL
);
1122 clock_interval_to_deadline(EVENT_DELAY_IN_MS
, 1000 * 1000, &deadline
);
1124 thread_call_enter_delayed(event_delivery_timer
, deadline
);
1130 #define MAX_NUM_PENDING 16
1133 // NOTE: the watch table must be locked before calling
1137 watcher_add_event(fs_event_watcher
*watcher
, kfs_event
*kfse
)
1139 if (kfse
->abstime
> watcher
->max_event_id
) {
1140 watcher
->max_event_id
= kfse
->abstime
;
1143 if (((watcher
->wr
+ 1) % watcher
->eventq_size
) == watcher
->rd
) {
1144 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
1145 fsevents_wakeup(watcher
);
1149 OSAddAtomic(1, &kfse
->refcount
);
1150 watcher
->event_queue
[watcher
->wr
] = kfse
;
1152 watcher
->wr
= (watcher
->wr
+ 1) % watcher
->eventq_size
;
1155 // wake up the watcher if there are more than MAX_NUM_PENDING events.
1156 // otherwise schedule a timer (if one isn't already set) which will
1157 // send any pending events if no more are received in the next
1158 // EVENT_DELAY_IN_MS milli-seconds.
1160 int32_t num_pending
= 0;
1161 if (watcher
->rd
< watcher
->wr
) {
1162 num_pending
= watcher
->wr
- watcher
->rd
;
1165 if (watcher
->rd
> watcher
->wr
) {
1166 num_pending
= watcher
->wr
+ watcher
->eventq_size
- watcher
->rd
;
1169 if (num_pending
> (watcher
->eventq_size
*3/4) && !(watcher
->flags
& WATCHER_APPLE_SYSTEM_SERVICE
)) {
1170 /* Non-Apple Service is falling behind, start dropping events for this process */
1171 lck_rw_lock_exclusive(&event_handling_lock
);
1172 while (watcher
->rd
!= watcher
->wr
) {
1173 kfse
= watcher
->event_queue
[watcher
->rd
];
1174 watcher
->event_queue
[watcher
->rd
] = NULL
;
1175 watcher
->rd
= (watcher
->rd
+1) % watcher
->eventq_size
;
1177 if (kfse
!= NULL
&& kfse
->type
!= FSE_INVALID
&& kfse
->refcount
>= 1) {
1178 release_event_ref(kfse
);
1181 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
1182 lck_rw_unlock_exclusive(&event_handling_lock
);
1184 printf("fsevents: watcher falling behind: %s (pid: %d) rd: %4d wr: %4d q_size: %4d flags: 0x%x\n",
1185 watcher
->proc_name
, watcher
->pid
, watcher
->rd
, watcher
->wr
,
1186 watcher
->eventq_size
, watcher
->flags
);
1188 fsevents_wakeup(watcher
);
1189 } else if (num_pending
> MAX_NUM_PENDING
) {
1190 fsevents_wakeup(watcher
);
1191 } else if (timer_set
== 0) {
1192 schedule_event_wakeup();
1199 fill_buff(uint16_t type
, int32_t size
, const void *data
,
1200 char *buff
, int32_t *_buff_idx
, int32_t buff_sz
,
1203 int32_t amt
, error
= 0, buff_idx
= *_buff_idx
;
1207 // the +1 on the size is to guarantee that the main data
1208 // copy loop will always copy at least 1 byte
1210 if ((buff_sz
- buff_idx
) <= (int)(2*sizeof(uint16_t) + 1)) {
1211 if (buff_idx
> uio_resid(uio
)) {
1216 error
= uiomove(buff
, buff_idx
, uio
);
1223 // copy out the header (type & size)
1224 memcpy(&buff
[buff_idx
], &type
, sizeof(uint16_t));
1225 buff_idx
+= sizeof(uint16_t);
1227 tmp
= size
& 0xffff;
1228 memcpy(&buff
[buff_idx
], &tmp
, sizeof(uint16_t));
1229 buff_idx
+= sizeof(uint16_t);
1231 // now copy the body of the data, flushing along the way
1232 // if the buffer fills up.
1235 amt
= (size
< (buff_sz
- buff_idx
)) ? size
: (buff_sz
- buff_idx
);
1236 memcpy(&buff
[buff_idx
], data
, amt
);
1240 data
= (const char *)data
+ amt
;
1241 if (size
> (buff_sz
- buff_idx
)) {
1242 if (buff_idx
> uio_resid(uio
)) {
1246 error
= uiomove(buff
, buff_idx
, uio
);
1253 if (amt
== 0) { // just in case...
1259 *_buff_idx
= buff_idx
;
1265 static int copy_out_kfse(fs_event_watcher
*watcher
, kfs_event
*kfse
, struct uio
*uio
) __attribute__((noinline
));
1268 copy_out_kfse(fs_event_watcher
*watcher
, kfs_event
*kfse
, struct uio
*uio
)
1277 if (kfse
->type
== FSE_INVALID
) {
1278 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
);
1281 if (kfse
->flags
& KFSE_BEING_CREATED
) {
1285 if (kfse
->type
== FSE_RENAME
&& kfse
->dest
== NULL
) {
1287 // This can happen if an event gets recycled but we had a
1288 // pointer to it in our event queue. The event is the
1289 // destination of a rename which we'll process separately
1290 // (that is, another kfse points to this one so it's ok
1291 // to skip this guy because we'll process it when we process
1297 if (watcher
->flags
& WATCHER_WANTS_EXTENDED_INFO
) {
1299 type
= (kfse
->type
& 0xfff);
1301 if (kfse
->flags
& KFSE_CONTAINS_DROPPED_EVENTS
) {
1302 type
|= (FSE_CONTAINS_DROPPED_EVENTS
<< FSE_FLAG_SHIFT
);
1303 } else if (kfse
->flags
& KFSE_COMBINED_EVENTS
) {
1304 type
|= (FSE_COMBINED_EVENTS
<< FSE_FLAG_SHIFT
);
1308 type
= (int32_t)kfse
->type
;
1311 // copy out the type of the event
1312 memcpy(evbuff
, &type
, sizeof(int32_t));
1313 evbuff_idx
+= sizeof(int32_t);
1315 // copy out the pid of the person that generated the event
1316 memcpy(&evbuff
[evbuff_idx
], &kfse
->pid
, sizeof(pid_t
));
1317 evbuff_idx
+= sizeof(pid_t
);
1323 if (kfse
->type
== FSE_DOCID_CHANGED
|| kfse
->type
== FSE_DOCID_CREATED
) {
1324 dev_t dev
= cur
->dev
;
1325 ino_t ino
= cur
->ino
;
1328 error
= fill_buff(FSE_ARG_DEV
, sizeof(dev_t
), &dev
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1333 error
= fill_buff(FSE_ARG_INO
, sizeof(ino_t
), &ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1338 memcpy(&ino
, &cur
->str
, sizeof(ino_t
));
1339 error
= fill_buff(FSE_ARG_INO
, sizeof(ino_t
), &ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1344 memcpy(&ival
, &cur
->uid
, sizeof(uint64_t)); // the docid gets stuffed into the ino field
1345 error
= fill_buff(FSE_ARG_INT64
, sizeof(uint64_t), &ival
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1353 if (cur
->str
== NULL
|| cur
->str
[0] == '\0') {
1354 printf("copy_out_kfse:2: empty/short path (%s)\n", cur
->str
);
1355 error
= fill_buff(FSE_ARG_STRING
, 2, "/", evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1357 error
= fill_buff(FSE_ARG_STRING
, cur
->len
, cur
->str
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1363 if (cur
->dev
== 0 && cur
->ino
== 0) {
1364 // this happens when a rename event happens and the
1365 // destination of the rename did not previously exist.
1366 // it thus has no other file info so skip copying out
1367 // the stuff below since it isn't initialized
1372 if (watcher
->flags
& WATCHER_WANTS_COMPACT_EVENTS
) {
1375 finfo_size
= sizeof(dev_t
) + sizeof(ino64_t
) + sizeof(int32_t) + sizeof(uid_t
) + sizeof(gid_t
);
1376 error
= fill_buff(FSE_ARG_FINFO
, finfo_size
, &cur
->ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1383 error
= fill_buff(FSE_ARG_DEV
, sizeof(dev_t
), &cur
->dev
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1388 ino
= (ino_t
)cur
->ino
;
1389 error
= fill_buff(FSE_ARG_INO
, sizeof(ino_t
), &ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1394 error
= fill_buff(FSE_ARG_MODE
, sizeof(int32_t), &cur
->mode
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1399 error
= fill_buff(FSE_ARG_UID
, sizeof(uid_t
), &cur
->uid
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1404 error
= fill_buff(FSE_ARG_GID
, sizeof(gid_t
), &cur
->gid
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1417 // very last thing: the time stamp
1418 error
= fill_buff(FSE_ARG_INT64
, sizeof(uint64_t), &cur
->abstime
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1423 // check if the FSE_ARG_DONE will fit
1424 if (sizeof(uint16_t) > sizeof(evbuff
) - evbuff_idx
) {
1425 if (evbuff_idx
> uio_resid(uio
)) {
1429 error
= uiomove(evbuff
, evbuff_idx
, uio
);
1436 tmp16
= FSE_ARG_DONE
;
1437 memcpy(&evbuff
[evbuff_idx
], &tmp16
, sizeof(uint16_t));
1438 evbuff_idx
+= sizeof(uint16_t);
1440 // flush any remaining data in the buffer (and hopefully
1441 // in most cases this is the only uiomove we'll do)
1442 if (evbuff_idx
> uio_resid(uio
)) {
1445 error
= uiomove(evbuff
, evbuff_idx
, uio
);
1456 fmod_watch(fs_event_watcher
*watcher
, struct uio
*uio
)
1459 user_ssize_t last_full_event_resid
;
1464 last_full_event_resid
= uio_resid(uio
);
1466 // need at least 2048 bytes of space (maxpathlen + 1 event buf)
1467 if (uio_resid(uio
) < 2048 || watcher
== NULL
) {
1471 if (watcher
->flags
& WATCHER_CLOSING
) {
1475 if (OSAddAtomic(1, &watcher
->num_readers
) != 0) {
1476 // don't allow multiple threads to read from the fd at the same time
1477 OSAddAtomic(-1, &watcher
->num_readers
);
1482 if (watcher
->rd
== watcher
->wr
) {
1483 if (watcher
->flags
& WATCHER_CLOSING
) {
1484 OSAddAtomic(-1, &watcher
->num_readers
);
1487 OSAddAtomic(1, &watcher
->blockers
);
1489 // there's nothing to do, go to sleep
1490 error
= tsleep((caddr_t
)watcher
, PUSER
|PCATCH
, "fsevents_empty", 0);
1492 OSAddAtomic(-1, &watcher
->blockers
);
1494 if (error
!= 0 || (watcher
->flags
& WATCHER_CLOSING
)) {
1495 OSAddAtomic(-1, &watcher
->num_readers
);
1500 // if we dropped events, return that as an event first
1501 if (watcher
->flags
& WATCHER_DROPPED_EVENTS
) {
1502 int32_t val
= FSE_EVENTS_DROPPED
;
1504 error
= uiomove((caddr_t
)&val
, sizeof(int32_t), uio
);
1506 val
= 0; // a fake pid
1507 error
= uiomove((caddr_t
)&val
, sizeof(int32_t), uio
);
1509 tmp16
= FSE_ARG_DONE
; // makes it a consistent msg
1510 error
= uiomove((caddr_t
)&tmp16
, sizeof(int16_t), uio
);
1512 last_full_event_resid
= uio_resid(uio
);
1516 OSAddAtomic(-1, &watcher
->num_readers
);
1520 watcher
->flags
&= ~WATCHER_DROPPED_EVENTS
;
1525 lck_rw_lock_shared(&event_handling_lock
);
1526 while (uio_resid(uio
) > 0 && watcher
->rd
!= watcher
->wr
) {
1527 if (watcher
->flags
& WATCHER_CLOSING
) {
1532 // check if the event is something of interest to us
1533 // (since it may have been recycled/reused and changed
1534 // its type or which device it is for)
1536 kfse
= watcher
->event_queue
[watcher
->rd
];
1537 if (!kfse
|| kfse
->type
== FSE_INVALID
|| kfse
->refcount
< 1) {
1541 if (watcher
->event_list
[kfse
->type
] == FSE_REPORT
&& watcher_cares_about_dev(watcher
, kfse
->dev
)) {
1543 if (!(watcher
->flags
& WATCHER_APPLE_SYSTEM_SERVICE
) && kfse
->type
!= FSE_DOCID_CHANGED
&& is_ignored_directory(kfse
->str
)) {
1544 // If this is not an Apple System Service, skip specified directories
1551 if (last_event_ptr
== kfse
) {
1552 last_event_ptr
= NULL
;
1553 last_event_type
= -1;
1554 last_coalesced_time
= 0;
1556 error
= copy_out_kfse(watcher
, kfse
, uio
);
1558 // if an event won't fit or encountered an error while
1559 // we were copying it out, then backup to the last full
1560 // event and just bail out. if the error was ENOENT
1561 // then we can continue regular processing, otherwise
1562 // we should unlock things and return.
1563 uio_setresid(uio
, last_full_event_resid
);
1564 if (error
!= ENOENT
) {
1565 lck_rw_unlock_shared(&event_handling_lock
);
1571 last_full_event_resid
= uio_resid(uio
);
1575 watcher
->event_queue
[watcher
->rd
] = NULL
;
1576 watcher
->rd
= (watcher
->rd
+ 1) % watcher
->eventq_size
;
1578 release_event_ref(kfse
);
1580 lck_rw_unlock_shared(&event_handling_lock
);
1582 if (skipped
&& error
== 0) {
1587 OSAddAtomic(-1, &watcher
->num_readers
);
1593 // release any references we might have on vnodes which are
1594 // the mount point passed to us (so that it can be cleanly
1597 // since we don't want to lose the events we'll convert the
1598 // vnode refs to full paths.
1601 fsevent_unmount(__unused
struct mount
*mp
)
1603 // we no longer maintain pointers to vnodes so
1604 // there is nothing to do...
1609 // /dev/fsevents device code
1611 static int fsevents_installed
= 0;
1613 typedef struct fsevent_handle
{
1616 fs_event_watcher
*watcher
;
1617 struct klist knotes
;
1621 #define FSEH_CLOSING 0x0001
1624 fseventsf_read(struct fileproc
*fp
, struct uio
*uio
,
1625 __unused
int flags
, __unused vfs_context_t ctx
)
1627 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
1630 error
= fmod_watch(fseh
->watcher
, uio
);
1637 fseventsf_write(__unused
struct fileproc
*fp
, __unused
struct uio
*uio
,
1638 __unused
int flags
, __unused vfs_context_t ctx
)
1643 #pragma pack(push, 4)
1644 typedef struct ext_fsevent_dev_filter_args
{
1645 uint32_t num_devices
;
1646 user_addr_t devices
;
1647 } ext_fsevent_dev_filter_args
;
1650 #define NEW_FSEVENTS_DEVICE_FILTER _IOW('s', 100, ext_fsevent_dev_filter_args)
1652 typedef struct old_fsevent_dev_filter_args
{
1653 uint32_t num_devices
;
1655 } old_fsevent_dev_filter_args
;
1657 #define OLD_FSEVENTS_DEVICE_FILTER _IOW('s', 100, old_fsevent_dev_filter_args)
1660 /* need this in spite of the padding due to alignment of devices */
1661 typedef struct fsevent_dev_filter_args32
{
1662 uint32_t num_devices
;
1665 } fsevent_dev_filter_args32
;
1669 fseventsf_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
, vfs_context_t ctx
)
1671 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
1673 ext_fsevent_dev_filter_args
*devfilt_args
, _devfilt_args
;
1675 if (proc_is64bit(vfs_context_proc(ctx
))) {
1676 devfilt_args
= (ext_fsevent_dev_filter_args
*)data
;
1678 else if (cmd
== OLD_FSEVENTS_DEVICE_FILTER
) {
1679 old_fsevent_dev_filter_args
*udev_filt_args
= (old_fsevent_dev_filter_args
*)data
;
1681 devfilt_args
= &_devfilt_args
;
1682 memset(devfilt_args
, 0, sizeof(ext_fsevent_dev_filter_args
));
1684 devfilt_args
->num_devices
= udev_filt_args
->num_devices
;
1685 devfilt_args
->devices
= CAST_USER_ADDR_T(udev_filt_args
->devices
);
1689 fsevent_dev_filter_args32
*udev_filt_args
= (fsevent_dev_filter_args32
*)data
;
1691 fsevent_dev_filter_args
*udev_filt_args
= (fsevent_dev_filter_args
*)data
;
1694 devfilt_args
= &_devfilt_args
;
1695 memset(devfilt_args
, 0, sizeof(ext_fsevent_dev_filter_args
));
1697 devfilt_args
->num_devices
= udev_filt_args
->num_devices
;
1698 devfilt_args
->devices
= CAST_USER_ADDR_T(udev_filt_args
->devices
);
1701 OSAddAtomic(1, &fseh
->active
);
1702 if (fseh
->flags
& FSEH_CLOSING
) {
1703 OSAddAtomic(-1, &fseh
->active
);
1712 case FSEVENTS_WANT_COMPACT_EVENTS
: {
1713 fseh
->watcher
->flags
|= WATCHER_WANTS_COMPACT_EVENTS
;
1717 case FSEVENTS_WANT_EXTENDED_INFO
: {
1718 fseh
->watcher
->flags
|= WATCHER_WANTS_EXTENDED_INFO
;
1722 case FSEVENTS_GET_CURRENT_ID
: {
1723 *(uint64_t *)data
= fseh
->watcher
->max_event_id
;
1728 case OLD_FSEVENTS_DEVICE_FILTER
:
1729 case NEW_FSEVENTS_DEVICE_FILTER
: {
1730 int new_num_devices
;
1731 dev_t
*devices_not_to_watch
, *tmp
=NULL
;
1733 if (devfilt_args
->num_devices
> 256) {
1738 new_num_devices
= devfilt_args
->num_devices
;
1739 if (new_num_devices
== 0) {
1740 tmp
= fseh
->watcher
->devices_not_to_watch
;
1743 fseh
->watcher
->devices_not_to_watch
= NULL
;
1744 fseh
->watcher
->num_devices
= new_num_devices
;
1745 unlock_watch_table();
1753 MALLOC(devices_not_to_watch
, dev_t
*,
1754 new_num_devices
* sizeof(dev_t
),
1756 if (devices_not_to_watch
== NULL
) {
1761 ret
= copyin(devfilt_args
->devices
,
1762 (void *)devices_not_to_watch
,
1763 new_num_devices
* sizeof(dev_t
));
1765 FREE(devices_not_to_watch
, M_TEMP
);
1770 fseh
->watcher
->num_devices
= new_num_devices
;
1771 tmp
= fseh
->watcher
->devices_not_to_watch
;
1772 fseh
->watcher
->devices_not_to_watch
= devices_not_to_watch
;
1773 unlock_watch_table();
1787 OSAddAtomic(-1, &fseh
->active
);
1793 fseventsf_select(struct fileproc
*fp
, int which
, __unused
void *wql
, vfs_context_t ctx
)
1795 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
1798 if ((which
!= FREAD
) || (fseh
->watcher
->flags
& WATCHER_CLOSING
)) {
1803 // if there's nothing in the queue, we're not ready
1804 if (fseh
->watcher
->rd
!= fseh
->watcher
->wr
) {
1809 selrecord(vfs_context_proc(ctx
), &fseh
->si
, wql
);
1818 fseventsf_stat(__unused
struct fileproc
*fp
, __unused
struct stat
*sb
, __unused vfs_context_t ctx
)
1825 fseventsf_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1827 fsevent_handle
*fseh
= (struct fsevent_handle
*)fg
->fg_data
;
1828 fs_event_watcher
*watcher
;
1830 OSBitOrAtomic(FSEH_CLOSING
, &fseh
->flags
);
1831 while (OSAddAtomic(0, &fseh
->active
) > 0) {
1832 tsleep((caddr_t
)fseh
->watcher
, PRIBIO
, "fsevents-close", 1);
1835 watcher
= fseh
->watcher
;
1837 fseh
->watcher
= NULL
;
1839 remove_watcher(watcher
);
1846 filt_fsevent_detach(struct knote
*kn
)
1848 fsevent_handle
*fseh
= (struct fsevent_handle
*)kn
->kn_hook
;
1852 KNOTE_DETACH(&fseh
->knotes
, kn
);
1854 unlock_watch_table();
1858 * Determine whether this knote should be active
1860 * This is kind of subtle.
1861 * --First, notice if the vnode has been revoked: in so, override hint
1862 * --EVFILT_READ knotes are checked no matter what the hint is
1863 * --Other knotes activate based on hint.
1864 * --If hint is revoke, set special flags and activate
1867 filt_fsevent(struct knote
*kn
, long hint
)
1869 fsevent_handle
*fseh
= (struct fsevent_handle
*)kn
->kn_hook
;
1871 int32_t rd
, wr
, amt
;
1873 if (NOTE_REVOKE
== hint
) {
1874 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
1878 rd
= fseh
->watcher
->rd
;
1879 wr
= fseh
->watcher
->wr
;
1883 amt
= fseh
->watcher
->eventq_size
- (rd
- wr
);
1886 switch(kn
->kn_filter
) {
1890 if (kn
->kn_data
!= 0) {
1895 /* Check events this note matches against the hint */
1896 if (kn
->kn_sfflags
& hint
) {
1897 kn
->kn_fflags
|= hint
; /* Set which event occurred */
1899 if (kn
->kn_fflags
!= 0) {
1913 struct filterops fsevent_filtops
= {
1916 .f_detach
= filt_fsevent_detach
,
1917 .f_event
= filt_fsevent
1921 fseventsf_kqfilter(__unused
struct fileproc
*fp
, __unused
struct knote
*kn
, __unused vfs_context_t ctx
)
1923 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
1925 kn
->kn_hook
= (void*)fseh
;
1927 kn
->kn_fop
= &fsevent_filtops
;
1931 KNOTE_ATTACH(&fseh
->knotes
, kn
);
1933 unlock_watch_table();
1939 fseventsf_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
1942 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->f_fglob
->fg_data
;
1944 fseh
->watcher
->flags
|= WATCHER_CLOSING
;
1946 // if there are people still waiting, sleep for 10ms to
1947 // let them clean up and get out of there. however we
1948 // also don't want to get stuck forever so if they don't
1949 // exit after 5 seconds we're tearing things down anyway.
1950 while(fseh
->watcher
->blockers
&& counter
++ < 500) {
1951 // issue wakeup in case anyone is blocked waiting for an event
1952 // do this each time we wakeup in case the blocker missed
1953 // the wakeup due to the unprotected test of WATCHER_CLOSING
1954 // and decision to tsleep in fmod_watch... this bit of
1955 // latency is a decent tradeoff against not having to
1956 // take and drop a lock in fmod_watch
1958 fsevents_wakeup(fseh
->watcher
);
1959 unlock_watch_table();
1961 tsleep((caddr_t
)fseh
->watcher
, PRIBIO
, "watcher-close", 1);
1969 fseventsopen(__unused dev_t dev
, __unused
int flag
, __unused
int mode
, __unused
struct proc
*p
)
1971 if (!kauth_cred_issuser(kauth_cred_get())) {
1979 fseventsclose(__unused dev_t dev
, __unused
int flag
, __unused
int mode
, __unused
struct proc
*p
)
1985 fseventsread(__unused dev_t dev
, __unused
struct uio
*uio
, __unused
int ioflag
)
1992 parse_buffer_and_add_events(const char *buffer
, int bufsize
, vfs_context_t ctx
, long *remainder
)
1994 const fse_info
*finfo
, *dest_finfo
;
1995 const char *path
, *ptr
, *dest_path
, *event_start
=buffer
;
1996 int path_len
, type
, dest_path_len
, err
= 0;
2000 while ((ptr
+sizeof(int)+sizeof(fse_info
)+1) < buffer
+bufsize
) {
2001 type
= *(const int *)ptr
;
2002 if (type
< 0 || type
>= FSE_MAX_EVENTS
) {
2009 finfo
= (const fse_info
*)ptr
;
2010 ptr
+= sizeof(fse_info
);
2013 while(ptr
< buffer
+bufsize
&& *ptr
!= '\0') {
2017 if (ptr
>= buffer
+bufsize
) {
2021 ptr
++; // advance over the trailing '\0'
2023 path_len
= ptr
- path
;
2025 if (type
!= FSE_RENAME
&& type
!= FSE_EXCHANGE
) {
2026 event_start
= ptr
; // record where the next event starts
2028 err
= add_fsevent(type
, ctx
, FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
, FSE_ARG_DONE
);
2036 // if we're here we have to slurp up the destination finfo
2037 // and path so that we can pass them to the add_fsevent()
2038 // call. basically it's a copy of the above code.
2040 dest_finfo
= (const fse_info
*)ptr
;
2041 ptr
+= sizeof(fse_info
);
2044 while(ptr
< buffer
+bufsize
&& *ptr
!= '\0') {
2048 if (ptr
>= buffer
+bufsize
) {
2052 ptr
++; // advance over the trailing '\0'
2053 event_start
= ptr
; // record where the next event starts
2055 dest_path_len
= ptr
- dest_path
;
2057 // If the destination inode number is non-zero, generate a rename
2058 // with both source and destination FSE_ARG_FINFO. Otherwise generate
2059 // a rename with only one FSE_ARG_FINFO. If you need to inject an
2060 // exchange with an inode of zero, just make that inode (and its path)
2061 // come in as the first one, not the second.
2063 if (dest_finfo
->ino
) {
2064 err
= add_fsevent(type
, ctx
,
2065 FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
,
2066 FSE_ARG_STRING
, dest_path_len
, dest_path
, FSE_ARG_FINFO
, dest_finfo
,
2069 err
= add_fsevent(type
, ctx
,
2070 FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
,
2071 FSE_ARG_STRING
, dest_path_len
, dest_path
,
2081 // if the last event wasn't complete, set the remainder
2082 // to be the last event start boundary.
2084 *remainder
= (long)((buffer
+bufsize
) - event_start
);
2091 // Note: this buffer size can not ever be less than
2092 // 2*MAXPATHLEN + 2*sizeof(fse_info) + sizeof(int)
2093 // because that is the max size for a single event.
2094 // I made it 4k to be a "nice" size. making it
2095 // smaller is not a good idea.
2097 #define WRITE_BUFFER_SIZE 4096
2098 char *write_buffer
=NULL
;
2101 fseventswrite(__unused dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
2104 vfs_context_t ctx
= vfs_context_current();
2105 long offset
=0, remainder
;
2107 lck_mtx_lock(&event_writer_lock
);
2109 if (write_buffer
== NULL
) {
2110 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&write_buffer
, WRITE_BUFFER_SIZE
)) {
2111 lck_mtx_unlock(&event_writer_lock
);
2117 // this loop copies in and processes the events written.
2118 // it takes care to copy in reasonable size chunks and
2119 // process them. if there is an event that spans a chunk
2120 // boundary we're careful to copy those bytes down to the
2121 // beginning of the buffer and read the next chunk in just
2124 while(uio_resid(uio
)) {
2125 if (uio_resid(uio
) > (WRITE_BUFFER_SIZE
-offset
)) {
2126 count
= WRITE_BUFFER_SIZE
- offset
;
2128 count
= uio_resid(uio
);
2131 error
= uiomove(write_buffer
+offset
, count
, uio
);
2136 // printf("fsevents: write: copied in %d bytes (offset: %ld)\n", count, offset);
2137 error
= parse_buffer_and_add_events(write_buffer
, offset
+count
, ctx
, &remainder
);
2143 // if there's any remainder, copy it down to the beginning
2144 // of the buffer so that it will get processed the next time
2145 // through the loop. note that the remainder always starts
2146 // at an event boundary.
2148 if (remainder
!= 0) {
2149 // printf("fsevents: write: an event spanned a %d byte boundary. remainder: %ld\n",
2150 // WRITE_BUFFER_SIZE, remainder);
2151 memmove(write_buffer
, (write_buffer
+count
+offset
) - remainder
, remainder
);
2158 lck_mtx_unlock(&event_writer_lock
);
2164 static const struct fileops fsevents_fops
= {
2175 typedef struct ext_fsevent_clone_args
{
2176 user_addr_t event_list
;
2178 int32_t event_queue_depth
;
2180 } ext_fsevent_clone_args
;
2182 typedef struct old_fsevent_clone_args
{
2183 uint32_t event_list
;
2185 int32_t event_queue_depth
;
2187 } old_fsevent_clone_args
;
2189 #define OLD_FSEVENTS_CLONE _IOW('s', 1, old_fsevent_clone_args)
2192 fseventsioctl(__unused dev_t dev
, u_long cmd
, caddr_t data
, __unused
int flag
, struct proc
*p
)
2196 fsevent_handle
*fseh
= NULL
;
2197 ext_fsevent_clone_args
*fse_clone_args
, _fse_clone
;
2199 int is64bit
= proc_is64bit(p
);
2202 case OLD_FSEVENTS_CLONE
: {
2203 old_fsevent_clone_args
*old_args
= (old_fsevent_clone_args
*)data
;
2205 fse_clone_args
= &_fse_clone
;
2206 memset(fse_clone_args
, 0, sizeof(ext_fsevent_clone_args
));
2208 fse_clone_args
->event_list
= CAST_USER_ADDR_T(old_args
->event_list
);
2209 fse_clone_args
->num_events
= old_args
->num_events
;
2210 fse_clone_args
->event_queue_depth
= old_args
->event_queue_depth
;
2211 fse_clone_args
->fd
= CAST_USER_ADDR_T(old_args
->fd
);
2215 case FSEVENTS_CLONE
:
2217 fse_clone_args
= (ext_fsevent_clone_args
*)data
;
2219 fsevent_clone_args
*ufse_clone
= (fsevent_clone_args
*)data
;
2221 fse_clone_args
= &_fse_clone
;
2222 memset(fse_clone_args
, 0, sizeof(ext_fsevent_clone_args
));
2224 fse_clone_args
->event_list
= CAST_USER_ADDR_T(ufse_clone
->event_list
);
2225 fse_clone_args
->num_events
= ufse_clone
->num_events
;
2226 fse_clone_args
->event_queue_depth
= ufse_clone
->event_queue_depth
;
2227 fse_clone_args
->fd
= CAST_USER_ADDR_T(ufse_clone
->fd
);
2231 if (fse_clone_args
->num_events
< 0 || fse_clone_args
->num_events
> 4096) {
2235 MALLOC(fseh
, fsevent_handle
*, sizeof(fsevent_handle
),
2240 memset(fseh
, 0, sizeof(fsevent_handle
));
2242 klist_init(&fseh
->knotes
);
2244 MALLOC(event_list
, int8_t *,
2245 fse_clone_args
->num_events
* sizeof(int8_t),
2247 if (event_list
== NULL
) {
2252 error
= copyin(fse_clone_args
->event_list
,
2254 fse_clone_args
->num_events
* sizeof(int8_t));
2256 FREE(event_list
, M_TEMP
);
2261 error
= add_watcher(event_list
,
2262 fse_clone_args
->num_events
,
2263 fse_clone_args
->event_queue_depth
,
2267 FREE(event_list
, M_TEMP
);
2272 fseh
->watcher
->fseh
= fseh
;
2274 error
= falloc(p
, &f
, &fd
, vfs_context_current());
2276 remove_watcher(fseh
->watcher
);
2277 FREE(event_list
, M_TEMP
);
2282 f
->f_fglob
->fg_flag
= FREAD
| FWRITE
;
2283 f
->f_fglob
->fg_ops
= &fsevents_fops
;
2284 f
->f_fglob
->fg_data
= (caddr_t
) fseh
;
2286 error
= copyout((void *)&fd
, fse_clone_args
->fd
, sizeof(int32_t));
2291 procfdtbl_releasefd(p
, fd
, NULL
);
2292 fp_drop(p
, fd
, f
, 1);
2306 fsevents_wakeup(fs_event_watcher
*watcher
)
2308 selwakeup(&watcher
->fseh
->si
);
2309 KNOTE(&watcher
->fseh
->knotes
, NOTE_WRITE
|NOTE_NONE
);
2310 wakeup((caddr_t
)watcher
);
2315 * A struct describing which functions will get invoked for certain
2318 static struct cdevsw fsevents_cdevsw
=
2320 fseventsopen
, /* open */
2321 fseventsclose
, /* close */
2322 fseventsread
, /* read */
2323 fseventswrite
, /* write */
2324 fseventsioctl
, /* ioctl */
2325 (stop_fcn_t
*)&nulldev
, /* stop */
2326 (reset_fcn_t
*)&nulldev
, /* reset */
2328 eno_select
, /* select */
2329 eno_mmap
, /* mmap */
2330 eno_strat
, /* strategy */
2331 eno_getc
, /* getc */
2332 eno_putc
, /* putc */
2338 * Called to initialize our device,
2339 * and to register ourselves with devfs
2347 if (fsevents_installed
) {
2351 fsevents_installed
= 1;
2353 ret
= cdevsw_add(-1, &fsevents_cdevsw
);
2355 fsevents_installed
= 0;
2359 devfs_make_node(makedev (ret
, 0), DEVFS_CHAR
,
2360 UID_ROOT
, GID_WHEEL
, 0644, "fsevents", 0);
2362 fsevents_internal_init();
2371 MALLOC_ZONE(path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2376 release_pathbuff(char *path
)
2382 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
2386 get_fse_info(struct vnode
*vp
, fse_info
*fse
, __unused vfs_context_t ctx
)
2388 struct vnode_attr va
;
2391 VATTR_WANTED(&va
, va_fsid
);
2392 VATTR_WANTED(&va
, va_fileid
);
2393 VATTR_WANTED(&va
, va_mode
);
2394 VATTR_WANTED(&va
, va_uid
);
2395 VATTR_WANTED(&va
, va_gid
);
2396 if (vp
->v_flag
& VISHARDLINK
) {
2397 if (vp
->v_type
== VDIR
) {
2398 VATTR_WANTED(&va
, va_dirlinkcount
);
2400 VATTR_WANTED(&va
, va_nlink
);
2404 if (vnode_getattr(vp
, &va
, vfs_context_kernel()) != 0) {
2405 memset(fse
, 0, sizeof(fse_info
));
2409 return vnode_get_fse_info_from_vap(vp
, fse
, &va
);
2413 vnode_get_fse_info_from_vap(vnode_t vp
, fse_info
*fse
, struct vnode_attr
*vap
)
2415 fse
->ino
= (ino64_t
)vap
->va_fileid
;
2416 fse
->dev
= (dev_t
)vap
->va_fsid
;
2417 fse
->mode
= (int32_t)vnode_vttoif(vnode_vtype(vp
)) | vap
->va_mode
;
2418 fse
->uid
= (uid_t
)vap
->va_uid
;
2419 fse
->gid
= (gid_t
)vap
->va_gid
;
2420 if (vp
->v_flag
& VISHARDLINK
) {
2421 fse
->mode
|= FSE_MODE_HLINK
;
2422 if (vp
->v_type
== VDIR
) {
2423 fse
->nlink
= (uint64_t)vap
->va_dirlinkcount
;
2425 fse
->nlink
= (uint64_t)vap
->va_nlink
;
2433 create_fsevent_from_kevent(vnode_t vp
, uint32_t kevents
, struct vnode_attr
*vap
)
2435 int fsevent_type
=FSE_CONTENT_MODIFIED
, len
; // the default is the most pessimistic
2436 char pathbuf
[MAXPATHLEN
];
2440 if (kevents
& VNODE_EVENT_DELETE
) {
2441 fsevent_type
= FSE_DELETE
;
2442 } else if (kevents
& (VNODE_EVENT_EXTEND
|VNODE_EVENT_WRITE
)) {
2443 fsevent_type
= FSE_CONTENT_MODIFIED
;
2444 } else if (kevents
& VNODE_EVENT_LINK
) {
2445 fsevent_type
= FSE_CREATE_FILE
;
2446 } else if (kevents
& VNODE_EVENT_RENAME
) {
2447 fsevent_type
= FSE_CREATE_FILE
; // XXXdbg - should use FSE_RENAME but we don't have the destination info;
2448 } else if (kevents
& (VNODE_EVENT_FILE_CREATED
|VNODE_EVENT_FILE_REMOVED
|VNODE_EVENT_DIR_CREATED
|VNODE_EVENT_DIR_REMOVED
)) {
2449 fsevent_type
= FSE_STAT_CHANGED
; // XXXdbg - because vp is a dir and the thing created/removed lived inside it
2450 } else { // a catch all for VNODE_EVENT_PERMS, VNODE_EVENT_ATTRIB and anything else
2451 fsevent_type
= FSE_STAT_CHANGED
;
2454 // printf("convert_kevent: kevents 0x%x fsevent type 0x%x (for %s)\n", kevents, fsevent_type, vp->v_name ? vp->v_name : "(no-name)");
2456 fse
.dev
= vap
->va_fsid
;
2457 fse
.ino
= vap
->va_fileid
;
2458 fse
.mode
= vnode_vttoif(vnode_vtype(vp
)) | (uint32_t)vap
->va_mode
;
2459 if (vp
->v_flag
& VISHARDLINK
) {
2460 fse
.mode
|= FSE_MODE_HLINK
;
2461 if (vp
->v_type
== VDIR
) {
2462 fse
.nlink
= vap
->va_dirlinkcount
;
2464 fse
.nlink
= vap
->va_nlink
;
2468 if (vp
->v_type
== VDIR
) {
2469 fse
.mode
|= FSE_REMOTE_DIR_EVENT
;
2473 fse
.uid
= vap
->va_uid
;
2474 fse
.gid
= vap
->va_gid
;
2476 len
= sizeof(pathbuf
);
2477 if (vn_getpath(vp
, pathbuf
, &len
) == 0) {
2478 add_fsevent(fsevent_type
, vfs_context_current(), FSE_ARG_STRING
, len
, pathbuf
, FSE_ARG_FINFO
, &fse
, FSE_ARG_DONE
);
2483 #else /* CONFIG_FSE */
2485 * The get_pathbuff and release_pathbuff routines are used in places not
2486 * related to fsevents, and it's a handy abstraction, so define trivial
2487 * versions that don't cache a pool of buffers. This way, we don't have
2488 * to conditionalize the callers, and they still get the advantage of the
2489 * pool of buffers if CONFIG_FSE is turned on.
2495 MALLOC_ZONE(path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2500 release_pathbuff(char *path
)
2502 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
2504 #endif /* CONFIG_FSE */