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