pnfs_dev.c 9.91 KB
Newer Older
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
/*
 *  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.
 */

31
#include <linux/export.h>
32 33 34
#include <linux/nfs_fs.h>
#include "nfs4session.h"
#include "internal.h"
35 36
#include "pnfs.h"

37 38
#include "nfs4trace.h"

39 40 41 42 43 44 45 46 47
#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)

48

49 50 51
static struct hlist_head nfs4_deviceid_cache[NFS4_DEVICE_ID_HASH_SIZE];
static DEFINE_SPINLOCK(nfs4_deviceid_lock);

52
#ifdef NFS_DEBUG
53 54 55 56 57 58 59 60 61
void
nfs4_print_deviceid(const struct nfs4_deviceid *id)
{
	u32 *p = (u32 *)id;

	dprintk("%s: device id= [%x%x%x%x]\n", __func__,
		p[0], p[1], p[2], p[3]);
}
EXPORT_SYMBOL_GPL(nfs4_print_deviceid);
62
#endif
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

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;
}

Marc Eshel's avatar
Marc Eshel committed
78
static struct nfs4_deviceid_node *
79 80
_lookup_deviceid(const struct pnfs_layoutdriver_type *ld,
		 const struct nfs_client *clp, const struct nfs4_deviceid *id,
Marc Eshel's avatar
Marc Eshel committed
81 82 83 84
		 long hash)
{
	struct nfs4_deviceid_node *d;

85
	hlist_for_each_entry_rcu(d, &nfs4_deviceid_cache[hash], node)
86 87
		if (d->ld == ld && d->nfs_client == clp &&
		    !memcmp(&d->deviceid, id, sizeof(*id))) {
Marc Eshel's avatar
Marc Eshel committed
88 89 90 91 92 93 94 95
			if (atomic_read(&d->ref))
				return d;
			else
				continue;
		}
	return NULL;
}

96 97 98
static struct nfs4_deviceid_node *
nfs4_get_device_info(struct nfs_server *server,
		const struct nfs4_deviceid *dev_id,
99
		const struct cred *cred, gfp_t gfp_flags)
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
{
	struct nfs4_deviceid_node *d = NULL;
	struct pnfs_device *pdev = NULL;
	struct page **pages = NULL;
	u32 max_resp_sz;
	int max_pages;
	int rc, i;

	/*
	 * Use the session max response size as the basis for setting
	 * GETDEVICEINFO's maxcount
	 */
	max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
	max_pages = nfs_page_array_len(0, max_resp_sz);
	dprintk("%s: server %p max_resp_sz %u max_pages %d\n",
		__func__, server, max_resp_sz, max_pages);

	pdev = kzalloc(sizeof(*pdev), gfp_flags);
	if (!pdev)
		return NULL;

	pages = kcalloc(max_pages, sizeof(struct page *), gfp_flags);
	if (!pages)
		goto out_free_pdev;

	for (i = 0; i < max_pages; i++) {
		pages[i] = alloc_page(gfp_flags);
		if (!pages[i])
			goto out_free_pages;
	}

	memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id));
	pdev->layout_type = server->pnfs_curr_ld->id;
	pdev->pages = pages;
	pdev->pgbase = 0;
	pdev->pglen = max_resp_sz;
	pdev->mincount = 0;
	pdev->maxcount = max_resp_sz - nfs41_maxgetdevinfo_overhead;

	rc = nfs4_proc_getdeviceinfo(server, pdev, cred);
	dprintk("%s getdevice info returns %d\n", __func__, rc);
	if (rc)
		goto out_free_pages;

	/*
	 * Found new device, need to decode it and then add it to the
	 * list of known devices for this mountpoint.
	 */
	d = server->pnfs_curr_ld->alloc_deviceid_node(server, pdev,
			gfp_flags);
150 151
	if (d && pdev->nocache)
		set_bit(NFS_DEVICEID_NOCACHE, &d->flags);
152 153

out_free_pages:
154
	while (--i >= 0)
155 156 157 158 159 160 161 162
		__free_page(pages[i]);
	kfree(pages);
out_free_pdev:
	kfree(pdev);
	dprintk("<-- %s d %p\n", __func__, d);
	return d;
}

163 164 165 166 167 168
/*
 * 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
 */
169
static struct nfs4_deviceid_node *
170 171
__nfs4_find_get_deviceid(struct nfs_server *server,
		const struct nfs4_deviceid *id, long hash)
Marc Eshel's avatar
Marc Eshel committed
172 173 174 175
{
	struct nfs4_deviceid_node *d;

	rcu_read_lock();
176 177
	d = _lookup_deviceid(server->pnfs_curr_ld, server->nfs_client, id,
			hash);
178 179
	if (d != NULL && !atomic_inc_not_zero(&d->ref))
		d = NULL;
Marc Eshel's avatar
Marc Eshel committed
180 181 182 183
	rcu_read_unlock();
	return d;
}

184
struct nfs4_deviceid_node *
185
nfs4_find_get_deviceid(struct nfs_server *server,
186
		const struct nfs4_deviceid *id, const struct cred *cred,
187
		gfp_t gfp_mask)
Marc Eshel's avatar
Marc Eshel committed
188
{
189 190 191 192 193
	long hash = nfs4_deviceid_hash(id);
	struct nfs4_deviceid_node *d, *new;

	d = __nfs4_find_get_deviceid(server, id, hash);
	if (d)
194
		goto found;
195 196

	new = nfs4_get_device_info(server, id, cred, gfp_mask);
197 198
	if (!new) {
		trace_nfs4_find_deviceid(server, id, -ENOENT);
199
		return new;
200
	}
201 202 203 204 205 206

	spin_lock(&nfs4_deviceid_lock);
	d = __nfs4_find_get_deviceid(server, id, hash);
	if (d) {
		spin_unlock(&nfs4_deviceid_lock);
		server->pnfs_curr_ld->free_deviceid_node(new);
207 208 209 210 211
	} else {
		atomic_inc(&new->ref);
		hlist_add_head_rcu(&new->node, &nfs4_deviceid_cache[hash]);
		spin_unlock(&nfs4_deviceid_lock);
		d = new;
212
	}
213 214 215
found:
	trace_nfs4_find_deviceid(server, id, 0);
	return d;
Marc Eshel's avatar
Marc Eshel committed
216 217 218 219
}
EXPORT_SYMBOL_GPL(nfs4_find_get_deviceid);

/*
220
 * Remove a deviceid from cache
Marc Eshel's avatar
Marc Eshel committed
221 222 223 224 225 226
 *
 * @clp nfs_client associated with deviceid
 * @id the deviceid to unhash
 *
 * @ret the unhashed node, if found and dereferenced to zero, NULL otherwise.
 */
227 228
void
nfs4_delete_deviceid(const struct pnfs_layoutdriver_type *ld,
229
			 const struct nfs_client *clp, const struct nfs4_deviceid *id)
230 231 232
{
	struct nfs4_deviceid_node *d;

Marc Eshel's avatar
Marc Eshel committed
233
	spin_lock(&nfs4_deviceid_lock);
234
	rcu_read_lock();
235
	d = _lookup_deviceid(ld, clp, id, nfs4_deviceid_hash(id));
236
	rcu_read_unlock();
Marc Eshel's avatar
Marc Eshel committed
237 238
	if (!d) {
		spin_unlock(&nfs4_deviceid_lock);
239
		return;
Marc Eshel's avatar
Marc Eshel committed
240 241
	}
	hlist_del_init_rcu(&d->node);
242
	clear_bit(NFS_DEVICEID_NOCACHE, &d->flags);
Marc Eshel's avatar
Marc Eshel committed
243 244 245
	spin_unlock(&nfs4_deviceid_lock);

	/* balance the initial ref set in pnfs_insert_deviceid */
246
	nfs4_put_deviceid_node(d);
Marc Eshel's avatar
Marc Eshel committed
247 248
}
EXPORT_SYMBOL_GPL(nfs4_delete_deviceid);
249 250

void
251
nfs4_init_deviceid_node(struct nfs4_deviceid_node *d, struct nfs_server *server,
252 253
			const struct nfs4_deviceid *id)
{
254
	INIT_HLIST_NODE(&d->node);
255
	INIT_HLIST_NODE(&d->tmpnode);
256 257
	d->ld = server->pnfs_curr_ld;
	d->nfs_client = server->nfs_client;
258
	d->flags = 0;
259
	d->deviceid = *id;
260
	atomic_set(&d->ref, 1);
261 262 263 264 265 266 267 268 269
}
EXPORT_SYMBOL_GPL(nfs4_init_deviceid_node);

/*
 * Dereference a deviceid node and delete it when its reference count drops
 * to zero.
 *
 * @d deviceid node to put
 *
270 271 272
 * 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.
273 274 275 276
 */
bool
nfs4_put_deviceid_node(struct nfs4_deviceid_node *d)
{
277 278 279 280 281
	if (test_bit(NFS_DEVICEID_NOCACHE, &d->flags)) {
		if (atomic_add_unless(&d->ref, -1, 2))
			return false;
		nfs4_delete_deviceid(d->ld, d->nfs_client, &d->deviceid);
	}
282
	if (!atomic_dec_and_test(&d->ref))
283
		return false;
284
	trace_nfs4_deviceid_free(d->nfs_client, &d->deviceid);
285
	d->ld->free_deviceid_node(d);
286 287 288
	return true;
}
EXPORT_SYMBOL_GPL(nfs4_put_deviceid_node);
289

290 291 292 293 294 295 296 297 298 299
void
nfs4_mark_deviceid_available(struct nfs4_deviceid_node *node)
{
	if (test_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags)) {
		clear_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags);
		smp_mb__after_atomic();
	}
}
EXPORT_SYMBOL_GPL(nfs4_mark_deviceid_available);

300 301 302 303
void
nfs4_mark_deviceid_unavailable(struct nfs4_deviceid_node *node)
{
	node->timestamp_unavailable = jiffies;
304
	smp_mb__before_atomic();
305
	set_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags);
306
	smp_mb__after_atomic();
307 308 309 310 311 312 313 314 315 316 317 318 319 320
}
EXPORT_SYMBOL_GPL(nfs4_mark_deviceid_unavailable);

bool
nfs4_test_deviceid_unavailable(struct nfs4_deviceid_node *node)
{
	if (test_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags)) {
		unsigned long start, end;

		end = jiffies;
		start = end - PNFS_DEVICE_RETRY_TIMEOUT;
		if (time_in_range(node->timestamp_unavailable, start, end))
			return true;
		clear_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags);
321
		smp_mb__after_atomic();
322 323 324 325 326
	}
	return false;
}
EXPORT_SYMBOL_GPL(nfs4_test_deviceid_unavailable);

327 328 329 330 331 332
static void
_deviceid_purge_client(const struct nfs_client *clp, long hash)
{
	struct nfs4_deviceid_node *d;
	HLIST_HEAD(tmp);

333
	spin_lock(&nfs4_deviceid_lock);
334
	rcu_read_lock();
335
	hlist_for_each_entry_rcu(d, &nfs4_deviceid_cache[hash], node)
336 337
		if (d->nfs_client == clp && atomic_read(&d->ref)) {
			hlist_del_init_rcu(&d->node);
338
			hlist_add_head(&d->tmpnode, &tmp);
339
			clear_bit(NFS_DEVICEID_NOCACHE, &d->flags);
340 341
		}
	rcu_read_unlock();
342
	spin_unlock(&nfs4_deviceid_lock);
343 344 345 346

	if (hlist_empty(&tmp))
		return;

347 348 349
	while (!hlist_empty(&tmp)) {
		d = hlist_entry(tmp.first, struct nfs4_deviceid_node, tmpnode);
		hlist_del(&d->tmpnode);
350
		nfs4_put_deviceid_node(d);
351
	}
352 353 354 355 356 357 358
}

void
nfs4_deviceid_purge_client(const struct nfs_client *clp)
{
	long h;

359 360
	if (!(clp->cl_exchange_flags & EXCHGID4_FLAG_USE_PNFS_MDS))
		return;
361 362 363
	for (h = 0; h < NFS4_DEVICE_ID_HASH_SIZE; h++)
		_deviceid_purge_client(clp, h);
}
364 365 366 367 368 369 370 371 372 373 374 375

/*
 * 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;
	int i;

	rcu_read_lock();
	for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i ++){
376
		hlist_for_each_entry_rcu(d, &nfs4_deviceid_cache[i], node)
377 378 379 380 381
			if (d->nfs_client == clp)
				set_bit(NFS_DEVICEID_INVALID, &d->flags);
	}
	rcu_read_unlock();
}