Whamcloud - gitweb
ebc5d64786978b05ba503f58c8dd2e7168fc84ac
[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-2003 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 #ifndef EXPORT_SYMTAB
34 # define EXPORT_SYMTAB
35 #endif
36 #define DEBUG_SUBSYSTEM S_OST
37
38 #include <linux/module.h>
39 #include <linux/obd_ost.h>
40 #include <linux/lustre_net.h>
41 #include <linux/lustre_dlm.h>
42 #include <linux/lustre_export.h>
43 #include <linux/lustre_debug.h>
44 #include <linux/init.h>
45 #include <linux/lprocfs_status.h>
46 #include <linux/lustre_commit_confd.h>
47 #include <portals/list.h>
48 #include "ost_internal.h"
49
50 void oti_init(struct obd_trans_info *oti, struct ptlrpc_request *req)
51 {
52         if (oti == NULL)
53                 return;
54         memset(oti, 0, sizeof *oti);
55
56         if (req->rq_repmsg && req->rq_reqmsg != 0)
57                 oti->oti_transno = req->rq_repmsg->transno;
58 }
59
60 void oti_to_request(struct obd_trans_info *oti, struct ptlrpc_request *req)
61 {
62         struct oti_req_ack_lock *ack_lock;
63         int i;
64
65         if (oti == NULL)
66                 return;
67
68         if (req->rq_repmsg)
69                 req->rq_repmsg->transno = oti->oti_transno;
70
71         /* XXX 4 == entries in oti_ack_locks??? */
72         for (ack_lock = oti->oti_ack_locks, i = 0; i < 4; i++, ack_lock++) {
73                 if (!ack_lock->mode)
74                         break;
75                 /* XXX not even calling target_send_reply in some cases... */
76                 ptlrpc_save_lock (req, &ack_lock->lock, ack_lock->mode);
77         }
78 }
79
80 static int ost_destroy(struct obd_export *exp, struct ptlrpc_request *req, 
81                        struct obd_trans_info *oti)
82 {
83         struct ost_body *body, *repbody;
84         int rc, size = sizeof(*body);
85         ENTRY;
86
87         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
88         if (body == NULL)
89                 RETURN(-EFAULT);
90
91         rc = lustre_pack_reply(req, 1, &size, NULL);
92         if (rc)
93                 RETURN(rc);
94
95         if (body->oa.o_valid & OBD_MD_FLCOOKIE)
96                 oti->oti_logcookies = obdo_logcookie(&body->oa);
97         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
98         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
99         req->rq_status = obd_destroy(exp, &body->oa, NULL, oti);
100         RETURN(0);
101 }
102
103 static int ost_getattr(struct obd_export *exp, struct ptlrpc_request *req)
104 {
105         struct ost_body *body, *repbody;
106         int rc, size = sizeof(*body);
107         ENTRY;
108
109         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
110         if (body == NULL)
111                 RETURN(-EFAULT);
112
113         rc = lustre_pack_reply(req, 1, &size, NULL);
114         if (rc)
115                 RETURN(rc);
116
117         repbody = lustre_msg_buf (req->rq_repmsg, 0, sizeof(*repbody));
118         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
119         req->rq_status = obd_getattr(exp, &repbody->oa, NULL);
120         RETURN(0);
121 }
122
123 static int ost_statfs(struct ptlrpc_request *req)
124 {
125         struct obd_statfs *osfs;
126         int rc, size = sizeof(*osfs);
127         ENTRY;
128
129         rc = lustre_pack_reply(req, 1, &size, NULL);
130         if (rc)
131                 RETURN(rc);
132
133         osfs = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*osfs));
134
135         req->rq_status = obd_statfs(req->rq_export->exp_obd, osfs, jiffies-HZ);
136         if (req->rq_status != 0)
137                 CERROR("ost: statfs failed: rc %d\n", req->rq_status);
138
139         RETURN(0);
140 }
141
142 static int ost_create(struct obd_export *exp, struct ptlrpc_request *req,
143                       struct obd_trans_info *oti)
144 {
145         struct ost_body *body, *repbody;
146         int rc, size = sizeof(*repbody);
147         ENTRY;
148
149         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
150         if (body == NULL)
151                 RETURN(-EFAULT);
152
153         rc = lustre_pack_reply(req, 1, &size, NULL);
154         if (rc)
155                 RETURN(rc);
156
157         repbody = lustre_msg_buf (req->rq_repmsg, 0, sizeof(*repbody));
158         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
159         oti->oti_logcookies = obdo_logcookie(&repbody->oa);
160         req->rq_status = obd_create(exp, &repbody->oa, NULL, oti);
161         //obd_log_cancel(conn, NULL, 1, oti->oti_logcookies, 0);
162         RETURN(0);
163 }
164
165 static int ost_punch(struct obd_export *exp, struct ptlrpc_request *req, 
166                      struct obd_trans_info *oti)
167 {
168         struct ost_body *body, *repbody;
169         int rc, size = sizeof(*repbody);
170         ENTRY;
171
172         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
173         if (body == NULL)
174                 RETURN(-EFAULT);
175
176         if ((body->oa.o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
177             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
178                 RETURN(-EINVAL);
179
180         rc = lustre_pack_reply(req, 1, &size, NULL);
181         if (rc)
182                 RETURN(rc);
183
184         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
185         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
186         req->rq_status = obd_punch(exp, &repbody->oa, NULL, repbody->oa.o_size,
187                                    repbody->oa.o_blocks, oti);
188         RETURN(0);
189 }
190
191 static int ost_sync(struct obd_export *exp, struct ptlrpc_request *req)
192 {
193         struct ost_body *body, *repbody;
194         int rc, size = sizeof(*repbody);
195         ENTRY;
196
197         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
198         if (body == NULL)
199                 RETURN(-EFAULT);
200
201         rc = lustre_pack_reply(req, 1, &size, NULL);
202         if (rc)
203                 RETURN(rc);
204
205         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
206         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
207         req->rq_status = obd_sync(exp, &repbody->oa, NULL, repbody->oa.o_size,
208                                   repbody->oa.o_blocks);
209         RETURN(0);
210 }
211
212 static int ost_setattr(struct obd_export *exp, struct ptlrpc_request *req, 
213                        struct obd_trans_info *oti)
214 {
215         struct ost_body *body, *repbody;
216         int rc, size = sizeof(*repbody);
217         ENTRY;
218
219         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
220         if (body == NULL)
221                 RETURN(-EFAULT);
222
223         rc = lustre_pack_reply(req, 1, &size, NULL);
224         if (rc)
225                 RETURN(rc);
226
227         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
228         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
229
230         req->rq_status = obd_setattr(exp, &repbody->oa, NULL, oti);
231         RETURN(0);
232 }
233
234 static int ost_bulk_timeout(void *data)
235 {
236         ENTRY;
237         /* We don't fail the connection here, because having the export
238          * killed makes the (vital) call to commitrw very sad.
239          */
240         RETURN(1);
241 }
242
243 static int get_per_page_niobufs(struct obd_ioobj *ioo, int nioo,
244                                 struct niobuf_remote *rnb, int nrnb,
245                                 struct niobuf_remote **pp_rnbp)
246 {
247         /* Copy a remote niobuf, splitting it into page-sized chunks
248          * and setting ioo[i].ioo_bufcnt accordingly */
249         struct niobuf_remote *pp_rnb;
250         int   i;
251         int   j;
252         int   page;
253         int   rnbidx = 0;
254         int   npages = 0;
255
256         /* first count and check the number of pages required */
257         for (i = 0; i < nioo; i++)
258                 for (j = 0; j < ioo->ioo_bufcnt; j++, rnbidx++) {
259                         obd_off offset = rnb[rnbidx].offset;
260                         obd_off p0 = offset >> PAGE_SHIFT;
261                         obd_off pn = (offset + rnb[rnbidx].len - 1)>>PAGE_SHIFT;
262
263                         LASSERT(rnbidx < nrnb);
264
265                         npages += (pn + 1 - p0);
266
267                         if (rnb[rnbidx].len == 0) {
268                                 CERROR("zero len BRW: obj %d objid "LPX64
269                                        " buf %u\n", i, ioo[i].ioo_id, j);
270                                 return -EINVAL;
271                         }
272                         if (j > 0 &&
273                             rnb[rnbidx].offset <= rnb[rnbidx-1].offset) {
274                                 CERROR("unordered BRW: obj %d objid "LPX64
275                                        " buf %u offset "LPX64" <= "LPX64"\n",
276                                        i, ioo[i].ioo_id, j, rnb[rnbidx].offset,
277                                        rnb[rnbidx].offset);
278                                 return -EINVAL;
279                         }
280                 }
281
282         LASSERT(rnbidx == nrnb);
283
284         if (npages == nrnb) {       /* all niobufs are for single pages */
285                 *pp_rnbp = rnb;
286                 return npages;
287         }
288
289         OBD_ALLOC(pp_rnb, sizeof(*pp_rnb) * npages);
290         if (pp_rnb == NULL)
291                 return -ENOMEM;
292
293         /* now do the actual split */
294         page = rnbidx = 0;
295         for (i = 0; i < nioo; i++) {
296                 int  obj_pages = 0;
297
298                 for (j = 0; j < ioo[i].ioo_bufcnt; j++, rnbidx++) {
299                         obd_off off = rnb[rnbidx].offset;
300                         int     nob = rnb[rnbidx].len;
301
302                         LASSERT(rnbidx < nrnb);
303                         do {
304                                 obd_off  poff = off & (PAGE_SIZE - 1);
305                                 int      pnob = (poff + nob > PAGE_SIZE) ?
306                                                 PAGE_SIZE - poff : nob;
307
308                                 LASSERT(page < npages);
309                                 pp_rnb[page].len = pnob;
310                                 pp_rnb[page].offset = off;
311                                 pp_rnb[page].flags = rnb[rnbidx].flags;
312
313                                 CDEBUG(0, "   obj %d id "LPX64
314                                        "page %d(%d) "LPX64" for %d, flg %x\n",
315                                        i, ioo[i].ioo_id, obj_pages, page,
316                                        pp_rnb[page].offset, pp_rnb[page].len,
317                                        pp_rnb[page].flags);
318                                 page++;
319                                 obj_pages++;
320
321                                 off += pnob;
322                                 nob -= pnob;
323                         } while (nob > 0);
324                         LASSERT(nob == 0);
325                 }
326                 ioo[i].ioo_bufcnt = obj_pages;
327         }
328         LASSERT(page == npages);
329
330         *pp_rnbp = pp_rnb;
331         return npages;
332 }
333
334 static void free_per_page_niobufs (int npages, struct niobuf_remote *pp_rnb,
335                                    struct niobuf_remote *rnb)
336 {
337         if (pp_rnb == rnb)                      /* didn't allocate above */
338                 return;
339
340         OBD_FREE(pp_rnb, sizeof(*pp_rnb) * npages);
341 }
342
343 #if CHECKSUM_BULK
344 obd_count ost_checksum_bulk(struct ptlrpc_bulk_desc *desc)
345 {
346         obd_count cksum = 0;
347         int i;
348
349         for (i = 0; i < desc->bd_iov_count; i++) {
350                 struct page *page = desc->bd_iov[i].kiov_page;
351                 char *ptr = kmap(page);
352                 int psum, off = desc->bd_iov[i].kiov_offset & ~PAGE_MASK;
353                 int count = desc->bd_iov[i].kiov_len;
354
355                 while (count > 0) {
356                         ost_checksum(&cksum, &psum, ptr + off,
357                                      count > CHECKSUM_CHUNK ?
358                                      CHECKSUM_CHUNK : count);
359                         LL_CDEBUG_PAGE(D_PAGE, page, "off %d checksum %x\n",
360                                        off, psum);
361                         off += CHECKSUM_CHUNK;
362                         count -= CHECKSUM_CHUNK;
363                 }
364                 kunmap(page);
365         }
366
367         return cksum;
368 }
369 #endif
370
371 static int ost_brw_read(struct ptlrpc_request *req, struct obd_trans_info *oti)
372 {
373         struct ptlrpc_bulk_desc *desc;
374         struct niobuf_remote    *remote_nb;
375         struct niobuf_remote    *pp_rnb;
376         struct niobuf_local     *local_nb;
377         struct obd_ioobj        *ioo;
378         struct ost_body         *body, *repbody;
379         struct l_wait_info       lwi;
380         int                      size[1] = { sizeof(*body) };
381         int                      comms_error = 0;
382         int                      niocount;
383         int                      npages;
384         int                      nob = 0;
385         int                      rc;
386         int                      i;
387         ENTRY;
388
389         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
390                 GOTO(out, rc = -EIO);
391
392         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK | OBD_FAIL_ONCE,
393                          (obd_timeout + 1) / 4);
394
395         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
396         if (body == NULL) {
397                 CERROR("Missing/short ost_body\n");
398                 GOTO(out, rc = -EFAULT);
399         }
400
401         ioo = lustre_swab_reqbuf(req, 1, sizeof(*ioo), lustre_swab_obd_ioobj);
402         if (ioo == NULL) {
403                 CERROR("Missing/short ioobj\n");
404                 GOTO(out, rc = -EFAULT);
405         }
406
407         niocount = ioo->ioo_bufcnt;
408         remote_nb = lustre_swab_reqbuf(req, 2, niocount * sizeof(*remote_nb),
409                                        lustre_swab_niobuf_remote);
410         if (remote_nb == NULL) {
411                 CERROR("Missing/short niobuf\n");
412                 GOTO(out, rc = -EFAULT);
413         }
414         if (lustre_msg_swabbed(req->rq_reqmsg)) { /* swab remaining niobufs */
415                 for (i = 1; i < niocount; i++)
416                         lustre_swab_niobuf_remote (&remote_nb[i]);
417         }
418
419         rc = lustre_pack_reply(req, 1, size, NULL);
420         if (rc)
421                 GOTO(out, rc);
422
423         /* FIXME all niobuf splitting should be done in obdfilter if needed */
424         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
425         npages = get_per_page_niobufs(ioo, 1, remote_nb, niocount, &pp_rnb);
426         if (npages < 0)
427                 GOTO(out, rc = npages);
428
429         OBD_ALLOC(local_nb, sizeof(*local_nb) * npages);
430         if (local_nb == NULL)
431                 GOTO(out_pp_rnb, rc = -ENOMEM);
432
433         desc = ptlrpc_prep_bulk_exp (req, npages, 
434                                      BULK_PUT_SOURCE, OST_BULK_PORTAL);
435         if (desc == NULL)
436                 GOTO(out_local, rc = -ENOMEM);
437
438         rc = obd_preprw(OBD_BRW_READ, req->rq_export, &body->oa, 1,
439                         ioo, npages, pp_rnb, local_nb, oti);
440         if (rc != 0)
441                 GOTO(out_bulk, rc);
442
443         /* We're finishing using body->oa as an input variable */
444         body->oa.o_valid = 0;
445
446         nob = 0;
447         for (i = 0; i < npages; i++) {
448                 int page_rc = local_nb[i].rc;
449
450                 if (page_rc < 0) {              /* error */
451                         rc = page_rc;
452                         break;
453                 }
454
455                 LASSERT(page_rc <= pp_rnb[i].len);
456                 nob += page_rc;
457                 if (page_rc != 0) {             /* some data! */
458                         LASSERT (local_nb[i].page != NULL);
459                         ptlrpc_prep_bulk_page(desc, local_nb[i].page,
460                                               pp_rnb[i].offset & (PAGE_SIZE-1),
461                                               page_rc);
462                 }
463
464                 if (page_rc != pp_rnb[i].len) { /* short read */
465                         /* All subsequent pages should be 0 */
466                         while(++i < npages)
467                                 LASSERT(local_nb[i].rc == 0);
468                         break;
469                 }
470         }
471
472         if (rc == 0) {
473                 rc = ptlrpc_start_bulk_transfer(desc);
474                 if (rc == 0) {
475                         lwi = LWI_TIMEOUT(obd_timeout * HZ / 4,
476                                           ost_bulk_timeout, desc);
477                         rc = l_wait_event(desc->bd_waitq,
478                                           !ptlrpc_bulk_active(desc), &lwi);
479                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
480                         if (rc == -ETIMEDOUT) {
481                                 DEBUG_REQ(D_ERROR, req, "timeout on bulk PUT");
482                                 ptlrpc_abort_bulk(desc);
483                         } else if (!desc->bd_success ||
484                                    desc->bd_nob_transferred != desc->bd_nob) {
485                                 DEBUG_REQ(D_ERROR, req, "%s bulk PUT %d(%d)",
486                                           desc->bd_success ?
487                                           "truncated" : "network error on",
488                                           desc->bd_nob_transferred,
489                                           desc->bd_nob);
490                                 /* XXX should this be a different errno? */
491                                 rc = -ETIMEDOUT;
492                         }
493                 } else {
494                         DEBUG_REQ(D_ERROR, req, "bulk PUT failed: rc %d\n", rc);
495                 }
496                 comms_error = rc != 0;
497         }
498
499         /* Must commit after prep above in all cases */
500         rc = obd_commitrw(OBD_BRW_READ, req->rq_export, &body->oa, 1,
501                           ioo, npages, local_nb, oti, rc);
502
503         if (rc == 0) {
504                 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
505                 memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
506
507 #if CHECKSUM_BULK
508                 repbody->oa.o_cksum = ost_checksum_bulk(desc);
509                 repbody->oa.o_valid |= OBD_MD_FLCKSUM;
510 #endif
511         }
512
513  out_bulk:
514         ptlrpc_free_bulk(desc);
515  out_local:
516         OBD_FREE(local_nb, sizeof(*local_nb) * npages);
517  out_pp_rnb:
518         free_per_page_niobufs(npages, pp_rnb, remote_nb);
519  out:
520         LASSERT(rc <= 0);
521         if (rc == 0) {
522                 req->rq_status = nob;
523                 target_committed_to_req(req);
524                 ptlrpc_reply(req);
525         } else if (!comms_error) {
526                 /* only reply if comms OK */
527                 target_committed_to_req(req);
528                 req->rq_status = rc;
529                 ptlrpc_error(req);
530         } else {
531                 if (req->rq_reqmsg->conn_cnt == req->rq_export->exp_conn_cnt) {
532                         CERROR("bulk IO comms error: "
533                                "evicting %s@%s id %s\n",
534                                req->rq_export->exp_client_uuid.uuid,
535                                req->rq_export->exp_connection->c_remote_uuid.uuid,
536                                req->rq_peerstr);
537                         ptlrpc_fail_export(req->rq_export);
538                 } else {
539                         CERROR("ignoring bulk IO comms error: "
540                                "client reconnected %s@%s id %s\n",  
541                                req->rq_export->exp_client_uuid.uuid,
542                                req->rq_export->exp_connection->c_remote_uuid.uuid,
543                                req->rq_peerstr);
544                 }
545         }
546
547         RETURN(rc);
548 }
549
550 static int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
551 {
552         struct ptlrpc_bulk_desc *desc;
553         struct niobuf_remote    *remote_nb;
554         struct niobuf_remote    *pp_rnb;
555         struct niobuf_local     *local_nb;
556         struct obd_ioobj        *ioo;
557         struct ost_body         *body, *repbody;
558         struct l_wait_info       lwi;
559         __u32                   *rcs;
560         int                      size[2] = { sizeof(*body) };
561         int                      objcount, niocount, npages;
562         int                      comms_error = 0;
563         int                      rc, swab, i, j;
564         ENTRY;
565
566         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
567                 GOTO(out, rc = -EIO);
568
569         /* pause before transaction has been started */
570         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK | OBD_FAIL_ONCE,
571                          (obd_timeout + 1) / 4);
572
573         swab = lustre_msg_swabbed(req->rq_reqmsg);
574         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
575         if (body == NULL) {
576                 CERROR("Missing/short ost_body\n");
577                 GOTO(out, rc = -EFAULT);
578         }
579
580         LASSERT_REQSWAB(req, 1);
581         objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
582         if (objcount == 0) {
583                 CERROR("Missing/short ioobj\n");
584                 GOTO(out, rc = -EFAULT);
585         }
586         ioo = lustre_msg_buf (req->rq_reqmsg, 1, objcount * sizeof(*ioo));
587         LASSERT (ioo != NULL);
588         for (niocount = i = 0; i < objcount; i++) {
589                 if (swab)
590                         lustre_swab_obd_ioobj (&ioo[i]);
591                 if (ioo[i].ioo_bufcnt == 0) {
592                         CERROR("ioo[%d] has zero bufcnt\n", i);
593                         GOTO(out, rc = -EFAULT);
594                 }
595                 niocount += ioo[i].ioo_bufcnt;
596         }
597
598         remote_nb = lustre_swab_reqbuf(req, 2, niocount * sizeof(*remote_nb),
599                                        lustre_swab_niobuf_remote);
600         if (remote_nb == NULL) {
601                 CERROR("Missing/short niobuf\n");
602                 GOTO(out, rc = -EFAULT);
603         }
604         if (swab) {                             /* swab the remaining niobufs */
605                 for (i = 1; i < niocount; i++)
606                         lustre_swab_niobuf_remote (&remote_nb[i]);
607         }
608
609         size[1] = niocount * sizeof(*rcs);
610         rc = lustre_pack_reply(req, 2, size, NULL);
611         if (rc != 0)
612                 GOTO(out, rc);
613         rcs = lustre_msg_buf(req->rq_repmsg, 1, niocount * sizeof(*rcs));
614
615         /* FIXME all niobuf splitting should be done in obdfilter if needed */
616         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
617         npages = get_per_page_niobufs(ioo, objcount,remote_nb,niocount,&pp_rnb);
618         if (npages < 0)
619                 GOTO(out, rc = npages);
620
621         OBD_ALLOC(local_nb, sizeof(*local_nb) * npages);
622         if (local_nb == NULL)
623                 GOTO(out_pp_rnb, rc = -ENOMEM);
624
625         desc = ptlrpc_prep_bulk_exp (req, npages, 
626                                      BULK_GET_SINK, OST_BULK_PORTAL);
627         if (desc == NULL)
628                 GOTO(out_local, rc = -ENOMEM);
629
630         rc = obd_preprw(OBD_BRW_WRITE, req->rq_export, &body->oa, objcount,
631                         ioo, npages, pp_rnb, local_nb, oti);
632         if (rc != 0)
633                 GOTO(out_bulk, rc);
634
635         /* NB Having prepped, we must commit... */
636
637         for (i = 0; i < npages; i++)
638                 ptlrpc_prep_bulk_page(desc, local_nb[i].page, 
639                                       pp_rnb[i].offset & (PAGE_SIZE - 1),
640                                       pp_rnb[i].len);
641
642         rc = ptlrpc_start_bulk_transfer (desc);
643         if (rc == 0) {
644                 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4,
645                                   ost_bulk_timeout, desc);
646                 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), 
647                                   &lwi);
648                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
649                 if (rc == -ETIMEDOUT) {
650                         DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
651                         ptlrpc_abort_bulk(desc);
652                 } else if (!desc->bd_success ||
653                            desc->bd_nob_transferred != desc->bd_nob) {
654                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
655                                   desc->bd_success ? 
656                                   "truncated" : "network error on",
657                                   desc->bd_nob_transferred, desc->bd_nob);
658                         /* XXX should this be a different errno? */
659                         rc = -ETIMEDOUT;
660                 }
661         } else {
662                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d\n", rc);
663         }
664         comms_error = rc != 0;
665
666         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
667         memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
668
669 #if CHECKSUM_BULK
670         if (rc == 0 && (body->oa.o_valid & OBD_MD_FLCKSUM) != 0) {
671                 static int cksum_counter;
672                 obd_count client_cksum = body->oa.o_cksum;
673                 obd_count cksum = ost_checksum_bulk(desc);
674
675                 if (client_cksum != cksum) {
676                         CERROR("Bad checksum: client %x, server %x id %s\n",
677                                client_cksum, cksum,
678                                req->rq_peerstr);
679                         cksum_counter = 1;
680                         repbody->oa.o_cksum = cksum;
681                 } else {
682                         cksum_counter++;
683                         if ((cksum_counter & (-cksum_counter)) == cksum_counter)
684                                 CWARN("Checksum %u from %s: %x OK\n",
685                                       cksum_counter,
686                                       req->rq_peerstr,
687                                       cksum);
688                 }
689         }
690 #endif
691         /* Must commit after prep above in all cases */
692         rc = obd_commitrw(OBD_BRW_WRITE, req->rq_export, &repbody->oa,
693                            objcount, ioo, npages, local_nb, oti, rc);
694
695         if (rc == 0) {
696                 /* set per-requested niobuf return codes */
697                 for (i = j = 0; i < niocount; i++) {
698                         int nob = remote_nb[i].len;
699
700                         rcs[i] = 0;
701                         do {
702                                 LASSERT(j < npages);
703                                 if (local_nb[j].rc < 0)
704                                         rcs[i] = local_nb[j].rc;
705                                 nob -= pp_rnb[j].len;
706                                 j++;
707                         } while (nob > 0);
708                         LASSERT(nob == 0);
709                 }
710                 LASSERT(j == npages);
711         }
712
713  out_bulk:
714         ptlrpc_free_bulk(desc);
715  out_local:
716         OBD_FREE(local_nb, sizeof(*local_nb) * npages);
717  out_pp_rnb:
718         free_per_page_niobufs(npages, pp_rnb, remote_nb);
719  out:
720         if (rc == 0) {
721                 oti_to_request(oti, req);
722                 target_committed_to_req(req);
723                 rc = ptlrpc_reply(req);
724         } else if (!comms_error) {
725                 /* Only reply if there was no comms problem with bulk */
726                 target_committed_to_req(req);
727                 req->rq_status = rc;
728                 ptlrpc_error(req);
729         } else {
730                 if (req->rq_reqmsg->conn_cnt == req->rq_export->exp_conn_cnt) {
731                         CERROR("%s: bulk IO comm error evicting %s@%s id %s\n",
732                                req->rq_export->exp_obd->obd_name,
733                                req->rq_export->exp_client_uuid.uuid,
734                                req->rq_export->exp_connection->c_remote_uuid.uuid,
735                                req->rq_peerstr);
736                         ptlrpc_fail_export(req->rq_export);
737                 } else {
738                         CERROR("ignoring bulk IO comms error: "
739                                "client reconnected %s@%s id %s\n",
740                                req->rq_export->exp_client_uuid.uuid,
741                                req->rq_export->exp_connection->c_remote_uuid.uuid,
742                                req->rq_peerstr);
743                 }
744         }
745         RETURN(rc);
746 }
747
748 static int ost_san_brw(struct ptlrpc_request *req, int cmd)
749 {
750         struct niobuf_remote *remote_nb, *res_nb, *pp_rnb;
751         struct obd_ioobj *ioo;
752         struct ost_body *body, *repbody;
753         int rc, i, objcount, niocount, size[2] = {sizeof(*body)}, npages;
754         int swab;
755         ENTRY;
756
757         /* XXX not set to use latest protocol */
758
759         swab = lustre_msg_swabbed(req->rq_reqmsg);
760         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
761         if (body == NULL) {
762                 CERROR("Missing/short ost_body\n");
763                 GOTO(out, rc = -EFAULT);
764         }
765
766         ioo = lustre_swab_reqbuf(req, 1, sizeof(*ioo), lustre_swab_obd_ioobj);
767         if (ioo == NULL) {
768                 CERROR("Missing/short ioobj\n");
769                 GOTO(out, rc = -EFAULT);
770         }
771         objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
772         niocount = ioo[0].ioo_bufcnt;
773         for (i = 1; i < objcount; i++) {
774                 if (swab)
775                         lustre_swab_obd_ioobj (&ioo[i]);
776                 niocount += ioo[i].ioo_bufcnt;
777         }
778
779         remote_nb = lustre_swab_reqbuf(req, 2, niocount * sizeof(*remote_nb),
780                                        lustre_swab_niobuf_remote);
781         if (remote_nb == NULL) {
782                 CERROR("Missing/short niobuf\n");
783                 GOTO(out, rc = -EFAULT);
784         }
785         if (swab) {                             /* swab the remaining niobufs */
786                 for (i = 1; i < niocount; i++)
787                         lustre_swab_niobuf_remote (&remote_nb[i]);
788         }
789
790         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
791         npages = get_per_page_niobufs(ioo, objcount,remote_nb,niocount,&pp_rnb);
792         if (npages < 0)
793                 GOTO (out, rc = npages);
794  
795         size[1] = npages * sizeof(*pp_rnb);
796         rc = lustre_pack_reply(req, 2, size, NULL);
797         if (rc)
798                 GOTO(out_pp_rnb, rc);
799
800         req->rq_status = obd_san_preprw(cmd, req->rq_export, &body->oa,
801                                         objcount, ioo, npages, pp_rnb);
802
803         if (req->rq_status)
804                 GOTO(out_pp_rnb, rc = 0);
805
806         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
807         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
808
809         res_nb = lustre_msg_buf(req->rq_repmsg, 1, size[1]);
810         memcpy(res_nb, remote_nb, size[1]);
811         rc = 0;
812 out_pp_rnb:
813         free_per_page_niobufs(npages, pp_rnb, remote_nb);
814 out:
815         target_committed_to_req(req);
816         if (rc) {
817                 req->rq_status = rc;
818                 ptlrpc_error(req);
819         } else {
820                 ptlrpc_reply(req);
821         }
822
823         return rc;
824 }
825
826
827 static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
828 {
829         char *key;
830         int keylen, rc = 0;
831         ENTRY;
832
833         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
834         if (key == NULL) {
835                 DEBUG_REQ(D_HA, req, "no set_info key");
836                 RETURN(-EFAULT);
837         }
838         keylen = req->rq_reqmsg->buflens[0];
839
840         rc = lustre_pack_reply(req, 0, NULL, NULL);
841         if (rc)
842                 RETURN(rc);
843
844         rc = obd_set_info(exp, keylen, key, 0, NULL);
845         req->rq_repmsg->status = 0;
846         RETURN(rc);
847 }
848
849 static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
850 {
851         char *key;
852         int keylen, rc = 0, size = sizeof(obd_id);
853         obd_id *reply;
854         ENTRY;
855
856         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
857         if (key == NULL) {
858                 DEBUG_REQ(D_HA, req, "no get_info key");
859                 RETURN(-EFAULT);
860         }
861         keylen = req->rq_reqmsg->buflens[0];
862
863         if (keylen < strlen("last_id") || memcmp(key, "last_id", 7) != 0)
864                 RETURN(-EPROTO);
865
866         rc = lustre_pack_reply(req, 1, &size, NULL);
867         if (rc)
868                 RETURN(rc);
869
870         reply = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply));
871         rc = obd_get_info(exp, keylen, key, &size, reply);
872         req->rq_repmsg->status = 0;
873         RETURN(rc);
874 }
875
876 static int ost_filter_recovery_request(struct ptlrpc_request *req,
877                                        struct obd_device *obd, int *process)
878 {
879         switch (req->rq_reqmsg->opc) {
880         case OST_CONNECT: /* This will never get here, but for completeness. */
881         case OST_DISCONNECT:
882                *process = 1;
883                RETURN(0);
884
885         case OBD_PING:
886         case OST_CREATE:
887         case OST_DESTROY:
888         case OST_PUNCH:
889         case OST_SETATTR:
890         case OST_SYNC:
891         case OST_WRITE:
892         case OBD_LOG_CANCEL:
893         case LDLM_ENQUEUE:
894                 *process = target_queue_recovery_request(req, obd);
895                 RETURN(0);
896
897         default:
898                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
899                 *process = 0;
900                 /* XXX what should we set rq_status to here? */
901                 req->rq_status = -EAGAIN;
902                 RETURN(ptlrpc_error(req));
903         }
904 }
905
906
907
908 static int ost_handle(struct ptlrpc_request *req)
909 {
910         struct obd_trans_info trans_info = { 0, };
911         struct obd_trans_info *oti = &trans_info;
912         int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
913         struct obd_device *obd = NULL;
914         ENTRY;
915
916         LASSERT(current->journal_info == NULL);
917         /* XXX identical to MDS */
918         if (req->rq_reqmsg->opc != OST_CONNECT) {
919                 int abort_recovery, recovering;
920
921                 if (req->rq_export == NULL) {
922                         CDEBUG(D_HA, "operation %d on unconnected OST\n",
923                                req->rq_reqmsg->opc);
924                         req->rq_status = -ENOTCONN;
925                         GOTO(out, rc = -ENOTCONN);
926                 }
927
928                 obd = req->rq_export->exp_obd;
929
930                 /* Check for aborted recovery. */
931                 spin_lock_bh(&obd->obd_processing_task_lock);
932                 abort_recovery = obd->obd_abort_recovery;
933                 recovering = obd->obd_recovering;
934                 spin_unlock_bh(&obd->obd_processing_task_lock);
935                 if (abort_recovery) {
936                         target_abort_recovery(obd);
937                 } else if (recovering) {
938                         rc = ost_filter_recovery_request(req, obd,
939                                                          &should_process);
940                         if (rc || !should_process)
941                                 RETURN(rc);
942                 }
943         }
944
945         oti_init(oti, req);
946
947         switch (req->rq_reqmsg->opc) {
948         case OST_CONNECT: {
949                 CDEBUG(D_INODE, "connect\n");
950                 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
951                 rc = target_handle_connect(req, ost_handle);
952                 if (!rc)
953                         obd = req->rq_export->exp_obd;
954                 break;
955         }
956         case OST_DISCONNECT:
957                 CDEBUG(D_INODE, "disconnect\n");
958                 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
959                 rc = target_handle_disconnect(req);
960                 break;
961         case OST_CREATE:
962                 CDEBUG(D_INODE, "create\n");
963                 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
964                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_ENOSPC))
965                         GOTO(out, rc = -ENOSPC);
966                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
967                         GOTO(out, rc = -EROFS);
968                 rc = ost_create(req->rq_export, req, oti);
969                 break;
970         case OST_DESTROY:
971                 CDEBUG(D_INODE, "destroy\n");
972                 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
973                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
974                         GOTO(out, rc = -EROFS);
975                 rc = ost_destroy(req->rq_export, req, oti);
976                 break;
977         case OST_GETATTR:
978                 CDEBUG(D_INODE, "getattr\n");
979                 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
980                 rc = ost_getattr(req->rq_export, req);
981                 break;
982         case OST_SETATTR:
983                 CDEBUG(D_INODE, "setattr\n");
984                 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
985                 rc = ost_setattr(req->rq_export, req, oti);
986                 break;
987         case OST_WRITE:
988                 CDEBUG(D_INODE, "write\n");
989                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
990                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_ENOSPC))
991                         GOTO(out, rc = -ENOSPC);
992                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
993                         GOTO(out, rc = -EROFS);
994                 rc = ost_brw_write(req, oti);
995                 LASSERT(current->journal_info == NULL);
996                 /* ost_brw sends its own replies */
997                 RETURN(rc);
998         case OST_READ:
999                 CDEBUG(D_INODE, "read\n");
1000                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1001                 rc = ost_brw_read(req, oti);
1002                 LASSERT(current->journal_info == NULL);
1003                 /* ost_brw sends its own replies */
1004                 RETURN(rc);
1005         case OST_SAN_READ:
1006                 CDEBUG(D_INODE, "san read\n");
1007                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1008                 rc = ost_san_brw(req, OBD_BRW_READ);
1009                 /* ost_san_brw sends its own replies */
1010                 RETURN(rc);
1011         case OST_SAN_WRITE:
1012                 CDEBUG(D_INODE, "san write\n");
1013                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1014                 rc = ost_san_brw(req, OBD_BRW_WRITE);
1015                 /* ost_san_brw sends its own replies */
1016                 RETURN(rc);
1017         case OST_PUNCH:
1018                 CDEBUG(D_INODE, "punch\n");
1019                 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
1020                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1021                         GOTO(out, rc = -EROFS);
1022                 rc = ost_punch(req->rq_export, req, oti);
1023                 break;
1024         case OST_STATFS:
1025                 CDEBUG(D_INODE, "statfs\n");
1026                 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
1027                 rc = ost_statfs(req);
1028                 break;
1029         case OST_SYNC:
1030                 CDEBUG(D_INODE, "sync\n");
1031                 OBD_FAIL_RETURN(OBD_FAIL_OST_SYNC_NET, 0);
1032                 rc = ost_sync(req->rq_export, req);
1033                 break;
1034         case OST_SET_INFO:
1035                 DEBUG_REQ(D_INODE, req, "set_info");
1036                 rc = ost_set_info(req->rq_export, req);
1037                 break;
1038         case OST_GET_INFO:
1039                 DEBUG_REQ(D_INODE, req, "get_info");
1040                 rc = ost_get_info(req->rq_export, req);
1041                 break;
1042         case OBD_PING:
1043                 DEBUG_REQ(D_INODE, req, "ping");
1044                 rc = target_handle_ping(req);
1045                 break;
1046         /* FIXME - just reply status */
1047         case LLOG_ORIGIN_CONNECT:
1048                 DEBUG_REQ(D_INODE, req, "log connect\n");
1049                 rc = llog_handle_connect(req); 
1050                 req->rq_status = rc;
1051                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1052                 if (rc)
1053                         RETURN(rc);
1054                 RETURN(ptlrpc_reply(req));
1055         case OBD_LOG_CANCEL:
1056                 CDEBUG(D_INODE, "log cancel\n");
1057                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1058                 rc = llog_origin_handle_cancel(req);
1059                 req->rq_status = rc;
1060                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1061                 if (rc)
1062                         RETURN(rc);
1063                 RETURN(ptlrpc_reply(req));
1064         case LDLM_ENQUEUE:
1065                 CDEBUG(D_INODE, "enqueue\n");
1066                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1067                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1068                                          ldlm_server_blocking_ast,
1069                                          ldlm_server_glimpse_ast);
1070                 fail = OBD_FAIL_OST_LDLM_REPLY_NET;
1071                 break;
1072         case LDLM_CONVERT:
1073                 CDEBUG(D_INODE, "convert\n");
1074                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1075                 rc = ldlm_handle_convert(req);
1076                 break;
1077         case LDLM_CANCEL:
1078                 CDEBUG(D_INODE, "cancel\n");
1079                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1080                 rc = ldlm_handle_cancel(req);
1081                 break;
1082         case LDLM_BL_CALLBACK:
1083         case LDLM_CP_CALLBACK:
1084                 CDEBUG(D_INODE, "callback\n");
1085                 CERROR("callbacks should not happen on OST\n");
1086                 /* fall through */
1087         default:
1088                 CERROR("Unexpected opcode %d\n", req->rq_reqmsg->opc);
1089                 req->rq_status = -ENOTSUPP;
1090                 rc = ptlrpc_error(req);
1091                 RETURN(rc);
1092         }
1093
1094         LASSERT(current->journal_info == NULL);
1095
1096         EXIT;
1097         /* If we're DISCONNECTing, the export_data is already freed */
1098         if (!rc && req->rq_reqmsg->opc != OST_DISCONNECT)
1099                 target_committed_to_req(req);
1100
1101 out:
1102         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
1103                 if (obd && obd->obd_recovering) {
1104                         DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
1105                         return target_queue_final_reply(req, rc);
1106                 }
1107                 /* Lost a race with recovery; let the error path DTRT. */
1108                 rc = req->rq_status = -ENOTCONN;
1109         }
1110
1111         if (!rc)
1112                 oti_to_request(oti, req);
1113
1114         target_send_reply(req, rc, fail);
1115         return 0;
1116 }
1117
1118 static int ost_setup(struct obd_device *obd, obd_count len, void *buf)
1119 {
1120         struct ost_obd *ost = &obd->u.ost;
1121         struct lprocfs_static_vars lvars;
1122         int rc;
1123         ENTRY;
1124
1125         rc = cleanup_group_info();
1126         if (rc)
1127                 RETURN(rc);
1128
1129         rc = llog_start_commit_thread();
1130         if (rc < 0)
1131                 RETURN(rc);
1132
1133         lprocfs_init_vars(ost, &lvars);
1134         lprocfs_obd_setup(obd, lvars.obd_vars);
1135
1136         ost->ost_service =
1137                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1138                                 OST_REQUEST_PORTAL, OSC_REPLY_PORTAL, 30000,
1139                                 ost_handle, "ost",
1140                                 obd->obd_proc_entry,
1141                                 ost_print_req);
1142         if (ost->ost_service == NULL) {
1143                 CERROR("failed to start service\n");
1144                 GOTO(out_lprocfs, rc = -ENOMEM);
1145         }
1146
1147         rc = ptlrpc_start_n_threads(obd, ost->ost_service, OST_NUM_THREADS,
1148                                     "ll_ost");
1149         if (rc)
1150                 GOTO(out_service, rc = -EINVAL);
1151
1152         ost->ost_create_service =
1153                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1154                                 OST_CREATE_PORTAL, OSC_REPLY_PORTAL, 30000,
1155                                 ost_handle, "ost_create",
1156                                 obd->obd_proc_entry,
1157                                 ost_print_req);
1158         if (ost->ost_create_service == NULL) {
1159                 CERROR("failed to start OST create service\n");
1160                 GOTO(out_service, rc = -ENOMEM);
1161         }
1162
1163         rc = ptlrpc_start_n_threads(obd, ost->ost_create_service, 1,
1164                                     "ll_ost_creat");
1165         if (rc)
1166                 GOTO(out_create, rc = -EINVAL);
1167
1168         RETURN(0);
1169
1170 out_create:
1171         ptlrpc_unregister_service(ost->ost_create_service);
1172 out_service:
1173         ptlrpc_unregister_service(ost->ost_service);
1174 out_lprocfs:
1175         lprocfs_obd_cleanup(obd);
1176         RETURN(rc);
1177 }
1178
1179 static int ost_cleanup(struct obd_device *obd, int flags)
1180 {
1181         struct ost_obd *ost = &obd->u.ost;
1182         int err = 0;
1183         ENTRY;
1184
1185         spin_lock_bh(&obd->obd_processing_task_lock);
1186         if (obd->obd_recovering) {
1187                 target_cancel_recovery_timer(obd);
1188                 obd->obd_recovering = 0;
1189         }
1190         spin_unlock_bh(&obd->obd_processing_task_lock);
1191
1192         ptlrpc_unregister_service(ost->ost_service);
1193         ptlrpc_unregister_service(ost->ost_create_service);
1194
1195         lprocfs_obd_cleanup(obd);
1196
1197         RETURN(err);
1198 }
1199
1200 /* use obd ops to offer management infrastructure */
1201 static struct obd_ops ost_obd_ops = {
1202         .o_owner        = THIS_MODULE,
1203         .o_setup        = ost_setup,
1204         .o_cleanup      = ost_cleanup,
1205 };
1206
1207 static int __init ost_init(void)
1208 {
1209         struct lprocfs_static_vars lvars;
1210         ENTRY;
1211
1212         lprocfs_init_vars(ost,&lvars);
1213         RETURN(class_register_type(&ost_obd_ops, lvars.module_vars,
1214                                    LUSTRE_OST_NAME));
1215 }
1216
1217 static void /*__exit*/ ost_exit(void)
1218 {
1219         class_unregister_type(LUSTRE_OST_NAME);
1220 }
1221
1222 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1223 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
1224 MODULE_LICENSE("GPL");
1225
1226 module_init(ost_init);
1227 module_exit(ost_exit);