Whamcloud - gitweb
* obdfs/flushd.c: conditionalized number of C_DEBUG messages.
[fs/lustre-release.git] / lustre / obdclass / genops.c
1 /*
2  *  linux/fs/ext2_obd/sim_obd.c
3  *
4  * These are the only exported functions; they provide the simulated object-
5  * oriented disk.
6  *
7  */
8
9
10 #include <linux/mm.h>
11 #include <linux/fs.h>
12 #include <linux/sched.h>
13 #include <asm/uaccess.h>
14
15 #include <linux/obd_support.h>
16 #include <linux/obd_class.h>
17
18
19 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
20 kmem_cache_t *obdo_cachep = NULL;
21
22 int obd_init_obdo_cache(void)
23 {
24         ENTRY;
25         if (obdo_cachep == NULL) {
26                 CDEBUG(D_CACHE, "allocating obdo_cache\n");
27                 obdo_cachep = kmem_cache_create("obdo_cache",
28                                                 sizeof(struct obdo),
29                                                 0, SLAB_HWCACHE_ALIGN,
30                                                 NULL, NULL);
31                 if (obdo_cachep == NULL) {
32                         EXIT;
33                         return -ENOMEM;
34                 } else {
35                         CDEBUG(D_CACHE, "allocated cache at %p\n", obdo_cachep);
36                 }
37         } else {
38                 CDEBUG(D_CACHE, "using existing cache at %p\n", obdo_cachep);
39         }
40         EXIT;
41         return 0;
42 }
43
44 void obd_cleanup_obdo_cache(void)
45 {
46         ENTRY;
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");
52         } else
53                 printk(KERN_INFO __FUNCTION__
54                        ": called with NULL cache pointer\n");
55
56         obdo_cachep = NULL;
57         EXIT;
58 }
59
60
61 /* map connection to client */
62 struct obd_client *gen_client(const struct obd_conn *conn)
63 {
64         struct obd_device * obddev = conn->oc_dev;
65         struct list_head * lh, * next;
66         struct obd_client * cli;
67
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);
71                 
72                 if (cli->cli_id == conn->oc_id)
73                         return cli;
74         }
75
76         return NULL;
77 } /* obd_client */
78
79
80 /* a connection defines a context in which preallocation can be managed. */ 
81 int gen_connect (struct obd_conn *conn)
82 {
83         struct obd_client * cli;
84
85         OBD_ALLOC(cli, struct obd_client *, sizeof(struct obd_client));
86         if ( !cli ) {
87                 printk(__FUNCTION__ ": no memory! (minor %d)\n", 
88                        conn->oc_dev->obd_minor);
89                 return -ENOMEM;
90         }
91
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);
98
99         CDEBUG(D_INFO, "connect: new ID %u\n", cli->cli_id);
100         conn->oc_id = cli->cli_id;
101         return 0;
102 } /* gen_obd_connect */
103
104
105 int gen_disconnect(struct obd_conn *conn)
106 {
107         struct obd_client * cli;
108         ENTRY;
109
110         if (!(cli = gen_client(conn))) {
111                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
112                        "nonexistent client %u\n", conn->oc_id);
113                 return -EINVAL;
114         }
115
116
117         list_del(&(cli->cli_chain));
118         OBD_FREE(cli, sizeof(struct obd_client));
119
120         CDEBUG(D_INFO, "disconnect: ID %u\n", conn->oc_id);
121
122         EXIT;
123         return 0;
124 } /* gen_obd_disconnect */
125
126
127 /* 
128  *   raid1 defines a number of connections to child devices,
129  *   used to make calls to these devices.
130  *   data holds nothing
131  */ 
132 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
133 {
134         int i;
135
136         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
137                 int rc;
138                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
139                 rc  = OBP(ch_conn->oc_dev, connect)(ch_conn);
140
141                 if ( rc != 0 ) {
142                         int j;
143
144                         for (j = --i; j >= 0; --j) {
145                                 ch_conn = &obddev->obd_multi_conn[i];
146                                 OBP(ch_conn->oc_dev, disconnect)(ch_conn);
147                         }
148                         return -EINVAL;
149                 }
150         }               
151         return 0;
152 }
153
154
155 #if 0
156 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
157 {
158         int i;
159         int count;
160         struct obd_device *rdev = obddev->obd_multi_dev[0];
161
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)];
166                 rdev = rdev + 1;
167                 CDEBUG(D_INFO, "OBD RAID1: replicator %d is of type %s\n", i,
168                        (rdev + i)->obd_type->typ_name);
169         }
170         return 0;
171 }
172 #endif
173
174
175 /*
176  *    remove all connections to this device
177  *    close all connections to lower devices
178  *    needed for forced unloads of OBD client drivers
179  */
180 int gen_multi_cleanup(struct obd_device *obddev)
181 {
182         int i;
183
184         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
185                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
186                 int rc;
187                 rc  = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
188
189                 if ( rc != 0 ) {
190                         printk(KERN_WARNING __FUNCTION__
191                                ": disconnect failure %d\n",
192                                ch_conn->oc_dev->obd_minor);
193                 }
194         }               
195         return 0;
196 } /* gen_multi_cleanup_device */
197
198
199 /*
200  *    forced cleanup of the device:
201  *    - remove connections from the device
202  *    - cleanup the device afterwards
203  */
204 int gen_cleanup(struct obd_device * obddev)
205 {
206         struct list_head * lh, * tmp;
207         struct obd_client * cli;
208
209         ENTRY;
210
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",
215                        cli->cli_id, cli);
216         }
217         return 0;
218 } /* sim_cleanup_device */
219
220 void ___wait_on_page(struct page *page)
221 {
222         struct task_struct *tsk = current;
223         DECLARE_WAITQUEUE(wait, tsk);
224
225         add_wait_queue(&page->wait, &wait);
226         do {
227                 run_task_queue(&tq_disk);
228                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
229                 if (!PageLocked(page))
230                         break;
231                 schedule();
232         } while (PageLocked(page));
233         tsk->state = TASK_RUNNING;
234         remove_wait_queue(&page->wait, &wait);
235 }
236
237 void lck_page(struct page *page)
238 {
239         while (TryLockPage(page))
240                 ___wait_on_page(page);
241 }
242
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)
246 {
247         struct page *page;
248         unsigned long index = 0;
249         int err = 0;
250
251         ENTRY;
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);
255         if ( !page ) {
256                 EXIT;
257                 return -ENOMEM;
258         }
259         
260         lck_page(page);
261         
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.
265          */
266         while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
267                 obd_count        num_oa = 1;
268                 obd_count        num_buf = 1;
269                 char            *buf;
270                 obd_size         brw_count = PAGE_SIZE;
271                 obd_off          brw_offset = (page->index) << PAGE_SHIFT;
272                 obd_flag         flagr = 0;
273                 obd_flag         flagw = OBD_BRW_CREATE;
274                 
275                 page->index = index;
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);
280
281                 if ( err ) {
282                         EXIT;
283                         break;
284                 }
285                 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
286
287                 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
288                                                  &num_buf, &buf, &brw_count,
289                                                  &brw_offset, &flagw);
290
291                 /* XXX should handle dst->o_size, dst->o_blocks here */
292                 if ( err ) {
293                         EXIT;
294                         break;
295                 }
296
297                 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
298                 
299                 index++;
300         }
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);
305         __free_page(page);
306
307         EXIT;
308         return err;
309 }