Blame view
fs/nfs/pnfs_dev.c
7.27 KB
a1eaecbc4
|
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 |
/* * Device operations for the pnfs client. * * Copyright (c) 2002 * The Regents of the University of Michigan * All Rights Reserved * * Dean Hildebrand <dhildebz@umich.edu> * Garth Goodson <Garth.Goodson@netapp.com> * * Permission is granted to use, copy, create derivative works, and * redistribute this software and such derivative works for any purpose, * so long as the name of the University of Michigan is not used in * any advertising or publicity pertaining to the use or distribution * of this software without specific, written prior authorization. If * the above copyright notice or any other identification of the * University of Michigan is included in any copy of any portion of * this software, then the disclaimer below must also be included. * * This software is provided as is, without representation or warranty * of any kind either express or implied, including without limitation * the implied warranties of merchantability, fitness for a particular * purpose, or noninfringement. The Regents of the University of * Michigan shall not be liable for any damages, including special, * indirect, incidental, or consequential damages, with respect to any * claim arising out of or in connection with the use of the software, * even if it has been or is hereafter advised of the possibility of * such damages. */ |
afeacc8c1
|
30 |
#include <linux/export.h> |
a1eaecbc4
|
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 |
#include "pnfs.h" #define NFSDBG_FACILITY NFSDBG_PNFS /* * Device ID RCU cache. A device ID is unique per server and layout type. */ #define NFS4_DEVICE_ID_HASH_BITS 5 #define NFS4_DEVICE_ID_HASH_SIZE (1 << NFS4_DEVICE_ID_HASH_BITS) #define NFS4_DEVICE_ID_HASH_MASK (NFS4_DEVICE_ID_HASH_SIZE - 1) static struct hlist_head nfs4_deviceid_cache[NFS4_DEVICE_ID_HASH_SIZE]; static DEFINE_SPINLOCK(nfs4_deviceid_lock); void nfs4_print_deviceid(const struct nfs4_deviceid *id) { u32 *p = (u32 *)id; dprintk("%s: device id= [%x%x%x%x] ", __func__, p[0], p[1], p[2], p[3]); } EXPORT_SYMBOL_GPL(nfs4_print_deviceid); static inline u32 nfs4_deviceid_hash(const struct nfs4_deviceid *id) { unsigned char *cptr = (unsigned char *)id->data; unsigned int nbytes = NFS4_DEVICEID4_SIZE; u32 x = 0; while (nbytes--) { x *= 37; x += *cptr++; } return x & NFS4_DEVICE_ID_HASH_MASK; } |
1be5683b0
|
69 |
static struct nfs4_deviceid_node * |
35c8bb543
|
70 71 |
_lookup_deviceid(const struct pnfs_layoutdriver_type *ld, const struct nfs_client *clp, const struct nfs4_deviceid *id, |
1be5683b0
|
72 73 74 75 76 77 |
long hash) { struct nfs4_deviceid_node *d; struct hlist_node *n; hlist_for_each_entry_rcu(d, n, &nfs4_deviceid_cache[hash], node) |
35c8bb543
|
78 79 |
if (d->ld == ld && d->nfs_client == clp && !memcmp(&d->deviceid, id, sizeof(*id))) { |
1be5683b0
|
80 81 82 83 84 85 86 |
if (atomic_read(&d->ref)) return d; else continue; } return NULL; } |
a1eaecbc4
|
87 88 89 90 91 92 93 |
/* * Lookup a deviceid in cache and get a reference count on it if found * * @clp nfs_client associated with deviceid * @id deviceid to look up */ struct nfs4_deviceid_node * |
35c8bb543
|
94 95 |
_find_get_deviceid(const struct pnfs_layoutdriver_type *ld, const struct nfs_client *clp, const struct nfs4_deviceid *id, |
1be5683b0
|
96 97 98 99 100 |
long hash) { struct nfs4_deviceid_node *d; rcu_read_lock(); |
35c8bb543
|
101 |
d = _lookup_deviceid(ld, clp, id, hash); |
47cb498e9
|
102 103 |
if (d != NULL) atomic_inc(&d->ref); |
1be5683b0
|
104 105 106 107 108 |
rcu_read_unlock(); return d; } struct nfs4_deviceid_node * |
35c8bb543
|
109 110 |
nfs4_find_get_deviceid(const struct pnfs_layoutdriver_type *ld, const struct nfs_client *clp, const struct nfs4_deviceid *id) |
a1eaecbc4
|
111 |
{ |
35c8bb543
|
112 |
return _find_get_deviceid(ld, clp, id, nfs4_deviceid_hash(id)); |
1be5683b0
|
113 114 115 116 |
} EXPORT_SYMBOL_GPL(nfs4_find_get_deviceid); /* |
47cb498e9
|
117 |
* Remove a deviceid from cache |
1be5683b0
|
118 119 120 121 122 123 |
* * @clp nfs_client associated with deviceid * @id the deviceid to unhash * * @ret the unhashed node, if found and dereferenced to zero, NULL otherwise. */ |
47cb498e9
|
124 125 |
void nfs4_delete_deviceid(const struct pnfs_layoutdriver_type *ld, |
35c8bb543
|
126 |
const struct nfs_client *clp, const struct nfs4_deviceid *id) |
1be5683b0
|
127 |
{ |
a1eaecbc4
|
128 |
struct nfs4_deviceid_node *d; |
a1eaecbc4
|
129 |
|
1be5683b0
|
130 |
spin_lock(&nfs4_deviceid_lock); |
a1eaecbc4
|
131 |
rcu_read_lock(); |
35c8bb543
|
132 |
d = _lookup_deviceid(ld, clp, id, nfs4_deviceid_hash(id)); |
a1eaecbc4
|
133 |
rcu_read_unlock(); |
1be5683b0
|
134 135 |
if (!d) { spin_unlock(&nfs4_deviceid_lock); |
47cb498e9
|
136 |
return; |
1be5683b0
|
137 138 139 140 141 142 143 |
} hlist_del_init_rcu(&d->node); spin_unlock(&nfs4_deviceid_lock); synchronize_rcu(); /* balance the initial ref set in pnfs_insert_deviceid */ if (atomic_dec_and_test(&d->ref)) |
47cb498e9
|
144 |
d->ld->free_deviceid_node(d); |
1be5683b0
|
145 146 |
} EXPORT_SYMBOL_GPL(nfs4_delete_deviceid); |
a1eaecbc4
|
147 148 149 |
void nfs4_init_deviceid_node(struct nfs4_deviceid_node *d, |
1775bc342
|
150 |
const struct pnfs_layoutdriver_type *ld, |
a1eaecbc4
|
151 152 153 |
const struct nfs_client *nfs_client, const struct nfs4_deviceid *id) { |
1775bc342
|
154 |
INIT_HLIST_NODE(&d->node); |
9e3bd4e24
|
155 |
INIT_HLIST_NODE(&d->tmpnode); |
1775bc342
|
156 |
d->ld = ld; |
a1eaecbc4
|
157 |
d->nfs_client = nfs_client; |
c47abcf8f
|
158 |
d->flags = 0; |
a1eaecbc4
|
159 |
d->deviceid = *id; |
1775bc342
|
160 |
atomic_set(&d->ref, 1); |
a1eaecbc4
|
161 162 163 164 165 166 167 |
} EXPORT_SYMBOL_GPL(nfs4_init_deviceid_node); /* * Uniquely initialize and insert a deviceid node into cache * * @new new deviceid node |
1775bc342
|
168 169 170 171 |
* Note that the caller must set up the following members: * new->ld * new->nfs_client * new->deviceid |
a1eaecbc4
|
172 173 174 175 176 177 178 179 180 181 |
* * @ret the inserted node, if none found, otherwise, the found entry. */ struct nfs4_deviceid_node * nfs4_insert_deviceid_node(struct nfs4_deviceid_node *new) { struct nfs4_deviceid_node *d; long hash; spin_lock(&nfs4_deviceid_lock); |
1be5683b0
|
182 |
hash = nfs4_deviceid_hash(&new->deviceid); |
35c8bb543
|
183 |
d = _find_get_deviceid(new->ld, new->nfs_client, &new->deviceid, hash); |
a1eaecbc4
|
184 185 186 187 |
if (d) { spin_unlock(&nfs4_deviceid_lock); return d; } |
a1eaecbc4
|
188 189 |
hlist_add_head_rcu(&new->node, &nfs4_deviceid_cache[hash]); spin_unlock(&nfs4_deviceid_lock); |
1d92a08da
|
190 |
atomic_inc(&new->ref); |
a1eaecbc4
|
191 192 193 194 195 196 197 198 199 200 201 |
return new; } EXPORT_SYMBOL_GPL(nfs4_insert_deviceid_node); /* * Dereference a deviceid node and delete it when its reference count drops * to zero. * * @d deviceid node to put * |
47cb498e9
|
202 203 204 |
* return true iff the node was deleted * Note that since the test for d->ref == 0 is sufficient to establish * that the node is no longer hashed in the global device id cache. |
a1eaecbc4
|
205 206 207 208 |
*/ bool nfs4_put_deviceid_node(struct nfs4_deviceid_node *d) { |
47cb498e9
|
209 |
if (!atomic_dec_and_test(&d->ref)) |
a1eaecbc4
|
210 |
return false; |
1775bc342
|
211 |
d->ld->free_deviceid_node(d); |
a1eaecbc4
|
212 213 214 |
return true; } EXPORT_SYMBOL_GPL(nfs4_put_deviceid_node); |
1775bc342
|
215 216 217 218 219 |
static void _deviceid_purge_client(const struct nfs_client *clp, long hash) { struct nfs4_deviceid_node *d; |
9e3bd4e24
|
220 |
struct hlist_node *n; |
1775bc342
|
221 |
HLIST_HEAD(tmp); |
9e3bd4e24
|
222 |
spin_lock(&nfs4_deviceid_lock); |
1775bc342
|
223 224 225 226 |
rcu_read_lock(); hlist_for_each_entry_rcu(d, n, &nfs4_deviceid_cache[hash], node) if (d->nfs_client == clp && atomic_read(&d->ref)) { hlist_del_init_rcu(&d->node); |
9e3bd4e24
|
227 |
hlist_add_head(&d->tmpnode, &tmp); |
1775bc342
|
228 229 |
} rcu_read_unlock(); |
9e3bd4e24
|
230 |
spin_unlock(&nfs4_deviceid_lock); |
1775bc342
|
231 232 233 234 235 |
if (hlist_empty(&tmp)) return; synchronize_rcu(); |
9e3bd4e24
|
236 237 238 |
while (!hlist_empty(&tmp)) { d = hlist_entry(tmp.first, struct nfs4_deviceid_node, tmpnode); hlist_del(&d->tmpnode); |
1775bc342
|
239 240 |
if (atomic_dec_and_test(&d->ref)) d->ld->free_deviceid_node(d); |
9e3bd4e24
|
241 |
} |
1775bc342
|
242 243 244 245 246 247 |
} void nfs4_deviceid_purge_client(const struct nfs_client *clp) { long h; |
9e3bd4e24
|
248 249 |
if (!(clp->cl_exchange_flags & EXCHGID4_FLAG_USE_PNFS_MDS)) return; |
1775bc342
|
250 251 |
for (h = 0; h < NFS4_DEVICE_ID_HASH_SIZE; h++) _deviceid_purge_client(clp, h); |
1775bc342
|
252 |
} |
c47abcf8f
|
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
/* * Stop use of all deviceids associated with an nfs_client */ void nfs4_deviceid_mark_client_invalid(struct nfs_client *clp) { struct nfs4_deviceid_node *d; struct hlist_node *n; int i; rcu_read_lock(); for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i ++){ hlist_for_each_entry_rcu(d, n, &nfs4_deviceid_cache[i], node) if (d->nfs_client == clp) set_bit(NFS_DEVICEID_INVALID, &d->flags); } rcu_read_unlock(); } |