vlocation.c 17.1 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 14 15 16
 * 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>
#include <linux/init.h>
#include "internal.h"

17 18
unsigned afs_vlocation_timeout = 10;	/* volume location timeout in seconds */
unsigned afs_vlocation_update_timeout = 10 * 60;
L
Linus Torvalds 已提交
19

20 21
static void afs_vlocation_reaper(struct work_struct *);
static void afs_vlocation_updater(struct work_struct *);
L
Linus Torvalds 已提交
22

23 24 25 26 27 28 29
static LIST_HEAD(afs_vlocation_updates);
static LIST_HEAD(afs_vlocation_graveyard);
static DEFINE_SPINLOCK(afs_vlocation_updates_lock);
static DEFINE_SPINLOCK(afs_vlocation_graveyard_lock);
static DECLARE_DELAYED_WORK(afs_vlocation_reap, afs_vlocation_reaper);
static DECLARE_DELAYED_WORK(afs_vlocation_update, afs_vlocation_updater);
static struct workqueue_struct *afs_vlocation_update_worker;
L
Linus Torvalds 已提交
30 31 32 33 34

/*
 * iterate through the VL servers in a cell until one of them admits knowing
 * about the volume in question
 */
35
static int afs_vlocation_access_vl_by_name(struct afs_vlocation *vl,
L
Linus Torvalds 已提交
36 37
					   struct afs_cache_vlocation *vldb)
{
38 39
	struct afs_cell *cell = vl->cell;
	struct in_addr addr;
L
Linus Torvalds 已提交
40 41
	int count, ret;

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

44
	down_write(&vl->cell->vl_sem);
L
Linus Torvalds 已提交
45 46
	ret = -ENOMEDIUM;
	for (count = cell->vl_naddrs; count > 0; count--) {
47 48 49
		addr = cell->vl_addrs[cell->vl_curr_svix];

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

		/* attempt to access the VL server */
52 53
		ret = afs_vl_get_entry_by_name(&addr, vl->vldb.name, vldb,
					       &afs_sync_call);
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67
		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:
			goto out;
		default:
68
			ret = -EIO;
L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77
			goto rotate;
		}

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

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

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

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

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

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

		/* attempt to access the VL server */
107 108
		ret = afs_vl_get_entry_by_id(&addr, volid, voltype, vldb,
					     &afs_sync_call);
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116 117 118 119
		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;
120 121 122 123 124 125 126 127 128 129 130 131
		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);
					__set_current_state(TASK_RUNNING);
				}
				continue;
			}
			break;
L
Linus Torvalds 已提交
132
		case -ENOMEDIUM:
133 134
			vl->upd_rej_cnt++;
			goto rotate;
L
Linus Torvalds 已提交
135
		default:
136
			ret = -EIO;
L
Linus Torvalds 已提交
137 138 139 140 141 142 143
			goto rotate;
		}

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

D
David Howells 已提交
147
out:
148 149 150 151 152 153 154 155 156
	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 已提交
157 158
	_leave(" = %d", ret);
	return ret;
D
David Howells 已提交
159
}
L
Linus Torvalds 已提交
160 161

/*
162
 * allocate a volume location record
L
Linus Torvalds 已提交
163
 */
164 165 166
static struct afs_vlocation *afs_vlocation_alloc(struct afs_cell *cell,
						 const char *name,
						 size_t namesz)
L
Linus Torvalds 已提交
167
{
168 169 170 171 172 173 174 175 176 177 178 179 180
	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);
		rwlock_init(&vl->lock);
		memcpy(vl->vldb.name, name, namesz);
L
Linus Torvalds 已提交
181 182
	}

183 184 185
	_leave(" = %p", vl);
	return vl;
}
L
Linus Torvalds 已提交
186

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

	/* 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) }",
199 200 201 202 203 204 205 206
	       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 已提交
207 208

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

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

228 229 230 231
	/* contact the server to make sure the volume is still available
	 * - TODO: need to handle disconnected operation here
	 */
	ret = afs_vlocation_access_vl_by_id(vl, vid, voltype, vldb);
L
Linus Torvalds 已提交
232 233 234
	switch (ret) {
		/* net error */
	default:
235 236 237 238 239
		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 已提交
240 241 242

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

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

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

258 259 260 261 262 263 264 265 266 267 268
/*
 * 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 已提交
269

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

273 274 275 276
	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 已提交
277

278
	vl->vldb = *vldb;
L
Linus Torvalds 已提交
279 280 281

#ifdef AFS_CACHING_SUPPORT
	/* update volume entry in local cache */
282
	cachefs_update_cookie(vl->cache);
L
Linus Torvalds 已提交
283
#endif
D
David Howells 已提交
284
}
L
Linus Torvalds 已提交
285 286

/*
287 288
 * fill in a volume location record, consulting the cache and the VL server
 * both
L
Linus Torvalds 已提交
289
 */
290
static int afs_vlocation_fill_in_record(struct afs_vlocation *vl)
L
Linus Torvalds 已提交
291
{
292 293
	struct afs_cache_vlocation vldb;
	int ret;
L
Linus Torvalds 已提交
294

295
	_enter("");
L
Linus Torvalds 已提交
296

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

299
	memset(&vldb, 0, sizeof(vldb));
L
Linus Torvalds 已提交
300

301 302 303 304 305 306 307
	/* see if we have an in-cache copy (will set vl->valid if there is) */
#ifdef AFS_CACHING_SUPPORT
	cachefs_acquire_cookie(cell->cache,
			       &afs_volume_cache_index_def,
			       vlocation,
			       &vl->cache);
#endif
L
Linus Torvalds 已提交
308

309 310 311 312 313 314 315 316 317 318 319 320 321 322
	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");
		ret = afs_vlocation_update_record(vl, &vldb);
	} else {
		/* try to look up an unknown volume in the cell VL databases by
		 * name */
		ret = afs_vlocation_access_vl_by_name(vl, &vldb);
		if (ret < 0) {
			printk("kAFS: failed to locate '%s' in cell '%s'\n",
			       vl->vldb.name, vl->cell->name);
			return ret;
		}
L
Linus Torvalds 已提交
323 324
	}

325 326 327
	afs_vlocation_apply_update(vl, &vldb);
	_leave(" = 0");
	return 0;
D
David Howells 已提交
328
}
L
Linus Torvalds 已提交
329 330

/*
331
 * queue a vlocation record for updates
L
Linus Torvalds 已提交
332
 */
333
void afs_vlocation_queue_for_updates(struct afs_vlocation *vl)
L
Linus Torvalds 已提交
334
{
335
	struct afs_vlocation *xvl;
L
Linus Torvalds 已提交
336

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	/* wait at least 10 minutes before updating... */
	vl->update_at = get_seconds() + afs_vlocation_update_timeout;

	spin_lock(&afs_vlocation_updates_lock);

	if (!list_empty(&afs_vlocation_updates)) {
		/* ... 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
		 */
		xvl = list_entry(afs_vlocation_updates.prev,
				 struct afs_vlocation, update);
		if (vl->update_at <= xvl->update_at)
			vl->update_at = xvl->update_at + 1;
	} else {
		queue_delayed_work(afs_vlocation_update_worker,
				   &afs_vlocation_update,
				   afs_vlocation_update_timeout * HZ);
L
Linus Torvalds 已提交
355
	}
356 357 358

	list_add_tail(&vl->update, &afs_vlocation_updates);
	spin_unlock(&afs_vlocation_updates_lock);
D
David Howells 已提交
359
}
L
Linus Torvalds 已提交
360 361

/*
362 363 364 365 366
 * 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 已提交
367
 */
368 369 370
struct afs_vlocation *afs_vlocation_lookup(struct afs_cell *cell,
					   const char *name,
					   size_t namesz)
L
Linus Torvalds 已提交
371
{
372 373
	struct afs_vlocation *vl;
	int ret;
L
Linus Torvalds 已提交
374

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

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

383 384 385 386 387 388 389 390
	/* 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 已提交
391
	}
392
	spin_unlock(&cell->vl_lock);
L
Linus Torvalds 已提交
393

394 395 396 397 398 399
	/* 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 已提交
400

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

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

407 408 409 410 411 412
fill_in_record:
	ret = afs_vlocation_fill_in_record(vl);
	if (ret < 0)
		goto error_abandon;
	vl->state = AFS_VL_VALID;
	wake_up(&vl->waitq);
L
Linus Torvalds 已提交
413

414 415 416
	/* schedule for regular updates */
	afs_vlocation_queue_for_updates(vl);
	goto success;
L
Linus Torvalds 已提交
417

418 419 420 421 422 423 424 425 426
found_in_memory:
	/* found in memory */
	_debug("found in memory");
	atomic_inc(&vl->usage);
	spin_unlock(&cell->vl_lock);
	if (!list_empty(&vl->grave)) {
		spin_lock(&afs_vlocation_graveyard_lock);
		list_del_init(&vl->grave);
		spin_unlock(&afs_vlocation_graveyard_lock);
L
Linus Torvalds 已提交
427
	}
428
	up_write(&cell->vl_sem);
L
Linus Torvalds 已提交
429

430 431 432
	/* see if it was an abandoned record that we might try filling in */
	while (vl->state != AFS_VL_VALID) {
		afs_vlocation_state_t state = vl->state;
L
Linus Torvalds 已提交
433

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

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
		if ((state == AFS_VL_NEW || state == AFS_VL_NO_VOLUME)) {
			if (cmpxchg(&vl->state, state, AFS_VL_CREATING) ==
			    state)
				goto fill_in_record;
			continue;
		}

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

		ret = wait_event_interruptible(
			vl->waitq,
			vl->state == AFS_VL_NEW ||
			vl->state == AFS_VL_VALID ||
			vl->state == AFS_VL_NO_VOLUME);
		if (ret < 0)
			goto error;
L
Linus Torvalds 已提交
454 455
	}

456 457 458 459 460 461 462 463 464 465
success:
	_leave(" = %p",vl);
	return vl;

error_abandon:
	vl->state = AFS_VL_NEW;
	wake_up(&vl->waitq);
error:
	ASSERT(vl != NULL);
	afs_put_vlocation(vl);
L
Linus Torvalds 已提交
466
	_leave(" = %d", ret);
467
	return ERR_PTR(ret);
D
David Howells 已提交
468
}
L
Linus Torvalds 已提交
469 470

/*
471
 * finish using a volume location record
L
Linus Torvalds 已提交
472
 */
473
void afs_put_vlocation(struct afs_vlocation *vl)
L
Linus Torvalds 已提交
474
{
475 476
	if (!vl)
		return;
L
Linus Torvalds 已提交
477

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

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

482 483 484 485
	if (likely(!atomic_dec_and_test(&vl->usage))) {
		_leave("");
		return;
	}
L
Linus Torvalds 已提交
486

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
	spin_lock(&afs_vlocation_graveyard_lock);
	if (atomic_read(&vl->usage) == 0) {
		_debug("buried");
		list_move_tail(&vl->grave, &afs_vlocation_graveyard);
		vl->time_of_death = get_seconds();
		schedule_delayed_work(&afs_vlocation_reap,
				      afs_vlocation_timeout * HZ);

		/* suspend updates on this record */
		if (!list_empty(&vl->update)) {
			spin_lock(&afs_vlocation_updates_lock);
			list_del_init(&vl->update);
			spin_unlock(&afs_vlocation_updates_lock);
		}
	}
	spin_unlock(&afs_vlocation_graveyard_lock);
	_leave(" [killed?]");
D
David Howells 已提交
504
}
L
Linus Torvalds 已提交
505 506

/*
507
 * destroy a dead volume location record
L
Linus Torvalds 已提交
508
 */
509
static void afs_vlocation_destroy(struct afs_vlocation *vl)
L
Linus Torvalds 已提交
510
{
511
	_enter("%p", vl);
L
Linus Torvalds 已提交
512

513 514 515
#ifdef AFS_CACHING_SUPPORT
	cachefs_relinquish_cookie(vl->cache, 0);
#endif
L
Linus Torvalds 已提交
516

517 518 519 520 521 522 523 524 525 526 527 528 529
	afs_put_cell(vl->cell);
	kfree(vl);
}

/*
 * reap dead volume location records
 */
static void afs_vlocation_reaper(struct work_struct *work)
{
	LIST_HEAD(corpses);
	struct afs_vlocation *vl;
	unsigned long delay, expiry;
	time_t now;
L
Linus Torvalds 已提交
530

531
	_enter("");
L
Linus Torvalds 已提交
532

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
	now = get_seconds();
	spin_lock(&afs_vlocation_graveyard_lock);

	while (!list_empty(&afs_vlocation_graveyard)) {
		vl = list_entry(afs_vlocation_graveyard.next,
				struct afs_vlocation, grave);

		_debug("check %p", vl);

		/* the queue is ordered most dead first */
		expiry = vl->time_of_death + afs_vlocation_timeout;
		if (expiry > now) {
			delay = (expiry - now) * HZ;
			_debug("delay %lu", delay);
			if (!schedule_delayed_work(&afs_vlocation_reap,
						   delay)) {
				cancel_delayed_work(&afs_vlocation_reap);
				schedule_delayed_work(&afs_vlocation_reap,
						      delay);
			}
			break;
		}
L
Linus Torvalds 已提交
555

556 557 558 559
		spin_lock(&vl->cell->vl_lock);
		if (atomic_read(&vl->usage) > 0) {
			_debug("no reap");
			list_del_init(&vl->grave);
D
David Howells 已提交
560
		} else {
561 562 563
			_debug("reap");
			list_move_tail(&vl->grave, &corpses);
			list_del_init(&vl->link);
L
Linus Torvalds 已提交
564
		}
565
		spin_unlock(&vl->cell->vl_lock);
L
Linus Torvalds 已提交
566 567
	}

568
	spin_unlock(&afs_vlocation_graveyard_lock);
L
Linus Torvalds 已提交
569

570 571 572 573 574
	/* now reap the corpses we've extracted */
	while (!list_empty(&corpses)) {
		vl = list_entry(corpses.next, struct afs_vlocation, grave);
		list_del(&vl->grave);
		afs_vlocation_destroy(vl);
L
Linus Torvalds 已提交
575 576 577
	}

	_leave("");
578
}
L
Linus Torvalds 已提交
579

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
/*
 * initialise the VL update process
 */
int __init afs_vlocation_update_init(void)
{
	afs_vlocation_update_worker =
		create_singlethread_workqueue("kafs_vlupdated");
	return afs_vlocation_update_worker ? 0 : -ENOMEM;
}

/*
 * discard all the volume location records for rmmod
 */
void __exit afs_vlocation_purge(void)
{
	afs_vlocation_timeout = 0;

	spin_lock(&afs_vlocation_updates_lock);
	list_del_init(&afs_vlocation_updates);
	spin_unlock(&afs_vlocation_updates_lock);
	cancel_delayed_work(&afs_vlocation_update);
	queue_delayed_work(afs_vlocation_update_worker,
			   &afs_vlocation_update, 0);
	destroy_workqueue(afs_vlocation_update_worker);

	cancel_delayed_work(&afs_vlocation_reap);
	schedule_delayed_work(&afs_vlocation_reap, 0);
D
David Howells 已提交
607
}
L
Linus Torvalds 已提交
608 609

/*
610
 * update a volume location
L
Linus Torvalds 已提交
611
 */
612
static void afs_vlocation_updater(struct work_struct *work)
L
Linus Torvalds 已提交
613 614
{
	struct afs_cache_vlocation vldb;
615 616 617
	struct afs_vlocation *vl, *xvl;
	time_t now;
	long timeout;
L
Linus Torvalds 已提交
618 619
	int ret;

620
	_enter("");
L
Linus Torvalds 已提交
621

622
	now = get_seconds();
L
Linus Torvalds 已提交
623

624 625 626 627 628 629 630 631
	/* find a record to update */
	spin_lock(&afs_vlocation_updates_lock);
	for (;;) {
		if (list_empty(&afs_vlocation_updates)) {
			spin_unlock(&afs_vlocation_updates_lock);
			_leave(" [nothing]");
			return;
		}
L
Linus Torvalds 已提交
632

633 634 635 636 637
		vl = list_entry(afs_vlocation_updates.next,
				struct afs_vlocation, update);
		if (atomic_read(&vl->usage) > 0)
			break;
		list_del_init(&vl->update);
L
Linus Torvalds 已提交
638 639
	}

640 641 642 643 644 645
	timeout = vl->update_at - now;
	if (timeout > 0) {
		queue_delayed_work(afs_vlocation_update_worker,
				   &afs_vlocation_update, timeout * HZ);
		spin_unlock(&afs_vlocation_updates_lock);
		_leave(" [nothing]");
L
Linus Torvalds 已提交
646 647 648
		return;
	}

649 650 651
	list_del_init(&vl->update);
	atomic_inc(&vl->usage);
	spin_unlock(&afs_vlocation_updates_lock);
L
Linus Torvalds 已提交
652

653 654 655 656 657
	/* 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 已提交
658

659 660 661 662 663 664 665 666 667 668 669 670 671
	ret = afs_vlocation_update_record(vl, &vldb);
	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;
	}
L
Linus Torvalds 已提交
672

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

677
	spin_lock(&afs_vlocation_updates_lock);
L
Linus Torvalds 已提交
678

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
	if (!list_empty(&afs_vlocation_updates)) {
		/* 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
		 */
		xvl = list_entry(afs_vlocation_updates.prev,
				 struct afs_vlocation, update);
		if (vl->update_at <= xvl->update_at)
			vl->update_at = xvl->update_at + 1;
		xvl = list_entry(afs_vlocation_updates.next,
				 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, &afs_vlocation_updates);
L
Linus Torvalds 已提交
700

701 702 703 704 705
	_debug("timeout %ld", timeout);
	queue_delayed_work(afs_vlocation_update_worker,
			   &afs_vlocation_update, timeout * HZ);
	spin_unlock(&afs_vlocation_updates_lock);
	afs_put_vlocation(vl);
D
David Howells 已提交
706
}