1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2000 Stelias Computing, Inc.
5 * Copyright (C) 2000 Red Hat, Inc.
7 * This file is part of InterMezzo, http://www.inter-mezzo.org.
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.
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.
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.
23 #include <linux/module.h>
24 #include <asm/bitops.h>
25 #include <asm/uaccess.h>
26 #include <asm/system.h>
28 #include <linux/errno.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>
39 #include "intermezzo_fs.h"
40 #include "intermezzo_psdev.h"
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.
50 extern kmem_cache_t * presto_dentry_slab;
52 /* the intent of this hash is to have collision chains of length 1 */
54 #define CACHES_SIZE (1 << CACHES_BITS)
55 #define CACHES_MASK CACHES_SIZE - 1
56 static struct list_head presto_caches[CACHES_SIZE];
58 static inline int presto_cache_hash(struct super_block *s)
60 return (CACHES_MASK) & ((unsigned long)s >> L1_CACHE_SHIFT);
63 inline void presto_cache_add(struct presto_cache *cache)
65 list_add(&cache->cache_chain,
66 &presto_caches[presto_cache_hash(cache->cache_sb)]);
69 inline void presto_cache_init_hash(void)
72 for ( i = 0; i < CACHES_SIZE; i++ ) {
73 INIT_LIST_HEAD(&presto_caches[i]);
77 int izo_ioctl_packlen(struct izo_ioctl_data *data)
79 int len = sizeof(struct izo_ioctl_data);
80 len += size_round(data->ioc_inllen1);
81 len += size_round(data->ioc_inllen2);
85 /* map a device to a cache */
86 struct presto_cache *presto_cache_find(struct super_block *s)
88 struct presto_cache *cache;
89 struct list_head *lh, *tmp;
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)
101 /* map an inode to a cache */
102 struct presto_cache *presto_get_cache(struct inode *inode)
104 struct presto_cache *cache;
106 /* find the correct presto_cache here, based on the device */
107 cache = presto_cache_find(inode->i_sb);
109 CERROR("WARNING: no presto cache for %s, ino %ld\n",
110 inode->i_sb->s_id, inode->i_ino);
118 /* another debugging routine: check fs is InterMezzo fs */
119 int presto_ispresto(struct inode *inode)
121 struct presto_cache *cache;
125 cache = presto_get_cache(inode);
128 return inode->i_sb == cache->cache_sb;
131 /* setup a cache structure when we need one */
132 struct presto_cache *presto_cache_init(void)
134 struct presto_cache *cache;
136 PRESTO_ALLOC(cache, sizeof(struct presto_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;
147 /* free a cache structure and all of the memory it is pointing to */
148 inline void presto_free_cache(struct presto_cache *cache)
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;
161 PRESTO_FREE(cache, sizeof(struct presto_cache));
164 int presto_reserve_space(struct presto_cache *cache, loff_t req)
166 struct filter_fs *filter;
168 struct super_block *sb = cache->cache_sb;
169 filter = cache->cache_filter;
174 if (!filter->o_trops ) {
178 if (!filter->o_trops->tr_avail ) {
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);
193 cache->cache_reserved += req;
194 spin_unlock(&cache->cache_lock);
200 void presto_release_space(struct presto_cache *cache, loff_t req)
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);