Whamcloud - gitweb
* l_wait_event can now do interrupts without a timeout, if we're feeling brave.
[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         int rc, cmd, i, j, objcount, niocount, size = sizeof(*body);
219         ENTRY;
220
221         body = lustre_msg_buf(req->rq_reqmsg, 0);
222         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
223         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
224         end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
225         objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
226         niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
227         cmd = OBD_BRW_READ;
228
229         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
230                 GOTO(out, rc = 0);
231
232         for (i = 0; i < objcount; i++) {
233                 ost_unpack_ioo(&tmp1, &ioo);
234                 if (tmp2 + ioo->ioo_bufcnt > end2) {
235                         LBUG();
236                         GOTO(out, rc = -EFAULT);
237                 }
238                 for (j = 0; j < ioo->ioo_bufcnt; j++)
239                         ost_unpack_niobuf(&tmp2, &remote_nb);
240         }
241
242         OBD_ALLOC(local_nb, sizeof(*local_nb) * niocount);
243         if (local_nb == NULL)
244                 GOTO(out, rc = -ENOMEM);
245
246         /* The unpackers move tmp1 and tmp2, so reset them before using */
247         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
248         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
249         req->rq_status = obd_preprw(cmd, conn, objcount,
250                                     tmp1, niocount, tmp2, local_nb, NULL);
251
252         if (req->rq_status)
253                 GOTO(out, 0);
254
255         desc = ptlrpc_prep_bulk(req->rq_connection);
256         if (desc == NULL)
257                 GOTO(out_local, rc = -ENOMEM);
258         desc->b_portal = OST_BULK_PORTAL;
259
260         for (i = 0; i < niocount; i++) {
261                 struct ptlrpc_bulk_page *bulk;
262                 bulk = ptlrpc_prep_bulk_page(desc);
263                 if (bulk == NULL)
264                         GOTO(out_bulk, rc = -ENOMEM);
265                 remote_nb = &(((struct niobuf_remote *)tmp2)[i]);
266                 bulk->b_xid = remote_nb->xid;
267                 bulk->b_buf = (void *)(unsigned long)local_nb[i].addr;
268                 bulk->b_buflen = PAGE_SIZE;
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         /* The unpackers move tmp1 and tmp2, so reset them before using */
283         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
284         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
285         req->rq_status = obd_commitrw(cmd, conn, objcount,
286                                       tmp1, niocount, local_nb, NULL);
287
288         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
289
290 out_bulk:
291         ptlrpc_free_bulk(desc);
292 out_local:
293         OBD_FREE(local_nb, sizeof(*local_nb) * niocount);
294 out:
295         if (rc)
296                 ptlrpc_error(req->rq_svc, req);
297         else
298                 ptlrpc_reply(req->rq_svc, req);
299         RETURN(rc);
300 }
301
302 static int ost_brw_write(struct ptlrpc_request *req)
303 {
304         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
305         struct ptlrpc_bulk_desc *desc;
306         struct niobuf_remote *remote_nb;
307         struct niobuf_local *local_nb, *lnb;
308         struct obd_ioobj *ioo;
309         struct ost_body *body;
310         int cmd, rc, i, j, objcount, niocount, size[2] = {sizeof(*body)};
311         void *tmp1, *tmp2, *end2;
312         void *desc_priv = NULL;
313         int reply_sent = 0;
314         struct ptlrpc_service *srv;
315         struct l_wait_info lwi;
316         __u32 xid;
317         ENTRY;
318
319         body = lustre_msg_buf(req->rq_reqmsg, 0);
320         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
321         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
322         end2 = (char *)tmp2 + req->rq_reqmsg->buflens[2];
323         objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
324         niocount = req->rq_reqmsg->buflens[2] / sizeof(*remote_nb);
325         cmd = OBD_BRW_WRITE;
326
327         for (i = 0; i < objcount; i++) {
328                 ost_unpack_ioo((void *)&tmp1, &ioo);
329                 if (tmp2 + ioo->ioo_bufcnt > end2) {
330                         rc = -EFAULT;
331                         break;
332                 }
333                 for (j = 0; j < ioo->ioo_bufcnt; j++)
334                         ost_unpack_niobuf((void *)&tmp2, &remote_nb);
335         }
336
337         size[1] = niocount * sizeof(*remote_nb);
338         rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
339         if (rc)
340                 GOTO(out, rc);
341         remote_nb = lustre_msg_buf(req->rq_repmsg, 1);
342
343         OBD_ALLOC(local_nb, niocount * sizeof(*local_nb));
344         if (local_nb == NULL)
345                 GOTO(out, rc = -ENOMEM);
346
347         /* The unpackers move tmp1 and tmp2, so reset them before using */
348         tmp1 = lustre_msg_buf(req->rq_reqmsg, 1);
349         tmp2 = lustre_msg_buf(req->rq_reqmsg, 2);
350         req->rq_status = obd_preprw(cmd, conn, objcount,
351                                     tmp1, niocount, tmp2, local_nb, &desc_priv);
352         if (req->rq_status)
353                 GOTO(out_free, rc = 0); /* XXX is this correct? */
354
355         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
356                 GOTO(fail_preprw, rc = 0);
357
358         desc = ptlrpc_prep_bulk(req->rq_connection);
359         if (desc == NULL)
360                 GOTO(fail_preprw, rc = -ENOMEM);
361         desc->b_cb = NULL;
362         desc->b_portal = OSC_BULK_PORTAL;
363         desc->b_desc_private = desc_priv;
364         memcpy(&(desc->b_conn), &conn, sizeof(conn));
365
366         srv = req->rq_obd->u.ost.ost_service;
367         spin_lock(&srv->srv_lock);
368         xid = srv->srv_xid++;                   /* single xid for all pages */
369         spin_unlock(&srv->srv_lock);
370
371         for (i = 0, lnb = local_nb; i < niocount; i++, lnb++) {
372                 struct ptlrpc_bulk_page *bulk;
373
374                 bulk = ptlrpc_prep_bulk_page(desc);
375                 if (bulk == NULL)
376                         GOTO(fail_bulk, rc = -ENOMEM);
377
378                 bulk->b_xid = xid;              /* single xid for all pages */
379
380                 bulk->b_buf = lnb->addr;
381                 bulk->b_page = lnb->page;
382                 bulk->b_flags = lnb->flags;
383                 bulk->b_dentry = lnb->dentry;
384                 bulk->b_buflen = PAGE_SIZE;
385                 bulk->b_cb = NULL;
386
387                 /* this advances remote_nb */
388                 ost_pack_niobuf((void **)&remote_nb, lnb->offset, lnb->len, 0,
389                                 bulk->b_xid);
390         }
391
392         rc = ptlrpc_register_bulk(desc);
393         if (rc)
394                 GOTO(fail_bulk, rc);
395
396         reply_sent = 1;
397         ptlrpc_reply(req->rq_svc, req);
398
399         lwi = LWI_TIMEOUT(obd_timeout * HZ, ost_bulk_timeout, desc);
400         rc = l_wait_event(desc->b_waitq, desc->b_flags & PTL_BULK_FL_RCVD, &lwi);
401         if (rc) {
402                 if (rc != -ETIMEDOUT)
403                         LBUG();
404                 GOTO(fail_bulk, rc);
405         }
406
407         rc = obd_commitrw(cmd, conn, objcount, tmp1, niocount, local_nb,
408                           desc->b_desc_private);
409         ptlrpc_free_bulk(desc);
410         EXIT;
411 out_free:
412         OBD_FREE(local_nb, niocount * sizeof(*local_nb));
413 out:
414         if (!reply_sent) {
415                 if (rc)
416                         ptlrpc_error(req->rq_svc, req);
417                 else
418                         ptlrpc_reply(req->rq_svc, req);
419         }
420         return rc;
421
422 fail_bulk:
423         ptlrpc_free_bulk(desc);
424 fail_preprw:
425         /* FIXME: how do we undo the preprw? */
426         goto out_free;
427 }
428
429 static int ost_handle(struct ptlrpc_request *req)
430 {
431         int rc;
432         ENTRY;
433
434         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
435         if (rc || OBD_FAIL_CHECK(OBD_FAIL_OST_HANDLE_UNPACK)) {
436                 CERROR("lustre_ost: Invalid request\n");
437                 GOTO(out, rc);
438         }
439
440         if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
441                 CERROR("lustre_ost: wrong packet type sent %d\n",
442                        req->rq_reqmsg->type);
443                 GOTO(out, rc = -EINVAL);
444         }
445
446         if (req->rq_reqmsg->opc != OST_CONNECT &&
447             req->rq_export == NULL) {
448                 CERROR("lustre_ost: operation %d on unconnected OST\n",
449                        req->rq_reqmsg->opc);
450                 GOTO(out, rc = -ENOTCONN);
451         }
452
453         if (strcmp(req->rq_obd->obd_type->typ_name, "ost") != 0)
454                 GOTO(out, rc = -EINVAL);
455
456         switch (req->rq_reqmsg->opc) {
457         case OST_CONNECT:
458                 CDEBUG(D_INODE, "connect\n");
459                 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
460                 rc = target_handle_connect(req);
461                 break;
462         case OST_DISCONNECT:
463                 CDEBUG(D_INODE, "disconnect\n");
464                 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
465                 rc = target_handle_disconnect(req);
466                 break;
467         case OST_CREATE:
468                 CDEBUG(D_INODE, "create\n");
469                 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
470                 rc = ost_create(req);
471                 break;
472         case OST_DESTROY:
473                 CDEBUG(D_INODE, "destroy\n");
474                 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
475                 rc = ost_destroy(req);
476                 break;
477         case OST_GETATTR:
478                 CDEBUG(D_INODE, "getattr\n");
479                 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
480                 rc = ost_getattr(req);
481                 break;
482         case OST_SETATTR:
483                 CDEBUG(D_INODE, "setattr\n");
484                 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
485                 rc = ost_setattr(req);
486                 break;
487         case OST_OPEN:
488                 CDEBUG(D_INODE, "setattr\n");
489                 OBD_FAIL_RETURN(OBD_FAIL_OST_OPEN_NET, 0);
490                 rc = ost_open(req);
491                 break;
492         case OST_CLOSE:
493                 CDEBUG(D_INODE, "setattr\n");
494                 OBD_FAIL_RETURN(OBD_FAIL_OST_CLOSE_NET, 0);
495                 rc = ost_close(req);
496                 break;
497         case OST_WRITE:
498                 CDEBUG(D_INODE, "write\n");
499                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
500                 rc = ost_brw_write(req);
501                 /* ost_brw sends its own replies */
502                 RETURN(rc);
503         case OST_READ:
504                 CDEBUG(D_INODE, "read\n");
505                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
506                 rc = ost_brw_read(req);
507                 /* ost_brw sends its own replies */
508                 RETURN(rc);
509         case OST_PUNCH:
510                 CDEBUG(D_INODE, "punch\n");
511                 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
512                 rc = ost_punch(req);
513                 break;
514         case OST_STATFS:
515                 CDEBUG(D_INODE, "statfs\n");
516                 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
517                 rc = ost_statfs(req);
518                 break;
519         case LDLM_ENQUEUE:
520                 CDEBUG(D_INODE, "enqueue\n");
521                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
522                 rc = ldlm_handle_enqueue(req);
523                 if (rc)
524                         break;
525                 RETURN(0);
526         case LDLM_CONVERT:
527                 CDEBUG(D_INODE, "convert\n");
528                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
529                 rc = ldlm_handle_convert(req);
530                 if (rc)
531                         break;
532                 RETURN(0);
533         case LDLM_CANCEL:
534                 CDEBUG(D_INODE, "cancel\n");
535                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
536                 rc = ldlm_handle_cancel(req);
537                 if (rc)
538                         break;
539                 RETURN(0);
540         case LDLM_BL_CALLBACK:
541         case LDLM_CP_CALLBACK:
542                 CDEBUG(D_INODE, "callback\n");
543                 CERROR("callbacks should not happen on OST\n");
544                 LBUG();
545                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
546                 break;
547         default:
548                 req->rq_status = -ENOTSUPP;
549                 rc = ptlrpc_error(req->rq_svc, req);
550                 RETURN(rc);
551         }
552
553         EXIT;
554 out:
555         //req->rq_status = rc;
556         if (rc) {
557                 CERROR("ost: processing error (opcode=%d): %d\n",
558                        req->rq_reqmsg->opc, rc);
559                 ptlrpc_error(req->rq_svc, req);
560         } else {
561                 CDEBUG(D_INODE, "sending reply\n");
562                 if (req->rq_repmsg == NULL)
563                         CERROR("handler for opcode %d returned rc=0 without "
564                                "creating rq_repmsg; needs to return rc != "
565                                "0!\n", req->rq_reqmsg->opc);
566                 ptlrpc_reply(req->rq_svc, req);
567         }
568
569         return 0;
570 }
571
572 #define OST_NUM_THREADS 6
573
574 /* mount the file system (secretly) */
575 static int ost_setup(struct obd_device *obddev, obd_count len, void *buf)
576 {
577         struct obd_ioctl_data* data = buf;
578         struct ost_obd *ost = &obddev->u.ost;
579         struct obd_device *tgt;
580         int err;
581         int i;
582         ENTRY;
583
584         if (data->ioc_inllen1 < 1) {
585                 CERROR("requires a TARGET OBD UUID\n");
586                 RETURN(-EINVAL);
587         }
588         if (data->ioc_inllen1 > 37) {
589                 CERROR("OBD UUID must be less than 38 characters\n");
590                 RETURN(-EINVAL);
591         }
592
593         MOD_INC_USE_COUNT;
594         tgt = class_uuid2obd(data->ioc_inlbuf1);
595         if (!tgt || !(tgt->obd_flags & OBD_ATTACHED) ||
596             !(tgt->obd_flags & OBD_SET_UP)) {
597                 CERROR("device not attached or not set up (%d)\n",
598                        data->ioc_dev);
599                 GOTO(error_dec, err = -EINVAL);
600         }
601
602         err = obd_connect(&ost->ost_conn, tgt, NULL);
603         if (err) {
604                 CERROR("fail to connect to device %d\n", data->ioc_dev);
605                 GOTO(error_dec, err = -EINVAL);
606         }
607
608         ost->ost_service = ptlrpc_init_svc(64 * 1024, OST_REQUEST_PORTAL,
609                                            OSC_REPLY_PORTAL, "self",ost_handle);
610         if (!ost->ost_service) {
611                 CERROR("failed to start service\n");
612                 GOTO(error_disc, err = -EINVAL);
613         }
614
615         for (i = 0; i < OST_NUM_THREADS; i++) {
616                 char name[32];
617                 sprintf(name, "lustre_ost_%02d", i);
618                 err = ptlrpc_start_thread(obddev, ost->ost_service, name);
619                 if (err) {
620                         CERROR("error starting thread #%d: rc %d\n", i, err);
621                         GOTO(error_disc, err = -EINVAL);
622                 }
623         }
624
625         RETURN(0);
626
627 error_disc:
628         obd_disconnect(&ost->ost_conn);
629 error_dec:
630         MOD_DEC_USE_COUNT;
631         RETURN(err);
632 }
633
634 static int ost_cleanup(struct obd_device * obddev)
635 {
636         struct ost_obd *ost = &obddev->u.ost;
637         int err;
638
639         ENTRY;
640
641         if ( !list_empty(&obddev->obd_exports) ) {
642                 CERROR("still has clients!\n");
643                 RETURN(-EBUSY);
644         }
645
646         ptlrpc_stop_all_threads(ost->ost_service);
647         ptlrpc_unregister_service(ost->ost_service);
648
649         err = obd_disconnect(&ost->ost_conn);
650         if (err) {
651                 CERROR("lustre ost: fail to disconnect device\n");
652                 RETURN(-EINVAL);
653         }
654
655         MOD_DEC_USE_COUNT;
656         RETURN(0);
657 }
658
659 /* use obd ops to offer management infrastructure */
660 static struct obd_ops ost_obd_ops = {
661         o_setup:       ost_setup,
662         o_cleanup:     ost_cleanup,
663 };
664
665 static int __init ost_init(void)
666 {
667         class_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
668         return 0;
669 }
670
671 static void __exit ost_exit(void)
672 {
673         class_unregister_type(LUSTRE_OST_NAME);
674 }
675
676 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
677 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
678 MODULE_LICENSE("GPL");
679
680 module_init(ost_init);
681 module_exit(ost_exit);