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 Sun Microsystem.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Lin Ma <lin.ma@sun.com>
32 #include "fen-kernel.h"
35 #define FK_W if (fk_debug_enabled) g_warning
36 static gboolean fk_debug_enabled = FALSE;
38 G_GNUC_INTERNAL G_LOCK_DEFINE (fen_lock);
40 #define F_PORT(pp) (((_f *)(fo))->port->port)
41 #define F_NAME(pp) (((_f *)(fo))->fobj->fo_name)
42 #define FEN_ALL_EVENTS (FILE_MODIFIED | FILE_ATTRIB | FILE_NOFOLLOW)
43 #define FEN_IGNORE_EVENTS (FILE_ACCESS)
44 #define PROCESS_PORT_EVENTS_TIME 400 /* in milliseconds */
46 static GHashTable *_obj_fen_hash = NULL; /* <user_data, port> */
47 static ulong max_port_events = 512;
48 static GList *pn_vq; /* the queue of ports which don't have the max objs */
49 static GList *pn_fq; /* the queue of ports which have the max objs */
50 static GQueue *g_eventq = NULL;
51 static void (*add_event_cb) (gpointer, fnode_event_t*);
55 long ref; /* how many fds are associated to this port */
68 static gboolean port_fetch_event_cb (void *arg);
69 static pnode_t *pnode_new ();
70 static void pnode_delete (pnode_t *pn);
73 is_ported (gpointer f)
75 _f* fo = g_hash_table_lookup (_obj_fen_hash, f);
84 printevent (const char *pname, int event, const char *tag)
87 str = g_string_new ("");
89 g_string_printf (str, "[%s] [%-20s]", tag, pname);
90 if (event & FILE_ACCESS) {
91 str = g_string_append (str, " ACCESS");
93 if (event & FILE_MODIFIED) {
94 str = g_string_append (str, " MODIFIED");
96 if (event & FILE_ATTRIB) {
97 str = g_string_append (str, " ATTRIB");
99 if (event & FILE_DELETE) {
100 str = g_string_append (str, " DELETE");
102 if (event & FILE_RENAME_TO) {
103 str = g_string_append (str, " RENAME_TO");
105 if (event & FILE_RENAME_FROM) {
106 str = g_string_append (str, " RENAME_FROM");
108 if (event & UNMOUNTED) {
109 str = g_string_append (str, " UNMOUNTED");
111 if (event & MOUNTEDOVER) {
112 str = g_string_append (str, " MOUNTEDOVER");
115 FK_W ("%s\n", str->str);
116 g_string_free (str, TRUE);
120 port_add_kevent (int e, gpointer f)
122 fnode_event_t *ev, *tail;
124 gboolean has_twin = FALSE;
126 /* printevent (F_NAME(f), e, "org"); */
128 * Child FILE_DELETE | FILE_RENAME_FROM will trigger parent FILE_MODIFIED.
129 * FILE_MODIFIED will trigger FILE_ATTRIB.
132 if ((e & FILE_ATTRIB) && e != FILE_ATTRIB) {
136 if (e == FILE_RENAME_FROM) {
139 if (e == FILE_RENAME_TO) {
145 case FILE_RENAME_FROM:
154 g_assert_not_reached ();
158 tail = (fnode_event_t*) g_queue_peek_tail (g_eventq);
160 if (tail->user_data == f) {
162 tail->has_twin = (has_twin | (tail->has_twin ^ has_twin));
163 /* skip the current */
165 } else if (e == FILE_MODIFIED && !has_twin
166 && tail->e == FILE_ATTRIB) {
167 tail->e = FILE_MODIFIED;
168 tail->has_twin = TRUE;
170 } else if (e == FILE_ATTRIB
171 && tail->e == FILE_MODIFIED && !tail->has_twin) {
172 tail->has_twin = TRUE;
178 if ((ev = fnode_event_new (e, has_twin, f)) != NULL) {
179 g_queue_push_tail (g_eventq, ev);
184 port_process_kevents ()
188 while ((ev = (fnode_event_t*)g_queue_pop_head (g_eventq)) != NULL) {
189 FK_W ("[%s] 0x%p %s\n", __func__, ev, _event_string (ev->e));
190 add_event_cb (ev->user_data, ev);
195 port_fetch_event_cb (void *arg)
197 pnode_t *pn = (pnode_t *)arg;
200 port_event_t pe[PE_ALLOC];
205 /* FK_W ("IN <======== %s\n", __func__); */
208 memset (&timeout, 0, sizeof (timespec_t));
211 if (port_getn (pn->port, pe, PE_ALLOC, &nget, &timeout) == 0) {
213 for (i = 0; i < nget; i++) {
214 fo = (_f*)pe[i].portev_user;
216 switch (pe[i].portev_source) {
217 case PORT_SOURCE_FILE:
218 /* If got FILE_EXCEPTION or add to port failed,
220 fo->is_active = FALSE;
222 printevent (F_NAME(fo), pe[i].portev_events, "RAW");
223 port_add_kevent (pe[i].portev_events, fo->user_data);
225 /* fnode is deleted */
228 if (pe[i].portev_events & FILE_EXCEPTION) {
229 g_hash_table_remove (_obj_fen_hash, fo->user_data);
231 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
232 pnode_delete (fo->port);
237 /* case PORT_SOURCE_TIMER: */
238 FK_W ("[kernel] unknown portev_source %d\n", pe[i].portev_source);
242 FK_W ("[kernel] port_getn %s\n", g_strerror (errno));
245 } while (nget == PE_ALLOC);
247 /* Processing g_eventq */
248 port_process_kevents ();
251 pn->port_source_id = 0;
255 /* FK_W ("OUT ========> %s\n", __func__); */
260 * ref - 1 if remove a watching file succeeded.
263 pnode_delete (pnode_t *pn)
265 g_assert (pn->ref <= max_port_events);
267 if (pn->ref == max_port_events) {
268 FK_W ("PORT : move to visible queue - [pn] 0x%p [ref] %d\n", pn, pn->ref);
269 pn_fq = g_list_remove (pn_fq, pn);
270 pn_vq = g_list_prepend (pn_vq, pn);
272 if ((-- pn->ref) == 0) {
273 /* Should dispatch the source */
275 FK_W ("%s [pn] 0x%p [ref] %d\n", __func__, pn, pn->ref);
279 * malloc pnode_t and port_create, start thread at pnode_ref.
280 * if pnode_new succeeded, the pnode_t will never
281 * be freed. So pnode_t can be freed only in pnode_new.
282 * Note pnode_monitor_remove_all can also free pnode_t, but currently no one
291 pn = (pnode_t*)pn_vq->data;
292 g_assert (pn->ref < max_port_events);
294 pn = g_new0 (pnode_t, 1);
296 if ((pn->port = port_create ()) >= 0) {
297 g_assert (g_list_find (pn_vq, pn) == NULL);
298 pn_vq = g_list_prepend (pn_vq, pn);
300 FK_W ("PORT_CREATE %s\n", g_strerror (errno));
307 FK_W ("%s [pn] 0x%p [ref] %d\n", __func__, pn, pn->ref);
309 if (pn->ref == max_port_events) {
310 FK_W ("PORT : move to full queue - [pn] 0x%p [ref] %d\n", pn, pn->ref);
311 pn_vq = g_list_remove (pn_vq, pn);
312 pn_fq = g_list_prepend (pn_fq, pn);
313 g_assert (g_list_find (pn_vq, pn) == NULL);
315 /* attach the source */
316 if (pn->port_source_id == 0) {
317 pn->port_source_id = g_timeout_add (PROCESS_PORT_EVENTS_TIME,
320 g_assert (pn->port_source_id > 0);
331 * Unsafe, need lock fen_lock.
334 port_add_internal (file_obj_t* fobj, off_t* len,
335 gpointer f, gboolean need_stat)
341 g_assert (f && fobj);
342 FK_W ("%s [0x%p] %s\n", __func__, f, fobj->fo_name);
344 if ((fo = g_hash_table_lookup (_obj_fen_hash, f)) == NULL) {
349 FK_W ("[ NEW_FO ] [0x%p] %s\n", fo, F_NAME(fo));
350 g_hash_table_insert (_obj_fen_hash, f, fo);
357 if (fo->port == NULL) {
358 fo->port = pnode_new ();
362 if (FN_STAT (F_NAME(fo), &buf) != 0) {
363 FK_W ("LSTAT [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
367 fo->fobj->fo_atime = buf.st_atim;
368 fo->fobj->fo_mtime = buf.st_mtim;
369 fo->fobj->fo_ctime = buf.st_ctim;
373 if (port_associate (F_PORT(fo),
378 fo->is_active = TRUE;
379 FK_W ("%s %s\n", "PORT_ASSOCIATE", F_NAME(fo));
382 FK_W ("PORT_ASSOCIATE [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
384 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
385 g_hash_table_remove (_obj_fen_hash, f);
386 pnode_delete (fo->port);
393 port_add (file_obj_t* fobj, off_t* len, gpointer f)
395 return port_add_internal (fobj, len, f, TRUE);
399 port_add_simple (file_obj_t* fobj, gpointer f)
401 return port_add_internal (fobj, NULL, f, FALSE);
408 * Unsafe, need lock fen_lock.
411 port_remove (gpointer f)
415 FK_W ("%s\n", __func__);
416 if ((fo = g_hash_table_lookup (_obj_fen_hash, f)) != NULL) {
418 fo->user_data = NULL;
419 g_hash_table_remove (_obj_fen_hash, f);
421 if (port_dissociate (F_PORT(fo),
423 (uintptr_t)fo->fobj) == 0) {
425 * Note, we can run foode_delete if dissociating is failed,
426 * because there may be some pending events (mostly like
427 * FILE_DELETE) in the port_get. If we delete the foode
428 * the fnode may be deleted, then port_get will run on an invalid
431 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
432 pnode_delete (fo->port);
435 FK_W ("PORT_DISSOCIATE [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
441 _event_string (int event)
445 return "FILE_DELETE";
446 case FILE_RENAME_FROM:
447 return "FILE_RENAME_FROM";
449 return "FILE_MODIFIED";
451 return "FILE_RENAME_TO";
453 return "MOUNTEDOVER";
455 return "FILE_ATTRIB";
459 return "FILE_ACCESS";
461 return "EVENT_UNKNOWN";
466 * Get Solaris resouce values.
471 port_class_init (void (*user_add_event) (gpointer, fnode_event_t*))
474 FK_W ("%s\n", __func__);
475 if ((rblk = malloc (rctlblk_size ())) == NULL) {
476 FK_W ("[kernel] rblk malloc %s\n", g_strerror (errno));
479 if (getrctl ("process.max-port-events", NULL, rblk, RCTL_FIRST) == -1) {
480 FK_W ("[kernel] getrctl %s\n", g_strerror (errno));
484 max_port_events = rctlblk_get_value(rblk);
485 FK_W ("[kernel] max_port_events = %u\n", max_port_events);
488 if ((_obj_fen_hash = g_hash_table_new(g_direct_hash,
489 g_direct_equal)) == NULL) {
490 FK_W ("[kernel] fobj hash initializing faild\n");
493 if ((g_eventq = g_queue_new ()) == NULL) {
494 FK_W ("[kernel] FEN global event queue initializing faild\n");
496 if (user_add_event == NULL) {
499 add_event_cb = user_add_event;
504 fnode_event_new (int event, gboolean has_twin, gpointer user_data)
508 if ((ev = g_new (fnode_event_t, 1)) != NULL) {
511 ev->user_data = user_data;
512 ev->has_twin = has_twin;
513 /* Default isn't a pending event. */
514 ev->is_pending = FALSE;
520 fnode_event_delete (fnode_event_t* ev)