0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #define FSCACHE_DEBUG_LEVEL COOKIE
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include "internal.h"
0015
0016 struct kmem_cache *fscache_cookie_jar;
0017
0018 static void fscache_cookie_lru_timed_out(struct timer_list *timer);
0019 static void fscache_cookie_lru_worker(struct work_struct *work);
0020 static void fscache_cookie_worker(struct work_struct *work);
0021 static void fscache_unhash_cookie(struct fscache_cookie *cookie);
0022 static void fscache_perform_invalidation(struct fscache_cookie *cookie);
0023
0024 #define fscache_cookie_hash_shift 15
0025 static struct hlist_bl_head fscache_cookie_hash[1 << fscache_cookie_hash_shift];
0026 static LIST_HEAD(fscache_cookies);
0027 static DEFINE_RWLOCK(fscache_cookies_lock);
0028 static LIST_HEAD(fscache_cookie_lru);
0029 static DEFINE_SPINLOCK(fscache_cookie_lru_lock);
0030 DEFINE_TIMER(fscache_cookie_lru_timer, fscache_cookie_lru_timed_out);
0031 static DECLARE_WORK(fscache_cookie_lru_work, fscache_cookie_lru_worker);
0032 static const char fscache_cookie_states[FSCACHE_COOKIE_STATE__NR] = "-LCAIFUWRD";
0033 static unsigned int fscache_lru_cookie_timeout = 10 * HZ;
0034
0035 void fscache_print_cookie(struct fscache_cookie *cookie, char prefix)
0036 {
0037 const u8 *k;
0038
0039 pr_err("%c-cookie c=%08x [fl=%lx na=%u nA=%u s=%c]\n",
0040 prefix,
0041 cookie->debug_id,
0042 cookie->flags,
0043 atomic_read(&cookie->n_active),
0044 atomic_read(&cookie->n_accesses),
0045 fscache_cookie_states[cookie->state]);
0046 pr_err("%c-cookie V=%08x [%s]\n",
0047 prefix,
0048 cookie->volume->debug_id,
0049 cookie->volume->key);
0050
0051 k = (cookie->key_len <= sizeof(cookie->inline_key)) ?
0052 cookie->inline_key : cookie->key;
0053 pr_err("%c-key=[%u] '%*phN'\n", prefix, cookie->key_len, cookie->key_len, k);
0054 }
0055
0056 static void fscache_free_cookie(struct fscache_cookie *cookie)
0057 {
0058 if (WARN_ON_ONCE(!list_empty(&cookie->commit_link))) {
0059 spin_lock(&fscache_cookie_lru_lock);
0060 list_del_init(&cookie->commit_link);
0061 spin_unlock(&fscache_cookie_lru_lock);
0062 fscache_stat_d(&fscache_n_cookies_lru);
0063 fscache_stat(&fscache_n_cookies_lru_removed);
0064 }
0065
0066 if (WARN_ON_ONCE(test_bit(FSCACHE_COOKIE_IS_HASHED, &cookie->flags))) {
0067 fscache_print_cookie(cookie, 'F');
0068 return;
0069 }
0070
0071 write_lock(&fscache_cookies_lock);
0072 list_del(&cookie->proc_link);
0073 write_unlock(&fscache_cookies_lock);
0074 if (cookie->aux_len > sizeof(cookie->inline_aux))
0075 kfree(cookie->aux);
0076 if (cookie->key_len > sizeof(cookie->inline_key))
0077 kfree(cookie->key);
0078 fscache_stat_d(&fscache_n_cookies);
0079 kmem_cache_free(fscache_cookie_jar, cookie);
0080 }
0081
0082 static void __fscache_queue_cookie(struct fscache_cookie *cookie)
0083 {
0084 if (!queue_work(fscache_wq, &cookie->work))
0085 fscache_put_cookie(cookie, fscache_cookie_put_over_queued);
0086 }
0087
0088 static void fscache_queue_cookie(struct fscache_cookie *cookie,
0089 enum fscache_cookie_trace where)
0090 {
0091 fscache_get_cookie(cookie, where);
0092 __fscache_queue_cookie(cookie);
0093 }
0094
0095
0096
0097
0098
0099
0100
0101 static void fscache_init_access_gate(struct fscache_cookie *cookie)
0102 {
0103 int n_accesses;
0104
0105 n_accesses = atomic_read(&cookie->n_accesses);
0106 trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
0107 n_accesses, fscache_access_cache_pin);
0108 set_bit(FSCACHE_COOKIE_NO_ACCESS_WAKE, &cookie->flags);
0109 }
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121 void fscache_end_cookie_access(struct fscache_cookie *cookie,
0122 enum fscache_access_trace why)
0123 {
0124 int n_accesses;
0125
0126 smp_mb__before_atomic();
0127 n_accesses = atomic_dec_return(&cookie->n_accesses);
0128 trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
0129 n_accesses, why);
0130 if (n_accesses == 0 &&
0131 !test_bit(FSCACHE_COOKIE_NO_ACCESS_WAKE, &cookie->flags))
0132 fscache_queue_cookie(cookie, fscache_cookie_get_end_access);
0133 }
0134 EXPORT_SYMBOL(fscache_end_cookie_access);
0135
0136
0137
0138
0139 static void __fscache_begin_cookie_access(struct fscache_cookie *cookie,
0140 enum fscache_access_trace why)
0141 {
0142 int n_accesses;
0143
0144 n_accesses = atomic_inc_return(&cookie->n_accesses);
0145 smp_mb__after_atomic();
0146
0147
0148 trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
0149 n_accesses, why);
0150 }
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 bool fscache_begin_cookie_access(struct fscache_cookie *cookie,
0179 enum fscache_access_trace why)
0180 {
0181 if (!test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags))
0182 return false;
0183 __fscache_begin_cookie_access(cookie, why);
0184 if (!test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags) ||
0185 !fscache_cache_is_live(cookie->volume->cache)) {
0186 fscache_end_cookie_access(cookie, fscache_access_unlive);
0187 return false;
0188 }
0189 return true;
0190 }
0191
0192 static inline void wake_up_cookie_state(struct fscache_cookie *cookie)
0193 {
0194
0195
0196
0197
0198
0199 smp_mb();
0200 wake_up_var(&cookie->state);
0201 }
0202
0203
0204
0205
0206
0207
0208 static void __fscache_set_cookie_state(struct fscache_cookie *cookie,
0209 enum fscache_cookie_state state)
0210 {
0211 smp_store_release(&cookie->state, state);
0212 }
0213
0214 static void fscache_set_cookie_state(struct fscache_cookie *cookie,
0215 enum fscache_cookie_state state)
0216 {
0217 spin_lock(&cookie->lock);
0218 __fscache_set_cookie_state(cookie, state);
0219 spin_unlock(&cookie->lock);
0220 wake_up_cookie_state(cookie);
0221 }
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233 void fscache_cookie_lookup_negative(struct fscache_cookie *cookie)
0234 {
0235 set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
0236 fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_CREATING);
0237 }
0238 EXPORT_SYMBOL(fscache_cookie_lookup_negative);
0239
0240
0241
0242
0243
0244
0245
0246
0247 void fscache_resume_after_invalidation(struct fscache_cookie *cookie)
0248 {
0249 fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_ACTIVE);
0250 }
0251 EXPORT_SYMBOL(fscache_resume_after_invalidation);
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262 void fscache_caching_failed(struct fscache_cookie *cookie)
0263 {
0264 clear_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags);
0265 fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_FAILED);
0266 trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref),
0267 fscache_cookie_failed);
0268 }
0269 EXPORT_SYMBOL(fscache_caching_failed);
0270
0271
0272
0273
0274
0275
0276
0277 static int fscache_set_key(struct fscache_cookie *cookie,
0278 const void *index_key, size_t index_key_len)
0279 {
0280 void *buf;
0281 size_t buf_size;
0282
0283 buf_size = round_up(index_key_len, sizeof(__le32));
0284
0285 if (index_key_len > sizeof(cookie->inline_key)) {
0286 buf = kzalloc(buf_size, GFP_KERNEL);
0287 if (!buf)
0288 return -ENOMEM;
0289 cookie->key = buf;
0290 } else {
0291 buf = cookie->inline_key;
0292 }
0293
0294 memcpy(buf, index_key, index_key_len);
0295 cookie->key_hash = fscache_hash(cookie->volume->key_hash,
0296 buf, buf_size);
0297 return 0;
0298 }
0299
0300 static bool fscache_cookie_same(const struct fscache_cookie *a,
0301 const struct fscache_cookie *b)
0302 {
0303 const void *ka, *kb;
0304
0305 if (a->key_hash != b->key_hash ||
0306 a->volume != b->volume ||
0307 a->key_len != b->key_len)
0308 return false;
0309
0310 if (a->key_len <= sizeof(a->inline_key)) {
0311 ka = &a->inline_key;
0312 kb = &b->inline_key;
0313 } else {
0314 ka = a->key;
0315 kb = b->key;
0316 }
0317 return memcmp(ka, kb, a->key_len) == 0;
0318 }
0319
0320 static atomic_t fscache_cookie_debug_id = ATOMIC_INIT(1);
0321
0322
0323
0324
0325 static struct fscache_cookie *fscache_alloc_cookie(
0326 struct fscache_volume *volume,
0327 u8 advice,
0328 const void *index_key, size_t index_key_len,
0329 const void *aux_data, size_t aux_data_len,
0330 loff_t object_size)
0331 {
0332 struct fscache_cookie *cookie;
0333
0334
0335 cookie = kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL);
0336 if (!cookie)
0337 return NULL;
0338 fscache_stat(&fscache_n_cookies);
0339
0340 cookie->volume = volume;
0341 cookie->advice = advice;
0342 cookie->key_len = index_key_len;
0343 cookie->aux_len = aux_data_len;
0344 cookie->object_size = object_size;
0345 if (object_size == 0)
0346 __set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
0347
0348 if (fscache_set_key(cookie, index_key, index_key_len) < 0)
0349 goto nomem;
0350
0351 if (cookie->aux_len <= sizeof(cookie->inline_aux)) {
0352 memcpy(cookie->inline_aux, aux_data, cookie->aux_len);
0353 } else {
0354 cookie->aux = kmemdup(aux_data, cookie->aux_len, GFP_KERNEL);
0355 if (!cookie->aux)
0356 goto nomem;
0357 }
0358
0359 refcount_set(&cookie->ref, 1);
0360 cookie->debug_id = atomic_inc_return(&fscache_cookie_debug_id);
0361 spin_lock_init(&cookie->lock);
0362 INIT_LIST_HEAD(&cookie->commit_link);
0363 INIT_WORK(&cookie->work, fscache_cookie_worker);
0364 __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
0365
0366 write_lock(&fscache_cookies_lock);
0367 list_add_tail(&cookie->proc_link, &fscache_cookies);
0368 write_unlock(&fscache_cookies_lock);
0369 fscache_see_cookie(cookie, fscache_cookie_new_acquire);
0370 return cookie;
0371
0372 nomem:
0373 fscache_free_cookie(cookie);
0374 return NULL;
0375 }
0376
0377 static inline bool fscache_cookie_is_dropped(struct fscache_cookie *cookie)
0378 {
0379 return READ_ONCE(cookie->state) == FSCACHE_COOKIE_STATE_DROPPED;
0380 }
0381
0382 static void fscache_wait_on_collision(struct fscache_cookie *candidate,
0383 struct fscache_cookie *wait_for)
0384 {
0385 enum fscache_cookie_state *statep = &wait_for->state;
0386
0387 wait_var_event_timeout(statep, fscache_cookie_is_dropped(wait_for),
0388 20 * HZ);
0389 if (!fscache_cookie_is_dropped(wait_for)) {
0390 pr_notice("Potential collision c=%08x old: c=%08x",
0391 candidate->debug_id, wait_for->debug_id);
0392 wait_var_event(statep, fscache_cookie_is_dropped(wait_for));
0393 }
0394 }
0395
0396
0397
0398
0399
0400
0401 static bool fscache_hash_cookie(struct fscache_cookie *candidate)
0402 {
0403 struct fscache_cookie *cursor, *wait_for = NULL;
0404 struct hlist_bl_head *h;
0405 struct hlist_bl_node *p;
0406 unsigned int bucket;
0407
0408 bucket = candidate->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
0409 h = &fscache_cookie_hash[bucket];
0410
0411 hlist_bl_lock(h);
0412 hlist_bl_for_each_entry(cursor, p, h, hash_link) {
0413 if (fscache_cookie_same(candidate, cursor)) {
0414 if (!test_bit(FSCACHE_COOKIE_RELINQUISHED, &cursor->flags))
0415 goto collision;
0416 wait_for = fscache_get_cookie(cursor,
0417 fscache_cookie_get_hash_collision);
0418 break;
0419 }
0420 }
0421
0422 fscache_get_volume(candidate->volume, fscache_volume_get_cookie);
0423 atomic_inc(&candidate->volume->n_cookies);
0424 hlist_bl_add_head(&candidate->hash_link, h);
0425 set_bit(FSCACHE_COOKIE_IS_HASHED, &candidate->flags);
0426 hlist_bl_unlock(h);
0427
0428 if (wait_for) {
0429 fscache_wait_on_collision(candidate, wait_for);
0430 fscache_put_cookie(wait_for, fscache_cookie_put_hash_collision);
0431 }
0432 return true;
0433
0434 collision:
0435 trace_fscache_cookie(cursor->debug_id, refcount_read(&cursor->ref),
0436 fscache_cookie_collision);
0437 pr_err("Duplicate cookie detected\n");
0438 fscache_print_cookie(cursor, 'O');
0439 fscache_print_cookie(candidate, 'N');
0440 hlist_bl_unlock(h);
0441 return false;
0442 }
0443
0444
0445
0446
0447
0448
0449
0450 struct fscache_cookie *__fscache_acquire_cookie(
0451 struct fscache_volume *volume,
0452 u8 advice,
0453 const void *index_key, size_t index_key_len,
0454 const void *aux_data, size_t aux_data_len,
0455 loff_t object_size)
0456 {
0457 struct fscache_cookie *cookie;
0458
0459 _enter("V=%x", volume->debug_id);
0460
0461 if (!index_key || !index_key_len || index_key_len > 255 || aux_data_len > 255)
0462 return NULL;
0463 if (!aux_data || !aux_data_len) {
0464 aux_data = NULL;
0465 aux_data_len = 0;
0466 }
0467
0468 fscache_stat(&fscache_n_acquires);
0469
0470 cookie = fscache_alloc_cookie(volume, advice,
0471 index_key, index_key_len,
0472 aux_data, aux_data_len,
0473 object_size);
0474 if (!cookie) {
0475 fscache_stat(&fscache_n_acquires_oom);
0476 return NULL;
0477 }
0478
0479 if (!fscache_hash_cookie(cookie)) {
0480 fscache_see_cookie(cookie, fscache_cookie_discard);
0481 fscache_free_cookie(cookie);
0482 return NULL;
0483 }
0484
0485 trace_fscache_acquire(cookie);
0486 fscache_stat(&fscache_n_acquires_ok);
0487 _leave(" = c=%08x", cookie->debug_id);
0488 return cookie;
0489 }
0490 EXPORT_SYMBOL(__fscache_acquire_cookie);
0491
0492
0493
0494
0495 static void fscache_prepare_to_write(struct fscache_cookie *cookie)
0496 {
0497 cookie->volume->cache->ops->prepare_to_write(cookie);
0498 }
0499
0500
0501
0502
0503 static void fscache_perform_lookup(struct fscache_cookie *cookie)
0504 {
0505 enum fscache_access_trace trace = fscache_access_lookup_cookie_end_failed;
0506 bool need_withdraw = false;
0507
0508 _enter("");
0509
0510 if (!cookie->volume->cache_priv) {
0511 fscache_create_volume(cookie->volume, true);
0512 if (!cookie->volume->cache_priv) {
0513 fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
0514 goto out;
0515 }
0516 }
0517
0518 if (!cookie->volume->cache->ops->lookup_cookie(cookie)) {
0519 if (cookie->state != FSCACHE_COOKIE_STATE_FAILED)
0520 fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
0521 need_withdraw = true;
0522 _leave(" [fail]");
0523 goto out;
0524 }
0525
0526 fscache_see_cookie(cookie, fscache_cookie_see_active);
0527 spin_lock(&cookie->lock);
0528 if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
0529 __fscache_set_cookie_state(cookie,
0530 FSCACHE_COOKIE_STATE_INVALIDATING);
0531 else
0532 __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_ACTIVE);
0533 spin_unlock(&cookie->lock);
0534 wake_up_cookie_state(cookie);
0535 trace = fscache_access_lookup_cookie_end;
0536
0537 out:
0538 fscache_end_cookie_access(cookie, trace);
0539 if (need_withdraw)
0540 fscache_withdraw_cookie(cookie);
0541 fscache_end_volume_access(cookie->volume, cookie, trace);
0542 }
0543
0544
0545
0546
0547
0548 static bool fscache_begin_lookup(struct fscache_cookie *cookie, bool will_modify)
0549 {
0550 if (will_modify) {
0551 set_bit(FSCACHE_COOKIE_LOCAL_WRITE, &cookie->flags);
0552 set_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags);
0553 }
0554 if (!fscache_begin_volume_access(cookie->volume, cookie,
0555 fscache_access_lookup_cookie))
0556 return false;
0557
0558 __fscache_begin_cookie_access(cookie, fscache_access_lookup_cookie);
0559 __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_LOOKING_UP);
0560 set_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags);
0561 set_bit(FSCACHE_COOKIE_HAS_BEEN_CACHED, &cookie->flags);
0562 return true;
0563 }
0564
0565
0566
0567
0568
0569 void __fscache_use_cookie(struct fscache_cookie *cookie, bool will_modify)
0570 {
0571 enum fscache_cookie_state state;
0572 bool queue = false;
0573 int n_active;
0574
0575 _enter("c=%08x", cookie->debug_id);
0576
0577 if (WARN(test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags),
0578 "Trying to use relinquished cookie\n"))
0579 return;
0580
0581 spin_lock(&cookie->lock);
0582
0583 n_active = atomic_inc_return(&cookie->n_active);
0584 trace_fscache_active(cookie->debug_id, refcount_read(&cookie->ref),
0585 n_active, atomic_read(&cookie->n_accesses),
0586 will_modify ?
0587 fscache_active_use_modify : fscache_active_use);
0588
0589 again:
0590 state = fscache_cookie_state(cookie);
0591 switch (state) {
0592 case FSCACHE_COOKIE_STATE_QUIESCENT:
0593 queue = fscache_begin_lookup(cookie, will_modify);
0594 break;
0595
0596 case FSCACHE_COOKIE_STATE_LOOKING_UP:
0597 case FSCACHE_COOKIE_STATE_CREATING:
0598 if (will_modify)
0599 set_bit(FSCACHE_COOKIE_LOCAL_WRITE, &cookie->flags);
0600 break;
0601 case FSCACHE_COOKIE_STATE_ACTIVE:
0602 case FSCACHE_COOKIE_STATE_INVALIDATING:
0603 if (will_modify &&
0604 !test_and_set_bit(FSCACHE_COOKIE_LOCAL_WRITE, &cookie->flags)) {
0605 set_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags);
0606 queue = true;
0607 }
0608 break;
0609
0610 case FSCACHE_COOKIE_STATE_FAILED:
0611 case FSCACHE_COOKIE_STATE_WITHDRAWING:
0612 break;
0613
0614 case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
0615 spin_unlock(&cookie->lock);
0616 wait_var_event(&cookie->state,
0617 fscache_cookie_state(cookie) !=
0618 FSCACHE_COOKIE_STATE_LRU_DISCARDING);
0619 spin_lock(&cookie->lock);
0620 goto again;
0621
0622 case FSCACHE_COOKIE_STATE_DROPPED:
0623 case FSCACHE_COOKIE_STATE_RELINQUISHING:
0624 WARN(1, "Can't use cookie in state %u\n", state);
0625 break;
0626 }
0627
0628 spin_unlock(&cookie->lock);
0629 if (queue)
0630 fscache_queue_cookie(cookie, fscache_cookie_get_use_work);
0631 _leave("");
0632 }
0633 EXPORT_SYMBOL(__fscache_use_cookie);
0634
0635 static void fscache_unuse_cookie_locked(struct fscache_cookie *cookie)
0636 {
0637 clear_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags);
0638 if (!test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags))
0639 return;
0640
0641 cookie->unused_at = jiffies;
0642 spin_lock(&fscache_cookie_lru_lock);
0643 if (list_empty(&cookie->commit_link)) {
0644 fscache_get_cookie(cookie, fscache_cookie_get_lru);
0645 fscache_stat(&fscache_n_cookies_lru);
0646 }
0647 list_move_tail(&cookie->commit_link, &fscache_cookie_lru);
0648
0649 spin_unlock(&fscache_cookie_lru_lock);
0650 timer_reduce(&fscache_cookie_lru_timer,
0651 jiffies + fscache_lru_cookie_timeout);
0652 }
0653
0654
0655
0656
0657 void __fscache_unuse_cookie(struct fscache_cookie *cookie,
0658 const void *aux_data, const loff_t *object_size)
0659 {
0660 unsigned int debug_id = cookie->debug_id;
0661 unsigned int r = refcount_read(&cookie->ref);
0662 unsigned int a = atomic_read(&cookie->n_accesses);
0663 unsigned int c;
0664
0665 if (aux_data || object_size)
0666 __fscache_update_cookie(cookie, aux_data, object_size);
0667
0668
0669 c = atomic_fetch_add_unless(&cookie->n_active, -1, 1);
0670 if (c != 1) {
0671 trace_fscache_active(debug_id, r, c - 1, a, fscache_active_unuse);
0672 return;
0673 }
0674
0675 spin_lock(&cookie->lock);
0676 r = refcount_read(&cookie->ref);
0677 a = atomic_read(&cookie->n_accesses);
0678 c = atomic_dec_return(&cookie->n_active);
0679 trace_fscache_active(debug_id, r, c, a, fscache_active_unuse);
0680 if (c == 0)
0681 fscache_unuse_cookie_locked(cookie);
0682 spin_unlock(&cookie->lock);
0683 }
0684 EXPORT_SYMBOL(__fscache_unuse_cookie);
0685
0686
0687
0688
0689
0690
0691
0692 static void fscache_cookie_state_machine(struct fscache_cookie *cookie)
0693 {
0694 enum fscache_cookie_state state;
0695 bool wake = false;
0696
0697 _enter("c=%x", cookie->debug_id);
0698
0699 again:
0700 spin_lock(&cookie->lock);
0701 again_locked:
0702 state = cookie->state;
0703 switch (state) {
0704 case FSCACHE_COOKIE_STATE_QUIESCENT:
0705
0706
0707
0708
0709 if (atomic_read(&cookie->n_accesses) == 0 &&
0710 test_bit(FSCACHE_COOKIE_DO_RELINQUISH, &cookie->flags)) {
0711 __fscache_set_cookie_state(cookie,
0712 FSCACHE_COOKIE_STATE_RELINQUISHING);
0713 wake = true;
0714 goto again_locked;
0715 }
0716 break;
0717
0718 case FSCACHE_COOKIE_STATE_LOOKING_UP:
0719 spin_unlock(&cookie->lock);
0720 fscache_init_access_gate(cookie);
0721 fscache_perform_lookup(cookie);
0722 goto again;
0723
0724 case FSCACHE_COOKIE_STATE_INVALIDATING:
0725 spin_unlock(&cookie->lock);
0726 fscache_perform_invalidation(cookie);
0727 goto again;
0728
0729 case FSCACHE_COOKIE_STATE_ACTIVE:
0730 if (test_and_clear_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags)) {
0731 spin_unlock(&cookie->lock);
0732 fscache_prepare_to_write(cookie);
0733 spin_lock(&cookie->lock);
0734 }
0735 if (test_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags)) {
0736 __fscache_set_cookie_state(cookie,
0737 FSCACHE_COOKIE_STATE_LRU_DISCARDING);
0738 wake = true;
0739 goto again_locked;
0740 }
0741 fallthrough;
0742
0743 case FSCACHE_COOKIE_STATE_FAILED:
0744 if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
0745 fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
0746
0747 if (atomic_read(&cookie->n_accesses) != 0)
0748 break;
0749 if (test_bit(FSCACHE_COOKIE_DO_RELINQUISH, &cookie->flags)) {
0750 __fscache_set_cookie_state(cookie,
0751 FSCACHE_COOKIE_STATE_RELINQUISHING);
0752 wake = true;
0753 goto again_locked;
0754 }
0755 if (test_bit(FSCACHE_COOKIE_DO_WITHDRAW, &cookie->flags)) {
0756 __fscache_set_cookie_state(cookie,
0757 FSCACHE_COOKIE_STATE_WITHDRAWING);
0758 wake = true;
0759 goto again_locked;
0760 }
0761 break;
0762
0763 case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
0764 case FSCACHE_COOKIE_STATE_RELINQUISHING:
0765 case FSCACHE_COOKIE_STATE_WITHDRAWING:
0766 if (cookie->cache_priv) {
0767 spin_unlock(&cookie->lock);
0768 cookie->volume->cache->ops->withdraw_cookie(cookie);
0769 spin_lock(&cookie->lock);
0770 }
0771
0772 if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
0773 fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
0774
0775 switch (state) {
0776 case FSCACHE_COOKIE_STATE_RELINQUISHING:
0777 fscache_see_cookie(cookie, fscache_cookie_see_relinquish);
0778 fscache_unhash_cookie(cookie);
0779 __fscache_set_cookie_state(cookie,
0780 FSCACHE_COOKIE_STATE_DROPPED);
0781 wake = true;
0782 goto out;
0783 case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
0784 fscache_see_cookie(cookie, fscache_cookie_see_lru_discard);
0785 break;
0786 case FSCACHE_COOKIE_STATE_WITHDRAWING:
0787 fscache_see_cookie(cookie, fscache_cookie_see_withdraw);
0788 break;
0789 default:
0790 BUG();
0791 }
0792
0793 clear_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &cookie->flags);
0794 clear_bit(FSCACHE_COOKIE_DO_WITHDRAW, &cookie->flags);
0795 clear_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags);
0796 clear_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags);
0797 set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
0798 __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
0799 wake = true;
0800 goto again_locked;
0801
0802 case FSCACHE_COOKIE_STATE_DROPPED:
0803 break;
0804
0805 default:
0806 WARN_ONCE(1, "Cookie %x in unexpected state %u\n",
0807 cookie->debug_id, state);
0808 break;
0809 }
0810
0811 out:
0812 spin_unlock(&cookie->lock);
0813 if (wake)
0814 wake_up_cookie_state(cookie);
0815 _leave("");
0816 }
0817
0818 static void fscache_cookie_worker(struct work_struct *work)
0819 {
0820 struct fscache_cookie *cookie = container_of(work, struct fscache_cookie, work);
0821
0822 fscache_see_cookie(cookie, fscache_cookie_see_work);
0823 fscache_cookie_state_machine(cookie);
0824 fscache_put_cookie(cookie, fscache_cookie_put_work);
0825 }
0826
0827
0828
0829
0830
0831
0832 static void __fscache_withdraw_cookie(struct fscache_cookie *cookie)
0833 {
0834 int n_accesses;
0835 bool unpinned;
0836
0837 unpinned = test_and_clear_bit(FSCACHE_COOKIE_NO_ACCESS_WAKE, &cookie->flags);
0838
0839
0840 n_accesses = atomic_read(&cookie->n_accesses);
0841 if (unpinned)
0842 trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
0843 n_accesses, fscache_access_cache_unpin);
0844 if (n_accesses == 0)
0845 fscache_queue_cookie(cookie, fscache_cookie_get_end_access);
0846 }
0847
0848 static void fscache_cookie_lru_do_one(struct fscache_cookie *cookie)
0849 {
0850 fscache_see_cookie(cookie, fscache_cookie_see_lru_do_one);
0851
0852 spin_lock(&cookie->lock);
0853 if (cookie->state != FSCACHE_COOKIE_STATE_ACTIVE ||
0854 time_before(jiffies, cookie->unused_at + fscache_lru_cookie_timeout) ||
0855 atomic_read(&cookie->n_active) > 0) {
0856 spin_unlock(&cookie->lock);
0857 fscache_stat(&fscache_n_cookies_lru_removed);
0858 } else {
0859 set_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags);
0860 spin_unlock(&cookie->lock);
0861 fscache_stat(&fscache_n_cookies_lru_expired);
0862 _debug("lru c=%x", cookie->debug_id);
0863 __fscache_withdraw_cookie(cookie);
0864 }
0865
0866 fscache_put_cookie(cookie, fscache_cookie_put_lru);
0867 }
0868
0869 static void fscache_cookie_lru_worker(struct work_struct *work)
0870 {
0871 struct fscache_cookie *cookie;
0872 unsigned long unused_at;
0873
0874 spin_lock(&fscache_cookie_lru_lock);
0875
0876 while (!list_empty(&fscache_cookie_lru)) {
0877 cookie = list_first_entry(&fscache_cookie_lru,
0878 struct fscache_cookie, commit_link);
0879 unused_at = cookie->unused_at + fscache_lru_cookie_timeout;
0880 if (time_before(jiffies, unused_at)) {
0881 timer_reduce(&fscache_cookie_lru_timer, unused_at);
0882 break;
0883 }
0884
0885 list_del_init(&cookie->commit_link);
0886 fscache_stat_d(&fscache_n_cookies_lru);
0887 spin_unlock(&fscache_cookie_lru_lock);
0888 fscache_cookie_lru_do_one(cookie);
0889 spin_lock(&fscache_cookie_lru_lock);
0890 }
0891
0892 spin_unlock(&fscache_cookie_lru_lock);
0893 }
0894
0895 static void fscache_cookie_lru_timed_out(struct timer_list *timer)
0896 {
0897 queue_work(fscache_wq, &fscache_cookie_lru_work);
0898 }
0899
0900 static void fscache_cookie_drop_from_lru(struct fscache_cookie *cookie)
0901 {
0902 bool need_put = false;
0903
0904 if (!list_empty(&cookie->commit_link)) {
0905 spin_lock(&fscache_cookie_lru_lock);
0906 if (!list_empty(&cookie->commit_link)) {
0907 list_del_init(&cookie->commit_link);
0908 fscache_stat_d(&fscache_n_cookies_lru);
0909 fscache_stat(&fscache_n_cookies_lru_dropped);
0910 need_put = true;
0911 }
0912 spin_unlock(&fscache_cookie_lru_lock);
0913 if (need_put)
0914 fscache_put_cookie(cookie, fscache_cookie_put_lru);
0915 }
0916 }
0917
0918
0919
0920
0921 static void fscache_unhash_cookie(struct fscache_cookie *cookie)
0922 {
0923 struct hlist_bl_head *h;
0924 unsigned int bucket;
0925
0926 bucket = cookie->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
0927 h = &fscache_cookie_hash[bucket];
0928
0929 hlist_bl_lock(h);
0930 hlist_bl_del(&cookie->hash_link);
0931 clear_bit(FSCACHE_COOKIE_IS_HASHED, &cookie->flags);
0932 hlist_bl_unlock(h);
0933 fscache_stat(&fscache_n_relinquishes_dropped);
0934 }
0935
0936 static void fscache_drop_withdraw_cookie(struct fscache_cookie *cookie)
0937 {
0938 fscache_cookie_drop_from_lru(cookie);
0939 __fscache_withdraw_cookie(cookie);
0940 }
0941
0942
0943
0944
0945
0946
0947
0948
0949 void fscache_withdraw_cookie(struct fscache_cookie *cookie)
0950 {
0951 set_bit(FSCACHE_COOKIE_DO_WITHDRAW, &cookie->flags);
0952 fscache_drop_withdraw_cookie(cookie);
0953 }
0954 EXPORT_SYMBOL(fscache_withdraw_cookie);
0955
0956
0957
0958
0959
0960 void __fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
0961 {
0962 fscache_stat(&fscache_n_relinquishes);
0963 if (retire)
0964 fscache_stat(&fscache_n_relinquishes_retire);
0965
0966 _enter("c=%08x{%d},%d",
0967 cookie->debug_id, atomic_read(&cookie->n_active), retire);
0968
0969 if (WARN(test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags),
0970 "Cookie c=%x already relinquished\n", cookie->debug_id))
0971 return;
0972
0973 if (retire)
0974 set_bit(FSCACHE_COOKIE_RETIRED, &cookie->flags);
0975 trace_fscache_relinquish(cookie, retire);
0976
0977 ASSERTCMP(atomic_read(&cookie->n_active), ==, 0);
0978 ASSERTCMP(atomic_read(&cookie->volume->n_cookies), >, 0);
0979 atomic_dec(&cookie->volume->n_cookies);
0980
0981 if (test_bit(FSCACHE_COOKIE_HAS_BEEN_CACHED, &cookie->flags)) {
0982 set_bit(FSCACHE_COOKIE_DO_RELINQUISH, &cookie->flags);
0983 fscache_drop_withdraw_cookie(cookie);
0984 } else {
0985 fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_DROPPED);
0986 fscache_unhash_cookie(cookie);
0987 }
0988 fscache_put_cookie(cookie, fscache_cookie_put_relinquish);
0989 }
0990 EXPORT_SYMBOL(__fscache_relinquish_cookie);
0991
0992
0993
0994
0995 void fscache_put_cookie(struct fscache_cookie *cookie,
0996 enum fscache_cookie_trace where)
0997 {
0998 struct fscache_volume *volume = cookie->volume;
0999 unsigned int cookie_debug_id = cookie->debug_id;
1000 bool zero;
1001 int ref;
1002
1003 zero = __refcount_dec_and_test(&cookie->ref, &ref);
1004 trace_fscache_cookie(cookie_debug_id, ref - 1, where);
1005 if (zero) {
1006 fscache_free_cookie(cookie);
1007 fscache_put_volume(volume, fscache_volume_put_cookie);
1008 }
1009 }
1010 EXPORT_SYMBOL(fscache_put_cookie);
1011
1012
1013
1014
1015 struct fscache_cookie *fscache_get_cookie(struct fscache_cookie *cookie,
1016 enum fscache_cookie_trace where)
1017 {
1018 int ref;
1019
1020 __refcount_inc(&cookie->ref, &ref);
1021 trace_fscache_cookie(cookie->debug_id, ref + 1, where);
1022 return cookie;
1023 }
1024 EXPORT_SYMBOL(fscache_get_cookie);
1025
1026
1027
1028
1029 static void fscache_perform_invalidation(struct fscache_cookie *cookie)
1030 {
1031 if (!cookie->volume->cache->ops->invalidate_cookie(cookie))
1032 fscache_caching_failed(cookie);
1033 fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
1034 }
1035
1036
1037
1038
1039 void __fscache_invalidate(struct fscache_cookie *cookie,
1040 const void *aux_data, loff_t new_size,
1041 unsigned int flags)
1042 {
1043 bool is_caching;
1044
1045 _enter("c=%x", cookie->debug_id);
1046
1047 fscache_stat(&fscache_n_invalidates);
1048
1049 if (WARN(test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags),
1050 "Trying to invalidate relinquished cookie\n"))
1051 return;
1052
1053 if ((flags & FSCACHE_INVAL_DIO_WRITE) &&
1054 test_and_set_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags))
1055 return;
1056
1057 spin_lock(&cookie->lock);
1058 set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
1059 fscache_update_aux(cookie, aux_data, &new_size);
1060 cookie->inval_counter++;
1061 trace_fscache_invalidate(cookie, new_size);
1062
1063 switch (cookie->state) {
1064 case FSCACHE_COOKIE_STATE_INVALIDATING:
1065 default:
1066 spin_unlock(&cookie->lock);
1067 _leave(" [no %u]", cookie->state);
1068 return;
1069
1070 case FSCACHE_COOKIE_STATE_LOOKING_UP:
1071 if (!test_and_set_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
1072 __fscache_begin_cookie_access(cookie, fscache_access_invalidate_cookie);
1073 fallthrough;
1074 case FSCACHE_COOKIE_STATE_CREATING:
1075 spin_unlock(&cookie->lock);
1076 _leave(" [look %x]", cookie->inval_counter);
1077 return;
1078
1079 case FSCACHE_COOKIE_STATE_ACTIVE:
1080 is_caching = fscache_begin_cookie_access(
1081 cookie, fscache_access_invalidate_cookie);
1082 if (is_caching)
1083 __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_INVALIDATING);
1084 spin_unlock(&cookie->lock);
1085 wake_up_cookie_state(cookie);
1086
1087 if (is_caching)
1088 fscache_queue_cookie(cookie, fscache_cookie_get_inval_work);
1089 _leave(" [inv]");
1090 return;
1091 }
1092 }
1093 EXPORT_SYMBOL(__fscache_invalidate);
1094
1095 #ifdef CONFIG_PROC_FS
1096
1097
1098
1099 static int fscache_cookies_seq_show(struct seq_file *m, void *v)
1100 {
1101 struct fscache_cookie *cookie;
1102 unsigned int keylen = 0, auxlen = 0;
1103 u8 *p;
1104
1105 if (v == &fscache_cookies) {
1106 seq_puts(m,
1107 "COOKIE VOLUME REF ACT ACC S FL DEF \n"
1108 "======== ======== === === === = == ================\n"
1109 );
1110 return 0;
1111 }
1112
1113 cookie = list_entry(v, struct fscache_cookie, proc_link);
1114
1115 seq_printf(m,
1116 "%08x %08x %3d %3d %3d %c %02lx",
1117 cookie->debug_id,
1118 cookie->volume->debug_id,
1119 refcount_read(&cookie->ref),
1120 atomic_read(&cookie->n_active),
1121 atomic_read(&cookie->n_accesses),
1122 fscache_cookie_states[cookie->state],
1123 cookie->flags);
1124
1125 keylen = cookie->key_len;
1126 auxlen = cookie->aux_len;
1127
1128 if (keylen > 0 || auxlen > 0) {
1129 seq_puts(m, " ");
1130 p = keylen <= sizeof(cookie->inline_key) ?
1131 cookie->inline_key : cookie->key;
1132 for (; keylen > 0; keylen--)
1133 seq_printf(m, "%02x", *p++);
1134 if (auxlen > 0) {
1135 seq_puts(m, ", ");
1136 p = auxlen <= sizeof(cookie->inline_aux) ?
1137 cookie->inline_aux : cookie->aux;
1138 for (; auxlen > 0; auxlen--)
1139 seq_printf(m, "%02x", *p++);
1140 }
1141 }
1142
1143 seq_puts(m, "\n");
1144 return 0;
1145 }
1146
1147 static void *fscache_cookies_seq_start(struct seq_file *m, loff_t *_pos)
1148 __acquires(fscache_cookies_lock)
1149 {
1150 read_lock(&fscache_cookies_lock);
1151 return seq_list_start_head(&fscache_cookies, *_pos);
1152 }
1153
1154 static void *fscache_cookies_seq_next(struct seq_file *m, void *v, loff_t *_pos)
1155 {
1156 return seq_list_next(v, &fscache_cookies, _pos);
1157 }
1158
1159 static void fscache_cookies_seq_stop(struct seq_file *m, void *v)
1160 __releases(rcu)
1161 {
1162 read_unlock(&fscache_cookies_lock);
1163 }
1164
1165
1166 const struct seq_operations fscache_cookies_seq_ops = {
1167 .start = fscache_cookies_seq_start,
1168 .next = fscache_cookies_seq_next,
1169 .stop = fscache_cookies_seq_stop,
1170 .show = fscache_cookies_seq_show,
1171 };
1172 #endif