cregit-Linux how code gets into the kernel

Release 4.10 fs/fscache/object.c

Directory: fs/fscache
/* FS-Cache object state machine handler
 *
 * Copyright (C) 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.
 *
 * See Documentation/filesystems/caching/object.txt for a description of the
 * object state machine and the in-kernel representations.
 */


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

static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *, int);
static const struct fscache_state *fscache_kill_dependents(struct fscache_object *, int);
static const struct fscache_state *fscache_drop_object(struct fscache_object *, int);
static const struct fscache_state *fscache_initialise_object(struct fscache_object *, int);
static const struct fscache_state *fscache_invalidate_object(struct fscache_object *, int);
static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *, int);
static const struct fscache_state *fscache_kill_object(struct fscache_object *, int);
static const struct fscache_state *fscache_lookup_failure(struct fscache_object *, int);
static const struct fscache_state *fscache_look_up_object(struct fscache_object *, int);
static const struct fscache_state *fscache_object_available(struct fscache_object *, int);
static const struct fscache_state *fscache_parent_ready(struct fscache_object *, int);
static const struct fscache_state *fscache_update_object(struct fscache_object *, int);
static const struct fscache_state *fscache_object_dead(struct fscache_object *, int);


#define __STATE_NAME(n) fscache_osm_##n

#define STATE(n) (&__STATE_NAME(n))

/*
 * Define a work state.  Work states are execution states.  No event processing
 * is performed by them.  The function attached to a work state returns a
 * pointer indicating the next state to which the state machine should
 * transition.  Returning NO_TRANSIT repeats the current state, but goes back
 * to the scheduler first.
 */

#define WORK_STATE(n, sn, f) \
	const struct fscache_state __STATE_NAME(n) = {                  \
                .name = #n,                                             \
                .short_name = sn,                                       \
                .work = f                                               \
        }

/*
 * Returns from work states.
 */

#define transit_to(state) ({ prefetch(&STATE(state)->work); STATE(state); })


#define NO_TRANSIT ((struct fscache_state *)NULL)

/*
 * Define a wait state.  Wait states are event processing states.  No execution
 * is performed by them.  Wait states are just tables of "if event X occurs,
 * clear it and transition to state Y".  The dispatcher returns to the
 * scheduler if none of the events in which the wait state has an interest are
 * currently pending.
 */

#define WAIT_STATE(n, sn, ...) \
	const struct fscache_state __STATE_NAME(n) = {                  \
                .name = #n,                                             \
                .short_name = sn,                                       \
                .work = NULL,                                           \
                .transitions = { __VA_ARGS__, { 0, NULL } }             \
        }


#define TRANSIT_TO(state, emask) \
	{ .events = (emask), .transit_to = STATE(state) }

/*
 * The object state machine.
 */
static WORK_STATE(INIT_OBJECT,		"INIT", fscache_initialise_object);
static WORK_STATE(PARENT_READY,		"PRDY", fscache_parent_ready);
static WORK_STATE(ABORT_INIT,		"ABRT", fscache_abort_initialisation);
static WORK_STATE(LOOK_UP_OBJECT,	"LOOK", fscache_look_up_object);
static WORK_STATE(CREATE_OBJECT,	"CRTO", fscache_look_up_object);
static WORK_STATE(OBJECT_AVAILABLE,	"AVBL", fscache_object_available);
static WORK_STATE(JUMPSTART_DEPS,	"JUMP", fscache_jumpstart_dependents);

static WORK_STATE(INVALIDATE_OBJECT,	"INVL", fscache_invalidate_object);
static WORK_STATE(UPDATE_OBJECT,	"UPDT", fscache_update_object);

static WORK_STATE(LOOKUP_FAILURE,	"LCFL", fscache_lookup_failure);
static WORK_STATE(KILL_OBJECT,		"KILL", fscache_kill_object);
static WORK_STATE(KILL_DEPENDENTS,	"KDEP", fscache_kill_dependents);
static WORK_STATE(DROP_OBJECT,		"DROP", fscache_drop_object);
static WORK_STATE(OBJECT_DEAD,		"DEAD", fscache_object_dead);

static WAIT_STATE(WAIT_FOR_INIT,	"?INI",
		  TRANSIT_TO(INIT_OBJECT,	1 << FSCACHE_OBJECT_EV_NEW_CHILD));

static WAIT_STATE(WAIT_FOR_PARENT,	"?PRN",
		  TRANSIT_TO(PARENT_READY,	1 << FSCACHE_OBJECT_EV_PARENT_READY));

static WAIT_STATE(WAIT_FOR_CMD,		"?CMD",
		  TRANSIT_TO(INVALIDATE_OBJECT,	1 << FSCACHE_OBJECT_EV_INVALIDATE),
		  TRANSIT_TO(UPDATE_OBJECT,	1 << FSCACHE_OBJECT_EV_UPDATE),
		  TRANSIT_TO(JUMPSTART_DEPS,	1 << FSCACHE_OBJECT_EV_NEW_CHILD));

static WAIT_STATE(WAIT_FOR_CLEARANCE,	"?CLR",
		  TRANSIT_TO(KILL_OBJECT,	1 << FSCACHE_OBJECT_EV_CLEARED));

/*
 * Out-of-band event transition tables.  These are for handling unexpected
 * events, such as an I/O error.  If an OOB event occurs, the state machine
 * clears and disables the event and forces a transition to the nominated work
 * state (acurrently executing work states will complete first).
 *
 * In such a situation, object->state remembers the state the machine should
 * have been in/gone to and returning NO_TRANSIT returns to that.
 */

static const struct fscache_transition fscache_osm_init_oob[] = {
	   TRANSIT_TO(ABORT_INIT,
		      (1 << FSCACHE_OBJECT_EV_ERROR) |
		      (1 << FSCACHE_OBJECT_EV_KILL)),
	   { 0, NULL }
};


static const struct fscache_transition fscache_osm_lookup_oob[] = {
	   TRANSIT_TO(LOOKUP_FAILURE,
		      (1 << FSCACHE_OBJECT_EV_ERROR) |
		      (1 << FSCACHE_OBJECT_EV_KILL)),
	   { 0, NULL }
};


static const struct fscache_transition fscache_osm_run_oob[] = {
	   TRANSIT_TO(KILL_OBJECT,
		      (1 << FSCACHE_OBJECT_EV_ERROR) |
		      (1 << FSCACHE_OBJECT_EV_KILL)),
	   { 0, NULL }
};

static int  fscache_get_object(struct fscache_object *);
static void fscache_put_object(struct fscache_object *);
static bool fscache_enqueue_dependents(struct fscache_object *, int);
static void fscache_dequeue_object(struct fscache_object *);

/*
 * we need to notify the parent when an op completes that we had outstanding
 * upon it
 */

static inline void fscache_done_parent_op(struct fscache_object *object) { struct fscache_object *parent = object->parent; _enter("OBJ%x {OBJ%x,%x}", object->debug_id, parent->debug_id, parent->n_ops); spin_lock_nested(&parent->lock, 1); parent->n_obj_ops--; parent->n_ops--; if (parent->n_ops == 0) fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED); spin_unlock(&parent->lock); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells81100.00%4100.00%
Total81100.00%4100.00%

/* * Object state machine dispatcher. */
static void fscache_object_sm_dispatcher(struct fscache_object *object) { const struct fscache_transition *t; const struct fscache_state *state, *new_state; unsigned long events, event_mask; int event = -1; ASSERT(object != NULL); _enter("{OBJ%x,%s,%lx}", object->debug_id, object->state->name, object->events); event_mask = object->event_mask; restart: object->event_mask = 0; /* Mask normal event handling */ state = object->state; restart_masked: events = object->events; /* Handle any out-of-band events (typically an error) */ if (events & object->oob_event_mask) { _debug("{OBJ%x} oob %lx", object->debug_id, events & object->oob_event_mask); for (t = object->oob_table; t->events; t++) { if (events & t->events) { state = t->transit_to; ASSERT(state->work != NULL); event = fls(events & t->events) - 1; __clear_bit(event, &object->oob_event_mask); clear_bit(event, &object->events); goto execute_work_state; } } } /* Wait states are just transition tables */ if (!state->work) { if (events & event_mask) { for (t = state->transitions; t->events; t++) { if (events & t->events) { new_state = t->transit_to; event = fls(events & t->events) - 1; clear_bit(event, &object->events); _debug("{OBJ%x} ev %d: %s -> %s", object->debug_id, event, state->name, new_state->name); object->state = state = new_state; goto execute_work_state; } } /* The event mask didn't include all the tabled bits */ BUG(); } /* Randomly woke up */ goto unmask_events; } execute_work_state: _debug("{OBJ%x} exec %s", object->debug_id, state->name); new_state = state->work(object, event); event = -1; if (new_state == NO_TRANSIT) { _debug("{OBJ%x} %s notrans", object->debug_id, state->name); if (unlikely(state == STATE(OBJECT_DEAD))) { _leave(" [dead]"); return; } fscache_enqueue_object(object); event_mask = object->oob_event_mask; goto unmask_events; } _debug("{OBJ%x} %s -> %s", object->debug_id, state->name, new_state->name); object->state = state = new_state; if (state->work) { if (unlikely(state == STATE(OBJECT_DEAD))) { _leave(" [dead]"); return; } goto restart_masked; } /* Transited to wait state */ event_mask = object->oob_event_mask; for (t = state->transitions; t->events; t++) event_mask |= t->events; unmask_events: object->event_mask = event_mask; smp_mb(); events = object->events; if (events & event_mask) goto restart; _leave(" [msk %lx]", event_mask); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells512100.00%6100.00%
Total512100.00%6100.00%

/* * execute an object */
static void fscache_object_work_func(struct work_struct *work) { struct fscache_object *object = container_of(work, struct fscache_object, work); unsigned long start; _enter("{OBJ%x}", object->debug_id); start = jiffies; fscache_object_sm_dispatcher(object); fscache_hist(fscache_objs_histogram, start); fscache_put_object(object); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells5795.00%480.00%
tejun heotejun heo35.00%120.00%
Total60100.00%5100.00%

/** * fscache_object_init - Initialise a cache object description * @object: Object description * @cookie: Cookie object will be attached to * @cache: Cache in which backing object will be found * * Initialise a cache object description to its basic values. * * See Documentation/filesystems/caching/backend-api.txt for a complete * description. */
void fscache_object_init(struct fscache_object *object, struct fscache_cookie *cookie, struct fscache_cache *cache) { const struct fscache_transition *t; atomic_inc(&cache->object_count); object->state = STATE(WAIT_FOR_INIT); object->oob_table = fscache_osm_init_oob; object->flags = 1 << FSCACHE_OBJECT_IS_LIVE; spin_lock_init(&object->lock); INIT_LIST_HEAD(&object->cache_link); INIT_HLIST_NODE(&object->cookie_link); INIT_WORK(&object->work, fscache_object_work_func); INIT_LIST_HEAD(&object->dependents); INIT_LIST_HEAD(&object->dep_link); INIT_LIST_HEAD(&object->pending_ops); object->n_children = 0; object->n_ops = object->n_in_progress = object->n_exclusive = 0; object->events = 0; object->store_limit = 0; object->store_limit_l = 0; object->cache = cache; object->cookie = cookie; object->parent = NULL; #ifdef CONFIG_FSCACHE_OBJECT_LIST RB_CLEAR_NODE(&object->objlist_link); #endif object->oob_event_mask = 0; for (t = object->oob_table; t->events; t++) object->oob_event_mask |= t->events; object->event_mask = object->oob_event_mask; for (t = object->state->transitions; t->events; t++) object->event_mask |= t->events; }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells246100.00%3100.00%
Total246100.00%3100.00%

EXPORT_SYMBOL(fscache_object_init); /* * Mark the object as no longer being live, making sure that we synchronise * against op submission. */
static inline void fscache_mark_object_dead(struct fscache_object *object) { spin_lock(&object->lock); clear_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags); spin_unlock(&object->lock); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells38100.00%1100.00%
Total38100.00%1100.00%

/* * Abort object initialisation before we start it. */
static const struct fscache_state *fscache_abort_initialisation(struct fscache_object *object, int event) { _enter("{OBJ%x},%d", object->debug_id, event); object->oob_event_mask = 0; fscache_dequeue_object(object); return transit_to(KILL_OBJECT); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells45100.00%2100.00%
Total45100.00%2100.00%

/* * initialise an object * - check the specified object's parent to see if we can make use of it * immediately to do a creation * - we may need to start the process of creating a parent and we need to wait * for the parent's lookup and creation to complete if it's not there yet */
static const struct fscache_state *fscache_initialise_object(struct fscache_object *object, int event) { struct fscache_object *parent; bool success; _enter("{OBJ%x},%d", object->debug_id, event); ASSERT(list_empty(&object->dep_link)); parent = object->parent; if (!parent) { _leave(" [no parent]"); return transit_to(DROP_OBJECT); } _debug("parent: %s of:%lx", parent->state->name, parent->flags); if (fscache_object_is_dying(parent)) { _leave(" [bad parent]"); return transit_to(DROP_OBJECT); } if (fscache_object_is_available(parent)) { _leave(" [ready]"); return transit_to(PARENT_READY); } _debug("wait"); spin_lock(&parent->lock); fscache_stat(&fscache_n_cop_grab_object); success = false; if (fscache_object_is_live(parent) && object->cache->ops->grab_object(object)) { list_add(&object->dep_link, &parent->dependents); success = true; } fscache_stat_d(&fscache_n_cop_grab_object); spin_unlock(&parent->lock); if (!success) { _leave(" [grab failed]"); return transit_to(DROP_OBJECT); } /* fscache_acquire_non_index_cookie() uses this * to wake the chain up */ fscache_raise_event(parent, FSCACHE_OBJECT_EV_NEW_CHILD); _leave(" [wait]"); return transit_to(WAIT_FOR_PARENT); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells237100.00%5100.00%
Total237100.00%5100.00%

/* * Once the parent object is ready, we should kick off our lookup op. */
static const struct fscache_state *fscache_parent_ready(struct fscache_object *object, int event) { struct fscache_object *parent = object->parent; _enter("{OBJ%x},%d", object->debug_id, event); ASSERT(parent != NULL); spin_lock(&parent->lock); parent->n_ops++; parent->n_obj_ops++; object->lookup_jif = jiffies; spin_unlock(&parent->lock); _leave(""); return transit_to(LOOK_UP_OBJECT); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells87100.00%2100.00%
Total87100.00%2100.00%

/* * look an object up in the cache from which it was allocated * - we hold an "access lock" on the parent object, so the parent object cannot * be withdrawn by either party till we've finished */
static const struct fscache_state *fscache_look_up_object(struct fscache_object *object, int event) { struct fscache_cookie *cookie = object->cookie; struct fscache_object *parent = object->parent; int ret; _enter("{OBJ%x},%d", object->debug_id, event); object->oob_table = fscache_osm_lookup_oob; ASSERT(parent != NULL); ASSERTCMP(parent->n_ops, >, 0); ASSERTCMP(parent->n_obj_ops, >, 0); /* make sure the parent is still available */ ASSERT(fscache_object_is_available(parent)); if (fscache_object_is_dying(parent) || test_bit(FSCACHE_IOERROR, &object->cache->flags) || !fscache_use_cookie(object)) { _leave(" [unavailable]"); return transit_to(LOOKUP_FAILURE); } _debug("LOOKUP \"%s\" in \"%s\"", cookie->def->name, object->cache->tag->name); fscache_stat(&fscache_n_object_lookups); fscache_stat(&fscache_n_cop_lookup_object); ret = object->cache->ops->lookup_object(object); fscache_stat_d(&fscache_n_cop_lookup_object); fscache_unuse_cookie(object); if (ret == -ETIMEDOUT) { /* probably stuck behind another object, so move this one to * the back of the queue */ fscache_stat(&fscache_n_object_lookups_timed_out); _leave(" [timeout]"); return NO_TRANSIT; } if (ret < 0) { _leave(" [error]"); return transit_to(LOOKUP_FAILURE); } _leave(" [ok]"); return transit_to(OBJECT_AVAILABLE); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells240100.00%6100.00%
Total240100.00%6100.00%

/** * fscache_object_lookup_negative - Note negative cookie lookup * @object: Object pointing to cookie to mark * * Note negative lookup, permitting those waiting to read data from an already * existing backing object to continue as there's no data for them to read. */
void fscache_object_lookup_negative(struct fscache_object *object) { struct fscache_cookie *cookie = object->cookie; _enter("{OBJ%x,%s}", object->debug_id, object->state->name); if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) { fscache_stat(&fscache_n_object_lookups_negative); /* Allow write requests to begin stacking up and read requests to begin * returning ENODATA. */ set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags); _debug("wake up lookup %p", &cookie->flags); clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags); wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP); } _leave(""); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells111100.00%3100.00%
Total111100.00%3100.00%

EXPORT_SYMBOL(fscache_object_lookup_negative); /** * fscache_obtained_object - Note successful object lookup or creation * @object: Object pointing to cookie to mark * * Note successful lookup and/or creation, permitting those waiting to write * data to a backing object to continue. * * Note that after calling this, an object's cookie may be relinquished by the * netfs, and so must be accessed with object lock held. */
void fscache_obtained_object(struct fscache_object *object) { struct fscache_cookie *cookie = object->cookie; _enter("{OBJ%x,%s}", object->debug_id, object->state->name); /* if we were still looking up, then we must have a positive lookup * result, in which case there may be data available */ if (!test_and_set_bit(FSCACHE_OBJECT_IS_LOOKED_UP, &object->flags)) { fscache_stat(&fscache_n_object_lookups_positive); /* We do (presumably) have data */ clear_bit_unlock(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); clear_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags); /* Allow write requests to begin stacking up and read requests * to begin shovelling data. */ clear_bit_unlock(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags); wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP); } else { fscache_stat(&fscache_n_object_created); } set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags); _leave(""); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells122100.00%3100.00%
Total122100.00%3100.00%

EXPORT_SYMBOL(fscache_obtained_object); /* * handle an object that has just become available */
static const struct fscache_state *fscache_object_available(struct fscache_object *object, int event) { _enter("{OBJ%x},%d", object->debug_id, event); object->oob_table = fscache_osm_run_oob; spin_lock(&object->lock); fscache_done_parent_op(object); if (object->n_in_progress == 0) { if (object->n_ops > 0) { ASSERTCMP(object->n_ops, >=, object->n_obj_ops); fscache_start_operations(object); } else { ASSERT(list_empty(&object->pending_ops)); } } spin_unlock(&object->lock); fscache_stat(&fscache_n_cop_lookup_complete); object->cache->ops->lookup_complete(object); fscache_stat_d(&fscache_n_cop_lookup_complete); fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif); fscache_stat(&fscache_n_object_avail); _leave(""); return transit_to(JUMPSTART_DEPS); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells156100.00%2100.00%
Total156100.00%2100.00%

/* * Wake up this object's dependent objects now that we've become available. */
static const struct fscache_state *fscache_jumpstart_dependents(struct fscache_object *object, int event) { _enter("{OBJ%x},%d", object->debug_id, event); if (!fscache_enqueue_dependents(object, FSCACHE_OBJECT_EV_PARENT_READY)) return NO_TRANSIT; /* Not finished; requeue */ return transit_to(WAIT_FOR_CMD); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells48100.00%1100.00%
Total48100.00%1100.00%

/* * Handle lookup or creation failute. */
static const struct fscache_state *fscache_lookup_failure(struct fscache_object *object, int event) { struct fscache_cookie *cookie; _enter("{OBJ%x},%d", object->debug_id, event); object->oob_event_mask = 0; fscache_stat(&fscache_n_cop_lookup_complete); object->cache->ops->lookup_complete(object); fscache_stat_d(&fscache_n_cop_lookup_complete); set_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->flags); cookie = object->cookie; set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags); if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags)) wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP); fscache_done_parent_op(object); return transit_to(KILL_OBJECT); }

Contributors

PersonTokensPropCommitsCommitProp
david howellsdavid howells121100.00%4100.00%
Total121100.00%4100.00%

/* * Wait for completion of all active operations on this object and the death of * all child objects of this object. */
static const struct fscache_state *fscache_kill_object(struct fscache_object *object, int event) { _enter("{OBJ%x,%d,%d},%d", object->debug_id, object->n_ops, object->n_children, event); fscache_mark_object_dead(object); object->oob_event_mask = 0; if (test_bit(FSCACHE_OBJECT_RETIRED, &object->flags)) { /* Reject any new read/write ops and abort any that are pending. */ clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags); fscache_cancel_all_ops(object); } if (list_empty