summaryrefslogtreecommitdiff
path: root/unionfs/update.c
blob: 8ec66883aea586cacdf87a90fe0ee02e1eff564d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Hurd unionfs
   Copyright (C) 2005 Free Software Foundation, Inc.
   Written by Gianluca Guida <glguida@gmail.com>.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or * (at your option) any later version.
 
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA.  */

/* Update thread: A clean way to solve locking issues of 
   root node update.  */

#define _GNU_SOURCE

#include <errno.h>
#include <string.h>
#include <cthreads.h>
#include <rwlock.h>

#include "ncache.h"
#include "node.h"
#include "ulfs.h"

/* Reader lock is used by threads that are going to
   add/remove an ulfs; writer lock is hold by the 
   update thread.  */
static struct rwlock update_rwlock;
static struct condition update_wakeup;
static struct mutex update_lock;

static void
_root_update_thread ()
{
  error_t err;
  
  while (1)
    {
      if (hurd_condition_wait (&update_wakeup, &update_lock))
	mutex_unlock (&update_lock);

      rwlock_writer_lock (&update_rwlock);

      do 
	{
	  ulfs_check();
	  err = node_init_root (netfs_root_node);
	}
      while (err == ENOENT);

      if (err)
	{
	  fprintf (stderr, "update thread: got a %s\n", strerror (err));
	}

      ncache_reset ();

      rwlock_writer_unlock (&update_rwlock);
    }
}

void
root_update_schedule ()
{
  condition_signal (&update_wakeup);
}

void
root_update_disable ()
{
  rwlock_reader_lock (&update_rwlock);
}

void
root_update_enable ()
{
  rwlock_reader_unlock (&update_rwlock);
}

void
root_update_init()
{
  mutex_init (&update_lock);
  rwlock_init (&update_rwlock);
  condition_init (&update_wakeup);

  cthread_detach (cthread_fork ( (cthread_fn_t)_root_update_thread, 0));
}