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_get_info(struct ptlrpc_request *req)
175 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
176 struct ost_body *body;
177 int rc, size[2] = {sizeof(*body)};
178 char *bufs[2] = {NULL, NULL}, *ptr;
181 body = lustre_msg_buf(req->rq_reqmsg, 0);
183 ptr = lustre_msg_buf(req->rq_reqmsg, 1);
187 req->rq_status = obd_get_info(conn, req->rq_reqmsg->buflens[1], ptr,
188 &(size[1]), (void **)&(bufs[1]));
190 rc = lustre_pack_msg(2, size, bufs, &req->rq_replen, &req->rq_repmsg);
192 CERROR("cannot pack reply\n");
197 static int ost_brw_read(struct ptlrpc_request *req)
199 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
200 struct ptlrpc_bulk_desc *desc;
201 void *tmp1, *tmp2, *end2;
202 struct niobuf_remote *remote_nb;
203 struct niobuf_local *local_nb = NULL;
204 struct obd_ioobj *ioo;
205 struct ost_body *body;
206 int rc, cmd, i, j, objcount, niocount, size = sizeof(*body);
209 body = lustre_msg_buf(req->rq_reqmsg, 0);
210 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
211 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
212 end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
213 objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
214 niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
217 for (i = 0; i < objcount; i++) {
218 ost_unpack_ioo(&tmp1, &ioo);
219 if (tmp2 + ioo->ioo_bufcnt > end2) {
221 GOTO(out, rc = -EFAULT);
223 for (j = 0; j < ioo->ioo_bufcnt; j++)
224 ost_unpack_niobuf(&tmp2, &remote_nb);
227 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
230 OBD_ALLOC(local_nb, sizeof(*local_nb) * niocount);
231 if (local_nb == NULL)
234 /* The unpackers move tmp1 and tmp2, so reset them before using */
235 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
236 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
237 req->rq_status = obd_preprw(cmd, conn, objcount,
238 tmp1, niocount, tmp2, local_nb, NULL);
243 desc = ptlrpc_prep_bulk(req->rq_connection);
245 GOTO(out_local, rc = -ENOMEM);
246 desc->b_portal = OST_BULK_PORTAL;
248 for (i = 0; i < niocount; i++) {
249 struct ptlrpc_bulk_page *bulk;
250 bulk = ptlrpc_prep_bulk_page(desc);
252 GOTO(out_bulk, rc = -ENOMEM);
253 remote_nb = &(((struct niobuf_remote *)tmp2)[i]);
254 bulk->b_xid = remote_nb->xid;
255 bulk->b_buf = (void *)(unsigned long)local_nb[i].addr;
256 bulk->b_buflen = PAGE_SIZE;
259 rc = ptlrpc_send_bulk(desc);
263 wait_event_interruptible(desc->b_waitq, ptlrpc_check_bulk_sent(desc));
264 if (desc->b_flags & PTL_RPC_FL_INTR)
267 /* The unpackers move tmp1 and tmp2, so reset them before using */
268 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
269 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
270 req->rq_status = obd_commitrw(cmd, conn, objcount,
271 tmp1, niocount, local_nb, NULL);
274 ptlrpc_free_bulk(desc);
276 OBD_FREE(local_nb, sizeof(*local_nb) * niocount);
279 ptlrpc_error(req->rq_svc, req);
281 ptlrpc_reply(req->rq_svc, req);
285 static int ost_brw_write(struct ptlrpc_request *req)
287 struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
288 struct ptlrpc_bulk_desc *desc;
289 struct niobuf_remote *remote_nb;
290 struct niobuf_local *local_nb, *lnb;
291 struct obd_ioobj *ioo;
292 struct ost_body *body;
293 int cmd, rc, i, j, objcount, niocount, size[2] = {sizeof(*body)};
294 void *tmp1, *tmp2, *end2;
295 void *desc_priv = NULL;
299 body = lustre_msg_buf(req->rq_reqmsg, 0);
300 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
301 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
302 end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
303 objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
304 niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
307 for (i = 0; i < objcount; i++) {
308 ost_unpack_ioo((void *)&tmp1, &ioo);
309 if (tmp2 + ioo->ioo_bufcnt > end2) {
313 for (j = 0; j < ioo->ioo_bufcnt; j++)
314 ost_unpack_niobuf((void *)&tmp2, &remote_nb);
317 size[1] = niocount * sizeof(*remote_nb);
318 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
321 remote_nb = lustre_msg_buf(req->rq_repmsg, 1);
323 OBD_ALLOC(local_nb, niocount * sizeof(*local_nb));
324 if (local_nb == NULL)
325 GOTO(out, rc = -ENOMEM);
327 /* The unpackers move tmp1 and tmp2, so reset them before using */
328 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
329 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
330 req->rq_status = obd_preprw(cmd, conn, objcount,
331 tmp1, niocount, tmp2, local_nb, &desc_priv);
333 GOTO(out_free, rc = 0); /* XXX is this correct? */
335 desc = ptlrpc_prep_bulk(req->rq_connection);
337 GOTO(fail_preprw, rc = -ENOMEM);
339 desc->b_portal = OSC_BULK_PORTAL;
340 desc->b_desc_private = desc_priv;
341 memcpy(&(desc->b_conn), &conn, sizeof(conn));
343 for (i = 0, lnb = local_nb; i < niocount; i++, lnb++) {
344 struct ptlrpc_service *srv = req->rq_obd->u.ost.ost_service;
345 struct ptlrpc_bulk_page *bulk;
347 bulk = ptlrpc_prep_bulk_page(desc);
349 GOTO(fail_bulk, rc = -ENOMEM);
351 spin_lock(&srv->srv_lock);
352 bulk->b_xid = srv->srv_xid++;
353 spin_unlock(&srv->srv_lock);
355 bulk->b_buf = lnb->addr;
356 bulk->b_page = lnb->page;
357 bulk->b_flags = lnb->flags;
358 bulk->b_dentry = lnb->dentry;
359 bulk->b_buflen = PAGE_SIZE;
362 /* this advances remote_nb */
363 ost_pack_niobuf((void **)&remote_nb, lnb->offset, lnb->len, 0,
367 rc = ptlrpc_register_bulk(desc);
372 ptlrpc_reply(req->rq_svc, req);
374 wait_event_interruptible(desc->b_waitq,
375 desc->b_flags & PTL_BULK_FL_RCVD);
377 rc = obd_commitrw(cmd, conn, objcount, tmp1, niocount, local_nb,
378 desc->b_desc_private);
379 ptlrpc_free_bulk(desc);
382 OBD_FREE(local_nb, niocount * sizeof(*local_nb));
386 ptlrpc_error(req->rq_svc, req);
388 ptlrpc_reply(req->rq_svc, req);
393 ptlrpc_free_bulk(desc);
395 /* FIXME: how do we undo the preprw? */
399 static int ost_brw(struct ptlrpc_request *req)
401 struct ost_body *body = lustre_msg_buf(req->rq_reqmsg, 0);
403 if (body->data & OBD_BRW_WRITE)
404 return ost_brw_write(req);
406 return ost_brw_read(req);
410 static int ost_handle(struct ptlrpc_request *req)
415 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
416 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
417 CERROR("lustre_mds: Invalid request\n");
421 if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
422 CERROR("lustre_mds: wrong packet type sent %d\n",
423 req->rq_reqmsg->type);
424 GOTO(out, rc = -EINVAL);
427 if (req->rq_reqmsg->opc != OST_CONNECT &&
428 req->rq_export == NULL)
429 GOTO(out, rc = -ENOTCONN);
431 if (strcmp(req->rq_obd->obd_type->typ_name, "ost") != 0)
432 GOTO(out, rc = -EINVAL);
434 switch (req->rq_reqmsg->opc) {
436 CDEBUG(D_INODE, "connect\n");
437 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
438 rc = target_handle_connect(req);
441 CDEBUG(D_INODE, "disconnect\n");
442 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
443 rc = target_handle_disconnect(req);
446 CDEBUG(D_INODE, "get_info\n");
447 OBD_FAIL_RETURN(OBD_FAIL_OST_GET_INFO_NET, 0);
448 rc = ost_get_info(req);
451 CDEBUG(D_INODE, "create\n");
452 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
453 rc = ost_create(req);
456 CDEBUG(D_INODE, "destroy\n");
457 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
458 rc = ost_destroy(req);
461 CDEBUG(D_INODE, "getattr\n");
462 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
463 rc = ost_getattr(req);
466 CDEBUG(D_INODE, "setattr\n");
467 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
468 rc = ost_setattr(req);
471 CDEBUG(D_INODE, "setattr\n");
472 OBD_FAIL_RETURN(OBD_FAIL_OST_OPEN_NET, 0);
476 CDEBUG(D_INODE, "setattr\n");
477 OBD_FAIL_RETURN(OBD_FAIL_OST_CLOSE_NET, 0);
481 CDEBUG(D_INODE, "brw\n");
482 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
484 /* ost_brw sends its own replies */
487 CDEBUG(D_INODE, "punch\n");
488 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
493 CDEBUG(D_INODE, "statfs\n");
494 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
495 rc = ost_statfs(req);
499 req->rq_status = -ENOTSUPP;
500 rc = ptlrpc_error(req->rq_svc, req);
506 //req->rq_status = rc;
508 CERROR("ost: processing error %d\n", rc);
509 ptlrpc_error(req->rq_svc, req);
511 CDEBUG(D_INODE, "sending reply\n");
512 ptlrpc_reply(req->rq_svc, req);
518 #define OST_NUM_THREADS 6
520 /* mount the file system (secretly) */
521 static int ost_setup(struct obd_device *obddev, obd_count len, void *buf)
523 struct obd_ioctl_data* data = buf;
524 struct ost_obd *ost = &obddev->u.ost;
525 struct obd_device *tgt;
530 if (data->ioc_dev < 0 || data->ioc_dev > MAX_OBD_DEVICES)
534 tgt = &obd_dev[data->ioc_dev];
535 if (!(tgt->obd_flags & OBD_ATTACHED) ||
536 !(tgt->obd_flags & OBD_SET_UP)) {
537 CERROR("device not attached or not set up (%d)\n",
539 GOTO(error_dec, err = -EINVAL);
542 err = obd_connect(&ost->ost_conn, tgt);
544 CERROR("fail to connect to device %d\n", data->ioc_dev);
545 GOTO(error_dec, err = -EINVAL);
548 obddev->obd_namespace =
549 ldlm_namespace_new("ost", LDLM_NAMESPACE_SERVER);
550 if (obddev->obd_namespace == NULL)
553 ost->ost_service = ptlrpc_init_svc(64 * 1024, OST_REQUEST_PORTAL,
554 OSC_REPLY_PORTAL, "self",ost_handle);
555 if (!ost->ost_service) {
556 CERROR("failed to start service\n");
557 GOTO(error_disc, err = -EINVAL);
560 for (i = 0; i < OST_NUM_THREADS; i++) {
561 err = ptlrpc_start_thread(obddev, ost->ost_service,
564 CERROR("error starting thread #%d: rc %d\n", i, err);
565 GOTO(error_disc, err = -EINVAL);
572 obd_disconnect(&ost->ost_conn);
578 static int ost_cleanup(struct obd_device * obddev)
580 struct ost_obd *ost = &obddev->u.ost;
585 if ( !list_empty(&obddev->obd_exports) ) {
586 CERROR("still has clients!\n");
590 ptlrpc_stop_all_threads(ost->ost_service);
591 ptlrpc_unregister_service(ost->ost_service);
593 err = obd_disconnect(&ost->ost_conn);
595 CERROR("lustre ost: fail to disconnect device\n");
599 ldlm_namespace_free(obddev->obd_namespace);
605 /* use obd ops to offer management infrastructure */
606 static struct obd_ops ost_obd_ops = {
608 o_cleanup: ost_cleanup,
611 static int __init ost_init(void)
613 obd_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
617 static void __exit ost_exit(void)
619 obd_unregister_type(LUSTRE_OST_NAME);
622 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
623 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
624 MODULE_LICENSE("GPL");
626 module_init(ost_init);
627 module_exit(ost_exit);