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