Whamcloud - gitweb
- Added DEBUG_SUBSYSTEMs
[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                         printk(KERN_WARNING __FUNCTION__
56                                ": unable to free cache\n");
57         } else
58                 printk(KERN_INFO __FUNCTION__
59                        ": called with NULL cache pointer\n");
60
61         obdo_cachep = NULL;
62         EXIT;
63 }
64
65
66 /* map connection to client */
67 struct obd_client *gen_client(const struct obd_conn *conn)
68 {
69         struct obd_device * obddev = conn->oc_dev;
70         struct list_head * lh, * next;
71         struct obd_client * cli;
72
73         lh = next = &obddev->obd_gen_clients;
74         while ((lh = lh->next) != &obddev->obd_gen_clients) {
75                 cli = list_entry(lh, struct obd_client, cli_chain);
76                 
77                 if (cli->cli_id == conn->oc_id)
78                         return cli;
79         }
80
81         return NULL;
82 } /* obd_client */
83
84
85 /* a connection defines a context in which preallocation can be managed. */ 
86 int gen_connect (struct obd_conn *conn)
87 {
88         struct obd_client * cli;
89
90         OBD_ALLOC(cli, sizeof(struct obd_client));
91         if ( !cli ) {
92                 printk(__FUNCTION__ ": no memory! (minor %d)\n", 
93                        conn->oc_dev->obd_minor);
94                 return -ENOMEM;
95         }
96
97         INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
98         /* XXX this should probably spinlocked? */
99         cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
100         cli->cli_prealloc_quota = 0;
101         cli->cli_obd = conn->oc_dev;
102         list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
103
104         CDEBUG(D_INFO, "connect: new ID %u\n", cli->cli_id);
105         conn->oc_id = cli->cli_id;
106         return 0;
107 } /* gen_obd_connect */
108
109
110 int gen_disconnect(struct obd_conn *conn)
111 {
112         struct obd_client * cli;
113         ENTRY;
114
115         if (!(cli = gen_client(conn))) {
116                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
117                        "nonexistent client %u\n", conn->oc_id);
118                 return -EINVAL;
119         }
120
121
122         list_del(&(cli->cli_chain));
123         OBD_FREE(cli, sizeof(struct obd_client));
124
125         CDEBUG(D_INFO, "disconnect: ID %u\n", conn->oc_id);
126
127         EXIT;
128         return 0;
129 } /* gen_obd_disconnect */
130
131
132 /* 
133  *   raid1 defines a number of connections to child devices,
134  *   used to make calls to these devices.
135  *   data holds nothing
136  */ 
137 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
138 {
139         int i;
140
141         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
142                 int rc;
143                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
144                 rc  = OBP(ch_conn->oc_dev, connect)(ch_conn);
145
146                 if ( rc != 0 ) {
147                         int j;
148
149                         for (j = --i; j >= 0; --j) {
150                                 ch_conn = &obddev->obd_multi_conn[i];
151                                 OBP(ch_conn->oc_dev, disconnect)(ch_conn);
152                         }
153                         return -EINVAL;
154                 }
155         }               
156         return 0;
157 }
158
159
160 #if 0
161 int gen_multi_attach(struct obd_device *obddev, int len, void *data)
162 {
163         int i;
164         int count;
165         struct obd_device *rdev = obddev->obd_multi_dev[0];
166
167         count = len/sizeof(int);
168         obddev->obd_multi_count = count;
169         for (i=0 ; i<count ; i++) {
170                 rdev = &obd_dev[*((int *)data + i)];
171                 rdev = rdev + 1;
172                 CDEBUG(D_INFO, "OBD RAID1: replicator %d is of type %s\n", i,
173                        (rdev + i)->obd_type->typ_name);
174         }
175         return 0;
176 }
177 #endif
178
179
180 /*
181  *    remove all connections to this device
182  *    close all connections to lower devices
183  *    needed for forced unloads of OBD client drivers
184  */
185 int gen_multi_cleanup(struct obd_device *obddev)
186 {
187         int i;
188
189         for (i = 0 ; i < obddev->obd_multi_count ; i++ ) {
190                 struct obd_conn *ch_conn = &obddev->obd_multi_conn[i];
191                 int rc;
192                 rc  = OBP(ch_conn->oc_dev, disconnect)(ch_conn);
193
194                 if ( rc != 0 ) {
195                         printk(KERN_WARNING __FUNCTION__
196                                ": 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                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         /* XXX with brw vector I/O, we could batch up reads and writes here,
268          *     all we need to do is allocate multiple pages to handle the I/Os
269          *     and arrays to handle the request parameters.
270          */
271         while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
272                 obd_count        num_oa = 1;
273                 obd_count        num_buf = 1;
274                 obd_size         brw_count = PAGE_SIZE;
275                 obd_off          brw_offset = (page->index) << PAGE_SHIFT;
276                 obd_flag         flagr = 0;
277                 obd_flag         flagw = OBD_BRW_CREATE;
278                 
279                 page->index = index;
280                 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, num_oa, &src,
281                                                  &num_buf, &page, &brw_count,
282                                                  &brw_offset, &flagr);
283
284                 if ( err ) {
285                         EXIT;
286                         break;
287                 }
288                 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
289
290                 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
291                                                  &num_buf, &page, &brw_count,
292                                                  &brw_offset, &flagw);
293
294                 /* XXX should handle dst->o_size, dst->o_blocks here */
295                 if ( err ) {
296                         EXIT;
297                         break;
298                 }
299
300                 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
301                 
302                 index++;
303         }
304         dst->o_size = src->o_size;
305         dst->o_blocks = src->o_blocks;
306         dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
307         obd_unlock_page(page);
308         __free_page(page);
309
310         EXIT;
311         return err;
312 }