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