Whamcloud - gitweb
Don't update your trees yet..... Brian and I are trying to get our
[fs/lustre-release.git] / lustre / ost / ost_handler.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
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.
13  *
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.
18  *
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.
22  *
23  *  Storage Target Handling functions
24  *  Lustre Object Server Module (OST)
25  *
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.)
31  */
32
33 #define EXPORT_SYMTAB
34 #define DEBUG_SUBSYSTEM S_OST
35
36 #include <linux/module.h>
37 #include <linux/obd_ost.h>
38 #include <linux/lustre_net.h>
39 #include <linux/lustre_dlm.h>
40
41 static int ost_destroy(struct ptlrpc_request *req)
42 {
43         struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
44         struct ost_body *body;
45         int rc, size = sizeof(*body);
46         ENTRY;
47
48         body = lustre_msg_buf(req->rq_reqmsg, 0);
49
50         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
51         if (rc)
52                 RETURN(rc);
53
54         req->rq_status = obd_destroy(conn, &body->oa);
55         RETURN(0);
56 }
57
58 static int ost_getattr(struct ptlrpc_request *req)
59 {
60         struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
61         struct ost_body *body, *repbody;
62         int rc, size = sizeof(*body);
63         ENTRY;
64
65         body = lustre_msg_buf(req->rq_reqmsg, 0);
66
67         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
68         if (rc)
69                 RETURN(rc);
70
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);
74         RETURN(0);
75 }
76
77 static int ost_open(struct ptlrpc_request *req)
78 {
79         struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
80         struct ost_body *body, *repbody;
81         int rc, size = sizeof(*body);
82         ENTRY;
83
84         body = lustre_msg_buf(req->rq_reqmsg, 0);
85
86         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
87         if (rc)
88                 RETURN(rc);
89
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);
93         RETURN(0);
94 }
95
96 static int ost_close(struct ptlrpc_request *req)
97 {
98         struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
99         struct ost_body *body, *repbody;
100         int rc, size = sizeof(*body);
101         ENTRY;
102
103         body = lustre_msg_buf(req->rq_reqmsg, 0);
104
105         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
106         if (rc)
107                 RETURN(rc);
108
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);
112         RETURN(0);
113 }
114
115 static int ost_create(struct ptlrpc_request *req)
116 {
117         struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
118         struct ost_body *body, *repbody;
119         int rc, size = sizeof(*body);
120         ENTRY;
121
122         body = lustre_msg_buf(req->rq_reqmsg, 0);
123
124         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
125         if (rc)
126                 RETURN(rc);
127
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);
131         RETURN(0);
132 }
133
134 static int ost_punch(struct ptlrpc_request *req)
135 {
136         struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
137         struct ost_body *body, *repbody;
138         int rc, size = sizeof(*body);
139         ENTRY;
140
141         body = lustre_msg_buf(req->rq_reqmsg, 0);
142
143         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
144         if (rc)
145                 RETURN(rc);
146
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);
151         RETURN(0);
152 }
153
154 static int ost_setattr(struct ptlrpc_request *req)
155 {
156         struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
157         struct ost_body *body, *repbody;
158         int rc, size = sizeof(*body);
159         ENTRY;
160
161         body = lustre_msg_buf(req->rq_reqmsg, 0);
162
163         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
164         if (rc)
165                 RETURN(rc);
166
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);
170         RETURN(0);
171 }
172
173 static int ost_connect(struct ptlrpc_request *req)
174 {
175         struct ost_body *body;
176         struct obd_device *target;
177         struct obd_conn conn;
178         char *uuid;
179         int rc, size = sizeof(*body), i;
180         ENTRY;
181
182         uuid = lustre_msg_buf(req->rq_reqmsg, 0);
183         if (req->rq_reqmsg->buflens[0] > 37) {
184                 /* Invalid UUID */
185                 req->rq_status = -EINVAL;
186                 RETURN(0);
187         }
188
189         i = obd_class_uuid2dev(uuid);
190         if (i == -1) {
191                 req->rq_status = -ENODEV;
192                 RETURN(0);
193         }
194
195         target = &obd_dev[i];
196         if (!target) {
197                 req->rq_status = -ENODEV;
198                 RETURN(0);
199         }
200
201         conn.addr = req->rq_reqmsg->conn;
202         conn.cookie = req->rq_reqmsg->token;
203
204         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
205         if (rc)
206                 RETURN(rc);
207
208         req->rq_status = obd_connect(&conn, target);
209         req->rq_repmsg->conn = conn.addr;
210         req->rq_repmsg->token = conn.cookie;
211
212         CDEBUG(D_IOCTL, "rep buffer %p, id %d\n", req->rq_repmsg, conn.oc_id);
213         body = lustre_msg_buf(req->rq_repmsg, 0);
214         body->connid = conn.oc_id;
215         RETURN(0);
216 }
217
218 static int ost_disconnect(struct ptlrpc_request *req)
219 {
220         struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
221         struct ost_body *body;
222         int rc;
223         ENTRY;
224
225         body = lustre_msg_buf(req->rq_reqmsg, 0);
226
227         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
228         if (rc)
229                 RETURN(rc);
230
231         req->rq_status = obd_disconnect(conn);
232         RETURN(0);
233 }
234
235 static int ost_get_info(struct ptlrpc_request *req)
236 {
237         struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
238         struct ost_body *body;
239         int rc, size[2] = {sizeof(*body)};
240         char *bufs[2] = {NULL, NULL}, *ptr;
241         ENTRY;
242
243         body = lustre_msg_buf(req->rq_reqmsg, 0);
244
245         ptr = lustre_msg_buf(req->rq_reqmsg, 1);
246         if (!ptr)
247                 RETURN(-EINVAL);
248
249         req->rq_status = obd_get_info(conn, req->rq_reqmsg->buflens[1], ptr,
250                                       &(size[1]), (void **)&(bufs[1]));
251
252         rc = lustre_pack_msg(2, size, bufs, &req->rq_replen, &req->rq_repmsg);
253         if (rc)
254                 CERROR("cannot pack reply\n");
255
256         RETURN(rc);
257 }
258
259 static int ost_brw_read(struct ptlrpc_request *req)
260 {
261         struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
262         struct ptlrpc_bulk_desc *desc;
263         void *tmp1, *tmp2, *end2;
264         struct niobuf_remote *remote_nb;
265         struct niobuf_local *local_nb = NULL;
266         struct obd_ioobj *ioo;
267         struct ost_body *body;
268         int rc, cmd, i, j, objcount, niocount, size = sizeof(*body);
269         ENTRY;
270
271         body = lustre_msg_buf(req->rq_reqmsg, 0);
272         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
273         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
274         end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
275         objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
276         niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
277         cmd = body->data;
278
279         for (i = 0; i < objcount; i++) {
280                 ost_unpack_ioo(&tmp1, &ioo);
281                 if (tmp2 + ioo->ioo_bufcnt > end2) {
282                         LBUG();
283                         GOTO(out, rc = -EFAULT);
284                 }
285                 for (j = 0; j < ioo->ioo_bufcnt; j++)
286                         ost_unpack_niobuf(&tmp2, &remote_nb);
287         }
288
289         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
290         if (rc)
291                 RETURN(rc);
292         OBD_ALLOC(local_nb, sizeof(*local_nb) * niocount);
293         if (local_nb == NULL)
294                 RETURN(-ENOMEM);
295
296         /* The unpackers move tmp1 and tmp2, so reset them before using */
297         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
298         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
299         req->rq_status = obd_preprw(cmd, conn, objcount,
300                                     tmp1, niocount, tmp2, local_nb, NULL);
301
302         if (req->rq_status)
303                 GOTO(out_local, 0);
304
305         desc = ptlrpc_prep_bulk(req->rq_connection);
306         if (desc == NULL)
307                 GOTO(out_local, rc = -ENOMEM);
308         desc->b_portal = OST_BULK_PORTAL;
309
310         for (i = 0; i < niocount; i++) {
311                 struct ptlrpc_bulk_page *bulk;
312                 bulk = ptlrpc_prep_bulk_page(desc);
313                 if (bulk == NULL)
314                         GOTO(out_bulk, rc = -ENOMEM);
315                 remote_nb = &(((struct niobuf_remote *)tmp2)[i]);
316                 bulk->b_xid = remote_nb->xid;
317                 bulk->b_buf = (void *)(unsigned long)local_nb[i].addr;
318                 bulk->b_buflen = PAGE_SIZE;
319         }
320
321         rc = ptlrpc_send_bulk(desc);
322         if (rc)
323                 GOTO(out_bulk, rc);
324
325         wait_event_interruptible(desc->b_waitq, ptlrpc_check_bulk_sent(desc));
326         if (desc->b_flags & PTL_RPC_FL_INTR)
327                 rc = -EINTR;
328
329         /* The unpackers move tmp1 and tmp2, so reset them before using */
330         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
331         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
332         req->rq_status = obd_commitrw(cmd, conn, objcount,
333                                       tmp1, niocount, local_nb, NULL);
334
335 out_bulk:
336         ptlrpc_free_bulk(desc);
337 out_local:
338         OBD_FREE(local_nb, sizeof(*local_nb) * niocount);
339 out:
340         if (rc)
341                 ptlrpc_error(req->rq_svc, req);
342         else
343                 ptlrpc_reply(req->rq_svc, req);
344         RETURN(rc);
345 }
346
347 static int ost_brw_write(struct ptlrpc_request *req)
348 {
349         struct obd_conn *conn = (struct obd_conn *)req->rq_reqmsg;
350         struct ptlrpc_bulk_desc *desc;
351         struct niobuf_remote *remote_nb;
352         struct niobuf_local *local_nb, *lnb;
353         struct obd_ioobj *ioo;
354         struct ost_body *body;
355         int cmd, rc, i, j, objcount, niocount, size[2] = {sizeof(*body)};
356         void *tmp1, *tmp2, *end2;
357         void *desc_priv = NULL;
358         int reply_sent = 0;
359         ENTRY;
360
361         body = lustre_msg_buf(req->rq_reqmsg, 0);
362         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
363         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
364         end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
365         objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
366         niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
367         cmd = body->data;
368
369         for (i = 0; i < objcount; i++) {
370                 ost_unpack_ioo((void *)&tmp1, &ioo);
371                 if (tmp2 + ioo->ioo_bufcnt > end2) {
372                         rc = -EFAULT;
373                         break;
374                 }
375                 for (j = 0; j < ioo->ioo_bufcnt; j++)
376                         ost_unpack_niobuf((void *)&tmp2, &remote_nb);
377         }
378
379         size[1] = niocount * sizeof(*remote_nb);
380         rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
381         if (rc)
382                 GOTO(out, rc);
383         remote_nb = lustre_msg_buf(req->rq_repmsg, 1);
384
385         OBD_ALLOC(local_nb, niocount * sizeof(*local_nb));
386         if (local_nb == NULL)
387                 GOTO(out, rc = -ENOMEM);
388
389         /* The unpackers move tmp1 and tmp2, so reset them before using */
390         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
391         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
392         req->rq_status = obd_preprw(cmd, conn, objcount,
393                                     tmp1, niocount, tmp2, local_nb, &desc_priv);
394         if (req->rq_status)
395                 GOTO(out_free, rc = 0); /* XXX is this correct? */
396
397         desc = ptlrpc_prep_bulk(req->rq_connection);
398         if (desc == NULL)
399                 GOTO(fail_preprw, rc = -ENOMEM);
400         desc->b_cb = NULL;
401         desc->b_portal = OSC_BULK_PORTAL;
402         desc->b_desc_private = desc_priv;
403         memcpy(&(desc->b_conn), &conn, sizeof(conn));
404
405         for (i = 0, lnb = local_nb; i < niocount; i++, lnb++) {
406                 struct ptlrpc_service *srv = req->rq_obd->u.ost.ost_service;
407                 struct ptlrpc_bulk_page *bulk;
408
409                 bulk = ptlrpc_prep_bulk_page(desc);
410                 if (bulk == NULL)
411                         GOTO(fail_bulk, rc = -ENOMEM);
412
413                 spin_lock(&srv->srv_lock);
414                 bulk->b_xid = srv->srv_xid++;
415                 spin_unlock(&srv->srv_lock);
416
417                 bulk->b_buf = lnb->addr;
418                 bulk->b_page = lnb->page;
419                 bulk->b_flags = lnb->flags;
420                 bulk->b_dentry = lnb->dentry;
421                 bulk->b_buflen = PAGE_SIZE;
422                 bulk->b_cb = NULL;
423
424                 /* this advances remote_nb */
425                 ost_pack_niobuf((void **)&remote_nb, lnb->offset, lnb->len, 0,
426                                 bulk->b_xid);
427         }
428
429         rc = ptlrpc_register_bulk(desc);
430         if (rc)
431                 GOTO(fail_bulk, rc);
432
433         reply_sent = 1;
434         ptlrpc_reply(req->rq_svc, req);
435
436         wait_event_interruptible(desc->b_waitq,
437                                  desc->b_flags & PTL_BULK_FL_RCVD);
438
439         rc = obd_commitrw(cmd, conn, objcount, tmp1, niocount, local_nb,
440                           desc->b_desc_private);
441         ptlrpc_free_bulk(desc);
442         EXIT;
443 out_free:
444         OBD_FREE(local_nb, niocount * sizeof(*local_nb));
445 out:
446         if (!reply_sent) {
447                 if (rc)
448                         ptlrpc_error(req->rq_svc, req);
449                 else
450                         ptlrpc_reply(req->rq_svc, req);
451         }
452         return rc;
453
454 fail_bulk:
455         ptlrpc_free_bulk(desc);
456 fail_preprw:
457         /* FIXME: how do we undo the preprw? */
458         goto out_free;
459 }
460
461 static int ost_brw(struct ptlrpc_request *req)
462 {
463         struct ost_body *body = lustre_msg_buf(req->rq_reqmsg, 0);
464
465         if (body->data & OBD_BRW_WRITE)
466                 return ost_brw_write(req);
467         else
468                 return ost_brw_read(req);
469 }
470
471
472 static int ost_handle(struct ptlrpc_request *req)
473 {
474         int rc;
475         ENTRY;
476
477         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
478         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
479                 CERROR("lustre_mds: Invalid request\n");
480                 GOTO(out, rc);
481         }
482
483         if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
484                 CERROR("lustre_mds: wrong packet type sent %d\n",
485                        req->rq_reqmsg->type);
486                 GOTO(out, rc = -EINVAL);
487         }
488
489         if (req->rq_reqmsg->opc != OST_CONNECT) {
490                 struct obd_export *export;
491                 export = gen_client((struct obd_conn *) req->rq_reqmsg); 
492                 if (!export) 
493                         GOTO(out, rc = -ENOTCONN);
494                 if (strcmp(req->rq_obd->obd_type->typ_name, "ost") != 0)
495                         GOTO(out, rc = -EINVAL);
496         }
497
498         switch (req->rq_reqmsg->opc) {
499         case OST_CONNECT:
500                 CDEBUG(D_INODE, "connect\n");
501                 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
502                 rc = ost_connect(req);
503                 break;
504         case OST_DISCONNECT:
505                 CDEBUG(D_INODE, "disconnect\n");
506                 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
507                 rc = ost_disconnect(req);
508                 break;
509         case OST_GET_INFO:
510                 CDEBUG(D_INODE, "get_info\n");
511                 OBD_FAIL_RETURN(OBD_FAIL_OST_GET_INFO_NET, 0);
512                 rc = ost_get_info(req);
513                 break;
514         case OST_CREATE:
515                 CDEBUG(D_INODE, "create\n");
516                 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
517                 rc = ost_create(req);
518                 break;
519         case OST_DESTROY:
520                 CDEBUG(D_INODE, "destroy\n");
521                 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
522                 rc = ost_destroy(req);
523                 break;
524         case OST_GETATTR:
525                 CDEBUG(D_INODE, "getattr\n");
526                 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
527                 rc = ost_getattr(req);
528                 break;
529         case OST_SETATTR:
530                 CDEBUG(D_INODE, "setattr\n");
531                 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
532                 rc = ost_setattr(req);
533                 break;
534         case OST_OPEN:
535                 CDEBUG(D_INODE, "setattr\n");
536                 OBD_FAIL_RETURN(OBD_FAIL_OST_OPEN_NET, 0);
537                 rc = ost_open(req);
538                 break;
539         case OST_CLOSE:
540                 CDEBUG(D_INODE, "setattr\n");
541                 OBD_FAIL_RETURN(OBD_FAIL_OST_CLOSE_NET, 0);
542                 rc = ost_close(req);
543                 break;
544         case OST_BRW:
545                 CDEBUG(D_INODE, "brw\n");
546                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
547                 rc = ost_brw(req);
548                 /* ost_brw sends its own replies */
549                 RETURN(rc);
550         case OST_PUNCH:
551                 CDEBUG(D_INODE, "punch\n");
552                 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
553                 rc = ost_punch(req);
554                 break;
555 #if 0
556         case OST_STATFS:
557                 CDEBUG(D_INODE, "statfs\n");
558                 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
559                 rc = ost_statfs(req);
560                 break;
561 #endif
562         default:
563                 req->rq_status = -ENOTSUPP;
564                 rc = ptlrpc_error(req->rq_svc, req);
565                 RETURN(rc);
566         }
567
568         EXIT;
569 out:
570         //req->rq_status = rc;
571         if (rc) {
572                 CERROR("ost: processing error %d\n", rc);
573                 ptlrpc_error(req->rq_svc, req);
574         } else {
575                 CDEBUG(D_INODE, "sending reply\n");
576                 ptlrpc_reply(req->rq_svc, req);
577         }
578
579         return 0;
580 }
581
582 #define OST_NUM_THREADS 6
583
584 /* mount the file system (secretly) */
585 static int ost_setup(struct obd_device *obddev, obd_count len, void *buf)
586 {
587         struct obd_ioctl_data* data = buf;
588         struct ost_obd *ost = &obddev->u.ost;
589         struct obd_device *tgt;
590         int err;
591         int i;
592         ENTRY;
593
594         if (data->ioc_dev < 0 || data->ioc_dev > MAX_OBD_DEVICES)
595                 RETURN(-ENODEV);
596
597         MOD_INC_USE_COUNT;
598         tgt = &obd_dev[data->ioc_dev];
599         if (!(tgt->obd_flags & OBD_ATTACHED) ||
600             !(tgt->obd_flags & OBD_SET_UP)) {
601                 CERROR("device not attached or not set up (%d)\n",
602                        data->ioc_dev);
603                 GOTO(error_dec, err = -EINVAL);
604         }
605
606         err = obd_connect(&ost->ost_conn, tgt);
607         if (err) {
608                 CERROR("fail to connect to device %d\n", data->ioc_dev);
609                 GOTO(error_dec, err = -EINVAL);
610         }
611
612         obddev->obd_namespace =
613                 ldlm_namespace_new("ost", LDLM_NAMESPACE_SERVER);
614         if (obddev->obd_namespace == NULL)
615                 LBUG();
616
617         ost->ost_service = ptlrpc_init_svc(64 * 1024, OST_REQUEST_PORTAL,
618                                            OSC_REPLY_PORTAL, "self",ost_handle);
619         if (!ost->ost_service) {
620                 CERROR("failed to start service\n");
621                 GOTO(error_disc, err = -EINVAL);
622         }
623
624         for (i = 0; i < OST_NUM_THREADS; i++) {
625                 err = ptlrpc_start_thread(obddev, ost->ost_service,
626                                           "lustre_ost");
627                 if (err) {
628                         CERROR("error starting thread #%d: rc %d\n", i, err);
629                         GOTO(error_disc, err = -EINVAL);
630                 }
631         }
632
633         RETURN(0);
634
635 error_disc:
636         obd_disconnect(&ost->ost_conn);
637 error_dec:
638         MOD_DEC_USE_COUNT;
639         RETURN(err);
640 }
641
642 static int ost_cleanup(struct obd_device * obddev)
643 {
644         struct ost_obd *ost = &obddev->u.ost;
645         int err;
646
647         ENTRY;
648
649         if ( !list_empty(&obddev->obd_exports) ) {
650                 CERROR("still has clients!\n");
651                 RETURN(-EBUSY);
652         }
653
654         ptlrpc_stop_all_threads(ost->ost_service);
655         ptlrpc_unregister_service(ost->ost_service);
656
657         err = obd_disconnect(&ost->ost_conn);
658         if (err) {
659                 CERROR("lustre ost: fail to disconnect device\n");
660                 RETURN(-EINVAL);
661         }
662
663         ldlm_namespace_free(obddev->obd_namespace);
664
665         MOD_DEC_USE_COUNT;
666         RETURN(0);
667 }
668
669 /* use obd ops to offer management infrastructure */
670 static struct obd_ops ost_obd_ops = {
671         o_setup:       ost_setup,
672         o_cleanup:     ost_cleanup,
673 };
674
675 static int __init ost_init(void)
676 {
677         obd_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
678         return 0;
679 }
680
681 static void __exit ost_exit(void)
682 {
683         obd_unregister_type(LUSTRE_OST_NAME);
684 }
685
686 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
687 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
688 MODULE_LICENSE("GPL");
689
690 module_init(ost_init);
691 module_exit(ost_exit);