2 * linux/fs/ext2_obd/sim_obd.c
4 * These are the only exported functions; they provide the simulated object-
12 #include <linux/sched.h>
13 #include <asm/uaccess.h>
15 #include <linux/obd_support.h>
16 #include <linux/obd_class.h>
19 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
20 kmem_cache_t *obdo_cachep = NULL;
22 int obd_init_obdo_cache(void)
25 if (obdo_cachep == NULL) {
26 CDEBUG(D_CACHE, "allocating obdo_cache\n");
27 obdo_cachep = kmem_cache_create("obdo_cache",
29 0, SLAB_HWCACHE_ALIGN,
31 if (obdo_cachep == NULL) {
35 CDEBUG(D_CACHE, "allocated cache at %p\n", obdo_cachep);
38 CDEBUG(D_CACHE, "using existing cache at %p\n", obdo_cachep);
44 void obd_cleanup_obdo_cache(void)
47 if (obdo_cachep != NULL) {
48 CDEBUG(D_CACHE, "destroying obdo_cache at %p\n", obdo_cachep);
49 if (kmem_cache_destroy(obdo_cachep))
50 printk(KERN_WARNING __FUNCTION__
51 ": unable to free cache\n");
53 printk(KERN_INFO __FUNCTION__
54 ": called with NULL cache pointer\n");
61 /* map connection to client */
62 struct obd_client *gen_client(struct obd_conn *conn)
64 struct obd_device * obddev = conn->oc_dev;
65 struct list_head * lh, * next;
66 struct obd_client * cli;
68 lh = next = &obddev->obd_gen_clients;
69 while ((lh = lh->next) != &obddev->obd_gen_clients) {
70 cli = list_entry(lh, struct obd_client, cli_chain);
72 if (cli->cli_id == conn->oc_id)
80 /* a connection defines a context in which preallocation can be managed. */
81 int gen_connect (struct obd_conn *conn)
83 struct obd_client * cli;
85 OBD_ALLOC(cli, struct obd_client *, sizeof(struct obd_client));
87 printk(__FUNCTION__ ": no memory! (minor %d)\n",
88 conn->oc_dev->obd_minor);
92 INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
93 /* XXX this should probably spinlocked? */
94 cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
95 cli->cli_prealloc_quota = 0;
96 cli->cli_obd = conn->oc_dev;
97 list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
99 CDEBUG(D_INFO, "connect: new ID %u\n", cli->cli_id);
100 conn->oc_id = cli->cli_id;
102 } /* gen_obd_connect */
105 int gen_disconnect(struct obd_conn *conn)
107 struct obd_client * cli;
110 if (!(cli = gen_client(conn))) {
111 CDEBUG(D_IOCTL, "disconnect: attempting to free "
112 "nonexistent client %u\n", conn->oc_id);
117 list_del(&(cli->cli_chain));
118 OBD_FREE(cli, sizeof(struct obd_client));
120 CDEBUG(D_INFO, "disconnect: ID %u\n", conn->oc_id);
124 } /* gen_obd_disconnect */
128 * raid1 defines a number of connections to child devices,
129 * used to make calls to these devices.
132 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
136 for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
138 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
139 rc = OBP(ch_conn->oc_dev, connect)(ch_conn);
144 for (j = --i; j >= 0; --j) {
145 ch_conn = &obddev->obd_multi_conn[i];
146 OBP(ch_conn->oc_dev, disconnect)(ch_conn);
156 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
160 struct obd_device *rdev = obddev->obd_multi_dev[0];
162 count = len/sizeof(int);
163 obddev->obd_multi_count = count;
164 for (i=0 ; i<count ; i++) {
165 rdev = &obd_dev[*((int *)data + i)];
167 CDEBUG(D_INFO, "OBD RAID1: replicator %d is of type %s\n", i,
168 (rdev + i)->obd_type->typ_name);
176 * remove all connections to this device
177 * close all connections to lower devices
178 * needed for forced unloads of OBD client drivers
180 int gen_multi_cleanup(struct obd_device *obddev)
184 for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
185 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
187 rc = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
190 printk(KERN_WARNING __FUNCTION__
191 ": disconnect failure %d\n",
192 ch_conn->oc_dev->obd_minor);
196 } /* gen_multi_cleanup_device */
200 * forced cleanup of the device:
201 * - remove connections from the device
202 * - cleanup the device afterwards
204 int gen_cleanup(struct obd_device * obddev)
206 struct list_head * lh, * tmp;
207 struct obd_client * cli;
211 lh = tmp = &obddev->obd_gen_clients;
212 while ((tmp = tmp->next) != lh) {
213 cli = list_entry(tmp, struct obd_client, cli_chain);
214 CDEBUG(D_INFO, "Disconnecting obd_connection %d, at %p\n",
218 } /* sim_cleanup_device */
220 void ___wait_on_page(struct page *page)
222 struct task_struct *tsk = current;
223 DECLARE_WAITQUEUE(wait, tsk);
225 add_wait_queue(&page->wait, &wait);
227 run_task_queue(&tq_disk);
228 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
229 if (!PageLocked(page))
232 } while (PageLocked(page));
233 tsk->state = TASK_RUNNING;
234 remove_wait_queue(&page->wait, &wait);
237 void lck_page(struct page *page)
239 while (TryLockPage(page))
240 ___wait_on_page(page);
243 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
244 struct obd_conn *src_conn, struct obdo *src,
245 obd_size count, obd_off offset)
248 unsigned long index = 0;
252 CDEBUG(D_INFO, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n",
253 src->o_id, src->o_blocks, src->o_size, dst->o_id);
254 page = alloc_page(GFP_USER);
262 /* XXX with brw vector I/O, we could batch up reads and writes here,
263 * all we need to do is allocate multiple pages to handle the I/Os
264 * and arrays to handle the request parameters.
266 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
267 obd_count num_oa = 1;
268 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 buf = (char *)page_address(page);
277 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, num_oa, &src,
278 &num_buf, &buf, &brw_count,
279 &brw_offset, &flagr);
285 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
287 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
288 &num_buf, &buf, &brw_count,
289 &brw_offset, &flagw);
291 /* XXX should handle dst->o_size, dst->o_blocks here */
297 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
301 dst->o_size = src->o_size;
302 dst->o_blocks = src->o_blocks;
303 dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
304 obd_unlock_page(page);