Blame view

fs/fscache/cache.c 10.8 KB
0e04d4cef   David Howells   FS-Cache: Add cac...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /* FS-Cache cache handling
   *
   * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
   * Written by David Howells (dhowells@redhat.com)
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version
   * 2 of the License, or (at your option) any later version.
   */
  
  #define FSCACHE_DEBUG_LEVEL CACHE
  #include <linux/module.h>
  #include <linux/slab.h>
  #include "internal.h"
  
  LIST_HEAD(fscache_cache_list);
  DECLARE_RWSEM(fscache_addremove_sem);
4c515dd47   David Howells   FS-Cache: Add cac...
19
20
  DECLARE_WAIT_QUEUE_HEAD(fscache_cache_cleared_wq);
  EXPORT_SYMBOL(fscache_cache_cleared_wq);
0e04d4cef   David Howells   FS-Cache: Add cac...
21
22
23
24
25
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  
  static LIST_HEAD(fscache_cache_tag_list);
  
  /*
   * look up a cache tag
   */
  struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *name)
  {
  	struct fscache_cache_tag *tag, *xtag;
  
  	/* firstly check for the existence of the tag under read lock */
  	down_read(&fscache_addremove_sem);
  
  	list_for_each_entry(tag, &fscache_cache_tag_list, link) {
  		if (strcmp(tag->name, name) == 0) {
  			atomic_inc(&tag->usage);
  			up_read(&fscache_addremove_sem);
  			return tag;
  		}
  	}
  
  	up_read(&fscache_addremove_sem);
  
  	/* the tag does not exist - create a candidate */
  	xtag = kzalloc(sizeof(*xtag) + strlen(name) + 1, GFP_KERNEL);
  	if (!xtag)
  		/* return a dummy tag if out of memory */
  		return ERR_PTR(-ENOMEM);
  
  	atomic_set(&xtag->usage, 1);
  	strcpy(xtag->name, name);
  
  	/* write lock, search again and add if still not present */
  	down_write(&fscache_addremove_sem);
  
  	list_for_each_entry(tag, &fscache_cache_tag_list, link) {
  		if (strcmp(tag->name, name) == 0) {
  			atomic_inc(&tag->usage);
  			up_write(&fscache_addremove_sem);
  			kfree(xtag);
  			return tag;
  		}
  	}
  
  	list_add_tail(&xtag->link, &fscache_cache_tag_list);
  	up_write(&fscache_addremove_sem);
  	return xtag;
  }
  
  /*
   * release a reference to a cache tag
   */
  void __fscache_release_cache_tag(struct fscache_cache_tag *tag)
  {
  	if (tag != ERR_PTR(-ENOMEM)) {
  		down_write(&fscache_addremove_sem);
  
  		if (atomic_dec_and_test(&tag->usage))
  			list_del_init(&tag->link);
  		else
  			tag = NULL;
  
  		up_write(&fscache_addremove_sem);
  
  		kfree(tag);
  	}
  }
  
  /*
   * select a cache in which to store an object
   * - the cache addremove semaphore must be at least read-locked by the caller
   * - the object will never be an index
   */
  struct fscache_cache *fscache_select_cache_for_object(
  	struct fscache_cookie *cookie)
  {
  	struct fscache_cache_tag *tag;
  	struct fscache_object *object;
  	struct fscache_cache *cache;
  
  	_enter("");
  
  	if (list_empty(&fscache_cache_list)) {
  		_leave(" = NULL [no cache]");
  		return NULL;
  	}
  
  	/* we check the parent to determine the cache to use */
  	spin_lock(&cookie->lock);
  
  	/* the first in the parent's backing list should be the preferred
  	 * cache */
  	if (!hlist_empty(&cookie->backing_objects)) {
  		object = hlist_entry(cookie->backing_objects.first,
  				     struct fscache_object, cookie_link);
  
  		cache = object->cache;
493f7bc11   David Howells   FS-Cache: Wrap ch...
118
  		if (fscache_object_is_dying(object) ||
0e04d4cef   David Howells   FS-Cache: Add cac...
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
  		    test_bit(FSCACHE_IOERROR, &cache->flags))
  			cache = NULL;
  
  		spin_unlock(&cookie->lock);
  		_leave(" = %p [parent]", cache);
  		return cache;
  	}
  
  	/* the parent is unbacked */
  	if (cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) {
  		/* cookie not an index and is unbacked */
  		spin_unlock(&cookie->lock);
  		_leave(" = NULL [cookie ub,ni]");
  		return NULL;
  	}
  
  	spin_unlock(&cookie->lock);
  
  	if (!cookie->def->select_cache)
  		goto no_preference;
  
  	/* ask the netfs for its preference */
  	tag = cookie->def->select_cache(cookie->parent->netfs_data,
  					cookie->netfs_data);
  	if (!tag)
  		goto no_preference;
  
  	if (tag == ERR_PTR(-ENOMEM)) {
  		_leave(" = NULL [nomem tag]");
  		return NULL;
  	}
  
  	if (!tag->cache) {
  		_leave(" = NULL [unbacked tag]");
  		return NULL;
  	}
  
  	if (test_bit(FSCACHE_IOERROR, &tag->cache->flags))
  		return NULL;
  
  	_leave(" = %p [specific]", tag->cache);
  	return tag->cache;
  
  no_preference:
  	/* netfs has no preference - just select first cache */
  	cache = list_entry(fscache_cache_list.next,
  			   struct fscache_cache, link);
  	_leave(" = %p [first]", cache);
  	return cache;
  }
4c515dd47   David Howells   FS-Cache: Add cac...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  
  /**
   * fscache_init_cache - Initialise a cache record
   * @cache: The cache record to be initialised
   * @ops: The cache operations to be installed in that record
   * @idfmt: Format string to define identifier
   * @...: sprintf-style arguments
   *
   * Initialise a record of a cache and fill in the name.
   *
   * See Documentation/filesystems/caching/backend-api.txt for a complete
   * description.
   */
  void fscache_init_cache(struct fscache_cache *cache,
  			const struct fscache_cache_ops *ops,
  			const char *idfmt,
  			...)
  {
  	va_list va;
  
  	memset(cache, 0, sizeof(*cache));
  
  	cache->ops = ops;
  
  	va_start(va, idfmt);
  	vsnprintf(cache->identifier, sizeof(cache->identifier), idfmt, va);
  	va_end(va);
952efe7b7   David Howells   FS-Cache: Add and...
196
  	INIT_WORK(&cache->op_gc, fscache_operation_gc);
4c515dd47   David Howells   FS-Cache: Add cac...
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
  	INIT_LIST_HEAD(&cache->link);
  	INIT_LIST_HEAD(&cache->object_list);
  	INIT_LIST_HEAD(&cache->op_gc_list);
  	spin_lock_init(&cache->object_list_lock);
  	spin_lock_init(&cache->op_gc_list_lock);
  }
  EXPORT_SYMBOL(fscache_init_cache);
  
  /**
   * fscache_add_cache - Declare a cache as being open for business
   * @cache: The record describing the cache
   * @ifsdef: The record of the cache object describing the top-level index
   * @tagname: The tag describing this cache
   *
   * Add a cache to the system, making it available for netfs's to use.
   *
   * See Documentation/filesystems/caching/backend-api.txt for a complete
   * description.
   */
  int fscache_add_cache(struct fscache_cache *cache,
  		      struct fscache_object *ifsdef,
  		      const char *tagname)
  {
  	struct fscache_cache_tag *tag;
  
  	BUG_ON(!cache->ops);
  	BUG_ON(!ifsdef);
  
  	cache->flags = 0;
caaef6900   David Howells   FS-Cache: Fix obj...
226
227
228
229
  	ifsdef->event_mask =
  		((1 << NR_FSCACHE_OBJECT_EVENTS) - 1) &
  		~(1 << FSCACHE_OBJECT_EV_CLEARED);
  	__set_bit(FSCACHE_OBJECT_IS_AVAILABLE, &ifsdef->flags);
4c515dd47   David Howells   FS-Cache: Add cac...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  
  	if (!tagname)
  		tagname = cache->identifier;
  
  	BUG_ON(!tagname[0]);
  
  	_enter("{%s.%s},,%s", cache->ops->name, cache->identifier, tagname);
  
  	/* we use the cache tag to uniquely identify caches */
  	tag = __fscache_lookup_cache_tag(tagname);
  	if (IS_ERR(tag))
  		goto nomem;
  
  	if (test_and_set_bit(FSCACHE_TAG_RESERVED, &tag->flags))
  		goto tag_in_use;
  
  	cache->kobj = kobject_create_and_add(tagname, fscache_root);
  	if (!cache->kobj)
  		goto error;
  
  	ifsdef->cookie = &fscache_fsdef_index;
  	ifsdef->cache = cache;
  	cache->fsdef = ifsdef;
  
  	down_write(&fscache_addremove_sem);
  
  	tag->cache = cache;
  	cache->tag = tag;
  
  	/* add the cache to the list */
  	list_add(&cache->link, &fscache_cache_list);
  
  	/* add the cache's netfs definition index object to the cache's
  	 * list */
  	spin_lock(&cache->object_list_lock);
  	list_add_tail(&ifsdef->cache_link, &cache->object_list);
  	spin_unlock(&cache->object_list_lock);
4fbf4291a   David Howells   FS-Cache: Allow t...
267
  	fscache_objlist_add(ifsdef);
4c515dd47   David Howells   FS-Cache: Add cac...
268
269
270
271
272
273
274
275
276
277
278
279
280
  
  	/* add the cache's netfs definition index object to the top level index
  	 * cookie as a known backing object */
  	spin_lock(&fscache_fsdef_index.lock);
  
  	hlist_add_head(&ifsdef->cookie_link,
  		       &fscache_fsdef_index.backing_objects);
  
  	atomic_inc(&fscache_fsdef_index.usage);
  
  	/* done */
  	spin_unlock(&fscache_fsdef_index.lock);
  	up_write(&fscache_addremove_sem);
36dfd116e   Fabian Frederick   fs/fscache: conve...
281
282
283
  	pr_notice("Cache \"%s\" added (type %s)
  ",
  		  cache->tag->name, cache->ops->name);
4c515dd47   David Howells   FS-Cache: Add cac...
284
285
286
287
288
289
  	kobject_uevent(cache->kobj, KOBJ_ADD);
  
  	_leave(" = 0 [%s]", cache->identifier);
  	return 0;
  
  tag_in_use:
36dfd116e   Fabian Frederick   fs/fscache: conve...
290
291
  	pr_err("Cache tag '%s' already in use
  ", tagname);
4c515dd47   David Howells   FS-Cache: Add cac...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
  	__fscache_release_cache_tag(tag);
  	_leave(" = -EXIST");
  	return -EEXIST;
  
  error:
  	__fscache_release_cache_tag(tag);
  	_leave(" = -EINVAL");
  	return -EINVAL;
  
  nomem:
  	_leave(" = -ENOMEM");
  	return -ENOMEM;
  }
  EXPORT_SYMBOL(fscache_add_cache);
  
  /**
   * fscache_io_error - Note a cache I/O error
   * @cache: The record describing the cache
   *
   * Note that an I/O error occurred in a cache and that it should no longer be
   * used for anything.  This also reports the error into the kernel log.
   *
   * See Documentation/filesystems/caching/backend-api.txt for a complete
   * description.
   */
  void fscache_io_error(struct fscache_cache *cache)
  {
75bc41138   David Howells   FS-Cache: Limit t...
319
  	if (!test_and_set_bit(FSCACHE_IOERROR, &cache->flags))
36dfd116e   Fabian Frederick   fs/fscache: conve...
320
321
  		pr_err("Cache '%s' stopped due to I/O error
  ",
75bc41138   David Howells   FS-Cache: Limit t...
322
  		       cache->ops->name);
4c515dd47   David Howells   FS-Cache: Add cac...
323
324
325
326
327
328
329
330
331
332
333
  }
  EXPORT_SYMBOL(fscache_io_error);
  
  /*
   * request withdrawal of all the objects in a cache
   * - all the objects being withdrawn are moved onto the supplied list
   */
  static void fscache_withdraw_all_objects(struct fscache_cache *cache,
  					 struct list_head *dying_objects)
  {
  	struct fscache_object *object;
4c515dd47   David Howells   FS-Cache: Add cac...
334
  	while (!list_empty(&cache->object_list)) {
caaef6900   David Howells   FS-Cache: Fix obj...
335
  		spin_lock(&cache->object_list_lock);
4c515dd47   David Howells   FS-Cache: Add cac...
336

caaef6900   David Howells   FS-Cache: Fix obj...
337
338
339
340
  		if (!list_empty(&cache->object_list)) {
  			object = list_entry(cache->object_list.next,
  					    struct fscache_object, cache_link);
  			list_move_tail(&object->cache_link, dying_objects);
4c515dd47   David Howells   FS-Cache: Add cac...
341

caaef6900   David Howells   FS-Cache: Fix obj...
342
343
344
345
346
347
348
  			_debug("withdraw %p", object->cookie);
  
  			/* This must be done under object_list_lock to prevent
  			 * a race with fscache_drop_object().
  			 */
  			fscache_raise_event(object, FSCACHE_OBJECT_EV_KILL);
  		}
4c515dd47   David Howells   FS-Cache: Add cac...
349

caaef6900   David Howells   FS-Cache: Fix obj...
350
  		spin_unlock(&cache->object_list_lock);
4c515dd47   David Howells   FS-Cache: Add cac...
351
  		cond_resched();
4c515dd47   David Howells   FS-Cache: Add cac...
352
  	}
4c515dd47   David Howells   FS-Cache: Add cac...
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  }
  
  /**
   * fscache_withdraw_cache - Withdraw a cache from the active service
   * @cache: The record describing the cache
   *
   * Withdraw a cache from service, unbinding all its cache objects from the
   * netfs cookies they're currently representing.
   *
   * See Documentation/filesystems/caching/backend-api.txt for a complete
   * description.
   */
  void fscache_withdraw_cache(struct fscache_cache *cache)
  {
  	LIST_HEAD(dying_objects);
  
  	_enter("");
36dfd116e   Fabian Frederick   fs/fscache: conve...
370
371
372
  	pr_notice("Withdrawing cache \"%s\"
  ",
  		  cache->tag->name);
4c515dd47   David Howells   FS-Cache: Add cac...
373
374
375
376
377
378
379
380
381
382
383
384
  
  	/* make the cache unavailable for cookie acquisition */
  	if (test_and_set_bit(FSCACHE_CACHE_WITHDRAWN, &cache->flags))
  		BUG();
  
  	down_write(&fscache_addremove_sem);
  	list_del_init(&cache->link);
  	cache->tag->cache = NULL;
  	up_write(&fscache_addremove_sem);
  
  	/* make sure all pages pinned by operations on behalf of the netfs are
  	 * written to disk */
52bd75fdb   David Howells   FS-Cache: Add cou...
385
  	fscache_stat(&fscache_n_cop_sync_cache);
4c515dd47   David Howells   FS-Cache: Add cac...
386
  	cache->ops->sync_cache(cache);
52bd75fdb   David Howells   FS-Cache: Add cou...
387
  	fscache_stat_d(&fscache_n_cop_sync_cache);
4c515dd47   David Howells   FS-Cache: Add cac...
388
389
390
  
  	/* dissociate all the netfs pages backed by this cache from the block
  	 * mappings in the cache */
52bd75fdb   David Howells   FS-Cache: Add cou...
391
  	fscache_stat(&fscache_n_cop_dissociate_pages);
4c515dd47   David Howells   FS-Cache: Add cac...
392
  	cache->ops->dissociate_pages(cache);
52bd75fdb   David Howells   FS-Cache: Add cou...
393
  	fscache_stat_d(&fscache_n_cop_dissociate_pages);
4c515dd47   David Howells   FS-Cache: Add cac...
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
  
  	/* we now have to destroy all the active objects pertaining to this
  	 * cache - which we do by passing them off to thread pool to be
  	 * disposed of */
  	_debug("destroy");
  
  	fscache_withdraw_all_objects(cache, &dying_objects);
  
  	/* wait for all extant objects to finish their outstanding operations
  	 * and go away */
  	_debug("wait for finish");
  	wait_event(fscache_cache_cleared_wq,
  		   atomic_read(&cache->object_count) == 0);
  	_debug("wait for clearance");
  	wait_event(fscache_cache_cleared_wq,
  		   list_empty(&cache->object_list));
  	_debug("cleared");
  	ASSERT(list_empty(&dying_objects));
  
  	kobject_put(cache->kobj);
  
  	clear_bit(FSCACHE_TAG_RESERVED, &cache->tag->flags);
  	fscache_release_cache_tag(cache->tag);
  	cache->tag = NULL;
  
  	_leave("");
  }
  EXPORT_SYMBOL(fscache_withdraw_cache);