Whamcloud - gitweb
* Split struct niobuf into niobuf_local and niobuf_remote
[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 #define DEBUG_SUBSYSTEM S_CLASS
14
15 #include <linux/obd_class.h>
16
17 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
18 kmem_cache_t *obdo_cachep = NULL;
19
20 int obd_init_obdo_cache(void)
21 {
22         ENTRY;
23         if (obdo_cachep == NULL) {
24                 CDEBUG(D_CACHE, "allocating obdo_cache\n");
25                 obdo_cachep = kmem_cache_create("obdo_cache",
26                                                 sizeof(struct obdo),
27                                                 0, SLAB_HWCACHE_ALIGN,
28                                                 NULL, NULL);
29                 if (obdo_cachep == NULL)
30                         RETURN(-ENOMEM);
31                 else
32                         CDEBUG(D_CACHE, "allocated cache at %p\n", obdo_cachep);
33         } else {
34                 CDEBUG(D_CACHE, "using existing cache at %p\n", obdo_cachep);
35         }
36         RETURN(0);
37 }
38
39 void obd_cleanup_obdo_cache(void)
40 {
41         ENTRY;
42         if (obdo_cachep != NULL) {
43                 CDEBUG(D_CACHE, "destroying obdo_cache at %p\n", obdo_cachep);
44                 if (kmem_cache_destroy(obdo_cachep))
45                         CERROR("unable to free cache\n");
46         } else
47                 CERROR("called with NULL cache pointer\n");
48
49         obdo_cachep = NULL;
50         EXIT;
51 }
52
53
54 /* map connection to client */
55 struct obd_client *gen_client(const struct obd_conn *conn)
56 {
57         struct obd_device * obddev;
58         struct list_head * lh, * next;
59         struct obd_client * cli;
60
61         if (!conn)
62                 return NULL;
63
64         obddev = conn->oc_dev;
65         lh = next = &obddev->obd_gen_clients;
66         while ((lh = lh->next) != &obddev->obd_gen_clients) {
67                 cli = list_entry(lh, struct obd_client, cli_chain);
68
69                 if (cli->cli_id == conn->oc_id)
70                         return cli;
71         }
72
73         return NULL;
74 } /* gen_client */
75
76
77 /* a connection defines a context in which preallocation can be managed. */
78 int gen_connect (struct obd_conn *conn)
79 {
80         struct obd_client * cli;
81
82         OBD_ALLOC(cli, sizeof(*cli));
83         if ( !cli ) {
84                 CERROR("no memory! (minor %d)\n", conn->oc_dev->obd_minor);
85                 return -ENOMEM;
86         }
87
88         INIT_LIST_HEAD(&cli->cli_prealloc_inodes);
89         /* XXX this should probably spinlocked? */
90         cli->cli_id = ++conn->oc_dev->obd_gen_last_id;
91         cli->cli_prealloc_quota = 0;
92         cli->cli_obd = conn->oc_dev;
93         list_add(&(cli->cli_chain), conn->oc_dev->obd_gen_clients.prev);
94
95         CDEBUG(D_INFO, "connect: new ID %u\n", cli->cli_id);
96         conn->oc_id = cli->cli_id;
97         return 0;
98 } /* gen_connect */
99
100
101 int gen_disconnect(struct obd_conn *conn)
102 {
103         struct obd_client * cli;
104         ENTRY;
105
106         if (!(cli = gen_client(conn))) {
107                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
108                        "nonexistent client %u\n", conn->oc_id);
109                 RETURN(-EINVAL);
110         }
111
112
113         list_del(&(cli->cli_chain));
114         OBD_FREE(cli, sizeof(*cli));
115
116         CDEBUG(D_INFO, "disconnect: ID %u\n", conn->oc_id);
117
118         RETURN(0);
119 } /* gen_obd_disconnect */
120
121 /* FIXME: Data is a space- or comma-separated list of device IDs.  This will
122  * have to change. */
123 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
124 {
125         int count, rc;
126         char *p;
127         ENTRY;
128
129         for (p = data, count = 0; p < (char *)data + len; count++) {
130                 char *end;
131                 int tmp = simple_strtoul(p, &end, 0);
132
133                 if (p == end) {
134                         CERROR("invalid device ID starting at: %s\n", p);
135                         GOTO(err_disconnect, rc = -EINVAL);
136                 }
137
138                 obddev->obd_multi_conn[count].oc_dev = &obd_dev[tmp];
139                 rc = obd_connect(&obddev->obd_multi_conn[count]);
140                 if (rc) {
141                         CERROR("cannot connect to device %d: rc = %d\n", tmp,
142                                rc);
143                         GOTO(err_disconnect, rc);
144                 }
145
146                 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
147                        obd_dev[tmp].obd_type->typ_name);
148
149                 p = end + 1;
150         }
151
152         obddev->obd_multi_count = count;
153
154         RETURN(0);
155
156  err_disconnect:
157         for (count--; count >= 0; count--)
158                 obd_disconnect(&obddev->obd_multi_conn[count]);
159         return rc;
160 }
161
162 /*
163  *    remove all connections to this device
164  *    close all connections to lower devices
165  *    needed for forced unloads of OBD client drivers
166  */
167 int gen_multi_cleanup(struct obd_device *obddev)
168 {
169         int i;
170
171         for (i = 0; i < obddev->obd_multi_count; i++) {
172                 int rc = obd_disconnect(&obddev->obd_multi_conn[i]);
173                 if (rc)
174                         CERROR("disconnect failure %d\n",
175                                obddev->obd_multi_conn[i].oc_dev->obd_minor);
176         }
177         return 0;
178 }
179
180
181 /*
182  *    forced cleanup of the device:
183  *    - remove connections from the device
184  *    - cleanup the device afterwards
185  */
186 int gen_cleanup(struct obd_device * obddev)
187 {
188         struct list_head * lh, * tmp;
189         struct obd_client * cli;
190
191         ENTRY;
192
193         lh = tmp = &obddev->obd_gen_clients;
194         while ((tmp = tmp->next) != lh) {
195                 cli = list_entry(tmp, struct obd_client, cli_chain);
196                 CDEBUG(D_INFO, "Disconnecting obd_connection %d, at %p\n",
197                        cli->cli_id, cli);
198         }
199         return 0;
200 } /* sim_cleanup_device */
201
202 void ___wait_on_page(struct page *page)
203 {
204         struct task_struct *tsk = current;
205         DECLARE_WAITQUEUE(wait, tsk);
206
207         add_wait_queue(&page->wait, &wait);
208         do {
209                 run_task_queue(&tq_disk);
210                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
211                 if (!PageLocked(page))
212                         break;
213                 schedule();
214         } while (PageLocked(page));
215         tsk->state = TASK_RUNNING;
216         remove_wait_queue(&page->wait, &wait);
217 }
218
219 void lck_page(struct page *page)
220 {
221         while (TryLockPage(page))
222                 ___wait_on_page(page);
223 }
224
225 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
226                   struct obd_conn *src_conn, struct obdo *src,
227                   obd_size count, obd_off offset)
228 {
229         struct page *page;
230         unsigned long index = 0;
231         int err = 0;
232
233         ENTRY;
234         CDEBUG(D_INFO, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n",
235                (unsigned long long)src->o_id, (unsigned long long)src->o_blocks,
236                (unsigned long long)src->o_size, (unsigned long long)dst->o_id);
237         page = alloc_page(GFP_USER);
238         if (page == NULL)
239                 RETURN(-ENOMEM);
240
241         lck_page(page);
242
243         /* XXX with brw vector I/O, we could batch up reads and writes here,
244          *     all we need to do is allocate multiple pages to handle the I/Os
245          *     and arrays to handle the request parameters.
246          */
247         while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
248                 obd_count        num_oa = 1;
249                 obd_count        num_buf = 1;
250                 obd_size         brw_count = PAGE_SIZE;
251                 obd_off          brw_offset = (page->index) << PAGE_SHIFT;
252                 obd_flag         flagr = 0;
253                 obd_flag         flagw = OBD_BRW_CREATE;
254
255                 page->index = index;
256                 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, num_oa, &src,
257                                                  &num_buf, &page, &brw_count,
258                                                  &brw_offset, &flagr);
259
260                 if ( err ) {
261                         EXIT;
262                         break;
263                 }
264                 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
265
266                 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
267                                                  &num_buf, &page, &brw_count,
268                                                  &brw_offset, &flagw);
269
270                 /* XXX should handle dst->o_size, dst->o_blocks here */
271                 if ( err ) {
272                         EXIT;
273                         break;
274                 }
275
276                 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
277
278                 index++;
279         }
280         dst->o_size = src->o_size;
281         dst->o_blocks = src->o_blocks;
282         dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
283         UnlockPage(page);
284         __free_page(page);
285
286         RETURN(err);
287 }