]> git.saurik.com Git - apple/xnu.git/blame - bsd/vfs/vfs_fsevents.c
xnu-1486.2.11.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_fsevents.c
CommitLineData
91447636 1/*
b0d623f7 2 * Copyright (c) 2004-2008 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
91447636 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
91447636
A
27 */
28#include <stdarg.h>
29#include <sys/param.h>
30#include <sys/systm.h>
b0d623f7 31#include <sys/event.h> // for kqueue related stuff
2d21ac55
A
32#include <sys/fsevents.h>
33
34#if CONFIG_FSE
91447636
A
35#include <sys/namei.h>
36#include <sys/filedesc.h>
37#include <sys/kernel.h>
38#include <sys/file_internal.h>
39#include <sys/stat.h>
40#include <sys/vnode_internal.h>
41#include <sys/mount_internal.h>
42#include <sys/proc_internal.h>
43#include <sys/kauth.h>
44#include <sys/uio.h>
45#include <sys/malloc.h>
46#include <sys/dirent.h>
47#include <sys/attr.h>
48#include <sys/sysctl.h>
49#include <sys/ubc.h>
50#include <machine/cons.h>
51#include <miscfs/specfs/specdev.h>
52#include <miscfs/devfs/devfs.h>
53#include <sys/filio.h>
91447636
A
54#include <kern/locks.h>
55#include <libkern/OSAtomic.h>
2d21ac55
A
56#include <kern/zalloc.h>
57#include <mach/mach_time.h>
58#include <kern/thread_call.h>
59#include <kern/clock.h>
91447636 60
b0d623f7 61#include <security/audit/audit.h>
91447636
A
62#include <bsm/audit_kevents.h>
63
91447636
A
64
65
91447636 66typedef struct kfs_event {
2d21ac55
A
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
73
74 uint64_t abstime; // when this event happened (mach_absolute_time())
75 ino64_t ino;
76 dev_t dev;
77 int32_t mode;
78 uid_t uid;
79 gid_t gid;
80
81 const char *str;
82
83 struct kfs_event *dest; // if this is a two-file op
91447636
A
84} kfs_event;
85
2d21ac55
A
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
91
92LIST_HEAD(kfse_list, kfs_event) kfse_list_head = LIST_HEAD_INITIALIZER(x);
93int num_events_outstanding = 0;
94int num_pending_rename = 0;
95
96
97struct fsevent_handle;
91447636
A
98
99typedef struct fs_event_watcher {
91447636
A
100 int8_t *event_list; // the events we're interested in
101 int32_t num_events;
b0d623f7 102 dev_t *devices_not_to_watch; // report events from devices not in this list
91447636
A
103 uint32_t num_devices;
104 int32_t flags;
105 kfs_event **event_queue;
106 int32_t eventq_size; // number of event pointers in queue
0c530ab8 107 int32_t num_readers;
2d21ac55
A
108 int32_t rd; // read index into the event_queue
109 int32_t wr; // write index into the event_queue
110 int32_t blockers;
111 int32_t my_id;
112 uint32_t num_dropped;
b0d623f7 113 uint64_t max_event_id;
2d21ac55 114 struct fsevent_handle *fseh;
91447636
A
115} fs_event_watcher;
116
117// fs_event_watcher flags
2d21ac55
A
118#define WATCHER_DROPPED_EVENTS 0x0001
119#define WATCHER_CLOSING 0x0002
120#define WATCHER_WANTS_COMPACT_EVENTS 0x0004
121#define WATCHER_WANTS_EXTENDED_INFO 0x0008
122
91447636 123
2d21ac55
A
124#define MAX_WATCHERS 8
125static fs_event_watcher *watcher_table[MAX_WATCHERS];
91447636
A
126
127
2d21ac55 128#define MAX_KFS_EVENTS 4096
91447636 129
2d21ac55
A
130// we allocate kfs_event structures out of this zone
131static zone_t event_zone;
91447636
A
132static int fs_event_init = 0;
133
134//
135// this array records whether anyone is interested in a
136// particular type of event. if no one is, we bail out
137// early from the event delivery
138//
139static int16_t fs_event_type_watchers[FSE_MAX_EVENTS];
140
141static int watcher_add_event(fs_event_watcher *watcher, kfs_event *kfse);
2d21ac55 142static void fsevents_wakeup(fs_event_watcher *watcher);
91447636
A
143
144//
145// Locks
146//
147static lck_grp_attr_t * fsevent_group_attr;
148static lck_attr_t * fsevent_lock_attr;
149static lck_grp_t * fsevent_mutex_group;
150
151static lck_grp_t * fsevent_rw_group;
152
2d21ac55
A
153static lck_rw_t event_handling_lock; // handles locking for event manipulation and recycling
154static lck_mtx_t watch_table_lock;
91447636 155static lck_mtx_t event_buf_lock;
2d21ac55 156static lck_mtx_t event_writer_lock;
91447636 157
b0d623f7
A
158
159/* Explicitly declare qsort so compiler doesn't complain */
160__private_extern__ void qsort(
161 void * array,
162 size_t nmembers,
163 size_t member_size,
164 int (*)(const void *, const void *));
91447636
A
165
166
167static void
168fsevents_internal_init(void)
169{
170 int i;
171
172 if (fs_event_init++ != 0) {
173 return;
174 }
175
176 for(i=0; i < FSE_MAX_EVENTS; i++) {
177 fs_event_type_watchers[i] = 0;
178 }
179
2d21ac55 180 memset(watcher_table, 0, sizeof(watcher_table));
91447636
A
181
182 fsevent_lock_attr = lck_attr_alloc_init();
183 fsevent_group_attr = lck_grp_attr_alloc_init();
184 fsevent_mutex_group = lck_grp_alloc_init("fsevent-mutex", fsevent_group_attr);
185 fsevent_rw_group = lck_grp_alloc_init("fsevent-rw", fsevent_group_attr);
186
2d21ac55 187 lck_mtx_init(&watch_table_lock, fsevent_mutex_group, fsevent_lock_attr);
91447636 188 lck_mtx_init(&event_buf_lock, fsevent_mutex_group, fsevent_lock_attr);
2d21ac55
A
189 lck_mtx_init(&event_writer_lock, fsevent_mutex_group, fsevent_lock_attr);
190
191 lck_rw_init(&event_handling_lock, fsevent_rw_group, fsevent_lock_attr);
91447636 192
2d21ac55
A
193 event_zone = zinit(sizeof(kfs_event),
194 MAX_KFS_EVENTS * sizeof(kfs_event),
195 MAX_KFS_EVENTS * sizeof(kfs_event),
196 "fs-event-buf");
197 if (event_zone == NULL) {
198 printf("fsevents: failed to initialize the event zone.\n");
199 }
200
201 if (zfill(event_zone, MAX_KFS_EVENTS) != MAX_KFS_EVENTS) {
202 printf("fsevents: failed to pre-fill the event zone.\n");
203 }
204
205 // mark the zone as exhaustible so that it will not
206 // ever grow beyond what we initially filled it with
207 zone_change(event_zone, Z_EXHAUST, TRUE);
208 zone_change(event_zone, Z_COLLECT, FALSE);
91447636
A
209}
210
211static void
2d21ac55 212lock_watch_table(void)
91447636 213{
2d21ac55 214 lck_mtx_lock(&watch_table_lock);
91447636
A
215}
216
217static void
2d21ac55 218unlock_watch_table(void)
91447636 219{
2d21ac55 220 lck_mtx_unlock(&watch_table_lock);
91447636
A
221}
222
223static void
2d21ac55 224lock_fs_event_list(void)
91447636
A
225{
226 lck_mtx_lock(&event_buf_lock);
227}
228
229static void
2d21ac55 230unlock_fs_event_list(void)
91447636
A
231{
232 lck_mtx_unlock(&event_buf_lock);
233}
234
235// forward prototype
2d21ac55 236static void release_event_ref(kfs_event *kfse);
91447636
A
237
238static int
239watcher_cares_about_dev(fs_event_watcher *watcher, dev_t dev)
240{
241 unsigned int i;
242
b0d623f7
A
243 // if devices_not_to_watch is NULL then we care about all
244 // events from all devices
245 if (watcher->devices_not_to_watch == NULL) {
91447636
A
246 return 1;
247 }
248
249 for(i=0; i < watcher->num_devices; i++) {
b0d623f7
A
250 if (dev == watcher->devices_not_to_watch[i]) {
251 // found a match! that means we do not
252 // want events from this device.
253 return 0;
91447636
A
254 }
255 }
256
b0d623f7
A
257 // if we're here it's not in the devices_not_to_watch[]
258 // list so that means we do care about it
259 return 1;
91447636
A
260}
261
262
263int
264need_fsevent(int type, vnode_t vp)
265{
2d21ac55
A
266 if (type >= 0 && type < FSE_MAX_EVENTS && fs_event_type_watchers[type] == 0)
267 return (0);
268
269 // events in /dev aren't really interesting...
270 if (vp->v_tag == VT_DEVFS) {
271 return (0);
272 }
273
274 return 1;
275}
276
277static int
278prefix_match_len(const char *str1, const char *str2)
279{
280 int len=0;
281
282 while(*str1 && *str2 && *str1 == *str2) {
283 len++;
284 str1++;
285 str2++;
286 }
287
288 if (*str1 == '\0' && *str2 == '\0') {
289 len++;
290 }
291
292 return len;
293}
294
295
296struct history_item {
297 kfs_event *kfse;
298 kfs_event *oldest_kfse;
299 int counter;
300};
301
302static int
303compare_history_items(const void *_a, const void *_b)
304{
305 const struct history_item *a = (const struct history_item *)_a;
306 const struct history_item *b = (const struct history_item *)_b;
307
308 // we want a descending order
309 return (b->counter - a->counter);
310}
311
312#define is_throw_away(x) ((x) == FSE_STAT_CHANGED || (x) == FSE_CONTENT_MODIFIED)
91447636 313
91447636 314
2d21ac55
A
315// Ways that an event can be reused:
316//
317// "combined" events mean that there were two events for
318// the same vnode or path and we're combining both events
319// into a single event. The primary event gets a bit that
320// marks it as having been combined. The secondary event
321// is essentially dropped and the kfse structure reused.
322//
323// "collapsed" means that multiple events below a given
324// directory are collapsed into a single event. in this
325// case, the directory that we collapse into and all of
326// its children must be re-scanned.
327//
328// "recycled" means that we're completely blowing away
329// the event since there are other events that have info
330// about the same vnode or path (and one of those other
331// events will be marked as combined or collapsed as
332// appropriate).
333//
334#define KFSE_COMBINED 0x0001
335#define KFSE_COLLAPSED 0x0002
336#define KFSE_RECYCLED 0x0004
337
338int num_dropped = 0;
339int num_combined_events = 0;
340int num_added_to_parent = 0;
341int num_parent_switch = 0;
342int num_recycled_rename = 0;
343
344//
345// NOTE: you must call lock_fs_event_list() before calling
346// this function.
347//
348static kfs_event *
349find_an_event(const char *str, int len, kfs_event *do_not_reuse, int *reuse_type, int *longest_match_len)
350{
351 kfs_event *kfse, *best_kfse=NULL;
352
353// this seems to be enough to find most duplicate events for the same vnode
354#define MAX_HISTORY 12
355 struct history_item history[MAX_HISTORY];
356 int i;
357
358 *longest_match_len = 0;
359 *reuse_type = 0;
91447636 360
2d21ac55
A
361 memset(history, 0, sizeof(history));
362
363 //
364 // now walk the list of events and try to find the best match
365 // for this event. if we have a vnode, we look for an event
366 // that already references the vnode. if we don't find one
367 // we'll also take the parent of this vnode (in which case it
368 // will be marked as having dropped events within it).
369 //
370 // if we have a string we look for the longest match on the
371 // path we have.
372 //
373
374 LIST_FOREACH(kfse, &kfse_list_head, kevent_list) {
375 int match_len;
376
377 //
378 // don't look at events that are still in the process of being
379 // created, have a null vnode ptr or rename/exchange events.
380 //
381 if ( (kfse->flags & KFSE_BEING_CREATED) || kfse->type == FSE_RENAME || kfse->type == FSE_EXCHANGE) {
382
383 continue;
384 }
385
386 if (str != NULL) {
387 if (kfse->len != 0 && kfse->str != NULL) {
388 match_len = prefix_match_len(str, kfse->str);
389 if (match_len > *longest_match_len) {
390 best_kfse = kfse;
391 *longest_match_len = match_len;
91447636 392 }
2d21ac55
A
393 }
394 }
395
396 if (kfse == do_not_reuse) {
397 continue;
398 }
399
400 for(i=0; i < MAX_HISTORY; i++) {
401 if (history[i].kfse == NULL) {
402 break;
403 }
404
405 //
406 // do a quick check to see if we've got two simple events
407 // that we can cheaply combine. if the event we're looking
408 // at and one of the events in the history table are for the
409 // same path then we'll just mark the newer event as combined
410 // and recyle the older event.
411 //
412 if (history[i].kfse->str == kfse->str) {
413
414 OSBitOrAtomic16(KFSE_COMBINED_EVENTS, &kfse->flags);
415 *reuse_type = KFSE_RECYCLED;
416 history[i].kfse->flags |= KFSE_RECYCLED_EVENT;
417 return history[i].kfse;
418 }
419 }
420
421 if (i < MAX_HISTORY && history[i].kfse == NULL) {
422 history[i].kfse = kfse;
423 history[i].counter = 1;
424 } else if (i >= MAX_HISTORY) {
425 qsort(history, MAX_HISTORY, sizeof(struct history_item), compare_history_items);
426
427 // pluck off the lowest guy if he's only got a count of 1
428 if (history[MAX_HISTORY-1].counter == 1) {
429 history[MAX_HISTORY-1].kfse = kfse;
430 }
91447636 431 }
2d21ac55
A
432 }
433
91447636 434
2d21ac55
A
435 if (str != NULL && best_kfse) {
436 if (*longest_match_len <= 1) {
437 // if the best match we had was "/" then basically we're toast...
438 *longest_match_len = 0;
439 best_kfse = NULL;
440 } else if (*longest_match_len != len) {
441 OSBitOrAtomic16(KFSE_CONTAINS_DROPPED_EVENTS, &best_kfse->flags);
442 *reuse_type = KFSE_COLLAPSED;
443 } else {
444 OSBitOrAtomic16(KFSE_COMBINED_EVENTS, &best_kfse->flags);
445 *reuse_type = KFSE_COMBINED;
446 }
447 }
448
449 return best_kfse;
91447636
A
450}
451
452
2d21ac55
A
453static struct timeval last_print;
454
455//
456// These variables are used to track coalescing multiple identical
457// events for the same vnode/pathname. If we get the same event
458// type and same vnode/pathname as the previous event, we just drop
459// the event since it's superfluous. This improves some micro-
460// benchmarks considerably and actually has a real-world impact on
461// tests like a Finder copy where multiple stat-changed events can
462// get coalesced.
463//
464static int last_event_type=-1;
465static void *last_ptr=NULL;
466static char last_str[MAXPATHLEN];
467static int last_nlen=0;
468static int last_vid=-1;
469static uint64_t last_coalesced_time=0;
b0d623f7 470static void *last_event_ptr=NULL;
2d21ac55
A
471int last_coalesced = 0;
472static mach_timebase_info_data_t sTimebaseInfo = { 0, 0 };
473
474
91447636
A
475int
476add_fsevent(int type, vfs_context_t ctx, ...)
477{
478 struct proc *p = vfs_context_proc(ctx);
2d21ac55
A
479 int i, arg_type, skip_init=0, longest_match_len, ret;
480 kfs_event *kfse, *kfse_dest=NULL, *cur;
91447636
A
481 fs_event_watcher *watcher;
482 va_list ap;
2d21ac55 483 int error = 0, did_alloc=0, need_event_unlock = 0;
91447636 484 dev_t dev = 0;
2d21ac55
A
485 uint64_t now, elapsed;
486 int reuse_type = 0;
487 char *pathbuff=NULL;
488 int pathbuff_len;
489
91447636 490
b0d623f7 491
91447636
A
492 va_start(ap, ctx);
493
2d21ac55
A
494 // ignore bogus event types..
495 if (type < 0 || type >= FSE_MAX_EVENTS) {
496 return EINVAL;
497 }
498
91447636
A
499 // if no one cares about this type of event, bail out
500 if (fs_event_type_watchers[type] == 0) {
501 va_end(ap);
b0d623f7 502
91447636
A
503 return 0;
504 }
505
2d21ac55 506 now = mach_absolute_time();
91447636
A
507
508 // find a free event and snag it for our use
509 // NOTE: do not do anything that would block until
510 // the lock is dropped.
2d21ac55 511 lock_fs_event_list();
91447636 512
2d21ac55
A
513 //
514 // check if this event is identical to the previous one...
515 // (as long as it's not an event type that can never be the
516 // same as a previous event)
517 //
cf7d32b8 518 if (type != FSE_CREATE_FILE && type != FSE_DELETE && type != FSE_RENAME && type != FSE_EXCHANGE && type != FSE_CHOWN) {
2d21ac55
A
519 void *ptr=NULL;
520 int vid=0, was_str=0, nlen=0;
521
522 for(arg_type=va_arg(ap, int32_t); arg_type != FSE_ARG_DONE; arg_type=va_arg(ap, int32_t)) {
523 switch(arg_type) {
524 case FSE_ARG_VNODE: {
525 ptr = va_arg(ap, void *);
526 vid = vnode_vid((struct vnode *)ptr);
527 last_str[0] = '\0';
528 break;
529 }
530 case FSE_ARG_STRING: {
531 nlen = va_arg(ap, int32_t);
532 ptr = va_arg(ap, void *);
533 was_str = 1;
534 break;
535 }
536 }
537 if (ptr != NULL) {
538 break;
539 }
540 }
541
542 if ( sTimebaseInfo.denom == 0 ) {
543 (void) clock_timebase_info(&sTimebaseInfo);
544 }
545
546 elapsed = (now - last_coalesced_time);
547 if (sTimebaseInfo.denom != sTimebaseInfo.numer) {
548 if (sTimebaseInfo.denom == 1) {
549 elapsed *= sTimebaseInfo.numer;
550 } else {
551 // this could overflow... the worst that will happen is that we'll
552 // send (or not send) an extra event so I'm not going to worry about
553 // doing the math right like dtrace_abs_to_nano() does.
554 elapsed = (elapsed * sTimebaseInfo.numer) / (uint64_t)sTimebaseInfo.denom;
555 }
556 }
557
558 if (type == last_event_type
559 && (elapsed < 1000000000)
560 &&
561 ((vid && vid == last_vid && last_ptr == ptr)
562 ||
563 (last_str[0] && last_nlen == nlen && ptr && strcmp(last_str, ptr) == 0))
564 ) {
565
566 last_coalesced++;
567 unlock_fs_event_list();
568 va_end(ap);
b0d623f7 569
2d21ac55
A
570 return 0;
571 } else {
572 last_ptr = ptr;
573 if (was_str) {
574 strlcpy(last_str, ptr, sizeof(last_str));
575 }
576 last_nlen = nlen;
577 last_vid = vid;
578 last_event_type = type;
579 last_coalesced_time = now;
91447636
A
580 }
581 }
2d21ac55
A
582 va_start(ap, ctx);
583
584
585 kfse = zalloc_noblock(event_zone);
586 if (kfse && (type == FSE_RENAME || type == FSE_EXCHANGE)) {
587 kfse_dest = zalloc_noblock(event_zone);
588 if (kfse_dest == NULL) {
589 did_alloc = 1;
590 zfree(event_zone, kfse);
591 kfse = NULL;
592 }
593 }
594
595
596 if (kfse == NULL) { // yikes! no free events
597 int len=0;
598 char *str;
599
600 //
601 // Figure out what kind of reference we have to the
602 // file in this event. This helps us find an event
603 // to combine/collapse into to make room.
604 //
605 // If we have a rename or exchange event then we
606 // don't want to go through the normal path, we
607 // want to "steal" an event instead (which is what
608 // find_an_event() will do if str is null).
609 //
610 arg_type = va_arg(ap, int32_t);
611 if (type == FSE_RENAME || type == FSE_EXCHANGE) {
612 str = NULL;
613 } else if (arg_type == FSE_ARG_STRING) {
614 len = va_arg(ap, int32_t);
615 str = va_arg(ap, char *);
616 } else if (arg_type == FSE_ARG_VNODE) {
617 struct vnode *vp;
618
619 vp = va_arg(ap, struct vnode *);
620 pathbuff = get_pathbuff();
621 pathbuff_len = MAXPATHLEN;
622 if (vn_getpath(vp, pathbuff, &pathbuff_len) != 0 || pathbuff[0] == '\0') {
623 release_pathbuff(pathbuff);
624 pathbuff = NULL;
625 }
626 str = pathbuff;
627 } else {
628 str = NULL;
629 }
630
631 //
632 // This will go through all events and find one that we
633 // can combine with (hopefully), or "collapse" into (i.e
634 // it has the same parent) or in the worst case we have
635 // to "recycle" an event which means that it will combine
636 // two other events and return us the now unused event.
637 // failing all that, find_an_event() could still return
638 // null and if it does then we have a catastrophic dropped
639 // events scenario.
640 //
641 kfse = find_an_event(str, len, NULL, &reuse_type, &longest_match_len);
642
643 if (kfse == NULL) {
644 bail_early:
645
646 unlock_fs_event_list();
647 lock_watch_table();
648
649 for(i=0; i < MAX_WATCHERS; i++) {
650 watcher = watcher_table[i];
651 if (watcher == NULL) {
652 continue;
653 }
654
655 watcher->flags |= WATCHER_DROPPED_EVENTS;
656 fsevents_wakeup(watcher);
657 }
658 unlock_watch_table();
659
660 {
661 struct timeval current_tv;
662
663 num_dropped++;
664
665 // only print a message at most once every 5 seconds
666 microuptime(&current_tv);
667 if ((current_tv.tv_sec - last_print.tv_sec) > 10) {
668 int ii;
669 void *junkptr=zalloc_noblock(event_zone), *listhead=kfse_list_head.lh_first;
670
671 printf("add_fsevent: event queue is full! dropping events (num dropped events: %d; num events outstanding: %d).\n", num_dropped, num_events_outstanding);
672 printf("add_fsevent: kfse_list head %p ; num_pending_rename %d\n", listhead, num_pending_rename);
673 printf("add_fsevent: zalloc sez: %p\n", junkptr);
b0d623f7 674 printf("add_fsevent: event_zone info: %d 0x%x\n", ((int *)event_zone)[0], ((int *)event_zone)[1]);
2d21ac55
A
675 for(ii=0; ii < MAX_WATCHERS; ii++) {
676 if (watcher_table[ii] == NULL) {
677 continue;
678 }
679
680 printf("add_fsevent: watcher %p: num dropped %d rd %4d wr %4d q_size %4d flags 0x%x\n",
681 watcher_table[ii], watcher_table[ii]->num_dropped,
682 watcher_table[ii]->rd, watcher_table[ii]->wr,
683 watcher_table[ii]->eventq_size, watcher_table[ii]->flags);
684 }
685
686 last_print = current_tv;
687 if (junkptr) {
688 zfree(event_zone, junkptr);
689 }
690 }
691 }
692
693 if (pathbuff) {
694 release_pathbuff(pathbuff);
695 pathbuff = NULL;
696 }
697
698 return ENOSPC;
699 }
700
701 if ((type == FSE_RENAME || type == FSE_EXCHANGE) && reuse_type != KFSE_RECYCLED) {
702 panic("add_fsevent: type == %d but reuse type == %d!\n", type, reuse_type);
703 } else if ((kfse->type == FSE_RENAME || kfse->type == FSE_EXCHANGE) && kfse->dest == NULL) {
704 panic("add_fsevent: bogus kfse %p (type %d, but dest is NULL)\n", kfse, kfse->type);
705 } else if (kfse->type == FSE_RENAME || kfse->type == FSE_EXCHANGE) {
706 panic("add_fsevent: we should never re-use rename events (kfse %p reuse type %d)!\n", kfse, reuse_type);
707 }
708
709 if (reuse_type == KFSE_COLLAPSED) {
710 if (str) {
711 const char *tmp_ptr, *new_str;
712
713 //
714 // if we collapsed and have a string we have to chop off the
715 // tail component of the pathname to get the parent.
716 //
717 // NOTE: it is VERY IMPORTANT that we leave the trailing slash
718 // on the pathname. user-level code depends on this.
719 //
720 if (str[0] == '\0' || longest_match_len <= 1) {
721 printf("add_fsevent: strange state (str %s / longest_match_len %d)\n", str, longest_match_len);
722 if (longest_match_len < 0) {
723 panic("add_fsevent: longest_match_len %d\n", longest_match_len);
724 }
725 }
726 // chop off the tail component if it's not the
727 // first character...
728 if (longest_match_len > 1) {
729 str[longest_match_len] = '\0';
730 } else if (longest_match_len == 0) {
731 longest_match_len = 1;
732 }
733
734 new_str = vfs_addname(str, longest_match_len, 0, 0);
735 if (new_str == NULL || new_str[0] == '\0') {
736 panic("add_fsevent: longest match is strange (new_str %p).\n", new_str);
737 }
738
739 lck_rw_lock_exclusive(&event_handling_lock);
740
741 kfse->len = longest_match_len;
742 tmp_ptr = kfse->str;
743 kfse->str = new_str;
744 kfse->ino = 0;
745 kfse->mode = 0;
746 kfse->uid = 0;
747 kfse->gid = 0;
748
749 lck_rw_unlock_exclusive(&event_handling_lock);
750
751 vfs_removename(tmp_ptr);
752 } else {
753 panic("add_fsevent: don't have a vnode or a string pointer (kfse %p)\n", kfse);
754 }
755 }
756
757 if (reuse_type == KFSE_RECYCLED && (type == FSE_RENAME || type == FSE_EXCHANGE)) {
758
759 // if we're recycling this kfse and we have a rename or
760 // exchange event then we need to also get an event for
761 // kfse_dest.
762 //
763 if (did_alloc) {
764 // only happens if we allocated one but then failed
765 // for kfse_dest (and thus free'd the first one we
766 // allocated)
767 kfse_dest = zalloc_noblock(event_zone);
768 if (kfse_dest != NULL) {
769 memset(kfse_dest, 0, sizeof(kfs_event));
770 kfse_dest->refcount = 1;
771 OSBitOrAtomic16(KFSE_BEING_CREATED, &kfse_dest->flags);
772 } else {
773 did_alloc = 0;
774 }
775 }
776
777 if (kfse_dest == NULL) {
778 int dest_reuse_type, dest_match_len;
779
780 kfse_dest = find_an_event(NULL, 0, kfse, &dest_reuse_type, &dest_match_len);
781
782 if (kfse_dest == NULL) {
783 // nothing we can do... gotta bail out
784 goto bail_early;
785 }
786
787 if (dest_reuse_type != KFSE_RECYCLED) {
788 panic("add_fsevent: type == %d but dest_reuse type == %d!\n", type, dest_reuse_type);
789 }
790 }
791 }
792
793
794 //
795 // Here we check for some fast-path cases so that we can
796 // jump over the normal initialization and just get on
797 // with delivering the event. These cases are when we're
798 // combining/collapsing an event and so basically there is
799 // no more work to do (aside from a little book-keeping)
800 //
801 if (str && kfse->len != 0) {
802 kfse->abstime = now;
b0d623f7 803 OSAddAtomic(1, &kfse->refcount);
2d21ac55
A
804 skip_init = 1;
805
806 if (reuse_type == KFSE_COMBINED) {
807 num_combined_events++;
808 } else if (reuse_type == KFSE_COLLAPSED) {
809 num_added_to_parent++;
810 }
811 } else if (reuse_type != KFSE_RECYCLED) {
812 panic("add_fsevent: I'm so confused! (reuse_type %d str %p kfse->len %d)\n",
813 reuse_type, str, kfse->len);
814 }
91447636 815
91447636
A
816 va_end(ap);
817
2d21ac55
A
818
819 if (skip_init) {
820 if (kfse->refcount < 1) {
821 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__, kfse->refcount);
822 }
823
b0d623f7 824 last_event_ptr = kfse;
2d21ac55
A
825 unlock_fs_event_list();
826 goto normal_delivery;
827
828 } else if (reuse_type == KFSE_RECYCLED || reuse_type == KFSE_COMBINED) {
829
830 //
831 // If we're here we have to clear out the kfs_event(s)
832 // that we were given by find_an_event() and set it
833 // up to be re-filled in by the normal code path.
834 //
835 va_start(ap, ctx);
836
837 need_event_unlock = 1;
838 lck_rw_lock_exclusive(&event_handling_lock);
839
b0d623f7 840 OSAddAtomic(1, &kfse->refcount);
2d21ac55
A
841
842 if (kfse->refcount < 1) {
843 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__, kfse->refcount);
844 }
845
846 if (kfse->len == 0) {
847 panic("%s:%d: no more fref.vp\n", __FILE__, __LINE__);
848 // vnode_rele_ext(kfse->fref.vp, O_EVTONLY, 0);
849 } else {
850 vfs_removename(kfse->str);
851 kfse->len = 0;
852 }
853 kfse->str = NULL;
854
855 if (kfse->kevent_list.le_prev != NULL) {
856 num_events_outstanding--;
857 if (kfse->type == FSE_RENAME) {
858 num_pending_rename--;
859 }
860 LIST_REMOVE(kfse, kevent_list);
861 memset(&kfse->kevent_list, 0, sizeof(kfse->kevent_list));
862 }
863
864 kfse->flags = 0 | KFSE_RECYCLED_EVENT;
865
866 if (kfse_dest) {
b0d623f7 867 OSAddAtomic(1, &kfse_dest->refcount);
2d21ac55
A
868 kfse_dest->flags = 0 | KFSE_RECYCLED_EVENT;
869
870 if (did_alloc == 0) {
871 if (kfse_dest->len == 0) {
872 panic("%s:%d: no more fref.vp\n", __FILE__, __LINE__);
873 // vnode_rele_ext(kfse_dest->fref.vp, O_EVTONLY, 0);
874 } else {
875 vfs_removename(kfse_dest->str);
876 kfse_dest->len = 0;
877 }
878 kfse_dest->str = NULL;
879
880 if (kfse_dest->kevent_list.le_prev != NULL) {
881 num_events_outstanding--;
882 LIST_REMOVE(kfse_dest, kevent_list);
883 memset(&kfse_dest->kevent_list, 0, sizeof(kfse_dest->kevent_list));
884 }
885
886 if (kfse_dest->dest) {
887 panic("add_fsevent: should never recycle a rename event! kfse %p\n", kfse);
888 }
889 }
890 }
891
892 OSBitOrAtomic16(KFSE_BEING_CREATED, &kfse->flags);
893 if (kfse_dest) {
894 OSBitOrAtomic16(KFSE_BEING_CREATED, &kfse_dest->flags);
895 }
896
897 goto process_normally;
91447636 898 }
2d21ac55 899 }
91447636 900
2d21ac55
A
901 if (reuse_type != 0) {
902 panic("fsevents: we have a reuse_type (%d) but are about to clear out kfse %p\n", reuse_type, kfse);
91447636
A
903 }
904
2d21ac55
A
905 //
906 // we only want to do this for brand new events, not
907 // events which have been recycled.
908 //
909 memset(kfse, 0, sizeof(kfs_event));
910 kfse->refcount = 1;
911 OSBitOrAtomic16(KFSE_BEING_CREATED, &kfse->flags);
91447636 912
2d21ac55 913 process_normally:
b0d623f7 914 last_event_ptr = kfse;
91447636 915 kfse->type = type;
2d21ac55 916 kfse->abstime = now;
91447636 917 kfse->pid = p->p_pid;
2d21ac55
A
918 if (type == FSE_RENAME || type == FSE_EXCHANGE) {
919 if (need_event_unlock == 0) {
920 memset(kfse_dest, 0, sizeof(kfs_event));
921 kfse_dest->refcount = 1;
922 OSBitOrAtomic16(KFSE_BEING_CREATED, &kfse_dest->flags);
923 }
924 kfse_dest->type = type;
925 kfse_dest->pid = p->p_pid;
926 kfse_dest->abstime = now;
927
928 kfse->dest = kfse_dest;
929 }
930
931 num_events_outstanding++;
932 if (kfse->type == FSE_RENAME) {
933 num_pending_rename++;
934 }
935 LIST_INSERT_HEAD(&kfse_list_head, kfse, kevent_list);
91447636 936
2d21ac55
A
937 if (kfse->refcount < 1) {
938 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__, kfse->refcount);
939 }
940
941 unlock_fs_event_list(); // at this point it's safe to unlock
91447636
A
942
943 //
944 // now process the arguments passed in and copy them into
945 // the kfse
946 //
2d21ac55
A
947 if (need_event_unlock == 0) {
948 lck_rw_lock_shared(&event_handling_lock);
949 }
950
951 cur = kfse;
952 for(arg_type=va_arg(ap, int32_t); arg_type != FSE_ARG_DONE; arg_type=va_arg(ap, int32_t))
91447636 953
2d21ac55 954 switch(arg_type) {
91447636
A
955 case FSE_ARG_VNODE: {
956 // this expands out into multiple arguments to the client
957 struct vnode *vp;
958 struct vnode_attr va;
959
2d21ac55
A
960 if (kfse->str != NULL) {
961 cur = kfse_dest;
91447636
A
962 }
963
2d21ac55
A
964 vp = va_arg(ap, struct vnode *);
965 if (vp == NULL) {
966 panic("add_fsevent: you can't pass me a NULL vnode ptr (type %d)!\n",
967 cur->type);
91447636 968 }
2d21ac55 969
91447636
A
970 VATTR_INIT(&va);
971 VATTR_WANTED(&va, va_fsid);
972 VATTR_WANTED(&va, va_fileid);
973 VATTR_WANTED(&va, va_mode);
974 VATTR_WANTED(&va, va_uid);
975 VATTR_WANTED(&va, va_gid);
cf7d32b8 976 if ((ret = vnode_getattr(vp, &va, vfs_context_kernel())) != 0) {
2d21ac55
A
977 // printf("add_fsevent: failed to getattr on vp %p (%d)\n", cur->fref.vp, ret);
978 cur->str = NULL;
91447636 979 error = EINVAL;
2d21ac55
A
980 if (need_event_unlock == 0) {
981 // then we only grabbed it shared
982 lck_rw_unlock_shared(&event_handling_lock);
983 }
91447636
A
984 goto clean_up;
985 }
986
2d21ac55 987 cur->dev = dev = (dev_t)va.va_fsid;
b0d623f7 988 cur->ino = (ino64_t)va.va_fileid;
2d21ac55
A
989 cur->mode = (int32_t)vnode_vttoif(vnode_vtype(vp)) | va.va_mode;
990 cur->uid = va.va_uid;
991 cur->gid = va.va_gid;
91447636 992
2d21ac55
A
993 // if we haven't gotten the path yet, get it.
994 if (pathbuff == NULL) {
995 pathbuff = get_pathbuff();
996 pathbuff_len = MAXPATHLEN;
997
998 pathbuff[0] = '\0';
b0d623f7
A
999 if ((ret = vn_getpath(vp, pathbuff, &pathbuff_len)) != 0 || pathbuff[0] == '\0') {
1000 struct vnode *orig_vp = vp;
1001
1002 if (ret != ENOSPC) {
1003 printf("add_fsevent: unable to get path for vp %p (%s; ret %d; type %d)\n",
1004 vp, vp->v_name ? vp->v_name : "-UNKNOWN-FILE", ret, type);
1005 }
1006
1007 cur->flags |= KFSE_CONTAINS_DROPPED_EVENTS;
1008
1009 do {
1010 if (vp->v_parent != NULL) {
1011 vp = vp->v_parent;
1012 } else if (vp->v_mount) {
1013 strlcpy(pathbuff, vp->v_mount->mnt_vfsstat.f_mntonname, MAXPATHLEN);
1014 break;
1015 } else {
1016 vp = NULL;
1017 }
1018
1019 if (vp == NULL) {
1020 break;
1021 }
1022
1023 pathbuff_len = MAXPATHLEN;
1024 ret = vn_getpath(vp, pathbuff, &pathbuff_len);
1025 } while (ret == ENOSPC);
1026
1027 if (ret != 0 || vp == NULL) {
1028 printf("add_fsevent: unabled to get a path for vp %p. dropping the event.\n", orig_vp);
1029 error = ENOENT;
1030 if (need_event_unlock == 0) {
1031 // then we only grabbed it shared
1032 lck_rw_unlock_shared(&event_handling_lock);
1033 }
1034 goto clean_up;
2d21ac55 1035 }
2d21ac55
A
1036 }
1037 }
91447636 1038
2d21ac55
A
1039 // store the path by adding it to the global string table
1040 cur->len = pathbuff_len;
1041 cur->str = vfs_addname(pathbuff, pathbuff_len, 0, 0);
1042 if (cur->str == NULL || cur->str[0] == '\0') {
1043 panic("add_fsevent: was not able to add path %s to event %p.\n", pathbuff, cur);
1044 }
1045
1046 release_pathbuff(pathbuff);
1047 pathbuff = NULL;
91447636 1048
91447636
A
1049 break;
1050 }
1051
1052 case FSE_ARG_FINFO: {
1053 fse_info *fse;
1054
1055 fse = va_arg(ap, fse_info *);
1056
2d21ac55 1057 cur->dev = dev = (dev_t)fse->dev;
b0d623f7 1058 cur->ino = (ino64_t)fse->ino;
2d21ac55
A
1059 cur->mode = (int32_t)fse->mode;
1060 cur->uid = (uid_t)fse->uid;
1061 cur->gid = (uid_t)fse->gid;
1062 // if it's a hard-link and this is the last link, flag it
1063 if ((fse->mode & FSE_MODE_HLINK) && fse->nlink == 0) {
1064 cur->mode |= FSE_MODE_LAST_HLINK;
1065 }
b0d623f7
A
1066 if (cur->mode & FSE_TRUNCATED_PATH) {
1067 cur->flags |= KFSE_CONTAINS_DROPPED_EVENTS;
1068 cur->mode &= ~FSE_TRUNCATED_PATH;
1069 }
91447636
A
1070 break;
1071 }
1072
1073 case FSE_ARG_STRING:
2d21ac55
A
1074 if (kfse->str != NULL) {
1075 cur = kfse_dest;
1076 }
91447636 1077
2d21ac55
A
1078 cur->len = (int16_t)(va_arg(ap, int32_t) & 0x7fff);
1079 if (cur->len >= 1) {
1080 cur->str = vfs_addname(va_arg(ap, char *), cur->len, 0, 0);
1081 } else {
1082 printf("add_fsevent: funny looking string length: %d\n", (int)cur->len);
1083 cur->len = 2;
1084 cur->str = vfs_addname("/", cur->len, 0, 0);
1085 }
1086 if (cur->str[0] == 0) {
1087 printf("add_fsevent: bogus looking string (len %d)\n", cur->len);
1088 }
91447636
A
1089 break;
1090
91447636 1091 default:
2d21ac55 1092 printf("add_fsevent: unknown type %d\n", arg_type);
91447636
A
1093 // just skip one 32-bit word and hope we sync up...
1094 (void)va_arg(ap, int32_t);
1095 }
91447636
A
1096
1097 va_end(ap);
1098
2d21ac55
A
1099 OSBitAndAtomic16(~KFSE_BEING_CREATED, &kfse->flags);
1100 if (kfse_dest) {
1101 OSBitAndAtomic16(~KFSE_BEING_CREATED, &kfse_dest->flags);
1102 }
1103
1104 if (need_event_unlock == 0) {
1105 // then we only grabbed it shared
1106 lck_rw_unlock_shared(&event_handling_lock);
1107 }
1108
1109 normal_delivery:
1110 // unlock this here so we don't hold it across the
1111 // event delivery loop.
1112 if (need_event_unlock) {
1113 lck_rw_unlock_exclusive(&event_handling_lock);
1114 need_event_unlock = 0;
1115 }
1116
91447636
A
1117 //
1118 // now we have to go and let everyone know that
2d21ac55 1119 // is interested in this type of event
91447636 1120 //
2d21ac55 1121 lock_watch_table();
91447636 1122
2d21ac55
A
1123 for(i=0; i < MAX_WATCHERS; i++) {
1124 watcher = watcher_table[i];
1125 if (watcher == NULL) {
1126 continue;
1127 }
1128
1129 if ( watcher->event_list[type] == FSE_REPORT
1130 && watcher_cares_about_dev(watcher, dev)) {
1131
1132 if (watcher_add_event(watcher, kfse) != 0) {
1133 watcher->num_dropped++;
91447636
A
1134 }
1135 }
2d21ac55
A
1136
1137 if (kfse->refcount < 1) {
1138 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__, kfse->refcount);
1139 }
91447636
A
1140 }
1141
2d21ac55
A
1142 unlock_watch_table();
1143
91447636 1144 clean_up:
2d21ac55
A
1145 // have to check if this needs to be unlocked (in
1146 // case we came here from an error handling path)
1147 if (need_event_unlock) {
1148 lck_rw_unlock_exclusive(&event_handling_lock);
1149 need_event_unlock = 0;
1150 }
1151
1152 if (pathbuff) {
1153 release_pathbuff(pathbuff);
1154 pathbuff = NULL;
1155 }
1156
1157 release_event_ref(kfse);
91447636 1158
91447636
A
1159 return error;
1160}
1161
2d21ac55 1162
91447636 1163static void
2d21ac55 1164release_event_ref(kfs_event *kfse)
91447636 1165{
2d21ac55
A
1166 int old_refcount;
1167 kfs_event copy, dest_copy;
91447636 1168
91447636 1169
b0d623f7 1170 old_refcount = OSAddAtomic(-1, &kfse->refcount);
2d21ac55
A
1171 if (old_refcount > 1) {
1172 return;
1173 }
1174
1175 lock_fs_event_list();
b0d623f7
A
1176 if (last_event_ptr == kfse) {
1177 last_event_ptr = NULL;
1178 last_event_type = -1;
1179 last_coalesced_time = 0;
1180 }
1181
2d21ac55
A
1182 if (kfse->refcount < 0) {
1183 panic("release_event_ref: bogus kfse refcount %d\n", kfse->refcount);
0c530ab8 1184 }
91447636 1185
2d21ac55
A
1186 if (kfse->refcount > 0 || kfse->type == FSE_INVALID) {
1187 // This is very subtle. Either of these conditions can
1188 // be true if an event got recycled while we were waiting
1189 // on the fs_event_list lock or the event got recycled,
1190 // delivered, _and_ free'd by someone else while we were
1191 // waiting on the fs event list lock. In either case
1192 // we need to just unlock the list and return without
1193 // doing anything because if the refcount is > 0 then
1194 // someone else will take care of free'ing it and when
1195 // the kfse->type is invalid then someone else already
1196 // has handled free'ing the event (while we were blocked
1197 // on the event list lock).
1198 //
1199 unlock_fs_event_list();
1200 return;
1201 }
1202
1203 //
91447636
A
1204 // make a copy of this so we can free things without
1205 // holding the fs_event_buf lock
1206 //
2d21ac55 1207 copy = *kfse;
b0d623f7 1208 if (kfse->dest && OSAddAtomic(-1, &kfse->dest->refcount) == 1) {
2d21ac55
A
1209 dest_copy = *kfse->dest;
1210 } else {
1211 dest_copy.str = NULL;
1212 dest_copy.len = 0;
1213 dest_copy.type = FSE_INVALID;
1214 }
1215
1216 kfse->pid = kfse->type; // save this off for debugging...
b0d623f7
A
1217 kfse->uid = (uid_t)(long)kfse->str; // save this off for debugging...
1218 kfse->gid = (gid_t)(long)current_thread();
2d21ac55
A
1219
1220 kfse->str = (char *)0xdeadbeef; // XXXdbg - catch any cheaters...
1221
1222 if (dest_copy.type != FSE_INVALID) {
1223 kfse->dest->str = (char *)0xbadc0de; // XXXdbg - catch any cheaters...
1224 kfse->dest->type = FSE_INVALID;
1225
1226 if (kfse->dest->kevent_list.le_prev != NULL) {
1227 num_events_outstanding--;
1228 LIST_REMOVE(kfse->dest, kevent_list);
1229 memset(&kfse->dest->kevent_list, 0xa5, sizeof(kfse->dest->kevent_list));
1230 }
1231
1232 zfree(event_zone, kfse->dest);
1233 }
91447636 1234
0c530ab8 1235 // mark this fsevent as invalid
2d21ac55
A
1236 {
1237 int otype;
1238
1239 otype = kfse->type;
0c530ab8
A
1240 kfse->type = FSE_INVALID;
1241
2d21ac55
A
1242 if (kfse->kevent_list.le_prev != NULL) {
1243 num_events_outstanding--;
1244 if (otype == FSE_RENAME) {
1245 num_pending_rename--;
1246 }
1247 LIST_REMOVE(kfse, kevent_list);
1248 memset(&kfse->kevent_list, 0, sizeof(kfse->kevent_list));
1249 }
1250 }
91447636 1251
2d21ac55
A
1252 zfree(event_zone, kfse);
1253
1254 unlock_fs_event_list();
1255
1256 // if we have a pointer in the union
1257 if (copy.str) {
1258 if (copy.len == 0) { // and it's not a string
1259 panic("%s:%d: no more fref.vp!\n", __FILE__, __LINE__);
1260 // vnode_rele_ext(copy.fref.vp, O_EVTONLY, 0);
1261 } else { // else it's a string
1262 vfs_removename(copy.str);
91447636 1263 }
2d21ac55 1264 }
91447636 1265
2d21ac55
A
1266 if (dest_copy.type != FSE_INVALID && dest_copy.str) {
1267 if (dest_copy.len == 0) {
1268 panic("%s:%d: no more fref.vp!\n", __FILE__, __LINE__);
1269 // vnode_rele_ext(dest_copy.fref.vp, O_EVTONLY, 0);
1270 } else {
1271 vfs_removename(dest_copy.str);
91447636
A
1272 }
1273 }
1274}
1275
1276
1277static int
1278add_watcher(int8_t *event_list, int32_t num_events, int32_t eventq_size, fs_event_watcher **watcher_out)
1279{
1280 int i;
1281 fs_event_watcher *watcher;
1282
2d21ac55 1283 if (eventq_size <= 0 || eventq_size > 100*MAX_KFS_EVENTS) {
91447636
A
1284 eventq_size = MAX_KFS_EVENTS;
1285 }
1286
1287 // Note: the event_queue follows the fs_event_watcher struct
1288 // in memory so we only have to do one allocation
1289 MALLOC(watcher,
1290 fs_event_watcher *,
1291 sizeof(fs_event_watcher) + eventq_size * sizeof(kfs_event *),
1292 M_TEMP, M_WAITOK);
2d21ac55
A
1293 if (watcher == NULL) {
1294 return ENOMEM;
1295 }
91447636
A
1296
1297 watcher->event_list = event_list;
1298 watcher->num_events = num_events;
b0d623f7 1299 watcher->devices_not_to_watch = NULL;
91447636
A
1300 watcher->num_devices = 0;
1301 watcher->flags = 0;
1302 watcher->event_queue = (kfs_event **)&watcher[1];
1303 watcher->eventq_size = eventq_size;
1304 watcher->rd = 0;
1305 watcher->wr = 0;
1306 watcher->blockers = 0;
0c530ab8 1307 watcher->num_readers = 0;
b0d623f7 1308 watcher->max_event_id = 0;
2d21ac55
A
1309 watcher->fseh = NULL;
1310
1311 watcher->num_dropped = 0; // XXXdbg - debugging
91447636 1312
2d21ac55 1313 lock_watch_table();
91447636
A
1314
1315 // now update the global list of who's interested in
1316 // events of a particular type...
1317 for(i=0; i < num_events; i++) {
1318 if (event_list[i] != FSE_IGNORE && i < FSE_MAX_EVENTS) {
1319 fs_event_type_watchers[i]++;
1320 }
1321 }
1322
2d21ac55
A
1323 for(i=0; i < MAX_WATCHERS; i++) {
1324 if (watcher_table[i] == NULL) {
1325 watcher->my_id = i;
1326 watcher_table[i] = watcher;
1327 break;
1328 }
1329 }
1330
1331 if (i > MAX_WATCHERS) {
1332 printf("fsevents: too many watchers!\n");
1333 unlock_watch_table();
1334 return ENOSPC;
1335 }
91447636 1336
2d21ac55 1337 unlock_watch_table();
91447636
A
1338
1339 *watcher_out = watcher;
1340
1341 return 0;
1342}
1343
2d21ac55
A
1344
1345
91447636
A
1346static void
1347remove_watcher(fs_event_watcher *target)
1348{
2d21ac55 1349 int i, j, counter=0;
91447636
A
1350 fs_event_watcher *watcher;
1351 kfs_event *kfse;
1352
2d21ac55 1353 lock_watch_table();
91447636 1354
2d21ac55
A
1355 for(j=0; j < MAX_WATCHERS; j++) {
1356 watcher = watcher_table[j];
1357 if (watcher != target) {
1358 continue;
1359 }
91447636 1360
2d21ac55
A
1361 watcher_table[j] = NULL;
1362
1363 for(i=0; i < watcher->num_events; i++) {
1364 if (watcher->event_list[i] != FSE_IGNORE && i < FSE_MAX_EVENTS) {
1365 fs_event_type_watchers[i]--;
91447636 1366 }
2d21ac55 1367 }
91447636 1368
2d21ac55
A
1369 if (watcher->flags & WATCHER_CLOSING) {
1370 unlock_watch_table();
1371 return;
1372 }
1373
1374 // printf("fsevents: removing watcher %p (rd %d wr %d num_readers %d flags 0x%x)\n", watcher, watcher->rd, watcher->wr, watcher->num_readers, watcher->flags);
1375 watcher->flags |= WATCHER_CLOSING;
b0d623f7 1376 OSAddAtomic(1, &watcher->num_readers);
2d21ac55
A
1377
1378 unlock_watch_table();
91447636 1379
2d21ac55
A
1380 while (watcher->num_readers > 1 && counter++ < 5000) {
1381 fsevents_wakeup(watcher); // in case they're asleep
1382
1383 tsleep(watcher, PRIBIO, "fsevents-close", 1);
1384 }
1385 if (counter++ >= 5000) {
1386 // printf("fsevents: close: still have readers! (%d)\n", watcher->num_readers);
1387 panic("fsevents: close: still have readers! (%d)\n", watcher->num_readers);
1388 }
1389
1390 // drain the event_queue
1391 while(watcher->rd != watcher->wr) {
1392 lck_rw_lock_shared(&event_handling_lock);
1393
1394 kfse = watcher->event_queue[watcher->rd];
1395 if (kfse->type == FSE_INVALID || kfse->refcount < 1) {
1396 panic("remove_watcher: bogus kfse %p during cleanup (type %d refcount %d rd %d wr %d)\n", kfse, kfse->type, kfse->refcount, watcher->rd, watcher->wr);
1397 }
1398
1399 lck_rw_unlock_shared(&event_handling_lock);
1400
1401 watcher->rd = (watcher->rd+1) % watcher->eventq_size;
1402
1403 if (kfse != NULL) {
1404 release_event_ref(kfse);
91447636 1405 }
2d21ac55 1406 }
91447636 1407
2d21ac55
A
1408 if (watcher->event_list) {
1409 FREE(watcher->event_list, M_TEMP);
1410 watcher->event_list = NULL;
1411 }
b0d623f7
A
1412 if (watcher->devices_not_to_watch) {
1413 FREE(watcher->devices_not_to_watch, M_TEMP);
1414 watcher->devices_not_to_watch = NULL;
2d21ac55
A
1415 }
1416 FREE(watcher, M_TEMP);
1417
1418 return;
1419 }
1420
1421 unlock_watch_table();
1422}
1423
1424
1425#define EVENT_DELAY_IN_MS 10
1426static thread_call_t event_delivery_timer = NULL;
1427static int timer_set = 0;
1428
1429
1430static void
1431delayed_event_delivery(__unused void *param0, __unused void *param1)
1432{
1433 int i;
1434
1435 lock_watch_table();
1436
1437 for(i=0; i < MAX_WATCHERS; i++) {
1438 if (watcher_table[i] != NULL && watcher_table[i]->rd != watcher_table[i]->wr) {
1439 fsevents_wakeup(watcher_table[i]);
1440 }
1441 }
1442
1443 timer_set = 0;
1444
1445 unlock_watch_table();
1446}
1447
1448
1449//
1450// The watch table must be locked before calling this function.
1451//
1452static void
1453schedule_event_wakeup(void)
1454{
1455 uint64_t deadline;
1456
1457 if (event_delivery_timer == NULL) {
1458 event_delivery_timer = thread_call_allocate((thread_call_func_t)delayed_event_delivery, NULL);
1459 }
1460
1461 clock_interval_to_deadline(EVENT_DELAY_IN_MS, 1000 * 1000, &deadline);
1462
1463 thread_call_enter_delayed(event_delivery_timer, deadline);
1464 timer_set = 1;
1465}
1466
1467
1468
1469#define MAX_NUM_PENDING 16
1470
1471//
1472// NOTE: the watch table must be locked before calling
1473// this routine.
1474//
1475static int
1476watcher_add_event(fs_event_watcher *watcher, kfs_event *kfse)
1477{
b0d623f7
A
1478 if (kfse->abstime > watcher->max_event_id) {
1479 watcher->max_event_id = kfse->abstime;
1480 }
1481
2d21ac55
A
1482 if (((watcher->wr + 1) % watcher->eventq_size) == watcher->rd) {
1483 watcher->flags |= WATCHER_DROPPED_EVENTS;
1484 fsevents_wakeup(watcher);
1485 return ENOSPC;
1486 }
1487
b0d623f7 1488 OSAddAtomic(1, &kfse->refcount);
2d21ac55
A
1489 watcher->event_queue[watcher->wr] = kfse;
1490 OSSynchronizeIO();
1491 watcher->wr = (watcher->wr + 1) % watcher->eventq_size;
1492
1493 //
1494 // wake up the watcher if there are more than MAX_NUM_PENDING events.
1495 // otherwise schedule a timer (if one isn't already set) which will
1496 // send any pending events if no more are received in the next
1497 // EVENT_DELAY_IN_MS milli-seconds.
1498 //
1499 if ( (watcher->rd < watcher->wr && (watcher->wr - watcher->rd) > MAX_NUM_PENDING)
1500 || (watcher->rd > watcher->wr && (watcher->wr + watcher->eventq_size - watcher->rd) > MAX_NUM_PENDING)) {
1501
1502 fsevents_wakeup(watcher);
1503
1504 } else if (timer_set == 0) {
1505
1506 schedule_event_wakeup();
1507 }
1508
1509 return 0;
1510}
1511
2d21ac55
A
1512static int
1513fill_buff(uint16_t type, int32_t size, const void *data,
1514 char *buff, int32_t *_buff_idx, int32_t buff_sz,
1515 struct uio *uio)
1516{
1517 int32_t amt, error = 0, buff_idx = *_buff_idx;
1518 uint16_t tmp;
1519
1520 //
1521 // the +1 on the size is to guarantee that the main data
1522 // copy loop will always copy at least 1 byte
1523 //
1524 if ((buff_sz - buff_idx) <= (int)(2*sizeof(uint16_t) + 1)) {
1525 if (buff_idx > uio_resid(uio)) {
1526 error = ENOSPC;
1527 goto get_out;
1528 }
1529
1530 error = uiomove(buff, buff_idx, uio);
1531 if (error) {
1532 goto get_out;
1533 }
1534 buff_idx = 0;
1535 }
1536
1537 // copy out the header (type & size)
1538 memcpy(&buff[buff_idx], &type, sizeof(uint16_t));
1539 buff_idx += sizeof(uint16_t);
1540
1541 tmp = size & 0xffff;
1542 memcpy(&buff[buff_idx], &tmp, sizeof(uint16_t));
1543 buff_idx += sizeof(uint16_t);
1544
1545 // now copy the body of the data, flushing along the way
1546 // if the buffer fills up.
1547 //
1548 while(size > 0) {
1549 amt = (size < (buff_sz - buff_idx)) ? size : (buff_sz - buff_idx);
1550 memcpy(&buff[buff_idx], data, amt);
1551
1552 size -= amt;
1553 buff_idx += amt;
1554 data = (const char *)data + amt;
1555 if (size > (buff_sz - buff_idx)) {
1556 if (buff_idx > uio_resid(uio)) {
1557 error = ENOSPC;
1558 goto get_out;
91447636 1559 }
2d21ac55
A
1560 error = uiomove(buff, buff_idx, uio);
1561 if (error) {
1562 goto get_out;
91447636 1563 }
2d21ac55
A
1564 buff_idx = 0;
1565 }
1566
1567 if (amt == 0) { // just in case...
1568 break;
1569 }
1570 }
1571
1572 get_out:
1573 *_buff_idx = buff_idx;
1574
1575 return error;
1576}
1577
1578
1579static int copy_out_kfse(fs_event_watcher *watcher, kfs_event *kfse, struct uio *uio) __attribute__((noinline));
1580
1581static int
1582copy_out_kfse(fs_event_watcher *watcher, kfs_event *kfse, struct uio *uio)
1583{
1584 int error;
1585 uint16_t tmp16;
1586 int32_t type;
1587 kfs_event *cur;
1588 char evbuff[512];
1589 int evbuff_idx = 0;
1590
1591 if (kfse->type == FSE_INVALID) {
1592 panic("fsevents: copy_out_kfse: asked to copy out an invalid event (kfse %p, refcount %d fref ptr %p)\n", kfse, kfse->refcount, kfse->str);
1593 }
1594
1595 if (kfse->flags & KFSE_BEING_CREATED) {
1596 return 0;
1597 }
1598
1599 if (kfse->type == FSE_RENAME && kfse->dest == NULL) {
1600 //
1601 // This can happen if an event gets recycled but we had a
1602 // pointer to it in our event queue. The event is the
1603 // destination of a rename which we'll process separately
1604 // (that is, another kfse points to this one so it's ok
1605 // to skip this guy because we'll process it when we process
1606 // the other one)
1607 error = 0;
1608 goto get_out;
1609 }
1610
1611 if (watcher->flags & WATCHER_WANTS_EXTENDED_INFO) {
1612
1613 type = (kfse->type & 0xfff);
1614
1615 if (kfse->flags & KFSE_CONTAINS_DROPPED_EVENTS) {
1616 type |= (FSE_CONTAINS_DROPPED_EVENTS << FSE_FLAG_SHIFT);
1617 } else if (kfse->flags & KFSE_COMBINED_EVENTS) {
1618 type |= (FSE_COMBINED_EVENTS << FSE_FLAG_SHIFT);
1619 }
1620
1621 } else {
1622 type = (int32_t)kfse->type;
1623 }
1624
1625 // copy out the type of the event
1626 memcpy(evbuff, &type, sizeof(int32_t));
1627 evbuff_idx += sizeof(int32_t);
1628
1629 // copy out the pid of the person that generated the event
1630 memcpy(&evbuff[evbuff_idx], &kfse->pid, sizeof(pid_t));
1631 evbuff_idx += sizeof(pid_t);
1632
1633 cur = kfse;
1634
1635 copy_again:
1636
1637 if (cur->str == NULL || cur->str[0] == '\0') {
1638 printf("copy_out_kfse:2: empty/short path (%s)\n", cur->str);
1639 error = fill_buff(FSE_ARG_STRING, 2, "/", evbuff, &evbuff_idx, sizeof(evbuff), uio);
1640 } else {
1641 error = fill_buff(FSE_ARG_STRING, cur->len, cur->str, evbuff, &evbuff_idx, sizeof(evbuff), uio);
1642 }
1643 if (error != 0) {
1644 goto get_out;
1645 }
1646
1647 if (cur->dev == 0 && cur->ino == 0) {
1648 // this happens when a rename event happens and the
1649 // destination of the rename did not previously exist.
1650 // it thus has no other file info so skip copying out
1651 // the stuff below since it isn't initialized
1652 goto done;
1653 }
1654
1655
1656 if (watcher->flags & WATCHER_WANTS_COMPACT_EVENTS) {
1657 int32_t finfo_size;
1658
1659 finfo_size = sizeof(dev_t) + sizeof(ino64_t) + sizeof(int32_t) + sizeof(uid_t) + sizeof(gid_t);
1660 error = fill_buff(FSE_ARG_FINFO, finfo_size, &cur->ino, evbuff, &evbuff_idx, sizeof(evbuff), uio);
1661 if (error != 0) {
1662 goto get_out;
1663 }
1664 } else {
1665 ino_t ino;
1666
1667 error = fill_buff(FSE_ARG_DEV, sizeof(dev_t), &cur->dev, evbuff, &evbuff_idx, sizeof(evbuff), uio);
1668 if (error != 0) {
1669 goto get_out;
1670 }
1671
1672 ino = (ino_t)cur->ino;
1673 error = fill_buff(FSE_ARG_INO, sizeof(ino_t), &ino, evbuff, &evbuff_idx, sizeof(evbuff), uio);
1674 if (error != 0) {
1675 goto get_out;
1676 }
1677
1678 error = fill_buff(FSE_ARG_MODE, sizeof(int32_t), &cur->mode, evbuff, &evbuff_idx, sizeof(evbuff), uio);
1679 if (error != 0) {
1680 goto get_out;
1681 }
1682
1683 error = fill_buff(FSE_ARG_UID, sizeof(uid_t), &cur->uid, evbuff, &evbuff_idx, sizeof(evbuff), uio);
1684 if (error != 0) {
1685 goto get_out;
1686 }
91447636 1687
2d21ac55
A
1688 error = fill_buff(FSE_ARG_GID, sizeof(gid_t), &cur->gid, evbuff, &evbuff_idx, sizeof(evbuff), uio);
1689 if (error != 0) {
1690 goto get_out;
91447636
A
1691 }
1692 }
1693
91447636 1694
2d21ac55
A
1695 if (cur->dest) {
1696 cur = cur->dest;
1697 goto copy_again;
1698 }
91447636 1699
2d21ac55
A
1700 done:
1701 // very last thing: the time stamp
1702 error = fill_buff(FSE_ARG_INT64, sizeof(uint64_t), &cur->abstime, evbuff, &evbuff_idx, sizeof(evbuff), uio);
1703 if (error != 0) {
1704 goto get_out;
91447636
A
1705 }
1706
2d21ac55
A
1707 // check if the FSE_ARG_DONE will fit
1708 if (sizeof(uint16_t) > sizeof(evbuff) - evbuff_idx) {
1709 if (evbuff_idx > uio_resid(uio)) {
1710 error = ENOSPC;
1711 goto get_out;
1712 }
1713 error = uiomove(evbuff, evbuff_idx, uio);
1714 if (error) {
1715 goto get_out;
1716 }
1717 evbuff_idx = 0;
1718 }
91447636 1719
2d21ac55
A
1720 tmp16 = FSE_ARG_DONE;
1721 memcpy(&evbuff[evbuff_idx], &tmp16, sizeof(uint16_t));
1722 evbuff_idx += sizeof(uint16_t);
1723
1724 // flush any remaining data in the buffer (and hopefully
1725 // in most cases this is the only uiomove we'll do)
1726 if (evbuff_idx > uio_resid(uio)) {
1727 error = ENOSPC;
1728 } else {
1729 error = uiomove(evbuff, evbuff_idx, uio);
1730 }
1731
1732 get_out:
1733
1734 return error;
91447636
A
1735}
1736
1737
2d21ac55 1738
91447636
A
1739static int
1740fmod_watch(fs_event_watcher *watcher, struct uio *uio)
1741{
b0d623f7
A
1742 int error=0;
1743 user_ssize_t last_full_event_resid;
91447636 1744 kfs_event *kfse;
91447636
A
1745 uint16_t tmp16;
1746
91447636
A
1747 last_full_event_resid = uio_resid(uio);
1748
1749 // need at least 2048 bytes of space (maxpathlen + 1 event buf)
1750 if (uio_resid(uio) < 2048 || watcher == NULL) {
1751 return EINVAL;
1752 }
1753
2d21ac55
A
1754 if (watcher->flags & WATCHER_CLOSING) {
1755 return 0;
1756 }
1757
b0d623f7 1758 if (OSAddAtomic(1, &watcher->num_readers) != 0) {
0c530ab8 1759 // don't allow multiple threads to read from the fd at the same time
b0d623f7 1760 OSAddAtomic(-1, &watcher->num_readers);
0c530ab8
A
1761 return EAGAIN;
1762 }
91447636
A
1763
1764 if (watcher->rd == watcher->wr) {
1765 if (watcher->flags & WATCHER_CLOSING) {
b0d623f7 1766 OSAddAtomic(-1, &watcher->num_readers);
91447636
A
1767 return 0;
1768 }
b0d623f7 1769 OSAddAtomic(1, &watcher->blockers);
91447636
A
1770
1771 // there's nothing to do, go to sleep
1772 error = tsleep((caddr_t)watcher, PUSER|PCATCH, "fsevents_empty", 0);
1773
b0d623f7 1774 OSAddAtomic(-1, &watcher->blockers);
91447636
A
1775
1776 if (error != 0 || (watcher->flags & WATCHER_CLOSING)) {
b0d623f7 1777 OSAddAtomic(-1, &watcher->num_readers);
91447636
A
1778 return error;
1779 }
1780 }
1781
1782 // if we dropped events, return that as an event first
1783 if (watcher->flags & WATCHER_DROPPED_EVENTS) {
1784 int32_t val = FSE_EVENTS_DROPPED;
1785
1786 error = uiomove((caddr_t)&val, sizeof(int32_t), uio);
1787 if (error == 0) {
1788 val = 0; // a fake pid
1789 error = uiomove((caddr_t)&val, sizeof(int32_t), uio);
1790
1791 tmp16 = FSE_ARG_DONE; // makes it a consistent msg
1792 error = uiomove((caddr_t)&tmp16, sizeof(int16_t), uio);
2d21ac55 1793
2d21ac55 1794 last_full_event_resid = uio_resid(uio);
91447636
A
1795 }
1796
1797 if (error) {
b0d623f7 1798 OSAddAtomic(-1, &watcher->num_readers);
91447636
A
1799 return error;
1800 }
1801
1802 watcher->flags &= ~WATCHER_DROPPED_EVENTS;
1803 }
1804
2d21ac55
A
1805 while (uio_resid(uio) > 0 && watcher->rd != watcher->wr) {
1806 if (watcher->flags & WATCHER_CLOSING) {
1807 break;
91447636 1808 }
2d21ac55
A
1809
1810 //
1811 // check if the event is something of interest to us
1812 // (since it may have been recycled/reused and changed
1813 // its type or which device it is for)
1814 //
1815 lck_rw_lock_shared(&event_handling_lock);
91447636 1816
2d21ac55
A
1817 kfse = watcher->event_queue[watcher->rd];
1818 if (kfse->type == FSE_INVALID || kfse->refcount < 1) {
1819 panic("fmod_watch: someone left me a bogus kfse %p (type %d refcount %d rd %d wr %d)\n", kfse, kfse->type, kfse->refcount, watcher->rd, watcher->wr);
91447636
A
1820 }
1821
2d21ac55
A
1822 if (watcher->event_list[kfse->type] == FSE_REPORT && watcher_cares_about_dev(watcher, kfse->dev)) {
1823
1824 error = copy_out_kfse(watcher, kfse, uio);
1825 if (error != 0) {
1826 // if an event won't fit or encountered an error while
1827 // we were copying it out, then backup to the last full
1828 // event and just bail out. if the error was ENOENT
1829 // then we can continue regular processing, otherwise
1830 // we should unlock things and return.
1831 uio_setresid(uio, last_full_event_resid);
1832 if (error != ENOENT) {
1833 lck_rw_unlock_shared(&event_handling_lock);
1834 error = 0;
1835 goto get_out;
1836 }
91447636
A
1837 }
1838
2d21ac55 1839 last_full_event_resid = uio_resid(uio);
91447636
A
1840 }
1841
2d21ac55 1842 lck_rw_unlock_shared(&event_handling_lock);
91447636 1843
91447636 1844 watcher->rd = (watcher->rd + 1) % watcher->eventq_size;
2d21ac55 1845 OSSynchronizeIO();
91447636 1846
2d21ac55
A
1847 if (kfse->type == FSE_INVALID || kfse->refcount < 1) {
1848 panic("fmod_watch:2: my kfse became bogus! kfse %p (type %d refcount %d rd %d wr %d)\n", kfse, kfse->type, kfse->refcount, watcher->rd, watcher->wr);
91447636 1849 }
2d21ac55
A
1850
1851 release_event_ref(kfse);
91447636
A
1852 }
1853
1854 get_out:
b0d623f7 1855 OSAddAtomic(-1, &watcher->num_readers);
2d21ac55 1856
91447636
A
1857 return error;
1858}
1859
1860
1861// release any references we might have on vnodes which are
1862// the mount point passed to us (so that it can be cleanly
1863// unmounted).
1864//
1865// since we don't want to lose the events we'll convert the
2d21ac55 1866// vnode refs to full paths.
91447636
A
1867//
1868void
2d21ac55 1869fsevent_unmount(__unused struct mount *mp)
91447636 1870{
2d21ac55
A
1871 // we no longer maintain pointers to vnodes so
1872 // there is nothing to do...
91447636
A
1873}
1874
1875
1876//
1877// /dev/fsevents device code
1878//
1879static int fsevents_installed = 0;
91447636
A
1880
1881typedef struct fsevent_handle {
0c530ab8
A
1882 UInt32 flags;
1883 SInt32 active;
91447636 1884 fs_event_watcher *watcher;
b0d623f7 1885 struct klist knotes;
91447636
A
1886 struct selinfo si;
1887} fsevent_handle;
1888
0c530ab8 1889#define FSEH_CLOSING 0x0001
91447636
A
1890
1891static int
1892fseventsf_read(struct fileproc *fp, struct uio *uio,
2d21ac55 1893 __unused int flags, __unused vfs_context_t ctx)
91447636
A
1894{
1895 fsevent_handle *fseh = (struct fsevent_handle *)fp->f_fglob->fg_data;
1896 int error;
1897
1898 error = fmod_watch(fseh->watcher, uio);
1899
1900 return error;
1901}
1902
2d21ac55 1903
91447636
A
1904static int
1905fseventsf_write(__unused struct fileproc *fp, __unused struct uio *uio,
2d21ac55 1906 __unused int flags, __unused vfs_context_t ctx)
91447636
A
1907{
1908 return EIO;
1909}
1910
b0d623f7 1911#pragma pack(push, 4)
2d21ac55
A
1912typedef struct ext_fsevent_dev_filter_args {
1913 uint32_t num_devices;
1914 user_addr_t devices;
1915} ext_fsevent_dev_filter_args;
b0d623f7 1916#pragma pack(pop)
2d21ac55
A
1917
1918typedef struct old_fsevent_dev_filter_args {
1919 uint32_t num_devices;
1920 int32_t devices;
1921} old_fsevent_dev_filter_args;
1922
1923#define OLD_FSEVENTS_DEVICE_FILTER _IOW('s', 100, old_fsevent_dev_filter_args)
1924#define NEW_FSEVENTS_DEVICE_FILTER _IOW('s', 100, ext_fsevent_dev_filter_args)
1925
b0d623f7
A
1926#if __LP64__
1927/* need this in spite of the padding due to alignment of devices */
1928typedef struct fsevent_dev_filter_args32 {
1929 uint32_t num_devices;
1930 uint32_t devices;
1931 int32_t pad1;
1932} fsevent_dev_filter_args32;
1933#endif
91447636
A
1934
1935static int
2d21ac55 1936fseventsf_ioctl(struct fileproc *fp, u_long cmd, caddr_t data, vfs_context_t ctx)
91447636
A
1937{
1938 fsevent_handle *fseh = (struct fsevent_handle *)fp->f_fglob->fg_data;
1939 int ret = 0;
2d21ac55
A
1940 ext_fsevent_dev_filter_args *devfilt_args, _devfilt_args;
1941
1942 if (proc_is64bit(vfs_context_proc(ctx))) {
1943 devfilt_args = (ext_fsevent_dev_filter_args *)data;
1944 } else if (cmd == OLD_FSEVENTS_DEVICE_FILTER) {
1945 old_fsevent_dev_filter_args *udev_filt_args = (old_fsevent_dev_filter_args *)data;
1946
1947 devfilt_args = &_devfilt_args;
1948 memset(devfilt_args, 0, sizeof(ext_fsevent_dev_filter_args));
1949
1950 devfilt_args->num_devices = udev_filt_args->num_devices;
1951 devfilt_args->devices = CAST_USER_ADDR_T(udev_filt_args->devices);
1952 } else {
b0d623f7
A
1953#if __LP64__
1954 fsevent_dev_filter_args32 *udev_filt_args = (fsevent_dev_filter_args32 *)data;
1955#else
2d21ac55 1956 fsevent_dev_filter_args *udev_filt_args = (fsevent_dev_filter_args *)data;
b0d623f7 1957#endif
2d21ac55
A
1958
1959 devfilt_args = &_devfilt_args;
1960 memset(devfilt_args, 0, sizeof(ext_fsevent_dev_filter_args));
1961
1962 devfilt_args->num_devices = udev_filt_args->num_devices;
1963 devfilt_args->devices = CAST_USER_ADDR_T(udev_filt_args->devices);
1964 }
91447636 1965
0c530ab8
A
1966 OSAddAtomic(1, &fseh->active);
1967 if (fseh->flags & FSEH_CLOSING) {
1968 OSAddAtomic(-1, &fseh->active);
1969 return 0;
1970 }
1971
91447636
A
1972 switch (cmd) {
1973 case FIONBIO:
1974 case FIOASYNC:
0c530ab8 1975 break;
91447636 1976
2d21ac55
A
1977 case FSEVENTS_WANT_COMPACT_EVENTS: {
1978 fseh->watcher->flags |= WATCHER_WANTS_COMPACT_EVENTS;
1979 break;
1980 }
1981
1982 case FSEVENTS_WANT_EXTENDED_INFO: {
1983 fseh->watcher->flags |= WATCHER_WANTS_EXTENDED_INFO;
1984 break;
1985 }
1986
b0d623f7
A
1987 case FSEVENTS_GET_CURRENT_ID: {
1988 *(uint64_t *)data = fseh->watcher->max_event_id;
1989 ret = 0;
1990 break;
1991 }
1992
2d21ac55
A
1993 case OLD_FSEVENTS_DEVICE_FILTER:
1994 case NEW_FSEVENTS_DEVICE_FILTER: {
91447636 1995 int new_num_devices;
b0d623f7 1996 dev_t *devices_not_to_watch, *tmp=NULL;
91447636
A
1997
1998 if (devfilt_args->num_devices > 256) {
1999 ret = EINVAL;
2000 break;
2001 }
2002
2003 new_num_devices = devfilt_args->num_devices;
2004 if (new_num_devices == 0) {
b0d623f7 2005 tmp = fseh->watcher->devices_not_to_watch;
91447636 2006
2d21ac55 2007 lock_watch_table();
b0d623f7 2008 fseh->watcher->devices_not_to_watch = NULL;
91447636 2009 fseh->watcher->num_devices = new_num_devices;
2d21ac55 2010 unlock_watch_table();
91447636
A
2011
2012 if (tmp) {
2013 FREE(tmp, M_TEMP);
2014 }
2015 break;
2016 }
2017
b0d623f7 2018 MALLOC(devices_not_to_watch, dev_t *,
91447636
A
2019 new_num_devices * sizeof(dev_t),
2020 M_TEMP, M_WAITOK);
b0d623f7 2021 if (devices_not_to_watch == NULL) {
91447636
A
2022 ret = ENOMEM;
2023 break;
2024 }
2025
2d21ac55 2026 ret = copyin(devfilt_args->devices,
b0d623f7 2027 (void *)devices_not_to_watch,
91447636
A
2028 new_num_devices * sizeof(dev_t));
2029 if (ret) {
b0d623f7 2030 FREE(devices_not_to_watch, M_TEMP);
91447636
A
2031 break;
2032 }
2033
2d21ac55 2034 lock_watch_table();
91447636 2035 fseh->watcher->num_devices = new_num_devices;
b0d623f7
A
2036 tmp = fseh->watcher->devices_not_to_watch;
2037 fseh->watcher->devices_not_to_watch = devices_not_to_watch;
2d21ac55 2038 unlock_watch_table();
91447636
A
2039
2040 if (tmp) {
2041 FREE(tmp, M_TEMP);
2042 }
2043
2044 break;
2045 }
2046
2047 default:
2048 ret = EINVAL;
2049 break;
2050 }
2051
0c530ab8 2052 OSAddAtomic(-1, &fseh->active);
91447636
A
2053 return (ret);
2054}
2055
2056
2057static int
2d21ac55 2058fseventsf_select(struct fileproc *fp, int which, __unused void *wql, vfs_context_t ctx)
91447636
A
2059{
2060 fsevent_handle *fseh = (struct fsevent_handle *)fp->f_fglob->fg_data;
2061 int ready = 0;
2062
2063 if ((which != FREAD) || (fseh->watcher->flags & WATCHER_CLOSING)) {
2064 return 0;
2065 }
2066
2067
2068 // if there's nothing in the queue, we're not ready
2d21ac55 2069 if (fseh->watcher->rd != fseh->watcher->wr) {
91447636
A
2070 ready = 1;
2071 }
2072
2073 if (!ready) {
2d21ac55 2074 selrecord(vfs_context_proc(ctx), &fseh->si, wql);
91447636
A
2075 }
2076
2077 return ready;
2078}
2079
2080
2d21ac55 2081#if NOTUSED
91447636 2082static int
2d21ac55 2083fseventsf_stat(__unused struct fileproc *fp, __unused struct stat *sb, __unused vfs_context_t ctx)
91447636
A
2084{
2085 return ENOTSUP;
2086}
2d21ac55 2087#endif
91447636
A
2088
2089static int
2d21ac55 2090fseventsf_close(struct fileglob *fg, __unused vfs_context_t ctx)
91447636
A
2091{
2092 fsevent_handle *fseh = (struct fsevent_handle *)fg->fg_data;
0c530ab8 2093 fs_event_watcher *watcher;
2d21ac55 2094
0c530ab8
A
2095 OSBitOrAtomic(FSEH_CLOSING, &fseh->flags);
2096 while (OSAddAtomic(0, &fseh->active) > 0) {
2097 tsleep((caddr_t)fseh->watcher, PRIBIO, "fsevents-close", 1);
2098 }
91447636 2099
0c530ab8 2100 watcher = fseh->watcher;
0c530ab8 2101 fg->fg_data = NULL;
2d21ac55 2102 fseh->watcher = NULL;
0c530ab8
A
2103
2104 remove_watcher(watcher);
91447636
A
2105 FREE(fseh, M_TEMP);
2106
2107 return 0;
2108}
2109
b0d623f7
A
2110static void
2111filt_fsevent_detach(struct knote *kn)
2112{
2113 fsevent_handle *fseh = (struct fsevent_handle *)kn->kn_hook;
2114
2115 lock_watch_table();
2116
2117 KNOTE_DETACH(&fseh->knotes, kn);
2118
2119 unlock_watch_table();
2120}
2121
2122/*
2123 * Determine whether this knote should be active
2124 *
2125 * This is kind of subtle.
2126 * --First, notice if the vnode has been revoked: in so, override hint
2127 * --EVFILT_READ knotes are checked no matter what the hint is
2128 * --Other knotes activate based on hint.
2129 * --If hint is revoke, set special flags and activate
2130 */
2131static int
2132filt_fsevent(struct knote *kn, long hint)
2133{
2134 fsevent_handle *fseh = (struct fsevent_handle *)kn->kn_hook;
2135 int activate = 0;
2136 int32_t rd, wr, amt;
2137
2138 if (NOTE_REVOKE == hint) {
2139 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2140 activate = 1;
2141 }
2142
2143 rd = fseh->watcher->rd;
2144 wr = fseh->watcher->wr;
2145 if (rd <= wr) {
2146 amt = wr - rd;
2147 } else {
2148 amt = fseh->watcher->eventq_size - (rd - wr);
2149 }
2150
2151 switch(kn->kn_filter) {
2152 case EVFILT_READ:
2153 kn->kn_data = amt;
2154
2155 if (kn->kn_data != 0) {
2156 activate = 1;
2157 }
2158 break;
2159 case EVFILT_VNODE:
2160 /* Check events this note matches against the hint */
2161 if (kn->kn_sfflags & hint) {
2162 kn->kn_fflags |= hint; /* Set which event occurred */
2163 }
2164 if (kn->kn_fflags != 0) {
2165 activate = 1;
2166 }
2167 break;
2168 default: {
2169 // nothing to do...
2170 break;
2171 }
2172 }
2173
2174 return (activate);
2175}
2176
2177
2178struct filterops fsevent_filtops = {
2179 .f_isfd = 1,
2180 .f_attach = NULL,
2181 .f_detach = filt_fsevent_detach,
2182 .f_event = filt_fsevent
2183};
2184
2d21ac55
A
2185static int
2186fseventsf_kqfilter(__unused struct fileproc *fp, __unused struct knote *kn, __unused vfs_context_t ctx)
91447636 2187{
b0d623f7
A
2188 fsevent_handle *fseh = (struct fsevent_handle *)fp->f_fglob->fg_data;
2189
2190 kn->kn_hook = (void*)fseh;
2191 kn->kn_hookid = 1;
2192 kn->kn_fop = &fsevent_filtops;
2193
2194 lock_watch_table();
2195
2196 KNOTE_ATTACH(&fseh->knotes, kn);
2197
2198 unlock_watch_table();
91447636
A
2199 return 0;
2200}
2201
2202
2203static int
2d21ac55 2204fseventsf_drain(struct fileproc *fp, __unused vfs_context_t ctx)
91447636
A
2205{
2206 int counter = 0;
2207 fsevent_handle *fseh = (struct fsevent_handle *)fp->f_fglob->fg_data;
2208
2209 fseh->watcher->flags |= WATCHER_CLOSING;
2210
2211 // if there are people still waiting, sleep for 10ms to
2212 // let them clean up and get out of there. however we
2213 // also don't want to get stuck forever so if they don't
2214 // exit after 5 seconds we're tearing things down anyway.
2215 while(fseh->watcher->blockers && counter++ < 500) {
2216 // issue wakeup in case anyone is blocked waiting for an event
2217 // do this each time we wakeup in case the blocker missed
2218 // the wakeup due to the unprotected test of WATCHER_CLOSING
2219 // and decision to tsleep in fmod_watch... this bit of
2220 // latency is a decent tradeoff against not having to
2221 // take and drop a lock in fmod_watch
2d21ac55 2222 fsevents_wakeup(fseh->watcher);
91447636
A
2223
2224 tsleep((caddr_t)fseh->watcher, PRIBIO, "watcher-close", 1);
2225 }
2226
2227 return 0;
2228}
2229
2230
2231static int
2d21ac55 2232fseventsopen(__unused dev_t dev, __unused int flag, __unused int mode, __unused struct proc *p)
91447636
A
2233{
2234 if (!is_suser()) {
2235 return EPERM;
2236 }
2237
2238 return 0;
2239}
2240
2241static int
2d21ac55 2242fseventsclose(__unused dev_t dev, __unused int flag, __unused int mode, __unused struct proc *p)
91447636
A
2243{
2244 return 0;
2245}
2246
2247static int
2d21ac55 2248fseventsread(__unused dev_t dev, __unused struct uio *uio, __unused int ioflag)
91447636
A
2249{
2250 return EIO;
2251}
2252
2d21ac55 2253
91447636 2254static int
2d21ac55 2255parse_buffer_and_add_events(const char *buffer, int bufsize, vfs_context_t ctx, long *remainder)
91447636 2256{
2d21ac55
A
2257 const fse_info *finfo, *dest_finfo;
2258 const char *path, *ptr, *dest_path, *event_start=buffer;
2259 int path_len, type, dest_path_len, err = 0;
2260
2261
2262 ptr = buffer;
2263 while ((ptr+sizeof(int)+sizeof(fse_info)+1) < buffer+bufsize) {
2264 type = *(const int *)ptr;
2265 if (type < 0 || type >= FSE_MAX_EVENTS) {
2266 err = EINVAL;
2267 break;
2268 }
2269
2270 ptr += sizeof(int);
2271
2272 finfo = (const fse_info *)ptr;
2273 ptr += sizeof(fse_info);
2274
2275 path = ptr;
2276 while(ptr < buffer+bufsize && *ptr != '\0') {
2277 ptr++;
2278 }
2279
2280 if (ptr >= buffer+bufsize) {
2281 break;
2282 }
2283
2284 ptr++; // advance over the trailing '\0'
2285
2286 path_len = ptr - path;
2287
2288 if (type != FSE_RENAME && type != FSE_EXCHANGE) {
2289 event_start = ptr; // record where the next event starts
2290
2291 err = add_fsevent(type, ctx, FSE_ARG_STRING, path_len, path, FSE_ARG_FINFO, finfo, FSE_ARG_DONE);
2292 if (err) {
2293 break;
2294 }
2295 continue;
2296 }
2297
2298 //
2299 // if we're here we have to slurp up the destination finfo
2300 // and path so that we can pass them to the add_fsevent()
2301 // call. basically it's a copy of the above code.
2302 //
2303 dest_finfo = (const fse_info *)ptr;
2304 ptr += sizeof(fse_info);
2305
2306 dest_path = ptr;
2307 while(ptr < buffer+bufsize && *ptr != '\0') {
2308 ptr++;
2309 }
2310
2311 if (ptr >= buffer+bufsize) {
2312 break;
2313 }
2314
2315 ptr++; // advance over the trailing '\0'
2316 event_start = ptr; // record where the next event starts
2317
2318 dest_path_len = ptr - dest_path;
b0d623f7
A
2319 //
2320 // If the destination inode number is non-zero, generate a rename
2321 // with both source and destination FSE_ARG_FINFO. Otherwise generate
2322 // a rename with only one FSE_ARG_FINFO. If you need to inject an
2323 // exchange with an inode of zero, just make that inode (and its path)
2324 // come in as the first one, not the second.
2325 //
2326 if (dest_finfo->ino) {
2327 err = add_fsevent(type, ctx,
2328 FSE_ARG_STRING, path_len, path, FSE_ARG_FINFO, finfo,
2329 FSE_ARG_STRING, dest_path_len, dest_path, FSE_ARG_FINFO, dest_finfo,
2330 FSE_ARG_DONE);
2331 } else {
2332 err = add_fsevent(type, ctx,
2333 FSE_ARG_STRING, path_len, path, FSE_ARG_FINFO, finfo,
2334 FSE_ARG_STRING, dest_path_len, dest_path,
2335 FSE_ARG_DONE);
2336 }
2337
2d21ac55
A
2338 if (err) {
2339 break;
2340 }
2341
2342 }
2343
2344 // if the last event wasn't complete, set the remainder
2345 // to be the last event start boundary.
2346 //
2347 *remainder = (long)((buffer+bufsize) - event_start);
2348
2349 return err;
2350}
2351
2352
2353//
2354// Note: this buffer size can not ever be less than
2355// 2*MAXPATHLEN + 2*sizeof(fse_info) + sizeof(int)
2356// because that is the max size for a single event.
2357// I made it 4k to be a "nice" size. making it
2358// smaller is not a good idea.
2359//
2360#define WRITE_BUFFER_SIZE 4096
2361char *write_buffer=NULL;
2362
2363static int
2364fseventswrite(__unused dev_t dev, struct uio *uio, __unused int ioflag)
2365{
2366 int error=0, count;
2367 vfs_context_t ctx = vfs_context_current();
2368 long offset=0, remainder;
2369
2370 lck_mtx_lock(&event_writer_lock);
2371
2372 if (write_buffer == NULL) {
2373 if (kmem_alloc(kernel_map, (vm_offset_t *)&write_buffer, WRITE_BUFFER_SIZE)) {
2374 lck_mtx_unlock(&event_writer_lock);
2375 return ENOMEM;
2376 }
2377 }
2378
2379 //
2380 // this loop copies in and processes the events written.
2381 // it takes care to copy in reasonable size chunks and
2382 // process them. if there is an event that spans a chunk
2383 // boundary we're careful to copy those bytes down to the
2384 // beginning of the buffer and read the next chunk in just
2385 // after it.
2386 //
2387 while(uio_resid(uio)) {
2388 if (uio_resid(uio) > (WRITE_BUFFER_SIZE-offset)) {
2389 count = WRITE_BUFFER_SIZE - offset;
2390 } else {
2391 count = uio_resid(uio);
2392 }
2393
2394 error = uiomove(write_buffer+offset, count, uio);
2395 if (error) {
2396 break;
2397 }
2398
2399 // printf("fsevents: write: copied in %d bytes (offset: %ld)\n", count, offset);
2400 error = parse_buffer_and_add_events(write_buffer, offset+count, ctx, &remainder);
2401 if (error) {
2402 break;
2403 }
2404
2405 //
2406 // if there's any remainder, copy it down to the beginning
2407 // of the buffer so that it will get processed the next time
2408 // through the loop. note that the remainder always starts
2409 // at an event boundary.
2410 //
2411 if (remainder != 0) {
2412 // printf("fsevents: write: an event spanned a %d byte boundary. remainder: %ld\n",
2413 // WRITE_BUFFER_SIZE, remainder);
2414 memmove(write_buffer, (write_buffer+count+offset) - remainder, remainder);
2415 offset = remainder;
2416 } else {
2417 offset = 0;
2418 }
2419 }
2420
2421 lck_mtx_unlock(&event_writer_lock);
2422
2423 return error;
91447636
A
2424}
2425
2426
2427static struct fileops fsevents_fops = {
2428 fseventsf_read,
2429 fseventsf_write,
2430 fseventsf_ioctl,
2431 fseventsf_select,
2432 fseventsf_close,
2433 fseventsf_kqfilter,
2434 fseventsf_drain
2435};
2436
2d21ac55
A
2437typedef struct ext_fsevent_clone_args {
2438 user_addr_t event_list;
2439 int32_t num_events;
2440 int32_t event_queue_depth;
2441 user_addr_t fd;
2442} ext_fsevent_clone_args;
2443
2444typedef struct old_fsevent_clone_args {
b0d623f7 2445 uint32_t event_list;
2d21ac55
A
2446 int32_t num_events;
2447 int32_t event_queue_depth;
b0d623f7 2448 uint32_t fd;
2d21ac55 2449} old_fsevent_clone_args;
91447636 2450
2d21ac55 2451#define OLD_FSEVENTS_CLONE _IOW('s', 1, old_fsevent_clone_args)
91447636
A
2452
2453static int
2d21ac55 2454fseventsioctl(__unused dev_t dev, u_long cmd, caddr_t data, __unused int flag, struct proc *p)
91447636
A
2455{
2456 struct fileproc *f;
2457 int fd, error;
2458 fsevent_handle *fseh = NULL;
2d21ac55 2459 ext_fsevent_clone_args *fse_clone_args, _fse_clone;
91447636 2460 int8_t *event_list;
2d21ac55 2461 int is64bit = proc_is64bit(p);
91447636
A
2462
2463 switch (cmd) {
2d21ac55
A
2464 case OLD_FSEVENTS_CLONE: {
2465 old_fsevent_clone_args *old_args = (old_fsevent_clone_args *)data;
2466
2467 fse_clone_args = &_fse_clone;
2468 memset(fse_clone_args, 0, sizeof(ext_fsevent_clone_args));
2469
2470 fse_clone_args->event_list = CAST_USER_ADDR_T(old_args->event_list);
2471 fse_clone_args->num_events = old_args->num_events;
2472 fse_clone_args->event_queue_depth = old_args->event_queue_depth;
2473 fse_clone_args->fd = CAST_USER_ADDR_T(old_args->fd);
2474 goto handle_clone;
2475 }
2476
91447636 2477 case FSEVENTS_CLONE:
2d21ac55
A
2478 if (is64bit) {
2479 fse_clone_args = (ext_fsevent_clone_args *)data;
2480 } else {
2481 fsevent_clone_args *ufse_clone = (fsevent_clone_args *)data;
2482
2483 fse_clone_args = &_fse_clone;
2484 memset(fse_clone_args, 0, sizeof(ext_fsevent_clone_args));
2485
2486 fse_clone_args->event_list = CAST_USER_ADDR_T(ufse_clone->event_list);
2487 fse_clone_args->num_events = ufse_clone->num_events;
2488 fse_clone_args->event_queue_depth = ufse_clone->event_queue_depth;
2489 fse_clone_args->fd = CAST_USER_ADDR_T(ufse_clone->fd);
2490 }
2491
2492 handle_clone:
91447636
A
2493 if (fse_clone_args->num_events < 0 || fse_clone_args->num_events > 4096) {
2494 return EINVAL;
2495 }
2496
2497 MALLOC(fseh, fsevent_handle *, sizeof(fsevent_handle),
2498 M_TEMP, M_WAITOK);
2d21ac55
A
2499 if (fseh == NULL) {
2500 return ENOMEM;
2501 }
91447636 2502 memset(fseh, 0, sizeof(fsevent_handle));
b0d623f7
A
2503
2504 klist_init(&fseh->knotes);
91447636
A
2505
2506 MALLOC(event_list, int8_t *,
2507 fse_clone_args->num_events * sizeof(int8_t),
2508 M_TEMP, M_WAITOK);
2d21ac55
A
2509 if (event_list == NULL) {
2510 FREE(fseh, M_TEMP);
2511 return ENOMEM;
2512 }
91447636 2513
2d21ac55 2514 error = copyin(fse_clone_args->event_list,
91447636
A
2515 (void *)event_list,
2516 fse_clone_args->num_events * sizeof(int8_t));
2517 if (error) {
2518 FREE(event_list, M_TEMP);
2519 FREE(fseh, M_TEMP);
2520 return error;
2521 }
2522
2523 error = add_watcher(event_list,
2524 fse_clone_args->num_events,
2525 fse_clone_args->event_queue_depth,
2526 &fseh->watcher);
2527 if (error) {
2528 FREE(event_list, M_TEMP);
2529 FREE(fseh, M_TEMP);
2530 return error;
2531 }
2532
2d21ac55
A
2533 // connect up the watcher with this fsevent_handle
2534 fseh->watcher->fseh = fseh;
2535
2536 error = falloc(p, &f, &fd, vfs_context_current());
91447636
A
2537 if (error) {
2538 FREE(event_list, M_TEMP);
2539 FREE(fseh, M_TEMP);
2540 return (error);
2541 }
2542 proc_fdlock(p);
2543 f->f_fglob->fg_flag = FREAD | FWRITE;
2544 f->f_fglob->fg_type = DTYPE_FSEVENTS;
2545 f->f_fglob->fg_ops = &fsevents_fops;
2546 f->f_fglob->fg_data = (caddr_t) fseh;
2d21ac55
A
2547 proc_fdunlock(p);
2548 error = copyout((void *)&fd, fse_clone_args->fd, sizeof(int32_t));
2549 if (error != 0) {
2550 fp_free(p, fd, f);
2551 } else {
91447636 2552 proc_fdlock(p);
6601e61a 2553 procfdtbl_releasefd(p, fd, NULL);
91447636
A
2554 fp_drop(p, fd, f, 1);
2555 proc_fdunlock(p);
2d21ac55 2556 }
91447636
A
2557 break;
2558
2559 default:
2560 error = EINVAL;
2561 break;
2562 }
2563
2564 return error;
2565}
2566
91447636 2567static void
2d21ac55 2568fsevents_wakeup(fs_event_watcher *watcher)
91447636 2569{
2d21ac55 2570 selwakeup(&watcher->fseh->si);
b0d623f7
A
2571 KNOTE(&watcher->fseh->knotes, NOTE_WRITE|NOTE_NONE);
2572 wakeup((caddr_t)watcher);
91447636
A
2573}
2574
2575
2576/*
2577 * A struct describing which functions will get invoked for certain
2578 * actions.
2579 */
2580static struct cdevsw fsevents_cdevsw =
2581{
2582 fseventsopen, /* open */
2583 fseventsclose, /* close */
2584 fseventsread, /* read */
2585 fseventswrite, /* write */
2586 fseventsioctl, /* ioctl */
2d21ac55
A
2587 (stop_fcn_t *)&nulldev, /* stop */
2588 (reset_fcn_t *)&nulldev, /* reset */
91447636
A
2589 NULL, /* tty's */
2590 eno_select, /* select */
2591 eno_mmap, /* mmap */
2592 eno_strat, /* strategy */
2593 eno_getc, /* getc */
2594 eno_putc, /* putc */
2595 0 /* type */
2596};
2597
2598
2599/*
2600 * Called to initialize our device,
2601 * and to register ourselves with devfs
2602 */
2603
2604void
2605fsevents_init(void)
2606{
2607 int ret;
2608
2609 if (fsevents_installed) {
2610 return;
2611 }
2612
2613 fsevents_installed = 1;
2614
91447636
A
2615 ret = cdevsw_add(-1, &fsevents_cdevsw);
2616 if (ret < 0) {
2617 fsevents_installed = 0;
2618 return;
2619 }
2620
2621 devfs_make_node(makedev (ret, 0), DEVFS_CHAR,
2622 UID_ROOT, GID_WHEEL, 0644, "fsevents", 0);
2623
2624 fsevents_internal_init();
2625}
2626
2627
91447636
A
2628char *
2629get_pathbuff(void)
2630{
b0d623f7 2631 char *path;
91447636 2632
b0d623f7
A
2633 MALLOC_ZONE(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
2634 return path;
91447636
A
2635}
2636
2637void
2638release_pathbuff(char *path)
2639{
91447636
A
2640
2641 if (path == NULL) {
2642 return;
2643 }
91447636
A
2644 FREE_ZONE(path, MAXPATHLEN, M_NAMEI);
2645}
2646
2647int
cf7d32b8 2648get_fse_info(struct vnode *vp, fse_info *fse, __unused vfs_context_t ctx)
91447636
A
2649{
2650 struct vnode_attr va;
2651
2652 VATTR_INIT(&va);
2653 VATTR_WANTED(&va, va_fsid);
2654 VATTR_WANTED(&va, va_fileid);
2655 VATTR_WANTED(&va, va_mode);
2656 VATTR_WANTED(&va, va_uid);
2657 VATTR_WANTED(&va, va_gid);
2d21ac55
A
2658 if (vp->v_flag & VISHARDLINK) {
2659 if (vp->v_type == VDIR) {
2660 VATTR_WANTED(&va, va_dirlinkcount);
2661 } else {
2662 VATTR_WANTED(&va, va_nlink);
2663 }
2664 }
2665
cf7d32b8 2666 if (vnode_getattr(vp, &va, vfs_context_kernel()) != 0) {
2d21ac55 2667 memset(fse, 0, sizeof(fse_info));
91447636
A
2668 return -1;
2669 }
2670
2d21ac55 2671 fse->ino = (ino64_t)va.va_fileid;
91447636 2672 fse->dev = (dev_t)va.va_fsid;
91447636
A
2673 fse->mode = (int32_t)vnode_vttoif(vnode_vtype(vp)) | va.va_mode;
2674 fse->uid = (uid_t)va.va_uid;
2675 fse->gid = (gid_t)va.va_gid;
2d21ac55
A
2676 if (vp->v_flag & VISHARDLINK) {
2677 fse->mode |= FSE_MODE_HLINK;
2678 if (vp->v_type == VDIR) {
2679 fse->nlink = (uint64_t)va.va_dirlinkcount;
2680 } else {
2681 fse->nlink = (uint64_t)va.va_nlink;
2682 }
2683 }
2684
91447636
A
2685 return 0;
2686}
2d21ac55 2687
b0d623f7
A
2688void
2689create_fsevent_from_kevent(vnode_t vp, uint32_t kevents, struct vnode_attr *vap)
2690{
2691 int fsevent_type=FSE_CONTENT_MODIFIED, len; // the default is the most pessimistic
2692 char pathbuf[MAXPATHLEN];
2693 fse_info fse;
2694
2695
2696 if (kevents & VNODE_EVENT_DELETE) {
2697 fsevent_type = FSE_DELETE;
2698 } else if (kevents & (VNODE_EVENT_EXTEND|VNODE_EVENT_WRITE)) {
2699 fsevent_type = FSE_CONTENT_MODIFIED;
2700 } else if (kevents & VNODE_EVENT_LINK) {
2701 fsevent_type = FSE_CREATE_FILE;
2702 } else if (kevents & VNODE_EVENT_RENAME) {
2703 fsevent_type = FSE_CREATE_FILE; // XXXdbg - should use FSE_RENAME but we don't have the destination info;
2704 } else if (kevents & (VNODE_EVENT_FILE_CREATED|VNODE_EVENT_FILE_REMOVED|VNODE_EVENT_DIR_CREATED|VNODE_EVENT_DIR_REMOVED)) {
2705 fsevent_type = FSE_STAT_CHANGED; // XXXdbg - because vp is a dir and the thing created/removed lived inside it
2706 } else { // a catch all for VNODE_EVENT_PERMS, VNODE_EVENT_ATTRIB and anything else
2707 fsevent_type = FSE_STAT_CHANGED;
2708 }
2709
2710 // printf("convert_kevent: kevents 0x%x fsevent type 0x%x (for %s)\n", kevents, fsevent_type, vp->v_name ? vp->v_name : "(no-name)");
2711
2712 fse.dev = vap->va_fsid;
2713 fse.ino = vap->va_fileid;
2714 fse.mode = vnode_vttoif(vnode_vtype(vp)) | (uint32_t)vap->va_mode;
2715 if (vp->v_flag & VISHARDLINK) {
2716 fse.mode |= FSE_MODE_HLINK;
2717 if (vp->v_type == VDIR) {
2718 fse.nlink = vap->va_dirlinkcount;
2719 } else {
2720 fse.nlink = vap->va_nlink;
2721 }
2722 }
2723
2724 if (vp->v_type == VDIR) {
2725 fse.mode |= FSE_REMOTE_DIR_EVENT;
2726 }
2727
2728
2729 fse.uid = vap->va_uid;
2730 fse.gid = vap->va_gid;
2731
2732 len = sizeof(pathbuf);
2733 if (vn_getpath(vp, pathbuf, &len) == 0) {
2734 add_fsevent(fsevent_type, vfs_context_current(), FSE_ARG_STRING, len, pathbuf, FSE_ARG_FINFO, &fse, FSE_ARG_DONE);
2735 }
2736 return;
2737}
2738
2d21ac55
A
2739#else /* CONFIG_FSE */
2740/*
2741 * The get_pathbuff and release_pathbuff routines are used in places not
2742 * related to fsevents, and it's a handy abstraction, so define trivial
2743 * versions that don't cache a pool of buffers. This way, we don't have
2744 * to conditionalize the callers, and they still get the advantage of the
2745 * pool of buffers if CONFIG_FSE is turned on.
2746 */
2747char *
2748get_pathbuff(void)
2749{
2750 char *path;
2751 MALLOC_ZONE(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
2752 return path;
2753}
2754
2755void
2756release_pathbuff(char *path)
2757{
2758 FREE_ZONE(path, MAXPATHLEN, M_NAMEI);
2759}
2760#endif /* CONFIG_FSE */