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