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.)
34 #define DEBUG_SUBSYSTEM S_OST
36 #include <linux/module.h>
37 #include <linux/obd_ost.h>
38 #include <linux/lustre_net.h>
39 #include <linux/lustre_dlm.h>
41 static int ost_destroy(struct ptlrpc_request *req)
43 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
44 struct ost_body *body;
45 int rc, size = sizeof(*body);
48 body = lustre_msg_buf(req->rq_reqmsg, 0);
50 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
54 req->rq_status = obd_destroy(conn, &body->oa);
58 static int ost_getattr(struct ptlrpc_request *req)
60 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
61 struct ost_body *body, *repbody;
62 int rc, size = sizeof(*body);
65 body = lustre_msg_buf(req->rq_reqmsg, 0);
67 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
71 repbody = lustre_msg_buf(req->rq_repmsg, 0);
72 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
73 req->rq_status = obd_getattr(conn, &repbody->oa);
77 static int ost_open(struct ptlrpc_request *req)
79 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
80 struct ost_body *body, *repbody;
81 int rc, size = sizeof(*body);
84 body = lustre_msg_buf(req->rq_reqmsg, 0);
86 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
90 repbody = lustre_msg_buf(req->rq_repmsg, 0);
91 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
92 req->rq_status = obd_open(conn, &repbody->oa);
96 static int ost_close(struct ptlrpc_request *req)
98 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
99 struct ost_body *body, *repbody;
100 int rc, size = sizeof(*body);
103 body = lustre_msg_buf(req->rq_reqmsg, 0);
105 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
109 repbody = lustre_msg_buf(req->rq_repmsg, 0);
110 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
111 req->rq_status = obd_close(conn, &repbody->oa);
115 static int ost_create(struct ptlrpc_request *req)
117 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
118 struct ost_body *body, *repbody;
119 int rc, size = sizeof(*body);
122 body = lustre_msg_buf(req->rq_reqmsg, 0);
124 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
128 repbody = lustre_msg_buf(req->rq_repmsg, 0);
129 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
130 req->rq_status = obd_create(conn, &repbody->oa);
134 static int ost_punch(struct ptlrpc_request *req)
136 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
137 struct ost_body *body, *repbody;
138 int rc, size = sizeof(*body);
141 body = lustre_msg_buf(req->rq_reqmsg, 0);
143 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
147 repbody = lustre_msg_buf(req->rq_repmsg, 0);
148 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
149 req->rq_status = obd_punch(conn, &repbody->oa,
150 repbody->oa.o_blocks, repbody->oa.o_size);
154 static int ost_setattr(struct ptlrpc_request *req)
156 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
157 struct ost_body *body, *repbody;
158 int rc, size = sizeof(*body);
161 body = lustre_msg_buf(req->rq_reqmsg, 0);
163 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
167 repbody = lustre_msg_buf(req->rq_repmsg, 0);
168 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
169 req->rq_status = obd_setattr(conn, &repbody->oa);
173 static int ost_connect(struct ptlrpc_request *req)
175 struct ost_body *body;
176 struct obd_device *target;
177 struct obd_conn conn;
179 int rc, size = sizeof(*body), i;
182 uuid = lustre_msg_buf(req->rq_reqmsg, 0);
183 if (req->rq_reqmsg->buflens[0] > 37) {
185 req->rq_status = -EINVAL;
189 i = obd_class_uuid2dev(uuid);
191 req->rq_status = -ENODEV;
195 target = &obd_dev[i];
197 req->rq_status = -ENODEV;
201 conn.addr = req->rq_reqmsg->conn;
202 conn.cookie = req->rq_reqmsg->token;
204 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
208 req->rq_status = obd_connect(&conn, target);
209 req->rq_repmsg->conn = conn.addr;
210 req->rq_repmsg->token = conn.cookie;
212 CDEBUG(D_IOCTL, "rep buffer %p, id %d\n", req->rq_repmsg, conn.oc_id);
213 body = lustre_msg_buf(req->rq_repmsg, 0);
214 body->connid = conn.oc_id;
218 static int ost_disconnect(struct ptlrpc_request *req)
220 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
221 struct ost_body *body;
225 body = lustre_msg_buf(req->rq_reqmsg, 0);
227 rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
231 req->rq_status = obd_disconnect(conn);
235 static int ost_get_info(struct ptlrpc_request *req)
237 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
238 struct ost_body *body;
239 int rc, size[2] = {sizeof(*body)};
240 char *bufs[2] = {NULL, NULL}, *ptr;
243 body = lustre_msg_buf(req->rq_reqmsg, 0);
245 ptr = lustre_msg_buf(req->rq_reqmsg, 1);
249 req->rq_status = obd_get_info(conn, req->rq_reqmsg->buflens[1], ptr,
250 &(size[1]), (void **)&(bufs[1]));
252 rc = lustre_pack_msg(2, size, bufs, &req->rq_replen, &req->rq_repmsg);
254 CERROR("cannot pack reply\n");
259 static int ost_brw_read(struct ptlrpc_request *req)
261 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
262 struct ptlrpc_bulk_desc *desc;
263 void *tmp1, *tmp2, *end2;
264 struct niobuf_remote *remote_nb;
265 struct niobuf_local *local_nb = NULL;
266 struct obd_ioobj *ioo;
267 struct ost_body *body;
268 int rc, cmd, i, j, objcount, niocount, size = sizeof(*body);
271 body = lustre_msg_buf(req->rq_reqmsg, 0);
272 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
273 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
274 end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
275 objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
276 niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
279 for (i = 0; i < objcount; i++) {
280 ost_unpack_ioo(&tmp1, &ioo);
281 if (tmp2 + ioo->ioo_bufcnt > end2) {
283 GOTO(out, rc = -EFAULT);
285 for (j = 0; j < ioo->ioo_bufcnt; j++)
286 ost_unpack_niobuf(&tmp2, &remote_nb);
289 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
292 OBD_ALLOC(local_nb, sizeof(*local_nb) * niocount);
293 if (local_nb == NULL)
296 /* The unpackers move tmp1 and tmp2, so reset them before using */
297 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
298 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
299 req->rq_status = obd_preprw(cmd, conn, objcount,
300 tmp1, niocount, tmp2, local_nb, NULL);
305 desc = ptlrpc_prep_bulk(req->rq_connection);
307 GOTO(out_local, rc = -ENOMEM);
308 desc->b_portal = OST_BULK_PORTAL;
310 for (i = 0; i < niocount; i++) {
311 struct ptlrpc_bulk_page *bulk;
312 bulk = ptlrpc_prep_bulk_page(desc);
314 GOTO(out_bulk, rc = -ENOMEM);
315 remote_nb = &(((struct niobuf_remote *)tmp2)[i]);
316 bulk->b_xid = remote_nb->xid;
317 bulk->b_buf = (void *)(unsigned long)local_nb[i].addr;
318 bulk->b_buflen = PAGE_SIZE;
321 rc = ptlrpc_send_bulk(desc);
325 wait_event_interruptible(desc->b_waitq, ptlrpc_check_bulk_sent(desc));
326 if (desc->b_flags & PTL_RPC_FL_INTR)
329 /* The unpackers move tmp1 and tmp2, so reset them before using */
330 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
331 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
332 req->rq_status = obd_commitrw(cmd, conn, objcount,
333 tmp1, niocount, local_nb, NULL);
336 ptlrpc_free_bulk(desc);
338 OBD_FREE(local_nb, sizeof(*local_nb) * niocount);
341 ptlrpc_error(req->rq_svc, req);
343 ptlrpc_reply(req->rq_svc, req);
347 static int ost_brw_write(struct ptlrpc_request *req)
349 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
350 struct ptlrpc_bulk_desc *desc;
351 struct niobuf_remote *remote_nb;
352 struct niobuf_local *local_nb, *lnb;
353 struct obd_ioobj *ioo;
354 struct ost_body *body;
355 int cmd, rc, i, j, objcount, niocount, size[2] = {sizeof(*body)};
356 void *tmp1, *tmp2, *end2;
357 void *desc_priv = NULL;
361 body = lustre_msg_buf(req->rq_reqmsg, 0);
362 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
363 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
364 end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
365 objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
366 niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
369 for (i = 0; i < objcount; i++) {
370 ost_unpack_ioo((void *)&tmp1, &ioo);
371 if (tmp2 + ioo->ioo_bufcnt > end2) {
375 for (j = 0; j < ioo->ioo_bufcnt; j++)
376 ost_unpack_niobuf((void *)&tmp2, &remote_nb);
379 size[1] = niocount * sizeof(*remote_nb);
380 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
383 remote_nb = lustre_msg_buf(req->rq_repmsg, 1);
385 OBD_ALLOC(local_nb, niocount * sizeof(*local_nb));
386 if (local_nb == NULL)
387 GOTO(out, rc = -ENOMEM);
389 /* The unpackers move tmp1 and tmp2, so reset them before using */
390 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
391 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
392 req->rq_status = obd_preprw(cmd, conn, objcount,
393 tmp1, niocount, tmp2, local_nb, &desc_priv);
395 GOTO(out_free, rc = 0); /* XXX is this correct? */
397 desc = ptlrpc_prep_bulk(req->rq_connection);
399 GOTO(fail_preprw, rc = -ENOMEM);
401 desc->b_portal = OSC_BULK_PORTAL;
402 desc->b_desc_private = desc_priv;
403 memcpy(&(desc->b_conn), &conn, sizeof(conn));
405 for (i = 0, lnb = local_nb; i < niocount; i++, lnb++) {
406 struct ptlrpc_service *srv = req->rq_obd->u.ost.ost_service;
407 struct ptlrpc_bulk_page *bulk;
409 bulk = ptlrpc_prep_bulk_page(desc);
411 GOTO(fail_bulk, rc = -ENOMEM);
413 spin_lock(&srv->srv_lock);
414 bulk->b_xid = srv->srv_xid++;
415 spin_unlock(&srv->srv_lock);
417 bulk->b_buf = lnb->addr;
418 bulk->b_page = lnb->page;
419 bulk->b_flags = lnb->flags;
420 bulk->b_dentry = lnb->dentry;
421 bulk->b_buflen = PAGE_SIZE;
424 /* this advances remote_nb */
425 ost_pack_niobuf((void **)&remote_nb, lnb->offset, lnb->len, 0,
429 rc = ptlrpc_register_bulk(desc);
434 ptlrpc_reply(req->rq_svc, req);
436 wait_event_interruptible(desc->b_waitq,
437 desc->b_flags & PTL_BULK_FL_RCVD);
439 rc = obd_commitrw(cmd, conn, objcount, tmp1, niocount, local_nb,
440 desc->b_desc_private);
441 ptlrpc_free_bulk(desc);
444 OBD_FREE(local_nb, niocount * sizeof(*local_nb));
448 ptlrpc_error(req->rq_svc, req);
450 ptlrpc_reply(req->rq_svc, req);
455 ptlrpc_free_bulk(desc);
457 /* FIXME: how do we undo the preprw? */
461 static int ost_brw(struct ptlrpc_request *req)
463 struct ost_body *body = lustre_msg_buf(req->rq_reqmsg, 0);
465 if (body->data & OBD_BRW_WRITE)
466 return ost_brw_write(req);
468 return ost_brw_read(req);
472 static int ost_handle(struct ptlrpc_request *req)
477 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
478 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
479 CERROR("lustre_mds: Invalid request\n");
483 if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
484 CERROR("lustre_mds: wrong packet type sent %d\n",
485 req->rq_reqmsg->type);
486 GOTO(out, rc = -EINVAL);
489 if (req->rq_reqmsg->opc != OST_CONNECT) {
490 struct obd_export *export;
491 export = gen_client((struct obd_conn *) req->rq_reqmsg);
493 GOTO(out, rc = -ENOTCONN);
494 if (strcmp(req->rq_obd->obd_type->typ_name, "ost") != 0)
495 GOTO(out, rc = -EINVAL);
498 switch (req->rq_reqmsg->opc) {
500 CDEBUG(D_INODE, "connect\n");
501 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
502 rc = ost_connect(req);
505 CDEBUG(D_INODE, "disconnect\n");
506 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
507 rc = ost_disconnect(req);
510 CDEBUG(D_INODE, "get_info\n");
511 OBD_FAIL_RETURN(OBD_FAIL_OST_GET_INFO_NET, 0);
512 rc = ost_get_info(req);
515 CDEBUG(D_INODE, "create\n");
516 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
517 rc = ost_create(req);
520 CDEBUG(D_INODE, "destroy\n");
521 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
522 rc = ost_destroy(req);
525 CDEBUG(D_INODE, "getattr\n");
526 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
527 rc = ost_getattr(req);
530 CDEBUG(D_INODE, "setattr\n");
531 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
532 rc = ost_setattr(req);
535 CDEBUG(D_INODE, "setattr\n");
536 OBD_FAIL_RETURN(OBD_FAIL_OST_OPEN_NET, 0);
540 CDEBUG(D_INODE, "setattr\n");
541 OBD_FAIL_RETURN(OBD_FAIL_OST_CLOSE_NET, 0);
545 CDEBUG(D_INODE, "brw\n");
546 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
548 /* ost_brw sends its own replies */
551 CDEBUG(D_INODE, "punch\n");
552 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
557 CDEBUG(D_INODE, "statfs\n");
558 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
559 rc = ost_statfs(req);
563 req->rq_status = -ENOTSUPP;
564 rc = ptlrpc_error(req->rq_svc, req);
570 //req->rq_status = rc;
572 CERROR("ost: processing error %d\n", rc);
573 ptlrpc_error(req->rq_svc, req);
575 CDEBUG(D_INODE, "sending reply\n");
576 ptlrpc_reply(req->rq_svc, req);
582 #define OST_NUM_THREADS 6
584 /* mount the file system (secretly) */
585 static int ost_setup(struct obd_device *obddev, obd_count len, void *buf)
587 struct obd_ioctl_data* data = buf;
588 struct ost_obd *ost = &obddev->u.ost;
589 struct obd_device *tgt;
594 if (data->ioc_dev < 0 || data->ioc_dev > MAX_OBD_DEVICES)
598 tgt = &obd_dev[data->ioc_dev];
599 if (!(tgt->obd_flags & OBD_ATTACHED) ||
600 !(tgt->obd_flags & OBD_SET_UP)) {
601 CERROR("device not attached or not set up (%d)\n",
603 GOTO(error_dec, err = -EINVAL);
606 err = obd_connect(&ost->ost_conn, tgt);
608 CERROR("fail to connect to device %d\n", data->ioc_dev);
609 GOTO(error_dec, err = -EINVAL);
612 obddev->obd_namespace =
613 ldlm_namespace_new("ost", LDLM_NAMESPACE_SERVER);
614 if (obddev->obd_namespace == NULL)
617 ost->ost_service = ptlrpc_init_svc(64 * 1024, OST_REQUEST_PORTAL,
618 OSC_REPLY_PORTAL, "self",ost_handle);
619 if (!ost->ost_service) {
620 CERROR("failed to start service\n");
621 GOTO(error_disc, err = -EINVAL);
624 for (i = 0; i < OST_NUM_THREADS; i++) {
625 err = ptlrpc_start_thread(obddev, ost->ost_service,
628 CERROR("error starting thread #%d: rc %d\n", i, err);
629 GOTO(error_disc, err = -EINVAL);
636 obd_disconnect(&ost->ost_conn);
642 static int ost_cleanup(struct obd_device * obddev)
644 struct ost_obd *ost = &obddev->u.ost;
649 if ( !list_empty(&obddev->obd_exports) ) {
650 CERROR("still has clients!\n");
654 ptlrpc_stop_all_threads(ost->ost_service);
655 ptlrpc_unregister_service(ost->ost_service);
657 err = obd_disconnect(&ost->ost_conn);
659 CERROR("lustre ost: fail to disconnect device\n");
663 ldlm_namespace_free(obddev->obd_namespace);
669 /* use obd ops to offer management infrastructure */
670 static struct obd_ops ost_obd_ops = {
672 o_cleanup: ost_cleanup,
675 static int __init ost_init(void)
677 obd_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
681 static void __exit ost_exit(void)
683 obd_unregister_type(LUSTRE_OST_NAME);
686 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
687 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
688 MODULE_LICENSE("GPL");
690 module_init(ost_init);
691 module_exit(ost_exit);