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 #define __NO_VERSION__
24 #include <linux/module.h>
26 #include <asm/bitops.h>
27 #include <asm/uaccess.h>
28 #include <asm/system.h>
30 #include <linux/errno.h>
32 #include <linux/ext2_fs.h>
33 #include <linux/slab.h>
34 #include <linux/vmalloc.h>
35 #include <linux/sched.h>
36 #include <linux/stat.h>
37 #include <linux/string.h>
38 #include <linux/locks.h>
39 #include <linux/blkdev.h>
40 #include <linux/init.h>
42 #include <linux/intermezzo_fs.h>
43 #include <linux/intermezzo_psdev.h>
46 This file contains the routines associated with managing a
47 cache of files for InterMezzo. These caches have two reqs:
48 - need to be found fast so they are hashed by the device,
49 with an attempt to have collision chains of length 1.
50 The methods for the cache are set up in methods.
53 extern kmem_cache_t * presto_dentry_slab;
55 /* the intent of this hash is to have collision chains of length 1 */
57 #define CACHES_SIZE (1 << CACHES_BITS)
58 #define CACHES_MASK CACHES_SIZE - 1
59 static struct list_head presto_caches[CACHES_SIZE];
61 static inline int presto_cache_hash(kdev_t dev)
63 return (CACHES_MASK) & ((0x000F & (dev)) + ((0x0F00 & (dev)) >>8));
66 inline void presto_cache_add(struct presto_cache *cache, kdev_t dev)
68 list_add(&cache->cache_chain,
69 &presto_caches[presto_cache_hash(dev)]);
70 cache->cache_dev = dev;
73 inline void presto_cache_init_hash(void)
76 for ( i = 0; i < CACHES_SIZE; i++ ) {
77 INIT_LIST_HEAD(&presto_caches[i]);
81 /* map a device to a cache */
82 struct presto_cache *presto_cache_find(kdev_t dev)
84 struct presto_cache *cache;
85 struct list_head *lh, *tmp;
87 lh = tmp = &(presto_caches[presto_cache_hash(dev)]);
88 while ( (tmp = lh->next) != lh ) {
89 cache = list_entry(tmp, struct presto_cache, cache_chain);
90 if ( cache->cache_dev == dev ) {
98 /* map an inode to a cache */
99 struct presto_cache *presto_get_cache(struct inode *inode)
101 struct presto_cache *cache;
103 /* find the correct presto_cache here, based on the device */
104 cache = presto_cache_find(inode->i_dev);
106 CERROR("WARNING: no presto cache for dev %x, ino %ld\n",
107 inode->i_dev, inode->i_ino);
115 /* another debugging routine: check fs is InterMezzo fs */
116 int presto_ispresto(struct inode *inode)
118 struct presto_cache *cache;
122 cache = presto_get_cache(inode);
125 return (inode->i_dev == cache->cache_dev);
128 /* setup a cache structure when we need one */
129 struct presto_cache *presto_cache_init(void)
131 struct presto_cache *cache;
133 PRESTO_ALLOC(cache, sizeof(struct presto_cache));
135 memset(cache, 0, sizeof(struct presto_cache));
136 INIT_LIST_HEAD(&cache->cache_chain);
137 INIT_LIST_HEAD(&cache->cache_fset_list);
138 cache->cache_lock = SPIN_LOCK_UNLOCKED;
139 cache->cache_reserved = 0;
144 /* free a cache structure and all of the memory it is pointing to */
145 inline void presto_free_cache(struct presto_cache *cache)
150 list_del(&cache->cache_chain);
151 if (cache->cache_sb && cache->cache_sb->s_root &&
152 presto_d2d(cache->cache_sb->s_root)) {
153 kmem_cache_free(presto_dentry_slab,
154 presto_d2d(cache->cache_sb->s_root));
155 cache->cache_sb->s_root->d_fsdata = NULL;
158 PRESTO_FREE(cache, sizeof(struct presto_cache));
161 int presto_reserve_space(struct presto_cache *cache, loff_t req)
163 struct filter_fs *filter;
165 struct super_block *sb = cache->cache_sb;
166 filter = cache->cache_filter;
171 if (!filter->o_trops ) {
175 if (!filter->o_trops->tr_avail ) {
180 spin_lock(&cache->cache_lock);
181 avail = filter->o_trops->tr_avail(cache, sb);
182 CDEBUG(D_SUPER, "ESC::%ld +++> %ld \n", (long) cache->cache_reserved,
183 (long) (cache->cache_reserved + req));
184 CDEBUG(D_SUPER, "ESC::Avail::%ld \n", (long) avail);
185 if (req + cache->cache_reserved > avail) {
186 spin_unlock(&cache->cache_lock);
190 cache->cache_reserved += req;
191 spin_unlock(&cache->cache_lock);
197 void presto_release_space(struct presto_cache *cache, loff_t req)
199 CDEBUG(D_SUPER, "ESC::%ld ---> %ld \n", (long) cache->cache_reserved,
200 (long) (cache->cache_reserved - req));
201 spin_lock(&cache->cache_lock);
202 cache->cache_reserved -= req;
203 spin_unlock(&cache->cache_lock);