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>
41 #include <linux/lprocfs_status.h>
43 extern struct lprocfs_vars status_var_nm_1[];
44 extern struct lprocfs_vars status_class_var[];
46 static int ost_destroy(struct ptlrpc_request *req)
48 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
49 struct ost_body *body;
50 int rc, size = sizeof(*body);
53 body = lustre_msg_buf(req->rq_reqmsg, 0);
55 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
59 req->rq_status = obd_destroy(conn, &body->oa, NULL);
63 static int ost_getattr(struct ptlrpc_request *req)
65 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
66 struct ost_body *body, *repbody;
67 int rc, size = sizeof(*body);
70 body = lustre_msg_buf(req->rq_reqmsg, 0);
72 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
76 repbody = lustre_msg_buf(req->rq_repmsg, 0);
77 /* FIXME: unpack only valid fields instead of memcpy, endianness */
78 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
79 req->rq_status = obd_getattr(conn, &repbody->oa, NULL);
83 static int ost_statfs(struct ptlrpc_request *req)
85 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
86 struct obd_statfs *osfs;
87 int rc, size = sizeof(*osfs);
90 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
94 osfs = lustre_msg_buf(req->rq_repmsg, 0);
95 memset(osfs, 0, size);
97 rc = obd_statfs(conn, osfs);
99 CERROR("ost: statfs failed: rc %d\n", rc);
103 obd_statfs_pack(osfs, osfs);
108 static int ost_open(struct ptlrpc_request *req)
110 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
111 struct ost_body *body, *repbody;
112 int rc, size = sizeof(*body);
115 body = lustre_msg_buf(req->rq_reqmsg, 0);
117 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
121 repbody = lustre_msg_buf(req->rq_repmsg, 0);
122 /* FIXME: unpack only valid fields instead of memcpy, endianness */
123 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
124 req->rq_status = obd_open(conn, &repbody->oa, NULL);
128 static int ost_close(struct ptlrpc_request *req)
130 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
131 struct ost_body *body, *repbody;
132 int rc, size = sizeof(*body);
135 body = lustre_msg_buf(req->rq_reqmsg, 0);
137 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
141 repbody = lustre_msg_buf(req->rq_repmsg, 0);
142 /* FIXME: unpack only valid fields instead of memcpy, endianness */
143 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
144 req->rq_status = obd_close(conn, &repbody->oa, NULL);
148 static int ost_create(struct ptlrpc_request *req)
150 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
151 struct ost_body *body, *repbody;
152 int rc, size = sizeof(*body);
155 body = lustre_msg_buf(req->rq_reqmsg, 0);
157 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
161 repbody = lustre_msg_buf(req->rq_repmsg, 0);
162 /* FIXME: unpack only valid fields instead of memcpy, endianness */
163 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
164 req->rq_status = obd_create(conn, &repbody->oa, NULL);
168 static int ost_punch(struct ptlrpc_request *req)
170 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
171 struct ost_body *body, *repbody;
172 int rc, size = sizeof(*body);
175 body = lustre_msg_buf(req->rq_reqmsg, 0);
177 if ((NTOH__u32(body->oa.o_valid) & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))!=
178 (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
181 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
185 repbody = lustre_msg_buf(req->rq_repmsg, 0);
186 /* FIXME: unpack only valid fields instead of memcpy, endianness */
187 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
188 req->rq_status = obd_punch(conn, &repbody->oa, NULL,
189 repbody->oa.o_size, repbody->oa.o_blocks);
193 static int ost_setattr(struct ptlrpc_request *req)
195 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
196 struct ost_body *body, *repbody;
197 int rc, size = sizeof(*body);
200 body = lustre_msg_buf(req->rq_reqmsg, 0);
202 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
206 repbody = lustre_msg_buf(req->rq_repmsg, 0);
207 /* FIXME: unpack only valid fields instead of memcpy, endianness */
208 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
209 req->rq_status = obd_setattr(conn, &repbody->oa, NULL);
213 static int ost_bulk_timeout(void *data)
215 struct ptlrpc_bulk_desc *desc = data;
218 CERROR("(not yet) starting recovery of client %p\n", desc->bd_client);
222 static int ost_brw_read(struct ptlrpc_request *req)
224 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
225 struct ptlrpc_bulk_desc *desc;
226 void *tmp1, *tmp2, *end2;
227 struct niobuf_remote *remote_nb;
228 struct niobuf_local *local_nb = NULL;
229 struct obd_ioobj *ioo;
230 struct ost_body *body;
231 struct l_wait_info lwi;
232 void *desc_priv = NULL;
233 int rc, cmd, i, j, objcount, niocount, size = sizeof(*body);
236 body = lustre_msg_buf(req->rq_reqmsg, 0);
237 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
238 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
239 end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
240 objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
241 niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
244 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
247 for (i = 0; i < objcount; i++) {
248 ost_unpack_ioo(&tmp1, &ioo);
249 if (tmp2 + ioo->ioo_bufcnt > end2) {
251 GOTO(out, rc = -EFAULT);
253 for (j = 0; j < ioo->ioo_bufcnt; j++)
254 ost_unpack_niobuf(&tmp2, &remote_nb);
257 OBD_ALLOC(local_nb, sizeof(*local_nb) * niocount);
258 if (local_nb == NULL)
259 GOTO(out, rc = -ENOMEM);
261 /* The unpackers move tmp1 and tmp2, so reset them before using */
262 ioo = lustre_msg_buf(req->rq_reqmsg, 1);
263 remote_nb = lustre_msg_buf(req->rq_reqmsg, 2);
264 req->rq_status = obd_preprw(cmd, conn, objcount, ioo, niocount,
265 remote_nb, local_nb, &desc_priv);
270 desc = ptlrpc_prep_bulk(req->rq_connection);
272 GOTO(out_local, rc = -ENOMEM);
273 desc->bd_portal = OST_BULK_PORTAL;
275 for (i = 0; i < niocount; i++) {
276 struct ptlrpc_bulk_page *bulk = ptlrpc_prep_bulk_page(desc);
279 GOTO(out_bulk, rc = -ENOMEM);
280 bulk->bp_xid = remote_nb[i].xid;
281 bulk->bp_buf = local_nb[i].addr;
282 bulk->bp_buflen = remote_nb[i].len;
285 rc = ptlrpc_send_bulk(desc);
289 lwi = LWI_TIMEOUT(obd_timeout * HZ, ost_bulk_timeout, desc);
290 rc = l_wait_event(desc->bd_waitq, desc->bd_flags &PTL_BULK_FL_SENT, &lwi);
292 LASSERT(rc == -ETIMEDOUT);
296 req->rq_status = obd_commitrw(cmd, conn, objcount, ioo, niocount,
297 local_nb, desc_priv);
299 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
302 ptlrpc_free_bulk(desc);
304 OBD_FREE(local_nb, sizeof(*local_nb) * niocount);
307 /* It's a lot of work to delay allocating the reply, and a lot
308 * less work to just free it here. */
309 OBD_FREE(req->rq_repmsg, req->rq_replen);
310 req->rq_repmsg = NULL;
311 ptlrpc_error(req->rq_svc, req);
313 ptlrpc_reply(req->rq_svc, req);
317 static int ost_brw_write(struct ptlrpc_request *req)
319 struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
320 struct ptlrpc_bulk_desc *desc;
321 struct niobuf_remote *remote_nb;
322 struct niobuf_local *local_nb, *lnb;
323 struct obd_ioobj *ioo;
324 struct ost_body *body;
325 int cmd, rc, i, j, objcount, niocount, size[2] = {sizeof(*body)};
326 void *tmp1, *tmp2, *end2;
327 void *desc_priv = NULL;
329 struct ptlrpc_service *srv;
330 struct l_wait_info lwi;
334 body = lustre_msg_buf(req->rq_reqmsg, 0);
335 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
336 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
337 end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
338 objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
339 niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
342 for (i = 0; i < objcount; i++) {
343 ost_unpack_ioo((void *)&tmp1, &ioo);
344 if (tmp2 + ioo->ioo_bufcnt > end2) {
348 for (j = 0; j < ioo->ioo_bufcnt; j++)
349 ost_unpack_niobuf((void *)&tmp2, &remote_nb);
352 size[1] = niocount * sizeof(*remote_nb);
353 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
356 remote_nb = lustre_msg_buf(req->rq_repmsg, 1);
358 OBD_ALLOC(local_nb, niocount * sizeof(*local_nb));
359 if (local_nb == NULL)
360 GOTO(out, rc = -ENOMEM);
362 /* The unpackers move tmp1 and tmp2, so reset them before using */
363 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
364 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
365 req->rq_status = obd_preprw(cmd, conn, objcount, tmp1, niocount, tmp2,
366 local_nb, &desc_priv);
368 GOTO(out_free, rc = 0); /* XXX is this correct? */
370 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
371 GOTO(fail_preprw, rc = 0);
373 desc = ptlrpc_prep_bulk(req->rq_connection);
375 GOTO(fail_preprw, rc = -ENOMEM);
376 desc->bd_ptl_ev_hdlr = NULL;
377 desc->bd_portal = OSC_BULK_PORTAL;
378 desc->bd_desc_private = desc_priv;
379 memcpy(&(desc->bd_conn), &conn, sizeof(conn));
381 srv = req->rq_obd->u.ost.ost_service;
382 spin_lock(&srv->srv_lock);
383 xid = srv->srv_xid++; /* single xid for all pages */
384 spin_unlock(&srv->srv_lock);
386 for (i = 0, lnb = local_nb; i < niocount; i++, lnb++) {
387 struct ptlrpc_bulk_page *bulk;
389 bulk = ptlrpc_prep_bulk_page(desc);
391 GOTO(fail_bulk, rc = -ENOMEM);
393 bulk->bp_xid = xid; /* single xid for all pages */
395 bulk->bp_buf = lnb->addr;
396 bulk->bp_page = lnb->page;
397 bulk->bp_flags = lnb->flags;
398 bulk->bp_dentry = lnb->dentry;
399 bulk->bp_buflen = lnb->len;
402 /* this advances remote_nb */
403 ost_pack_niobuf((void **)&remote_nb, lnb->offset, lnb->len, 0,
407 rc = ptlrpc_register_bulk(desc);
412 ptlrpc_reply(req->rq_svc, req);
414 lwi = LWI_TIMEOUT(obd_timeout * HZ, ost_bulk_timeout, desc);
415 rc = l_wait_event(desc->bd_waitq, desc->bd_flags & PTL_BULK_FL_RCVD,
418 if (rc != -ETIMEDOUT)
423 rc = obd_commitrw(cmd, conn, objcount, tmp1, niocount, local_nb,
424 desc->bd_desc_private);
425 ptlrpc_free_bulk(desc);
428 OBD_FREE(local_nb, niocount * sizeof(*local_nb));
432 OBD_FREE(req->rq_repmsg, req->rq_replen);
433 req->rq_repmsg = NULL;
434 ptlrpc_error(req->rq_svc, req);
436 ptlrpc_reply(req->rq_svc, req);
441 ptlrpc_free_bulk(desc);
443 /* FIXME: how do we undo the preprw? */
447 static int ost_handle(struct ptlrpc_request *req)
452 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
453 if (rc || OBD_FAIL_CHECK(OBD_FAIL_OST_HANDLE_UNPACK)) {
454 CERROR("lustre_ost: Invalid request\n");
458 if (req->rq_reqmsg->opc != OST_CONNECT &&
459 req->rq_export == NULL) {
460 CERROR("lustre_ost: operation %d on unconnected OST\n",
461 req->rq_reqmsg->opc);
462 GOTO(out, rc = -ENOTCONN);
465 if (strcmp(req->rq_obd->obd_type->typ_name, "ost") != 0)
466 GOTO(out, rc = -EINVAL);
468 switch (req->rq_reqmsg->opc) {
470 CDEBUG(D_INODE, "connect\n");
471 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
472 rc = target_handle_connect(req);
475 CDEBUG(D_INODE, "disconnect\n");
476 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
477 rc = target_handle_disconnect(req);
480 CDEBUG(D_INODE, "create\n");
481 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
482 rc = ost_create(req);
485 CDEBUG(D_INODE, "destroy\n");
486 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
487 rc = ost_destroy(req);
490 CDEBUG(D_INODE, "getattr\n");
491 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
492 rc = ost_getattr(req);
495 CDEBUG(D_INODE, "setattr\n");
496 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
497 rc = ost_setattr(req);
500 CDEBUG(D_INODE, "open\n");
501 OBD_FAIL_RETURN(OBD_FAIL_OST_OPEN_NET, 0);
505 CDEBUG(D_INODE, "close\n");
506 OBD_FAIL_RETURN(OBD_FAIL_OST_CLOSE_NET, 0);
510 CDEBUG(D_INODE, "write\n");
511 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
512 rc = ost_brw_write(req);
513 /* ost_brw sends its own replies */
516 CDEBUG(D_INODE, "read\n");
517 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
518 rc = ost_brw_read(req);
519 /* ost_brw sends its own replies */
522 CDEBUG(D_INODE, "punch\n");
523 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
527 CDEBUG(D_INODE, "statfs\n");
528 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
529 rc = ost_statfs(req);
532 CDEBUG(D_INODE, "enqueue\n");
533 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
534 rc = ldlm_handle_enqueue(req);
539 CDEBUG(D_INODE, "convert\n");
540 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
541 rc = ldlm_handle_convert(req);
546 CDEBUG(D_INODE, "cancel\n");
547 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
548 rc = ldlm_handle_cancel(req);
552 case LDLM_BL_CALLBACK:
553 case LDLM_CP_CALLBACK:
554 CDEBUG(D_INODE, "callback\n");
555 CERROR("callbacks should not happen on OST\n");
557 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
560 req->rq_status = -ENOTSUPP;
561 rc = ptlrpc_error(req->rq_svc, req);
567 //req->rq_status = rc;
569 CERROR("ost: processing error (opcode=%d): %d\n",
570 req->rq_reqmsg->opc, rc);
571 ptlrpc_error(req->rq_svc, req);
573 CDEBUG(D_INODE, "sending reply\n");
574 if (req->rq_repmsg == NULL)
575 CERROR("handler for opcode %d returned rc=0 without "
576 "creating rq_repmsg; needs to return rc != "
577 "0!\n", req->rq_reqmsg->opc);
578 ptlrpc_reply(req->rq_svc, req);
584 #define OST_NUM_THREADS 6
586 /* mount the file system (secretly) */
587 static int ost_setup(struct obd_device *obddev, obd_count len, void *buf)
589 struct obd_ioctl_data* data = buf;
590 struct ost_obd *ost = &obddev->u.ost;
591 struct obd_device *tgt;
596 if (data->ioc_inllen1 < 1) {
597 CERROR("requires a TARGET OBD UUID\n");
600 if (data->ioc_inllen1 > 37) {
601 CERROR("OBD UUID must be less than 38 characters\n");
606 tgt = class_uuid2obd(data->ioc_inlbuf1);
607 if (!tgt || !(tgt->obd_flags & OBD_ATTACHED) ||
608 !(tgt->obd_flags & OBD_SET_UP)) {
609 CERROR("device not attached or not set up (%d)\n",
611 GOTO(error_dec, err = -EINVAL);
614 err = obd_connect(&ost->ost_conn, tgt, NULL, NULL, NULL);
616 CERROR("fail to connect to device %d\n", data->ioc_dev);
617 GOTO(error_dec, err = -EINVAL);
620 ost->ost_service = ptlrpc_init_svc(OST_NEVENTS, OST_NBUFS,
621 OST_BUFSIZE, OST_MAXREQSIZE,
622 OST_REQUEST_PORTAL, OSC_REPLY_PORTAL,
623 "self", ost_handle, "ost");
624 if (!ost->ost_service) {
625 CERROR("failed to start service\n");
626 GOTO(error_disc, err = -EINVAL);
629 for (i = 0; i < OST_NUM_THREADS; i++) {
631 sprintf(name, "lustre_ost_%02d", i);
632 err = ptlrpc_start_thread(obddev, ost->ost_service, name);
634 CERROR("error starting thread #%d: rc %d\n", i, err);
635 GOTO(error_disc, err = -EINVAL);
642 obd_disconnect(&ost->ost_conn);
648 static int ost_cleanup(struct obd_device * obddev)
650 struct ost_obd *ost = &obddev->u.ost;
655 if ( !list_empty(&obddev->obd_exports) ) {
656 CERROR("still has clients!\n");
660 ptlrpc_stop_all_threads(ost->ost_service);
661 ptlrpc_unregister_service(ost->ost_service);
663 err = obd_disconnect(&ost->ost_conn);
665 CERROR("lustre ost: fail to disconnect device\n");
672 int ost_attach(struct obd_device *dev,
673 obd_count len, void *data)
675 /* lprocfs_reg_dev(dev, (lprocfs_group_t*)lprocfs_ptlrpc_nm,
676 sizeof(struct lprofiler_ptlrpc));
678 lprocfs_reg_obd(dev, (struct lprocfs_vars*)status_var_nm_1,
683 int ost_detach(struct obd_device *dev)
685 /* lprocfs_dereg_dev(dev); */
686 lprocfs_dereg_obd(dev);
693 /* use obd ops to offer management infrastructure */
694 static struct obd_ops ost_obd_ops = {
695 o_attach: ost_attach,
696 o_detach: ost_detach,
698 o_cleanup: ost_cleanup,
701 static int __init ost_init(void)
705 rc = class_register_type(&ost_obd_ops,
706 (struct lprocfs_vars*)status_class_var,
714 static void __exit ost_exit(void)
717 class_unregister_type(LUSTRE_OST_NAME);
720 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
721 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
722 MODULE_LICENSE("GPL");
724 module_init(ost_init);
725 module_exit(ost_exit);