summaryrefslogtreecommitdiff
path: root/ftpfs/ncache.c
blob: 612dc081a0fccbbd1a1bd9e5a5b89454393b1ee7 (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
/* Node caching

   Copyright (C) 1997 Free Software Foundation, Inc.
   Written by Miles Bader <miles@gnu.ai.mit.edu>
   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 <unistd.h>
#include <string.h>

#include <hurd/netfs.h>

#include "ftpfs.h"

/* Remove NN's node from its position in FS's node cache.  */
static void
node_unlink (struct node *node, struct ftpfs *fs)
{
  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 (fs->node_cache_mru == node)
    fs->node_cache_mru = nn->ncache_next;
  if (fs->node_cache_lru == node)
    fs->node_cache_lru = nn->ncache_prev;
  nn->ncache_next = 0;
  nn->ncache_prev = 0;
  fs->node_cache_len--;
}

/* Add NODE to the recently-used-node cache, which adds a reference to
   prevent it from going away.  NODE should be locked.  */
void
ftpfs_cache_node (struct node *node)
{
  struct netnode *nn = node->nn;
  struct ftpfs *fs = nn->fs;

  pthread_mutex_lock (&fs->node_cache_lock);

  if (fs->params.node_cache_max > 0 || fs->node_cache_len > 0)
    {
      if (fs->node_cache_mru != node)
	{
	  if (nn->ncache_next || nn->ncache_prev)
	    /* Node is already in the cache.  */
	    node_unlink (node, fs);
	  else
	    /* Add a reference from the cache.  */
	    netfs_nref (node);

	  nn->ncache_next = fs->node_cache_mru;
	  nn->ncache_prev = 0;
	  if (fs->node_cache_mru)
	    fs->node_cache_mru->nn->ncache_prev = node;
	  if (! fs->node_cache_lru)
	    fs->node_cache_lru = node;
	  fs->node_cache_mru = node;
	  fs->node_cache_len++;
	}

      /* Forget the least used nodes.  */
      while (fs->node_cache_len > fs->params.node_cache_max)
	{
	  struct node *lru = fs->node_cache_lru;
	  node_unlink (lru, fs);
	  netfs_nrele (lru);
	}
    }

  pthread_mutex_unlock (&fs->node_cache_lock);
}