Whamcloud - gitweb
ext2 page cache updates
[fs/lustre-release.git] / lustre / obdclass / genops.c
1 /*
2  *  linux/fs/ext2_obd/sim_obd.c
3  * Copyright (C) 2001  Cluster File Systems, Inc.
4  *
5  * This code is issued under the GNU General Public License.
6  * See the file COPYING in this distribution
7  *
8  * These are the only exported functions; they provide the simulated object-
9  * oriented disk.
10  *
11  */
12
13
14 #include <linux/mm.h>
15 #include <linux/fs.h>
16 #include <linux/sched.h>
17 #include <asm/uaccess.h>
18
19 #include <linux/obd_support.h>
20 #include <linux/obd_class.h>
21
22
23 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
24 kmem_cache_t *obdo_cachep = NULL;
25
26 int obd_init_obdo_cache(void)
27 {
28         ENTRY;
29         if (obdo_cachep == NULL) {
30                 CDEBUG(D_CACHE, "allocating obdo_cache\n");
31                 obdo_cachep = kmem_cache_create("obdo_cache",
32                                                 sizeof(struct obdo),
33                                                 0, SLAB_HWCACHE_ALIGN,
34                                                 NULL, NULL);
35                 if (obdo_cachep == NULL) {
36                         EXIT;
37                         return -ENOMEM;
38                 } else {
39                         CDEBUG(D_CACHE, "allocated cache at %p\n", obdo_cachep);
40                 }
41         } else {
42                 CDEBUG(D_CACHE, "using existing cache at %p\n", obdo_cachep);
43         }
44         EXIT;
45         return 0;
46 }
47
48 void obd_cleanup_obdo_cache(void)
49 {
50         ENTRY;
51         if (obdo_cachep != NULL) {
52                 CDEBUG(D_CACHE, "destroying obdo_cache at %p\n", obdo_cachep);
53                 if (kmem_cache_destroy(obdo_cachep))
54                         printk(KERN_WARNING __FUNCTION__
55                                ": unable to free cache\n");
56         } else
57                 printk(KERN_INFO __FUNCTION__
58                        ": called with NULL cache pointer\n");
59
60         obdo_cachep = NULL;
61         EXIT;
62 }
63
64
65 /* map connection to client */
66 struct obd_client *gen_client(const struct obd_conn *conn)
67 {
68         struct obd_device * obddev = conn->oc_dev;
69         struct list_head * lh, * next;
70         struct obd_client * cli;
71
72         lh = next = &obddev->obd_gen_clients;
73         while ((lh = lh->next) != &obddev->obd_gen_clients) {
74                 cli = list_entry(lh, struct obd_client, cli_chain);
75                 
76                 if (cli->cli_id == conn->oc_id)
77                         return cli;
78         }
79
80         return NULL;
81 } /* obd_client */
82
83
84 /* a connection defines a context in which preallocation can be managed. */ 
85 int gen_connect (struct obd_conn *conn)
86 {
87         struct obd_client * cli;
88
89         OBD_ALLOC(cli, struct obd_client *, sizeof(struct obd_client));
90         if ( !cli ) {
91                 printk(__FUNCTION__ ": no memory! (minor %d)\n", 
92                        conn->oc_dev->obd_minor);
93                 return -ENOMEM;
94         }
95
96         INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
97         /* XXX this should probably spinlocked? */
98         cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
99         cli->cli_prealloc_quota = 0;
100         cli->cli_obd = conn->oc_dev;
101         list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
102
103         CDEBUG(D_INFO, "connect: new ID %u\n", cli->cli_id);
104         conn->oc_id = cli->cli_id;
105         return 0;
106 } /* gen_obd_connect */
107
108
109 int gen_disconnect(struct obd_conn *conn)
110 {
111         struct obd_client * cli;
112         ENTRY;
113
114         if (!(cli = gen_client(conn))) {
115                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
116                        "nonexistent client %u\n", conn->oc_id);
117                 return -EINVAL;
118         }
119
120
121         list_del(&(cli->cli_chain));
122         OBD_FREE(cli, sizeof(struct obd_client));
123
124         CDEBUG(D_INFO, "disconnect: ID %u\n", conn->oc_id);
125
126         EXIT;
127         return 0;
128 } /* gen_obd_disconnect */
129
130
131 /* 
132  *   raid1 defines a number of connections to child devices,
133  *   used to make calls to these devices.
134  *   data holds nothing
135  */ 
136 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
137 {
138         int i;
139
140         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
141                 int rc;
142                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
143                 rc  = OBP(ch_conn->oc_dev, connect)(ch_conn);
144
145                 if ( rc != 0 ) {
146                         int j;
147
148                         for (j = --i; j >= 0; --j) {
149                                 ch_conn = &obddev->obd_multi_conn[i];
150                                 OBP(ch_conn->oc_dev, disconnect)(ch_conn);
151                         }
152                         return -EINVAL;
153                 }
154         }               
155         return 0;
156 }
157
158
159 #if 0
160 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
161 {
162         int i;
163         int count;
164         struct obd_device *rdev = obddev->obd_multi_dev[0];
165
166         count = len/sizeof(int);
167         obddev->obd_multi_count = count;
168         for (i=0 ; i<count ; i++) {
169                 rdev = &obd_dev[*((int *)data + i)];
170                 rdev = rdev + 1;
171                 CDEBUG(D_INFO, "OBD RAID1: replicator %d is of type %s\n", i,
172                        (rdev + i)->obd_type->typ_name);
173         }
174         return 0;
175 }
176 #endif
177
178
179 /*
180  *    remove all connections to this device
181  *    close all connections to lower devices
182  *    needed for forced unloads of OBD client drivers
183  */
184 int gen_multi_cleanup(struct obd_device *obddev)
185 {
186         int i;
187
188         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
189                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
190                 int rc;
191                 rc  = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
192
193                 if ( rc != 0 ) {
194                         printk(KERN_WARNING __FUNCTION__
195                                ": disconnect failure %d\n",
196                                ch_conn->oc_dev->obd_minor);
197                 }
198         }               
199         return 0;
200 } /* gen_multi_cleanup_device */
201
202
203 /*
204  *    forced cleanup of the device:
205  *    - remove connections from the device
206  *    - cleanup the device afterwards
207  */
208 int gen_cleanup(struct obd_device * obddev)
209 {
210         struct list_head * lh, * tmp;
211         struct obd_client * cli;
212
213         ENTRY;
214
215         lh = tmp = &obddev->obd_gen_clients;
216         while ((tmp = tmp->next) != lh) {
217                 cli = list_entry(tmp, struct obd_client, cli_chain);
218                 CDEBUG(D_INFO, "Disconnecting obd_connection %d, at %p\n",
219                        cli->cli_id, cli);
220         }
221         return 0;
222 } /* sim_cleanup_device */
223
224 void ___wait_on_page(struct page *page)
225 {
226         struct task_struct *tsk = current;
227         DECLARE_WAITQUEUE(wait, tsk);
228
229         add_wait_queue(&page->wait, &wait);
230         do {
231                 run_task_queue(&tq_disk);
232                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
233                 if (!PageLocked(page))
234                         break;
235                 schedule();
236         } while (PageLocked(page));
237         tsk->state = TASK_RUNNING;
238         remove_wait_queue(&page->wait, &wait);
239 }
240
241 void lck_page(struct page *page)
242 {
243         while (TryLockPage(page))
244                 ___wait_on_page(page);
245 }
246
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 err = 0;
254
255         ENTRY;
256         CDEBUG(D_INFO, "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         /* XXX with brw vector I/O, we could batch up reads and writes here,
267          *     all we need to do is allocate multiple pages to handle the I/Os
268          *     and arrays to handle the request parameters.
269          */
270         while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
271                 obd_count        num_oa = 1;
272                 obd_count        num_buf = 1;
273                 obd_size         brw_count = PAGE_SIZE;
274                 obd_off          brw_offset = (page->index) << PAGE_SHIFT;
275                 obd_flag         flagr = 0;
276                 obd_flag         flagw = OBD_BRW_CREATE;
277                 
278                 page->index = index;
279                 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, num_oa, &src,
280                                                  &num_buf, &page, &brw_count,
281                                                  &brw_offset, &flagr);
282
283                 if ( err ) {
284                         EXIT;
285                         break;
286                 }
287                 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
288
289                 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
290                                                  &num_buf, &page, &brw_count,
291                                                  &brw_offset, &flagw);
292
293                 /* XXX should handle dst->o_size, dst->o_blocks here */
294                 if ( err ) {
295                         EXIT;
296                         break;
297                 }
298
299                 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
300                 
301                 index++;
302         }
303         dst->o_size = src->o_size;
304         dst->o_blocks = src->o_blocks;
305         dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
306         obd_unlock_page(page);
307         __free_page(page);
308
309         EXIT;
310         return err;
311 }