1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5 * Author: Peter J. Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Storage Target Handling functions
24 * Lustre Object Server Module (OST)
26 * This server is single threaded at present (but can easily be multi
27 * threaded). For testing and management it is treated as an
28 * obd_device, although it does not export a full OBD method table
29 * (the requests are coming in over the wire, so object target
30 * modules do not have a full method table.)
35 #include <linux/version.h>
36 #include <linux/module.h>
38 #include <linux/stat.h>
39 #include <linux/locks.h>
40 #include <linux/ext2_fs.h>
41 #include <linux/quotaops.h>
42 #include <asm/unistd.h>
44 #define DEBUG_SUBSYSTEM S_OST
46 #include <linux/obd_ost.h>
47 #include <linux/lustre_net.h>
49 static int ost_destroy(struct ost_obd *ost, struct ptlrpc_request *req)
56 conn.oc_id = req->rq_req.ost->connid;
57 conn.oc_dev = ost->ost_tgt;
59 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
60 &req->rq_replen, &req->rq_repbuf);
62 CERROR("cannot pack reply\n");
66 req->rq_rep.ost->result = obd_destroy(&conn, &req->rq_req.ost->oa);
71 static int ost_getattr(struct ost_obd *ost, struct ptlrpc_request *req)
78 conn.oc_id = req->rq_req.ost->connid;
79 conn.oc_dev = ost->ost_tgt;
81 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
82 &req->rq_replen, &req->rq_repbuf);
84 CERROR("cannot pack reply\n");
87 req->rq_rep.ost->oa.o_id = req->rq_req.ost->oa.o_id;
88 req->rq_rep.ost->oa.o_valid = req->rq_req.ost->oa.o_valid;
90 req->rq_rep.ost->result = obd_getattr(&conn, &req->rq_rep.ost->oa);
95 static int ost_open(struct ost_obd *ost, struct ptlrpc_request *req)
102 conn.oc_id = req->rq_req.ost->connid;
103 conn.oc_dev = ost->ost_tgt;
105 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
106 &req->rq_replen, &req->rq_repbuf);
108 CERROR("cannot pack reply\n");
111 req->rq_rep.ost->oa.o_id = req->rq_req.ost->oa.o_id;
112 req->rq_rep.ost->oa.o_valid = req->rq_req.ost->oa.o_valid;
114 req->rq_rep.ost->result = obd_open(&conn, &req->rq_rep.ost->oa);
119 static int ost_close(struct ost_obd *ost, struct ptlrpc_request *req)
121 struct obd_conn conn;
126 conn.oc_id = req->rq_req.ost->connid;
127 conn.oc_dev = ost->ost_tgt;
129 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
130 &req->rq_replen, &req->rq_repbuf);
132 CERROR("cannot pack reply\n");
135 req->rq_rep.ost->oa.o_id = req->rq_req.ost->oa.o_id;
136 req->rq_rep.ost->oa.o_valid = req->rq_req.ost->oa.o_valid;
138 req->rq_rep.ost->result = obd_close(&conn, &req->rq_rep.ost->oa);
144 static int ost_create(struct ost_obd *ost, struct ptlrpc_request *req)
146 struct obd_conn conn;
151 conn.oc_id = req->rq_req.ost->connid;
152 conn.oc_dev = ost->ost_tgt;
154 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
155 &req->rq_replen, &req->rq_repbuf);
157 CERROR("cannot pack reply\n");
161 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
162 sizeof(req->rq_req.ost->oa));
164 req->rq_rep.ost->result =obd_create(&conn, &req->rq_rep.ost->oa);
169 static int ost_punch(struct ost_obd *ost, struct ptlrpc_request *req)
171 struct obd_conn conn;
176 conn.oc_id = req->rq_req.ost->connid;
177 conn.oc_dev = ost->ost_tgt;
179 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
180 &req->rq_replen, &req->rq_repbuf);
182 CERROR("cannot pack reply\n");
186 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
187 sizeof(req->rq_req.ost->oa));
189 req->rq_rep.ost->result = obd_punch(&conn, &req->rq_rep.ost->oa,
190 req->rq_rep.ost->oa.o_size,
191 req->rq_rep.ost->oa.o_blocks);
197 static int ost_setattr(struct ost_obd *ost, struct ptlrpc_request *req)
199 struct obd_conn conn;
204 conn.oc_id = req->rq_req.ost->connid;
205 conn.oc_dev = ost->ost_tgt;
207 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
208 &req->rq_replen, &req->rq_repbuf);
210 CERROR("cannot pack reply\n");
214 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
215 sizeof(req->rq_req.ost->oa));
217 req->rq_rep.ost->result = obd_setattr(&conn, &req->rq_rep.ost->oa);
222 static int ost_connect(struct ost_obd *ost, struct ptlrpc_request *req)
224 struct obd_conn conn;
229 conn.oc_dev = ost->ost_tgt;
231 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
232 &req->rq_replen, &req->rq_repbuf);
234 CERROR("cannot pack reply\n");
238 req->rq_rep.ost->result = obd_connect(&conn);
240 CDEBUG(D_IOCTL, "rep buffer %p, id %d\n", req->rq_repbuf, conn.oc_id);
241 req->rq_rep.ost->connid = conn.oc_id;
245 static int ost_disconnect(struct ost_obd *ost, struct ptlrpc_request *req)
247 struct obd_conn conn;
252 conn.oc_dev = ost->ost_tgt;
253 conn.oc_id = req->rq_req.ost->connid;
255 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
256 &req->rq_replen, &req->rq_repbuf);
258 CERROR("cannot pack reply\n");
261 CDEBUG(D_IOCTL, "Disconnecting %d\n", conn.oc_id);
262 req->rq_rep.ost->result = obd_disconnect(&conn);
267 static int ost_get_info(struct ost_obd *ost, struct ptlrpc_request *req)
269 struct obd_conn conn;
277 conn.oc_id = req->rq_req.ost->connid;
278 conn.oc_dev = ost->ost_tgt;
280 ptr = ost_req_buf1(req->rq_req.ost);
281 req->rq_rep.ost->result = obd_get_info(&conn, req->rq_req.ost->buflen1,
284 rc = ost_pack_rep(val, vallen, NULL, 0, &req->rq_rephdr,
285 &req->rq_rep, &req->rq_replen, &req->rq_repbuf);
287 CERROR("cannot pack reply\n");
292 static int ost_brw_read(struct ost_obd *obddev, struct ptlrpc_request *req)
294 struct ptlrpc_bulk_desc **bulk_vec = NULL;
295 struct ptlrpc_bulk_desc *bulk = NULL;
296 struct obd_conn conn;
299 int objcount, niocount;
300 char *tmp1, *tmp2, *end2;
302 struct niobuf *nb, *dst, *res = NULL;
303 struct obd_ioobj *ioo;
304 struct ost_req *r = req->rq_req.ost;
308 tmp1 = ost_req_buf1(r);
309 tmp2 = ost_req_buf2(r);
310 end2 = tmp2 + req->rq_req.ost->buflen2;
311 objcount = r->buflen1 / sizeof(*ioo);
312 niocount = r->buflen2 / sizeof(*nb);
315 conn.oc_id = req->rq_req.ost->connid;
316 conn.oc_dev = req->rq_obd->u.ost.ost_tgt;
318 for (i = 0; i < objcount; i++) {
319 ost_unpack_ioo((void *)&tmp1, &ioo);
320 if (tmp2 + ioo->ioo_bufcnt > end2) {
325 for (j = 0; j < ioo->ioo_bufcnt; j++)
326 ost_unpack_niobuf((void *)&tmp2, &nb);
329 rc = ost_pack_rep(NULL, 0, NULL, 0,
330 &req->rq_rephdr, &req->rq_rep,
331 &req->rq_replen, &req->rq_repbuf);
333 CERROR("cannot pack reply\n");
336 OBD_ALLOC(res, sizeof(*res) * niocount);
340 /* The unpackers move tmp1 and tmp2, so reset them before using */
341 tmp1 = ost_req_buf1(r);
342 tmp2 = ost_req_buf2(r);
343 req->rq_rep.ost->result = obd_preprw
344 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
345 niocount, (struct niobuf *)tmp2, res);
347 if (req->rq_rep.ost->result)
350 for (i = 0; i < niocount; i++) {
351 bulk = ptlrpc_prep_bulk(&req->rq_peer);
353 CERROR("cannot alloc bulk desc\n");
358 dst = &((struct niobuf *)tmp2)[i];
359 bulk->b_xid = dst->xid;
360 bulk->b_buf = (void *)(unsigned long)res[i].addr;
361 bulk->b_buflen = PAGE_SIZE;
362 rc = ptlrpc_send_bulk(bulk, OST_BULK_PORTAL);
365 wait_event_interruptible(bulk->b_waitq,
366 ptlrpc_check_bulk_sent(bulk));
368 if (bulk->b_flags == PTL_RPC_INTR)
371 OBD_FREE(bulk, sizeof(*bulk));
377 dst = &((struct niobuf *)tmp2)[i];
378 memcpy((void *)(unsigned long)dst->addr,
379 (void *)(unsigned long)src->addr, PAGE_SIZE);
383 /* The unpackers move tmp1 and tmp2, so reset them before using */
384 tmp1 = ost_req_buf1(r);
385 tmp2 = ost_req_buf2(r);
386 req->rq_rep.ost->result = obd_commitrw
387 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
393 OBD_FREE(res, sizeof(*res) * niocount);
395 OBD_FREE(bulk, sizeof(*bulk));
396 if (bulk_vec != NULL) {
397 for (i = 0; i < niocount; i++) {
398 if (bulk_vec[i] != NULL)
399 OBD_FREE(bulk_vec[i], sizeof(*bulk));
401 OBD_FREE(bulk_vec, niocount * sizeof(*bulk_vec));
407 static int ost_commit_page(struct obd_conn *conn, struct page *page)
409 struct obd_ioobj obj;
414 memset(&buf, 0, sizeof(buf));
415 memset(&obj, 0, sizeof(obj));
420 rc = obd_commitrw(OBD_BRW_WRITE, conn, 1, &obj, 1, &buf);
424 static int ost_brw_write_cb(struct ptlrpc_bulk_desc *bulk, void *data)
430 rc = ost_commit_page(&bulk->b_conn, bulk->b_page);
432 CERROR("ost_commit_page failed: %d\n", rc);
437 int ost_brw_write(struct ost_obd *obddev, struct ptlrpc_request *req)
439 struct obd_conn conn;
442 int objcount, niocount;
443 char *tmp1, *tmp2, *end2;
446 struct niobuf *nb, *dst;
447 struct obd_ioobj *ioo;
448 struct ost_req *r = req->rq_req.ost;
452 tmp1 = ost_req_buf1(r);
453 tmp2 = ost_req_buf2(r);
454 end2 = tmp2 + req->rq_req.ost->buflen2;
455 objcount = r->buflen1 / sizeof(*ioo);
456 niocount = r->buflen2 / sizeof(*nb);
459 conn.oc_id = req->rq_req.ost->connid;
460 conn.oc_dev = req->rq_obd->u.ost.ost_tgt;
462 for (i = 0; i < objcount; i++) {
463 ost_unpack_ioo((void *)&tmp1, &ioo);
464 if (tmp2 + ioo->ioo_bufcnt > end2) {
468 for (j = 0; j < ioo->ioo_bufcnt; j++)
469 ost_unpack_niobuf((void *)&tmp2, &nb);
472 rc = ost_pack_rep(NULL, 0, NULL, niocount * sizeof(*nb),
473 &req->rq_rephdr, &req->rq_rep,
474 &req->rq_replen, &req->rq_repbuf);
476 CERROR("cannot pack reply\n");
479 res = ost_rep_buf2(req->rq_rep.ost);
481 /* The unpackers move tmp1 and tmp2, so reset them before using */
482 tmp1 = ost_req_buf1(r);
483 tmp2 = ost_req_buf2(r);
484 req->rq_rep.ost->result = obd_preprw
485 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
486 niocount, (struct niobuf *)tmp2, (struct niobuf *)res);
488 if (req->rq_rep.ost->result)
491 for (i = 0; i < niocount; i++) {
492 struct ptlrpc_bulk_desc *bulk;
493 struct ptlrpc_service *srv = req->rq_obd->u.ost.ost_service;
495 bulk = ptlrpc_prep_bulk(&req->rq_peer);
497 CERROR("cannot alloc bulk desc\n");
502 spin_lock(&srv->srv_lock);
503 bulk->b_xid = srv->srv_xid++;
504 spin_unlock(&srv->srv_lock);
506 dst = &((struct niobuf *)res)[i];
507 dst->xid = HTON__u32(bulk->b_xid);
509 bulk->b_buf = (void *)(unsigned long)dst->addr;
510 bulk->b_cb = ost_brw_write_cb;
511 bulk->b_page = dst->page;
512 memcpy(&(bulk->b_conn), &conn, sizeof(conn));
513 bulk->b_buflen = PAGE_SIZE;
514 bulk->b_portal = OSC_BULK_PORTAL;
515 rc = ptlrpc_register_bulk(bulk);
521 src = &((struct niobuf *)tmp2)[i];
522 memcpy((void *)(unsigned long)dst->addr,
523 (void *)(unsigned long)src->addr, src->len);
530 /* FIXME: should we return 'rc' here? */
534 int ost_brw(struct ost_obd *obddev, struct ptlrpc_request *req)
536 struct ost_req *r = req->rq_req.ost;
539 if (cmd == OBD_BRW_READ)
540 return ost_brw_read(obddev, req);
542 return ost_brw_write(obddev, req);
545 static int ost_handle(struct obd_device *obddev, struct ptlrpc_service *svc,
546 struct ptlrpc_request *req)
549 struct ost_obd *ost = &obddev->u.ost;
550 struct ptlreq_hdr *hdr;
554 hdr = (struct ptlreq_hdr *)req->rq_reqbuf;
555 if (NTOH__u32(hdr->type) != PTL_RPC_REQUEST) {
556 CERROR("lustre_ost: wrong packet type sent %d\n",
557 NTOH__u32(hdr->type));
563 rc = ost_unpack_req(req->rq_reqbuf, req->rq_reqlen,
564 &req->rq_reqhdr, &req->rq_req);
566 CERROR("lustre_ost: Invalid request\n");
570 switch (req->rq_reqhdr->opc) {
573 CDEBUG(D_INODE, "connect\n");
574 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
575 rc = ost_connect(ost, req);
578 CDEBUG(D_INODE, "disconnect\n");
579 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
580 rc = ost_disconnect(ost, req);
583 CDEBUG(D_INODE, "get_info\n");
584 OBD_FAIL_RETURN(OBD_FAIL_OST_GET_INFO_NET, 0);
585 rc = ost_get_info(ost, req);
588 CDEBUG(D_INODE, "create\n");
589 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
590 rc = ost_create(ost, req);
593 CDEBUG(D_INODE, "destroy\n");
594 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
595 rc = ost_destroy(ost, req);
598 CDEBUG(D_INODE, "getattr\n");
599 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
600 rc = ost_getattr(ost, req);
603 CDEBUG(D_INODE, "setattr\n");
604 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
605 rc = ost_setattr(ost, req);
608 CDEBUG(D_INODE, "setattr\n");
609 OBD_FAIL_RETURN(OBD_FAIL_OST_OPEN_NET, 0);
610 rc = ost_open(ost, req);
613 CDEBUG(D_INODE, "setattr\n");
614 OBD_FAIL_RETURN(OBD_FAIL_OST_CLOSE_NET, 0);
615 rc = ost_close(ost, req);
618 CDEBUG(D_INODE, "brw\n");
619 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
620 rc = ost_brw(ost, req);
623 CDEBUG(D_INODE, "punch\n");
624 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
625 rc = ost_punch(ost, req);
628 req->rq_status = -ENOTSUPP;
629 rc = ptlrpc_error(obddev, svc, req);
637 CERROR("ost: processing error %d\n", rc);
638 ptlrpc_error(obddev, svc, req);
640 CDEBUG(D_INODE, "sending reply\n");
641 ptlrpc_reply(obddev, svc, req);
648 /* mount the file system (secretly) */
649 static int ost_setup(struct obd_device *obddev, obd_count len,
653 struct obd_ioctl_data* data = buf;
654 struct ost_obd *ost = &obddev->u.ost;
655 struct obd_device *tgt;
659 if (data->ioc_dev < 0 || data->ioc_dev > MAX_OBD_DEVICES)
662 tgt = &obd_dev[data->ioc_dev];
664 if ( ! (tgt->obd_flags & OBD_ATTACHED) ||
665 ! (tgt->obd_flags & OBD_SET_UP) ){
666 CERROR("device not attached or not set up (%d)\n",
671 ost->ost_conn.oc_dev = tgt;
672 err = obd_connect(&ost->ost_conn);
674 CERROR("fail to connect to device %d\n", data->ioc_dev);
678 ost->ost_service = ptlrpc_init_svc(128 * 1024,
679 OST_REQUEST_PORTAL, OSC_REPLY_PORTAL,
681 if (!ost->ost_service) {
682 CERROR("failed to start service\n");
683 obd_disconnect(&ost->ost_conn);
687 err = ptlrpc_start_thread(obddev, ost->ost_service, "lustre_ost");
689 obd_disconnect(&ost->ost_conn);
697 static int ost_cleanup(struct obd_device * obddev)
699 struct ost_obd *ost = &obddev->u.ost;
704 if ( !list_empty(&obddev->obd_gen_clients) ) {
705 CERROR("still has clients!\n");
709 ptlrpc_stop_thread(ost->ost_service);
710 rpc_unregister_service(ost->ost_service);
712 if (!list_empty(&ost->ost_service->srv_reqs)) {
713 // XXX reply with errors and clean up
714 CERROR("Request list not empty!\n");
716 OBD_FREE(ost->ost_service, sizeof(*ost->ost_service));
718 err = obd_disconnect(&ost->ost_conn);
720 CERROR("lustre ost: fail to disconnect device\n");
728 /* use obd ops to offer management infrastructure */
729 static struct obd_ops ost_obd_ops = {
731 o_cleanup: ost_cleanup,
734 static int __init ost_init(void)
736 obd_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
740 static void __exit ost_exit(void)
742 obd_unregister_type(LUSTRE_OST_NAME);
745 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
746 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
747 MODULE_LICENSE("GPL");
749 module_init(ost_init);
750 module_exit(ost_exit);