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-
14 #include <linux/pagemap.h>
16 #include <linux/sched.h>
17 #include <asm/uaccess.h>
19 #define DEBUG_SUBSYSTEM S_CLASS
21 #include <linux/obd_support.h>
22 #include <linux/obd_class.h>
24 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
25 kmem_cache_t *obdo_cachep = NULL;
27 int obd_init_obdo_cache(void)
30 if (obdo_cachep == NULL) {
31 CDEBUG(D_CACHE, "allocating obdo_cache\n");
32 obdo_cachep = kmem_cache_create("obdo_cache",
34 0, SLAB_HWCACHE_ALIGN,
36 if (obdo_cachep == NULL) {
40 CDEBUG(D_CACHE, "allocated cache at %p\n", obdo_cachep);
43 CDEBUG(D_CACHE, "using existing cache at %p\n", obdo_cachep);
49 void obd_cleanup_obdo_cache(void)
52 if (obdo_cachep != NULL) {
53 CDEBUG(D_CACHE, "destroying obdo_cache at %p\n", obdo_cachep);
54 if (kmem_cache_destroy(obdo_cachep))
55 CERROR("unable to free cache\n");
57 CERROR("called with NULL cache pointer\n");
64 /* map connection to client */
65 struct obd_client *gen_client(const struct obd_conn *conn)
67 struct obd_device * obddev = conn->oc_dev;
68 struct list_head * lh, * next;
69 struct obd_client * cli;
71 lh = next = &obddev->obd_gen_clients;
72 while ((lh = lh->next) != &obddev->obd_gen_clients) {
73 cli = list_entry(lh, struct obd_client, cli_chain);
75 if (cli->cli_id == conn->oc_id)
83 /* a connection defines a context in which preallocation can be managed. */
84 int gen_connect (struct obd_conn *conn)
86 struct obd_client * cli;
88 OBD_ALLOC(cli, sizeof(struct obd_client));
90 CERROR("no memory! (minor %d)\n", conn->oc_dev->obd_minor);
94 INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
95 /* XXX this should probably spinlocked? */
96 cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
97 cli->cli_prealloc_quota = 0;
98 cli->cli_obd = conn->oc_dev;
99 list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
101 CDEBUG(D_INFO, "connect: new ID %u\n", cli->cli_id);
102 conn->oc_id = cli->cli_id;
104 } /* gen_obd_connect */
107 int gen_disconnect(struct obd_conn *conn)
109 struct obd_client * cli;
112 if (!(cli = gen_client(conn))) {
113 CDEBUG(D_IOCTL, "disconnect: attempting to free "
114 "nonexistent client %u\n", conn->oc_id);
119 list_del(&(cli->cli_chain));
120 OBD_FREE(cli, sizeof(struct obd_client));
122 CDEBUG(D_INFO, "disconnect: ID %u\n", conn->oc_id);
126 } /* gen_obd_disconnect */
130 * raid1 defines a number of connections to child devices,
131 * used to make calls to these devices.
134 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
138 for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
140 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
141 rc = OBP(ch_conn->oc_dev, connect)(ch_conn);
146 for (j = --i; j >= 0; --j) {
147 ch_conn = &obddev->obd_multi_conn[i];
148 OBP(ch_conn->oc_dev, disconnect)(ch_conn);
158 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
162 struct obd_device *rdev = obddev->obd_multi_dev[0];
164 count = len/sizeof(int);
165 obddev->obd_multi_count = count;
166 for (i=0 ; i<count ; i++) {
167 rdev = &obd_dev[*((int *)data + i)];
169 CDEBUG(D_INFO, "OBD RAID1: replicator %d is of type %s\n", i,
170 (rdev + i)->obd_type->typ_name);
178 * remove all connections to this device
179 * close all connections to lower devices
180 * needed for forced unloads of OBD client drivers
182 int gen_multi_cleanup(struct obd_device *obddev)
186 for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
187 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
189 rc = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
192 CERROR("disconnect failure %d\n",
193 ch_conn->oc_dev->obd_minor);
197 } /* gen_multi_cleanup_device */
201 * forced cleanup of the device:
202 * - remove connections from the device
203 * - cleanup the device afterwards
205 int gen_cleanup(struct obd_device * obddev)
207 struct list_head * lh, * tmp;
208 struct obd_client * cli;
212 lh = tmp = &obddev->obd_gen_clients;
213 while ((tmp = tmp->next) != lh) {
214 cli = list_entry(tmp, struct obd_client, cli_chain);
215 CDEBUG(D_INFO, "Disconnecting obd_connection %d, at %p\n",
219 } /* sim_cleanup_device */
221 void ___wait_on_page(struct page *page)
223 struct task_struct *tsk = current;
224 DECLARE_WAITQUEUE(wait, tsk);
226 add_wait_queue(&page->wait, &wait);
228 run_task_queue(&tq_disk);
229 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
230 if (!PageLocked(page))
233 } while (PageLocked(page));
234 tsk->state = TASK_RUNNING;
235 remove_wait_queue(&page->wait, &wait);
238 void lck_page(struct page *page)
240 while (TryLockPage(page))
241 ___wait_on_page(page);
244 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
245 struct obd_conn *src_conn, struct obdo *src,
246 obd_size count, obd_off offset)
249 unsigned long index = 0;
253 CDEBUG(D_INFO, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n",
254 src->o_id, src->o_blocks, src->o_size, dst->o_id);
255 page = alloc_page(GFP_USER);
263 /* XXX with brw vector I/O, we could batch up reads and writes here,
264 * all we need to do is allocate multiple pages to handle the I/Os
265 * and arrays to handle the request parameters.
267 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
268 obd_count num_oa = 1;
269 obd_count num_buf = 1;
270 obd_size brw_count = PAGE_SIZE;
271 obd_off brw_offset = (page->index) << PAGE_SHIFT;
273 obd_flag flagw = OBD_BRW_CREATE;
276 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, num_oa, &src,
277 &num_buf, &page, &brw_count,
278 &brw_offset, &flagr);
284 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
286 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
287 &num_buf, &page, &brw_count,
288 &brw_offset, &flagw);
290 /* XXX should handle dst->o_size, dst->o_blocks here */
296 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
300 dst->o_size = src->o_size;
301 dst->o_blocks = src->o_blocks;
302 dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);