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>
40 static int ost_destroy(struct ost_obd *ost, struct ptlrpc_request *req)
43 struct ost_body *body;
44 int rc, size = sizeof(*body);
47 body = lustre_msg_buf(req->rq_reqmsg, 0);
48 conn.oc_id = body->connid;
49 conn.oc_dev = ost->ost_tgt;
51 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
55 req->rq_status = obd_destroy(&conn, &body->oa);
59 static int ost_getattr(struct ost_obd *ost, struct ptlrpc_request *req)
62 struct ost_body *body, *repbody;
63 int rc, size = sizeof(*body);
66 body = lustre_msg_buf(req->rq_reqmsg, 0);
67 conn.oc_id = body->connid;
68 conn.oc_dev = ost->ost_tgt;
70 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
74 repbody = lustre_msg_buf(req->rq_repmsg, 0);
75 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
76 req->rq_status = obd_getattr(&conn, &repbody->oa);
80 static int ost_open(struct ost_obd *ost, struct ptlrpc_request *req)
83 struct ost_body *body, *repbody;
84 int rc, size = sizeof(*body);
87 body = lustre_msg_buf(req->rq_reqmsg, 0);
88 conn.oc_id = body->connid;
89 conn.oc_dev = ost->ost_tgt;
91 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
95 repbody = lustre_msg_buf(req->rq_repmsg, 0);
96 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
97 req->rq_status = obd_open(&conn, &repbody->oa);
101 static int ost_close(struct ost_obd *ost, struct ptlrpc_request *req)
103 struct obd_conn conn;
104 struct ost_body *body, *repbody;
105 int rc, size = sizeof(*body);
108 body = lustre_msg_buf(req->rq_reqmsg, 0);
109 conn.oc_id = body->connid;
110 conn.oc_dev = ost->ost_tgt;
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_close(&conn, &repbody->oa);
122 static int ost_create(struct ost_obd *ost, struct ptlrpc_request *req)
124 struct obd_conn conn;
125 struct ost_body *body, *repbody;
126 int rc, size = sizeof(*body);
129 body = lustre_msg_buf(req->rq_reqmsg, 0);
130 conn.oc_id = body->connid;
131 conn.oc_dev = ost->ost_tgt;
133 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
137 repbody = lustre_msg_buf(req->rq_repmsg, 0);
138 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
139 req->rq_status = obd_create(&conn, &repbody->oa);
143 static int ost_punch(struct ost_obd *ost, struct ptlrpc_request *req)
145 struct obd_conn conn;
146 struct ost_body *body, *repbody;
147 int rc, size = sizeof(*body);
150 body = lustre_msg_buf(req->rq_reqmsg, 0);
151 conn.oc_id = body->connid;
152 conn.oc_dev = ost->ost_tgt;
154 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
158 repbody = lustre_msg_buf(req->rq_repmsg, 0);
159 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
160 req->rq_status = obd_punch(&conn, &repbody->oa,
161 repbody->oa.o_size, repbody->oa.o_blocks);
165 static int ost_setattr(struct ost_obd *ost, struct ptlrpc_request *req)
167 struct obd_conn conn;
168 struct ost_body *body, *repbody;
169 int rc, size = sizeof(*body);
172 body = lustre_msg_buf(req->rq_reqmsg, 0);
173 conn.oc_id = body->connid;
174 conn.oc_dev = ost->ost_tgt;
176 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
180 repbody = lustre_msg_buf(req->rq_repmsg, 0);
181 memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
182 req->rq_status = obd_setattr(&conn, &repbody->oa);
186 static int ost_connect(struct ost_obd *ost, struct ptlrpc_request *req)
188 struct obd_conn conn;
189 struct ost_body *body;
190 int rc, size = sizeof(*body);
193 conn.oc_dev = ost->ost_tgt;
195 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
199 req->rq_status = obd_connect(&conn);
201 CDEBUG(D_IOCTL, "rep buffer %p, id %d\n", req->rq_repmsg, conn.oc_id);
202 body = lustre_msg_buf(req->rq_repmsg, 0);
203 body->connid = conn.oc_id;
207 static int ost_disconnect(struct ost_obd *ost, struct ptlrpc_request *req)
209 struct obd_conn conn;
210 struct ost_body *body;
211 int rc, size = sizeof(*body);
214 body = lustre_msg_buf(req->rq_reqmsg, 0);
215 conn.oc_id = body->connid;
216 conn.oc_dev = ost->ost_tgt;
218 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
222 CDEBUG(D_IOCTL, "Disconnecting %d\n", conn.oc_id);
223 req->rq_status = obd_disconnect(&conn);
227 static int ost_get_info(struct ost_obd *ost, struct ptlrpc_request *req)
229 struct obd_conn conn;
230 struct ost_body *body;
231 int rc, size[2] = {sizeof(*body)};
232 char *bufs[2] = {NULL, NULL}, *ptr;
235 body = lustre_msg_buf(req->rq_reqmsg, 0);
236 conn.oc_id = body->connid;
237 conn.oc_dev = ost->ost_tgt;
239 ptr = lustre_msg_buf(req->rq_reqmsg, 1);
243 req->rq_status = obd_get_info(&conn, req->rq_reqmsg->buflens[1], ptr,
244 &(size[1]), (void **)&(bufs[1]));
246 rc = lustre_pack_msg(2, size, bufs, &req->rq_replen, &req->rq_repmsg);
248 CERROR("cannot pack reply\n");
253 static int ost_brw_read(struct ost_obd *obddev, struct ptlrpc_request *req)
255 struct ptlrpc_bulk_desc *desc;
256 struct obd_conn conn;
257 void *tmp1, *tmp2, *end2;
258 struct niobuf_remote *remote_nb;
259 struct niobuf_local *local_nb = NULL;
260 struct obd_ioobj *ioo;
261 struct ost_body *body;
262 int rc, cmd, i, j, objcount, niocount, size = sizeof(*body);
265 body = lustre_msg_buf(req->rq_reqmsg, 0);
266 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
267 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
268 end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
269 objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
270 niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
273 conn.oc_id = body->connid;
274 conn.oc_dev = req->rq_obd->u.ost.ost_tgt;
276 for (i = 0; i < objcount; i++) {
277 ost_unpack_ioo(&tmp1, &ioo);
278 if (tmp2 + ioo->ioo_bufcnt > end2) {
280 GOTO(out, rc = -EFAULT);
282 for (j = 0; j < ioo->ioo_bufcnt; j++)
283 ost_unpack_niobuf(&tmp2, &remote_nb);
286 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
289 OBD_ALLOC(local_nb, sizeof(*local_nb) * niocount);
290 if (local_nb == NULL)
293 /* The unpackers move tmp1 and tmp2, so reset them before using */
294 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
295 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
296 req->rq_status = obd_preprw(cmd, &conn, objcount,
297 tmp1, niocount, tmp2, local_nb, NULL);
302 desc = ptlrpc_prep_bulk(req->rq_connection);
304 GOTO(out_local, rc = -ENOMEM);
305 desc->b_portal = OST_BULK_PORTAL;
307 for (i = 0; i < niocount; i++) {
308 struct ptlrpc_bulk_page *bulk;
309 bulk = ptlrpc_prep_bulk_page(desc);
311 GOTO(out_bulk, rc = -ENOMEM);
312 remote_nb = &(((struct niobuf_remote *)tmp2)[i]);
313 bulk->b_xid = remote_nb->xid;
314 bulk->b_buf = (void *)(unsigned long)local_nb[i].addr;
315 bulk->b_buflen = PAGE_SIZE;
318 rc = ptlrpc_send_bulk(desc);
322 wait_event_interruptible(desc->b_waitq, ptlrpc_check_bulk_sent(desc));
323 if (desc->b_flags & PTL_RPC_FL_INTR)
326 /* The unpackers move tmp1 and tmp2, so reset them before using */
327 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
328 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
329 req->rq_status = obd_commitrw(cmd, &conn, objcount,
330 tmp1, niocount, local_nb, NULL);
333 ptlrpc_free_bulk(desc);
335 OBD_FREE(local_nb, sizeof(*local_nb) * niocount);
340 static int ost_brw_write_cb(struct ptlrpc_bulk_page *bulk)
342 struct obd_ioobj obj;
343 struct niobuf_local lnb;
347 memset(&lnb, 0, sizeof(lnb));
348 memset(&obj, 0, sizeof(obj));
350 lnb.page = bulk->b_page;
351 lnb.dentry = bulk->b_dentry;
352 lnb.flags = bulk->b_flags;
355 rc = obd_commitrw(OBD_BRW_WRITE, &bulk->b_desc->b_conn, 1, &obj, 1,
356 &lnb, bulk->b_desc->b_desc_private);
358 CERROR("ost_commit_page failed: %d\n", rc);
363 static void ost_brw_write_finished_cb(struct ptlrpc_bulk_desc *desc, void *data)
366 ptlrpc_free_bulk(desc);
370 static int ost_brw_write(struct ost_obd *obddev, struct ptlrpc_request *req)
372 struct ptlrpc_bulk_desc *desc;
373 struct obd_conn conn;
374 struct niobuf_remote *remote_nb;
375 struct niobuf_local *local_nb, *lnb;
376 struct obd_ioobj *ioo;
377 struct ost_body *body;
378 int cmd, rc, i, j, objcount, niocount, size[2] = {sizeof(*body)};
379 void *tmp1, *tmp2, *end2;
380 void *desc_priv = NULL;
383 body = lustre_msg_buf(req->rq_reqmsg, 0);
384 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
385 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
386 end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
387 objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
388 niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
391 conn.oc_id = body->connid;
392 conn.oc_dev = req->rq_obd->u.ost.ost_tgt;
394 for (i = 0; i < objcount; i++) {
395 ost_unpack_ioo((void *)&tmp1, &ioo);
396 if (tmp2 + ioo->ioo_bufcnt > end2) {
400 for (j = 0; j < ioo->ioo_bufcnt; j++)
401 ost_unpack_niobuf((void *)&tmp2, &remote_nb);
404 size[1] = niocount * sizeof(*remote_nb);
405 rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
408 remote_nb = lustre_msg_buf(req->rq_repmsg, 1);
410 OBD_ALLOC(local_nb, niocount * sizeof(*local_nb));
411 if (local_nb == NULL)
412 GOTO(out, rc = -ENOMEM);
414 /* The unpackers move tmp1 and tmp2, so reset them before using */
415 tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
416 tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
417 req->rq_status = obd_preprw(cmd, &conn, objcount,
418 tmp1, niocount, tmp2, local_nb, &desc_priv);
420 GOTO(out_free, rc = 0); /* XXX is this correct? */
422 desc = ptlrpc_prep_bulk(req->rq_connection);
424 GOTO(fail_preprw, rc = -ENOMEM);
425 desc->b_cb = ost_brw_write_finished_cb;
426 desc->b_portal = OSC_BULK_PORTAL;
427 desc->b_desc_private = desc_priv;
428 memcpy(&(desc->b_conn), &conn, sizeof(conn));
430 for (i = 0, lnb = local_nb; i < niocount; i++, lnb++) {
431 struct ptlrpc_service *srv = req->rq_obd->u.ost.ost_service;
432 struct ptlrpc_bulk_page *bulk;
434 bulk = ptlrpc_prep_bulk_page(desc);
436 GOTO(fail_bulk, rc = -ENOMEM);
438 spin_lock(&srv->srv_lock);
439 bulk->b_xid = srv->srv_xid++;
440 spin_unlock(&srv->srv_lock);
442 bulk->b_buf = lnb->addr;
443 bulk->b_page = lnb->page;
444 bulk->b_flags = lnb->flags;
445 bulk->b_dentry = lnb->dentry;
446 bulk->b_buflen = PAGE_SIZE;
447 bulk->b_cb = ost_brw_write_cb;
449 /* this advances remote_nb */
450 ost_pack_niobuf((void **)&remote_nb, lnb->offset, lnb->len, 0,
454 rc = ptlrpc_register_bulk(desc);
460 OBD_FREE(local_nb, niocount * sizeof(*local_nb));
465 ptlrpc_free_bulk(desc);
467 /* FIXME: how do we undo the preprw? */
471 static int ost_brw(struct ost_obd *obddev, struct ptlrpc_request *req)
473 struct ost_body *body = lustre_msg_buf(req->rq_reqmsg, 0);
475 if (body->data == OBD_BRW_READ)
476 return ost_brw_read(obddev, req);
478 return ost_brw_write(obddev, req);
481 static int ost_handle(struct obd_device *obddev, struct ptlrpc_service *svc,
482 struct ptlrpc_request *req)
485 struct ost_obd *ost = &obddev->u.ost;
488 rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
489 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
490 CERROR("lustre_mds: Invalid request\n");
494 if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
495 CERROR("lustre_mds: wrong packet type sent %d\n",
496 req->rq_reqmsg->type);
497 GOTO(out, rc = -EINVAL);
500 switch (req->rq_reqmsg->opc) {
502 CDEBUG(D_INODE, "connect\n");
503 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
504 rc = ost_connect(ost, req);
507 CDEBUG(D_INODE, "disconnect\n");
508 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
509 rc = ost_disconnect(ost, req);
512 CDEBUG(D_INODE, "get_info\n");
513 OBD_FAIL_RETURN(OBD_FAIL_OST_GET_INFO_NET, 0);
514 rc = ost_get_info(ost, req);
517 CDEBUG(D_INODE, "create\n");
518 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
519 rc = ost_create(ost, req);
522 CDEBUG(D_INODE, "destroy\n");
523 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
524 rc = ost_destroy(ost, req);
527 CDEBUG(D_INODE, "getattr\n");
528 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
529 rc = ost_getattr(ost, req);
532 CDEBUG(D_INODE, "setattr\n");
533 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
534 rc = ost_setattr(ost, req);
537 CDEBUG(D_INODE, "setattr\n");
538 OBD_FAIL_RETURN(OBD_FAIL_OST_OPEN_NET, 0);
539 rc = ost_open(ost, req);
542 CDEBUG(D_INODE, "setattr\n");
543 OBD_FAIL_RETURN(OBD_FAIL_OST_CLOSE_NET, 0);
544 rc = ost_close(ost, req);
547 CDEBUG(D_INODE, "brw\n");
548 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
549 rc = ost_brw(ost, req);
552 CDEBUG(D_INODE, "punch\n");
553 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
554 rc = ost_punch(ost, req);
558 CDEBUG(D_INODE, "statfs\n");
559 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
560 rc = ost_statfs(ost, req);
564 req->rq_status = -ENOTSUPP;
565 rc = ptlrpc_error(svc, req);
571 //req->rq_status = rc;
573 CERROR("ost: processing error %d\n", rc);
574 ptlrpc_error(svc, req);
576 CDEBUG(D_INODE, "sending reply\n");
577 ptlrpc_reply(svc, req);
583 /* mount the file system (secretly) */
584 static int ost_setup(struct obd_device *obddev, obd_count len, void *buf)
586 struct obd_ioctl_data* data = buf;
587 struct ost_obd *ost = &obddev->u.ost;
588 struct obd_device *tgt;
592 if (data->ioc_dev < 0 || data->ioc_dev > MAX_OBD_DEVICES)
596 tgt = &obd_dev[data->ioc_dev];
598 if (!(tgt->obd_flags & OBD_ATTACHED) ||
599 !(tgt->obd_flags & OBD_SET_UP)) {
600 CERROR("device not attached or not set up (%d)\n",
602 GOTO(error_dec, err = -EINVAL);
605 ost->ost_conn.oc_dev = tgt;
606 err = obd_connect(&ost->ost_conn);
608 CERROR("fail to connect to device %d\n", data->ioc_dev);
609 GOTO(error_dec, err = -EINVAL);
612 ost->ost_service = ptlrpc_init_svc(128 * 1024,
613 OST_REQUEST_PORTAL, OSC_REPLY_PORTAL,
615 if (!ost->ost_service) {
616 CERROR("failed to start service\n");
617 GOTO(error_disc, err = -EINVAL);
620 err = ptlrpc_start_thread(obddev, ost->ost_service, "lustre_ost");
622 GOTO(error_disc, err = -EINVAL);
623 err = ptlrpc_start_thread(obddev, ost->ost_service, "lustre_ost");
625 GOTO(error_disc, err = -EINVAL);
630 obd_disconnect(&ost->ost_conn);
636 static int ost_cleanup(struct obd_device * obddev)
638 struct ost_obd *ost = &obddev->u.ost;
643 if ( !list_empty(&obddev->obd_gen_clients) ) {
644 CERROR("still has clients!\n");
648 ptlrpc_stop_all_threads(ost->ost_service);
649 rpc_unregister_service(ost->ost_service);
651 if (!list_empty(&ost->ost_service->srv_reqs)) {
652 // XXX reply with errors and clean up
653 CERROR("Request list not empty!\n");
655 OBD_FREE(ost->ost_service, sizeof(*ost->ost_service));
657 err = obd_disconnect(&ost->ost_conn);
659 CERROR("lustre ost: fail to disconnect device\n");
667 /* use obd ops to offer management infrastructure */
668 static struct obd_ops ost_obd_ops = {
670 o_cleanup: ost_cleanup,
673 static int __init ost_init(void)
675 obd_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
679 static void __exit ost_exit(void)
681 obd_unregister_type(LUSTRE_OST_NAME);
684 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
685 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
686 MODULE_LICENSE("GPL");
688 module_init(ost_init);
689 module_exit(ost_exit);