cookie.c 21.1 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* netfs cookie management
 *
 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
 * 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.
10 11 12
 *
 * See Documentation/filesystems/caching/netfs-api.txt for more information on
 * the netfs API.
13 14 15 16 17 18 19 20 21
 */

#define FSCACHE_DEBUG_LEVEL COOKIE
#include <linux/module.h>
#include <linux/slab.h>
#include "internal.h"

struct kmem_cache *fscache_cookie_jar;

22 23 24 25 26 27 28 29
static atomic_t fscache_object_debug_id = ATOMIC_INIT(0);

static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie);
static int fscache_alloc_object(struct fscache_cache *cache,
				struct fscache_cookie *cookie);
static int fscache_attach_object(struct fscache_cookie *cookie,
				 struct fscache_object *object);

30 31 32 33 34 35 36 37 38
/*
 * initialise an cookie jar slab element prior to any use
 */
void fscache_cookie_init_once(void *_cookie)
{
	struct fscache_cookie *cookie = _cookie;

	memset(cookie, 0, sizeof(*cookie));
	spin_lock_init(&cookie->lock);
39
	spin_lock_init(&cookie->stores_lock);
40 41 42
	INIT_HLIST_HEAD(&cookie->backing_objects);
}

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/*
 * request a cookie to represent an object (index, datafile, xattr, etc)
 * - parent specifies the parent object
 *   - the top level index cookie for each netfs is stored in the fscache_netfs
 *     struct upon registration
 * - def points to the definition
 * - the netfs_data will be passed to the functions pointed to in *def
 * - all attached caches will be searched to see if they contain this object
 * - index objects aren't stored on disk until there's a dependent file that
 *   needs storing
 * - other objects are stored in a selected cache immediately, and all the
 *   indices forming the path to it are instantiated if necessary
 * - we never let on to the netfs about errors
 *   - we may set a negative cookie pointer, but that's okay
 */
struct fscache_cookie *__fscache_acquire_cookie(
	struct fscache_cookie *parent,
	const struct fscache_cookie_def *def,
61 62
	const void *index_key, size_t index_key_len,
	const void *aux_data, size_t aux_data_len,
63 64
	void *netfs_data,
	bool enable)
65 66 67 68 69
{
	struct fscache_cookie *cookie;

	BUG_ON(!def);

70
	_enter("{%s},{%s},%p,%u",
71
	       parent ? (char *) parent->def->name : "<no-parent>",
72
	       def->name, netfs_data, enable);
73

74 75 76 77 78 79 80
	if (!index_key || !index_key_len || index_key_len > 255 || aux_data_len > 255)
		return NULL;
	if (!aux_data || !aux_data_len) {
		aux_data = NULL;
		aux_data_len = 0;
	}

81 82 83 84 85 86 87 88 89 90 91 92 93
	fscache_stat(&fscache_n_acquires);

	/* if there's no parent cookie, then we don't create one here either */
	if (!parent) {
		fscache_stat(&fscache_n_acquires_null);
		_leave(" [no parent]");
		return NULL;
	}

	/* validate the definition */
	BUG_ON(!def->name[0]);

	BUG_ON(def->type == FSCACHE_COOKIE_TYPE_INDEX &&
94
	       parent->type != FSCACHE_COOKIE_TYPE_INDEX);
95 96 97 98 99 100 101 102 103

	/* allocate and initialise a cookie */
	cookie = kmem_cache_alloc(fscache_cookie_jar, GFP_KERNEL);
	if (!cookie) {
		fscache_stat(&fscache_n_acquires_oom);
		_leave(" [ENOMEM]");
		return NULL;
	}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	cookie->key_len		= index_key_len;
	cookie->aux_len		= aux_data_len;

	if (cookie->key_len <= sizeof(cookie->inline_key)) {
		memcpy(cookie->inline_key, index_key, cookie->key_len);
	} else {
		cookie->key = kmemdup(index_key, cookie->key_len, GFP_KERNEL);
		if (!cookie->key)
			goto nomem;
	}

	if (cookie->aux_len <= sizeof(cookie->inline_aux)) {
		memcpy(cookie->inline_aux, aux_data, cookie->aux_len);
	} else {
		cookie->aux = kmemdup(aux_data, cookie->aux_len, GFP_KERNEL);
		if (!cookie->aux)
			goto nomem;
	}

123 124 125
	atomic_set(&cookie->usage, 1);
	atomic_set(&cookie->n_children, 0);

126 127 128 129 130
	/* We keep the active count elevated until relinquishment to prevent an
	 * attempt to wake up every time the object operations queue quiesces.
	 */
	atomic_set(&cookie->n_active, 1);

D
David Howells 已提交
131
	fscache_cookie_get(parent, fscache_cookie_get_acquire_parent);
132 133 134 135 136
	atomic_inc(&parent->n_children);

	cookie->def		= def;
	cookie->parent		= parent;
	cookie->netfs_data	= netfs_data;
137
	cookie->flags		= (1 << FSCACHE_COOKIE_NO_DATA_YET);
138 139
	cookie->type		= def->type;
	
140 141
	/* radix tree insertion won't use the preallocation pool unless it's
	 * told it may not wait */
142
	INIT_RADIX_TREE(&cookie->stores, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
143

144
	switch (cookie->type) {
145 146 147 148 149 150 151 152 153 154 155
	case FSCACHE_COOKIE_TYPE_INDEX:
		fscache_stat(&fscache_n_cookie_index);
		break;
	case FSCACHE_COOKIE_TYPE_DATAFILE:
		fscache_stat(&fscache_n_cookie_data);
		break;
	default:
		fscache_stat(&fscache_n_cookie_special);
		break;
	}

D
David Howells 已提交
156 157
	trace_fscache_acquire(cookie);

158 159 160 161
	if (enable) {
		/* if the object is an index then we need do nothing more here
		 * - we create indices on disk when we need them as an index
		 * may exist in multiple caches */
162
		if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) {
163 164 165 166
			if (fscache_acquire_non_index_cookie(cookie) == 0) {
				set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
			} else {
				atomic_dec(&parent->n_children);
D
David Howells 已提交
167 168
				fscache_cookie_put(cookie,
						   fscache_cookie_put_acquire_nobufs);
169 170 171 172 173 174
				fscache_stat(&fscache_n_acquires_nobufs);
				_leave(" = NULL");
				return NULL;
			}
		} else {
			set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
175 176 177 178 179 180
		}
	}

	fscache_stat(&fscache_n_acquires_ok);
	_leave(" = %p", cookie);
	return cookie;
181 182 183 184 185 186 187 188

nomem:
	if (cookie->aux_len > sizeof(cookie->inline_aux))
		kfree(cookie->aux);
	if (cookie->key_len > sizeof(cookie->inline_key))
		kfree(cookie->key);
	kmem_cache_free(fscache_cookie_jar, cookie);
	return NULL;
189 190 191
}
EXPORT_SYMBOL(__fscache_acquire_cookie);

192 193 194 195
/*
 * Enable a cookie to permit it to accept new operations.
 */
void __fscache_enable_cookie(struct fscache_cookie *cookie,
196
			     const void *aux_data,
197 198 199 200 201
			     bool (*can_enable)(void *data),
			     void *data)
{
	_enter("%p", cookie);

D
David Howells 已提交
202 203
	trace_fscache_enable(cookie);

204
	wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
205
			 TASK_UNINTERRUPTIBLE);
206

207 208
	fscache_update_aux(cookie, aux_data);

209 210 211 212 213
	if (test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
		goto out_unlock;

	if (can_enable && !can_enable(data)) {
		/* The netfs decided it didn't want to enable after all */
214
	} else if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) {
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
		/* Wait for outstanding disablement to complete */
		__fscache_wait_on_invalidate(cookie);

		if (fscache_acquire_non_index_cookie(cookie) == 0)
			set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
	} else {
		set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
	}

out_unlock:
	clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
	wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
}
EXPORT_SYMBOL(__fscache_enable_cookie);

230 231 232 233 234 235 236 237 238 239 240 241 242 243
/*
 * acquire a non-index cookie
 * - this must make sure the index chain is instantiated and instantiate the
 *   object representation too
 */
static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie)
{
	struct fscache_object *object;
	struct fscache_cache *cache;
	uint64_t i_size;
	int ret;

	_enter("");

244
	set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

	/* now we need to see whether the backing objects for this cookie yet
	 * exist, if not there'll be nothing to search */
	down_read(&fscache_addremove_sem);

	if (list_empty(&fscache_cache_list)) {
		up_read(&fscache_addremove_sem);
		_leave(" = 0 [no caches]");
		return 0;
	}

	/* select a cache in which to store the object */
	cache = fscache_select_cache_for_object(cookie->parent);
	if (!cache) {
		up_read(&fscache_addremove_sem);
		fscache_stat(&fscache_n_acquires_no_cache);
		_leave(" = -ENOMEDIUM [no cache]");
		return -ENOMEDIUM;
	}

	_debug("cache %s", cache->tag->name);

267
	set_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

	/* ask the cache to allocate objects for this cookie and its parent
	 * chain */
	ret = fscache_alloc_object(cache, cookie);
	if (ret < 0) {
		up_read(&fscache_addremove_sem);
		_leave(" = %d", ret);
		return ret;
	}

	/* pass on how big the object we're caching is supposed to be */
	cookie->def->get_attr(cookie->netfs_data, &i_size);

	spin_lock(&cookie->lock);
	if (hlist_empty(&cookie->backing_objects)) {
		spin_unlock(&cookie->lock);
		goto unavailable;
	}

	object = hlist_entry(cookie->backing_objects.first,
			     struct fscache_object, cookie_link);

	fscache_set_store_limit(object, i_size);

	/* initiate the process of looking up all the objects in the chain
	 * (done by fscache_initialise_object()) */
294
	fscache_raise_event(object, FSCACHE_OBJECT_EV_NEW_CHILD);
295 296 297 298 299 300 301

	spin_unlock(&cookie->lock);

	/* we may be required to wait for lookup to complete at this point */
	if (!fscache_defer_lookup) {
		_debug("non-deferred lookup %p", &cookie->flags);
		wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
302
			    TASK_UNINTERRUPTIBLE);
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		_debug("complete");
		if (test_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags))
			goto unavailable;
	}

	up_read(&fscache_addremove_sem);
	_leave(" = 0 [deferred]");
	return 0;

unavailable:
	up_read(&fscache_addremove_sem);
	_leave(" = -ENOBUFS");
	return -ENOBUFS;
}

/*
 * recursively allocate cache object records for a cookie/cache combination
 * - caller must be holding the addremove sem
 */
static int fscache_alloc_object(struct fscache_cache *cache,
				struct fscache_cookie *cookie)
{
	struct fscache_object *object;
	int ret;

	_enter("%p,%p{%s}", cache, cookie, cookie->def->name);

	spin_lock(&cookie->lock);
331
	hlist_for_each_entry(object, &cookie->backing_objects,
332 333 334 335 336 337 338 339
			     cookie_link) {
		if (object->cache == cache)
			goto object_already_extant;
	}
	spin_unlock(&cookie->lock);

	/* ask the cache to allocate an object (we may end up with duplicate
	 * objects at this stage, but we sort that out later) */
340
	fscache_stat(&fscache_n_cop_alloc_object);
341
	object = cache->ops->alloc_object(cache, cookie);
342
	fscache_stat_d(&fscache_n_cop_alloc_object);
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	if (IS_ERR(object)) {
		fscache_stat(&fscache_n_object_no_alloc);
		ret = PTR_ERR(object);
		goto error;
	}

	fscache_stat(&fscache_n_object_alloc);

	object->debug_id = atomic_inc_return(&fscache_object_debug_id);

	_debug("ALLOC OBJ%x: %s {%lx}",
	       object->debug_id, cookie->def->name, object->events);

	ret = fscache_alloc_object(cache, cookie->parent);
	if (ret < 0)
		goto error_put;

	/* only attach if we managed to allocate all we needed, otherwise
	 * discard the object we just allocated and instead use the one
	 * attached to the cookie */
363 364
	if (fscache_attach_object(cookie, object) < 0) {
		fscache_stat(&fscache_n_cop_put_object);
D
David Howells 已提交
365
		cache->ops->put_object(object, fscache_obj_put_attach_fail);
366 367
		fscache_stat_d(&fscache_n_cop_put_object);
	}
368 369 370 371 372 373

	_leave(" = 0");
	return 0;

object_already_extant:
	ret = -ENOBUFS;
374 375
	if (fscache_object_is_dying(object) ||
	    fscache_cache_is_broken(object)) {
376 377 378 379 380 381 382 383
		spin_unlock(&cookie->lock);
		goto error;
	}
	spin_unlock(&cookie->lock);
	_leave(" = 0 [found]");
	return 0;

error_put:
384
	fscache_stat(&fscache_n_cop_put_object);
D
David Howells 已提交
385
	cache->ops->put_object(object, fscache_obj_put_alloc_fail);
386
	fscache_stat_d(&fscache_n_cop_put_object);
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
error:
	_leave(" = %d", ret);
	return ret;
}

/*
 * attach a cache object to a cookie
 */
static int fscache_attach_object(struct fscache_cookie *cookie,
				 struct fscache_object *object)
{
	struct fscache_object *p;
	struct fscache_cache *cache = object->cache;
	int ret;

	_enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id);

	spin_lock(&cookie->lock);

	/* there may be multiple initial creations of this object, but we only
	 * want one */
	ret = -EEXIST;
409
	hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) {
410
		if (p->cache == object->cache) {
411
			if (fscache_object_is_dying(p))
412 413 414 415 416 417 418
				ret = -ENOBUFS;
			goto cant_attach_object;
		}
	}

	/* pin the parent object */
	spin_lock_nested(&cookie->parent->lock, 1);
419
	hlist_for_each_entry(p, &cookie->parent->backing_objects,
420 421
			     cookie_link) {
		if (p->cache == object->cache) {
422
			if (fscache_object_is_dying(p)) {
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
				ret = -ENOBUFS;
				spin_unlock(&cookie->parent->lock);
				goto cant_attach_object;
			}
			object->parent = p;
			spin_lock(&p->lock);
			p->n_children++;
			spin_unlock(&p->lock);
			break;
		}
	}
	spin_unlock(&cookie->parent->lock);

	/* attach to the cache's object list */
	if (list_empty(&object->cache_link)) {
		spin_lock(&cache->object_list_lock);
		list_add(&object->cache_link, &cache->object_list);
		spin_unlock(&cache->object_list_lock);
	}

	/* attach to the cookie */
	object->cookie = cookie;
D
David Howells 已提交
445
	fscache_cookie_get(cookie, fscache_cookie_get_attach_object);
446
	hlist_add_head(&object->cookie_link, &cookie->backing_objects);
447 448

	fscache_objlist_add(object);
449 450 451 452 453 454 455 456
	ret = 0;

cant_attach_object:
	spin_unlock(&cookie->lock);
	_leave(" = %d", ret);
	return ret;
}

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
/*
 * Invalidate an object.  Callable with spinlocks held.
 */
void __fscache_invalidate(struct fscache_cookie *cookie)
{
	struct fscache_object *object;

	_enter("{%s}", cookie->def->name);

	fscache_stat(&fscache_n_invalidates);

	/* Only permit invalidation of data files.  Invalidating an index will
	 * require the caller to release all its attachments to the tree rooted
	 * there, and if it's doing that, it may as well just retire the
	 * cookie.
	 */
473
	ASSERTCMP(cookie->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
474 475 476 477 478 479 480

	/* If there's an object, we tell the object state machine to handle the
	 * invalidation on our behalf, otherwise there's nothing to do.
	 */
	if (!hlist_empty(&cookie->backing_objects)) {
		spin_lock(&cookie->lock);

481 482
		if (fscache_cookie_enabled(cookie) &&
		    !hlist_empty(&cookie->backing_objects) &&
483 484 485 486 487
		    !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING,
				      &cookie->flags)) {
			object = hlist_entry(cookie->backing_objects.first,
					     struct fscache_object,
					     cookie_link);
488
			if (fscache_object_is_live(object))
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
				fscache_raise_event(
					object, FSCACHE_OBJECT_EV_INVALIDATE);
		}

		spin_unlock(&cookie->lock);
	}

	_leave("");
}
EXPORT_SYMBOL(__fscache_invalidate);

/*
 * Wait for object invalidation to complete.
 */
void __fscache_wait_on_invalidate(struct fscache_cookie *cookie)
{
	_enter("%p", cookie);

	wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING,
		    TASK_UNINTERRUPTIBLE);

	_leave("");
}
EXPORT_SYMBOL(__fscache_wait_on_invalidate);

514 515 516
/*
 * update the index entries backing a cookie
 */
517
void __fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data)
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
{
	struct fscache_object *object;

	fscache_stat(&fscache_n_updates);

	if (!cookie) {
		fscache_stat(&fscache_n_updates_null);
		_leave(" [no cookie]");
		return;
	}

	_enter("{%s}", cookie->def->name);

	spin_lock(&cookie->lock);

533 534
	fscache_update_aux(cookie, aux_data);

535 536 537 538 539 540 541 542
	if (fscache_cookie_enabled(cookie)) {
		/* update the index entry on disk in each cache backing this
		 * cookie.
		 */
		hlist_for_each_entry(object,
				     &cookie->backing_objects, cookie_link) {
			fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
		}
543 544 545 546 547 548 549 550
	}

	spin_unlock(&cookie->lock);
	_leave("");
}
EXPORT_SYMBOL(__fscache_update_cookie);

/*
551
 * Disable a cookie to stop it from accepting new requests from the netfs.
552
 */
553 554 555
void __fscache_disable_cookie(struct fscache_cookie *cookie,
			      const void *aux_data,
			      bool invalidate)
556 557
{
	struct fscache_object *object;
558
	bool awaken = false;
559

560
	_enter("%p,%u", cookie, invalidate);
561

D
David Howells 已提交
562 563
	trace_fscache_disable(cookie);

564
	ASSERTCMP(atomic_read(&cookie->n_active), >, 0);
565 566

	if (atomic_read(&cookie->n_children) != 0) {
567
		pr_err("Cookie '%s' still has children\n",
568 569 570 571
		       cookie->def->name);
		BUG();
	}

572
	wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
573
			 TASK_UNINTERRUPTIBLE);
574 575 576

	fscache_update_aux(cookie, aux_data);

577 578 579 580 581 582 583 584 585 586
	if (!test_and_clear_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
		goto out_unlock_enable;

	/* If the cookie is being invalidated, wait for that to complete first
	 * so that we can reuse the flag.
	 */
	__fscache_wait_on_invalidate(cookie);

	/* Dispose of the backing objects */
	set_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags);
587 588

	spin_lock(&cookie->lock);
589 590 591 592
	if (!hlist_empty(&cookie->backing_objects)) {
		hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) {
			if (invalidate)
				set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
593
			clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
594 595 596 597 598
			fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
		}
	} else {
		if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
			awaken = true;
599
	}
600
	spin_unlock(&cookie->lock);
601 602
	if (awaken)
		wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
603

604
	/* Wait for cessation of activity requiring access to the netfs (when
605 606
	 * n_active reaches 0).  This makes sure outstanding reads and writes
	 * have completed.
607
	 */
608 609 610 611
	if (!atomic_dec_and_test(&cookie->n_active)) {
		wait_var_event(&cookie->n_active,
			       !atomic_read(&cookie->n_active));
	}
612

613
	/* Make sure any pending writes are cancelled. */
614
	if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX)
615 616
		fscache_invalidate_writes(cookie);

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	/* Reset the cookie state if it wasn't relinquished */
	if (!test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags)) {
		atomic_inc(&cookie->n_active);
		set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
	}

out_unlock_enable:
	clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
	wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
	_leave("");
}
EXPORT_SYMBOL(__fscache_disable_cookie);

/*
 * release a cookie back to the cache
 * - the object will be marked as recyclable on disk if retire is true
 * - all dependents of this cookie must have already been unregistered
 *   (indices/files/pages)
 */
636 637 638
void __fscache_relinquish_cookie(struct fscache_cookie *cookie,
				 const void *aux_data,
				 bool retire)
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
{
	fscache_stat(&fscache_n_relinquishes);
	if (retire)
		fscache_stat(&fscache_n_relinquishes_retire);

	if (!cookie) {
		fscache_stat(&fscache_n_relinquishes_null);
		_leave(" [no cookie]");
		return;
	}

	_enter("%p{%s,%p,%d},%d",
	       cookie, cookie->def->name, cookie->netfs_data,
	       atomic_read(&cookie->n_active), retire);

D
David Howells 已提交
654 655
	trace_fscache_relinquish(cookie, retire);

656
	/* No further netfs-accessing operations on this cookie permitted */
657 658
	if (test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags))
		BUG();
659

660
	__fscache_disable_cookie(cookie, aux_data, retire);
661

662
	/* Clear pointers back to the netfs */
663 664
	cookie->netfs_data	= NULL;
	cookie->def		= NULL;
665
	BUG_ON(cookie->stores.rnode);
666 667 668 669 670 671 672

	if (cookie->parent) {
		ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0);
		ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0);
		atomic_dec(&cookie->parent->n_children);
	}

673
	/* Dispose of the netfs's link to the cookie */
674
	ASSERTCMP(atomic_read(&cookie->usage), >, 0);
D
David Howells 已提交
675
	fscache_cookie_put(cookie, fscache_cookie_put_relinquish);
676 677 678 679 680

	_leave("");
}
EXPORT_SYMBOL(__fscache_relinquish_cookie);

681
/*
D
David Howells 已提交
682
 * Drop a reference to a cookie.
683
 */
D
David Howells 已提交
684 685
void fscache_cookie_put(struct fscache_cookie *cookie,
			enum fscache_cookie_trace where)
686 687
{
	struct fscache_cookie *parent;
D
David Howells 已提交
688
	int usage;
689 690 691

	_enter("%p", cookie);

D
David Howells 已提交
692 693 694 695 696 697 698 699
	do {
		usage = atomic_dec_return(&cookie->usage);
		trace_fscache_cookie(cookie, where, usage);

		if (usage > 0)
			return;
		BUG_ON(usage < 0);

700 701
		parent = cookie->parent;
		BUG_ON(!hlist_empty(&cookie->backing_objects));
702 703 704 705
		if (cookie->aux_len > sizeof(cookie->inline_aux))
			kfree(cookie->aux);
		if (cookie->key_len > sizeof(cookie->inline_key))
			kfree(cookie->key);
706 707 708
		kmem_cache_free(fscache_cookie_jar, cookie);

		cookie = parent;
D
David Howells 已提交
709 710
		where = fscache_cookie_put_parent;
	} while (cookie);
711 712 713

	_leave("");
}
714 715 716 717 718 719

/*
 * check the consistency between the netfs inode and the backing cache
 *
 * NOTE: it only serves no-index type
 */
720 721
int __fscache_check_consistency(struct fscache_cookie *cookie,
				const void *aux_data)
722 723 724
{
	struct fscache_operation *op;
	struct fscache_object *object;
725
	bool wake_cookie = false;
726 727 728 729
	int ret;

	_enter("%p,", cookie);

730
	ASSERTCMP(cookie->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
731 732 733 734 735 736 737 738 739 740 741

	if (fscache_wait_for_deferred_lookup(cookie) < 0)
		return -ERESTARTSYS;

	if (hlist_empty(&cookie->backing_objects))
		return 0;

	op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
	if (!op)
		return -ENOMEM;

D
David Howells 已提交
742
	fscache_operation_init(cookie, op, NULL, NULL, NULL);
743
	op->flags = FSCACHE_OP_MYTHREAD |
744 745
		(1 << FSCACHE_OP_WAITING) |
		(1 << FSCACHE_OP_UNUSE_COOKIE);
D
David Howells 已提交
746
	trace_fscache_page_op(cookie, NULL, op, fscache_page_op_check_consistency);
747 748 749

	spin_lock(&cookie->lock);

750 751
	fscache_update_aux(cookie, aux_data);

752 753
	if (!fscache_cookie_enabled(cookie) ||
	    hlist_empty(&cookie->backing_objects))
754 755 756 757 758 759 760 761
		goto inconsistent;
	object = hlist_entry(cookie->backing_objects.first,
			     struct fscache_object, cookie_link);
	if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
		goto inconsistent;

	op->debug_id = atomic_inc_return(&fscache_op_debug_id);

762
	__fscache_use_cookie(cookie);
763 764 765 766 767 768
	if (fscache_submit_op(object, op) < 0)
		goto submit_failed;

	/* the work queue now carries its own ref on the object */
	spin_unlock(&cookie->lock);

769
	ret = fscache_wait_for_operation_activation(object, op, NULL, NULL);
770 771 772 773 774 775 776 777 778 779 780 781 782
	if (ret == 0) {
		/* ask the cache to honour the operation */
		ret = object->cache->ops->check_consistency(op);
		fscache_op_complete(op, false);
	} else if (ret == -ENOBUFS) {
		ret = 0;
	}

	fscache_put_operation(op);
	_leave(" = %d", ret);
	return ret;

submit_failed:
783
	wake_cookie = __fscache_unuse_cookie(cookie);
784 785
inconsistent:
	spin_unlock(&cookie->lock);
786 787
	if (wake_cookie)
		__fscache_wake_unused_cookie(cookie);
788 789 790 791 792
	kfree(op);
	_leave(" = -ESTALE");
	return -ESTALE;
}
EXPORT_SYMBOL(__fscache_check_consistency);