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;
68 struct list_head * lh, * next;
69 struct obd_client * cli;
74 obddev = conn->oc_dev;
75 lh = next = &obddev->obd_gen_clients;
76 while ((lh = lh->next) != &obddev->obd_gen_clients) {
77 cli = list_entry(lh, struct obd_client, cli_chain);
79 if (cli->cli_id == conn->oc_id)
87 /* a connection defines a context in which preallocation can be managed. */
88 int gen_connect (struct obd_conn *conn)
90 struct obd_client * cli;
92 OBD_ALLOC(cli, sizeof(*cli));
94 CERROR("no memory! (minor %d)\n", conn->oc_dev->obd_minor);
98 INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
99 /* XXX this should probably spinlocked? */
100 cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
101 cli->cli_prealloc_quota = 0;
102 cli->cli_obd = conn->oc_dev;
103 list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
105 CDEBUG(D_INFO, "connect: new ID %u\n", cli->cli_id);
106 conn->oc_id = cli->cli_id;
108 } /* gen_obd_connect */
111 int gen_disconnect(struct obd_conn *conn)
113 struct obd_client * cli;
116 if (!(cli = gen_client(conn))) {
117 CDEBUG(D_IOCTL, "disconnect: attempting to free "
118 "nonexistent client %u\n", conn->oc_id);
123 list_del(&(cli->cli_chain));
124 OBD_FREE(cli, sizeof(*cli));
126 CDEBUG(D_INFO, "disconnect: ID %u\n", conn->oc_id);
130 } /* gen_obd_disconnect */
134 * raid1 defines a number of connections to child devices,
135 * used to make calls to these devices.
138 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
142 for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
144 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
145 rc = OBP(ch_conn->oc_dev, connect)(ch_conn);
150 for (j = --i; j >= 0; --j) {
151 ch_conn = &obddev->obd_multi_conn[i];
152 OBP(ch_conn->oc_dev, disconnect)(ch_conn);
162 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
166 struct obd_device *rdev = obddev->obd_multi_dev[0];
168 count = len/sizeof(int);
169 obddev->obd_multi_count = count;
170 for (i=0 ; i<count ; i++) {
171 rdev = &obd_dev[*((int *)data + i)];
173 CDEBUG(D_INFO, "OBD RAID1: replicator %d is of type %s\n", i,
174 (rdev + i)->obd_type->typ_name);
182 * remove all connections to this device
183 * close all connections to lower devices
184 * needed for forced unloads of OBD client drivers
186 int gen_multi_cleanup(struct obd_device *obddev)
190 for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
191 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
193 rc = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
196 CERROR("disconnect failure %d\n",
197 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_INFO, "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_INFO, "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)) {
272 obd_count num_oa = 1;
273 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 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, num_oa, &src,
281 &num_buf, &page, &brw_count,
282 &brw_offset, &flagr);
288 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
290 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
291 &num_buf, &page, &brw_count,
292 &brw_offset, &flagw);
294 /* XXX should handle dst->o_size, dst->o_blocks here */
300 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
304 dst->o_size = src->o_size;
305 dst->o_blocks = src->o_blocks;
306 dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);