Whamcloud - gitweb
Branch b1_4_mountconf
[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 the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  *
26  *  Storage Target Handling functions
27  *  Lustre Object Server Module (OST)
28  *
29  *  This server is single threaded at present (but can easily be multi
30  *  threaded). For testing and management it is treated as an
31  *  obd_device, although it does not export a full OBD method table
32  *  (the requests are coming in over the wire, so object target
33  *  modules do not have a full method table.)
34  */
35
36 #ifndef EXPORT_SYMTAB
37 # define EXPORT_SYMTAB
38 #endif
39 #define DEBUG_SUBSYSTEM S_OST
40
41 #include <linux/module.h>
42 #include <linux/obd_ost.h>
43 #include <linux/lustre_net.h>
44 #include <linux/lustre_dlm.h>
45 #include <linux/lustre_export.h>
46 #include <linux/lustre_debug.h>
47 #include <linux/init.h>
48 #include <linux/lprocfs_status.h>
49 #include <linux/lustre_commit_confd.h>
50 #include <libcfs/list.h>
51 #include <linux/lustre_quota.h>
52 #include "ost_internal.h"
53
54 void oti_init(struct obd_trans_info *oti, struct ptlrpc_request *req)
55 {
56         if (oti == NULL)
57                 return;
58         memset(oti, 0, sizeof *oti);
59
60         if (req->rq_repmsg && req->rq_reqmsg != 0)
61                 oti->oti_transno = req->rq_repmsg->transno;
62         oti->oti_thread = req->rq_svc_thread;
63 }
64
65 void oti_to_request(struct obd_trans_info *oti, struct ptlrpc_request *req)
66 {
67         struct oti_req_ack_lock *ack_lock;
68         int i;
69
70         if (oti == NULL)
71                 return;
72
73         if (req->rq_repmsg)
74                 req->rq_repmsg->transno = oti->oti_transno;
75
76         /* XXX 4 == entries in oti_ack_locks??? */
77         for (ack_lock = oti->oti_ack_locks, i = 0; i < 4; i++, ack_lock++) {
78                 if (!ack_lock->mode)
79                         break;
80                 /* XXX not even calling target_send_reply in some cases... */
81                 ptlrpc_save_lock (req, &ack_lock->lock, ack_lock->mode);
82         }
83 }
84
85 static int ost_destroy(struct obd_export *exp, struct ptlrpc_request *req,
86                        struct obd_trans_info *oti)
87 {
88         struct ost_body *body, *repbody;
89         int rc, size = sizeof(*body);
90         ENTRY;
91
92         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
93         if (body == NULL)
94                 RETURN(-EFAULT);
95
96         rc = lustre_pack_reply(req, 1, &size, NULL);
97         if (rc)
98                 RETURN(rc);
99
100         if (body->oa.o_valid & OBD_MD_FLCOOKIE)
101                 oti->oti_logcookies = obdo_logcookie(&body->oa);
102         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
103         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
104         req->rq_status = obd_destroy(exp, &body->oa, NULL, oti);
105         RETURN(0);
106 }
107
108 static int ost_getattr(struct obd_export *exp, struct ptlrpc_request *req)
109 {
110         struct ost_body *body, *repbody;
111         int rc, size = sizeof(*body);
112         ENTRY;
113
114         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
115         if (body == NULL)
116                 RETURN(-EFAULT);
117
118         rc = lustre_pack_reply(req, 1, &size, NULL);
119         if (rc)
120                 RETURN(rc);
121
122         repbody = lustre_msg_buf (req->rq_repmsg, 0, sizeof(*repbody));
123         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
124         req->rq_status = obd_getattr(exp, &repbody->oa, NULL);
125         RETURN(0);
126 }
127
128 static int ost_statfs(struct ptlrpc_request *req)
129 {
130         struct obd_statfs *osfs;
131         int rc, size = sizeof(*osfs);
132         ENTRY;
133
134         rc = lustre_pack_reply(req, 1, &size, NULL);
135         if (rc)
136                 RETURN(rc);
137
138         osfs = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*osfs));
139
140         req->rq_status = obd_statfs(req->rq_export->exp_obd, osfs, jiffies-HZ);
141         if (req->rq_status != 0)
142                 CERROR("ost: statfs failed: rc %d\n", req->rq_status);
143
144         RETURN(0);
145 }
146
147 static int ost_create(struct obd_export *exp, struct ptlrpc_request *req,
148                       struct obd_trans_info *oti)
149 {
150         struct ost_body *body, *repbody;
151         int rc, size = sizeof(*repbody);
152         ENTRY;
153
154         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
155         if (body == NULL)
156                 RETURN(-EFAULT);
157
158         rc = lustre_pack_reply(req, 1, &size, NULL);
159         if (rc)
160                 RETURN(rc);
161
162         repbody = lustre_msg_buf (req->rq_repmsg, 0, sizeof(*repbody));
163         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
164         oti->oti_logcookies = obdo_logcookie(&repbody->oa);
165         req->rq_status = obd_create(exp, &repbody->oa, NULL, oti);
166         //obd_log_cancel(conn, NULL, 1, oti->oti_logcookies, 0);
167         RETURN(0);
168 }
169
170 /*
171  * Helper function for ost_punch(): if asked by client, acquire [size, EOF]
172  * lock on the file being truncated.
173  */
174 static int ost_punch_lock_get(struct obd_export *exp, struct obdo *oa,
175                               struct lustre_handle *lh)
176 {
177         int flags;
178         struct ldlm_res_id res_id = { .name = { oa->o_id } };
179         ldlm_policy_data_t policy;
180         __u64 start;
181         __u64 finis;
182
183         ENTRY;
184
185         LASSERT(!lustre_handle_is_used(lh));
186
187         if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
188             !(oa->o_flags & OBD_FL_TRUNCLOCK))
189                 RETURN(0);
190
191         CDEBUG(D_INODE, "OST-side truncate lock.\n");
192
193         start = oa->o_size;
194         finis = start + oa->o_blocks;
195
196         /*
197          * standard truncate optimization: if file body is completely
198          * destroyed, don't send data back to the server.
199          */
200         flags = (start == 0) ? LDLM_AST_DISCARD_DATA : 0;
201
202         policy.l_extent.start = start & CFS_PAGE_MASK;
203
204         /*
205          * If ->o_blocks is EOF it means "lock till the end of the
206          * file". Otherwise, it's size of a hole being punched (in bytes)
207          */
208         if (oa->o_blocks == OBD_OBJECT_EOF || finis < start)
209                 policy.l_extent.end = OBD_OBJECT_EOF;
210         else
211                 policy.l_extent.end = finis | ~CFS_PAGE_MASK;
212
213         RETURN(ldlm_cli_enqueue(NULL, NULL, exp->exp_obd->obd_namespace,
214                                 res_id, LDLM_EXTENT, &policy, LCK_PW, &flags,
215                                 ldlm_blocking_ast, ldlm_completion_ast,
216                                 ldlm_glimpse_ast,
217                                 NULL, NULL, 0, NULL, lh));
218 }
219
220 /*
221  * Helper function for ost_punch(): release lock acquired by
222  * ost_punch_lock_get(), if any.
223  */
224 static void ost_punch_lock_put(struct obd_export *exp, struct obdo *oa,
225                                struct lustre_handle *lh)
226 {
227         ENTRY;
228         if (lustre_handle_is_used(lh))
229                 ldlm_lock_decref(lh, LCK_PW);
230         EXIT;
231 }
232
233 static int ost_punch(struct obd_export *exp, struct ptlrpc_request *req,
234                      struct obd_trans_info *oti)
235 {
236         struct obdo     *oa;
237         struct ost_body *body, *repbody;
238         struct lustre_handle lh = {0,};
239
240         int rc, size = sizeof(*repbody);
241
242         ENTRY;
243
244         /*
245          * check that we do support OBD_CONNECT_TRUNCLOCK.
246          */
247         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
248
249         body = lustre_swab_reqbuf(req, 0, sizeof *body, lustre_swab_ost_body);
250         if (body == NULL)
251                 RETURN(-EFAULT);
252
253         oa = &body->oa;
254         if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
255             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
256                 RETURN(-EINVAL);
257
258         rc = lustre_pack_reply(req, 1, &size, NULL);
259         if (rc)
260                 RETURN(rc);
261
262         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
263         repbody->oa = *oa;
264         rc = ost_punch_lock_get(exp, oa, &lh);
265         if (rc == 0) {
266                 if (oa->o_valid & OBD_MD_FLFLAGS &&
267                     oa->o_flags == OBD_FL_TRUNCLOCK)
268                         /*
269                          * If OBD_FL_TRUNCLOCK is the only bit set in
270                          * ->o_flags, clear OBD_MD_FLFLAGS to avoid falling
271                          * through filter_setattr() to filter_iocontrol().
272                          */
273                         oa->o_valid &= ~OBD_MD_FLFLAGS;
274
275                 req->rq_status = obd_punch(exp, oa, NULL,
276                                            oa->o_size, oa->o_blocks, oti);
277                 ost_punch_lock_put(exp, oa, &lh);
278         }
279         RETURN(rc);
280 }
281
282 static int ost_sync(struct obd_export *exp, struct ptlrpc_request *req)
283 {
284         struct ost_body *body, *repbody;
285         int rc, size = sizeof(*repbody);
286         ENTRY;
287
288         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
289         if (body == NULL)
290                 RETURN(-EFAULT);
291
292         rc = lustre_pack_reply(req, 1, &size, NULL);
293         if (rc)
294                 RETURN(rc);
295
296         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
297         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
298         req->rq_status = obd_sync(exp, &repbody->oa, NULL, repbody->oa.o_size,
299                                   repbody->oa.o_blocks);
300         RETURN(0);
301 }
302
303 static int ost_setattr(struct obd_export *exp, struct ptlrpc_request *req,
304                        struct obd_trans_info *oti)
305 {
306         struct ost_body *body, *repbody;
307         int rc, size = sizeof(*repbody);
308         ENTRY;
309
310         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
311         if (body == NULL)
312                 RETURN(-EFAULT);
313
314         rc = lustre_pack_reply(req, 1, &size, NULL);
315         if (rc)
316                 RETURN(rc);
317
318         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
319         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
320
321         req->rq_status = obd_setattr(exp, &repbody->oa, NULL, oti);
322         RETURN(0);
323 }
324
325 static int ost_bulk_timeout(void *data)
326 {
327         ENTRY;
328         /* We don't fail the connection here, because having the export
329          * killed makes the (vital) call to commitrw very sad.
330          */
331         RETURN(1);
332 }
333
334 static int get_per_page_niobufs(struct obd_ioobj *ioo, int nioo,
335                                 struct niobuf_remote *rnb, int nrnb,
336                                 struct niobuf_remote **pp_rnbp)
337 {
338         /* Copy a remote niobuf, splitting it into page-sized chunks
339          * and setting ioo[i].ioo_bufcnt accordingly */
340         struct niobuf_remote *pp_rnb;
341         int   i;
342         int   j;
343         int   page;
344         int   rnbidx = 0;
345         int   npages = 0;
346
347         /*
348          * array of sufficient size already preallocated by caller
349          */
350         LASSERT(pp_rnbp != NULL);
351         LASSERT(*pp_rnbp != NULL);
352
353         /* first count and check the number of pages required */
354         for (i = 0; i < nioo; i++)
355                 for (j = 0; j < ioo->ioo_bufcnt; j++, rnbidx++) {
356                         obd_off offset = rnb[rnbidx].offset;
357                         obd_off p0 = offset >> PAGE_SHIFT;
358                         obd_off pn = (offset + rnb[rnbidx].len - 1)>>PAGE_SHIFT;
359
360                         LASSERT(rnbidx < nrnb);
361
362                         npages += (pn + 1 - p0);
363
364                         if (rnb[rnbidx].len == 0) {
365                                 CERROR("zero len BRW: obj %d objid "LPX64
366                                        " buf %u\n", i, ioo[i].ioo_id, j);
367                                 return -EINVAL;
368                         }
369                         if (j > 0 &&
370                             rnb[rnbidx].offset <= rnb[rnbidx-1].offset) {
371                                 CERROR("unordered BRW: obj %d objid "LPX64
372                                        " buf %u offset "LPX64" <= "LPX64"\n",
373                                        i, ioo[i].ioo_id, j, rnb[rnbidx].offset,
374                                        rnb[rnbidx].offset);
375                                 return -EINVAL;
376                         }
377                 }
378
379         LASSERT(rnbidx == nrnb);
380
381         if (npages == nrnb) {       /* all niobufs are for single pages */
382                 *pp_rnbp = rnb;
383                 return npages;
384         }
385
386         pp_rnb = *pp_rnbp;
387
388         /* now do the actual split */
389         page = rnbidx = 0;
390         for (i = 0; i < nioo; i++) {
391                 int  obj_pages = 0;
392
393                 for (j = 0; j < ioo[i].ioo_bufcnt; j++, rnbidx++) {
394                         obd_off off = rnb[rnbidx].offset;
395                         int     nob = rnb[rnbidx].len;
396
397                         LASSERT(rnbidx < nrnb);
398                         do {
399                                 obd_off  poff = off & (PAGE_SIZE - 1);
400                                 int      pnob = (poff + nob > PAGE_SIZE) ?
401                                                 PAGE_SIZE - poff : nob;
402
403                                 LASSERT(page < npages);
404                                 pp_rnb[page].len = pnob;
405                                 pp_rnb[page].offset = off;
406                                 pp_rnb[page].flags = rnb[rnbidx].flags;
407
408                                 CDEBUG(0, "   obj %d id "LPX64
409                                        "page %d(%d) "LPX64" for %d, flg %x\n",
410                                        i, ioo[i].ioo_id, obj_pages, page,
411                                        pp_rnb[page].offset, pp_rnb[page].len,
412                                        pp_rnb[page].flags);
413                                 page++;
414                                 obj_pages++;
415
416                                 off += pnob;
417                                 nob -= pnob;
418                         } while (nob > 0);
419                         LASSERT(nob == 0);
420                 }
421                 ioo[i].ioo_bufcnt = obj_pages;
422         }
423         LASSERT(page == npages);
424
425         return npages;
426 }
427
428 static __u32 ost_checksum_bulk(struct ptlrpc_bulk_desc *desc)
429 {
430         __u32 cksum = ~0;
431         int i;
432
433         for (i = 0; i < desc->bd_iov_count; i++) {
434                 struct page *page = desc->bd_iov[i].kiov_page;
435                 int off = desc->bd_iov[i].kiov_offset & ~PAGE_MASK;
436                 char *ptr = kmap(page) + off;
437                 int len = desc->bd_iov[i].kiov_len;
438
439                 cksum = crc32_le(cksum, ptr, len);
440         }
441
442         return cksum;
443 }
444
445 /*
446  * populate @nio by @nrpages pages from per-thread page pool
447  */
448 static void ost_nio_pages_get(struct ptlrpc_request *req,
449                               struct niobuf_local *nio, int nrpages)
450 {
451         int i;
452         struct ost_thread_local_cache *tls;
453
454         ENTRY;
455
456         LASSERT(nrpages <= OST_THREAD_POOL_SIZE);
457         LASSERT(req != NULL);
458         LASSERT(req->rq_svc_thread != NULL);
459
460         tls = ost_tls(req);
461         LASSERT(tls != NULL);
462
463         memset(nio, 0, nrpages * sizeof *nio);
464         for (i = 0; i < nrpages; ++ i) {
465                 struct page *page;
466
467                 page = tls->page[i];
468                 LASSERT(page != NULL);
469                 POISON_PAGE(page, 0xf1);
470                 nio[i].page = page;
471                 LL_CDEBUG_PAGE(D_INFO, page, "%d\n", i);
472         }
473         EXIT;
474 }
475
476 /*
477  * Dual for ost_nio_pages_get(). Poison pages in pool for debugging
478  */
479 static void ost_nio_pages_put(struct ptlrpc_request *req,
480                               struct niobuf_local *nio, int nrpages)
481 {
482         int i;
483
484         ENTRY;
485
486         LASSERT(nrpages <= OST_THREAD_POOL_SIZE);
487
488         for (i = 0; i < nrpages; ++ i)
489                 POISON_PAGE(nio[i].page, 0xf2);
490         EXIT;
491 }
492
493 /* cut-n-paste of mds_blocking_ast() */
494 static int ost_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
495                             void *data, int flag)
496 {
497         int do_ast;
498         ENTRY;
499
500         if (flag == LDLM_CB_CANCELING) {
501                 /* Don't need to do anything here. */
502                 RETURN(0);
503         }
504
505         /* XXX layering violation!  -phil */
506         l_lock(&lock->l_resource->lr_namespace->ns_lock);
507         /* Get this: if mds_blocking_ast is racing with mds_intent_policy,
508          * such that mds_blocking_ast is called just before l_i_p takes the
509          * ns_lock, then by the time we get the lock, we might not be the
510          * correct blocking function anymore.  So check, and return early, if
511          * so. */
512         if (lock->l_blocking_ast != ost_blocking_ast) {
513                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
514                 RETURN(0);
515         }
516
517         lock->l_flags |= LDLM_FL_CBPENDING;
518         do_ast = (!lock->l_readers && !lock->l_writers);
519         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
520
521         if (do_ast) {
522                 struct lustre_handle lockh;
523                 int rc;
524
525                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
526                 ldlm_lock2handle(lock, &lockh);
527                 rc = ldlm_cli_cancel(&lockh);
528                 if (rc < 0)
529                         CERROR("ldlm_cli_cancel: %d\n", rc);
530         } else {
531                 LDLM_DEBUG(lock, "Lock still has references, will be "
532                            "cancelled later");
533         }
534         RETURN(0);
535 }
536
537 static int ost_brw_lock_get(int mode, struct obd_export *exp,
538                             struct obd_ioobj *obj, struct niobuf_remote *nb,
539                             struct lustre_handle *lh)
540 {
541         int flags                 = 0;
542         int nrbufs                = obj->ioo_bufcnt;
543         struct ldlm_res_id res_id = { .name = { obj->ioo_id } };
544         ldlm_policy_data_t policy;
545         int i;
546
547         ENTRY;
548
549         LASSERT(mode == LCK_PR || mode == LCK_PW);
550         LASSERT(!lustre_handle_is_used(lh));
551
552         /* EXPENSIVE ASSERTION */
553         for (i = 1; i < nrbufs; i ++)
554                 LASSERT((nb[0].flags & OBD_BRW_SRVLOCK) ==
555                         (nb[i].flags & OBD_BRW_SRVLOCK));
556
557         if (nrbufs == 0 || !(nb[0].flags & OBD_BRW_SRVLOCK))
558                 RETURN(0);
559
560         policy.l_extent.start = nb[0].offset & CFS_PAGE_MASK;
561         policy.l_extent.end   = (nb[nrbufs - 1].offset +
562                                  nb[nrbufs - 1].len - 1) | ~CFS_PAGE_MASK;
563
564         RETURN(ldlm_cli_enqueue(NULL, NULL, exp->exp_obd->obd_namespace,
565                                 res_id, LDLM_EXTENT, &policy, mode, &flags,
566                                 ldlm_blocking_ast, ldlm_completion_ast,
567                                 ldlm_glimpse_ast,
568                                 NULL, NULL, 0, NULL, lh));
569 }
570
571 static void ost_brw_lock_put(int mode,
572                              struct obd_ioobj *obj, struct niobuf_remote *niob,
573                              struct lustre_handle *lh)
574 {
575         ENTRY;
576         LASSERT(mode == LCK_PR || mode == LCK_PW);
577         LASSERT((obj->ioo_bufcnt > 0 && (niob[0].flags & OBD_BRW_SRVLOCK)) ==
578                 lustre_handle_is_used(lh));
579         if (lustre_handle_is_used(lh))
580                 ldlm_lock_decref(lh, mode);
581         EXIT;
582 }
583
584 static int ost_brw_read(struct ptlrpc_request *req, struct obd_trans_info *oti)
585 {
586         struct ptlrpc_bulk_desc *desc;
587         struct niobuf_remote    *remote_nb;
588         struct niobuf_remote    *pp_rnb = NULL;
589         struct niobuf_local     *local_nb;
590         struct obd_ioobj        *ioo;
591         struct ost_body         *body, *repbody;
592         struct l_wait_info       lwi;
593         struct lustre_handle     lockh = {0};
594         int                      size[1] = { sizeof(*body) };
595         int                      comms_error = 0;
596         int                      niocount;
597         int                      npages;
598         int                      nob = 0;
599         int                      rc;
600         int                      i, do_checksum;
601         ENTRY;
602
603         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
604                 GOTO(out, rc = -EIO);
605
606         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK | OBD_FAIL_ONCE,
607                          (obd_timeout + 1) / 4);
608
609         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
610         if (body == NULL) {
611                 CERROR("Missing/short ost_body\n");
612                 GOTO(out, rc = -EFAULT);
613         }
614
615         ioo = lustre_swab_reqbuf(req, 1, sizeof(*ioo), lustre_swab_obd_ioobj);
616         if (ioo == NULL) {
617                 CERROR("Missing/short ioobj\n");
618                 GOTO(out, rc = -EFAULT);
619         }
620
621         niocount = ioo->ioo_bufcnt;
622         if (niocount > PTLRPC_MAX_BRW_PAGES) {
623                 DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)\n",
624                           niocount);
625                 GOTO(out, rc = -EFAULT);
626         }
627
628         remote_nb = lustre_swab_reqbuf(req, 2, niocount * sizeof(*remote_nb),
629                                        lustre_swab_niobuf_remote);
630         if (remote_nb == NULL) {
631                 CERROR("Missing/short niobuf\n");
632                 GOTO(out, rc = -EFAULT);
633         }
634         if (lustre_msg_swabbed(req->rq_reqmsg)) { /* swab remaining niobufs */
635                 for (i = 1; i < niocount; i++)
636                         lustre_swab_niobuf_remote (&remote_nb[i]);
637         }
638
639         rc = lustre_pack_reply(req, 1, size, NULL);
640         if (rc)
641                 GOTO(out, rc);
642
643         /*
644          * Per-thread array of struct niobuf_{local,remote}'s was allocated by
645          * ost_thread_init().
646          */
647         local_nb = ost_tls(req)->local;
648         pp_rnb   = ost_tls(req)->remote;
649
650         /* FIXME all niobuf splitting should be done in obdfilter if needed */
651         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
652         npages = get_per_page_niobufs(ioo, 1, remote_nb, niocount, &pp_rnb);
653         if (npages < 0)
654                 GOTO(out, rc = npages);
655
656         LASSERT(npages <= OST_THREAD_POOL_SIZE);
657
658         ost_nio_pages_get(req, local_nb, npages);
659
660         desc = ptlrpc_prep_bulk_exp(req, npages,
661                                      BULK_PUT_SOURCE, OST_BULK_PORTAL);
662         if (desc == NULL)
663                 GOTO(out, rc = -ENOMEM);
664
665         rc = ost_brw_lock_get(LCK_PR, req->rq_export, ioo, pp_rnb, &lockh);
666         if (rc != 0)
667                 GOTO(out_bulk, rc);
668
669         rc = obd_preprw(OBD_BRW_READ, req->rq_export, &body->oa, 1,
670                         ioo, npages, pp_rnb, local_nb, oti);
671         if (rc != 0)
672                 GOTO(out_lock, rc);
673
674         /* We're finishing using body->oa as an input variable */
675         do_checksum = (body->oa.o_valid & OBD_MD_FLCKSUM);
676         body->oa.o_valid = 0;
677
678         nob = 0;
679         for (i = 0; i < npages; i++) {
680                 int page_rc = local_nb[i].rc;
681
682                 if (page_rc < 0) {              /* error */
683                         rc = page_rc;
684                         break;
685                 }
686
687                 LASSERTF(page_rc <= pp_rnb[i].len, "page_rc (%d) > "
688                          "pp_rnb[%d].len (%d)\n", page_rc, i, pp_rnb[i].len);
689                 nob += page_rc;
690                 if (page_rc != 0) {             /* some data! */
691                         LASSERT (local_nb[i].page != NULL);
692                         ptlrpc_prep_bulk_page(desc, local_nb[i].page,
693                                               pp_rnb[i].offset & (PAGE_SIZE-1),
694                                               page_rc);
695                 }
696
697                 if (page_rc != pp_rnb[i].len) { /* short read */
698                         /* All subsequent pages should be 0 */
699                         while(++i < npages)
700                                 LASSERT(local_nb[i].rc == 0);
701                         break;
702                 }
703         }
704
705         if (rc == 0) {
706                 rc = ptlrpc_start_bulk_transfer(desc);
707                 if (rc == 0) {
708                         lwi = LWI_TIMEOUT(obd_timeout * HZ / 4,
709                                           ost_bulk_timeout, desc);
710                         rc = l_wait_event(desc->bd_waitq,
711                                           !ptlrpc_bulk_active(desc), &lwi);
712                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
713                         if (rc == -ETIMEDOUT) {
714                                 DEBUG_REQ(D_ERROR, req, "timeout on bulk PUT");
715                                 ptlrpc_abort_bulk(desc);
716                         } else if (!desc->bd_success ||
717                                    desc->bd_nob_transferred != desc->bd_nob) {
718                                 DEBUG_REQ(D_ERROR, req, "%s bulk PUT %d(%d)",
719                                           desc->bd_success ?
720                                           "truncated" : "network error on",
721                                           desc->bd_nob_transferred,
722                                           desc->bd_nob);
723                                 /* XXX should this be a different errno? */
724                                 rc = -ETIMEDOUT;
725                         }
726                 } else {
727                         DEBUG_REQ(D_ERROR, req, "bulk PUT failed: rc %d\n", rc);
728                 }
729                 comms_error = rc != 0;
730         }
731
732         /* Must commit after prep above in all cases */
733         rc = obd_commitrw(OBD_BRW_READ, req->rq_export, &body->oa, 1,
734                           ioo, npages, local_nb, oti, rc);
735
736         ost_nio_pages_put(req, local_nb, npages);
737
738         if (rc == 0) {
739                 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
740                 memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
741
742                 if (unlikely(do_checksum)) {
743                         repbody->oa.o_cksum = ost_checksum_bulk(desc);
744                         repbody->oa.o_valid |= OBD_MD_FLCKSUM;
745                         CDEBUG(D_PAGE, "checksum at read origin: %x\n",
746                                repbody->oa.o_cksum);
747                 }
748         }
749
750  out_lock:
751         ost_brw_lock_put(LCK_PR, ioo, pp_rnb, &lockh);
752  out_bulk:
753         ptlrpc_free_bulk(desc);
754  out:
755         LASSERT(rc <= 0);
756         if (rc == 0) {
757                 req->rq_status = nob;
758                 target_committed_to_req(req);
759                 ptlrpc_reply(req);
760         } else if (!comms_error) {
761                 /* only reply if comms OK */
762                 target_committed_to_req(req);
763                 req->rq_status = rc;
764                 ptlrpc_error(req);
765         } else {
766                 if (req->rq_reply_state != NULL) {
767                         /* reply out callback would free */
768                         ptlrpc_rs_decref(req->rq_reply_state);
769                         req->rq_reply_state = NULL;
770                 }
771                 if (req->rq_reqmsg->conn_cnt == req->rq_export->exp_conn_cnt) {
772                         CERROR("bulk IO comms error: "
773                                "evicting %s@%s id %s\n",
774                                req->rq_export->exp_client_uuid.uuid,
775                                req->rq_export->exp_connection->c_remote_uuid.uuid,
776                                libcfs_id2str(req->rq_peer));
777                         class_fail_export(req->rq_export);
778                 } else {
779                         CERROR("ignoring bulk IO comms error: "
780                                "client reconnected %s@%s id %s\n",
781                                req->rq_export->exp_client_uuid.uuid,
782                                req->rq_export->exp_connection->c_remote_uuid.uuid,
783                                libcfs_id2str(req->rq_peer));
784                 }
785         }
786
787         RETURN(rc);
788 }
789
790 static int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
791 {
792         struct ptlrpc_bulk_desc *desc;
793         struct niobuf_remote    *remote_nb;
794         struct niobuf_remote    *pp_rnb;
795         struct niobuf_local     *local_nb;
796         struct obd_ioobj        *ioo;
797         struct ost_body         *body, *repbody;
798         struct l_wait_info       lwi;
799         struct lustre_handle     lockh = {0};
800         __u32                   *rcs;
801         int                      size[2] = { sizeof(*body) };
802         int                      objcount, niocount, npages;
803         int                      comms_error = 0;
804         int                      rc, swab, i, j, do_checksum;
805         ENTRY;
806
807         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
808                 GOTO(out, rc = -EIO);
809
810         /* pause before transaction has been started */
811         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK | OBD_FAIL_ONCE,
812                          (obd_timeout + 1) / 4);
813
814         swab = lustre_msg_swabbed(req->rq_reqmsg);
815         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
816         if (body == NULL) {
817                 CERROR("Missing/short ost_body\n");
818                 GOTO(out, rc = -EFAULT);
819         }
820
821         LASSERT_REQSWAB(req, 1);
822         objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
823         if (objcount == 0) {
824                 CERROR("Missing/short ioobj\n");
825                 GOTO(out, rc = -EFAULT);
826         }
827         if (objcount > 1) {
828                 CERROR("too many ioobjs (%d)\n", objcount);
829                 GOTO(out, rc = -EFAULT);
830         }
831
832         ioo = lustre_msg_buf (req->rq_reqmsg, 1, objcount * sizeof(*ioo));
833         LASSERT (ioo != NULL);
834         for (niocount = i = 0; i < objcount; i++) {
835                 if (swab)
836                         lustre_swab_obd_ioobj (&ioo[i]);
837                 if (ioo[i].ioo_bufcnt == 0) {
838                         CERROR("ioo[%d] has zero bufcnt\n", i);
839                         GOTO(out, rc = -EFAULT);
840                 }
841                 niocount += ioo[i].ioo_bufcnt;
842         }
843
844         if (niocount > PTLRPC_MAX_BRW_PAGES) {
845                 DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)\n",
846                           niocount);
847                 GOTO(out, rc = -EFAULT);
848         }
849
850         remote_nb = lustre_swab_reqbuf(req, 2, niocount * sizeof(*remote_nb),
851                                        lustre_swab_niobuf_remote);
852         if (remote_nb == NULL) {
853                 CERROR("Missing/short niobuf\n");
854                 GOTO(out, rc = -EFAULT);
855         }
856         if (swab) {                             /* swab the remaining niobufs */
857                 for (i = 1; i < niocount; i++)
858                         lustre_swab_niobuf_remote (&remote_nb[i]);
859         }
860
861         size[1] = niocount * sizeof(*rcs);
862         rc = lustre_pack_reply(req, 2, size, NULL);
863         if (rc != 0)
864                 GOTO(out, rc);
865         rcs = lustre_msg_buf(req->rq_repmsg, 1, niocount * sizeof(*rcs));
866
867         /*
868          * Per-thread array of struct niobuf_{local,remote}'s was allocated by
869          * ost_thread_init().
870          */
871         local_nb = ost_tls(req)->local;
872         pp_rnb   = ost_tls(req)->remote;
873
874         /* FIXME all niobuf splitting should be done in obdfilter if needed */
875         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
876         npages = get_per_page_niobufs(ioo, objcount,remote_nb,niocount,&pp_rnb);
877         if (npages < 0)
878                 GOTO(out, rc = npages);
879
880         LASSERT(npages <= OST_THREAD_POOL_SIZE);
881
882         ost_nio_pages_get(req, local_nb, npages);
883
884         desc = ptlrpc_prep_bulk_exp(req, npages,
885                                      BULK_GET_SINK, OST_BULK_PORTAL);
886         if (desc == NULL)
887                 GOTO(out, rc = -ENOMEM);
888
889         rc = ost_brw_lock_get(LCK_PW, req->rq_export, ioo, pp_rnb, &lockh);
890         if (rc != 0)
891                 GOTO(out_bulk, rc);
892
893         /* obd_preprw clobbers oa->valid, so save what we need */
894         do_checksum = (body->oa.o_valid & OBD_MD_FLCKSUM);
895
896         rc = obd_preprw(OBD_BRW_WRITE, req->rq_export, &body->oa, objcount,
897                         ioo, npages, pp_rnb, local_nb, oti);
898         if (rc != 0)
899                 GOTO(out_lock, rc);
900
901         /* NB Having prepped, we must commit... */
902
903         for (i = 0; i < npages; i++)
904                 ptlrpc_prep_bulk_page(desc, local_nb[i].page,
905                                       pp_rnb[i].offset & (PAGE_SIZE - 1),
906                                       pp_rnb[i].len);
907
908         rc = ptlrpc_start_bulk_transfer (desc);
909         if (rc == 0) {
910                 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4,
911                                   ost_bulk_timeout, desc);
912                 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc),
913                                   &lwi);
914                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
915                 if (rc == -ETIMEDOUT) {
916                         DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
917                         ptlrpc_abort_bulk(desc);
918                 } else if (!desc->bd_success ||
919                            desc->bd_nob_transferred != desc->bd_nob) {
920                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
921                                   desc->bd_success ?
922                                   "truncated" : "network error on",
923                                   desc->bd_nob_transferred, desc->bd_nob);
924                         /* XXX should this be a different errno? */
925                         rc = -ETIMEDOUT;
926                 }
927         } else {
928                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d\n", rc);
929         }
930         comms_error = rc != 0;
931
932         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
933         memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
934
935         if (unlikely(do_checksum && rc == 0)) {
936                 static int cksum_counter;
937                 obd_count client_cksum = body->oa.o_cksum;
938                 obd_count cksum = ost_checksum_bulk(desc);
939
940                 cksum_counter++;
941                 if (client_cksum != cksum) {
942                         CERROR("Bad checksum: client %x, server %x id %s\n",
943                                client_cksum, cksum,
944                                libcfs_id2str(req->rq_peer));
945                         cksum_counter = 0;
946                         repbody->oa.o_cksum = cksum;
947                         repbody->oa.o_valid |= OBD_MD_FLCKSUM;
948                 } else if ((cksum_counter & (-cksum_counter)) ==
949                            cksum_counter) {
950                         CWARN("Checksum %u from %s: %x OK\n", cksum_counter,
951                               libcfs_id2str(req->rq_peer), cksum);
952                 } else {
953                         cksum_counter++;
954                         if ((cksum_counter & (-cksum_counter)) == cksum_counter)
955                                 CWARN("Checksum %u from %s: %x OK\n",
956                                       cksum_counter,
957                                       libcfs_id2str(req->rq_peer), cksum);
958                 }
959         }
960
961         /* Must commit after prep above in all cases */
962         rc = obd_commitrw(OBD_BRW_WRITE, req->rq_export, &repbody->oa,
963                            objcount, ioo, npages, local_nb, oti, rc);
964
965         ost_nio_pages_put(req, local_nb, npages);
966
967         if (rc == 0) {
968                 /* set per-requested niobuf return codes */
969                 for (i = j = 0; i < niocount; i++) {
970                         int nob = remote_nb[i].len;
971
972                         rcs[i] = 0;
973                         do {
974                                 LASSERT(j < npages);
975                                 if (local_nb[j].rc < 0)
976                                         rcs[i] = local_nb[j].rc;
977                                 nob -= pp_rnb[j].len;
978                                 j++;
979                         } while (nob > 0);
980                         LASSERT(nob == 0);
981                 }
982                 LASSERT(j == npages);
983         }
984
985  out_lock:
986         ost_brw_lock_put(LCK_PW, ioo, pp_rnb, &lockh);
987  out_bulk:
988         ptlrpc_free_bulk(desc);
989  out:
990         if (rc == 0) {
991                 oti_to_request(oti, req);
992                 target_committed_to_req(req);
993                 rc = ptlrpc_reply(req);
994         } else if (!comms_error) {
995                 /* Only reply if there was no comms problem with bulk */
996                 target_committed_to_req(req);
997                 req->rq_status = rc;
998                 ptlrpc_error(req);
999         } else {
1000                 if (req->rq_reply_state != NULL) {
1001                         /* reply out callback would free */
1002                         ptlrpc_rs_decref(req->rq_reply_state);
1003                         req->rq_reply_state = NULL;
1004                 }
1005                 if (req->rq_reqmsg->conn_cnt == req->rq_export->exp_conn_cnt) {
1006                         CERROR("%s: bulk IO comm error evicting %s@%s id %s\n",
1007                                req->rq_export->exp_obd->obd_name,
1008                                req->rq_export->exp_client_uuid.uuid,
1009                                req->rq_export->exp_connection->c_remote_uuid.uuid,
1010                                libcfs_id2str(req->rq_peer));
1011                         class_fail_export(req->rq_export);
1012                 } else {
1013                         CERROR("ignoring bulk IO comms error: "
1014                                "client reconnected %s@%s id %s\n",
1015                                req->rq_export->exp_client_uuid.uuid,
1016                                req->rq_export->exp_connection->c_remote_uuid.uuid,
1017                                libcfs_id2str(req->rq_peer));
1018                 }
1019         }
1020         RETURN(rc);
1021 }
1022
1023 static int ost_san_brw(struct ptlrpc_request *req, int cmd)
1024 {
1025         struct niobuf_remote *remote_nb, *res_nb, *pp_rnb = NULL;
1026         struct obd_ioobj *ioo;
1027         struct ost_body *body, *repbody;
1028         int rc, i, objcount, niocount, size[2] = {sizeof(*body)}, npages;
1029         int swab;
1030         ENTRY;
1031
1032         /* XXX not set to use latest protocol */
1033
1034         swab = lustre_msg_swabbed(req->rq_reqmsg);
1035         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
1036         if (body == NULL) {
1037                 CERROR("Missing/short ost_body\n");
1038                 GOTO(out, rc = -EFAULT);
1039         }
1040
1041         ioo = lustre_swab_reqbuf(req, 1, sizeof(*ioo), lustre_swab_obd_ioobj);
1042         if (ioo == NULL) {
1043                 CERROR("Missing/short ioobj\n");
1044                 GOTO(out, rc = -EFAULT);
1045         }
1046         objcount = req->rq_reqmsg->buflens[1] / sizeof(*ioo);
1047         niocount = ioo[0].ioo_bufcnt;
1048         for (i = 1; i < objcount; i++) {
1049                 if (swab)
1050                         lustre_swab_obd_ioobj (&ioo[i]);
1051                 niocount += ioo[i].ioo_bufcnt;
1052         }
1053
1054         remote_nb = lustre_swab_reqbuf(req, 2, niocount * sizeof(*remote_nb),
1055                                        lustre_swab_niobuf_remote);
1056         if (remote_nb == NULL) {
1057                 CERROR("Missing/short niobuf\n");
1058                 GOTO(out, rc = -EFAULT);
1059         }
1060         if (swab) {                             /* swab the remaining niobufs */
1061                 for (i = 1; i < niocount; i++)
1062                         lustre_swab_niobuf_remote (&remote_nb[i]);
1063         }
1064
1065         /*
1066          * Per-thread array of struct niobuf_remote's was allocated by
1067          * ost_thread_init().
1068          */
1069         pp_rnb = ost_tls(req)->remote;
1070
1071         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
1072         npages = get_per_page_niobufs(ioo, objcount,remote_nb,niocount,&pp_rnb);
1073         if (npages < 0)
1074                 GOTO (out, rc = npages);
1075
1076         size[1] = npages * sizeof(*pp_rnb);
1077         rc = lustre_pack_reply(req, 2, size, NULL);
1078         if (rc)
1079                 GOTO(out, rc);
1080
1081         req->rq_status = obd_san_preprw(cmd, req->rq_export, &body->oa,
1082                                         objcount, ioo, npages, pp_rnb);
1083
1084         if (req->rq_status)
1085                 GOTO(out, rc = 0);
1086
1087         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
1088         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
1089
1090         res_nb = lustre_msg_buf(req->rq_repmsg, 1, size[1]);
1091         memcpy(res_nb, remote_nb, size[1]);
1092         rc = 0;
1093 out:
1094         target_committed_to_req(req);
1095         if (rc) {
1096                 req->rq_status = rc;
1097                 ptlrpc_error(req);
1098         } else {
1099                 ptlrpc_reply(req);
1100         }
1101
1102         return rc;
1103 }
1104
1105
1106 static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
1107 {
1108         char *key, *val = NULL;
1109         int keylen, vallen, rc = 0;
1110         ENTRY;
1111
1112         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1113         if (key == NULL) {
1114                 DEBUG_REQ(D_HA, req, "no set_info key");
1115                 RETURN(-EFAULT);
1116         }
1117         keylen = lustre_msg_buflen(req->rq_reqmsg,0);
1118
1119         rc = lustre_pack_reply(req, 0, NULL, NULL);
1120         if (rc)
1121                 RETURN(rc);
1122
1123         vallen = lustre_msg_buflen(req->rq_reqmsg, 1);
1124         if (vallen)
1125                 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
1126
1127         if (KEY_IS("evict_by_nid")) {
1128                 if (val && vallen)
1129                         obd_export_evict_by_nid(exp->exp_obd, val);
1130
1131                 GOTO(out, rc = 0);
1132         }
1133
1134         rc = obd_set_info(exp, keylen, key, vallen, val);
1135 out:
1136         req->rq_repmsg->status = 0;
1137         RETURN(rc);
1138 }
1139
1140 static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
1141 {
1142         char *key;
1143         int keylen, rc = 0, size = sizeof(obd_id);
1144         obd_id *reply;
1145         ENTRY;
1146
1147         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1148         if (key == NULL) {
1149                 DEBUG_REQ(D_HA, req, "no get_info key");
1150                 RETURN(-EFAULT);
1151         }
1152         keylen = req->rq_reqmsg->buflens[0];
1153
1154         if (keylen < strlen("last_id") || memcmp(key, "last_id", 7) != 0)
1155                 RETURN(-EPROTO);
1156
1157         rc = lustre_pack_reply(req, 1, &size, NULL);
1158         if (rc)
1159                 RETURN(rc);
1160
1161         reply = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply));
1162         rc = obd_get_info(exp, keylen, key, &size, reply);
1163         req->rq_repmsg->status = 0;
1164         RETURN(rc);
1165 }
1166
1167 static int ost_filter_recovery_request(struct ptlrpc_request *req,
1168                                        struct obd_device *obd, int *process)
1169 {
1170         switch (req->rq_reqmsg->opc) {
1171         case OST_CONNECT: /* This will never get here, but for completeness. */
1172         case OST_DISCONNECT:
1173                *process = 1;
1174                RETURN(0);
1175
1176         case OBD_PING:
1177         case OST_CREATE:
1178         case OST_DESTROY:
1179         case OST_PUNCH:
1180         case OST_SETATTR:
1181         case OST_SYNC:
1182         case OST_WRITE:
1183         case OBD_LOG_CANCEL:
1184         case LDLM_ENQUEUE:
1185                 *process = target_queue_recovery_request(req, obd);
1186                 RETURN(0);
1187
1188         default:
1189                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1190                 *process = 0;
1191                 /* XXX what should we set rq_status to here? */
1192                 req->rq_status = -EAGAIN;
1193                 RETURN(ptlrpc_error(req));
1194         }
1195 }
1196
1197 static int ost_handle(struct ptlrpc_request *req)
1198 {
1199         struct obd_trans_info trans_info = { 0, };
1200         struct obd_trans_info *oti = &trans_info;
1201         int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
1202         struct obd_device *obd = NULL;
1203         ENTRY;
1204
1205         LASSERT(current->journal_info == NULL);
1206         /* XXX identical to MDS */
1207         if (req->rq_reqmsg->opc != OST_CONNECT) {
1208                 int abort_recovery, recovering;
1209
1210                 if (req->rq_export == NULL) {
1211                         CDEBUG(D_HA,"operation %d on unconnected OST from %s\n",
1212                                req->rq_reqmsg->opc, libcfs_id2str(req->rq_peer));
1213                         req->rq_status = -ENOTCONN;
1214                         GOTO(out, rc = -ENOTCONN);
1215                 }
1216
1217                 obd = req->rq_export->exp_obd;
1218
1219                 /* Check for aborted recovery. */
1220                 spin_lock_bh(&obd->obd_processing_task_lock);
1221                 abort_recovery = obd->obd_abort_recovery;
1222                 recovering = obd->obd_recovering;
1223                 spin_unlock_bh(&obd->obd_processing_task_lock);
1224                 if (abort_recovery) {
1225                         target_abort_recovery(obd);
1226                 } else if (recovering) {
1227                         rc = ost_filter_recovery_request(req, obd,
1228                                                          &should_process);
1229                         if (rc || !should_process)
1230                                 RETURN(rc);
1231                 }
1232         }
1233
1234         oti_init(oti, req);
1235
1236         switch (req->rq_reqmsg->opc) {
1237         case OST_CONNECT: {
1238                 CDEBUG(D_INODE, "connect\n");
1239                 OBD_FAIL_RETURN(OBD_FAIL_OST_CONNECT_NET, 0);
1240                 rc = target_handle_connect(req, ost_handle);
1241                 if (!rc)
1242                         obd = req->rq_export->exp_obd;
1243                 break;
1244         }
1245         case OST_DISCONNECT:
1246                 CDEBUG(D_INODE, "disconnect\n");
1247                 OBD_FAIL_RETURN(OBD_FAIL_OST_DISCONNECT_NET, 0);
1248                 rc = target_handle_disconnect(req);
1249                 break;
1250         case OST_CREATE:
1251                 CDEBUG(D_INODE, "create\n");
1252                 OBD_FAIL_RETURN(OBD_FAIL_OST_CREATE_NET, 0);
1253                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_ENOSPC))
1254                         GOTO(out, rc = -ENOSPC);
1255                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1256                         GOTO(out, rc = -EROFS);
1257                 rc = ost_create(req->rq_export, req, oti);
1258                 break;
1259         case OST_DESTROY:
1260                 CDEBUG(D_INODE, "destroy\n");
1261                 OBD_FAIL_RETURN(OBD_FAIL_OST_DESTROY_NET, 0);
1262                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1263                         GOTO(out, rc = -EROFS);
1264                 rc = ost_destroy(req->rq_export, req, oti);
1265                 break;
1266         case OST_GETATTR:
1267                 CDEBUG(D_INODE, "getattr\n");
1268                 OBD_FAIL_RETURN(OBD_FAIL_OST_GETATTR_NET, 0);
1269                 rc = ost_getattr(req->rq_export, req);
1270                 break;
1271         case OST_SETATTR:
1272                 CDEBUG(D_INODE, "setattr\n");
1273                 OBD_FAIL_RETURN(OBD_FAIL_OST_SETATTR_NET, 0);
1274                 rc = ost_setattr(req->rq_export, req, oti);
1275                 break;
1276         case OST_WRITE:
1277                 CDEBUG(D_INODE, "write\n");
1278                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1279                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_ENOSPC))
1280                         GOTO(out, rc = -ENOSPC);
1281                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1282                         GOTO(out, rc = -EROFS);
1283                 rc = ost_brw_write(req, oti);
1284                 LASSERT(current->journal_info == NULL);
1285                 /* ost_brw_write sends its own replies */
1286                 RETURN(rc);
1287         case OST_READ:
1288                 CDEBUG(D_INODE, "read\n");
1289                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1290                 rc = ost_brw_read(req, oti);
1291                 LASSERT(current->journal_info == NULL);
1292                 /* ost_brw_read sends its own replies */
1293                 RETURN(rc);
1294         case OST_SAN_READ:
1295                 CDEBUG(D_INODE, "san read\n");
1296                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1297                 rc = ost_san_brw(req, OBD_BRW_READ);
1298                 /* ost_san_brw sends its own replies */
1299                 RETURN(rc);
1300         case OST_SAN_WRITE:
1301                 CDEBUG(D_INODE, "san write\n");
1302                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1303                 rc = ost_san_brw(req, OBD_BRW_WRITE);
1304                 /* ost_san_brw sends its own replies */
1305                 RETURN(rc);
1306         case OST_PUNCH:
1307                 CDEBUG(D_INODE, "punch\n");
1308                 OBD_FAIL_RETURN(OBD_FAIL_OST_PUNCH_NET, 0);
1309                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_OST_EROFS))
1310                         GOTO(out, rc = -EROFS);
1311                 rc = ost_punch(req->rq_export, req, oti);
1312                 break;
1313         case OST_STATFS:
1314                 CDEBUG(D_INODE, "statfs\n");
1315                 OBD_FAIL_RETURN(OBD_FAIL_OST_STATFS_NET, 0);
1316                 rc = ost_statfs(req);
1317                 break;
1318         case OST_SYNC:
1319                 CDEBUG(D_INODE, "sync\n");
1320                 OBD_FAIL_RETURN(OBD_FAIL_OST_SYNC_NET, 0);
1321                 rc = ost_sync(req->rq_export, req);
1322                 break;
1323         case OST_SET_INFO:
1324                 DEBUG_REQ(D_INODE, req, "set_info");
1325                 rc = ost_set_info(req->rq_export, req);
1326                 break;
1327         case OST_GET_INFO:
1328                 DEBUG_REQ(D_INODE, req, "get_info");
1329                 rc = ost_get_info(req->rq_export, req);
1330                 break;
1331         case OST_QUOTACHECK:
1332                 CDEBUG(D_INODE, "quotacheck\n");
1333                 OBD_FAIL_RETURN(OBD_FAIL_OST_QUOTACHECK_NET, 0);
1334                 rc = ost_quotacheck(req);
1335                 break;
1336         case OST_QUOTACTL:
1337                 CDEBUG(D_INODE, "quotactl\n");
1338                 OBD_FAIL_RETURN(OBD_FAIL_OST_QUOTACTL_NET, 0);
1339                 rc = ost_quotactl(req);
1340                 break;
1341         case OBD_PING:
1342                 DEBUG_REQ(D_INODE, req, "ping");
1343                 rc = target_handle_ping(req);
1344                 break;
1345         /* FIXME - just reply status */
1346         case LLOG_ORIGIN_CONNECT:
1347                 DEBUG_REQ(D_INODE, req, "log connect\n");
1348                 rc = llog_handle_connect(req);
1349                 req->rq_status = rc;
1350                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1351                 if (rc)
1352                         RETURN(rc);
1353                 RETURN(ptlrpc_reply(req));
1354         case OBD_LOG_CANCEL:
1355                 CDEBUG(D_INODE, "log cancel\n");
1356                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1357                 rc = llog_origin_handle_cancel(req);
1358                 req->rq_status = rc;
1359                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1360                 if (rc)
1361                         RETURN(rc);
1362                 RETURN(ptlrpc_reply(req));
1363         case LDLM_ENQUEUE:
1364                 CDEBUG(D_INODE, "enqueue\n");
1365                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1366                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1367                                          ldlm_server_blocking_ast,
1368                                          ldlm_server_glimpse_ast);
1369                 fail = OBD_FAIL_OST_LDLM_REPLY_NET;
1370                 break;
1371         case LDLM_CONVERT:
1372                 CDEBUG(D_INODE, "convert\n");
1373                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1374                 rc = ldlm_handle_convert(req);
1375                 break;
1376         case LDLM_CANCEL:
1377                 CDEBUG(D_INODE, "cancel\n");
1378                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1379                 rc = ldlm_handle_cancel(req);
1380                 break;
1381         case LDLM_BL_CALLBACK:
1382         case LDLM_CP_CALLBACK:
1383                 CDEBUG(D_INODE, "callback\n");
1384                 CERROR("callbacks should not happen on OST\n");
1385                 /* fall through */
1386         default:
1387                 CERROR("Unexpected opcode %d\n", req->rq_reqmsg->opc);
1388                 req->rq_status = -ENOTSUPP;
1389                 rc = ptlrpc_error(req);
1390                 RETURN(rc);
1391         }
1392
1393         LASSERT(current->journal_info == NULL);
1394
1395         EXIT;
1396         /* If we're DISCONNECTing, the export_data is already freed */
1397         if (!rc && req->rq_reqmsg->opc != OST_DISCONNECT)
1398                 target_committed_to_req(req);
1399
1400 out:
1401         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
1402                 if (obd && obd->obd_recovering) {
1403                         DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
1404                         return target_queue_final_reply(req, rc);
1405                 }
1406                 /* Lost a race with recovery; let the error path DTRT. */
1407                 rc = req->rq_status = -ENOTCONN;
1408         }
1409
1410         if (!rc)
1411                 oti_to_request(oti, req);
1412
1413         target_send_reply(req, rc, fail);
1414         return 0;
1415 }
1416
1417 /*
1418  * free per-thread pool created by ost_thread_init().
1419  */
1420 static void ost_thread_done(struct ptlrpc_thread *thread)
1421 {
1422         int i;
1423         struct ost_thread_local_cache *tls; /* TLS stands for Thread-Local
1424                                              * Storage */
1425
1426         ENTRY;
1427
1428         LASSERT(thread != NULL);
1429         LASSERT(thread->t_data != NULL);
1430
1431         /*
1432          * be prepared to handle partially-initialized pools (because this is
1433          * called from ost_thread_init() for cleanup.
1434          */
1435         tls = thread->t_data;
1436         if (tls != NULL) {
1437                 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1438                         if (tls->page[i] != NULL)
1439                                 __free_page(tls->page[i]);
1440                 }
1441                 OBD_FREE_PTR(tls);
1442                 thread->t_data = NULL;
1443         }
1444         EXIT;
1445 }
1446
1447 /*
1448  * initialize per-thread page pool (bug 5137).
1449  */
1450 static int ost_thread_init(struct ptlrpc_thread *thread)
1451 {
1452         int result;
1453         int i;
1454         struct ost_thread_local_cache *tls;
1455
1456         ENTRY;
1457
1458         LASSERT(thread != NULL);
1459         LASSERT(thread->t_data == NULL);
1460         LASSERT(thread->t_id < OST_NUM_THREADS);
1461
1462         OBD_ALLOC_PTR(tls);
1463         if (tls != NULL) {
1464                 result = 0;
1465                 thread->t_data = tls;
1466                 /*
1467                  * populate pool
1468                  */
1469                 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1470                         tls->page[i] = alloc_page(OST_THREAD_POOL_GFP);
1471                         if (tls->page[i] == NULL) {
1472                                 ost_thread_done(thread);
1473                                 result = -ENOMEM;
1474                                 break;
1475                         }
1476                 }
1477         } else
1478                 result = -ENOMEM;
1479         RETURN(result);
1480 }
1481
1482 static int ost_setup(struct obd_device *obd, obd_count len, void *buf)
1483 {
1484         struct ost_obd *ost = &obd->u.ost;
1485         struct lprocfs_static_vars lvars;
1486         int rc;
1487         ENTRY;
1488
1489         rc = cleanup_group_info();
1490         if (rc)
1491                 RETURN(rc);
1492
1493         rc = llog_start_commit_thread();
1494         if (rc < 0)
1495                 RETURN(rc);
1496
1497         lprocfs_init_vars(ost, &lvars);
1498         lprocfs_obd_setup(obd, lvars.obd_vars);
1499
1500         sema_init(&ost->ost_health_sem, 1);
1501
1502         ost->ost_service =
1503                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1504                                 OST_MAXREPSIZE, OST_REQUEST_PORTAL,
1505                                 OSC_REPLY_PORTAL,
1506                                 obd_timeout * 1000, ost_handle, LUSTRE_OST_NAME,
1507                                 obd->obd_proc_entry, ost_print_req,
1508                                 OST_NUM_THREADS);
1509         if (ost->ost_service == NULL) {
1510                 CERROR("failed to start service\n");
1511                 GOTO(out_lprocfs, rc = -ENOMEM);
1512         }
1513
1514         rc = ptlrpc_start_threads(obd, ost->ost_service, "ll_ost");
1515         if (rc)
1516                 GOTO(out_service, rc = -EINVAL);
1517
1518         ost->ost_create_service =
1519                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1520                                 OST_MAXREPSIZE, OST_CREATE_PORTAL,
1521                                 OSC_REPLY_PORTAL,
1522                                 obd_timeout * 1000, ost_handle, "ost_create",
1523                                 obd->obd_proc_entry, ost_print_req, 1);
1524         if (ost->ost_create_service == NULL) {
1525                 CERROR("failed to start OST create service\n");
1526                 GOTO(out_service, rc = -ENOMEM);
1527         }
1528
1529         rc = ptlrpc_start_threads(obd, ost->ost_create_service,
1530                                   "ll_ost_creat");
1531         if (rc)
1532                 GOTO(out_create, rc = -EINVAL);
1533
1534         ost->ost_io_service =
1535                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1536                                 OST_MAXREPSIZE, OST_IO_PORTAL,
1537                                 OSC_REPLY_PORTAL,
1538                                 obd_timeout * 1000, ost_handle, "ost_io",
1539                                 obd->obd_proc_entry, ost_print_req,
1540                                 OST_NUM_THREADS);
1541         if (ost->ost_io_service == NULL) {
1542                 CERROR("failed to start OST I/O service\n");
1543                 GOTO(out_create, rc = -ENOMEM);
1544         }
1545
1546         ost->ost_io_service->srv_init = ost_thread_init;
1547         ost->ost_io_service->srv_done = ost_thread_done;
1548         ost->ost_io_service->srv_cpu_affinity = 1;
1549         rc = ptlrpc_start_threads(obd, ost->ost_io_service,
1550                                   "ll_ost_io");
1551         if (rc)
1552                 GOTO(out_io, rc = -EINVAL);
1553
1554         RETURN(0);
1555
1556 out_io:
1557         ptlrpc_unregister_service(ost->ost_io_service);
1558         ost->ost_io_service = NULL;
1559 out_create:
1560         ptlrpc_unregister_service(ost->ost_create_service);
1561         ost->ost_create_service = NULL;
1562 out_service:
1563         ptlrpc_unregister_service(ost->ost_service);
1564         ost->ost_service = NULL;
1565 out_lprocfs:
1566         lprocfs_obd_cleanup(obd);
1567         RETURN(rc);
1568 }
1569
1570 static int ost_cleanup(struct obd_device *obd)
1571 {
1572         struct ost_obd *ost = &obd->u.ost;
1573         int err = 0;
1574         ENTRY;
1575
1576         spin_lock_bh(&obd->obd_processing_task_lock);
1577         if (obd->obd_recovering) {
1578                 target_cancel_recovery_timer(obd);
1579                 obd->obd_recovering = 0;
1580         }
1581         spin_unlock_bh(&obd->obd_processing_task_lock);
1582
1583         down(&ost->ost_health_sem);
1584         ptlrpc_unregister_service(ost->ost_service);
1585         ptlrpc_unregister_service(ost->ost_create_service);
1586         ptlrpc_unregister_service(ost->ost_io_service);
1587         ost->ost_service = NULL;
1588         ost->ost_create_service = NULL;
1589         up(&ost->ost_health_sem);
1590
1591         lprocfs_obd_cleanup(obd);
1592
1593         RETURN(err);
1594 }
1595
1596 static int ost_health_check(struct obd_device *obd)
1597 {
1598         struct ost_obd *ost = &obd->u.ost;
1599         int rc = 0;
1600
1601         down(&ost->ost_health_sem);
1602         rc |= ptlrpc_service_health_check(ost->ost_service);
1603         rc |= ptlrpc_service_health_check(ost->ost_create_service);
1604         rc |= ptlrpc_service_health_check(ost->ost_io_service);
1605         up(&ost->ost_health_sem);
1606
1607         /*
1608          * health_check to return 0 on healthy
1609          * and 1 on unhealthy.
1610          */
1611         if( rc != 0)
1612                 rc = 1;
1613
1614         return rc;
1615 }
1616
1617 struct ost_thread_local_cache *ost_tls(struct ptlrpc_request *r)
1618 {
1619         return (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
1620 }
1621
1622 /* use obd ops to offer management infrastructure */
1623 static struct obd_ops ost_obd_ops = {
1624         .o_owner        = THIS_MODULE,
1625         .o_setup        = ost_setup,
1626         .o_cleanup      = ost_cleanup,
1627         .o_health_check = ost_health_check,
1628 };
1629
1630 static int __init ost_init(void)
1631 {
1632         struct lprocfs_static_vars lvars;
1633         ENTRY;
1634
1635         lprocfs_init_vars(ost,&lvars);
1636         RETURN(class_register_type(&ost_obd_ops, lvars.module_vars,
1637                                    LUSTRE_OST_NAME));
1638 }
1639
1640 static void /*__exit*/ ost_exit(void)
1641 {
1642         class_unregister_type(LUSTRE_OST_NAME);
1643 }
1644
1645 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1646 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
1647 MODULE_LICENSE("GPL");
1648
1649 module_init(ost_init);
1650 module_exit(ost_exit);