This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / intermezzo / cache.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2000 Stelias Computing, Inc.
5  *  Copyright (C) 2000 Red Hat, Inc.
6  *
7  *   This file is part of InterMezzo, http://www.inter-mezzo.org.
8  *
9  *   InterMezzo is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   InterMezzo is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with InterMezzo; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <linux/module.h>
24 #include <asm/bitops.h>
25 #include <asm/uaccess.h>
26 #include <asm/system.h>
27
28 #include <linux/errno.h>
29 #include <linux/fs.h>
30 #include <linux/ext2_fs.h>
31 #include <linux/slab.h>
32 #include <linux/vmalloc.h>
33 #include <linux/sched.h>
34 #include <linux/stat.h>
35 #include <linux/string.h>
36 #include <linux/blkdev.h>
37 #include <linux/init.h>
38
39 #include "intermezzo_fs.h"
40 #include "intermezzo_psdev.h"
41
42 /*
43    This file contains the routines associated with managing a
44    cache of files for InterMezzo.  These caches have two reqs:
45    - need to be found fast so they are hashed by the device, 
46      with an attempt to have collision chains of length 1.
47    The methods for the cache are set up in methods.
48 */
49
50 extern kmem_cache_t * presto_dentry_slab;
51
52 /* the intent of this hash is to have collision chains of length 1 */
53 #define CACHES_BITS 8
54 #define CACHES_SIZE (1 << CACHES_BITS)
55 #define CACHES_MASK CACHES_SIZE - 1
56 static struct list_head presto_caches[CACHES_SIZE];
57
58 static inline int presto_cache_hash(struct super_block *s)
59 {
60         return (CACHES_MASK) & ((unsigned long)s >> L1_CACHE_SHIFT);
61 }
62
63 inline void presto_cache_add(struct presto_cache *cache)
64 {
65         list_add(&cache->cache_chain,
66                  &presto_caches[presto_cache_hash(cache->cache_sb)]);
67 }
68
69 inline void presto_cache_init_hash(void)
70 {
71         int i;
72         for ( i = 0; i < CACHES_SIZE; i++ ) {
73                 INIT_LIST_HEAD(&presto_caches[i]);
74         }
75 }
76
77 int izo_ioctl_packlen(struct izo_ioctl_data *data)
78 {
79         int len = sizeof(struct izo_ioctl_data);
80         len += size_round(data->ioc_inllen1);
81         len += size_round(data->ioc_inllen2);
82         return len;
83 }
84
85 /* map a device to a cache */
86 struct presto_cache *presto_cache_find(struct super_block *s)
87 {
88         struct presto_cache *cache;
89         struct list_head *lh, *tmp;
90
91         lh = tmp = &(presto_caches[presto_cache_hash(s)]);
92         while ( (tmp = lh->next) != lh ) {
93                 cache = list_entry(tmp, struct presto_cache, cache_chain);
94                 if (cache->cache_sb == s)
95                         return cache;
96         }
97         return NULL;
98 }
99
100
101 /* map an inode to a cache */
102 struct presto_cache *presto_get_cache(struct inode *inode)
103 {
104         struct presto_cache *cache;
105         ENTRY;
106         /* find the correct presto_cache here, based on the device */
107         cache = presto_cache_find(inode->i_sb);
108         if ( !cache ) {
109                 CERROR("WARNING: no presto cache for %s, ino %ld\n",
110                        inode->i_sb->s_id, inode->i_ino);
111                 EXIT;
112                 return NULL;
113         }
114         EXIT;
115         return cache;
116 }
117
118 /* another debugging routine: check fs is InterMezzo fs */
119 int presto_ispresto(struct inode *inode)
120 {
121         struct presto_cache *cache;
122
123         if ( !inode )
124                 return 0;
125         cache = presto_get_cache(inode);
126         if ( !cache )
127                 return 0;
128         return inode->i_sb == cache->cache_sb;
129 }
130
131 /* setup a cache structure when we need one */
132 struct presto_cache *presto_cache_init(void)
133 {
134         struct presto_cache *cache;
135
136         PRESTO_ALLOC(cache, sizeof(struct presto_cache));
137         if ( cache ) {
138                 memset(cache, 0, sizeof(struct presto_cache));
139                 INIT_LIST_HEAD(&cache->cache_chain);
140                 INIT_LIST_HEAD(&cache->cache_fset_list);
141                 cache->cache_lock = SPIN_LOCK_UNLOCKED;
142                 cache->cache_reserved = 0; 
143         }
144         return cache;
145 }
146
147 /* free a cache structure and all of the memory it is pointing to */
148 inline void presto_free_cache(struct presto_cache *cache)
149 {
150         if (!cache)
151                 return;
152
153         list_del(&cache->cache_chain);
154         if (cache->cache_sb && cache->cache_sb->s_root &&
155                         presto_d2d(cache->cache_sb->s_root)) {
156                 kmem_cache_free(presto_dentry_slab, 
157                                 presto_d2d(cache->cache_sb->s_root));
158                 cache->cache_sb->s_root->d_fsdata = NULL;
159         }
160
161         PRESTO_FREE(cache, sizeof(struct presto_cache));
162 }
163
164 int presto_reserve_space(struct presto_cache *cache, loff_t req)
165 {
166         struct filter_fs *filter; 
167         loff_t avail; 
168         struct super_block *sb = cache->cache_sb;
169         filter = cache->cache_filter;
170         if (!filter ) {
171                 EXIT;
172                 return 0; 
173         }
174         if (!filter->o_trops ) {
175                 EXIT;
176                 return 0; 
177         }
178         if (!filter->o_trops->tr_avail ) {
179                 EXIT;
180                 return 0; 
181         }
182
183         spin_lock(&cache->cache_lock);
184         avail = filter->o_trops->tr_avail(cache, sb); 
185         CDEBUG(D_SUPER, "ESC::%ld +++> %ld \n", (long) cache->cache_reserved,
186                  (long) (cache->cache_reserved + req)); 
187         CDEBUG(D_SUPER, "ESC::Avail::%ld \n", (long) avail);
188         if (req + cache->cache_reserved > avail) {
189                 spin_unlock(&cache->cache_lock);
190                 EXIT;
191                 return -ENOSPC;
192         }
193         cache->cache_reserved += req; 
194         spin_unlock(&cache->cache_lock);
195
196         EXIT;
197         return 0;
198 }
199
200 void presto_release_space(struct presto_cache *cache, loff_t req)
201 {
202         CDEBUG(D_SUPER, "ESC::%ld ---> %ld \n", (long) cache->cache_reserved,
203                  (long) (cache->cache_reserved - req)); 
204         spin_lock(&cache->cache_lock);
205         cache->cache_reserved -= req; 
206         spin_unlock(&cache->cache_lock);
207 }