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