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