Whamcloud - gitweb
Remove ___wait_on_page entirely. If we ever need it again, it will be
[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 lck_page(struct page *page)
203 {
204         while (TryLockPage(page))
205                 ___wait_on_page(page);
206 }
207
208 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
209                   struct obd_conn *src_conn, struct obdo *src,
210                   obd_size count, obd_off offset)
211 {
212         struct page *page;
213         unsigned long index = 0;
214         int err = 0;
215
216         ENTRY;
217         CDEBUG(D_INFO, "src: ino %Ld blocks %Ld, size %Ld, dst: ino %Ld\n",
218                (unsigned long long)src->o_id, (unsigned long long)src->o_blocks,
219                (unsigned long long)src->o_size, (unsigned long long)dst->o_id);
220         page = alloc_page(GFP_USER);
221         if (page == NULL)
222                 RETURN(-ENOMEM);
223
224         lck_page(page);
225
226         /* XXX with brw vector I/O, we could batch up reads and writes here,
227          *     all we need to do is allocate multiple pages to handle the I/Os
228          *     and arrays to handle the request parameters.
229          */
230         while (index < ((src->o_size + PAGE_SIZE - 1) >> PAGE_SHIFT)) {
231                 obd_count        num_oa = 1;
232                 obd_count        num_buf = 1;
233                 obd_size         brw_count = PAGE_SIZE;
234                 obd_off          brw_offset = (page->index) << PAGE_SHIFT;
235                 obd_flag         flagr = 0;
236                 obd_flag         flagw = OBD_BRW_CREATE;
237
238                 page->index = index;
239                 err = OBP(src_conn->oc_dev, brw)(READ, src_conn, num_oa, &src,
240                                                  &num_buf, &page, &brw_count,
241                                                  &brw_offset, &flagr);
242
243                 if ( err ) {
244                         EXIT;
245                         break;
246                 }
247                 CDEBUG(D_INFO, "Read page %ld ...\n", page->index);
248
249                 err = OBP(dst_conn->oc_dev, brw)(WRITE, dst_conn, num_oa, &dst,
250                                                  &num_buf, &page, &brw_count,
251                                                  &brw_offset, &flagw);
252
253                 /* XXX should handle dst->o_size, dst->o_blocks here */
254                 if ( err ) {
255                         EXIT;
256                         break;
257                 }
258
259                 CDEBUG(D_INFO, "Wrote page %ld ...\n", page->index);
260
261                 index++;
262         }
263         dst->o_size = src->o_size;
264         dst->o_blocks = src->o_blocks;
265         dst->o_valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
266         UnlockPage(page);
267         __free_page(page);
268
269         RETURN(err);
270 }