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