Whamcloud - gitweb
78f8fcf12b67fe23d335fcd13b5af5f71d0ceac0
[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 #include <linux/init.h>
41
42 static int ost_destroy(struct ptlrpc_request *req)
43 {
44         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
45         struct ost_body *body;
46         int rc, size = sizeof(*body);
47         ENTRY;
48
49         body = lustre_msg_buf(req->rq_reqmsg, 0);
50
51         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
52         if (rc)
53                 RETURN(rc);
54
55         req->rq_status = obd_destroy(conn, &body->oa, NULL);
56         RETURN(0);
57 }
58
59 static int ost_getattr(struct ptlrpc_request *req)
60 {
61         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
62         struct ost_body *body, *repbody;
63         int rc, size = sizeof(*body);
64         ENTRY;
65
66         body = lustre_msg_buf(req->rq_reqmsg, 0);
67
68         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
69         if (rc)
70                 RETURN(rc);
71
72         repbody = lustre_msg_buf(req->rq_repmsg, 0);
73         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
74         req->rq_status = obd_getattr(conn, &repbody->oa, NULL);
75         RETURN(0);
76 }
77
78 static int ost_statfs(struct ptlrpc_request *req)
79 {
80         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
81         struct obd_statfs *osfs;
82         struct statfs sfs;
83         int rc, size = sizeof(*osfs);
84         ENTRY;
85
86         rc = obd_statfs(conn, &sfs);
87         if (rc) {
88                 CERROR("ost: statfs failed: rc %d\n", rc);
89                 req->rq_status = rc;
90                 RETURN(rc);
91         }
92
93         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
94         if (rc)
95                 RETURN(rc);
96
97         osfs = lustre_msg_buf(req->rq_repmsg, 0);
98         memset(osfs, 0, size);
99         obd_statfs_pack(osfs, &sfs);
100         RETURN(0);
101 }
102
103 static int ost_open(struct ptlrpc_request *req)
104 {
105         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
106         struct ost_body *body, *repbody;
107         int rc, size = sizeof(*body);
108         ENTRY;
109
110         body = lustre_msg_buf(req->rq_reqmsg, 0);
111
112         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
113         if (rc)
114                 RETURN(rc);
115
116         repbody = lustre_msg_buf(req->rq_repmsg, 0);
117         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
118         req->rq_status = obd_open(conn, &repbody->oa, NULL);
119         RETURN(0);
120 }
121
122 static int ost_close(struct ptlrpc_request *req)
123 {
124         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
125         struct ost_body *body, *repbody;
126         int rc, size = sizeof(*body);
127         ENTRY;
128
129         body = lustre_msg_buf(req->rq_reqmsg, 0);
130
131         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
132         if (rc)
133                 RETURN(rc);
134
135         repbody = lustre_msg_buf(req->rq_repmsg, 0);
136         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
137         req->rq_status = obd_close(conn, &repbody->oa, NULL);
138         RETURN(0);
139 }
140
141 static int ost_create(struct ptlrpc_request *req)
142 {
143         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
144         struct ost_body *body, *repbody;
145         int rc, size = sizeof(*body);
146         ENTRY;
147
148         body = lustre_msg_buf(req->rq_reqmsg, 0);
149
150         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
151         if (rc)
152                 RETURN(rc);
153
154         repbody = lustre_msg_buf(req->rq_repmsg, 0);
155         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
156         req->rq_status = obd_create(conn, &repbody->oa, NULL);
157         RETURN(0);
158 }
159
160 static int ost_punch(struct ptlrpc_request *req)
161 {
162         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
163         struct ost_body *body, *repbody;
164         int rc, size = sizeof(*body);
165         ENTRY;
166
167         body = lustre_msg_buf(req->rq_reqmsg, 0);
168
169         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
170         if (rc)
171                 RETURN(rc);
172
173         repbody = lustre_msg_buf(req->rq_repmsg, 0);
174         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
175         req->rq_status = obd_punch(conn, &repbody->oa, NULL, 
176                                    repbody->oa.o_blocks, repbody->oa.o_size);
177         RETURN(0);
178 }
179
180 static int ost_setattr(struct ptlrpc_request *req)
181 {
182         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
183         struct ost_body *body, *repbody;
184         int rc, size = sizeof(*body);
185         ENTRY;
186
187         body = lustre_msg_buf(req->rq_reqmsg, 0);
188
189         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
190         if (rc)
191                 RETURN(rc);
192
193         repbody = lustre_msg_buf(req->rq_repmsg, 0);
194         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
195         req->rq_status = obd_setattr(conn, &repbody->oa, NULL);
196         RETURN(0);
197 }
198
199 static int ost_bulk_timeout(void *data)
200 {
201         struct ptlrpc_bulk_desc *desc = data;
202
203         ENTRY;
204         CERROR("(not yet) starting recovery of client %p\n", desc->b_client);
205         RETURN(1);
206 }
207
208 static int ost_brw_read(struct ptlrpc_request *req)
209 {
210         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
211         struct ptlrpc_bulk_desc *desc;
212         void *tmp1, *tmp2, *end2;
213         struct niobuf_remote *remote_nb;
214         struct niobuf_local *local_nb = NULL;
215         struct obd_ioobj *ioo;
216         struct ost_body *body;
217         struct l_wait_info lwi;
218         void *desc_priv = NULL;
219         int rc, cmd, i, j, objcount, niocount, size = sizeof(*body);
220         ENTRY;
221
222         body = lustre_msg_buf(req->rq_reqmsg, 0);
223         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
224         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
225         end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
226         objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
227         niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
228         cmd = OBD_BRW_READ;
229
230         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
231                 GOTO(out, rc = 0);
232
233         for (i = 0; i < objcount; i++) {
234                 ost_unpack_ioo(&tmp1, &ioo);
235                 if (tmp2 + ioo->ioo_bufcnt > end2) {
236                         LBUG();
237                         GOTO(out, rc = -EFAULT);
238                 }
239                 for (j = 0; j < ioo->ioo_bufcnt; j++)
240                         ost_unpack_niobuf(&tmp2, &remote_nb);
241         }
242
243         OBD_ALLOC(local_nb, sizeof(*local_nb) * niocount);
244         if (local_nb == NULL)
245                 GOTO(out, rc = -ENOMEM);
246
247         /* The unpackers move tmp1 and tmp2, so reset them before using */
248         ioo = lustre_msg_buf(req->rq_reqmsg, 1);
249         remote_nb = lustre_msg_buf(req->rq_reqmsg, 2);
250         req->rq_status = obd_preprw(cmd, conn, objcount, ioo, niocount,
251                                     remote_nb, local_nb, &desc_priv);
252
253         if (req->rq_status)
254                 GOTO(out, rc = 0);
255
256         desc = ptlrpc_prep_bulk(req->rq_connection);
257         if (desc == NULL)
258                 GOTO(out_local, rc = -ENOMEM);
259         desc->b_portal = OST_BULK_PORTAL;
260
261         for (i = 0; i < niocount; i++) {
262                 struct ptlrpc_bulk_page *bulk = ptlrpc_prep_bulk_page(desc);
263
264                 if (bulk == NULL)
265                         GOTO(out_bulk, rc = -ENOMEM);
266                 bulk->b_xid = remote_nb[i].xid;
267                 bulk->b_buf = local_nb[i].addr;
268                 bulk->b_buflen = remote_nb[i].len;
269         }
270
271         rc = ptlrpc_send_bulk(desc);
272         if (rc)
273                 GOTO(out_bulk, rc);
274
275         lwi = LWI_TIMEOUT(obd_timeout * HZ, ost_bulk_timeout, desc);
276         rc = l_wait_event(desc->b_waitq, desc->b_flags &PTL_BULK_FL_SENT, &lwi);
277         if (rc) {
278                 LASSERT(rc == -ETIMEDOUT);
279                 GOTO(out_bulk, rc);
280         }
281
282         req->rq_status = obd_commitrw(cmd, conn, objcount, ioo, niocount,
283                                       local_nb, desc_priv);
284
285         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
286
287 out_bulk:
288         ptlrpc_free_bulk(desc);
289 out_local:
290         OBD_FREE(local_nb, sizeof(*local_nb) * niocount);
291 out:
292         if (rc)
293                 ptlrpc_error(req->rq_svc, req);
294         else
295                 ptlrpc_reply(req->rq_svc, req);
296         RETURN(rc);
297 }
298
299 static int ost_brw_write(struct ptlrpc_request *req)
300 {
301         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
302         struct ptlrpc_bulk_desc *desc;
303         struct niobuf_remote *remote_nb;
304         struct niobuf_local *local_nb, *lnb;
305         struct obd_ioobj *ioo;
306         struct ost_body *body;
307         int cmd, rc, i, j, objcount, niocount, size[2] = {sizeof(*body)};
308         void *tmp1, *tmp2, *end2;
309         void *desc_priv = NULL;
310         int reply_sent = 0;
311         struct ptlrpc_service *srv;
312         struct l_wait_info lwi;
313         __u32 xid;
314         ENTRY;
315
316         body = lustre_msg_buf(req->rq_reqmsg, 0);
317         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
318         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
319         end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
320         objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
321         niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
322         cmd = OBD_BRW_WRITE;
323
324         for (i = 0; i < objcount; i++) {
325                 ost_unpack_ioo((void *)&tmp1, &ioo);
326                 if (tmp2 + ioo->ioo_bufcnt > end2) {
327                         rc = -EFAULT;
328                         break;
329                 }
330                 for (j = 0; j < ioo->ioo_bufcnt; j++)
331                         ost_unpack_niobuf((void *)&tmp2, &remote_nb);
332         }
333
334         size[1] = niocount * sizeof(*remote_nb);
335         rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
336         if (rc)
337                 GOTO(out, rc);
338         remote_nb = lustre_msg_buf(req->rq_repmsg, 1);
339
340         OBD_ALLOC(local_nb, niocount * sizeof(*local_nb));
341         if (local_nb == NULL)
342                 GOTO(out, rc = -ENOMEM);
343
344         /* The unpackers move tmp1 and tmp2, so reset them before using */
345         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
346         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
347         req->rq_status = obd_preprw(cmd, conn, objcount, tmp1, niocount, tmp2,
348                                     local_nb, &desc_priv);
349         if (req->rq_status)
350                 GOTO(out_free, rc = 0); /* XXX is this correct? */
351
352         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
353                 GOTO(fail_preprw, rc = 0);
354
355         desc = ptlrpc_prep_bulk(req->rq_connection);
356         if (desc == NULL)
357                 GOTO(fail_preprw, rc = -ENOMEM);
358         desc->b_cb = NULL;
359         desc->b_portal = OSC_BULK_PORTAL;
360         desc->b_desc_private = desc_priv;
361         memcpy(&(desc->b_conn), &conn, sizeof(conn));
362
363         srv = req->rq_obd->u.ost.ost_service;
364         spin_lock(&srv->srv_lock);
365         xid = srv->srv_xid++;                   /* single xid for all pages */
366         spin_unlock(&srv->srv_lock);
367
368         for (i = 0, lnb = local_nb; i < niocount; i++, lnb++) {
369                 struct ptlrpc_bulk_page *bulk;
370
371                 bulk = ptlrpc_prep_bulk_page(desc);
372                 if (bulk == NULL)
373                         GOTO(fail_bulk, rc = -ENOMEM);
374
375                 bulk->b_xid = xid;              /* single xid for all pages */
376
377                 bulk->b_buf = lnb->addr;
378                 bulk->b_page = lnb->page;
379                 bulk->b_flags = lnb->flags;
380                 bulk->b_dentry = lnb->dentry;
381                 bulk->b_buflen = lnb->len;
382                 bulk->b_cb = NULL;
383
384                 /* this advances remote_nb */
385                 ost_pack_niobuf((void **)&remote_nb, lnb->offset, lnb->len, 0,
386                                 bulk->b_xid);
387         }
388
389         rc = ptlrpc_register_bulk(desc);
390         if (rc)
391                 GOTO(fail_bulk, rc);
392
393         reply_sent = 1;
394         ptlrpc_reply(req->rq_svc, req);
395
396         lwi = LWI_TIMEOUT(obd_timeout * HZ, ost_bulk_timeout, desc);
397         rc = l_wait_event(desc->b_waitq, desc->b_flags & PTL_BULK_FL_RCVD, &lwi);
398         if (rc) {
399                 if (rc != -ETIMEDOUT)
400                         LBUG();
401                 GOTO(fail_bulk, rc);
402         }
403
404         rc = obd_commitrw(cmd, conn, objcount, tmp1, niocount, local_nb,
405                           desc->b_desc_private);
406         ptlrpc_free_bulk(desc);
407         EXIT;
408 out_free:
409         OBD_FREE(local_nb, niocount * sizeof(*local_nb));
410 out:
411         if (!reply_sent) {
412                 if (rc)
413                         ptlrpc_error(req->rq_svc, req);
414                 else
415                         ptlrpc_reply(req->rq_svc, req);
416         }
417         return rc;
418
419 fail_bulk:
420         ptlrpc_free_bulk(desc);
421 fail_preprw:
422         /* FIXME: how do we undo the preprw? */
423         goto out_free;
424 }
425
426 static int ost_handle(struct ptlrpc_request *req)
427 {
428         int rc;
429         ENTRY;
430
431         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
432         if (rc || OBD_FAIL_CHECK(OBD_FAIL_OST_HANDLE_UNPACK)) {
433                 CERROR("lustre_ost: Invalid request\n");
434                 GOTO(out, rc);
435         }
436
437         if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
438                 CERROR("lustre_ost: wrong packet type sent %d\n",
439                        req->rq_reqmsg->type);
440                 GOTO(out, rc = -EINVAL);
441         }
442
443         if (req->rq_reqmsg->opc != OST_CONNECT &&
444             req->rq_export == NULL) {
445                 CERROR("lustre_ost: operation %d on unconnected OST\n",
446                        req->rq_reqmsg->opc);
447                 GOTO(out, rc = -ENOTCONN);
448         }
449
450         if (strcmp(req->rq_obd->obd_type->typ_name, "ost") != 0)
451                 GOTO(out, rc = -EINVAL);
452
453         switch (req->rq_reqmsg->opc) {
454         case OST_CONNECT:
455                 CDEBUG(D_INODE, "connect\n");
456                 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
457                 rc = target_handle_connect(req);
458                 break;
459         case OST_DISCONNECT:
460                 CDEBUG(D_INODE, "disconnect\n");
461                 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
462                 rc = target_handle_disconnect(req);
463                 break;
464         case OST_CREATE:
465                 CDEBUG(D_INODE, "create\n");
466                 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
467                 rc = ost_create(req);
468                 break;
469         case OST_DESTROY:
470                 CDEBUG(D_INODE, "destroy\n");
471                 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
472                 rc = ost_destroy(req);
473                 break;
474         case OST_GETATTR:
475                 CDEBUG(D_INODE, "getattr\n");
476                 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
477                 rc = ost_getattr(req);
478                 break;
479         case OST_SETATTR:
480                 CDEBUG(D_INODE, "setattr\n");
481                 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
482                 rc = ost_setattr(req);
483                 break;
484         case OST_OPEN:
485                 CDEBUG(D_INODE, "open\n");
486                 OBD_FAIL_RETURN(OBD_FAIL_OST_OPEN_NET, 0);
487                 rc = ost_open(req);
488                 break;
489         case OST_CLOSE:
490                 CDEBUG(D_INODE, "close\n");
491                 OBD_FAIL_RETURN(OBD_FAIL_OST_CLOSE_NET, 0);
492                 rc = ost_close(req);
493                 break;
494         case OST_WRITE:
495                 CDEBUG(D_INODE, "write\n");
496                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
497                 rc = ost_brw_write(req);
498                 /* ost_brw sends its own replies */
499                 RETURN(rc);
500         case OST_READ:
501                 CDEBUG(D_INODE, "read\n");
502                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
503                 rc = ost_brw_read(req);
504                 /* ost_brw sends its own replies */
505                 RETURN(rc);
506         case OST_PUNCH:
507                 CDEBUG(D_INODE, "punch\n");
508                 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
509                 rc = ost_punch(req);
510                 break;
511         case OST_STATFS:
512                 CDEBUG(D_INODE, "statfs\n");
513                 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
514                 rc = ost_statfs(req);
515                 break;
516         case LDLM_ENQUEUE:
517                 CDEBUG(D_INODE, "enqueue\n");
518                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
519                 rc = ldlm_handle_enqueue(req);
520                 if (rc)
521                         break;
522                 RETURN(0);
523         case LDLM_CONVERT:
524                 CDEBUG(D_INODE, "convert\n");
525                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
526                 rc = ldlm_handle_convert(req);
527                 if (rc)
528                         break;
529                 RETURN(0);
530         case LDLM_CANCEL:
531                 CDEBUG(D_INODE, "cancel\n");
532                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
533                 rc = ldlm_handle_cancel(req);
534                 if (rc)
535                         break;
536                 RETURN(0);
537         case LDLM_BL_CALLBACK:
538         case LDLM_CP_CALLBACK:
539                 CDEBUG(D_INODE, "callback\n");
540                 CERROR("callbacks should not happen on OST\n");
541                 LBUG();
542                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
543                 break;
544         default:
545                 req->rq_status = -ENOTSUPP;
546                 rc = ptlrpc_error(req->rq_svc, req);
547                 RETURN(rc);
548         }
549
550         EXIT;
551 out:
552         //req->rq_status = rc;
553         if (rc) {
554                 CERROR("ost: processing error (opcode=%d): %d\n",
555                        req->rq_reqmsg->opc, rc);
556                 ptlrpc_error(req->rq_svc, req);
557         } else {
558                 CDEBUG(D_INODE, "sending reply\n");
559                 if (req->rq_repmsg == NULL)
560                         CERROR("handler for opcode %d returned rc=0 without "
561                                "creating rq_repmsg; needs to return rc != "
562                                "0!\n", req->rq_reqmsg->opc);
563                 ptlrpc_reply(req->rq_svc, req);
564         }
565
566         return 0;
567 }
568
569 #define OST_NUM_THREADS 6
570
571 /* mount the file system (secretly) */
572 static int ost_setup(struct obd_device *obddev, obd_count len, void *buf)
573 {
574         struct obd_ioctl_data* data = buf;
575         struct ost_obd *ost = &obddev->u.ost;
576         struct obd_device *tgt;
577         int err;
578         int i;
579         ENTRY;
580
581         if (data->ioc_inllen1 < 1) {
582                 CERROR("requires a TARGET OBD UUID\n");
583                 RETURN(-EINVAL);
584         }
585         if (data->ioc_inllen1 > 37) {
586                 CERROR("OBD UUID must be less than 38 characters\n");
587                 RETURN(-EINVAL);
588         }
589
590         MOD_INC_USE_COUNT;
591         tgt = class_uuid2obd(data->ioc_inlbuf1);
592         if (!tgt || !(tgt->obd_flags & OBD_ATTACHED) ||
593             !(tgt->obd_flags & OBD_SET_UP)) {
594                 CERROR("device not attached or not set up (%d)\n",
595                        data->ioc_dev);
596                 GOTO(error_dec, err = -EINVAL);
597         }
598
599         err = obd_connect(&ost->ost_conn, tgt, NULL);
600         if (err) {
601                 CERROR("fail to connect to device %d\n", data->ioc_dev);
602                 GOTO(error_dec, err = -EINVAL);
603         }
604
605         ost->ost_service = ptlrpc_init_svc(64 * 1024, OST_REQUEST_PORTAL,
606                                            OSC_REPLY_PORTAL, "self",ost_handle, 
607                                            "ost");
608         if (!ost->ost_service) {
609                 CERROR("failed to start service\n");
610                 GOTO(error_disc, err = -EINVAL);
611         }
612
613         for (i = 0; i < OST_NUM_THREADS; i++) {
614                 char name[32];
615                 sprintf(name, "lustre_ost_%02d", i);
616                 err = ptlrpc_start_thread(obddev, ost->ost_service, name);
617                 if (err) {
618                         CERROR("error starting thread #%d: rc %d\n", i, err);
619                         GOTO(error_disc, err = -EINVAL);
620                 }
621         }
622
623         RETURN(0);
624
625 error_disc:
626         obd_disconnect(&ost->ost_conn);
627 error_dec:
628         MOD_DEC_USE_COUNT;
629         RETURN(err);
630 }
631
632 static int ost_cleanup(struct obd_device * obddev)
633 {
634         struct ost_obd *ost = &obddev->u.ost;
635         int err;
636
637         ENTRY;
638
639         if ( !list_empty(&obddev->obd_exports) ) {
640                 CERROR("still has clients!\n");
641                 RETURN(-EBUSY);
642         }
643
644         ptlrpc_stop_all_threads(ost->ost_service);
645         ptlrpc_unregister_service(ost->ost_service);
646
647         err = obd_disconnect(&ost->ost_conn);
648         if (err) {
649                 CERROR("lustre ost: fail to disconnect device\n");
650                 RETURN(-EINVAL);
651         }
652
653         MOD_DEC_USE_COUNT;
654         RETURN(0);
655 }
656
657 /* use obd ops to offer management infrastructure */
658 static struct obd_ops ost_obd_ops = {
659         o_setup:       ost_setup,
660         o_cleanup:     ost_cleanup,
661 };
662
663 static int __init ost_init(void)
664 {
665         class_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
666         return 0;
667 }
668
669 static void __exit ost_exit(void)
670 {
671         class_unregister_type(LUSTRE_OST_NAME);
672 }
673
674 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
675 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
676 MODULE_LICENSE("GPL");
677
678 module_init(ost_init);
679 module_exit(ost_exit);