2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* net/sunrpc/cache.c
|
|
|
|
*
|
|
|
|
* Generic code for various authentication-related caches
|
|
|
|
* used by sunrpc clients and servers.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
|
|
|
|
*
|
|
|
|
* Released under terms in GPL version 2. See COPYING.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/signal.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/kmod.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/workqueue.h>
|
2006-03-21 14:33:17 +08:00
|
|
|
#include <linux/mutex.h>
|
2009-08-10 03:14:28 +08:00
|
|
|
#include <linux/pagemap.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/ioctls.h>
|
|
|
|
#include <linux/sunrpc/types.h>
|
|
|
|
#include <linux/sunrpc/cache.h>
|
|
|
|
#include <linux/sunrpc/stats.h>
|
2009-08-10 03:14:30 +08:00
|
|
|
#include <linux/sunrpc/rpc_pipe_fs.h>
|
2010-09-27 18:01:58 +08:00
|
|
|
#include "netns.h"
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#define RPCDBG_FACILITY RPCDBG_CACHE
|
|
|
|
|
2011-01-03 10:28:34 +08:00
|
|
|
static bool cache_defer_req(struct cache_req *req, struct cache_head *item);
|
2005-04-17 06:20:36 +08:00
|
|
|
static void cache_revisit_request(struct cache_head *item);
|
|
|
|
|
2006-03-27 17:15:10 +08:00
|
|
|
static void cache_init(struct cache_head *h)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-08-12 14:55:22 +08:00
|
|
|
time_t now = seconds_since_boot();
|
2005-04-17 06:20:36 +08:00
|
|
|
h->next = NULL;
|
|
|
|
h->flags = 0;
|
2006-03-27 17:15:09 +08:00
|
|
|
kref_init(&h->ref);
|
2005-04-17 06:20:36 +08:00
|
|
|
h->expiry_time = now + CACHE_NEW_EXPIRY;
|
|
|
|
h->last_refresh = now;
|
|
|
|
}
|
|
|
|
|
2010-02-03 14:31:31 +08:00
|
|
|
static inline int cache_is_expired(struct cache_detail *detail, struct cache_head *h)
|
|
|
|
{
|
2010-08-12 14:55:22 +08:00
|
|
|
return (h->expiry_time < seconds_since_boot()) ||
|
2010-02-03 14:31:31 +08:00
|
|
|
(detail->flush_time > h->last_refresh);
|
|
|
|
}
|
|
|
|
|
2006-03-27 17:15:02 +08:00
|
|
|
struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
|
|
|
|
struct cache_head *key, int hash)
|
|
|
|
{
|
|
|
|
struct cache_head **head, **hp;
|
2010-02-03 14:31:31 +08:00
|
|
|
struct cache_head *new = NULL, *freeme = NULL;
|
2006-03-27 17:15:02 +08:00
|
|
|
|
|
|
|
head = &detail->hash_table[hash];
|
|
|
|
|
|
|
|
read_lock(&detail->hash_lock);
|
|
|
|
|
|
|
|
for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
|
|
|
|
struct cache_head *tmp = *hp;
|
|
|
|
if (detail->match(tmp, key)) {
|
2010-02-03 14:31:31 +08:00
|
|
|
if (cache_is_expired(detail, tmp))
|
|
|
|
/* This entry is expired, we will discard it. */
|
|
|
|
break;
|
2006-03-27 17:15:02 +08:00
|
|
|
cache_get(tmp);
|
|
|
|
read_unlock(&detail->hash_lock);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
read_unlock(&detail->hash_lock);
|
|
|
|
/* Didn't find anything, insert an empty entry */
|
|
|
|
|
|
|
|
new = detail->alloc();
|
|
|
|
if (!new)
|
|
|
|
return NULL;
|
2006-08-06 03:14:29 +08:00
|
|
|
/* must fully initialise 'new', else
|
|
|
|
* we might get lose if we need to
|
|
|
|
* cache_put it soon.
|
|
|
|
*/
|
2006-03-27 17:15:02 +08:00
|
|
|
cache_init(new);
|
2006-08-06 03:14:29 +08:00
|
|
|
detail->init(new, key);
|
2006-03-27 17:15:02 +08:00
|
|
|
|
|
|
|
write_lock(&detail->hash_lock);
|
|
|
|
|
|
|
|
/* check if entry appeared while we slept */
|
|
|
|
for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
|
|
|
|
struct cache_head *tmp = *hp;
|
|
|
|
if (detail->match(tmp, key)) {
|
2010-02-03 14:31:31 +08:00
|
|
|
if (cache_is_expired(detail, tmp)) {
|
|
|
|
*hp = tmp->next;
|
|
|
|
tmp->next = NULL;
|
|
|
|
detail->entries --;
|
|
|
|
freeme = tmp;
|
|
|
|
break;
|
|
|
|
}
|
2006-03-27 17:15:02 +08:00
|
|
|
cache_get(tmp);
|
|
|
|
write_unlock(&detail->hash_lock);
|
2006-03-27 17:15:09 +08:00
|
|
|
cache_put(new, detail);
|
2006-03-27 17:15:02 +08:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new->next = *head;
|
|
|
|
*head = new;
|
|
|
|
detail->entries++;
|
|
|
|
cache_get(new);
|
|
|
|
write_unlock(&detail->hash_lock);
|
|
|
|
|
2010-02-03 14:31:31 +08:00
|
|
|
if (freeme)
|
|
|
|
cache_put(freeme, detail);
|
2006-03-27 17:15:02 +08:00
|
|
|
return new;
|
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
|
2006-03-27 17:15:02 +08:00
|
|
|
|
2006-03-27 17:15:08 +08:00
|
|
|
|
2009-08-04 13:22:38 +08:00
|
|
|
static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
|
2006-03-27 17:15:08 +08:00
|
|
|
|
2009-09-09 14:32:54 +08:00
|
|
|
static void cache_fresh_locked(struct cache_head *head, time_t expiry)
|
2006-03-27 17:15:08 +08:00
|
|
|
{
|
|
|
|
head->expiry_time = expiry;
|
2010-08-12 14:55:22 +08:00
|
|
|
head->last_refresh = seconds_since_boot();
|
2011-01-05 03:12:47 +08:00
|
|
|
smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
|
2009-09-09 14:32:54 +08:00
|
|
|
set_bit(CACHE_VALID, &head->flags);
|
2006-03-27 17:15:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cache_fresh_unlocked(struct cache_head *head,
|
2009-09-09 14:32:54 +08:00
|
|
|
struct cache_detail *detail)
|
2006-03-27 17:15:08 +08:00
|
|
|
{
|
|
|
|
if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
|
|
|
|
cache_revisit_request(head);
|
2009-08-04 13:22:38 +08:00
|
|
|
cache_dequeue(detail, head);
|
2006-03-27 17:15:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-27 17:15:02 +08:00
|
|
|
struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
|
|
|
|
struct cache_head *new, struct cache_head *old, int hash)
|
|
|
|
{
|
|
|
|
/* The 'old' entry is to be replaced by 'new'.
|
|
|
|
* If 'old' is not VALID, we update it directly,
|
|
|
|
* otherwise we need to replace it
|
|
|
|
*/
|
|
|
|
struct cache_head **head;
|
|
|
|
struct cache_head *tmp;
|
|
|
|
|
|
|
|
if (!test_bit(CACHE_VALID, &old->flags)) {
|
|
|
|
write_lock(&detail->hash_lock);
|
|
|
|
if (!test_bit(CACHE_VALID, &old->flags)) {
|
|
|
|
if (test_bit(CACHE_NEGATIVE, &new->flags))
|
|
|
|
set_bit(CACHE_NEGATIVE, &old->flags);
|
|
|
|
else
|
|
|
|
detail->update(old, new);
|
2009-09-09 14:32:54 +08:00
|
|
|
cache_fresh_locked(old, new->expiry_time);
|
2006-03-27 17:15:02 +08:00
|
|
|
write_unlock(&detail->hash_lock);
|
2009-09-09 14:32:54 +08:00
|
|
|
cache_fresh_unlocked(old, detail);
|
2006-03-27 17:15:02 +08:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
write_unlock(&detail->hash_lock);
|
|
|
|
}
|
|
|
|
/* We need to insert a new entry */
|
|
|
|
tmp = detail->alloc();
|
|
|
|
if (!tmp) {
|
2006-03-27 17:15:09 +08:00
|
|
|
cache_put(old, detail);
|
2006-03-27 17:15:02 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cache_init(tmp);
|
|
|
|
detail->init(tmp, old);
|
|
|
|
head = &detail->hash_table[hash];
|
|
|
|
|
|
|
|
write_lock(&detail->hash_lock);
|
|
|
|
if (test_bit(CACHE_NEGATIVE, &new->flags))
|
|
|
|
set_bit(CACHE_NEGATIVE, &tmp->flags);
|
|
|
|
else
|
|
|
|
detail->update(tmp, new);
|
|
|
|
tmp->next = *head;
|
|
|
|
*head = tmp;
|
2006-05-23 13:35:25 +08:00
|
|
|
detail->entries++;
|
2006-03-27 17:15:02 +08:00
|
|
|
cache_get(tmp);
|
2009-09-09 14:32:54 +08:00
|
|
|
cache_fresh_locked(tmp, new->expiry_time);
|
2006-03-27 17:15:08 +08:00
|
|
|
cache_fresh_locked(old, 0);
|
2006-03-27 17:15:02 +08:00
|
|
|
write_unlock(&detail->hash_lock);
|
2009-09-09 14:32:54 +08:00
|
|
|
cache_fresh_unlocked(tmp, detail);
|
|
|
|
cache_fresh_unlocked(old, detail);
|
2006-03-27 17:15:09 +08:00
|
|
|
cache_put(old, detail);
|
2006-03-27 17:15:02 +08:00
|
|
|
return tmp;
|
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(sunrpc_cache_update);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
|
|
|
|
{
|
2013-02-04 19:02:50 +08:00
|
|
|
if (cd->cache_upcall)
|
|
|
|
return cd->cache_upcall(cd, h);
|
2013-02-04 19:02:55 +08:00
|
|
|
return sunrpc_cache_pipe_upcall(cd, h);
|
2009-08-10 03:14:29 +08:00
|
|
|
}
|
2009-08-04 13:22:38 +08:00
|
|
|
|
|
|
|
static inline int cache_is_valid(struct cache_detail *detail, struct cache_head *h)
|
|
|
|
{
|
2010-02-03 14:31:31 +08:00
|
|
|
if (!test_bit(CACHE_VALID, &h->flags))
|
2009-08-04 13:22:38 +08:00
|
|
|
return -EAGAIN;
|
|
|
|
else {
|
|
|
|
/* entry is valid */
|
|
|
|
if (test_bit(CACHE_NEGATIVE, &h->flags))
|
|
|
|
return -ENOENT;
|
2011-01-05 03:12:47 +08:00
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* In combination with write barrier in
|
|
|
|
* sunrpc_cache_update, ensures that anyone
|
|
|
|
* using the cache entry after this sees the
|
|
|
|
* updated contents:
|
|
|
|
*/
|
|
|
|
smp_rmb();
|
2009-08-04 13:22:38 +08:00
|
|
|
return 0;
|
2011-01-05 03:12:47 +08:00
|
|
|
}
|
2009-08-04 13:22:38 +08:00
|
|
|
}
|
|
|
|
}
|
2009-08-21 23:27:29 +08:00
|
|
|
|
2011-01-04 04:10:27 +08:00
|
|
|
static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
write_lock(&detail->hash_lock);
|
|
|
|
rv = cache_is_valid(detail, h);
|
|
|
|
if (rv != -EAGAIN) {
|
|
|
|
write_unlock(&detail->hash_lock);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
set_bit(CACHE_NEGATIVE, &h->flags);
|
|
|
|
cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
|
|
|
|
write_unlock(&detail->hash_lock);
|
|
|
|
cache_fresh_unlocked(h, detail);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* This is the generic cache management routine for all
|
|
|
|
* the authentication caches.
|
|
|
|
* It checks the currency of a cache item and will (later)
|
|
|
|
* initiate an upcall to fill it if needed.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Returns 0 if the cache_head can be used, or cache_puts it and returns
|
2009-08-04 13:22:38 +08:00
|
|
|
* -EAGAIN if upcall is pending and request has been queued
|
|
|
|
* -ETIMEDOUT if upcall failed or request could not be queue or
|
|
|
|
* upcall completed but item is still invalid (implying that
|
|
|
|
* the cache item has been replaced with a newer one).
|
2005-04-17 06:20:36 +08:00
|
|
|
* -ENOENT if cache entry was negative
|
|
|
|
*/
|
|
|
|
int cache_check(struct cache_detail *detail,
|
|
|
|
struct cache_head *h, struct cache_req *rqstp)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
long refresh_age, age;
|
|
|
|
|
|
|
|
/* First decide return status as best we can */
|
2009-08-04 13:22:38 +08:00
|
|
|
rv = cache_is_valid(detail, h);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* now see if we want to start an upcall */
|
|
|
|
refresh_age = (h->expiry_time - h->last_refresh);
|
2010-08-12 14:55:22 +08:00
|
|
|
age = seconds_since_boot() - h->last_refresh;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (rqstp == NULL) {
|
|
|
|
if (rv == -EAGAIN)
|
|
|
|
rv = -ENOENT;
|
|
|
|
} else if (rv == -EAGAIN || age > refresh_age/2) {
|
2007-02-01 01:14:08 +08:00
|
|
|
dprintk("RPC: Want update, refage=%ld, age=%ld\n",
|
|
|
|
refresh_age, age);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
|
|
|
|
switch (cache_make_upcall(detail, h)) {
|
|
|
|
case -EINVAL:
|
|
|
|
clear_bit(CACHE_PENDING, &h->flags);
|
2009-08-04 13:22:38 +08:00
|
|
|
cache_revisit_request(h);
|
2011-01-04 04:10:27 +08:00
|
|
|
rv = try_to_negate_entry(detail, h);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
case -EAGAIN:
|
|
|
|
clear_bit(CACHE_PENDING, &h->flags);
|
|
|
|
cache_revisit_request(h);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-04 13:22:38 +08:00
|
|
|
if (rv == -EAGAIN) {
|
2011-01-03 10:28:34 +08:00
|
|
|
if (!cache_defer_req(rqstp, h)) {
|
|
|
|
/*
|
|
|
|
* Request was not deferred; handle it as best
|
|
|
|
* we can ourselves:
|
|
|
|
*/
|
2009-08-04 13:22:38 +08:00
|
|
|
rv = cache_is_valid(detail, h);
|
|
|
|
if (rv == -EAGAIN)
|
|
|
|
rv = -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
2006-03-27 17:15:07 +08:00
|
|
|
if (rv)
|
2006-03-27 17:15:09 +08:00
|
|
|
cache_put(h, detail);
|
2005-04-17 06:20:36 +08:00
|
|
|
return rv;
|
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(cache_check);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* caches need to be periodically cleaned.
|
|
|
|
* For this we maintain a list of cache_detail and
|
|
|
|
* a current pointer into that list and into the table
|
|
|
|
* for that entry.
|
|
|
|
*
|
|
|
|
* Each time clean_cache is called it finds the next non-empty entry
|
|
|
|
* in the current table and walks the list in that entry
|
|
|
|
* looking for entries that can be removed.
|
|
|
|
*
|
|
|
|
* An entry gets removed if:
|
|
|
|
* - The expiry is before current time
|
|
|
|
* - The last_refresh time is before the flush_time for that cache
|
|
|
|
*
|
|
|
|
* later we might drop old entries with non-NEVER expiry if that table
|
|
|
|
* is getting 'full' for some definition of 'full'
|
|
|
|
*
|
|
|
|
* The question of "how often to scan a table" is an interesting one
|
|
|
|
* and is answered in part by the use of the "nextcheck" field in the
|
|
|
|
* cache_detail.
|
|
|
|
* When a scan of a table begins, the nextcheck field is set to a time
|
|
|
|
* that is well into the future.
|
|
|
|
* While scanning, if an expiry time is found that is earlier than the
|
|
|
|
* current nextcheck time, nextcheck is set to that expiry time.
|
|
|
|
* If the flush_time is ever set to a time earlier than the nextcheck
|
|
|
|
* time, the nextcheck time is then set to that flush_time.
|
|
|
|
*
|
|
|
|
* A table is then only scanned if the current time is at least
|
|
|
|
* the nextcheck time.
|
2007-02-10 07:38:13 +08:00
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
static LIST_HEAD(cache_list);
|
|
|
|
static DEFINE_SPINLOCK(cache_list_lock);
|
|
|
|
static struct cache_detail *current_detail;
|
|
|
|
static int current_index;
|
|
|
|
|
2006-11-22 22:55:48 +08:00
|
|
|
static void do_cache_clean(struct work_struct *work);
|
sunrpc: make the cache cleaner workqueue deferrable
This patch makes the cache_cleaner workqueue deferrable, to prevent
unnecessary system wake-ups, which is very important for embedded
battery-powered devices.
do_cache_clean() is called every 30 seconds at the moment, and often
makes the system wake up from its power-save sleep state. With this
change, when the workqueue uses a deferrable timer, the
do_cache_clean() invocation will be delayed and combined with the
closest "real" wake-up. This improves the power consumption situation.
Note, I tried to create a DECLARE_DELAYED_WORK_DEFERRABLE() helper
macro, similar to DECLARE_DELAYED_WORK(), but failed because of the
way the timer wheel core stores the deferrable flag (it is the
LSBit in the time->base pointer). My attempt to define a static
variable with this bit set ended up with the "initializer element is
not constant" error.
Thus, I have to use run-time initialization, so I created a new
cache_initialize() function which is called once when sunrpc is
being initialized.
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu>
2010-07-01 23:05:56 +08:00
|
|
|
static struct delayed_work cache_cleaner;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-11-25 22:12:40 +08:00
|
|
|
void sunrpc_init_cache_detail(struct cache_detail *cd)
|
2007-11-13 06:04:29 +08:00
|
|
|
{
|
2005-04-17 06:20:36 +08:00
|
|
|
rwlock_init(&cd->hash_lock);
|
|
|
|
INIT_LIST_HEAD(&cd->queue);
|
|
|
|
spin_lock(&cache_list_lock);
|
|
|
|
cd->nextcheck = 0;
|
|
|
|
cd->entries = 0;
|
|
|
|
atomic_set(&cd->readers, 0);
|
|
|
|
cd->last_close = 0;
|
|
|
|
cd->last_warn = -1;
|
|
|
|
list_add(&cd->others, &cache_list);
|
|
|
|
spin_unlock(&cache_list_lock);
|
|
|
|
|
|
|
|
/* start the cleaning process */
|
2006-11-22 22:54:01 +08:00
|
|
|
schedule_delayed_work(&cache_cleaner, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2011-11-25 22:12:40 +08:00
|
|
|
EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-11-25 22:12:40 +08:00
|
|
|
void sunrpc_destroy_cache_detail(struct cache_detail *cd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
cache_purge(cd);
|
|
|
|
spin_lock(&cache_list_lock);
|
|
|
|
write_lock(&cd->hash_lock);
|
|
|
|
if (cd->entries || atomic_read(&cd->inuse)) {
|
|
|
|
write_unlock(&cd->hash_lock);
|
|
|
|
spin_unlock(&cache_list_lock);
|
2007-11-09 05:09:59 +08:00
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
if (current_detail == cd)
|
|
|
|
current_detail = NULL;
|
|
|
|
list_del_init(&cd->others);
|
|
|
|
write_unlock(&cd->hash_lock);
|
|
|
|
spin_unlock(&cache_list_lock);
|
|
|
|
if (list_empty(&cache_list)) {
|
|
|
|
/* module must be being unloaded so its safe to kill the worker */
|
2007-08-08 03:33:01 +08:00
|
|
|
cancel_delayed_work_sync(&cache_cleaner);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-11-09 05:09:59 +08:00
|
|
|
return;
|
|
|
|
out:
|
|
|
|
printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2011-11-25 22:12:40 +08:00
|
|
|
EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* clean cache tries to find something to clean
|
|
|
|
* and cleans it.
|
|
|
|
* It returns 1 if it cleaned something,
|
|
|
|
* 0 if it didn't find anything this time
|
|
|
|
* -1 if it fell off the end of the list.
|
|
|
|
*/
|
|
|
|
static int cache_clean(void)
|
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
struct list_head *next;
|
|
|
|
|
|
|
|
spin_lock(&cache_list_lock);
|
|
|
|
|
|
|
|
/* find a suitable table if we don't already have one */
|
|
|
|
while (current_detail == NULL ||
|
|
|
|
current_index >= current_detail->hash_size) {
|
|
|
|
if (current_detail)
|
|
|
|
next = current_detail->others.next;
|
|
|
|
else
|
|
|
|
next = cache_list.next;
|
|
|
|
if (next == &cache_list) {
|
|
|
|
current_detail = NULL;
|
|
|
|
spin_unlock(&cache_list_lock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
current_detail = list_entry(next, struct cache_detail, others);
|
2010-08-12 14:55:22 +08:00
|
|
|
if (current_detail->nextcheck > seconds_since_boot())
|
2005-04-17 06:20:36 +08:00
|
|
|
current_index = current_detail->hash_size;
|
|
|
|
else {
|
|
|
|
current_index = 0;
|
2010-08-12 14:55:22 +08:00
|
|
|
current_detail->nextcheck = seconds_since_boot()+30*60;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find a non-empty bucket in the table */
|
|
|
|
while (current_detail &&
|
|
|
|
current_index < current_detail->hash_size &&
|
|
|
|
current_detail->hash_table[current_index] == NULL)
|
|
|
|
current_index++;
|
|
|
|
|
|
|
|
/* find a cleanable entry in the bucket and clean it, or set to next bucket */
|
2007-02-10 07:38:13 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (current_detail && current_index < current_detail->hash_size) {
|
|
|
|
struct cache_head *ch, **cp;
|
|
|
|
struct cache_detail *d;
|
2007-02-10 07:38:13 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
write_lock(¤t_detail->hash_lock);
|
|
|
|
|
|
|
|
/* Ok, now to clean this strand */
|
2007-02-10 07:38:13 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
cp = & current_detail->hash_table[current_index];
|
2010-02-03 14:31:31 +08:00
|
|
|
for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (current_detail->nextcheck > ch->expiry_time)
|
|
|
|
current_detail->nextcheck = ch->expiry_time+1;
|
2010-02-03 14:31:31 +08:00
|
|
|
if (!cache_is_expired(current_detail, ch))
|
2005-04-17 06:20:36 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
*cp = ch->next;
|
|
|
|
ch->next = NULL;
|
|
|
|
current_detail->entries--;
|
|
|
|
rv = 1;
|
2010-02-03 14:31:31 +08:00
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2010-02-03 14:31:31 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
write_unlock(¤t_detail->hash_lock);
|
|
|
|
d = current_detail;
|
|
|
|
if (!ch)
|
|
|
|
current_index ++;
|
|
|
|
spin_unlock(&cache_list_lock);
|
2009-08-04 13:22:38 +08:00
|
|
|
if (ch) {
|
2010-02-03 14:31:31 +08:00
|
|
|
if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
|
|
|
|
cache_dequeue(current_detail, ch);
|
2009-08-04 13:22:38 +08:00
|
|
|
cache_revisit_request(ch);
|
2006-03-27 17:15:09 +08:00
|
|
|
cache_put(ch, d);
|
2009-08-04 13:22:38 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
} else
|
|
|
|
spin_unlock(&cache_list_lock);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We want to regularly clean the cache, so we need to schedule some work ...
|
|
|
|
*/
|
2006-11-22 22:55:48 +08:00
|
|
|
static void do_cache_clean(struct work_struct *work)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int delay = 5;
|
|
|
|
if (cache_clean() == -1)
|
2009-06-11 03:52:21 +08:00
|
|
|
delay = round_jiffies_relative(30*HZ);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (list_empty(&cache_list))
|
|
|
|
delay = 0;
|
|
|
|
|
|
|
|
if (delay)
|
|
|
|
schedule_delayed_work(&cache_cleaner, delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-10 07:38:13 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Clean all caches promptly. This just calls cache_clean
|
2007-02-10 07:38:13 +08:00
|
|
|
* repeatedly until we are sure that every cache has had a chance to
|
2005-04-17 06:20:36 +08:00
|
|
|
* be fully cleaned
|
|
|
|
*/
|
|
|
|
void cache_flush(void)
|
|
|
|
{
|
|
|
|
while (cache_clean() != -1)
|
|
|
|
cond_resched();
|
|
|
|
while (cache_clean() != -1)
|
|
|
|
cond_resched();
|
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(cache_flush);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
void cache_purge(struct cache_detail *detail)
|
|
|
|
{
|
|
|
|
detail->flush_time = LONG_MAX;
|
2010-08-12 14:55:22 +08:00
|
|
|
detail->nextcheck = seconds_since_boot();
|
2005-04-17 06:20:36 +08:00
|
|
|
cache_flush();
|
|
|
|
detail->flush_time = 1;
|
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(cache_purge);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deferral and Revisiting of Requests.
|
|
|
|
*
|
|
|
|
* If a cache lookup finds a pending entry, we
|
|
|
|
* need to defer the request and revisit it later.
|
|
|
|
* All deferred requests are stored in a hash table,
|
|
|
|
* indexed by "struct cache_head *".
|
|
|
|
* As it may be wasteful to store a whole request
|
2007-02-10 07:38:13 +08:00
|
|
|
* structure, we allow the request to provide a
|
2005-04-17 06:20:36 +08:00
|
|
|
* deferred form, which must contain a
|
|
|
|
* 'struct cache_deferred_req'
|
|
|
|
* This cache_deferred_req contains a method to allow
|
|
|
|
* it to be revisited when cache info is available
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
|
|
|
|
#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
|
|
|
|
|
|
|
|
#define DFR_MAX 300 /* ??? */
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(cache_defer_lock);
|
|
|
|
static LIST_HEAD(cache_defer_list);
|
2010-08-12 15:04:08 +08:00
|
|
|
static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
|
2005-04-17 06:20:36 +08:00
|
|
|
static int cache_defer_cnt;
|
|
|
|
|
2010-08-27 01:19:52 +08:00
|
|
|
static void __unhash_deferred_req(struct cache_deferred_req *dreq)
|
|
|
|
{
|
2010-08-12 15:04:08 +08:00
|
|
|
hlist_del_init(&dreq->hash);
|
2010-10-07 12:29:46 +08:00
|
|
|
if (!list_empty(&dreq->recent)) {
|
|
|
|
list_del_init(&dreq->recent);
|
|
|
|
cache_defer_cnt--;
|
|
|
|
}
|
2010-08-27 01:19:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int hash = DFR_HASH(item);
|
|
|
|
|
2010-10-07 12:29:46 +08:00
|
|
|
INIT_LIST_HEAD(&dreq->recent);
|
2010-08-12 15:04:08 +08:00
|
|
|
hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
|
2010-08-27 01:19:52 +08:00
|
|
|
}
|
|
|
|
|
2010-10-07 12:29:46 +08:00
|
|
|
static void setup_deferral(struct cache_deferred_req *dreq,
|
|
|
|
struct cache_head *item,
|
|
|
|
int count_me)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
dreq->item = item;
|
|
|
|
|
|
|
|
spin_lock(&cache_defer_lock);
|
|
|
|
|
2010-08-27 01:19:52 +08:00
|
|
|
__hash_deferred_req(dreq, item);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-10-07 12:29:46 +08:00
|
|
|
if (count_me) {
|
|
|
|
cache_defer_cnt++;
|
|
|
|
list_add(&dreq->recent, &cache_defer_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2010-10-07 12:29:46 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock(&cache_defer_lock);
|
|
|
|
|
2010-08-27 04:56:23 +08:00
|
|
|
}
|
sunrpc/cache: allow threads to block while waiting for cache update.
The current practice of waiting for cache updates by queueing the
whole request to be retried has (at least) two problems.
1/ With NFSv4, requests can be quite complex and re-trying a whole
request when a later part fails should only be a last-resort, not a
normal practice.
2/ Large requests, and in particular any 'write' request, will not be
queued by the current code and doing so would be undesirable.
In many cases only a very sort wait is needed before the cache gets
valid data.
So, providing the underlying transport permits it by setting
->thread_wait,
arrange to wait briefly for an upcall to be completed (as reflected in
the clearing of CACHE_PENDING).
If the short wait was not long enough and CACHE_PENDING is still set,
fall back on the old approach.
The 'thread_wait' value is set to 5 seconds when there are spare
threads, and 1 second when there are no spare threads.
These values are probably much higher than needed, but will ensure
some forward progress.
Note that as we only request an update for a non-valid item, and as
non-valid items are updated in place it is extremely unlikely that
cache_check will return -ETIMEDOUT. Normally cache_defer_req will
sleep for a short while and then find that the item is_valid.
Signed-off-by: NeilBrown <neilb@suse.de>
Signed-off-by: J. Bruce Fields <bfields@redhat.com>
2010-08-12 15:04:06 +08:00
|
|
|
|
2010-08-27 04:56:23 +08:00
|
|
|
struct thread_deferred_req {
|
|
|
|
struct cache_deferred_req handle;
|
|
|
|
struct completion completion;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
|
|
|
|
{
|
|
|
|
struct thread_deferred_req *dr =
|
|
|
|
container_of(dreq, struct thread_deferred_req, handle);
|
|
|
|
complete(&dr->completion);
|
|
|
|
}
|
|
|
|
|
2010-10-07 12:29:46 +08:00
|
|
|
static void cache_wait_req(struct cache_req *req, struct cache_head *item)
|
2010-08-27 04:56:23 +08:00
|
|
|
{
|
|
|
|
struct thread_deferred_req sleeper;
|
|
|
|
struct cache_deferred_req *dreq = &sleeper.handle;
|
|
|
|
|
|
|
|
sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
|
|
|
|
dreq->revisit = cache_restart_thread;
|
|
|
|
|
2010-10-07 12:29:46 +08:00
|
|
|
setup_deferral(dreq, item, 0);
|
2010-08-27 04:56:23 +08:00
|
|
|
|
2010-10-07 12:29:46 +08:00
|
|
|
if (!test_bit(CACHE_PENDING, &item->flags) ||
|
2010-09-22 10:55:06 +08:00
|
|
|
wait_for_completion_interruptible_timeout(
|
2010-08-27 04:56:23 +08:00
|
|
|
&sleeper.completion, req->thread_wait) <= 0) {
|
|
|
|
/* The completion wasn't completed, so we need
|
|
|
|
* to clean up
|
|
|
|
*/
|
|
|
|
spin_lock(&cache_defer_lock);
|
2010-08-12 15:04:08 +08:00
|
|
|
if (!hlist_unhashed(&sleeper.handle.hash)) {
|
2010-08-27 04:56:23 +08:00
|
|
|
__unhash_deferred_req(&sleeper.handle);
|
|
|
|
spin_unlock(&cache_defer_lock);
|
|
|
|
} else {
|
|
|
|
/* cache_revisit_request already removed
|
|
|
|
* this from the hash table, but hasn't
|
|
|
|
* called ->revisit yet. It will very soon
|
|
|
|
* and we need to wait for it.
|
sunrpc/cache: allow threads to block while waiting for cache update.
The current practice of waiting for cache updates by queueing the
whole request to be retried has (at least) two problems.
1/ With NFSv4, requests can be quite complex and re-trying a whole
request when a later part fails should only be a last-resort, not a
normal practice.
2/ Large requests, and in particular any 'write' request, will not be
queued by the current code and doing so would be undesirable.
In many cases only a very sort wait is needed before the cache gets
valid data.
So, providing the underlying transport permits it by setting
->thread_wait,
arrange to wait briefly for an upcall to be completed (as reflected in
the clearing of CACHE_PENDING).
If the short wait was not long enough and CACHE_PENDING is still set,
fall back on the old approach.
The 'thread_wait' value is set to 5 seconds when there are spare
threads, and 1 second when there are no spare threads.
These values are probably much higher than needed, but will ensure
some forward progress.
Note that as we only request an update for a non-valid item, and as
non-valid items are updated in place it is extremely unlikely that
cache_check will return -ETIMEDOUT. Normally cache_defer_req will
sleep for a short while and then find that the item is_valid.
Signed-off-by: NeilBrown <neilb@suse.de>
Signed-off-by: J. Bruce Fields <bfields@redhat.com>
2010-08-12 15:04:06 +08:00
|
|
|
*/
|
2010-08-27 04:56:23 +08:00
|
|
|
spin_unlock(&cache_defer_lock);
|
|
|
|
wait_for_completion(&sleeper.completion);
|
sunrpc/cache: allow threads to block while waiting for cache update.
The current practice of waiting for cache updates by queueing the
whole request to be retried has (at least) two problems.
1/ With NFSv4, requests can be quite complex and re-trying a whole
request when a later part fails should only be a last-resort, not a
normal practice.
2/ Large requests, and in particular any 'write' request, will not be
queued by the current code and doing so would be undesirable.
In many cases only a very sort wait is needed before the cache gets
valid data.
So, providing the underlying transport permits it by setting
->thread_wait,
arrange to wait briefly for an upcall to be completed (as reflected in
the clearing of CACHE_PENDING).
If the short wait was not long enough and CACHE_PENDING is still set,
fall back on the old approach.
The 'thread_wait' value is set to 5 seconds when there are spare
threads, and 1 second when there are no spare threads.
These values are probably much higher than needed, but will ensure
some forward progress.
Note that as we only request an update for a non-valid item, and as
non-valid items are updated in place it is extremely unlikely that
cache_check will return -ETIMEDOUT. Normally cache_defer_req will
sleep for a short while and then find that the item is_valid.
Signed-off-by: NeilBrown <neilb@suse.de>
Signed-off-by: J. Bruce Fields <bfields@redhat.com>
2010-08-12 15:04:06 +08:00
|
|
|
}
|
2010-08-27 04:56:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-07 12:29:46 +08:00
|
|
|
static void cache_limit_defers(void)
|
2010-08-27 04:56:23 +08:00
|
|
|
{
|
2010-10-07 12:29:46 +08:00
|
|
|
/* Make sure we haven't exceed the limit of allowed deferred
|
|
|
|
* requests.
|
|
|
|
*/
|
|
|
|
struct cache_deferred_req *discard = NULL;
|
2010-08-27 04:56:23 +08:00
|
|
|
|
2010-10-07 12:29:46 +08:00
|
|
|
if (cache_defer_cnt <= DFR_MAX)
|
|
|
|
return;
|
2010-10-07 12:29:46 +08:00
|
|
|
|
2010-10-07 12:29:46 +08:00
|
|
|
spin_lock(&cache_defer_lock);
|
|
|
|
|
|
|
|
/* Consider removing either the first or the last */
|
|
|
|
if (cache_defer_cnt > DFR_MAX) {
|
|
|
|
if (net_random() & 1)
|
|
|
|
discard = list_entry(cache_defer_list.next,
|
|
|
|
struct cache_deferred_req, recent);
|
|
|
|
else
|
|
|
|
discard = list_entry(cache_defer_list.prev,
|
|
|
|
struct cache_deferred_req, recent);
|
|
|
|
__unhash_deferred_req(discard);
|
|
|
|
}
|
|
|
|
spin_unlock(&cache_defer_lock);
|
2009-09-09 14:32:54 +08:00
|
|
|
if (discard)
|
|
|
|
discard->revisit(discard, 1);
|
2010-10-07 12:29:46 +08:00
|
|
|
}
|
2009-09-09 14:32:54 +08:00
|
|
|
|
2011-01-03 10:28:34 +08:00
|
|
|
/* Return true if and only if a deferred request is queued. */
|
|
|
|
static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
|
2010-10-07 12:29:46 +08:00
|
|
|
{
|
|
|
|
struct cache_deferred_req *dreq;
|
2010-10-07 12:29:46 +08:00
|
|
|
|
2010-08-27 04:56:23 +08:00
|
|
|
if (req->thread_wait) {
|
2010-10-07 12:29:46 +08:00
|
|
|
cache_wait_req(req, item);
|
|
|
|
if (!test_bit(CACHE_PENDING, &item->flags))
|
2011-01-03 10:28:34 +08:00
|
|
|
return false;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2010-08-27 04:56:23 +08:00
|
|
|
dreq = req->defer(req);
|
|
|
|
if (dreq == NULL)
|
2011-01-03 10:28:34 +08:00
|
|
|
return false;
|
2010-10-07 12:29:46 +08:00
|
|
|
setup_deferral(dreq, item, 1);
|
2010-10-07 12:29:46 +08:00
|
|
|
if (!test_bit(CACHE_PENDING, &item->flags))
|
|
|
|
/* Bit could have been cleared before we managed to
|
|
|
|
* set up the deferral, so need to revisit just in case
|
|
|
|
*/
|
|
|
|
cache_revisit_request(item);
|
2010-10-07 12:29:46 +08:00
|
|
|
|
|
|
|
cache_limit_defers();
|
2011-01-03 10:28:34 +08:00
|
|
|
return true;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cache_revisit_request(struct cache_head *item)
|
|
|
|
{
|
|
|
|
struct cache_deferred_req *dreq;
|
|
|
|
struct list_head pending;
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 09:06:00 +08:00
|
|
|
struct hlist_node *tmp;
|
2005-04-17 06:20:36 +08:00
|
|
|
int hash = DFR_HASH(item);
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&pending);
|
|
|
|
spin_lock(&cache_defer_lock);
|
2007-02-10 07:38:13 +08:00
|
|
|
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 09:06:00 +08:00
|
|
|
hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
|
2010-08-12 15:04:08 +08:00
|
|
|
if (dreq->item == item) {
|
|
|
|
__unhash_deferred_req(dreq);
|
|
|
|
list_add(&dreq->recent, &pending);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2010-08-12 15:04:08 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock(&cache_defer_lock);
|
|
|
|
|
|
|
|
while (!list_empty(&pending)) {
|
|
|
|
dreq = list_entry(pending.next, struct cache_deferred_req, recent);
|
|
|
|
list_del_init(&dreq->recent);
|
|
|
|
dreq->revisit(dreq, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cache_clean_deferred(void *owner)
|
|
|
|
{
|
|
|
|
struct cache_deferred_req *dreq, *tmp;
|
|
|
|
struct list_head pending;
|
|
|
|
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&pending);
|
|
|
|
spin_lock(&cache_defer_lock);
|
2007-02-10 07:38:13 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
|
|
|
|
if (dreq->owner == owner) {
|
2010-08-27 01:19:52 +08:00
|
|
|
__unhash_deferred_req(dreq);
|
2010-09-22 10:55:06 +08:00
|
|
|
list_add(&dreq->recent, &pending);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock(&cache_defer_lock);
|
|
|
|
|
|
|
|
while (!list_empty(&pending)) {
|
|
|
|
dreq = list_entry(pending.next, struct cache_deferred_req, recent);
|
|
|
|
list_del_init(&dreq->recent);
|
|
|
|
dreq->revisit(dreq, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* communicate with user-space
|
|
|
|
*
|
2007-11-07 03:15:19 +08:00
|
|
|
* We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
|
|
|
|
* On read, you get a full request, or block.
|
|
|
|
* On write, an update request is processed.
|
|
|
|
* Poll works if anything to read, and always allows write.
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
2007-02-10 07:38:13 +08:00
|
|
|
* Implemented by linked list of requests. Each open file has
|
2007-11-07 03:15:19 +08:00
|
|
|
* a ->private that also exists in this list. New requests are added
|
2005-04-17 06:20:36 +08:00
|
|
|
* to the end and may wakeup and preceding readers.
|
|
|
|
* New readers are added to the head. If, on read, an item is found with
|
|
|
|
* CACHE_UPCALLING clear, we free it from the list.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(queue_lock);
|
2006-03-21 14:33:17 +08:00
|
|
|
static DEFINE_MUTEX(queue_io_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct cache_queue {
|
|
|
|
struct list_head list;
|
|
|
|
int reader; /* if 0, then request */
|
|
|
|
};
|
|
|
|
struct cache_request {
|
|
|
|
struct cache_queue q;
|
|
|
|
struct cache_head *item;
|
|
|
|
char * buf;
|
|
|
|
int len;
|
|
|
|
int readers;
|
|
|
|
};
|
|
|
|
struct cache_reader {
|
|
|
|
struct cache_queue q;
|
|
|
|
int offset; /* if non-0, we have a refcnt on next request */
|
|
|
|
};
|
|
|
|
|
2013-02-04 19:03:03 +08:00
|
|
|
static int cache_request(struct cache_detail *detail,
|
|
|
|
struct cache_request *crq)
|
|
|
|
{
|
|
|
|
char *bp = crq->buf;
|
|
|
|
int len = PAGE_SIZE;
|
|
|
|
|
|
|
|
detail->cache_request(detail, crq->item, &bp, &len);
|
|
|
|
if (len < 0)
|
|
|
|
return -EAGAIN;
|
|
|
|
return PAGE_SIZE - len;
|
|
|
|
}
|
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
|
|
|
|
loff_t *ppos, struct cache_detail *cd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct cache_reader *rp = filp->private_data;
|
|
|
|
struct cache_request *rq;
|
2013-01-24 06:07:38 +08:00
|
|
|
struct inode *inode = file_inode(filp);
|
2005-04-17 06:20:36 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
return 0;
|
|
|
|
|
2009-08-10 03:14:28 +08:00
|
|
|
mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
|
2005-04-17 06:20:36 +08:00
|
|
|
* readers on this file */
|
|
|
|
again:
|
|
|
|
spin_lock(&queue_lock);
|
|
|
|
/* need to find next request */
|
|
|
|
while (rp->q.list.next != &cd->queue &&
|
|
|
|
list_entry(rp->q.list.next, struct cache_queue, list)
|
|
|
|
->reader) {
|
|
|
|
struct list_head *next = rp->q.list.next;
|
|
|
|
list_move(&rp->q.list, next);
|
|
|
|
}
|
|
|
|
if (rp->q.list.next == &cd->queue) {
|
|
|
|
spin_unlock(&queue_lock);
|
2009-08-10 03:14:28 +08:00
|
|
|
mutex_unlock(&inode->i_mutex);
|
2012-10-23 22:43:36 +08:00
|
|
|
WARN_ON_ONCE(rp->offset);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rq = container_of(rp->q.list.next, struct cache_request, q.list);
|
2012-10-23 22:43:36 +08:00
|
|
|
WARN_ON_ONCE(rq->q.reader);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (rp->offset == 0)
|
|
|
|
rq->readers++;
|
|
|
|
spin_unlock(&queue_lock);
|
|
|
|
|
2013-02-04 19:03:03 +08:00
|
|
|
if (rq->len == 0) {
|
|
|
|
err = cache_request(cd, rq);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
|
|
|
rq->len = err;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
|
|
|
|
err = -EAGAIN;
|
|
|
|
spin_lock(&queue_lock);
|
|
|
|
list_move(&rp->q.list, &rq->q.list);
|
|
|
|
spin_unlock(&queue_lock);
|
|
|
|
} else {
|
|
|
|
if (rp->offset + count > rq->len)
|
|
|
|
count = rq->len - rp->offset;
|
|
|
|
err = -EFAULT;
|
|
|
|
if (copy_to_user(buf, rq->buf + rp->offset, count))
|
|
|
|
goto out;
|
|
|
|
rp->offset += count;
|
|
|
|
if (rp->offset >= rq->len) {
|
|
|
|
rp->offset = 0;
|
|
|
|
spin_lock(&queue_lock);
|
|
|
|
list_move(&rp->q.list, &rq->q.list);
|
|
|
|
spin_unlock(&queue_lock);
|
|
|
|
}
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (rp->offset == 0) {
|
|
|
|
/* need to release rq */
|
|
|
|
spin_lock(&queue_lock);
|
|
|
|
rq->readers--;
|
|
|
|
if (rq->readers == 0 &&
|
|
|
|
!test_bit(CACHE_PENDING, &rq->item->flags)) {
|
|
|
|
list_del(&rq->q.list);
|
|
|
|
spin_unlock(&queue_lock);
|
2006-03-27 17:15:09 +08:00
|
|
|
cache_put(rq->item, cd);
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(rq->buf);
|
|
|
|
kfree(rq);
|
|
|
|
} else
|
|
|
|
spin_unlock(&queue_lock);
|
|
|
|
}
|
|
|
|
if (err == -EAGAIN)
|
|
|
|
goto again;
|
2009-08-10 03:14:28 +08:00
|
|
|
mutex_unlock(&inode->i_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
return err ? err : count;
|
|
|
|
}
|
|
|
|
|
2009-08-10 03:14:28 +08:00
|
|
|
static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
|
|
|
|
size_t count, struct cache_detail *cd)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-01-18 17:56:02 +08:00
|
|
|
if (count == 0)
|
|
|
|
return -EINVAL;
|
2009-08-10 03:14:28 +08:00
|
|
|
if (copy_from_user(kaddr, buf, count))
|
|
|
|
return -EFAULT;
|
|
|
|
kaddr[count] = '\0';
|
|
|
|
ret = cd->cache_parse(cd, kaddr, count);
|
|
|
|
if (!ret)
|
|
|
|
ret = count;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t cache_slow_downcall(const char __user *buf,
|
|
|
|
size_t count, struct cache_detail *cd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2009-08-10 03:14:28 +08:00
|
|
|
static char write_buf[8192]; /* protected by queue_io_mutex */
|
|
|
|
ssize_t ret = -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (count >= sizeof(write_buf))
|
2009-08-10 03:14:28 +08:00
|
|
|
goto out;
|
2006-03-21 14:33:17 +08:00
|
|
|
mutex_lock(&queue_io_mutex);
|
2009-08-10 03:14:28 +08:00
|
|
|
ret = cache_do_downcall(write_buf, buf, count, cd);
|
|
|
|
mutex_unlock(&queue_io_mutex);
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-08-10 03:14:28 +08:00
|
|
|
static ssize_t cache_downcall(struct address_space *mapping,
|
|
|
|
const char __user *buf,
|
|
|
|
size_t count, struct cache_detail *cd)
|
|
|
|
{
|
|
|
|
struct page *page;
|
|
|
|
char *kaddr;
|
|
|
|
ssize_t ret = -ENOMEM;
|
|
|
|
|
|
|
|
if (count >= PAGE_CACHE_SIZE)
|
|
|
|
goto out_slow;
|
|
|
|
|
|
|
|
page = find_or_create_page(mapping, 0, GFP_KERNEL);
|
|
|
|
if (!page)
|
|
|
|
goto out_slow;
|
|
|
|
|
|
|
|
kaddr = kmap(page);
|
|
|
|
ret = cache_do_downcall(kaddr, buf, count, cd);
|
|
|
|
kunmap(page);
|
|
|
|
unlock_page(page);
|
|
|
|
page_cache_release(page);
|
|
|
|
return ret;
|
|
|
|
out_slow:
|
|
|
|
return cache_slow_downcall(buf, count, cd);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
static ssize_t cache_write(struct file *filp, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos,
|
|
|
|
struct cache_detail *cd)
|
2009-08-10 03:14:28 +08:00
|
|
|
{
|
|
|
|
struct address_space *mapping = filp->f_mapping;
|
2013-01-24 06:07:38 +08:00
|
|
|
struct inode *inode = file_inode(filp);
|
2009-08-10 03:14:28 +08:00
|
|
|
ssize_t ret = -EINVAL;
|
|
|
|
|
|
|
|
if (!cd->cache_parse)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
mutex_lock(&inode->i_mutex);
|
|
|
|
ret = cache_downcall(mapping, buf, count, cd);
|
|
|
|
mutex_unlock(&inode->i_mutex);
|
|
|
|
out:
|
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
|
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
static unsigned int cache_poll(struct file *filp, poll_table *wait,
|
|
|
|
struct cache_detail *cd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned int mask;
|
|
|
|
struct cache_reader *rp = filp->private_data;
|
|
|
|
struct cache_queue *cq;
|
|
|
|
|
|
|
|
poll_wait(filp, &queue_wait, wait);
|
|
|
|
|
|
|
|
/* alway allow write */
|
|
|
|
mask = POLL_OUT | POLLWRNORM;
|
|
|
|
|
|
|
|
if (!rp)
|
|
|
|
return mask;
|
|
|
|
|
|
|
|
spin_lock(&queue_lock);
|
|
|
|
|
|
|
|
for (cq= &rp->q; &cq->list != &cd->queue;
|
|
|
|
cq = list_entry(cq->list.next, struct cache_queue, list))
|
|
|
|
if (!cq->reader) {
|
|
|
|
mask |= POLLIN | POLLRDNORM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
spin_unlock(&queue_lock);
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
static int cache_ioctl(struct inode *ino, struct file *filp,
|
|
|
|
unsigned int cmd, unsigned long arg,
|
|
|
|
struct cache_detail *cd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
struct cache_reader *rp = filp->private_data;
|
|
|
|
struct cache_queue *cq;
|
|
|
|
|
|
|
|
if (cmd != FIONREAD || !rp)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
spin_lock(&queue_lock);
|
|
|
|
|
|
|
|
/* only find the length remaining in current request,
|
|
|
|
* or the length of the next request
|
|
|
|
*/
|
|
|
|
for (cq= &rp->q; &cq->list != &cd->queue;
|
|
|
|
cq = list_entry(cq->list.next, struct cache_queue, list))
|
|
|
|
if (!cq->reader) {
|
|
|
|
struct cache_request *cr =
|
|
|
|
container_of(cq, struct cache_request, q);
|
|
|
|
len = cr->len - rp->offset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
spin_unlock(&queue_lock);
|
|
|
|
|
|
|
|
return put_user(len, (int __user *)arg);
|
|
|
|
}
|
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
static int cache_open(struct inode *inode, struct file *filp,
|
|
|
|
struct cache_detail *cd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct cache_reader *rp = NULL;
|
|
|
|
|
2009-08-20 06:13:00 +08:00
|
|
|
if (!cd || !try_module_get(cd->owner))
|
|
|
|
return -EACCES;
|
2005-04-17 06:20:36 +08:00
|
|
|
nonseekable_open(inode, filp);
|
|
|
|
if (filp->f_mode & FMODE_READ) {
|
|
|
|
rp = kmalloc(sizeof(*rp), GFP_KERNEL);
|
2013-03-23 04:36:44 +08:00
|
|
|
if (!rp) {
|
|
|
|
module_put(cd->owner);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
2013-03-23 04:36:44 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
rp->offset = 0;
|
|
|
|
rp->q.reader = 1;
|
|
|
|
atomic_inc(&cd->readers);
|
|
|
|
spin_lock(&queue_lock);
|
|
|
|
list_add(&rp->q.list, &cd->queue);
|
|
|
|
spin_unlock(&queue_lock);
|
|
|
|
}
|
|
|
|
filp->private_data = rp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
static int cache_release(struct inode *inode, struct file *filp,
|
|
|
|
struct cache_detail *cd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct cache_reader *rp = filp->private_data;
|
|
|
|
|
|
|
|
if (rp) {
|
|
|
|
spin_lock(&queue_lock);
|
|
|
|
if (rp->offset) {
|
|
|
|
struct cache_queue *cq;
|
|
|
|
for (cq= &rp->q; &cq->list != &cd->queue;
|
|
|
|
cq = list_entry(cq->list.next, struct cache_queue, list))
|
|
|
|
if (!cq->reader) {
|
|
|
|
container_of(cq, struct cache_request, q)
|
|
|
|
->readers--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rp->offset = 0;
|
|
|
|
}
|
|
|
|
list_del(&rp->q.list);
|
|
|
|
spin_unlock(&queue_lock);
|
|
|
|
|
|
|
|
filp->private_data = NULL;
|
|
|
|
kfree(rp);
|
|
|
|
|
2010-08-12 14:55:22 +08:00
|
|
|
cd->last_close = seconds_since_boot();
|
2005-04-17 06:20:36 +08:00
|
|
|
atomic_dec(&cd->readers);
|
|
|
|
}
|
2009-08-20 06:13:00 +08:00
|
|
|
module_put(cd->owner);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-04 13:22:38 +08:00
|
|
|
static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct cache_queue *cq;
|
|
|
|
spin_lock(&queue_lock);
|
|
|
|
list_for_each_entry(cq, &detail->queue, list)
|
|
|
|
if (!cq->reader) {
|
|
|
|
struct cache_request *cr = container_of(cq, struct cache_request, q);
|
|
|
|
if (cr->item != ch)
|
|
|
|
continue;
|
|
|
|
if (cr->readers != 0)
|
2006-03-27 17:15:07 +08:00
|
|
|
continue;
|
2005-04-17 06:20:36 +08:00
|
|
|
list_del(&cr->q.list);
|
|
|
|
spin_unlock(&queue_lock);
|
2006-03-27 17:15:09 +08:00
|
|
|
cache_put(cr->item, detail);
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(cr->buf);
|
|
|
|
kfree(cr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
spin_unlock(&queue_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Support routines for text-based upcalls.
|
|
|
|
* Fields are separated by spaces.
|
|
|
|
* Fields are either mangled to quote space tab newline slosh with slosh
|
|
|
|
* or a hexified with a leading \x
|
|
|
|
* Record is terminated with newline.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void qword_add(char **bpp, int *lp, char *str)
|
|
|
|
{
|
|
|
|
char *bp = *bpp;
|
|
|
|
int len = *lp;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (len < 0) return;
|
|
|
|
|
|
|
|
while ((c=*str++) && len)
|
|
|
|
switch(c) {
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\\':
|
|
|
|
if (len >= 4) {
|
|
|
|
*bp++ = '\\';
|
|
|
|
*bp++ = '0' + ((c & 0300)>>6);
|
|
|
|
*bp++ = '0' + ((c & 0070)>>3);
|
|
|
|
*bp++ = '0' + ((c & 0007)>>0);
|
|
|
|
}
|
|
|
|
len -= 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*bp++ = c;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
if (c || len <1) len = -1;
|
|
|
|
else {
|
|
|
|
*bp++ = ' ';
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
*bpp = bp;
|
|
|
|
*lp = len;
|
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(qword_add);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
void qword_addhex(char **bpp, int *lp, char *buf, int blen)
|
|
|
|
{
|
|
|
|
char *bp = *bpp;
|
|
|
|
int len = *lp;
|
|
|
|
|
|
|
|
if (len < 0) return;
|
|
|
|
|
|
|
|
if (len > 2) {
|
|
|
|
*bp++ = '\\';
|
|
|
|
*bp++ = 'x';
|
|
|
|
len -= 2;
|
|
|
|
while (blen && len >= 2) {
|
|
|
|
unsigned char c = *buf++;
|
|
|
|
*bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
|
|
|
|
*bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
|
|
|
|
len -= 2;
|
|
|
|
blen--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (blen || len<1) len = -1;
|
|
|
|
else {
|
|
|
|
*bp++ = ' ';
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
*bpp = bp;
|
|
|
|
*lp = len;
|
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(qword_addhex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static void warn_no_listener(struct cache_detail *detail)
|
|
|
|
{
|
|
|
|
if (detail->last_warn != detail->last_close) {
|
|
|
|
detail->last_warn = detail->last_close;
|
|
|
|
if (detail->warn_no_listener)
|
2009-08-10 03:14:26 +08:00
|
|
|
detail->warn_no_listener(detail, detail->last_close != 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-20 10:55:06 +08:00
|
|
|
static bool cache_listeners_exist(struct cache_detail *detail)
|
|
|
|
{
|
|
|
|
if (atomic_read(&detail->readers))
|
|
|
|
return true;
|
|
|
|
if (detail->last_close == 0)
|
|
|
|
/* This cache was never opened */
|
|
|
|
return false;
|
|
|
|
if (detail->last_close < seconds_since_boot() - 30)
|
|
|
|
/*
|
|
|
|
* We allow for the possibility that someone might
|
|
|
|
* restart a userspace daemon without restarting the
|
|
|
|
* server; but after 30 seconds, we give up.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2009-08-10 03:14:29 +08:00
|
|
|
* register an upcall request to user-space and queue it up for read() by the
|
|
|
|
* upcall daemon.
|
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Each request is at most one page long.
|
|
|
|
*/
|
2013-02-04 19:02:55 +08:00
|
|
|
int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
char *buf;
|
|
|
|
struct cache_request *crq;
|
|
|
|
|
2013-02-04 19:02:50 +08:00
|
|
|
if (!detail->cache_request)
|
|
|
|
return -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-09-20 10:55:06 +08:00
|
|
|
if (!cache_listeners_exist(detail)) {
|
|
|
|
warn_no_listener(detail);
|
|
|
|
return -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
|
|
|
|
if (!buf)
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
crq = kmalloc(sizeof (*crq), GFP_KERNEL);
|
|
|
|
if (!crq) {
|
|
|
|
kfree(buf);
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
crq->q.reader = 0;
|
|
|
|
crq->item = cache_get(h);
|
|
|
|
crq->buf = buf;
|
2013-02-04 19:03:03 +08:00
|
|
|
crq->len = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
crq->readers = 0;
|
|
|
|
spin_lock(&queue_lock);
|
|
|
|
list_add_tail(&crq->q.list, &detail->queue);
|
|
|
|
spin_unlock(&queue_lock);
|
|
|
|
wake_up(&queue_wait);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-10 03:14:29 +08:00
|
|
|
EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* parse a message from user-space and pass it
|
|
|
|
* to an appropriate cache
|
|
|
|
* Messages are, like requests, separated into fields by
|
|
|
|
* spaces and dequotes as \xHEXSTRING or embedded \nnn octal
|
|
|
|
*
|
2007-02-10 07:38:13 +08:00
|
|
|
* Message is
|
2005-04-17 06:20:36 +08:00
|
|
|
* reply cachename expiry key ... content....
|
|
|
|
*
|
2007-02-10 07:38:13 +08:00
|
|
|
* key and content are both parsed by cache
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
int qword_get(char **bpp, char *dest, int bufsize)
|
|
|
|
{
|
|
|
|
/* return bytes copied, or -1 on error */
|
|
|
|
char *bp = *bpp;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
while (*bp == ' ') bp++;
|
|
|
|
|
|
|
|
if (bp[0] == '\\' && bp[1] == 'x') {
|
|
|
|
/* HEX STRING */
|
|
|
|
bp += 2;
|
2010-09-21 14:40:25 +08:00
|
|
|
while (len < bufsize) {
|
|
|
|
int h, l;
|
|
|
|
|
|
|
|
h = hex_to_bin(bp[0]);
|
|
|
|
if (h < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
l = hex_to_bin(bp[1]);
|
|
|
|
if (l < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
*dest++ = (h << 4) | l;
|
|
|
|
bp += 2;
|
2005-04-17 06:20:36 +08:00
|
|
|
len++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* text with \nnn octal quoting */
|
|
|
|
while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
|
|
|
|
if (*bp == '\\' &&
|
|
|
|
isodigit(bp[1]) && (bp[1] <= '3') &&
|
|
|
|
isodigit(bp[2]) &&
|
|
|
|
isodigit(bp[3])) {
|
|
|
|
int byte = (*++bp -'0');
|
|
|
|
bp++;
|
|
|
|
byte = (byte << 3) | (*bp++ - '0');
|
|
|
|
byte = (byte << 3) | (*bp++ - '0');
|
|
|
|
*dest++ = byte;
|
|
|
|
len++;
|
|
|
|
} else {
|
|
|
|
*dest++ = *bp++;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*bp != ' ' && *bp != '\n' && *bp != '\0')
|
|
|
|
return -1;
|
|
|
|
while (*bp == ' ') bp++;
|
|
|
|
*bpp = bp;
|
|
|
|
*dest = '\0';
|
|
|
|
return len;
|
|
|
|
}
|
2008-12-24 05:30:12 +08:00
|
|
|
EXPORT_SYMBOL_GPL(qword_get);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* support /proc/sunrpc/cache/$CACHENAME/content
|
|
|
|
* as a seqfile.
|
|
|
|
* We call ->cache_show passing NULL for the item to
|
|
|
|
* get a header, then pass each real item in the cache
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct handle {
|
|
|
|
struct cache_detail *cd;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *c_start(struct seq_file *m, loff_t *pos)
|
2008-01-02 13:58:02 +08:00
|
|
|
__acquires(cd->hash_lock)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
loff_t n = *pos;
|
2012-04-15 13:58:06 +08:00
|
|
|
unsigned int hash, entry;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct cache_head *ch;
|
|
|
|
struct cache_detail *cd = ((struct handle*)m->private)->cd;
|
2007-02-10 07:38:13 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
read_lock(&cd->hash_lock);
|
|
|
|
if (!n--)
|
|
|
|
return SEQ_START_TOKEN;
|
|
|
|
hash = n >> 32;
|
|
|
|
entry = n & ((1LL<<32) - 1);
|
|
|
|
|
|
|
|
for (ch=cd->hash_table[hash]; ch; ch=ch->next)
|
|
|
|
if (!entry--)
|
|
|
|
return ch;
|
|
|
|
n &= ~((1LL<<32) - 1);
|
|
|
|
do {
|
|
|
|
hash++;
|
|
|
|
n += 1LL<<32;
|
2007-02-10 07:38:13 +08:00
|
|
|
} while(hash < cd->hash_size &&
|
2005-04-17 06:20:36 +08:00
|
|
|
cd->hash_table[hash]==NULL);
|
|
|
|
if (hash >= cd->hash_size)
|
|
|
|
return NULL;
|
|
|
|
*pos = n+1;
|
|
|
|
return cd->hash_table[hash];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *c_next(struct seq_file *m, void *p, loff_t *pos)
|
|
|
|
{
|
|
|
|
struct cache_head *ch = p;
|
|
|
|
int hash = (*pos >> 32);
|
|
|
|
struct cache_detail *cd = ((struct handle*)m->private)->cd;
|
|
|
|
|
|
|
|
if (p == SEQ_START_TOKEN)
|
|
|
|
hash = 0;
|
|
|
|
else if (ch->next == NULL) {
|
|
|
|
hash++;
|
|
|
|
*pos += 1LL<<32;
|
|
|
|
} else {
|
|
|
|
++*pos;
|
|
|
|
return ch->next;
|
|
|
|
}
|
|
|
|
*pos &= ~((1LL<<32) - 1);
|
|
|
|
while (hash < cd->hash_size &&
|
|
|
|
cd->hash_table[hash] == NULL) {
|
|
|
|
hash++;
|
|
|
|
*pos += 1LL<<32;
|
|
|
|
}
|
|
|
|
if (hash >= cd->hash_size)
|
|
|
|
return NULL;
|
|
|
|
++*pos;
|
|
|
|
return cd->hash_table[hash];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void c_stop(struct seq_file *m, void *p)
|
2008-01-02 13:58:02 +08:00
|
|
|
__releases(cd->hash_lock)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct cache_detail *cd = ((struct handle*)m->private)->cd;
|
|
|
|
read_unlock(&cd->hash_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int c_show(struct seq_file *m, void *p)
|
|
|
|
{
|
|
|
|
struct cache_head *cp = p;
|
|
|
|
struct cache_detail *cd = ((struct handle*)m->private)->cd;
|
|
|
|
|
|
|
|
if (p == SEQ_START_TOKEN)
|
|
|
|
return cd->cache_show(m, cd, NULL);
|
|
|
|
|
|
|
|
ifdebug(CACHE)
|
2006-03-27 17:15:07 +08:00
|
|
|
seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
|
2010-08-12 14:55:22 +08:00
|
|
|
convert_to_wallclock(cp->expiry_time),
|
|
|
|
atomic_read(&cp->ref.refcount), cp->flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
cache_get(cp);
|
|
|
|
if (cache_check(cd, cp, NULL))
|
|
|
|
/* cache_check does a cache_put on failure */
|
|
|
|
seq_printf(m, "# ");
|
2012-07-12 08:37:34 +08:00
|
|
|
else {
|
|
|
|
if (cache_is_expired(cd, cp))
|
|
|
|
seq_printf(m, "# ");
|
2005-04-17 06:20:36 +08:00
|
|
|
cache_put(cp, cd);
|
2012-07-12 08:37:34 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return cd->cache_show(m, cd, cp);
|
|
|
|
}
|
|
|
|
|
2007-07-11 14:07:31 +08:00
|
|
|
static const struct seq_operations cache_content_op = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.start = c_start,
|
|
|
|
.next = c_next,
|
|
|
|
.stop = c_stop,
|
|
|
|
.show = c_show,
|
|
|
|
};
|
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
static int content_open(struct inode *inode, struct file *file,
|
|
|
|
struct cache_detail *cd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct handle *han;
|
|
|
|
|
2009-08-20 06:13:00 +08:00
|
|
|
if (!cd || !try_module_get(cd->owner))
|
|
|
|
return -EACCES;
|
2007-10-10 17:31:07 +08:00
|
|
|
han = __seq_open_private(file, &cache_content_op, sizeof(*han));
|
2010-03-12 06:08:10 +08:00
|
|
|
if (han == NULL) {
|
|
|
|
module_put(cd->owner);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
2010-03-12 06:08:10 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
han->cd = cd;
|
2007-10-10 17:31:07 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2009-08-20 06:13:00 +08:00
|
|
|
static int content_release(struct inode *inode, struct file *file,
|
|
|
|
struct cache_detail *cd)
|
|
|
|
{
|
|
|
|
int ret = seq_release_private(inode, file);
|
|
|
|
module_put(cd->owner);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_flush(struct inode *inode, struct file *file,
|
|
|
|
struct cache_detail *cd)
|
|
|
|
{
|
|
|
|
if (!cd || !try_module_get(cd->owner))
|
|
|
|
return -EACCES;
|
|
|
|
return nonseekable_open(inode, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int release_flush(struct inode *inode, struct file *file,
|
|
|
|
struct cache_detail *cd)
|
|
|
|
{
|
|
|
|
module_put(cd->owner);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static ssize_t read_flush(struct file *file, char __user *buf,
|
2009-08-10 03:14:29 +08:00
|
|
|
size_t count, loff_t *ppos,
|
|
|
|
struct cache_detail *cd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-07-17 06:01:26 +08:00
|
|
|
char tbuf[22];
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long p = *ppos;
|
2007-10-27 01:31:20 +08:00
|
|
|
size_t len;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-07-17 06:01:26 +08:00
|
|
|
snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
|
2005-04-17 06:20:36 +08:00
|
|
|
len = strlen(tbuf);
|
|
|
|
if (p >= len)
|
|
|
|
return 0;
|
|
|
|
len -= p;
|
2007-10-27 01:31:20 +08:00
|
|
|
if (len > count)
|
|
|
|
len = count;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (copy_to_user(buf, (void*)(tbuf+p), len))
|
2007-10-27 01:31:20 +08:00
|
|
|
return -EFAULT;
|
|
|
|
*ppos += len;
|
2005-04-17 06:20:36 +08:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
static ssize_t write_flush(struct file *file, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos,
|
|
|
|
struct cache_detail *cd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
char tbuf[20];
|
2010-08-12 14:55:22 +08:00
|
|
|
char *bp, *ep;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (*ppos || count > sizeof(tbuf)-1)
|
|
|
|
return -EINVAL;
|
|
|
|
if (copy_from_user(tbuf, buf, count))
|
|
|
|
return -EFAULT;
|
|
|
|
tbuf[count] = 0;
|
2010-08-12 14:55:22 +08:00
|
|
|
simple_strtoul(tbuf, &ep, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (*ep && *ep != '\n')
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-08-12 14:55:22 +08:00
|
|
|
bp = tbuf;
|
|
|
|
cd->flush_time = get_expiry(&bp);
|
|
|
|
cd->nextcheck = seconds_since_boot();
|
2005-04-17 06:20:36 +08:00
|
|
|
cache_flush();
|
|
|
|
|
|
|
|
*ppos += count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2013-04-01 06:16:14 +08:00
|
|
|
struct cache_detail *cd = PDE_DATA(file_inode(filp));
|
2009-08-10 03:14:29 +08:00
|
|
|
|
|
|
|
return cache_read(filp, buf, count, ppos, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2013-04-01 06:16:14 +08:00
|
|
|
struct cache_detail *cd = PDE_DATA(file_inode(filp));
|
2009-08-10 03:14:29 +08:00
|
|
|
|
|
|
|
return cache_write(filp, buf, count, ppos, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
|
|
|
|
{
|
2013-04-01 06:16:14 +08:00
|
|
|
struct cache_detail *cd = PDE_DATA(file_inode(filp));
|
2009-08-10 03:14:29 +08:00
|
|
|
|
|
|
|
return cache_poll(filp, wait, cd);
|
|
|
|
}
|
|
|
|
|
2010-03-30 13:27:50 +08:00
|
|
|
static long cache_ioctl_procfs(struct file *filp,
|
|
|
|
unsigned int cmd, unsigned long arg)
|
2009-08-10 03:14:29 +08:00
|
|
|
{
|
2013-01-24 06:07:38 +08:00
|
|
|
struct inode *inode = file_inode(filp);
|
2013-04-01 06:16:14 +08:00
|
|
|
struct cache_detail *cd = PDE_DATA(inode);
|
2009-08-10 03:14:29 +08:00
|
|
|
|
2010-10-05 03:18:23 +08:00
|
|
|
return cache_ioctl(inode, filp, cmd, arg, cd);
|
2009-08-10 03:14:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cache_open_procfs(struct inode *inode, struct file *filp)
|
|
|
|
{
|
2013-04-01 06:16:14 +08:00
|
|
|
struct cache_detail *cd = PDE_DATA(inode);
|
2009-08-10 03:14:29 +08:00
|
|
|
|
|
|
|
return cache_open(inode, filp, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cache_release_procfs(struct inode *inode, struct file *filp)
|
|
|
|
{
|
2013-04-01 06:16:14 +08:00
|
|
|
struct cache_detail *cd = PDE_DATA(inode);
|
2009-08-10 03:14:29 +08:00
|
|
|
|
|
|
|
return cache_release(inode, filp, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations cache_file_operations_procfs = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.llseek = no_llseek,
|
|
|
|
.read = cache_read_procfs,
|
|
|
|
.write = cache_write_procfs,
|
|
|
|
.poll = cache_poll_procfs,
|
2010-03-30 13:27:50 +08:00
|
|
|
.unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
|
2009-08-10 03:14:29 +08:00
|
|
|
.open = cache_open_procfs,
|
|
|
|
.release = cache_release_procfs,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
2009-08-10 03:14:29 +08:00
|
|
|
|
|
|
|
static int content_open_procfs(struct inode *inode, struct file *filp)
|
|
|
|
{
|
2013-04-01 06:16:14 +08:00
|
|
|
struct cache_detail *cd = PDE_DATA(inode);
|
2009-08-10 03:14:29 +08:00
|
|
|
|
|
|
|
return content_open(inode, filp, cd);
|
|
|
|
}
|
|
|
|
|
2009-08-20 06:13:00 +08:00
|
|
|
static int content_release_procfs(struct inode *inode, struct file *filp)
|
|
|
|
{
|
2013-04-01 06:16:14 +08:00
|
|
|
struct cache_detail *cd = PDE_DATA(inode);
|
2009-08-20 06:13:00 +08:00
|
|
|
|
|
|
|
return content_release(inode, filp, cd);
|
|
|
|
}
|
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
static const struct file_operations content_file_operations_procfs = {
|
|
|
|
.open = content_open_procfs,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
2009-08-20 06:13:00 +08:00
|
|
|
.release = content_release_procfs,
|
2009-08-10 03:14:29 +08:00
|
|
|
};
|
|
|
|
|
2009-08-20 06:13:00 +08:00
|
|
|
static int open_flush_procfs(struct inode *inode, struct file *filp)
|
|
|
|
{
|
2013-04-01 06:16:14 +08:00
|
|
|
struct cache_detail *cd = PDE_DATA(inode);
|
2009-08-20 06:13:00 +08:00
|
|
|
|
|
|
|
return open_flush(inode, filp, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int release_flush_procfs(struct inode *inode, struct file *filp)
|
|
|
|
{
|
2013-04-01 06:16:14 +08:00
|
|
|
struct cache_detail *cd = PDE_DATA(inode);
|
2009-08-20 06:13:00 +08:00
|
|
|
|
|
|
|
return release_flush(inode, filp, cd);
|
|
|
|
}
|
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2013-04-01 06:16:14 +08:00
|
|
|
struct cache_detail *cd = PDE_DATA(file_inode(filp));
|
2009-08-10 03:14:29 +08:00
|
|
|
|
|
|
|
return read_flush(filp, buf, count, ppos, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t write_flush_procfs(struct file *filp,
|
|
|
|
const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2013-04-01 06:16:14 +08:00
|
|
|
struct cache_detail *cd = PDE_DATA(file_inode(filp));
|
2009-08-10 03:14:29 +08:00
|
|
|
|
|
|
|
return write_flush(filp, buf, count, ppos, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations cache_flush_operations_procfs = {
|
2009-08-20 06:13:00 +08:00
|
|
|
.open = open_flush_procfs,
|
2009-08-10 03:14:29 +08:00
|
|
|
.read = read_flush_procfs,
|
|
|
|
.write = write_flush_procfs,
|
2009-08-20 06:13:00 +08:00
|
|
|
.release = release_flush_procfs,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-16 00:52:59 +08:00
|
|
|
.llseek = no_llseek,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
2009-08-10 03:14:29 +08:00
|
|
|
|
2010-09-27 18:00:15 +08:00
|
|
|
static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
|
2009-08-10 03:14:29 +08:00
|
|
|
{
|
2010-09-27 18:01:58 +08:00
|
|
|
struct sunrpc_net *sn;
|
|
|
|
|
2009-08-10 03:14:29 +08:00
|
|
|
if (cd->u.procfs.proc_ent == NULL)
|
|
|
|
return;
|
|
|
|
if (cd->u.procfs.flush_ent)
|
|
|
|
remove_proc_entry("flush", cd->u.procfs.proc_ent);
|
|
|
|
if (cd->u.procfs.channel_ent)
|
|
|
|
remove_proc_entry("channel", cd->u.procfs.proc_ent);
|
|
|
|
if (cd->u.procfs.content_ent)
|
|
|
|
remove_proc_entry("content", cd->u.procfs.proc_ent);
|
|
|
|
cd->u.procfs.proc_ent = NULL;
|
2010-09-27 18:01:58 +08:00
|
|
|
sn = net_generic(net, sunrpc_net_id);
|
|
|
|
remove_proc_entry(cd->name, sn->proc_net_rpc);
|
2009-08-10 03:14:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PROC_FS
|
2010-09-27 18:00:15 +08:00
|
|
|
static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
|
2009-08-10 03:14:29 +08:00
|
|
|
{
|
|
|
|
struct proc_dir_entry *p;
|
2010-09-27 18:01:58 +08:00
|
|
|
struct sunrpc_net *sn;
|
2009-08-10 03:14:29 +08:00
|
|
|
|
2010-09-27 18:01:58 +08:00
|
|
|
sn = net_generic(net, sunrpc_net_id);
|
|
|
|
cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
|
2009-08-10 03:14:29 +08:00
|
|
|
if (cd->u.procfs.proc_ent == NULL)
|
|
|
|
goto out_nomem;
|
|
|
|
cd->u.procfs.channel_ent = NULL;
|
|
|
|
cd->u.procfs.content_ent = NULL;
|
|
|
|
|
|
|
|
p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
|
|
|
|
cd->u.procfs.proc_ent,
|
|
|
|
&cache_flush_operations_procfs, cd);
|
|
|
|
cd->u.procfs.flush_ent = p;
|
|
|
|
if (p == NULL)
|
|
|
|
goto out_nomem;
|
|
|
|
|
2013-02-04 19:02:50 +08:00
|
|
|
if (cd->cache_request || cd->cache_parse) {
|
2009-08-10 03:14:29 +08:00
|
|
|
p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
|
|
|
|
cd->u.procfs.proc_ent,
|
|
|
|
&cache_file_operations_procfs, cd);
|
|
|
|
cd->u.procfs.channel_ent = p;
|
|
|
|
if (p == NULL)
|
|
|
|
goto out_nomem;
|
|
|
|
}
|
|
|
|
if (cd->cache_show) {
|
2013-01-04 19:45:35 +08:00
|
|
|
p = proc_create_data("content", S_IFREG|S_IRUSR,
|
2009-08-10 03:14:29 +08:00
|
|
|
cd->u.procfs.proc_ent,
|
|
|
|
&content_file_operations_procfs, cd);
|
|
|
|
cd->u.procfs.content_ent = p;
|
|
|
|
if (p == NULL)
|
|
|
|
goto out_nomem;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
out_nomem:
|
2010-09-27 18:00:15 +08:00
|
|
|
remove_cache_proc_entries(cd, net);
|
2009-08-10 03:14:29 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
#else /* CONFIG_PROC_FS */
|
2010-09-27 18:00:15 +08:00
|
|
|
static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
|
2009-08-10 03:14:29 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
sunrpc: make the cache cleaner workqueue deferrable
This patch makes the cache_cleaner workqueue deferrable, to prevent
unnecessary system wake-ups, which is very important for embedded
battery-powered devices.
do_cache_clean() is called every 30 seconds at the moment, and often
makes the system wake up from its power-save sleep state. With this
change, when the workqueue uses a deferrable timer, the
do_cache_clean() invocation will be delayed and combined with the
closest "real" wake-up. This improves the power consumption situation.
Note, I tried to create a DECLARE_DELAYED_WORK_DEFERRABLE() helper
macro, similar to DECLARE_DELAYED_WORK(), but failed because of the
way the timer wheel core stores the deferrable flag (it is the
LSBit in the time->base pointer). My attempt to define a static
variable with this bit set ended up with the "initializer element is
not constant" error.
Thus, I have to use run-time initialization, so I created a new
cache_initialize() function which is called once when sunrpc is
being initialized.
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu>
2010-07-01 23:05:56 +08:00
|
|
|
void __init cache_initialize(void)
|
|
|
|
{
|
2012-08-22 04:18:23 +08:00
|
|
|
INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
|
sunrpc: make the cache cleaner workqueue deferrable
This patch makes the cache_cleaner workqueue deferrable, to prevent
unnecessary system wake-ups, which is very important for embedded
battery-powered devices.
do_cache_clean() is called every 30 seconds at the moment, and often
makes the system wake up from its power-save sleep state. With this
change, when the workqueue uses a deferrable timer, the
do_cache_clean() invocation will be delayed and combined with the
closest "real" wake-up. This improves the power consumption situation.
Note, I tried to create a DECLARE_DELAYED_WORK_DEFERRABLE() helper
macro, similar to DECLARE_DELAYED_WORK(), but failed because of the
way the timer wheel core stores the deferrable flag (it is the
LSBit in the time->base pointer). My attempt to define a static
variable with this bit set ended up with the "initializer element is
not constant" error.
Thus, I have to use run-time initialization, so I created a new
cache_initialize() function which is called once when sunrpc is
being initialized.
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu>
2010-07-01 23:05:56 +08:00
|
|
|
}
|
|
|
|
|
2010-09-27 18:00:15 +08:00
|
|
|
int cache_register_net(struct cache_detail *cd, struct net *net)
|
2009-08-10 03:14:29 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
sunrpc_init_cache_detail(cd);
|
2010-09-27 18:00:15 +08:00
|
|
|
ret = create_cache_proc_entries(cd, net);
|
2009-08-10 03:14:29 +08:00
|
|
|
if (ret)
|
|
|
|
sunrpc_destroy_cache_detail(cd);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-12-07 17:57:56 +08:00
|
|
|
EXPORT_SYMBOL_GPL(cache_register_net);
|
2010-09-27 18:00:15 +08:00
|
|
|
|
|
|
|
void cache_unregister_net(struct cache_detail *cd, struct net *net)
|
2009-08-10 03:14:29 +08:00
|
|
|
{
|
2010-09-27 18:00:15 +08:00
|
|
|
remove_cache_proc_entries(cd, net);
|
2009-08-10 03:14:29 +08:00
|
|
|
sunrpc_destroy_cache_detail(cd);
|
|
|
|
}
|
2011-12-07 17:57:56 +08:00
|
|
|
EXPORT_SYMBOL_GPL(cache_unregister_net);
|
2010-09-27 18:00:15 +08:00
|
|
|
|
2012-01-20 01:42:21 +08:00
|
|
|
struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
|
|
|
|
{
|
|
|
|
struct cache_detail *cd;
|
|
|
|
|
|
|
|
cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
|
|
|
|
if (cd == NULL)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
cd->hash_table = kzalloc(cd->hash_size * sizeof(struct cache_head *),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (cd->hash_table == NULL) {
|
|
|
|
kfree(cd);
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
}
|
|
|
|
cd->net = net;
|
|
|
|
return cd;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(cache_create_net);
|
|
|
|
|
|
|
|
void cache_destroy_net(struct cache_detail *cd, struct net *net)
|
2010-09-27 18:00:15 +08:00
|
|
|
{
|
2012-01-20 01:42:21 +08:00
|
|
|
kfree(cd->hash_table);
|
|
|
|
kfree(cd);
|
2010-09-27 18:00:15 +08:00
|
|
|
}
|
2012-01-20 01:42:21 +08:00
|
|
|
EXPORT_SYMBOL_GPL(cache_destroy_net);
|
2009-08-10 03:14:30 +08:00
|
|
|
|
|
|
|
static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2013-01-24 06:07:38 +08:00
|
|
|
struct cache_detail *cd = RPC_I(file_inode(filp))->private;
|
2009-08-10 03:14:30 +08:00
|
|
|
|
|
|
|
return cache_read(filp, buf, count, ppos, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2013-01-24 06:07:38 +08:00
|
|
|
struct cache_detail *cd = RPC_I(file_inode(filp))->private;
|
2009-08-10 03:14:30 +08:00
|
|
|
|
|
|
|
return cache_write(filp, buf, count, ppos, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
|
|
|
|
{
|
2013-01-24 06:07:38 +08:00
|
|
|
struct cache_detail *cd = RPC_I(file_inode(filp))->private;
|
2009-08-10 03:14:30 +08:00
|
|
|
|
|
|
|
return cache_poll(filp, wait, cd);
|
|
|
|
}
|
|
|
|
|
2010-05-19 21:08:17 +08:00
|
|
|
static long cache_ioctl_pipefs(struct file *filp,
|
2009-08-10 03:14:30 +08:00
|
|
|
unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
2013-01-24 06:07:38 +08:00
|
|
|
struct inode *inode = file_inode(filp);
|
2009-08-10 03:14:30 +08:00
|
|
|
struct cache_detail *cd = RPC_I(inode)->private;
|
|
|
|
|
2010-10-05 03:18:23 +08:00
|
|
|
return cache_ioctl(inode, filp, cmd, arg, cd);
|
2009-08-10 03:14:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cache_open_pipefs(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct cache_detail *cd = RPC_I(inode)->private;
|
|
|
|
|
|
|
|
return cache_open(inode, filp, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cache_release_pipefs(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct cache_detail *cd = RPC_I(inode)->private;
|
|
|
|
|
|
|
|
return cache_release(inode, filp, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct file_operations cache_file_operations_pipefs = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.llseek = no_llseek,
|
|
|
|
.read = cache_read_pipefs,
|
|
|
|
.write = cache_write_pipefs,
|
|
|
|
.poll = cache_poll_pipefs,
|
2010-05-19 21:08:17 +08:00
|
|
|
.unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
|
2009-08-10 03:14:30 +08:00
|
|
|
.open = cache_open_pipefs,
|
|
|
|
.release = cache_release_pipefs,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int content_open_pipefs(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct cache_detail *cd = RPC_I(inode)->private;
|
|
|
|
|
|
|
|
return content_open(inode, filp, cd);
|
|
|
|
}
|
|
|
|
|
2009-08-20 06:13:00 +08:00
|
|
|
static int content_release_pipefs(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct cache_detail *cd = RPC_I(inode)->private;
|
|
|
|
|
|
|
|
return content_release(inode, filp, cd);
|
|
|
|
}
|
|
|
|
|
2009-08-10 03:14:30 +08:00
|
|
|
const struct file_operations content_file_operations_pipefs = {
|
|
|
|
.open = content_open_pipefs,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
2009-08-20 06:13:00 +08:00
|
|
|
.release = content_release_pipefs,
|
2009-08-10 03:14:30 +08:00
|
|
|
};
|
|
|
|
|
2009-08-20 06:13:00 +08:00
|
|
|
static int open_flush_pipefs(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct cache_detail *cd = RPC_I(inode)->private;
|
|
|
|
|
|
|
|
return open_flush(inode, filp, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int release_flush_pipefs(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct cache_detail *cd = RPC_I(inode)->private;
|
|
|
|
|
|
|
|
return release_flush(inode, filp, cd);
|
|
|
|
}
|
|
|
|
|
2009-08-10 03:14:30 +08:00
|
|
|
static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2013-01-24 06:07:38 +08:00
|
|
|
struct cache_detail *cd = RPC_I(file_inode(filp))->private;
|
2009-08-10 03:14:30 +08:00
|
|
|
|
|
|
|
return read_flush(filp, buf, count, ppos, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t write_flush_pipefs(struct file *filp,
|
|
|
|
const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2013-01-24 06:07:38 +08:00
|
|
|
struct cache_detail *cd = RPC_I(file_inode(filp))->private;
|
2009-08-10 03:14:30 +08:00
|
|
|
|
|
|
|
return write_flush(filp, buf, count, ppos, cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct file_operations cache_flush_operations_pipefs = {
|
2009-08-20 06:13:00 +08:00
|
|
|
.open = open_flush_pipefs,
|
2009-08-10 03:14:30 +08:00
|
|
|
.read = read_flush_pipefs,
|
|
|
|
.write = write_flush_pipefs,
|
2009-08-20 06:13:00 +08:00
|
|
|
.release = release_flush_pipefs,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-16 00:52:59 +08:00
|
|
|
.llseek = no_llseek,
|
2009-08-10 03:14:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int sunrpc_cache_register_pipefs(struct dentry *parent,
|
2011-07-25 12:35:13 +08:00
|
|
|
const char *name, umode_t umode,
|
2009-08-10 03:14:30 +08:00
|
|
|
struct cache_detail *cd)
|
|
|
|
{
|
|
|
|
struct qstr q;
|
|
|
|
struct dentry *dir;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
q.name = name;
|
|
|
|
q.len = strlen(name);
|
|
|
|
q.hash = full_name_hash(q.name, q.len);
|
|
|
|
dir = rpc_create_cache_dir(parent, &q, umode, cd);
|
|
|
|
if (!IS_ERR(dir))
|
|
|
|
cd->u.pipefs.dir = dir;
|
2011-11-25 22:12:40 +08:00
|
|
|
else
|
2009-08-10 03:14:30 +08:00
|
|
|
ret = PTR_ERR(dir);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
|
|
|
|
|
|
|
|
void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
|
|
|
|
{
|
|
|
|
rpc_remove_cache_dir(cd->u.pipefs.dir);
|
|
|
|
cd->u.pipefs.dir = NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
|
|
|
|
|