linux/include/linux/fscache-cache.h
<<
>>
Prefs
   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>
  23#include <linux/workqueue.h>
  24
  25#define NR_MAXCACHES BITS_PER_LONG
  26
  27struct fscache_cache;
  28struct fscache_cache_ops;
  29struct fscache_object;
  30struct fscache_operation;
  31
  32enum 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
  44/*
  45 * cache tag definition
  46 */
  47struct 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 */
  59struct 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
  80extern 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 */
  87typedef void (*fscache_operation_release_t)(struct fscache_operation *op);
  88typedef void (*fscache_operation_processor_t)(struct fscache_operation *op);
  89typedef void (*fscache_operation_cancel_t)(struct fscache_operation *op);
  90
  91enum 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
 101struct fscache_operation {
 102        struct work_struct      work;           /* record for async ops */
 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 */
 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 */
 110#define FSCACHE_OP_WAITING      4       /* cleared when op is woken */
 111#define FSCACHE_OP_EXCLUSIVE    5       /* exclusive op, other ops must wait */
 112#define FSCACHE_OP_DEC_READ_CNT 6       /* decrement object->n_reads on destruction */
 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 */
 115
 116        enum fscache_operation_state state;
 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
 125        /* Operation cancellation cleanup (optional) */
 126        fscache_operation_cancel_t cancel;
 127
 128        /* operation releaser */
 129        fscache_operation_release_t release;
 130};
 131
 132extern atomic_t fscache_op_debug_id;
 133extern void fscache_op_work_func(struct work_struct *work);
 134
 135extern void fscache_enqueue_operation(struct fscache_operation *);
 136extern void fscache_op_complete(struct fscache_operation *, bool);
 137extern void fscache_put_operation(struct fscache_operation *);
 138extern void fscache_operation_init(struct fscache_cookie *,
 139                                   struct fscache_operation *,
 140                                   fscache_operation_processor_t,
 141                                   fscache_operation_cancel_t,
 142                                   fscache_operation_release_t);
 143
 144/*
 145 * data read operation
 146 */
 147struct fscache_retrieval {
 148        struct fscache_operation op;
 149        struct fscache_cookie   *cookie;        /* The netfs cookie */
 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 */
 155        atomic_t                n_pages;        /* number of pages to be retrieved */
 156};
 157
 158typedef int (*fscache_page_retrieval_func_t)(struct fscache_retrieval *op,
 159                                             struct page *page,
 160                                             gfp_t gfp);
 161
 162typedef 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 */
 173static inline
 174struct 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 */
 186static inline void fscache_enqueue_retrieval(struct fscache_retrieval *op)
 187{
 188        fscache_enqueue_operation(&op->op);
 189}
 190
 191/**
 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 */
 196static inline void fscache_retrieval_complete(struct fscache_retrieval *op,
 197                                              int n_pages)
 198{
 199        if (atomic_sub_return_relaxed(n_pages, &op->n_pages) <= 0)
 200                fscache_op_complete(&op->op, false);
 201}
 202
 203/**
 204 * fscache_put_retrieval - Drop a reference to a retrieval operation
 205 * @op: The retrieval operation affected
 206 *
 207 * Drop a reference to a retrieval operation.
 208 */
 209static inline void fscache_put_retrieval(struct fscache_retrieval *op)
 210{
 211        fscache_put_operation(&op->op);
 212}
 213
 214/*
 215 * cached page storage work item
 216 * - used to do three things:
 217 *   - batch writes to the cache
 218 *   - do cache writes asynchronously
 219 *   - defer writes until cache object lookup completion
 220 */
 221struct fscache_storage {
 222        struct fscache_operation op;
 223        pgoff_t                 store_limit;    /* don't write more than this */
 224};
 225
 226/*
 227 * cache operations
 228 */
 229struct fscache_cache_ops {
 230        /* name of cache provider */
 231        const char *name;
 232
 233        /* allocate an object record for a cookie */
 234        struct fscache_object *(*alloc_object)(struct fscache_cache *cache,
 235                                               struct fscache_cookie *cookie);
 236
 237        /* look up the object for a cookie
 238         * - return -ETIMEDOUT to be requeued
 239         */
 240        int (*lookup_object)(struct fscache_object *object);
 241
 242        /* finished looking up */
 243        void (*lookup_complete)(struct fscache_object *object);
 244
 245        /* increment the usage count on this object (may fail if unmounting) */
 246        struct fscache_object *(*grab_object)(struct fscache_object *object,
 247                                              enum fscache_obj_ref_trace why);
 248
 249        /* pin an object in the cache */
 250        int (*pin_object)(struct fscache_object *object);
 251
 252        /* unpin an object in the cache */
 253        void (*unpin_object)(struct fscache_object *object);
 254
 255        /* check the consistency between the backing cache and the FS-Cache
 256         * cookie */
 257        int (*check_consistency)(struct fscache_operation *op);
 258
 259        /* store the updated auxiliary data on an object */
 260        void (*update_object)(struct fscache_object *object);
 261
 262        /* Invalidate an object */
 263        void (*invalidate_object)(struct fscache_operation *op);
 264
 265        /* discard the resources pinned by an object and effect retirement if
 266         * necessary */
 267        void (*drop_object)(struct fscache_object *object);
 268
 269        /* dispose of a reference to an object */
 270        void (*put_object)(struct fscache_object *object,
 271                           enum fscache_obj_ref_trace why);
 272
 273        /* sync a cache */
 274        void (*sync_cache)(struct fscache_cache *cache);
 275
 276        /* notification that the attributes of a non-index object (such as
 277         * i_size) have changed */
 278        int (*attr_changed)(struct fscache_object *object);
 279
 280        /* reserve space for an object's data and associated metadata */
 281        int (*reserve_space)(struct fscache_object *object, loff_t i_size);
 282
 283        /* request a backing block for a page be read or allocated in the
 284         * cache */
 285        fscache_page_retrieval_func_t read_or_alloc_page;
 286
 287        /* request backing blocks for a list of pages be read or allocated in
 288         * the cache */
 289        fscache_pages_retrieval_func_t read_or_alloc_pages;
 290
 291        /* request a backing block for a page be allocated in the cache so that
 292         * it can be written directly */
 293        fscache_page_retrieval_func_t allocate_page;
 294
 295        /* request backing blocks for pages be allocated in the cache so that
 296         * they can be written directly */
 297        fscache_pages_retrieval_func_t allocate_pages;
 298
 299        /* write a page to its backing block in the cache */
 300        int (*write_page)(struct fscache_storage *op, struct page *page);
 301
 302        /* detach backing block from a page (optional)
 303         * - must release the cookie lock before returning
 304         * - may sleep
 305         */
 306        void (*uncache_page)(struct fscache_object *object,
 307                             struct page *page);
 308
 309        /* dissociate a cache from all the pages it was backing */
 310        void (*dissociate_pages)(struct fscache_cache *cache);
 311};
 312
 313extern struct fscache_cookie fscache_fsdef_index;
 314
 315/*
 316 * Event list for fscache_object::{event_mask,events}
 317 */
 318enum {
 319        FSCACHE_OBJECT_EV_NEW_CHILD,    /* T if object has a new child */
 320        FSCACHE_OBJECT_EV_PARENT_READY, /* T if object's parent is ready */
 321        FSCACHE_OBJECT_EV_UPDATE,       /* T if object should be updated */
 322        FSCACHE_OBJECT_EV_INVALIDATE,   /* T if cache requested object invalidation */
 323        FSCACHE_OBJECT_EV_CLEARED,      /* T if accessors all gone */
 324        FSCACHE_OBJECT_EV_ERROR,        /* T if fatal error occurred during processing */
 325        FSCACHE_OBJECT_EV_KILL,         /* T if netfs relinquished or cache withdrew object */
 326        NR_FSCACHE_OBJECT_EVENTS
 327};
 328
 329#define FSCACHE_OBJECT_EVENTS_MASK ((1UL << NR_FSCACHE_OBJECT_EVENTS) - 1)
 330
 331/*
 332 * States for object state machine.
 333 */
 334struct fscache_transition {
 335        unsigned long events;
 336        const struct fscache_state *transit_to;
 337};
 338
 339struct fscache_state {
 340        char name[24];
 341        char short_name[8];
 342        const struct fscache_state *(*work)(struct fscache_object *object,
 343                                            int event);
 344        const struct fscache_transition transitions[];
 345};
 346
 347/*
 348 * on-disk cache file or index handle
 349 */
 350struct fscache_object {
 351        const struct fscache_state *state;      /* Object state machine state */
 352        const struct fscache_transition *oob_table; /* OOB state transition table */
 353        int                     debug_id;       /* debugging ID */
 354        int                     n_children;     /* number of child objects */
 355        int                     n_ops;          /* number of extant ops on object */
 356        int                     n_obj_ops;      /* number of object ops outstanding on object */
 357        int                     n_in_progress;  /* number of ops in progress */
 358        int                     n_exclusive;    /* number of exclusive ops queued or in progress */
 359        atomic_t                n_reads;        /* number of read ops in progress */
 360        spinlock_t              lock;           /* state and operations lock */
 361
 362        unsigned long           lookup_jif;     /* time at which lookup started */
 363        unsigned long           oob_event_mask; /* OOB events this object is interested in */
 364        unsigned long           event_mask;     /* events this object is interested in */
 365        unsigned long           events;         /* events to be processed by this object
 366                                                 * (order is important - using fls) */
 367
 368        unsigned long           flags;
 369#define FSCACHE_OBJECT_LOCK             0       /* T if object is busy being processed */
 370#define FSCACHE_OBJECT_PENDING_WRITE    1       /* T if object has pending write */
 371#define FSCACHE_OBJECT_WAITING          2       /* T if object is waiting on its parent */
 372#define FSCACHE_OBJECT_IS_LIVE          3       /* T if object is not withdrawn or relinquished */
 373#define FSCACHE_OBJECT_IS_LOOKED_UP     4       /* T if object has been looked up */
 374#define FSCACHE_OBJECT_IS_AVAILABLE     5       /* T if object has become active */
 375#define FSCACHE_OBJECT_RETIRED          6       /* T if object was retired on relinquishment */
 376#define FSCACHE_OBJECT_KILLED_BY_CACHE  7       /* T if object was killed by the cache */
 377#define FSCACHE_OBJECT_RUN_AFTER_DEAD   8       /* T if object has been dispatched after death */
 378
 379        struct list_head        cache_link;     /* link in cache->object_list */
 380        struct hlist_node       cookie_link;    /* link in cookie->backing_objects */
 381        struct fscache_cache    *cache;         /* cache that supplied this object */
 382        struct fscache_cookie   *cookie;        /* netfs's file/index object */
 383        struct fscache_object   *parent;        /* parent object */
 384        struct work_struct      work;           /* attention scheduling record */
 385        struct list_head        dependents;     /* FIFO of dependent objects */
 386        struct list_head        dep_link;       /* link in parent's dependents list */
 387        struct list_head        pending_ops;    /* unstarted operations on this object */
 388#ifdef CONFIG_FSCACHE_OBJECT_LIST
 389        struct rb_node          objlist_link;   /* link in global object list */
 390#endif
 391        pgoff_t                 store_limit;    /* current storage limit */
 392        loff_t                  store_limit_l;  /* current storage limit */
 393};
 394
 395extern void fscache_object_init(struct fscache_object *, struct fscache_cookie *,
 396                                struct fscache_cache *);
 397extern void fscache_object_destroy(struct fscache_object *);
 398
 399extern void fscache_object_lookup_negative(struct fscache_object *object);
 400extern void fscache_obtained_object(struct fscache_object *object);
 401
 402static inline bool fscache_object_is_live(struct fscache_object *object)
 403{
 404        return test_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
 405}
 406
 407static inline bool fscache_object_is_dying(struct fscache_object *object)
 408{
 409        return !fscache_object_is_live(object);
 410}
 411
 412static inline bool fscache_object_is_available(struct fscache_object *object)
 413{
 414        return test_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
 415}
 416
 417static inline bool fscache_cache_is_broken(struct fscache_object *object)
 418{
 419        return test_bit(FSCACHE_IOERROR, &object->cache->flags);
 420}
 421
 422static inline bool fscache_object_is_active(struct fscache_object *object)
 423{
 424        return fscache_object_is_available(object) &&
 425                fscache_object_is_live(object) &&
 426                !fscache_cache_is_broken(object);
 427}
 428
 429/**
 430 * fscache_object_destroyed - Note destruction of an object in a cache
 431 * @cache: The cache from which the object came
 432 *
 433 * Note the destruction and deallocation of an object record in a cache.
 434 */
 435static inline void fscache_object_destroyed(struct fscache_cache *cache)
 436{
 437        if (atomic_dec_and_test(&cache->object_count))
 438                wake_up_all(&fscache_cache_cleared_wq);
 439}
 440
 441/**
 442 * fscache_object_lookup_error - Note an object encountered an error
 443 * @object: The object on which the error was encountered
 444 *
 445 * Note that an object encountered a fatal error (usually an I/O error) and
 446 * that it should be withdrawn as soon as possible.
 447 */
 448static inline void fscache_object_lookup_error(struct fscache_object *object)
 449{
 450        set_bit(FSCACHE_OBJECT_EV_ERROR, &object->events);
 451}
 452
 453/**
 454 * fscache_set_store_limit - Set the maximum size to be stored in an object
 455 * @object: The object to set the maximum on
 456 * @i_size: The limit to set in bytes
 457 *
 458 * Set the maximum size an object is permitted to reach, implying the highest
 459 * byte that may be written.  Intended to be called by the attr_changed() op.
 460 *
 461 * See Documentation/filesystems/caching/backend-api.txt for a complete
 462 * description.
 463 */
 464static inline
 465void fscache_set_store_limit(struct fscache_object *object, loff_t i_size)
 466{
 467        object->store_limit_l = i_size;
 468        object->store_limit = i_size >> PAGE_SHIFT;
 469        if (i_size & ~PAGE_MASK)
 470                object->store_limit++;
 471}
 472
 473/**
 474 * fscache_end_io - End a retrieval operation on a page
 475 * @op: The FS-Cache operation covering the retrieval
 476 * @page: The page that was to be fetched
 477 * @error: The error code (0 if successful)
 478 *
 479 * Note the end of an operation to retrieve a page, as covered by a particular
 480 * operation record.
 481 */
 482static inline void fscache_end_io(struct fscache_retrieval *op,
 483                                  struct page *page, int error)
 484{
 485        op->end_io_func(page, op->context, error);
 486}
 487
 488static inline void __fscache_use_cookie(struct fscache_cookie *cookie)
 489{
 490        atomic_inc(&cookie->n_active);
 491}
 492
 493/**
 494 * fscache_use_cookie - Request usage of cookie attached to an object
 495 * @object: Object description
 496 * 
 497 * Request usage of the cookie attached to an object.  NULL is returned if the
 498 * relinquishment had reduced the cookie usage count to 0.
 499 */
 500static inline bool fscache_use_cookie(struct fscache_object *object)
 501{
 502        struct fscache_cookie *cookie = object->cookie;
 503        return atomic_inc_not_zero(&cookie->n_active) != 0;
 504}
 505
 506static inline bool __fscache_unuse_cookie(struct fscache_cookie *cookie)
 507{
 508        return atomic_dec_and_test(&cookie->n_active);
 509}
 510
 511static inline void __fscache_wake_unused_cookie(struct fscache_cookie *cookie)
 512{
 513        wake_up_var(&cookie->n_active);
 514}
 515
 516/**
 517 * fscache_unuse_cookie - Cease usage of cookie attached to an object
 518 * @object: Object description
 519 * 
 520 * Cease usage of the cookie attached to an object.  When the users count
 521 * reaches zero then the cookie relinquishment will be permitted to proceed.
 522 */
 523static inline void fscache_unuse_cookie(struct fscache_object *object)
 524{
 525        struct fscache_cookie *cookie = object->cookie;
 526        if (__fscache_unuse_cookie(cookie))
 527                __fscache_wake_unused_cookie(cookie);
 528}
 529
 530/*
 531 * out-of-line cache backend functions
 532 */
 533extern __printf(3, 4)
 534void fscache_init_cache(struct fscache_cache *cache,
 535                        const struct fscache_cache_ops *ops,
 536                        const char *idfmt, ...);
 537
 538extern int fscache_add_cache(struct fscache_cache *cache,
 539                             struct fscache_object *fsdef,
 540                             const char *tagname);
 541extern void fscache_withdraw_cache(struct fscache_cache *cache);
 542
 543extern void fscache_io_error(struct fscache_cache *cache);
 544
 545extern void fscache_mark_page_cached(struct fscache_retrieval *op,
 546                                     struct page *page);
 547
 548extern void fscache_mark_pages_cached(struct fscache_retrieval *op,
 549                                      struct pagevec *pagevec);
 550
 551extern bool fscache_object_sleep_till_congested(signed long *timeoutp);
 552
 553extern enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
 554                                               const void *data,
 555                                               uint16_t datalen,
 556                                               loff_t object_size);
 557
 558extern void fscache_object_retrying_stale(struct fscache_object *object);
 559
 560enum fscache_why_object_killed {
 561        FSCACHE_OBJECT_IS_STALE,
 562        FSCACHE_OBJECT_NO_SPACE,
 563        FSCACHE_OBJECT_WAS_RETIRED,
 564        FSCACHE_OBJECT_WAS_CULLED,
 565};
 566extern void fscache_object_mark_killed(struct fscache_object *object,
 567                                       enum fscache_why_object_killed why);
 568
 569#endif /* _LINUX_FSCACHE_CACHE_H */
 570