Blame view

fs/fscache/cookie.c 25.2 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
955d00917   David Howells   FS-Cache: Provide...
2
3
4
5
6
  /* netfs cookie management
   *
   * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
   * Written by David Howells (dhowells@redhat.com)
   *
ccc4fc3d1   David Howells   FS-Cache: Impleme...
7
8
   * See Documentation/filesystems/caching/netfs-api.txt for more information on
   * the netfs API.
955d00917   David Howells   FS-Cache: Provide...
9
10
11
12
13
14
15
16
   */
  
  #define FSCACHE_DEBUG_LEVEL COOKIE
  #include <linux/module.h>
  #include <linux/slab.h>
  #include "internal.h"
  
  struct kmem_cache *fscache_cookie_jar;
ccc4fc3d1   David Howells   FS-Cache: Impleme...
17
  static atomic_t fscache_object_debug_id = ATOMIC_INIT(0);
ec0328e46   David Howells   fscache: Maintain...
18
19
  #define fscache_cookie_hash_shift 15
  static struct hlist_bl_head fscache_cookie_hash[1 << fscache_cookie_hash_shift];
ee1235a9a   David Howells   fscache: Pass obj...
20
21
  static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie,
  					    loff_t object_size);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
22
23
24
25
  static int fscache_alloc_object(struct fscache_cache *cache,
  				struct fscache_cookie *cookie);
  static int fscache_attach_object(struct fscache_cookie *cookie,
  				 struct fscache_object *object);
ec0328e46   David Howells   fscache: Maintain...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  static void fscache_print_cookie(struct fscache_cookie *cookie, char prefix)
  {
  	struct hlist_node *object;
  	const u8 *k;
  	unsigned loop;
  
  	pr_err("%c-cookie c=%p [p=%p fl=%lx nc=%u na=%u]
  ",
  	       prefix, cookie, cookie->parent, cookie->flags,
  	       atomic_read(&cookie->n_children),
  	       atomic_read(&cookie->n_active));
  	pr_err("%c-cookie d=%p n=%p
  ",
  	       prefix, cookie->def, cookie->netfs_data);
  
  	object = READ_ONCE(cookie->backing_objects.first);
  	if (object)
  		pr_err("%c-cookie o=%p
  ",
  		       prefix, hlist_entry(object, struct fscache_object, cookie_link));
  
  	pr_err("%c-key=[%u] '", prefix, cookie->key_len);
  	k = (cookie->key_len <= sizeof(cookie->inline_key)) ?
  		cookie->inline_key : cookie->key;
  	for (loop = 0; loop < cookie->key_len; loop++)
  		pr_cont("%02x", k[loop]);
  	pr_cont("'
  ");
  }
  
  void fscache_free_cookie(struct fscache_cookie *cookie)
  {
  	if (cookie) {
  		BUG_ON(!hlist_empty(&cookie->backing_objects));
  		if (cookie->aux_len > sizeof(cookie->inline_aux))
  			kfree(cookie->aux);
  		if (cookie->key_len > sizeof(cookie->inline_key))
  			kfree(cookie->key);
  		kmem_cache_free(fscache_cookie_jar, cookie);
  	}
  }
955d00917   David Howells   FS-Cache: Provide...
67
  /*
fa520c47e   Eric Sandeen   fscache: Fix out ...
68
   * Set the index key in a cookie.  The cookie struct has space for a 16-byte
ec0328e46   David Howells   fscache: Maintain...
69
70
71
72
73
74
75
76
77
   * key plus length and hash, but if that's not big enough, it's instead a
   * pointer to a buffer containing 3 bytes of hash, 1 byte of length and then
   * the key data.
   */
  static int fscache_set_key(struct fscache_cookie *cookie,
  			   const void *index_key, size_t index_key_len)
  {
  	unsigned long long h;
  	u32 *buf;
fa520c47e   Eric Sandeen   fscache: Fix out ...
78
  	int bufs;
ec0328e46   David Howells   fscache: Maintain...
79
  	int i;
fa520c47e   Eric Sandeen   fscache: Fix out ...
80
  	bufs = DIV_ROUND_UP(index_key_len, sizeof(*buf));
ec0328e46   David Howells   fscache: Maintain...
81
  	if (index_key_len > sizeof(cookie->inline_key)) {
fa520c47e   Eric Sandeen   fscache: Fix out ...
82
  		buf = kcalloc(bufs, sizeof(*buf), GFP_KERNEL);
ec0328e46   David Howells   fscache: Maintain...
83
84
85
86
87
  		if (!buf)
  			return -ENOMEM;
  		cookie->key = buf;
  	} else {
  		buf = (u32 *)cookie->inline_key;
ec0328e46   David Howells   fscache: Maintain...
88
89
90
91
92
93
94
95
96
  	}
  
  	memcpy(buf, index_key, index_key_len);
  
  	/* Calculate a hash and combine this with the length in the first word
  	 * or first half word
  	 */
  	h = (unsigned long)cookie->parent;
  	h += index_key_len + cookie->type;
fa520c47e   Eric Sandeen   fscache: Fix out ...
97
98
  
  	for (i = 0; i < bufs; i++)
ec0328e46   David Howells   fscache: Maintain...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  		h += buf[i];
  
  	cookie->key_hash = h ^ (h >> 32);
  	return 0;
  }
  
  static long fscache_compare_cookie(const struct fscache_cookie *a,
  				   const struct fscache_cookie *b)
  {
  	const void *ka, *kb;
  
  	if (a->key_hash != b->key_hash)
  		return (long)a->key_hash - (long)b->key_hash;
  	if (a->parent != b->parent)
  		return (long)a->parent - (long)b->parent;
  	if (a->key_len != b->key_len)
  		return (long)a->key_len - (long)b->key_len;
  	if (a->type != b->type)
  		return (long)a->type - (long)b->type;
  
  	if (a->key_len <= sizeof(a->inline_key)) {
  		ka = &a->inline_key;
  		kb = &b->inline_key;
  	} else {
  		ka = a->key;
  		kb = b->key;
  	}
  	return memcmp(ka, kb, a->key_len);
  }
  
  /*
   * Allocate a cookie.
   */
  struct fscache_cookie *fscache_alloc_cookie(
  	struct fscache_cookie *parent,
  	const struct fscache_cookie_def *def,
  	const void *index_key, size_t index_key_len,
  	const void *aux_data, size_t aux_data_len,
  	void *netfs_data,
  	loff_t object_size)
  {
  	struct fscache_cookie *cookie;
  
  	/* allocate and initialise a cookie */
1ff22883b   David Howells   fscache: Fix inco...
143
  	cookie = kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL);
ec0328e46   David Howells   fscache: Maintain...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  	if (!cookie)
  		return NULL;
  
  	cookie->key_len = index_key_len;
  	cookie->aux_len = aux_data_len;
  
  	if (fscache_set_key(cookie, index_key, index_key_len) < 0)
  		goto nomem;
  
  	if (cookie->aux_len <= sizeof(cookie->inline_aux)) {
  		memcpy(cookie->inline_aux, aux_data, cookie->aux_len);
  	} else {
  		cookie->aux = kmemdup(aux_data, cookie->aux_len, GFP_KERNEL);
  		if (!cookie->aux)
  			goto nomem;
  	}
  
  	atomic_set(&cookie->usage, 1);
  	atomic_set(&cookie->n_children, 0);
  
  	/* We keep the active count elevated until relinquishment to prevent an
  	 * attempt to wake up every time the object operations queue quiesces.
  	 */
  	atomic_set(&cookie->n_active, 1);
  
  	cookie->def		= def;
  	cookie->parent		= parent;
  	cookie->netfs_data	= netfs_data;
  	cookie->flags		= (1 << FSCACHE_COOKIE_NO_DATA_YET);
  	cookie->type		= def->type;
1ff22883b   David Howells   fscache: Fix inco...
174
175
176
  	spin_lock_init(&cookie->lock);
  	spin_lock_init(&cookie->stores_lock);
  	INIT_HLIST_HEAD(&cookie->backing_objects);
ec0328e46   David Howells   fscache: Maintain...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  
  	/* radix tree insertion won't use the preallocation pool unless it's
  	 * told it may not wait */
  	INIT_RADIX_TREE(&cookie->stores, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
  	return cookie;
  
  nomem:
  	fscache_free_cookie(cookie);
  	return NULL;
  }
  
  /*
   * Attempt to insert the new cookie into the hash.  If there's a collision, we
   * return the old cookie if it's not in use and an error otherwise.
   */
  struct fscache_cookie *fscache_hash_cookie(struct fscache_cookie *candidate)
  {
  	struct fscache_cookie *cursor;
  	struct hlist_bl_head *h;
  	struct hlist_bl_node *p;
  	unsigned int bucket;
  
  	bucket = candidate->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
  	h = &fscache_cookie_hash[bucket];
  
  	hlist_bl_lock(h);
  	hlist_bl_for_each_entry(cursor, p, h, hash_link) {
  		if (fscache_compare_cookie(candidate, cursor) == 0)
  			goto collision;
  	}
  
  	__set_bit(FSCACHE_COOKIE_ACQUIRED, &candidate->flags);
  	fscache_cookie_get(candidate->parent, fscache_cookie_get_acquire_parent);
  	atomic_inc(&candidate->parent->n_children);
  	hlist_bl_add_head(&candidate->hash_link, h);
  	hlist_bl_unlock(h);
  	return candidate;
  
  collision:
  	if (test_and_set_bit(FSCACHE_COOKIE_ACQUIRED, &cursor->flags)) {
  		trace_fscache_cookie(cursor, fscache_cookie_collision,
  				     atomic_read(&cursor->usage));
  		pr_err("Duplicate cookie detected
  ");
  		fscache_print_cookie(cursor, 'O');
  		fscache_print_cookie(candidate, 'N');
  		hlist_bl_unlock(h);
  		return NULL;
  	}
  
  	fscache_cookie_get(cursor, fscache_cookie_get_reacquire);
  	hlist_bl_unlock(h);
  	return cursor;
  }
  
  /*
ccc4fc3d1   David Howells   FS-Cache: Impleme...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
   * request a cookie to represent an object (index, datafile, xattr, etc)
   * - parent specifies the parent object
   *   - the top level index cookie for each netfs is stored in the fscache_netfs
   *     struct upon registration
   * - def points to the definition
   * - the netfs_data will be passed to the functions pointed to in *def
   * - all attached caches will be searched to see if they contain this object
   * - index objects aren't stored on disk until there's a dependent file that
   *   needs storing
   * - other objects are stored in a selected cache immediately, and all the
   *   indices forming the path to it are instantiated if necessary
   * - we never let on to the netfs about errors
   *   - we may set a negative cookie pointer, but that's okay
   */
  struct fscache_cookie *__fscache_acquire_cookie(
  	struct fscache_cookie *parent,
  	const struct fscache_cookie_def *def,
402cb8dda   David Howells   fscache: Attach t...
250
251
  	const void *index_key, size_t index_key_len,
  	const void *aux_data, size_t aux_data_len,
94d30ae90   David Howells   FS-Cache: Provide...
252
  	void *netfs_data,
ee1235a9a   David Howells   fscache: Pass obj...
253
  	loff_t object_size,
94d30ae90   David Howells   FS-Cache: Provide...
254
  	bool enable)
ccc4fc3d1   David Howells   FS-Cache: Impleme...
255
  {
ec0328e46   David Howells   fscache: Maintain...
256
  	struct fscache_cookie *candidate, *cookie;
ccc4fc3d1   David Howells   FS-Cache: Impleme...
257
258
  
  	BUG_ON(!def);
94d30ae90   David Howells   FS-Cache: Provide...
259
  	_enter("{%s},{%s},%p,%u",
ccc4fc3d1   David Howells   FS-Cache: Impleme...
260
  	       parent ? (char *) parent->def->name : "<no-parent>",
94d30ae90   David Howells   FS-Cache: Provide...
261
  	       def->name, netfs_data, enable);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
262

402cb8dda   David Howells   fscache: Attach t...
263
264
265
266
267
268
  	if (!index_key || !index_key_len || index_key_len > 255 || aux_data_len > 255)
  		return NULL;
  	if (!aux_data || !aux_data_len) {
  		aux_data = NULL;
  		aux_data_len = 0;
  	}
ccc4fc3d1   David Howells   FS-Cache: Impleme...
269
270
271
272
273
274
275
276
277
278
  	fscache_stat(&fscache_n_acquires);
  
  	/* if there's no parent cookie, then we don't create one here either */
  	if (!parent) {
  		fscache_stat(&fscache_n_acquires_null);
  		_leave(" [no parent]");
  		return NULL;
  	}
  
  	/* validate the definition */
ccc4fc3d1   David Howells   FS-Cache: Impleme...
279
280
281
  	BUG_ON(!def->name[0]);
  
  	BUG_ON(def->type == FSCACHE_COOKIE_TYPE_INDEX &&
402cb8dda   David Howells   fscache: Attach t...
282
  	       parent->type != FSCACHE_COOKIE_TYPE_INDEX);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
283

ec0328e46   David Howells   fscache: Maintain...
284
285
286
287
288
  	candidate = fscache_alloc_cookie(parent, def,
  					 index_key, index_key_len,
  					 aux_data, aux_data_len,
  					 netfs_data, object_size);
  	if (!candidate) {
ccc4fc3d1   David Howells   FS-Cache: Impleme...
289
290
291
292
  		fscache_stat(&fscache_n_acquires_oom);
  		_leave(" [ENOMEM]");
  		return NULL;
  	}
ec0328e46   David Howells   fscache: Maintain...
293
294
295
296
  	cookie = fscache_hash_cookie(candidate);
  	if (!cookie) {
  		trace_fscache_cookie(candidate, fscache_cookie_discard, 1);
  		goto out;
402cb8dda   David Howells   fscache: Attach t...
297
  	}
ec0328e46   David Howells   fscache: Maintain...
298
299
  	if (cookie == candidate)
  		candidate = NULL;
ccc4fc3d1   David Howells   FS-Cache: Impleme...
300

402cb8dda   David Howells   fscache: Attach t...
301
  	switch (cookie->type) {
ccc4fc3d1   David Howells   FS-Cache: Impleme...
302
303
304
305
306
307
308
309
310
311
  	case FSCACHE_COOKIE_TYPE_INDEX:
  		fscache_stat(&fscache_n_cookie_index);
  		break;
  	case FSCACHE_COOKIE_TYPE_DATAFILE:
  		fscache_stat(&fscache_n_cookie_data);
  		break;
  	default:
  		fscache_stat(&fscache_n_cookie_special);
  		break;
  	}
a18feb557   David Howells   fscache: Add trac...
312
  	trace_fscache_acquire(cookie);
94d30ae90   David Howells   FS-Cache: Provide...
313
314
315
316
  	if (enable) {
  		/* if the object is an index then we need do nothing more here
  		 * - we create indices on disk when we need them as an index
  		 * may exist in multiple caches */
402cb8dda   David Howells   fscache: Attach t...
317
  		if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) {
ee1235a9a   David Howells   fscache: Pass obj...
318
  			if (fscache_acquire_non_index_cookie(cookie, object_size) == 0) {
94d30ae90   David Howells   FS-Cache: Provide...
319
320
321
  				set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
  			} else {
  				atomic_dec(&parent->n_children);
a18feb557   David Howells   fscache: Add trac...
322
323
  				fscache_cookie_put(cookie,
  						   fscache_cookie_put_acquire_nobufs);
94d30ae90   David Howells   FS-Cache: Provide...
324
325
326
327
328
329
  				fscache_stat(&fscache_n_acquires_nobufs);
  				_leave(" = NULL");
  				return NULL;
  			}
  		} else {
  			set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
330
331
332
333
  		}
  	}
  
  	fscache_stat(&fscache_n_acquires_ok);
402cb8dda   David Howells   fscache: Attach t...
334

ec0328e46   David Howells   fscache: Maintain...
335
336
337
  out:
  	fscache_free_cookie(candidate);
  	return cookie;
ccc4fc3d1   David Howells   FS-Cache: Impleme...
338
339
340
341
  }
  EXPORT_SYMBOL(__fscache_acquire_cookie);
  
  /*
94d30ae90   David Howells   FS-Cache: Provide...
342
343
344
   * Enable a cookie to permit it to accept new operations.
   */
  void __fscache_enable_cookie(struct fscache_cookie *cookie,
402cb8dda   David Howells   fscache: Attach t...
345
  			     const void *aux_data,
ee1235a9a   David Howells   fscache: Pass obj...
346
  			     loff_t object_size,
94d30ae90   David Howells   FS-Cache: Provide...
347
348
349
350
  			     bool (*can_enable)(void *data),
  			     void *data)
  {
  	_enter("%p", cookie);
a18feb557   David Howells   fscache: Add trac...
351
  	trace_fscache_enable(cookie);
94d30ae90   David Howells   FS-Cache: Provide...
352
  	wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
743162013   NeilBrown   sched: Remove pro...
353
  			 TASK_UNINTERRUPTIBLE);
94d30ae90   David Howells   FS-Cache: Provide...
354

402cb8dda   David Howells   fscache: Attach t...
355
  	fscache_update_aux(cookie, aux_data);
94d30ae90   David Howells   FS-Cache: Provide...
356
357
358
359
360
  	if (test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
  		goto out_unlock;
  
  	if (can_enable && !can_enable(data)) {
  		/* The netfs decided it didn't want to enable after all */
402cb8dda   David Howells   fscache: Attach t...
361
  	} else if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX) {
94d30ae90   David Howells   FS-Cache: Provide...
362
363
  		/* Wait for outstanding disablement to complete */
  		__fscache_wait_on_invalidate(cookie);
ee1235a9a   David Howells   fscache: Pass obj...
364
  		if (fscache_acquire_non_index_cookie(cookie, object_size) == 0)
94d30ae90   David Howells   FS-Cache: Provide...
365
366
367
368
369
370
371
372
373
374
375
376
  			set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
  	} else {
  		set_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
  	}
  
  out_unlock:
  	clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
  	wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
  }
  EXPORT_SYMBOL(__fscache_enable_cookie);
  
  /*
ccc4fc3d1   David Howells   FS-Cache: Impleme...
377
378
379
380
   * acquire a non-index cookie
   * - this must make sure the index chain is instantiated and instantiate the
   *   object representation too
   */
ee1235a9a   David Howells   fscache: Pass obj...
381
382
  static int fscache_acquire_non_index_cookie(struct fscache_cookie *cookie,
  					    loff_t object_size)
ccc4fc3d1   David Howells   FS-Cache: Impleme...
383
384
385
  {
  	struct fscache_object *object;
  	struct fscache_cache *cache;
ccc4fc3d1   David Howells   FS-Cache: Impleme...
386
387
388
  	int ret;
  
  	_enter("");
94d30ae90   David Howells   FS-Cache: Provide...
389
  	set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
  
  	/* now we need to see whether the backing objects for this cookie yet
  	 * exist, if not there'll be nothing to search */
  	down_read(&fscache_addremove_sem);
  
  	if (list_empty(&fscache_cache_list)) {
  		up_read(&fscache_addremove_sem);
  		_leave(" = 0 [no caches]");
  		return 0;
  	}
  
  	/* select a cache in which to store the object */
  	cache = fscache_select_cache_for_object(cookie->parent);
  	if (!cache) {
  		up_read(&fscache_addremove_sem);
  		fscache_stat(&fscache_n_acquires_no_cache);
  		_leave(" = -ENOMEDIUM [no cache]");
  		return -ENOMEDIUM;
  	}
  
  	_debug("cache %s", cache->tag->name);
94d30ae90   David Howells   FS-Cache: Provide...
411
  	set_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
412
413
414
415
416
417
418
419
420
  
  	/* ask the cache to allocate objects for this cookie and its parent
  	 * chain */
  	ret = fscache_alloc_object(cache, cookie);
  	if (ret < 0) {
  		up_read(&fscache_addremove_sem);
  		_leave(" = %d", ret);
  		return ret;
  	}
ccc4fc3d1   David Howells   FS-Cache: Impleme...
421
422
423
424
425
426
427
428
  	spin_lock(&cookie->lock);
  	if (hlist_empty(&cookie->backing_objects)) {
  		spin_unlock(&cookie->lock);
  		goto unavailable;
  	}
  
  	object = hlist_entry(cookie->backing_objects.first,
  			     struct fscache_object, cookie_link);
ee1235a9a   David Howells   fscache: Pass obj...
429
  	fscache_set_store_limit(object, object_size);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
430
431
432
  
  	/* initiate the process of looking up all the objects in the chain
  	 * (done by fscache_initialise_object()) */
caaef6900   David Howells   FS-Cache: Fix obj...
433
  	fscache_raise_event(object, FSCACHE_OBJECT_EV_NEW_CHILD);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
434
435
436
437
438
439
440
  
  	spin_unlock(&cookie->lock);
  
  	/* we may be required to wait for lookup to complete at this point */
  	if (!fscache_defer_lookup) {
  		_debug("non-deferred lookup %p", &cookie->flags);
  		wait_on_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP,
743162013   NeilBrown   sched: Remove pro...
441
  			    TASK_UNINTERRUPTIBLE);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
  		_debug("complete");
  		if (test_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags))
  			goto unavailable;
  	}
  
  	up_read(&fscache_addremove_sem);
  	_leave(" = 0 [deferred]");
  	return 0;
  
  unavailable:
  	up_read(&fscache_addremove_sem);
  	_leave(" = -ENOBUFS");
  	return -ENOBUFS;
  }
  
  /*
   * recursively allocate cache object records for a cookie/cache combination
   * - caller must be holding the addremove sem
   */
  static int fscache_alloc_object(struct fscache_cache *cache,
  				struct fscache_cookie *cookie)
  {
  	struct fscache_object *object;
ccc4fc3d1   David Howells   FS-Cache: Impleme...
465
466
467
468
469
  	int ret;
  
  	_enter("%p,%p{%s}", cache, cookie, cookie->def->name);
  
  	spin_lock(&cookie->lock);
b67bfe0d4   Sasha Levin   hlist: drop the n...
470
  	hlist_for_each_entry(object, &cookie->backing_objects,
ccc4fc3d1   David Howells   FS-Cache: Impleme...
471
472
473
474
475
476
477
478
  			     cookie_link) {
  		if (object->cache == cache)
  			goto object_already_extant;
  	}
  	spin_unlock(&cookie->lock);
  
  	/* ask the cache to allocate an object (we may end up with duplicate
  	 * objects at this stage, but we sort that out later) */
52bd75fdb   David Howells   FS-Cache: Add cou...
479
  	fscache_stat(&fscache_n_cop_alloc_object);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
480
  	object = cache->ops->alloc_object(cache, cookie);
52bd75fdb   David Howells   FS-Cache: Add cou...
481
  	fscache_stat_d(&fscache_n_cop_alloc_object);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
482
483
484
485
486
  	if (IS_ERR(object)) {
  		fscache_stat(&fscache_n_object_no_alloc);
  		ret = PTR_ERR(object);
  		goto error;
  	}
f29507ce6   Kiran Kumar Modukuri   fscache: Fix refe...
487
  	ASSERTCMP(object->cookie, ==, cookie);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
  	fscache_stat(&fscache_n_object_alloc);
  
  	object->debug_id = atomic_inc_return(&fscache_object_debug_id);
  
  	_debug("ALLOC OBJ%x: %s {%lx}",
  	       object->debug_id, cookie->def->name, object->events);
  
  	ret = fscache_alloc_object(cache, cookie->parent);
  	if (ret < 0)
  		goto error_put;
  
  	/* only attach if we managed to allocate all we needed, otherwise
  	 * discard the object we just allocated and instead use the one
  	 * attached to the cookie */
52bd75fdb   David Howells   FS-Cache: Add cou...
502
503
  	if (fscache_attach_object(cookie, object) < 0) {
  		fscache_stat(&fscache_n_cop_put_object);
a18feb557   David Howells   fscache: Add trac...
504
  		cache->ops->put_object(object, fscache_obj_put_attach_fail);
52bd75fdb   David Howells   FS-Cache: Add cou...
505
506
  		fscache_stat_d(&fscache_n_cop_put_object);
  	}
ccc4fc3d1   David Howells   FS-Cache: Impleme...
507
508
509
510
511
512
  
  	_leave(" = 0");
  	return 0;
  
  object_already_extant:
  	ret = -ENOBUFS;
870215263   David Howells   FS-Cache: fscache...
513
514
  	if (fscache_object_is_dying(object) ||
  	    fscache_cache_is_broken(object)) {
ccc4fc3d1   David Howells   FS-Cache: Impleme...
515
516
517
518
519
520
521
522
  		spin_unlock(&cookie->lock);
  		goto error;
  	}
  	spin_unlock(&cookie->lock);
  	_leave(" = 0 [found]");
  	return 0;
  
  error_put:
52bd75fdb   David Howells   FS-Cache: Add cou...
523
  	fscache_stat(&fscache_n_cop_put_object);
a18feb557   David Howells   fscache: Add trac...
524
  	cache->ops->put_object(object, fscache_obj_put_alloc_fail);
52bd75fdb   David Howells   FS-Cache: Add cou...
525
  	fscache_stat_d(&fscache_n_cop_put_object);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
526
527
528
529
530
531
532
533
534
535
536
537
538
  error:
  	_leave(" = %d", ret);
  	return ret;
  }
  
  /*
   * attach a cache object to a cookie
   */
  static int fscache_attach_object(struct fscache_cookie *cookie,
  				 struct fscache_object *object)
  {
  	struct fscache_object *p;
  	struct fscache_cache *cache = object->cache;
ccc4fc3d1   David Howells   FS-Cache: Impleme...
539
540
541
  	int ret;
  
  	_enter("{%s},{OBJ%x}", cookie->def->name, object->debug_id);
f29507ce6   Kiran Kumar Modukuri   fscache: Fix refe...
542
  	ASSERTCMP(object->cookie, ==, cookie);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
543
544
545
546
547
  	spin_lock(&cookie->lock);
  
  	/* there may be multiple initial creations of this object, but we only
  	 * want one */
  	ret = -EEXIST;
b67bfe0d4   Sasha Levin   hlist: drop the n...
548
  	hlist_for_each_entry(p, &cookie->backing_objects, cookie_link) {
ccc4fc3d1   David Howells   FS-Cache: Impleme...
549
  		if (p->cache == object->cache) {
493f7bc11   David Howells   FS-Cache: Wrap ch...
550
  			if (fscache_object_is_dying(p))
ccc4fc3d1   David Howells   FS-Cache: Impleme...
551
552
553
554
555
556
557
  				ret = -ENOBUFS;
  			goto cant_attach_object;
  		}
  	}
  
  	/* pin the parent object */
  	spin_lock_nested(&cookie->parent->lock, 1);
b67bfe0d4   Sasha Levin   hlist: drop the n...
558
  	hlist_for_each_entry(p, &cookie->parent->backing_objects,
ccc4fc3d1   David Howells   FS-Cache: Impleme...
559
560
  			     cookie_link) {
  		if (p->cache == object->cache) {
493f7bc11   David Howells   FS-Cache: Wrap ch...
561
  			if (fscache_object_is_dying(p)) {
ccc4fc3d1   David Howells   FS-Cache: Impleme...
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
  				ret = -ENOBUFS;
  				spin_unlock(&cookie->parent->lock);
  				goto cant_attach_object;
  			}
  			object->parent = p;
  			spin_lock(&p->lock);
  			p->n_children++;
  			spin_unlock(&p->lock);
  			break;
  		}
  	}
  	spin_unlock(&cookie->parent->lock);
  
  	/* attach to the cache's object list */
  	if (list_empty(&object->cache_link)) {
  		spin_lock(&cache->object_list_lock);
  		list_add(&object->cache_link, &cache->object_list);
  		spin_unlock(&cache->object_list_lock);
  	}
f29507ce6   Kiran Kumar Modukuri   fscache: Fix refe...
581
  	/* Attach to the cookie.  The object already has a ref on it. */
ccc4fc3d1   David Howells   FS-Cache: Impleme...
582
  	hlist_add_head(&object->cookie_link, &cookie->backing_objects);
4fbf4291a   David Howells   FS-Cache: Allow t...
583
584
  
  	fscache_objlist_add(object);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
585
586
587
588
589
590
591
592
593
  	ret = 0;
  
  cant_attach_object:
  	spin_unlock(&cookie->lock);
  	_leave(" = %d", ret);
  	return ret;
  }
  
  /*
ef778e7ae   David Howells   FS-Cache: Provide...
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
   * Invalidate an object.  Callable with spinlocks held.
   */
  void __fscache_invalidate(struct fscache_cookie *cookie)
  {
  	struct fscache_object *object;
  
  	_enter("{%s}", cookie->def->name);
  
  	fscache_stat(&fscache_n_invalidates);
  
  	/* Only permit invalidation of data files.  Invalidating an index will
  	 * require the caller to release all its attachments to the tree rooted
  	 * there, and if it's doing that, it may as well just retire the
  	 * cookie.
  	 */
402cb8dda   David Howells   fscache: Attach t...
609
  	ASSERTCMP(cookie->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
ef778e7ae   David Howells   FS-Cache: Provide...
610
611
612
613
614
615
  
  	/* If there's an object, we tell the object state machine to handle the
  	 * invalidation on our behalf, otherwise there's nothing to do.
  	 */
  	if (!hlist_empty(&cookie->backing_objects)) {
  		spin_lock(&cookie->lock);
94d30ae90   David Howells   FS-Cache: Provide...
616
617
  		if (fscache_cookie_enabled(cookie) &&
  		    !hlist_empty(&cookie->backing_objects) &&
ef778e7ae   David Howells   FS-Cache: Provide...
618
619
620
621
622
  		    !test_and_set_bit(FSCACHE_COOKIE_INVALIDATING,
  				      &cookie->flags)) {
  			object = hlist_entry(cookie->backing_objects.first,
  					     struct fscache_object,
  					     cookie_link);
493f7bc11   David Howells   FS-Cache: Wrap ch...
623
  			if (fscache_object_is_live(object))
ef778e7ae   David Howells   FS-Cache: Provide...
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
  				fscache_raise_event(
  					object, FSCACHE_OBJECT_EV_INVALIDATE);
  		}
  
  		spin_unlock(&cookie->lock);
  	}
  
  	_leave("");
  }
  EXPORT_SYMBOL(__fscache_invalidate);
  
  /*
   * Wait for object invalidation to complete.
   */
  void __fscache_wait_on_invalidate(struct fscache_cookie *cookie)
  {
  	_enter("%p", cookie);
  
  	wait_on_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING,
ef778e7ae   David Howells   FS-Cache: Provide...
643
644
645
646
647
648
649
  		    TASK_UNINTERRUPTIBLE);
  
  	_leave("");
  }
  EXPORT_SYMBOL(__fscache_wait_on_invalidate);
  
  /*
ccc4fc3d1   David Howells   FS-Cache: Impleme...
650
651
   * update the index entries backing a cookie
   */
402cb8dda   David Howells   fscache: Attach t...
652
  void __fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data)
ccc4fc3d1   David Howells   FS-Cache: Impleme...
653
654
  {
  	struct fscache_object *object;
ccc4fc3d1   David Howells   FS-Cache: Impleme...
655
656
657
658
659
660
661
662
663
664
  
  	fscache_stat(&fscache_n_updates);
  
  	if (!cookie) {
  		fscache_stat(&fscache_n_updates_null);
  		_leave(" [no cookie]");
  		return;
  	}
  
  	_enter("{%s}", cookie->def->name);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
665
  	spin_lock(&cookie->lock);
402cb8dda   David Howells   fscache: Attach t...
666
  	fscache_update_aux(cookie, aux_data);
94d30ae90   David Howells   FS-Cache: Provide...
667
668
669
670
671
672
673
674
  	if (fscache_cookie_enabled(cookie)) {
  		/* update the index entry on disk in each cache backing this
  		 * cookie.
  		 */
  		hlist_for_each_entry(object,
  				     &cookie->backing_objects, cookie_link) {
  			fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
  		}
ccc4fc3d1   David Howells   FS-Cache: Impleme...
675
676
677
678
679
680
681
682
  	}
  
  	spin_unlock(&cookie->lock);
  	_leave("");
  }
  EXPORT_SYMBOL(__fscache_update_cookie);
  
  /*
94d30ae90   David Howells   FS-Cache: Provide...
683
   * Disable a cookie to stop it from accepting new requests from the netfs.
ccc4fc3d1   David Howells   FS-Cache: Impleme...
684
   */
402cb8dda   David Howells   fscache: Attach t...
685
686
687
  void __fscache_disable_cookie(struct fscache_cookie *cookie,
  			      const void *aux_data,
  			      bool invalidate)
ccc4fc3d1   David Howells   FS-Cache: Impleme...
688
  {
ccc4fc3d1   David Howells   FS-Cache: Impleme...
689
  	struct fscache_object *object;
94d30ae90   David Howells   FS-Cache: Provide...
690
  	bool awaken = false;
ccc4fc3d1   David Howells   FS-Cache: Impleme...
691

94d30ae90   David Howells   FS-Cache: Provide...
692
  	_enter("%p,%u", cookie, invalidate);
1362729b1   David Howells   FS-Cache: Simplif...
693

a18feb557   David Howells   fscache: Add trac...
694
  	trace_fscache_disable(cookie);
1362729b1   David Howells   FS-Cache: Simplif...
695
  	ASSERTCMP(atomic_read(&cookie->n_active), >, 0);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
696
697
  
  	if (atomic_read(&cookie->n_children) != 0) {
36dfd116e   Fabian Frederick   fs/fscache: conve...
698
699
  		pr_err("Cookie '%s' still has children
  ",
ccc4fc3d1   David Howells   FS-Cache: Impleme...
700
701
702
  		       cookie->def->name);
  		BUG();
  	}
94d30ae90   David Howells   FS-Cache: Provide...
703
  	wait_on_bit_lock(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK,
743162013   NeilBrown   sched: Remove pro...
704
  			 TASK_UNINTERRUPTIBLE);
402cb8dda   David Howells   fscache: Attach t...
705
706
  
  	fscache_update_aux(cookie, aux_data);
94d30ae90   David Howells   FS-Cache: Provide...
707
708
709
710
711
712
713
714
715
716
  	if (!test_and_clear_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags))
  		goto out_unlock_enable;
  
  	/* If the cookie is being invalidated, wait for that to complete first
  	 * so that we can reuse the flag.
  	 */
  	__fscache_wait_on_invalidate(cookie);
  
  	/* Dispose of the backing objects */
  	set_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
717

ccc4fc3d1   David Howells   FS-Cache: Impleme...
718
  	spin_lock(&cookie->lock);
94d30ae90   David Howells   FS-Cache: Provide...
719
720
721
722
  	if (!hlist_empty(&cookie->backing_objects)) {
  		hlist_for_each_entry(object, &cookie->backing_objects, cookie_link) {
  			if (invalidate)
  				set_bit(FSCACHE_OBJECT_RETIRED, &object->flags);
6bdded59c   David Howells   fscache: Clear ou...
723
  			clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
94d30ae90   David Howells   FS-Cache: Provide...
724
725
726
727
728
  			fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
  		}
  	} else {
  		if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
  			awaken = true;
ccc4fc3d1   David Howells   FS-Cache: Impleme...
729
  	}
1362729b1   David Howells   FS-Cache: Simplif...
730
  	spin_unlock(&cookie->lock);
94d30ae90   David Howells   FS-Cache: Provide...
731
732
  	if (awaken)
  		wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
733

1362729b1   David Howells   FS-Cache: Simplif...
734
  	/* Wait for cessation of activity requiring access to the netfs (when
94d30ae90   David Howells   FS-Cache: Provide...
735
736
  	 * n_active reaches 0).  This makes sure outstanding reads and writes
  	 * have completed.
1362729b1   David Howells   FS-Cache: Simplif...
737
  	 */
dc5d4afbb   Peter Zijlstra   sched/wait, fs/fs...
738
739
740
741
  	if (!atomic_dec_and_test(&cookie->n_active)) {
  		wait_var_event(&cookie->n_active,
  			       !atomic_read(&cookie->n_active));
  	}
1362729b1   David Howells   FS-Cache: Simplif...
742

6bdded59c   David Howells   fscache: Clear ou...
743
  	/* Make sure any pending writes are cancelled. */
402cb8dda   David Howells   fscache: Attach t...
744
  	if (cookie->type != FSCACHE_COOKIE_TYPE_INDEX)
6bdded59c   David Howells   fscache: Clear ou...
745
  		fscache_invalidate_writes(cookie);
94d30ae90   David Howells   FS-Cache: Provide...
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
  	/* Reset the cookie state if it wasn't relinquished */
  	if (!test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags)) {
  		atomic_inc(&cookie->n_active);
  		set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
  	}
  
  out_unlock_enable:
  	clear_bit_unlock(FSCACHE_COOKIE_ENABLEMENT_LOCK, &cookie->flags);
  	wake_up_bit(&cookie->flags, FSCACHE_COOKIE_ENABLEMENT_LOCK);
  	_leave("");
  }
  EXPORT_SYMBOL(__fscache_disable_cookie);
  
  /*
   * release a cookie back to the cache
   * - the object will be marked as recyclable on disk if retire is true
   * - all dependents of this cookie must have already been unregistered
   *   (indices/files/pages)
   */
402cb8dda   David Howells   fscache: Attach t...
765
766
767
  void __fscache_relinquish_cookie(struct fscache_cookie *cookie,
  				 const void *aux_data,
  				 bool retire)
94d30ae90   David Howells   FS-Cache: Provide...
768
769
770
771
772
773
774
775
776
777
778
779
780
781
  {
  	fscache_stat(&fscache_n_relinquishes);
  	if (retire)
  		fscache_stat(&fscache_n_relinquishes_retire);
  
  	if (!cookie) {
  		fscache_stat(&fscache_n_relinquishes_null);
  		_leave(" [no cookie]");
  		return;
  	}
  
  	_enter("%p{%s,%p,%d},%d",
  	       cookie, cookie->def->name, cookie->netfs_data,
  	       atomic_read(&cookie->n_active), retire);
a18feb557   David Howells   fscache: Add trac...
782
  	trace_fscache_relinquish(cookie, retire);
94d30ae90   David Howells   FS-Cache: Provide...
783
  	/* No further netfs-accessing operations on this cookie permitted */
d0fb31ecd   David Howells   fscache: Detect m...
784
785
  	if (test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags))
  		BUG();
94d30ae90   David Howells   FS-Cache: Provide...
786

402cb8dda   David Howells   fscache: Attach t...
787
  	__fscache_disable_cookie(cookie, aux_data, retire);
94d30ae90   David Howells   FS-Cache: Provide...
788

1362729b1   David Howells   FS-Cache: Simplif...
789
  	/* Clear pointers back to the netfs */
7e311a207   David Howells   FS-Cache: Clear n...
790
791
  	cookie->netfs_data	= NULL;
  	cookie->def		= NULL;
e5a955419   Matthew Wilcox   fscache: use appr...
792
  	BUG_ON(!radix_tree_empty(&cookie->stores));
ccc4fc3d1   David Howells   FS-Cache: Impleme...
793
794
795
796
797
798
  
  	if (cookie->parent) {
  		ASSERTCMP(atomic_read(&cookie->parent->usage), >, 0);
  		ASSERTCMP(atomic_read(&cookie->parent->n_children), >, 0);
  		atomic_dec(&cookie->parent->n_children);
  	}
1362729b1   David Howells   FS-Cache: Simplif...
799
  	/* Dispose of the netfs's link to the cookie */
ccc4fc3d1   David Howells   FS-Cache: Impleme...
800
  	ASSERTCMP(atomic_read(&cookie->usage), >, 0);
a18feb557   David Howells   fscache: Add trac...
801
  	fscache_cookie_put(cookie, fscache_cookie_put_relinquish);
ccc4fc3d1   David Howells   FS-Cache: Impleme...
802
803
804
805
806
807
  
  	_leave("");
  }
  EXPORT_SYMBOL(__fscache_relinquish_cookie);
  
  /*
ec0328e46   David Howells   fscache: Maintain...
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
   * Remove a cookie from the hash table.
   */
  static void fscache_unhash_cookie(struct fscache_cookie *cookie)
  {
  	struct hlist_bl_head *h;
  	unsigned int bucket;
  
  	bucket = cookie->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
  	h = &fscache_cookie_hash[bucket];
  
  	hlist_bl_lock(h);
  	hlist_bl_del(&cookie->hash_link);
  	hlist_bl_unlock(h);
  }
  
  /*
a18feb557   David Howells   fscache: Add trac...
824
   * Drop a reference to a cookie.
955d00917   David Howells   FS-Cache: Provide...
825
   */
a18feb557   David Howells   fscache: Add trac...
826
827
  void fscache_cookie_put(struct fscache_cookie *cookie,
  			enum fscache_cookie_trace where)
955d00917   David Howells   FS-Cache: Provide...
828
829
  {
  	struct fscache_cookie *parent;
a18feb557   David Howells   fscache: Add trac...
830
  	int usage;
955d00917   David Howells   FS-Cache: Provide...
831
832
  
  	_enter("%p", cookie);
a18feb557   David Howells   fscache: Add trac...
833
834
835
836
837
838
839
  	do {
  		usage = atomic_dec_return(&cookie->usage);
  		trace_fscache_cookie(cookie, where, usage);
  
  		if (usage > 0)
  			return;
  		BUG_ON(usage < 0);
955d00917   David Howells   FS-Cache: Provide...
840
  		parent = cookie->parent;
ec0328e46   David Howells   fscache: Maintain...
841
842
  		fscache_unhash_cookie(cookie);
  		fscache_free_cookie(cookie);
955d00917   David Howells   FS-Cache: Provide...
843

955d00917   David Howells   FS-Cache: Provide...
844
  		cookie = parent;
a18feb557   David Howells   fscache: Add trac...
845
846
  		where = fscache_cookie_put_parent;
  	} while (cookie);
955d00917   David Howells   FS-Cache: Provide...
847
848
849
  
  	_leave("");
  }
da9803bc8   David Howells   FS-Cache: Add int...
850
851
852
853
854
855
  
  /*
   * check the consistency between the netfs inode and the backing cache
   *
   * NOTE: it only serves no-index type
   */
402cb8dda   David Howells   fscache: Attach t...
856
857
  int __fscache_check_consistency(struct fscache_cookie *cookie,
  				const void *aux_data)
da9803bc8   David Howells   FS-Cache: Add int...
858
859
860
  {
  	struct fscache_operation *op;
  	struct fscache_object *object;
8fb883f3e   David Howells   FS-Cache: Add use...
861
  	bool wake_cookie = false;
da9803bc8   David Howells   FS-Cache: Add int...
862
863
864
  	int ret;
  
  	_enter("%p,", cookie);
402cb8dda   David Howells   fscache: Attach t...
865
  	ASSERTCMP(cookie->type, ==, FSCACHE_COOKIE_TYPE_DATAFILE);
da9803bc8   David Howells   FS-Cache: Add int...
866
867
868
869
870
871
872
873
874
875
  
  	if (fscache_wait_for_deferred_lookup(cookie) < 0)
  		return -ERESTARTSYS;
  
  	if (hlist_empty(&cookie->backing_objects))
  		return 0;
  
  	op = kzalloc(sizeof(*op), GFP_NOIO | __GFP_NOMEMALLOC | __GFP_NORETRY);
  	if (!op)
  		return -ENOMEM;
08c2e3d08   David Howells   fscache: Add more...
876
  	fscache_operation_init(cookie, op, NULL, NULL, NULL);
da9803bc8   David Howells   FS-Cache: Add int...
877
  	op->flags = FSCACHE_OP_MYTHREAD |
9c89d6294   Milosz Tanski   fscache: check co...
878
879
  		(1 << FSCACHE_OP_WAITING) |
  		(1 << FSCACHE_OP_UNUSE_COOKIE);
08c2e3d08   David Howells   fscache: Add more...
880
  	trace_fscache_page_op(cookie, NULL, op, fscache_page_op_check_consistency);
da9803bc8   David Howells   FS-Cache: Add int...
881
882
  
  	spin_lock(&cookie->lock);
402cb8dda   David Howells   fscache: Attach t...
883
  	fscache_update_aux(cookie, aux_data);
94d30ae90   David Howells   FS-Cache: Provide...
884
885
  	if (!fscache_cookie_enabled(cookie) ||
  	    hlist_empty(&cookie->backing_objects))
da9803bc8   David Howells   FS-Cache: Add int...
886
887
888
889
890
891
892
  		goto inconsistent;
  	object = hlist_entry(cookie->backing_objects.first,
  			     struct fscache_object, cookie_link);
  	if (test_bit(FSCACHE_IOERROR, &object->cache->flags))
  		goto inconsistent;
  
  	op->debug_id = atomic_inc_return(&fscache_op_debug_id);
8fb883f3e   David Howells   FS-Cache: Add use...
893
  	__fscache_use_cookie(cookie);
da9803bc8   David Howells   FS-Cache: Add int...
894
895
896
897
898
  	if (fscache_submit_op(object, op) < 0)
  		goto submit_failed;
  
  	/* the work queue now carries its own ref on the object */
  	spin_unlock(&cookie->lock);
d3b97ca4a   David Howells   FS-Cache: The ope...
899
  	ret = fscache_wait_for_operation_activation(object, op, NULL, NULL);
da9803bc8   David Howells   FS-Cache: Add int...
900
901
902
903
904
905
906
907
908
909
910
911
912
  	if (ret == 0) {
  		/* ask the cache to honour the operation */
  		ret = object->cache->ops->check_consistency(op);
  		fscache_op_complete(op, false);
  	} else if (ret == -ENOBUFS) {
  		ret = 0;
  	}
  
  	fscache_put_operation(op);
  	_leave(" = %d", ret);
  	return ret;
  
  submit_failed:
8fb883f3e   David Howells   FS-Cache: Add use...
913
  	wake_cookie = __fscache_unuse_cookie(cookie);
da9803bc8   David Howells   FS-Cache: Add int...
914
915
  inconsistent:
  	spin_unlock(&cookie->lock);
8fb883f3e   David Howells   FS-Cache: Add use...
916
917
  	if (wake_cookie)
  		__fscache_wake_unused_cookie(cookie);
da9803bc8   David Howells   FS-Cache: Add int...
918
919
920
921
922
  	kfree(op);
  	_leave(" = -ESTALE");
  	return -ESTALE;
  }
  EXPORT_SYMBOL(__fscache_check_consistency);