Skip to content

Commit caaef69

Browse files
committed
FS-Cache: Fix object state machine to have separate work and wait states
Fix object state machine to have separate work and wait states as that makes it easier to envision. There are now three kinds of state: (1) Work state. This is an execution state. No event processing is performed by a work state. The function attached to a work state returns a pointer indicating the next state to which the OSM should transition. Returning NO_TRANSIT repeats the current state, but goes back to the scheduler first. (2) Wait state. This is an event processing state. No execution is performed by a wait state. 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. (3) Out-of-band state. This is a special work state. Transitions to normal states can be overridden when an unexpected event occurs (eg. I/O error). Instead the dispatcher disables and clears the OOB event and transits to the specified work state. This then acts as an ordinary work state, though object->state points to the overridden destination. Returning NO_TRANSIT resumes the overridden transition. In addition, the states have names in their definitions, so there's no need for tables of state names. Further, the EV_REQUEUE event is no longer necessary as that is automatic for work states. Since the states are now separate structs rather than values in an enum, it's not possible to use comparisons other than (non-)equality between them, so use some object->flags to indicate what phase an object is in. The EV_RELEASE, EV_RETIRE and EV_WITHDRAW events have been squished into one (EV_KILL). An object flag now carries the information about retirement. Similarly, the RELEASING, RECYCLING and WITHDRAWING states have been merged into an KILL_OBJECT state and additional states have been added for handling waiting dependent objects (JUMPSTART_DEPS and KILL_DEPENDENTS). A state has also been added for synchronising with parent object initialisation (WAIT_FOR_PARENT) and another for initiating look up (PARENT_READY). Signed-off-by: David Howells <dhowells@redhat.com> Tested-By: Milosz Tanski <milosz@adfin.com> Acked-by: Jeff Layton <jlayton@redhat.com>
1 parent 493f7bc commit caaef69

10 files changed

Lines changed: 605 additions & 577 deletions

File tree

‎fs/cachefiles/interface.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ static void cachefiles_drop_object(struct fscache_object *_object)
263263
#endif
264264

265265
/* delete retired objects */
266-
if (object->fscache.state == FSCACHE_OBJECT_RECYCLING &&
266+
if (test_bit(FSCACHE_OBJECT_RETIRE, &object->fscache.flags) &&
267267
_object != cache->cache.fsdef
268268
) {
269269
_debug("- retire object OBJ%x", object->fscache.debug_id);

‎fs/cachefiles/namei.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void __cachefiles_printk_object(struct cachefiles_object *object,
3838
printk(KERN_ERR "%sobject: OBJ%x\n",
3939
prefix, object->fscache.debug_id);
4040
printk(KERN_ERR "%sobjstate=%s fl=%lx wbusy=%x ev=%lx[%lx]\n",
41-
prefix, fscache_object_states[object->fscache.state],
41+
prefix, object->fscache.state->name,
4242
object->fscache.flags, work_busy(&object->fscache.work),
4343
object->fscache.events, object->fscache.event_mask);
4444
printk(KERN_ERR "%sops=%u inp=%u exc=%u\n",
@@ -127,7 +127,7 @@ static void cachefiles_mark_object_buried(struct cachefiles_cache *cache,
127127
found_dentry:
128128
kdebug("preemptive burial: OBJ%x [%s] %p",
129129
object->fscache.debug_id,
130-
fscache_object_states[object->fscache.state],
130+
object->fscache.state->name,
131131
dentry);
132132

133133
if (fscache_object_is_live(&object->fscache)) {

‎fs/fscache/cache.c‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,10 @@ int fscache_add_cache(struct fscache_cache *cache,
224224
BUG_ON(!ifsdef);
225225

226226
cache->flags = 0;
227-
ifsdef->event_mask = ULONG_MAX & ~(1 << FSCACHE_OBJECT_EV_CLEARED);
228-
ifsdef->state = FSCACHE_OBJECT_ACTIVE;
227+
ifsdef->event_mask =
228+
((1 << NR_FSCACHE_OBJECT_EVENTS) - 1) &
229+
~(1 << FSCACHE_OBJECT_EV_CLEARED);
230+
__set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &ifsdef->flags);
229231

230232
if (!tagname)
231233
tagname = cache->identifier;
@@ -330,25 +332,25 @@ static void fscache_withdraw_all_objects(struct fscache_cache *cache,
330332
{
331333
struct fscache_object *object;
332334

333-
spin_lock(&cache->object_list_lock);
334-
335335
while (!list_empty(&cache->object_list)) {
336-
object = list_entry(cache->object_list.next,
337-
struct fscache_object, cache_link);
338-
list_move_tail(&object->cache_link, dying_objects);
336+
spin_lock(&cache->object_list_lock);
339337

340-
_debug("withdraw %p", object->cookie);
338+
if (!list_empty(&cache->object_list)) {
339+
object = list_entry(cache->object_list.next,
340+
struct fscache_object, cache_link);
341+
list_move_tail(&object->cache_link, dying_objects);
341342

342-
spin_lock(&object->lock);
343-
spin_unlock(&cache->object_list_lock);
344-
fscache_raise_event(object, FSCACHE_OBJECT_EV_WITHDRAW);
345-
spin_unlock(&object->lock);
343+
_debug("withdraw %p", object->cookie);
344+
345+
/* This must be done under object_list_lock to prevent
346+
* a race with fscache_drop_object().
347+
*/
348+
fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
349+
}
346350

351+
spin_unlock(&cache->object_list_lock);
347352
cond_resched();
348-
spin_lock(&cache->object_list_lock);
349353
}
350-
351-
spin_unlock(&cache->object_list_lock);
352354
}
353355

354356
/**

‎fs/fscache/cookie.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie)
205205

206206
/* initiate the process of looking up all the objects in the chain
207207
* (done by fscache_initialise_object()) */
208-
fscache_enqueue_object(object);
208+
fscache_raise_event(object, FSCACHE_OBJECT_EV_NEW_CHILD);
209209

210210
spin_unlock(&cookie->lock);
211211

@@ -469,7 +469,6 @@ void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
469469
{
470470
struct fscache_cache *cache;
471471
struct fscache_object *object;
472-
unsigned long event;
473472

474473
fscache_stat(&fscache_n_relinquishes);
475474
if (retire)
@@ -497,8 +496,6 @@ void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
497496
fscache_wait_bit, TASK_UNINTERRUPTIBLE);
498497
}
499498

500-
event = retire ? FSCACHE_OBJECT_EV_RETIRE : FSCACHE_OBJECT_EV_RELEASE;
501-
502499
try_again:
503500
spin_lock(&cookie->lock);
504501

@@ -533,7 +530,9 @@ void __fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
533530

534531
cache = object->cache;
535532
object->cookie = NULL;
536-
fscache_raise_event(object, event);
533+
if (retire)
534+
set_bit(FSCACHE_OBJECT_RETIRE, &object->flags);
535+
fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
537536
spin_unlock(&object->lock);
538537

539538
if (atomic_dec_and_test(&cookie->usage))

‎fs/fscache/internal.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,6 @@ extern int fscache_wait_bit_interruptible(void *);
9797
/*
9898
* object.c
9999
*/
100-
extern const char fscache_object_states_short[FSCACHE_OBJECT__NSTATES][5];
101-
102-
extern void fscache_withdrawing_object(struct fscache_cache *,
103-
struct fscache_object *);
104100
extern void fscache_enqueue_object(struct fscache_object *);
105101

106102
/*
@@ -291,6 +287,10 @@ static inline void fscache_raise_event(struct fscache_object *object,
291287
unsigned event)
292288
{
293289
BUG_ON(event >= NR_FSCACHE_OBJECT_EVENTS);
290+
#if 0
291+
printk("*** fscache_raise_event(OBJ%d{%lx},%x)\n",
292+
object->debug_id, object->event_mask, (1 << event));
293+
#endif
294294
if (!test_and_set_bit(event, &object->events) &&
295295
test_bit(event, &object->event_mask))
296296
fscache_enqueue_object(object);

‎fs/fscache/object-list.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static int fscache_objlist_show(struct seq_file *m, void *v)
174174

175175
if ((unsigned long) v == 1) {
176176
seq_puts(m, "OBJECT PARENT STAT CHLDN OPS OOP IPR EX READS"
177-
" EM EV F S"
177+
" EM EV FL S"
178178
" | NETFS_COOKIE_DEF TY FL NETFS_DATA");
179179
if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
180180
FSCACHE_OBJLIST_CONFIG_AUX))
@@ -193,7 +193,7 @@ static int fscache_objlist_show(struct seq_file *m, void *v)
193193

194194
if ((unsigned long) v == 2) {
195195
seq_puts(m, "======== ======== ==== ===== === === === == ====="
196-
" == == = ="
196+
" == == == ="
197197
" | ================ == == ================");
198198
if (config & (FSCACHE_OBJLIST_CONFIG_KEY |
199199
FSCACHE_OBJLIST_CONFIG_AUX))
@@ -219,7 +219,7 @@ static int fscache_objlist_show(struct seq_file *m, void *v)
219219
if (~config) {
220220
FILTER(obj->cookie,
221221
COOKIE, NOCOOKIE);
222-
FILTER(obj->state != FSCACHE_OBJECT_ACTIVE ||
222+
FILTER(fscache_object_is_active(obj) ||
223223
obj->n_ops != 0 ||
224224
obj->n_obj_ops != 0 ||
225225
obj->flags ||
@@ -235,10 +235,10 @@ static int fscache_objlist_show(struct seq_file *m, void *v)
235235
}
236236

237237
seq_printf(m,
238-
"%8x %8x %s %5u %3u %3u %3u %2u %5u %2lx %2lx %1lx %1x | ",
238+
"%8x %8x %s %5u %3u %3u %3u %2u %5u %2lx %2lx %2lx %1x | ",
239239
obj->debug_id,
240240
obj->parent ? obj->parent->debug_id : -1,
241-
fscache_object_states_short[obj->state],
241+
obj->state->short_name,
242242
obj->n_children,
243243
obj->n_ops,
244244
obj->n_obj_ops,

0 commit comments

Comments
 (0)