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>
40 #include <linux/init.h>
42 static int ost_destroy(struct ptlrpc_request *req)
44 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
45 struct ost_body *body;
46 int rc, size = sizeof(*body);
49 body = lustre_msg_buf(req->rq_reqmsg, 0);
51 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
55 req->rq_status = obd_destroy(conn, &body->oa, NULL);
59 static int ost_getattr(struct ptlrpc_request *req)
61 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
62 struct ost_body *body, *repbody;
63 int rc, size = sizeof(*body);
66 body = lustre_msg_buf(req->rq_reqmsg, 0);
68 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
72 repbody = lustre_msg_buf(req->rq_repmsg, 0);
73 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
74 req->rq_status = obd_getattr(conn, &repbody->oa, NULL);
78 static int ost_statfs(struct ptlrpc_request *req)
80 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
81 struct obd_statfs *osfs;
82 int rc, size = sizeof(*osfs);
85 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
89 osfs = lustre_msg_buf(req->rq_repmsg, 0);
90 memset(osfs, 0, size);
92 rc = obd_statfs(conn, osfs);
94 CERROR("ost: statfs failed: rc %d\n", rc);
98 obd_statfs_pack(osfs, osfs);
103 static int ost_open(struct ptlrpc_request *req)
105 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
106 struct ost_body *body, *repbody;
107 int rc, size = sizeof(*body);
110 body = lustre_msg_buf(req->rq_reqmsg, 0);
112 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
116 repbody = lustre_msg_buf(req->rq_repmsg, 0);
117 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
118 req->rq_status = obd_open(conn, &repbody->oa, NULL);
122 static int ost_close(struct ptlrpc_request *req)
124 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
125 struct ost_body *body, *repbody;
126 int rc, size = sizeof(*body);
129 body = lustre_msg_buf(req->rq_reqmsg, 0);
131 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
135 repbody = lustre_msg_buf(req->rq_repmsg, 0);
136 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
137 req->rq_status = obd_close(conn, &repbody->oa, NULL);
141 static int ost_create(struct ptlrpc_request *req)
143 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
144 struct ost_body *body, *repbody;
145 int rc, size = sizeof(*body);
148 body = lustre_msg_buf(req->rq_reqmsg, 0);
150 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
154 repbody = lustre_msg_buf(req->rq_repmsg, 0);
155 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
156 req->rq_status = obd_create(conn, &repbody->oa, NULL);
160 static int ost_punch(struct ptlrpc_request *req)
162 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
163 struct ost_body *body, *repbody;
164 int rc, size = sizeof(*body);
167 body = lustre_msg_buf(req->rq_reqmsg, 0);
169 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
173 repbody = lustre_msg_buf(req->rq_repmsg, 0);
174 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
175 req->rq_status = obd_punch(conn, &repbody->oa, NULL,
176 repbody->oa.o_blocks, repbody->oa.o_size);
180 static int ost_setattr(struct ptlrpc_request *req)
182 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
183 struct ost_body *body, *repbody;
184 int rc, size = sizeof(*body);
187 body = lustre_msg_buf(req->rq_reqmsg, 0);
189 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
193 repbody = lustre_msg_buf(req->rq_repmsg, 0);
194 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
195 req->rq_status = obd_setattr(conn, &repbody->oa, NULL);
199 static int ost_bulk_timeout(void *data)
201 struct ptlrpc_bulk_desc *desc = data;
204 CERROR("(not yet) starting recovery of client %p\n", desc->bd_client);
208 static int ost_brw_read(struct ptlrpc_request *req)
210 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
211 struct ptlrpc_bulk_desc *desc;
212 void *tmp1, *tmp2, *end2;
213 struct niobuf_remote *remote_nb;
214 struct niobuf_local *local_nb = NULL;
215 struct obd_ioobj *ioo;
216 struct ost_body *body;
217 struct l_wait_info lwi;
218 void *desc_priv = NULL;
219 int rc, cmd, i, j, objcount, niocount, size = sizeof(*body);
222 body = lustre_msg_buf(req->rq_reqmsg, 0);
223 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
224 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
225 end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
226 objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
227 niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
230 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
233 for (i = 0; i < objcount; i++) {
234 ost_unpack_ioo(&tmp1, &ioo);
235 if (tmp2 + ioo->ioo_bufcnt > end2) {
237 GOTO(out, rc = -EFAULT);
239 for (j = 0; j < ioo->ioo_bufcnt; j++)
240 ost_unpack_niobuf(&tmp2, &remote_nb);
243 OBD_ALLOC(local_nb, sizeof(*local_nb) * niocount);
244 if (local_nb == NULL)
245 GOTO(out, rc = -ENOMEM);
247 /* The unpackers move tmp1 and tmp2, so reset them before using */
248 ioo = lustre_msg_buf(req->rq_reqmsg, 1);
249 remote_nb = lustre_msg_buf(req->rq_reqmsg, 2);
250 req->rq_status = obd_preprw(cmd, conn, objcount, ioo, niocount,
251 remote_nb, local_nb, &desc_priv);
256 desc = ptlrpc_prep_bulk(req->rq_connection);
258 GOTO(out_local, rc = -ENOMEM);
259 desc->bd_portal = OST_BULK_PORTAL;
261 for (i = 0; i < niocount; i++) {
262 struct ptlrpc_bulk_page *bulk = ptlrpc_prep_bulk_page(desc);
265 GOTO(out_bulk, rc = -ENOMEM);
266 bulk->bp_xid = remote_nb[i].xid;
267 bulk->bp_buf = local_nb[i].addr;
268 bulk->bp_buflen = remote_nb[i].len;
271 rc = ptlrpc_send_bulk(desc);
275 lwi = LWI_TIMEOUT(obd_timeout * HZ, ost_bulk_timeout, desc);
276 rc = l_wait_event(desc->bd_waitq, desc->bd_flags &PTL_BULK_FL_SENT, &lwi);
278 LASSERT(rc == -ETIMEDOUT);
282 req->rq_status = obd_commitrw(cmd, conn, objcount, ioo, niocount,
283 local_nb, desc_priv);
285 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
288 ptlrpc_free_bulk(desc);
290 OBD_FREE(local_nb, sizeof(*local_nb) * niocount);
293 ptlrpc_error(req->rq_svc, req);
295 ptlrpc_reply(req->rq_svc, req);
299 static int ost_brw_write(struct ptlrpc_request *req)
301 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
302 struct ptlrpc_bulk_desc *desc;
303 struct niobuf_remote *remote_nb;
304 struct niobuf_local *local_nb, *lnb;
305 struct obd_ioobj *ioo;
306 struct ost_body *body;
307 int cmd, rc, i, j, objcount, niocount, size[2] = {sizeof(*body)};
308 void *tmp1, *tmp2, *end2;
309 void *desc_priv = NULL;
311 struct ptlrpc_service *srv;
312 struct l_wait_info lwi;
316 body = lustre_msg_buf(req->rq_reqmsg, 0);
317 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
318 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
319 end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
320 objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
321 niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
324 for (i = 0; i < objcount; i++) {
325 ost_unpack_ioo((void *)&tmp1, &ioo);
326 if (tmp2 + ioo->ioo_bufcnt > end2) {
330 for (j = 0; j < ioo->ioo_bufcnt; j++)
331 ost_unpack_niobuf((void *)&tmp2, &remote_nb);
334 size[1] = niocount * sizeof(*remote_nb);
335 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
338 remote_nb = lustre_msg_buf(req->rq_repmsg, 1);
340 OBD_ALLOC(local_nb, niocount * sizeof(*local_nb));
341 if (local_nb == NULL)
342 GOTO(out, rc = -ENOMEM);
344 /* The unpackers move tmp1 and tmp2, so reset them before using */
345 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
346 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
347 req->rq_status = obd_preprw(cmd, conn, objcount, tmp1, niocount, tmp2,
348 local_nb, &desc_priv);
350 GOTO(out_free, rc = 0); /* XXX is this correct? */
352 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
353 GOTO(fail_preprw, rc = 0);
355 desc = ptlrpc_prep_bulk(req->rq_connection);
357 GOTO(fail_preprw, rc = -ENOMEM);
359 desc->bd_portal = OSC_BULK_PORTAL;
360 desc->bd_desc_private = desc_priv;
361 memcpy(&(desc->bd_conn), &conn, sizeof(conn));
363 srv = req->rq_obd->u.ost.ost_service;
364 spin_lock(&srv->srv_lock);
365 xid = srv->srv_xid++; /* single xid for all pages */
366 spin_unlock(&srv->srv_lock);
368 for (i = 0, lnb = local_nb; i < niocount; i++, lnb++) {
369 struct ptlrpc_bulk_page *bulk;
371 bulk = ptlrpc_prep_bulk_page(desc);
373 GOTO(fail_bulk, rc = -ENOMEM);
375 bulk->bp_xid = xid; /* single xid for all pages */
377 bulk->bp_buf = lnb->addr;
378 bulk->bp_page = lnb->page;
379 bulk->bp_flags = lnb->flags;
380 bulk->bp_dentry = lnb->dentry;
381 bulk->bp_buflen = lnb->len;
384 /* this advances remote_nb */
385 ost_pack_niobuf((void **)&remote_nb, lnb->offset, lnb->len, 0,
389 rc = ptlrpc_register_bulk(desc);
394 ptlrpc_reply(req->rq_svc, req);
396 lwi = LWI_TIMEOUT(obd_timeout * HZ, ost_bulk_timeout, desc);
397 rc = l_wait_event(desc->bd_waitq, desc->bd_flags & PTL_BULK_FL_RCVD,
400 if (rc != -ETIMEDOUT)
405 rc = obd_commitrw(cmd, conn, objcount, tmp1, niocount, local_nb,
406 desc->bd_desc_private);
407 ptlrpc_free_bulk(desc);
410 OBD_FREE(local_nb, niocount * sizeof(*local_nb));
414 ptlrpc_error(req->rq_svc, req);
416 ptlrpc_reply(req->rq_svc, req);
421 ptlrpc_free_bulk(desc);
423 /* FIXME: how do we undo the preprw? */
427 static int ost_handle(struct ptlrpc_request *req)
432 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
433 if (rc || OBD_FAIL_CHECK(OBD_FAIL_OST_HANDLE_UNPACK)) {
434 CERROR("lustre_ost: Invalid request\n");
438 if (req->rq_reqmsg->opc != OST_CONNECT &&
439 req->rq_export == NULL) {
440 CERROR("lustre_ost: operation %d on unconnected OST\n",
441 req->rq_reqmsg->opc);
442 GOTO(out, rc = -ENOTCONN);
445 if (strcmp(req->rq_obd->obd_type->typ_name, "ost") != 0)
446 GOTO(out, rc = -EINVAL);
448 switch (req->rq_reqmsg->opc) {
450 CDEBUG(D_INODE, "connect\n");
451 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
452 rc = target_handle_connect(req);
455 CDEBUG(D_INODE, "disconnect\n");
456 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
457 rc = target_handle_disconnect(req);
460 CDEBUG(D_INODE, "create\n");
461 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
462 rc = ost_create(req);
465 CDEBUG(D_INODE, "destroy\n");
466 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
467 rc = ost_destroy(req);
470 CDEBUG(D_INODE, "getattr\n");
471 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
472 rc = ost_getattr(req);
475 CDEBUG(D_INODE, "setattr\n");
476 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
477 rc = ost_setattr(req);
480 CDEBUG(D_INODE, "open\n");
481 OBD_FAIL_RETURN(OBD_FAIL_OST_OPEN_NET, 0);
485 CDEBUG(D_INODE, "close\n");
486 OBD_FAIL_RETURN(OBD_FAIL_OST_CLOSE_NET, 0);
490 CDEBUG(D_INODE, "write\n");
491 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
492 rc = ost_brw_write(req);
493 /* ost_brw sends its own replies */
496 CDEBUG(D_INODE, "read\n");
497 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
498 rc = ost_brw_read(req);
499 /* ost_brw sends its own replies */
502 CDEBUG(D_INODE, "punch\n");
503 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
507 CDEBUG(D_INODE, "statfs\n");
508 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
509 rc = ost_statfs(req);
512 CDEBUG(D_INODE, "enqueue\n");
513 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
514 rc = ldlm_handle_enqueue(req);
519 CDEBUG(D_INODE, "convert\n");
520 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
521 rc = ldlm_handle_convert(req);
526 CDEBUG(D_INODE, "cancel\n");
527 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
528 rc = ldlm_handle_cancel(req);
532 case LDLM_BL_CALLBACK:
533 case LDLM_CP_CALLBACK:
534 CDEBUG(D_INODE, "callback\n");
535 CERROR("callbacks should not happen on OST\n");
537 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
540 req->rq_status = -ENOTSUPP;
541 rc = ptlrpc_error(req->rq_svc, req);
547 //req->rq_status = rc;
549 CERROR("ost: processing error (opcode=%d): %d\n",
550 req->rq_reqmsg->opc, rc);
551 ptlrpc_error(req->rq_svc, req);
553 CDEBUG(D_INODE, "sending reply\n");
554 if (req->rq_repmsg == NULL)
555 CERROR("handler for opcode %d returned rc=0 without "
556 "creating rq_repmsg; needs to return rc != "
557 "0!\n", req->rq_reqmsg->opc);
558 ptlrpc_reply(req->rq_svc, req);
564 #define OST_NUM_THREADS 6
566 /* mount the file system (secretly) */
567 static int ost_setup(struct obd_device *obddev, obd_count len, void *buf)
569 struct obd_ioctl_data* data = buf;
570 struct ost_obd *ost = &obddev->u.ost;
571 struct obd_device *tgt;
576 if (data->ioc_inllen1 < 1) {
577 CERROR("requires a TARGET OBD UUID\n");
580 if (data->ioc_inllen1 > 37) {
581 CERROR("OBD UUID must be less than 38 characters\n");
586 tgt = class_uuid2obd(data->ioc_inlbuf1);
587 if (!tgt || !(tgt->obd_flags & OBD_ATTACHED) ||
588 !(tgt->obd_flags & OBD_SET_UP)) {
589 CERROR("device not attached or not set up (%d)\n",
591 GOTO(error_dec, err = -EINVAL);
594 err = obd_connect(&ost->ost_conn, tgt, NULL);
596 CERROR("fail to connect to device %d\n", data->ioc_dev);
597 GOTO(error_dec, err = -EINVAL);
600 ost->ost_service = ptlrpc_init_svc(1024, 640, OST_REQUEST_PORTAL,
601 OSC_REPLY_PORTAL, "self", ost_handle,
603 if (!ost->ost_service) {
604 CERROR("failed to start service\n");
605 GOTO(error_disc, err = -EINVAL);
608 for (i = 0; i < OST_NUM_THREADS; i++) {
610 sprintf(name, "lustre_ost_%02d", i);
611 err = ptlrpc_start_thread(obddev, ost->ost_service, name);
613 CERROR("error starting thread #%d: rc %d\n", i, err);
614 GOTO(error_disc, err = -EINVAL);
621 obd_disconnect(&ost->ost_conn);
627 static int ost_cleanup(struct obd_device * obddev)
629 struct ost_obd *ost = &obddev->u.ost;
634 if ( !list_empty(&obddev->obd_exports) ) {
635 CERROR("still has clients!\n");
639 ptlrpc_stop_all_threads(ost->ost_service);
640 ptlrpc_unregister_service(ost->ost_service);
642 err = obd_disconnect(&ost->ost_conn);
644 CERROR("lustre ost: fail to disconnect device\n");
652 /* use obd ops to offer management infrastructure */
653 static struct obd_ops ost_obd_ops = {
655 o_cleanup: ost_cleanup,
658 static int __init ost_init(void)
660 class_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
664 static void __exit ost_exit(void)
666 class_unregister_type(LUSTRE_OST_NAME);
669 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
670 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
671 MODULE_LICENSE("GPL");
673 module_init(ost_init);
674 module_exit(ost_exit);