2 * linux/fs/ext2_obd/sim_obd.c
4 * These are the only exported functions; they provide the simulated object-
9 #include <linux/config.h>
10 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/stat.h>
14 #include <linux/errno.h>
15 #include <linux/locks.h>
16 #include <linux/unistd.h>
18 #include <asm/system.h>
19 #include <asm/uaccess.h>
22 #include <linux/stat.h>
23 #include <asm/uaccess.h>
24 #include <linux/vmalloc.h>
25 #include <asm/segment.h>
27 #include <linux/pagemap.h>
28 #include <linux/smp_lock.h>
30 #include <linux/obd_support.h>
31 #include <linux/obd_class.h>
34 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
35 kmem_cache_t *obdo_cachep = NULL;
37 int obd_init_obdo_cache(void)
40 if (obdo_cachep == NULL) {
41 CDEBUG(D_CACHE, "allocating obdo_cache\n");
42 obdo_cachep = kmem_cache_create("obdo_cache",
44 0, SLAB_HWCACHE_ALIGN,
46 if (obdo_cachep == NULL) {
50 CDEBUG(D_CACHE, "allocated cache at %p\n", obdo_cachep);
53 CDEBUG(D_CACHE, "using existing cache at %p\n", obdo_cachep);
59 void obd_cleanup_obdo_cache(void)
62 if (obdo_cachep != NULL) {
63 CDEBUG(D_CACHE, "destroying obdo_cache at %p\n", obdo_cachep);
64 if (kmem_cache_destroy(obdo_cachep))
65 printk(KERN_WARNING __FUNCTION__
66 ": unable to free cache\n");
68 printk(KERN_INFO __FUNCTION__
69 ": called with NULL cache pointer\n");
76 /* map connection to client */
77 struct obd_client *gen_client(struct obd_conn *conn)
79 struct obd_device * obddev = conn->oc_dev;
80 struct list_head * lh, * next;
81 struct obd_client * cli;
83 lh = next = &obddev->obd_gen_clients;
84 while ((lh = lh->next) != &obddev->obd_gen_clients) {
85 cli = list_entry(lh, struct obd_client, cli_chain);
87 if (cli->cli_id == conn->oc_id)
95 /* a connection defines a context in which preallocation can be managed. */
96 int gen_connect (struct obd_conn *conn)
98 struct obd_client * cli;
100 OBD_ALLOC(cli, struct obd_client *, sizeof(struct obd_client));
102 printk(__FUNCTION__ ": no memory! (minor %d)\n",
103 conn->oc_dev->obd_minor);
107 INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
108 /* XXX this should probably spinlocked? */
109 cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
110 cli->cli_prealloc_quota = 0;
111 cli->cli_obd = conn->oc_dev;
112 list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
114 CDEBUG(D_INFO, "connect: new ID %u\n", cli->cli_id);
115 conn->oc_id = cli->cli_id;
117 } /* gen_obd_connect */
120 int gen_disconnect(struct obd_conn *conn)
122 struct obd_client * cli;
125 if (!(cli = gen_client(conn))) {
126 CDEBUG(D_IOCTL, "disconnect: attempting to free "
127 "nonexistent client %u\n", conn->oc_id);
132 list_del(&(cli->cli_chain));
133 OBD_FREE(cli, sizeof(struct obd_client));
135 CDEBUG(D_INFO, "disconnect: ID %u\n", conn->oc_id);
139 } /* gen_obd_disconnect */
143 * raid1 defines a number of connections to child devices,
144 * used to make calls to these devices.
147 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
151 for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
153 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
154 rc = OBP(ch_conn->oc_dev, connect)(ch_conn);
159 for (j = --i; j >= 0; --j) {
160 ch_conn = &obddev->obd_multi_conn[i];
161 OBP(ch_conn->oc_dev, disconnect)(ch_conn);
171 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
175 struct obd_device *rdev = obddev->obd_multi_dev[0];
177 count = len/sizeof(int);
178 obddev->obd_multi_count = count;
179 for (i=0 ; i<count ; i++) {
180 rdev = &obd_dev[*((int *)data + i)];
182 CDEBUG(D_INFO, "OBD RAID1: replicator %d is of type %s\n", i,
183 (rdev + i)->obd_type->typ_name);
191 * remove all connections to this device
192 * close all connections to lower devices
193 * needed for forced unloads of OBD client drivers
195 int gen_multi_cleanup(struct obd_device *obddev)
199 for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
200 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
202 rc = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
205 printk(KERN_WARNING __FUNCTION__
206 ": disconnect failure %d\n",
207 ch_conn->oc_dev->obd_minor);
211 } /* gen_multi_cleanup_device */
215 * forced cleanup of the device:
216 * - remove connections from the device
217 * - cleanup the device afterwards
219 int gen_cleanup(struct obd_device * obddev)
221 struct list_head * lh, * tmp;
222 struct obd_client * cli;
226 lh = tmp = &obddev->obd_gen_clients;
227 while ((tmp = tmp->next) != lh) {
228 cli = list_entry(tmp, struct obd_client, cli_chain);
229 CDEBUG(D_INFO, "Disconnecting obd_connection %d, at %p\n",
233 } /* sim_cleanup_device */
235 void ___wait_on_page(struct page *page)
237 struct task_struct *tsk = current;
238 DECLARE_WAITQUEUE(wait, tsk);
240 add_wait_queue(&page->wait, &wait);
242 run_task_queue(&tq_disk);
243 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
244 if (!PageLocked(page))
247 } while (PageLocked(page));
248 tsk->state = TASK_RUNNING;
249 remove_wait_queue(&page->wait, &wait);
252 void lck_page(struct page *page)
254 while (TryLockPage(page))
255 ___wait_on_page(page);
258 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
259 struct obd_conn *src_conn, struct obdo *src,
260 obd_size count, obd_off offset)
263 unsigned long index = 0;
267 CDEBUG(D_INFO, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n",
268 src->o_id, src->o_blocks, src->o_size, dst->o_id);
269 page = alloc_page(GFP_USER);
277 /* XXX with brw vector I/O, we could batch up reads and writes here,
278 * all we need to do is allocate multiple pages to handle the I/Os
279 * and arrays to handle the request parameters.
281 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
282 obd_count num_oa = 1;
283 obd_count num_buf = 1;
285 obd_size brw_count = PAGE_SIZE;
286 obd_off brw_offset = (page->index) << PAGE_SHIFT;
288 obd_flag flagw = OBD_BRW_CREATE;
291 buf = (char *)page_address(page);
292 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, num_oa, &src,
293 &num_buf, &buf, &brw_count,
294 &brw_offset, &flagr);
300 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
302 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
303 &num_buf, &buf, &brw_count,
304 &brw_offset, &flagw);
306 /* XXX should handle dst->o_size, dst->o_blocks here */
312 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
316 dst->o_size = src->o_size;
317 dst->o_blocks = src->o_blocks;
318 dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);