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