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 | |
| 24 | static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie); |
| 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 | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 30 | /* |
| 31 | * initialise an cookie jar slab element prior to any use |
| 32 | */ |
| 33 | void fscache_cookie_init_once(void *_cookie) |
| 34 | { |
| 35 | struct fscache_cookie *cookie = _cookie; |
| 36 | |
| 37 | memset(cookie, 0, sizeof(*cookie)); |
| 38 | spin_lock_init(&cookie->lock); |
David Howells | 1bccf51 | 2009-11-19 18:11:25 +0000 | [diff] [blame] | 39 | spin_lock_init(&cookie->stores_lock); |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 40 | INIT_HLIST_HEAD(&cookie->backing_objects); |
| 41 | } |
| 42 | |
| 43 | /* |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 44 | * request a cookie to represent an object (index, datafile, xattr, etc) |
| 45 | * - parent specifies the parent object |
| 46 | * - the top level index cookie for each netfs is stored in the fscache_netfs |
| 47 | * struct upon registration |
| 48 | * - def points to the definition |
| 49 | * - the netfs_data will be passed to the functions pointed to in *def |
| 50 | * - all attached caches will be searched to see if they contain this object |
| 51 | * - index objects aren't stored on disk until there's a dependent file that |
| 52 | * needs storing |
| 53 | * - other objects are stored in a selected cache immediately, and all the |
| 54 | * indices forming the path to it are instantiated if necessary |
| 55 | * - we never let on to the netfs about errors |
| 56 | * - we may set a negative cookie pointer, but that's okay |
| 57 | */ |
| 58 | struct fscache_cookie *__fscache_acquire_cookie( |
| 59 | struct fscache_cookie *parent, |
| 60 | const struct fscache_cookie_def *def, |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 61 | void *netfs_data, |
| 62 | bool enable) |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 63 | { |
| 64 | struct fscache_cookie *cookie; |
| 65 | |
| 66 | BUG_ON(!def); |
| 67 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 68 | _enter("{%s},{%s},%p,%u", |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 69 | parent ? (char *) parent->def->name : "<no-parent>", |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 70 | def->name, netfs_data, enable); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 71 | |
| 72 | fscache_stat(&fscache_n_acquires); |
| 73 | |
| 74 | /* if there's no parent cookie, then we don't create one here either */ |
| 75 | if (!parent) { |
| 76 | fscache_stat(&fscache_n_acquires_null); |
| 77 | _leave(" [no parent]"); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | /* validate the definition */ |
| 82 | BUG_ON(!def->get_key); |
| 83 | BUG_ON(!def->name[0]); |
| 84 | |
| 85 | BUG_ON(def->type == FSCACHE_COOKIE_TYPE_INDEX && |
| 86 | parent->def->type != FSCACHE_COOKIE_TYPE_INDEX); |
| 87 | |
| 88 | /* allocate and initialise a cookie */ |
| 89 | cookie = kmem_cache_alloc(fscache_cookie_jar, GFP_KERNEL); |
| 90 | if (!cookie) { |
| 91 | fscache_stat(&fscache_n_acquires_oom); |
| 92 | _leave(" [ENOMEM]"); |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | atomic_set(&cookie->usage, 1); |
| 97 | atomic_set(&cookie->n_children, 0); |
| 98 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 99 | /* We keep the active count elevated until relinquishment to prevent an |
| 100 | * attempt to wake up every time the object operations queue quiesces. |
| 101 | */ |
| 102 | atomic_set(&cookie->n_active, 1); |
| 103 | |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 104 | atomic_inc(&parent->usage); |
| 105 | atomic_inc(&parent->n_children); |
| 106 | |
| 107 | cookie->def = def; |
| 108 | cookie->parent = parent; |
| 109 | cookie->netfs_data = netfs_data; |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 110 | cookie->flags = (1 << FSCACHE_COOKIE_NO_DATA_YET); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 111 | |
David Howells | b34df79 | 2009-11-19 18:11:14 +0000 | [diff] [blame] | 112 | /* radix tree insertion won't use the preallocation pool unless it's |
| 113 | * told it may not wait */ |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 114 | INIT_RADIX_TREE(&cookie->stores, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 115 | |
| 116 | switch (cookie->def->type) { |
| 117 | case FSCACHE_COOKIE_TYPE_INDEX: |
| 118 | fscache_stat(&fscache_n_cookie_index); |
| 119 | break; |
| 120 | case FSCACHE_COOKIE_TYPE_DATAFILE: |
| 121 | fscache_stat(&fscache_n_cookie_data); |
| 122 | break; |
| 123 | default: |
| 124 | fscache_stat(&fscache_n_cookie_special); |
| 125 | break; |
| 126 | } |
| 127 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 128 | if (enable) { |
| 129 | /* if the object is an index then we need do nothing more here |
| 130 | * - we create indices on disk when we need them as an index |
| 131 | * may exist in multiple caches */ |
| 132 | if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) { |
| 133 | if (fscache_acquire_non_index_cookie(cookie) == 0) { |
| 134 | set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags); |
| 135 | } else { |
| 136 | atomic_dec(&parent->n_children); |
| 137 | __fscache_cookie_put(cookie); |
| 138 | fscache_stat(&fscache_n_acquires_nobufs); |
| 139 | _leave(" = NULL"); |
| 140 | return NULL; |
| 141 | } |
| 142 | } else { |
| 143 | set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | fscache_stat(&fscache_n_acquires_ok); |
| 148 | _leave(" = %p", cookie); |
| 149 | return cookie; |
| 150 | } |
| 151 | EXPORT_SYMBOL(__fscache_acquire_cookie); |
| 152 | |
| 153 | /* |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 154 | * Enable a cookie to permit it to accept new operations. |
| 155 | */ |
| 156 | void __fscache_enable_cookie(struct fscache_cookie *cookie, |
| 157 | bool (*can_enable)(void *data), |
| 158 | void *data) |
| 159 | { |
| 160 | _enter("%p", cookie); |
| 161 | |
| 162 | wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK, |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 163 | TASK_UNINTERRUPTIBLE); |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 164 | |
| 165 | if (test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags)) |
| 166 | goto out_unlock; |
| 167 | |
| 168 | if (can_enable && !can_enable(data)) { |
| 169 | /* The netfs decided it didn't want to enable after all */ |
| 170 | } else if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) { |
| 171 | /* Wait for outstanding disablement to complete */ |
| 172 | __fscache_wait_on_invalidate(cookie); |
| 173 | |
| 174 | if (fscache_acquire_non_index_cookie(cookie) == 0) |
| 175 | set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags); |
| 176 | } else { |
| 177 | set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags); |
| 178 | } |
| 179 | |
| 180 | out_unlock: |
| 181 | clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags); |
| 182 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK); |
| 183 | } |
| 184 | EXPORT_SYMBOL(__fscache_enable_cookie); |
| 185 | |
| 186 | /* |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 187 | * acquire a non-index cookie |
| 188 | * - this must make sure the index chain is instantiated and instantiate the |
| 189 | * object representation too |
| 190 | */ |
| 191 | static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie) |
| 192 | { |
| 193 | struct fscache_object *object; |
| 194 | struct fscache_cache *cache; |
| 195 | uint64_t i_size; |
| 196 | int ret; |
| 197 | |
| 198 | _enter(""); |
| 199 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 200 | set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 201 | |
| 202 | /* now we need to see whether the backing objects for this cookie yet |
| 203 | * exist, if not there'll be nothing to search */ |
| 204 | down_read(&fscache_addremove_sem); |
| 205 | |
| 206 | if (list_empty(&fscache_cache_list)) { |
| 207 | up_read(&fscache_addremove_sem); |
| 208 | _leave(" = 0 [no caches]"); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* select a cache in which to store the object */ |
| 213 | cache = fscache_select_cache_for_object(cookie->parent); |
| 214 | if (!cache) { |
| 215 | up_read(&fscache_addremove_sem); |
| 216 | fscache_stat(&fscache_n_acquires_no_cache); |
| 217 | _leave(" = -ENOMEDIUM [no cache]"); |
| 218 | return -ENOMEDIUM; |
| 219 | } |
| 220 | |
| 221 | _debug("cache %s", cache->tag->name); |
| 222 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 223 | set_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 224 | |
| 225 | /* ask the cache to allocate objects for this cookie and its parent |
| 226 | * chain */ |
| 227 | ret = fscache_alloc_object(cache, cookie); |
| 228 | if (ret < 0) { |
| 229 | up_read(&fscache_addremove_sem); |
| 230 | _leave(" = %d", ret); |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | /* pass on how big the object we're caching is supposed to be */ |
| 235 | cookie->def->get_attr(cookie->netfs_data, &i_size); |
| 236 | |
| 237 | spin_lock(&cookie->lock); |
| 238 | if (hlist_empty(&cookie->backing_objects)) { |
| 239 | spin_unlock(&cookie->lock); |
| 240 | goto unavailable; |
| 241 | } |
| 242 | |
| 243 | object = hlist_entry(cookie->backing_objects.first, |
| 244 | struct fscache_object, cookie_link); |
| 245 | |
| 246 | fscache_set_store_limit(object, i_size); |
| 247 | |
| 248 | /* initiate the process of looking up all the objects in the chain |
| 249 | * (done by fscache_initialise_object()) */ |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 250 | fscache_raise_event(object, FSCACHE_OBJECT_EV_NEW_CHILD); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 251 | |
| 252 | spin_unlock(&cookie->lock); |
| 253 | |
| 254 | /* we may be required to wait for lookup to complete at this point */ |
| 255 | if (!fscache_defer_lookup) { |
| 256 | _debug("non-deferred lookup %p", &cookie->flags); |
| 257 | wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP, |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 258 | TASK_UNINTERRUPTIBLE); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 259 | _debug("complete"); |
| 260 | if (test_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags)) |
| 261 | goto unavailable; |
| 262 | } |
| 263 | |
| 264 | up_read(&fscache_addremove_sem); |
| 265 | _leave(" = 0 [deferred]"); |
| 266 | return 0; |
| 267 | |
| 268 | unavailable: |
| 269 | up_read(&fscache_addremove_sem); |
| 270 | _leave(" = -ENOBUFS"); |
| 271 | return -ENOBUFS; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * recursively allocate cache object records for a cookie/cache combination |
| 276 | * - caller must be holding the addremove sem |
| 277 | */ |
| 278 | static int fscache_alloc_object(struct fscache_cache *cache, |
| 279 | struct fscache_cookie *cookie) |
| 280 | { |
| 281 | struct fscache_object *object; |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 282 | int ret; |
| 283 | |
| 284 | _enter("%p,%p{%s}", cache, cookie, cookie->def->name); |
| 285 | |
| 286 | spin_lock(&cookie->lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 287 | hlist_for_each_entry(object, &cookie->backing_objects, |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 288 | cookie_link) { |
| 289 | if (object->cache == cache) |
| 290 | goto object_already_extant; |
| 291 | } |
| 292 | spin_unlock(&cookie->lock); |
| 293 | |
| 294 | /* ask the cache to allocate an object (we may end up with duplicate |
| 295 | * objects at this stage, but we sort that out later) */ |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 296 | fscache_stat(&fscache_n_cop_alloc_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 297 | object = cache->ops->alloc_object(cache, cookie); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 298 | fscache_stat_d(&fscache_n_cop_alloc_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 299 | if (IS_ERR(object)) { |
| 300 | fscache_stat(&fscache_n_object_no_alloc); |
| 301 | ret = PTR_ERR(object); |
| 302 | goto error; |
| 303 | } |
| 304 | |
| 305 | fscache_stat(&fscache_n_object_alloc); |
| 306 | |
| 307 | object->debug_id = atomic_inc_return(&fscache_object_debug_id); |
| 308 | |
| 309 | _debug("ALLOC OBJ%x: %s {%lx}", |
| 310 | object->debug_id, cookie->def->name, object->events); |
| 311 | |
| 312 | ret = fscache_alloc_object(cache, cookie->parent); |
| 313 | if (ret < 0) |
| 314 | goto error_put; |
| 315 | |
| 316 | /* only attach if we managed to allocate all we needed, otherwise |
| 317 | * discard the object we just allocated and instead use the one |
| 318 | * attached to the cookie */ |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 319 | if (fscache_attach_object(cookie, object) < 0) { |
| 320 | fscache_stat(&fscache_n_cop_put_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 321 | cache->ops->put_object(object); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 322 | fscache_stat_d(&fscache_n_cop_put_object); |
| 323 | } |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 324 | |
| 325 | _leave(" = 0"); |
| 326 | return 0; |
| 327 | |
| 328 | object_already_extant: |
| 329 | ret = -ENOBUFS; |
David Howells | 8702152 | 2015-02-24 10:52:51 +0000 | [diff] [blame] | 330 | if (fscache_object_is_dying(object) || |
| 331 | fscache_cache_is_broken(object)) { |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 332 | spin_unlock(&cookie->lock); |
| 333 | goto error; |
| 334 | } |
| 335 | spin_unlock(&cookie->lock); |
| 336 | _leave(" = 0 [found]"); |
| 337 | return 0; |
| 338 | |
| 339 | error_put: |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 340 | fscache_stat(&fscache_n_cop_put_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 341 | cache->ops->put_object(object); |
David Howells | 52bd75f | 2009-11-19 18:11:08 +0000 | [diff] [blame] | 342 | fscache_stat_d(&fscache_n_cop_put_object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 343 | error: |
| 344 | _leave(" = %d", ret); |
| 345 | return ret; |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * attach a cache object to a cookie |
| 350 | */ |
| 351 | static int fscache_attach_object(struct fscache_cookie *cookie, |
| 352 | struct fscache_object *object) |
| 353 | { |
| 354 | struct fscache_object *p; |
| 355 | struct fscache_cache *cache = object->cache; |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 356 | int ret; |
| 357 | |
| 358 | _enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id); |
| 359 | |
| 360 | spin_lock(&cookie->lock); |
| 361 | |
| 362 | /* there may be multiple initial creations of this object, but we only |
| 363 | * want one */ |
| 364 | ret = -EEXIST; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 365 | hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) { |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 366 | if (p->cache == object->cache) { |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 367 | if (fscache_object_is_dying(p)) |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 368 | ret = -ENOBUFS; |
| 369 | goto cant_attach_object; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /* pin the parent object */ |
| 374 | spin_lock_nested(&cookie->parent->lock, 1); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 375 | hlist_for_each_entry(p, &cookie->parent->backing_objects, |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 376 | cookie_link) { |
| 377 | if (p->cache == object->cache) { |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 378 | if (fscache_object_is_dying(p)) { |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 379 | ret = -ENOBUFS; |
| 380 | spin_unlock(&cookie->parent->lock); |
| 381 | goto cant_attach_object; |
| 382 | } |
| 383 | object->parent = p; |
| 384 | spin_lock(&p->lock); |
| 385 | p->n_children++; |
| 386 | spin_unlock(&p->lock); |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | spin_unlock(&cookie->parent->lock); |
| 391 | |
| 392 | /* attach to the cache's object list */ |
| 393 | if (list_empty(&object->cache_link)) { |
| 394 | spin_lock(&cache->object_list_lock); |
| 395 | list_add(&object->cache_link, &cache->object_list); |
| 396 | spin_unlock(&cache->object_list_lock); |
| 397 | } |
| 398 | |
| 399 | /* attach to the cookie */ |
| 400 | object->cookie = cookie; |
| 401 | atomic_inc(&cookie->usage); |
| 402 | hlist_add_head(&object->cookie_link, &cookie->backing_objects); |
David Howells | 4fbf429 | 2009-11-19 18:11:04 +0000 | [diff] [blame] | 403 | |
| 404 | fscache_objlist_add(object); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 405 | ret = 0; |
| 406 | |
| 407 | cant_attach_object: |
| 408 | spin_unlock(&cookie->lock); |
| 409 | _leave(" = %d", ret); |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | /* |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 414 | * Invalidate an object. Callable with spinlocks held. |
| 415 | */ |
| 416 | void __fscache_invalidate(struct fscache_cookie *cookie) |
| 417 | { |
| 418 | struct fscache_object *object; |
| 419 | |
| 420 | _enter("{%s}", cookie->def->name); |
| 421 | |
| 422 | fscache_stat(&fscache_n_invalidates); |
| 423 | |
| 424 | /* Only permit invalidation of data files. Invalidating an index will |
| 425 | * require the caller to release all its attachments to the tree rooted |
| 426 | * there, and if it's doing that, it may as well just retire the |
| 427 | * cookie. |
| 428 | */ |
| 429 | ASSERTCMP(cookie->def->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE); |
| 430 | |
| 431 | /* We will be updating the cookie too. */ |
| 432 | BUG_ON(!cookie->def->get_aux); |
| 433 | |
| 434 | /* If there's an object, we tell the object state machine to handle the |
| 435 | * invalidation on our behalf, otherwise there's nothing to do. |
| 436 | */ |
| 437 | if (!hlist_empty(&cookie->backing_objects)) { |
| 438 | spin_lock(&cookie->lock); |
| 439 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 440 | if (fscache_cookie_enabled(cookie) && |
| 441 | !hlist_empty(&cookie->backing_objects) && |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 442 | !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING, |
| 443 | &cookie->flags)) { |
| 444 | object = hlist_entry(cookie->backing_objects.first, |
| 445 | struct fscache_object, |
| 446 | cookie_link); |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 447 | if (fscache_object_is_live(object)) |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 448 | fscache_raise_event( |
| 449 | object, FSCACHE_OBJECT_EV_INVALIDATE); |
| 450 | } |
| 451 | |
| 452 | spin_unlock(&cookie->lock); |
| 453 | } |
| 454 | |
| 455 | _leave(""); |
| 456 | } |
| 457 | EXPORT_SYMBOL(__fscache_invalidate); |
| 458 | |
| 459 | /* |
| 460 | * Wait for object invalidation to complete. |
| 461 | */ |
| 462 | void __fscache_wait_on_invalidate(struct fscache_cookie *cookie) |
| 463 | { |
| 464 | _enter("%p", cookie); |
| 465 | |
| 466 | wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING, |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 467 | TASK_UNINTERRUPTIBLE); |
| 468 | |
| 469 | _leave(""); |
| 470 | } |
| 471 | EXPORT_SYMBOL(__fscache_wait_on_invalidate); |
| 472 | |
| 473 | /* |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 474 | * update the index entries backing a cookie |
| 475 | */ |
| 476 | void __fscache_update_cookie(struct fscache_cookie *cookie) |
| 477 | { |
| 478 | struct fscache_object *object; |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 479 | |
| 480 | fscache_stat(&fscache_n_updates); |
| 481 | |
| 482 | if (!cookie) { |
| 483 | fscache_stat(&fscache_n_updates_null); |
| 484 | _leave(" [no cookie]"); |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | _enter("{%s}", cookie->def->name); |
| 489 | |
| 490 | BUG_ON(!cookie->def->get_aux); |
| 491 | |
| 492 | spin_lock(&cookie->lock); |
| 493 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 494 | if (fscache_cookie_enabled(cookie)) { |
| 495 | /* update the index entry on disk in each cache backing this |
| 496 | * cookie. |
| 497 | */ |
| 498 | hlist_for_each_entry(object, |
| 499 | &cookie->backing_objects, cookie_link) { |
| 500 | fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE); |
| 501 | } |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | spin_unlock(&cookie->lock); |
| 505 | _leave(""); |
| 506 | } |
| 507 | EXPORT_SYMBOL(__fscache_update_cookie); |
| 508 | |
| 509 | /* |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 510 | * Disable a cookie to stop it from accepting new requests from the netfs. |
| 511 | */ |
| 512 | void __fscache_disable_cookie(struct fscache_cookie *cookie, bool invalidate) |
| 513 | { |
| 514 | struct fscache_object *object; |
| 515 | bool awaken = false; |
| 516 | |
| 517 | _enter("%p,%u", cookie, invalidate); |
| 518 | |
| 519 | ASSERTCMP(atomic_read(&cookie->n_active), >, 0); |
| 520 | |
| 521 | if (atomic_read(&cookie->n_children) != 0) { |
Fabian Frederick | 36dfd11 | 2014-06-04 16:05:38 -0700 | [diff] [blame] | 522 | pr_err("Cookie '%s' still has children\n", |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 523 | cookie->def->name); |
| 524 | BUG(); |
| 525 | } |
| 526 | |
| 527 | wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK, |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 528 | TASK_UNINTERRUPTIBLE); |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 529 | if (!test_and_clear_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags)) |
| 530 | goto out_unlock_enable; |
| 531 | |
| 532 | /* If the cookie is being invalidated, wait for that to complete first |
| 533 | * so that we can reuse the flag. |
| 534 | */ |
| 535 | __fscache_wait_on_invalidate(cookie); |
| 536 | |
| 537 | /* Dispose of the backing objects */ |
| 538 | set_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags); |
| 539 | |
| 540 | spin_lock(&cookie->lock); |
| 541 | if (!hlist_empty(&cookie->backing_objects)) { |
| 542 | hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) { |
| 543 | if (invalidate) |
| 544 | set_bit(FSCACHE_OBJECT_RETIRED, &object->flags); |
David Howells | 6bdded5 | 2017-01-18 14:29:25 +0000 | [diff] [blame] | 545 | clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags); |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 546 | fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL); |
| 547 | } |
| 548 | } else { |
| 549 | if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags)) |
| 550 | awaken = true; |
| 551 | } |
| 552 | spin_unlock(&cookie->lock); |
| 553 | if (awaken) |
| 554 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING); |
| 555 | |
| 556 | /* Wait for cessation of activity requiring access to the netfs (when |
| 557 | * n_active reaches 0). This makes sure outstanding reads and writes |
| 558 | * have completed. |
| 559 | */ |
| 560 | if (!atomic_dec_and_test(&cookie->n_active)) |
| 561 | wait_on_atomic_t(&cookie->n_active, fscache_wait_atomic_t, |
| 562 | TASK_UNINTERRUPTIBLE); |
| 563 | |
David Howells | 6bdded5 | 2017-01-18 14:29:25 +0000 | [diff] [blame] | 564 | /* Make sure any pending writes are cancelled. */ |
| 565 | if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) |
| 566 | fscache_invalidate_writes(cookie); |
| 567 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 568 | /* Reset the cookie state if it wasn't relinquished */ |
| 569 | if (!test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags)) { |
| 570 | atomic_inc(&cookie->n_active); |
| 571 | set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags); |
| 572 | } |
| 573 | |
| 574 | out_unlock_enable: |
| 575 | clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags); |
| 576 | wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK); |
| 577 | _leave(""); |
| 578 | } |
| 579 | EXPORT_SYMBOL(__fscache_disable_cookie); |
| 580 | |
| 581 | /* |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 582 | * release a cookie back to the cache |
| 583 | * - the object will be marked as recyclable on disk if retire is true |
| 584 | * - all dependents of this cookie must have already been unregistered |
| 585 | * (indices/files/pages) |
| 586 | */ |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 587 | void __fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire) |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 588 | { |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 589 | fscache_stat(&fscache_n_relinquishes); |
David Howells | 2175bb0 | 2009-11-19 18:11:38 +0000 | [diff] [blame] | 590 | if (retire) |
| 591 | fscache_stat(&fscache_n_relinquishes_retire); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 592 | |
| 593 | if (!cookie) { |
| 594 | fscache_stat(&fscache_n_relinquishes_null); |
| 595 | _leave(" [no cookie]"); |
| 596 | return; |
| 597 | } |
| 598 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 599 | _enter("%p{%s,%p,%d},%d", |
| 600 | cookie, cookie->def->name, cookie->netfs_data, |
| 601 | atomic_read(&cookie->n_active), retire); |
| 602 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 603 | /* No further netfs-accessing operations on this cookie permitted */ |
| 604 | set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 605 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 606 | __fscache_disable_cookie(cookie, retire); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 607 | |
| 608 | /* Clear pointers back to the netfs */ |
David Howells | 7e311a2 | 2009-11-19 18:11:11 +0000 | [diff] [blame] | 609 | cookie->netfs_data = NULL; |
| 610 | cookie->def = NULL; |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 611 | BUG_ON(cookie->stores.rnode); |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 612 | |
| 613 | if (cookie->parent) { |
| 614 | ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0); |
| 615 | ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0); |
| 616 | atomic_dec(&cookie->parent->n_children); |
| 617 | } |
| 618 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 619 | /* Dispose of the netfs's link to the cookie */ |
David Howells | ccc4fc3 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 620 | ASSERTCMP(atomic_read(&cookie->usage), >, 0); |
| 621 | fscache_cookie_put(cookie); |
| 622 | |
| 623 | _leave(""); |
| 624 | } |
| 625 | EXPORT_SYMBOL(__fscache_relinquish_cookie); |
| 626 | |
| 627 | /* |
David Howells | 955d0091 | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 628 | * destroy a cookie |
| 629 | */ |
| 630 | void __fscache_cookie_put(struct fscache_cookie *cookie) |
| 631 | { |
| 632 | struct fscache_cookie *parent; |
| 633 | |
| 634 | _enter("%p", cookie); |
| 635 | |
| 636 | for (;;) { |
| 637 | _debug("FREE COOKIE %p", cookie); |
| 638 | parent = cookie->parent; |
| 639 | BUG_ON(!hlist_empty(&cookie->backing_objects)); |
| 640 | kmem_cache_free(fscache_cookie_jar, cookie); |
| 641 | |
| 642 | if (!parent) |
| 643 | break; |
| 644 | |
| 645 | cookie = parent; |
| 646 | BUG_ON(atomic_read(&cookie->usage) <= 0); |
| 647 | if (!atomic_dec_and_test(&cookie->usage)) |
| 648 | break; |
| 649 | } |
| 650 | |
| 651 | _leave(""); |
| 652 | } |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 653 | |
| 654 | /* |
| 655 | * check the consistency between the netfs inode and the backing cache |
| 656 | * |
| 657 | * NOTE: it only serves no-index type |
| 658 | */ |
| 659 | int __fscache_check_consistency(struct fscache_cookie *cookie) |
| 660 | { |
| 661 | struct fscache_operation *op; |
| 662 | struct fscache_object *object; |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 663 | bool wake_cookie = false; |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 664 | int ret; |
| 665 | |
| 666 | _enter("%p,", cookie); |
| 667 | |
| 668 | ASSERTCMP(cookie->def->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE); |
| 669 | |
| 670 | if (fscache_wait_for_deferred_lookup(cookie) < 0) |
| 671 | return -ERESTARTSYS; |
| 672 | |
| 673 | if (hlist_empty(&cookie->backing_objects)) |
| 674 | return 0; |
| 675 | |
| 676 | op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY); |
| 677 | if (!op) |
| 678 | return -ENOMEM; |
| 679 | |
David Howells | d3b97ca | 2015-02-24 10:05:29 +0000 | [diff] [blame] | 680 | fscache_operation_init(op, NULL, NULL, NULL); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 681 | op->flags = FSCACHE_OP_MYTHREAD | |
Milosz Tanski | 9c89d62 | 2013-09-09 14:28:57 -0400 | [diff] [blame] | 682 | (1 << FSCACHE_OP_WAITING) | |
| 683 | (1 << FSCACHE_OP_UNUSE_COOKIE); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 684 | |
| 685 | spin_lock(&cookie->lock); |
| 686 | |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 687 | if (!fscache_cookie_enabled(cookie) || |
| 688 | hlist_empty(&cookie->backing_objects)) |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 689 | goto inconsistent; |
| 690 | object = hlist_entry(cookie->backing_objects.first, |
| 691 | struct fscache_object, cookie_link); |
| 692 | if (test_bit(FSCACHE_IOERROR, &object->cache->flags)) |
| 693 | goto inconsistent; |
| 694 | |
| 695 | op->debug_id = atomic_inc_return(&fscache_op_debug_id); |
| 696 | |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 697 | __fscache_use_cookie(cookie); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 698 | if (fscache_submit_op(object, op) < 0) |
| 699 | goto submit_failed; |
| 700 | |
| 701 | /* the work queue now carries its own ref on the object */ |
| 702 | spin_unlock(&cookie->lock); |
| 703 | |
David Howells | d3b97ca | 2015-02-24 10:05:29 +0000 | [diff] [blame] | 704 | ret = fscache_wait_for_operation_activation(object, op, NULL, NULL); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 705 | if (ret == 0) { |
| 706 | /* ask the cache to honour the operation */ |
| 707 | ret = object->cache->ops->check_consistency(op); |
| 708 | fscache_op_complete(op, false); |
| 709 | } else if (ret == -ENOBUFS) { |
| 710 | ret = 0; |
| 711 | } |
| 712 | |
| 713 | fscache_put_operation(op); |
| 714 | _leave(" = %d", ret); |
| 715 | return ret; |
| 716 | |
| 717 | submit_failed: |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 718 | wake_cookie = __fscache_unuse_cookie(cookie); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 719 | inconsistent: |
| 720 | spin_unlock(&cookie->lock); |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 721 | if (wake_cookie) |
| 722 | __fscache_wake_unused_cookie(cookie); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 723 | kfree(op); |
| 724 | _leave(" = -ESTALE"); |
| 725 | return -ESTALE; |
| 726 | } |
| 727 | EXPORT_SYMBOL(__fscache_check_consistency); |