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);
72 static int ost_getattr(struct ost_obd *ost, struct ptlrpc_request *req)
79 conn.oc_id = req->rq_req.ost->connid;
80 conn.oc_dev = ost->ost_tgt;
82 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
83 &req->rq_replen, &req->rq_repbuf);
85 CERROR("cannot pack reply\n");
88 req->rq_rep.ost->oa.o_id = req->rq_req.ost->oa.o_id;
89 req->rq_rep.ost->oa.o_valid = req->rq_req.ost->oa.o_valid;
91 req->rq_rep.ost->result = obd_getattr(&conn, &req->rq_rep.ost->oa);
97 static int ost_open(struct ost_obd *ost, struct ptlrpc_request *req)
104 conn.oc_id = req->rq_req.ost->connid;
105 conn.oc_dev = ost->ost_tgt;
107 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
108 &req->rq_replen, &req->rq_repbuf);
110 CERROR("cannot pack reply\n");
113 req->rq_rep.ost->oa.o_id = req->rq_req.ost->oa.o_id;
114 req->rq_rep.ost->oa.o_valid = req->rq_req.ost->oa.o_valid;
116 req->rq_rep.ost->result = obd_open(&conn, &req->rq_rep.ost->oa);
122 static int ost_close(struct ost_obd *ost, struct ptlrpc_request *req)
124 struct obd_conn conn;
129 conn.oc_id = req->rq_req.ost->connid;
130 conn.oc_dev = ost->ost_tgt;
132 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
133 &req->rq_replen, &req->rq_repbuf);
135 CERROR("cannot pack reply\n");
138 req->rq_rep.ost->oa.o_id = req->rq_req.ost->oa.o_id;
139 req->rq_rep.ost->oa.o_valid = req->rq_req.ost->oa.o_valid;
141 req->rq_rep.ost->result = obd_close(&conn, &req->rq_rep.ost->oa);
148 static int ost_create(struct ost_obd *ost, struct ptlrpc_request *req)
150 struct obd_conn conn;
155 conn.oc_id = req->rq_req.ost->connid;
156 conn.oc_dev = ost->ost_tgt;
158 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
159 &req->rq_replen, &req->rq_repbuf);
161 CERROR("cannot pack reply\n");
165 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
166 sizeof(req->rq_req.ost->oa));
168 req->rq_rep.ost->result =obd_create(&conn, &req->rq_rep.ost->oa);
174 static int ost_punch(struct ost_obd *ost, struct ptlrpc_request *req)
176 struct obd_conn conn;
181 conn.oc_id = req->rq_req.ost->connid;
182 conn.oc_dev = ost->ost_tgt;
184 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
185 &req->rq_replen, &req->rq_repbuf);
187 CERROR("cannot pack reply\n");
191 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
192 sizeof(req->rq_req.ost->oa));
194 req->rq_rep.ost->result = obd_punch(&conn, &req->rq_rep.ost->oa,
195 req->rq_rep.ost->oa.o_size,
196 req->rq_rep.ost->oa.o_blocks);
203 static int ost_setattr(struct ost_obd *ost, struct ptlrpc_request *req)
205 struct obd_conn conn;
210 conn.oc_id = req->rq_req.ost->connid;
211 conn.oc_dev = ost->ost_tgt;
213 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
214 &req->rq_replen, &req->rq_repbuf);
216 CERROR("cannot pack reply\n");
220 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
221 sizeof(req->rq_req.ost->oa));
223 req->rq_rep.ost->result = obd_setattr(&conn, &req->rq_rep.ost->oa);
229 static int ost_connect(struct ost_obd *ost, struct ptlrpc_request *req)
231 struct obd_conn conn;
236 conn.oc_dev = ost->ost_tgt;
238 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
239 &req->rq_replen, &req->rq_repbuf);
241 CERROR("cannot pack reply\n");
245 req->rq_rep.ost->result = obd_connect(&conn);
247 CDEBUG(D_IOCTL, "rep buffer %p, id %d\n", req->rq_repbuf, conn.oc_id);
248 req->rq_rep.ost->connid = conn.oc_id;
253 static int ost_disconnect(struct ost_obd *ost, struct ptlrpc_request *req)
255 struct obd_conn conn;
260 conn.oc_dev = ost->ost_tgt;
261 conn.oc_id = req->rq_req.ost->connid;
263 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
264 &req->rq_replen, &req->rq_repbuf);
266 CERROR("cannot pack reply\n");
269 CDEBUG(D_IOCTL, "Disconnecting %d\n", conn.oc_id);
270 req->rq_rep.ost->result = obd_disconnect(&conn);
276 static int ost_get_info(struct ost_obd *ost, struct ptlrpc_request *req)
278 struct obd_conn conn;
286 conn.oc_id = req->rq_req.ost->connid;
287 conn.oc_dev = ost->ost_tgt;
289 ptr = ost_req_buf1(req->rq_req.ost);
290 req->rq_rep.ost->result = obd_get_info(&conn,
291 req->rq_req.ost->buflen1, ptr,
294 rc = ost_pack_rep(val, vallen, NULL, 0, &req->rq_rephdr,
295 &req->rq_rep, &req->rq_replen, &req->rq_repbuf);
297 CERROR("cannot pack reply\n");
305 static int ost_brw_read(struct ost_obd *obddev, struct ptlrpc_request *req)
307 struct ptlrpc_bulk_desc **bulk_vec = NULL;
308 struct ptlrpc_bulk_desc *bulk = NULL;
309 struct obd_conn conn;
312 int objcount, niocount;
313 char *tmp1, *tmp2, *end2;
316 struct niobuf *nb, *src, *dst;
317 struct obd_ioobj *ioo;
318 struct ost_req *r = req->rq_req.ost;
322 tmp1 = ost_req_buf1(r);
323 tmp2 = ost_req_buf2(r);
324 end2 = tmp2 + req->rq_req.ost->buflen2;
325 objcount = r->buflen1 / sizeof(*ioo);
326 niocount = r->buflen2 / sizeof(*nb);
329 conn.oc_id = req->rq_req.ost->connid;
330 conn.oc_dev = req->rq_obd->u.ost.ost_tgt;
332 for (i = 0; i < objcount; i++) {
333 ost_unpack_ioo((void *)&tmp1, &ioo);
334 if (tmp2 + ioo->ioo_bufcnt > end2) {
339 for (j = 0; j < ioo->ioo_bufcnt; j++) {
340 ost_unpack_niobuf((void *)&tmp2, &nb);
344 rc = ost_pack_rep(NULL, 0, NULL, 0,
345 &req->rq_rephdr, &req->rq_rep,
346 &req->rq_replen, &req->rq_repbuf);
348 CERROR("cannot pack reply\n");
351 OBD_ALLOC(res, sizeof(struct niobuf) * niocount);
357 /* The unpackers move tmp1 and tmp2, so reset them before using */
358 tmp1 = ost_req_buf1(r);
359 tmp2 = ost_req_buf2(r);
360 req->rq_rep.ost->result = obd_preprw
361 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
362 niocount, (struct niobuf *)tmp2, (struct niobuf *)res);
364 if (req->rq_rep.ost->result) {
369 for (i = 0; i < niocount; i++) {
370 bulk = ptlrpc_prep_bulk(&req->rq_peer);
372 CERROR("cannot alloc bulk desc\n");
377 src = &((struct niobuf *)res)[i];
378 dst = &((struct niobuf *)tmp2)[i];
379 bulk->b_xid = dst->xid;
380 bulk->b_buf = (void *)(unsigned long)src->addr;
381 bulk->b_buflen = PAGE_SIZE;
382 rc = ptlrpc_send_bulk(bulk, OST_BULK_PORTAL);
387 wait_event_interruptible(bulk->b_waitq,
388 ptlrpc_check_bulk_sent(bulk));
390 if (bulk->b_flags == PTL_RPC_INTR) {
395 OBD_FREE(bulk, sizeof(*bulk));
401 dst = &((struct niobuf *)tmp2)[i];
402 memcpy((void *)(unsigned long)dst->addr,
403 (void *)(unsigned long)src->addr, PAGE_SIZE);
407 /* The unpackers move tmp1 and tmp2, so reset them before using */
408 tmp1 = ost_req_buf1(r);
409 tmp2 = ost_req_buf2(r);
410 req->rq_rep.ost->result = obd_commitrw
411 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
412 niocount, (struct niobuf *)res);
416 OBD_FREE(res, sizeof(struct niobuf) * niocount);
418 OBD_FREE(bulk, sizeof(*bulk));
419 if (bulk_vec != NULL) {
420 for (i = 0; i < niocount; i++) {
421 if (bulk_vec[i] != NULL)
422 OBD_FREE(bulk_vec[i], sizeof(*bulk));
425 niocount * sizeof(struct ptlrpc_bulk_desc *));
432 static int ost_commit_page(struct obd_conn *conn, struct page *page)
434 struct obd_ioobj obj;
439 memset(&buf, 0, sizeof(buf));
440 memset(&obj, 0, sizeof(obj));
445 rc = obd_commitrw(OBD_BRW_WRITE, conn, 1, &obj, 1, &buf);
450 static int ost_brw_write_cb(struct ptlrpc_bulk_desc *bulk, void *data)
456 rc = ost_commit_page(&bulk->b_conn, bulk->b_page);
458 CERROR("ost_commit_page failed: %d\n", rc);
464 int ost_brw_write(struct ost_obd *obddev, struct ptlrpc_request *req)
466 struct obd_conn conn;
469 int objcount, niocount;
470 char *tmp1, *tmp2, *end2;
473 struct niobuf *nb, *dst;
474 struct obd_ioobj *ioo;
475 struct ost_req *r = req->rq_req.ost;
479 tmp1 = ost_req_buf1(r);
480 tmp2 = ost_req_buf2(r);
481 end2 = tmp2 + req->rq_req.ost->buflen2;
482 objcount = r->buflen1 / sizeof(*ioo);
483 niocount = r->buflen2 / sizeof(*nb);
486 conn.oc_id = req->rq_req.ost->connid;
487 conn.oc_dev = req->rq_obd->u.ost.ost_tgt;
489 for (i = 0; i < objcount; i++) {
490 ost_unpack_ioo((void *)&tmp1, &ioo);
491 if (tmp2 + ioo->ioo_bufcnt > end2) {
495 for (j = 0; j < ioo->ioo_bufcnt; j++) {
496 ost_unpack_niobuf((void *)&tmp2, &nb);
500 rc = ost_pack_rep(NULL, 0, NULL, niocount * sizeof(*nb),
501 &req->rq_rephdr, &req->rq_rep,
502 &req->rq_replen, &req->rq_repbuf);
504 CERROR("cannot pack reply\n");
507 res = ost_rep_buf2(req->rq_rep.ost);
509 /* The unpackers move tmp1 and tmp2, so reset them before using */
510 tmp1 = ost_req_buf1(r);
511 tmp2 = ost_req_buf2(r);
512 req->rq_rep.ost->result = obd_preprw
513 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
514 niocount, (struct niobuf *)tmp2, (struct niobuf *)res);
516 if (req->rq_rep.ost->result) {
521 for (i = 0; i < niocount; i++) {
522 struct ptlrpc_bulk_desc *bulk;
523 struct ptlrpc_service *srv = req->rq_obd->u.ost.ost_service;
525 bulk = ptlrpc_prep_bulk(&req->rq_peer);
527 CERROR("cannot alloc bulk desc\n");
532 spin_lock(&srv->srv_lock);
533 bulk->b_xid = srv->srv_xid++;
534 spin_unlock(&srv->srv_lock);
536 dst = &((struct niobuf *)res)[i];
537 dst->xid = HTON__u32(bulk->b_xid);
539 bulk->b_buf = (void *)(unsigned long)dst->addr;
540 bulk->b_cb = ost_brw_write_cb;
541 bulk->b_page = dst->page;
542 memcpy(&(bulk->b_conn), &conn, sizeof(conn));
543 bulk->b_buflen = PAGE_SIZE;
544 bulk->b_portal = OSC_BULK_PORTAL;
545 rc = ptlrpc_register_bulk(bulk);
551 src = &((struct niobuf *)tmp2)[i];
552 memcpy((void *)(unsigned long)dst->addr,
553 (void *)(unsigned long)src->addr, src->len);
563 int ost_brw(struct ost_obd *obddev, struct ptlrpc_request *req)
565 struct ost_req *r = req->rq_req.ost;
568 if (cmd == OBD_BRW_READ)
569 return ost_brw_read(obddev, req);
571 return ost_brw_write(obddev, req);
574 static int ost_handle(struct obd_device *obddev,
575 struct ptlrpc_service *svc,
576 struct ptlrpc_request *req)
579 struct ost_obd *ost = &obddev->u.ost;
580 struct ptlreq_hdr *hdr;
584 hdr = (struct ptlreq_hdr *)req->rq_reqbuf;
585 if (NTOH__u32(hdr->type) != OST_TYPE_REQ) {
586 CERROR("lustre_ost: wrong packet type sent %d\n",
587 NTOH__u32(hdr->type));
593 rc = ost_unpack_req(req->rq_reqbuf, req->rq_reqlen,
594 &req->rq_reqhdr, &req->rq_req);
596 CERROR("lustre_ost: Invalid request\n");
601 switch (req->rq_reqhdr->opc) {
604 CDEBUG(D_INODE, "connect\n");
605 rc = ost_connect(ost, req);
608 CDEBUG(D_INODE, "disconnect\n");
609 rc = ost_disconnect(ost, req);
612 CDEBUG(D_INODE, "get_info\n");
613 rc = ost_get_info(ost, req);
616 CDEBUG(D_INODE, "create\n");
617 rc = ost_create(ost, req);
620 CDEBUG(D_INODE, "destroy\n");
621 rc = ost_destroy(ost, req);
624 CDEBUG(D_INODE, "getattr\n");
625 rc = ost_getattr(ost, req);
628 CDEBUG(D_INODE, "setattr\n");
629 rc = ost_setattr(ost, req);
632 CDEBUG(D_INODE, "setattr\n");
633 rc = ost_open(ost, req);
636 CDEBUG(D_INODE, "setattr\n");
637 rc = ost_close(ost, req);
640 CDEBUG(D_INODE, "brw\n");
641 rc = ost_brw(ost, req);
644 CDEBUG(D_INODE, "punch\n");
645 rc = ost_punch(ost, req);
648 req->rq_status = -ENOTSUPP;
649 return ptlrpc_error(obddev, svc, req);
655 CERROR("ost: processing error %d\n", rc);
656 ptlrpc_error(obddev, svc, req);
658 CDEBUG(D_INODE, "sending reply\n");
659 ptlrpc_reply(obddev, svc, req);
666 /* mount the file system (secretly) */
667 static int ost_setup(struct obd_device *obddev, obd_count len,
671 struct obd_ioctl_data* data = buf;
672 struct ost_obd *ost = &obddev->u.ost;
673 struct obd_device *tgt;
677 if (data->ioc_dev < 0 || data->ioc_dev > MAX_OBD_DEVICES) {
682 tgt = &obd_dev[data->ioc_dev];
684 if ( ! (tgt->obd_flags & OBD_ATTACHED) ||
685 ! (tgt->obd_flags & OBD_SET_UP) ){
686 CERROR("device not attached or not set up (%d)\n",
692 ost->ost_conn.oc_dev = tgt;
693 err = obd_connect(&ost->ost_conn);
695 CERROR("fail to connect to device %d\n", data->ioc_dev);
699 ost->ost_service = ptlrpc_init_svc( 64 * 1024,
706 if (!ost->ost_service) {
707 obd_disconnect(&ost->ost_conn);
711 rpc_register_service(ost->ost_service, "self");
713 err = ptlrpc_start_thread(obddev, ost->ost_service, "lustre_ost");
715 obd_disconnect(&ost->ost_conn);
724 static int ost_cleanup(struct obd_device * obddev)
726 struct ost_obd *ost = &obddev->u.ost;
731 if ( !list_empty(&obddev->obd_gen_clients) ) {
732 CERROR("still has clients!\n");
737 ptlrpc_stop_thread(ost->ost_service);
738 rpc_unregister_service(ost->ost_service);
740 if (!list_empty(&ost->ost_service->srv_reqs)) {
741 // XXX reply with errors and clean up
742 CERROR("Request list not empty!\n");
744 OBD_FREE(ost->ost_service, sizeof(*ost->ost_service));
746 err = obd_disconnect(&ost->ost_conn);
748 CERROR("lustre ost: fail to disconnect device\n");
757 /* use obd ops to offer management infrastructure */
758 static struct obd_ops ost_obd_ops = {
760 o_cleanup: ost_cleanup,
763 static int __init ost_init(void)
765 obd_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
769 static void __exit ost_exit(void)
771 obd_unregister_type(LUSTRE_OST_NAME);
774 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
775 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
776 MODULE_LICENSE("GPL");
778 module_init(ost_init);
779 module_exit(ost_exit);