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_INODE, "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_INODE, "allocated cache at %p\n", obdo_cachep);
53 CDEBUG(D_INODE, "using existing cache at %p\n", obdo_cachep);
59 void obd_cleanup_obdo_cache(void)
62 if (obdo_cachep != NULL) {
63 CDEBUG(D_INODE, "destroying obdo_cache at %p\n", obdo_cachep);
64 if (kmem_cache_destroy(obdo_cachep))
65 printk(KERN_INFO "obd_cleanup_obdo_cache: unable to free all of cache\n");
67 printk(KERN_INFO "obd_cleanup_obdo_cache: called with NULL cache pointer\n");
73 /* map connection to client */
74 struct obd_client *gen_client(struct obd_conn *conn)
76 struct obd_device * obddev = conn->oc_dev;
77 struct list_head * lh, * next;
78 struct obd_client * cli;
80 lh = next = &obddev->obd_gen_clients;
81 while ((lh = lh->next) != &obddev->obd_gen_clients) {
82 cli = list_entry(lh, struct obd_client, cli_chain);
84 if (cli->cli_id == conn->oc_id)
92 /* a connection defines a context in which preallocation can be managed. */
93 int gen_connect (struct obd_conn *conn)
95 struct obd_client * cli;
97 OBD_ALLOC(cli, struct obd_client *, sizeof(struct obd_client));
99 printk("obd_connect (minor %d): no memory!\n",
100 conn->oc_dev->obd_minor);
104 INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
105 /* XXX this should probably spinlocked? */
106 cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
107 cli->cli_prealloc_quota = 0;
108 cli->cli_obd = conn->oc_dev;
109 list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
111 CDEBUG(D_IOCTL, "connect: new ID %u\n", cli->cli_id);
112 conn->oc_id = cli->cli_id;
114 } /* gen_obd_connect */
117 int gen_disconnect(struct obd_conn *conn)
119 struct obd_client * cli;
122 if (!(cli = gen_client(conn))) {
123 CDEBUG(D_IOCTL, "disconnect: attempting to free "
124 "nonexistent client %u\n", conn->oc_id);
129 list_del(&(cli->cli_chain));
130 OBD_FREE(cli, sizeof(struct obd_client));
132 CDEBUG(D_IOCTL, "disconnect: ID %u\n", conn->oc_id);
136 } /* gen_obd_disconnect */
140 * raid1 defines a number of connections to child devices,
141 * used to make calls to these devices.
144 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
148 for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
150 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
151 rc = OBP(ch_conn->oc_dev, connect)(ch_conn);
154 /* XXX disconnect others */
163 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
167 struct obd_device *rdev = obddev->obd_multi_dev[0];
169 count = len/sizeof(int);
170 obddev->obd_multi_count = count;
171 for (i=0 ; i<count ; i++) {
172 rdev = &obd_dev[*((int *)data + i)];
174 CDEBUG(D_IOCTL, "OBD RAID1: replicator %d is of type %s\n", i,
175 (rdev + i)->obd_type->typ_name);
183 * remove all connections to this device
184 * close all connections to lower devices
185 * needed for forced unloads of OBD client drivers
187 int gen_multi_cleanup(struct obd_device *obddev)
191 for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
192 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
194 rc = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
197 printk("OBD multi cleanup dev: disconnect failure %d\n", ch_conn->oc_dev->obd_minor);
201 } /* gen_multi_cleanup_device */
205 * forced cleanup of the device:
206 * - remove connections from the device
207 * - cleanup the device afterwards
209 int gen_cleanup(struct obd_device * obddev)
211 struct list_head * lh, * tmp;
212 struct obd_client * cli;
216 lh = tmp = &obddev->obd_gen_clients;
217 while ((tmp = tmp->next) != lh) {
218 cli = list_entry(tmp, struct obd_client, cli_chain);
219 CDEBUG(D_IOCTL, "Disconnecting obd_connection %d, at %p\n",
223 } /* sim_cleanup_device */
225 void ___wait_on_page(struct page *page)
227 struct task_struct *tsk = current;
228 DECLARE_WAITQUEUE(wait, tsk);
230 add_wait_queue(&page->wait, &wait);
232 run_task_queue(&tq_disk);
233 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
234 if (!PageLocked(page))
237 } while (PageLocked(page));
238 tsk->state = TASK_RUNNING;
239 remove_wait_queue(&page->wait, &wait);
242 void lck_page(struct page *page)
244 while (TryLockPage(page))
245 ___wait_on_page(page);
248 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
249 struct obd_conn *src_conn, struct obdo *src,
250 obd_size count, obd_off offset)
253 unsigned long index = 0;
257 CDEBUG(D_INODE, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n",
258 src->o_id, src->o_blocks, src->o_size, dst->o_id);
259 page = alloc_page(GFP_USER);
267 /* XXX with brw vector I/O, we could batch up reads and writes here,
268 * all we need to do is allocate multiple pages to handle the I/Os
269 * and arrays to handle the request parameters.
271 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
274 obd_size brw_size = PAGE_SIZE;
275 obd_size *brw_count = &brw_size;
276 obd_off brw_offset = (page->index) << PAGE_SHIFT;
278 obd_flag flagw = OBD_BRW_CREATE;
281 buf = (char *)page_address(page);
282 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, &num, &src,
283 &buf, &brw_count, &brw_offset,
290 CDEBUG(D_INODE, "Read page %ld ...\n", page->index);
292 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, &num, &dst,
293 &buf, &brw_count, &brw_offset,
296 /* XXX should handle dst->o_size, dst->o_blocks here */
302 CDEBUG(D_INODE, "Wrote page %ld ...\n", page->index);
306 dst->o_size = src->o_size;
307 dst->o_blocks = src->o_blocks;
308 dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);