1 /* fscache-cache.h: general filesystem caching backing cache interface
3 * Copyright (C) 2004-6 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
13 * Documentation/filesystems/caching/backend-api.txt
15 * for a description of the cache backend interface declared here.
18 #ifndef _LINUX_FSCACHE_CACHE_H
19 #define _LINUX_FSCACHE_CACHE_H
21 #include <linux/fscache.h>
23 #define NR_MAXCACHES BITS_PER_LONG
26 struct fscache_cache_ops;
27 struct fscache_object;
30 * cache tag definition
32 struct fscache_cache_tag {
33 struct list_head link;
34 struct fscache_cache *cache; /* cache referred to by this tag */
36 char name[0]; /* tag name */
42 struct fscache_cache {
43 struct fscache_cache_ops *ops;
44 struct fscache_cache_tag *tag; /* tag representing this cache */
45 struct list_head link; /* link in list of caches */
46 struct rw_semaphore withdrawal_sem; /* withdrawal control sem */
47 size_t max_index_size; /* maximum size of index data */
48 char identifier[32]; /* cache label */
51 struct list_head object_list; /* list of data/index objects */
52 spinlock_t object_list_lock;
53 struct fscache_object *fsdef; /* object for the fsdef index */
55 #define FSCACHE_IOERROR 0 /* cache stopped on I/O error */
58 extern void fscache_init_cache(struct fscache_cache *cache,
59 struct fscache_cache_ops *ops,
61 ...) __attribute__ ((format (printf,3,4)));
63 extern int fscache_add_cache(struct fscache_cache *cache,
64 struct fscache_object *fsdef,
66 extern void fscache_withdraw_cache(struct fscache_cache *cache);
68 extern void fscache_io_error(struct fscache_cache *cache);
70 /*****************************************************************************/
74 struct fscache_cache_ops {
75 /* name of cache provider */
78 /* look up the object for a cookie, creating it on disc if necessary */
79 struct fscache_object *(*lookup_object)(struct fscache_cache *cache,
80 struct fscache_object *parent,
81 struct fscache_cookie *cookie);
83 /* increment the usage count on this object (may fail if unmounting) */
84 struct fscache_object *(*grab_object)(struct fscache_object *object);
86 /* lock a semaphore on an object */
87 void (*lock_object)(struct fscache_object *object);
89 /* unlock a semaphore on an object */
90 void (*unlock_object)(struct fscache_object *object);
92 /* pin an object in the cache */
93 int (*pin_object)(struct fscache_object *object);
95 /* unpin an object in the cache */
96 void (*unpin_object)(struct fscache_object *object);
98 /* store the updated auxilliary data on an object */
99 void (*update_object)(struct fscache_object *object);
101 /* dispose of a reference to an object */
102 void (*put_object)(struct fscache_object *object);
105 void (*sync_cache)(struct fscache_cache *cache);
107 /* set the data size of an object */
108 int (*set_i_size)(struct fscache_object *object, loff_t i_size);
110 /* reserve space for an object's data and associated metadata */
111 int (*reserve_space)(struct fscache_object *object, loff_t i_size);
113 /* request a backing block for a page be read or allocated in the
115 int (*read_or_alloc_page)(struct fscache_object *object,
117 fscache_rw_complete_t end_io_func,
121 /* request backing blocks for a list of pages be read or allocated in
123 int (*read_or_alloc_pages)(struct fscache_object *object,
124 struct address_space *mapping,
125 struct list_head *pages,
127 fscache_rw_complete_t end_io_func,
131 /* request a backing block for a page be allocated in the cache so that
132 * it can be written directly */
133 int (*allocate_page)(struct fscache_object *object,
137 /* write a page to its backing block in the cache */
138 int (*write_page)(struct fscache_object *object,
140 fscache_rw_complete_t end_io_func,
144 /* write several pages to their backing blocks in the cache */
145 int (*write_pages)(struct fscache_object *object,
146 struct pagevec *pagevec,
147 fscache_rw_complete_t end_io_func,
151 /* detach backing block from a bunch of pages */
152 void (*uncache_pages)(struct fscache_object *object,
153 struct pagevec *pagevec);
155 /* dissociate a cache from all the pages it was backing */
156 void (*dissociate_pages)(struct fscache_cache *cache);
159 /*****************************************************************************/
161 * data file or index object cookie
162 * - a file will only appear in one cache
163 * - a request to cache a file may or may not be honoured, subject to
164 * constraints such as disc space
165 * - indexes files are created on disc just-in-time
167 struct fscache_cookie {
168 atomic_t usage; /* number of users of this cookie */
169 atomic_t children; /* number of children of this cookie */
170 struct rw_semaphore sem; /* list creation vs scan lock */
171 struct hlist_head backing_objects; /* object(s) backing this file/index */
172 struct fscache_cookie_def *def; /* definition */
173 struct fscache_cookie *parent; /* parent of this entry */
174 struct fscache_netfs *netfs; /* owner network fs definition */
175 void *netfs_data; /* back pointer to netfs */
178 extern struct fscache_cookie fscache_fsdef_index;
180 /*****************************************************************************/
182 * on-disc cache file or index handle
184 struct fscache_object {
186 #define FSCACHE_OBJECT_RELEASING 0 /* T if object is being released */
187 #define FSCACHE_OBJECT_RECYCLING 1 /* T if object is being retired */
188 #define FSCACHE_OBJECT_WITHDRAWN 2 /* T if object has been withdrawn */
190 struct list_head cache_link; /* link in cache->object_list */
191 struct hlist_node cookie_link; /* link in cookie->backing_objects */
192 struct fscache_cache *cache; /* cache that supplied this object */
193 struct fscache_cookie *cookie; /* netfs's file/index object */
197 void fscache_object_init(struct fscache_object *object)
200 INIT_LIST_HEAD(&object->cache_link);
201 INIT_HLIST_NODE(&object->cookie_link);
202 object->cache = NULL;
203 object->cookie = NULL;
206 /* find the parent index object for a object */
208 struct fscache_object *fscache_find_parent_object(struct fscache_object *object)
210 struct fscache_object *parent;
211 struct fscache_cookie *cookie = object->cookie;
212 struct fscache_cache *cache = object->cache;
213 struct hlist_node *_p;
215 hlist_for_each_entry(parent, _p,
216 &cookie->parent->backing_objects,
219 if (parent->cache == cache)
226 /* get an extra reference to a context */
228 void *fscache_get_context(struct fscache_cookie *cookie, void *context)
230 if (cookie->def->get_context)
231 cookie->def->get_context(cookie->netfs_data, context);
235 /* release an extra reference to a context */
237 void fscache_put_context(struct fscache_cookie *cookie, void *context)
239 if (cookie->def->put_context)
240 cookie->def->put_context(cookie->netfs_data, context);
243 #endif /* _LINUX_FSCACHE_CACHE_H */