Whamcloud - gitweb
LU-8935 ptlrpc: missing barrier before wake_up
[fs/lustre-release.git] / lustre / target / tgt_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2013, 2016, Intel Corporation.
25  */
26 /*
27  * lustre/target/tgt_handler.c
28  *
29  * Lustre Unified Target request handler code
30  *
31  * Author: Brian Behlendorf <behlendorf1@llnl.gov>
32  * Author: Mikhail Pershin <mike.pershin@intel.com>
33  */
34
35 #define DEBUG_SUBSYSTEM S_CLASS
36
37 #include <linux/user_namespace.h>
38 #ifdef HAVE_UIDGID_HEADER
39 # include <linux/uidgid.h>
40 #endif
41
42 #include <obd.h>
43 #include <obd_class.h>
44 #include <obd_cksum.h>
45 #include <lustre_lfsck.h>
46 #include <lustre_nodemap.h>
47 #include <lustre_acl.h>
48
49 #include "tgt_internal.h"
50
51 char *tgt_name(struct lu_target *tgt)
52 {
53         LASSERT(tgt->lut_obd != NULL);
54         return tgt->lut_obd->obd_name;
55 }
56 EXPORT_SYMBOL(tgt_name);
57
58 /*
59  * Generic code handling requests that have struct mdt_body passed in:
60  *
61  *  - extract mdt_body from request and save it in @tsi, if present;
62  *
63  *  - create lu_object, corresponding to the fid in mdt_body, and save it in
64  *  @tsi;
65  *
66  *  - if HABEO_CORPUS flag is set for this request type check whether object
67  *  actually exists on storage (lu_object_exists()).
68  *
69  */
70 static int tgt_mdt_body_unpack(struct tgt_session_info *tsi, __u32 flags)
71 {
72         const struct mdt_body   *body;
73         struct lu_object        *obj;
74         struct req_capsule      *pill = tsi->tsi_pill;
75         int                      rc;
76
77         ENTRY;
78
79         body = req_capsule_client_get(pill, &RMF_MDT_BODY);
80         if (body == NULL)
81                 RETURN(-EFAULT);
82
83         tsi->tsi_mdt_body = body;
84
85         if (!(body->mbo_valid & OBD_MD_FLID))
86                 RETURN(0);
87
88         /* mdc_pack_body() doesn't check if fid is zero and set OBD_ML_FID
89          * in any case in pre-2.5 clients. Fix that here if needed */
90         if (unlikely(fid_is_zero(&body->mbo_fid1)))
91                 RETURN(0);
92
93         if (!fid_is_sane(&body->mbo_fid1)) {
94                 CERROR("%s: invalid FID: "DFID"\n", tgt_name(tsi->tsi_tgt),
95                        PFID(&body->mbo_fid1));
96                 RETURN(-EINVAL);
97         }
98
99         obj = lu_object_find(tsi->tsi_env,
100                              &tsi->tsi_tgt->lut_bottom->dd_lu_dev,
101                              &body->mbo_fid1, NULL);
102         if (!IS_ERR(obj)) {
103                 if ((flags & HABEO_CORPUS) && !lu_object_exists(obj)) {
104                         lu_object_put(tsi->tsi_env, obj);
105                         rc = -ENOENT;
106                 } else {
107                         tsi->tsi_corpus = obj;
108                         rc = 0;
109                 }
110         } else {
111                 rc = PTR_ERR(obj);
112         }
113
114         tsi->tsi_fid = body->mbo_fid1;
115
116         RETURN(rc);
117 }
118
119 /**
120  * Validate oa from client.
121  * If the request comes from 2.0 clients, currently only RSVD seq and IDIF
122  * req are valid.
123  *    a. objects in Single MDT FS  seq = FID_SEQ_OST_MDT0, oi_id != 0
124  *    b. Echo objects(seq = 2), old echo client still use oi_id/oi_seq to
125  *       pack ost_id. Because non-zero oi_seq will make it diffcult to tell
126  *       whether this is oi_fid or real ostid. So it will check
127  *       OBD_CONNECT_FID, then convert the ostid to FID for old client.
128  *    c. Old FID-disable osc will send IDIF.
129  *    d. new FID-enable osc/osp will send normal FID.
130  *
131  * And also oi_id/f_oid should always start from 1. oi_id/f_oid = 0 will
132  * be used for LAST_ID file, and only being accessed inside OST now.
133  */
134 int tgt_validate_obdo(struct tgt_session_info *tsi, struct obdo *oa)
135 {
136         struct ost_id   *oi     = &oa->o_oi;
137         u64              seq    = ostid_seq(oi);
138         u64              id     = ostid_id(oi);
139         int              rc;
140         ENTRY;
141
142         if (unlikely(!(exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_FID) &&
143                      fid_seq_is_echo(seq))) {
144                 /* Sigh 2.[123] client still sends echo req with oi_id = 0
145                  * during create, and we will reset this to 1, since this
146                  * oi_id is basically useless in the following create process,
147                  * but oi_id == 0 will make it difficult to tell whether it is
148                  * real FID or ost_id. */
149                 oi->oi_fid.f_seq = FID_SEQ_ECHO;
150                 oi->oi_fid.f_oid = id ?: 1;
151                 oi->oi_fid.f_ver = 0;
152         } else {
153                 struct tgt_thread_info *tti = tgt_th_info(tsi->tsi_env);
154
155                 if (unlikely((oa->o_valid & OBD_MD_FLID) && id == 0))
156                         GOTO(out, rc = -EPROTO);
157
158                 /* Note: this check might be forced in 2.5 or 2.6, i.e.
159                  * all of the requests are required to setup FLGROUP */
160                 if (unlikely(!(oa->o_valid & OBD_MD_FLGROUP))) {
161                         ostid_set_seq_mdt0(oi);
162                         oa->o_valid |= OBD_MD_FLGROUP;
163                         seq = ostid_seq(oi);
164                 }
165
166                 if (unlikely(!(fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq) ||
167                                fid_seq_is_norm(seq) || fid_seq_is_echo(seq))))
168                         GOTO(out, rc = -EPROTO);
169
170                 rc = ostid_to_fid(&tti->tti_fid1, oi,
171                                   tsi->tsi_tgt->lut_lsd.lsd_osd_index);
172                 if (unlikely(rc != 0))
173                         GOTO(out, rc);
174
175                 oi->oi_fid = tti->tti_fid1;
176         }
177
178         RETURN(0);
179
180 out:
181         CERROR("%s: client %s sent bad object "DOSTID": rc = %d\n",
182                tgt_name(tsi->tsi_tgt), obd_export_nid2str(tsi->tsi_exp),
183                seq, id, rc);
184         return rc;
185 }
186 EXPORT_SYMBOL(tgt_validate_obdo);
187
188 static int tgt_io_data_unpack(struct tgt_session_info *tsi, struct ost_id *oi)
189 {
190         unsigned                 max_brw;
191         struct niobuf_remote    *rnb;
192         struct obd_ioobj        *ioo;
193         int                      obj_count;
194
195         ENTRY;
196
197         ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
198         if (ioo == NULL)
199                 RETURN(-EPROTO);
200
201         rnb = req_capsule_client_get(tsi->tsi_pill, &RMF_NIOBUF_REMOTE);
202         if (rnb == NULL)
203                 RETURN(-EPROTO);
204
205         max_brw = ioobj_max_brw_get(ioo);
206         if (unlikely((max_brw & (max_brw - 1)) != 0)) {
207                 CERROR("%s: client %s sent bad ioobj max %u for "DOSTID
208                        ": rc = %d\n", tgt_name(tsi->tsi_tgt),
209                        obd_export_nid2str(tsi->tsi_exp), max_brw,
210                        POSTID(oi), -EPROTO);
211                 RETURN(-EPROTO);
212         }
213         ioo->ioo_oid = *oi;
214
215         obj_count = req_capsule_get_size(tsi->tsi_pill, &RMF_OBD_IOOBJ,
216                                         RCL_CLIENT) / sizeof(*ioo);
217         if (obj_count == 0) {
218                 CERROR("%s: short ioobj\n", tgt_name(tsi->tsi_tgt));
219                 RETURN(-EPROTO);
220         } else if (obj_count > 1) {
221                 CERROR("%s: too many ioobjs (%d)\n", tgt_name(tsi->tsi_tgt),
222                        obj_count);
223                 RETURN(-EPROTO);
224         }
225
226         if (ioo->ioo_bufcnt == 0) {
227                 CERROR("%s: ioo has zero bufcnt\n", tgt_name(tsi->tsi_tgt));
228                 RETURN(-EPROTO);
229         }
230
231         if (ioo->ioo_bufcnt > PTLRPC_MAX_BRW_PAGES) {
232                 DEBUG_REQ(D_RPCTRACE, tgt_ses_req(tsi),
233                           "bulk has too many pages (%d)",
234                           ioo->ioo_bufcnt);
235                 RETURN(-EPROTO);
236         }
237
238         RETURN(0);
239 }
240
241 static int tgt_ost_body_unpack(struct tgt_session_info *tsi, __u32 flags)
242 {
243         struct ost_body         *body;
244         struct req_capsule      *pill = tsi->tsi_pill;
245         struct lu_nodemap       *nodemap;
246         int                      rc;
247
248         ENTRY;
249
250         body = req_capsule_client_get(pill, &RMF_OST_BODY);
251         if (body == NULL)
252                 RETURN(-EFAULT);
253
254         rc = tgt_validate_obdo(tsi, &body->oa);
255         if (rc)
256                 RETURN(rc);
257
258         nodemap = nodemap_get_from_exp(tsi->tsi_exp);
259         if (IS_ERR(nodemap))
260                 RETURN(PTR_ERR(nodemap));
261
262         body->oa.o_uid = nodemap_map_id(nodemap, NODEMAP_UID,
263                                         NODEMAP_CLIENT_TO_FS,
264                                         body->oa.o_uid);
265         body->oa.o_gid = nodemap_map_id(nodemap, NODEMAP_GID,
266                                         NODEMAP_CLIENT_TO_FS,
267                                         body->oa.o_gid);
268         nodemap_putref(nodemap);
269
270         tsi->tsi_ost_body = body;
271         tsi->tsi_fid = body->oa.o_oi.oi_fid;
272
273         if (req_capsule_has_field(pill, &RMF_OBD_IOOBJ, RCL_CLIENT)) {
274                 rc = tgt_io_data_unpack(tsi, &body->oa.o_oi);
275                 if (rc < 0)
276                         RETURN(rc);
277         }
278
279         if (!(body->oa.o_valid & OBD_MD_FLID)) {
280                 if (flags & HABEO_CORPUS) {
281                         CERROR("%s: OBD_MD_FLID flag is not set in ost_body "
282                                "but OID/FID is mandatory with HABEO_CORPUS\n",
283                                tgt_name(tsi->tsi_tgt));
284                         RETURN(-EPROTO);
285                 } else {
286                         RETURN(0);
287                 }
288         }
289
290         ost_fid_build_resid(&tsi->tsi_fid, &tsi->tsi_resid);
291
292         /*
293          * OST doesn't get object in advance for further use to prevent
294          * situations with nested object_find which is potential deadlock.
295          */
296         tsi->tsi_corpus = NULL;
297         RETURN(rc);
298 }
299
300 /*
301  * Do necessary preprocessing according to handler ->th_flags.
302  */
303 static int tgt_request_preprocess(struct tgt_session_info *tsi,
304                                   struct tgt_handler *h,
305                                   struct ptlrpc_request *req)
306 {
307         struct req_capsule      *pill = tsi->tsi_pill;
308         __u32                    flags = h->th_flags;
309         int                      rc = 0;
310
311         ENTRY;
312
313         if (tsi->tsi_preprocessed)
314                 RETURN(0);
315
316         LASSERT(h->th_act != NULL);
317         LASSERT(h->th_opc == lustre_msg_get_opc(req->rq_reqmsg));
318         LASSERT(current->journal_info == NULL);
319
320         LASSERT(ergo(flags & (HABEO_CORPUS | HABEO_REFERO),
321                      h->th_fmt != NULL));
322         if (h->th_fmt != NULL) {
323                 req_capsule_set(pill, h->th_fmt);
324                 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT)) {
325                         rc = tgt_mdt_body_unpack(tsi, flags);
326                         if (rc < 0)
327                                 RETURN(rc);
328                 } else if (req_capsule_has_field(pill, &RMF_OST_BODY,
329                                                  RCL_CLIENT)) {
330                         rc = tgt_ost_body_unpack(tsi, flags);
331                         if (rc < 0)
332                                 RETURN(rc);
333                 }
334         }
335
336         if (flags & MUTABOR && tgt_conn_flags(tsi) & OBD_CONNECT_RDONLY)
337                 RETURN(-EROFS);
338
339         if (flags & HABEO_CLAVIS) {
340                 struct ldlm_request *dlm_req;
341
342                 LASSERT(h->th_fmt != NULL);
343
344                 dlm_req = req_capsule_client_get(pill, &RMF_DLM_REQ);
345                 if (dlm_req != NULL) {
346                         union ldlm_wire_policy_data *policy =
347                                         &dlm_req->lock_desc.l_policy_data;
348
349                         if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
350                                      LDLM_IBITS &&
351                                      (policy->l_inodebits.bits |
352                                       policy->l_inodebits.try_bits) == 0)) {
353                                 /*
354                                  * Lock without inodebits makes no sense and
355                                  * will oops later in ldlm. If client miss to
356                                  * set such bits, do not trigger ASSERTION.
357                                  *
358                                  * For liblustre flock case, it maybe zero.
359                                  */
360                                 rc = -EPROTO;
361                         } else {
362                                 tsi->tsi_dlm_req = dlm_req;
363                         }
364                 } else {
365                         rc = -EFAULT;
366                 }
367         }
368         tsi->tsi_preprocessed = 1;
369         RETURN(rc);
370 }
371
372 /*
373  * Invoke handler for this request opc. Also do necessary preprocessing
374  * (according to handler ->th_flags), and post-processing (setting of
375  * ->last_{xid,committed}).
376  */
377 static int tgt_handle_request0(struct tgt_session_info *tsi,
378                                struct tgt_handler *h,
379                                struct ptlrpc_request *req)
380 {
381         int      serious = 0;
382         int      rc;
383         __u32    opc = lustre_msg_get_opc(req->rq_reqmsg);
384
385         ENTRY;
386
387
388         /* When dealing with sec context requests, no export is associated yet,
389          * because these requests are sent before *_CONNECT requests.
390          * A NULL req->rq_export means the normal *_common_slice handlers will
391          * not be called, because there is no reference to the target.
392          * So deal with them by hand and jump directly to target_send_reply().
393          */
394         switch (opc) {
395         case SEC_CTX_INIT:
396         case SEC_CTX_INIT_CONT:
397         case SEC_CTX_FINI:
398                 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
399                 GOTO(out, rc = 0);
400         }
401
402         /*
403          * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
404          * to put same checks into handlers like mdt_close(), mdt_reint(),
405          * etc., without talking to mdt authors first. Checking same thing
406          * there again is useless and returning 0 error without packing reply
407          * is buggy! Handlers either pack reply or return error.
408          *
409          * We return 0 here and do not send any reply in order to emulate
410          * network failure. Do not send any reply in case any of NET related
411          * fail_id has occured.
412          */
413         if (OBD_FAIL_CHECK_ORSET(h->th_fail_id, OBD_FAIL_ONCE))
414                 RETURN(0);
415         if (unlikely(lustre_msg_get_opc(req->rq_reqmsg) == MDS_REINT &&
416                      OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_MULTI_NET)))
417                 RETURN(0);
418
419         rc = tgt_request_preprocess(tsi, h, req);
420         /* pack reply if reply format is fixed */
421         if (rc == 0 && h->th_flags & HABEO_REFERO) {
422                 /* Pack reply */
423                 if (req_capsule_has_field(tsi->tsi_pill, &RMF_MDT_MD,
424                                           RCL_SERVER))
425                         req_capsule_set_size(tsi->tsi_pill, &RMF_MDT_MD,
426                                              RCL_SERVER,
427                                              tsi->tsi_mdt_body->mbo_eadatasize);
428                 if (req_capsule_has_field(tsi->tsi_pill, &RMF_LOGCOOKIES,
429                                           RCL_SERVER))
430                         req_capsule_set_size(tsi->tsi_pill, &RMF_LOGCOOKIES,
431                                              RCL_SERVER, 0);
432                 if (req_capsule_has_field(tsi->tsi_pill, &RMF_ACL, RCL_SERVER))
433                         req_capsule_set_size(tsi->tsi_pill,
434                                              &RMF_ACL, RCL_SERVER,
435                                              LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
436
437                 rc = req_capsule_server_pack(tsi->tsi_pill);
438         }
439
440         if (likely(rc == 0)) {
441                 /*
442                  * Process request, there can be two types of rc:
443                  * 1) errors with msg unpack/pack, other failures outside the
444                  * operation itself. This is counted as serious errors;
445                  * 2) errors during fs operation, should be placed in rq_status
446                  * only
447                  */
448                 rc = h->th_act(tsi);
449                 if (!is_serious(rc) &&
450                     !req->rq_no_reply && req->rq_reply_state == NULL) {
451                         DEBUG_REQ(D_ERROR, req, "%s \"handler\" %s did not "
452                                   "pack reply and returned 0 error\n",
453                                   tgt_name(tsi->tsi_tgt), h->th_name);
454                         LBUG();
455                 }
456                 serious = is_serious(rc);
457                 rc = clear_serious(rc);
458         } else {
459                 serious = 1;
460         }
461
462         req->rq_status = rc;
463
464         /*
465          * ELDLM_* codes which > 0 should be in rq_status only as well as
466          * all non-serious errors.
467          */
468         if (rc > 0 || !serious)
469                 rc = 0;
470
471         LASSERT(current->journal_info == NULL);
472
473         if (likely(rc == 0 && req->rq_export))
474                 target_committed_to_req(req);
475
476 out:
477         target_send_reply(req, rc, tsi->tsi_reply_fail_id);
478         RETURN(0);
479 }
480
481 static int tgt_filter_recovery_request(struct ptlrpc_request *req,
482                                        struct obd_device *obd, int *process)
483 {
484         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
485         case MDS_DISCONNECT:
486         case OST_DISCONNECT:
487         case OBD_IDX_READ:
488                 *process = 1;
489                 RETURN(0);
490         case MDS_CLOSE:
491         case MDS_SYNC: /* used in unmounting */
492         case OBD_PING:
493         case MDS_REINT:
494         case OUT_UPDATE:
495         case SEQ_QUERY:
496         case FLD_QUERY:
497         case FLD_READ:
498         case LDLM_ENQUEUE:
499         case OST_CREATE:
500         case OST_DESTROY:
501         case OST_PUNCH:
502         case OST_SETATTR:
503         case OST_SYNC:
504         case OST_WRITE:
505         case MDS_HSM_PROGRESS:
506         case MDS_HSM_STATE_SET:
507         case MDS_HSM_REQUEST:
508                 *process = target_queue_recovery_request(req, obd);
509                 RETURN(0);
510
511         default:
512                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
513                 *process = -EAGAIN;
514                 RETURN(0);
515         }
516 }
517
518 /*
519  * Handle recovery. Return:
520  *        +1: continue request processing;
521  *       -ve: abort immediately with the given error code;
522  *         0: send reply with error code in req->rq_status;
523  */
524 static int tgt_handle_recovery(struct ptlrpc_request *req, int reply_fail_id)
525 {
526         ENTRY;
527
528         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
529         case MDS_CONNECT:
530         case OST_CONNECT:
531         case MGS_CONNECT:
532         case SEC_CTX_INIT:
533         case SEC_CTX_INIT_CONT:
534         case SEC_CTX_FINI:
535                 RETURN(+1);
536         }
537
538         if (!req->rq_export->exp_obd->obd_replayable)
539                 RETURN(+1);
540
541         /* sanity check: if the xid matches, the request must be marked as a
542          * resent or replayed */
543         if (req_can_reconstruct(req, NULL)) {
544                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
545                       (MSG_RESENT | MSG_REPLAY))) {
546                         DEBUG_REQ(D_WARNING, req, "rq_xid %llu matches "
547                                   "saved xid, expected REPLAY or RESENT flag "
548                                   "(%x)", req->rq_xid,
549                                   lustre_msg_get_flags(req->rq_reqmsg));
550                         req->rq_status = -ENOTCONN;
551                         RETURN(-ENOTCONN);
552                 }
553         }
554         /* else: note the opposite is not always true; a RESENT req after a
555          * failover will usually not match the last_xid, since it was likely
556          * never committed. A REPLAYed request will almost never match the
557          * last xid, however it could for a committed, but still retained,
558          * open. */
559
560         /* Check for aborted recovery... */
561         if (unlikely(req->rq_export->exp_obd->obd_recovering)) {
562                 int rc;
563                 int should_process;
564
565                 DEBUG_REQ(D_INFO, req, "Got new replay");
566                 rc = tgt_filter_recovery_request(req, req->rq_export->exp_obd,
567                                                  &should_process);
568                 if (rc != 0 || !should_process)
569                         RETURN(rc);
570                 else if (should_process < 0) {
571                         req->rq_status = should_process;
572                         rc = ptlrpc_error(req);
573                         RETURN(rc);
574                 }
575         }
576         RETURN(+1);
577 }
578
579 /* Initial check for request, it is validation mostly */
580 static struct tgt_handler *tgt_handler_find_check(struct ptlrpc_request *req)
581 {
582         struct tgt_handler      *h;
583         struct tgt_opc_slice    *s;
584         struct lu_target        *tgt;
585         __u32                    opc = lustre_msg_get_opc(req->rq_reqmsg);
586
587         ENTRY;
588
589         tgt = class_exp2tgt(req->rq_export);
590         if (unlikely(tgt == NULL)) {
591                 DEBUG_REQ(D_ERROR, req, "%s: No target for connected export\n",
592                           class_exp2obd(req->rq_export)->obd_name);
593                 RETURN(ERR_PTR(-EINVAL));
594         }
595
596         for (s = tgt->lut_slice; s->tos_hs != NULL; s++)
597                 if (s->tos_opc_start <= opc && opc < s->tos_opc_end)
598                         break;
599
600         /* opcode was not found in slice */
601         if (unlikely(s->tos_hs == NULL)) {
602                 CERROR("%s: no handlers for opcode 0x%x\n", tgt_name(tgt),
603                        opc);
604                 RETURN(ERR_PTR(-ENOTSUPP));
605         }
606
607         LASSERT(opc >= s->tos_opc_start && opc < s->tos_opc_end);
608         h = s->tos_hs + (opc - s->tos_opc_start);
609         if (unlikely(h->th_opc == 0)) {
610                 CERROR("%s: unsupported opcode 0x%x\n", tgt_name(tgt), opc);
611                 RETURN(ERR_PTR(-ENOTSUPP));
612         }
613
614         RETURN(h);
615 }
616
617 static int process_req_last_xid(struct ptlrpc_request *req)
618 {
619         __u64   last_xid;
620         ENTRY;
621
622         /* check request's xid is consistent with export's last_xid */
623         last_xid = lustre_msg_get_last_xid(req->rq_reqmsg);
624         if (last_xid > req->rq_export->exp_last_xid)
625                 req->rq_export->exp_last_xid = last_xid;
626
627         if (req->rq_xid == 0 ||
628             (req->rq_xid <= req->rq_export->exp_last_xid)) {
629                 DEBUG_REQ(D_ERROR, req, "Unexpected xid %llx vs. "
630                           "last_xid %llx\n", req->rq_xid,
631                           req->rq_export->exp_last_xid);
632                 /* Some request is allowed to be sent during replay,
633                  * such as OUT update requests, FLD requests, so it
634                  * is possible that replay requests has smaller XID
635                  * than the exp_last_xid.
636                  *
637                  * Some non-replay requests may have smaller XID as
638                  * well:
639                  *
640                  * - Client send a no_resend RPC, like statfs;
641                  * - The RPC timedout (or some other error) on client,
642                  *   then it's removed from the unreplied list;
643                  * - Client send some other request to bump the
644                  *   exp_last_xid on server;
645                  * - The former RPC got chance to be processed;
646                  */
647                 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))
648                         RETURN(-EPROTO);
649         }
650
651         /* try to release in-memory reply data */
652         if (tgt_is_multimodrpcs_client(req->rq_export)) {
653                 tgt_handle_received_xid(req->rq_export,
654                                 lustre_msg_get_last_xid(req->rq_reqmsg));
655                 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
656                       (MSG_RESENT | MSG_REPLAY)))
657                         tgt_handle_tag(req->rq_export,
658                                        lustre_msg_get_tag(req->rq_reqmsg));
659         }
660         RETURN(0);
661 }
662
663 int tgt_request_handle(struct ptlrpc_request *req)
664 {
665         struct tgt_session_info *tsi = tgt_ses_info(req->rq_svc_thread->t_env);
666
667         struct lustre_msg       *msg = req->rq_reqmsg;
668         struct tgt_handler      *h;
669         struct lu_target        *tgt;
670         int                      request_fail_id = 0;
671         __u32                    opc = lustre_msg_get_opc(msg);
672         struct obd_device       *obd;
673         int                      rc;
674         bool                     is_connect = false;
675         ENTRY;
676
677         /* Refill the context, to make sure all thread keys are allocated */
678         lu_env_refill(req->rq_svc_thread->t_env);
679
680         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
681         tsi->tsi_pill = &req->rq_pill;
682         tsi->tsi_env = req->rq_svc_thread->t_env;
683
684         /* if request has export then get handlers slice from corresponding
685          * target, otherwise that should be connect operation */
686         if (opc == MDS_CONNECT || opc == OST_CONNECT ||
687             opc == MGS_CONNECT) {
688                 is_connect = true;
689                 req_capsule_set(&req->rq_pill, &RQF_CONNECT);
690                 rc = target_handle_connect(req);
691                 if (rc != 0) {
692                         rc = ptlrpc_error(req);
693                         GOTO(out, rc);
694                 }
695                 /* recovery-small test 18c asks to drop connect reply */
696                 if (unlikely(opc == OST_CONNECT &&
697                              OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET2)))
698                         GOTO(out, rc = 0);
699         }
700
701         if (unlikely(!class_connected_export(req->rq_export))) {
702                 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT ||
703                     opc == SEC_CTX_FINI) {
704                         /* sec context initialization has to be handled
705                          * by hand in tgt_handle_request0() */
706                         tsi->tsi_reply_fail_id = OBD_FAIL_SEC_CTX_INIT_NET;
707                         h = NULL;
708                         GOTO(handle_recov, rc = 0);
709                 }
710                 CDEBUG(D_HA, "operation %d on unconnected OST from %s\n",
711                        opc, libcfs_id2str(req->rq_peer));
712                 req->rq_status = -ENOTCONN;
713                 rc = ptlrpc_error(req);
714                 GOTO(out, rc);
715         }
716
717         tsi->tsi_tgt = tgt = class_exp2tgt(req->rq_export);
718         tsi->tsi_exp = req->rq_export;
719         if (exp_connect_flags(req->rq_export) & OBD_CONNECT_JOBSTATS)
720                 tsi->tsi_jobid = lustre_msg_get_jobid(req->rq_reqmsg);
721         else
722                 tsi->tsi_jobid = NULL;
723
724         if (tgt == NULL) {
725                 DEBUG_REQ(D_ERROR, req, "%s: No target for connected export\n",
726                           class_exp2obd(req->rq_export)->obd_name);
727                 req->rq_status = -EINVAL;
728                 rc = ptlrpc_error(req);
729                 GOTO(out, rc);
730         }
731
732         /* Skip last_xid processing for the recovery thread, otherwise, the
733          * last_xid on same request could be processed twice: first time when
734          * processing the incoming request, second time when the request is
735          * being processed by recovery thread. */
736         obd = class_exp2obd(req->rq_export);
737         if (is_connect) {
738                 /* reset the exp_last_xid on each connection. */
739                 req->rq_export->exp_last_xid = 0;
740         } else if (obd->obd_recovery_data.trd_processing_task !=
741                    current_pid()) {
742                 rc = process_req_last_xid(req);
743                 if (rc) {
744                         req->rq_status = rc;
745                         rc = ptlrpc_error(req);
746                         GOTO(out, rc);
747                 }
748         }
749
750         request_fail_id = tgt->lut_request_fail_id;
751         tsi->tsi_reply_fail_id = tgt->lut_reply_fail_id;
752
753         h = tgt_handler_find_check(req);
754         if (IS_ERR(h)) {
755                 req->rq_status = PTR_ERR(h);
756                 rc = ptlrpc_error(req);
757                 GOTO(out, rc);
758         }
759
760         LASSERTF(h->th_opc == opc, "opcode mismatch %d != %d\n",
761                  h->th_opc, opc);
762
763         if (CFS_FAIL_CHECK_ORSET(request_fail_id, CFS_FAIL_ONCE))
764                 GOTO(out, rc = 0);
765
766         rc = lustre_msg_check_version(msg, h->th_version);
767         if (unlikely(rc)) {
768                 DEBUG_REQ(D_ERROR, req, "%s: drop mal-formed request, version"
769                           " %08x, expecting %08x\n", tgt_name(tgt),
770                           lustre_msg_get_version(msg), h->th_version);
771                 req->rq_status = -EINVAL;
772                 rc = ptlrpc_error(req);
773                 GOTO(out, rc);
774         }
775
776 handle_recov:
777         rc = tgt_handle_recovery(req, tsi->tsi_reply_fail_id);
778         if (likely(rc == 1)) {
779                 rc = tgt_handle_request0(tsi, h, req);
780                 if (rc)
781                         GOTO(out, rc);
782         }
783         EXIT;
784 out:
785         req_capsule_fini(tsi->tsi_pill);
786         if (tsi->tsi_corpus != NULL) {
787                 lu_object_put(tsi->tsi_env, tsi->tsi_corpus);
788                 tsi->tsi_corpus = NULL;
789         }
790         return rc;
791 }
792 EXPORT_SYMBOL(tgt_request_handle);
793
794 /** Assign high priority operations to the request if needed. */
795 int tgt_hpreq_handler(struct ptlrpc_request *req)
796 {
797         struct tgt_session_info *tsi = tgt_ses_info(req->rq_svc_thread->t_env);
798         struct tgt_handler      *h;
799         int                      rc;
800
801         ENTRY;
802
803         if (req->rq_export == NULL)
804                 RETURN(0);
805
806         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
807         tsi->tsi_pill = &req->rq_pill;
808         tsi->tsi_env = req->rq_svc_thread->t_env;
809         tsi->tsi_tgt = class_exp2tgt(req->rq_export);
810         tsi->tsi_exp = req->rq_export;
811
812         h = tgt_handler_find_check(req);
813         if (IS_ERR(h)) {
814                 rc = PTR_ERR(h);
815                 RETURN(rc);
816         }
817
818         rc = tgt_request_preprocess(tsi, h, req);
819         if (unlikely(rc != 0))
820                 RETURN(rc);
821
822         if (h->th_hp != NULL)
823                 h->th_hp(tsi);
824         RETURN(0);
825 }
826 EXPORT_SYMBOL(tgt_hpreq_handler);
827
828 void tgt_counter_incr(struct obd_export *exp, int opcode)
829 {
830         lprocfs_counter_incr(exp->exp_obd->obd_stats, opcode);
831         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
832                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
833 }
834 EXPORT_SYMBOL(tgt_counter_incr);
835
836 /*
837  * Unified target generic handlers.
838  */
839
840 int tgt_connect_check_sptlrpc(struct ptlrpc_request *req, struct obd_export *exp)
841 {
842         struct lu_target        *tgt = class_exp2tgt(exp);
843         struct sptlrpc_flavor    flvr;
844         int                      rc = 0;
845
846         LASSERT(tgt);
847         LASSERT(tgt->lut_obd);
848         LASSERT(tgt->lut_slice);
849
850         /* always allow ECHO client */
851         if (unlikely(strcmp(exp->exp_obd->obd_type->typ_name,
852                             LUSTRE_ECHO_NAME) == 0)) {
853                 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_ANY;
854                 return 0;
855         }
856
857         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
858                 read_lock(&tgt->lut_sptlrpc_lock);
859                 sptlrpc_target_choose_flavor(&tgt->lut_sptlrpc_rset,
860                                              req->rq_sp_from,
861                                              req->rq_peer.nid,
862                                              &flvr);
863                 read_unlock(&tgt->lut_sptlrpc_lock);
864
865                 spin_lock(&exp->exp_lock);
866                 exp->exp_sp_peer = req->rq_sp_from;
867                 exp->exp_flvr = flvr;
868
869                 /* when on mgs, if no restriction is set, or if client
870                  * is loopback, allow any flavor */
871                 if ((strcmp(exp->exp_obd->obd_type->typ_name,
872                            LUSTRE_MGS_NAME) == 0) &&
873                      (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_NULL ||
874                       LNET_NETTYP(LNET_NIDNET(exp->exp_connection->c_peer.nid))
875                       == LOLND))
876                         exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_ANY;
877
878                 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
879                     exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
880                         CERROR("%s: unauthorized rpc flavor %x from %s, "
881                                "expect %x\n", tgt_name(tgt),
882                                req->rq_flvr.sf_rpc,
883                                libcfs_nid2str(req->rq_peer.nid),
884                                exp->exp_flvr.sf_rpc);
885                         rc = -EACCES;
886                 }
887                 spin_unlock(&exp->exp_lock);
888         } else {
889                 if (exp->exp_sp_peer != req->rq_sp_from) {
890                         CERROR("%s: RPC source %s doesn't match %s\n",
891                                tgt_name(tgt),
892                                sptlrpc_part2name(req->rq_sp_from),
893                                sptlrpc_part2name(exp->exp_sp_peer));
894                         rc = -EACCES;
895                 } else {
896                         rc = sptlrpc_target_export_check(exp, req);
897                 }
898         }
899
900         return rc;
901 }
902
903 int tgt_adapt_sptlrpc_conf(struct lu_target *tgt)
904 {
905         struct sptlrpc_rule_set  tmp_rset;
906         int                      rc;
907
908         if (unlikely(tgt == NULL)) {
909                 CERROR("No target passed");
910                 return -EINVAL;
911         }
912
913         sptlrpc_rule_set_init(&tmp_rset);
914         rc = sptlrpc_conf_target_get_rules(tgt->lut_obd, &tmp_rset);
915         if (rc) {
916                 CERROR("%s: failed get sptlrpc rules: rc = %d\n",
917                        tgt_name(tgt), rc);
918                 return rc;
919         }
920
921         sptlrpc_target_update_exp_flavor(tgt->lut_obd, &tmp_rset);
922
923         write_lock(&tgt->lut_sptlrpc_lock);
924         sptlrpc_rule_set_free(&tgt->lut_sptlrpc_rset);
925         tgt->lut_sptlrpc_rset = tmp_rset;
926         write_unlock(&tgt->lut_sptlrpc_lock);
927
928         return 0;
929 }
930 EXPORT_SYMBOL(tgt_adapt_sptlrpc_conf);
931
932 int tgt_connect(struct tgt_session_info *tsi)
933 {
934         struct ptlrpc_request   *req = tgt_ses_req(tsi);
935         struct obd_connect_data *reply;
936         int                      rc;
937
938         ENTRY;
939
940         /* XXX: better to call this check right after getting new export but
941          * before last_rcvd slot allocation to avoid server load upon insecure
942          * connects. This is to be fixed after unifiyng all targets.
943          */
944         rc = tgt_connect_check_sptlrpc(req, tsi->tsi_exp);
945         if (rc)
946                 GOTO(out, rc);
947
948         /* To avoid exposing partially initialized connection flags, changes up
949          * to this point have been staged in reply->ocd_connect_flags. Now that
950          * connection handling has completed successfully, atomically update
951          * the connect flags in the shared export data structure. LU-1623 */
952         reply = req_capsule_server_get(tsi->tsi_pill, &RMF_CONNECT_DATA);
953         spin_lock(&tsi->tsi_exp->exp_lock);
954         *exp_connect_flags_ptr(tsi->tsi_exp) = reply->ocd_connect_flags;
955         tsi->tsi_exp->exp_connect_data.ocd_brw_size = reply->ocd_brw_size;
956         spin_unlock(&tsi->tsi_exp->exp_lock);
957
958         RETURN(0);
959 out:
960         obd_disconnect(class_export_get(tsi->tsi_exp));
961         return rc;
962 }
963 EXPORT_SYMBOL(tgt_connect);
964
965 int tgt_disconnect(struct tgt_session_info *tsi)
966 {
967         int rc;
968
969         ENTRY;
970
971         rc = target_handle_disconnect(tgt_ses_req(tsi));
972         if (rc)
973                 RETURN(err_serious(rc));
974
975         RETURN(rc);
976 }
977 EXPORT_SYMBOL(tgt_disconnect);
978
979 /*
980  * Unified target OBD handlers
981  */
982 int tgt_obd_ping(struct tgt_session_info *tsi)
983 {
984         int rc;
985
986         ENTRY;
987
988         rc = target_handle_ping(tgt_ses_req(tsi));
989         if (rc)
990                 RETURN(err_serious(rc));
991
992         RETURN(rc);
993 }
994 EXPORT_SYMBOL(tgt_obd_ping);
995
996 int tgt_obd_log_cancel(struct tgt_session_info *tsi)
997 {
998         return err_serious(-EOPNOTSUPP);
999 }
1000
1001 int tgt_send_buffer(struct tgt_session_info *tsi, struct lu_rdbuf *rdbuf)
1002 {
1003         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
1004         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1005         struct obd_export       *exp = req->rq_export;
1006         struct ptlrpc_bulk_desc *desc;
1007         struct l_wait_info      *lwi = &tti->tti_u.update.tti_wait_info;
1008         int                      i;
1009         int                      rc;
1010
1011         ENTRY;
1012
1013         desc = ptlrpc_prep_bulk_exp(req, rdbuf->rb_nbufs, 1,
1014                                   PTLRPC_BULK_PUT_SOURCE | PTLRPC_BULK_BUF_KVEC,
1015                                     MDS_BULK_PORTAL, &ptlrpc_bulk_kvec_ops);
1016         if (desc == NULL)
1017                 RETURN(-ENOMEM);
1018
1019         for (i = 0; i < rdbuf->rb_nbufs; i++)
1020                 desc->bd_frag_ops->add_iov_frag(desc,
1021                                         rdbuf->rb_bufs[i].lb_buf,
1022                                         rdbuf->rb_bufs[i].lb_len);
1023
1024         rc = target_bulk_io(exp, desc, lwi);
1025         ptlrpc_free_bulk(desc);
1026         RETURN(rc);
1027 }
1028 EXPORT_SYMBOL(tgt_send_buffer);
1029
1030 int tgt_sendpage(struct tgt_session_info *tsi, struct lu_rdpg *rdpg, int nob)
1031 {
1032         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
1033         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1034         struct obd_export       *exp = req->rq_export;
1035         struct ptlrpc_bulk_desc *desc;
1036         struct l_wait_info      *lwi = &tti->tti_u.rdpg.tti_wait_info;
1037         int                      tmpcount;
1038         int                      tmpsize;
1039         int                      i;
1040         int                      rc;
1041
1042         ENTRY;
1043
1044         desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, 1,
1045                                     PTLRPC_BULK_PUT_SOURCE |
1046                                         PTLRPC_BULK_BUF_KIOV,
1047                                     MDS_BULK_PORTAL,
1048                                     &ptlrpc_bulk_kiov_pin_ops);
1049         if (desc == NULL)
1050                 RETURN(-ENOMEM);
1051
1052         if (!(exp_connect_flags(exp) & OBD_CONNECT_BRW_SIZE))
1053                 /* old client requires reply size in it's PAGE_SIZE,
1054                  * which is rdpg->rp_count */
1055                 nob = rdpg->rp_count;
1056
1057         for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1058              i++, tmpcount -= tmpsize) {
1059                 tmpsize = min_t(int, tmpcount, PAGE_SIZE);
1060                 desc->bd_frag_ops->add_kiov_frag(desc, rdpg->rp_pages[i], 0,
1061                                                  tmpsize);
1062         }
1063
1064         LASSERT(desc->bd_nob == nob);
1065         rc = target_bulk_io(exp, desc, lwi);
1066         ptlrpc_free_bulk(desc);
1067         RETURN(rc);
1068 }
1069 EXPORT_SYMBOL(tgt_sendpage);
1070
1071 /*
1072  * OBD_IDX_READ handler
1073  */
1074 static int tgt_obd_idx_read(struct tgt_session_info *tsi)
1075 {
1076         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
1077         struct lu_rdpg          *rdpg = &tti->tti_u.rdpg.tti_rdpg;
1078         struct idx_info         *req_ii, *rep_ii;
1079         int                      rc, i;
1080
1081         ENTRY;
1082
1083         memset(rdpg, 0, sizeof(*rdpg));
1084         req_capsule_set(tsi->tsi_pill, &RQF_OBD_IDX_READ);
1085
1086         /* extract idx_info buffer from request & reply */
1087         req_ii = req_capsule_client_get(tsi->tsi_pill, &RMF_IDX_INFO);
1088         if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
1089                 RETURN(err_serious(-EPROTO));
1090
1091         rc = req_capsule_server_pack(tsi->tsi_pill);
1092         if (rc)
1093                 RETURN(err_serious(rc));
1094
1095         rep_ii = req_capsule_server_get(tsi->tsi_pill, &RMF_IDX_INFO);
1096         if (rep_ii == NULL)
1097                 RETURN(err_serious(-EFAULT));
1098         rep_ii->ii_magic = IDX_INFO_MAGIC;
1099
1100         /* extract hash to start with */
1101         rdpg->rp_hash = req_ii->ii_hash_start;
1102
1103         /* extract requested attributes */
1104         rdpg->rp_attrs = req_ii->ii_attrs;
1105
1106         /* check that fid packed in request is valid and supported */
1107         if (!fid_is_sane(&req_ii->ii_fid))
1108                 RETURN(-EINVAL);
1109         rep_ii->ii_fid = req_ii->ii_fid;
1110
1111         /* copy flags */
1112         rep_ii->ii_flags = req_ii->ii_flags;
1113
1114         /* compute number of pages to allocate, ii_count is the number of 4KB
1115          * containers */
1116         if (req_ii->ii_count <= 0)
1117                 GOTO(out, rc = -EFAULT);
1118         rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
1119                                exp_max_brw_size(tsi->tsi_exp));
1120         rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1121
1122         /* allocate pages to store the containers */
1123         OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
1124         if (rdpg->rp_pages == NULL)
1125                 GOTO(out, rc = -ENOMEM);
1126         for (i = 0; i < rdpg->rp_npages; i++) {
1127                 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1128                 if (rdpg->rp_pages[i] == NULL)
1129                         GOTO(out, rc = -ENOMEM);
1130         }
1131
1132         /* populate pages with key/record pairs */
1133         rc = dt_index_read(tsi->tsi_env, tsi->tsi_tgt->lut_bottom, rep_ii, rdpg);
1134         if (rc < 0)
1135                 GOTO(out, rc);
1136
1137         LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
1138                  "asked %d > %d\n", rc, rdpg->rp_count);
1139
1140         /* send pages to client */
1141         rc = tgt_sendpage(tsi, rdpg, rc);
1142         if (rc)
1143                 GOTO(out, rc);
1144         EXIT;
1145 out:
1146         if (rdpg->rp_pages) {
1147                 for (i = 0; i < rdpg->rp_npages; i++)
1148                         if (rdpg->rp_pages[i])
1149                                 __free_page(rdpg->rp_pages[i]);
1150                 OBD_FREE(rdpg->rp_pages,
1151                          rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
1152         }
1153         return rc;
1154 }
1155
1156 struct tgt_handler tgt_obd_handlers[] = {
1157 TGT_OBD_HDL    (0,      OBD_PING,               tgt_obd_ping),
1158 TGT_OBD_HDL_VAR(0,      OBD_LOG_CANCEL,         tgt_obd_log_cancel),
1159 TGT_OBD_HDL    (0,      OBD_IDX_READ,           tgt_obd_idx_read)
1160 };
1161 EXPORT_SYMBOL(tgt_obd_handlers);
1162
1163 int tgt_sync(const struct lu_env *env, struct lu_target *tgt,
1164              struct dt_object *obj, __u64 start, __u64 end)
1165 {
1166         int rc = 0;
1167
1168         ENTRY;
1169
1170         /* if no objid is specified, it means "sync whole filesystem" */
1171         if (obj == NULL) {
1172                 rc = dt_sync(env, tgt->lut_bottom);
1173         } else if (dt_version_get(env, obj) >
1174                    tgt->lut_obd->obd_last_committed) {
1175                 rc = dt_object_sync(env, obj, start, end);
1176         }
1177         atomic_inc(&tgt->lut_sync_count);
1178
1179         RETURN(rc);
1180 }
1181 EXPORT_SYMBOL(tgt_sync);
1182 /*
1183  * Unified target DLM handlers.
1184  */
1185
1186 /**
1187  * Unified target BAST
1188  *
1189  * Ensure data and metadata are synced to disk when lock is canceled if Sync on
1190  * Cancel (SOC) is enabled. If it's extent lock, normally sync obj is enough,
1191  * but if it's cross-MDT lock, because remote object version is not set, a
1192  * filesystem sync is needed.
1193  *
1194  * \param lock server side lock
1195  * \param desc lock desc
1196  * \param data ldlm_cb_set_arg
1197  * \param flag  indicates whether this cancelling or blocking callback
1198  * \retval      0 on success
1199  * \retval      negative number on error
1200  */
1201 static int tgt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
1202                             void *data, int flag)
1203 {
1204         struct lu_env            env;
1205         struct lu_target        *tgt;
1206         struct dt_object        *obj = NULL;
1207         struct lu_fid            fid;
1208         int                      rc = 0;
1209
1210         ENTRY;
1211
1212         tgt = class_exp2tgt(lock->l_export);
1213
1214         if (unlikely(tgt == NULL)) {
1215                 CDEBUG(D_ERROR, "%s: No target for connected export\n",
1216                        class_exp2obd(lock->l_export)->obd_name);
1217                 RETURN(-EINVAL);
1218         }
1219
1220         if (flag == LDLM_CB_CANCELING &&
1221             (lock->l_granted_mode & (LCK_EX | LCK_PW | LCK_GROUP)) &&
1222             (tgt->lut_sync_lock_cancel == ALWAYS_SYNC_ON_CANCEL ||
1223              (tgt->lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL &&
1224               ldlm_is_cbpending(lock))) &&
1225             ((exp_connect_flags(lock->l_export) & OBD_CONNECT_MDS_MDS) ||
1226              lock->l_resource->lr_type == LDLM_EXTENT)) {
1227                 __u64 start = 0;
1228                 __u64 end = OBD_OBJECT_EOF;
1229
1230                 rc = lu_env_init(&env, LCT_DT_THREAD);
1231                 if (unlikely(rc != 0))
1232                         RETURN(rc);
1233
1234                 ost_fid_from_resid(&fid, &lock->l_resource->lr_name,
1235                                    tgt->lut_lsd.lsd_osd_index);
1236
1237                 if (lock->l_resource->lr_type == LDLM_EXTENT) {
1238                         obj = dt_locate(&env, tgt->lut_bottom, &fid);
1239                         if (IS_ERR(obj))
1240                                 GOTO(err_env, rc = PTR_ERR(obj));
1241
1242                         if (!dt_object_exists(obj))
1243                                 GOTO(err_put, rc = -ENOENT);
1244
1245                         start = lock->l_policy_data.l_extent.start;
1246                         end = lock->l_policy_data.l_extent.end;
1247                 }
1248
1249                 rc = tgt_sync(&env, tgt, obj, start, end);
1250                 if (rc < 0) {
1251                         CERROR("%s: syncing "DFID" (%llu-%llu) on lock "
1252                                "cancel: rc = %d\n",
1253                                tgt_name(tgt), PFID(&fid),
1254                                lock->l_policy_data.l_extent.start,
1255                                lock->l_policy_data.l_extent.end, rc);
1256                 }
1257 err_put:
1258                 if (obj != NULL)
1259                         dt_object_put(&env, obj);
1260 err_env:
1261                 lu_env_fini(&env);
1262         }
1263
1264         rc = ldlm_server_blocking_ast(lock, desc, data, flag);
1265         RETURN(rc);
1266 }
1267
1268 static struct ldlm_callback_suite tgt_dlm_cbs = {
1269         .lcs_completion = ldlm_server_completion_ast,
1270         .lcs_blocking   = tgt_blocking_ast,
1271         .lcs_glimpse    = ldlm_server_glimpse_ast
1272 };
1273
1274 int tgt_enqueue(struct tgt_session_info *tsi)
1275 {
1276         struct ptlrpc_request *req = tgt_ses_req(tsi);
1277         int rc;
1278
1279         ENTRY;
1280         /*
1281          * tsi->tsi_dlm_req was already swapped and (if necessary) converted,
1282          * tsi->tsi_dlm_cbs was set by the *_req_handle() function.
1283          */
1284         LASSERT(tsi->tsi_dlm_req != NULL);
1285         rc = ldlm_handle_enqueue0(tsi->tsi_exp->exp_obd->obd_namespace, req,
1286                                   tsi->tsi_dlm_req, &tgt_dlm_cbs);
1287         if (rc)
1288                 RETURN(err_serious(rc));
1289
1290         switch (LUT_FAIL_CLASS(tsi->tsi_reply_fail_id)) {
1291         case LUT_FAIL_MDT:
1292                 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_LDLM_REPLY_NET;
1293                 break;
1294         case LUT_FAIL_OST:
1295                 tsi->tsi_reply_fail_id = OBD_FAIL_OST_LDLM_REPLY_NET;
1296                 break;
1297         case LUT_FAIL_MGT:
1298                 tsi->tsi_reply_fail_id = OBD_FAIL_MGS_LDLM_REPLY_NET;
1299                 break;
1300         default:
1301                 tsi->tsi_reply_fail_id = OBD_FAIL_LDLM_REPLY;
1302                 break;
1303         }
1304         RETURN(req->rq_status);
1305 }
1306 EXPORT_SYMBOL(tgt_enqueue);
1307
1308 int tgt_convert(struct tgt_session_info *tsi)
1309 {
1310         struct ptlrpc_request *req = tgt_ses_req(tsi);
1311         int rc;
1312
1313         ENTRY;
1314         LASSERT(tsi->tsi_dlm_req);
1315         rc = ldlm_handle_convert0(req, tsi->tsi_dlm_req);
1316         if (rc)
1317                 RETURN(err_serious(rc));
1318
1319         RETURN(req->rq_status);
1320 }
1321
1322 int tgt_bl_callback(struct tgt_session_info *tsi)
1323 {
1324         return err_serious(-EOPNOTSUPP);
1325 }
1326
1327 int tgt_cp_callback(struct tgt_session_info *tsi)
1328 {
1329         return err_serious(-EOPNOTSUPP);
1330 }
1331
1332 /* generic LDLM target handler */
1333 struct tgt_handler tgt_dlm_handlers[] = {
1334 TGT_DLM_HDL    (HABEO_CLAVIS,   LDLM_ENQUEUE,           tgt_enqueue),
1335 TGT_DLM_HDL_VAR(HABEO_CLAVIS,   LDLM_CONVERT,           tgt_convert),
1336 TGT_DLM_HDL_VAR(0,              LDLM_BL_CALLBACK,       tgt_bl_callback),
1337 TGT_DLM_HDL_VAR(0,              LDLM_CP_CALLBACK,       tgt_cp_callback)
1338 };
1339 EXPORT_SYMBOL(tgt_dlm_handlers);
1340
1341 /*
1342  * Unified target LLOG handlers.
1343  */
1344 int tgt_llog_open(struct tgt_session_info *tsi)
1345 {
1346         int rc;
1347
1348         ENTRY;
1349
1350         rc = llog_origin_handle_open(tgt_ses_req(tsi));
1351
1352         RETURN(rc);
1353 }
1354 EXPORT_SYMBOL(tgt_llog_open);
1355
1356 int tgt_llog_close(struct tgt_session_info *tsi)
1357 {
1358         int rc;
1359
1360         ENTRY;
1361
1362         rc = llog_origin_handle_close(tgt_ses_req(tsi));
1363
1364         RETURN(rc);
1365 }
1366 EXPORT_SYMBOL(tgt_llog_close);
1367
1368
1369 int tgt_llog_destroy(struct tgt_session_info *tsi)
1370 {
1371         int rc;
1372
1373         ENTRY;
1374
1375         rc = llog_origin_handle_destroy(tgt_ses_req(tsi));
1376
1377         RETURN(rc);
1378 }
1379
1380 int tgt_llog_read_header(struct tgt_session_info *tsi)
1381 {
1382         int rc;
1383
1384         ENTRY;
1385
1386         rc = llog_origin_handle_read_header(tgt_ses_req(tsi));
1387
1388         RETURN(rc);
1389 }
1390 EXPORT_SYMBOL(tgt_llog_read_header);
1391
1392 int tgt_llog_next_block(struct tgt_session_info *tsi)
1393 {
1394         int rc;
1395
1396         ENTRY;
1397
1398         rc = llog_origin_handle_next_block(tgt_ses_req(tsi));
1399
1400         RETURN(rc);
1401 }
1402 EXPORT_SYMBOL(tgt_llog_next_block);
1403
1404 int tgt_llog_prev_block(struct tgt_session_info *tsi)
1405 {
1406         int rc;
1407
1408         ENTRY;
1409
1410         rc = llog_origin_handle_prev_block(tgt_ses_req(tsi));
1411
1412         RETURN(rc);
1413 }
1414 EXPORT_SYMBOL(tgt_llog_prev_block);
1415
1416 /* generic llog target handler */
1417 struct tgt_handler tgt_llog_handlers[] = {
1418 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_CREATE,      tgt_llog_open),
1419 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_NEXT_BLOCK,  tgt_llog_next_block),
1420 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
1421 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_PREV_BLOCK,  tgt_llog_prev_block),
1422 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_DESTROY,     tgt_llog_destroy),
1423 TGT_LLOG_HDL_VAR(0,     LLOG_ORIGIN_HANDLE_CLOSE,       tgt_llog_close),
1424 };
1425 EXPORT_SYMBOL(tgt_llog_handlers);
1426
1427 /*
1428  * sec context handlers
1429  */
1430 /* XXX: Implement based on mdt_sec_ctx_handle()? */
1431 static int tgt_sec_ctx_handle(struct tgt_session_info *tsi)
1432 {
1433         return 0;
1434 }
1435
1436 struct tgt_handler tgt_sec_ctx_handlers[] = {
1437 TGT_SEC_HDL_VAR(0,      SEC_CTX_INIT,           tgt_sec_ctx_handle),
1438 TGT_SEC_HDL_VAR(0,      SEC_CTX_INIT_CONT,      tgt_sec_ctx_handle),
1439 TGT_SEC_HDL_VAR(0,      SEC_CTX_FINI,           tgt_sec_ctx_handle),
1440 };
1441 EXPORT_SYMBOL(tgt_sec_ctx_handlers);
1442
1443 int (*tgt_lfsck_in_notify_local)(const struct lu_env *env,
1444                                  struct dt_device *key,
1445                                  struct lfsck_req_local *lrl,
1446                                  struct thandle *th) = NULL;
1447
1448 void tgt_register_lfsck_in_notify_local(int (*notify)(const struct lu_env *,
1449                                                       struct dt_device *,
1450                                                       struct lfsck_req_local *,
1451                                                       struct thandle *))
1452 {
1453         tgt_lfsck_in_notify_local = notify;
1454 }
1455 EXPORT_SYMBOL(tgt_register_lfsck_in_notify_local);
1456
1457 int (*tgt_lfsck_in_notify)(const struct lu_env *env,
1458                            struct dt_device *key,
1459                            struct lfsck_request *lr) = NULL;
1460
1461 void tgt_register_lfsck_in_notify(int (*notify)(const struct lu_env *,
1462                                                 struct dt_device *,
1463                                                 struct lfsck_request *))
1464 {
1465         tgt_lfsck_in_notify = notify;
1466 }
1467 EXPORT_SYMBOL(tgt_register_lfsck_in_notify);
1468
1469 static int (*tgt_lfsck_query)(const struct lu_env *env,
1470                               struct dt_device *key,
1471                               struct lfsck_request *req,
1472                               struct lfsck_reply *rep,
1473                               struct lfsck_query *que) = NULL;
1474
1475 void tgt_register_lfsck_query(int (*query)(const struct lu_env *,
1476                                            struct dt_device *,
1477                                            struct lfsck_request *,
1478                                            struct lfsck_reply *,
1479                                            struct lfsck_query *))
1480 {
1481         tgt_lfsck_query = query;
1482 }
1483 EXPORT_SYMBOL(tgt_register_lfsck_query);
1484
1485 /* LFSCK request handlers */
1486 static int tgt_handle_lfsck_notify(struct tgt_session_info *tsi)
1487 {
1488         const struct lu_env     *env = tsi->tsi_env;
1489         struct dt_device        *key = tsi->tsi_tgt->lut_bottom;
1490         struct lfsck_request    *lr;
1491         int                      rc;
1492         ENTRY;
1493
1494         lr = req_capsule_client_get(tsi->tsi_pill, &RMF_LFSCK_REQUEST);
1495         if (lr == NULL)
1496                 RETURN(-EPROTO);
1497
1498         rc = tgt_lfsck_in_notify(env, key, lr);
1499
1500         RETURN(rc);
1501 }
1502
1503 static int tgt_handle_lfsck_query(struct tgt_session_info *tsi)
1504 {
1505         struct lfsck_request    *request;
1506         struct lfsck_reply      *reply;
1507         int                      rc;
1508         ENTRY;
1509
1510         request = req_capsule_client_get(tsi->tsi_pill, &RMF_LFSCK_REQUEST);
1511         if (request == NULL)
1512                 RETURN(-EPROTO);
1513
1514         reply = req_capsule_server_get(tsi->tsi_pill, &RMF_LFSCK_REPLY);
1515         if (reply == NULL)
1516                 RETURN(-ENOMEM);
1517
1518         rc = tgt_lfsck_query(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
1519                              request, reply, NULL);
1520
1521         RETURN(rc < 0 ? rc : 0);
1522 }
1523
1524 struct tgt_handler tgt_lfsck_handlers[] = {
1525 TGT_LFSCK_HDL(HABEO_REFERO,     LFSCK_NOTIFY,   tgt_handle_lfsck_notify),
1526 TGT_LFSCK_HDL(HABEO_REFERO,     LFSCK_QUERY,    tgt_handle_lfsck_query),
1527 };
1528 EXPORT_SYMBOL(tgt_lfsck_handlers);
1529
1530 /*
1531  * initialize per-thread page pool (bug 5137).
1532  */
1533 int tgt_io_thread_init(struct ptlrpc_thread *thread)
1534 {
1535         struct tgt_thread_big_cache *tbc;
1536
1537         ENTRY;
1538
1539         LASSERT(thread != NULL);
1540         LASSERT(thread->t_data == NULL);
1541
1542         OBD_ALLOC_LARGE(tbc, sizeof(*tbc));
1543         if (tbc == NULL)
1544                 RETURN(-ENOMEM);
1545         thread->t_data = tbc;
1546         RETURN(0);
1547 }
1548 EXPORT_SYMBOL(tgt_io_thread_init);
1549
1550 /*
1551  * free per-thread pool created by tgt_thread_init().
1552  */
1553 void tgt_io_thread_done(struct ptlrpc_thread *thread)
1554 {
1555         struct tgt_thread_big_cache *tbc;
1556
1557         ENTRY;
1558
1559         LASSERT(thread != NULL);
1560
1561         /*
1562          * be prepared to handle partially-initialized pools (because this is
1563          * called from ost_io_thread_init() for cleanup.
1564          */
1565         tbc = thread->t_data;
1566         if (tbc != NULL) {
1567                 OBD_FREE_LARGE(tbc, sizeof(*tbc));
1568                 thread->t_data = NULL;
1569         }
1570         EXIT;
1571 }
1572 EXPORT_SYMBOL(tgt_io_thread_done);
1573 /**
1574  * Helper function for getting server side [start, start+count] DLM lock
1575  * if asked by client.
1576  */
1577 int tgt_extent_lock(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
1578                     __u64 start, __u64 end, struct lustre_handle *lh,
1579                     int mode, __u64 *flags)
1580 {
1581         union ldlm_policy_data policy;
1582         int rc;
1583
1584         ENTRY;
1585
1586         LASSERT(lh != NULL);
1587         LASSERT(ns != NULL);
1588         LASSERT(!lustre_handle_is_used(lh));
1589
1590         policy.l_extent.gid = 0;
1591         policy.l_extent.start = start & PAGE_MASK;
1592
1593         /*
1594          * If ->o_blocks is EOF it means "lock till the end of the file".
1595          * Otherwise, it's size of an extent or hole being punched (in bytes).
1596          */
1597         if (end == OBD_OBJECT_EOF || end < start)
1598                 policy.l_extent.end = OBD_OBJECT_EOF;
1599         else
1600                 policy.l_extent.end = end | ~PAGE_MASK;
1601
1602         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_EXTENT, &policy, mode,
1603                                     flags, ldlm_blocking_ast,
1604                                     ldlm_completion_ast, ldlm_glimpse_ast,
1605                                     NULL, 0, LVB_T_NONE, NULL, lh);
1606         RETURN(rc == ELDLM_OK ? 0 : -EIO);
1607 }
1608 EXPORT_SYMBOL(tgt_extent_lock);
1609
1610 void tgt_extent_unlock(struct lustre_handle *lh, enum ldlm_mode mode)
1611 {
1612         LASSERT(lustre_handle_is_used(lh));
1613         ldlm_lock_decref(lh, mode);
1614 }
1615 EXPORT_SYMBOL(tgt_extent_unlock);
1616
1617 int tgt_brw_lock(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
1618                  struct obd_ioobj *obj, struct niobuf_remote *nb,
1619                  struct lustre_handle *lh, enum ldlm_mode mode)
1620 {
1621         __u64                    flags = 0;
1622         int                      nrbufs = obj->ioo_bufcnt;
1623         int                      i;
1624
1625         ENTRY;
1626
1627         LASSERT(mode == LCK_PR || mode == LCK_PW);
1628         LASSERT(!lustre_handle_is_used(lh));
1629
1630         if (ns->ns_obd->obd_recovering)
1631                 RETURN(0);
1632
1633         if (nrbufs == 0 || !(nb[0].rnb_flags & OBD_BRW_SRVLOCK))
1634                 RETURN(0);
1635
1636         for (i = 1; i < nrbufs; i++)
1637                 if (!(nb[i].rnb_flags & OBD_BRW_SRVLOCK))
1638                         RETURN(-EFAULT);
1639
1640         RETURN(tgt_extent_lock(ns, res_id, nb[0].rnb_offset,
1641                                nb[nrbufs - 1].rnb_offset +
1642                                nb[nrbufs - 1].rnb_len - 1,
1643                                lh, mode, &flags));
1644 }
1645
1646 void tgt_brw_unlock(struct obd_ioobj *obj, struct niobuf_remote *niob,
1647                     struct lustre_handle *lh, enum ldlm_mode mode)
1648 {
1649         ENTRY;
1650
1651         LASSERT(mode == LCK_PR || mode == LCK_PW);
1652         LASSERT((obj->ioo_bufcnt > 0 &&
1653                  (niob[0].rnb_flags & OBD_BRW_SRVLOCK)) ==
1654                 lustre_handle_is_used(lh));
1655
1656         if (lustre_handle_is_used(lh))
1657                 tgt_extent_unlock(lh, mode);
1658         EXIT;
1659 }
1660
1661 static __u32 tgt_checksum_bulk(struct lu_target *tgt,
1662                                struct ptlrpc_bulk_desc *desc, int opc,
1663                                cksum_type_t cksum_type)
1664 {
1665         struct cfs_crypto_hash_desc     *hdesc;
1666         unsigned int                    bufsize;
1667         int                             i, err;
1668         unsigned char                   cfs_alg = cksum_obd2cfs(cksum_type);
1669         __u32                           cksum;
1670
1671         LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
1672
1673         hdesc = cfs_crypto_hash_init(cfs_alg, NULL, 0);
1674         if (IS_ERR(hdesc)) {
1675                 CERROR("%s: unable to initialize checksum hash %s\n",
1676                        tgt_name(tgt), cfs_crypto_hash_name(cfs_alg));
1677                 return PTR_ERR(hdesc);
1678         }
1679
1680         CDEBUG(D_INFO, "Checksum for algo %s\n", cfs_crypto_hash_name(cfs_alg));
1681         for (i = 0; i < desc->bd_iov_count; i++) {
1682                 /* corrupt the data before we compute the checksum, to
1683                  * simulate a client->OST data error */
1684                 if (i == 0 && opc == OST_WRITE &&
1685                     OBD_FAIL_CHECK(OBD_FAIL_OST_CHECKSUM_RECEIVE)) {
1686                         int off = BD_GET_KIOV(desc, i).kiov_offset &
1687                                 ~PAGE_MASK;
1688                         int len = BD_GET_KIOV(desc, i).kiov_len;
1689                         struct page *np = tgt_page_to_corrupt;
1690                         char *ptr = kmap(BD_GET_KIOV(desc, i).kiov_page) + off;
1691
1692                         if (np) {
1693                                 char *ptr2 = kmap(np) + off;
1694
1695                                 memcpy(ptr2, ptr, len);
1696                                 memcpy(ptr2, "bad3", min(4, len));
1697                                 kunmap(np);
1698
1699                                 /* LU-8376 to preserve original index for
1700                                  * display in dump_all_bulk_pages() */
1701                                 np->index = BD_GET_KIOV(desc,
1702                                                         i).kiov_page->index;
1703
1704                                 BD_GET_KIOV(desc, i).kiov_page = np;
1705                         } else {
1706                                 CERROR("%s: can't alloc page for corruption\n",
1707                                        tgt_name(tgt));
1708                         }
1709                 }
1710                 cfs_crypto_hash_update_page(hdesc,
1711                                   BD_GET_KIOV(desc, i).kiov_page,
1712                                   BD_GET_KIOV(desc, i).kiov_offset &
1713                                         ~PAGE_MASK,
1714                                   BD_GET_KIOV(desc, i).kiov_len);
1715
1716                  /* corrupt the data after we compute the checksum, to
1717                  * simulate an OST->client data error */
1718                 if (i == 0 && opc == OST_READ &&
1719                     OBD_FAIL_CHECK(OBD_FAIL_OST_CHECKSUM_SEND)) {
1720                         int off = BD_GET_KIOV(desc, i).kiov_offset
1721                           & ~PAGE_MASK;
1722                         int len = BD_GET_KIOV(desc, i).kiov_len;
1723                         struct page *np = tgt_page_to_corrupt;
1724                         char *ptr =
1725                           kmap(BD_GET_KIOV(desc, i).kiov_page) + off;
1726
1727                         if (np) {
1728                                 char *ptr2 = kmap(np) + off;
1729
1730                                 memcpy(ptr2, ptr, len);
1731                                 memcpy(ptr2, "bad4", min(4, len));
1732                                 kunmap(np);
1733
1734                                 /* LU-8376 to preserve original index for
1735                                  * display in dump_all_bulk_pages() */
1736                                 np->index = BD_GET_KIOV(desc,
1737                                                         i).kiov_page->index;
1738
1739                                 BD_GET_KIOV(desc, i).kiov_page = np;
1740                         } else {
1741                                 CERROR("%s: can't alloc page for corruption\n",
1742                                        tgt_name(tgt));
1743                         }
1744                 }
1745         }
1746
1747         bufsize = sizeof(cksum);
1748         err = cfs_crypto_hash_final(hdesc, (unsigned char *)&cksum, &bufsize);
1749
1750         return cksum;
1751 }
1752
1753 char dbgcksum_file_name[PATH_MAX];
1754
1755 static void dump_all_bulk_pages(struct obdo *oa, int count,
1756                                     lnet_kiov_t *iov, __u32 server_cksum,
1757                                     __u32 client_cksum)
1758 {
1759         struct file *filp;
1760         int rc, i;
1761         unsigned int len;
1762         char *buf;
1763         mm_segment_t oldfs;
1764
1765         /* will only keep dump of pages on first error for the same range in
1766          * file/fid, not during the resends/retries. */
1767         snprintf(dbgcksum_file_name, sizeof(dbgcksum_file_name),
1768                  "%s-checksum_dump-ost-"DFID":[%llu-%llu]-%x-%x",
1769                  (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0 ?
1770                   libcfs_debug_file_path_arr :
1771                   LIBCFS_DEBUG_FILE_PATH_DEFAULT),
1772                  oa->o_valid & OBD_MD_FLFID ? oa->o_parent_seq : (__u64)0,
1773                  oa->o_valid & OBD_MD_FLFID ? oa->o_parent_oid : 0,
1774                  oa->o_valid & OBD_MD_FLFID ? oa->o_parent_ver : 0,
1775                  (__u64)iov[0].kiov_page->index << PAGE_SHIFT,
1776                  ((__u64)iov[count - 1].kiov_page->index << PAGE_SHIFT) +
1777                  iov[count - 1].kiov_len - 1, client_cksum, server_cksum);
1778         filp = filp_open(dbgcksum_file_name,
1779                          O_CREAT | O_EXCL | O_WRONLY | O_LARGEFILE, 0600);
1780         if (IS_ERR(filp)) {
1781                 rc = PTR_ERR(filp);
1782                 if (rc == -EEXIST)
1783                         CDEBUG(D_INFO, "%s: can't open to dump pages with "
1784                                "checksum error: rc = %d\n", dbgcksum_file_name,
1785                                rc);
1786                 else
1787                         CERROR("%s: can't open to dump pages with checksum "
1788                                "error: rc = %d\n", dbgcksum_file_name, rc);
1789                 return;
1790         }
1791
1792         oldfs = get_fs();
1793         set_fs(KERNEL_DS);
1794         for (i = 0; i < count; i++) {
1795                 len = iov[i].kiov_len;
1796                 buf = kmap(iov[i].kiov_page);
1797                 while (len != 0) {
1798                         rc = vfs_write(filp, (__force const char __user *)buf,
1799                                        len, &filp->f_pos);
1800                         if (rc < 0) {
1801                                 CERROR("%s: wanted to write %u but got %d "
1802                                        "error\n", dbgcksum_file_name, len, rc);
1803                                 break;
1804                         }
1805                         len -= rc;
1806                         buf += rc;
1807                         CDEBUG(D_INFO, "%s: wrote %d bytes\n",
1808                                dbgcksum_file_name, rc);
1809                 }
1810                 kunmap(iov[i].kiov_page);
1811         }
1812         set_fs(oldfs);
1813
1814         rc = ll_vfs_fsync_range(filp, 0, LLONG_MAX, 1);
1815         if (rc)
1816                 CERROR("%s: sync returns %d\n", dbgcksum_file_name, rc);
1817         filp_close(filp, NULL);
1818         return;
1819 }
1820
1821 static int check_read_checksum(struct ptlrpc_bulk_desc *desc, struct obdo *oa,
1822                                const lnet_process_id_t *peer,
1823                                __u32 client_cksum, __u32 server_cksum,
1824                                cksum_type_t server_cksum_type)
1825 {
1826         char *msg;
1827         cksum_type_t cksum_type;
1828
1829         /* unlikely to happen and only if resend does not occur due to cksum
1830          * control failure on Client */
1831         if (unlikely(server_cksum == client_cksum)) {
1832                 CDEBUG(D_PAGE, "checksum %x confirmed upon retry\n",
1833                        client_cksum);
1834                 return 0;
1835         }
1836
1837         if (desc->bd_export->exp_obd->obd_checksum_dump)
1838                 dump_all_bulk_pages(oa, desc->bd_iov_count,
1839                                     &BD_GET_KIOV(desc, 0), server_cksum,
1840                                     client_cksum);
1841
1842         cksum_type = cksum_type_unpack(oa->o_valid & OBD_MD_FLFLAGS ?
1843                                        oa->o_flags : 0);
1844
1845         if (cksum_type != server_cksum_type)
1846                 msg = "the server may have not used the checksum type specified"
1847                       " in the original request - likely a protocol problem";
1848         else
1849                 msg = "should have changed on the client or in transit";
1850
1851         LCONSOLE_ERROR_MSG(0x132, "%s: BAD READ CHECKSUM: %s: from %s inode "
1852                 DFID " object "DOSTID" extent [%llu-%llu], client returned csum"
1853                 " %x (type %x), server csum %x (type %x)\n",
1854                 desc->bd_export->exp_obd->obd_name,
1855                 msg, libcfs_nid2str(peer->nid),
1856                 oa->o_valid & OBD_MD_FLFID ? oa->o_parent_seq : 0ULL,
1857                 oa->o_valid & OBD_MD_FLFID ? oa->o_parent_oid : 0,
1858                 oa->o_valid & OBD_MD_FLFID ? oa->o_parent_ver : 0,
1859                 POSTID(&oa->o_oi),
1860                 (__u64)BD_GET_KIOV(desc, 0).kiov_page->index << PAGE_SHIFT,
1861                 ((__u64)BD_GET_KIOV(desc,
1862                                     desc->bd_iov_count - 1).kiov_page->index
1863                         << PAGE_SHIFT) +
1864                         BD_GET_KIOV(desc, desc->bd_iov_count - 1).kiov_len - 1,
1865                 client_cksum, cksum_type, server_cksum, server_cksum_type);
1866         return 1;
1867 }
1868
1869 int tgt_brw_read(struct tgt_session_info *tsi)
1870 {
1871         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1872         struct ptlrpc_bulk_desc *desc = NULL;
1873         struct obd_export       *exp = tsi->tsi_exp;
1874         struct niobuf_remote    *remote_nb;
1875         struct niobuf_local     *local_nb;
1876         struct obd_ioobj        *ioo;
1877         struct ost_body         *body, *repbody;
1878         struct l_wait_info       lwi;
1879         struct lustre_handle     lockh = { 0 };
1880         int                      npages, nob = 0, rc, i, no_reply = 0;
1881         struct tgt_thread_big_cache *tbc = req->rq_svc_thread->t_data;
1882
1883         ENTRY;
1884
1885         if (ptlrpc_req2svc(req)->srv_req_portal != OST_IO_PORTAL) {
1886                 CERROR("%s: deny read request from %s to portal %u\n",
1887                        tgt_name(tsi->tsi_tgt),
1888                        obd_export_nid2str(req->rq_export),
1889                        ptlrpc_req2svc(req)->srv_req_portal);
1890                 RETURN(-EPROTO);
1891         }
1892
1893         req->rq_bulk_read = 1;
1894
1895         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
1896                 RETURN(-EIO);
1897
1898         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, cfs_fail_val > 0 ?
1899                          cfs_fail_val : (obd_timeout + 1) / 4);
1900
1901         /* Check if there is eviction in progress, and if so, wait for it to
1902          * finish */
1903         if (unlikely(atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
1904                 /* We do not care how long it takes */
1905                 lwi = LWI_INTR(NULL, NULL);
1906                 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
1907                          !atomic_read(&exp->exp_obd->obd_evict_inprogress),
1908                          &lwi);
1909         }
1910
1911         /* There must be big cache in current thread to process this request
1912          * if it is NULL then something went wrong and it wasn't allocated,
1913          * report -ENOMEM in that case */
1914         if (tbc == NULL)
1915                 RETURN(-ENOMEM);
1916
1917         body = tsi->tsi_ost_body;
1918         LASSERT(body != NULL);
1919
1920         ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
1921         LASSERT(ioo != NULL); /* must exists after tgt_ost_body_unpack */
1922
1923         remote_nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1924         LASSERT(remote_nb != NULL); /* must exists after tgt_ost_body_unpack */
1925
1926         local_nb = tbc->local;
1927
1928         rc = tgt_brw_lock(exp->exp_obd->obd_namespace, &tsi->tsi_resid, ioo,
1929                           remote_nb, &lockh, LCK_PR);
1930         if (rc != 0)
1931                 RETURN(rc);
1932
1933         /*
1934          * If getting the lock took more time than
1935          * client was willing to wait, drop it. b=11330
1936          */
1937         if (ktime_get_real_seconds() > req->rq_deadline ||
1938             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
1939                 no_reply = 1;
1940                 CERROR("Dropping timed-out read from %s because locking object " DOSTID " took %lld seconds (limit was %lld).\n",
1941                        libcfs_id2str(req->rq_peer), POSTID(&ioo->ioo_oid),
1942                        ktime_get_real_seconds() - req->rq_arrival_time.tv_sec,
1943                        req->rq_deadline - req->rq_arrival_time.tv_sec);
1944                 GOTO(out_lock, rc = -ETIMEDOUT);
1945         }
1946
1947         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
1948         repbody->oa = body->oa;
1949
1950         npages = PTLRPC_MAX_BRW_PAGES;
1951         rc = obd_preprw(tsi->tsi_env, OBD_BRW_READ, exp, &repbody->oa, 1,
1952                         ioo, remote_nb, &npages, local_nb);
1953         if (rc != 0)
1954                 GOTO(out_lock, rc);
1955
1956         desc = ptlrpc_prep_bulk_exp(req, npages, ioobj_max_brw_get(ioo),
1957                                     PTLRPC_BULK_PUT_SOURCE |
1958                                         PTLRPC_BULK_BUF_KIOV,
1959                                     OST_BULK_PORTAL,
1960                                     &ptlrpc_bulk_kiov_nopin_ops);
1961         if (desc == NULL)
1962                 GOTO(out_commitrw, rc = -ENOMEM);
1963
1964         nob = 0;
1965         for (i = 0; i < npages; i++) {
1966                 int page_rc = local_nb[i].lnb_rc;
1967
1968                 if (page_rc < 0) {
1969                         rc = page_rc;
1970                         break;
1971                 }
1972
1973                 nob += page_rc;
1974                 if (page_rc != 0) { /* some data! */
1975                         LASSERT(local_nb[i].lnb_page != NULL);
1976                         desc->bd_frag_ops->add_kiov_frag
1977                           (desc, local_nb[i].lnb_page,
1978                            local_nb[i].lnb_page_offset,
1979                            page_rc);
1980                 }
1981
1982                 if (page_rc != local_nb[i].lnb_len) { /* short read */
1983                         /* All subsequent pages should be 0 */
1984                         while (++i < npages)
1985                                 LASSERT(local_nb[i].lnb_rc == 0);
1986                         break;
1987                 }
1988         }
1989         if (OBD_FAIL_CHECK(OBD_FAIL_OST_READ_SIZE) &&
1990             nob != cfs_fail_val)
1991                 rc = -E2BIG;
1992
1993         if (body->oa.o_valid & OBD_MD_FLCKSUM) {
1994                 cksum_type_t cksum_type =
1995                         cksum_type_unpack(body->oa.o_valid & OBD_MD_FLFLAGS ?
1996                                           body->oa.o_flags : 0);
1997
1998                 repbody->oa.o_flags = cksum_type_pack(cksum_type);
1999                 repbody->oa.o_valid = OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
2000                 repbody->oa.o_cksum = tgt_checksum_bulk(tsi->tsi_tgt, desc,
2001                                                         OST_READ, cksum_type);
2002                 CDEBUG(D_PAGE, "checksum at read origin: %x\n",
2003                        repbody->oa.o_cksum);
2004
2005                 /* if a resend it could be for a cksum error, so check Server
2006                  * cksum with returned Client cksum (this should even cover
2007                  * zero-cksum case) */
2008                 if ((body->oa.o_valid & OBD_MD_FLFLAGS) &&
2009                     (body->oa.o_flags & OBD_FL_RECOV_RESEND))
2010                         check_read_checksum(desc, &body->oa, &req->rq_peer,
2011                                             body->oa.o_cksum,
2012                                             repbody->oa.o_cksum, cksum_type);
2013         } else {
2014                 repbody->oa.o_valid = 0;
2015         }
2016         /* We're finishing using body->oa as an input variable */
2017
2018         /* Check if client was evicted while we were doing i/o before touching
2019          * network */
2020         if (likely(rc == 0 &&
2021                    !CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CLIENT_BULK_CB2) &&
2022                    !CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_BULK))) {
2023                 rc = target_bulk_io(exp, desc, &lwi);
2024                 no_reply = rc != 0;
2025         }
2026
2027 out_commitrw:
2028         /* Must commit after prep above in all cases */
2029         rc = obd_commitrw(tsi->tsi_env, OBD_BRW_READ, exp, &repbody->oa, 1, ioo,
2030                           remote_nb, npages, local_nb, rc);
2031 out_lock:
2032         tgt_brw_unlock(ioo, remote_nb, &lockh, LCK_PR);
2033
2034         if (desc && !CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CLIENT_BULK_CB2))
2035                 ptlrpc_free_bulk(desc);
2036
2037         LASSERT(rc <= 0);
2038         if (rc == 0) {
2039                 rc = nob;
2040                 ptlrpc_lprocfs_brw(req, nob);
2041         } else if (no_reply) {
2042                 req->rq_no_reply = 1;
2043                 /* reply out callback would free */
2044                 ptlrpc_req_drop_rs(req);
2045                 LCONSOLE_WARN("%s: Bulk IO read error with %s (at %s), "
2046                               "client will retry: rc %d\n",
2047                               exp->exp_obd->obd_name,
2048                               obd_uuid2str(&exp->exp_client_uuid),
2049                               obd_export_nid2str(exp), rc);
2050         }
2051         /* send a bulk after reply to simulate a network delay or reordering
2052          * by a router */
2053         if (unlikely(CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CLIENT_BULK_CB2))) {
2054                 wait_queue_head_t        waitq;
2055                 struct l_wait_info       lwi1;
2056
2057                 CDEBUG(D_INFO, "reorder BULK\n");
2058                 init_waitqueue_head(&waitq);
2059
2060                 lwi1 = LWI_TIMEOUT_INTR(cfs_time_seconds(3), NULL, NULL, NULL);
2061                 l_wait_event(waitq, 0, &lwi1);
2062                 target_bulk_io(exp, desc, &lwi);
2063                 ptlrpc_free_bulk(desc);
2064         }
2065
2066         RETURN(rc);
2067 }
2068 EXPORT_SYMBOL(tgt_brw_read);
2069
2070 static void tgt_warn_on_cksum(struct ptlrpc_request *req,
2071                               struct ptlrpc_bulk_desc *desc,
2072                               struct niobuf_local *local_nb, int npages,
2073                               u32 client_cksum, u32 server_cksum,
2074                               bool mmap)
2075 {
2076         struct obd_export *exp = req->rq_export;
2077         struct ost_body *body;
2078         char *router = "";
2079         char *via = "";
2080
2081         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
2082         LASSERT(body != NULL);
2083
2084         if (req->rq_peer.nid != desc->bd_sender) {
2085                 via = " via ";
2086                 router = libcfs_nid2str(desc->bd_sender);
2087         }
2088
2089         if (exp->exp_obd->obd_checksum_dump)
2090                 dump_all_bulk_pages(&body->oa, desc->bd_iov_count,
2091                                     &BD_GET_KIOV(desc, 0), server_cksum,
2092                                     client_cksum);
2093
2094         if (mmap) {
2095                 CDEBUG_LIMIT(D_INFO, "client csum %x, server csum %x\n",
2096                              client_cksum, server_cksum);
2097                 return;
2098         }
2099
2100         LCONSOLE_ERROR_MSG(0x168, "%s: BAD WRITE CHECKSUM: from %s%s%s inode "
2101                            DFID" object "DOSTID" extent [%llu-%llu"
2102                            "]: client csum %x, server csum %x\n",
2103                            exp->exp_obd->obd_name, libcfs_id2str(req->rq_peer),
2104                            via, router,
2105                            body->oa.o_valid & OBD_MD_FLFID ?
2106                            body->oa.o_parent_seq : (__u64)0,
2107                            body->oa.o_valid & OBD_MD_FLFID ?
2108                            body->oa.o_parent_oid : 0,
2109                            body->oa.o_valid & OBD_MD_FLFID ?
2110                            body->oa.o_parent_ver : 0,
2111                            POSTID(&body->oa.o_oi),
2112                            local_nb[0].lnb_file_offset,
2113                            local_nb[npages-1].lnb_file_offset +
2114                            local_nb[npages - 1].lnb_len - 1,
2115                            client_cksum, server_cksum);
2116 }
2117
2118 int tgt_brw_write(struct tgt_session_info *tsi)
2119 {
2120         struct ptlrpc_request   *req = tgt_ses_req(tsi);
2121         struct ptlrpc_bulk_desc *desc = NULL;
2122         struct obd_export       *exp = req->rq_export;
2123         struct niobuf_remote    *remote_nb;
2124         struct niobuf_local     *local_nb;
2125         struct obd_ioobj        *ioo;
2126         struct ost_body         *body, *repbody;
2127         struct l_wait_info       lwi;
2128         struct lustre_handle     lockh = {0};
2129         __u32                   *rcs;
2130         int                      objcount, niocount, npages;
2131         int                      rc, i, j;
2132         cksum_type_t             cksum_type = OBD_CKSUM_CRC32;
2133         bool                     no_reply = false, mmap;
2134         struct tgt_thread_big_cache *tbc = req->rq_svc_thread->t_data;
2135         bool wait_sync = false;
2136
2137         ENTRY;
2138
2139         if (ptlrpc_req2svc(req)->srv_req_portal != OST_IO_PORTAL) {
2140                 CERROR("%s: deny write request from %s to portal %u\n",
2141                        tgt_name(tsi->tsi_tgt),
2142                        obd_export_nid2str(req->rq_export),
2143                        ptlrpc_req2svc(req)->srv_req_portal);
2144                 RETURN(err_serious(-EPROTO));
2145         }
2146
2147         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
2148                 RETURN(err_serious(-ENOSPC));
2149         if (OBD_FAIL_TIMEOUT(OBD_FAIL_OST_EROFS, 1))
2150                 RETURN(err_serious(-EROFS));
2151
2152         req->rq_bulk_write = 1;
2153
2154         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
2155                 RETURN(err_serious(-EIO));
2156         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK2))
2157                 RETURN(err_serious(-EFAULT));
2158
2159         /* pause before transaction has been started */
2160         CFS_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, cfs_fail_val > 0 ?
2161                          cfs_fail_val : (obd_timeout + 1) / 4);
2162
2163         /* There must be big cache in current thread to process this request
2164          * if it is NULL then something went wrong and it wasn't allocated,
2165          * report -ENOMEM in that case */
2166         if (tbc == NULL)
2167                 RETURN(-ENOMEM);
2168
2169         body = tsi->tsi_ost_body;
2170         LASSERT(body != NULL);
2171
2172         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
2173         LASSERT(ioo != NULL); /* must exists after tgt_ost_body_unpack */
2174
2175         objcount = req_capsule_get_size(&req->rq_pill, &RMF_OBD_IOOBJ,
2176                                         RCL_CLIENT) / sizeof(*ioo);
2177
2178         for (niocount = i = 0; i < objcount; i++)
2179                 niocount += ioo[i].ioo_bufcnt;
2180
2181         remote_nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
2182         LASSERT(remote_nb != NULL); /* must exists after tgt_ost_body_unpack */
2183         if (niocount != req_capsule_get_size(&req->rq_pill,
2184                                              &RMF_NIOBUF_REMOTE, RCL_CLIENT) /
2185                         sizeof(*remote_nb))
2186                 RETURN(err_serious(-EPROTO));
2187
2188         if ((remote_nb[0].rnb_flags & OBD_BRW_MEMALLOC) &&
2189             (exp->exp_connection->c_peer.nid == exp->exp_connection->c_self))
2190                 memory_pressure_set();
2191
2192         req_capsule_set_size(&req->rq_pill, &RMF_RCS, RCL_SERVER,
2193                              niocount * sizeof(*rcs));
2194         rc = req_capsule_server_pack(&req->rq_pill);
2195         if (rc != 0)
2196                 GOTO(out, rc = err_serious(rc));
2197
2198         CFS_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_PACK, cfs_fail_val);
2199         rcs = req_capsule_server_get(&req->rq_pill, &RMF_RCS);
2200
2201         local_nb = tbc->local;
2202
2203         rc = tgt_brw_lock(exp->exp_obd->obd_namespace, &tsi->tsi_resid, ioo,
2204                           remote_nb, &lockh, LCK_PW);
2205         if (rc != 0)
2206                 GOTO(out, rc);
2207
2208         /*
2209          * If getting the lock took more time than
2210          * client was willing to wait, drop it. b=11330
2211          */
2212         if (ktime_get_real_seconds() > req->rq_deadline ||
2213             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
2214                 no_reply = true;
2215                 CERROR("%s: Dropping timed-out write from %s because locking object " DOSTID " took %lld seconds (limit was %lld).\n",
2216                        tgt_name(tsi->tsi_tgt), libcfs_id2str(req->rq_peer),
2217                        POSTID(&ioo->ioo_oid),
2218                        ktime_get_real_seconds() - req->rq_arrival_time.tv_sec,
2219                        req->rq_deadline - req->rq_arrival_time.tv_sec);
2220                 GOTO(out_lock, rc = -ETIMEDOUT);
2221         }
2222
2223         /* Because we already sync grant info with client when reconnect,
2224          * grant info will be cleared for resent req, then fed_grant and
2225          * total_grant will not be modified in following preprw_write */
2226         if (lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY)) {
2227                 DEBUG_REQ(D_CACHE, req, "clear resent/replay req grant info");
2228                 body->oa.o_valid &= ~OBD_MD_FLGRANT;
2229         }
2230
2231         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
2232         if (repbody == NULL)
2233                 GOTO(out_lock, rc = -ENOMEM);
2234         repbody->oa = body->oa;
2235
2236         npages = PTLRPC_MAX_BRW_PAGES;
2237         rc = obd_preprw(tsi->tsi_env, OBD_BRW_WRITE, exp, &repbody->oa,
2238                         objcount, ioo, remote_nb, &npages, local_nb);
2239         if (rc < 0)
2240                 GOTO(out_lock, rc);
2241
2242         desc = ptlrpc_prep_bulk_exp(req, npages, ioobj_max_brw_get(ioo),
2243                                     PTLRPC_BULK_GET_SINK | PTLRPC_BULK_BUF_KIOV,
2244                                     OST_BULK_PORTAL,
2245                                     &ptlrpc_bulk_kiov_nopin_ops);
2246         if (desc == NULL)
2247                 GOTO(skip_transfer, rc = -ENOMEM);
2248
2249         /* NB Having prepped, we must commit... */
2250         for (i = 0; i < npages; i++)
2251                 desc->bd_frag_ops->add_kiov_frag(desc,
2252                                                  local_nb[i].lnb_page,
2253                                                  local_nb[i].lnb_page_offset,
2254                                                  local_nb[i].lnb_len);
2255
2256         rc = sptlrpc_svc_prep_bulk(req, desc);
2257         if (rc != 0)
2258                 GOTO(skip_transfer, rc);
2259
2260         rc = target_bulk_io(exp, desc, &lwi);
2261         no_reply = rc != 0;
2262
2263 skip_transfer:
2264         if (body->oa.o_valid & OBD_MD_FLCKSUM && rc == 0) {
2265                 static int cksum_counter;
2266
2267                 if (body->oa.o_valid & OBD_MD_FLFLAGS)
2268                         cksum_type = cksum_type_unpack(body->oa.o_flags);
2269
2270                 repbody->oa.o_valid |= OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
2271                 repbody->oa.o_flags &= ~OBD_FL_CKSUM_ALL;
2272                 repbody->oa.o_flags |= cksum_type_pack(cksum_type);
2273                 repbody->oa.o_cksum = tgt_checksum_bulk(tsi->tsi_tgt, desc,
2274                                                         OST_WRITE, cksum_type);
2275                 cksum_counter++;
2276
2277                 if (unlikely(body->oa.o_cksum != repbody->oa.o_cksum)) {
2278                         mmap = (body->oa.o_valid & OBD_MD_FLFLAGS &&
2279                                 body->oa.o_flags & OBD_FL_MMAP);
2280
2281                         tgt_warn_on_cksum(req, desc, local_nb, npages,
2282                                           body->oa.o_cksum,
2283                                           repbody->oa.o_cksum, mmap);
2284                         cksum_counter = 0;
2285                 } else if ((cksum_counter & (-cksum_counter)) ==
2286                            cksum_counter) {
2287                         CDEBUG(D_INFO, "Checksum %u from %s OK: %x\n",
2288                                cksum_counter, libcfs_id2str(req->rq_peer),
2289                                repbody->oa.o_cksum);
2290                 }
2291         }
2292
2293         /* Must commit after prep above in all cases */
2294         rc = obd_commitrw(tsi->tsi_env, OBD_BRW_WRITE, exp, &repbody->oa,
2295                           objcount, ioo, remote_nb, npages, local_nb, rc);
2296         if (rc == -ENOTCONN)
2297                 /* quota acquire process has been given up because
2298                  * either the client has been evicted or the client
2299                  * has timed out the request already */
2300                 no_reply = true;
2301
2302         for (i = 0; i < niocount; i++) {
2303                 if (!(local_nb[i].lnb_flags & OBD_BRW_ASYNC)) {
2304                         wait_sync = true;
2305                         break;
2306                 }
2307         }
2308         /*
2309          * Disable sending mtime back to the client. If the client locked the
2310          * whole object, then it has already updated the mtime on its side,
2311          * otherwise it will have to glimpse anyway (see bug 21489, comment 32)
2312          */
2313         repbody->oa.o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLATIME);
2314
2315         if (rc == 0) {
2316                 int nob = 0;
2317
2318                 /* set per-requested niobuf return codes */
2319                 for (i = j = 0; i < niocount; i++) {
2320                         int len = remote_nb[i].rnb_len;
2321
2322                         nob += len;
2323                         rcs[i] = 0;
2324                         do {
2325                                 LASSERT(j < npages);
2326                                 if (local_nb[j].lnb_rc < 0)
2327                                         rcs[i] = local_nb[j].lnb_rc;
2328                                 len -= local_nb[j].lnb_len;
2329                                 j++;
2330                         } while (len > 0);
2331                         LASSERT(len == 0);
2332                 }
2333                 LASSERT(j == npages);
2334                 ptlrpc_lprocfs_brw(req, nob);
2335         }
2336 out_lock:
2337         tgt_brw_unlock(ioo, remote_nb, &lockh, LCK_PW);
2338         if (desc)
2339                 ptlrpc_free_bulk(desc);
2340 out:
2341         if (unlikely(no_reply || (exp->exp_obd->obd_no_transno && wait_sync))) {
2342                 req->rq_no_reply = 1;
2343                 /* reply out callback would free */
2344                 ptlrpc_req_drop_rs(req);
2345                 if (!exp->exp_obd->obd_no_transno)
2346                         LCONSOLE_WARN("%s: Bulk IO write error with %s (at %s),"
2347                                       " client will retry: rc = %d\n",
2348                                       exp->exp_obd->obd_name,
2349                                       obd_uuid2str(&exp->exp_client_uuid),
2350                                       obd_export_nid2str(exp), rc);
2351         }
2352         memory_pressure_clr();
2353         RETURN(rc);
2354 }
2355 EXPORT_SYMBOL(tgt_brw_write);
2356
2357 /* Check if request can be reconstructed from saved reply data
2358  * A copy of the reply data is returned in @trd if the pointer is not NULL
2359  */
2360 bool req_can_reconstruct(struct ptlrpc_request *req,
2361                          struct tg_reply_data *trd)
2362 {
2363         struct tg_export_data *ted = &req->rq_export->exp_target_data;
2364         struct lsd_client_data *lcd = ted->ted_lcd;
2365         bool found;
2366
2367         if (tgt_is_multimodrpcs_client(req->rq_export))
2368                 return tgt_lookup_reply(req, trd);
2369
2370         mutex_lock(&ted->ted_lcd_lock);
2371         found = req->rq_xid == lcd->lcd_last_xid ||
2372                 req->rq_xid == lcd->lcd_last_close_xid;
2373
2374         if (found && trd != NULL) {
2375                 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
2376                         trd->trd_reply.lrd_xid = lcd->lcd_last_close_xid;
2377                         trd->trd_reply.lrd_transno =
2378                                                 lcd->lcd_last_close_transno;
2379                         trd->trd_reply.lrd_result = lcd->lcd_last_close_result;
2380                 } else {
2381                         trd->trd_reply.lrd_xid = lcd->lcd_last_xid;
2382                         trd->trd_reply.lrd_transno = lcd->lcd_last_transno;
2383                         trd->trd_reply.lrd_result = lcd->lcd_last_result;
2384                         trd->trd_reply.lrd_data = lcd->lcd_last_data;
2385                         trd->trd_pre_versions[0] = lcd->lcd_pre_versions[0];
2386                         trd->trd_pre_versions[1] = lcd->lcd_pre_versions[1];
2387                         trd->trd_pre_versions[2] = lcd->lcd_pre_versions[2];
2388                         trd->trd_pre_versions[3] = lcd->lcd_pre_versions[3];
2389                 }
2390         }
2391         mutex_unlock(&ted->ted_lcd_lock);
2392
2393         return found;
2394 }
2395 EXPORT_SYMBOL(req_can_reconstruct);
2396