Whamcloud - gitweb
doc/OBD-HOWTO.sgml: updated references to attach types from *_obd to obd*
[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 #include <linux/config.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.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>
17
18 #include <asm/system.h>
19 #include <asm/uaccess.h>
20
21 #include <linux/fs.h>
22 #include <linux/stat.h>
23 #include <asm/uaccess.h>
24 #include <linux/vmalloc.h>
25 #include <asm/segment.h>
26 #include <linux/mm.h>
27 #include <linux/pagemap.h>
28 #include <linux/smp_lock.h>
29
30 #include <linux/obd_support.h>
31 #include <linux/obd_class.h>
32
33
34 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
35 kmem_cache_t *obdo_cachep = NULL;
36
37 int obd_init_obdo_cache(void)
38 {
39         ENTRY;
40         if (obdo_cachep == NULL) {
41                 CDEBUG(D_CACHE, "allocating obdo_cache\n");
42                 obdo_cachep = kmem_cache_create("obdo_cache",
43                                                 sizeof(struct obdo),
44                                                 0, SLAB_HWCACHE_ALIGN,
45                                                 NULL, NULL);
46                 if (obdo_cachep == NULL) {
47                         EXIT;
48                         return -ENOMEM;
49                 } else {
50                         CDEBUG(D_CACHE, "allocated cache at %p\n", obdo_cachep);
51                 }
52         } else {
53                 CDEBUG(D_CACHE, "using existing cache at %p\n", obdo_cachep);
54         }
55         EXIT;
56         return 0;
57 }
58
59 void obd_cleanup_obdo_cache(void)
60 {
61         ENTRY;
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");
67         } else
68                 printk(KERN_INFO __FUNCTION__
69                        ": called with NULL cache pointer\n");
70
71         obdo_cachep = NULL;
72         EXIT;
73 }
74
75
76 /* map connection to client */
77 struct obd_client *gen_client(struct obd_conn *conn)
78 {
79         struct obd_device * obddev = conn->oc_dev;
80         struct list_head * lh, * next;
81         struct obd_client * cli;
82
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);
86                 
87                 if (cli->cli_id == conn->oc_id)
88                         return cli;
89         }
90
91         return NULL;
92 } /* obd_client */
93
94
95 /* a connection defines a context in which preallocation can be managed. */ 
96 int gen_connect (struct obd_conn *conn)
97 {
98         struct obd_client * cli;
99
100         OBD_ALLOC(cli, struct obd_client *, sizeof(struct obd_client));
101         if ( !cli ) {
102                 printk(__FUNCTION__ ": no memory! (minor %d)\n", 
103                        conn->oc_dev->obd_minor);
104                 return -ENOMEM;
105         }
106
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);
113
114         CDEBUG(D_INFO, "connect: new ID %u\n", cli->cli_id);
115         conn->oc_id = cli->cli_id;
116         return 0;
117 } /* gen_obd_connect */
118
119
120 int gen_disconnect(struct obd_conn *conn)
121 {
122         struct obd_client * cli;
123         ENTRY;
124
125         if (!(cli = gen_client(conn))) {
126                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
127                        "nonexistent client %u\n", conn->oc_id);
128                 return -EINVAL;
129         }
130
131
132         list_del(&(cli->cli_chain));
133         OBD_FREE(cli, sizeof(struct obd_client));
134
135         CDEBUG(D_INFO, "disconnect: ID %u\n", conn->oc_id);
136
137         EXIT;
138         return 0;
139 } /* gen_obd_disconnect */
140
141
142 /* 
143  *   raid1 defines a number of connections to child devices,
144  *   used to make calls to these devices.
145  *   data holds nothing
146  */ 
147 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
148 {
149         int i;
150
151         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
152                 int rc;
153                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
154                 rc  = OBP(ch_conn->oc_dev, connect)(ch_conn);
155
156                 if ( rc != 0 ) {
157                         int j;
158
159                         for (j = --i; j >= 0; --j) {
160                                 ch_conn = &obddev->obd_multi_conn[i];
161                                 OBP(ch_conn->oc_dev, disconnect)(ch_conn);
162                         }
163                         return -EINVAL;
164                 }
165         }               
166         return 0;
167 }
168
169
170 #if 0
171 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
172 {
173         int i;
174         int count;
175         struct obd_device *rdev = obddev->obd_multi_dev[0];
176
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)];
181                 rdev = rdev + 1;
182                 CDEBUG(D_INFO, "OBD RAID1: replicator %d is of type %s\n", i,
183                        (rdev + i)->obd_type->typ_name);
184         }
185         return 0;
186 }
187 #endif
188
189
190 /*
191  *    remove all connections to this device
192  *    close all connections to lower devices
193  *    needed for forced unloads of OBD client drivers
194  */
195 int gen_multi_cleanup(struct obd_device *obddev)
196 {
197         int i;
198
199         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
200                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
201                 int rc;
202                 rc  = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
203
204                 if ( rc != 0 ) {
205                         printk(KERN_WARNING __FUNCTION__
206                                ": disconnect failure %d\n",
207                                ch_conn->oc_dev->obd_minor);
208                 }
209         }               
210         return 0;
211 } /* gen_multi_cleanup_device */
212
213
214 /*
215  *    forced cleanup of the device:
216  *    - remove connections from the device
217  *    - cleanup the device afterwards
218  */
219 int gen_cleanup(struct obd_device * obddev)
220 {
221         struct list_head * lh, * tmp;
222         struct obd_client * cli;
223
224         ENTRY;
225
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",
230                        cli->cli_id, cli);
231         }
232         return 0;
233 } /* sim_cleanup_device */
234
235 void ___wait_on_page(struct page *page)
236 {
237         struct task_struct *tsk = current;
238         DECLARE_WAITQUEUE(wait, tsk);
239
240         add_wait_queue(&page->wait, &wait);
241         do {
242                 run_task_queue(&tq_disk);
243                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
244                 if (!PageLocked(page))
245                         break;
246                 schedule();
247         } while (PageLocked(page));
248         tsk->state = TASK_RUNNING;
249         remove_wait_queue(&page->wait, &wait);
250 }
251
252 void lck_page(struct page *page)
253 {
254         while (TryLockPage(page))
255                 ___wait_on_page(page);
256 }
257
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)
261 {
262         struct page *page;
263         unsigned long index = 0;
264         int err = 0;
265
266         ENTRY;
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);
270         if ( !page ) {
271                 EXIT;
272                 return -ENOMEM;
273         }
274         
275         lck_page(page);
276         
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.
280          */
281         while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
282                 obd_count        num_oa = 1;
283                 obd_count        num_buf = 1;
284                 char            *buf;
285                 obd_size         brw_count = PAGE_SIZE;
286                 obd_off          brw_offset = (page->index) << PAGE_SHIFT;
287                 obd_flag         flagr = 0;
288                 obd_flag         flagw = OBD_BRW_CREATE;
289                 
290                 page->index = index;
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);
295
296                 if ( err ) {
297                         EXIT;
298                         break;
299                 }
300                 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
301
302                 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
303                                                  &num_buf, &buf, &brw_count,
304                                                  &brw_offset, &flagw);
305
306                 /* XXX should handle dst->o_size, dst->o_blocks here */
307                 if ( err ) {
308                         EXIT;
309                         break;
310                 }
311
312                 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
313                 
314                 index++;
315         }
316         dst->o_size = src->o_size;
317         dst->o_blocks = src->o_blocks;
318         dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
319         UnlockPage(page);
320         __free_page(page);
321
322         EXIT;
323         return err;
324 }