David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 1 | /* General filesystem caching backing cache interface |
| 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. |
| 10 | * |
| 11 | * NOTE!!! See: |
| 12 | * |
| 13 | * Documentation/filesystems/caching/backend-api.txt |
| 14 | * |
| 15 | * for a description of the cache backend interface declared here. |
| 16 | */ |
| 17 | |
| 18 | #ifndef _LINUX_FSCACHE_CACHE_H |
| 19 | #define _LINUX_FSCACHE_CACHE_H |
| 20 | |
| 21 | #include <linux/fscache.h> |
| 22 | #include <linux/sched.h> |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 23 | #include <linux/workqueue.h> |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 24 | |
| 25 | #define NR_MAXCACHES BITS_PER_LONG |
| 26 | |
| 27 | struct fscache_cache; |
| 28 | struct fscache_cache_ops; |
| 29 | struct fscache_object; |
| 30 | struct fscache_operation; |
| 31 | |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 32 | enum fscache_obj_ref_trace { |
| 33 | fscache_obj_get_add_to_deps, |
| 34 | fscache_obj_get_queue, |
| 35 | fscache_obj_put_alloc_fail, |
| 36 | fscache_obj_put_attach_fail, |
| 37 | fscache_obj_put_drop_obj, |
| 38 | fscache_obj_put_enq_dep, |
| 39 | fscache_obj_put_queue, |
| 40 | fscache_obj_put_work, |
| 41 | fscache_obj_ref__nr_traces |
| 42 | }; |
| 43 | |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 44 | /* |
| 45 | * cache tag definition |
| 46 | */ |
| 47 | struct fscache_cache_tag { |
| 48 | struct list_head link; |
| 49 | struct fscache_cache *cache; /* cache referred to by this tag */ |
| 50 | unsigned long flags; |
| 51 | #define FSCACHE_TAG_RESERVED 0 /* T if tag is reserved for a cache */ |
| 52 | atomic_t usage; |
| 53 | char name[0]; /* tag name */ |
| 54 | }; |
| 55 | |
| 56 | /* |
| 57 | * cache definition |
| 58 | */ |
| 59 | struct fscache_cache { |
| 60 | const struct fscache_cache_ops *ops; |
| 61 | struct fscache_cache_tag *tag; /* tag representing this cache */ |
| 62 | struct kobject *kobj; /* system representation of this cache */ |
| 63 | struct list_head link; /* link in list of caches */ |
| 64 | size_t max_index_size; /* maximum size of index data */ |
| 65 | char identifier[36]; /* cache label */ |
| 66 | |
| 67 | /* node management */ |
| 68 | struct work_struct op_gc; /* operation garbage collector */ |
| 69 | struct list_head object_list; /* list of data/index objects */ |
| 70 | struct list_head op_gc_list; /* list of ops to be deleted */ |
| 71 | spinlock_t object_list_lock; |
| 72 | spinlock_t op_gc_list_lock; |
| 73 | atomic_t object_count; /* no. of live objects in this cache */ |
| 74 | struct fscache_object *fsdef; /* object for the fsdef index */ |
| 75 | unsigned long flags; |
| 76 | #define FSCACHE_IOERROR 0 /* cache stopped on I/O error */ |
| 77 | #define FSCACHE_CACHE_WITHDRAWN 1 /* cache has been withdrawn */ |
| 78 | }; |
| 79 | |
| 80 | extern wait_queue_head_t fscache_cache_cleared_wq; |
| 81 | |
| 82 | /* |
| 83 | * operation to be applied to a cache object |
| 84 | * - retrieval initiation operations are done in the context of the process |
| 85 | * that issued them, and not in an async thread pool |
| 86 | */ |
| 87 | typedef void (*fscache_operation_release_t)(struct fscache_operation *op); |
| 88 | typedef void (*fscache_operation_processor_t)(struct fscache_operation *op); |
David Howells | d3b97ca | 2015-02-24 10:05:29 +0000 | [diff] [blame] | 89 | typedef void (*fscache_operation_cancel_t)(struct fscache_operation *op); |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 90 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 91 | enum fscache_operation_state { |
| 92 | FSCACHE_OP_ST_BLANK, /* Op is not yet submitted */ |
| 93 | FSCACHE_OP_ST_INITIALISED, /* Op is initialised */ |
| 94 | FSCACHE_OP_ST_PENDING, /* Op is blocked from running */ |
| 95 | FSCACHE_OP_ST_IN_PROGRESS, /* Op is in progress */ |
| 96 | FSCACHE_OP_ST_COMPLETE, /* Op is complete */ |
| 97 | FSCACHE_OP_ST_CANCELLED, /* Op has been cancelled */ |
| 98 | FSCACHE_OP_ST_DEAD /* Op is now dead */ |
| 99 | }; |
| 100 | |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 101 | struct fscache_operation { |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 102 | struct work_struct work; /* record for async ops */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 103 | struct list_head pend_link; /* link in object->pending_ops */ |
| 104 | struct fscache_object *object; /* object to be operated upon */ |
| 105 | |
| 106 | unsigned long flags; |
| 107 | #define FSCACHE_OP_TYPE 0x000f /* operation type */ |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 108 | #define FSCACHE_OP_ASYNC 0x0001 /* - async op, processor may sleep for disk */ |
| 109 | #define FSCACHE_OP_MYTHREAD 0x0002 /* - processing is done be issuing thread, not pool */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 110 | #define FSCACHE_OP_WAITING 4 /* cleared when op is woken */ |
| 111 | #define FSCACHE_OP_EXCLUSIVE 5 /* exclusive op, other ops must wait */ |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 112 | #define FSCACHE_OP_DEC_READ_CNT 6 /* decrement object->n_reads on destruction */ |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 113 | #define FSCACHE_OP_UNUSE_COOKIE 7 /* call fscache_unuse_cookie() on completion */ |
| 114 | #define FSCACHE_OP_KEEP_FLAGS 0x00f0 /* flags to keep when repurposing an op */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 115 | |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 116 | enum fscache_operation_state state; |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 117 | atomic_t usage; |
| 118 | unsigned debug_id; /* debugging ID */ |
| 119 | |
| 120 | /* operation processor callback |
| 121 | * - can be NULL if FSCACHE_OP_WAITING is going to be used to perform |
| 122 | * the op in a non-pool thread */ |
| 123 | fscache_operation_processor_t processor; |
| 124 | |
David Howells | d3b97ca | 2015-02-24 10:05:29 +0000 | [diff] [blame] | 125 | /* Operation cancellation cleanup (optional) */ |
| 126 | fscache_operation_cancel_t cancel; |
| 127 | |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 128 | /* operation releaser */ |
| 129 | fscache_operation_release_t release; |
| 130 | }; |
| 131 | |
| 132 | extern atomic_t fscache_op_debug_id; |
Tejun Heo | 8af7c12 | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 133 | extern void fscache_op_work_func(struct work_struct *work); |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 134 | |
| 135 | extern void fscache_enqueue_operation(struct fscache_operation *); |
David Howells | 1f372df | 2012-12-13 20:03:13 +0000 | [diff] [blame] | 136 | extern void fscache_op_complete(struct fscache_operation *, bool); |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 137 | extern void fscache_put_operation(struct fscache_operation *); |
David Howells | 08c2e3d | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 138 | extern void fscache_operation_init(struct fscache_cookie *, |
| 139 | struct fscache_operation *, |
David Howells | 1339ec9 | 2015-02-25 13:26:39 +0000 | [diff] [blame] | 140 | fscache_operation_processor_t, |
David Howells | d3b97ca | 2015-02-24 10:05:29 +0000 | [diff] [blame] | 141 | fscache_operation_cancel_t, |
David Howells | 1339ec9 | 2015-02-25 13:26:39 +0000 | [diff] [blame] | 142 | fscache_operation_release_t); |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 143 | |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 144 | /* |
| 145 | * data read operation |
| 146 | */ |
| 147 | struct fscache_retrieval { |
| 148 | struct fscache_operation op; |
David Howells | 4a47132 | 2015-02-24 10:05:29 +0000 | [diff] [blame] | 149 | struct fscache_cookie *cookie; /* The netfs cookie */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 150 | struct address_space *mapping; /* netfs pages */ |
| 151 | fscache_rw_complete_t end_io_func; /* function to call on I/O completion */ |
| 152 | void *context; /* netfs read context (pinned) */ |
| 153 | struct list_head to_do; /* list of things to be done by the backend */ |
| 154 | unsigned long start_time; /* time at which retrieval started */ |
David Howells | 1bb4b7f9 | 2013-05-21 13:44:15 +0100 | [diff] [blame] | 155 | atomic_t n_pages; /* number of pages to be retrieved */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | typedef int (*fscache_page_retrieval_func_t)(struct fscache_retrieval *op, |
| 159 | struct page *page, |
| 160 | gfp_t gfp); |
| 161 | |
| 162 | typedef int (*fscache_pages_retrieval_func_t)(struct fscache_retrieval *op, |
| 163 | struct list_head *pages, |
| 164 | unsigned *nr_pages, |
| 165 | gfp_t gfp); |
| 166 | |
| 167 | /** |
| 168 | * fscache_get_retrieval - Get an extra reference on a retrieval operation |
| 169 | * @op: The retrieval operation to get a reference on |
| 170 | * |
| 171 | * Get an extra reference on a retrieval operation. |
| 172 | */ |
| 173 | static inline |
| 174 | struct fscache_retrieval *fscache_get_retrieval(struct fscache_retrieval *op) |
| 175 | { |
| 176 | atomic_inc(&op->op.usage); |
| 177 | return op; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * fscache_enqueue_retrieval - Enqueue a retrieval operation for processing |
| 182 | * @op: The retrieval operation affected |
| 183 | * |
| 184 | * Enqueue a retrieval operation for processing by the FS-Cache thread pool. |
| 185 | */ |
| 186 | static inline void fscache_enqueue_retrieval(struct fscache_retrieval *op) |
| 187 | { |
| 188 | fscache_enqueue_operation(&op->op); |
| 189 | } |
| 190 | |
| 191 | /** |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 192 | * fscache_retrieval_complete - Record (partial) completion of a retrieval |
| 193 | * @op: The retrieval operation affected |
| 194 | * @n_pages: The number of pages to account for |
| 195 | */ |
| 196 | static inline void fscache_retrieval_complete(struct fscache_retrieval *op, |
| 197 | int n_pages) |
| 198 | { |
David Howells | 1bb4b7f9 | 2013-05-21 13:44:15 +0100 | [diff] [blame] | 199 | atomic_sub(n_pages, &op->n_pages); |
| 200 | if (atomic_read(&op->n_pages) <= 0) |
David Howells | b27ddd4 | 2018-04-04 13:41:26 +0100 | [diff] [blame] | 201 | fscache_op_complete(&op->op, false); |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 205 | * fscache_put_retrieval - Drop a reference to a retrieval operation |
| 206 | * @op: The retrieval operation affected |
| 207 | * |
| 208 | * Drop a reference to a retrieval operation. |
| 209 | */ |
| 210 | static inline void fscache_put_retrieval(struct fscache_retrieval *op) |
| 211 | { |
| 212 | fscache_put_operation(&op->op); |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * cached page storage work item |
| 217 | * - used to do three things: |
| 218 | * - batch writes to the cache |
| 219 | * - do cache writes asynchronously |
| 220 | * - defer writes until cache object lookup completion |
| 221 | */ |
| 222 | struct fscache_storage { |
| 223 | struct fscache_operation op; |
| 224 | pgoff_t store_limit; /* don't write more than this */ |
| 225 | }; |
| 226 | |
| 227 | /* |
| 228 | * cache operations |
| 229 | */ |
| 230 | struct fscache_cache_ops { |
| 231 | /* name of cache provider */ |
| 232 | const char *name; |
| 233 | |
| 234 | /* allocate an object record for a cookie */ |
| 235 | struct fscache_object *(*alloc_object)(struct fscache_cache *cache, |
| 236 | struct fscache_cookie *cookie); |
| 237 | |
David Howells | fee096d | 2009-11-19 18:12:05 +0000 | [diff] [blame] | 238 | /* look up the object for a cookie |
| 239 | * - return -ETIMEDOUT to be requeued |
| 240 | */ |
| 241 | int (*lookup_object)(struct fscache_object *object); |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 242 | |
| 243 | /* finished looking up */ |
| 244 | void (*lookup_complete)(struct fscache_object *object); |
| 245 | |
| 246 | /* increment the usage count on this object (may fail if unmounting) */ |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 247 | struct fscache_object *(*grab_object)(struct fscache_object *object, |
| 248 | enum fscache_obj_ref_trace why); |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 249 | |
| 250 | /* pin an object in the cache */ |
| 251 | int (*pin_object)(struct fscache_object *object); |
| 252 | |
| 253 | /* unpin an object in the cache */ |
| 254 | void (*unpin_object)(struct fscache_object *object); |
| 255 | |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 256 | /* check the consistency between the backing cache and the FS-Cache |
| 257 | * cookie */ |
Yan, Zheng | 480ce08 | 2016-05-20 18:32:31 +0800 | [diff] [blame] | 258 | int (*check_consistency)(struct fscache_operation *op); |
David Howells | da9803b | 2013-08-21 17:29:38 -0400 | [diff] [blame] | 259 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 260 | /* store the updated auxiliary data on an object */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 261 | void (*update_object)(struct fscache_object *object); |
| 262 | |
David Howells | ef778e7 | 2012-12-20 21:52:36 +0000 | [diff] [blame] | 263 | /* Invalidate an object */ |
| 264 | void (*invalidate_object)(struct fscache_operation *op); |
| 265 | |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 266 | /* discard the resources pinned by an object and effect retirement if |
| 267 | * necessary */ |
| 268 | void (*drop_object)(struct fscache_object *object); |
| 269 | |
| 270 | /* dispose of a reference to an object */ |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 271 | void (*put_object)(struct fscache_object *object, |
| 272 | enum fscache_obj_ref_trace why); |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 273 | |
| 274 | /* sync a cache */ |
| 275 | void (*sync_cache)(struct fscache_cache *cache); |
| 276 | |
| 277 | /* notification that the attributes of a non-index object (such as |
| 278 | * i_size) have changed */ |
| 279 | int (*attr_changed)(struct fscache_object *object); |
| 280 | |
| 281 | /* reserve space for an object's data and associated metadata */ |
| 282 | int (*reserve_space)(struct fscache_object *object, loff_t i_size); |
| 283 | |
| 284 | /* request a backing block for a page be read or allocated in the |
| 285 | * cache */ |
| 286 | fscache_page_retrieval_func_t read_or_alloc_page; |
| 287 | |
| 288 | /* request backing blocks for a list of pages be read or allocated in |
| 289 | * the cache */ |
| 290 | fscache_pages_retrieval_func_t read_or_alloc_pages; |
| 291 | |
| 292 | /* request a backing block for a page be allocated in the cache so that |
| 293 | * it can be written directly */ |
| 294 | fscache_page_retrieval_func_t allocate_page; |
| 295 | |
| 296 | /* request backing blocks for pages be allocated in the cache so that |
| 297 | * they can be written directly */ |
| 298 | fscache_pages_retrieval_func_t allocate_pages; |
| 299 | |
| 300 | /* write a page to its backing block in the cache */ |
| 301 | int (*write_page)(struct fscache_storage *op, struct page *page); |
| 302 | |
| 303 | /* detach backing block from a page (optional) |
| 304 | * - must release the cookie lock before returning |
| 305 | * - may sleep |
| 306 | */ |
| 307 | void (*uncache_page)(struct fscache_object *object, |
| 308 | struct page *page); |
| 309 | |
| 310 | /* dissociate a cache from all the pages it was backing */ |
| 311 | void (*dissociate_pages)(struct fscache_cache *cache); |
| 312 | }; |
| 313 | |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 314 | extern struct fscache_cookie fscache_fsdef_index; |
| 315 | |
| 316 | /* |
David Howells | 36a02de | 2012-12-05 13:34:46 +0000 | [diff] [blame] | 317 | * Event list for fscache_object::{event_mask,events} |
| 318 | */ |
| 319 | enum { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 320 | FSCACHE_OBJECT_EV_NEW_CHILD, /* T if object has a new child */ |
| 321 | FSCACHE_OBJECT_EV_PARENT_READY, /* T if object's parent is ready */ |
David Howells | 36a02de | 2012-12-05 13:34:46 +0000 | [diff] [blame] | 322 | FSCACHE_OBJECT_EV_UPDATE, /* T if object should be updated */ |
| 323 | FSCACHE_OBJECT_EV_INVALIDATE, /* T if cache requested object invalidation */ |
| 324 | FSCACHE_OBJECT_EV_CLEARED, /* T if accessors all gone */ |
| 325 | FSCACHE_OBJECT_EV_ERROR, /* T if fatal error occurred during processing */ |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 326 | FSCACHE_OBJECT_EV_KILL, /* T if netfs relinquished or cache withdrew object */ |
David Howells | 36a02de | 2012-12-05 13:34:46 +0000 | [diff] [blame] | 327 | NR_FSCACHE_OBJECT_EVENTS |
| 328 | }; |
| 329 | |
| 330 | #define FSCACHE_OBJECT_EVENTS_MASK ((1UL << NR_FSCACHE_OBJECT_EVENTS) - 1) |
| 331 | |
| 332 | /* |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 333 | * States for object state machine. |
| 334 | */ |
| 335 | struct fscache_transition { |
| 336 | unsigned long events; |
| 337 | const struct fscache_state *transit_to; |
| 338 | }; |
| 339 | |
| 340 | struct fscache_state { |
| 341 | char name[24]; |
| 342 | char short_name[8]; |
| 343 | const struct fscache_state *(*work)(struct fscache_object *object, |
| 344 | int event); |
| 345 | const struct fscache_transition transitions[]; |
| 346 | }; |
| 347 | |
| 348 | /* |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 349 | * on-disk cache file or index handle |
| 350 | */ |
| 351 | struct fscache_object { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 352 | const struct fscache_state *state; /* Object state machine state */ |
| 353 | const struct fscache_transition *oob_table; /* OOB state transition table */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 354 | int debug_id; /* debugging ID */ |
| 355 | int n_children; /* number of child objects */ |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 356 | int n_ops; /* number of extant ops on object */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 357 | int n_obj_ops; /* number of object ops outstanding on object */ |
| 358 | int n_in_progress; /* number of ops in progress */ |
David Howells | 9f10523 | 2012-12-20 21:52:35 +0000 | [diff] [blame] | 359 | int n_exclusive; /* number of exclusive ops queued or in progress */ |
David Howells | 4fbf429 | 2009-11-19 18:11:04 +0000 | [diff] [blame] | 360 | atomic_t n_reads; /* number of read ops in progress */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 361 | spinlock_t lock; /* state and operations lock */ |
| 362 | |
| 363 | unsigned long lookup_jif; /* time at which lookup started */ |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 364 | unsigned long oob_event_mask; /* OOB events this object is interested in */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 365 | unsigned long event_mask; /* events this object is interested in */ |
| 366 | unsigned long events; /* events to be processed by this object |
| 367 | * (order is important - using fls) */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 368 | |
| 369 | unsigned long flags; |
| 370 | #define FSCACHE_OBJECT_LOCK 0 /* T if object is busy being processed */ |
| 371 | #define FSCACHE_OBJECT_PENDING_WRITE 1 /* T if object has pending write */ |
| 372 | #define FSCACHE_OBJECT_WAITING 2 /* T if object is waiting on its parent */ |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 373 | #define FSCACHE_OBJECT_IS_LIVE 3 /* T if object is not withdrawn or relinquished */ |
| 374 | #define FSCACHE_OBJECT_IS_LOOKED_UP 4 /* T if object has been looked up */ |
| 375 | #define FSCACHE_OBJECT_IS_AVAILABLE 5 /* T if object has become active */ |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 376 | #define FSCACHE_OBJECT_RETIRED 6 /* T if object was retired on relinquishment */ |
David Howells | 182d919 | 2015-02-19 23:47:31 +0000 | [diff] [blame] | 377 | #define FSCACHE_OBJECT_KILLED_BY_CACHE 7 /* T if object was killed by the cache */ |
David Howells | e26bfeb | 2017-01-31 09:45:28 +0000 | [diff] [blame] | 378 | #define FSCACHE_OBJECT_RUN_AFTER_DEAD 8 /* T if object has been dispatched after death */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 379 | |
| 380 | struct list_head cache_link; /* link in cache->object_list */ |
| 381 | struct hlist_node cookie_link; /* link in cookie->backing_objects */ |
| 382 | struct fscache_cache *cache; /* cache that supplied this object */ |
| 383 | struct fscache_cookie *cookie; /* netfs's file/index object */ |
| 384 | struct fscache_object *parent; /* parent object */ |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 385 | struct work_struct work; /* attention scheduling record */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 386 | struct list_head dependents; /* FIFO of dependent objects */ |
| 387 | struct list_head dep_link; /* link in parent's dependents list */ |
| 388 | struct list_head pending_ops; /* unstarted operations on this object */ |
David Howells | 4fbf429 | 2009-11-19 18:11:04 +0000 | [diff] [blame] | 389 | #ifdef CONFIG_FSCACHE_OBJECT_LIST |
| 390 | struct rb_node objlist_link; /* link in global object list */ |
| 391 | #endif |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 392 | pgoff_t store_limit; /* current storage limit */ |
David Howells | a17754f | 2009-11-19 18:11:52 +0000 | [diff] [blame] | 393 | loff_t store_limit_l; /* current storage limit */ |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 394 | }; |
| 395 | |
David Howells | 610be24 | 2013-05-10 19:50:25 +0100 | [diff] [blame] | 396 | extern void fscache_object_init(struct fscache_object *, struct fscache_cookie *, |
| 397 | struct fscache_cache *); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 398 | extern void fscache_object_destroy(struct fscache_object *); |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 399 | |
| 400 | extern void fscache_object_lookup_negative(struct fscache_object *object); |
| 401 | extern void fscache_obtained_object(struct fscache_object *object); |
| 402 | |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 403 | static inline bool fscache_object_is_live(struct fscache_object *object) |
| 404 | { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 405 | return test_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags); |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static inline bool fscache_object_is_dying(struct fscache_object *object) |
| 409 | { |
| 410 | return !fscache_object_is_live(object); |
| 411 | } |
| 412 | |
| 413 | static inline bool fscache_object_is_available(struct fscache_object *object) |
| 414 | { |
David Howells | caaef69 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 415 | return test_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags); |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 416 | } |
| 417 | |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame] | 418 | static inline bool fscache_cache_is_broken(struct fscache_object *object) |
| 419 | { |
| 420 | return test_bit(FSCACHE_IOERROR, &object->cache->flags); |
| 421 | } |
| 422 | |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 423 | static inline bool fscache_object_is_active(struct fscache_object *object) |
| 424 | { |
| 425 | return fscache_object_is_available(object) && |
| 426 | fscache_object_is_live(object) && |
David Howells | 30ceec6 | 2015-02-24 10:05:27 +0000 | [diff] [blame] | 427 | !fscache_cache_is_broken(object); |
David Howells | 493f7bc | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 428 | } |
| 429 | |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 430 | /** |
| 431 | * fscache_object_destroyed - Note destruction of an object in a cache |
| 432 | * @cache: The cache from which the object came |
| 433 | * |
| 434 | * Note the destruction and deallocation of an object record in a cache. |
| 435 | */ |
| 436 | static inline void fscache_object_destroyed(struct fscache_cache *cache) |
| 437 | { |
| 438 | if (atomic_dec_and_test(&cache->object_count)) |
| 439 | wake_up_all(&fscache_cache_cleared_wq); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * fscache_object_lookup_error - Note an object encountered an error |
| 444 | * @object: The object on which the error was encountered |
| 445 | * |
| 446 | * Note that an object encountered a fatal error (usually an I/O error) and |
| 447 | * that it should be withdrawn as soon as possible. |
| 448 | */ |
| 449 | static inline void fscache_object_lookup_error(struct fscache_object *object) |
| 450 | { |
| 451 | set_bit(FSCACHE_OBJECT_EV_ERROR, &object->events); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * fscache_set_store_limit - Set the maximum size to be stored in an object |
| 456 | * @object: The object to set the maximum on |
| 457 | * @i_size: The limit to set in bytes |
| 458 | * |
| 459 | * Set the maximum size an object is permitted to reach, implying the highest |
| 460 | * byte that may be written. Intended to be called by the attr_changed() op. |
| 461 | * |
| 462 | * See Documentation/filesystems/caching/backend-api.txt for a complete |
| 463 | * description. |
| 464 | */ |
| 465 | static inline |
| 466 | void fscache_set_store_limit(struct fscache_object *object, loff_t i_size) |
| 467 | { |
David Howells | a17754f | 2009-11-19 18:11:52 +0000 | [diff] [blame] | 468 | object->store_limit_l = i_size; |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 469 | object->store_limit = i_size >> PAGE_SHIFT; |
| 470 | if (i_size & ~PAGE_MASK) |
| 471 | object->store_limit++; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * fscache_end_io - End a retrieval operation on a page |
| 476 | * @op: The FS-Cache operation covering the retrieval |
| 477 | * @page: The page that was to be fetched |
| 478 | * @error: The error code (0 if successful) |
| 479 | * |
| 480 | * Note the end of an operation to retrieve a page, as covered by a particular |
| 481 | * operation record. |
| 482 | */ |
| 483 | static inline void fscache_end_io(struct fscache_retrieval *op, |
| 484 | struct page *page, int error) |
| 485 | { |
| 486 | op->end_io_func(page, op->context, error); |
| 487 | } |
| 488 | |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 489 | static inline void __fscache_use_cookie(struct fscache_cookie *cookie) |
| 490 | { |
| 491 | atomic_inc(&cookie->n_active); |
| 492 | } |
| 493 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 494 | /** |
| 495 | * fscache_use_cookie - Request usage of cookie attached to an object |
| 496 | * @object: Object description |
| 497 | * |
| 498 | * Request usage of the cookie attached to an object. NULL is returned if the |
| 499 | * relinquishment had reduced the cookie usage count to 0. |
| 500 | */ |
| 501 | static inline bool fscache_use_cookie(struct fscache_object *object) |
| 502 | { |
| 503 | struct fscache_cookie *cookie = object->cookie; |
| 504 | return atomic_inc_not_zero(&cookie->n_active) != 0; |
| 505 | } |
| 506 | |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 507 | static inline bool __fscache_unuse_cookie(struct fscache_cookie *cookie) |
| 508 | { |
| 509 | return atomic_dec_and_test(&cookie->n_active); |
| 510 | } |
| 511 | |
| 512 | static inline void __fscache_wake_unused_cookie(struct fscache_cookie *cookie) |
| 513 | { |
Peter Zijlstra | dc5d4afb | 2018-03-15 11:43:43 +0100 | [diff] [blame] | 514 | wake_up_var(&cookie->n_active); |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 515 | } |
| 516 | |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 517 | /** |
| 518 | * fscache_unuse_cookie - Cease usage of cookie attached to an object |
| 519 | * @object: Object description |
| 520 | * |
| 521 | * Cease usage of the cookie attached to an object. When the users count |
| 522 | * reaches zero then the cookie relinquishment will be permitted to proceed. |
| 523 | */ |
| 524 | static inline void fscache_unuse_cookie(struct fscache_object *object) |
| 525 | { |
| 526 | struct fscache_cookie *cookie = object->cookie; |
David Howells | 8fb883f | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 527 | if (__fscache_unuse_cookie(cookie)) |
| 528 | __fscache_wake_unused_cookie(cookie); |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 529 | } |
| 530 | |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 531 | /* |
| 532 | * out-of-line cache backend functions |
| 533 | */ |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 534 | extern __printf(3, 4) |
| 535 | void fscache_init_cache(struct fscache_cache *cache, |
| 536 | const struct fscache_cache_ops *ops, |
| 537 | const char *idfmt, ...); |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 538 | |
| 539 | extern int fscache_add_cache(struct fscache_cache *cache, |
| 540 | struct fscache_object *fsdef, |
| 541 | const char *tagname); |
| 542 | extern void fscache_withdraw_cache(struct fscache_cache *cache); |
| 543 | |
| 544 | extern void fscache_io_error(struct fscache_cache *cache); |
| 545 | |
David Howells | c4d6d8d | 2012-12-20 21:52:32 +0000 | [diff] [blame] | 546 | extern void fscache_mark_page_cached(struct fscache_retrieval *op, |
| 547 | struct page *page); |
| 548 | |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 549 | extern void fscache_mark_pages_cached(struct fscache_retrieval *op, |
| 550 | struct pagevec *pagevec); |
| 551 | |
Tejun Heo | 8b8edef | 2010-07-20 22:09:01 +0200 | [diff] [blame] | 552 | extern bool fscache_object_sleep_till_congested(signed long *timeoutp); |
| 553 | |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 554 | extern enum fscache_checkaux fscache_check_aux(struct fscache_object *object, |
| 555 | const void *data, |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame^] | 556 | uint16_t datalen, |
| 557 | loff_t object_size); |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 558 | |
David Howells | 182d919 | 2015-02-19 23:47:31 +0000 | [diff] [blame] | 559 | extern void fscache_object_retrying_stale(struct fscache_object *object); |
| 560 | |
| 561 | enum fscache_why_object_killed { |
| 562 | FSCACHE_OBJECT_IS_STALE, |
| 563 | FSCACHE_OBJECT_NO_SPACE, |
| 564 | FSCACHE_OBJECT_WAS_RETIRED, |
| 565 | FSCACHE_OBJECT_WAS_CULLED, |
| 566 | }; |
| 567 | extern void fscache_object_mark_killed(struct fscache_object *object, |
| 568 | enum fscache_why_object_killed why); |
| 569 | |
David Howells | 0dfc41d | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 570 | #endif /* _LINUX_FSCACHE_CACHE_H */ |