2 * linux/fs/ext2_obd/sim_obd.c
3 * Copyright (C) 2001 Cluster File Systems, Inc.
5 * This code is issued under the GNU General Public License.
6 * See the file COPYING in this distribution
8 * These are the only exported functions; they provide the simulated object-
16 #include <linux/sched.h>
17 #include <asm/uaccess.h>
19 #include <linux/obd_support.h>
20 #include <linux/obd_class.h>
23 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
24 kmem_cache_t *obdo_cachep = NULL;
26 int obd_init_obdo_cache(void)
29 if (obdo_cachep == NULL) {
30 CDEBUG(D_CACHE, "allocating obdo_cache\n");
31 obdo_cachep = kmem_cache_create("obdo_cache",
33 0, SLAB_HWCACHE_ALIGN,
35 if (obdo_cachep == NULL) {
39 CDEBUG(D_CACHE, "allocated cache at %p\n", obdo_cachep);
42 CDEBUG(D_CACHE, "using existing cache at %p\n", obdo_cachep);
48 void obd_cleanup_obdo_cache(void)
51 if (obdo_cachep != NULL) {
52 CDEBUG(D_CACHE, "destroying obdo_cache at %p\n", obdo_cachep);
53 if (kmem_cache_destroy(obdo_cachep))
54 printk(KERN_WARNING __FUNCTION__
55 ": unable to free cache\n");
57 printk(KERN_INFO __FUNCTION__
58 ": called with NULL cache pointer\n");
65 /* map connection to client */
66 struct obd_client *gen_client(const struct obd_conn *conn)
68 struct obd_device * obddev = conn->oc_dev;
69 struct list_head * lh, * next;
70 struct obd_client * cli;
72 lh = next = &obddev->obd_gen_clients;
73 while ((lh = lh->next) != &obddev->obd_gen_clients) {
74 cli = list_entry(lh, struct obd_client, cli_chain);
76 if (cli->cli_id == conn->oc_id)
84 /* a connection defines a context in which preallocation can be managed. */
85 int gen_connect (struct obd_conn *conn)
87 struct obd_client * cli;
89 OBD_ALLOC(cli, struct obd_client *, sizeof(struct obd_client));
91 printk(__FUNCTION__ ": no memory! (minor %d)\n",
92 conn->oc_dev->obd_minor);
96 INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
97 /* XXX this should probably spinlocked? */
98 cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
99 cli->cli_prealloc_quota = 0;
100 cli->cli_obd = conn->oc_dev;
101 list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
103 CDEBUG(D_INFO, "connect: new ID %u\n", cli->cli_id);
104 conn->oc_id = cli->cli_id;
106 } /* gen_obd_connect */
109 int gen_disconnect(struct obd_conn *conn)
111 struct obd_client * cli;
114 if (!(cli = gen_client(conn))) {
115 CDEBUG(D_IOCTL, "disconnect: attempting to free "
116 "nonexistent client %u\n", conn->oc_id);
121 list_del(&(cli->cli_chain));
122 OBD_FREE(cli, sizeof(struct obd_client));
124 CDEBUG(D_INFO, "disconnect: ID %u\n", conn->oc_id);
128 } /* gen_obd_disconnect */
132 * raid1 defines a number of connections to child devices,
133 * used to make calls to these devices.
136 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
140 for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
142 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
143 rc = OBP(ch_conn->oc_dev, connect)(ch_conn);
148 for (j = --i; j >= 0; --j) {
149 ch_conn = &obddev->obd_multi_conn[i];
150 OBP(ch_conn->oc_dev, disconnect)(ch_conn);
160 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
164 struct obd_device *rdev = obddev->obd_multi_dev[0];
166 count = len/sizeof(int);
167 obddev->obd_multi_count = count;
168 for (i=0 ; i<count ; i++) {
169 rdev = &obd_dev[*((int *)data + i)];
171 CDEBUG(D_INFO, "OBD RAID1: replicator %d is of type %s\n", i,
172 (rdev + i)->obd_type->typ_name);
180 * remove all connections to this device
181 * close all connections to lower devices
182 * needed for forced unloads of OBD client drivers
184 int gen_multi_cleanup(struct obd_device *obddev)
188 for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
189 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
191 rc = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
194 printk(KERN_WARNING __FUNCTION__
195 ": disconnect failure %d\n",
196 ch_conn->oc_dev->obd_minor);
200 } /* gen_multi_cleanup_device */
204 * forced cleanup of the device:
205 * - remove connections from the device
206 * - cleanup the device afterwards
208 int gen_cleanup(struct obd_device * obddev)
210 struct list_head * lh, * tmp;
211 struct obd_client * cli;
215 lh = tmp = &obddev->obd_gen_clients;
216 while ((tmp = tmp->next) != lh) {
217 cli = list_entry(tmp, struct obd_client, cli_chain);
218 CDEBUG(D_INFO, "Disconnecting obd_connection %d, at %p\n",
222 } /* sim_cleanup_device */
224 void ___wait_on_page(struct page *page)
226 struct task_struct *tsk = current;
227 DECLARE_WAITQUEUE(wait, tsk);
229 add_wait_queue(&page->wait, &wait);
231 run_task_queue(&tq_disk);
232 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
233 if (!PageLocked(page))
236 } while (PageLocked(page));
237 tsk->state = TASK_RUNNING;
238 remove_wait_queue(&page->wait, &wait);
241 void lck_page(struct page *page)
243 while (TryLockPage(page))
244 ___wait_on_page(page);
247 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
248 struct obd_conn *src_conn, struct obdo *src,
249 obd_size count, obd_off offset)
252 unsigned long index = 0;
256 CDEBUG(D_INFO, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n",
257 src->o_id, src->o_blocks, src->o_size, dst->o_id);
258 page = alloc_page(GFP_USER);
266 /* XXX with brw vector I/O, we could batch up reads and writes here,
267 * all we need to do is allocate multiple pages to handle the I/Os
268 * and arrays to handle the request parameters.
270 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
271 obd_count num_oa = 1;
272 obd_count num_buf = 1;
274 obd_size brw_count = PAGE_SIZE;
275 obd_off brw_offset = (page->index) << PAGE_SHIFT;
277 obd_flag flagw = OBD_BRW_CREATE;
280 buf = (char *)page_address(page);
281 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, num_oa, &src,
282 &num_buf, &buf, &brw_count,
283 &brw_offset, &flagr);
289 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
291 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
292 &num_buf, &buf, &brw_count,
293 &brw_offset, &flagw);
295 /* XXX should handle dst->o_size, dst->o_blocks here */
301 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
305 dst->o_size = src->o_size;
306 dst->o_blocks = src->o_blocks;
307 dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
308 obd_unlock_page(page);