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