Whamcloud - gitweb
- updated documentation (introduction)
[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
35 #include <linux/version.h>
36 #include <linux/module.h>
37 #include <linux/fs.h>
38 #include <linux/stat.h>
39 #include <linux/locks.h>
40 #include <linux/ext2_fs.h>
41 #include <linux/quotaops.h>
42 #include <asm/unistd.h>
43
44 #define DEBUG_SUBSYSTEM S_OST
45
46 #include <linux/obd_ost.h>
47 #include <linux/lustre_net.h>
48
49 static int ost_destroy(struct ost_obd *ost, struct ptlrpc_request *req)
50 {
51         struct obd_conn conn;
52         int rc;
53
54         ENTRY;
55
56         conn.oc_id = req->rq_req.ost->connid;
57         conn.oc_dev = ost->ost_tgt;
58
59         rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
60                           &req->rq_replen, &req->rq_repbuf);
61         if (rc) {
62                 CERROR("cannot pack reply\n");
63                 RETURN(rc);
64         }
65
66         req->rq_rep.ost->result = obd_destroy(&conn, &req->rq_req.ost->oa);
67
68         RETURN(0);
69 }
70
71 static int ost_getattr(struct ost_obd *ost, struct ptlrpc_request *req)
72 {
73         struct obd_conn conn;
74         int rc;
75
76         ENTRY;
77
78         conn.oc_id = req->rq_req.ost->connid;
79         conn.oc_dev = ost->ost_tgt;
80
81         rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
82                           &req->rq_replen, &req->rq_repbuf);
83         if (rc) {
84                 CERROR("cannot pack reply\n");
85                 RETURN(rc);
86         }
87         req->rq_rep.ost->oa.o_id = req->rq_req.ost->oa.o_id;
88         req->rq_rep.ost->oa.o_valid = req->rq_req.ost->oa.o_valid;
89
90         req->rq_rep.ost->result =  obd_getattr(&conn, &req->rq_rep.ost->oa);
91
92         RETURN(0);
93 }
94
95 static int ost_open(struct ost_obd *ost, struct ptlrpc_request *req)
96 {
97         struct obd_conn conn;
98         int rc;
99
100         ENTRY;
101
102         conn.oc_id = req->rq_req.ost->connid;
103         conn.oc_dev = ost->ost_tgt;
104
105         rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
106                           &req->rq_replen, &req->rq_repbuf);
107         if (rc) {
108                 CERROR("cannot pack reply\n");
109                 RETURN(rc);
110         }
111         req->rq_rep.ost->oa.o_id = req->rq_req.ost->oa.o_id;
112         req->rq_rep.ost->oa.o_valid = req->rq_req.ost->oa.o_valid;
113
114         req->rq_rep.ost->result =  obd_open(&conn, &req->rq_rep.ost->oa);
115
116         RETURN(0);
117 }
118
119 static int ost_close(struct ost_obd *ost, struct ptlrpc_request *req)
120 {
121         struct obd_conn conn;
122         int rc;
123
124         ENTRY;
125
126         conn.oc_id = req->rq_req.ost->connid;
127         conn.oc_dev = ost->ost_tgt;
128
129         rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
130                           &req->rq_replen, &req->rq_repbuf);
131         if (rc) {
132                 CERROR("cannot pack reply\n");
133                 RETURN(rc);
134         }
135         req->rq_rep.ost->oa.o_id = req->rq_req.ost->oa.o_id;
136         req->rq_rep.ost->oa.o_valid = req->rq_req.ost->oa.o_valid;
137
138         req->rq_rep.ost->result =  obd_close(&conn, &req->rq_rep.ost->oa);
139
140         RETURN(0);
141 }
142
143
144 static int ost_create(struct ost_obd *ost, struct ptlrpc_request *req)
145 {
146         struct obd_conn conn;
147         int rc;
148
149         ENTRY;
150
151         conn.oc_id = req->rq_req.ost->connid;
152         conn.oc_dev = ost->ost_tgt;
153
154         rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
155                           &req->rq_replen, &req->rq_repbuf);
156         if (rc) {
157                 CERROR("cannot pack reply\n");
158                 RETURN(rc);
159         }
160
161         memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
162                sizeof(req->rq_req.ost->oa));
163
164         req->rq_rep.ost->result =obd_create(&conn, &req->rq_rep.ost->oa);
165
166         RETURN(0);
167 }
168
169 static int ost_punch(struct ost_obd *ost, struct ptlrpc_request *req)
170 {
171         struct obd_conn conn;
172         int rc;
173
174         ENTRY;
175
176         conn.oc_id = req->rq_req.ost->connid;
177         conn.oc_dev = ost->ost_tgt;
178
179         rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
180                           &req->rq_replen, &req->rq_repbuf);
181         if (rc) {
182                 CERROR("cannot pack reply\n");
183                 RETURN(rc);
184         }
185
186         memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
187                sizeof(req->rq_req.ost->oa));
188
189         req->rq_rep.ost->result = obd_punch(&conn, &req->rq_rep.ost->oa,
190                                             req->rq_rep.ost->oa.o_size,
191                                             req->rq_rep.ost->oa.o_blocks);
192
193         RETURN(0);
194 }
195
196
197 static int ost_setattr(struct ost_obd *ost, struct ptlrpc_request *req)
198 {
199         struct obd_conn conn;
200         int rc;
201
202         ENTRY;
203
204         conn.oc_id = req->rq_req.ost->connid;
205         conn.oc_dev = ost->ost_tgt;
206
207         rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
208                           &req->rq_replen, &req->rq_repbuf);
209         if (rc) {
210                 CERROR("cannot pack reply\n");
211                 RETURN(rc);
212         }
213
214         memcpy(&req->rq_rep.ost->oa, &req->rq_req.ost->oa,
215                sizeof(req->rq_req.ost->oa));
216
217         req->rq_rep.ost->result = obd_setattr(&conn, &req->rq_rep.ost->oa);
218
219         RETURN(0);
220 }
221
222 static int ost_connect(struct ost_obd *ost, struct ptlrpc_request *req)
223 {
224         struct obd_conn conn;
225         int rc;
226
227         ENTRY;
228
229         conn.oc_dev = ost->ost_tgt;
230
231         rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
232                           &req->rq_replen, &req->rq_repbuf);
233         if (rc) {
234                 CERROR("cannot pack reply\n");
235                 RETURN(rc);
236         }
237
238         req->rq_rep.ost->result = obd_connect(&conn);
239
240         CDEBUG(D_IOCTL, "rep buffer %p, id %d\n", req->rq_repbuf, conn.oc_id);
241         req->rq_rep.ost->connid = conn.oc_id;
242         RETURN(0);
243 }
244
245 static int ost_disconnect(struct ost_obd *ost, struct ptlrpc_request *req)
246 {
247         struct obd_conn conn;
248         int rc;
249
250         ENTRY;
251
252         conn.oc_dev = ost->ost_tgt;
253         conn.oc_id = req->rq_req.ost->connid;
254
255         rc = ost_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
256                           &req->rq_replen, &req->rq_repbuf);
257         if (rc) {
258                 CERROR("cannot pack reply\n");
259                 RETURN(rc);
260         }
261         CDEBUG(D_IOCTL, "Disconnecting %d\n", conn.oc_id);
262         req->rq_rep.ost->result = obd_disconnect(&conn);
263
264         RETURN(0);
265 }
266
267 static int ost_get_info(struct ost_obd *ost, struct ptlrpc_request *req)
268 {
269         struct obd_conn conn;
270         int rc;
271         int vallen;
272         void *val;
273         char *ptr;
274
275         ENTRY;
276
277         conn.oc_id = req->rq_req.ost->connid;
278         conn.oc_dev = ost->ost_tgt;
279
280         ptr = ost_req_buf1(req->rq_req.ost);
281         req->rq_rep.ost->result = obd_get_info(&conn, req->rq_req.ost->buflen1,
282                                                ptr, &vallen, &val);
283
284         rc = ost_pack_rep(val, vallen, NULL, 0, &req->rq_rephdr,
285                           &req->rq_rep, &req->rq_replen, &req->rq_repbuf);
286         if (rc)
287                 CERROR("cannot pack reply\n");
288
289         RETURN(rc);
290 }
291
292 static int ost_brw_read(struct ost_obd *obddev, struct ptlrpc_request *req)
293 {
294         struct ptlrpc_bulk_desc **bulk_vec = NULL;
295         struct ptlrpc_bulk_desc *bulk = NULL;
296         struct obd_conn conn;
297         int rc;
298         int i, j;
299         int objcount, niocount;
300         char *tmp1, *tmp2, *end2;
301         char *res = NULL;
302         int cmd;
303         struct niobuf *nb, *src, *dst;
304         struct obd_ioobj *ioo;
305         struct ost_req *r = req->rq_req.ost;
306
307         ENTRY;
308
309         tmp1 = ost_req_buf1(r);
310         tmp2 = ost_req_buf2(r);
311         end2 = tmp2 + req->rq_req.ost->buflen2;
312         objcount = r->buflen1 / sizeof(*ioo);
313         niocount = r->buflen2 / sizeof(*nb);
314         cmd = r->cmd;
315
316         conn.oc_id = req->rq_req.ost->connid;
317         conn.oc_dev = req->rq_obd->u.ost.ost_tgt;
318
319         for (i = 0; i < objcount; i++) {
320                 ost_unpack_ioo((void *)&tmp1, &ioo);
321                 if (tmp2 + ioo->ioo_bufcnt > end2) {
322                         BUG();
323                         rc = -EFAULT;
324                         break;
325                 }
326                 for (j = 0; j < ioo->ioo_bufcnt; j++)
327                         ost_unpack_niobuf((void *)&tmp2, &nb);
328         }
329
330         rc = ost_pack_rep(NULL, 0, NULL, 0,
331                           &req->rq_rephdr, &req->rq_rep,
332                           &req->rq_replen, &req->rq_repbuf);
333         if (rc) {
334                 CERROR("cannot pack reply\n");
335                 RETURN(rc);
336         }
337         OBD_ALLOC(res, sizeof(struct niobuf) * niocount);
338         if (res == NULL)
339                 RETURN(-ENOMEM);
340
341         /* The unpackers move tmp1 and tmp2, so reset them before using */
342         tmp1 = ost_req_buf1(r);
343         tmp2 = ost_req_buf2(r);
344         req->rq_rep.ost->result = obd_preprw
345                 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
346                  niocount, (struct niobuf *)tmp2, (struct niobuf *)res);
347
348         if (req->rq_rep.ost->result)
349                 GOTO(out, 0);
350
351         for (i = 0; i < niocount; i++) {
352                 bulk = ptlrpc_prep_bulk(&req->rq_peer);
353                 if (bulk == NULL) {
354                         CERROR("cannot alloc bulk desc\n");
355                         rc = -ENOMEM;
356                         GOTO(out, rc);
357                 }
358
359                 src = &((struct niobuf *)res)[i];
360                 dst = &((struct niobuf *)tmp2)[i];
361                 bulk->b_xid = dst->xid;
362                 bulk->b_buf = (void *)(unsigned long)src->addr;
363                 bulk->b_buflen = PAGE_SIZE;
364                 rc = ptlrpc_send_bulk(bulk, OST_BULK_PORTAL);
365                 if (rc)
366                         GOTO(out, rc);
367                 wait_event_interruptible(bulk->b_waitq,
368                                          ptlrpc_check_bulk_sent(bulk));
369
370                 if (bulk->b_flags == PTL_RPC_INTR)
371                         GOTO(out, 0);
372
373                 OBD_FREE(bulk, sizeof(*bulk));
374                 bulk = NULL;
375         }
376
377 #if 0
378         /* Local delivery */
379         dst = &((struct niobuf *)tmp2)[i];
380         memcpy((void *)(unsigned long)dst->addr,
381                (void *)(unsigned long)src->addr, PAGE_SIZE);
382 #endif
383         barrier();
384
385         /* The unpackers move tmp1 and tmp2, so reset them before using */
386         tmp1 = ost_req_buf1(r);
387         tmp2 = ost_req_buf2(r);
388         req->rq_rep.ost->result = obd_commitrw
389                 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
390                  niocount, (struct niobuf *)res);
391
392         EXIT;
393  out:
394         if (res != NULL)
395                 OBD_FREE(res, sizeof(struct niobuf) * niocount);
396         if (bulk != NULL)
397                 OBD_FREE(bulk, sizeof(*bulk));
398         if (bulk_vec != NULL) {
399                 for (i = 0; i < niocount; i++) {
400                         if (bulk_vec[i] != NULL)
401                                 OBD_FREE(bulk_vec[i], sizeof(*bulk));
402                 }
403                 OBD_FREE(bulk_vec,
404                          niocount * sizeof(struct ptlrpc_bulk_desc *));
405         }
406
407         return 0;
408 }
409
410 static int ost_commit_page(struct obd_conn *conn, struct page *page)
411 {
412         struct obd_ioobj obj;
413         struct niobuf buf;
414         int rc;
415         ENTRY;
416
417         memset(&buf, 0, sizeof(buf));
418         memset(&obj, 0, sizeof(obj));
419
420         buf.page = page;
421         obj.ioo_bufcnt = 1;
422
423         rc = obd_commitrw(OBD_BRW_WRITE, conn, 1, &obj, 1, &buf);
424         RETURN(rc);
425 }
426
427 static int ost_brw_write_cb(struct ptlrpc_bulk_desc *bulk, void *data)
428 {
429         int rc;
430
431         ENTRY;
432
433         rc = ost_commit_page(&bulk->b_conn, bulk->b_page);
434         if (rc)
435                 CERROR("ost_commit_page failed: %d\n", rc);
436
437         RETURN(rc);
438 }
439
440 int ost_brw_write(struct ost_obd *obddev, struct ptlrpc_request *req)
441 {
442         struct obd_conn conn;
443         int rc;
444         int i, j;
445         int objcount, niocount;
446         char *tmp1, *tmp2, *end2;
447         char *res;
448         int cmd;
449         struct niobuf *nb, *dst;
450         struct obd_ioobj *ioo;
451         struct ost_req *r = req->rq_req.ost;
452
453         ENTRY;
454
455         tmp1 = ost_req_buf1(r);
456         tmp2 = ost_req_buf2(r);
457         end2 = tmp2 + req->rq_req.ost->buflen2;
458         objcount = r->buflen1 / sizeof(*ioo);
459         niocount = r->buflen2 / sizeof(*nb);
460         cmd = r->cmd;
461
462         conn.oc_id = req->rq_req.ost->connid;
463         conn.oc_dev = req->rq_obd->u.ost.ost_tgt;
464
465         for (i = 0; i < objcount; i++) {
466                 ost_unpack_ioo((void *)&tmp1, &ioo);
467                 if (tmp2 + ioo->ioo_bufcnt > end2) {
468                         rc = -EFAULT;
469                         break;
470                 }
471                 for (j = 0; j < ioo->ioo_bufcnt; j++)
472                         ost_unpack_niobuf((void *)&tmp2, &nb);
473         }
474
475         rc = ost_pack_rep(NULL, 0, NULL, niocount * sizeof(*nb),
476                           &req->rq_rephdr, &req->rq_rep,
477                           &req->rq_replen, &req->rq_repbuf);
478         if (rc) {
479                 CERROR("cannot pack reply\n");
480                 RETURN(rc);
481         }
482         res = ost_rep_buf2(req->rq_rep.ost);
483
484         /* The unpackers move tmp1 and tmp2, so reset them before using */
485         tmp1 = ost_req_buf1(r);
486         tmp2 = ost_req_buf2(r);
487         req->rq_rep.ost->result = obd_preprw
488                 (cmd, &conn, objcount, (struct obd_ioobj *)tmp1,
489                  niocount, (struct niobuf *)tmp2, (struct niobuf *)res);
490
491         if (req->rq_rep.ost->result)
492                 GOTO(out, 0);
493
494         for (i = 0; i < niocount; i++) {
495                 struct ptlrpc_bulk_desc *bulk;
496                 struct ptlrpc_service *srv = req->rq_obd->u.ost.ost_service;
497
498                 bulk = ptlrpc_prep_bulk(&req->rq_peer);
499                 if (bulk == NULL) {
500                         CERROR("cannot alloc bulk desc\n");
501                         rc = -ENOMEM;
502                         GOTO(out, rc);
503                 }
504
505                 spin_lock(&srv->srv_lock);
506                 bulk->b_xid = srv->srv_xid++;
507                 spin_unlock(&srv->srv_lock);
508
509                 dst = &((struct niobuf *)res)[i];
510                 dst->xid = HTON__u32(bulk->b_xid);
511
512                 bulk->b_buf = (void *)(unsigned long)dst->addr;
513                 bulk->b_cb = ost_brw_write_cb;
514                 bulk->b_page = dst->page;
515                 memcpy(&(bulk->b_conn), &conn, sizeof(conn));
516                 bulk->b_buflen = PAGE_SIZE;
517                 bulk->b_portal = OSC_BULK_PORTAL;
518                 rc = ptlrpc_register_bulk(bulk);
519                 if (rc)
520                         GOTO(out, rc);
521
522 #if 0
523                 /* Local delivery */
524                 src = &((struct niobuf *)tmp2)[i];
525                 memcpy((void *)(unsigned long)dst->addr,
526                        (void *)(unsigned long)src->addr, src->len);
527 #endif
528         }
529         barrier();
530
531         EXIT;
532  out:
533         /* FIXME: should we return 'rc' here? */
534         return 0;
535 }
536
537 int ost_brw(struct ost_obd *obddev, struct ptlrpc_request *req)
538 {
539         struct ost_req *r = req->rq_req.ost;
540         int cmd = r->cmd;
541
542         if (cmd == OBD_BRW_READ)
543                 return ost_brw_read(obddev, req);
544         else
545                 return ost_brw_write(obddev, req);
546 }
547
548 static int ost_handle(struct obd_device *obddev, struct ptlrpc_service *svc,
549                       struct ptlrpc_request *req)
550 {
551         int rc;
552         struct ost_obd *ost = &obddev->u.ost;
553         struct ptlreq_hdr *hdr;
554
555         ENTRY;
556
557         hdr = (struct ptlreq_hdr *)req->rq_reqbuf;
558         if (NTOH__u32(hdr->type) != OST_TYPE_REQ) {
559                 CERROR("lustre_ost: wrong packet type sent %d\n",
560                        NTOH__u32(hdr->type));
561                 BUG();
562                 rc = -EINVAL;
563                 GOTO(out, rc);
564         }
565
566         rc = ost_unpack_req(req->rq_reqbuf, req->rq_reqlen,
567                             &req->rq_reqhdr, &req->rq_req);
568         if (rc) {
569                 CERROR("lustre_ost: Invalid request\n");
570                 GOTO(out, rc);
571         }
572
573         switch (req->rq_reqhdr->opc) {
574
575         case OST_CONNECT:
576                 CDEBUG(D_INODE, "connect\n");
577                 OBD_CHECK_DROP_PACKET(req, OBD_INST_OST_CONNECT);
578                 rc = ost_connect(ost, req);
579                 break;
580         case OST_DISCONNECT:
581                 CDEBUG(D_INODE, "disconnect\n");
582                 OBD_CHECK_DROP_PACKET(req, OBD_INST_OST_DISCONNECT);
583                 rc = ost_disconnect(ost, req);
584                 break;
585         case OST_GET_INFO:
586                 CDEBUG(D_INODE, "get_info\n");
587                 OBD_CHECK_DROP_PACKET(req, OBD_INST_OST_GET_INFO);
588                 rc = ost_get_info(ost, req);
589                 break;
590         case OST_CREATE:
591                 CDEBUG(D_INODE, "create\n");
592                 OBD_CHECK_DROP_PACKET(req, OBD_INST_OST_CREATE);
593                 rc = ost_create(ost, req);
594                 break;
595         case OST_DESTROY:
596                 CDEBUG(D_INODE, "destroy\n");
597                 OBD_CHECK_DROP_PACKET(req, OBD_INST_OST_DESTROY);
598                 rc = ost_destroy(ost, req);
599                 break;
600         case OST_GETATTR:
601                 CDEBUG(D_INODE, "getattr\n");
602                 OBD_CHECK_DROP_PACKET(req, OBD_INST_OST_GETATTR);
603                 rc = ost_getattr(ost, req);
604                 break;
605         case OST_SETATTR:
606                 CDEBUG(D_INODE, "setattr\n");
607                 OBD_CHECK_DROP_PACKET(req, OBD_INST_OST_SETATTR);
608                 rc = ost_setattr(ost, req);
609                 break;
610         case OST_OPEN:
611                 CDEBUG(D_INODE, "setattr\n");
612                 OBD_CHECK_DROP_PACKET(req, OBD_INST_OST_OPEN);
613                 rc = ost_open(ost, req);
614                 break;
615         case OST_CLOSE:
616                 CDEBUG(D_INODE, "setattr\n");
617                 OBD_CHECK_DROP_PACKET(req, OBD_INST_OST_CLOSE);
618                 rc = ost_close(ost, req);
619                 break;
620         case OST_BRW:
621                 CDEBUG(D_INODE, "brw\n");
622                 OBD_CHECK_DROP_PACKET(req, OBD_INST_OST_BRW);
623                 rc = ost_brw(ost, req);
624                 break;
625         case OST_PUNCH:
626                 CDEBUG(D_INODE, "punch\n");
627                 OBD_CHECK_DROP_PACKET(req, OBD_INST_OST_PUNCH);
628                 rc = ost_punch(ost, req);
629                 break;
630         default:
631                 req->rq_status = -ENOTSUPP;
632                 rc = ptlrpc_error(obddev, svc, req);
633                 RETURN(rc);
634         }
635
636         EXIT;
637 out:
638         req->rq_status = rc;
639         if (rc) {
640                 CERROR("ost: processing error %d\n", rc);
641                 ptlrpc_error(obddev, svc, req);
642         } else {
643                 CDEBUG(D_INODE, "sending reply\n");
644                 ptlrpc_reply(obddev, svc, req);
645         }
646
647         return 0;
648 }
649
650
651 /* mount the file system (secretly) */
652 static int ost_setup(struct obd_device *obddev, obd_count len,
653                         void *buf)
654
655 {
656         struct obd_ioctl_data* data = buf;
657         struct ost_obd *ost = &obddev->u.ost;
658         struct obd_device *tgt;
659         int err;
660         ENTRY;
661
662         if (data->ioc_dev  < 0 || data->ioc_dev > MAX_OBD_DEVICES)
663                 RETURN(-ENODEV);
664
665         tgt = &obd_dev[data->ioc_dev];
666         ost->ost_tgt = tgt;
667         if ( ! (tgt->obd_flags & OBD_ATTACHED) ||
668              ! (tgt->obd_flags & OBD_SET_UP) ){
669                 CERROR("device not attached or not set up (%d)\n",
670                        data->ioc_dev);
671                 RETURN(-EINVAL);
672         }
673
674         ost->ost_conn.oc_dev = tgt;
675         err = obd_connect(&ost->ost_conn);
676         if (err) {
677                 CERROR("fail to connect to device %d\n", data->ioc_dev);
678                 RETURN(-EINVAL);
679         }
680
681         ost->ost_service = ptlrpc_init_svc( 2 * 1024, 
682                                             OST_REQUEST_PORTAL,
683                                             OSC_REPLY_PORTAL,
684                                             "self",
685                                             ost_unpack_req,
686                                             ost_pack_rep,
687                                             ost_handle);
688         if (!ost->ost_service) {
689                 obd_disconnect(&ost->ost_conn);
690                 RETURN(-EINVAL);
691         }
692
693         rpc_register_service(ost->ost_service, "self");
694
695         err = ptlrpc_start_thread(obddev, ost->ost_service, "lustre_ost");
696         if (err) {
697                 obd_disconnect(&ost->ost_conn);
698                 RETURN(-EINVAL);
699         }
700
701         MOD_INC_USE_COUNT;
702         RETURN(0);
703 }
704
705 static int ost_cleanup(struct obd_device * obddev)
706 {
707         struct ost_obd *ost = &obddev->u.ost;
708         int err;
709
710         ENTRY;
711
712         if ( !list_empty(&obddev->obd_gen_clients) ) {
713                 CERROR("still has clients!\n");
714                 RETURN(-EBUSY);
715         }
716
717         ptlrpc_stop_thread(ost->ost_service);
718         rpc_unregister_service(ost->ost_service);
719
720         if (!list_empty(&ost->ost_service->srv_reqs)) {
721                 // XXX reply with errors and clean up
722                 CERROR("Request list not empty!\n");
723         }
724         OBD_FREE(ost->ost_service, sizeof(*ost->ost_service));
725
726         err = obd_disconnect(&ost->ost_conn);
727         if (err) {
728                 CERROR("lustre ost: fail to disconnect device\n");
729                 RETURN(-EINVAL);
730         }
731
732         MOD_DEC_USE_COUNT;
733         RETURN(0);
734 }
735
736 /* use obd ops to offer management infrastructure */
737 static struct obd_ops ost_obd_ops = {
738         o_setup:       ost_setup,
739         o_cleanup:     ost_cleanup,
740 };
741
742 static int __init ost_init(void)
743 {
744         obd_register_type(&ost_obd_ops, LUSTRE_OST_NAME);
745         return 0;
746 }
747
748 static void __exit ost_exit(void)
749 {
750         obd_unregister_type(LUSTRE_OST_NAME);
751 }
752
753 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
754 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
755 MODULE_LICENSE("GPL");
756
757 module_init(ost_init);
758 module_exit(ost_exit);