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 int izo_ioctl_packlen(struct izo_ioctl_data *data)
83 int len = sizeof(struct izo_ioctl_data);
84 len += size_round(data->ioc_inllen1);
85 len += size_round(data->ioc_inllen2);
89 /* map a device to a cache */
90 struct presto_cache *presto_cache_find(kdev_t dev)
92 struct presto_cache *cache;
93 struct list_head *lh, *tmp;
95 lh = tmp = &(presto_caches[presto_cache_hash(dev)]);
96 while ( (tmp = lh->next) != lh ) {
97 cache = list_entry(tmp, struct presto_cache, cache_chain);
98 if ( cache->cache_dev == dev ) {
106 /* map an inode to a cache */
107 struct presto_cache *presto_get_cache(struct inode *inode)
109 struct presto_cache *cache;
111 /* find the correct presto_cache here, based on the device */
112 cache = presto_cache_find(inode->i_dev);
114 CERROR("WARNING: no presto cache for dev %x, ino %ld\n",
115 inode->i_dev, inode->i_ino);
123 /* another debugging routine: check fs is InterMezzo fs */
124 int presto_ispresto(struct inode *inode)
126 struct presto_cache *cache;
130 cache = presto_get_cache(inode);
133 return (inode->i_dev == cache->cache_dev);
136 /* setup a cache structure when we need one */
137 struct presto_cache *presto_cache_init(void)
139 struct presto_cache *cache;
141 PRESTO_ALLOC(cache, sizeof(struct presto_cache));
143 memset(cache, 0, sizeof(struct presto_cache));
144 INIT_LIST_HEAD(&cache->cache_chain);
145 INIT_LIST_HEAD(&cache->cache_fset_list);
146 cache->cache_lock = SPIN_LOCK_UNLOCKED;
147 cache->cache_reserved = 0;
152 /* free a cache structure and all of the memory it is pointing to */
153 inline void presto_free_cache(struct presto_cache *cache)
158 list_del(&cache->cache_chain);
159 if (cache->cache_sb && cache->cache_sb->s_root &&
160 presto_d2d(cache->cache_sb->s_root)) {
161 kmem_cache_free(presto_dentry_slab,
162 presto_d2d(cache->cache_sb->s_root));
163 cache->cache_sb->s_root->d_fsdata = NULL;
166 if (cache->cache_type)
167 PRESTO_FREE(cache->cache_type, strlen(cache->cache_type) + 1 );
169 PRESTO_FREE(cache, sizeof(struct presto_cache));
172 int presto_reserve_space(struct presto_cache *cache, loff_t req)
174 struct filter_fs *filter;
176 struct super_block *sb = cache->cache_sb;
177 filter = cache->cache_filter;
182 if (!filter->o_trops ) {
186 if (!filter->o_trops->tr_avail ) {
191 spin_lock(&cache->cache_lock);
192 avail = filter->o_trops->tr_avail(cache, sb);
193 CDEBUG(D_SUPER, "ESC::%ld +++> %ld \n", (long) cache->cache_reserved,
194 (long) (cache->cache_reserved + req));
195 CDEBUG(D_SUPER, "ESC::Avail::%ld \n", (long) avail);
196 if (req + cache->cache_reserved > avail) {
197 spin_unlock(&cache->cache_lock);
201 cache->cache_reserved += req;
202 spin_unlock(&cache->cache_lock);
208 void presto_release_space(struct presto_cache *cache, loff_t req)
210 CDEBUG(D_SUPER, "ESC::%ld ---> %ld \n", (long) cache->cache_reserved,
211 (long) (cache->cache_reserved - req));
212 spin_lock(&cache->cache_lock);
213 cache->cache_reserved -= req;
214 spin_unlock(&cache->cache_lock);