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 Microsystems, Inc. All rights reserved.
5 * Use is subject to license terms.
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>
33 #include "fen-kernel.h"
36 #define FK_W if (fk_debug_enabled) g_warning
37 #ifdef GIO_COMPILATION
38 static gboolean fk_debug_enabled = FALSE;
40 static gboolean fk_debug_enabled = TRUE;
43 G_GNUC_INTERNAL G_LOCK_DEFINE (fen_lock);
45 #define F_PORT(pp) (((_f *)(fo))->port->port)
46 #define F_NAME(pp) (((_f *)(fo))->fobj->fo_name)
47 #define FEN_ALL_EVENTS (FILE_MODIFIED | FILE_ATTRIB | FILE_NOFOLLOW)
48 #define FEN_IGNORE_EVENTS (FILE_ACCESS)
49 #define PROCESS_PORT_EVENTS_TIME 400 /* in milliseconds */
51 static GHashTable *_obj_fen_hash = NULL; /* <user_data, port> */
52 static ulong max_port_events = 512;
53 static GList *pn_vq; /* the queue of ports which don't have the max objs */
54 static GList *pn_fq; /* the queue of ports which have the max objs */
55 static GQueue *g_eventq = NULL;
56 static void (*add_event_cb) (gpointer, fnode_event_t*);
60 long ref; /* how many fds are associated to this port */
73 static gboolean port_fetch_event_cb (void *arg);
74 static pnode_t *pnode_new ();
75 static void pnode_delete (pnode_t *pn);
78 is_ported (gpointer f)
80 _f* fo = g_hash_table_lookup (_obj_fen_hash, f);
89 printevent (const char *pname, int event, const char *tag)
92 str = g_string_new ("");
94 g_string_printf (str, "[%s] [%-20s]", tag, pname);
95 if (event & FILE_ACCESS) {
96 str = g_string_append (str, " ACCESS");
98 if (event & FILE_MODIFIED) {
99 str = g_string_append (str, " MODIFIED");
101 if (event & FILE_ATTRIB) {
102 str = g_string_append (str, " ATTRIB");
104 if (event & FILE_DELETE) {
105 str = g_string_append (str, " DELETE");
107 if (event & FILE_RENAME_TO) {
108 str = g_string_append (str, " RENAME_TO");
110 if (event & FILE_RENAME_FROM) {
111 str = g_string_append (str, " RENAME_FROM");
113 if (event & UNMOUNTED) {
114 str = g_string_append (str, " UNMOUNTED");
116 if (event & MOUNTEDOVER) {
117 str = g_string_append (str, " MOUNTEDOVER");
120 FK_W ("%s\n", str->str);
121 g_string_free (str, TRUE);
125 port_add_kevent (int e, gpointer f)
127 fnode_event_t *ev, *tail;
129 gboolean has_twin = FALSE;
131 /* printevent (F_NAME(f), e, "org"); */
133 * Child FILE_DELETE | FILE_RENAME_FROM will trigger parent FILE_MODIFIED.
134 * FILE_MODIFIED will trigger FILE_ATTRIB.
137 if ((e & FILE_ATTRIB) && e != FILE_ATTRIB) {
141 if (e == FILE_RENAME_FROM) {
144 if (e == FILE_RENAME_TO) {
150 case FILE_RENAME_FROM:
159 g_assert_not_reached ();
163 tail = (fnode_event_t*) g_queue_peek_tail (g_eventq);
165 if (tail->user_data == f) {
167 tail->has_twin = (has_twin | (tail->has_twin ^ has_twin));
168 /* skip the current */
170 } else if (e == FILE_MODIFIED && !has_twin
171 && tail->e == FILE_ATTRIB) {
172 tail->e = FILE_MODIFIED;
173 tail->has_twin = TRUE;
175 } else if (e == FILE_ATTRIB
176 && tail->e == FILE_MODIFIED && !tail->has_twin) {
177 tail->has_twin = TRUE;
183 if ((ev = fnode_event_new (e, has_twin, f)) != NULL) {
184 g_queue_push_tail (g_eventq, ev);
189 port_process_kevents ()
193 while ((ev = (fnode_event_t*)g_queue_pop_head (g_eventq)) != NULL) {
194 FK_W ("[%s] 0x%p %s\n", __func__, ev, _event_string (ev->e));
195 add_event_cb (ev->user_data, ev);
200 port_fetch_event_cb (void *arg)
202 pnode_t *pn = (pnode_t *)arg;
205 port_event_t pe[PE_ALLOC];
210 /* FK_W ("IN <======== %s\n", __func__); */
213 memset (&timeout, 0, sizeof (timespec_t));
216 if (port_getn (pn->port, pe, PE_ALLOC, &nget, &timeout) == 0) {
218 for (i = 0; i < nget; i++) {
219 fo = (_f*)pe[i].portev_user;
221 switch (pe[i].portev_source) {
222 case PORT_SOURCE_FILE:
223 /* If got FILE_EXCEPTION or add to port failed,
225 fo->is_active = FALSE;
227 printevent (F_NAME(fo), pe[i].portev_events, "RAW");
228 port_add_kevent (pe[i].portev_events, fo->user_data);
230 /* fnode is deleted */
233 if (pe[i].portev_events & FILE_EXCEPTION) {
234 g_hash_table_remove (_obj_fen_hash, fo->user_data);
236 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
237 pnode_delete (fo->port);
242 /* case PORT_SOURCE_TIMER: */
243 FK_W ("[kernel] unknown portev_source %d\n", pe[i].portev_source);
247 FK_W ("[kernel] port_getn %s\n", g_strerror (errno));
250 } while (nget == PE_ALLOC);
252 /* Processing g_eventq */
253 port_process_kevents ();
256 pn->port_source_id = 0;
260 /* FK_W ("OUT ========> %s\n", __func__); */
265 * ref - 1 if remove a watching file succeeded.
268 pnode_delete (pnode_t *pn)
270 g_assert (pn->ref <= max_port_events);
272 if (pn->ref == max_port_events) {
273 FK_W ("PORT : move to visible queue - [pn] 0x%p [ref] %d\n", pn, pn->ref);
274 pn_fq = g_list_remove (pn_fq, pn);
275 pn_vq = g_list_prepend (pn_vq, pn);
277 if ((-- pn->ref) == 0) {
278 /* Should dispatch the source */
280 FK_W ("%s [pn] 0x%p [ref] %d\n", __func__, pn, pn->ref);
284 * malloc pnode_t and port_create, start thread at pnode_ref.
285 * if pnode_new succeeded, the pnode_t will never
286 * be freed. So pnode_t can be freed only in pnode_new.
287 * Note pnode_monitor_remove_all can also free pnode_t, but currently no one
296 pn = (pnode_t*)pn_vq->data;
297 g_assert (pn->ref < max_port_events);
299 pn = g_new0 (pnode_t, 1);
301 if ((pn->port = port_create ()) >= 0) {
302 g_assert (g_list_find (pn_vq, pn) == NULL);
303 pn_vq = g_list_prepend (pn_vq, pn);
305 FK_W ("PORT_CREATE %s\n", g_strerror (errno));
312 FK_W ("%s [pn] 0x%p [ref] %d\n", __func__, pn, pn->ref);
314 if (pn->ref == max_port_events) {
315 FK_W ("PORT : move to full queue - [pn] 0x%p [ref] %d\n", pn, pn->ref);
316 pn_vq = g_list_remove (pn_vq, pn);
317 pn_fq = g_list_prepend (pn_fq, pn);
318 g_assert (g_list_find (pn_vq, pn) == NULL);
320 /* attach the source */
321 if (pn->port_source_id == 0) {
322 pn->port_source_id = g_timeout_add (PROCESS_PORT_EVENTS_TIME,
325 g_assert (pn->port_source_id > 0);
336 * Unsafe, need lock fen_lock.
339 port_add_internal (file_obj_t* fobj, off_t* len,
340 gpointer f, gboolean need_stat)
346 g_assert (f && fobj);
347 FK_W ("%s [0x%p] %s\n", __func__, f, fobj->fo_name);
349 if ((fo = g_hash_table_lookup (_obj_fen_hash, f)) == NULL) {
354 FK_W ("[ NEW_FO ] [0x%p] %s\n", fo, F_NAME(fo));
355 g_hash_table_insert (_obj_fen_hash, f, fo);
362 if (fo->port == NULL) {
363 fo->port = pnode_new ();
367 if (FN_STAT (F_NAME(fo), &buf) != 0) {
368 FK_W ("LSTAT [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
372 fo->fobj->fo_atime = buf.st_atim;
373 fo->fobj->fo_mtime = buf.st_mtim;
374 fo->fobj->fo_ctime = buf.st_ctim;
378 if (port_associate (F_PORT(fo),
383 fo->is_active = TRUE;
384 FK_W ("%s %s\n", "PORT_ASSOCIATE", F_NAME(fo));
387 FK_W ("PORT_ASSOCIATE [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
389 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
390 g_hash_table_remove (_obj_fen_hash, f);
391 pnode_delete (fo->port);
398 port_add (file_obj_t* fobj, off_t* len, gpointer f)
400 return port_add_internal (fobj, len, f, TRUE);
404 port_add_simple (file_obj_t* fobj, gpointer f)
406 return port_add_internal (fobj, NULL, f, FALSE);
413 * Unsafe, need lock fen_lock.
416 port_remove (gpointer f)
420 FK_W ("%s\n", __func__);
421 if ((fo = g_hash_table_lookup (_obj_fen_hash, f)) != NULL) {
423 fo->user_data = NULL;
424 g_hash_table_remove (_obj_fen_hash, f);
426 if (port_dissociate (F_PORT(fo),
428 (uintptr_t)fo->fobj) == 0) {
430 * Note, we can run foode_delete if dissociating is failed,
431 * because there may be some pending events (mostly like
432 * FILE_DELETE) in the port_get. If we delete the foode
433 * the fnode may be deleted, then port_get will run on an invalid
436 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
437 pnode_delete (fo->port);
440 FK_W ("PORT_DISSOCIATE [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
446 _event_string (int event)
450 return "FILE_DELETE";
451 case FILE_RENAME_FROM:
452 return "FILE_RENAME_FROM";
454 return "FILE_MODIFIED";
456 return "FILE_RENAME_TO";
458 return "MOUNTEDOVER";
460 return "FILE_ATTRIB";
464 return "FILE_ACCESS";
466 return "EVENT_UNKNOWN";
471 * Get Solaris resouce values.
476 port_class_init (void (*user_add_event) (gpointer, fnode_event_t*))
479 FK_W ("%s\n", __func__);
480 if ((rblk = malloc (rctlblk_size ())) == NULL) {
481 FK_W ("[kernel] rblk malloc %s\n", g_strerror (errno));
484 if (getrctl ("process.max-port-events", NULL, rblk, RCTL_FIRST) == -1) {
485 FK_W ("[kernel] getrctl %s\n", g_strerror (errno));
489 max_port_events = rctlblk_get_value(rblk);
490 FK_W ("[kernel] max_port_events = %u\n", max_port_events);
493 if ((_obj_fen_hash = g_hash_table_new(g_direct_hash,
494 g_direct_equal)) == NULL) {
495 FK_W ("[kernel] fobj hash initializing faild\n");
498 if ((g_eventq = g_queue_new ()) == NULL) {
499 FK_W ("[kernel] FEN global event queue initializing faild\n");
501 if (user_add_event == NULL) {
504 add_event_cb = user_add_event;
509 fnode_event_new (int event, gboolean has_twin, gpointer user_data)
513 if ((ev = g_new (fnode_event_t, 1)) != NULL) {
516 ev->user_data = user_data;
517 ev->has_twin = has_twin;
518 /* Default isn't a pending event. */
519 ev->is_pending = FALSE;
525 fnode_event_delete (fnode_event_t* ev)