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