Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Mar 24-27, 2025, special US time zones
Register
Loading...
v4.6
   1/* FS-Cache object state machine handler
   2 *
   3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
   4 * Written by David Howells (dhowells@redhat.com)
   5 *
   6 * This program is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU General Public License
   8 * as published by the Free Software Foundation; either version
   9 * 2 of the License, or (at your option) any later version.
  10 *
  11 * See Documentation/filesystems/caching/object.txt for a description of the
  12 * object state machine and the in-kernel representations.
  13 */
  14
  15#define FSCACHE_DEBUG_LEVEL COOKIE
  16#include <linux/module.h>
  17#include <linux/slab.h>
  18#include <linux/prefetch.h>
  19#include "internal.h"
  20
  21static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *, int);
  22static const struct fscache_state *fscache_kill_dependents(struct fscache_object *, int);
  23static const struct fscache_state *fscache_drop_object(struct fscache_object *, int);
  24static const struct fscache_state *fscache_initialise_object(struct fscache_object *, int);
  25static const struct fscache_state *fscache_invalidate_object(struct fscache_object *, int);
  26static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *, int);
  27static const struct fscache_state *fscache_kill_object(struct fscache_object *, int);
  28static const struct fscache_state *fscache_lookup_failure(struct fscache_object *, int);
  29static const struct fscache_state *fscache_look_up_object(struct fscache_object *, int);
  30static const struct fscache_state *fscache_object_available(struct fscache_object *, int);
  31static const struct fscache_state *fscache_parent_ready(struct fscache_object *, int);
  32static const struct fscache_state *fscache_update_object(struct fscache_object *, int);
  33
  34#define __STATE_NAME(n) fscache_osm_##n
  35#define STATE(n) (&__STATE_NAME(n))
  36
  37/*
  38 * Define a work state.  Work states are execution states.  No event processing
  39 * is performed by them.  The function attached to a work state returns a
  40 * pointer indicating the next state to which the state machine should
  41 * transition.  Returning NO_TRANSIT repeats the current state, but goes back
  42 * to the scheduler first.
  43 */
  44#define WORK_STATE(n, sn, f) \
  45	const struct fscache_state __STATE_NAME(n) = {			\
  46		.name = #n,						\
  47		.short_name = sn,					\
  48		.work = f						\
  49	}
  50
  51/*
  52 * Returns from work states.
  53 */
  54#define transit_to(state) ({ prefetch(&STATE(state)->work); STATE(state); })
  55
  56#define NO_TRANSIT ((struct fscache_state *)NULL)
  57
  58/*
  59 * Define a wait state.  Wait states are event processing states.  No execution
  60 * is performed by them.  Wait states are just tables of "if event X occurs,
  61 * clear it and transition to state Y".  The dispatcher returns to the
  62 * scheduler if none of the events in which the wait state has an interest are
  63 * currently pending.
  64 */
  65#define WAIT_STATE(n, sn, ...) \
  66	const struct fscache_state __STATE_NAME(n) = {			\
  67		.name = #n,						\
  68		.short_name = sn,					\
  69		.work = NULL,						\
  70		.transitions = { __VA_ARGS__, { 0, NULL } }		\
  71	}
  72
  73#define TRANSIT_TO(state, emask) \
  74	{ .events = (emask), .transit_to = STATE(state) }
  75
  76/*
  77 * The object state machine.
  78 */
  79static WORK_STATE(INIT_OBJECT,		"INIT", fscache_initialise_object);
  80static WORK_STATE(PARENT_READY,		"PRDY", fscache_parent_ready);
  81static WORK_STATE(ABORT_INIT,		"ABRT", fscache_abort_initialisation);
  82static WORK_STATE(LOOK_UP_OBJECT,	"LOOK", fscache_look_up_object);
  83static WORK_STATE(CREATE_OBJECT,	"CRTO", fscache_look_up_object);
  84static WORK_STATE(OBJECT_AVAILABLE,	"AVBL", fscache_object_available);
  85static WORK_STATE(JUMPSTART_DEPS,	"JUMP", fscache_jumpstart_dependents);
  86
  87static WORK_STATE(INVALIDATE_OBJECT,	"INVL", fscache_invalidate_object);
  88static WORK_STATE(UPDATE_OBJECT,	"UPDT", fscache_update_object);
  89
  90static WORK_STATE(LOOKUP_FAILURE,	"LCFL", fscache_lookup_failure);
  91static WORK_STATE(KILL_OBJECT,		"KILL", fscache_kill_object);
  92static WORK_STATE(KILL_DEPENDENTS,	"KDEP", fscache_kill_dependents);
  93static WORK_STATE(DROP_OBJECT,		"DROP", fscache_drop_object);
  94static WORK_STATE(OBJECT_DEAD,		"DEAD", (void*)2UL);
  95
  96static WAIT_STATE(WAIT_FOR_INIT,	"?INI",
  97		  TRANSIT_TO(INIT_OBJECT,	1 << FSCACHE_OBJECT_EV_NEW_CHILD));
  98
  99static WAIT_STATE(WAIT_FOR_PARENT,	"?PRN",
 100		  TRANSIT_TO(PARENT_READY,	1 << FSCACHE_OBJECT_EV_PARENT_READY));
 101
 102static WAIT_STATE(WAIT_FOR_CMD,		"?CMD",
 103		  TRANSIT_TO(INVALIDATE_OBJECT,	1 << FSCACHE_OBJECT_EV_INVALIDATE),
 104		  TRANSIT_TO(UPDATE_OBJECT,	1 << FSCACHE_OBJECT_EV_UPDATE),
 105		  TRANSIT_TO(JUMPSTART_DEPS,	1 << FSCACHE_OBJECT_EV_NEW_CHILD));
 106
 107static WAIT_STATE(WAIT_FOR_CLEARANCE,	"?CLR",
 108		  TRANSIT_TO(KILL_OBJECT,	1 << FSCACHE_OBJECT_EV_CLEARED));
 109
 110/*
 111 * Out-of-band event transition tables.  These are for handling unexpected
 112 * events, such as an I/O error.  If an OOB event occurs, the state machine
 113 * clears and disables the event and forces a transition to the nominated work
 114 * state (acurrently executing work states will complete first).
 115 *
 116 * In such a situation, object->state remembers the state the machine should
 117 * have been in/gone to and returning NO_TRANSIT returns to that.
 118 */
 119static const struct fscache_transition fscache_osm_init_oob[] = {
 120	   TRANSIT_TO(ABORT_INIT,
 121		      (1 << FSCACHE_OBJECT_EV_ERROR) |
 122		      (1 << FSCACHE_OBJECT_EV_KILL)),
 123	   { 0, NULL }
 124};
 
 125
 126static const struct fscache_transition fscache_osm_lookup_oob[] = {
 127	   TRANSIT_TO(LOOKUP_FAILURE,
 128		      (1 << FSCACHE_OBJECT_EV_ERROR) |
 129		      (1 << FSCACHE_OBJECT_EV_KILL)),
 130	   { 0, NULL }
 131};
 132
 133static const struct fscache_transition fscache_osm_run_oob[] = {
 134	   TRANSIT_TO(KILL_OBJECT,
 135		      (1 << FSCACHE_OBJECT_EV_ERROR) |
 136		      (1 << FSCACHE_OBJECT_EV_KILL)),
 137	   { 0, NULL }
 
 
 138};
 139
 140static int  fscache_get_object(struct fscache_object *);
 141static void fscache_put_object(struct fscache_object *);
 142static bool fscache_enqueue_dependents(struct fscache_object *, int);
 
 
 
 
 
 143static void fscache_dequeue_object(struct fscache_object *);
 144
 145/*
 146 * we need to notify the parent when an op completes that we had outstanding
 147 * upon it
 148 */
 149static inline void fscache_done_parent_op(struct fscache_object *object)
 150{
 151	struct fscache_object *parent = object->parent;
 152
 153	_enter("OBJ%x {OBJ%x,%x}",
 154	       object->debug_id, parent->debug_id, parent->n_ops);
 155
 156	spin_lock_nested(&parent->lock, 1);
 157	parent->n_obj_ops--;
 158	parent->n_ops--;
 
 159	if (parent->n_ops == 0)
 160		fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
 161	spin_unlock(&parent->lock);
 162}
 163
 164/*
 165 * Object state machine dispatcher.
 
 
 
 
 
 166 */
 167static void fscache_object_sm_dispatcher(struct fscache_object *object)
 168{
 169	const struct fscache_transition *t;
 170	const struct fscache_state *state, *new_state;
 171	unsigned long events, event_mask;
 172	int event = -1;
 173
 174	ASSERT(object != NULL);
 175
 176	_enter("{OBJ%x,%s,%lx}",
 177	       object->debug_id, object->state->name, object->events);
 
 178
 179	event_mask = object->event_mask;
 180restart:
 181	object->event_mask = 0; /* Mask normal event handling */
 182	state = object->state;
 183restart_masked:
 184	events = object->events;
 185
 186	/* Handle any out-of-band events (typically an error) */
 187	if (events & object->oob_event_mask) {
 188		_debug("{OBJ%x} oob %lx",
 189		       object->debug_id, events & object->oob_event_mask);
 190		for (t = object->oob_table; t->events; t++) {
 191			if (events & t->events) {
 192				state = t->transit_to;
 193				ASSERT(state->work != NULL);
 194				event = fls(events & t->events) - 1;
 195				__clear_bit(event, &object->oob_event_mask);
 196				clear_bit(event, &object->events);
 197				goto execute_work_state;
 198			}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 199		}
 200	}
 201
 202	/* Wait states are just transition tables */
 203	if (!state->work) {
 204		if (events & event_mask) {
 205			for (t = state->transitions; t->events; t++) {
 206				if (events & t->events) {
 207					new_state = t->transit_to;
 208					event = fls(events & t->events) - 1;
 209					clear_bit(event, &object->events);
 210					_debug("{OBJ%x} ev %d: %s -> %s",
 211					       object->debug_id, event,
 212					       state->name, new_state->name);
 213					object->state = state = new_state;
 214					goto execute_work_state;
 215				}
 216			}
 217
 218			/* The event mask didn't include all the tabled bits */
 219			BUG();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 220		}
 221		/* Randomly woke up */
 222		goto unmask_events;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 223	}
 224
 225execute_work_state:
 226	_debug("{OBJ%x} exec %s", object->debug_id, state->name);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 227
 228	new_state = state->work(object, event);
 229	event = -1;
 230	if (new_state == NO_TRANSIT) {
 231		_debug("{OBJ%x} %s notrans", object->debug_id, state->name);
 232		fscache_enqueue_object(object);
 233		event_mask = object->oob_event_mask;
 234		goto unmask_events;
 
 
 
 
 
 
 
 
 
 
 235	}
 236
 237	_debug("{OBJ%x} %s -> %s",
 238	       object->debug_id, state->name, new_state->name);
 239	object->state = state = new_state;
 240
 241	if (state->work) {
 242		if (unlikely(state->work == ((void *)2UL))) {
 243			_leave(" [dead]");
 244			return;
 245		}
 246		goto restart_masked;
 
 
 
 
 
 
 
 
 
 
 
 
 247	}
 248
 249	/* Transited to wait state */
 250	event_mask = object->oob_event_mask;
 251	for (t = state->transitions; t->events; t++)
 252		event_mask |= t->events;
 253
 254unmask_events:
 255	object->event_mask = event_mask;
 256	smp_mb();
 257	events = object->events;
 258	if (events & event_mask)
 259		goto restart;
 260	_leave(" [msk %lx]", event_mask);
 
 
 
 261}
 262
 263/*
 264 * execute an object
 265 */
 266static void fscache_object_work_func(struct work_struct *work)
 267{
 268	struct fscache_object *object =
 269		container_of(work, struct fscache_object, work);
 270	unsigned long start;
 271
 272	_enter("{OBJ%x}", object->debug_id);
 273
 274	start = jiffies;
 275	fscache_object_sm_dispatcher(object);
 276	fscache_hist(fscache_objs_histogram, start);
 
 
 
 277	fscache_put_object(object);
 278}
 279
 280/**
 281 * fscache_object_init - Initialise a cache object description
 282 * @object: Object description
 283 * @cookie: Cookie object will be attached to
 284 * @cache: Cache in which backing object will be found
 285 *
 286 * Initialise a cache object description to its basic values.
 287 *
 288 * See Documentation/filesystems/caching/backend-api.txt for a complete
 289 * description.
 290 */
 291void fscache_object_init(struct fscache_object *object,
 292			 struct fscache_cookie *cookie,
 293			 struct fscache_cache *cache)
 294{
 295	const struct fscache_transition *t;
 296
 297	atomic_inc(&cache->object_count);
 298
 299	object->state = STATE(WAIT_FOR_INIT);
 300	object->oob_table = fscache_osm_init_oob;
 301	object->flags = 1 << FSCACHE_OBJECT_IS_LIVE;
 302	spin_lock_init(&object->lock);
 303	INIT_LIST_HEAD(&object->cache_link);
 304	INIT_HLIST_NODE(&object->cookie_link);
 305	INIT_WORK(&object->work, fscache_object_work_func);
 306	INIT_LIST_HEAD(&object->dependents);
 307	INIT_LIST_HEAD(&object->dep_link);
 308	INIT_LIST_HEAD(&object->pending_ops);
 309	object->n_children = 0;
 310	object->n_ops = object->n_in_progress = object->n_exclusive = 0;
 311	object->events = 0;
 312	object->store_limit = 0;
 313	object->store_limit_l = 0;
 314	object->cache = cache;
 315	object->cookie = cookie;
 316	object->parent = NULL;
 317#ifdef CONFIG_FSCACHE_OBJECT_LIST
 318	RB_CLEAR_NODE(&object->objlist_link);
 319#endif
 320
 321	object->oob_event_mask = 0;
 322	for (t = object->oob_table; t->events; t++)
 323		object->oob_event_mask |= t->events;
 324	object->event_mask = object->oob_event_mask;
 325	for (t = object->state->transitions; t->events; t++)
 326		object->event_mask |= t->events;
 327}
 328EXPORT_SYMBOL(fscache_object_init);
 329
 330/*
 331 * Mark the object as no longer being live, making sure that we synchronise
 332 * against op submission.
 333 */
 334static inline void fscache_mark_object_dead(struct fscache_object *object)
 335{
 336	spin_lock(&object->lock);
 337	clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
 338	spin_unlock(&object->lock);
 339}
 340
 341/*
 342 * Abort object initialisation before we start it.
 343 */
 344static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object,
 345								int event)
 346{
 347	_enter("{OBJ%x},%d", object->debug_id, event);
 348
 349	object->oob_event_mask = 0;
 350	fscache_dequeue_object(object);
 351	return transit_to(KILL_OBJECT);
 352}
 353
 354/*
 355 * initialise an object
 356 * - check the specified object's parent to see if we can make use of it
 357 *   immediately to do a creation
 358 * - we may need to start the process of creating a parent and we need to wait
 359 *   for the parent's lookup and creation to complete if it's not there yet
 
 
 360 */
 361static const struct fscache_state *fscache_initialise_object(struct fscache_object *object,
 362							     int event)
 363{
 364	struct fscache_object *parent;
 365	bool success;
 366
 367	_enter("{OBJ%x},%d", object->debug_id, event);
 
 
 
 
 
 
 
 
 
 
 
 
 
 368
 369	ASSERT(list_empty(&object->dep_link));
 
 370
 371	parent = object->parent;
 372	if (!parent) {
 373		_leave(" [no parent]");
 374		return transit_to(DROP_OBJECT);
 375	}
 376
 377	_debug("parent: %s of:%lx", parent->state->name, parent->flags);
 378
 379	if (fscache_object_is_dying(parent)) {
 380		_leave(" [bad parent]");
 381		return transit_to(DROP_OBJECT);
 382	}
 383
 384	if (fscache_object_is_available(parent)) {
 385		_leave(" [ready]");
 386		return transit_to(PARENT_READY);
 387	}
 388
 389	_debug("wait");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 390
 391	spin_lock(&parent->lock);
 392	fscache_stat(&fscache_n_cop_grab_object);
 393	success = false;
 394	if (fscache_object_is_live(parent) &&
 395	    object->cache->ops->grab_object(object)) {
 396		list_add(&object->dep_link, &parent->dependents);
 397		success = true;
 398	}
 399	fscache_stat_d(&fscache_n_cop_grab_object);
 400	spin_unlock(&parent->lock);
 401	if (!success) {
 402		_leave(" [grab failed]");
 403		return transit_to(DROP_OBJECT);
 404	}
 405
 406	/* fscache_acquire_non_index_cookie() uses this
 407	 * to wake the chain up */
 408	fscache_raise_event(parent, FSCACHE_OBJECT_EV_NEW_CHILD);
 409	_leave(" [wait]");
 410	return transit_to(WAIT_FOR_PARENT);
 411}
 412
 413/*
 414 * Once the parent object is ready, we should kick off our lookup op.
 415 */
 416static const struct fscache_state *fscache_parent_ready(struct fscache_object *object,
 417							int event)
 418{
 419	struct fscache_object *parent = object->parent;
 420
 421	_enter("{OBJ%x},%d", object->debug_id, event);
 422
 423	ASSERT(parent != NULL);
 424
 425	spin_lock(&parent->lock);
 426	parent->n_ops++;
 427	parent->n_obj_ops++;
 428	object->lookup_jif = jiffies;
 429	spin_unlock(&parent->lock);
 430
 431	_leave("");
 432	return transit_to(LOOK_UP_OBJECT);
 433}
 434
 435/*
 436 * look an object up in the cache from which it was allocated
 437 * - we hold an "access lock" on the parent object, so the parent object cannot
 438 *   be withdrawn by either party till we've finished
 
 
 439 */
 440static const struct fscache_state *fscache_look_up_object(struct fscache_object *object,
 441							  int event)
 442{
 443	struct fscache_cookie *cookie = object->cookie;
 444	struct fscache_object *parent = object->parent;
 445	int ret;
 446
 447	_enter("{OBJ%x},%d", object->debug_id, event);
 448
 449	object->oob_table = fscache_osm_lookup_oob;
 450
 
 451	ASSERT(parent != NULL);
 452	ASSERTCMP(parent->n_ops, >, 0);
 453	ASSERTCMP(parent->n_obj_ops, >, 0);
 454
 455	/* make sure the parent is still available */
 456	ASSERT(fscache_object_is_available(parent));
 457
 458	if (fscache_object_is_dying(parent) ||
 459	    test_bit(FSCACHE_IOERROR, &object->cache->flags) ||
 460	    !fscache_use_cookie(object)) {
 461		_leave(" [unavailable]");
 462		return transit_to(LOOKUP_FAILURE);
 
 463	}
 464
 465	_debug("LOOKUP \"%s\" in \"%s\"",
 466	       cookie->def->name, object->cache->tag->name);
 
 467
 468	fscache_stat(&fscache_n_object_lookups);
 469	fscache_stat(&fscache_n_cop_lookup_object);
 470	ret = object->cache->ops->lookup_object(object);
 471	fscache_stat_d(&fscache_n_cop_lookup_object);
 472
 473	fscache_unuse_cookie(object);
 
 474
 475	if (ret == -ETIMEDOUT) {
 476		/* probably stuck behind another object, so move this one to
 477		 * the back of the queue */
 478		fscache_stat(&fscache_n_object_lookups_timed_out);
 479		_leave(" [timeout]");
 480		return NO_TRANSIT;
 481	}
 482
 483	if (ret < 0) {
 484		_leave(" [error]");
 485		return transit_to(LOOKUP_FAILURE);
 486	}
 487
 488	_leave(" [ok]");
 489	return transit_to(OBJECT_AVAILABLE);
 490}
 491
 492/**
 493 * fscache_object_lookup_negative - Note negative cookie lookup
 494 * @object: Object pointing to cookie to mark
 495 *
 496 * Note negative lookup, permitting those waiting to read data from an already
 497 * existing backing object to continue as there's no data for them to read.
 498 */
 499void fscache_object_lookup_negative(struct fscache_object *object)
 500{
 501	struct fscache_cookie *cookie = object->cookie;
 502
 503	_enter("{OBJ%x,%s}", object->debug_id, object->state->name);
 
 504
 505	if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
 
 506		fscache_stat(&fscache_n_object_lookups_negative);
 507
 508		/* Allow write requests to begin stacking up and read requests to begin
 509		 * returning ENODATA.
 510		 */
 
 
 
 511		set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
 512		clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
 513
 514		_debug("wake up lookup %p", &cookie->flags);
 515		clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
 
 
 516		wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
 
 
 
 
 517	}
 
 518	_leave("");
 519}
 520EXPORT_SYMBOL(fscache_object_lookup_negative);
 521
 522/**
 523 * fscache_obtained_object - Note successful object lookup or creation
 524 * @object: Object pointing to cookie to mark
 525 *
 526 * Note successful lookup and/or creation, permitting those waiting to write
 527 * data to a backing object to continue.
 528 *
 529 * Note that after calling this, an object's cookie may be relinquished by the
 530 * netfs, and so must be accessed with object lock held.
 531 */
 532void fscache_obtained_object(struct fscache_object *object)
 533{
 534	struct fscache_cookie *cookie = object->cookie;
 535
 536	_enter("{OBJ%x,%s}", object->debug_id, object->state->name);
 
 537
 538	/* if we were still looking up, then we must have a positive lookup
 539	 * result, in which case there may be data available */
 540	if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) {
 
 541		fscache_stat(&fscache_n_object_lookups_positive);
 542
 543		/* We do (presumably) have data */
 544		clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
 545		clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
 546
 547		/* Allow write requests to begin stacking up and read requests
 548		 * to begin shovelling data.
 549		 */
 550		clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
 551		wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
 
 552	} else {
 
 553		fscache_stat(&fscache_n_object_created);
 
 
 
 
 
 554	}
 555
 556	set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
 
 
 557	_leave("");
 558}
 559EXPORT_SYMBOL(fscache_obtained_object);
 560
 561/*
 562 * handle an object that has just become available
 563 */
 564static const struct fscache_state *fscache_object_available(struct fscache_object *object,
 565							    int event)
 566{
 567	_enter("{OBJ%x},%d", object->debug_id, event);
 568
 569	object->oob_table = fscache_osm_run_oob;
 570
 571	spin_lock(&object->lock);
 572
 
 
 
 
 573	fscache_done_parent_op(object);
 574	if (object->n_in_progress == 0) {
 575		if (object->n_ops > 0) {
 576			ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
 
 
 577			fscache_start_operations(object);
 578		} else {
 579			ASSERT(list_empty(&object->pending_ops));
 580		}
 581	}
 582	spin_unlock(&object->lock);
 583
 584	fscache_stat(&fscache_n_cop_lookup_complete);
 585	object->cache->ops->lookup_complete(object);
 586	fscache_stat_d(&fscache_n_cop_lookup_complete);
 
 587
 588	fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
 589	fscache_stat(&fscache_n_object_avail);
 590
 591	_leave("");
 592	return transit_to(JUMPSTART_DEPS);
 593}
 594
 595/*
 596 * Wake up this object's dependent objects now that we've become available.
 597 */
 598static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *object,
 599								int event)
 600{
 601	_enter("{OBJ%x},%d", object->debug_id, event);
 
 602
 603	if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_PARENT_READY))
 604		return NO_TRANSIT; /* Not finished; requeue */
 605	return transit_to(WAIT_FOR_CMD);
 606}
 607
 608/*
 609 * Handle lookup or creation failute.
 610 */
 611static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object,
 612							  int event)
 613{
 614	struct fscache_cookie *cookie;
 615
 616	_enter("{OBJ%x},%d", object->debug_id, event);
 
 
 617
 618	object->oob_event_mask = 0;
 
 
 619
 620	fscache_stat(&fscache_n_cop_lookup_complete);
 621	object->cache->ops->lookup_complete(object);
 622	fscache_stat_d(&fscache_n_cop_lookup_complete);
 623
 624	set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags);
 
 
 
 
 
 
 625
 626	cookie = object->cookie;
 627	set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
 628	if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags))
 629		wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
 630
 631	fscache_done_parent_op(object);
 632	return transit_to(KILL_OBJECT);
 633}
 634
 635/*
 636 * Wait for completion of all active operations on this object and the death of
 637 * all child objects of this object.
 638 */
 639static const struct fscache_state *fscache_kill_object(struct fscache_object *object,
 640						       int event)
 641{
 642	_enter("{OBJ%x,%d,%d},%d",
 643	       object->debug_id, object->n_ops, object->n_children, event);
 644
 645	fscache_mark_object_dead(object);
 646	object->oob_event_mask = 0;
 647
 648	if (list_empty(&object->dependents) &&
 649	    object->n_ops == 0 &&
 650	    object->n_children == 0)
 651		return transit_to(DROP_OBJECT);
 652
 653	if (object->n_in_progress == 0) {
 654		spin_lock(&object->lock);
 655		if (object->n_ops > 0 && object->n_in_progress == 0)
 656			fscache_start_operations(object);
 657		spin_unlock(&object->lock);
 658	}
 659
 660	if (!list_empty(&object->dependents))
 661		return transit_to(KILL_DEPENDENTS);
 662
 663	return transit_to(WAIT_FOR_CLEARANCE);
 664}
 665
 666/*
 667 * Kill dependent objects.
 668 */
 669static const struct fscache_state *fscache_kill_dependents(struct fscache_object *object,
 670							   int event)
 671{
 672	_enter("{OBJ%x},%d", object->debug_id, event);
 
 673
 674	if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_KILL))
 675		return NO_TRANSIT; /* Not finished */
 676	return transit_to(WAIT_FOR_CLEARANCE);
 677}
 678
 679/*
 680 * Drop an object's attachments
 681 */
 682static const struct fscache_state *fscache_drop_object(struct fscache_object *object,
 683						       int event)
 684{
 685	struct fscache_object *parent = object->parent;
 686	struct fscache_cookie *cookie = object->cookie;
 687	struct fscache_cache *cache = object->cache;
 688	bool awaken = false;
 689
 690	_enter("{OBJ%x,%d},%d", object->debug_id, object->n_children, event);
 
 
 691
 692	ASSERT(cookie != NULL);
 693	ASSERT(!hlist_unhashed(&object->cookie_link));
 
 
 
 
 
 
 
 
 694
 695	/* Make sure the cookie no longer points here and that the netfs isn't
 696	 * waiting for us.
 697	 */
 698	spin_lock(&cookie->lock);
 699	hlist_del_init(&object->cookie_link);
 700	if (hlist_empty(&cookie->backing_objects) &&
 701	    test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
 702		awaken = true;
 703	spin_unlock(&cookie->lock);
 704
 705	if (awaken)
 706		wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
 707
 708	/* Prevent a race with our last child, which has to signal EV_CLEARED
 709	 * before dropping our spinlock.
 710	 */
 711	spin_lock(&object->lock);
 712	spin_unlock(&object->lock);
 713
 714	/* Discard from the cache's collection of objects */
 715	spin_lock(&cache->object_list_lock);
 716	list_del_init(&object->cache_link);
 717	spin_unlock(&cache->object_list_lock);
 718
 719	fscache_stat(&fscache_n_cop_drop_object);
 720	cache->ops->drop_object(object);
 721	fscache_stat_d(&fscache_n_cop_drop_object);
 
 
 
 
 
 
 
 
 
 
 
 722
 723	/* The parent object wants to know when all it dependents have gone */
 724	if (parent) {
 725		_debug("release parent OBJ%x {%d}",
 726		       parent->debug_id, parent->n_children);
 727
 728		spin_lock(&parent->lock);
 729		parent->n_children--;
 730		if (parent->n_children == 0)
 731			fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
 732		spin_unlock(&parent->lock);
 733		object->parent = NULL;
 734	}
 
 735
 736	/* this just shifts the object release to the work processor */
 737	fscache_put_object(object);
 738	fscache_stat(&fscache_n_object_dead);
 739
 740	_leave("");
 741	return transit_to(OBJECT_DEAD);
 742}
 743
 744/*
 745 * get a ref on an object
 746 */
 747static int fscache_get_object(struct fscache_object *object)
 748{
 749	int ret;
 750
 751	fscache_stat(&fscache_n_cop_grab_object);
 752	ret = object->cache->ops->grab_object(object) ? 0 : -EAGAIN;
 753	fscache_stat_d(&fscache_n_cop_grab_object);
 754	return ret;
 755}
 756
 757/*
 758 * Discard a ref on an object
 759 */
 760static void fscache_put_object(struct fscache_object *object)
 761{
 762	fscache_stat(&fscache_n_cop_put_object);
 763	object->cache->ops->put_object(object);
 764	fscache_stat_d(&fscache_n_cop_put_object);
 765}
 766
 767/**
 768 * fscache_object_destroy - Note that a cache object is about to be destroyed
 769 * @object: The object to be destroyed
 770 *
 771 * Note the imminent destruction and deallocation of a cache object record.
 772 */
 773void fscache_object_destroy(struct fscache_object *object)
 774{
 775	fscache_objlist_remove(object);
 776
 777	/* We can get rid of the cookie now */
 778	fscache_cookie_put(object->cookie);
 779	object->cookie = NULL;
 780}
 781EXPORT_SYMBOL(fscache_object_destroy);
 782
 783/*
 784 * enqueue an object for metadata-type processing
 785 */
 786void fscache_enqueue_object(struct fscache_object *object)
 787{
 788	_enter("{OBJ%x}", object->debug_id);
 789
 790	if (fscache_get_object(object) >= 0) {
 791		wait_queue_head_t *cong_wq =
 792			&get_cpu_var(fscache_object_cong_wait);
 793
 794		if (queue_work(fscache_object_wq, &object->work)) {
 795			if (fscache_object_congested())
 796				wake_up(cong_wq);
 797		} else
 798			fscache_put_object(object);
 799
 800		put_cpu_var(fscache_object_cong_wait);
 801	}
 802}
 803
 804/**
 805 * fscache_object_sleep_till_congested - Sleep until object wq is congested
 806 * @timeoutp: Scheduler sleep timeout
 807 *
 808 * Allow an object handler to sleep until the object workqueue is congested.
 809 *
 810 * The caller must set up a wake up event before calling this and must have set
 811 * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
 812 * condition before calling this function as no test is made here.
 813 *
 814 * %true is returned if the object wq is congested, %false otherwise.
 815 */
 816bool fscache_object_sleep_till_congested(signed long *timeoutp)
 817{
 818	wait_queue_head_t *cong_wq = this_cpu_ptr(&fscache_object_cong_wait);
 819	DEFINE_WAIT(wait);
 820
 821	if (fscache_object_congested())
 822		return true;
 823
 824	add_wait_queue_exclusive(cong_wq, &wait);
 825	if (!fscache_object_congested())
 826		*timeoutp = schedule_timeout(*timeoutp);
 827	finish_wait(cong_wq, &wait);
 828
 829	return fscache_object_congested();
 830}
 831EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
 832
 833/*
 834 * Enqueue the dependents of an object for metadata-type processing.
 835 *
 836 * If we don't manage to finish the list before the scheduler wants to run
 837 * again then return false immediately.  We return true if the list was
 838 * cleared.
 839 */
 840static bool fscache_enqueue_dependents(struct fscache_object *object, int event)
 841{
 842	struct fscache_object *dep;
 843	bool ret = true;
 844
 845	_enter("{OBJ%x}", object->debug_id);
 846
 847	if (list_empty(&object->dependents))
 848		return true;
 849
 850	spin_lock(&object->lock);
 851
 852	while (!list_empty(&object->dependents)) {
 853		dep = list_entry(object->dependents.next,
 854				 struct fscache_object, dep_link);
 855		list_del_init(&dep->dep_link);
 856
 857		fscache_raise_event(dep, event);
 
 
 858		fscache_put_object(dep);
 859
 860		if (!list_empty(&object->dependents) && need_resched()) {
 861			ret = false;
 862			break;
 863		}
 864	}
 865
 866	spin_unlock(&object->lock);
 867	return ret;
 868}
 869
 870/*
 871 * remove an object from whatever queue it's waiting on
 
 872 */
 873static void fscache_dequeue_object(struct fscache_object *object)
 874{
 875	_enter("{OBJ%x}", object->debug_id);
 876
 877	if (!list_empty(&object->dep_link)) {
 878		spin_lock(&object->parent->lock);
 879		list_del_init(&object->dep_link);
 880		spin_unlock(&object->parent->lock);
 881	}
 882
 883	_leave("");
 884}
 885
 886/**
 887 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
 888 * @object: The object to ask about
 889 * @data: The auxiliary data for the object
 890 * @datalen: The size of the auxiliary data
 891 *
 892 * This function consults the netfs about the coherency state of an object.
 893 * The caller must be holding a ref on cookie->n_active (held by
 894 * fscache_look_up_object() on behalf of the cache backend during object lookup
 895 * and creation).
 896 */
 897enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
 898					const void *data, uint16_t datalen)
 899{
 900	enum fscache_checkaux result;
 901
 902	if (!object->cookie->def->check_aux) {
 903		fscache_stat(&fscache_n_checkaux_none);
 904		return FSCACHE_CHECKAUX_OKAY;
 905	}
 906
 907	result = object->cookie->def->check_aux(object->cookie->netfs_data,
 908						data, datalen);
 909	switch (result) {
 910		/* entry okay as is */
 911	case FSCACHE_CHECKAUX_OKAY:
 912		fscache_stat(&fscache_n_checkaux_okay);
 913		break;
 914
 915		/* entry requires update */
 916	case FSCACHE_CHECKAUX_NEEDS_UPDATE:
 917		fscache_stat(&fscache_n_checkaux_update);
 918		break;
 919
 920		/* entry requires deletion */
 921	case FSCACHE_CHECKAUX_OBSOLETE:
 922		fscache_stat(&fscache_n_checkaux_obsolete);
 923		break;
 924
 925	default:
 926		BUG();
 927	}
 928
 929	return result;
 930}
 931EXPORT_SYMBOL(fscache_check_aux);
 932
 933/*
 934 * Asynchronously invalidate an object.
 935 */
 936static const struct fscache_state *_fscache_invalidate_object(struct fscache_object *object,
 937							      int event)
 938{
 939	struct fscache_operation *op;
 940	struct fscache_cookie *cookie = object->cookie;
 941
 942	_enter("{OBJ%x},%d", object->debug_id, event);
 943
 944	/* We're going to need the cookie.  If the cookie is not available then
 945	 * retire the object instead.
 946	 */
 947	if (!fscache_use_cookie(object)) {
 948		ASSERT(object->cookie->stores.rnode == NULL);
 949		set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
 950		_leave(" [no cookie]");
 951		return transit_to(KILL_OBJECT);
 952	}
 953
 954	/* Reject any new read/write ops and abort any that are pending. */
 955	fscache_invalidate_writes(cookie);
 956	clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
 957	fscache_cancel_all_ops(object);
 958
 959	/* Now we have to wait for in-progress reads and writes */
 960	op = kzalloc(sizeof(*op), GFP_KERNEL);
 961	if (!op)
 962		goto nomem;
 963
 964	fscache_operation_init(op, object->cache->ops->invalidate_object,
 965			       NULL, NULL);
 966	op->flags = FSCACHE_OP_ASYNC |
 967		(1 << FSCACHE_OP_EXCLUSIVE) |
 968		(1 << FSCACHE_OP_UNUSE_COOKIE);
 969
 970	spin_lock(&cookie->lock);
 971	if (fscache_submit_exclusive_op(object, op) < 0)
 972		goto submit_op_failed;
 973	spin_unlock(&cookie->lock);
 974	fscache_put_operation(op);
 975
 976	/* Once we've completed the invalidation, we know there will be no data
 977	 * stored in the cache and thus we can reinstate the data-check-skip
 978	 * optimisation.
 979	 */
 980	set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
 981
 982	/* We can allow read and write requests to come in once again.  They'll
 983	 * queue up behind our exclusive invalidation operation.
 984	 */
 985	if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
 986		wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
 987	_leave(" [ok]");
 988	return transit_to(UPDATE_OBJECT);
 989
 990nomem:
 991	fscache_mark_object_dead(object);
 992	fscache_unuse_cookie(object);
 993	_leave(" [ENOMEM]");
 994	return transit_to(KILL_OBJECT);
 995
 996submit_op_failed:
 997	fscache_mark_object_dead(object);
 998	spin_unlock(&cookie->lock);
 999	fscache_unuse_cookie(object);
1000	kfree(op);
1001	_leave(" [EIO]");
1002	return transit_to(KILL_OBJECT);
1003}
1004
1005static const struct fscache_state *fscache_invalidate_object(struct fscache_object *object,
1006							     int event)
1007{
1008	const struct fscache_state *s;
1009
1010	fscache_stat(&fscache_n_invalidates_run);
1011	fscache_stat(&fscache_n_cop_invalidate_object);
1012	s = _fscache_invalidate_object(object, event);
1013	fscache_stat_d(&fscache_n_cop_invalidate_object);
1014	return s;
1015}
1016
1017/*
1018 * Asynchronously update an object.
1019 */
1020static const struct fscache_state *fscache_update_object(struct fscache_object *object,
1021							 int event)
1022{
1023	_enter("{OBJ%x},%d", object->debug_id, event);
1024
1025	fscache_stat(&fscache_n_updates_run);
1026	fscache_stat(&fscache_n_cop_update_object);
1027	object->cache->ops->update_object(object);
1028	fscache_stat_d(&fscache_n_cop_update_object);
1029
1030	_leave("");
1031	return transit_to(WAIT_FOR_CMD);
1032}
1033
1034/**
1035 * fscache_object_retrying_stale - Note retrying stale object
1036 * @object: The object that will be retried
1037 *
1038 * Note that an object lookup found an on-disk object that was adjudged to be
1039 * stale and has been deleted.  The lookup will be retried.
1040 */
1041void fscache_object_retrying_stale(struct fscache_object *object)
1042{
1043	fscache_stat(&fscache_n_cache_no_space_reject);
1044}
1045EXPORT_SYMBOL(fscache_object_retrying_stale);
1046
1047/**
1048 * fscache_object_mark_killed - Note that an object was killed
1049 * @object: The object that was culled
1050 * @why: The reason the object was killed.
1051 *
1052 * Note that an object was killed.  Returns true if the object was
1053 * already marked killed, false if it wasn't.
1054 */
1055void fscache_object_mark_killed(struct fscache_object *object,
1056				enum fscache_why_object_killed why)
1057{
1058	if (test_and_set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags)) {
1059		pr_err("Error: Object already killed by cache [%s]\n",
1060		       object->cache->identifier);
1061		return;
1062	}
1063
1064	switch (why) {
1065	case FSCACHE_OBJECT_NO_SPACE:
1066		fscache_stat(&fscache_n_cache_no_space_reject);
1067		break;
1068	case FSCACHE_OBJECT_IS_STALE:
1069		fscache_stat(&fscache_n_cache_stale_objects);
1070		break;
1071	case FSCACHE_OBJECT_WAS_RETIRED:
1072		fscache_stat(&fscache_n_cache_retired_objects);
1073		break;
1074	case FSCACHE_OBJECT_WAS_CULLED:
1075		fscache_stat(&fscache_n_cache_culled_objects);
1076		break;
1077	}
1078}
1079EXPORT_SYMBOL(fscache_object_mark_killed);
v3.1
  1/* FS-Cache object state machine handler
  2 *
  3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4 * Written by David Howells (dhowells@redhat.com)
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License
  8 * as published by the Free Software Foundation; either version
  9 * 2 of the License, or (at your option) any later version.
 10 *
 11 * See Documentation/filesystems/caching/object.txt for a description of the
 12 * object state machine and the in-kernel representations.
 13 */
 14
 15#define FSCACHE_DEBUG_LEVEL COOKIE
 16#include <linux/module.h>
 
 
 17#include "internal.h"
 18
 19const char *fscache_object_states[FSCACHE_OBJECT__NSTATES] = {
 20	[FSCACHE_OBJECT_INIT]		= "OBJECT_INIT",
 21	[FSCACHE_OBJECT_LOOKING_UP]	= "OBJECT_LOOKING_UP",
 22	[FSCACHE_OBJECT_CREATING]	= "OBJECT_CREATING",
 23	[FSCACHE_OBJECT_AVAILABLE]	= "OBJECT_AVAILABLE",
 24	[FSCACHE_OBJECT_ACTIVE]		= "OBJECT_ACTIVE",
 25	[FSCACHE_OBJECT_UPDATING]	= "OBJECT_UPDATING",
 26	[FSCACHE_OBJECT_DYING]		= "OBJECT_DYING",
 27	[FSCACHE_OBJECT_LC_DYING]	= "OBJECT_LC_DYING",
 28	[FSCACHE_OBJECT_ABORT_INIT]	= "OBJECT_ABORT_INIT",
 29	[FSCACHE_OBJECT_RELEASING]	= "OBJECT_RELEASING",
 30	[FSCACHE_OBJECT_RECYCLING]	= "OBJECT_RECYCLING",
 31	[FSCACHE_OBJECT_WITHDRAWING]	= "OBJECT_WITHDRAWING",
 32	[FSCACHE_OBJECT_DEAD]		= "OBJECT_DEAD",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 33};
 34EXPORT_SYMBOL(fscache_object_states);
 35
 36const char fscache_object_states_short[FSCACHE_OBJECT__NSTATES][5] = {
 37	[FSCACHE_OBJECT_INIT]		= "INIT",
 38	[FSCACHE_OBJECT_LOOKING_UP]	= "LOOK",
 39	[FSCACHE_OBJECT_CREATING]	= "CRTN",
 40	[FSCACHE_OBJECT_AVAILABLE]	= "AVBL",
 41	[FSCACHE_OBJECT_ACTIVE]		= "ACTV",
 42	[FSCACHE_OBJECT_UPDATING]	= "UPDT",
 43	[FSCACHE_OBJECT_DYING]		= "DYNG",
 44	[FSCACHE_OBJECT_LC_DYING]	= "LCDY",
 45	[FSCACHE_OBJECT_ABORT_INIT]	= "ABTI",
 46	[FSCACHE_OBJECT_RELEASING]	= "RELS",
 47	[FSCACHE_OBJECT_RECYCLING]	= "RCYC",
 48	[FSCACHE_OBJECT_WITHDRAWING]	= "WTHD",
 49	[FSCACHE_OBJECT_DEAD]		= "DEAD",
 50};
 51
 52static int  fscache_get_object(struct fscache_object *);
 53static void fscache_put_object(struct fscache_object *);
 54static void fscache_initialise_object(struct fscache_object *);
 55static void fscache_lookup_object(struct fscache_object *);
 56static void fscache_object_available(struct fscache_object *);
 57static void fscache_release_object(struct fscache_object *);
 58static void fscache_withdraw_object(struct fscache_object *);
 59static void fscache_enqueue_dependents(struct fscache_object *);
 60static void fscache_dequeue_object(struct fscache_object *);
 61
 62/*
 63 * we need to notify the parent when an op completes that we had outstanding
 64 * upon it
 65 */
 66static inline void fscache_done_parent_op(struct fscache_object *object)
 67{
 68	struct fscache_object *parent = object->parent;
 69
 70	_enter("OBJ%x {OBJ%x,%x}",
 71	       object->debug_id, parent->debug_id, parent->n_ops);
 72
 73	spin_lock_nested(&parent->lock, 1);
 
 74	parent->n_ops--;
 75	parent->n_obj_ops--;
 76	if (parent->n_ops == 0)
 77		fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
 78	spin_unlock(&parent->lock);
 79}
 80
 81/*
 82 * process events that have been sent to an object's state machine
 83 * - initiates parent lookup
 84 * - does object lookup
 85 * - does object creation
 86 * - does object recycling and retirement
 87 * - does object withdrawal
 88 */
 89static void fscache_object_state_machine(struct fscache_object *object)
 90{
 91	enum fscache_object_state new_state;
 92	struct fscache_cookie *cookie;
 
 
 93
 94	ASSERT(object != NULL);
 95
 96	_enter("{OBJ%x,%s,%lx}",
 97	       object->debug_id, fscache_object_states[object->state],
 98	       object->events);
 99
100	switch (object->state) {
101		/* wait for the parent object to become ready */
102	case FSCACHE_OBJECT_INIT:
103		object->event_mask =
104			ULONG_MAX & ~(1 << FSCACHE_OBJECT_EV_CLEARED);
105		fscache_initialise_object(object);
106		goto done;
107
108		/* look up the object metadata on disk */
109	case FSCACHE_OBJECT_LOOKING_UP:
110		fscache_lookup_object(object);
111		goto lookup_transit;
112
113		/* create the object metadata on disk */
114	case FSCACHE_OBJECT_CREATING:
115		fscache_lookup_object(object);
116		goto lookup_transit;
117
118		/* handle an object becoming available; start pending
119		 * operations and queue dependent operations for processing */
120	case FSCACHE_OBJECT_AVAILABLE:
121		fscache_object_available(object);
122		goto active_transit;
123
124		/* normal running state */
125	case FSCACHE_OBJECT_ACTIVE:
126		goto active_transit;
127
128		/* update the object metadata on disk */
129	case FSCACHE_OBJECT_UPDATING:
130		clear_bit(FSCACHE_OBJECT_EV_UPDATE, &object->events);
131		fscache_stat(&fscache_n_updates_run);
132		fscache_stat(&fscache_n_cop_update_object);
133		object->cache->ops->update_object(object);
134		fscache_stat_d(&fscache_n_cop_update_object);
135		goto active_transit;
136
137		/* handle an object dying during lookup or creation */
138	case FSCACHE_OBJECT_LC_DYING:
139		object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE);
140		fscache_stat(&fscache_n_cop_lookup_complete);
141		object->cache->ops->lookup_complete(object);
142		fscache_stat_d(&fscache_n_cop_lookup_complete);
143
144		spin_lock(&object->lock);
145		object->state = FSCACHE_OBJECT_DYING;
146		cookie = object->cookie;
147		if (cookie) {
148			if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP,
149					       &cookie->flags))
150				wake_up_bit(&cookie->flags,
151					    FSCACHE_COOKIE_LOOKING_UP);
152			if (test_and_clear_bit(FSCACHE_COOKIE_CREATING,
153					       &cookie->flags))
154				wake_up_bit(&cookie->flags,
155					    FSCACHE_COOKIE_CREATING);
156		}
157		spin_unlock(&object->lock);
158
159		fscache_done_parent_op(object);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
161		/* wait for completion of all active operations on this object
162		 * and the death of all child objects of this object */
163	case FSCACHE_OBJECT_DYING:
164	dying:
165		clear_bit(FSCACHE_OBJECT_EV_CLEARED, &object->events);
166		spin_lock(&object->lock);
167		_debug("dying OBJ%x {%d,%d}",
168		       object->debug_id, object->n_ops, object->n_children);
169		if (object->n_ops == 0 && object->n_children == 0) {
170			object->event_mask &=
171				~(1 << FSCACHE_OBJECT_EV_CLEARED);
172			object->event_mask |=
173				(1 << FSCACHE_OBJECT_EV_WITHDRAW) |
174				(1 << FSCACHE_OBJECT_EV_RETIRE) |
175				(1 << FSCACHE_OBJECT_EV_RELEASE) |
176				(1 << FSCACHE_OBJECT_EV_ERROR);
177		} else {
178			object->event_mask &=
179				~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
180				  (1 << FSCACHE_OBJECT_EV_RETIRE) |
181				  (1 << FSCACHE_OBJECT_EV_RELEASE) |
182				  (1 << FSCACHE_OBJECT_EV_ERROR));
183			object->event_mask |=
184				1 << FSCACHE_OBJECT_EV_CLEARED;
185		}
186		spin_unlock(&object->lock);
187		fscache_enqueue_dependents(object);
188		fscache_start_operations(object);
189		goto terminal_transit;
190
191		/* handle an abort during initialisation */
192	case FSCACHE_OBJECT_ABORT_INIT:
193		_debug("handle abort init %lx", object->events);
194		object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE);
195
196		spin_lock(&object->lock);
197		fscache_dequeue_object(object);
198
199		object->state = FSCACHE_OBJECT_DYING;
200		if (test_and_clear_bit(FSCACHE_COOKIE_CREATING,
201				       &object->cookie->flags))
202			wake_up_bit(&object->cookie->flags,
203				    FSCACHE_COOKIE_CREATING);
204		spin_unlock(&object->lock);
205		goto dying;
206
207		/* handle the netfs releasing an object and possibly marking it
208		 * obsolete too */
209	case FSCACHE_OBJECT_RELEASING:
210	case FSCACHE_OBJECT_RECYCLING:
211		object->event_mask &=
212			~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
213			  (1 << FSCACHE_OBJECT_EV_RETIRE) |
214			  (1 << FSCACHE_OBJECT_EV_RELEASE) |
215			  (1 << FSCACHE_OBJECT_EV_ERROR));
216		fscache_release_object(object);
217		spin_lock(&object->lock);
218		object->state = FSCACHE_OBJECT_DEAD;
219		spin_unlock(&object->lock);
220		fscache_stat(&fscache_n_object_dead);
221		goto terminal_transit;
222
223		/* handle the parent cache of this object being withdrawn from
224		 * active service */
225	case FSCACHE_OBJECT_WITHDRAWING:
226		object->event_mask &=
227			~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
228			  (1 << FSCACHE_OBJECT_EV_RETIRE) |
229			  (1 << FSCACHE_OBJECT_EV_RELEASE) |
230			  (1 << FSCACHE_OBJECT_EV_ERROR));
231		fscache_withdraw_object(object);
232		spin_lock(&object->lock);
233		object->state = FSCACHE_OBJECT_DEAD;
234		spin_unlock(&object->lock);
235		fscache_stat(&fscache_n_object_dead);
236		goto terminal_transit;
237
238		/* complain about the object being woken up once it is
239		 * deceased */
240	case FSCACHE_OBJECT_DEAD:
241		printk(KERN_ERR "FS-Cache:"
242		       " Unexpected event in dead state %lx\n",
243		       object->events & object->event_mask);
244		BUG();
245
246	default:
247		printk(KERN_ERR "FS-Cache: Unknown object state %u\n",
248		       object->state);
249		BUG();
250	}
251
252	/* determine the transition from a lookup state */
253lookup_transit:
254	switch (fls(object->events & object->event_mask) - 1) {
255	case FSCACHE_OBJECT_EV_WITHDRAW:
256	case FSCACHE_OBJECT_EV_RETIRE:
257	case FSCACHE_OBJECT_EV_RELEASE:
258	case FSCACHE_OBJECT_EV_ERROR:
259		new_state = FSCACHE_OBJECT_LC_DYING;
260		goto change_state;
261	case FSCACHE_OBJECT_EV_REQUEUE:
262		goto done;
263	case -1:
264		goto done; /* sleep until event */
265	default:
266		goto unsupported_event;
267	}
268
269	/* determine the transition from an active state */
270active_transit:
271	switch (fls(object->events & object->event_mask) - 1) {
272	case FSCACHE_OBJECT_EV_WITHDRAW:
273	case FSCACHE_OBJECT_EV_RETIRE:
274	case FSCACHE_OBJECT_EV_RELEASE:
275	case FSCACHE_OBJECT_EV_ERROR:
276		new_state = FSCACHE_OBJECT_DYING;
277		goto change_state;
278	case FSCACHE_OBJECT_EV_UPDATE:
279		new_state = FSCACHE_OBJECT_UPDATING;
280		goto change_state;
281	case -1:
282		new_state = FSCACHE_OBJECT_ACTIVE;
283		goto change_state; /* sleep until event */
284	default:
285		goto unsupported_event;
286	}
287
288	/* determine the transition from a terminal state */
289terminal_transit:
290	switch (fls(object->events & object->event_mask) - 1) {
291	case FSCACHE_OBJECT_EV_WITHDRAW:
292		new_state = FSCACHE_OBJECT_WITHDRAWING;
293		goto change_state;
294	case FSCACHE_OBJECT_EV_RETIRE:
295		new_state = FSCACHE_OBJECT_RECYCLING;
296		goto change_state;
297	case FSCACHE_OBJECT_EV_RELEASE:
298		new_state = FSCACHE_OBJECT_RELEASING;
299		goto change_state;
300	case FSCACHE_OBJECT_EV_ERROR:
301		new_state = FSCACHE_OBJECT_WITHDRAWING;
302		goto change_state;
303	case FSCACHE_OBJECT_EV_CLEARED:
304		new_state = FSCACHE_OBJECT_DYING;
305		goto change_state;
306	case -1:
307		goto done; /* sleep until event */
308	default:
309		goto unsupported_event;
310	}
311
312change_state:
313	spin_lock(&object->lock);
314	object->state = new_state;
315	spin_unlock(&object->lock);
316
317done:
318	_leave(" [->%s]", fscache_object_states[object->state]);
319	return;
320
321unsupported_event:
322	printk(KERN_ERR "FS-Cache:"
323	       " Unsupported event %lx [mask %lx] in state %s\n",
324	       object->events, object->event_mask,
325	       fscache_object_states[object->state]);
326	BUG();
327}
328
329/*
330 * execute an object
331 */
332void fscache_object_work_func(struct work_struct *work)
333{
334	struct fscache_object *object =
335		container_of(work, struct fscache_object, work);
336	unsigned long start;
337
338	_enter("{OBJ%x}", object->debug_id);
339
340	start = jiffies;
341	fscache_object_state_machine(object);
342	fscache_hist(fscache_objs_histogram, start);
343	if (object->events & object->event_mask)
344		fscache_enqueue_object(object);
345	clear_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
346	fscache_put_object(object);
347}
348EXPORT_SYMBOL(fscache_object_work_func);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
349
350/*
351 * initialise an object
352 * - check the specified object's parent to see if we can make use of it
353 *   immediately to do a creation
354 * - we may need to start the process of creating a parent and we need to wait
355 *   for the parent's lookup and creation to complete if it's not there yet
356 * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
357 *   leaf-most cookies of the object and all its children
358 */
359static void fscache_initialise_object(struct fscache_object *object)
 
360{
361	struct fscache_object *parent;
 
362
363	_enter("");
364	ASSERT(object->cookie != NULL);
365	ASSERT(object->cookie->parent != NULL);
366
367	if (object->events & ((1 << FSCACHE_OBJECT_EV_ERROR) |
368			      (1 << FSCACHE_OBJECT_EV_RELEASE) |
369			      (1 << FSCACHE_OBJECT_EV_RETIRE) |
370			      (1 << FSCACHE_OBJECT_EV_WITHDRAW))) {
371		_debug("abort init %lx", object->events);
372		spin_lock(&object->lock);
373		object->state = FSCACHE_OBJECT_ABORT_INIT;
374		spin_unlock(&object->lock);
375		return;
376	}
377
378	spin_lock(&object->cookie->lock);
379	spin_lock_nested(&object->cookie->parent->lock, 1);
380
381	parent = object->parent;
382	if (!parent) {
383		_debug("no parent");
384		set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
385	} else {
386		spin_lock(&object->lock);
387		spin_lock_nested(&parent->lock, 1);
388		_debug("parent %s", fscache_object_states[parent->state]);
 
 
 
 
 
 
 
 
 
389
390		if (parent->state >= FSCACHE_OBJECT_DYING) {
391			_debug("bad parent");
392			set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
393		} else if (parent->state < FSCACHE_OBJECT_AVAILABLE) {
394			_debug("wait");
395
396			/* we may get woken up in this state by child objects
397			 * binding on to us, so we need to make sure we don't
398			 * add ourself to the list multiple times */
399			if (list_empty(&object->dep_link)) {
400				fscache_stat(&fscache_n_cop_grab_object);
401				object->cache->ops->grab_object(object);
402				fscache_stat_d(&fscache_n_cop_grab_object);
403				list_add(&object->dep_link,
404					 &parent->dependents);
405
406				/* fscache_acquire_non_index_cookie() uses this
407				 * to wake the chain up */
408				if (parent->state == FSCACHE_OBJECT_INIT)
409					fscache_enqueue_object(parent);
410			}
411		} else {
412			_debug("go");
413			parent->n_ops++;
414			parent->n_obj_ops++;
415			object->lookup_jif = jiffies;
416			object->state = FSCACHE_OBJECT_LOOKING_UP;
417			set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
418		}
419
420		spin_unlock(&parent->lock);
421		spin_unlock(&object->lock);
 
 
 
 
 
 
 
 
 
 
 
422	}
423
424	spin_unlock(&object->cookie->parent->lock);
425	spin_unlock(&object->cookie->lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
426	_leave("");
 
427}
428
429/*
430 * look an object up in the cache from which it was allocated
431 * - we hold an "access lock" on the parent object, so the parent object cannot
432 *   be withdrawn by either party till we've finished
433 * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
434 *   leaf-most cookies of the object and all its children
435 */
436static void fscache_lookup_object(struct fscache_object *object)
 
437{
438	struct fscache_cookie *cookie = object->cookie;
439	struct fscache_object *parent;
440	int ret;
441
442	_enter("");
 
 
443
444	parent = object->parent;
445	ASSERT(parent != NULL);
446	ASSERTCMP(parent->n_ops, >, 0);
447	ASSERTCMP(parent->n_obj_ops, >, 0);
448
449	/* make sure the parent is still available */
450	ASSERTCMP(parent->state, >=, FSCACHE_OBJECT_AVAILABLE);
451
452	if (parent->state >= FSCACHE_OBJECT_DYING ||
453	    test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
454		_debug("unavailable");
455		set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
456		_leave("");
457		return;
458	}
459
460	_debug("LOOKUP \"%s/%s\" in \"%s\"",
461	       parent->cookie->def->name, cookie->def->name,
462	       object->cache->tag->name);
463
464	fscache_stat(&fscache_n_object_lookups);
465	fscache_stat(&fscache_n_cop_lookup_object);
466	ret = object->cache->ops->lookup_object(object);
467	fscache_stat_d(&fscache_n_cop_lookup_object);
468
469	if (test_bit(FSCACHE_OBJECT_EV_ERROR, &object->events))
470		set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
471
472	if (ret == -ETIMEDOUT) {
473		/* probably stuck behind another object, so move this one to
474		 * the back of the queue */
475		fscache_stat(&fscache_n_object_lookups_timed_out);
476		set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
 
477	}
478
479	_leave("");
 
 
 
 
 
 
480}
481
482/**
483 * fscache_object_lookup_negative - Note negative cookie lookup
484 * @object: Object pointing to cookie to mark
485 *
486 * Note negative lookup, permitting those waiting to read data from an already
487 * existing backing object to continue as there's no data for them to read.
488 */
489void fscache_object_lookup_negative(struct fscache_object *object)
490{
491	struct fscache_cookie *cookie = object->cookie;
492
493	_enter("{OBJ%x,%s}",
494	       object->debug_id, fscache_object_states[object->state]);
495
496	spin_lock(&object->lock);
497	if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
498		fscache_stat(&fscache_n_object_lookups_negative);
499
500		/* transit here to allow write requests to begin stacking up
501		 * and read requests to begin returning ENODATA */
502		object->state = FSCACHE_OBJECT_CREATING;
503		spin_unlock(&object->lock);
504
505		set_bit(FSCACHE_COOKIE_PENDING_FILL, &cookie->flags);
506		set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
 
507
508		_debug("wake up lookup %p", &cookie->flags);
509		smp_mb__before_clear_bit();
510		clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
511		smp_mb__after_clear_bit();
512		wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
513		set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
514	} else {
515		ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
516		spin_unlock(&object->lock);
517	}
518
519	_leave("");
520}
521EXPORT_SYMBOL(fscache_object_lookup_negative);
522
523/**
524 * fscache_obtained_object - Note successful object lookup or creation
525 * @object: Object pointing to cookie to mark
526 *
527 * Note successful lookup and/or creation, permitting those waiting to write
528 * data to a backing object to continue.
529 *
530 * Note that after calling this, an object's cookie may be relinquished by the
531 * netfs, and so must be accessed with object lock held.
532 */
533void fscache_obtained_object(struct fscache_object *object)
534{
535	struct fscache_cookie *cookie = object->cookie;
536
537	_enter("{OBJ%x,%s}",
538	       object->debug_id, fscache_object_states[object->state]);
539
540	/* if we were still looking up, then we must have a positive lookup
541	 * result, in which case there may be data available */
542	spin_lock(&object->lock);
543	if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
544		fscache_stat(&fscache_n_object_lookups_positive);
545
546		clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
547
548		object->state = FSCACHE_OBJECT_AVAILABLE;
549		spin_unlock(&object->lock);
550
551		smp_mb__before_clear_bit();
552		clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
553		smp_mb__after_clear_bit();
554		wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
555		set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
556	} else {
557		ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
558		fscache_stat(&fscache_n_object_created);
559
560		object->state = FSCACHE_OBJECT_AVAILABLE;
561		spin_unlock(&object->lock);
562		set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
563		smp_wmb();
564	}
565
566	if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, &cookie->flags))
567		wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
568
569	_leave("");
570}
571EXPORT_SYMBOL(fscache_obtained_object);
572
573/*
574 * handle an object that has just become available
575 */
576static void fscache_object_available(struct fscache_object *object)
 
577{
578	_enter("{OBJ%x}", object->debug_id);
 
 
579
580	spin_lock(&object->lock);
581
582	if (object->cookie &&
583	    test_and_clear_bit(FSCACHE_COOKIE_CREATING, &object->cookie->flags))
584		wake_up_bit(&object->cookie->flags, FSCACHE_COOKIE_CREATING);
585
586	fscache_done_parent_op(object);
587	if (object->n_in_progress == 0) {
588		if (object->n_ops > 0) {
589			ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
590			ASSERTIF(object->n_ops > object->n_obj_ops,
591				 !list_empty(&object->pending_ops));
592			fscache_start_operations(object);
593		} else {
594			ASSERT(list_empty(&object->pending_ops));
595		}
596	}
597	spin_unlock(&object->lock);
598
599	fscache_stat(&fscache_n_cop_lookup_complete);
600	object->cache->ops->lookup_complete(object);
601	fscache_stat_d(&fscache_n_cop_lookup_complete);
602	fscache_enqueue_dependents(object);
603
604	fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
605	fscache_stat(&fscache_n_object_avail);
606
607	_leave("");
 
608}
609
610/*
611 * drop an object's attachments
612 */
613static void fscache_drop_object(struct fscache_object *object)
 
614{
615	struct fscache_object *parent = object->parent;
616	struct fscache_cache *cache = object->cache;
617
618	_enter("{OBJ%x,%d}", object->debug_id, object->n_children);
 
 
 
619
620	ASSERTCMP(object->cookie, ==, NULL);
621	ASSERT(hlist_unhashed(&object->cookie_link));
 
 
 
 
 
622
623	spin_lock(&cache->object_list_lock);
624	list_del_init(&object->cache_link);
625	spin_unlock(&cache->object_list_lock);
626
627	fscache_stat(&fscache_n_cop_drop_object);
628	cache->ops->drop_object(object);
629	fscache_stat_d(&fscache_n_cop_drop_object);
630
631	if (parent) {
632		_debug("release parent OBJ%x {%d}",
633		       parent->debug_id, parent->n_children);
634
635		spin_lock(&parent->lock);
636		parent->n_children--;
637		if (parent->n_children == 0)
638			fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
639		spin_unlock(&parent->lock);
640		object->parent = NULL;
641	}
642
643	/* this just shifts the object release to the work processor */
644	fscache_put_object(object);
 
 
645
646	_leave("");
 
647}
648
649/*
650 * release or recycle an object that the netfs has discarded
 
651 */
652static void fscache_release_object(struct fscache_object *object)
 
653{
654	_enter("");
 
 
 
 
655
656	fscache_drop_object(object);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
657}
658
659/*
660 * withdraw an object from active service
661 */
662static void fscache_withdraw_object(struct fscache_object *object)
 
663{
664	struct fscache_cookie *cookie;
665	bool detached;
666
667	_enter("");
 
 
 
668
669	spin_lock(&object->lock);
670	cookie = object->cookie;
671	if (cookie) {
672		/* need to get the cookie lock before the object lock, starting
673		 * from the object pointer */
674		atomic_inc(&cookie->usage);
675		spin_unlock(&object->lock);
 
 
 
676
677		detached = false;
678		spin_lock(&cookie->lock);
679		spin_lock(&object->lock);
680
681		if (object->cookie == cookie) {
682			hlist_del_init(&object->cookie_link);
683			object->cookie = NULL;
684			detached = true;
685		}
686		spin_unlock(&cookie->lock);
687		fscache_cookie_put(cookie);
688		if (detached)
689			fscache_cookie_put(cookie);
690	}
691
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
692	spin_unlock(&object->lock);
693
694	fscache_drop_object(object);
695}
 
 
696
697/*
698 * withdraw an object from active service at the behest of the cache
699 * - need break the links to a cached object cookie
700 * - called under two situations:
701 *   (1) recycler decides to reclaim an in-use object
702 *   (2) a cache is unmounted
703 * - have to take care as the cookie can be being relinquished by the netfs
704 *   simultaneously
705 * - the object is pinned by the caller holding a refcount on it
706 */
707void fscache_withdrawing_object(struct fscache_cache *cache,
708				struct fscache_object *object)
709{
710	bool enqueue = false;
711
712	_enter(",OBJ%x", object->debug_id);
 
 
 
713
714	spin_lock(&object->lock);
715	if (object->state < FSCACHE_OBJECT_WITHDRAWING) {
716		object->state = FSCACHE_OBJECT_WITHDRAWING;
717		enqueue = true;
 
 
718	}
719	spin_unlock(&object->lock);
720
721	if (enqueue)
722		fscache_enqueue_object(object);
 
723
724	_leave("");
 
725}
726
727/*
728 * get a ref on an object
729 */
730static int fscache_get_object(struct fscache_object *object)
731{
732	int ret;
733
734	fscache_stat(&fscache_n_cop_grab_object);
735	ret = object->cache->ops->grab_object(object) ? 0 : -EAGAIN;
736	fscache_stat_d(&fscache_n_cop_grab_object);
737	return ret;
738}
739
740/*
741 * discard a ref on a work item
742 */
743static void fscache_put_object(struct fscache_object *object)
744{
745	fscache_stat(&fscache_n_cop_put_object);
746	object->cache->ops->put_object(object);
747	fscache_stat_d(&fscache_n_cop_put_object);
748}
749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
750/*
751 * enqueue an object for metadata-type processing
752 */
753void fscache_enqueue_object(struct fscache_object *object)
754{
755	_enter("{OBJ%x}", object->debug_id);
756
757	if (fscache_get_object(object) >= 0) {
758		wait_queue_head_t *cong_wq =
759			&get_cpu_var(fscache_object_cong_wait);
760
761		if (queue_work(fscache_object_wq, &object->work)) {
762			if (fscache_object_congested())
763				wake_up(cong_wq);
764		} else
765			fscache_put_object(object);
766
767		put_cpu_var(fscache_object_cong_wait);
768	}
769}
770
771/**
772 * fscache_object_sleep_till_congested - Sleep until object wq is congested
773 * @timoutp: Scheduler sleep timeout
774 *
775 * Allow an object handler to sleep until the object workqueue is congested.
776 *
777 * The caller must set up a wake up event before calling this and must have set
778 * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
779 * condition before calling this function as no test is made here.
780 *
781 * %true is returned if the object wq is congested, %false otherwise.
782 */
783bool fscache_object_sleep_till_congested(signed long *timeoutp)
784{
785	wait_queue_head_t *cong_wq = &__get_cpu_var(fscache_object_cong_wait);
786	DEFINE_WAIT(wait);
787
788	if (fscache_object_congested())
789		return true;
790
791	add_wait_queue_exclusive(cong_wq, &wait);
792	if (!fscache_object_congested())
793		*timeoutp = schedule_timeout(*timeoutp);
794	finish_wait(cong_wq, &wait);
795
796	return fscache_object_congested();
797}
798EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
799
800/*
801 * enqueue the dependents of an object for metadata-type processing
802 * - the caller must hold the object's lock
803 * - this may cause an already locked object to wind up being processed again
 
 
804 */
805static void fscache_enqueue_dependents(struct fscache_object *object)
806{
807	struct fscache_object *dep;
 
808
809	_enter("{OBJ%x}", object->debug_id);
810
811	if (list_empty(&object->dependents))
812		return;
813
814	spin_lock(&object->lock);
815
816	while (!list_empty(&object->dependents)) {
817		dep = list_entry(object->dependents.next,
818				 struct fscache_object, dep_link);
819		list_del_init(&dep->dep_link);
820
821
822		/* sort onto appropriate lists */
823		fscache_enqueue_object(dep);
824		fscache_put_object(dep);
825
826		if (!list_empty(&object->dependents))
827			cond_resched_lock(&object->lock);
 
 
828	}
829
830	spin_unlock(&object->lock);
 
831}
832
833/*
834 * remove an object from whatever queue it's waiting on
835 * - the caller must hold object->lock
836 */
837void fscache_dequeue_object(struct fscache_object *object)
838{
839	_enter("{OBJ%x}", object->debug_id);
840
841	if (!list_empty(&object->dep_link)) {
842		spin_lock(&object->parent->lock);
843		list_del_init(&object->dep_link);
844		spin_unlock(&object->parent->lock);
845	}
846
847	_leave("");
848}
849
850/**
851 * fscache_check_aux - Ask the netfs whether an object on disk is still valid
852 * @object: The object to ask about
853 * @data: The auxiliary data for the object
854 * @datalen: The size of the auxiliary data
855 *
856 * This function consults the netfs about the coherency state of an object
 
 
 
857 */
858enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
859					const void *data, uint16_t datalen)
860{
861	enum fscache_checkaux result;
862
863	if (!object->cookie->def->check_aux) {
864		fscache_stat(&fscache_n_checkaux_none);
865		return FSCACHE_CHECKAUX_OKAY;
866	}
867
868	result = object->cookie->def->check_aux(object->cookie->netfs_data,
869						data, datalen);
870	switch (result) {
871		/* entry okay as is */
872	case FSCACHE_CHECKAUX_OKAY:
873		fscache_stat(&fscache_n_checkaux_okay);
874		break;
875
876		/* entry requires update */
877	case FSCACHE_CHECKAUX_NEEDS_UPDATE:
878		fscache_stat(&fscache_n_checkaux_update);
879		break;
880
881		/* entry requires deletion */
882	case FSCACHE_CHECKAUX_OBSOLETE:
883		fscache_stat(&fscache_n_checkaux_obsolete);
884		break;
885
886	default:
887		BUG();
888	}
889
890	return result;
891}
892EXPORT_SYMBOL(fscache_check_aux);