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_support.h>
47 #include <linux/obd.h>
48 #include <linux/obd_class.h>
49 #include <linux/lustre_lib.h>
50 #include <linux/lustre_idl.h>
51 #include <linux/lustre_mds.h>
52 #include <linux/obd_class.h>
56 static int ost_destroy(struct ost_obd *ost, struct ptlrpc_request *req)
63 conn.oc_id = req->rq_req.ost->connid;
64 conn.oc_dev = ost->ost_tgt;
66 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
67 &req->rq_replen, &req->rq_repbuf);
69 CERROR("cannot pack reply\n");
73 req->rq_rep.ost->result = obd_destroy(&conn, &req->rq_req.ost->oa);
79 static int ost_getattr(struct ost_obd *ost, struct ptlrpc_request *req)
86 conn.oc_id = req->rq_req.ost->connid;
87 conn.oc_dev = ost->ost_tgt;
89 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
90 &req->rq_replen, &req->rq_repbuf);
92 CERROR("cannot pack reply\n");
95 req->rq_rep.ost->oa.o_id = req->rq_req.ost->oa.o_id;
96 req->rq_rep.ost->oa.o_valid = req->rq_req.ost->oa.o_valid;
98 req->rq_rep.ost->result = obd_getattr(&conn, &req->rq_rep.ost->oa);
104 static int ost_create(struct ost_obd *ost, struct ptlrpc_request *req)
106 struct obd_conn conn;
111 conn.oc_id = req->rq_req.ost->connid;
112 conn.oc_dev = ost->ost_tgt;
114 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
115 &req->rq_replen, &req->rq_repbuf);
117 CERROR("cannot pack reply\n");
121 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
122 sizeof(req->rq_req.ost->oa));
124 req->rq_rep.ost->result =obd_create(&conn, &req->rq_rep.ost->oa);
130 static int ost_punch(struct ost_obd *ost, struct ptlrpc_request *req)
132 struct obd_conn conn;
137 conn.oc_id = req->rq_req.ost->connid;
138 conn.oc_dev = ost->ost_tgt;
140 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
141 &req->rq_replen, &req->rq_repbuf);
143 CERROR("cannot pack reply\n");
147 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
148 sizeof(req->rq_req.ost->oa));
150 req->rq_rep.ost->result = obd_punch(&conn, &req->rq_rep.ost->oa,
151 req->rq_rep.ost->oa.o_size,
152 req->rq_rep.ost->oa.o_blocks);
159 static int ost_setattr(struct ost_obd *ost, struct ptlrpc_request *req)
161 struct obd_conn conn;
166 conn.oc_id = req->rq_req.ost->connid;
167 conn.oc_dev = ost->ost_tgt;
169 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
170 &req->rq_replen, &req->rq_repbuf);
172 CERROR("cannot pack reply\n");
176 memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
177 sizeof(req->rq_req.ost->oa));
179 req->rq_rep.ost->result = obd_setattr(&conn, &req->rq_rep.ost->oa);
185 static int ost_connect(struct ost_obd *ost, struct ptlrpc_request *req)
187 struct obd_conn conn;
192 conn.oc_dev = ost->ost_tgt;
194 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
195 &req->rq_replen, &req->rq_repbuf);
197 CERROR("cannot pack reply\n");
201 req->rq_rep.ost->result = obd_connect(&conn);
203 CDEBUG(D_IOCTL, "rep buffer %p, id %d\n", req->rq_repbuf, conn.oc_id);
204 req->rq_rep.ost->connid = conn.oc_id;
209 static int ost_disconnect(struct ost_obd *ost, struct ptlrpc_request *req)
211 struct obd_conn conn;
216 conn.oc_dev = ost->ost_tgt;
217 conn.oc_id = req->rq_req.ost->connid;
219 rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
220 &req->rq_replen, &req->rq_repbuf);
222 CERROR("cannot pack reply\n");
225 CDEBUG(D_IOCTL, "Disconnecting %d\n", conn.oc_id);
226 req->rq_rep.ost->result = obd_disconnect(&conn);
232 static int ost_get_info(struct ost_obd *ost, struct ptlrpc_request *req)
234 struct obd_conn conn;
242 conn.oc_id = req->rq_req.ost->connid;
243 conn.oc_dev = ost->ost_tgt;
245 ptr = ost_req_buf1(req->rq_req.ost);
246 req->rq_rep.ost->result = obd_get_info(&conn,
247 req->rq_req.ost->buflen1, ptr,
250 rc = ost_pack_rep(val, vallen, NULL, 0, &req->rq_rephdr,
251 &req->rq_rep, &req->rq_replen, &req->rq_repbuf);
253 CERROR("cannot pack reply\n");
261 static int ost_brw_read(struct ost_obd *obddev, struct ptlrpc_request *req)
263 struct ptlrpc_bulk_desc **bulk_vec = NULL;
264 struct ptlrpc_bulk_desc *bulk = NULL;
265 struct obd_conn conn;
268 int objcount, niocount;
269 char *tmp1, *tmp2, *end2;
272 struct niobuf *nb, *src;
273 struct obd_ioobj *ioo;
274 struct ost_req *r = req->rq_req.ost;
278 tmp1 = ost_req_buf1(r);
279 tmp2 = ost_req_buf2(r);
280 end2 = tmp2 + req->rq_req.ost->buflen2;
281 objcount = r->buflen1 / sizeof(*ioo);
282 niocount = r->buflen2 / sizeof(*nb);
285 conn.oc_id = req->rq_req.ost->connid;
286 conn.oc_dev = req->rq_obd->u.ost.ost_tgt;
288 for (i = 0; i < objcount; i++) {
289 ost_unpack_ioo((void *)&tmp1, &ioo);
290 if (tmp2 + ioo->ioo_bufcnt > end2) {
294 for (j = 0; j < ioo->ioo_bufcnt; j++) {
295 ost_unpack_niobuf((void *)&tmp2, &nb);
299 rc = ost_pack_rep(NULL, 0, NULL, 0,
300 &req->rq_rephdr, &req->rq_rep,
301 &req->rq_replen, &req->rq_repbuf);
303 CERROR("cannot pack reply\n");
306 OBD_ALLOC(res, sizeof(struct niobuf) * niocount);
312 /* The unpackers move tmp1 and tmp2, so reset them before using */
313 tmp1 = ost_req_buf1(r);
314 tmp2 = ost_req_buf2(r);
315 req->rq_rep.ost->result = obd_preprw
316 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
317 niocount, (struct niobuf *)tmp2, (struct niobuf *)res);
319 if (req->rq_rep.ost->result) {
324 for (i = 0; i < niocount; i++) {
325 bulk = ptlrpc_prep_bulk(&req->rq_peer);
327 CERROR("cannot alloc bulk desc\n");
332 src = &((struct niobuf *)tmp2)[i];
334 bulk->b_xid = src->xid;
335 bulk->b_buf = (void *)(unsigned long)src->addr;
336 bulk->b_buflen = PAGE_SIZE;
337 rc = ptlrpc_send_bulk(bulk, OST_BULK_PORTAL);
342 wait_event_interruptible(bulk->b_waitq,
343 ptlrpc_check_bulk_sent(bulk));
345 if (bulk->b_flags == PTL_RPC_INTR) {
350 OBD_FREE(bulk, sizeof(*bulk));
356 dst = &((struct niobuf *)tmp2)[i];
357 memcpy((void *)(unsigned long)dst->addr,
358 (void *)(unsigned long)src->addr, PAGE_SIZE);
362 /* The unpackers move tmp1 and tmp2, so reset them before using */
363 tmp1 = ost_req_buf1(r);
364 tmp2 = ost_req_buf2(r);
365 req->rq_rep.ost->result = obd_commitrw
366 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
367 niocount, (struct niobuf *)tmp2);
371 OBD_FREE(res, sizeof(struct niobuf) * niocount);
373 OBD_FREE(bulk, sizeof(*bulk));
374 if (bulk_vec != NULL) {
375 for (i = 0; i < niocount; i++) {
376 if (bulk_vec[i] != NULL)
377 OBD_FREE(bulk_vec[i], sizeof(*bulk));
380 niocount * sizeof(struct ptlrpc_bulk_desc *));
387 static int ost_commit_page(struct obd_conn *conn, struct page *page)
389 struct obd_ioobj obj;
394 memset(&buf, 0, sizeof(buf));
395 memset(&obj, 0, sizeof(obj));
400 rc = obd_commitrw(OBD_BRW_WRITE, conn, 1, &obj, 1, &buf);
405 static int ost_brw_write_cb(struct ptlrpc_bulk_desc *bulk, void *data)
407 int rc = ost_commit_page(&bulk->b_conn, bulk->b_page);
409 CERROR("ost_commit_page failed: %d\n", rc);
413 int ost_brw_write(struct ost_obd *obddev, struct ptlrpc_request *req)
415 struct ptlrpc_bulk_desc **bulk_vec = NULL;
416 struct ptlrpc_bulk_desc *bulk = NULL;
417 struct obd_conn conn;
420 int objcount, niocount;
421 char *tmp1, *tmp2, *end2;
424 struct niobuf *nb, *dst;
425 struct obd_ioobj *ioo;
426 struct ost_req *r = req->rq_req.ost;
430 tmp1 = ost_req_buf1(r);
431 tmp2 = ost_req_buf2(r);
432 end2 = tmp2 + req->rq_req.ost->buflen2;
433 objcount = r->buflen1 / sizeof(*ioo);
434 niocount = r->buflen2 / sizeof(*nb);
437 conn.oc_id = req->rq_req.ost->connid;
438 conn.oc_dev = req->rq_obd->u.ost.ost_tgt;
440 for (i = 0; i < objcount; i++) {
441 ost_unpack_ioo((void *)&tmp1, &ioo);
442 if (tmp2 + ioo->ioo_bufcnt > end2) {
446 for (j = 0; j < ioo->ioo_bufcnt; j++) {
447 ost_unpack_niobuf((void *)&tmp2, &nb);
451 rc = ost_pack_rep(NULL, 0, NULL, niocount * sizeof(*nb),
452 &req->rq_rephdr, &req->rq_rep,
453 &req->rq_replen, &req->rq_repbuf);
455 CERROR("cannot pack reply\n");
458 res = ost_rep_buf2(req->rq_rep.ost);
460 /* The unpackers move tmp1 and tmp2, so reset them before using */
461 tmp1 = ost_req_buf1(r);
462 tmp2 = ost_req_buf2(r);
463 req->rq_rep.ost->result = obd_preprw
464 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
465 niocount, (struct niobuf *)tmp2, (struct niobuf *)res);
467 if (req->rq_rep.ost->result) {
472 /* Setup buffers for the incoming pages, then send the niobufs
473 * describing those buffers to the OSC. */
474 OBD_ALLOC(bulk_vec, niocount * sizeof(struct ptlrpc_bulk_desc *));
475 if (bulk_vec == NULL) {
476 CERROR("cannot alloc bulk desc vector\n");
479 memset(bulk_vec, 0, niocount * sizeof(struct ptlrpc_bulk_desc *));
481 for (i = 0; i < niocount; i++) {
482 struct ptlrpc_service *srv = req->rq_obd->u.ost.ost_service;
484 bulk_vec[i] = ptlrpc_prep_bulk(&req->rq_peer);
485 if (bulk_vec[i] == NULL) {
486 CERROR("cannot alloc bulk desc\n");
491 spin_lock(&srv->srv_lock);
492 bulk_vec[i]->b_xid = srv->srv_xid++;
493 spin_unlock(&srv->srv_lock);
495 dst = &((struct niobuf *)res)[i];
496 dst->xid = HTON__u32(bulk_vec[i]->b_xid);
498 bulk_vec[i]->b_buf = (void *)(unsigned long)dst->addr;
499 bulk_vec[i]->b_cb = ost_brw_write_cb;
500 bulk_vec[i]->b_page = dst->page;
501 memcpy(&bulk_vec[i]->b_conn, &conn, sizeof(conn));
502 bulk_vec[i]->b_buflen = PAGE_SIZE;
503 bulk_vec[i]->b_portal = OSC_BULK_PORTAL;
504 rc = ptlrpc_register_bulk(bulk_vec[i]);
510 src = &((struct niobuf *)tmp2)[i];
511 memcpy((void *)(unsigned long)dst->addr,
512 (void *)(unsigned long)src->addr, src->len);
519 OBD_FREE(bulk, sizeof(*bulk));
520 if (bulk_vec != NULL) {
521 for (i = 0; i < niocount; i++) {
522 if (bulk_vec[i] != NULL)
523 OBD_FREE(bulk_vec[i], sizeof(*bulk));
526 niocount * sizeof(struct ptlrpc_bulk_desc *));
533 int ost_brw(struct ost_obd *obddev, struct ptlrpc_request *req)
535 struct ost_req *r = req->rq_req.ost;
538 if (cmd == OBD_BRW_READ)
539 return ost_brw_read(obddev, req);
541 return ost_brw_write(obddev, req);
544 static int ost_handle(struct obd_device *obddev,
545 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) != OST_TYPE_REQ) {
556 CERROR("lustre_ost: wrong packet type sent %d\n",
557 NTOH__u32(hdr->type));
562 rc = ost_unpack_req(req->rq_reqbuf, req->rq_reqlen,
563 &req->rq_reqhdr, &req->rq_req);
565 CERROR("lustre_ost: Invalid request\n");
570 switch (req->rq_reqhdr->opc) {
573 CDEBUG(D_INODE, "connect\n");
574 rc = ost_connect(ost, req);
577 CDEBUG(D_INODE, "disconnect\n");
578 rc = ost_disconnect(ost, req);
581 CDEBUG(D_INODE, "get_info\n");
582 rc = ost_get_info(ost, req);
585 CDEBUG(D_INODE, "create\n");
586 rc = ost_create(ost, req);
589 CDEBUG(D_INODE, "destroy\n");
590 rc = ost_destroy(ost, req);
593 CDEBUG(D_INODE, "getattr\n");
594 rc = ost_getattr(ost, req);
597 CDEBUG(D_INODE, "setattr\n");
598 rc = ost_setattr(ost, req);
601 CDEBUG(D_INODE, "brw\n");
602 rc = ost_brw(ost, req);
605 CDEBUG(D_INODE, "punch\n");
606 rc = ost_punch(ost, req);
609 req->rq_status = -ENOTSUPP;
610 return ptlrpc_error(obddev, svc, req);
616 CERROR("ost: processing error %d\n", rc);
617 ptlrpc_error(obddev, svc, req);
619 CDEBUG(D_INODE, "sending reply\n");
620 ptlrpc_reply(obddev, svc, req);
627 /* mount the file system (secretly) */
628 static int ost_setup(struct obd_device *obddev, obd_count len,
632 struct obd_ioctl_data* data = buf;
633 struct ost_obd *ost = &obddev->u.ost;
634 struct obd_device *tgt;
638 if (data->ioc_dev < 0 || data->ioc_dev > MAX_OBD_DEVICES) {
643 tgt = &obd_dev[data->ioc_dev];
645 if ( ! (tgt->obd_flags & OBD_ATTACHED) ||
646 ! (tgt->obd_flags & OBD_SET_UP) ){
647 CERROR("device not attached or not set up (%d)\n",
653 ost->ost_conn.oc_dev = tgt;
654 err = obd_connect(&ost->ost_conn);
656 CERROR("fail to connect to device %d\n", data->ioc_dev);
660 ost->ost_service = ptlrpc_init_svc( 64 * 1024,
667 if (!ost->ost_service) {
668 obd_disconnect(&ost->ost_conn);
672 rpc_register_service(ost->ost_service, "self");
674 err = ptlrpc_start_thread(obddev, ost->ost_service, "lustre_ost");
676 obd_disconnect(&ost->ost_conn);
685 static int ost_cleanup(struct obd_device * obddev)
687 struct ost_obd *ost = &obddev->u.ost;
692 if ( !list_empty(&obddev->obd_gen_clients) ) {
693 CERROR("still has clients!\n");
698 ptlrpc_stop_thread(ost->ost_service);
699 rpc_unregister_service(ost->ost_service);
701 if (!list_empty(&ost->ost_service->srv_reqs)) {
702 // XXX reply with errors and clean up
703 CERROR("Request list not empty!\n");
705 OBD_FREE(ost->ost_service, sizeof(*ost->ost_service));
707 err = obd_disconnect(&ost->ost_conn);
709 CERROR("lustre ost: fail to disconnect device\n");
718 /* use obd ops to offer management infrastructure */
719 static struct obd_ops ost_obd_ops = {
721 o_cleanup: ost_cleanup,
724 static int __init ost_init(void)
726 obd_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
730 static void __exit ost_exit(void)
732 obd_unregister_type(LUSTRE_OST_NAME);
735 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
736 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
737 MODULE_LICENSE("GPL");
739 module_init(ost_init);
740 module_exit(ost_exit);