1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copryright (C) 2001 Cluster File Systems, Inc.
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
9 * Author Peter Braam <braam@clusterfs.com>
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.)
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/stat.h>
27 #include <linux/errno.h>
28 #include <linux/locks.h>
29 #include <linux/unistd.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
35 #include <linux/stat.h>
36 #include <asm/uaccess.h>
37 #include <asm/segment.h>
38 #include <linux/miscdevice.h>
40 #define DEBUG_SUBSYSTEM S_OSC
42 #include <linux/obd_support.h>
43 #include <linux/obd_class.h>
44 #include <linux/lustre_lib.h>
45 #include <linux/lustre_idl.h>
47 struct ptlrpc_client *osc_con2cl(struct obd_conn *conn)
49 struct osc_obd *osc = &conn->oc_dev->u.osc;
50 return &osc->osc_peer;
54 static int osc_connect(struct obd_conn *conn)
56 struct ptlrpc_request *request;
57 struct ptlrpc_client *peer = osc_con2cl(conn);
61 request = ptlrpc_prep_req(peer, OST_CONNECT, 0, NULL, 0, NULL);
63 CERROR("cannot pack req!\n");
68 sizeof(struct ptlrep_hdr) + sizeof(struct ost_rep);
70 rc = ptlrpc_queue_wait(peer, request);
76 CDEBUG(D_INODE, "received connid %d\n", request->rq_rep.ost->connid);
78 conn->oc_id = request->rq_rep.ost->connid;
80 ptlrpc_free_req(request);
85 static int osc_disconnect(struct obd_conn *conn)
87 struct ptlrpc_request *request;
88 struct ptlrpc_client *peer = osc_con2cl(conn);
92 request = ptlrpc_prep_req(peer, OST_DISCONNECT, 0, NULL, 0, NULL);
94 CERROR("cannot pack req!\n");
97 request->rq_req.ost->connid = conn->oc_id;
99 sizeof(struct ptlrep_hdr) + sizeof(struct ost_rep);
101 rc = ptlrpc_queue_wait(peer, request);
107 ptlrpc_free_req(request);
113 static int osc_getattr(struct obd_conn *conn, struct obdo *oa)
115 struct ptlrpc_request *request;
116 struct ptlrpc_client *peer = osc_con2cl(conn);
119 request = ptlrpc_prep_req(peer, OST_GETATTR, 0, NULL, 0, NULL);
121 CERROR("cannot pack req!\n");
125 memcpy(&request->rq_req.ost->oa, oa, sizeof(*oa));
126 request->rq_req.ost->oa.o_valid = ~0;
128 sizeof(struct ptlrep_hdr) + sizeof(struct ost_rep);
130 rc = ptlrpc_queue_wait(peer, request);
136 CDEBUG(D_INODE, "mode: %o\n", request->rq_rep.ost->oa.o_mode);
138 memcpy(oa, &request->rq_rep.ost->oa, sizeof(*oa));
142 ptlrpc_free_req(request);
146 static int osc_setattr(struct obd_conn *conn, struct obdo *oa)
148 struct ptlrpc_request *request;
149 struct ptlrpc_client *peer = osc_con2cl(conn);
152 request = ptlrpc_prep_req(peer, OST_SETATTR, 0, NULL, 0, NULL);
154 CERROR("cannot pack req!\n");
158 memcpy(&request->rq_req.ost->oa, oa, sizeof(*oa));
160 sizeof(struct ptlrep_hdr) + sizeof(struct ost_rep);
162 rc = ptlrpc_queue_wait(peer, request);
169 ptlrpc_free_req(request);
173 static int osc_create(struct obd_conn *conn, struct obdo *oa)
175 struct ptlrpc_request *request;
176 struct ptlrpc_client *peer = osc_con2cl(conn);
182 request = ptlrpc_prep_req(peer, OST_CREATE, 0, NULL, 0, NULL);
184 CERROR("cannot pack req!\n");
188 memcpy(&request->rq_req.ost->oa, oa, sizeof(*oa));
189 request->rq_req.ost->oa.o_valid = ~0;
191 sizeof(struct ptlrep_hdr) + sizeof(struct ost_rep);
193 rc = ptlrpc_queue_wait(peer, request);
198 memcpy(oa, &request->rq_rep.ost->oa, sizeof(*oa));
201 ptlrpc_free_req(request);
205 static int osc_punch(struct obd_conn *conn, struct obdo *oa, obd_size count,
208 struct ptlrpc_request *request;
209 struct ptlrpc_client *peer = osc_con2cl(conn);
215 request = ptlrpc_prep_req(peer, OST_PUNCH, 0, NULL, 0, NULL);
217 CERROR("cannot pack req!\n");
221 memcpy(&request->rq_req.ost->oa, oa, sizeof(*oa));
222 request->rq_req.ost->oa.o_valid = ~0;
223 request->rq_req.ost->oa.o_size = offset;
224 request->rq_req.ost->oa.o_blocks = count;
226 sizeof(struct ptlrep_hdr) + sizeof(struct ost_rep);
228 rc = ptlrpc_queue_wait(peer, request);
233 memcpy(oa, &request->rq_rep.ost->oa, sizeof(*oa));
236 ptlrpc_free_req(request);
240 static int osc_destroy(struct obd_conn *conn, struct obdo *oa)
242 struct ptlrpc_request *request;
243 struct ptlrpc_client *peer = osc_con2cl(conn);
249 request = ptlrpc_prep_req(peer, OST_DESTROY, 0, NULL, 0, NULL);
251 CERROR("cannot pack req!\n");
255 memcpy(&request->rq_req.ost->oa, oa, sizeof(*oa));
256 request->rq_req.ost->oa.o_valid = ~0;
258 sizeof(struct ptlrep_hdr) + sizeof(struct ost_rep);
260 rc = ptlrpc_queue_wait(peer, request);
265 memcpy(oa, &request->rq_rep.ost->oa, sizeof(*oa));
268 ptlrpc_free_req(request);
272 int osc_sendpage(struct obd_conn *conn, struct ptlrpc_request *req,
273 struct niobuf *dst, struct niobuf *src)
275 if (conn->oc_id != -1) {
277 memcpy((char *)(unsigned long)dst->addr,
278 (char *)(unsigned long)src->addr, src->len);
280 struct ptlrpc_client *cl = osc_con2cl(conn);
281 struct ptlrpc_bulk_desc *bulk;
285 bulk = ptlrpc_prep_bulk(&cl->cli_server);
289 spin_lock(&cl->cli_lock);
290 bulk->b_xid = cl->cli_xid++;
291 spin_unlock(&cl->cli_lock);
293 OBD_ALLOC(buf, src->len);
295 OBD_FREE(bulk, sizeof(*bulk));
299 memcpy(buf, (char *)(unsigned long)src->addr, src->len);
302 bulk->b_buflen = src->len;
303 /* FIXME: maybe we should add an XID to struct niobuf? */
304 bulk->b_xid = (__u32)(unsigned long)src->page;
306 rc = ptlrpc_send_bulk(bulk, OSC_BULK_PORTAL);
308 CERROR("send_bulk failed: %d\n", rc);
312 wait_event_interruptible(bulk->b_waitq,
313 ptlrpc_check_bulk_sent(bulk));
315 if (bulk->b_flags == PTL_RPC_INTR) {
317 /* FIXME: hey hey, we leak here. */
321 OBD_FREE(bulk, sizeof(*bulk));
322 OBD_FREE(buf, src->len);
328 int osc_brw_read(struct obd_conn *conn, obd_count num_oa, struct obdo **oa,
329 obd_count *oa_bufs, struct page **buf, obd_size *count,
330 obd_off *offset, obd_flag *flags)
332 struct ptlrpc_client *cl = osc_con2cl(conn);
333 struct ptlrpc_request *request;
336 struct obd_ioobj ioo;
338 int size1, size2 = 0;
341 struct ptlrpc_bulk_desc **bulk;
343 size1 = num_oa * sizeof(ioo);
345 for (i = 0; i < num_oa; i++) {
346 size2 += oa_bufs[i] * sizeof(src);
350 /* We actually pack a _third_ buffer, with XIDs for bulk pages */
351 size2 += pages * sizeof(__u32);
352 request = ptlrpc_prep_req(cl, OST_BRW, size1, NULL, size2, NULL);
354 CERROR("cannot pack req!\n");
357 request->rq_req.ost->cmd = OBD_BRW_READ;
359 OBD_ALLOC(bulk, pages * sizeof(struct ptlrpc_bulk_desc *));
361 CERROR("cannot alloc bulk desc vector\n");
364 memset(bulk, 0, pages * sizeof(struct ptlrpc_bulk_desc *));
367 ptr1 = ost_req_buf1(request->rq_req.ost);
368 ptr2 = ost_req_buf2(request->rq_req.ost);
369 for (i = 0; i < num_oa; i++) {
370 ost_pack_ioo(&ptr1, oa[i], oa_bufs[i]);
371 for (j = 0; j < oa_bufs[i]; j++) {
372 bulk[n] = ptlrpc_prep_bulk(&cl->cli_server);
373 if (bulk[n] == NULL) {
374 CERROR("cannot alloc bulk desc\n");
379 spin_lock(&cl->cli_lock);
380 bulk[n]->b_xid = cl->cli_xid++;
381 spin_unlock(&cl->cli_lock);
382 bulk[n]->b_buf = kmap(buf[n]);
383 bulk[n]->b_buflen = PAGE_SIZE;
384 bulk[n]->b_portal = OST_BULK_PORTAL;
385 ost_pack_niobuf(&ptr2, bulk[n]->b_buf, offset[n],
391 /* This is kinda silly--put the XIDs in the "third" buffer. */
392 for (n = 0; n < pages; n++) {
393 *(__u32 *)ptr2 = bulk[n]->b_xid;
394 ptr2 = (char *)ptr2 + sizeof(__u32);
396 rc = ptlrpc_wait_bulk(bulk[n]);
401 request->rq_replen = sizeof(struct ptlrep_hdr) + sizeof(struct ost_rep);
402 rc = ptlrpc_queue_wait(cl, request);
405 /* FIXME: if we've called ptlrpc_wait_bulk but rc != 0, we need to
406 * abort those bulk listeners. */
408 if (request->rq_rephdr)
409 OBD_FREE(request->rq_rephdr, request->rq_replen);
411 for (i = 0; i < num_oa; i++) {
412 for (j = 0; j < oa_bufs[i]; j++) {
415 kunmap(bulk[n]->b_buf);
416 OBD_FREE(bulk[n], sizeof(struct ptlrpc_bulk_desc));
421 OBD_FREE(bulk, pages * sizeof(struct ptlrpc_bulk_desc *));
422 ptlrpc_free_req(request);
426 int osc_brw_write(struct obd_conn *conn, obd_count num_oa, struct obdo **oa,
427 obd_count *oa_bufs, struct page **buf, obd_size *count,
428 obd_off *offset, obd_flag *flags)
430 struct ptlrpc_client *cl = osc_con2cl(conn);
431 struct ptlrpc_request *request;
432 struct obd_ioobj ioo;
434 int pages, rc, i, j, n, size1, size2 = 0;
437 size1 = num_oa * sizeof(ioo);
439 for (i = 0; i < num_oa; i++) {
440 size2 += oa_bufs[i] * sizeof(src);
444 request = ptlrpc_prep_req(cl, OST_BRW, size1, NULL, size2, NULL);
446 CERROR("cannot pack req!\n");
449 request->rq_req.ost->cmd = OBD_BRW_WRITE;
452 ptr1 = ost_req_buf1(request->rq_req.ost);
453 ptr2 = ost_req_buf2(request->rq_req.ost);
454 for (i = 0; i < num_oa; i++) {
455 ost_pack_ioo(&ptr1, oa[i], oa_bufs[i]);
456 for (j = 0; j < oa_bufs[i]; j++) {
457 ost_pack_niobuf(&ptr2, kmap(buf[n]), offset[n],
463 request->rq_replen = sizeof(struct ptlrep_hdr) +
464 sizeof(struct ost_rep) + pages * sizeof(struct niobuf);
465 rc = ptlrpc_queue_wait(cl, request);
471 ptr2 = ost_rep_buf2(request->rq_rep.ost);
472 if (request->rq_rep.ost->buflen2 != n * sizeof(struct niobuf)) {
473 CERROR("buffer length wrong (%d vs. %d)\n",
474 request->rq_rep.ost->buflen2, n * sizeof(struct niobuf));
479 for (i = 0; i < num_oa; i++) {
480 for (j = 0; j < oa_bufs[i]; j++) {
482 src.addr = (__u64)(unsigned long)buf[n];
484 ost_unpack_niobuf(&ptr2, &dst);
485 osc_sendpage(conn, request, dst, &src);
490 /* Reuse the request structure for the completion request. */
491 OBD_FREE(request->rq_rephdr, request->rq_replen);
492 request->rq_rephdr = NULL;
493 request->rq_repbuf = NULL;
494 request->rq_reqhdr->opc = OST_BRW_COMPLETE;
495 request->rq_replen = sizeof(struct ptlrep_hdr) + sizeof(struct ost_rep);
496 rc = ptlrpc_queue_wait(cl, request);
503 if (request->rq_rephdr)
504 OBD_FREE(request->rq_rephdr, request->rq_replen);
506 for (i = 0; i < num_oa; i++) {
507 for (j = 0; j < oa_bufs[i]; j++) {
513 ptlrpc_free_req(request);
517 int osc_brw(int rw, struct obd_conn *conn, obd_count num_oa,
518 struct obdo **oa, obd_count *oa_bufs, struct page **buf,
519 obd_size *count, obd_off *offset, obd_flag *flags)
521 if (rw == OBD_BRW_READ) {
522 return osc_brw_read(conn, num_oa, oa, oa_bufs, buf, count,
525 return osc_brw_write(conn, num_oa, oa, oa_bufs, buf, count,
530 /* mount the file system (secretly) */
531 static int osc_setup(struct obd_device *obddev, obd_count len,
535 struct osc_obd *osc = &obddev->u.osc;
536 struct obd_ioctl_data *data = (struct obd_ioctl_data *)buf;
538 int dev = data->ioc_dev;
541 rc = ptlrpc_connect_client(dev, "ost",
553 static int osc_cleanup(struct obd_device * obddev)
559 struct obd_ops osc_obd_ops = {
561 o_cleanup: osc_cleanup,
562 o_create: osc_create,
563 o_destroy: osc_destroy,
564 o_getattr: osc_getattr,
565 o_setattr: osc_setattr,
566 o_connect: osc_connect,
567 o_disconnect: osc_disconnect,
572 static int __init osc_init(void)
574 obd_register_type(&osc_obd_ops, LUSTRE_OSC_NAME);
578 static void __exit osc_exit(void)
580 obd_unregister_type(LUSTRE_OSC_NAME);
583 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
584 MODULE_DESCRIPTION("Lustre Object Storage Client (OSC) v1.0");
585 MODULE_LICENSE("GPL");
587 module_init(osc_init);
588 module_exit(osc_exit);