Whamcloud - gitweb
Merged branch 'peter' with the tip. Pre-merge tag is 't_20020302_networking'.
[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 = conn->oc_dev;
68         struct list_head * lh, * next;
69         struct obd_client * cli;
70
71         lh = next = &obddev->obd_gen_clients;
72         while ((lh = lh->next) != &obddev->obd_gen_clients) {
73                 cli = list_entry(lh, struct obd_client, cli_chain);
74                 
75                 if (cli->cli_id == conn->oc_id)
76                         return cli;
77         }
78
79         return NULL;
80 } /* obd_client */
81
82
83 /* a connection defines a context in which preallocation can be managed. */ 
84 int gen_connect (struct obd_conn *conn)
85 {
86         struct obd_client * cli;
87
88         OBD_ALLOC(cli, sizeof(struct obd_client));
89         if ( !cli ) {
90                 CERROR("no memory! (minor %d)\n", conn->oc_dev->obd_minor);
91                 return -ENOMEM;
92         }
93
94         INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
95         /* XXX this should probably spinlocked? */
96         cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
97         cli->cli_prealloc_quota = 0;
98         cli->cli_obd = conn->oc_dev;
99         list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
100
101         CDEBUG(D_INFO, "connect: new ID %u\n", cli->cli_id);
102         conn->oc_id = cli->cli_id;
103         return 0;
104 } /* gen_obd_connect */
105
106
107 int gen_disconnect(struct obd_conn *conn)
108 {
109         struct obd_client * cli;
110         ENTRY;
111
112         if (!(cli = gen_client(conn))) {
113                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
114                        "nonexistent client %u\n", conn->oc_id);
115                 return -EINVAL;
116         }
117
118
119         list_del(&(cli->cli_chain));
120         OBD_FREE(cli, sizeof(struct obd_client));
121
122         CDEBUG(D_INFO, "disconnect: ID %u\n", conn->oc_id);
123
124         EXIT;
125         return 0;
126 } /* gen_obd_disconnect */
127
128
129 /* 
130  *   raid1 defines a number of connections to child devices,
131  *   used to make calls to these devices.
132  *   data holds nothing
133  */ 
134 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
135 {
136         int i;
137
138         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
139                 int rc;
140                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
141                 rc  = OBP(ch_conn->oc_dev, connect)(ch_conn);
142
143                 if ( rc != 0 ) {
144                         int j;
145
146                         for (j = --i; j >= 0; --j) {
147                                 ch_conn = &obddev->obd_multi_conn[i];
148                                 OBP(ch_conn->oc_dev, disconnect)(ch_conn);
149                         }
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_INFO, "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                         CERROR("disconnect failure %d\n",
193                                ch_conn->oc_dev->obd_minor);
194                 }
195         }               
196         return 0;
197 } /* gen_multi_cleanup_device */
198
199
200 /*
201  *    forced cleanup of the device:
202  *    - remove connections from the device
203  *    - cleanup the device afterwards
204  */
205 int gen_cleanup(struct obd_device * obddev)
206 {
207         struct list_head * lh, * tmp;
208         struct obd_client * cli;
209
210         ENTRY;
211
212         lh = tmp = &obddev->obd_gen_clients;
213         while ((tmp = tmp->next) != lh) {
214                 cli = list_entry(tmp, struct obd_client, cli_chain);
215                 CDEBUG(D_INFO, "Disconnecting obd_connection %d, at %p\n",
216                        cli->cli_id, cli);
217         }
218         return 0;
219 } /* sim_cleanup_device */
220
221 void ___wait_on_page(struct page *page)
222 {
223         struct task_struct *tsk = current;
224         DECLARE_WAITQUEUE(wait, tsk);
225
226         add_wait_queue(&page->wait, &wait);
227         do {
228                 run_task_queue(&tq_disk);
229                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
230                 if (!PageLocked(page))
231                         break;
232                 schedule();
233         } while (PageLocked(page));
234         tsk->state = TASK_RUNNING;
235         remove_wait_queue(&page->wait, &wait);
236 }
237
238 void lck_page(struct page *page)
239 {
240         while (TryLockPage(page))
241                 ___wait_on_page(page);
242 }
243
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 err = 0;
251
252         ENTRY;
253         CDEBUG(D_INFO, "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         /* XXX with brw vector I/O, we could batch up reads and writes here,
264          *     all we need to do is allocate multiple pages to handle the I/Os
265          *     and arrays to handle the request parameters.
266          */
267         while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
268                 obd_count        num_oa = 1;
269                 obd_count        num_buf = 1;
270                 obd_size         brw_count = PAGE_SIZE;
271                 obd_off          brw_offset = (page->index) << PAGE_SHIFT;
272                 obd_flag         flagr = 0;
273                 obd_flag         flagw = OBD_BRW_CREATE;
274                 
275                 page->index = index;
276                 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, num_oa, &src,
277                                                  &num_buf, &page, &brw_count,
278                                                  &brw_offset, &flagr);
279
280                 if ( err ) {
281                         EXIT;
282                         break;
283                 }
284                 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
285
286                 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
287                                                  &num_buf, &page, &brw_count,
288                                                  &brw_offset, &flagw);
289
290                 /* XXX should handle dst->o_size, dst->o_blocks here */
291                 if ( err ) {
292                         EXIT;
293                         break;
294                 }
295
296                 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
297                 
298                 index++;
299         }
300         dst->o_size = src->o_size;
301         dst->o_blocks = src->o_blocks;
302         dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
303         UnlockPage(page);
304         __free_page(page);
305
306         EXIT;
307         return err;
308 }