1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set expandtab ts=4 shiftwidth=4: */
4 * Copyright (c) 2008, 2010 Oracle and/or its affiliates, Inc. All rights
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Authors: Lin Ma <lin.ma@sun.com>
30 #include "fen-kernel.h"
33 #ifdef GIO_COMPILATION
34 #define FK_W if (FALSE) g_debug
36 #include "gam_error.h"
37 #define FK_W(...) GAM_DEBUG(DEBUG_INFO, __VA_ARGS__)
40 G_LOCK_DEFINE (fen_lock);
42 static ulong max_port_events = 512;
43 static GList *pn_visible_list; /* the queue of ports which don't have the max objs */
44 static GQueue *g_eventq = NULL;
45 static timespec_t zero_wait;
46 static void (*user_process_events_cb) (gpointer, node_event_t*);
47 static port_event_t *pevents = NULL;
48 static gint PE_ALLOC = 2048;
49 static GHashTable *renamed_hash = NULL; /* <parent node, ev> */
51 typedef struct _PSource {
52 GSource source; /* Inherit from GSource, must be the first. */
55 uint_t event_growing_factor;
56 uint_t pending_events;
59 #define PGPFD(s) (&((PSource *)(s))->gfd)
60 #define SLEEP_BASE_TIME 10 /* in milliseconds */
61 #define EXPECT_INC_EVENTS(pn) (1 << (pn->event_growing_factor))
63 #define RENAME_EVENTS_INTERVAL 500 /* in milliseconds */
64 #define PROCESS_PORT_EVENTS_TIME 1000 /* in milliseconds */
65 guint process_port_event_id = 0;
67 static gchar* _event_strings(int event);
68 static const gchar* _event_string (int event);
69 static GSource *psource_new();
71 static gboolean port_prepare(GSource *source, gint *timeout_);
72 static gboolean port_check(GSource *source);
73 static gboolean port_dispatch(GSource *source, GSourceFunc callback, gpointer user_data);
74 static GSourceFuncs fen_source_func = {
82 port_prepare(GSource *source, gint *timeout_)
88 port_check(GSource *source)
90 PSource *pn = (PSource *)source;
95 g_source_add_poll(source, PGPFD(source));
96 g_source_unref(source);
100 if (!(PGPFD(pn)->revents & G_IO_IN))
103 if (port_getn(PGPFD(source)->fd, NULL, 0, &nget, 0) == 0) {
104 if (nget - pn->pending_events > EXPECT_INC_EVENTS(pn)) {
105 /* Sleep for a while. */
106 pn->pending_events = nget;
107 pn->event_growing_factor ++;
110 g_source_ref(source);
111 g_source_remove_poll(source, PGPFD(source));
112 g_timeout_add(SLEEP_BASE_TIME,
113 (GSourceFunc)port_check,
119 pn->pending_events = 0;
120 pn->event_growing_factor = 0;
126 port_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
132 FK_W ("%s 0x%p fd %d\n", __func__, source, PGPFD(source)->fd);
137 if (port_getn(PGPFD(source)->fd, pevents, PE_ALLOC, &nget, &zero_wait) == 0) {
139 for (i = 0; i < nget; i++) {
140 f = (node_t *)pevents[i].portev_user;
142 if (pevents[i].portev_source == PORT_SOURCE_FILE) {
144 NODE_CLE_STATE(f, NODE_STATE_ASSOCIATED);
145 NODE_SET_STATE(f, NODE_STATE_HAS_EVENTS);
147 if (HAS_NO_EXCEPTION_EVENTS(pevents[i].portev_events)) {
148 /* If the events do not show it's deleted, update
149 * file timestamp to avoid missing events next time.
151 if (node_lstat(f) != 0 /* || port_add(f) != 0 */) {
152 /* Included deleted event. */
153 pevents[i].portev_events |= FILE_DELETE;
157 /* Queue it and waiting for processing. */
158 g_queue_push_tail(g_eventq,
159 node_event_new(pevents[i].portev_events, (gpointer)f));
162 FK_W ("[kernel] unknown portev_source %d\n", pevents[i].portev_source);
169 FK_W ("[kernel] port_getn %s\n", g_strerror (errno));
172 } while (nget == PE_ALLOC);
176 if (total > 0 && callback) {
177 FK_W ("[kernel] get total %ld events\n", total);
178 return callback (user_data);
184 process_renamed_hash_cb(gpointer key, gpointer value, gpointer user_data)
186 node_event_t *ev = value;
189 node_add_event(ev->user_data, ev);
191 user_process_events_cb(ev->user_data, ev);
193 /* Always delete self from hash. */
198 port_events_process_cb(gpointer user_data)
204 /* Processing g_eventq */
205 while ((ev = (node_event_t*)g_queue_pop_head (g_eventq)) != NULL) {
207 /* FK_W ("[%s] 0x%p %s\n", __func__, ev, _event_string (ev->e)); */
210 gchar *log = _event_strings(ev->e);
211 FK_W ("%s %s %s\n", __func__, NODE_NAME(ev->user_data), log);
215 #ifdef GIO_COMPILATION
216 /* Use the parent node as a hash, because only the dir_subs in the
217 * parent node should receive MOVE event.
219 if (NODE_PARENT(ev->user_data)) {
220 if (ev->e == FILE_RENAME_TO) {
221 g_hash_table_insert(renamed_hash, NODE_PARENT(ev->user_data), ev);
222 g_time_val_add(&ev->rename_tv, RENAME_EVENTS_INTERVAL);
225 if (ev->e == FILE_RENAME_FROM) {
226 node_event_t *pair_ev;
228 pair_ev = g_hash_table_lookup(renamed_hash, NODE_PARENT(ev->user_data));
229 if (pair_ev && node_timeval_lt(&ev->ctv, &pair_ev->rename_tv)) {
230 g_hash_table_remove(renamed_hash, NODE_PARENT(ev->user_data));
231 pair_ev->pair_data = ev->user_data;
232 /* Free ev, exchange pair_ev and ev. */
233 node_event_delete(ev);
241 node_add_event(ev->user_data, ev);
243 user_process_events_cb(ev->user_data, ev);
247 /* Processing the events in renamed_hash. TODO we should delay it and wait
248 * for more possible pair.
250 g_hash_table_foreach_remove(renamed_hash, process_renamed_hash_cb, NULL);
254 process_port_event_id = 0;
259 port_events_read_cb(gpointer user_data)
262 if (process_port_event_id == 0) {
263 process_port_event_id = g_timeout_add(PROCESS_PORT_EVENTS_TIME,
264 port_events_process_cb,
272 * malloc PSource and port_create, start thread at pnode_ref.
273 * if psource_new succeeded, the PSource will never
274 * be freed. So PSource can be freed only in psource_new.
275 * Note pnode_monitor_remove_all can also free PSource, but currently no one
281 GSource *source = NULL;
284 if ((fd = port_create()) >= 0) {
285 source = g_source_new(&fen_source_func, sizeof(PSource));
286 PGPFD(source)->fd = fd;
287 PGPFD(source)->events = G_IO_IN | G_IO_HUP | G_IO_ERR;
288 g_source_set_callback(source, port_events_read_cb, NULL, NULL);
289 g_source_attach(source, NULL);
290 g_source_unref(source);
291 g_source_add_poll(source, PGPFD(source));
293 FK_W ("%s 0x%p fd %d\n", __func__, source, PGPFD(source)->fd);
295 FK_W ("PORT_CREATE %s\n", g_strerror(errno));
296 g_return_val_if_reached(NULL);
306 * Unsafe, need lock fen_lock.
307 * port_add will associate a GSource to @f->source
312 GSource *source = f->source;
314 FK_W ("%s [0x%p] %s\n", __func__, f, NODE_NAME(f));
317 g_assert(NODE_HAS_FLAG(f, NODE_FLAG_STAT_UPDATED));
319 /* if (!NODE_HAS_FLAG(f, NODE_FLAG_STAT_DONE)) { */
320 /* if (NODE_STAT(f) != 0) { */
325 /* Try re-use f->pn. f->pn may be used by other request, e.g. f is deleted
326 * for a long time. So if pn is full, we try to open a new one.
330 /* Try the next visible source. */
331 if (pn_visible_list) {
332 source = (GSource *)pn_visible_list->data;
334 if ((source = psource_new()) != NULL) {
335 g_assert (g_list_find (pn_visible_list, source) == NULL);
336 pn_visible_list = g_list_prepend (pn_visible_list, source);
341 if (port_associate(PGPFD(source)->fd, PORT_SOURCE_FILE, (uintptr_t)FILE_OBJECT(f),
345 NODE_SET_STATE(f, NODE_STATE_ASSOCIATED);
346 NODE_CLE_FLAG(f, NODE_FLAG_STAT_UPDATED);
347 FK_W ("PORT_ASSOCIATE 0x%p OK\n", f);
349 } else if (errno == EAGAIN) {
350 /* Full, remove it. */
351 pn_visible_list = g_list_remove (pn_visible_list, source);
355 } else if (errno == ENOENT) {
356 /* File is not exist */
357 } else if (errno == ENOTSUP) {
358 /* FS is not supported. Currently we think it no longer make sense to
359 * monitor it, so clean the stat info and return 0 to ignore this
360 * node. If there are requirement, we can consider to add polling
363 NODE_CLE_FLAG(f, NODE_FLAG_STAT_UPDATED);
366 FK_W ("PORT_ASSOCIATE 0x%p %s\n", f, g_strerror (errno));
369 /* No matter if associated successfully, stat info is out-of-date, so clean it. */
370 NODE_CLE_FLAG(f, NODE_FLAG_STAT_UPDATED);
378 * Unsafe, need lock fen_lock.
381 port_remove (node_t *f)
383 /* g_assert(f->source); */
385 if (NODE_HAS_STATE(f, NODE_STATE_ASSOCIATED)) {
386 /* Mark unregisted. */
387 if (port_dissociate(PGPFD(f->source)->fd, PORT_SOURCE_FILE, (uintptr_t)FILE_OBJECT(f)) == 0) {
389 * Note, we can run foode_delete if dissociating is failed,
390 * because there may be some pending events (mostly like
391 * FILE_DELETE) in the port_get. If we delete the foode
392 * the fnode may be deleted, then port_get will run on an invalid
395 NODE_CLE_STATE(f, NODE_STATE_ASSOCIATED);
396 FK_W ("PORT_DISSOCIATE 0x%p OK\n", f);
397 } else if (errno == ENOENT) {
398 /* The file has been removed from port, after port_get or before
399 * port_get but DELETED event has been generated.
402 FK_W ("PORT_DISSOCIATE 0x%p %s\n", f, g_strerror (errno));
403 g_return_if_reached();
409 * Get Solaris resouce values.
414 port_class_init (void (*user_process_events_callback) (gpointer, node_event_t*))
418 if ((rblk = malloc (rctlblk_size ())) == NULL) {
419 FK_W ("[kernel] rblk malloc %s\n", g_strerror (errno));
422 if (getrctl ("process.max-port-events", NULL, rblk, RCTL_FIRST) == -1) {
423 FK_W ("[kernel] getrctl %s\n", g_strerror (errno));
427 max_port_events = rctlblk_get_value(rblk);
428 FK_W ("max_port_events = %u\n", max_port_events);
431 renamed_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
433 if (renamed_hash == NULL) {
434 FK_W ("[kernel] FEN global renamed queue initializing faild\n");
437 if ((g_eventq = g_queue_new ()) == NULL) {
438 FK_W ("[kernel] FEN global event queue initializing faild\n");
441 if (user_process_events_callback == NULL) {
442 FK_W ("[kernel] FEN global no user_process_events_callback\n");
445 user_process_events_cb = user_process_events_callback;
446 memset (&zero_wait, 0, sizeof (timespec_t));
448 pevents = g_malloc(PE_ALLOC * sizeof(port_event_t));
449 if (pevents == NULL) {
450 FK_W ("[kernel] FEN global alloc pevents failed\n");
458 printevent (const char *pname, int event, const char *tag)
460 static gchar *event_string = NULL;
464 g_free(event_string);
467 str = g_string_new ("");
468 g_string_printf (str, "[%s] [%-20s]", tag, pname);
469 if (event & FILE_ACCESS) {
470 str = g_string_append (str, " ACCESS");
472 if (event & FILE_MODIFIED) {
473 str = g_string_append (str, " MODIFIED");
475 if (event & FILE_ATTRIB) {
476 str = g_string_append (str, " ATTRIB");
478 if (event & FILE_DELETE) {
479 str = g_string_append (str, " DELETE");
481 if (event & FILE_RENAME_TO) {
482 str = g_string_append (str, " RENAME_TO");
484 if (event & FILE_RENAME_FROM) {
485 str = g_string_append (str, " RENAME_FROM");
487 if (event & UNMOUNTED) {
488 str = g_string_append (str, " UNMOUNTED");
490 if (event & MOUNTEDOVER) {
491 str = g_string_append (str, " MOUNTEDOVER");
493 event_string = str->str;
494 g_string_free (str, FALSE);
499 _event_strings(int event)
501 GString *str = g_string_sized_new(80);
503 if (event & FILE_DELETE)
504 g_string_append(str, " FILE_DELETE");
506 if (event & FILE_RENAME_FROM)
507 g_string_append(str, " FILE_RENAME_FROM");
509 if (event & FILE_MODIFIED)
510 g_string_append(str, " FILE_MODIFIED");
512 if (event & FILE_RENAME_TO)
513 g_string_append(str, " FILE_RENAME_TO");
515 if (event & MOUNTEDOVER)
516 g_string_append(str, " MOUNTEDOVER");
518 if (event & FILE_ATTRIB)
519 g_string_append(str, " FILE_ATTRIB");
521 if (event & UNMOUNTED)
522 g_string_append(str, " UNMOUNTED");
524 if (event & FILE_ACCESS)
525 g_string_append(str, " FILE_ACCESS");
527 return g_string_free(str, FALSE);
531 _event_string (int event)
535 return "FILE_DELETE";
536 case FILE_RENAME_FROM:
537 return "FILE_RENAME_FROM";
539 return "FILE_MODIFIED";
541 return "FILE_RENAME_TO";
543 return "MOUNTEDOVER";
545 return "FILE_ATTRIB";
549 return "FILE_ACCESS";
551 return "EVENT_UNKNOWN";