OpenCloudOS-Kernel/include/linux/fscache.h

549 lines
17 KiB
C
Raw Normal View History

FS-Cache: Add the FS-Cache netfs API and documentation Add the API for a generic facility (FS-Cache) by which filesystems (such as AFS or NFS) may call on local caching capabilities without having to know anything about how the cache works, or even if there is a cache: +---------+ | | +--------------+ | NFS |--+ | | | | | +-->| CacheFS | +---------+ | +----------+ | | /dev/hda5 | | | | | +--------------+ +---------+ +-->| | | | | | |--+ | AFS |----->| FS-Cache | | | | |--+ +---------+ +-->| | | | | | | +--------------+ +---------+ | +----------+ | | | | | | +-->| CacheFiles | | ISOFS |--+ | /var/cache | | | +--------------+ +---------+ General documentation and documentation of the netfs specific API are provided in addition to the header files. As this patch stands, it is possible to build a filesystem against the facility and attempt to use it. All that will happen is that all requests will be immediately denied as if no cache is present. Further patches will implement the core of the facility. The facility will transfer requests from networking filesystems to appropriate caches if possible, or else gracefully deny them. If this facility is disabled in the kernel configuration, then all its operations will trivially reduce to nothing during compilation. WHY NOT I_MAPPING? ================== I have added my own API to implement caching rather than using i_mapping to do this for a number of reasons. These have been discussed a lot on the LKML and CacheFS mailing lists, but to summarise the basics: (1) Most filesystems don't do hole reportage. Holes in files are treated as blocks of zeros and can't be distinguished otherwise, making it difficult to distinguish blocks that have been read from the network and cached from those that haven't. (2) The backing inode must be fully populated before being exposed to userspace through the main inode because the VM/VFS goes directly to the backing inode and does not interrogate the front inode's VM ops. Therefore: (a) The backing inode must fit entirely within the cache. (b) All backed files currently open must fit entirely within the cache at the same time. (c) A working set of files in total larger than the cache may not be cached. (d) A file may not grow larger than the available space in the cache. (e) A file that's open and cached, and remotely grows larger than the cache is potentially stuffed. (3) Writes go to the backing filesystem, and can only be transferred to the network when the file is closed. (4) There's no record of what changes have been made, so the whole file must be written back. (5) The pages belong to the backing filesystem, and all metadata associated with that page are relevant only to the backing filesystem, and not anything stacked atop it. OVERVIEW ======== FS-Cache provides (or will provide) the following facilities: (1) Caches can be added / removed at any time, even whilst in use. (2) Adds a facility by which tags can be used to refer to caches, even if they're not available yet. (3) More than one cache can be used at once. Caches can be selected explicitly by use of tags. (4) The netfs is provided with an interface that allows either party to withdraw caching facilities from a file (required for (1)). (5) A netfs may annotate cache objects that belongs to it. This permits the storage of coherency maintenance data. (6) Cache objects will be pinnable and space reservations will be possible. (7) The interface to the netfs returns as few errors as possible, preferring rather to let the netfs remain oblivious. (8) Cookies are used to represent indices, files and other objects to the netfs. The simplest cookie is just a NULL pointer - indicating nothing cached there. (9) The netfs is allowed to propose - dynamically - any index hierarchy it desires, though it must be aware that the index search function is recursive, stack space is limited, and indices can only be children of indices. (10) Indices can be used to group files together to reduce key size and to make group invalidation easier. The use of indices may make lookup quicker, but that's cache dependent. (11) Data I/O is effectively done directly to and from the netfs's pages. The netfs indicates that page A is at index B of the data-file represented by cookie C, and that it should be read or written. The cache backend may or may not start I/O on that page, but if it does, a netfs callback will be invoked to indicate completion. The I/O may be either synchronous or asynchronous. (12) Cookies can be "retired" upon release. At this point FS-Cache will mark them as obsolete and the index hierarchy rooted at that point will get recycled. (13) The netfs provides a "match" function for index searches. In addition to saying whether a match was made or not, this can also specify that an entry should be updated or deleted. FS-Cache maintains a virtual index tree in which all indices, files, objects and pages are kept. Bits of this tree may actually reside in one or more caches. FSDEF | +------------------------------------+ | | NFS AFS | | +--------------------------+ +-----------+ | | | | homedir mirror afs.org redhat.com | | | +------------+ +---------------+ +----------+ | | | | | | 00001 00002 00007 00125 vol00001 vol00002 | | | | | +---+---+ +-----+ +---+ +------+------+ +-----+----+ | | | | | | | | | | | | | PG0 PG1 PG2 PG0 XATTR PG0 PG1 DIRENT DIRENT DIRENT R/W R/O Bak | | PG0 +-------+ | | 00001 00003 | +---+---+ | | | PG0 PG1 PG2 In the example above, two netfs's can be seen to be backed: NFS and AFS. These have different index hierarchies: (*) The NFS primary index will probably contain per-server indices. Each server index is indexed by NFS file handles to get data file objects. Each data file objects can have an array of pages, but may also have further child objects, such as extended attributes and directory entries. Extended attribute objects themselves have page-array contents. (*) The AFS primary index contains per-cell indices. Each cell index contains per-logical-volume indices. Each of volume index contains up to three indices for the read-write, read-only and backup mirrors of those volumes. Each of these contains vnode data file objects, each of which contains an array of pages. The very top index is the FS-Cache master index in which individual netfs's have entries. Any index object may reside in more than one cache, provided it only has index children. Any index with non-index object children will be assumed to only reside in one cache. The FS-Cache overview can be found in: Documentation/filesystems/caching/fscache.txt The netfs API to FS-Cache can be found in: Documentation/filesystems/caching/netfs-api.txt Signed-off-by: David Howells <dhowells@redhat.com> Acked-by: Steve Dickson <steved@redhat.com> Acked-by: Trond Myklebust <Trond.Myklebust@netapp.com> Acked-by: Al Viro <viro@zeniv.linux.org.uk> Tested-by: Daire Byrne <Daire.Byrne@framestore.com>
2009-04-03 23:42:36 +08:00
/* General filesystem caching interface
*
* Copyright (C) 2004-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.
*
* NOTE!!! See:
*
* Documentation/filesystems/caching/netfs-api.txt
*
* for a description of the network filesystem interface declared here.
*/
#ifndef _LINUX_FSCACHE_H
#define _LINUX_FSCACHE_H
#include <linux/fs.h>
#include <linux/list.h>
#include <linux/pagemap.h>
#include <linux/pagevec.h>
#if defined(CONFIG_FSCACHE) || defined(CONFIG_FSCACHE_MODULE)
#define fscache_available() (1)
#define fscache_cookie_valid(cookie) (cookie)
#else
#define fscache_available() (0)
#define fscache_cookie_valid(cookie) (0)
#endif
/*
* overload PG_private_2 to give us PG_fscache - this is used to indicate that
* a page is currently backed by a local disk cache
*/
#define PageFsCache(page) PagePrivate2((page))
#define SetPageFsCache(page) SetPagePrivate2((page))
#define ClearPageFsCache(page) ClearPagePrivate2((page))
#define TestSetPageFsCache(page) TestSetPagePrivate2((page))
#define TestClearPageFsCache(page) TestClearPagePrivate2((page))
/* pattern used to fill dead space in an index entry */
#define FSCACHE_INDEX_DEADFILL_PATTERN 0x79
struct pagevec;
struct fscache_cache_tag;
struct fscache_cookie;
struct fscache_netfs;
typedef void (*fscache_rw_complete_t)(struct page *page,
void *context,
int error);
/* result of index entry consultation */
enum fscache_checkaux {
FSCACHE_CHECKAUX_OKAY, /* entry okay as is */
FSCACHE_CHECKAUX_NEEDS_UPDATE, /* entry requires update */
FSCACHE_CHECKAUX_OBSOLETE, /* entry requires deletion */
};
/*
* fscache cookie definition
*/
struct fscache_cookie_def {
/* name of cookie type */
char name[16];
/* cookie type */
uint8_t type;
#define FSCACHE_COOKIE_TYPE_INDEX 0
#define FSCACHE_COOKIE_TYPE_DATAFILE 1
/* select the cache into which to insert an entry in this index
* - optional
* - should return a cache identifier or NULL to cause the cache to be
* inherited from the parent if possible or the first cache picked
* for a non-index file if not
*/
struct fscache_cache_tag *(*select_cache)(
const void *parent_netfs_data,
const void *cookie_netfs_data);
/* get an index key
* - should store the key data in the buffer
* - should return the amount of amount stored
* - not permitted to return an error
* - the netfs data from the cookie being used as the source is
* presented
*/
uint16_t (*get_key)(const void *cookie_netfs_data,
void *buffer,
uint16_t bufmax);
/* get certain file attributes from the netfs data
* - this function can be absent for an index
* - not permitted to return an error
* - the netfs data from the cookie being used as the source is
* presented
*/
void (*get_attr)(const void *cookie_netfs_data, uint64_t *size);
/* get the auxilliary data from netfs data
* - this function can be absent if the index carries no state data
* - should store the auxilliary data in the buffer
* - should return the amount of amount stored
* - not permitted to return an error
* - the netfs data from the cookie being used as the source is
* presented
*/
uint16_t (*get_aux)(const void *cookie_netfs_data,
void *buffer,
uint16_t bufmax);
/* consult the netfs about the state of an object
* - this function can be absent if the index carries no state data
* - the netfs data from the cookie being used as the target is
* presented, as is the auxilliary data
*/
enum fscache_checkaux (*check_aux)(void *cookie_netfs_data,
const void *data,
uint16_t datalen);
/* get an extra reference on a read context
* - this function can be absent if the completion function doesn't
* require a context
*/
void (*get_context)(void *cookie_netfs_data, void *context);
/* release an extra reference on a read context
* - this function can be absent if the completion function doesn't
* require a context
*/
void (*put_context)(void *cookie_netfs_data, void *context);
/* indicate pages that now have cache metadata retained
* - this function should mark the specified pages as now being cached
* - the pages will have been marked with PG_fscache before this is
* called, so this is optional
*/
void (*mark_pages_cached)(void *cookie_netfs_data,
struct address_space *mapping,
struct pagevec *cached_pvec);
/* indicate the cookie is no longer cached
* - this function is called when the backing store currently caching
* a cookie is removed
* - the netfs should use this to clean up any markers indicating
* cached pages
* - this is mandatory for any object that may have data
*/
void (*now_uncached)(void *cookie_netfs_data);
};
/*
* fscache cached network filesystem type
* - name, version and ops must be filled in before registration
* - all other fields will be set during registration
*/
struct fscache_netfs {
uint32_t version; /* indexing version */
const char *name; /* filesystem name */
struct fscache_cookie *primary_index;
struct list_head link; /* internal link */
};
/*
* slow-path functions for when there is actually caching available, and the
* netfs does actually have a valid token
* - these are not to be called directly
* - these are undefined symbols when FS-Cache is not configured and the
* optimiser takes care of not using them
*/
/**
* fscache_register_netfs - Register a filesystem as desiring caching services
* @netfs: The description of the filesystem
*
* Register a filesystem as desiring caching services if they're available.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
int fscache_register_netfs(struct fscache_netfs *netfs)
{
return 0;
}
/**
* fscache_unregister_netfs - Indicate that a filesystem no longer desires
* caching services
* @netfs: The description of the filesystem
*
* Indicate that a filesystem no longer desires caching services for the
* moment.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
void fscache_unregister_netfs(struct fscache_netfs *netfs)
{
}
/**
* fscache_lookup_cache_tag - Look up a cache tag
* @name: The name of the tag to search for
*
* Acquire a specific cache referral tag that can be used to select a specific
* cache in which to cache an index.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
struct fscache_cache_tag *fscache_lookup_cache_tag(const char *name)
{
return NULL;
}
/**
* fscache_release_cache_tag - Release a cache tag
* @tag: The tag to release
*
* Release a reference to a cache referral tag previously looked up.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
void fscache_release_cache_tag(struct fscache_cache_tag *tag)
{
}
/**
* fscache_acquire_cookie - Acquire a cookie to represent a cache object
* @parent: The cookie that's to be the parent of this one
* @def: A description of the cache object, including callback operations
* @netfs_data: An arbitrary piece of data to be kept in the cookie to
* represent the cache object to the netfs
*
* This function is used to inform FS-Cache about part of an index hierarchy
* that can be used to locate files. This is done by requesting a cookie for
* each index in the path to the file.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
struct fscache_cookie *fscache_acquire_cookie(
struct fscache_cookie *parent,
const struct fscache_cookie_def *def,
void *netfs_data)
{
return NULL;
}
/**
* fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
* it
* @cookie: The cookie being returned
* @retire: True if the cache object the cookie represents is to be discarded
*
* This function returns a cookie to the cache, forcibly discarding the
* associated cache object if retire is set to true.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
void fscache_relinquish_cookie(struct fscache_cookie *cookie, int retire)
{
}
/**
* fscache_update_cookie - Request that a cache object be updated
* @cookie: The cookie representing the cache object
*
* Request an update of the index data for the cache object associated with the
* cookie.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
void fscache_update_cookie(struct fscache_cookie *cookie)
{
}
/**
* fscache_pin_cookie - Pin a data-storage cache object in its cache
* @cookie: The cookie representing the cache object
*
* Permit data-storage cache objects to be pinned in the cache.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
int fscache_pin_cookie(struct fscache_cookie *cookie)
{
return -ENOBUFS;
}
/**
* fscache_pin_cookie - Unpin a data-storage cache object in its cache
* @cookie: The cookie representing the cache object
*
* Permit data-storage cache objects to be unpinned from the cache.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
void fscache_unpin_cookie(struct fscache_cookie *cookie)
{
}
/**
* fscache_attr_changed - Notify cache that an object's attributes changed
* @cookie: The cookie representing the cache object
*
* Send a notification to the cache indicating that an object's attributes have
* changed. This includes the data size. These attributes will be obtained
* through the get_attr() cookie definition op.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
int fscache_attr_changed(struct fscache_cookie *cookie)
{
return -ENOBUFS;
}
/**
* fscache_reserve_space - Reserve data space for a cached object
* @cookie: The cookie representing the cache object
* @i_size: The amount of space to be reserved
*
* Reserve an amount of space in the cache for the cache object attached to a
* cookie so that a write to that object within the space can always be
* honoured.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
int fscache_reserve_space(struct fscache_cookie *cookie, loff_t size)
{
return -ENOBUFS;
}
/**
* fscache_read_or_alloc_page - Read a page from the cache or allocate a block
* in which to store it
* @cookie: The cookie representing the cache object
* @page: The netfs page to fill if possible
* @end_io_func: The callback to invoke when and if the page is filled
* @context: An arbitrary piece of data to pass on to end_io_func()
* @gfp: The conditions under which memory allocation should be made
*
* Read a page from the cache, or if that's not possible make a potential
* one-block reservation in the cache into which the page may be stored once
* fetched from the server.
*
* If the page is not backed by the cache object, or if it there's some reason
* it can't be, -ENOBUFS will be returned and nothing more will be done for
* that page.
*
* Else, if that page is backed by the cache, a read will be initiated directly
* to the netfs's page and 0 will be returned by this function. The
* end_io_func() callback will be invoked when the operation terminates on a
* completion or failure. Note that the callback may be invoked before the
* return.
*
* Else, if the page is unbacked, -ENODATA is returned and a block may have
* been allocated in the cache.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
int fscache_read_or_alloc_page(struct fscache_cookie *cookie,
struct page *page,
fscache_rw_complete_t end_io_func,
void *context,
gfp_t gfp)
{
return -ENOBUFS;
}
/**
* fscache_read_or_alloc_pages - Read pages from the cache and/or allocate
* blocks in which to store them
* @cookie: The cookie representing the cache object
* @mapping: The netfs inode mapping to which the pages will be attached
* @pages: A list of potential netfs pages to be filled
* @end_io_func: The callback to invoke when and if each page is filled
* @context: An arbitrary piece of data to pass on to end_io_func()
* @gfp: The conditions under which memory allocation should be made
*
* Read a set of pages from the cache, or if that's not possible, attempt to
* make a potential one-block reservation for each page in the cache into which
* that page may be stored once fetched from the server.
*
* If some pages are not backed by the cache object, or if it there's some
* reason they can't be, -ENOBUFS will be returned and nothing more will be
* done for that pages.
*
* Else, if some of the pages are backed by the cache, a read will be initiated
* directly to the netfs's page and 0 will be returned by this function. The
* end_io_func() callback will be invoked when the operation terminates on a
* completion or failure. Note that the callback may be invoked before the
* return.
*
* Else, if a page is unbacked, -ENODATA is returned and a block may have
* been allocated in the cache.
*
* Because the function may want to return all of -ENOBUFS, -ENODATA and 0 in
* regard to different pages, the return values are prioritised in that order.
* Any pages submitted for reading are removed from the pages list.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
int fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
struct address_space *mapping,
struct list_head *pages,
unsigned *nr_pages,
fscache_rw_complete_t end_io_func,
void *context,
gfp_t gfp)
{
return -ENOBUFS;
}
/**
* fscache_alloc_page - Allocate a block in which to store a page
* @cookie: The cookie representing the cache object
* @page: The netfs page to allocate a page for
* @gfp: The conditions under which memory allocation should be made
*
* Request Allocation a block in the cache in which to store a netfs page
* without retrieving any contents from the cache.
*
* If the page is not backed by a file then -ENOBUFS will be returned and
* nothing more will be done, and no reservation will be made.
*
* Else, a block will be allocated if one wasn't already, and 0 will be
* returned
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
int fscache_alloc_page(struct fscache_cookie *cookie,
struct page *page,
gfp_t gfp)
{
return -ENOBUFS;
}
/**
* fscache_write_page - Request storage of a page in the cache
* @cookie: The cookie representing the cache object
* @page: The netfs page to store
* @gfp: The conditions under which memory allocation should be made
*
* Request the contents of the netfs page be written into the cache. This
* request may be ignored if no cache block is currently allocated, in which
* case it will return -ENOBUFS.
*
* If a cache block was already allocated, a write will be initiated and 0 will
* be returned. The PG_fscache_write page bit is set immediately and will then
* be cleared at the completion of the write to indicate the success or failure
* of the operation. Note that the completion may happen before the return.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
int fscache_write_page(struct fscache_cookie *cookie,
struct page *page,
gfp_t gfp)
{
return -ENOBUFS;
}
/**
* fscache_uncache_page - Indicate that caching is no longer required on a page
* @cookie: The cookie representing the cache object
* @page: The netfs page that was being cached.
*
* Tell the cache that we no longer want a page to be cached and that it should
* remove any knowledge of the netfs page it may have.
*
* Note that this cannot cancel any outstanding I/O operations between this
* page and the cache.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
void fscache_uncache_page(struct fscache_cookie *cookie,
struct page *page)
{
}
/**
* fscache_check_page_write - Ask if a page is being writing to the cache
* @cookie: The cookie representing the cache object
* @page: The netfs page that is being cached.
*
* Ask the cache if a page is being written to the cache.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
bool fscache_check_page_write(struct fscache_cookie *cookie,
struct page *page)
{
return false;
}
/**
* fscache_wait_on_page_write - Wait for a page to complete writing to the cache
* @cookie: The cookie representing the cache object
* @page: The netfs page that is being cached.
*
* Ask the cache to wake us up when a page is no longer being written to the
* cache.
*
* See Documentation/filesystems/caching/netfs-api.txt for a complete
* description.
*/
static inline
void fscache_wait_on_page_write(struct fscache_cookie *cookie,
struct page *page)
{
}
#endif /* _LINUX_FSCACHE_H */