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-
13 #define DEBUG_SUBSYSTEM S_CLASS
15 #include <linux/obd_class.h>
17 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
18 kmem_cache_t *obdo_cachep = NULL;
20 int obd_init_obdo_cache(void)
23 if (obdo_cachep == NULL) {
24 CDEBUG(D_CACHE, "allocating obdo_cache\n");
25 obdo_cachep = kmem_cache_create("obdo_cache",
27 0, SLAB_HWCACHE_ALIGN,
29 if (obdo_cachep == NULL)
32 CDEBUG(D_CACHE, "allocated cache at %p\n", obdo_cachep);
34 CDEBUG(D_CACHE, "using existing cache at %p\n", obdo_cachep);
39 void obd_cleanup_obdo_cache(void)
42 if (obdo_cachep != NULL) {
43 CDEBUG(D_CACHE, "destroying obdo_cache at %p\n", obdo_cachep);
44 if (kmem_cache_destroy(obdo_cachep))
45 CERROR("unable to free cache\n");
47 CERROR("called with NULL cache pointer\n");
54 /* map connection to client */
55 struct obd_client *gen_client(const struct obd_conn *conn)
57 struct obd_device * obddev;
58 struct list_head * lh, * next;
59 struct obd_client * cli;
64 obddev = conn->oc_dev;
65 lh = next = &obddev->obd_gen_clients;
66 while ((lh = lh->next) != &obddev->obd_gen_clients) {
67 cli = list_entry(lh, struct obd_client, cli_chain);
69 if (cli->cli_id == conn->oc_id)
77 /* a connection defines a context in which preallocation can be managed. */
78 int gen_connect (struct obd_conn *conn)
80 struct obd_client * cli;
82 OBD_ALLOC(cli, sizeof(*cli));
84 CERROR("no memory! (minor %d)\n", conn->oc_dev->obd_minor);
88 INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
89 /* XXX this should probably spinlocked? */
90 cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
91 cli->cli_prealloc_quota = 0;
92 cli->cli_obd = conn->oc_dev;
93 list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
95 CDEBUG(D_INFO, "connect: new ID %u\n", cli->cli_id);
96 conn->oc_id = cli->cli_id;
101 int gen_disconnect(struct obd_conn *conn)
103 struct obd_client * cli;
106 if (!(cli = gen_client(conn))) {
107 CDEBUG(D_IOCTL, "disconnect: attempting to free "
108 "nonexistent client %u\n", conn->oc_id);
113 list_del(&(cli->cli_chain));
114 OBD_FREE(cli, sizeof(*cli));
116 CDEBUG(D_INFO, "disconnect: ID %u\n", conn->oc_id);
119 } /* gen_obd_disconnect */
121 /* FIXME: Data is a space- or comma-separated list of device IDs. This will
123 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
129 for (p = data, count = 0; p < (char *)data + len; count++) {
131 int tmp = simple_strtoul(p, &end, 0);
134 CERROR("invalid device ID starting at: %s\n", p);
135 GOTO(err_disconnect, rc = -EINVAL);
138 obddev->obd_multi_conn[count].oc_dev = &obd_dev[tmp];
139 rc = obd_connect(&obddev->obd_multi_conn[count]);
141 CERROR("cannot connect to device %d: rc = %d\n", tmp,
143 GOTO(err_disconnect, rc);
146 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
147 obd_dev[tmp].obd_type->typ_name);
152 obddev->obd_multi_count = count;
157 for (count--; count >= 0; count--)
158 obd_disconnect(&obddev->obd_multi_conn[count]);
163 * remove all connections to this device
164 * close all connections to lower devices
165 * needed for forced unloads of OBD client drivers
167 int gen_multi_cleanup(struct obd_device *obddev)
171 for (i = 0; i < obddev->obd_multi_count; i++) {
172 int rc = obd_disconnect(&obddev->obd_multi_conn[i]);
174 CERROR("disconnect failure %d\n",
175 obddev->obd_multi_conn[i].oc_dev->obd_minor);
182 * forced cleanup of the device:
183 * - remove connections from the device
184 * - cleanup the device afterwards
186 int gen_cleanup(struct obd_device * obddev)
188 struct list_head * lh, * tmp;
189 struct obd_client * cli;
193 lh = tmp = &obddev->obd_gen_clients;
194 while ((tmp = tmp->next) != lh) {
195 cli = list_entry(tmp, struct obd_client, cli_chain);
196 CDEBUG(D_INFO, "Disconnecting obd_connection %d, at %p\n",
200 } /* sim_cleanup_device */
202 void lck_page(struct page *page)
204 while (TryLockPage(page))
205 ___wait_on_page(page);
208 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
209 struct obd_conn *src_conn, struct obdo *src,
210 obd_size count, obd_off offset)
213 unsigned long index = 0;
217 CDEBUG(D_INFO, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n",
218 (unsigned long long)src->o_id, (unsigned long long)src->o_blocks,
219 (unsigned long long)src->o_size, (unsigned long long)dst->o_id);
220 page = alloc_page(GFP_USER);
226 /* XXX with brw vector I/O, we could batch up reads and writes here,
227 * all we need to do is allocate multiple pages to handle the I/Os
228 * and arrays to handle the request parameters.
230 while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
231 obd_count num_oa = 1;
232 obd_count num_buf = 1;
233 obd_size brw_count = PAGE_SIZE;
234 obd_off brw_offset = (page->index) << PAGE_SHIFT;
236 obd_flag flagw = OBD_BRW_CREATE;
239 err = obd_brw(OBD_BRW_READ, src_conn, num_oa, &src, &num_buf,
240 &page, &brw_count, &brw_offset, &flagr, NULL);
246 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
248 err = obd_brw(OBD_BRW_WRITE, dst_conn, num_oa, &dst, &num_buf,
249 &page, &brw_count, &brw_offset, &flagw, NULL);
251 /* XXX should handle dst->o_size, dst->o_blocks here */
257 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
261 dst->o_size = src->o_size;
262 dst->o_blocks = src->o_blocks;
263 dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);