Whamcloud - gitweb
Working on add_to_page_cache oops.
[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", sizeof(struct obdo),
48                                         0, SLAB_HWCACHE_ALIGN, NULL, NULL);
49         if (obdo_cachep == NULL) {
50                 EXIT;
51                 return -ENOMEM;
52         }
53         EXIT;
54         return 0;
55 }
56
57 void obd_cleanup_obdo_cache(void)
58 {
59         ENTRY;
60         if (obdo_cachep != NULL)
61                 kmem_cache_destroy(obdo_cachep);
62
63         obdo_cachep = NULL;
64         EXIT;
65 }
66
67
68 /* map connection to client */
69 struct obd_client *gen_client(struct obd_conn *conn)
70 {
71         struct obd_device * obddev = conn->oc_dev;
72         struct list_head * lh, * next;
73         struct obd_client * cli;
74
75         lh = next = &obddev->obd_gen_clients;
76         while ((lh = lh->next) != &obddev->obd_gen_clients) {
77                 cli = list_entry(lh, struct obd_client, cli_chain);
78                 
79                 if (cli->cli_id == conn->oc_id)
80                         return cli;
81         }
82
83         return NULL;
84 } /* obd_client */
85
86
87 /* a connection defines a context in which preallocation can be managed. */ 
88 int gen_connect (struct obd_conn *conn)
89 {
90         struct obd_client * cli;
91
92         OBD_ALLOC(cli, struct obd_client *, sizeof(struct obd_client));
93         if ( !cli ) {
94                 printk("obd_connect (minor %d): no memory!\n", 
95                        conn->oc_dev->obd_minor);
96                 return -ENOMEM;
97         }
98
99         INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
100         /* XXX this should probably spinlocked? */
101         cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
102         cli->cli_prealloc_quota = 0;
103         cli->cli_obd = conn->oc_dev;
104         list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
105
106         CDEBUG(D_IOCTL, "connect: new ID %u\n", cli->cli_id);
107         conn->oc_id = cli->cli_id;
108         return 0;
109 } /* gen_obd_connect */
110
111
112 int gen_disconnect(struct obd_conn *conn)
113 {
114         struct obd_client * cli;
115         ENTRY;
116
117         if (!(cli = gen_client(conn))) {
118                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
119                        "nonexistent client %u\n", conn->oc_id);
120                 return -EINVAL;
121         }
122
123
124         list_del(&(cli->cli_chain));
125         OBD_FREE(cli, sizeof(struct obd_client));
126
127         CDEBUG(D_IOCTL, "disconnect: ID %u\n", conn->oc_id);
128
129         EXIT;
130         return 0;
131 } /* gen_obd_disconnect */
132
133
134 /* 
135  *   raid1 defines a number of connections to child devices,
136  *   used to make calls to these devices.
137  *   data holds nothing
138  */ 
139 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
140 {
141         int i;
142
143         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
144                 int rc;
145                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
146                 rc  = OBP(ch_conn->oc_dev, connect)(ch_conn);
147
148                 if ( rc != 0 ) {
149                         /* XXX disconnect others */
150                         return -EINVAL;
151                 }
152         }               
153         return 0;
154 }
155
156
157 #if 0
158 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
159 {
160         int i;
161         int count;
162         struct obd_device *rdev = obddev->obd_multi_dev[0];
163
164         count = len/sizeof(int);
165         obddev->obd_multi_count = count;
166         for (i=0 ; i<count ; i++) {
167                 rdev = &obd_dev[*((int *)data + i)];
168                 rdev = rdev + 1;
169                 CDEBUG(D_IOCTL, "OBD RAID1: replicator %d is of type %s\n", i,
170                        (rdev + i)->obd_type->typ_name);
171         }
172         return 0;
173 }
174 #endif
175
176
177 /*
178  *    remove all connections to this device
179  *    close all connections to lower devices
180  *    needed for forced unloads of OBD client drivers
181  */
182 int gen_multi_cleanup(struct obd_device *obddev)
183 {
184         int i;
185
186         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
187                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
188                 int rc;
189                 rc  = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
190
191                 if ( rc != 0 ) {
192                         printk("OBD multi cleanup dev: disconnect failure %d\n", 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_IOCTL, "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 /* XXX this should return errors correctly, so should migrate!!! */
244 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
245                   struct obd_conn *src_conn, struct obdo *src,
246                   obd_size count, obd_off offset)
247 {
248         struct page *page;
249         unsigned long index = 0;
250         int rc;
251         ENTRY;
252
253         CDEBUG(D_INODE, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n", 
254                src->o_id, src->o_blocks, src->o_size, dst->o_id);
255         page = alloc_page(GFP_USER);
256         if ( !page ) {
257                 EXIT;
258                 return -ENOMEM;
259         }
260         
261         lck_page(page);
262         
263         while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
264                 
265                 page->index = index;
266                 rc = OBP(src_conn->oc_dev, brw)
267                         (READ, src_conn, src, (char *)page_address(page), 
268                          PAGE_SIZE, (page->index) << PAGE_SHIFT, 0);
269
270                 if ( rc != PAGE_SIZE ) 
271                         break;
272                 CDEBUG(D_INODE, "Read page %ld ...\n", page->index);
273
274                 rc = OBP(dst_conn->oc_dev, brw)
275                         (WRITE, dst_conn, dst,  (char *)page_address(page), 
276                          PAGE_SIZE, (page->index) << PAGE_SHIFT, 1);
277                 if ( rc != PAGE_SIZE)
278                         break;
279
280                 CDEBUG(D_INODE, "Wrote page %ld ...\n", page->index);
281                 
282                 index ++;
283         }
284         dst->o_size = src->o_size;
285         dst->o_blocks = src->o_blocks;
286         dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
287         UnlockPage(page);
288         __free_page(page);
289
290         EXIT;
291         return 0;
292 }