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