Whamcloud - gitweb
0564a7974159cfbfce0d611a327a358ef315347b
[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_INODE, "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_INODE, "allocated cache at %p\n", obdo_cachep);
51                 }
52         } else {
53                 CDEBUG(D_INODE, "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_INODE, "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_IOCTL, "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_IOCTL, "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                         /* XXX disconnect others */
158                         return -EINVAL;
159                 }
160         }               
161         return 0;
162 }
163
164
165 #if 0
166 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
167 {
168         int i;
169         int count;
170         struct obd_device *rdev = obddev->obd_multi_dev[0];
171
172         count = len/sizeof(int);
173         obddev->obd_multi_count = count;
174         for (i=0 ; i<count ; i++) {
175                 rdev = &obd_dev[*((int *)data + i)];
176                 rdev = rdev + 1;
177                 CDEBUG(D_IOCTL, "OBD RAID1: replicator %d is of type %s\n", i,
178                        (rdev + i)->obd_type->typ_name);
179         }
180         return 0;
181 }
182 #endif
183
184
185 /*
186  *    remove all connections to this device
187  *    close all connections to lower devices
188  *    needed for forced unloads of OBD client drivers
189  */
190 int gen_multi_cleanup(struct obd_device *obddev)
191 {
192         int i;
193
194         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
195                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
196                 int rc;
197                 rc  = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
198
199                 if ( rc != 0 ) {
200                         printk(KERN_WARNING __FUNCTION__
201                                ": disconnect failure %d\n",
202                                ch_conn->oc_dev->obd_minor);
203                 }
204         }               
205         return 0;
206 } /* gen_multi_cleanup_device */
207
208
209 /*
210  *    forced cleanup of the device:
211  *    - remove connections from the device
212  *    - cleanup the device afterwards
213  */
214 int gen_cleanup(struct obd_device * obddev)
215 {
216         struct list_head * lh, * tmp;
217         struct obd_client * cli;
218
219         ENTRY;
220
221         lh = tmp = &obddev->obd_gen_clients;
222         while ((tmp = tmp->next) != lh) {
223                 cli = list_entry(tmp, struct obd_client, cli_chain);
224                 CDEBUG(D_IOCTL, "Disconnecting obd_connection %d, at %p\n",
225                        cli->cli_id, cli);
226         }
227         return 0;
228 } /* sim_cleanup_device */
229
230 void ___wait_on_page(struct page *page)
231 {
232         struct task_struct *tsk = current;
233         DECLARE_WAITQUEUE(wait, tsk);
234
235         add_wait_queue(&page->wait, &wait);
236         do {
237                 run_task_queue(&tq_disk);
238                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
239                 if (!PageLocked(page))
240                         break;
241                 schedule();
242         } while (PageLocked(page));
243         tsk->state = TASK_RUNNING;
244         remove_wait_queue(&page->wait, &wait);
245 }
246
247 void lck_page(struct page *page)
248 {
249         while (TryLockPage(page))
250                 ___wait_on_page(page);
251 }
252
253 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
254                   struct obd_conn *src_conn, struct obdo *src,
255                   obd_size count, obd_off offset)
256 {
257         struct page *page;
258         unsigned long index = 0;
259         int err = 0;
260
261         ENTRY;
262         CDEBUG(D_INODE, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n", 
263                src->o_id, src->o_blocks, src->o_size, dst->o_id);
264         page = alloc_page(GFP_USER);
265         if ( !page ) {
266                 EXIT;
267                 return -ENOMEM;
268         }
269         
270         lck_page(page);
271         
272         /* XXX with brw vector I/O, we could batch up reads and writes here,
273          *     all we need to do is allocate multiple pages to handle the I/Os
274          *     and arrays to handle the request parameters.
275          */
276         while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
277                 obd_count        num_oa = 1;
278                 obd_count        num_buf = 1;
279                 char            *buf;
280                 obd_size         brw_count = PAGE_SIZE;
281                 obd_off          brw_offset = (page->index) << PAGE_SHIFT;
282                 obd_flag         flagr = 0;
283                 obd_flag         flagw = OBD_BRW_CREATE;
284                 
285                 page->index = index;
286                 buf = (char *)page_address(page); 
287                 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, num_oa, &src,
288                                                  &num_buf, &buf, &brw_count,
289                                                  &brw_offset, &flagr);
290
291                 if ( err ) {
292                         EXIT;
293                         break;
294                 }
295                 CDEBUG(D_INODE, "Read page %ld ...\n", page->index);
296
297                 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
298                                                  &num_buf, &buf, &brw_count,
299                                                  &brw_offset, &flagw);
300
301                 /* XXX should handle dst->o_size, dst->o_blocks here */
302                 if ( err ) {
303                         EXIT;
304                         break;
305                 }
306
307                 CDEBUG(D_INODE, "Wrote page %ld ...\n", page->index);
308                 
309                 index++;
310         }
311         dst->o_size = src->o_size;
312         dst->o_blocks = src->o_blocks;
313         dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
314         UnlockPage(page);
315         __free_page(page);
316
317         EXIT;
318         return err;
319 }