summaryrefslogtreecommitdiff
path: root/libfshelp/get-identity.c
blob: ac1b4f0ba25a34e1e4fc7939df6bf21c668a6aea (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
/* Helper function for io_identity
   Copyright (C) 1996, 1999 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 <fshelp.h>
#include <hurd/ports.h>
#include <hurd/ihash.h>
#include <stddef.h>
#include <assert-backtrace.h>

static struct port_class *idclass = 0;
static pthread_mutex_t idlock = PTHREAD_MUTEX_INITIALIZER;

struct idspec
{
  struct port_info pi;
  hurd_ihash_locp_t id_hashloc;
  ino_t cache_id;
};

/* The size of ino_t is larger than hurd_ihash_key_t on 32 bit
   platforms.  We therefore have to use libihashs generalized key
   interface.  */

/* This is the mix function of fasthash, see
   https://code.google.com/p/fast-hash/ for reference.  */
#define mix_fasthash(h) ({              \
        (h) ^= (h) >> 23;               \
        (h) *= 0x2127599bf4325c37ULL;   \
        (h) ^= (h) >> 47; })

static hurd_ihash_key_t
hash (const void *key)
{
  ino_t i;
  i = *(ino_t *) key;
  mix_fasthash (i);
  return (hurd_ihash_key_t) i;
}

static int
compare (const void *a, const void *b)
{
  return *(ino_t *) a == *(ino_t *) b;
}

static struct hurd_ihash idhash
  = HURD_IHASH_INITIALIZER_GKI (offsetof (struct idspec, id_hashloc),
                                NULL, NULL, hash, compare);

static void
id_clean (void *cookie)
{
  struct idspec *i = cookie;
  pthread_mutex_lock (&idlock);
  if (refcounts_hard_references(&i->pi.refcounts) == 0
      && i->id_hashloc != NULL)
    {
      /* Nobody got a send right in between, we can remove i from the hash.  */
      hurd_ihash_locp_remove (&idhash, i->id_hashloc);
      i->id_hashloc = NULL;
      ports_port_deref_weak (&i->pi);
    }
  pthread_mutex_unlock (&idlock);
}

static void
id_initialize ()
{
  assert_backtrace (!idclass);
  idclass = ports_create_class (NULL, id_clean);
}

error_t
fshelp_get_identity (struct port_bucket *bucket,
		     ino_t fileno,
		     mach_port_t *pt)
{
  struct idspec *i;
  error_t err = 0;

  pthread_mutex_lock (&idlock);
  if (!idclass)
    id_initialize ();

  i = hurd_ihash_find (&idhash, (hurd_ihash_key_t) &fileno);
  if (i == NULL)
    {
      err = ports_create_port (idclass, bucket, sizeof (struct idspec), &i);
      if (err)
        goto lose;
      i->cache_id = fileno;
      err = hurd_ihash_add (&idhash, (hurd_ihash_key_t) &i->cache_id, i);
      if (err)
        goto lose_port;

      /* Weak reference for the hash entry.  */
      ports_port_ref_weak(&i->pi);

      *pt = ports_get_right (i);
      ports_port_deref (i);
    }
  else
    *pt = ports_get_right (i);

  /* Success!  */
  goto lose;

 lose_port:
  ports_destroy_right (i);
 lose:
  pthread_mutex_unlock (&idlock);
  return err;
}