summaryrefslogtreecommitdiff
path: root/libdiskfs/name-cache.c
blob: a07e04641cdc3d1b0821e6d7d037b172668dc291 (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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Directory name lookup caching
   Copyright (C) 1996 Free Software Foundation, Inc.
   Written by Michael I. Bushnell, p/BSG.

   This file is part of the GNU Hurd.

   The GNU Hurd 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, or (at
   your option) any later version.

   The GNU Hurd 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, USA. */


#include "priv.h"
#include "name-cache.h"

/* Maximum number of names to cache at once */
#define MAXCACHE 256

/* Current number of names cached */
static int cache_cur;

/* Cache entry */
struct lookup_cache
{
  struct lookup_cache *next, *prev;
  
  /* These do not hold references; special code exists to call
     purge routines when the nodes lose refs. */
  struct node *dp;		/* directory */
  struct node *np;		/* node */

  /* strlen of name member */
  size_t namelen;
  char name[0];
};

/* Cache itself */
static struct lookup_cache *lookup_cache_head, *lookup_cache_tail;

/* Node NP has just been found in DIR with NAME.  If NP is null, that
   means that this name has been confirmed as absent in the directory. */
void
diskfs_enter_cache (struct node *dir, struct node *np, char *name)
{
  struct lookup_cache *lc, *tmp = 0;
  size_t len = strlen (name) + 1;
  
  
  lc = malloc (sizeof (struct lookup_cache) + len);
  lc->dp = dir;
  lc->np = np;
  lc->namelen = len - 1;
  bcopy (name, lc->name, len);

  spin_lock (&diskfs_node_refcnt_lock);
  if (cache_cur == MAXCACHE)
    {
      /* Free the entry at the tail */
      tmp = lookup_cache_tail;
      lookup_cache_tail = lookup_cache_tail->prev;
      if (lookup_cache_tail)
	lookup_cache_tail->next = 0;
      else
	lookup_cache_head = 0;
    }
  else
    cache_cur++;
  
  /* Add the new entry at the front */
  lc->next = lookup_cache_head;
  lc->prev = 0;
  lc->next->prev = lc;
  lookup_cache_head = lc;
  if (!lc->next)
    lookup_cache_tail = lc;
  
  spin_unlock (&diskfs_node_refcnt_lock);
  
  if (tmp)
    free (tmp);
}

/* Purge all references in the cache to NP as a node inside a directory */
void
diskfs_purge_cache_node (struct node *np)
{
  struct lookup_cache *lc, *nxt;
  
  spin_lock (&diskfs_node_refcnt_lock);
  for (lc = lookup_cache_head; lc; lc = nxt)
    if (lc->np == np)
      {
	if (lc->prev)
	  lc->prev->next = lc->next;
	if (lc->next)
	  lc->next->prev = lc->prev;
	nxt = lc->next;
	free (lc);
      }
    else
      nxt = lc->next;
  spin_unlock (&diskfs_node_refcnt_lock);
}

/* Purge all references in the cache to NP, either as a node or as a
   directory.  diskfs_node_refcnt_lock must be held around this call. */
void 
_diskfs_purge_cache (struct node *np)
{
  struct lookup_cache *lc, *nxt;
  
  for (lc = lookup_cache_head; lc; lc = nxt)
    if (lc->np == np || lc->dp == np)
      {
	if (lc->prev)
	  lc->prev->next = lc->next;
	if (lc->next)
	  lc->next->prev = lc->prev;
	nxt = lc->next;
	free (lc);
      }
    else
      nxt = lc->next;
}

/* Scan the cache looking for NAME inside DIR.  If we don't know
   anything entry at all, then return 0.  If the entry is confirmed to
   not exist, then return -1.  Otherwise, return NP for the entry, with
   a newly allocated reference. */
struct node *
diskfs_check_cache (struct node *dir, char *name)
{
  size_t len = strlen (name);
  
  spin_lock (&diskfs_node_refcnt_lock);
  for (lc = lookup_cache_head; lc; lc = lc->next)
    if (lc->dp == dir
	&& lc->namelen == len
	&& lc->name[0] == name[0]
	&& !strcmp (lc->name, name))
      {
	if (lc->np)
	  {
	    rp->refereces++;
	    spin_unlock (&diskfs_node_refcnt_lock);
	    return np;
	  }
	else
	  {
	    spin_unlock (&diskfs_node_refcnt_lock);
	    return (struct node *) -1;
	  }
      }
  spin_unlock (&diskfs_node_refcnt_lock);
  return 0;
}