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 (fk_debug_enabled) g_debug
37 static gboolean fk_debug_enabled = FALSE;
39 #include "gam_error.h"
40 #define FK_W(...) GAM_DEBUG(DEBUG_INFO, __VA_ARGS__)
43 G_GNUC_INTERNAL G_LOCK_DEFINE (fen_lock);
45 static ulong max_port_events = 512;
46 static GList *pn_visible_list; /* the queue of ports which don't have the max objs */
47 static GQueue *g_eventq = NULL;
48 static timespec_t zero_wait;
49 static void (*user_process_events_cb) (gpointer, node_event_t*);
50 static port_event_t *pevents = NULL;
51 static gint PE_ALLOC = 2048;
52 static GHashTable *renamed_hash = NULL; /* <parent node, ev> */
54 typedef struct _PSource {
55 GSource source; /* Inherit from GSource, must be the first. */
58 uint_t event_growing_factor;
59 uint_t pending_events;
62 #define PGPFD(s) (&((PSource *)(s))->gfd)
63 #define SLEEP_BASE_TIME 10 /* in milliseconds */
64 #define EXPECT_INC_EVENTS(pn) (1 << (pn->event_growing_factor))
66 #define RENAME_EVENTS_INTERVAL 500 /* in milliseconds */
67 #define PROCESS_PORT_EVENTS_TIME 1000 /* in milliseconds */
68 guint process_port_event_id = 0;
70 static gchar* _event_strings(int event);
71 static const gchar* _event_string (int event);
72 static GSource *psource_new();
74 static gboolean port_prepare(GSource *source, gint *timeout_);
75 static gboolean port_check(GSource *source);
76 static gboolean port_dispatch(GSource *source, GSourceFunc callback, gpointer user_data);
77 static GSourceFuncs fen_source_func = {
85 port_prepare(GSource *source, gint *timeout_)
91 port_check(GSource *source)
93 PSource *pn = (PSource *)source;
98 g_source_add_poll(source, PGPFD(source));
99 g_source_unref(source);
103 if (!(PGPFD(pn)->revents & G_IO_IN))
106 if (port_getn(PGPFD(source)->fd, NULL, 0, &nget, 0) == 0) {
107 if (nget - pn->pending_events > EXPECT_INC_EVENTS(pn)) {
108 /* Sleep for a while. */
109 pn->pending_events = nget;
110 pn->event_growing_factor ++;
113 g_source_ref(source);
114 g_source_remove_poll(source, PGPFD(source));
115 g_timeout_add(SLEEP_BASE_TIME,
116 (GSourceFunc)port_check,
122 pn->pending_events = 0;
123 pn->event_growing_factor = 0;
129 port_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
135 FK_W ("%s 0x%p fd %d\n", __func__, source, PGPFD(source)->fd);
140 if (port_getn(PGPFD(source)->fd, pevents, PE_ALLOC, &nget, &zero_wait) == 0) {
142 for (i = 0; i < nget; i++) {
143 f = (node_t *)pevents[i].portev_user;
145 if (pevents[i].portev_source == PORT_SOURCE_FILE) {
147 NODE_CLE_STATE(f, NODE_STATE_ASSOCIATED);
148 NODE_SET_STATE(f, NODE_STATE_HAS_EVENTS);
150 if (HAS_NO_EXCEPTION_EVENTS(pevents[i].portev_events)) {
151 /* If the events do not show it's deleted, update
152 * file timestamp to avoid missing events next time.
154 if (node_lstat(f) != 0 /* || port_add(f) != 0 */) {
155 /* Included deleted event. */
156 pevents[i].portev_events |= FILE_DELETE;
160 /* Queue it and waiting for processing. */
161 g_queue_push_tail(g_eventq,
162 node_event_new(pevents[i].portev_events, (gpointer)f));
165 FK_W ("[kernel] unknown portev_source %d\n", pevents[i].portev_source);
172 FK_W ("[kernel] port_getn %s\n", g_strerror (errno));
175 } while (nget == PE_ALLOC);
179 if (total > 0 && callback) {
180 FK_W ("[kernel] get total %ld events\n", total);
181 return callback (user_data);
187 process_renamed_hash_cb(gpointer key, gpointer value, gpointer user_data)
189 node_event_t *ev = value;
192 node_add_event(ev->user_data, ev);
194 user_process_events_cb(ev->user_data, ev);
196 /* Always delete self from hash. */
201 port_events_process_cb(gpointer user_data)
207 /* Processing g_eventq */
208 while ((ev = (node_event_t*)g_queue_pop_head (g_eventq)) != NULL) {
210 /* FK_W ("[%s] 0x%p %s\n", __func__, ev, _event_string (ev->e)); */
213 gchar *log = _event_strings(ev->e);
214 FK_W ("%s %s %s\n", __func__, NODE_NAME(ev->user_data), log);
218 #ifdef GIO_COMPILATION
219 /* Use the parent node as a hash, because only the dir_subs in the
220 * parent node should receive MOVE event.
222 if (NODE_PARENT(ev->user_data)) {
223 if (ev->e == FILE_RENAME_TO) {
224 g_hash_table_insert(renamed_hash, NODE_PARENT(ev->user_data), ev);
225 g_time_val_add(&ev->rename_tv, RENAME_EVENTS_INTERVAL);
228 if (ev->e == FILE_RENAME_FROM) {
229 node_event_t *pair_ev;
231 pair_ev = g_hash_table_lookup(renamed_hash, NODE_PARENT(ev->user_data));
232 if (pair_ev && node_timeval_lt(&ev->ctv, &pair_ev->rename_tv)) {
233 g_hash_table_remove(renamed_hash, NODE_PARENT(ev->user_data));
234 pair_ev->pair_data = ev->user_data;
235 /* Free ev, exchange pair_ev and ev. */
236 node_event_delete(ev);
244 node_add_event(ev->user_data, ev);
246 user_process_events_cb(ev->user_data, ev);
250 /* Processing the events in renamed_hash. TODO we should delay it and wait
251 * for more possible pair.
253 g_hash_table_foreach_remove(renamed_hash, process_renamed_hash_cb, NULL);
257 process_port_event_id = 0;
262 port_events_read_cb(gpointer user_data)
265 if (process_port_event_id == 0) {
266 process_port_event_id = g_timeout_add(PROCESS_PORT_EVENTS_TIME,
267 port_events_process_cb,
275 * malloc PSource and port_create, start thread at pnode_ref.
276 * if psource_new succeeded, the PSource will never
277 * be freed. So PSource can be freed only in psource_new.
278 * Note pnode_monitor_remove_all can also free PSource, but currently no one
284 GSource *source = NULL;
287 if ((fd = port_create()) >= 0) {
288 source = g_source_new(&fen_source_func, sizeof(PSource));
289 PGPFD(source)->fd = fd;
290 PGPFD(source)->events = G_IO_IN | G_IO_HUP | G_IO_ERR;
291 g_source_set_callback(source, port_events_read_cb, NULL, NULL);
292 g_source_attach(source, NULL);
293 g_source_unref(source);
294 g_source_add_poll(source, PGPFD(source));
296 FK_W ("%s 0x%p fd %d\n", __func__, source, PGPFD(source)->fd);
298 FK_W ("PORT_CREATE %s\n", g_strerror(errno));
299 g_return_val_if_reached(NULL);
309 * Unsafe, need lock fen_lock.
310 * port_add will associate a GSource to @f->source
315 GSource *source = f->source;
317 FK_W ("%s [0x%p] %s\n", __func__, f, NODE_NAME(f));
320 g_assert(NODE_HAS_FLAG(f, NODE_FLAG_STAT_UPDATED));
322 /* if (!NODE_HAS_FLAG(f, NODE_FLAG_STAT_DONE)) { */
323 /* if (NODE_STAT(f) != 0) { */
328 /* Try re-use f->pn. f->pn may be used by other request, e.g. f is deleted
329 * for a long time. So if pn is full, we try to open a new one.
333 /* Try the next visible source. */
334 if (pn_visible_list) {
335 source = (GSource *)pn_visible_list->data;
337 if ((source = psource_new()) != NULL) {
338 g_assert (g_list_find (pn_visible_list, source) == NULL);
339 pn_visible_list = g_list_prepend (pn_visible_list, source);
344 if (port_associate(PGPFD(source)->fd, PORT_SOURCE_FILE, (uintptr_t)FILE_OBJECT(f),
348 NODE_SET_STATE(f, NODE_STATE_ASSOCIATED);
349 NODE_CLE_FLAG(f, NODE_FLAG_STAT_UPDATED);
350 FK_W ("PORT_ASSOCIATE 0x%p OK\n", f);
352 } else if (errno == EAGAIN) {
353 /* Full, remove it. */
354 pn_visible_list = g_list_remove (pn_visible_list, source);
358 } else if (errno == ENOENT) {
359 /* File is not exist */
360 } else if (errno == ENOTSUP) {
361 /* FS is not supported. Currently we think it no longer make sense to
362 * monitor it, so clean the stat info and return 0 to ignore this
363 * node. If there are requirement, we can consider to add polling
366 NODE_CLE_FLAG(f, NODE_FLAG_STAT_UPDATED);
369 FK_W ("PORT_ASSOCIATE 0x%p %s\n", f, g_strerror (errno));
372 /* No matter if associated successfully, stat info is out-of-date, so clean it. */
373 NODE_CLE_FLAG(f, NODE_FLAG_STAT_UPDATED);
381 * Unsafe, need lock fen_lock.
384 port_remove (node_t *f)
386 /* g_assert(f->source); */
388 if (NODE_HAS_STATE(f, NODE_STATE_ASSOCIATED)) {
389 /* Mark unregisted. */
390 if (port_dissociate(PGPFD(f->source)->fd, PORT_SOURCE_FILE, (uintptr_t)FILE_OBJECT(f)) == 0) {
392 * Note, we can run foode_delete if dissociating is failed,
393 * because there may be some pending events (mostly like
394 * FILE_DELETE) in the port_get. If we delete the foode
395 * the fnode may be deleted, then port_get will run on an invalid
398 NODE_CLE_STATE(f, NODE_STATE_ASSOCIATED);
399 FK_W ("PORT_DISSOCIATE 0x%p OK\n", f);
400 } else if (errno == ENOENT) {
401 /* The file has been removed from port, after port_get or before
402 * port_get but DELETED event has been generated.
405 FK_W ("PORT_DISSOCIATE 0x%p %s\n", f, g_strerror (errno));
406 g_return_if_reached();
412 * Get Solaris resouce values.
417 port_class_init (void (*user_process_events_callback) (gpointer, node_event_t*))
421 if ((rblk = malloc (rctlblk_size ())) == NULL) {
422 FK_W ("[kernel] rblk malloc %s\n", g_strerror (errno));
425 if (getrctl ("process.max-port-events", NULL, rblk, RCTL_FIRST) == -1) {
426 FK_W ("[kernel] getrctl %s\n", g_strerror (errno));
430 max_port_events = rctlblk_get_value(rblk);
431 FK_W ("max_port_events = %u\n", max_port_events);
434 renamed_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
436 if (renamed_hash == NULL) {
437 FK_W ("[kernel] FEN global renamed queue initializing faild\n");
440 if ((g_eventq = g_queue_new ()) == NULL) {
441 FK_W ("[kernel] FEN global event queue initializing faild\n");
444 if (user_process_events_callback == NULL) {
445 FK_W ("[kernel] FEN global no user_process_events_callback\n");
448 user_process_events_cb = user_process_events_callback;
449 memset (&zero_wait, 0, sizeof (timespec_t));
451 pevents = g_malloc(PE_ALLOC * sizeof(port_event_t));
452 if (pevents == NULL) {
453 FK_W ("[kernel] FEN global alloc pevents failed\n");
461 printevent (const char *pname, int event, const char *tag)
463 static gchar *event_string = NULL;
467 g_free(event_string);
470 str = g_string_new ("");
471 g_string_printf (str, "[%s] [%-20s]", tag, pname);
472 if (event & FILE_ACCESS) {
473 str = g_string_append (str, " ACCESS");
475 if (event & FILE_MODIFIED) {
476 str = g_string_append (str, " MODIFIED");
478 if (event & FILE_ATTRIB) {
479 str = g_string_append (str, " ATTRIB");
481 if (event & FILE_DELETE) {
482 str = g_string_append (str, " DELETE");
484 if (event & FILE_RENAME_TO) {
485 str = g_string_append (str, " RENAME_TO");
487 if (event & FILE_RENAME_FROM) {
488 str = g_string_append (str, " RENAME_FROM");
490 if (event & UNMOUNTED) {
491 str = g_string_append (str, " UNMOUNTED");
493 if (event & MOUNTEDOVER) {
494 str = g_string_append (str, " MOUNTEDOVER");
496 event_string = str->str;
497 g_string_free (str, FALSE);
502 _event_strings(int event)
504 GString *str = g_string_sized_new(80);
506 if (event & FILE_DELETE)
507 g_string_append(str, " FILE_DELETE");
509 if (event & FILE_RENAME_FROM)
510 g_string_append(str, " FILE_RENAME_FROM");
512 if (event & FILE_MODIFIED)
513 g_string_append(str, " FILE_MODIFIED");
515 if (event & FILE_RENAME_TO)
516 g_string_append(str, " FILE_RENAME_TO");
518 if (event & MOUNTEDOVER)
519 g_string_append(str, " MOUNTEDOVER");
521 if (event & FILE_ATTRIB)
522 g_string_append(str, " FILE_ATTRIB");
524 if (event & UNMOUNTED)
525 g_string_append(str, " UNMOUNTED");
527 if (event & FILE_ACCESS)
528 g_string_append(str, " FILE_ACCESS");
530 return g_string_free(str, FALSE);
534 _event_string (int event)
538 return "FILE_DELETE";
539 case FILE_RENAME_FROM:
540 return "FILE_RENAME_FROM";
542 return "FILE_MODIFIED";
544 return "FILE_RENAME_TO";
546 return "MOUNTEDOVER";
548 return "FILE_ATTRIB";
552 return "FILE_ACCESS";
554 return "EVENT_UNKNOWN";