Whamcloud - gitweb
64-bit warning fixes. Someone should take a closer look at ext2_obd.c
[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 #include <linux/mm.h>
14 #include <linux/pagemap.h>
15 #include <linux/fs.h>
16 #include <linux/sched.h>
17 #include <asm/uaccess.h>
18
19 #define DEBUG_SUBSYSTEM S_CLASS
20
21 #include <linux/obd_support.h>
22 #include <linux/obd_class.h>
23
24 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
25 kmem_cache_t *obdo_cachep = NULL;
26
27 int obd_init_obdo_cache(void)
28 {
29         ENTRY;
30         if (obdo_cachep == NULL) {
31                 CDEBUG(D_CACHE, "allocating obdo_cache\n");
32                 obdo_cachep = kmem_cache_create("obdo_cache",
33                                                 sizeof(struct obdo),
34                                                 0, SLAB_HWCACHE_ALIGN,
35                                                 NULL, NULL);
36                 if (obdo_cachep == NULL) {
37                         EXIT;
38                         return -ENOMEM;
39                 } else {
40                         CDEBUG(D_CACHE, "allocated cache at %p\n", obdo_cachep);
41                 }
42         } else {
43                 CDEBUG(D_CACHE, "using existing cache at %p\n", obdo_cachep);
44         }
45         EXIT;
46         return 0;
47 }
48
49 void obd_cleanup_obdo_cache(void)
50 {
51         ENTRY;
52         if (obdo_cachep != NULL) {
53                 CDEBUG(D_CACHE, "destroying obdo_cache at %p\n", obdo_cachep);
54                 if (kmem_cache_destroy(obdo_cachep))
55                         CERROR("unable to free cache\n");
56         } else
57                 CERROR("called with NULL cache pointer\n");
58
59         obdo_cachep = NULL;
60         EXIT;
61 }
62
63
64 /* map connection to client */
65 struct obd_client *gen_client(const struct obd_conn *conn)
66 {
67         struct obd_device * obddev;
68         struct list_head * lh, * next;
69         struct obd_client * cli;
70
71         if (!conn)
72                 return NULL;
73
74         obddev = conn->oc_dev;
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, sizeof(*cli));
93         if ( !cli ) {
94                 CERROR("no memory! (minor %d)\n", 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_INFO, "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(*cli));
125
126         CDEBUG(D_INFO, "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                         int j;
149
150                         for (j = --i; j >= 0; --j) {
151                                 ch_conn = &obddev->obd_multi_conn[i];
152                                 OBP(ch_conn->oc_dev, disconnect)(ch_conn);
153                         }
154                         return -EINVAL;
155                 }
156         }               
157         return 0;
158 }
159
160
161 #if 0
162 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
163 {
164         int i;
165         int count;
166         struct obd_device *rdev = obddev->obd_multi_dev[0];
167
168         count = len/sizeof(int);
169         obddev->obd_multi_count = count;
170         for (i=0 ; i<count ; i++) {
171                 rdev = &obd_dev[*((int *)data + i)];
172                 rdev = rdev + 1;
173                 CDEBUG(D_INFO, "OBD RAID1: replicator %d is of type %s\n", i,
174                        (rdev + i)->obd_type->typ_name);
175         }
176         return 0;
177 }
178 #endif
179
180
181 /*
182  *    remove all connections to this device
183  *    close all connections to lower devices
184  *    needed for forced unloads of OBD client drivers
185  */
186 int gen_multi_cleanup(struct obd_device *obddev)
187 {
188         int i;
189
190         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
191                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
192                 int rc;
193                 rc  = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
194
195                 if ( rc != 0 ) {
196                         CERROR("disconnect failure %d\n",
197                                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_INFO, "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_INFO, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n", 
258                (unsigned long long)src->o_id, (unsigned long long)src->o_blocks,
259                (unsigned long long)src->o_size, (unsigned long long)dst->o_id);
260         page = alloc_page(GFP_USER);
261         if ( !page ) {
262                 EXIT;
263                 return -ENOMEM;
264         }
265         
266         lck_page(page);
267         
268         /* XXX with brw vector I/O, we could batch up reads and writes here,
269          *     all we need to do is allocate multiple pages to handle the I/Os
270          *     and arrays to handle the request parameters.
271          */
272         while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
273                 obd_count        num_oa = 1;
274                 obd_count        num_buf = 1;
275                 obd_size         brw_count = PAGE_SIZE;
276                 obd_off          brw_offset = (page->index) << PAGE_SHIFT;
277                 obd_flag         flagr = 0;
278                 obd_flag         flagw = OBD_BRW_CREATE;
279                 
280                 page->index = index;
281                 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, num_oa, &src,
282                                                  &num_buf, &page, &brw_count,
283                                                  &brw_offset, &flagr);
284
285                 if ( err ) {
286                         EXIT;
287                         break;
288                 }
289                 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
290
291                 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
292                                                  &num_buf, &page, &brw_count,
293                                                  &brw_offset, &flagw);
294
295                 /* XXX should handle dst->o_size, dst->o_blocks here */
296                 if ( err ) {
297                         EXIT;
298                         break;
299                 }
300
301                 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
302                 
303                 index++;
304         }
305         dst->o_size = src->o_size;
306         dst->o_blocks = src->o_blocks;
307         dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
308         UnlockPage(page);
309         __free_page(page);
310
311         EXIT;
312         return err;
313 }