David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 1 | /* netfs cookie management |
| 2 | * |
| 3 | * Copyright (C) 2004-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. |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 10 | * |
| 11 | * See Documentation/filesystems/caching/netfs-api.txt for more information on |
| 12 | * the netfs API. |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #define FSCACHE_DEBUG_LEVEL COOKIE |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include "internal.h" |
| 19 | |
| 20 | struct kmem_cache *fscache_cookie_jar; |
| 21 | |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 22 | static atomic_t fscache_object_debug_id = ATOMIC_INIT(0); |
| 23 | |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 24 | #define fscache_cookie_hash_shift 15 |
| 25 | static struct hlist_bl_head fscache_cookie_hash[1 << fscache_cookie_hash_shift]; |
| 26 | |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 27 | static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie, |
| 28 | loff_t object_size); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 29 | static int fscache_alloc_object(struct fscache_cache *cache, |
| 30 | struct fscache_cookie *cookie); |
| 31 | static int fscache_attach_object(struct fscache_cookie *cookie, |
| 32 | struct fscache_object *object); |
| 33 | |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 34 | static void fscache_print_cookie(struct fscache_cookie *cookie, char prefix) |
| 35 | { |
| 36 | struct hlist_node *object; |
| 37 | const u8 *k; |
| 38 | unsigned loop; |
| 39 | |
| 40 | pr_err("%c-cookie c=%p [p=%p fl=%lx nc=%u na=%u]\n", |
| 41 | prefix, cookie, cookie->parent, cookie->flags, |
| 42 | atomic_read(&cookie->n_children), |
| 43 | atomic_read(&cookie->n_active)); |
| 44 | pr_err("%c-cookie d=%p n=%p\n", |
| 45 | prefix, cookie->def, cookie->netfs_data); |
| 46 | |
| 47 | object = READ_ONCE(cookie->backing_objects.first); |
| 48 | if (object) |
| 49 | pr_err("%c-cookie o=%p\n", |
| 50 | prefix, hlist_entry(object, struct fscache_object, cookie_link)); |
| 51 | |
| 52 | pr_err("%c-key=[%u] '", prefix, cookie->key_len); |
| 53 | k = (cookie->key_len <= sizeof(cookie->inline_key)) ? |
| 54 | cookie->inline_key : cookie->key; |
| 55 | for (loop = 0; loop < cookie->key_len; loop++) |
| 56 | pr_cont("%02x", k[loop]); |
| 57 | pr_cont("'\n"); |
| 58 | } |
| 59 | |
| 60 | void fscache_free_cookie(struct fscache_cookie *cookie) |
| 61 | { |
| 62 | if (cookie) { |
| 63 | BUG_ON(!hlist_empty(&cookie->backing_objects)); |
| 64 | if (cookie->aux_len > sizeof(cookie->inline_aux)) |
| 65 | kfree(cookie->aux); |
| 66 | if (cookie->key_len > sizeof(cookie->inline_key)) |
| 67 | kfree(cookie->key); |
| 68 | kmem_cache_free(fscache_cookie_jar, cookie); |
| 69 | } |
| 70 | } |
| 71 | |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 72 | /* |
| 73 | * initialise an cookie jar slab element prior to any use |
| 74 | */ |
| 75 | void fscache_cookie_init_once(void *_cookie) |
| 76 | { |
| 77 | struct fscache_cookie *cookie = _cookie; |
| 78 | |
| 79 | memset(cookie, 0, sizeof(*cookie)); |
| 80 | spin_lock_init(&cookie->lock); |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 81 | spin_lock_init(&cookie->stores_lock); |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 82 | INIT_HLIST_HEAD(&cookie->backing_objects); |
| 83 | } |
| 84 | |
| 85 | /* |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 86 | * Set the index key in a cookie. The cookie struct has space for a 12-byte |
| 87 | * key plus length and hash, but if that's not big enough, it's instead a |
| 88 | * pointer to a buffer containing 3 bytes of hash, 1 byte of length and then |
| 89 | * the key data. |
| 90 | */ |
| 91 | static int fscache_set_key(struct fscache_cookie *cookie, |
| 92 | const void *index_key, size_t index_key_len) |
| 93 | { |
| 94 | unsigned long long h; |
| 95 | u32 *buf; |
| 96 | int i; |
| 97 | |
| 98 | cookie->key_len = index_key_len; |
| 99 | |
| 100 | if (index_key_len > sizeof(cookie->inline_key)) { |
| 101 | buf = kzalloc(index_key_len, GFP_KERNEL); |
| 102 | if (!buf) |
| 103 | return -ENOMEM; |
| 104 | cookie->key = buf; |
| 105 | } else { |
| 106 | buf = (u32 *)cookie->inline_key; |
| 107 | buf[0] = 0; |
| 108 | buf[1] = 0; |
| 109 | buf[2] = 0; |
| 110 | } |
| 111 | |
| 112 | memcpy(buf, index_key, index_key_len); |
| 113 | |
| 114 | /* Calculate a hash and combine this with the length in the first word |
| 115 | * or first half word |
| 116 | */ |
| 117 | h = (unsigned long)cookie->parent; |
| 118 | h += index_key_len + cookie->type; |
| 119 | for (i = 0; i < (index_key_len + sizeof(u32) - 1) / sizeof(u32); i++) |
| 120 | h += buf[i]; |
| 121 | |
| 122 | cookie->key_hash = h ^ (h >> 32); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static long fscache_compare_cookie(const struct fscache_cookie *a, |
| 127 | const struct fscache_cookie *b) |
| 128 | { |
| 129 | const void *ka, *kb; |
| 130 | |
| 131 | if (a->key_hash != b->key_hash) |
| 132 | return (long)a->key_hash - (long)b->key_hash; |
| 133 | if (a->parent != b->parent) |
| 134 | return (long)a->parent - (long)b->parent; |
| 135 | if (a->key_len != b->key_len) |
| 136 | return (long)a->key_len - (long)b->key_len; |
| 137 | if (a->type != b->type) |
| 138 | return (long)a->type - (long)b->type; |
| 139 | |
| 140 | if (a->key_len <= sizeof(a->inline_key)) { |
| 141 | ka = &a->inline_key; |
| 142 | kb = &b->inline_key; |
| 143 | } else { |
| 144 | ka = a->key; |
| 145 | kb = b->key; |
| 146 | } |
| 147 | return memcmp(ka, kb, a->key_len); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Allocate a cookie. |
| 152 | */ |
| 153 | struct fscache_cookie *fscache_alloc_cookie( |
| 154 | struct fscache_cookie *parent, |
| 155 | const struct fscache_cookie_def *def, |
| 156 | const void *index_key, size_t index_key_len, |
| 157 | const void *aux_data, size_t aux_data_len, |
| 158 | void *netfs_data, |
| 159 | loff_t object_size) |
| 160 | { |
| 161 | struct fscache_cookie *cookie; |
| 162 | |
| 163 | /* allocate and initialise a cookie */ |
| 164 | cookie = kmem_cache_alloc(fscache_cookie_jar, GFP_KERNEL); |
| 165 | if (!cookie) |
| 166 | return NULL; |
| 167 | |
| 168 | cookie->key_len = index_key_len; |
| 169 | cookie->aux_len = aux_data_len; |
| 170 | |
| 171 | if (fscache_set_key(cookie, index_key, index_key_len) < 0) |
| 172 | goto nomem; |
| 173 | |
| 174 | if (cookie->aux_len <= sizeof(cookie->inline_aux)) { |
| 175 | memcpy(cookie->inline_aux, aux_data, cookie->aux_len); |
| 176 | } else { |
| 177 | cookie->aux = kmemdup(aux_data, cookie->aux_len, GFP_KERNEL); |
| 178 | if (!cookie->aux) |
| 179 | goto nomem; |
| 180 | } |
| 181 | |
| 182 | atomic_set(&cookie->usage, 1); |
| 183 | atomic_set(&cookie->n_children, 0); |
| 184 | |
| 185 | /* We keep the active count elevated until relinquishment to prevent an |
| 186 | * attempt to wake up every time the object operations queue quiesces. |
| 187 | */ |
| 188 | atomic_set(&cookie->n_active, 1); |
| 189 | |
| 190 | cookie->def = def; |
| 191 | cookie->parent = parent; |
| 192 | cookie->netfs_data = netfs_data; |
| 193 | cookie->flags = (1 << FSCACHE_COOKIE_NO_DATA_YET); |
| 194 | cookie->type = def->type; |
| 195 | |
| 196 | /* radix tree insertion won't use the preallocation pool unless it's |
| 197 | * told it may not wait */ |
| 198 | INIT_RADIX_TREE(&cookie->stores, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
| 199 | return cookie; |
| 200 | |
| 201 | nomem: |
| 202 | fscache_free_cookie(cookie); |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Attempt to insert the new cookie into the hash. If there's a collision, we |
| 208 | * return the old cookie if it's not in use and an error otherwise. |
| 209 | */ |
| 210 | struct fscache_cookie *fscache_hash_cookie(struct fscache_cookie *candidate) |
| 211 | { |
| 212 | struct fscache_cookie *cursor; |
| 213 | struct hlist_bl_head *h; |
| 214 | struct hlist_bl_node *p; |
| 215 | unsigned int bucket; |
| 216 | |
| 217 | bucket = candidate->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1); |
| 218 | h = &fscache_cookie_hash[bucket]; |
| 219 | |
| 220 | hlist_bl_lock(h); |
| 221 | hlist_bl_for_each_entry(cursor, p, h, hash_link) { |
| 222 | if (fscache_compare_cookie(candidate, cursor) == 0) |
| 223 | goto collision; |
| 224 | } |
| 225 | |
| 226 | __set_bit(FSCACHE_COOKIE_ACQUIRED, &candidate->flags); |
| 227 | fscache_cookie_get(candidate->parent, fscache_cookie_get_acquire_parent); |
| 228 | atomic_inc(&candidate->parent->n_children); |
| 229 | hlist_bl_add_head(&candidate->hash_link, h); |
| 230 | hlist_bl_unlock(h); |
| 231 | return candidate; |
| 232 | |
| 233 | collision: |
| 234 | if (test_and_set_bit(FSCACHE_COOKIE_ACQUIRED, &cursor->flags)) { |
| 235 | trace_fscache_cookie(cursor, fscache_cookie_collision, |
| 236 | atomic_read(&cursor->usage)); |
| 237 | pr_err("Duplicate cookie detected\n"); |
| 238 | fscache_print_cookie(cursor, 'O'); |
| 239 | fscache_print_cookie(candidate, 'N'); |
| 240 | hlist_bl_unlock(h); |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | fscache_cookie_get(cursor, fscache_cookie_get_reacquire); |
| 245 | hlist_bl_unlock(h); |
| 246 | return cursor; |
| 247 | } |
| 248 | |
| 249 | /* |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 250 | * request a cookie to represent an object (index, datafile, xattr, etc) |
| 251 | * - parent specifies the parent object |
| 252 | * - the top level index cookie for each netfs is stored in the fscache_netfs |
| 253 | * struct upon registration |
| 254 | * - def points to the definition |
| 255 | * - the netfs_data will be passed to the functions pointed to in *def |
| 256 | * - all attached caches will be searched to see if they contain this object |
| 257 | * - index objects aren't stored on disk until there's a dependent file that |
| 258 | * needs storing |
| 259 | * - other objects are stored in a selected cache immediately, and all the |
| 260 | * indices forming the path to it are instantiated if necessary |
| 261 | * - we never let on to the netfs about errors |
| 262 | * - we may set a negative cookie pointer, but that's okay |
| 263 | */ |
| 264 | struct fscache_cookie *__fscache_acquire_cookie( |
| 265 | struct fscache_cookie *parent, |
| 266 | const struct fscache_cookie_def *def, |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 267 | const void *index_key, size_t index_key_len, |
| 268 | const void *aux_data, size_t aux_data_len, |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 269 | void *netfs_data, |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 270 | loff_t object_size, |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 271 | bool enable) |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 272 | { |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 273 | struct fscache_cookie *candidate, *cookie; |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 274 | |
| 275 | BUG_ON(!def); |
| 276 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 277 | _enter("{%s},{%s},%p,%u", |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 278 | parent ? (char *) parent->def->name : "<no-parent>", |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 279 | def->name, netfs_data, enable); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 280 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 281 | if (!index_key || !index_key_len || index_key_len > 255 || aux_data_len > 255) |
| 282 | return NULL; |
| 283 | if (!aux_data || !aux_data_len) { |
| 284 | aux_data = NULL; |
| 285 | aux_data_len = 0; |
| 286 | } |
| 287 | |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 288 | fscache_stat(&fscache_n_acquires); |
| 289 | |
| 290 | /* if there's no parent cookie, then we don't create one here either */ |
| 291 | if (!parent) { |
| 292 | fscache_stat(&fscache_n_acquires_null); |
| 293 | _leave(" [no parent]"); |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | /* validate the definition */ |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 298 | BUG_ON(!def->name[0]); |
| 299 | |
| 300 | BUG_ON(def->type == FSCACHE_COOKIE_TYPE_INDEX && |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 301 | parent->type != FSCACHE_COOKIE_TYPE_INDEX); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 302 | |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 303 | candidate = fscache_alloc_cookie(parent, def, |
| 304 | index_key, index_key_len, |
| 305 | aux_data, aux_data_len, |
| 306 | netfs_data, object_size); |
| 307 | if (!candidate) { |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 308 | fscache_stat(&fscache_n_acquires_oom); |
| 309 | _leave(" [ENOMEM]"); |
| 310 | return NULL; |
| 311 | } |
| 312 | |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 313 | cookie = fscache_hash_cookie(candidate); |
| 314 | if (!cookie) { |
| 315 | trace_fscache_cookie(candidate, fscache_cookie_discard, 1); |
| 316 | goto out; |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 317 | } |
| 318 | |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 319 | if (cookie == candidate) |
| 320 | candidate = NULL; |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 321 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 322 | switch (cookie->type) { |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 323 | case FSCACHE_COOKIE_TYPE_INDEX: |
| 324 | fscache_stat(&fscache_n_cookie_index); |
| 325 | break; |
| 326 | case FSCACHE_COOKIE_TYPE_DATAFILE: |
| 327 | fscache_stat(&fscache_n_cookie_data); |
| 328 | break; |
| 329 | default: |
| 330 | fscache_stat(&fscache_n_cookie_special); |
| 331 | break; |
| 332 | } |
| 333 | |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 334 | trace_fscache_acquire(cookie); |
| 335 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 336 | if (enable) { |
| 337 | /* if the object is an index then we need do nothing more here |
| 338 | * - we create indices on disk when we need them as an index |
| 339 | * may exist in multiple caches */ |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 340 | if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) { |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 341 | if (fscache_acquire_non_index_cookie(cookie, object_size) == 0) { |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 342 | set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags); |
| 343 | } else { |
| 344 | atomic_dec(&parent->n_children); |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 345 | fscache_cookie_put(cookie, |
| 346 | fscache_cookie_put_acquire_nobufs); |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 347 | fscache_stat(&fscache_n_acquires_nobufs); |
| 348 | _leave(" = NULL"); |
| 349 | return NULL; |
| 350 | } |
| 351 | } else { |
| 352 | set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | fscache_stat(&fscache_n_acquires_ok); |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 357 | |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 358 | out: |
| 359 | fscache_free_cookie(candidate); |
| 360 | return cookie; |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 361 | } |
| 362 | EXPORT_SYMBOL(__fscache_acquire_cookie); |
| 363 | |
| 364 | /* |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 365 | * Enable a cookie to permit it to accept new operations. |
| 366 | */ |
| 367 | void __fscache_enable_cookie(struct fscache_cookie *cookie, |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 368 | const void *aux_data, |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 369 | loff_t object_size, |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 370 | bool (*can_enable)(void *data), |
| 371 | void *data) |
| 372 | { |
| 373 | _enter("%p", cookie); |
| 374 | |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 375 | trace_fscache_enable(cookie); |
| 376 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 377 | wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK, |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 378 | TASK_UNINTERRUPTIBLE); |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 379 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 380 | fscache_update_aux(cookie, aux_data); |
| 381 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 382 | if (test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags)) |
| 383 | goto out_unlock; |
| 384 | |
| 385 | if (can_enable && !can_enable(data)) { |
| 386 | /* The netfs decided it didn't want to enable after all */ |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 387 | } else if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) { |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 388 | /* Wait for outstanding disablement to complete */ |
| 389 | __fscache_wait_on_invalidate(cookie); |
| 390 | |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 391 | if (fscache_acquire_non_index_cookie(cookie, object_size) == 0) |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 392 | set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags); |
| 393 | } else { |
| 394 | set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags); |
| 395 | } |
| 396 | |
| 397 | out_unlock: |
| 398 | clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags); |
| 399 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK); |
| 400 | } |
| 401 | EXPORT_SYMBOL(__fscache_enable_cookie); |
| 402 | |
| 403 | /* |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 404 | * acquire a non-index cookie |
| 405 | * - this must make sure the index chain is instantiated and instantiate the |
| 406 | * object representation too |
| 407 | */ |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 408 | static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie, |
| 409 | loff_t object_size) |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 410 | { |
| 411 | struct fscache_object *object; |
| 412 | struct fscache_cache *cache; |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 413 | int ret; |
| 414 | |
| 415 | _enter(""); |
| 416 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 417 | set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 418 | |
| 419 | /* now we need to see whether the backing objects for this cookie yet |
| 420 | * exist, if not there'll be nothing to search */ |
| 421 | down_read(&fscache_addremove_sem); |
| 422 | |
| 423 | if (list_empty(&fscache_cache_list)) { |
| 424 | up_read(&fscache_addremove_sem); |
| 425 | _leave(" = 0 [no caches]"); |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | /* select a cache in which to store the object */ |
| 430 | cache = fscache_select_cache_for_object(cookie->parent); |
| 431 | if (!cache) { |
| 432 | up_read(&fscache_addremove_sem); |
| 433 | fscache_stat(&fscache_n_acquires_no_cache); |
| 434 | _leave(" = -ENOMEDIUM [no cache]"); |
| 435 | return -ENOMEDIUM; |
| 436 | } |
| 437 | |
| 438 | _debug("cache %s", cache->tag->name); |
| 439 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 440 | set_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 441 | |
| 442 | /* ask the cache to allocate objects for this cookie and its parent |
| 443 | * chain */ |
| 444 | ret = fscache_alloc_object(cache, cookie); |
| 445 | if (ret < 0) { |
| 446 | up_read(&fscache_addremove_sem); |
| 447 | _leave(" = %d", ret); |
| 448 | return ret; |
| 449 | } |
| 450 | |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 451 | spin_lock(&cookie->lock); |
| 452 | if (hlist_empty(&cookie->backing_objects)) { |
| 453 | spin_unlock(&cookie->lock); |
| 454 | goto unavailable; |
| 455 | } |
| 456 | |
| 457 | object = hlist_entry(cookie->backing_objects.first, |
| 458 | struct fscache_object, cookie_link); |
| 459 | |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 460 | fscache_set_store_limit(object, object_size); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 461 | |
| 462 | /* initiate the process of looking up all the objects in the chain |
| 463 | * (done by fscache_initialise_object()) */ |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 464 | fscache_raise_event(object, FSCACHE_OBJECT_EV_NEW_CHILD); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 465 | |
| 466 | spin_unlock(&cookie->lock); |
| 467 | |
| 468 | /* we may be required to wait for lookup to complete at this point */ |
| 469 | if (!fscache_defer_lookup) { |
| 470 | _debug("non-deferred lookup %p", &cookie->flags); |
| 471 | wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP, |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 472 | TASK_UNINTERRUPTIBLE); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 473 | _debug("complete"); |
| 474 | if (test_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags)) |
| 475 | goto unavailable; |
| 476 | } |
| 477 | |
| 478 | up_read(&fscache_addremove_sem); |
| 479 | _leave(" = 0 [deferred]"); |
| 480 | return 0; |
| 481 | |
| 482 | unavailable: |
| 483 | up_read(&fscache_addremove_sem); |
| 484 | _leave(" = -ENOBUFS"); |
| 485 | return -ENOBUFS; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * recursively allocate cache object records for a cookie/cache combination |
| 490 | * - caller must be holding the addremove sem |
| 491 | */ |
| 492 | static int fscache_alloc_object(struct fscache_cache *cache, |
| 493 | struct fscache_cookie *cookie) |
| 494 | { |
| 495 | struct fscache_object *object; |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 496 | int ret; |
| 497 | |
| 498 | _enter("%p,%p{%s}", cache, cookie, cookie->def->name); |
| 499 | |
| 500 | spin_lock(&cookie->lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 501 | hlist_for_each_entry(object, &cookie->backing_objects, |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 502 | cookie_link) { |
| 503 | if (object->cache == cache) |
| 504 | goto object_already_extant; |
| 505 | } |
| 506 | spin_unlock(&cookie->lock); |
| 507 | |
| 508 | /* ask the cache to allocate an object (we may end up with duplicate |
| 509 | * objects at this stage, but we sort that out later) */ |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 510 | fscache_stat(&fscache_n_cop_alloc_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 511 | object = cache->ops->alloc_object(cache, cookie); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 512 | fscache_stat_d(&fscache_n_cop_alloc_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 513 | if (IS_ERR(object)) { |
| 514 | fscache_stat(&fscache_n_object_no_alloc); |
| 515 | ret = PTR_ERR(object); |
| 516 | goto error; |
| 517 | } |
| 518 | |
| 519 | fscache_stat(&fscache_n_object_alloc); |
| 520 | |
| 521 | object->debug_id = atomic_inc_return(&fscache_object_debug_id); |
| 522 | |
| 523 | _debug("ALLOC OBJ%x: %s {%lx}", |
| 524 | object->debug_id, cookie->def->name, object->events); |
| 525 | |
| 526 | ret = fscache_alloc_object(cache, cookie->parent); |
| 527 | if (ret < 0) |
| 528 | goto error_put; |
| 529 | |
| 530 | /* only attach if we managed to allocate all we needed, otherwise |
| 531 | * discard the object we just allocated and instead use the one |
| 532 | * attached to the cookie */ |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 533 | if (fscache_attach_object(cookie, object) < 0) { |
| 534 | fscache_stat(&fscache_n_cop_put_object); |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 535 | cache->ops->put_object(object, fscache_obj_put_attach_fail); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 536 | fscache_stat_d(&fscache_n_cop_put_object); |
| 537 | } |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 538 | |
| 539 | _leave(" = 0"); |
| 540 | return 0; |
| 541 | |
| 542 | object_already_extant: |
| 543 | ret = -ENOBUFS; |
David Howells | 8702152 | 2015-02-24 10:52:51 +0000 | [diff] [blame] | 544 | if (fscache_object_is_dying(object) || |
| 545 | fscache_cache_is_broken(object)) { |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 546 | spin_unlock(&cookie->lock); |
| 547 | goto error; |
| 548 | } |
| 549 | spin_unlock(&cookie->lock); |
| 550 | _leave(" = 0 [found]"); |
| 551 | return 0; |
| 552 | |
| 553 | error_put: |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 554 | fscache_stat(&fscache_n_cop_put_object); |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 555 | cache->ops->put_object(object, fscache_obj_put_alloc_fail); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 556 | fscache_stat_d(&fscache_n_cop_put_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 557 | error: |
| 558 | _leave(" = %d", ret); |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * attach a cache object to a cookie |
| 564 | */ |
| 565 | static int fscache_attach_object(struct fscache_cookie *cookie, |
| 566 | struct fscache_object *object) |
| 567 | { |
| 568 | struct fscache_object *p; |
| 569 | struct fscache_cache *cache = object->cache; |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 570 | int ret; |
| 571 | |
| 572 | _enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id); |
| 573 | |
| 574 | spin_lock(&cookie->lock); |
| 575 | |
| 576 | /* there may be multiple initial creations of this object, but we only |
| 577 | * want one */ |
| 578 | ret = -EEXIST; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 579 | hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) { |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 580 | if (p->cache == object->cache) { |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 581 | if (fscache_object_is_dying(p)) |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 582 | ret = -ENOBUFS; |
| 583 | goto cant_attach_object; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /* pin the parent object */ |
| 588 | spin_lock_nested(&cookie->parent->lock, 1); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 589 | hlist_for_each_entry(p, &cookie->parent->backing_objects, |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 590 | cookie_link) { |
| 591 | if (p->cache == object->cache) { |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 592 | if (fscache_object_is_dying(p)) { |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 593 | ret = -ENOBUFS; |
| 594 | spin_unlock(&cookie->parent->lock); |
| 595 | goto cant_attach_object; |
| 596 | } |
| 597 | object->parent = p; |
| 598 | spin_lock(&p->lock); |
| 599 | p->n_children++; |
| 600 | spin_unlock(&p->lock); |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | spin_unlock(&cookie->parent->lock); |
| 605 | |
| 606 | /* attach to the cache's object list */ |
| 607 | if (list_empty(&object->cache_link)) { |
| 608 | spin_lock(&cache->object_list_lock); |
| 609 | list_add(&object->cache_link, &cache->object_list); |
| 610 | spin_unlock(&cache->object_list_lock); |
| 611 | } |
| 612 | |
| 613 | /* attach to the cookie */ |
| 614 | object->cookie = cookie; |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 615 | fscache_cookie_get(cookie, fscache_cookie_get_attach_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 616 | hlist_add_head(&object->cookie_link, &cookie->backing_objects); |
David Howells | 4fbf429 | 2009-11-19 18:11:04 +0000 | [diff] [blame] | 617 | |
| 618 | fscache_objlist_add(object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 619 | ret = 0; |
| 620 | |
| 621 | cant_attach_object: |
| 622 | spin_unlock(&cookie->lock); |
| 623 | _leave(" = %d", ret); |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | /* |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 628 | * Invalidate an object. Callable with spinlocks held. |
| 629 | */ |
| 630 | void __fscache_invalidate(struct fscache_cookie *cookie) |
| 631 | { |
| 632 | struct fscache_object *object; |
| 633 | |
| 634 | _enter("{%s}", cookie->def->name); |
| 635 | |
| 636 | fscache_stat(&fscache_n_invalidates); |
| 637 | |
| 638 | /* Only permit invalidation of data files. Invalidating an index will |
| 639 | * require the caller to release all its attachments to the tree rooted |
| 640 | * there, and if it's doing that, it may as well just retire the |
| 641 | * cookie. |
| 642 | */ |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 643 | ASSERTCMP(cookie->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE); |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 644 | |
| 645 | /* If there's an object, we tell the object state machine to handle the |
| 646 | * invalidation on our behalf, otherwise there's nothing to do. |
| 647 | */ |
| 648 | if (!hlist_empty(&cookie->backing_objects)) { |
| 649 | spin_lock(&cookie->lock); |
| 650 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 651 | if (fscache_cookie_enabled(cookie) && |
| 652 | !hlist_empty(&cookie->backing_objects) && |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 653 | !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING, |
| 654 | &cookie->flags)) { |
| 655 | object = hlist_entry(cookie->backing_objects.first, |
| 656 | struct fscache_object, |
| 657 | cookie_link); |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 658 | if (fscache_object_is_live(object)) |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 659 | fscache_raise_event( |
| 660 | object, FSCACHE_OBJECT_EV_INVALIDATE); |
| 661 | } |
| 662 | |
| 663 | spin_unlock(&cookie->lock); |
| 664 | } |
| 665 | |
| 666 | _leave(""); |
| 667 | } |
| 668 | EXPORT_SYMBOL(__fscache_invalidate); |
| 669 | |
| 670 | /* |
| 671 | * Wait for object invalidation to complete. |
| 672 | */ |
| 673 | void __fscache_wait_on_invalidate(struct fscache_cookie *cookie) |
| 674 | { |
| 675 | _enter("%p", cookie); |
| 676 | |
| 677 | wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING, |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 678 | TASK_UNINTERRUPTIBLE); |
| 679 | |
| 680 | _leave(""); |
| 681 | } |
| 682 | EXPORT_SYMBOL(__fscache_wait_on_invalidate); |
| 683 | |
| 684 | /* |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 685 | * update the index entries backing a cookie |
| 686 | */ |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 687 | void __fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data) |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 688 | { |
| 689 | struct fscache_object *object; |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 690 | |
| 691 | fscache_stat(&fscache_n_updates); |
| 692 | |
| 693 | if (!cookie) { |
| 694 | fscache_stat(&fscache_n_updates_null); |
| 695 | _leave(" [no cookie]"); |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | _enter("{%s}", cookie->def->name); |
| 700 | |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 701 | spin_lock(&cookie->lock); |
| 702 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 703 | fscache_update_aux(cookie, aux_data); |
| 704 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 705 | if (fscache_cookie_enabled(cookie)) { |
| 706 | /* update the index entry on disk in each cache backing this |
| 707 | * cookie. |
| 708 | */ |
| 709 | hlist_for_each_entry(object, |
| 710 | &cookie->backing_objects, cookie_link) { |
| 711 | fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE); |
| 712 | } |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | spin_unlock(&cookie->lock); |
| 716 | _leave(""); |
| 717 | } |
| 718 | EXPORT_SYMBOL(__fscache_update_cookie); |
| 719 | |
| 720 | /* |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 721 | * Disable a cookie to stop it from accepting new requests from the netfs. |
| 722 | */ |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 723 | void __fscache_disable_cookie(struct fscache_cookie *cookie, |
| 724 | const void *aux_data, |
| 725 | bool invalidate) |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 726 | { |
| 727 | struct fscache_object *object; |
| 728 | bool awaken = false; |
| 729 | |
| 730 | _enter("%p,%u", cookie, invalidate); |
| 731 | |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 732 | trace_fscache_disable(cookie); |
| 733 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 734 | ASSERTCMP(atomic_read(&cookie->n_active), >, 0); |
| 735 | |
| 736 | if (atomic_read(&cookie->n_children) != 0) { |
Fabian Frederick | 36dfd11 | 2014-06-04 16:05:38 -0700 | [diff] [blame] | 737 | pr_err("Cookie '%s' still has children\n", |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 738 | cookie->def->name); |
| 739 | BUG(); |
| 740 | } |
| 741 | |
| 742 | wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK, |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 743 | TASK_UNINTERRUPTIBLE); |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 744 | |
| 745 | fscache_update_aux(cookie, aux_data); |
| 746 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 747 | if (!test_and_clear_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags)) |
| 748 | goto out_unlock_enable; |
| 749 | |
| 750 | /* If the cookie is being invalidated, wait for that to complete first |
| 751 | * so that we can reuse the flag. |
| 752 | */ |
| 753 | __fscache_wait_on_invalidate(cookie); |
| 754 | |
| 755 | /* Dispose of the backing objects */ |
| 756 | set_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags); |
| 757 | |
| 758 | spin_lock(&cookie->lock); |
| 759 | if (!hlist_empty(&cookie->backing_objects)) { |
| 760 | hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) { |
| 761 | if (invalidate) |
| 762 | set_bit(FSCACHE_OBJECT_RETIRED, &object->flags); |
David Howells | 6bdded5 | 2017-01-18 14:29:25 +0000 | [diff] [blame] | 763 | clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags); |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 764 | fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL); |
| 765 | } |
| 766 | } else { |
| 767 | if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) |
| 768 | awaken = true; |
| 769 | } |
| 770 | spin_unlock(&cookie->lock); |
| 771 | if (awaken) |
| 772 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING); |
| 773 | |
| 774 | /* Wait for cessation of activity requiring access to the netfs (when |
| 775 | * n_active reaches 0). This makes sure outstanding reads and writes |
| 776 | * have completed. |
| 777 | */ |
Peter Zijlstra | dc5d4afb | 2018-03-15 11:43:43 +0100 | [diff] [blame] | 778 | if (!atomic_dec_and_test(&cookie->n_active)) { |
| 779 | wait_var_event(&cookie->n_active, |
| 780 | !atomic_read(&cookie->n_active)); |
| 781 | } |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 782 | |
David Howells | 6bdded5 | 2017-01-18 14:29:25 +0000 | [diff] [blame] | 783 | /* Make sure any pending writes are cancelled. */ |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 784 | if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) |
David Howells | 6bdded5 | 2017-01-18 14:29:25 +0000 | [diff] [blame] | 785 | fscache_invalidate_writes(cookie); |
| 786 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 787 | /* Reset the cookie state if it wasn't relinquished */ |
| 788 | if (!test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags)) { |
| 789 | atomic_inc(&cookie->n_active); |
| 790 | set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); |
| 791 | } |
| 792 | |
| 793 | out_unlock_enable: |
| 794 | clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags); |
| 795 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK); |
| 796 | _leave(""); |
| 797 | } |
| 798 | EXPORT_SYMBOL(__fscache_disable_cookie); |
| 799 | |
| 800 | /* |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 801 | * release a cookie back to the cache |
| 802 | * - the object will be marked as recyclable on disk if retire is true |
| 803 | * - all dependents of this cookie must have already been unregistered |
| 804 | * (indices/files/pages) |
| 805 | */ |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 806 | void __fscache_relinquish_cookie(struct fscache_cookie *cookie, |
| 807 | const void *aux_data, |
| 808 | bool retire) |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 809 | { |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 810 | fscache_stat(&fscache_n_relinquishes); |
David Howells | 2175bb0 | 2009-11-19 18:11:38 +0000 | [diff] [blame] | 811 | if (retire) |
| 812 | fscache_stat(&fscache_n_relinquishes_retire); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 813 | |
| 814 | if (!cookie) { |
| 815 | fscache_stat(&fscache_n_relinquishes_null); |
| 816 | _leave(" [no cookie]"); |
| 817 | return; |
| 818 | } |
| 819 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 820 | _enter("%p{%s,%p,%d},%d", |
| 821 | cookie, cookie->def->name, cookie->netfs_data, |
| 822 | atomic_read(&cookie->n_active), retire); |
| 823 | |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 824 | trace_fscache_relinquish(cookie, retire); |
| 825 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 826 | /* No further netfs-accessing operations on this cookie permitted */ |
David Howells | d0fb31e | 2018-04-04 13:41:26 +0100 | [diff] [blame] | 827 | if (test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags)) |
| 828 | BUG(); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 829 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 830 | __fscache_disable_cookie(cookie, aux_data, retire); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 831 | |
| 832 | /* Clear pointers back to the netfs */ |
David Howells | 7e311a2 | 2009-11-19 18:11:11 +0000 | [diff] [blame] | 833 | cookie->netfs_data = NULL; |
| 834 | cookie->def = NULL; |
Matthew Wilcox | e5a9554 | 2018-04-10 16:36:48 -0700 | [diff] [blame] | 835 | BUG_ON(!radix_tree_empty(&cookie->stores)); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 836 | |
| 837 | if (cookie->parent) { |
| 838 | ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0); |
| 839 | ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0); |
| 840 | atomic_dec(&cookie->parent->n_children); |
| 841 | } |
| 842 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 843 | /* Dispose of the netfs's link to the cookie */ |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 844 | ASSERTCMP(atomic_read(&cookie->usage), >, 0); |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 845 | fscache_cookie_put(cookie, fscache_cookie_put_relinquish); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 846 | |
| 847 | _leave(""); |
| 848 | } |
| 849 | EXPORT_SYMBOL(__fscache_relinquish_cookie); |
| 850 | |
| 851 | /* |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 852 | * Remove a cookie from the hash table. |
| 853 | */ |
| 854 | static void fscache_unhash_cookie(struct fscache_cookie *cookie) |
| 855 | { |
| 856 | struct hlist_bl_head *h; |
| 857 | unsigned int bucket; |
| 858 | |
| 859 | bucket = cookie->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1); |
| 860 | h = &fscache_cookie_hash[bucket]; |
| 861 | |
| 862 | hlist_bl_lock(h); |
| 863 | hlist_bl_del(&cookie->hash_link); |
| 864 | hlist_bl_unlock(h); |
| 865 | } |
| 866 | |
| 867 | /* |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 868 | * Drop a reference to a cookie. |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 869 | */ |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 870 | void fscache_cookie_put(struct fscache_cookie *cookie, |
| 871 | enum fscache_cookie_trace where) |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 872 | { |
| 873 | struct fscache_cookie *parent; |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 874 | int usage; |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 875 | |
| 876 | _enter("%p", cookie); |
| 877 | |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 878 | do { |
| 879 | usage = atomic_dec_return(&cookie->usage); |
| 880 | trace_fscache_cookie(cookie, where, usage); |
| 881 | |
| 882 | if (usage > 0) |
| 883 | return; |
| 884 | BUG_ON(usage < 0); |
| 885 | |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 886 | parent = cookie->parent; |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 887 | fscache_unhash_cookie(cookie); |
| 888 | fscache_free_cookie(cookie); |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 889 | |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 890 | cookie = parent; |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 891 | where = fscache_cookie_put_parent; |
| 892 | } while (cookie); |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 893 | |
| 894 | _leave(""); |
| 895 | } |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 896 | |
| 897 | /* |
| 898 | * check the consistency between the netfs inode and the backing cache |
| 899 | * |
| 900 | * NOTE: it only serves no-index type |
| 901 | */ |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 902 | int __fscache_check_consistency(struct fscache_cookie *cookie, |
| 903 | const void *aux_data) |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 904 | { |
| 905 | struct fscache_operation *op; |
| 906 | struct fscache_object *object; |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 907 | bool wake_cookie = false; |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 908 | int ret; |
| 909 | |
| 910 | _enter("%p,", cookie); |
| 911 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 912 | ASSERTCMP(cookie->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 913 | |
| 914 | if (fscache_wait_for_deferred_lookup(cookie) < 0) |
| 915 | return -ERESTARTSYS; |
| 916 | |
| 917 | if (hlist_empty(&cookie->backing_objects)) |
| 918 | return 0; |
| 919 | |
| 920 | op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY); |
| 921 | if (!op) |
| 922 | return -ENOMEM; |
| 923 | |
David Howells | 08c2e3d | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 924 | fscache_operation_init(cookie, op, NULL, NULL, NULL); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 925 | op->flags = FSCACHE_OP_MYTHREAD | |
Milosz Tanski | 9c89d62 | 2013-09-09 14:28:57 -0400 | [diff] [blame] | 926 | (1 << FSCACHE_OP_WAITING) | |
| 927 | (1 << FSCACHE_OP_UNUSE_COOKIE); |
David Howells | 08c2e3d | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 928 | trace_fscache_page_op(cookie, NULL, op, fscache_page_op_check_consistency); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 929 | |
| 930 | spin_lock(&cookie->lock); |
| 931 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 932 | fscache_update_aux(cookie, aux_data); |
| 933 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 934 | if (!fscache_cookie_enabled(cookie) || |
| 935 | hlist_empty(&cookie->backing_objects)) |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 936 | goto inconsistent; |
| 937 | object = hlist_entry(cookie->backing_objects.first, |
| 938 | struct fscache_object, cookie_link); |
| 939 | if (test_bit(FSCACHE_IOERROR, &object->cache->flags)) |
| 940 | goto inconsistent; |
| 941 | |
| 942 | op->debug_id = atomic_inc_return(&fscache_op_debug_id); |
| 943 | |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 944 | __fscache_use_cookie(cookie); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 945 | if (fscache_submit_op(object, op) < 0) |
| 946 | goto submit_failed; |
| 947 | |
| 948 | /* the work queue now carries its own ref on the object */ |
| 949 | spin_unlock(&cookie->lock); |
| 950 | |
David Howells | d3b97ca | 2015-02-24 10:05:29 +0000 | [diff] [blame] | 951 | ret = fscache_wait_for_operation_activation(object, op, NULL, NULL); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 952 | if (ret == 0) { |
| 953 | /* ask the cache to honour the operation */ |
| 954 | ret = object->cache->ops->check_consistency(op); |
| 955 | fscache_op_complete(op, false); |
| 956 | } else if (ret == -ENOBUFS) { |
| 957 | ret = 0; |
| 958 | } |
| 959 | |
| 960 | fscache_put_operation(op); |
| 961 | _leave(" = %d", ret); |
| 962 | return ret; |
| 963 | |
| 964 | submit_failed: |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 965 | wake_cookie = __fscache_unuse_cookie(cookie); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 966 | inconsistent: |
| 967 | spin_unlock(&cookie->lock); |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 968 | if (wake_cookie) |
| 969 | __fscache_wake_unused_cookie(cookie); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 970 | kfree(op); |
| 971 | _leave(" = -ESTALE"); |
| 972 | return -ESTALE; |
| 973 | } |
| 974 | EXPORT_SYMBOL(__fscache_check_consistency); |