Whamcloud - gitweb
Pass inline data from filesystem through obdo to VFS.
[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                 printk(KERN_INFO "obdo_cache already exists\n");
42                 EXIT;
43                 /* XXX maybe this shoul be an error return? */
44                 return 0;
45         }
46
47         obdo_cachep = kmem_cache_create("obdo_cache",
48                                               sizeof(struct obdo),
49                                               0, SLAB_HWCACHE_ALIGN,
50                                               NULL, NULL);
51         if (obdo_cachep == NULL) {
52                 EXIT;
53                 return -ENOMEM;
54         }
55         EXIT;
56         return 0;
57 }
58
59 void obd_cleanup_obdo_cache(void)
60 {
61         ENTRY;
62         if (obdo_cachep != NULL)
63                 kmem_cache_destroy(obdo_cachep);
64
65         obdo_cachep = NULL;
66         EXIT;
67 }
68
69
70 /* map connection to client */
71 struct obd_client *gen_client(struct obd_conn *conn)
72 {
73         struct obd_device * obddev = conn->oc_dev;
74         struct list_head * lh, * next;
75         struct obd_client * cli;
76
77         lh = next = &obddev->obd_gen_clients;
78         while ((lh = lh->next) != &obddev->obd_gen_clients) {
79                 cli = list_entry(lh, struct obd_client, cli_chain);
80                 
81                 if (cli->cli_id == conn->oc_id)
82                         return cli;
83         }
84
85         return NULL;
86 } /* obd_client */
87
88
89
90 /* a connection defines a context in which preallocation can be managed. */ 
91 int gen_connect (struct obd_conn *conn)
92 {
93         struct obd_client * cli;
94
95         OBD_ALLOC(cli, struct obd_client *, sizeof(struct obd_client));
96         if ( !cli ) {
97                 printk("obd_connect (minor %d): no memory!\n", 
98                        conn->oc_dev->obd_minor);
99                 return -ENOMEM;
100         }
101
102         INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
103         /* this should probably spinlocked? */
104         cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
105         cli->cli_prealloc_quota = 0;
106         cli->cli_obd = conn->oc_dev;
107         list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
108
109         CDEBUG(D_IOCTL, "connect: new ID %u\n", cli->cli_id);
110         conn->oc_id = cli->cli_id;
111         return 0;
112 } /* gen_obd_connect */
113
114
115 int gen_disconnect(struct obd_conn *conn)
116 {
117         struct obd_client * cli;
118         ENTRY;
119
120         if (!(cli = gen_client(conn))) {
121                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
122                        "nonexistent client %u\n", conn->oc_id);
123                 return -EINVAL;
124         }
125
126
127         list_del(&(cli->cli_chain));
128         OBD_FREE(cli, sizeof(struct obd_client));
129
130         CDEBUG(D_IOCTL, "disconnect: ID %u\n", conn->oc_id);
131
132         EXIT;
133         return 0;
134 } /* gen_obd_disconnect */
135
136
137 /* 
138  *   raid1 defines a number of connections to child devices,
139  *   used to make calls to these devices.
140  *   data holds nothing
141  */ 
142 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
143 {
144         int i;
145
146         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
147                 int rc;
148                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
149                 rc  = OBP(ch_conn->oc_dev, connect)(ch_conn);
150
151                 if ( rc != 0 ) {
152                         /* XXX disconnect others */
153                         return -EINVAL;
154                 }
155         }               
156         return 0;
157 }
158
159
160 #if 0
161 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
162 {
163         int i;
164         int count;
165         struct obd_device *rdev = obddev->obd_multi_dev[0];
166
167         count = len/sizeof(int);
168         obddev->obd_multi_count = count;
169         for (i=0 ; i<count ; i++) {
170                 rdev = &obd_dev[*((int *)data + i)];
171                 rdev = rdev + 1;
172                 CDEBUG(D_IOCTL, "OBD RAID1: replicator %d is of type %s\n", i,
173                        (rdev + i)->obd_type->typ_name);
174         }
175         return 0;
176 }
177 #endif
178
179
180 /*
181  *    remove all connections to this device
182  *    close all connections to lower devices
183  *    needed for forced unloads of OBD client drivers
184  */
185 int gen_multi_cleanup(struct obd_device *obddev)
186 {
187         int i;
188
189         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
190                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
191                 int rc;
192                 rc  = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
193
194                 if ( rc != 0 ) {
195                         printk("OBD multi cleanup dev: disconnect failure %d\n", ch_conn->oc_dev->obd_minor);
196                 }
197         }               
198         return 0;
199 } /* gen_multi_cleanup_device */
200
201
202 /*
203  *    forced cleanup of the device:
204  *    - remove connections from the device
205  *    - cleanup the device afterwards
206  */
207 int gen_cleanup(struct obd_device * obddev)
208 {
209         struct list_head * lh, * tmp;
210         struct obd_client * cli;
211
212         ENTRY;
213
214         lh = tmp = &obddev->obd_gen_clients;
215         while ((tmp = tmp->next) != lh) {
216                 cli = list_entry(tmp, struct obd_client, cli_chain);
217                 CDEBUG(D_IOCTL, "Disconnecting obd_connection %d, at %p\n",
218                        cli->cli_id, cli);
219         }
220         return 0;
221 } /* sim_cleanup_device */
222
223 void ___wait_on_page(struct page *page)
224 {
225         struct task_struct *tsk = current;
226         DECLARE_WAITQUEUE(wait, tsk);
227
228         add_wait_queue(&page->wait, &wait);
229         do {
230                 run_task_queue(&tq_disk);
231                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
232                 if (!PageLocked(page))
233                         break;
234                 schedule();
235         } while (PageLocked(page));
236         tsk->state = TASK_RUNNING;
237         remove_wait_queue(&page->wait, &wait);
238 }
239
240 void lck_page(struct page *page)
241 {
242         while (TryLockPage(page))
243                 ___wait_on_page(page);
244 }
245
246 /* XXX this should return errors correctly, so should migrate!!! */
247 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
248                   struct obd_conn *src_conn, struct obdo *src,
249                   obd_size count, obd_off offset)
250 {
251         struct page *page;
252         unsigned long index = 0;
253         int rc;
254         ENTRY;
255
256         CDEBUG(D_INODE, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n", 
257                src->o_id, src->o_blocks, src->o_size, dst->o_id);
258         page = alloc_page(GFP_USER);
259         if ( !page ) {
260                 EXIT;
261                 return -ENOMEM;
262         }
263         
264         lck_page(page);
265         
266         while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
267                 
268                 page->index = index;
269                 rc = OBP(src_conn->oc_dev, brw)
270                         (READ, src_conn, src, (char *)page_address(page), 
271                          PAGE_SIZE, (page->index) << PAGE_SHIFT, 0);
272
273                 if ( rc != PAGE_SIZE ) 
274                         break;
275                 CDEBUG(D_INODE, "Read page %ld ...\n", page->index);
276
277                 rc = OBP(dst_conn->oc_dev, brw)
278                         (WRITE, dst_conn, dst,  (char *)page_address(page), 
279                          PAGE_SIZE, (page->index) << PAGE_SHIFT, 1);
280                 if ( rc != PAGE_SIZE)
281                         break;
282
283                 CDEBUG(D_INODE, "Wrote page %ld ...\n", page->index);
284                 
285                 index ++;
286         }
287         dst->o_size = src->o_size;
288         dst->o_blocks = src->o_blocks;
289         UnlockPage(page);
290         __free_page(page);
291
292         EXIT;
293         return 0;
294 }