Whamcloud - gitweb
2a1fa8c5118c6ec426f793731220e62c73e31ee0
[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 <obd_cksum.h>
43 #include <obd_ost.h>
44 #include <lustre_net.h>
45 #include <lustre_dlm.h>
46 #include <lustre_export.h>
47 #include <lustre_debug.h>
48 #include <linux/init.h>
49 #include <lprocfs_status.h>
50 #include <lustre_commit_confd.h>
51 #include <libcfs/list.h>
52 #include <lustre_quota.h>
53 #include "ost_internal.h"
54
55 static int oss_num_threads;
56 CFS_MODULE_PARM(oss_num_threads, "i", int, 0444,
57                 "number of OSS service threads to start");
58
59 static int ost_num_threads;
60 CFS_MODULE_PARM(ost_num_threads, "i", int, 0444,
61                 "number of OST service threads to start (deprecated)");
62
63 static int oss_num_create_threads;
64 CFS_MODULE_PARM(oss_num_create_threads, "i", int, 0444,
65                 "number of OSS create threads to start");
66
67 void oti_to_request(struct obd_trans_info *oti, struct ptlrpc_request *req)
68 {
69         struct oti_req_ack_lock *ack_lock;
70         int i;
71
72         if (oti == NULL)
73                 return;
74
75         if (req->rq_repmsg)
76                 lustre_msg_set_transno(req->rq_repmsg, oti->oti_transno);
77         req->rq_transno = oti->oti_transno;
78
79         /* XXX 4 == entries in oti_ack_locks??? */
80         for (ack_lock = oti->oti_ack_locks, i = 0; i < 4; i++, ack_lock++) {
81                 if (!ack_lock->mode)
82                         break;
83                 /* XXX not even calling target_send_reply in some cases... */
84                 ptlrpc_save_lock (req, &ack_lock->lock, ack_lock->mode);
85         }
86 }
87
88 static int ost_destroy(struct obd_export *exp, struct ptlrpc_request *req,
89                        struct obd_trans_info *oti)
90 {
91         struct ost_body *body, *repbody;
92         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
93         ENTRY;
94
95         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
96                                   lustre_swab_ost_body);
97         if (body == NULL)
98                 RETURN(-EFAULT);
99
100         if (body->oa.o_id == 0)
101                 RETURN(-EPROTO);
102
103         if (lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1)) {
104                 struct ldlm_request *dlm;
105                 dlm = lustre_swab_reqbuf(req, REQ_REC_OFF + 1, sizeof(*dlm),
106                                          lustre_swab_ldlm_request);
107                 if (dlm == NULL)
108                         RETURN (-EFAULT);
109                 ldlm_request_cancel(req, dlm, 0);
110         }
111         
112         rc = lustre_pack_reply(req, 2, size, NULL);
113         if (rc)
114                 RETURN(rc);
115
116         if (body->oa.o_valid & OBD_MD_FLCOOKIE)
117                 oti->oti_logcookies = obdo_logcookie(&body->oa);
118         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
119                                  sizeof(*repbody));
120         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
121         req->rq_status = obd_destroy(exp, &body->oa, NULL, oti, NULL);
122         RETURN(0);
123 }
124
125 static int ost_getattr(struct obd_export *exp, struct ptlrpc_request *req)
126 {
127         struct ost_body *body, *repbody;
128         struct obd_info oinfo = { { { 0 } } };
129         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
130         ENTRY;
131
132         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
133                                   lustre_swab_ost_body);
134         if (body == NULL)
135                 RETURN(-EFAULT);
136
137         rc = lustre_pack_reply(req, 2, size, NULL);
138         if (rc)
139                 RETURN(rc);
140
141         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
142                                  sizeof(*repbody));
143         repbody->oa = body->oa;
144
145         oinfo.oi_oa = &repbody->oa;
146         if (oinfo.oi_oa->o_valid & OBD_MD_FLOSSCAPA)
147                 oinfo.oi_capa = lustre_unpack_capa(req->rq_reqmsg,
148                                                    REQ_REC_OFF + 1);
149         req->rq_status = obd_getattr(exp, &oinfo);
150         RETURN(0);
151 }
152
153 static int ost_statfs(struct ptlrpc_request *req)
154 {
155         struct obd_statfs *osfs;
156         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*osfs) };
157         ENTRY;
158
159         rc = lustre_pack_reply(req, 2, size, NULL);
160         if (rc)
161                 RETURN(rc);
162
163         osfs = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*osfs));
164
165         req->rq_status = obd_statfs(req->rq_export->exp_obd, osfs,
166                                     cfs_time_current_64() - HZ, 0);
167         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
168                 osfs->os_bfree = osfs->os_bavail = 64;
169         if (req->rq_status != 0)
170                 CERROR("ost: statfs failed: rc %d\n", req->rq_status);
171
172         RETURN(0);
173 }
174
175 static int ost_create(struct obd_export *exp, struct ptlrpc_request *req,
176                       struct obd_trans_info *oti)
177 {
178         struct ost_body *body, *repbody;
179         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
180         ENTRY;
181
182         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
183                                   lustre_swab_ost_body);
184         if (body == NULL)
185                 RETURN(-EFAULT);
186
187         rc = lustre_pack_reply(req, 2, size, NULL);
188         if (rc)
189                 RETURN(rc);
190
191         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
192                                  sizeof(*repbody));
193         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
194         oti->oti_logcookies = obdo_logcookie(&repbody->oa);
195         req->rq_status = obd_create(exp, &repbody->oa, NULL, oti);
196         //obd_log_cancel(conn, NULL, 1, oti->oti_logcookies, 0);
197         RETURN(0);
198 }
199
200 /*
201  * Helper function for ost_punch(): if asked by client, acquire [size, EOF]
202  * lock on the file being truncated.
203  */
204 static int ost_punch_lock_get(struct obd_export *exp, struct obdo *oa,
205                               struct lustre_handle *lh)
206 {
207         int flags;
208         struct ldlm_res_id res_id = { .name = { oa->o_id, 0, oa->o_gr, 0} };
209         ldlm_policy_data_t policy;
210         __u64 start;
211         __u64 finis;
212
213         ENTRY;
214
215         LASSERT(!lustre_handle_is_used(lh));
216
217         if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
218             !(oa->o_flags & OBD_FL_TRUNCLOCK))
219                 RETURN(0);
220
221         CDEBUG(D_INODE, "OST-side truncate lock.\n");
222
223         start = oa->o_size;
224         finis = start + oa->o_blocks;
225
226         /*
227          * standard truncate optimization: if file body is completely
228          * destroyed, don't send data back to the server.
229          */
230         flags = (start == 0) ? LDLM_AST_DISCARD_DATA : 0;
231
232         policy.l_extent.start = start & CFS_PAGE_MASK;
233
234         /*
235          * If ->o_blocks is EOF it means "lock till the end of the
236          * file". Otherwise, it's size of a hole being punched (in bytes)
237          */
238         if (oa->o_blocks == OBD_OBJECT_EOF || finis < start)
239                 policy.l_extent.end = OBD_OBJECT_EOF;
240         else
241                 policy.l_extent.end = finis | ~CFS_PAGE_MASK;
242
243         RETURN(ldlm_cli_enqueue_local(exp->exp_obd->obd_namespace, &res_id,
244                                       LDLM_EXTENT, &policy, LCK_PW, &flags,
245                                       ldlm_blocking_ast, ldlm_completion_ast,
246                                       ldlm_glimpse_ast, NULL, 0, NULL, lh));
247 }
248
249 /*
250  * Helper function for ost_punch(): release lock acquired by
251  * ost_punch_lock_get(), if any.
252  */
253 static void ost_punch_lock_put(struct obd_export *exp, struct obdo *oa,
254                                struct lustre_handle *lh)
255 {
256         ENTRY;
257         if (lustre_handle_is_used(lh))
258                 ldlm_lock_decref(lh, LCK_PW);
259         EXIT;
260 }
261
262 static int ost_punch(struct obd_export *exp, struct ptlrpc_request *req,
263                      struct obd_trans_info *oti)
264 {
265         struct obd_info oinfo = { { { 0 } } };
266         struct ost_body *body, *repbody;
267         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
268         struct lustre_handle lh = {0,};
269         ENTRY;
270
271         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
272         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
273
274         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
275                                   lustre_swab_ost_body);
276         if (body == NULL)
277                 RETURN(-EFAULT);
278
279         oinfo.oi_oa = &body->oa;
280         oinfo.oi_policy.l_extent.start = oinfo.oi_oa->o_size;
281         oinfo.oi_policy.l_extent.end = oinfo.oi_oa->o_blocks;
282
283         if ((oinfo.oi_oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
284             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
285                 RETURN(-EINVAL);
286
287         rc = lustre_pack_reply(req, 2, size, NULL);
288         if (rc)
289                 RETURN(rc);
290
291         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
292                                  sizeof(*repbody));
293         rc = ost_punch_lock_get(exp, oinfo.oi_oa, &lh);
294         if (rc == 0) {
295                 if (oinfo.oi_oa->o_valid & OBD_MD_FLFLAGS &&
296                     oinfo.oi_oa->o_flags == OBD_FL_TRUNCLOCK)
297                         /*
298                          * If OBD_FL_TRUNCLOCK is the only bit set in
299                          * ->o_flags, clear OBD_MD_FLFLAGS to avoid falling
300                          * through filter_setattr() to filter_iocontrol().
301                          */
302                         oinfo.oi_oa->o_valid &= ~OBD_MD_FLFLAGS;
303
304                 if (oinfo.oi_oa->o_valid & OBD_MD_FLOSSCAPA)
305                         oinfo.oi_capa = lustre_unpack_capa(req->rq_reqmsg,
306                                                            REQ_REC_OFF + 1);
307                 req->rq_status = obd_punch(exp, &oinfo, oti, NULL);
308                 ost_punch_lock_put(exp, oinfo.oi_oa, &lh);
309         }
310         repbody->oa = *oinfo.oi_oa;
311         RETURN(rc);
312 }
313
314 static int ost_sync(struct obd_export *exp, struct ptlrpc_request *req)
315 {
316         struct ost_body *body, *repbody;
317         struct lustre_capa *capa = NULL;
318         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
319         ENTRY;
320
321         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
322                                   lustre_swab_ost_body);
323         if (body == NULL)
324                 RETURN(-EFAULT);
325
326         if (body->oa.o_valid & OBD_MD_FLOSSCAPA)
327                 capa = lustre_unpack_capa(req->rq_reqmsg, REQ_REC_OFF + 1);
328
329         rc = lustre_pack_reply(req, 2, size, NULL);
330         if (rc)
331                 RETURN(rc);
332
333         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
334                                  sizeof(*repbody));
335         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
336         req->rq_status = obd_sync(exp, &repbody->oa, NULL, repbody->oa.o_size,
337                                   repbody->oa.o_blocks, capa);
338         RETURN(0);
339 }
340
341 static int ost_setattr(struct obd_export *exp, struct ptlrpc_request *req,
342                        struct obd_trans_info *oti)
343 {
344         struct ost_body *body, *repbody;
345         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
346         struct obd_info oinfo = { { { 0 } } };
347         ENTRY;
348
349         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
350                                   lustre_swab_ost_body);
351         if (body == NULL)
352                 RETURN(-EFAULT);
353
354         rc = lustre_pack_reply(req, 2, size, NULL);
355         if (rc)
356                 RETURN(rc);
357
358         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
359                                  sizeof(*repbody));
360         repbody->oa = body->oa;
361
362         oinfo.oi_oa = &repbody->oa;
363         if (oinfo.oi_oa->o_valid & OBD_MD_FLOSSCAPA)
364                 oinfo.oi_capa = lustre_unpack_capa(req->rq_reqmsg,
365                                                    REQ_REC_OFF + 1);
366         req->rq_status = obd_setattr(exp, &oinfo, oti);
367         RETURN(0);
368 }
369
370 static int ost_bulk_timeout(void *data)
371 {
372         ENTRY;
373         /* We don't fail the connection here, because having the export
374          * killed makes the (vital) call to commitrw very sad.
375          */
376         RETURN(1);
377 }
378
379 static int get_per_page_niobufs(struct obd_ioobj *ioo, int nioo,
380                                 struct niobuf_remote *rnb, int nrnb,
381                                 struct niobuf_remote **pp_rnbp)
382 {
383         /* Copy a remote niobuf, splitting it into page-sized chunks
384          * and setting ioo[i].ioo_bufcnt accordingly */
385         struct niobuf_remote *pp_rnb;
386         int   i;
387         int   j;
388         int   page;
389         int   rnbidx = 0;
390         int   npages = 0;
391
392         /*
393          * array of sufficient size already preallocated by caller
394          */
395         LASSERT(pp_rnbp != NULL);
396         LASSERT(*pp_rnbp != NULL);
397
398         /* first count and check the number of pages required */
399         for (i = 0; i < nioo; i++)
400                 for (j = 0; j < ioo->ioo_bufcnt; j++, rnbidx++) {
401                         obd_off offset = rnb[rnbidx].offset;
402                         obd_off p0 = offset >> CFS_PAGE_SHIFT;
403                         obd_off pn = (offset + rnb[rnbidx].len - 1) >>
404                                      CFS_PAGE_SHIFT;
405
406                         LASSERT(rnbidx < nrnb);
407
408                         npages += (pn + 1 - p0);
409
410                         if (rnb[rnbidx].len == 0) {
411                                 CERROR("zero len BRW: obj %d objid "LPX64
412                                        " buf %u\n", i, ioo[i].ioo_id, j);
413                                 return -EINVAL;
414                         }
415                         if (j > 0 &&
416                             rnb[rnbidx].offset <= rnb[rnbidx-1].offset) {
417                                 CERROR("unordered BRW: obj %d objid "LPX64
418                                        " buf %u offset "LPX64" <= "LPX64"\n",
419                                        i, ioo[i].ioo_id, j, rnb[rnbidx].offset,
420                                        rnb[rnbidx].offset);
421                                 return -EINVAL;
422                         }
423                 }
424
425         LASSERT(rnbidx == nrnb);
426
427         if (npages == nrnb) {       /* all niobufs are for single pages */
428                 *pp_rnbp = rnb;
429                 return npages;
430         }
431
432         pp_rnb = *pp_rnbp;
433
434         /* now do the actual split */
435         page = rnbidx = 0;
436         for (i = 0; i < nioo; i++) {
437                 int  obj_pages = 0;
438
439                 for (j = 0; j < ioo[i].ioo_bufcnt; j++, rnbidx++) {
440                         obd_off off = rnb[rnbidx].offset;
441                         int     nob = rnb[rnbidx].len;
442
443                         LASSERT(rnbidx < nrnb);
444                         do {
445                                 obd_off  poff = off & ~CFS_PAGE_MASK;
446                                 int      pnob = (poff + nob > CFS_PAGE_SIZE) ?
447                                                 PAGE_SIZE - poff : nob;
448
449                                 LASSERT(page < npages);
450                                 pp_rnb[page].len = pnob;
451                                 pp_rnb[page].offset = off;
452                                 pp_rnb[page].flags = rnb[rnbidx].flags;
453
454                                 CDEBUG(0, "   obj %d id "LPX64
455                                        "page %d(%d) "LPX64" for %d, flg %x\n",
456                                        i, ioo[i].ioo_id, obj_pages, page,
457                                        pp_rnb[page].offset, pp_rnb[page].len,
458                                        pp_rnb[page].flags);
459                                 page++;
460                                 obj_pages++;
461
462                                 off += pnob;
463                                 nob -= pnob;
464                         } while (nob > 0);
465                         LASSERT(nob == 0);
466                 }
467                 ioo[i].ioo_bufcnt = obj_pages;
468         }
469         LASSERT(page == npages);
470
471         return npages;
472 }
473
474 static __u32 ost_checksum_bulk(struct ptlrpc_bulk_desc *desc, int opc,
475                                cksum_type_t cksum_type)
476 {
477         __u32 cksum;
478         int i;
479
480         cksum = init_checksum(cksum_type);
481         for (i = 0; i < desc->bd_iov_count; i++) {
482                 struct page *page = desc->bd_iov[i].kiov_page;
483                 int off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
484                 char *ptr = kmap(page) + off;
485                 int len = desc->bd_iov[i].kiov_len;
486
487                 /* corrupt the data before we compute the checksum, to
488                  * simulate a client->OST data error */
489                 if (i == 0 && opc == OST_WRITE &&
490                     OBD_FAIL_CHECK(OBD_FAIL_OST_CHECKSUM_RECEIVE))
491                         memcpy(ptr, "bad3", min(4, len));
492                 cksum = compute_checksum(cksum, ptr, len, cksum_type);
493                 /* corrupt the data after we compute the checksum, to
494                  * simulate an OST->client data error */
495                 if (i == 0 && opc == OST_READ &&
496                     OBD_FAIL_CHECK(OBD_FAIL_OST_CHECKSUM_SEND))
497                         memcpy(ptr, "bad4", min(4, len));
498                 kunmap(page);
499         }
500
501         return cksum;
502 }
503
504 /*
505  * populate @nio by @nrpages pages from per-thread page pool
506  */
507 static void ost_nio_pages_get(struct ptlrpc_request *req,
508                               struct niobuf_local *nio, int nrpages)
509 {
510         int i;
511         struct ost_thread_local_cache *tls;
512
513         ENTRY;
514
515         LASSERT(nrpages <= OST_THREAD_POOL_SIZE);
516         LASSERT(req != NULL);
517         LASSERT(req->rq_svc_thread != NULL);
518
519         tls = ost_tls(req);
520         LASSERT(tls != NULL);
521
522         memset(nio, 0, nrpages * sizeof *nio);
523         for (i = 0; i < nrpages; ++ i) {
524                 struct page *page;
525
526                 page = tls->page[i];
527                 LASSERT(page != NULL);
528                 POISON_PAGE(page, 0xf1);
529                 nio[i].page = page;
530                 LL_CDEBUG_PAGE(D_INFO, page, "%d\n", i);
531         }
532         EXIT;
533 }
534
535 /*
536  * Dual for ost_nio_pages_get(). Poison pages in pool for debugging
537  */
538 static void ost_nio_pages_put(struct ptlrpc_request *req,
539                               struct niobuf_local *nio, int nrpages)
540 {
541         int i;
542
543         ENTRY;
544
545         LASSERT(nrpages <= OST_THREAD_POOL_SIZE);
546
547         for (i = 0; i < nrpages; ++ i)
548                 POISON_PAGE(nio[i].page, 0xf2);
549         EXIT;
550 }
551
552 static int ost_brw_lock_get(int mode, struct obd_export *exp,
553                             struct obd_ioobj *obj, struct niobuf_remote *nb,
554                             struct lustre_handle *lh)
555 {
556         int flags                 = 0;
557         int nrbufs                = obj->ioo_bufcnt;
558         struct ldlm_res_id res_id = { .name = { obj->ioo_id, 0,
559                                                 obj->ioo_gr, 0} };
560         ldlm_policy_data_t policy;
561         int i;
562
563         ENTRY;
564
565         LASSERT(mode == LCK_PR || mode == LCK_PW);
566         LASSERT(!lustre_handle_is_used(lh));
567
568         if (nrbufs == 0 || !(nb[0].flags & OBD_BRW_SRVLOCK))
569                 RETURN(0);
570
571         /* EXPENSIVE ASSERTION */
572         for (i = 1; i < nrbufs; i ++)
573                 LASSERT((nb[0].flags & OBD_BRW_SRVLOCK) ==
574                         (nb[i].flags & OBD_BRW_SRVLOCK));
575
576         policy.l_extent.start = nb[0].offset & CFS_PAGE_MASK;
577         policy.l_extent.end   = (nb[nrbufs - 1].offset +
578                                  nb[nrbufs - 1].len - 1) | ~CFS_PAGE_MASK;
579
580         RETURN(ldlm_cli_enqueue_local(exp->exp_obd->obd_namespace, &res_id,
581                                       LDLM_EXTENT, &policy, mode, &flags,
582                                       ldlm_blocking_ast, ldlm_completion_ast,
583                                       ldlm_glimpse_ast, NULL, 0, NULL, lh));
584 }
585
586 static void ost_brw_lock_put(int mode,
587                              struct obd_ioobj *obj, struct niobuf_remote *niob,
588                              struct lustre_handle *lh)
589 {
590         ENTRY;
591         LASSERT(mode == LCK_PR || mode == LCK_PW);
592         LASSERT((obj->ioo_bufcnt > 0 && (niob[0].flags & OBD_BRW_SRVLOCK)) ==
593                 lustre_handle_is_used(lh));
594         if (lustre_handle_is_used(lh))
595                 ldlm_lock_decref(lh, mode);
596         EXIT;
597 }
598
599 struct ost_prolong_data {
600         struct obd_export *opd_exp;
601         ldlm_policy_data_t opd_policy;
602         ldlm_mode_t opd_mode;
603 };
604
605 static int ost_prolong_locks_iter(struct ldlm_lock *lock, void *data)
606 {
607         struct ost_prolong_data *opd = data;
608
609         LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
610
611         if (lock->l_req_mode != lock->l_granted_mode) {
612                 /* scan granted locks only */
613                 return LDLM_ITER_STOP;
614         }
615
616         if (lock->l_export != opd->opd_exp) {
617                 /* prolong locks only for given client */
618                 return LDLM_ITER_CONTINUE;
619         }
620
621         if (!(lock->l_granted_mode & opd->opd_mode)) {
622                 /* we aren't interesting in all type of locks */
623                 return LDLM_ITER_CONTINUE;
624         }
625
626         if (lock->l_policy_data.l_extent.end < opd->opd_policy.l_extent.start ||
627             lock->l_policy_data.l_extent.start > opd->opd_policy.l_extent.end) {
628                 /* the request doesn't cross the lock, skip it */
629                 return LDLM_ITER_CONTINUE;
630         }
631
632         if (!(lock->l_flags & LDLM_FL_AST_SENT)) {
633                 /* ignore locks not being cancelled */
634                 return LDLM_ITER_CONTINUE;
635         }
636
637         /* OK. this is a possible lock the user holds doing I/O
638          * let's refresh eviction timer for it */
639         ldlm_refresh_waiting_lock(lock);
640
641         return LDLM_ITER_CONTINUE;
642 }
643
644 static void ost_prolong_locks(struct obd_export *exp, struct obd_ioobj *obj,
645                               struct niobuf_remote *nb, ldlm_mode_t mode)
646 {
647         struct ldlm_res_id res_id = { .name = { obj->ioo_id, 0,
648                                                 obj->ioo_gr, 0} };
649         int nrbufs = obj->ioo_bufcnt;
650         struct ost_prolong_data opd;
651
652         ENTRY;
653
654         opd.opd_mode = mode;
655         opd.opd_exp = exp;
656         opd.opd_policy.l_extent.start = nb[0].offset & CFS_PAGE_MASK;
657         opd.opd_policy.l_extent.end = (nb[nrbufs - 1].offset +
658                                        nb[nrbufs - 1].len - 1) | ~CFS_PAGE_MASK;
659
660         CDEBUG(D_DLMTRACE,"refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
661                res_id.name[0], res_id.name[1], opd.opd_policy.l_extent.start,
662                opd.opd_policy.l_extent.end);
663         ldlm_resource_iterate(exp->exp_obd->obd_namespace, &res_id,
664                               ost_prolong_locks_iter, &opd);
665 }
666
667 static int ost_brw_read(struct ptlrpc_request *req, struct obd_trans_info *oti)
668 {
669         struct ptlrpc_bulk_desc *desc;
670         struct obd_export *exp = req->rq_export;
671         struct niobuf_remote *remote_nb;
672         struct niobuf_remote *pp_rnb = NULL;
673         struct niobuf_local *local_nb;
674         struct obd_ioobj *ioo;
675         struct ost_body *body, *repbody;
676         struct lustre_capa *capa = NULL;
677         struct l_wait_info lwi;
678         struct lustre_handle lockh = { 0 };
679         int size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
680         int niocount, npages, nob = 0, rc, i;
681         int no_reply = 0;
682         ENTRY;
683
684         req->rq_bulk_read = 1;
685
686         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
687                 GOTO(out, rc = -EIO);
688
689         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, (obd_timeout + 1) / 4);
690
691         /* Check if there is eviction in progress, and if so, wait for it to
692          * finish */
693         if (unlikely(atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
694                 lwi = LWI_INTR(NULL, NULL); // We do not care how long it takes
695                 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
696                         !atomic_read(&exp->exp_obd->obd_evict_inprogress),
697                         &lwi);
698         }
699         if (exp->exp_failed)
700                 GOTO(out, rc = -ENOTCONN);
701
702         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
703                                   lustre_swab_ost_body);
704         if (body == NULL) {
705                 CERROR("Missing/short ost_body\n");
706                 GOTO(out, rc = -EFAULT);
707         }
708
709         ioo = lustre_swab_reqbuf(req, REQ_REC_OFF + 1, sizeof(*ioo),
710                                  lustre_swab_obd_ioobj);
711         if (ioo == NULL) {
712                 CERROR("Missing/short ioobj\n");
713                 GOTO(out, rc = -EFAULT);
714         }
715
716         niocount = ioo->ioo_bufcnt;
717         if (niocount > PTLRPC_MAX_BRW_PAGES) {
718                 DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)",
719                           niocount);
720                 GOTO(out, rc = -EFAULT);
721         }
722
723         remote_nb = lustre_swab_reqbuf(req, REQ_REC_OFF + 2,
724                                        niocount * sizeof(*remote_nb),
725                                        lustre_swab_niobuf_remote);
726         if (remote_nb == NULL) {
727                 CERROR("Missing/short niobuf\n");
728                 GOTO(out, rc = -EFAULT);
729         }
730         if (lustre_msg_swabbed(req->rq_reqmsg)) { /* swab remaining niobufs */
731                 for (i = 1; i < niocount; i++)
732                         lustre_swab_niobuf_remote (&remote_nb[i]);
733         }
734
735         if (body->oa.o_valid & OBD_MD_FLOSSCAPA)
736                 capa = lustre_unpack_capa(req->rq_reqmsg, REQ_REC_OFF + 3);
737
738         rc = lustre_pack_reply(req, 2, size, NULL);
739         if (rc)
740                 GOTO(out, rc);
741
742         /*
743          * Per-thread array of struct niobuf_{local,remote}'s was allocated by
744          * ost_thread_init().
745          */
746         local_nb = ost_tls(req)->local;
747         pp_rnb   = ost_tls(req)->remote;
748
749         /* FIXME all niobuf splitting should be done in obdfilter if needed */
750         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
751         npages = get_per_page_niobufs(ioo, 1, remote_nb, niocount, &pp_rnb);
752         if (npages < 0)
753                 GOTO(out, rc = npages);
754
755         LASSERT(npages <= OST_THREAD_POOL_SIZE);
756
757         ost_nio_pages_get(req, local_nb, npages);
758
759         desc = ptlrpc_prep_bulk_exp(req, npages,
760                                      BULK_PUT_SOURCE, OST_BULK_PORTAL);
761         if (desc == NULL)
762                 GOTO(out, rc = -ENOMEM);
763
764         rc = ost_brw_lock_get(LCK_PR, exp, ioo, pp_rnb, &lockh);
765         if (rc != 0)
766                 GOTO(out_bulk, rc);
767
768         /* 
769          * If getting the lock took more time than
770          * client was willing to wait, drop it. b=11330
771          */
772         if (cfs_time_current_sec() > req->rq_arrival_time.tv_sec + obd_timeout || 
773             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
774                 no_reply = 1;
775                 CERROR("Dropping timed-out read from %s because locking"
776                        "object "LPX64" took %ld seconds.\n",
777                        libcfs_id2str(req->rq_peer), ioo->ioo_id,
778                        cfs_time_current_sec() - req->rq_arrival_time.tv_sec);
779                 GOTO(out_lock, rc = -ETIMEDOUT);
780         }
781
782         rc = obd_preprw(OBD_BRW_READ, exp, &body->oa, 1,
783                         ioo, npages, pp_rnb, local_nb, oti, capa);
784         if (rc != 0)
785                 GOTO(out_lock, rc);
786
787         ost_prolong_locks(exp, ioo, pp_rnb, LCK_PW | LCK_PR);
788
789         nob = 0;
790         for (i = 0; i < npages; i++) {
791                 int page_rc = local_nb[i].rc;
792
793                 if (page_rc < 0) {              /* error */
794                         rc = page_rc;
795                         break;
796                 }
797
798                 LASSERTF(page_rc <= pp_rnb[i].len, "page_rc (%d) > "
799                          "pp_rnb[%d].len (%d)\n", page_rc, i, pp_rnb[i].len);
800                 nob += page_rc;
801                 if (page_rc != 0) {             /* some data! */
802                         LASSERT (local_nb[i].page != NULL);
803                         ptlrpc_prep_bulk_page(desc, local_nb[i].page,
804                                               pp_rnb[i].offset & ~CFS_PAGE_MASK,
805                                               page_rc);
806                 }
807
808                 if (page_rc != pp_rnb[i].len) { /* short read */
809                         /* All subsequent pages should be 0 */
810                         while(++i < npages)
811                                 LASSERT(local_nb[i].rc == 0);
812                         break;
813                 }
814         }
815
816         if (body->oa.o_valid & OBD_MD_FLCKSUM) {
817                 cksum_type_t cksum_type = OBD_CKSUM_CRC32;
818
819                 if (body->oa.o_valid & OBD_MD_FLFLAGS)
820                         cksum_type = cksum_type_unpack(body->oa.o_flags);
821                 body->oa.o_flags = cksum_type_pack(cksum_type);
822                 body->oa.o_valid = OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
823                 body->oa.o_cksum = ost_checksum_bulk(desc, OST_READ, cksum_type);
824                 CDEBUG(D_PAGE,"checksum at read origin: %x\n",body->oa.o_cksum);
825         } else {
826                 body->oa.o_valid = 0;
827         }
828         /* We're finishing using body->oa as an input variable */
829
830         /* Check if client was evicted while we were doing i/o before touching
831            network */
832         if (rc == 0) {
833                 /* Check if there is eviction in progress, and if so, wait for
834                  * it to finish */
835                 if (unlikely(atomic_read(&exp->exp_obd->
836                                                 obd_evict_inprogress))) {
837                         lwi = LWI_INTR(NULL, NULL);
838                         rc = l_wait_event(exp->exp_obd->
839                                                 obd_evict_inprogress_waitq,
840                                           !atomic_read(&exp->exp_obd->
841                                                         obd_evict_inprogress),
842                                           &lwi);
843                 }
844                 if (exp->exp_failed)
845                         rc = -ENOTCONN;
846                 else {
847                         sptlrpc_svc_wrap_bulk(req, desc);
848
849                         rc = ptlrpc_start_bulk_transfer(desc);
850                 }
851
852                 if (rc == 0) {
853                         lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
854                                                    ost_bulk_timeout, desc);
855                         rc = l_wait_event(desc->bd_waitq,
856                                           !ptlrpc_bulk_active(desc) ||
857                                           exp->exp_failed, &lwi);
858                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
859                         if (rc == -ETIMEDOUT) {
860                                 DEBUG_REQ(D_ERROR, req, "timeout on bulk PUT");
861                                 ptlrpc_abort_bulk(desc);
862                         } else if (exp->exp_failed) {
863                                 DEBUG_REQ(D_ERROR, req, "Eviction on bulk PUT");
864                                 rc = -ENOTCONN;
865                                 ptlrpc_abort_bulk(desc);
866                         } else if (!desc->bd_success ||
867                                    desc->bd_nob_transferred != desc->bd_nob) {
868                                 DEBUG_REQ(D_ERROR, req, "%s bulk PUT %d(%d)",
869                                           desc->bd_success ?
870                                           "truncated" : "network error on",
871                                           desc->bd_nob_transferred,
872                                           desc->bd_nob);
873                                 /* XXX should this be a different errno? */
874                                 rc = -ETIMEDOUT;
875                         }
876                 } else {
877                         DEBUG_REQ(D_ERROR, req, "bulk PUT failed: rc %d", rc);
878                 }
879                 no_reply = rc != 0;
880         }
881
882         /* Must commit after prep above in all cases */
883         rc = obd_commitrw(OBD_BRW_READ, exp, &body->oa, 1,
884                           ioo, npages, local_nb, oti, rc);
885
886         ost_nio_pages_put(req, local_nb, npages);
887
888         if (rc == 0) {
889                 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
890                                          sizeof(*repbody));
891                 memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
892         }
893
894 out_lock:
895         ost_brw_lock_put(LCK_PR, ioo, pp_rnb, &lockh);
896 out_bulk:
897         ptlrpc_free_bulk(desc);
898 out:
899         LASSERT(rc <= 0);
900         if (rc == 0) {
901                 req->rq_status = nob;
902                 target_committed_to_req(req);
903                 ptlrpc_reply(req);
904         } else if (!no_reply) {
905                 /* Only reply if there was no comms problem with bulk */
906                 target_committed_to_req(req);
907                 req->rq_status = rc;
908                 ptlrpc_error(req);
909         } else {
910                 if (req->rq_reply_state != NULL) {
911                         /* reply out callback would free */
912                         ptlrpc_rs_decref(req->rq_reply_state);
913                         req->rq_reply_state = NULL;
914                 }
915                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
916                       "client will retry\n",
917                       exp->exp_obd->obd_name,
918                       exp->exp_client_uuid.uuid,
919                       exp->exp_connection->c_remote_uuid.uuid,
920                       libcfs_id2str(req->rq_peer));
921         }
922
923         RETURN(rc);
924 }
925
926 static int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
927 {
928         struct ptlrpc_bulk_desc *desc;
929         struct obd_export       *exp = req->rq_export;
930         struct niobuf_remote    *remote_nb;
931         struct niobuf_remote    *pp_rnb;
932         struct niobuf_local     *local_nb;
933         struct obd_ioobj        *ioo;
934         struct ost_body         *body, *repbody;
935         struct l_wait_info       lwi;
936         struct lustre_handle     lockh = {0};
937         struct lustre_capa      *capa = NULL;
938         __u32                   *rcs;
939         int size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
940         int objcount, niocount, npages;
941         int rc, swab, i, j;
942         obd_count                client_cksum = 0, server_cksum = 0;
943         cksum_type_t             cksum_type = OBD_CKSUM_CRC32;
944         int                      no_reply = 0; 
945         ENTRY;
946
947         req->rq_bulk_write = 1;
948
949         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
950                 GOTO(out, rc = -EIO);
951         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK2))
952                 GOTO(out, rc = -EFAULT);
953
954         /* pause before transaction has been started */
955         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, (obd_timeout + 1) / 4);
956
957         /* Check if there is eviction in progress, and if so, wait for it to
958          * finish */
959         if (unlikely(atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
960                 lwi = LWI_INTR(NULL, NULL); // We do not care how long it takes
961                 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
962                         !atomic_read(&exp->exp_obd->obd_evict_inprogress),
963                         &lwi);
964         }
965         if (exp->exp_failed)
966                 GOTO(out, rc = -ENOTCONN);
967
968         swab = lustre_msg_swabbed(req->rq_reqmsg);
969         body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
970                                   lustre_swab_ost_body);
971         if (body == NULL) {
972                 CERROR("Missing/short ost_body\n");
973                 GOTO(out, rc = -EFAULT);
974         }
975
976         lustre_set_req_swabbed(req, REQ_REC_OFF + 1);
977         objcount = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1) /
978                    sizeof(*ioo);
979         if (objcount == 0) {
980                 CERROR("Missing/short ioobj\n");
981                 GOTO(out, rc = -EFAULT);
982         }
983         if (objcount > 1) {
984                 CERROR("too many ioobjs (%d)\n", objcount);
985                 GOTO(out, rc = -EFAULT);
986         }
987
988         ioo = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1,
989                              objcount * sizeof(*ioo));
990         LASSERT (ioo != NULL);
991         for (niocount = i = 0; i < objcount; i++) {
992                 if (swab)
993                         lustre_swab_obd_ioobj(&ioo[i]);
994                 if (ioo[i].ioo_bufcnt == 0) {
995                         CERROR("ioo[%d] has zero bufcnt\n", i);
996                         GOTO(out, rc = -EFAULT);
997                 }
998                 niocount += ioo[i].ioo_bufcnt;
999         }
1000
1001         if (niocount > PTLRPC_MAX_BRW_PAGES) {
1002                 DEBUG_REQ(D_ERROR, req, "bulk has too many pages (%d)",
1003                           niocount);
1004                 GOTO(out, rc = -EFAULT);
1005         }
1006
1007         remote_nb = lustre_swab_reqbuf(req, REQ_REC_OFF + 2,
1008                                        niocount * sizeof(*remote_nb),
1009                                        lustre_swab_niobuf_remote);
1010         if (remote_nb == NULL) {
1011                 CERROR("Missing/short niobuf\n");
1012                 GOTO(out, rc = -EFAULT);
1013         }
1014         if (swab) {                             /* swab the remaining niobufs */
1015                 for (i = 1; i < niocount; i++)
1016                         lustre_swab_niobuf_remote (&remote_nb[i]);
1017         }
1018
1019         if (body->oa.o_valid & OBD_MD_FLOSSCAPA)
1020                 capa = lustre_unpack_capa(req->rq_reqmsg, REQ_REC_OFF + 3);
1021
1022         size[REPLY_REC_OFF + 1] = niocount * sizeof(*rcs);
1023         rc = lustre_pack_reply(req, 3, size, NULL);
1024         if (rc != 0)
1025                 GOTO(out, rc);
1026         rcs = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1,
1027                              niocount * sizeof(*rcs));
1028
1029         /*
1030          * Per-thread array of struct niobuf_{local,remote}'s was allocated by
1031          * ost_thread_init().
1032          */
1033         local_nb = ost_tls(req)->local;
1034         pp_rnb   = ost_tls(req)->remote;
1035
1036         /* FIXME all niobuf splitting should be done in obdfilter if needed */
1037         /* CAVEAT EMPTOR this sets ioo->ioo_bufcnt to # pages */
1038         npages = get_per_page_niobufs(ioo, objcount,remote_nb,niocount,&pp_rnb);
1039         if (npages < 0)
1040                 GOTO(out, rc = npages);
1041
1042         LASSERT(npages <= OST_THREAD_POOL_SIZE);
1043
1044         ost_nio_pages_get(req, local_nb, npages);
1045
1046         desc = ptlrpc_prep_bulk_exp(req, npages,
1047                                      BULK_GET_SINK, OST_BULK_PORTAL);
1048         if (desc == NULL)
1049                 GOTO(out, rc = -ENOMEM);
1050
1051         rc = ost_brw_lock_get(LCK_PW, exp, ioo, pp_rnb, &lockh);
1052         if (rc != 0)
1053                 GOTO(out_bulk, rc);
1054
1055         /* 
1056          * If getting the lock took more time than
1057          * client was willing to wait, drop it. b=11330
1058          */
1059         if (cfs_time_current_sec() > req->rq_arrival_time.tv_sec + obd_timeout || 
1060             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
1061                 no_reply = 1;
1062                 CERROR("Dropping timed-out write from %s because locking"
1063                        "object "LPX64" took %ld seconds.\n",
1064                        libcfs_id2str(req->rq_peer), ioo->ioo_id,
1065                        cfs_time_current_sec() - req->rq_arrival_time.tv_sec);
1066                 GOTO(out_lock, rc = -ETIMEDOUT);
1067         }
1068
1069         ost_prolong_locks(exp, ioo, pp_rnb, LCK_PW);
1070
1071         /* obd_preprw clobbers oa->valid, so save what we need */
1072         if (body->oa.o_valid & OBD_MD_FLCKSUM) {
1073                 client_cksum = body->oa.o_cksum;
1074                 if (body->oa.o_valid & OBD_MD_FLFLAGS)
1075                         cksum_type = cksum_type_unpack(body->oa.o_flags);
1076         }
1077         
1078         /* Because we already sync grant info with client when reconnect,
1079          * grant info will be cleared for resent req, then fed_grant and 
1080          * total_grant will not be modified in following preprw_write */ 
1081         if (lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY)) {
1082                 DEBUG_REQ(D_CACHE, req, "clear resent/replay req grant info");
1083                 body->oa.o_valid &= ~OBD_MD_FLGRANT;
1084         }
1085
1086         rc = obd_preprw(OBD_BRW_WRITE, exp, &body->oa, objcount,
1087                         ioo, npages, pp_rnb, local_nb, oti, capa);
1088         if (rc != 0)
1089                 GOTO(out_lock, rc);
1090
1091         /* NB Having prepped, we must commit... */
1092
1093         for (i = 0; i < npages; i++)
1094                 ptlrpc_prep_bulk_page(desc, local_nb[i].page,
1095                                       pp_rnb[i].offset & ~CFS_PAGE_MASK,
1096                                       pp_rnb[i].len);
1097
1098         /* Check if client was evicted while we were doing i/o before touching
1099            network */
1100         if (desc->bd_export->exp_failed)
1101                 rc = -ENOTCONN;
1102         else
1103                 rc = ptlrpc_start_bulk_transfer (desc);
1104         if (rc == 0) {
1105                 lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 2, HZ,
1106                                            ost_bulk_timeout, desc);
1107                 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1108                                   desc->bd_export->exp_failed, &lwi);
1109                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1110                 if (rc == -ETIMEDOUT) {
1111                         DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1112                         ptlrpc_abort_bulk(desc);
1113                 } else if (desc->bd_export->exp_failed) {
1114                         DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1115                         rc = -ENOTCONN;
1116                         ptlrpc_abort_bulk(desc);
1117                 } else if (!desc->bd_success ||
1118                            desc->bd_nob_transferred != desc->bd_nob) {
1119                         DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1120                                   desc->bd_success ?
1121                                   "truncated" : "network error on",
1122                                   desc->bd_nob_transferred, desc->bd_nob);
1123                         /* XXX should this be a different errno? */
1124                         rc = -ETIMEDOUT;
1125                 }
1126         } else {
1127                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1128         }
1129         no_reply = rc != 0;
1130
1131         if (rc == 0)
1132                 sptlrpc_svc_unwrap_bulk(req, desc);
1133
1134         repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1135                                  sizeof(*repbody));
1136         memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
1137
1138         if (unlikely(client_cksum != 0 && rc == 0)) {
1139                 static int cksum_counter;
1140                 repbody->oa.o_valid |= OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
1141                 repbody->oa.o_flags &= ~OBD_FL_CKSUM_ALL;
1142                 repbody->oa.o_flags |= cksum_type_pack(cksum_type);
1143                 server_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
1144                 repbody->oa.o_cksum = server_cksum;
1145                 cksum_counter++;
1146                 if (unlikely(client_cksum != server_cksum)) {
1147                         CERROR("client csum %x, server csum %x\n",
1148                                client_cksum, server_cksum);
1149                         cksum_counter = 0;
1150                 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
1151                         CDEBUG(D_INFO, "Checksum %u from %s OK: %x\n",
1152                                cksum_counter, libcfs_id2str(req->rq_peer),
1153                                server_cksum);
1154                 }
1155         }
1156
1157         /* Must commit after prep above in all cases */
1158         rc = obd_commitrw(OBD_BRW_WRITE, exp, &repbody->oa,
1159                            objcount, ioo, npages, local_nb, oti, rc);
1160
1161         if (unlikely(client_cksum != server_cksum && rc == 0)) {
1162                 int  new_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
1163                 char *msg;
1164                 char *via;
1165                 char *router;
1166
1167                 if (new_cksum == server_cksum)
1168                         msg = "changed in transit before arrival at OST";
1169                 else if (new_cksum == client_cksum)
1170                         msg = "initial checksum before message complete";
1171                 else
1172                         msg = "changed in transit AND after initial checksum";
1173
1174                 if (req->rq_peer.nid == desc->bd_sender) {
1175                         via = router = "";
1176                 } else {
1177                         via = " via ";
1178                         router = libcfs_nid2str(desc->bd_sender);
1179                 }
1180                 
1181                 LCONSOLE_ERROR_MSG(0x168, "%s: BAD WRITE CHECKSUM: %s from "
1182                                    "%s%s%s inum "LPU64"/"LPU64" object "
1183                                    LPU64"/"LPU64" extent ["LPU64"-"LPU64"]\n",
1184                                    exp->exp_obd->obd_name, msg,
1185                                    libcfs_id2str(req->rq_peer),
1186                                    via, router,
1187                                    body->oa.o_valid & OBD_MD_FLFID ?
1188                                                 body->oa.o_fid : (__u64)0,
1189                                    body->oa.o_valid & OBD_MD_FLFID ?
1190                                                 body->oa.o_generation :(__u64)0,
1191                                    body->oa.o_id,
1192                                    body->oa.o_valid & OBD_MD_FLGROUP ?
1193                                                 body->oa.o_gr : (__u64)0,
1194                                    pp_rnb[0].offset,
1195                                    pp_rnb[npages-1].offset+pp_rnb[npages-1].len
1196                                    - 1 );
1197                 CERROR("client csum %x, original server csum %x, "
1198                        "server csum now %x\n",
1199                        client_cksum, server_cksum, new_cksum);
1200         }
1201
1202         ost_nio_pages_put(req, local_nb, npages);
1203
1204         if (rc == 0) {
1205                 /* set per-requested niobuf return codes */
1206                 for (i = j = 0; i < niocount; i++) {
1207                         int nob = remote_nb[i].len;
1208
1209                         rcs[i] = 0;
1210                         do {
1211                                 LASSERT(j < npages);
1212                                 if (local_nb[j].rc < 0)
1213                                         rcs[i] = local_nb[j].rc;
1214                                 nob -= pp_rnb[j].len;
1215                                 j++;
1216                         } while (nob > 0);
1217                         LASSERT(nob == 0);
1218                 }
1219                 LASSERT(j == npages);
1220         }
1221
1222 out_lock:
1223         ost_brw_lock_put(LCK_PW, ioo, pp_rnb, &lockh);
1224 out_bulk:
1225         ptlrpc_free_bulk(desc);
1226 out:
1227         if (rc == 0) {
1228                 oti_to_request(oti, req);
1229                 target_committed_to_req(req);
1230                 rc = ptlrpc_reply(req);
1231         } else if (!no_reply) {
1232                 /* Only reply if there was no comms problem with bulk */
1233                 target_committed_to_req(req);
1234                 req->rq_status = rc;
1235                 ptlrpc_error(req);
1236         } else {
1237                 if (req->rq_reply_state != NULL) {
1238                         /* reply out callback would free */
1239                         ptlrpc_rs_decref(req->rq_reply_state);
1240                         req->rq_reply_state = NULL;
1241                 }
1242                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
1243                       "client will retry\n",
1244                       exp->exp_obd->obd_name,
1245                       exp->exp_client_uuid.uuid,
1246                       exp->exp_connection->c_remote_uuid.uuid,
1247                       libcfs_id2str(req->rq_peer));
1248         }
1249         RETURN(rc);
1250 }
1251
1252 static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
1253 {
1254         char *key, *val = NULL;
1255         int keylen, vallen, rc = 0;
1256         ENTRY;
1257
1258         key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
1259         if (key == NULL) {
1260                 DEBUG_REQ(D_HA, req, "no set_info key");
1261                 RETURN(-EFAULT);
1262         }
1263         keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
1264
1265         rc = lustre_pack_reply(req, 1, NULL, NULL);
1266         if (rc)
1267                 RETURN(rc);
1268
1269         vallen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1);
1270         if (vallen)
1271                 val = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
1272
1273         if (KEY_IS("evict_by_nid")) {
1274                 if (val && vallen)
1275                         obd_export_evict_by_nid(exp->exp_obd, val);
1276
1277                 GOTO(out, rc = 0);
1278         }
1279
1280         rc = obd_set_info_async(exp, keylen, key, vallen, val, NULL);
1281 out:
1282         lustre_msg_set_status(req->rq_repmsg, 0);
1283         RETURN(rc);
1284 }
1285
1286 static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
1287 {
1288         void *key, *reply;
1289         int keylen, replylen, rc = 0;
1290         struct req_capsule *pill = &req->rq_pill;
1291         ENTRY;
1292
1293         req_capsule_set(&req->rq_pill, &RQF_OST_GET_INFO_GENERIC);
1294
1295         /* this common part for get_info rpc */
1296         key = req_capsule_client_get(pill, &RMF_SETINFO_KEY);
1297         if (key == NULL) {
1298                 DEBUG_REQ(D_HA, req, "no get_info key");
1299                 RETURN(-EFAULT);
1300         }
1301         keylen = req_capsule_get_size(pill, &RMF_SETINFO_KEY, RCL_CLIENT);
1302
1303         rc = obd_get_info(exp, keylen, key, &replylen, NULL);
1304         if (rc)
1305                 RETURN(rc);
1306
1307         req_capsule_set_size(pill, &RMF_GENERIC_DATA,
1308                              RCL_SERVER, replylen);
1309
1310         rc = req_capsule_server_pack(pill);
1311         if (rc)
1312                 RETURN(rc);
1313
1314         reply = req_capsule_server_get(pill, &RMF_GENERIC_DATA);
1315         if (reply == NULL)
1316                 RETURN(-ENOMEM);
1317
1318         /* call again to fill in the reply buffer */
1319         rc = obd_get_info(exp, keylen, key, &replylen, reply);
1320
1321         lustre_msg_set_status(req->rq_repmsg, 0);
1322         RETURN(rc);
1323 }
1324
1325 static int ost_handle_quotactl(struct ptlrpc_request *req)
1326 {
1327         struct obd_quotactl *oqctl, *repoqc;
1328         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repoqc) };
1329         ENTRY;
1330
1331         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1332                                    lustre_swab_obd_quotactl);
1333         if (oqctl == NULL)
1334                 GOTO(out, rc = -EPROTO);
1335
1336         rc = lustre_pack_reply(req, 2, size, NULL);
1337         if (rc)
1338                 GOTO(out, rc);
1339
1340         repoqc = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*repoqc));
1341
1342         req->rq_status = obd_quotactl(req->rq_export, oqctl);
1343         *repoqc = *oqctl;
1344 out:
1345         RETURN(rc);
1346 }
1347
1348 static int ost_handle_quotacheck(struct ptlrpc_request *req)
1349 {
1350         struct obd_quotactl *oqctl;
1351         int rc;
1352         ENTRY;
1353
1354         oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1355         if (oqctl == NULL)
1356                 RETURN(-EPROTO);
1357
1358         rc = req_capsule_server_pack(&req->rq_pill);
1359         if (rc) {
1360                 CERROR("ost: out of memory while packing quotacheck reply\n");
1361                 RETURN(-ENOMEM);
1362         }
1363
1364         req->rq_status = obd_quotacheck(req->rq_export, oqctl);
1365         RETURN(0);
1366 }
1367
1368 static int ost_llog_handle_connect(struct obd_export *exp,
1369                                    struct ptlrpc_request *req)
1370 {
1371         struct llogd_conn_body *body;
1372         int rc;
1373         ENTRY;
1374
1375         body = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*body));
1376         rc = obd_llog_connect(exp, body);
1377         RETURN(rc);
1378 }
1379
1380 static int filter_export_check_flavor(struct filter_obd *filter,
1381                                       struct obd_export *exp,
1382                                       struct ptlrpc_request *req)
1383 {
1384         int     rc = 0;
1385
1386         /* FIXME
1387          * this should be done in filter_connect()/filter_reconnect(), but
1388          * we can't obtain information like NID, which stored in incoming
1389          * request, thus can't decide what flavor to use. so we do it here.
1390          *
1391          * This hack should be removed after the OST stack be rewritten, just
1392          * like what we are doing in mdt_obd_connect()/mdt_obd_reconnect().
1393          */
1394         if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_INVALID)
1395                 return 0;
1396
1397         CDEBUG(D_SEC, "from %s\n", sptlrpc_part2name(req->rq_sp_from));
1398         spin_lock(&exp->exp_lock);
1399         exp->exp_sp_peer = req->rq_sp_from;
1400
1401         read_lock(&filter->fo_sptlrpc_lock);
1402         sptlrpc_rule_set_choose(&filter->fo_sptlrpc_rset, exp->exp_sp_peer,
1403                                 req->rq_peer.nid, &exp->exp_flvr);
1404         read_unlock(&filter->fo_sptlrpc_lock);
1405
1406         if (exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
1407                 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
1408                        req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
1409                        libcfs_nid2str(req->rq_peer.nid));
1410                 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
1411                 rc = -EACCES;
1412         }
1413
1414         spin_unlock(&exp->exp_lock);
1415
1416         return rc;
1417 }
1418
1419 static int ost_filter_recovery_request(struct ptlrpc_request *req,
1420                                        struct obd_device *obd, int *process)
1421 {
1422         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1423         case OST_CONNECT: /* This will never get here, but for completeness. */
1424         case OST_DISCONNECT:
1425                *process = 1;
1426                RETURN(0);
1427
1428         case OBD_PING:
1429         case OST_CREATE:
1430         case OST_DESTROY:
1431         case OST_PUNCH:
1432         case OST_SETATTR:
1433         case OST_SYNC:
1434         case OST_WRITE:
1435         case OBD_LOG_CANCEL:
1436         case LDLM_ENQUEUE:
1437                 *process = target_queue_recovery_request(req, obd);
1438                 RETURN(0);
1439
1440         default:
1441                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1442                 *process = -EAGAIN;
1443                 RETURN(0);
1444         }
1445 }
1446
1447 int ost_msg_check_version(struct lustre_msg *msg)
1448 {
1449         int rc;
1450
1451         switch(lustre_msg_get_opc(msg)) {
1452         case OST_CONNECT:
1453         case OST_DISCONNECT:
1454         case OBD_PING:
1455         case SEC_CTX_INIT:
1456         case SEC_CTX_INIT_CONT:
1457         case SEC_CTX_FINI:
1458                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1459                 if (rc)
1460                         CERROR("bad opc %u version %08x, expecting %08x\n",
1461                                lustre_msg_get_opc(msg),
1462                                lustre_msg_get_version(msg),
1463                                LUSTRE_OBD_VERSION);
1464                 break;
1465         case OST_CREATE:
1466         case OST_DESTROY:
1467         case OST_GETATTR:
1468         case OST_SETATTR:
1469         case OST_WRITE:
1470         case OST_READ:
1471         case OST_PUNCH:
1472         case OST_STATFS:
1473         case OST_SYNC:
1474         case OST_SET_INFO:
1475         case OST_GET_INFO:
1476         case OST_QUOTACHECK:
1477         case OST_QUOTACTL:
1478                 rc = lustre_msg_check_version(msg, LUSTRE_OST_VERSION);
1479                 if (rc)
1480                         CERROR("bad opc %u version %08x, expecting %08x\n",
1481                                lustre_msg_get_opc(msg),
1482                                lustre_msg_get_version(msg),
1483                                LUSTRE_OST_VERSION);
1484                 break;
1485         case LDLM_ENQUEUE:
1486         case LDLM_CONVERT:
1487         case LDLM_CANCEL:
1488         case LDLM_BL_CALLBACK:
1489         case LDLM_CP_CALLBACK:
1490                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1491                 if (rc)
1492                         CERROR("bad opc %u version %08x, expecting %08x\n",
1493                                lustre_msg_get_opc(msg),
1494                                lustre_msg_get_version(msg),
1495                                LUSTRE_DLM_VERSION);
1496                 break;
1497         case LLOG_ORIGIN_CONNECT:
1498         case OBD_LOG_CANCEL:
1499                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1500                 if (rc)
1501                         CERROR("bad opc %u version %08x, expecting %08x\n",
1502                                lustre_msg_get_opc(msg),
1503                                lustre_msg_get_version(msg),
1504                                LUSTRE_LOG_VERSION);
1505                 break;
1506         default:
1507                 CERROR("Unexpected opcode %d\n", lustre_msg_get_opc(msg));
1508                 rc = -ENOTSUPP;
1509         }
1510         return rc;
1511 }
1512
1513 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1514 int ost_handle(struct ptlrpc_request *req)
1515 {
1516         struct obd_trans_info trans_info = { 0, };
1517         struct obd_trans_info *oti = &trans_info;
1518         int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
1519         struct obd_device *obd = NULL;
1520         ENTRY;
1521
1522         LASSERT(current->journal_info == NULL);
1523
1524         /* primordial rpcs don't affect server recovery */
1525         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1526         case SEC_CTX_INIT:
1527         case SEC_CTX_INIT_CONT:
1528         case SEC_CTX_FINI:
1529                 GOTO(out, rc = 0);
1530         }
1531
1532         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1533
1534         /* XXX identical to MDS */
1535         if (lustre_msg_get_opc(req->rq_reqmsg) != OST_CONNECT) {
1536                 int recovering;
1537
1538                 if (req->rq_export == NULL) {
1539                         CDEBUG(D_HA,"operation %d on unconnected OST from %s\n",
1540                                lustre_msg_get_opc(req->rq_reqmsg),
1541                                libcfs_id2str(req->rq_peer));
1542                         req->rq_status = -ENOTCONN;
1543                         GOTO(out, rc = -ENOTCONN);
1544                 }
1545
1546                 obd = req->rq_export->exp_obd;
1547
1548                 /* Check for aborted recovery. */
1549                 spin_lock_bh(&obd->obd_processing_task_lock);
1550                 recovering = obd->obd_recovering;
1551                 spin_unlock_bh(&obd->obd_processing_task_lock);
1552                 if (recovering) {
1553                         rc = ost_filter_recovery_request(req, obd,
1554                                                          &should_process);
1555                         if (rc || !should_process)
1556                                 RETURN(rc);
1557                         else if (should_process < 0) {
1558                                 req->rq_status = should_process;
1559                                 rc = ptlrpc_error(req);
1560                                 RETURN(rc);
1561                         }
1562                 }
1563         }
1564
1565         oti_init(oti, req);
1566         
1567         rc = ost_msg_check_version(req->rq_reqmsg);
1568         if (rc)
1569                 RETURN(rc);
1570
1571         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1572         case OST_CONNECT: {
1573                 CDEBUG(D_INODE, "connect\n");
1574                 req_capsule_set(&req->rq_pill, &RQF_OST_CONNECT);
1575                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET))
1576                         RETURN(0);
1577                 rc = target_handle_connect(req);
1578                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET2))
1579                         RETURN(0);
1580                 if (!rc) {
1581                         struct obd_export *exp = req->rq_export;
1582
1583                         obd = exp->exp_obd;
1584
1585                         rc = filter_export_check_flavor(&obd->u.filter,
1586                                                         exp, req);
1587                 }
1588                 break;
1589         }
1590         case OST_DISCONNECT:
1591                 CDEBUG(D_INODE, "disconnect\n");
1592                 req_capsule_set(&req->rq_pill, &RQF_OST_DISCONNECT);
1593                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DISCONNECT_NET))
1594                         RETURN(0);
1595                 rc = target_handle_disconnect(req);
1596                 break;
1597         case OST_CREATE:
1598                 CDEBUG(D_INODE, "create\n");
1599                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CREATE_NET))
1600                         RETURN(0);
1601                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
1602                         GOTO(out, rc = -ENOSPC);
1603                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1604                         GOTO(out, rc = -EROFS);
1605                 rc = ost_create(req->rq_export, req, oti);
1606                 break;
1607         case OST_DESTROY:
1608                 CDEBUG(D_INODE, "destroy\n");
1609                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DESTROY_NET))
1610                         RETURN(0);
1611                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1612                         GOTO(out, rc = -EROFS);
1613                 rc = ost_destroy(req->rq_export, req, oti);
1614                 break;
1615         case OST_GETATTR:
1616                 CDEBUG(D_INODE, "getattr\n");
1617                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_GETATTR_NET))
1618                         RETURN(0);
1619                 rc = ost_getattr(req->rq_export, req);
1620                 break;
1621         case OST_SETATTR:
1622                 CDEBUG(D_INODE, "setattr\n");
1623                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SETATTR_NET))
1624                         RETURN(0);
1625                 rc = ost_setattr(req->rq_export, req, oti);
1626                 break;
1627         case OST_WRITE:
1628                 CDEBUG(D_INODE, "write\n");
1629                 /* req->rq_request_portal would be nice, if it was set */
1630                 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
1631                         CERROR("%s: deny write request from %s to portal %u\n",
1632                                req->rq_export->exp_obd->obd_name,
1633                                obd_export_nid2str(req->rq_export),
1634                                req->rq_rqbd->rqbd_service->srv_req_portal);
1635                         GOTO(out, rc = -EPROTO);
1636                 }
1637                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
1638                         RETURN(0);
1639                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
1640                         GOTO(out, rc = -ENOSPC);
1641                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1642                         GOTO(out, rc = -EROFS);
1643                 rc = ost_brw_write(req, oti);
1644                 LASSERT(current->journal_info == NULL);
1645                 /* ost_brw_write sends its own replies */
1646                 RETURN(rc);
1647         case OST_READ:
1648                 CDEBUG(D_INODE, "read\n");
1649                 /* req->rq_request_portal would be nice, if it was set */
1650                 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
1651                         CERROR("%s: deny read request from %s to portal %u\n",
1652                                req->rq_export->exp_obd->obd_name,
1653                                obd_export_nid2str(req->rq_export),
1654                                req->rq_rqbd->rqbd_service->srv_req_portal);
1655                         GOTO(out, rc = -EPROTO);
1656                 }
1657                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
1658                         RETURN(0);
1659                 rc = ost_brw_read(req, oti);
1660                 LASSERT(current->journal_info == NULL);
1661                 /* ost_brw_read sends its own replies */
1662                 RETURN(rc);
1663         case OST_PUNCH:
1664                 CDEBUG(D_INODE, "punch\n");
1665                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_PUNCH_NET))
1666                         RETURN(0);
1667                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1668                         GOTO(out, rc = -EROFS);
1669                 rc = ost_punch(req->rq_export, req, oti);
1670                 break;
1671         case OST_STATFS:
1672                 CDEBUG(D_INODE, "statfs\n");
1673                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_NET))
1674                         RETURN(0);
1675                 rc = ost_statfs(req);
1676                 break;
1677         case OST_SYNC:
1678                 CDEBUG(D_INODE, "sync\n");
1679                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SYNC_NET))
1680                         RETURN(0);
1681                 rc = ost_sync(req->rq_export, req);
1682                 break;
1683         case OST_SET_INFO:
1684                 DEBUG_REQ(D_INODE, req, "set_info");
1685                 req_capsule_set(&req->rq_pill, &RQF_OST_SET_INFO);
1686                 rc = ost_set_info(req->rq_export, req);
1687                 break;
1688         case OST_GET_INFO:
1689                 DEBUG_REQ(D_INODE, req, "get_info");
1690                 rc = ost_get_info(req->rq_export, req);
1691                 break;
1692         case OST_QUOTACHECK:
1693                 CDEBUG(D_INODE, "quotacheck\n");
1694                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACHECK);
1695                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACHECK_NET))
1696                         RETURN(0);
1697                 rc = ost_handle_quotacheck(req);
1698                 break;
1699         case OST_QUOTACTL:
1700                 CDEBUG(D_INODE, "quotactl\n");
1701                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACTL);
1702                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACTL_NET))
1703                         RETURN(0);
1704                 rc = ost_handle_quotactl(req);
1705                 break;
1706         case OBD_PING:
1707                 DEBUG_REQ(D_INODE, req, "ping");
1708                 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
1709                 rc = target_handle_ping(req);
1710                 break;
1711         /* FIXME - just reply status */
1712         case LLOG_ORIGIN_CONNECT:
1713                 DEBUG_REQ(D_INODE, req, "log connect");
1714                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_CONNECT);
1715                 rc = ost_llog_handle_connect(req->rq_export, req);
1716                 req->rq_status = rc;
1717                 rc = req_capsule_server_pack(&req->rq_pill);
1718                 if (rc)
1719                         RETURN(rc);
1720                 RETURN(ptlrpc_reply(req));
1721         case OBD_LOG_CANCEL:
1722                 CDEBUG(D_INODE, "log cancel\n");
1723                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1724                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1725                         RETURN(0);
1726                 rc = llog_origin_handle_cancel(req);
1727                 req->rq_status = rc;
1728                 rc = req_capsule_server_pack(&req->rq_pill);
1729                 if (rc)
1730                         RETURN(rc);
1731                 RETURN(ptlrpc_reply(req));
1732         case LDLM_ENQUEUE:
1733                 CDEBUG(D_INODE, "enqueue\n");
1734                 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
1735                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE))
1736                         RETURN(0);
1737                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1738                                          ldlm_server_blocking_ast,
1739                                          ldlm_server_glimpse_ast);
1740                 fail = OBD_FAIL_OST_LDLM_REPLY_NET;
1741                 break;
1742         case LDLM_CONVERT:
1743                 CDEBUG(D_INODE, "convert\n");
1744                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CONVERT);
1745                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CONVERT))
1746                         RETURN(0);
1747                 rc = ldlm_handle_convert(req);
1748                 break;
1749         case LDLM_CANCEL:
1750                 CDEBUG(D_INODE, "cancel\n");
1751                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
1752                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1753                         RETURN(0);
1754                 rc = ldlm_handle_cancel(req);
1755                 break;
1756         case LDLM_BL_CALLBACK:
1757         case LDLM_CP_CALLBACK:
1758                 CDEBUG(D_INODE, "callback\n");
1759                 CERROR("callbacks should not happen on OST\n");
1760                 /* fall through */
1761         default:
1762                 CERROR("Unexpected opcode %d\n",
1763                        lustre_msg_get_opc(req->rq_reqmsg));
1764                 req->rq_status = -ENOTSUPP;
1765                 rc = ptlrpc_error(req);
1766                 RETURN(rc);
1767         }
1768
1769         LASSERT(current->journal_info == NULL);
1770
1771         EXIT;
1772         /* If we're DISCONNECTing, the export_data is already freed */
1773         if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != OST_DISCONNECT)
1774                 target_committed_to_req(req);
1775
1776 out:
1777         if (!rc)
1778                 oti_to_request(oti, req);
1779
1780         target_send_reply(req, rc, fail);
1781         return 0;
1782 }
1783 EXPORT_SYMBOL(ost_handle);
1784 /*
1785  * free per-thread pool created by ost_thread_init().
1786  */
1787 static void ost_thread_done(struct ptlrpc_thread *thread)
1788 {
1789         int i;
1790         struct ost_thread_local_cache *tls; /* TLS stands for Thread-Local
1791                                              * Storage */
1792
1793         ENTRY;
1794
1795         LASSERT(thread != NULL);
1796
1797         /*
1798          * be prepared to handle partially-initialized pools (because this is
1799          * called from ost_thread_init() for cleanup.
1800          */
1801         tls = thread->t_data;
1802         if (tls != NULL) {
1803                 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1804                         if (tls->page[i] != NULL)
1805                                 OBD_PAGE_FREE(tls->page[i]);
1806                 }
1807                 OBD_FREE_PTR(tls);
1808                 thread->t_data = NULL;
1809         }
1810         EXIT;
1811 }
1812
1813 /*
1814  * initialize per-thread page pool (bug 5137).
1815  */
1816 static int ost_thread_init(struct ptlrpc_thread *thread)
1817 {
1818         int result;
1819         int i;
1820         struct ost_thread_local_cache *tls;
1821
1822         ENTRY;
1823
1824         LASSERT(thread != NULL);
1825         LASSERT(thread->t_data == NULL);
1826         LASSERTF(thread->t_id <= OSS_THREADS_MAX, "%u\n", thread->t_id);
1827
1828         OBD_ALLOC_PTR(tls);
1829         if (tls != NULL) {
1830                 result = 0;
1831                 thread->t_data = tls;
1832                 /*
1833                  * populate pool
1834                  */
1835                 for (i = 0; i < OST_THREAD_POOL_SIZE; ++ i) {
1836                         OBD_PAGE_ALLOC(tls->page[i], OST_THREAD_POOL_GFP);
1837                         if (tls->page[i] == NULL) {
1838                                 ost_thread_done(thread);
1839                                 result = -ENOMEM;
1840                                 break;
1841                         }
1842                 }
1843         } else
1844                 result = -ENOMEM;
1845         RETURN(result);
1846 }
1847
1848 #define OST_WATCHDOG_TIMEOUT (obd_timeout * 1000)
1849
1850 /* Sigh - really, this is an OSS, the _server_, not the _target_ */
1851 static int ost_setup(struct obd_device *obd, struct lustre_cfg* lcfg)
1852 {
1853         struct ost_obd *ost = &obd->u.ost;
1854         struct lprocfs_static_vars lvars;
1855         int oss_min_threads;
1856         int oss_max_threads;
1857         int oss_min_create_threads;
1858         int oss_max_create_threads;
1859         int rc;
1860         ENTRY;
1861
1862         rc = cleanup_group_info();
1863         if (rc)
1864                 RETURN(rc);
1865
1866         lprocfs_ost_init_vars(&lvars);
1867         lprocfs_obd_setup(obd, lvars.obd_vars);
1868
1869         sema_init(&ost->ost_health_sem, 1);
1870
1871         if (oss_num_threads) {
1872                 /* If oss_num_threads is set, it is the min and the max. */
1873                 if (oss_num_threads > OSS_THREADS_MAX) 
1874                         oss_num_threads = OSS_THREADS_MAX;
1875                 if (oss_num_threads < OSS_THREADS_MIN)
1876                         oss_num_threads = OSS_THREADS_MIN;
1877                 oss_max_threads = oss_min_threads = oss_num_threads;
1878         } else {
1879                 /* Base min threads on memory and cpus */
1880                 oss_min_threads = num_possible_cpus() * num_physpages >> 
1881                         (27 - CFS_PAGE_SHIFT);
1882                 if (oss_min_threads < OSS_THREADS_MIN)
1883                         oss_min_threads = OSS_THREADS_MIN;
1884                 /* Insure a 4x range for dynamic threads */
1885                 if (oss_min_threads > OSS_THREADS_MAX / 4) 
1886                         oss_min_threads = OSS_THREADS_MAX / 4;
1887                 oss_max_threads = min(OSS_THREADS_MAX, oss_min_threads * 4);
1888         }
1889
1890         ost->ost_service =
1891                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1892                                 OST_MAXREPSIZE, OST_REQUEST_PORTAL,
1893                                 OSC_REPLY_PORTAL,
1894                                 OST_WATCHDOG_TIMEOUT, ost_handle,
1895                                 LUSTRE_OSS_NAME, obd->obd_proc_entry,
1896                                 ost_print_req, oss_min_threads,
1897                                 oss_max_threads, "ll_ost",
1898                                 LCT_DT_THREAD);
1899         if (ost->ost_service == NULL) {
1900                 CERROR("failed to start service\n");
1901                 GOTO(out_lprocfs, rc = -ENOMEM);
1902         }
1903
1904         rc = ptlrpc_start_threads(obd, ost->ost_service);
1905         if (rc)
1906                 GOTO(out_service, rc = -EINVAL);
1907
1908         if (oss_num_create_threads) {
1909                 if (oss_num_create_threads > OSS_MAX_CREATE_THREADS)
1910                         oss_num_create_threads = OSS_MAX_CREATE_THREADS;
1911                 if (oss_num_create_threads < OSS_DEF_CREATE_THREADS)
1912                         oss_num_create_threads = OSS_DEF_CREATE_THREADS;
1913                 oss_min_create_threads = oss_max_create_threads =
1914                         oss_num_create_threads;
1915         } else {
1916                 oss_min_create_threads = OSS_DEF_CREATE_THREADS;
1917                 oss_max_create_threads = OSS_MAX_CREATE_THREADS;
1918         }
1919
1920         ost->ost_create_service =
1921                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1922                                 OST_MAXREPSIZE, OST_CREATE_PORTAL,
1923                                 OSC_REPLY_PORTAL,
1924                                 OST_WATCHDOG_TIMEOUT, ost_handle, "ost_create",
1925                                 obd->obd_proc_entry, ost_print_req,
1926                                 oss_min_create_threads,
1927                                 oss_max_create_threads,
1928                                 "ll_ost_creat", LCT_DT_THREAD);
1929         if (ost->ost_create_service == NULL) {
1930                 CERROR("failed to start OST create service\n");
1931                 GOTO(out_service, rc = -ENOMEM);
1932         }
1933
1934         rc = ptlrpc_start_threads(obd, ost->ost_create_service);
1935         if (rc)
1936                 GOTO(out_create, rc = -EINVAL);
1937
1938         ost->ost_io_service =
1939                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
1940                                 OST_MAXREPSIZE, OST_IO_PORTAL,
1941                                 OSC_REPLY_PORTAL,
1942                                 OST_WATCHDOG_TIMEOUT, ost_handle, "ost_io",
1943                                 obd->obd_proc_entry, ost_print_req,
1944                                 oss_min_threads, oss_max_threads,
1945                                 "ll_ost_io", LCT_DT_THREAD);
1946         if (ost->ost_io_service == NULL) {
1947                 CERROR("failed to start OST I/O service\n");
1948                 GOTO(out_create, rc = -ENOMEM);
1949         }
1950
1951         ost->ost_io_service->srv_init = ost_thread_init;
1952         ost->ost_io_service->srv_done = ost_thread_done;
1953         ost->ost_io_service->srv_cpu_affinity = 1;
1954         rc = ptlrpc_start_threads(obd, ost->ost_io_service);
1955         if (rc)
1956                 GOTO(out_io, rc = -EINVAL);
1957
1958         ping_evictor_start();
1959
1960         RETURN(0);
1961
1962 out_io:
1963         ptlrpc_unregister_service(ost->ost_io_service);
1964         ost->ost_io_service = NULL;
1965 out_create:
1966         ptlrpc_unregister_service(ost->ost_create_service);
1967         ost->ost_create_service = NULL;
1968 out_service:
1969         ptlrpc_unregister_service(ost->ost_service);
1970         ost->ost_service = NULL;
1971 out_lprocfs:
1972         lprocfs_obd_cleanup(obd);
1973         RETURN(rc);
1974 }
1975
1976 static int ost_cleanup(struct obd_device *obd)
1977 {
1978         struct ost_obd *ost = &obd->u.ost;
1979         int err = 0;
1980         ENTRY;
1981
1982         ping_evictor_stop();
1983
1984         spin_lock_bh(&obd->obd_processing_task_lock);
1985         if (obd->obd_recovering) {
1986                 target_cancel_recovery_timer(obd);
1987                 obd->obd_recovering = 0;
1988         }
1989         spin_unlock_bh(&obd->obd_processing_task_lock);
1990
1991         down(&ost->ost_health_sem);
1992         ptlrpc_unregister_service(ost->ost_service);
1993         ptlrpc_unregister_service(ost->ost_create_service);
1994         ptlrpc_unregister_service(ost->ost_io_service);
1995         ost->ost_service = NULL;
1996         ost->ost_create_service = NULL;
1997         up(&ost->ost_health_sem);
1998
1999         lprocfs_obd_cleanup(obd);
2000
2001         RETURN(err);
2002 }
2003
2004 static int ost_health_check(struct obd_device *obd)
2005 {
2006         struct ost_obd *ost = &obd->u.ost;
2007         int rc = 0;
2008
2009         down(&ost->ost_health_sem);
2010         rc |= ptlrpc_service_health_check(ost->ost_service);
2011         rc |= ptlrpc_service_health_check(ost->ost_create_service);
2012         rc |= ptlrpc_service_health_check(ost->ost_io_service);
2013         up(&ost->ost_health_sem);
2014
2015         /*
2016          * health_check to return 0 on healthy
2017          * and 1 on unhealthy.
2018          */
2019         if( rc != 0)
2020                 rc = 1;
2021
2022         return rc;
2023 }
2024
2025 struct ost_thread_local_cache *ost_tls(struct ptlrpc_request *r)
2026 {
2027         return (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
2028 }
2029
2030 /* use obd ops to offer management infrastructure */
2031 static struct obd_ops ost_obd_ops = {
2032         .o_owner        = THIS_MODULE,
2033         .o_setup        = ost_setup,
2034         .o_cleanup      = ost_cleanup,
2035         .o_health_check = ost_health_check,
2036 };
2037
2038
2039 static int __init ost_init(void)
2040 {
2041         struct lprocfs_static_vars lvars;
2042         int rc;
2043         ENTRY;
2044
2045         lprocfs_ost_init_vars(&lvars);
2046         rc = class_register_type(&ost_obd_ops, NULL, lvars.module_vars,
2047                                  LUSTRE_OSS_NAME, NULL);
2048
2049         if (ost_num_threads != 0 && oss_num_threads == 0) {
2050                 LCONSOLE_INFO("ost_num_threads module parameter is deprecated, "
2051                               "use oss_num_threads instead or unset both for "
2052                               "dynamic thread startup\n");
2053                 oss_num_threads = ost_num_threads;
2054         }
2055
2056         RETURN(rc);
2057 }
2058
2059 static void /*__exit*/ ost_exit(void)
2060 {
2061         class_unregister_type(LUSTRE_OSS_NAME);
2062 }
2063
2064 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2065 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
2066 MODULE_LICENSE("GPL");
2067
2068 module_init(ost_init);
2069 module_exit(ost_exit);