vlocation.c 16.9 KB
Newer Older
1
/* AFS volume location management
L
Linus Torvalds 已提交
2
 *
3
 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program 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 of the License, or (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/module.h>
14
#include <linux/slab.h>
L
Linus Torvalds 已提交
15
#include <linux/init.h>
A
Alexey Dobriyan 已提交
16
#include <linux/sched.h>
L
Linus Torvalds 已提交
17 18
#include "internal.h"

19 20
struct workqueue_struct *afs_vlocation_update_worker;

A
Adrian Bunk 已提交
21 22
static unsigned afs_vlocation_timeout = 10;	/* volume location timeout in seconds */
static unsigned afs_vlocation_update_timeout = 10 * 60;
L
Linus Torvalds 已提交
23 24 25 26 27

/*
 * iterate through the VL servers in a cell until one of them admits knowing
 * about the volume in question
 */
28
static int afs_vlocation_access_vl_by_name(struct afs_vlocation *vl,
D
David Howells 已提交
29
					   struct key *key,
L
Linus Torvalds 已提交
30 31
					   struct afs_cache_vlocation *vldb)
{
32 33
	struct afs_cell *cell = vl->cell;
	struct in_addr addr;
L
Linus Torvalds 已提交
34 35
	int count, ret;

36
	_enter("%s,%s", cell->name, vl->vldb.name);
L
Linus Torvalds 已提交
37

38
	down_write(&vl->cell->vl_sem);
L
Linus Torvalds 已提交
39 40
	ret = -ENOMEDIUM;
	for (count = cell->vl_naddrs; count > 0; count--) {
41 42 43
		addr = cell->vl_addrs[cell->vl_curr_svix];

		_debug("CellServ[%hu]: %08x", cell->vl_curr_svix, addr.s_addr);
L
Linus Torvalds 已提交
44 45

		/* attempt to access the VL server */
46 47
		ret = afs_vl_get_entry_by_name(cell->net, &addr, key,
					       vl->vldb.name, vldb, false);
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58 59
		switch (ret) {
		case 0:
			goto out;
		case -ENOMEM:
		case -ENONET:
		case -ENETUNREACH:
		case -EHOSTUNREACH:
		case -ECONNREFUSED:
			if (ret == -ENOMEM || ret == -ENONET)
				goto out;
			goto rotate;
		case -ENOMEDIUM:
60 61
		case -EKEYREJECTED:
		case -EKEYEXPIRED:
L
Linus Torvalds 已提交
62 63
			goto out;
		default:
64
			ret = -EIO;
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73
			goto rotate;
		}

		/* rotate the server records upon lookup failure */
	rotate:
		cell->vl_curr_svix++;
		cell->vl_curr_svix %= cell->vl_naddrs;
	}

D
David Howells 已提交
74
out:
75
	up_write(&vl->cell->vl_sem);
L
Linus Torvalds 已提交
76 77
	_leave(" = %d", ret);
	return ret;
D
David Howells 已提交
78
}
L
Linus Torvalds 已提交
79 80 81 82 83

/*
 * iterate through the VL servers in a cell until one of them admits knowing
 * about the volume in question
 */
84
static int afs_vlocation_access_vl_by_id(struct afs_vlocation *vl,
D
David Howells 已提交
85
					 struct key *key,
L
Linus Torvalds 已提交
86 87 88 89
					 afs_volid_t volid,
					 afs_voltype_t voltype,
					 struct afs_cache_vlocation *vldb)
{
90 91
	struct afs_cell *cell = vl->cell;
	struct in_addr addr;
L
Linus Torvalds 已提交
92 93 94 95
	int count, ret;

	_enter("%s,%x,%d,", cell->name, volid, voltype);

96
	down_write(&vl->cell->vl_sem);
L
Linus Torvalds 已提交
97 98
	ret = -ENOMEDIUM;
	for (count = cell->vl_naddrs; count > 0; count--) {
99 100 101
		addr = cell->vl_addrs[cell->vl_curr_svix];

		_debug("CellServ[%hu]: %08x", cell->vl_curr_svix, addr.s_addr);
L
Linus Torvalds 已提交
102 103

		/* attempt to access the VL server */
104 105
		ret = afs_vl_get_entry_by_id(cell->net, &addr, key, volid,
					     voltype, vldb, false);
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115 116
		switch (ret) {
		case 0:
			goto out;
		case -ENOMEM:
		case -ENONET:
		case -ENETUNREACH:
		case -EHOSTUNREACH:
		case -ECONNREFUSED:
			if (ret == -ENOMEM || ret == -ENONET)
				goto out;
			goto rotate;
117 118 119 120 121 122 123 124 125 126 127
		case -EBUSY:
			vl->upd_busy_cnt++;
			if (vl->upd_busy_cnt <= 3) {
				if (vl->upd_busy_cnt > 1) {
					/* second+ BUSY - sleep a little bit */
					set_current_state(TASK_UNINTERRUPTIBLE);
					schedule_timeout(1);
				}
				continue;
			}
			break;
L
Linus Torvalds 已提交
128
		case -ENOMEDIUM:
129 130
			vl->upd_rej_cnt++;
			goto rotate;
L
Linus Torvalds 已提交
131
		default:
132
			ret = -EIO;
L
Linus Torvalds 已提交
133 134 135 136 137 138 139
			goto rotate;
		}

		/* rotate the server records upon lookup failure */
	rotate:
		cell->vl_curr_svix++;
		cell->vl_curr_svix %= cell->vl_naddrs;
140
		vl->upd_busy_cnt = 0;
L
Linus Torvalds 已提交
141 142
	}

D
David Howells 已提交
143
out:
144 145 146 147 148 149 150 151 152
	if (ret < 0 && vl->upd_rej_cnt > 0) {
		printk(KERN_NOTICE "kAFS:"
		       " Active volume no longer valid '%s'\n",
		       vl->vldb.name);
		vl->valid = 0;
		ret = -ENOMEDIUM;
	}

	up_write(&vl->cell->vl_sem);
L
Linus Torvalds 已提交
153 154
	_leave(" = %d", ret);
	return ret;
D
David Howells 已提交
155
}
L
Linus Torvalds 已提交
156 157

/*
158
 * allocate a volume location record
L
Linus Torvalds 已提交
159
 */
160 161 162
static struct afs_vlocation *afs_vlocation_alloc(struct afs_cell *cell,
						 const char *name,
						 size_t namesz)
L
Linus Torvalds 已提交
163
{
164 165 166 167 168 169 170 171 172 173 174
	struct afs_vlocation *vl;

	vl = kzalloc(sizeof(struct afs_vlocation), GFP_KERNEL);
	if (vl) {
		vl->cell = cell;
		vl->state = AFS_VL_NEW;
		atomic_set(&vl->usage, 1);
		INIT_LIST_HEAD(&vl->link);
		INIT_LIST_HEAD(&vl->grave);
		INIT_LIST_HEAD(&vl->update);
		init_waitqueue_head(&vl->waitq);
175
		spin_lock_init(&vl->lock);
176
		memcpy(vl->vldb.name, name, namesz);
L
Linus Torvalds 已提交
177 178
	}

179 180 181
	_leave(" = %p", vl);
	return vl;
}
L
Linus Torvalds 已提交
182

183 184 185 186
/*
 * update record if we found it in the cache
 */
static int afs_vlocation_update_record(struct afs_vlocation *vl,
D
David Howells 已提交
187
				       struct key *key,
188 189 190 191 192
				       struct afs_cache_vlocation *vldb)
{
	afs_voltype_t voltype;
	afs_volid_t vid;
	int ret;
L
Linus Torvalds 已提交
193 194 195

	/* try to look up a cached volume in the cell VL databases by ID */
	_debug("Locally Cached: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
196 197 198 199 200 201 202 203
	       vl->vldb.name,
	       vl->vldb.vidmask,
	       ntohl(vl->vldb.servers[0].s_addr),
	       vl->vldb.srvtmask[0],
	       ntohl(vl->vldb.servers[1].s_addr),
	       vl->vldb.srvtmask[1],
	       ntohl(vl->vldb.servers[2].s_addr),
	       vl->vldb.srvtmask[2]);
L
Linus Torvalds 已提交
204 205

	_debug("Vids: %08x %08x %08x",
206 207 208
	       vl->vldb.vid[0],
	       vl->vldb.vid[1],
	       vl->vldb.vid[2]);
L
Linus Torvalds 已提交
209

210 211
	if (vl->vldb.vidmask & AFS_VOL_VTM_RW) {
		vid = vl->vldb.vid[0];
L
Linus Torvalds 已提交
212
		voltype = AFSVL_RWVOL;
213 214
	} else if (vl->vldb.vidmask & AFS_VOL_VTM_RO) {
		vid = vl->vldb.vid[1];
L
Linus Torvalds 已提交
215
		voltype = AFSVL_ROVOL;
216 217
	} else if (vl->vldb.vidmask & AFS_VOL_VTM_BAK) {
		vid = vl->vldb.vid[2];
L
Linus Torvalds 已提交
218
		voltype = AFSVL_BACKVOL;
D
David Howells 已提交
219
	} else {
L
Linus Torvalds 已提交
220 221 222 223 224
		BUG();
		vid = 0;
		voltype = 0;
	}

225 226 227
	/* contact the server to make sure the volume is still available
	 * - TODO: need to handle disconnected operation here
	 */
D
David Howells 已提交
228
	ret = afs_vlocation_access_vl_by_id(vl, key, vid, voltype, vldb);
L
Linus Torvalds 已提交
229 230 231
	switch (ret) {
		/* net error */
	default:
232 233 234 235 236
		printk(KERN_WARNING "kAFS:"
		       " failed to update volume '%s' (%x) up in '%s': %d\n",
		       vl->vldb.name, vid, vl->cell->name, ret);
		_leave(" = %d", ret);
		return ret;
L
Linus Torvalds 已提交
237 238 239

		/* pulled from local cache into memory */
	case 0:
240 241
		_leave(" = 0");
		return 0;
L
Linus Torvalds 已提交
242 243 244

		/* uh oh... looks like the volume got deleted */
	case -ENOMEDIUM:
245 246 247
		printk(KERN_ERR "kAFS:"
		       " volume '%s' (%x) does not exist '%s'\n",
		       vl->vldb.name, vid, vl->cell->name);
L
Linus Torvalds 已提交
248 249

		/* TODO: make existing record unavailable */
250 251
		_leave(" = %d", ret);
		return ret;
L
Linus Torvalds 已提交
252
	}
253
}
L
Linus Torvalds 已提交
254

255 256 257 258 259 260 261 262 263 264 265
/*
 * apply the update to a VL record
 */
static void afs_vlocation_apply_update(struct afs_vlocation *vl,
				       struct afs_cache_vlocation *vldb)
{
	_debug("Done VL Lookup: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
	       vldb->name, vldb->vidmask,
	       ntohl(vldb->servers[0].s_addr), vldb->srvtmask[0],
	       ntohl(vldb->servers[1].s_addr), vldb->srvtmask[1],
	       ntohl(vldb->servers[2].s_addr), vldb->srvtmask[2]);
L
Linus Torvalds 已提交
266

267 268
	_debug("Vids: %08x %08x %08x",
	       vldb->vid[0], vldb->vid[1], vldb->vid[2]);
L
Linus Torvalds 已提交
269

270 271 272 273
	if (strcmp(vldb->name, vl->vldb.name) != 0)
		printk(KERN_NOTICE "kAFS:"
		       " name of volume '%s' changed to '%s' on server\n",
		       vl->vldb.name, vldb->name);
L
Linus Torvalds 已提交
274

275
	vl->vldb = *vldb;
L
Linus Torvalds 已提交
276

D
David Howells 已提交
277 278
#ifdef CONFIG_AFS_FSCACHE
	fscache_update_cookie(vl->cache);
L
Linus Torvalds 已提交
279
#endif
D
David Howells 已提交
280
}
L
Linus Torvalds 已提交
281 282

/*
283 284
 * fill in a volume location record, consulting the cache and the VL server
 * both
L
Linus Torvalds 已提交
285
 */
D
David Howells 已提交
286 287
static int afs_vlocation_fill_in_record(struct afs_vlocation *vl,
					struct key *key)
L
Linus Torvalds 已提交
288
{
289 290
	struct afs_cache_vlocation vldb;
	int ret;
L
Linus Torvalds 已提交
291

292
	_enter("");
L
Linus Torvalds 已提交
293

294
	ASSERTCMP(vl->valid, ==, 0);
L
Linus Torvalds 已提交
295

296
	memset(&vldb, 0, sizeof(vldb));
L
Linus Torvalds 已提交
297

298
	/* see if we have an in-cache copy (will set vl->valid if there is) */
D
David Howells 已提交
299 300
#ifdef CONFIG_AFS_FSCACHE
	vl->cache = fscache_acquire_cookie(vl->cell->cache,
301 302
					   &afs_vlocation_cache_index_def, vl,
					   true);
303
#endif
L
Linus Torvalds 已提交
304

305 306 307 308
	if (vl->valid) {
		/* try to update a known volume in the cell VL databases by
		 * ID as the name may have changed */
		_debug("found in cache");
D
David Howells 已提交
309
		ret = afs_vlocation_update_record(vl, key, &vldb);
310 311 312
	} else {
		/* try to look up an unknown volume in the cell VL databases by
		 * name */
D
David Howells 已提交
313
		ret = afs_vlocation_access_vl_by_name(vl, key, &vldb);
314 315 316 317 318
		if (ret < 0) {
			printk("kAFS: failed to locate '%s' in cell '%s'\n",
			       vl->vldb.name, vl->cell->name);
			return ret;
		}
L
Linus Torvalds 已提交
319 320
	}

321 322 323
	afs_vlocation_apply_update(vl, &vldb);
	_leave(" = 0");
	return 0;
D
David Howells 已提交
324
}
L
Linus Torvalds 已提交
325 326

/*
327
 * queue a vlocation record for updates
L
Linus Torvalds 已提交
328
 */
329 330
static void afs_vlocation_queue_for_updates(struct afs_net *net,
					    struct afs_vlocation *vl)
L
Linus Torvalds 已提交
331
{
332
	struct afs_vlocation *xvl;
L
Linus Torvalds 已提交
333

334
	/* wait at least 10 minutes before updating... */
335 336
	vl->update_at = ktime_get_real_seconds() +
			afs_vlocation_update_timeout;
337

338
	spin_lock(&net->vl_updates_lock);
339

340
	if (!list_empty(&net->vl_updates)) {
341 342 343 344
		/* ... but wait at least 1 second more than the newest record
		 * already queued so that we don't spam the VL server suddenly
		 * with lots of requests
		 */
345
		xvl = list_entry(net->vl_updates.prev,
346 347 348
				 struct afs_vlocation, update);
		if (vl->update_at <= xvl->update_at)
			vl->update_at = xvl->update_at + 1;
349
	} else if (net->live) {
350
		queue_delayed_work(afs_vlocation_update_worker,
351
				   &net->vl_updater,
352
				   afs_vlocation_update_timeout * HZ);
L
Linus Torvalds 已提交
353
	}
354

355 356
	list_add_tail(&vl->update, &net->vl_updates);
	spin_unlock(&net->vl_updates_lock);
D
David Howells 已提交
357
}
L
Linus Torvalds 已提交
358 359

/*
360 361 362 363 364
 * lookup volume location
 * - iterate through the VL servers in a cell until one of them admits knowing
 *   about the volume in question
 * - lookup in the local cache if not able to find on the VL server
 * - insert/update in the local cache if did get a VL response
L
Linus Torvalds 已提交
365
 */
366 367
struct afs_vlocation *afs_vlocation_lookup(struct afs_net *net,
					   struct afs_cell *cell,
D
David Howells 已提交
368
					   struct key *key,
369 370
					   const char *name,
					   size_t namesz)
L
Linus Torvalds 已提交
371
{
372 373
	struct afs_vlocation *vl;
	int ret;
L
Linus Torvalds 已提交
374

D
David Howells 已提交
375 376 377
	_enter("{%s},{%x},%*.*s,%zu",
	       cell->name, key_serial(key),
	       (int) namesz, (int) namesz, name, namesz);
L
Linus Torvalds 已提交
378

A
Adrian Bunk 已提交
379
	if (namesz >= sizeof(vl->vldb.name)) {
380 381 382
		_leave(" = -ENAMETOOLONG");
		return ERR_PTR(-ENAMETOOLONG);
	}
L
Linus Torvalds 已提交
383

384 385 386 387 388 389 390 391
	/* see if we have an in-memory copy first */
	down_write(&cell->vl_sem);
	spin_lock(&cell->vl_lock);
	list_for_each_entry(vl, &cell->vl_list, link) {
		if (vl->vldb.name[namesz] != '\0')
			continue;
		if (memcmp(vl->vldb.name, name, namesz) == 0)
			goto found_in_memory;
L
Linus Torvalds 已提交
392
	}
393
	spin_unlock(&cell->vl_lock);
L
Linus Torvalds 已提交
394

395 396 397 398 399 400
	/* not in the cell's in-memory lists - create a new record */
	vl = afs_vlocation_alloc(cell, name, namesz);
	if (!vl) {
		up_write(&cell->vl_sem);
		return ERR_PTR(-ENOMEM);
	}
L
Linus Torvalds 已提交
401

402
	afs_get_cell(cell);
L
Linus Torvalds 已提交
403

404 405 406
	list_add_tail(&vl->link, &cell->vl_list);
	vl->state = AFS_VL_CREATING;
	up_write(&cell->vl_sem);
L
Linus Torvalds 已提交
407

408
fill_in_record:
D
David Howells 已提交
409
	ret = afs_vlocation_fill_in_record(vl, key);
410 411
	if (ret < 0)
		goto error_abandon;
412
	spin_lock(&vl->lock);
413
	vl->state = AFS_VL_VALID;
414
	spin_unlock(&vl->lock);
415
	wake_up(&vl->waitq);
L
Linus Torvalds 已提交
416

D
David Howells 已提交
417 418 419 420 421
	/* update volume entry in local cache */
#ifdef CONFIG_AFS_FSCACHE
	fscache_update_cookie(vl->cache);
#endif

422
	/* schedule for regular updates */
423
	afs_vlocation_queue_for_updates(net, vl);
424
	goto success;
L
Linus Torvalds 已提交
425

426 427 428 429 430 431
found_in_memory:
	/* found in memory */
	_debug("found in memory");
	atomic_inc(&vl->usage);
	spin_unlock(&cell->vl_lock);
	if (!list_empty(&vl->grave)) {
432
		spin_lock(&net->vl_graveyard_lock);
433
		list_del_init(&vl->grave);
434
		spin_unlock(&net->vl_graveyard_lock);
L
Linus Torvalds 已提交
435
	}
436
	up_write(&cell->vl_sem);
L
Linus Torvalds 已提交
437

438
	/* see if it was an abandoned record that we might try filling in */
439
	spin_lock(&vl->lock);
440 441
	while (vl->state != AFS_VL_VALID) {
		afs_vlocation_state_t state = vl->state;
L
Linus Torvalds 已提交
442

443
		_debug("invalid [state %d]", state);
L
Linus Torvalds 已提交
444

445
		if (state == AFS_VL_NEW || state == AFS_VL_NO_VOLUME) {
446 447 448
			vl->state = AFS_VL_CREATING;
			spin_unlock(&vl->lock);
			goto fill_in_record;
449 450 451 452 453 454
		}

		/* must now wait for creation or update by someone else to
		 * complete */
		_debug("wait");

455
		spin_unlock(&vl->lock);
456 457 458 459
		ret = wait_event_interruptible(vl->waitq,
					       vl->state == AFS_VL_NEW ||
					       vl->state == AFS_VL_VALID ||
					       vl->state == AFS_VL_NO_VOLUME);
460 461
		if (ret < 0)
			goto error;
462
		spin_lock(&vl->lock);
L
Linus Torvalds 已提交
463
	}
464
	spin_unlock(&vl->lock);
L
Linus Torvalds 已提交
465

466
success:
D
David Howells 已提交
467
	_leave(" = %p", vl);
468 469 470
	return vl;

error_abandon:
471
	spin_lock(&vl->lock);
472
	vl->state = AFS_VL_NEW;
473
	spin_unlock(&vl->lock);
474
	wake_up(&vl->waitq);
475 476
error:
	ASSERT(vl != NULL);
477
	afs_put_vlocation(net, vl);
L
Linus Torvalds 已提交
478
	_leave(" = %d", ret);
479
	return ERR_PTR(ret);
D
David Howells 已提交
480
}
L
Linus Torvalds 已提交
481 482

/*
483
 * finish using a volume location record
L
Linus Torvalds 已提交
484
 */
485
void afs_put_vlocation(struct afs_net *net, struct afs_vlocation *vl)
L
Linus Torvalds 已提交
486
{
487 488
	if (!vl)
		return;
L
Linus Torvalds 已提交
489

490
	_enter("%s", vl->vldb.name);
L
Linus Torvalds 已提交
491

492
	ASSERTCMP(atomic_read(&vl->usage), >, 0);
L
Linus Torvalds 已提交
493

494 495 496 497
	if (likely(!atomic_dec_and_test(&vl->usage))) {
		_leave("");
		return;
	}
L
Linus Torvalds 已提交
498

499
	spin_lock(&net->vl_graveyard_lock);
500 501
	if (atomic_read(&vl->usage) == 0) {
		_debug("buried");
502
		list_move_tail(&vl->grave, &net->vl_graveyard);
503
		vl->time_of_death = ktime_get_real_seconds();
504
		queue_delayed_work(afs_wq, &net->vl_reaper,
505
				   afs_vlocation_timeout * HZ);
506 507 508

		/* suspend updates on this record */
		if (!list_empty(&vl->update)) {
509
			spin_lock(&net->vl_updates_lock);
510
			list_del_init(&vl->update);
511
			spin_unlock(&net->vl_updates_lock);
512 513
		}
	}
514
	spin_unlock(&net->vl_graveyard_lock);
515
	_leave(" [killed?]");
D
David Howells 已提交
516
}
L
Linus Torvalds 已提交
517 518

/*
519
 * destroy a dead volume location record
L
Linus Torvalds 已提交
520
 */
521
static void afs_vlocation_destroy(struct afs_net *net, struct afs_vlocation *vl)
L
Linus Torvalds 已提交
522
{
523
	_enter("%p", vl);
L
Linus Torvalds 已提交
524

D
David Howells 已提交
525 526
#ifdef CONFIG_AFS_FSCACHE
	fscache_relinquish_cookie(vl->cache, 0);
527
#endif
528
	afs_put_cell(net, vl->cell);
529 530 531 532 533 534
	kfree(vl);
}

/*
 * reap dead volume location records
 */
535
void afs_vlocation_reaper(struct work_struct *work)
536 537 538
{
	LIST_HEAD(corpses);
	struct afs_vlocation *vl;
539
	struct afs_net *net = container_of(work, struct afs_net, vl_reaper.work);
540
	unsigned long delay, expiry;
541
	time64_t now;
L
Linus Torvalds 已提交
542

543
	_enter("");
L
Linus Torvalds 已提交
544

545
	now = ktime_get_real_seconds();
546
	spin_lock(&net->vl_graveyard_lock);
547

548 549
	while (!list_empty(&net->vl_graveyard)) {
		vl = list_entry(net->vl_graveyard.next,
550 551 552 553 554
				struct afs_vlocation, grave);

		_debug("check %p", vl);

		/* the queue is ordered most dead first */
555 556 557 558 559 560 561 562
		if (net->live) {
			expiry = vl->time_of_death + afs_vlocation_timeout;
			if (expiry > now) {
				delay = (expiry - now) * HZ;
				_debug("delay %lu", delay);
				mod_delayed_work(afs_wq, &net->vl_reaper, delay);
				break;
			}
563
		}
L
Linus Torvalds 已提交
564

565 566 567 568
		spin_lock(&vl->cell->vl_lock);
		if (atomic_read(&vl->usage) > 0) {
			_debug("no reap");
			list_del_init(&vl->grave);
D
David Howells 已提交
569
		} else {
570 571 572
			_debug("reap");
			list_move_tail(&vl->grave, &corpses);
			list_del_init(&vl->link);
L
Linus Torvalds 已提交
573
		}
574
		spin_unlock(&vl->cell->vl_lock);
L
Linus Torvalds 已提交
575 576
	}

577
	spin_unlock(&net->vl_graveyard_lock);
L
Linus Torvalds 已提交
578

579 580 581 582
	/* now reap the corpses we've extracted */
	while (!list_empty(&corpses)) {
		vl = list_entry(corpses.next, struct afs_vlocation, grave);
		list_del(&vl->grave);
583
		afs_vlocation_destroy(net, vl);
L
Linus Torvalds 已提交
584 585 586
	}

	_leave("");
587
}
L
Linus Torvalds 已提交
588

589 590 591
/*
 * discard all the volume location records for rmmod
 */
592
void __net_exit afs_vlocation_purge(struct afs_net *net)
593
{
594 595 596 597 598
	spin_lock(&net->vl_updates_lock);
	list_del_init(&net->vl_updates);
	spin_unlock(&net->vl_updates_lock);
	mod_delayed_work(afs_vlocation_update_worker, &net->vl_updater, 0);
	mod_delayed_work(afs_wq, &net->vl_reaper, 0);
D
David Howells 已提交
599
}
L
Linus Torvalds 已提交
600 601

/*
602
 * update a volume location
L
Linus Torvalds 已提交
603
 */
604
void afs_vlocation_updater(struct work_struct *work)
L
Linus Torvalds 已提交
605 606
{
	struct afs_cache_vlocation vldb;
607
	struct afs_vlocation *vl, *xvl;
608
	struct afs_net *net = container_of(work, struct afs_net, vl_updater.work);
609
	time64_t now;
610
	long timeout;
L
Linus Torvalds 已提交
611 612
	int ret;

613 614 615
	if (!net->live)
		return;

616
	_enter("");
L
Linus Torvalds 已提交
617

618
	now = ktime_get_real_seconds();
L
Linus Torvalds 已提交
619

620
	/* find a record to update */
621
	spin_lock(&net->vl_updates_lock);
622
	for (;;) {
623 624
		if (list_empty(&net->vl_updates) || !net->live) {
			spin_unlock(&net->vl_updates_lock);
625 626 627
			_leave(" [nothing]");
			return;
		}
L
Linus Torvalds 已提交
628

629
		vl = list_entry(net->vl_updates.next,
630 631 632 633
				struct afs_vlocation, update);
		if (atomic_read(&vl->usage) > 0)
			break;
		list_del_init(&vl->update);
L
Linus Torvalds 已提交
634 635
	}

636 637 638
	timeout = vl->update_at - now;
	if (timeout > 0) {
		queue_delayed_work(afs_vlocation_update_worker,
639 640
				   &net->vl_updater, timeout * HZ);
		spin_unlock(&net->vl_updates_lock);
641
		_leave(" [nothing]");
L
Linus Torvalds 已提交
642 643 644
		return;
	}

645 646
	list_del_init(&vl->update);
	atomic_inc(&vl->usage);
647
	spin_unlock(&net->vl_updates_lock);
L
Linus Torvalds 已提交
648

649 650 651 652 653
	/* we can now perform the update */
	_debug("update %s", vl->vldb.name);
	vl->state = AFS_VL_UPDATING;
	vl->upd_rej_cnt = 0;
	vl->upd_busy_cnt = 0;
L
Linus Torvalds 已提交
654

D
David Howells 已提交
655
	ret = afs_vlocation_update_record(vl, NULL, &vldb);
656
	spin_lock(&vl->lock);
657 658 659 660 661 662 663 664 665 666 667 668
	switch (ret) {
	case 0:
		afs_vlocation_apply_update(vl, &vldb);
		vl->state = AFS_VL_VALID;
		break;
	case -ENOMEDIUM:
		vl->state = AFS_VL_VOLUME_DELETED;
		break;
	default:
		vl->state = AFS_VL_UNCERTAIN;
		break;
	}
669
	spin_unlock(&vl->lock);
670
	wake_up(&vl->waitq);
L
Linus Torvalds 已提交
671

672 673
	/* and then reschedule */
	_debug("reschedule");
674 675
	vl->update_at = ktime_get_real_seconds() +
			afs_vlocation_update_timeout;
L
Linus Torvalds 已提交
676

677
	spin_lock(&net->vl_updates_lock);
L
Linus Torvalds 已提交
678

679
	if (!list_empty(&net->vl_updates)) {
680 681 682 683
		/* next update in 10 minutes, but wait at least 1 second more
		 * than the newest record already queued so that we don't spam
		 * the VL server suddenly with lots of requests
		 */
684
		xvl = list_entry(net->vl_updates.prev,
685 686 687
				 struct afs_vlocation, update);
		if (vl->update_at <= xvl->update_at)
			vl->update_at = xvl->update_at + 1;
688
		xvl = list_entry(net->vl_updates.next,
689 690 691 692 693 694
				 struct afs_vlocation, update);
		timeout = xvl->update_at - now;
		if (timeout < 0)
			timeout = 0;
	} else {
		timeout = afs_vlocation_update_timeout;
L
Linus Torvalds 已提交
695 696
	}

697
	ASSERT(list_empty(&vl->update));
L
Linus Torvalds 已提交
698

699
	list_add_tail(&vl->update, &net->vl_updates);
L
Linus Torvalds 已提交
700

701
	_debug("timeout %ld", timeout);
702 703 704
	queue_delayed_work(afs_vlocation_update_worker, &net->vl_updater, timeout * HZ);
	spin_unlock(&net->vl_updates_lock);
	afs_put_vlocation(net, vl);
D
David Howells 已提交
705
}