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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Authors: Lin Ma <lin.ma@sun.com>
32 #include "fen-kernel.h"
35 #ifdef GIO_COMPILATION
36 #define FK_W if (FALSE) g_debug
38 #include "gam_error.h"
39 #define FK_W(...) GAM_DEBUG(DEBUG_INFO, __VA_ARGS__)
42 G_GNUC_INTERNAL G_LOCK_DEFINE (fen_lock);
44 static ulong max_port_events = 512;
45 static GList *pn_visible_list; /* the queue of ports which don't have the max objs */
46 static GQueue *g_eventq = NULL;
47 static timespec_t zero_wait;
48 static void (*user_process_events_cb) (gpointer, node_event_t*);
49 static port_event_t *pevents = NULL;
50 static gint PE_ALLOC = 2048;
51 static GHashTable *renamed_hash = NULL; /* <parent node, ev> */
53 typedef struct _PSource {
54 GSource source; /* Inherit from GSource, must be the first. */
57 uint_t event_growing_factor;
58 uint_t pending_events;
61 #define PGPFD(s) (&((PSource *)(s))->gfd)
62 #define SLEEP_BASE_TIME 10 /* in milliseconds */
63 #define EXPECT_INC_EVENTS(pn) (1 << (pn->event_growing_factor))
65 #define RENAME_EVENTS_INTERVAL 500 /* in milliseconds */
66 #define PROCESS_PORT_EVENTS_TIME 1000 /* in milliseconds */
67 guint process_port_event_id = 0;
69 static gchar* _event_strings(int event);
70 static const gchar* _event_string (int event);
71 static GSource *psource_new();
73 static gboolean port_prepare(GSource *source, gint *timeout_);
74 static gboolean port_check(GSource *source);
75 static gboolean port_dispatch(GSource *source, GSourceFunc callback, gpointer user_data);
76 static GSourceFuncs fen_source_func = {
84 port_prepare(GSource *source, gint *timeout_)
90 port_check(GSource *source)
92 PSource *pn = (PSource *)source;
97 g_source_add_poll(source, PGPFD(source));
98 g_source_unref(source);
102 if (!(PGPFD(pn)->revents & G_IO_IN))
105 if (port_getn(PGPFD(source)->fd, NULL, 0, &nget, 0) == 0) {
106 if (nget - pn->pending_events > EXPECT_INC_EVENTS(pn)) {
107 /* Sleep for a while. */
108 pn->pending_events = nget;
109 pn->event_growing_factor ++;
112 g_source_ref(source);
113 g_source_remove_poll(source, PGPFD(source));
114 g_timeout_add(SLEEP_BASE_TIME,
115 (GSourceFunc)port_check,
121 pn->pending_events = 0;
122 pn->event_growing_factor = 0;
128 port_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
134 FK_W ("%s 0x%p fd %d\n", __func__, source, PGPFD(source)->fd);
139 if (port_getn(PGPFD(source)->fd, pevents, PE_ALLOC, &nget, &zero_wait) == 0) {
141 for (i = 0; i < nget; i++) {
142 f = (node_t *)pevents[i].portev_user;
144 if (pevents[i].portev_source == PORT_SOURCE_FILE) {
146 NODE_CLE_STATE(f, NODE_STATE_ASSOCIATED);
147 NODE_SET_STATE(f, NODE_STATE_HAS_EVENTS);
149 if (HAS_NO_EXCEPTION_EVENTS(pevents[i].portev_events)) {
150 /* If the events do not show it's deleted, update
151 * file timestamp to avoid missing events next time.
153 if (node_lstat(f) != 0 /* || port_add(f) != 0 */) {
154 /* Included deleted event. */
155 pevents[i].portev_events |= FILE_DELETE;
159 /* Queue it and waiting for processing. */
160 g_queue_push_tail(g_eventq,
161 node_event_new(pevents[i].portev_events, (gpointer)f));
164 FK_W ("[kernel] unknown portev_source %d\n", pevents[i].portev_source);
171 FK_W ("[kernel] port_getn %s\n", g_strerror (errno));
174 } while (nget == PE_ALLOC);
178 if (total > 0 && callback) {
179 FK_W ("[kernel] get total %ld events\n", total);
180 return callback (user_data);
186 process_renamed_hash_cb(gpointer key, gpointer value, gpointer user_data)
188 node_event_t *ev = value;
191 node_add_event(ev->user_data, ev);
193 user_process_events_cb(ev->user_data, ev);
195 /* Always delete self from hash. */
200 port_events_process_cb(gpointer user_data)
206 /* Processing g_eventq */
207 while ((ev = (node_event_t*)g_queue_pop_head (g_eventq)) != NULL) {
209 /* FK_W ("[%s] 0x%p %s\n", __func__, ev, _event_string (ev->e)); */
212 gchar *log = _event_strings(ev->e);
213 FK_W ("%s %s %s\n", __func__, NODE_NAME(ev->user_data), log);
217 #ifdef GIO_COMPILATION
218 /* Use the parent node as a hash, because only the dir_subs in the
219 * parent node should receive MOVE event.
221 if (NODE_PARENT(ev->user_data)) {
222 if (ev->e == FILE_RENAME_TO) {
223 g_hash_table_insert(renamed_hash, NODE_PARENT(ev->user_data), ev);
224 g_time_val_add(&ev->rename_tv, RENAME_EVENTS_INTERVAL);
227 if (ev->e == FILE_RENAME_FROM) {
228 node_event_t *pair_ev;
230 pair_ev = g_hash_table_lookup(renamed_hash, NODE_PARENT(ev->user_data));
231 if (pair_ev && node_timeval_lt(&ev->ctv, &pair_ev->rename_tv)) {
232 g_hash_table_remove(renamed_hash, NODE_PARENT(ev->user_data));
233 pair_ev->pair_data = ev->user_data;
234 /* Free ev, exchange pair_ev and ev. */
235 node_event_delete(ev);
243 node_add_event(ev->user_data, ev);
245 user_process_events_cb(ev->user_data, ev);
249 /* Processing the events in renamed_hash. TODO we should delay it and wait
250 * for more possible pair.
252 g_hash_table_foreach_remove(renamed_hash, process_renamed_hash_cb, NULL);
256 process_port_event_id = 0;
261 port_events_read_cb(gpointer user_data)
264 if (process_port_event_id == 0) {
265 process_port_event_id = g_timeout_add(PROCESS_PORT_EVENTS_TIME,
266 port_events_process_cb,
274 * malloc PSource and port_create, start thread at pnode_ref.
275 * if psource_new succeeded, the PSource will never
276 * be freed. So PSource can be freed only in psource_new.
277 * Note pnode_monitor_remove_all can also free PSource, but currently no one
283 GSource *source = NULL;
286 if ((fd = port_create()) >= 0) {
287 source = g_source_new(&fen_source_func, sizeof(PSource));
288 PGPFD(source)->fd = fd;
289 PGPFD(source)->events = G_IO_IN | G_IO_HUP | G_IO_ERR;
290 g_source_set_callback(source, port_events_read_cb, NULL, NULL);
291 g_source_attach(source, NULL);
292 g_source_unref(source);
293 g_source_add_poll(source, PGPFD(source));
295 FK_W ("%s 0x%p fd %d\n", __func__, source, PGPFD(source)->fd);
297 FK_W ("PORT_CREATE %s\n", g_strerror(errno));
298 g_return_val_if_reached(NULL);
308 * Unsafe, need lock fen_lock.
309 * port_add will associate a GSource to @f->source
314 GSource *source = f->source;
316 FK_W ("%s [0x%p] %s\n", __func__, f, NODE_NAME(f));
319 g_assert(NODE_HAS_FLAG(f, NODE_FLAG_STAT_UPDATED));
321 /* if (!NODE_HAS_FLAG(f, NODE_FLAG_STAT_DONE)) { */
322 /* if (NODE_STAT(f) != 0) { */
327 /* Try re-use f->pn. f->pn may be used by other request, e.g. f is deleted
328 * for a long time. So if pn is full, we try to open a new one.
332 /* Try the next visible source. */
333 if (pn_visible_list) {
334 source = (GSource *)pn_visible_list->data;
336 if ((source = psource_new()) != NULL) {
337 g_assert (g_list_find (pn_visible_list, source) == NULL);
338 pn_visible_list = g_list_prepend (pn_visible_list, source);
343 if (port_associate(PGPFD(source)->fd, PORT_SOURCE_FILE, (uintptr_t)FILE_OBJECT(f),
347 NODE_SET_STATE(f, NODE_STATE_ASSOCIATED);
348 NODE_CLE_FLAG(f, NODE_FLAG_STAT_UPDATED);
349 FK_W ("PORT_ASSOCIATE 0x%p OK\n", f);
351 } else if (errno == EAGAIN) {
352 /* Full, remove it. */
353 pn_visible_list = g_list_remove (pn_visible_list, source);
357 } else if (errno == ENOENT) {
358 /* File is not exist */
359 } else if (errno == ENOTSUP) {
360 /* FS is not supported. Currently we think it no longer make sense to
361 * monitor it, so clean the stat info and return 0 to ignore this
362 * node. If there are requirement, we can consider to add polling
365 NODE_CLE_FLAG(f, NODE_FLAG_STAT_UPDATED);
368 FK_W ("PORT_ASSOCIATE 0x%p %s\n", f, g_strerror (errno));
371 /* No matter if associated successfully, stat info is out-of-date, so clean it. */
372 NODE_CLE_FLAG(f, NODE_FLAG_STAT_UPDATED);
380 * Unsafe, need lock fen_lock.
383 port_remove (node_t *f)
385 /* g_assert(f->source); */
387 if (NODE_HAS_STATE(f, NODE_STATE_ASSOCIATED)) {
388 /* Mark unregisted. */
389 if (port_dissociate(PGPFD(f->source)->fd, PORT_SOURCE_FILE, (uintptr_t)FILE_OBJECT(f)) == 0) {
391 * Note, we can run foode_delete if dissociating is failed,
392 * because there may be some pending events (mostly like
393 * FILE_DELETE) in the port_get. If we delete the foode
394 * the fnode may be deleted, then port_get will run on an invalid
397 NODE_CLE_STATE(f, NODE_STATE_ASSOCIATED);
398 FK_W ("PORT_DISSOCIATE 0x%p OK\n", f);
399 } else if (errno == ENOENT) {
400 /* The file has been removed from port, after port_get or before
401 * port_get but DELETED event has been generated.
404 FK_W ("PORT_DISSOCIATE 0x%p %s\n", f, g_strerror (errno));
405 g_return_if_reached();
411 * Get Solaris resouce values.
416 port_class_init (void (*user_process_events_callback) (gpointer, node_event_t*))
420 if ((rblk = malloc (rctlblk_size ())) == NULL) {
421 FK_W ("[kernel] rblk malloc %s\n", g_strerror (errno));
424 if (getrctl ("process.max-port-events", NULL, rblk, RCTL_FIRST) == -1) {
425 FK_W ("[kernel] getrctl %s\n", g_strerror (errno));
429 max_port_events = rctlblk_get_value(rblk);
430 FK_W ("max_port_events = %u\n", max_port_events);
433 renamed_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
435 if (renamed_hash == NULL) {
436 FK_W ("[kernel] FEN global renamed queue initializing faild\n");
439 if ((g_eventq = g_queue_new ()) == NULL) {
440 FK_W ("[kernel] FEN global event queue initializing faild\n");
443 if (user_process_events_callback == NULL) {
444 FK_W ("[kernel] FEN global no user_process_events_callback\n");
447 user_process_events_cb = user_process_events_callback;
448 memset (&zero_wait, 0, sizeof (timespec_t));
450 pevents = g_malloc(PE_ALLOC * sizeof(port_event_t));
451 if (pevents == NULL) {
452 FK_W ("[kernel] FEN global alloc pevents failed\n");
460 printevent (const char *pname, int event, const char *tag)
462 static gchar *event_string = NULL;
466 g_free(event_string);
469 str = g_string_new ("");
470 g_string_printf (str, "[%s] [%-20s]", tag, pname);
471 if (event & FILE_ACCESS) {
472 str = g_string_append (str, " ACCESS");
474 if (event & FILE_MODIFIED) {
475 str = g_string_append (str, " MODIFIED");
477 if (event & FILE_ATTRIB) {
478 str = g_string_append (str, " ATTRIB");
480 if (event & FILE_DELETE) {
481 str = g_string_append (str, " DELETE");
483 if (event & FILE_RENAME_TO) {
484 str = g_string_append (str, " RENAME_TO");
486 if (event & FILE_RENAME_FROM) {
487 str = g_string_append (str, " RENAME_FROM");
489 if (event & UNMOUNTED) {
490 str = g_string_append (str, " UNMOUNTED");
492 if (event & MOUNTEDOVER) {
493 str = g_string_append (str, " MOUNTEDOVER");
495 event_string = str->str;
496 g_string_free (str, FALSE);
501 _event_strings(int event)
503 GString *str = g_string_sized_new(80);
505 if (event & FILE_DELETE)
506 g_string_append(str, " FILE_DELETE");
508 if (event & FILE_RENAME_FROM)
509 g_string_append(str, " FILE_RENAME_FROM");
511 if (event & FILE_MODIFIED)
512 g_string_append(str, " FILE_MODIFIED");
514 if (event & FILE_RENAME_TO)
515 g_string_append(str, " FILE_RENAME_TO");
517 if (event & MOUNTEDOVER)
518 g_string_append(str, " MOUNTEDOVER");
520 if (event & FILE_ATTRIB)
521 g_string_append(str, " FILE_ATTRIB");
523 if (event & UNMOUNTED)
524 g_string_append(str, " UNMOUNTED");
526 if (event & FILE_ACCESS)
527 g_string_append(str, " FILE_ACCESS");
529 return g_string_free(str, FALSE);
533 _event_string (int event)
537 return "FILE_DELETE";
538 case FILE_RENAME_FROM:
539 return "FILE_RENAME_FROM";
541 return "FILE_MODIFIED";
543 return "FILE_RENAME_TO";
545 return "MOUNTEDOVER";
547 return "FILE_ATTRIB";
551 return "FILE_ACCESS";
553 return "EVENT_UNKNOWN";