Whamcloud - gitweb
Make a distinction between bulk callbacks and brw callbacks - the bulk
[fs/lustre-release.git] / lustre / osc / osc_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *  This code is issued under the GNU General Public License.
7  *  See the file COPYING in this distribution
8  *
9  *  Author Peter Braam <braam@clusterfs.com>
10  *
11  *  This server is single threaded at present (but can easily be multi
12  *  threaded). For testing and management it is treated as an
13  *  obd_device, although it does not export a full OBD method table
14  *  (the requests are coming in over the wire, so object target
15  *  modules do not have a full method table.)
16  *
17  */
18
19 #define EXPORT_SYMTAB
20 #define DEBUG_SUBSYSTEM S_OSC
21
22 #include <linux/module.h>
23 #include <linux/lustre_dlm.h>
24 #include <linux/lustre_mds.h> /* for mds_objid */
25 #include <linux/obd_ost.h>
26 #include <linux/obd_lov.h>
27
28 static void osc_con2cl(struct lustre_handle *conn, struct ptlrpc_client **cl,
29                        struct ptlrpc_connection **connection,
30                        struct lustre_handle **rconn)
31 {
32         struct obd_export *export = class_conn2export(conn);
33         struct osc_obd *osc = &export->exp_obd->u.osc;
34
35         *cl = osc->osc_client;
36         *connection = osc->osc_conn;
37         *rconn = &export->exp_rconnh;
38 }
39
40 static void osc_con2dlmcl(struct lustre_handle *conn, struct ptlrpc_client **cl,
41                           struct ptlrpc_connection **connection,
42                           struct lustre_handle **rconn)
43 {
44         struct obd_export *export = class_conn2export(conn);
45         struct osc_obd *osc = &export->exp_obd->u.osc;
46
47         *cl = osc->osc_ldlm_client;
48         *connection = osc->osc_conn;
49         *rconn = &export->exp_rconnh;
50 }
51
52 static int osc_connect(struct lustre_handle *conn, struct obd_device *obd)
53 {
54         struct osc_obd *osc = &obd->u.osc;
55         //struct obd_import *import;
56         struct ptlrpc_request *request;
57         char *tmp = osc->osc_target_uuid;
58         int rc, size = sizeof(osc->osc_target_uuid);
59         ENTRY;
60
61         /* not used yet
62         OBD_ALLOC(import, sizeof(*import));
63         if (!import)
64                 RETURN(-ENOMEM);
65          */
66
67         MOD_INC_USE_COUNT;
68         rc = class_connect(conn, obd);
69         if (rc)
70                 RETURN(rc);
71
72         request = ptlrpc_prep_req(osc->osc_client, osc->osc_conn,
73                                   OST_CONNECT, 1, &size, &tmp);
74         if (!request)
75                 GOTO(out_disco, rc = -ENOMEM);
76
77         request->rq_level = LUSTRE_CONN_NEW;
78         request->rq_replen = lustre_msg_size(0, NULL);
79         request->rq_reqmsg->addr = -1;
80         /* Sending our local connection info breaks for local connections
81         request->rq_reqmsg->addr = conn->addr;
82         request->rq_reqmsg->cookie = conn->cookie;
83          */
84
85         rc = ptlrpc_queue_wait(request);
86         rc = ptlrpc_check_status(request, rc);
87         if (rc) {
88                 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
89                 GOTO(out, rc);
90         }
91
92         /* XXX eventually maybe more refinement */
93         osc->osc_conn->c_level = LUSTRE_CONN_FULL;
94
95         class_rconn2export(conn, (struct lustre_handle *)request->rq_repmsg);
96
97         EXIT;
98  out:
99         ptlrpc_free_req(request);
100  out_disco:
101         if (rc) {
102                 class_disconnect(conn);
103                 MOD_DEC_USE_COUNT;
104         }
105         return rc;
106 }
107
108 static int osc_disconnect(struct lustre_handle *conn)
109 {
110         struct ptlrpc_request *request;
111         struct ptlrpc_client *cl;
112         struct ptlrpc_connection *connection;
113         struct lustre_handle *rconn;
114         int rc;
115         ENTRY;
116
117         osc_con2cl(conn, &cl, &connection, &rconn);
118         request = ptlrpc_prep_req2(cl, connection, rconn,
119                                    OST_DISCONNECT, 0, NULL, NULL);
120         if (!request)
121                 RETURN(-ENOMEM);
122         request->rq_replen = lustre_msg_size(0, NULL);
123
124         rc = ptlrpc_queue_wait(request);
125         if (rc)
126                 GOTO(out, rc);
127         rc = class_disconnect(conn);
128         if (!rc)
129                 MOD_DEC_USE_COUNT;
130
131  out:
132         ptlrpc_free_req(request);
133         return rc;
134 }
135
136 static int osc_getattr(struct lustre_handle *conn, struct obdo *oa)
137 {
138         struct ptlrpc_request *request;
139         struct ptlrpc_client *cl;
140         struct ptlrpc_connection *connection;
141         struct lustre_handle *rconn;
142         struct ost_body *body;
143         int rc, size = sizeof(*body);
144         ENTRY;
145
146         osc_con2cl(conn, &cl, &connection, &rconn);
147         request = ptlrpc_prep_req2(cl, connection, rconn,
148                                    OST_GETATTR, 1, &size, NULL);
149         if (!request)
150                 RETURN(-ENOMEM);
151
152         body = lustre_msg_buf(request->rq_reqmsg, 0);
153         memcpy(&body->oa, oa, sizeof(*oa));
154         body->oa.o_valid = ~0;
155
156         request->rq_replen = lustre_msg_size(1, &size);
157
158         rc = ptlrpc_queue_wait(request);
159         rc = ptlrpc_check_status(request, rc);
160         if (rc) {
161                 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
162                 GOTO(out, rc);
163         }
164
165         body = lustre_msg_buf(request->rq_repmsg, 0);
166         CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
167         if (oa)
168                 memcpy(oa, &body->oa, sizeof(*oa));
169
170         EXIT;
171  out:
172         ptlrpc_free_req(request);
173         return rc;
174 }
175
176 static int osc_open(struct lustre_handle *conn, struct obdo *oa,
177                     struct lov_stripe_md *md)
178 {
179         struct ptlrpc_request *request;
180         struct ptlrpc_client *cl;
181         struct ptlrpc_connection *connection;
182         struct lustre_handle *rconn;
183         struct ost_body *body;
184         int rc, size = sizeof(*body);
185         ENTRY;
186
187         osc_con2cl(conn, &cl, &connection, &rconn);
188         request = ptlrpc_prep_req2(cl, connection, rconn,
189                                    OST_OPEN, 1, &size, NULL);
190         if (!request)
191                 RETURN(-ENOMEM);
192
193         body = lustre_msg_buf(request->rq_reqmsg, 0);
194         memcpy(&body->oa, oa, sizeof(*oa));
195         body->oa.o_valid = (OBD_MD_FLMODE | OBD_MD_FLID);
196
197         request->rq_replen = lustre_msg_size(1, &size);
198
199         rc = ptlrpc_queue_wait(request);
200         rc = ptlrpc_check_status(request, rc);
201         if (rc)
202                 GOTO(out, rc);
203
204         body = lustre_msg_buf(request->rq_repmsg, 0);
205         CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
206         if (oa)
207                 memcpy(oa, &body->oa, sizeof(*oa));
208
209         EXIT;
210  out:
211         ptlrpc_free_req(request);
212         return rc;
213 }
214
215 static int osc_close(struct lustre_handle *conn, struct obdo *oa,
216                      struct lov_stripe_md *md)
217 {
218         struct ptlrpc_request *request;
219         struct ptlrpc_client *cl;
220         struct ptlrpc_connection *connection;
221         struct lustre_handle *rconn;
222         struct ost_body *body;
223         int rc, size = sizeof(*body);
224         ENTRY;
225
226         osc_con2cl(conn, &cl, &connection, &rconn);
227         request = ptlrpc_prep_req2(cl, connection, rconn,
228                                    OST_CLOSE, 1, &size, NULL);
229         if (!request)
230                 RETURN(-ENOMEM);
231
232         oa->o_id = md->lmd_object_id;
233         oa->o_mode = S_IFREG;
234         oa->o_valid = (OBD_MD_FLMODE | OBD_MD_FLID);
235         body = lustre_msg_buf(request->rq_reqmsg, 0);
236         memcpy(&body->oa, oa, sizeof(*oa));
237
238         request->rq_replen = lustre_msg_size(1, &size);
239
240         rc = ptlrpc_queue_wait(request);
241         rc = ptlrpc_check_status(request, rc);
242         if (rc)
243                 GOTO(out, rc);
244
245         body = lustre_msg_buf(request->rq_repmsg, 0);
246         CDEBUG(D_INODE, "mode: %o\n", body->oa.o_mode);
247         if (oa)
248                 memcpy(oa, &body->oa, sizeof(*oa));
249
250         EXIT;
251  out:
252         ptlrpc_free_req(request);
253         return rc;
254 }
255
256 static int osc_setattr(struct lustre_handle *conn, struct obdo *oa)
257 {
258         struct ptlrpc_request *request;
259         struct ptlrpc_client *cl;
260         struct ptlrpc_connection *connection;
261         struct lustre_handle *rconn;
262         struct ost_body *body;
263         int rc, size = sizeof(*body);
264         ENTRY;
265
266         osc_con2cl(conn, &cl, &connection, &rconn);
267         request = ptlrpc_prep_req2(cl, connection, rconn,
268                                   OST_SETATTR, 1, &size, NULL);
269         if (!request)
270                 RETURN(-ENOMEM);
271
272         body = lustre_msg_buf(request->rq_reqmsg, 0);
273         memcpy(&body->oa, oa, sizeof(*oa));
274
275         request->rq_replen = lustre_msg_size(1, &size);
276
277         rc = ptlrpc_queue_wait(request);
278         rc = ptlrpc_check_status(request, rc);
279         GOTO(out, rc);
280
281  out:
282         ptlrpc_free_req(request);
283         return rc;
284 }
285
286 static int osc_create(struct lustre_handle *conn, struct obdo *oa,
287                       struct lov_stripe_md **ea)
288 {
289         struct ptlrpc_request *request;
290         struct ptlrpc_client *cl;
291         struct ptlrpc_connection *connection;
292         struct lustre_handle *rconn;
293         struct ost_body *body;
294         int rc, size = sizeof(*body);
295         ENTRY;
296
297         if (!oa) {
298                 CERROR("oa NULL\n");
299                 RETURN(-EINVAL);
300         }
301
302         if (!ea) {
303                 LBUG();
304         }
305
306         if (!*ea) {
307                 OBD_ALLOC(*ea, oa->o_easize);
308                 if (!*ea)
309                         RETURN(-ENOMEM);
310                 (*ea)->lmd_size = oa->o_easize;
311         }
312
313         osc_con2cl(conn, &cl, &connection, &rconn);
314         request = ptlrpc_prep_req2(cl, connection, rconn,
315                                   OST_CREATE, 1, &size, NULL);
316         if (!request)
317                 RETURN(-ENOMEM);
318
319         body = lustre_msg_buf(request->rq_reqmsg, 0);
320         memcpy(&body->oa, oa, sizeof(*oa));
321
322         request->rq_replen = lustre_msg_size(1, &size);
323
324         rc = ptlrpc_queue_wait(request);
325         rc = ptlrpc_check_status(request, rc);
326         if (rc)
327                 GOTO(out, rc);
328
329         body = lustre_msg_buf(request->rq_repmsg, 0);
330         memcpy(oa, &body->oa, sizeof(*oa));
331
332         (*ea)->lmd_object_id = oa->o_id;
333         (*ea)->lmd_stripe_count = 1;
334         EXIT;
335  out:
336         ptlrpc_free_req(request);
337         return rc;
338 }
339
340 static int osc_punch(struct lustre_handle *conn, struct obdo *oa,
341                      struct lov_stripe_md *md, obd_size count,
342                      obd_off offset)
343 {
344         struct ptlrpc_request *request;
345         struct ptlrpc_client *cl;
346         struct ptlrpc_connection *connection;
347         struct lustre_handle *rconn;
348         struct ost_body *body;
349         int rc, size = sizeof(*body);
350         ENTRY;
351
352         if (!oa) {
353                 CERROR("oa NULL\n");
354                 RETURN(-EINVAL);
355         }
356         osc_con2cl(conn, &cl, &connection, &rconn);
357         request = ptlrpc_prep_req2(cl, connection, rconn,
358                                    OST_PUNCH, 1, &size, NULL);
359         if (!request)
360                 RETURN(-ENOMEM);
361
362         body = lustre_msg_buf(request->rq_reqmsg, 0);
363         memcpy(&body->oa, oa, sizeof(*oa));
364         body->oa.o_blocks = count;
365         body->oa.o_valid |= OBD_MD_FLBLOCKS;
366
367         request->rq_replen = lustre_msg_size(1, &size);
368
369         rc = ptlrpc_queue_wait(request);
370         rc = ptlrpc_check_status(request, rc);
371         if (rc)
372                 GOTO(out, rc);
373
374         body = lustre_msg_buf(request->rq_repmsg, 0);
375         memcpy(oa, &body->oa, sizeof(*oa));
376
377         EXIT;
378  out:
379         ptlrpc_free_req(request);
380         return rc;
381 }
382
383 static int osc_destroy(struct lustre_handle *conn, struct obdo *oa,
384                        struct lov_stripe_md *ea)
385 {
386         struct ptlrpc_request *request;
387         struct ptlrpc_client *cl;
388         struct ptlrpc_connection *connection;
389         struct lustre_handle *rconn;
390         struct ost_body *body;
391         int rc, size = sizeof(*body);
392         ENTRY;
393
394         if (!oa) {
395                 CERROR("oa NULL\n");
396                 RETURN(-EINVAL);
397         }
398         osc_con2cl(conn, &cl, &connection, &rconn);
399         request = ptlrpc_prep_req2(cl, connection, rconn,
400                                    OST_DESTROY, 1, &size, NULL);
401         if (!request)
402                 RETURN(-ENOMEM);
403
404         body = lustre_msg_buf(request->rq_reqmsg, 0);
405         memcpy(&body->oa, oa, sizeof(*oa));
406         body->oa.o_valid = ~0;
407
408         request->rq_replen = lustre_msg_size(1, &size);
409
410         rc = ptlrpc_queue_wait(request);
411         rc = ptlrpc_check_status(request, rc);
412         if (rc)
413                 GOTO(out, rc);
414
415         body = lustre_msg_buf(request->rq_repmsg, 0);
416         memcpy(oa, &body->oa, sizeof(*oa));
417
418         EXIT;
419  out:
420         ptlrpc_free_req(request);
421         return rc;
422 }
423
424 struct osc_brw_cb_data {
425         brw_callback_t callback;
426         void *cb_data;
427         void *obd_data;
428         size_t obd_size;
429 };
430
431 /* Our bulk-unmapping bottom half. */
432 static void unmap_and_decref_bulk_desc(void *data)
433 {
434         struct ptlrpc_bulk_desc *desc = data;
435         struct list_head *tmp;
436         ENTRY;
437
438         /* This feels wrong to me. */
439         list_for_each(tmp, &desc->b_page_list) {
440                 struct ptlrpc_bulk_page *bulk;
441                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
442
443                 kunmap(bulk->b_page);
444         }
445
446         ptlrpc_bulk_decref(desc);
447         EXIT;
448 }
449
450 static void brw_finish(struct ptlrpc_bulk_desc *desc, void *data)
451 {
452         struct osc_brw_cb_data *cb_data = data;
453         ENTRY;
454
455         if (desc->b_flags & PTL_RPC_FL_INTR)
456                 CERROR("got signal\n");
457
458         if (cb_data->callback)
459                 cb_data->callback(cb_data->cb_data);
460
461         OBD_FREE(cb_data->obd_data, cb_data->obd_size);
462         OBD_FREE(cb_data, sizeof(*cb_data));
463
464         /* We can't kunmap the desc from interrupt context, so we do it from
465          * the bottom half above. */
466         INIT_TQUEUE(&desc->b_queue, 0, 0);
467         PREPARE_TQUEUE(&desc->b_queue, unmap_and_decref_bulk_desc, desc);
468         schedule_task(&desc->b_queue);
469
470         EXIT;
471 }
472
473 static int osc_brw_read(struct lustre_handle *conn, struct lov_stripe_md *md,
474                         obd_count page_count, struct page **page_array,
475                         obd_size *count, obd_off *offset, obd_flag *flags,
476                         brw_callback_t callback, void *data)
477 {
478         struct ptlrpc_client *cl;
479         struct ptlrpc_connection *connection;
480         struct lustre_handle *rconn;
481         struct ptlrpc_request *request = NULL;
482         struct ptlrpc_bulk_desc *desc = NULL;
483         struct ost_body *body;
484         struct osc_brw_cb_data *cb_data = NULL;
485         int rc, size[3] = {sizeof(*body)};
486         void *iooptr, *nioptr;
487         int mapped = 0;
488         ENTRY;
489
490         size[1] = sizeof(struct obd_ioobj);
491         size[2] = page_count * sizeof(struct niobuf_remote);
492
493         osc_con2cl(conn, &cl, &connection, &rconn);
494         request = ptlrpc_prep_req2(cl, connection, rconn,
495                                    OST_BRW, 3, size, NULL);
496         if (!request)
497                 RETURN(-ENOMEM);
498
499         body = lustre_msg_buf(request->rq_reqmsg, 0);
500         body->data = OBD_BRW_READ;
501
502         desc = ptlrpc_prep_bulk(connection);
503         if (!desc)
504                 GOTO(out_free, rc = -ENOMEM);
505         desc->b_portal = OST_BULK_PORTAL;
506         desc->b_cb = brw_finish;
507         OBD_ALLOC(cb_data, sizeof(*cb_data));
508         if (!cb_data)
509                 GOTO(out_free, rc = -ENOMEM);
510         cb_data->callback = callback;
511         cb_data->cb_data = data;
512         desc->b_cb_data = cb_data;
513         /* end almost identical to brw_write case */
514
515         iooptr = lustre_msg_buf(request->rq_reqmsg, 1);
516         nioptr = lustre_msg_buf(request->rq_reqmsg, 2);
517         ost_pack_ioo(&iooptr, md, page_count);
518         for (mapped = 0; mapped < page_count; mapped++) {
519                 struct ptlrpc_bulk_page *bulk;
520                 bulk = ptlrpc_prep_bulk_page(desc);
521                 if (bulk == NULL)
522                         GOTO(out_unmap, rc = -ENOMEM);
523
524                 spin_lock(&connection->c_lock);
525                 bulk->b_xid = ++connection->c_xid_out;
526                 spin_unlock(&connection->c_lock);
527
528                 bulk->b_buf = kmap(page_array[mapped]);
529                 bulk->b_page = page_array[mapped];
530                 bulk->b_buflen = PAGE_SIZE;
531                 ost_pack_niobuf(&nioptr, offset[mapped], count[mapped],
532                                 flags[mapped], bulk->b_xid);
533         }
534
535         /*
536          * Register the bulk first, because the reply could arrive out of order,
537          * and we want to be ready for the bulk data.
538          *
539          * One reference is released by the bulk callback, the other when
540          * we finish sleeping on it (if we don't have a callback).
541          */
542         atomic_set(&desc->b_refcount, callback ? 1 : 2);
543         rc = ptlrpc_register_bulk(desc);
544         if (rc)
545                 GOTO(out_unmap, rc);
546
547         request->rq_replen = lustre_msg_size(1, size);
548         rc = ptlrpc_queue_wait(request);
549         rc = ptlrpc_check_status(request, rc);
550         if (rc) {
551                 ptlrpc_bulk_decref(desc);
552                 GOTO(out_unmap, rc);
553         }
554
555         /* Callbacks cause asynchronous handling. */
556         if (callback)
557                 RETURN(0);
558
559         l_wait_event_killable(desc->b_waitq, ptlrpc_check_bulk_received(desc));
560         rc = desc->b_flags & PTL_RPC_FL_INTR ? -EINTR : 0;
561         ptlrpc_bulk_decref(desc);
562         RETURN(rc);
563
564         /* Clean up on error. */
565  out_unmap:
566         while (mapped-- > 0)
567                 kunmap(page_array[mapped]);
568  out_free:
569         if (cb_data)
570                 OBD_FREE(cb_data, sizeof(*cb_data));
571         ptlrpc_free_bulk(desc);
572         ptlrpc_free_req(request);
573         return rc;
574 }
575
576 static int osc_brw_write(struct lustre_handle *conn,
577                          struct lov_stripe_md *md, obd_count page_count,
578                          struct page **pagearray, obd_size *count,
579                          obd_off *offset, obd_flag *flags,
580                          brw_callback_t callback, void *data)
581 {
582         struct ptlrpc_client *cl;
583         struct ptlrpc_connection *connection;
584         struct lustre_handle *rconn;
585         struct ptlrpc_request *request = NULL;
586         struct ptlrpc_bulk_desc *desc = NULL;
587         struct ost_body *body;
588         struct niobuf_local *local = NULL;
589         struct niobuf_remote *remote;
590         struct osc_brw_cb_data *cb_data = NULL;
591         int rc, j, size[3] = {sizeof(*body)};
592         void *iooptr, *nioptr;
593         int mapped = 0;
594         ENTRY;
595
596         size[1] = sizeof(struct obd_ioobj);
597         size[2] = page_count * sizeof(*remote);
598
599         osc_con2cl(conn, &cl, &connection, &rconn);
600         request = ptlrpc_prep_req2(cl, connection, rconn,
601                                    OST_BRW, 3, size, NULL);
602         if (!request)
603                 RETURN(-ENOMEM);
604
605         body = lustre_msg_buf(request->rq_reqmsg, 0);
606         body->data = OBD_BRW_WRITE;
607
608         OBD_ALLOC(local, page_count * sizeof(*local));
609         if (!local)
610                 GOTO(out_free, rc = -ENOMEM);
611
612         desc = ptlrpc_prep_bulk(connection);
613         if (!desc)
614                 GOTO(out_free, rc = -ENOMEM);
615         desc->b_portal = OSC_BULK_PORTAL;
616         desc->b_cb = brw_finish;
617         OBD_ALLOC(cb_data, sizeof(*cb_data));
618         if (!cb_data)
619                 GOTO(out_free, rc = -ENOMEM);
620         cb_data->callback = callback;
621         cb_data->cb_data = data;
622         desc->b_cb_data = cb_data;
623
624         iooptr = lustre_msg_buf(request->rq_reqmsg, 1);
625         nioptr = lustre_msg_buf(request->rq_reqmsg, 2);
626         ost_pack_ioo(&iooptr, md, page_count);
627         /* end almost identical to brw_read case */
628
629         cb_data->obd_data = local;
630         cb_data->obd_size = page_count * sizeof(*local);
631
632         for (mapped = 0; mapped < page_count; mapped++) {
633                 local[mapped].addr = kmap(pagearray[mapped]);
634                 local[mapped].offset = offset[mapped];
635                 local[mapped].len = count[mapped];
636                 ost_pack_niobuf(&nioptr, offset[mapped], count[mapped],
637                                 flags[mapped], 0);
638         }
639
640         size[1] = page_count * sizeof(*remote);
641         request->rq_replen = lustre_msg_size(2, size);
642         rc = ptlrpc_queue_wait(request);
643         rc = ptlrpc_check_status(request, rc);
644         if (rc)
645                 GOTO(out_unmap, rc);
646
647         nioptr = lustre_msg_buf(request->rq_repmsg, 1);
648         if (!nioptr)
649                 GOTO(out_unmap, rc = -EINVAL);
650
651         if (request->rq_repmsg->buflens[1] != size[1]) {
652                 CERROR("buffer length wrong (%d vs. %d)\n",
653                        request->rq_repmsg->buflens[1], size[1]);
654                 GOTO(out_unmap, rc = -EINVAL);
655         }
656
657         for (j = 0; j < page_count; j++) {
658                 struct ptlrpc_bulk_page *bulk;
659
660                 ost_unpack_niobuf(&nioptr, &remote);
661
662                 bulk = ptlrpc_prep_bulk_page(desc);
663                 if (!bulk)
664                         GOTO(out_unmap, rc = -ENOMEM);
665
666                 bulk->b_buf = (void *)(unsigned long)local[j].addr;
667                 bulk->b_buflen = local[j].len;
668                 bulk->b_xid = remote->xid;
669         }
670
671         if (desc->b_page_count != page_count)
672                 LBUG();
673
674         /*
675          * One is released when the bulk is complete, the other when we finish
676          * waiting on it.  (Callback cases don't sleep, so only one ref for
677          * them.)
678          */
679         atomic_set(&desc->b_refcount, callback ? 1 : 2);
680         CDEBUG(D_PAGE, "Set refcount of %p to %d\n", desc,
681                atomic_read(&desc->b_refcount));
682         rc = ptlrpc_send_bulk(desc);
683         if (rc)
684                 GOTO(out_unmap, rc);
685
686         /* Callbacks cause asynchronous handling. */
687         if (callback)
688                 RETURN(0);
689
690         /* If there's no callback function, sleep here until complete. */
691         l_wait_event_killable(desc->b_waitq, ptlrpc_check_bulk_sent(desc));
692         ptlrpc_bulk_decref(desc);
693         if (desc->b_flags & PTL_RPC_FL_INTR)
694                 RETURN(-EINTR);
695         RETURN(0);
696
697         /* Clean up on error. */
698  out_unmap:
699         while (mapped-- > 0)
700                 kunmap(pagearray[mapped]);
701
702  out_free:
703         OBD_FREE(cb_data, sizeof(*cb_data));
704         OBD_FREE(local, page_count * sizeof(*local));
705         ptlrpc_free_bulk(desc);
706         ptlrpc_req_finished(request);
707         return rc;
708 }
709
710 static int osc_brw(int cmd, struct lustre_handle *conn,
711                    struct lov_stripe_md *md, obd_count page_count,
712                    struct page **page_array, obd_size *count, obd_off *offset,
713                    obd_flag *flags, brw_callback_t callback, void *data)
714 {
715         if (cmd & OBD_BRW_WRITE)
716                 return osc_brw_write(conn, md, page_count, page_array, count,
717                                      offset, flags, callback, data);
718         else
719                 return osc_brw_read(conn, md, page_count, page_array, count,
720                                     offset, flags, callback, data);
721 }
722
723 static int osc_enqueue(struct lustre_handle *conn,
724                        struct lustre_handle *parent_lock, __u64 *res_id,
725                        __u32 type, void *extentp, int extent_len, __u32 mode,
726                        int *flags, void *callback, void *data, int datalen,
727                        struct lustre_handle *lockh)
728 {
729         struct obd_device *obddev = class_conn2obd(conn);
730         struct ptlrpc_connection *connection;
731         struct ptlrpc_client *cl;
732         struct lustre_handle *rconn;
733         struct ldlm_extent *extent = extentp;
734         int rc;
735         __u32 mode2;
736
737         /* Filesystem locks are given a bit of special treatment: first we
738          * fixup the lock to start and end on page boundaries. */
739         extent->start &= PAGE_MASK;
740         extent->end = (extent->end + PAGE_SIZE - 1) & PAGE_MASK;
741
742         /* Next, search for already existing extent locks that will cover us */
743         osc_con2dlmcl(conn, &cl, &connection, &rconn);
744         rc = ldlm_lock_match(obddev->obd_namespace, res_id, type, extent,
745                              sizeof(extent), mode, lockh);
746         if (rc == 1) {
747                 /* We already have a lock, and it's referenced */
748                 return 0;
749         }
750
751         /* Next, search for locks that we can upgrade (if we're trying to write)
752          * or are more than we need (if we're trying to read).  Because the VFS
753          * and page cache already protect us locally, lots of readers/writers
754          * can share a single PW lock. */
755         if (mode == LCK_PW)
756                 mode2 = LCK_PR;
757         else
758                 mode2 = LCK_PW;
759
760         rc = ldlm_lock_match(obddev->obd_namespace, res_id, type, extent,
761                              sizeof(extent), mode2, lockh);
762         if (rc == 1) {
763                 int flags;
764                 /* FIXME: This is not incredibly elegant, but it might
765                  * be more elegant than adding another parameter to
766                  * lock_match.  I want a second opinion. */
767                 ldlm_lock_addref(lockh, mode);
768                 ldlm_lock_decref(lockh, mode2);
769
770                 if (mode == LCK_PR)
771                         return 0;
772
773                 rc = ldlm_cli_convert(cl, lockh, rconn, mode, &flags);
774                 if (rc)
775                         LBUG();
776
777                 return rc;
778         }
779
780         rc = ldlm_cli_enqueue(cl, connection, rconn, NULL,obddev->obd_namespace,
781                               parent_lock, res_id, type, extent, sizeof(extent),
782                               mode, flags, callback, data, datalen, lockh);
783         return rc;
784 }
785
786 static int osc_cancel(struct lustre_handle *oconn, __u32 mode,
787                       struct lustre_handle *lockh)
788 {
789         ENTRY;
790
791         ldlm_lock_decref(lockh, mode);
792
793         RETURN(0);
794 }
795
796 static int osc_setup(struct obd_device *obddev, obd_count len, void *buf)
797 {
798         struct obd_ioctl_data* data = buf;
799         struct osc_obd *osc = &obddev->u.osc;
800         char server_uuid[37];
801         int rc;
802         ENTRY;
803
804         if (data->ioc_inllen1 < 1) {
805                 CERROR("osc setup requires a TARGET UUID\n");
806                 RETURN(-EINVAL);
807         }
808
809         if (data->ioc_inllen1 > 37) {
810                 CERROR("osc TARGET UUID must be less than 38 characters\n");
811                 RETURN(-EINVAL);
812         }
813
814         if (data->ioc_inllen2 < 1) {
815                 CERROR("osc setup requires a SERVER UUID\n");
816                 RETURN(-EINVAL);
817         }
818
819         if (data->ioc_inllen2 > 37) {
820                 CERROR("osc SERVER UUID must be less than 38 characters\n");
821                 RETURN(-EINVAL);
822         }
823
824         memcpy(osc->osc_target_uuid, data->ioc_inlbuf1, data->ioc_inllen1);
825         memcpy(server_uuid, data->ioc_inlbuf2, MIN(data->ioc_inllen2,
826                                                    sizeof(server_uuid)));
827
828         osc->osc_conn = ptlrpc_uuid_to_connection(server_uuid);
829         if (!osc->osc_conn)
830                 RETURN(-ENOENT);
831
832         obddev->obd_namespace =
833                 ldlm_namespace_new("osc", LDLM_NAMESPACE_CLIENT);
834         if (obddev->obd_namespace == NULL)
835                 GOTO(out_conn, rc = -ENOMEM);
836
837         OBD_ALLOC(osc->osc_client, sizeof(*osc->osc_client));
838         if (osc->osc_client == NULL)
839                 GOTO(out_ns, rc = -ENOMEM);
840
841         OBD_ALLOC(osc->osc_ldlm_client, sizeof(*osc->osc_ldlm_client));
842         if (osc->osc_ldlm_client == NULL)
843                 GOTO(out_client, rc = -ENOMEM);
844
845         ptlrpc_init_client(NULL, NULL, OST_REQUEST_PORTAL, OSC_REPLY_PORTAL,
846                            osc->osc_client);
847         ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
848                            osc->osc_ldlm_client);
849         osc->osc_client->cli_name = "osc";
850         osc->osc_ldlm_client->cli_name = "ldlm";
851
852         MOD_INC_USE_COUNT;
853         RETURN(0);
854
855  out_client:
856         OBD_FREE(osc->osc_client, sizeof(*osc->osc_client));
857  out_ns:
858         ldlm_namespace_free(obddev->obd_namespace);
859  out_conn:
860         ptlrpc_put_connection(osc->osc_conn);
861         return rc;
862 }
863
864 static int osc_cleanup(struct obd_device * obddev)
865 {
866         struct osc_obd *osc = &obddev->u.osc;
867
868         ldlm_namespace_free(obddev->obd_namespace);
869
870         ptlrpc_cleanup_client(osc->osc_client);
871         OBD_FREE(osc->osc_client, sizeof(*osc->osc_client));
872         ptlrpc_cleanup_client(osc->osc_ldlm_client);
873         OBD_FREE(osc->osc_ldlm_client, sizeof(*osc->osc_ldlm_client));
874         ptlrpc_put_connection(osc->osc_conn);
875
876         MOD_DEC_USE_COUNT;
877         return 0;
878 }
879
880 static int osc_statfs(struct lustre_handle *conn, struct statfs *sfs)
881 {
882         struct ptlrpc_request *request;
883         struct ptlrpc_client *cl;
884         struct ptlrpc_connection *connection;
885         struct lustre_handle *rconn;
886         struct obd_statfs *osfs;
887         int rc, size = sizeof(*osfs);
888         ENTRY;
889
890         osc_con2cl(conn, &cl, &connection, &rconn);
891         request = ptlrpc_prep_req2(cl, connection, rconn,
892                                    OST_STATFS, 0, NULL, NULL);
893         if (!request)
894                 RETURN(-ENOMEM);
895
896         request->rq_replen = lustre_msg_size(1, &size);
897
898         rc = ptlrpc_queue_wait(request);
899         rc = ptlrpc_check_status(request, rc);
900         if (rc) {
901                 CERROR("%s failed: rc = %d\n", __FUNCTION__, rc);
902                 GOTO(out, rc);
903         }
904
905         osfs = lustre_msg_buf(request->rq_repmsg, 0);
906         obd_statfs_unpack(osfs, sfs);
907
908         EXIT;
909  out:
910         ptlrpc_free_req(request);
911         return rc;
912 }
913
914 struct obd_ops osc_obd_ops = {
915         o_setup:        osc_setup,
916         o_cleanup:      osc_cleanup,
917         o_statfs:       osc_statfs,
918         o_create:       osc_create,
919         o_destroy:      osc_destroy,
920         o_getattr:      osc_getattr,
921         o_setattr:      osc_setattr,
922         o_open:         osc_open,
923         o_close:        osc_close,
924         o_connect:      osc_connect,
925         o_disconnect:   osc_disconnect,
926         o_brw:          osc_brw,
927         o_punch:        osc_punch,
928         o_enqueue:      osc_enqueue,
929         o_cancel:       osc_cancel
930 };
931
932 static int __init osc_init(void)
933 {
934         return class_register_type(&osc_obd_ops, LUSTRE_OSC_NAME);
935 }
936
937 static void __exit osc_exit(void)
938 {
939         class_unregister_type(LUSTRE_OSC_NAME);
940 }
941
942 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
943 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC) v1.0");
944 MODULE_LICENSE("GPL");
945
946 module_init(osc_init);
947 module_exit(osc_exit);