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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
/* Hurd unionfs
Copyright (C) 2001, 2002 Free Software Foundation, Inc.
Written by Moritz Schulte <moritz@duesseldorf.ccc.de>.
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. */
#define _GNU_SOURCE
#include <hurd/netfs.h>
#include <error.h>
#include <stdlib.h>
#include <assert.h>
#include "ncache.h"
#include "lib.h"
#include "unionfs.h"
/* The node cache. */
ncache_t ncache;
/* Cache size, may be overwritten by the user. */
int ncache_size = NCACHE_SIZE;
/* Initialize the node cache, set the maximum number of allowed nodes
in the cache to SIZE_MAX. */
void
ncache_init (int size_max)
{
ncache.mru = NULL;
ncache.lru = NULL;
ncache.size_max = size_max;
ncache.size_current = 0;
mutex_init (&ncache.lock);
}
/* Remove the given node NODE from the cache. */
static void
ncache_node_remove (node_t *node)
{
struct netnode *nn = node->nn;
if (nn->ncache_next)
nn->ncache_next->nn->ncache_prev = nn->ncache_prev;
if (nn->ncache_prev)
nn->ncache_prev->nn->ncache_next = nn->ncache_next;
if (ncache.mru == node)
ncache.mru = nn->ncache_next;
if (ncache.lru == node)
ncache.lru = nn->ncache_prev;
nn->ncache_next = NULL;
nn->ncache_prev = NULL;
ncache.size_current--;
}
void
ncache_reset (void)
{
node_t *node;
mutex_lock (&ncache.lock);
while ((node = ncache.mru))
ncache_node_remove (node);
mutex_unlock (&ncache.lock);
}
/* Lookup the node for the light node LNODE. If it does not exist
anymore in the cache, create a new node. Store the looked up node
in *NODE. */
error_t
ncache_node_lookup (lnode_t *lnode, node_t **node)
{
error_t err = 0;
node_t *n;
if (lnode->node)
{
debug_msg ("ncache_node_lookup for lnode: %s (found in cache)",
lnode->name);
n = lnode->node;
netfs_nref (n);
}
else
{
debug_msg ("ncache_node_lookup for lnode: %s (newly created)",
lnode->name);
err = node_create (lnode, &n);
}
if (! err)
{
mutex_lock (&n->lock);
*node = n;
}
return err;
}
/* Add the given node NODE to the node cache; remove
least-recently-used nodes, if needed. */
void
ncache_node_add (node_t *node)
{
mutex_lock (&ncache.lock);
debug_msg ("adding node to cache: %s", node->nn->lnode->name);
if (ncache.size_max > 0 || ncache.size_current > 0)
{
if (ncache.mru != node)
{
if (node->nn->ncache_next || node->nn->ncache_prev)
/* Node is already in the cache. */
ncache_node_remove (node);
else
/* Add a reference from the cache. */
netfs_nref (node);
node->nn->ncache_next = ncache.mru;
node->nn->ncache_prev = NULL;
if (ncache.mru)
ncache.mru->nn->ncache_prev = node;
if (! ncache.lru)
ncache.lru = node;
ncache.mru = node;
ncache.size_current++;
}
}
/* Forget the least used nodes. */
while (ncache.size_current > ncache.size_max)
{
struct node *lru = ncache.lru;
debug_msg ("removing cached node: %s", lru->nn->lnode->name);
ncache_node_remove (lru);
netfs_nrele (lru);
}
mutex_unlock (&ncache.lock);
}
|