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