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 /* FIXME: unpack only valid fields instead of memcpy, endianness */
74 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
75 req->rq_status = obd_getattr(conn, &repbody->oa, NULL);
79 static int ost_statfs(struct ptlrpc_request *req)
81 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
82 struct obd_statfs *osfs;
83 int rc, size = sizeof(*osfs);
86 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
90 osfs = lustre_msg_buf(req->rq_repmsg, 0);
91 memset(osfs, 0, size);
93 rc = obd_statfs(conn, osfs);
95 CERROR("ost: statfs failed: rc %d\n", rc);
99 obd_statfs_pack(osfs, osfs);
104 static int ost_open(struct ptlrpc_request *req)
106 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
107 struct ost_body *body, *repbody;
108 int rc, size = sizeof(*body);
111 body = lustre_msg_buf(req->rq_reqmsg, 0);
113 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
117 repbody = lustre_msg_buf(req->rq_repmsg, 0);
118 /* FIXME: unpack only valid fields instead of memcpy, endianness */
119 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
120 req->rq_status = obd_open(conn, &repbody->oa, NULL);
124 static int ost_close(struct ptlrpc_request *req)
126 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
127 struct ost_body *body, *repbody;
128 int rc, size = sizeof(*body);
131 body = lustre_msg_buf(req->rq_reqmsg, 0);
133 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
137 repbody = lustre_msg_buf(req->rq_repmsg, 0);
138 /* FIXME: unpack only valid fields instead of memcpy, endianness */
139 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
140 req->rq_status = obd_close(conn, &repbody->oa, NULL);
144 static int ost_create(struct ptlrpc_request *req)
146 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
147 struct ost_body *body, *repbody;
148 int rc, size = sizeof(*body);
151 body = lustre_msg_buf(req->rq_reqmsg, 0);
153 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
157 repbody = lustre_msg_buf(req->rq_repmsg, 0);
158 /* FIXME: unpack only valid fields instead of memcpy, endianness */
159 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
160 req->rq_status = obd_create(conn, &repbody->oa, NULL);
164 static int ost_punch(struct ptlrpc_request *req)
166 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
167 struct ost_body *body, *repbody;
168 int rc, size = sizeof(*body);
171 body = lustre_msg_buf(req->rq_reqmsg, 0);
173 if ((NTOH__u32(body->oa.o_valid) & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))!=
174 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
177 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
181 repbody = lustre_msg_buf(req->rq_repmsg, 0);
182 /* FIXME: unpack only valid fields instead of memcpy, endianness */
183 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
184 req->rq_status = obd_punch(conn, &repbody->oa, NULL,
185 repbody->oa.o_size, repbody->oa.o_blocks);
189 static int ost_setattr(struct ptlrpc_request *req)
191 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
192 struct ost_body *body, *repbody;
193 int rc, size = sizeof(*body);
196 body = lustre_msg_buf(req->rq_reqmsg, 0);
198 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
202 repbody = lustre_msg_buf(req->rq_repmsg, 0);
203 /* FIXME: unpack only valid fields instead of memcpy, endianness */
204 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
205 req->rq_status = obd_setattr(conn, &repbody->oa, NULL);
209 static int ost_bulk_timeout(void *data)
211 struct ptlrpc_bulk_desc *desc = data;
214 CERROR("(not yet) starting recovery of client %p\n", desc->bd_client);
218 static int ost_brw_read(struct ptlrpc_request *req)
220 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
221 struct ptlrpc_bulk_desc *desc;
222 void *tmp1, *tmp2, *end2;
223 struct niobuf_remote *remote_nb;
224 struct niobuf_local *local_nb = NULL;
225 struct obd_ioobj *ioo;
226 struct ost_body *body;
227 struct l_wait_info lwi;
228 void *desc_priv = NULL;
229 int rc, cmd, i, j, objcount, niocount, size = sizeof(*body);
232 body = lustre_msg_buf(req->rq_reqmsg, 0);
233 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
234 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
235 end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
236 objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
237 niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
240 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
243 for (i = 0; i < objcount; i++) {
244 ost_unpack_ioo(&tmp1, &ioo);
245 if (tmp2 + ioo->ioo_bufcnt > end2) {
247 GOTO(out, rc = -EFAULT);
249 for (j = 0; j < ioo->ioo_bufcnt; j++)
250 ost_unpack_niobuf(&tmp2, &remote_nb);
253 OBD_ALLOC(local_nb, sizeof(*local_nb) * niocount);
254 if (local_nb == NULL)
255 GOTO(out, rc = -ENOMEM);
257 /* The unpackers move tmp1 and tmp2, so reset them before using */
258 ioo = lustre_msg_buf(req->rq_reqmsg, 1);
259 remote_nb = lustre_msg_buf(req->rq_reqmsg, 2);
260 req->rq_status = obd_preprw(cmd, conn, objcount, ioo, niocount,
261 remote_nb, local_nb, &desc_priv);
266 desc = ptlrpc_prep_bulk(req->rq_connection);
268 GOTO(out_local, rc = -ENOMEM);
269 desc->bd_portal = OST_BULK_PORTAL;
271 for (i = 0; i < niocount; i++) {
272 struct ptlrpc_bulk_page *bulk = ptlrpc_prep_bulk_page(desc);
275 GOTO(out_bulk, rc = -ENOMEM);
276 bulk->bp_xid = remote_nb[i].xid;
277 bulk->bp_buf = local_nb[i].addr;
278 bulk->bp_buflen = remote_nb[i].len;
281 rc = ptlrpc_send_bulk(desc);
285 lwi = LWI_TIMEOUT(obd_timeout * HZ, ost_bulk_timeout, desc);
286 rc = l_wait_event(desc->bd_waitq, desc->bd_flags &PTL_BULK_FL_SENT, &lwi);
288 LASSERT(rc == -ETIMEDOUT);
292 req->rq_status = obd_commitrw(cmd, conn, objcount, ioo, niocount,
293 local_nb, desc_priv);
295 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
298 ptlrpc_free_bulk(desc);
300 OBD_FREE(local_nb, sizeof(*local_nb) * niocount);
303 /* It's a lot of work to delay allocating the reply, and a lot
304 * less work to just free it here. */
305 OBD_FREE(req->rq_repmsg, req->rq_replen);
306 req->rq_repmsg = NULL;
307 ptlrpc_error(req->rq_svc, req);
309 ptlrpc_reply(req->rq_svc, req);
313 static int ost_brw_write(struct ptlrpc_request *req)
315 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
316 struct ptlrpc_bulk_desc *desc;
317 struct niobuf_remote *remote_nb;
318 struct niobuf_local *local_nb, *lnb;
319 struct obd_ioobj *ioo;
320 struct ost_body *body;
321 int cmd, rc, i, j, objcount, niocount, size[2] = {sizeof(*body)};
322 void *tmp1, *tmp2, *end2;
323 void *desc_priv = NULL;
325 struct ptlrpc_service *srv;
326 struct l_wait_info lwi;
330 body = lustre_msg_buf(req->rq_reqmsg, 0);
331 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
332 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
333 end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
334 objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
335 niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
338 for (i = 0; i < objcount; i++) {
339 ost_unpack_ioo((void *)&tmp1, &ioo);
340 if (tmp2 + ioo->ioo_bufcnt > end2) {
344 for (j = 0; j < ioo->ioo_bufcnt; j++)
345 ost_unpack_niobuf((void *)&tmp2, &remote_nb);
348 size[1] = niocount * sizeof(*remote_nb);
349 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
352 remote_nb = lustre_msg_buf(req->rq_repmsg, 1);
354 OBD_ALLOC(local_nb, niocount * sizeof(*local_nb));
355 if (local_nb == NULL)
356 GOTO(out, rc = -ENOMEM);
358 /* The unpackers move tmp1 and tmp2, so reset them before using */
359 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
360 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
361 req->rq_status = obd_preprw(cmd, conn, objcount, tmp1, niocount, tmp2,
362 local_nb, &desc_priv);
364 GOTO(out_free, rc = 0); /* XXX is this correct? */
366 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
367 GOTO(fail_preprw, rc = 0);
369 desc = ptlrpc_prep_bulk(req->rq_connection);
371 GOTO(fail_preprw, rc = -ENOMEM);
373 desc->bd_portal = OSC_BULK_PORTAL;
374 desc->bd_desc_private = desc_priv;
375 memcpy(&(desc->bd_conn), &conn, sizeof(conn));
377 srv = req->rq_obd->u.ost.ost_service;
378 spin_lock(&srv->srv_lock);
379 xid = srv->srv_xid++; /* single xid for all pages */
380 spin_unlock(&srv->srv_lock);
382 for (i = 0, lnb = local_nb; i < niocount; i++, lnb++) {
383 struct ptlrpc_bulk_page *bulk;
385 bulk = ptlrpc_prep_bulk_page(desc);
387 GOTO(fail_bulk, rc = -ENOMEM);
389 bulk->bp_xid = xid; /* single xid for all pages */
391 bulk->bp_buf = lnb->addr;
392 bulk->bp_page = lnb->page;
393 bulk->bp_flags = lnb->flags;
394 bulk->bp_dentry = lnb->dentry;
395 bulk->bp_buflen = lnb->len;
398 /* this advances remote_nb */
399 ost_pack_niobuf((void **)&remote_nb, lnb->offset, lnb->len, 0,
403 rc = ptlrpc_register_bulk(desc);
408 ptlrpc_reply(req->rq_svc, req);
410 lwi = LWI_TIMEOUT(obd_timeout * HZ, ost_bulk_timeout, desc);
411 rc = l_wait_event(desc->bd_waitq, desc->bd_flags & PTL_BULK_FL_RCVD,
414 if (rc != -ETIMEDOUT)
419 rc = obd_commitrw(cmd, conn, objcount, tmp1, niocount, local_nb,
420 desc->bd_desc_private);
421 ptlrpc_free_bulk(desc);
424 OBD_FREE(local_nb, niocount * sizeof(*local_nb));
428 OBD_FREE(req->rq_repmsg, req->rq_replen);
429 req->rq_repmsg = NULL;
430 ptlrpc_error(req->rq_svc, req);
432 ptlrpc_reply(req->rq_svc, req);
437 ptlrpc_free_bulk(desc);
439 /* FIXME: how do we undo the preprw? */
443 static int ost_handle(struct ptlrpc_request *req)
448 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
449 if (rc || OBD_FAIL_CHECK(OBD_FAIL_OST_HANDLE_UNPACK)) {
450 CERROR("lustre_ost: Invalid request\n");
454 if (req->rq_reqmsg->opc != OST_CONNECT &&
455 req->rq_export == NULL) {
456 CERROR("lustre_ost: operation %d on unconnected OST\n",
457 req->rq_reqmsg->opc);
458 GOTO(out, rc = -ENOTCONN);
461 if (strcmp(req->rq_obd->obd_type->typ_name, "ost") != 0)
462 GOTO(out, rc = -EINVAL);
464 switch (req->rq_reqmsg->opc) {
466 CDEBUG(D_INODE, "connect\n");
467 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
468 rc = target_handle_connect(req);
471 CDEBUG(D_INODE, "disconnect\n");
472 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
473 rc = target_handle_disconnect(req);
476 CDEBUG(D_INODE, "create\n");
477 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
478 rc = ost_create(req);
481 CDEBUG(D_INODE, "destroy\n");
482 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
483 rc = ost_destroy(req);
486 CDEBUG(D_INODE, "getattr\n");
487 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
488 rc = ost_getattr(req);
491 CDEBUG(D_INODE, "setattr\n");
492 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
493 rc = ost_setattr(req);
496 CDEBUG(D_INODE, "open\n");
497 OBD_FAIL_RETURN(OBD_FAIL_OST_OPEN_NET, 0);
501 CDEBUG(D_INODE, "close\n");
502 OBD_FAIL_RETURN(OBD_FAIL_OST_CLOSE_NET, 0);
506 CDEBUG(D_INODE, "write\n");
507 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
508 rc = ost_brw_write(req);
509 /* ost_brw sends its own replies */
512 CDEBUG(D_INODE, "read\n");
513 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
514 rc = ost_brw_read(req);
515 /* ost_brw sends its own replies */
518 CDEBUG(D_INODE, "punch\n");
519 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
523 CDEBUG(D_INODE, "statfs\n");
524 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
525 rc = ost_statfs(req);
528 CDEBUG(D_INODE, "enqueue\n");
529 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
530 rc = ldlm_handle_enqueue(req);
535 CDEBUG(D_INODE, "convert\n");
536 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
537 rc = ldlm_handle_convert(req);
542 CDEBUG(D_INODE, "cancel\n");
543 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
544 rc = ldlm_handle_cancel(req);
548 case LDLM_BL_CALLBACK:
549 case LDLM_CP_CALLBACK:
550 CDEBUG(D_INODE, "callback\n");
551 CERROR("callbacks should not happen on OST\n");
553 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
556 req->rq_status = -ENOTSUPP;
557 rc = ptlrpc_error(req->rq_svc, req);
563 //req->rq_status = rc;
565 CERROR("ost: processing error (opcode=%d): %d\n",
566 req->rq_reqmsg->opc, rc);
567 ptlrpc_error(req->rq_svc, req);
569 CDEBUG(D_INODE, "sending reply\n");
570 if (req->rq_repmsg == NULL)
571 CERROR("handler for opcode %d returned rc=0 without "
572 "creating rq_repmsg; needs to return rc != "
573 "0!\n", req->rq_reqmsg->opc);
574 ptlrpc_reply(req->rq_svc, req);
580 #define OST_NUM_THREADS 6
582 /* mount the file system (secretly) */
583 static int ost_setup(struct obd_device *obddev, obd_count len, void *buf)
585 struct obd_ioctl_data* data = buf;
586 struct ost_obd *ost = &obddev->u.ost;
587 struct obd_device *tgt;
592 if (data->ioc_inllen1 < 1) {
593 CERROR("requires a TARGET OBD UUID\n");
596 if (data->ioc_inllen1 > 37) {
597 CERROR("OBD UUID must be less than 38 characters\n");
602 tgt = class_uuid2obd(data->ioc_inlbuf1);
603 if (!tgt || !(tgt->obd_flags & OBD_ATTACHED) ||
604 !(tgt->obd_flags & OBD_SET_UP)) {
605 CERROR("device not attached or not set up (%d)\n",
607 GOTO(error_dec, err = -EINVAL);
610 err = obd_connect(&ost->ost_conn, tgt, NULL, NULL, NULL);
612 CERROR("fail to connect to device %d\n", data->ioc_dev);
613 GOTO(error_dec, err = -EINVAL);
616 ost->ost_service = ptlrpc_init_svc(OST_NEVENTS, OST_NBUFS,
617 OST_BUFSIZE, OST_MAXREQSIZE,
618 OST_REQUEST_PORTAL, OSC_REPLY_PORTAL,
619 "self", ost_handle, "ost");
620 if (!ost->ost_service) {
621 CERROR("failed to start service\n");
622 GOTO(error_disc, err = -EINVAL);
625 for (i = 0; i < OST_NUM_THREADS; i++) {
627 sprintf(name, "lustre_ost_%02d", i);
628 err = ptlrpc_start_thread(obddev, ost->ost_service, name);
630 CERROR("error starting thread #%d: rc %d\n", i, err);
631 GOTO(error_disc, err = -EINVAL);
638 obd_disconnect(&ost->ost_conn);
644 static int ost_cleanup(struct obd_device * obddev)
646 struct ost_obd *ost = &obddev->u.ost;
651 if ( !list_empty(&obddev->obd_exports) ) {
652 CERROR("still has clients!\n");
656 ptlrpc_stop_all_threads(ost->ost_service);
657 ptlrpc_unregister_service(ost->ost_service);
659 err = obd_disconnect(&ost->ost_conn);
661 CERROR("lustre ost: fail to disconnect device\n");
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 class_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
681 static void __exit ost_exit(void)
683 class_unregister_type(LUSTRE_OST_NAME);
686 MODULE_AUTHOR("Cluster File Systems, Inc. <info@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);