Whamcloud - gitweb
873c23e0b87c84a607048778090df2dad19a900e
[fs/lustre-release.git] / lustre / ost / ost_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, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ost/ost_handler.c
37  *
38  * Author: Peter J. Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_OST
43
44 #include <linux/module.h>
45 #include <obd_cksum.h>
46 #include <obd_ost.h>
47 #include <lustre_net.h>
48 #include <lustre_dlm.h>
49 #include <lustre_export.h>
50 #include <lustre_debug.h>
51 #include <lustre_fid.h>
52 #include <lustre_fld.h>
53 #include <linux/init.h>
54 #include <lprocfs_status.h>
55 #include <libcfs/list.h>
56 #include <lustre_quota.h>
57 #include <lustre_fid.h>
58 #include "ost_internal.h"
59 #include <lustre_fid.h>
60
61 static int oss_num_threads;
62 CFS_MODULE_PARM(oss_num_threads, "i", int, 0444,
63                 "number of OSS service threads to start");
64
65 static int ost_num_threads;
66 CFS_MODULE_PARM(ost_num_threads, "i", int, 0444,
67                 "number of OST service threads to start (deprecated)");
68
69 static int oss_num_create_threads;
70 CFS_MODULE_PARM(oss_num_create_threads, "i", int, 0444,
71                 "number of OSS create threads to start");
72
73 static char *oss_cpts;
74 CFS_MODULE_PARM(oss_cpts, "s", charp, 0444,
75                 "CPU partitions OSS threads should run on");
76
77 static char *oss_io_cpts;
78 CFS_MODULE_PARM(oss_io_cpts, "s", charp, 0444,
79                 "CPU partitions OSS IO threads should run on");
80
81 /*
82  * this page is allocated statically when module is initializing
83  * it is used to simulate data corruptions, see ost_checksum_bulk()
84  * for details. as the original pages provided by the layers below
85  * can be remain in the internal cache, we do not want to modify
86  * them.
87  */
88 static struct page *ost_page_to_corrupt = NULL;
89
90 /**
91  * Do not return server-side uid/gid to remote client
92  */
93 static void ost_drop_id(struct obd_export *exp, struct obdo *oa)
94 {
95         if (exp_connect_rmtclient(exp)) {
96                 oa->o_uid = -1;
97                 oa->o_gid = -1;
98                 oa->o_valid &= ~(OBD_MD_FLUID | OBD_MD_FLGID);
99         }
100 }
101
102 /**
103  * Validate oa from client.
104  * If the request comes from 2.0 clients, currently only RSVD seq and IDIF
105  * req are valid.
106  *    a. for single MDS  seq = FID_SEQ_OST_MDT0,
107  *    b. for CMD, seq = FID_SEQ_OST_MDT0, FID_SEQ_OST_MDT1 - FID_SEQ_OST_MAX
108  */
109 static int ost_validate_obdo(struct obd_export *exp, struct obdo *oa,
110                              struct obd_ioobj *ioobj)
111 {
112         if (unlikely(oa != NULL && !(oa->o_valid & OBD_MD_FLGROUP))) {
113                 oa->o_seq = FID_SEQ_OST_MDT0;
114                 if (ioobj)
115                         ioobj->ioo_seq = FID_SEQ_OST_MDT0;
116         } else if (unlikely(oa == NULL || !(fid_seq_is_idif(oa->o_seq) ||
117                                             fid_seq_is_mdt(oa->o_seq) ||
118                                             fid_seq_is_echo(oa->o_seq)))) {
119                 CERROR("%s: client %s sent bad object "POSTID": rc = -EPROTO\n",
120                        exp->exp_obd->obd_name, obd_export_nid2str(exp),
121                        oa ? oa->o_id : -1, oa ? oa->o_seq : -1);
122                 return -EPROTO;
123         }
124
125         obdo_from_ostid(oa, &oa->o_oi);
126         if (ioobj != NULL) {
127                 unsigned max_brw = ioobj_max_brw_get(ioobj);
128
129                 if (unlikely((max_brw & (max_brw - 1)) != 0)) {
130                         CERROR("%s: client %s sent bad ioobj max %u for "POSTID
131                                ": rc = -EPROTO\n", exp->exp_obd->obd_name,
132                                obd_export_nid2str(exp), max_brw,
133                                oa->o_id, oa->o_seq);
134                         return -EPROTO;
135                 }
136                 ioobj_from_obdo(ioobj, oa);
137         }
138         return 0;
139 }
140
141 void oti_to_request(struct obd_trans_info *oti, struct ptlrpc_request *req)
142 {
143         struct oti_req_ack_lock *ack_lock;
144         int i;
145
146         if (oti == NULL)
147                 return;
148
149         if (req->rq_repmsg) {
150                 __u64 versions[PTLRPC_NUM_VERSIONS] = { 0 };
151                 lustre_msg_set_transno(req->rq_repmsg, oti->oti_transno);
152                 versions[0] = oti->oti_pre_version;
153                 lustre_msg_set_versions(req->rq_repmsg, versions);
154         }
155         req->rq_transno = oti->oti_transno;
156
157         /* XXX 4 == entries in oti_ack_locks??? */
158         for (ack_lock = oti->oti_ack_locks, i = 0; i < 4; i++, ack_lock++) {
159                 if (!ack_lock->mode)
160                         break;
161                 /* XXX not even calling target_send_reply in some cases... */
162                 ptlrpc_save_lock (req, &ack_lock->lock, ack_lock->mode, 0);
163         }
164 }
165
166 static int ost_destroy(struct obd_export *exp, struct ptlrpc_request *req,
167                        struct obd_trans_info *oti)
168 {
169         struct ost_body *body, *repbody;
170         struct lustre_capa *capa = NULL;
171         int rc;
172         ENTRY;
173
174         /* Get the request body */
175         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
176         if (body == NULL)
177                 RETURN(-EFAULT);
178
179         if (body->oa.o_id == 0)
180                 RETURN(-EPROTO);
181
182         rc = ost_validate_obdo(exp, &body->oa, NULL);
183         if (rc)
184                 RETURN(rc);
185
186         /* If there's a DLM request, cancel the locks mentioned in it*/
187         if (req_capsule_field_present(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT)) {
188                 struct ldlm_request *dlm;
189
190                 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
191                 if (dlm == NULL)
192                         RETURN (-EFAULT);
193                 ldlm_request_cancel(req, dlm, 0);
194         }
195
196         /* If there's a capability, get it */
197         if (body->oa.o_valid & OBD_MD_FLOSSCAPA) {
198                 capa = req_capsule_client_get(&req->rq_pill, &RMF_CAPA1);
199                 if (capa == NULL) {
200                         CERROR("Missing capability for OST DESTROY");
201                         RETURN (-EFAULT);
202                 }
203         }
204
205         /* Prepare the reply */
206         rc = req_capsule_server_pack(&req->rq_pill);
207         if (rc)
208                 RETURN(rc);
209
210         /* Get the log cancellation cookie */
211         if (body->oa.o_valid & OBD_MD_FLCOOKIE)
212                 oti->oti_logcookies = &body->oa.o_lcookie;
213
214         /* Finish the reply */
215         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
216         memcpy(&repbody->oa, &body->oa, sizeof(body->oa));
217
218         /* Do the destroy and set the reply status accordingly  */
219         req->rq_status = obd_destroy(req->rq_svc_thread->t_env, exp,
220                                      &repbody->oa, NULL, oti, NULL, capa);
221         RETURN(0);
222 }
223
224 /**
225  * Helper function for getting server side [start, start+count] DLM lock
226  * if asked by client.
227  */
228 static int ost_lock_get(struct obd_export *exp, struct obdo *oa,
229                         __u64 start, __u64 count, struct lustre_handle *lh,
230                         int mode, __u64 flags)
231 {
232         struct ldlm_res_id res_id;
233         ldlm_policy_data_t policy;
234         __u64 end = start + count;
235
236         ENTRY;
237
238         LASSERT(!lustre_handle_is_used(lh));
239         /* o_id and o_gr are used for localizing resource, if client miss to set
240          * them, do not trigger ASSERTION. */
241         if (unlikely((oa->o_valid & (OBD_MD_FLID | OBD_MD_FLGROUP)) !=
242                      (OBD_MD_FLID | OBD_MD_FLGROUP)))
243                 RETURN(-EPROTO);
244
245         if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
246             !(oa->o_flags & OBD_FL_SRVLOCK))
247                 RETURN(0);
248
249         ostid_build_res_name(&oa->o_oi, &res_id);
250         CDEBUG(D_INODE, "OST-side extent lock.\n");
251
252         policy.l_extent.start = start & CFS_PAGE_MASK;
253
254         /* If ->o_blocks is EOF it means "lock till the end of the
255          * file". Otherwise, it's size of a hole being punched (in bytes) */
256         if (count == OBD_OBJECT_EOF || end < start)
257                 policy.l_extent.end = OBD_OBJECT_EOF;
258         else
259                 policy.l_extent.end = end | ~CFS_PAGE_MASK;
260
261         RETURN(ldlm_cli_enqueue_local(exp->exp_obd->obd_namespace, &res_id,
262                                       LDLM_EXTENT, &policy, mode, &flags,
263                                       ldlm_blocking_ast, ldlm_completion_ast,
264                                       ldlm_glimpse_ast, NULL, 0, LVB_T_NONE,
265                                       NULL, lh));
266 }
267
268 /* Helper function: release lock, if any. */
269 static void ost_lock_put(struct obd_export *exp,
270                          struct lustre_handle *lh, int mode)
271 {
272         ENTRY;
273         if (lustre_handle_is_used(lh))
274                 ldlm_lock_decref(lh, mode);
275         EXIT;
276 }
277
278 static int ost_getattr(struct obd_export *exp, struct ptlrpc_request *req)
279 {
280         struct ost_body *body, *repbody;
281         struct obd_info *oinfo;
282         struct lustre_handle lh = { 0 };
283         struct lustre_capa *capa = NULL;
284         int rc;
285         ENTRY;
286
287         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
288         if (body == NULL)
289                 RETURN(-EFAULT);
290
291         rc = ost_validate_obdo(exp, &body->oa, NULL);
292         if (rc)
293                 RETURN(rc);
294
295         if (body->oa.o_valid & OBD_MD_FLOSSCAPA) {
296                 capa = req_capsule_client_get(&req->rq_pill, &RMF_CAPA1);
297                 if (capa == NULL) {
298                         CERROR("Missing capability for OST GETATTR");
299                         RETURN(-EFAULT);
300                 }
301         }
302
303         rc = req_capsule_server_pack(&req->rq_pill);
304         if (rc)
305                 RETURN(rc);
306
307         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
308         repbody->oa = body->oa;
309
310         rc = ost_lock_get(exp, &repbody->oa, 0, OBD_OBJECT_EOF, &lh, LCK_PR, 0);
311         if (rc)
312                 RETURN(rc);
313
314         OBD_ALLOC_PTR(oinfo);
315         if (!oinfo)
316                 GOTO(unlock, rc = -ENOMEM);
317         oinfo->oi_oa = &repbody->oa;
318         oinfo->oi_capa = capa;
319
320         req->rq_status = obd_getattr(req->rq_svc_thread->t_env, exp, oinfo);
321
322         OBD_FREE_PTR(oinfo);
323
324         ost_drop_id(exp, &repbody->oa);
325
326 unlock:
327         ost_lock_put(exp, &lh, LCK_PR);
328         RETURN(rc);
329 }
330
331 static int ost_statfs(struct ptlrpc_request *req)
332 {
333         struct obd_statfs *osfs;
334         int rc;
335         ENTRY;
336
337         rc = req_capsule_server_pack(&req->rq_pill);
338         if (rc)
339                 RETURN(rc);
340
341         osfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
342
343         req->rq_status = obd_statfs(req->rq_svc_thread->t_env, req->rq_export,
344                                     osfs,
345                                     cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
346                                     0);
347         if (req->rq_status != 0)
348                 CERROR("ost: statfs failed: rc %d\n", req->rq_status);
349
350         if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_EINPROGRESS))
351                 req->rq_status = -EINPROGRESS;
352
353         RETURN(0);
354 }
355
356 static int ost_create(struct obd_export *exp, struct ptlrpc_request *req,
357                       struct obd_trans_info *oti)
358 {
359         struct ost_body *body, *repbody;
360         int rc;
361         ENTRY;
362
363         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
364         if (body == NULL)
365                 RETURN(-EFAULT);
366
367         rc = ost_validate_obdo(req->rq_export, &body->oa, NULL);
368         if (rc)
369                 RETURN(rc);
370
371         rc = req_capsule_server_pack(&req->rq_pill);
372         if (rc)
373                 RETURN(rc);
374
375         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
376         repbody->oa = body->oa;
377         oti->oti_logcookies = &body->oa.o_lcookie;
378
379         req->rq_status = obd_create(req->rq_svc_thread->t_env, exp,
380                                     &repbody->oa, NULL, oti);
381         //obd_log_cancel(conn, NULL, 1, oti->oti_logcookies, 0);
382         RETURN(0);
383 }
384
385 static int ost_punch(struct obd_export *exp, struct ptlrpc_request *req,
386                      struct obd_trans_info *oti)
387 {
388         struct ost_body *body, *repbody;
389         __u64 flags = 0;
390         struct lustre_handle lh = {0,};
391         int rc;
392         ENTRY;
393
394         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
395         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
396
397         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
398         if (body == NULL)
399                 RETURN(-EFAULT);
400
401         rc = ost_validate_obdo(exp, &body->oa, NULL);
402         if (rc)
403                 RETURN(rc);
404
405         if ((body->oa.o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
406             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
407                 RETURN(-EPROTO);
408
409         rc = req_capsule_server_pack(&req->rq_pill);
410         if (rc)
411                 RETURN(rc);
412
413         /* standard truncate optimization: if file body is completely
414          * destroyed, don't send data back to the server. */
415         if (body->oa.o_size == 0)
416                 flags |= LDLM_AST_DISCARD_DATA;
417
418         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
419         repbody->oa = body->oa;
420
421         rc = ost_lock_get(exp, &repbody->oa, repbody->oa.o_size,
422                           repbody->oa.o_blocks, &lh, LCK_PW, flags);
423         if (rc == 0) {
424                 struct obd_info *oinfo;
425                 struct lustre_capa *capa = NULL;
426
427                 if (repbody->oa.o_valid & OBD_MD_FLFLAGS &&
428                     repbody->oa.o_flags == OBD_FL_SRVLOCK)
429                         /*
430                          * If OBD_FL_SRVLOCK is the only bit set in
431                          * ->o_flags, clear OBD_MD_FLFLAGS to avoid falling
432                          * through filter_setattr() to filter_iocontrol().
433                          */
434                         repbody->oa.o_valid &= ~OBD_MD_FLFLAGS;
435
436                 if (repbody->oa.o_valid & OBD_MD_FLOSSCAPA) {
437                         capa = req_capsule_client_get(&req->rq_pill,
438                                                       &RMF_CAPA1);
439                         if (capa == NULL) {
440                                 CERROR("Missing capability for OST PUNCH");
441                                 GOTO(unlock, rc = -EFAULT);
442                         }
443                 }
444
445                 OBD_ALLOC_PTR(oinfo);
446                 if (!oinfo)
447                         GOTO(unlock, rc = -ENOMEM);
448                 oinfo->oi_oa = &repbody->oa;
449                 oinfo->oi_policy.l_extent.start = oinfo->oi_oa->o_size;
450                 oinfo->oi_policy.l_extent.end = oinfo->oi_oa->o_blocks;
451                 oinfo->oi_capa = capa;
452                 oinfo->oi_flags = OBD_FL_PUNCH;
453
454                 req->rq_status = obd_punch(req->rq_svc_thread->t_env, exp,
455                                            oinfo, oti, NULL);
456                 OBD_FREE_PTR(oinfo);
457 unlock:
458                 ost_lock_put(exp, &lh, LCK_PW);
459         }
460
461         ost_drop_id(exp, &repbody->oa);
462         RETURN(rc);
463 }
464
465 static int ost_sync(struct obd_export *exp, struct ptlrpc_request *req,
466                     struct obd_trans_info *oti)
467 {
468         struct ost_body *body, *repbody;
469         struct obd_info *oinfo;
470         struct lustre_capa *capa = NULL;
471         int rc;
472         ENTRY;
473
474         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
475         if (body == NULL)
476                 RETURN(-EFAULT);
477
478         rc = ost_validate_obdo(exp, &body->oa, NULL);
479         if (rc)
480                 RETURN(rc);
481
482         if (body->oa.o_valid & OBD_MD_FLOSSCAPA) {
483                 capa = req_capsule_client_get(&req->rq_pill, &RMF_CAPA1);
484                 if (capa == NULL) {
485                         CERROR("Missing capability for OST SYNC");
486                         RETURN (-EFAULT);
487                 }
488         }
489
490         rc = req_capsule_server_pack(&req->rq_pill);
491         if (rc)
492                 RETURN(rc);
493
494         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
495         repbody->oa = body->oa;
496
497         OBD_ALLOC_PTR(oinfo);
498         if (!oinfo)
499                 RETURN(-ENOMEM);
500
501         oinfo->oi_oa = &repbody->oa;
502         oinfo->oi_capa = capa;
503         oinfo->oi_jobid = oti->oti_jobid;
504         req->rq_status = obd_sync(req->rq_svc_thread->t_env, exp, oinfo,
505                                   repbody->oa.o_size, repbody->oa.o_blocks,
506                                   NULL);
507         OBD_FREE_PTR(oinfo);
508
509         ost_drop_id(exp, &repbody->oa);
510         RETURN(0);
511 }
512
513 static int ost_setattr(struct obd_export *exp, struct ptlrpc_request *req,
514                        struct obd_trans_info *oti)
515 {
516         struct ost_body *body, *repbody;
517         struct obd_info *oinfo;
518         struct lustre_capa *capa = NULL;
519         int rc;
520         ENTRY;
521
522         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
523         if (body == NULL)
524                 RETURN(-EFAULT);
525
526         rc = ost_validate_obdo(req->rq_export, &body->oa, NULL);
527         if (rc)
528                 RETURN(rc);
529
530         rc = req_capsule_server_pack(&req->rq_pill);
531         if (rc)
532                 RETURN(rc);
533
534         if (body->oa.o_valid & OBD_MD_FLOSSCAPA) {
535                 capa = req_capsule_client_get(&req->rq_pill, &RMF_CAPA1);
536                 if (capa == NULL) {
537                         CERROR("Missing capability for OST SETATTR");
538                         RETURN (-EFAULT);
539                 }
540         }
541
542         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
543         repbody->oa = body->oa;
544
545         OBD_ALLOC_PTR(oinfo);
546         if (!oinfo)
547                 RETURN(-ENOMEM);
548         oinfo->oi_oa = &repbody->oa;
549         oinfo->oi_capa = capa;
550
551         req->rq_status = obd_setattr(req->rq_svc_thread->t_env, exp, oinfo,
552                                      oti);
553
554         OBD_FREE_PTR(oinfo);
555
556         ost_drop_id(exp, &repbody->oa);
557         RETURN(0);
558 }
559
560 static __u32 ost_checksum_bulk(struct ptlrpc_bulk_desc *desc, int opc,
561                                cksum_type_t cksum_type)
562 {
563         struct cfs_crypto_hash_desc     *hdesc;
564         unsigned int                    bufsize;
565         int                             i, err;
566         unsigned char                   cfs_alg = cksum_obd2cfs(cksum_type);
567         __u32                           cksum;
568
569         hdesc = cfs_crypto_hash_init(cfs_alg, NULL, 0);
570         if (IS_ERR(hdesc)) {
571                 CERROR("Unable to initialize checksum hash %s\n",
572                        cfs_crypto_hash_name(cfs_alg));
573                 return PTR_ERR(hdesc);
574         }
575         CDEBUG(D_INFO, "Checksum for algo %s\n", cfs_crypto_hash_name(cfs_alg));
576         for (i = 0; i < desc->bd_iov_count; i++) {
577
578                 /* corrupt the data before we compute the checksum, to
579                  * simulate a client->OST data error */
580                 if (i == 0 && opc == OST_WRITE &&
581                     OBD_FAIL_CHECK(OBD_FAIL_OST_CHECKSUM_RECEIVE)) {
582                         int off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
583                         int len = desc->bd_iov[i].kiov_len;
584                         struct page *np = ost_page_to_corrupt;
585                         char *ptr = kmap(desc->bd_iov[i].kiov_page) + off;
586
587                         if (np) {
588                                 char *ptr2 = kmap(np) + off;
589
590                                 memcpy(ptr2, ptr, len);
591                                 memcpy(ptr2, "bad3", min(4, len));
592                                 kunmap(np);
593                                 desc->bd_iov[i].kiov_page = np;
594                         } else {
595                                 CERROR("can't alloc page for corruption\n");
596                         }
597                 }
598                 cfs_crypto_hash_update_page(hdesc, desc->bd_iov[i].kiov_page,
599                                   desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK,
600                                   desc->bd_iov[i].kiov_len);
601
602                  /* corrupt the data after we compute the checksum, to
603                  * simulate an OST->client data error */
604                 if (i == 0 && opc == OST_READ &&
605                     OBD_FAIL_CHECK(OBD_FAIL_OST_CHECKSUM_SEND)) {
606                         int off = desc->bd_iov[i].kiov_offset & ~CFS_PAGE_MASK;
607                         int len = desc->bd_iov[i].kiov_len;
608                         struct page *np = ost_page_to_corrupt;
609                         char *ptr = kmap(desc->bd_iov[i].kiov_page) + off;
610
611                         if (np) {
612                                 char *ptr2 = kmap(np) + off;
613
614                                 memcpy(ptr2, ptr, len);
615                                 memcpy(ptr2, "bad4", min(4, len));
616                                 kunmap(np);
617                                 desc->bd_iov[i].kiov_page = np;
618                         } else {
619                                 CERROR("can't alloc page for corruption\n");
620                         }
621                 }
622         }
623
624         bufsize = 4;
625         err = cfs_crypto_hash_final(hdesc, (unsigned char *)&cksum, &bufsize);
626         if (err)
627                 cfs_crypto_hash_final(hdesc, NULL, NULL);
628
629         return cksum;
630 }
631
632 static int ost_brw_lock_get(int mode, struct obd_export *exp,
633                             struct obd_ioobj *obj, struct niobuf_remote *nb,
634                             struct lustre_handle *lh)
635 {
636         __u64 flags               = 0;
637         int nrbufs                = obj->ioo_bufcnt;
638         struct ldlm_res_id res_id;
639         ldlm_policy_data_t policy;
640         int i;
641         ENTRY;
642
643         ostid_build_res_name(&obj->ioo_oid, &res_id);
644         LASSERT(mode == LCK_PR || mode == LCK_PW);
645         LASSERT(!lustre_handle_is_used(lh));
646
647         if (nrbufs == 0 || !(nb[0].flags & OBD_BRW_SRVLOCK))
648                 RETURN(0);
649
650         for (i = 1; i < nrbufs; i ++)
651                 if ((nb[0].flags & OBD_BRW_SRVLOCK) !=
652                     (nb[i].flags & OBD_BRW_SRVLOCK))
653                         RETURN(-EFAULT);
654
655         policy.l_extent.start = nb[0].offset & CFS_PAGE_MASK;
656         policy.l_extent.end   = (nb[nrbufs - 1].offset +
657                                  nb[nrbufs - 1].len - 1) | ~CFS_PAGE_MASK;
658
659         RETURN(ldlm_cli_enqueue_local(exp->exp_obd->obd_namespace, &res_id,
660                                       LDLM_EXTENT, &policy, mode, &flags,
661                                       ldlm_blocking_ast, ldlm_completion_ast,
662                                       ldlm_glimpse_ast, NULL, 0, LVB_T_NONE,
663                                       NULL, lh));
664 }
665
666 static void ost_brw_lock_put(int mode,
667                              struct obd_ioobj *obj, struct niobuf_remote *niob,
668                              struct lustre_handle *lh)
669 {
670         ENTRY;
671         LASSERT(mode == LCK_PR || mode == LCK_PW);
672         LASSERT((obj->ioo_bufcnt > 0 && (niob[0].flags & OBD_BRW_SRVLOCK)) ==
673                 lustre_handle_is_used(lh));
674         if (lustre_handle_is_used(lh))
675                 ldlm_lock_decref(lh, mode);
676         EXIT;
677 }
678
679 /* Allocate thread local buffers if needed */
680 static struct ost_thread_local_cache *ost_tls_get(struct ptlrpc_request *r)
681 {
682         struct ost_thread_local_cache *tls =
683                 (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
684
685         /* In normal mode of operation an I/O request is serviced only
686          * by ll_ost_io threads each of them has own tls buffers allocated by
687          * ost_io_thread_init().
688          * During recovery, an I/O request may be queued until any of the ost
689          * service threads process it. Not necessary it should be one of
690          * ll_ost_io threads. In that case we dynamically allocating tls
691          * buffers for the request service time. */
692         if (unlikely(tls == NULL)) {
693                 LASSERT(r->rq_export->exp_in_recovery);
694                 OBD_ALLOC_PTR(tls);
695                 if (tls != NULL) {
696                         tls->temporary = 1;
697                         r->rq_svc_thread->t_data = tls;
698                 }
699         }
700         return  tls;
701 }
702
703 /* Free thread local buffers if they were allocated only for servicing
704  * this one request */
705 static void ost_tls_put(struct ptlrpc_request *r)
706 {
707         struct ost_thread_local_cache *tls =
708                 (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
709
710         if (unlikely(tls->temporary)) {
711                 OBD_FREE_PTR(tls);
712                 r->rq_svc_thread->t_data = NULL;
713         }
714 }
715
716 static int ost_brw_read(struct ptlrpc_request *req, struct obd_trans_info *oti)
717 {
718         struct ptlrpc_bulk_desc *desc = NULL;
719         struct obd_export *exp = req->rq_export;
720         struct niobuf_remote *remote_nb;
721         struct niobuf_local *local_nb;
722         struct obd_ioobj *ioo;
723         struct ost_body *body, *repbody;
724         struct lustre_capa *capa = NULL;
725         struct l_wait_info lwi;
726         struct lustre_handle lockh = { 0 };
727         int niocount, npages, nob = 0, rc, i;
728         int no_reply = 0;
729         struct ost_thread_local_cache *tls;
730         ENTRY;
731
732         req->rq_bulk_read = 1;
733
734         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_READ_BULK))
735                 GOTO(out, rc = -EIO);
736
737         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, (obd_timeout + 1) / 4);
738
739         /* Check if there is eviction in progress, and if so, wait for it to
740          * finish */
741         if (unlikely(cfs_atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
742                 lwi = LWI_INTR(NULL, NULL); // We do not care how long it takes
743                 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
744                         !cfs_atomic_read(&exp->exp_obd->obd_evict_inprogress),
745                         &lwi);
746         }
747         if (exp->exp_failed)
748                 GOTO(out, rc = -ENOTCONN);
749
750         /* ost_body, ioobj & noibuf_remote are verified and swabbed in
751          * ost_rw_hpreq_check(). */
752         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
753         if (body == NULL)
754                 GOTO(out, rc = -EFAULT);
755
756         /*
757          * A req_capsule_X_get_array(pill, field, ptr_to_element_count) function
758          * would be useful here and wherever we get &RMF_OBD_IOOBJ and
759          * &RMF_NIOBUF_REMOTE.
760          */
761         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
762         if (ioo == NULL)
763                 GOTO(out, rc = -EFAULT);
764
765         rc = ost_validate_obdo(exp, &body->oa, ioo);
766         if (rc)
767                 RETURN(rc);
768
769         niocount = ioo->ioo_bufcnt;
770         remote_nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
771         if (remote_nb == NULL)
772                 GOTO(out, rc = -EFAULT);
773
774         if (body->oa.o_valid & OBD_MD_FLOSSCAPA) {
775                 capa = req_capsule_client_get(&req->rq_pill, &RMF_CAPA1);
776                 if (capa == NULL) {
777                         CERROR("Missing capability for OST BRW READ");
778                         GOTO(out, rc = -EFAULT);
779                 }
780         }
781
782         rc = req_capsule_server_pack(&req->rq_pill);
783         if (rc)
784                 GOTO(out, rc);
785
786         tls = ost_tls_get(req);
787         if (tls == NULL)
788                 GOTO(out_bulk, rc = -ENOMEM);
789         local_nb = tls->local;
790
791         rc = ost_brw_lock_get(LCK_PR, exp, ioo, remote_nb, &lockh);
792         if (rc != 0)
793                 GOTO(out_tls, rc);
794
795         /*
796          * If getting the lock took more time than
797          * client was willing to wait, drop it. b=11330
798          */
799         if (cfs_time_current_sec() > req->rq_deadline ||
800             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
801                 no_reply = 1;
802                 CERROR("Dropping timed-out read from %s because locking"
803                        "object "LPX64" took %ld seconds (limit was %ld).\n",
804                        libcfs_id2str(req->rq_peer), ioo->ioo_id,
805                        cfs_time_current_sec() - req->rq_arrival_time.tv_sec,
806                        req->rq_deadline - req->rq_arrival_time.tv_sec);
807                 GOTO(out_lock, rc = -ETIMEDOUT);
808         }
809
810         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
811         memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
812
813         npages = OST_THREAD_POOL_SIZE;
814         rc = obd_preprw(req->rq_svc_thread->t_env, OBD_BRW_READ, exp,
815                         &repbody->oa, 1, ioo, remote_nb, &npages, local_nb,
816                         oti, capa);
817         if (rc != 0)
818                 GOTO(out_lock, rc);
819
820         desc = ptlrpc_prep_bulk_exp(req, npages, ioobj_max_brw_get(ioo),
821                                     BULK_PUT_SOURCE, OST_BULK_PORTAL);
822         if (desc == NULL)
823                 GOTO(out_commitrw, rc = -ENOMEM);
824
825         nob = 0;
826         for (i = 0; i < npages; i++) {
827                 int page_rc = local_nb[i].rc;
828
829                 if (page_rc < 0) {              /* error */
830                         rc = page_rc;
831                         break;
832                 }
833
834                 nob += page_rc;
835                 if (page_rc != 0) {             /* some data! */
836                         LASSERT (local_nb[i].page != NULL);
837                         ptlrpc_prep_bulk_page_nopin(desc, local_nb[i].page,
838                                                     local_nb[i].lnb_page_offset,
839                                                     page_rc);
840                 }
841
842                 if (page_rc != local_nb[i].len) { /* short read */
843                         /* All subsequent pages should be 0 */
844                         while(++i < npages)
845                                 LASSERT(local_nb[i].rc == 0);
846                         break;
847                 }
848         }
849
850         if (body->oa.o_valid & OBD_MD_FLCKSUM) {
851                 cksum_type_t cksum_type =
852                         cksum_type_unpack(repbody->oa.o_valid & OBD_MD_FLFLAGS ?
853                                           repbody->oa.o_flags : 0);
854                 repbody->oa.o_flags = cksum_type_pack(cksum_type);
855                 repbody->oa.o_valid = OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
856                 repbody->oa.o_cksum = ost_checksum_bulk(desc, OST_READ,cksum_type);
857                 CDEBUG(D_PAGE, "checksum at read origin: %x\n",
858                        repbody->oa.o_cksum);
859         } else {
860                 repbody->oa.o_valid = 0;
861         }
862         /* We're finishing using body->oa as an input variable */
863
864         /* Check if client was evicted while we were doing i/o before touching
865            network */
866         if (rc == 0) {
867                 if (likely(!CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CLIENT_BULK_CB2)))
868                         rc = target_bulk_io(exp, desc, &lwi);
869                 no_reply = rc != 0;
870         }
871
872 out_commitrw:
873         /* Must commit after prep above in all cases */
874         rc = obd_commitrw(req->rq_svc_thread->t_env, OBD_BRW_READ, exp,
875                           &repbody->oa, 1, ioo, remote_nb, npages, local_nb,
876                           oti, rc);
877
878         if (rc == 0)
879                 ost_drop_id(exp, &repbody->oa);
880
881 out_lock:
882         ost_brw_lock_put(LCK_PR, ioo, remote_nb, &lockh);
883 out_tls:
884         ost_tls_put(req);
885 out_bulk:
886         if (desc && !CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CLIENT_BULK_CB2))
887                 ptlrpc_free_bulk_nopin(desc);
888 out:
889         LASSERT(rc <= 0);
890         if (rc == 0) {
891                 req->rq_status = nob;
892                 ptlrpc_lprocfs_brw(req, nob);
893                 target_committed_to_req(req);
894                 ptlrpc_reply(req);
895         } else if (!no_reply) {
896                 /* Only reply if there was no comms problem with bulk */
897                 target_committed_to_req(req);
898                 req->rq_status = rc;
899                 ptlrpc_error(req);
900         } else {
901                 /* reply out callback would free */
902                 ptlrpc_req_drop_rs(req);
903                 LCONSOLE_WARN("%s: Bulk IO read error with %s (at %s), "
904                               "client will retry: rc %d\n",
905                               exp->exp_obd->obd_name,
906                               obd_uuid2str(&exp->exp_client_uuid),
907                               obd_export_nid2str(exp), rc);
908         }
909         /* send a bulk after reply to simulate a network delay or reordering
910          * by a router */
911         if (unlikely(CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CLIENT_BULK_CB2))) {
912                 cfs_waitq_t              waitq;
913                 struct l_wait_info       lwi1;
914
915                 CDEBUG(D_INFO, "reorder BULK\n");
916                 cfs_waitq_init(&waitq);
917
918                 lwi1 = LWI_TIMEOUT_INTR(cfs_time_seconds(3), NULL, NULL, NULL);
919                 l_wait_event(waitq, 0, &lwi1);
920                 rc = target_bulk_io(exp, desc, &lwi);
921                 ptlrpc_free_bulk_nopin(desc);
922         }
923
924         RETURN(rc);
925 }
926
927 static void ost_warn_on_cksum(struct ptlrpc_request *req,
928                               struct ptlrpc_bulk_desc *desc,
929                               struct niobuf_local *local_nb, int npages,
930                               obd_count client_cksum, obd_count server_cksum,
931                               int mmap)
932 {
933         struct obd_export *exp = req->rq_export;
934         struct ost_body *body;
935         char *router;
936         char *via;
937
938         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
939         LASSERT (body != NULL);
940
941         if (req->rq_peer.nid == desc->bd_sender) {
942                 via = router = "";
943         } else {
944                 via = " via ";
945                 router = libcfs_nid2str(desc->bd_sender);
946         }
947
948         if (mmap) {
949                 CDEBUG_LIMIT(D_INFO, "client csum %x, server csum %x\n",
950                              client_cksum, server_cksum);
951                 return;
952         }
953
954         LCONSOLE_ERROR_MSG(0x168, "BAD WRITE CHECKSUM: %s from %s%s%s inode "
955                            DFID" object "LPU64"/"LPU64" extent ["LPU64"-"LPU64
956                            "]: client csum %x, server csum %x\n",
957                            exp->exp_obd->obd_name, libcfs_id2str(req->rq_peer),
958                            via, router,
959                            body->oa.o_valid & OBD_MD_FLFID ?
960                            body->oa.o_parent_seq : (__u64)0,
961                            body->oa.o_valid & OBD_MD_FLFID ?
962                            body->oa.o_parent_oid : 0,
963                            body->oa.o_valid & OBD_MD_FLFID ?
964                            body->oa.o_parent_ver : 0,
965                            body->oa.o_id,
966                            body->oa.o_valid & OBD_MD_FLGROUP ?
967                            body->oa.o_seq : (__u64)0,
968                            local_nb[0].lnb_file_offset,
969                            local_nb[npages-1].lnb_file_offset +
970                            local_nb[npages-1].len - 1,
971                            client_cksum, server_cksum);
972 }
973
974 static int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
975 {
976         struct ptlrpc_bulk_desc *desc = NULL;
977         struct obd_export       *exp = req->rq_export;
978         struct niobuf_remote    *remote_nb;
979         struct niobuf_local     *local_nb;
980         struct obd_ioobj        *ioo;
981         struct ost_body         *body, *repbody;
982         struct l_wait_info       lwi;
983         struct lustre_handle     lockh = {0};
984         struct lustre_capa      *capa = NULL;
985         __u32                   *rcs;
986         int objcount, niocount, npages;
987         int rc, i, j;
988         obd_count                client_cksum = 0, server_cksum = 0;
989         cksum_type_t             cksum_type = OBD_CKSUM_CRC32;
990         int                      no_reply = 0, mmap = 0;
991         __u32                    o_uid = 0, o_gid = 0;
992         struct ost_thread_local_cache *tls;
993         ENTRY;
994
995         req->rq_bulk_write = 1;
996
997         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
998                 GOTO(out, rc = -EIO);
999         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK2))
1000                 GOTO(out, rc = -EFAULT);
1001
1002         /* pause before transaction has been started */
1003         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, (obd_timeout + 1) / 4);
1004
1005         /* ost_body, ioobj & noibuf_remote are verified and swabbed in
1006          * ost_rw_hpreq_check(). */
1007         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1008         if (body == NULL)
1009                 GOTO(out, rc = -EFAULT);
1010
1011         objcount = req_capsule_get_size(&req->rq_pill, &RMF_OBD_IOOBJ,
1012                                         RCL_CLIENT) / sizeof(*ioo);
1013         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1014         if (ioo == NULL)
1015                 GOTO(out, rc = -EFAULT);
1016
1017         rc = ost_validate_obdo(exp, &body->oa, ioo);
1018         if (rc)
1019                 RETURN(rc);
1020
1021         for (niocount = i = 0; i < objcount; i++)
1022                 niocount += ioo[i].ioo_bufcnt;
1023
1024         /*
1025          * It'd be nice to have a capsule function to indicate how many elements
1026          * there were in a buffer for an RMF that's declared to be an array.
1027          * It's easy enough to compute the number of elements here though.
1028          */
1029         remote_nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1030         if (remote_nb == NULL || niocount != (req_capsule_get_size(&req->rq_pill,
1031             &RMF_NIOBUF_REMOTE, RCL_CLIENT) / sizeof(*remote_nb)))
1032                 GOTO(out, rc = -EFAULT);
1033
1034         if ((remote_nb[0].flags & OBD_BRW_MEMALLOC) &&
1035             (exp->exp_connection->c_peer.nid == exp->exp_connection->c_self))
1036                 cfs_memory_pressure_set();
1037
1038         if (body->oa.o_valid & OBD_MD_FLOSSCAPA) {
1039                 capa = req_capsule_client_get(&req->rq_pill, &RMF_CAPA1);
1040                 if (capa == NULL) {
1041                         CERROR("Missing capability for OST BRW WRITE");
1042                         GOTO(out, rc = -EFAULT);
1043                 }
1044         }
1045
1046         req_capsule_set_size(&req->rq_pill, &RMF_RCS, RCL_SERVER,
1047                              niocount * sizeof(*rcs));
1048         rc = req_capsule_server_pack(&req->rq_pill);
1049         if (rc != 0)
1050                 GOTO(out, rc);
1051         CFS_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_PACK, cfs_fail_val);
1052         rcs = req_capsule_server_get(&req->rq_pill, &RMF_RCS);
1053
1054         tls = ost_tls_get(req);
1055         if (tls == NULL)
1056                 GOTO(out_bulk, rc = -ENOMEM);
1057         local_nb = tls->local;
1058
1059         rc = ost_brw_lock_get(LCK_PW, exp, ioo, remote_nb, &lockh);
1060         if (rc != 0)
1061                 GOTO(out_tls, rc);
1062
1063         /*
1064          * If getting the lock took more time than
1065          * client was willing to wait, drop it. b=11330
1066          */
1067         if (cfs_time_current_sec() > req->rq_deadline ||
1068             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
1069                 no_reply = 1;
1070                 CERROR("Dropping timed-out write from %s because locking "
1071                        "object "LPX64" took %ld seconds (limit was %ld).\n",
1072                        libcfs_id2str(req->rq_peer), ioo->ioo_id,
1073                        cfs_time_current_sec() - req->rq_arrival_time.tv_sec,
1074                        req->rq_deadline - req->rq_arrival_time.tv_sec);
1075                 GOTO(out_lock, rc = -ETIMEDOUT);
1076         }
1077
1078         /* obd_preprw clobbers oa->valid, so save what we need */
1079         if (body->oa.o_valid & OBD_MD_FLCKSUM) {
1080                 client_cksum = body->oa.o_cksum;
1081                 if (body->oa.o_valid & OBD_MD_FLFLAGS)
1082                         cksum_type = cksum_type_unpack(body->oa.o_flags);
1083         }
1084         if (body->oa.o_valid & OBD_MD_FLFLAGS && body->oa.o_flags & OBD_FL_MMAP)
1085                 mmap = 1;
1086
1087         /* Because we already sync grant info with client when reconnect,
1088          * grant info will be cleared for resent req, then fed_grant and
1089          * total_grant will not be modified in following preprw_write */
1090         if (lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY)) {
1091                 DEBUG_REQ(D_CACHE, req, "clear resent/replay req grant info");
1092                 body->oa.o_valid &= ~OBD_MD_FLGRANT;
1093         }
1094
1095         if (exp_connect_rmtclient(exp)) {
1096                 o_uid = body->oa.o_uid;
1097                 o_gid = body->oa.o_gid;
1098         }
1099
1100         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
1101         memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
1102
1103         npages = OST_THREAD_POOL_SIZE;
1104         rc = obd_preprw(req->rq_svc_thread->t_env, OBD_BRW_WRITE, exp,
1105                         &repbody->oa, objcount, ioo, remote_nb, &npages,
1106                         local_nb, oti, capa);
1107         if (rc != 0)
1108                 GOTO(out_lock, rc);
1109
1110         desc = ptlrpc_prep_bulk_exp(req, npages, ioobj_max_brw_get(ioo),
1111                                     BULK_GET_SINK, OST_BULK_PORTAL);
1112         if (desc == NULL)
1113                 GOTO(skip_transfer, rc = -ENOMEM);
1114
1115         /* NB Having prepped, we must commit... */
1116         for (i = 0; i < npages; i++)
1117                 ptlrpc_prep_bulk_page_nopin(desc, local_nb[i].page,
1118                                             local_nb[i].lnb_page_offset,
1119                                             local_nb[i].len);
1120
1121         rc = sptlrpc_svc_prep_bulk(req, desc);
1122         if (rc != 0)
1123                 GOTO(out_lock, rc);
1124
1125         rc = target_bulk_io(exp, desc, &lwi);
1126         no_reply = rc != 0;
1127
1128 skip_transfer:
1129         if (client_cksum != 0 && rc == 0) {
1130                 static int cksum_counter;
1131                 repbody->oa.o_valid |= OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
1132                 repbody->oa.o_flags &= ~OBD_FL_CKSUM_ALL;
1133                 repbody->oa.o_flags |= cksum_type_pack(cksum_type);
1134                 server_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
1135                 repbody->oa.o_cksum = server_cksum;
1136                 cksum_counter++;
1137                 if (unlikely(client_cksum != server_cksum)) {
1138                         ost_warn_on_cksum(req, desc, local_nb, npages,
1139                                           client_cksum, server_cksum, mmap);
1140                         cksum_counter = 0;
1141
1142                 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
1143                         CDEBUG(D_INFO, "Checksum %u from %s OK: %x\n",
1144                                cksum_counter, libcfs_id2str(req->rq_peer),
1145                                server_cksum);
1146                 }
1147         }
1148
1149         /* Must commit after prep above in all cases */
1150         rc = obd_commitrw(req->rq_svc_thread->t_env, OBD_BRW_WRITE, exp,
1151                           &repbody->oa, objcount, ioo, remote_nb, npages,
1152                           local_nb, oti, rc);
1153         if (rc == -ENOTCONN)
1154                 /* quota acquire process has been given up because
1155                  * either the client has been evicted or the client
1156                  * has timed out the request already */
1157                 no_reply = 1;
1158
1159         if (exp_connect_rmtclient(exp)) {
1160                 repbody->oa.o_uid = o_uid;
1161                 repbody->oa.o_gid = o_gid;
1162         }
1163
1164         /*
1165          * Disable sending mtime back to the client. If the client locked the
1166          * whole object, then it has already updated the mtime on its side,
1167          * otherwise it will have to glimpse anyway (see bug 21489, comment 32)
1168          */
1169         repbody->oa.o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLATIME);
1170
1171         if (rc == 0) {
1172                 int nob = 0;
1173
1174                 /* set per-requested niobuf return codes */
1175                 for (i = j = 0; i < niocount; i++) {
1176                         int len = remote_nb[i].len;
1177
1178                         nob += len;
1179                         rcs[i] = 0;
1180                         do {
1181                                 LASSERT(j < npages);
1182                                 if (local_nb[j].rc < 0)
1183                                         rcs[i] = local_nb[j].rc;
1184                                 len -= local_nb[j].len;
1185                                 j++;
1186                         } while (len > 0);
1187                         LASSERT(len == 0);
1188                 }
1189                 LASSERT(j == npages);
1190                 ptlrpc_lprocfs_brw(req, nob);
1191         }
1192
1193 out_lock:
1194         ost_brw_lock_put(LCK_PW, ioo, remote_nb, &lockh);
1195 out_tls:
1196         ost_tls_put(req);
1197 out_bulk:
1198         if (desc)
1199                 ptlrpc_free_bulk_nopin(desc);
1200 out:
1201         if (rc == 0) {
1202                 oti_to_request(oti, req);
1203                 target_committed_to_req(req);
1204                 rc = ptlrpc_reply(req);
1205         } else if (!no_reply) {
1206                 /* Only reply if there was no comms problem with bulk */
1207                 target_committed_to_req(req);
1208                 req->rq_status = rc;
1209                 ptlrpc_error(req);
1210         } else {
1211                 /* reply out callback would free */
1212                 ptlrpc_req_drop_rs(req);
1213                 LCONSOLE_WARN("%s: Bulk IO write error with %s (at %s), "
1214                               "client will retry: rc %d\n",
1215                               exp->exp_obd->obd_name,
1216                               obd_uuid2str(&exp->exp_client_uuid),
1217                               obd_export_nid2str(exp), rc);
1218         }
1219         cfs_memory_pressure_clr();
1220         RETURN(rc);
1221 }
1222
1223 /**
1224  * Implementation of OST_SET_INFO.
1225  *
1226  * OST_SET_INFO is like ioctl(): heavily overloaded.  Specifically, it takes a
1227  * "key" and a value RPC buffers as arguments, with the value's contents
1228  * interpreted according to the key.
1229  *
1230  * Value types that need swabbing have swabbing done explicitly, either here or
1231  * in functions called from here.  This should be corrected: all swabbing should
1232  * be done in the capsule abstraction, as that will then allow us to move
1233  * swabbing exclusively to the client without having to modify server code
1234  * outside the capsule abstraction's implementation itself.  To correct this
1235  * will require minor changes to the capsule abstraction; see the comments for
1236  * req_capsule_extend() in layout.c.
1237  */
1238 static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
1239 {
1240         struct ost_body *body = NULL, *repbody;
1241         char *key, *val = NULL;
1242         int keylen, vallen, rc = 0;
1243         int is_grant_shrink = 0;
1244         ENTRY;
1245
1246         key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1247         if (key == NULL) {
1248                 DEBUG_REQ(D_HA, req, "no set_info key");
1249                 RETURN(-EFAULT);
1250         }
1251         keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
1252                                       RCL_CLIENT);
1253
1254         vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
1255                                       RCL_CLIENT);
1256
1257         if ((is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK)))
1258                 /* In this case the value is actually an RMF_OST_BODY, so we
1259                  * transmutate the type of this PTLRPC */
1260                 req_capsule_extend(&req->rq_pill, &RQF_OST_SET_GRANT_INFO);
1261
1262         rc = req_capsule_server_pack(&req->rq_pill);
1263         if (rc)
1264                 RETURN(rc);
1265
1266         if (vallen) {
1267                 if (is_grant_shrink) {
1268                         body = req_capsule_client_get(&req->rq_pill,
1269                                                       &RMF_OST_BODY);
1270                         if (!body)
1271                                 RETURN(-EFAULT);
1272
1273                         repbody = req_capsule_server_get(&req->rq_pill,
1274                                                          &RMF_OST_BODY);
1275                         memcpy(repbody, body, sizeof(*body));
1276                         val = (char*)repbody;
1277                 } else {
1278                         val = req_capsule_client_get(&req->rq_pill,
1279                                                      &RMF_SETINFO_VAL);
1280                 }
1281         }
1282
1283         if (KEY_IS(KEY_EVICT_BY_NID)) {
1284                 if (val && vallen)
1285                         obd_export_evict_by_nid(exp->exp_obd, val);
1286                 GOTO(out, rc = 0);
1287         } else if (KEY_IS(KEY_MDS_CONN) && ptlrpc_req_need_swab(req)) {
1288                 if (vallen < sizeof(__u32))
1289                         RETURN(-EFAULT);
1290                 __swab32s((__u32 *)val);
1291         }
1292
1293         /* OBD will also check if KEY_IS(KEY_GRANT_SHRINK), and will cast val to
1294          * a struct ost_body * value */
1295         rc = obd_set_info_async(req->rq_svc_thread->t_env, exp, keylen,
1296                                 key, vallen, val, NULL);
1297 out:
1298         lustre_msg_set_status(req->rq_repmsg, 0);
1299         RETURN(rc);
1300 }
1301
1302 static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
1303 {
1304         void *key, *reply;
1305         int keylen, replylen, rc = 0;
1306         struct req_capsule *pill = &req->rq_pill;
1307         ENTRY;
1308
1309         /* this common part for get_info rpc */
1310         key = req_capsule_client_get(pill, &RMF_SETINFO_KEY);
1311         if (key == NULL) {
1312                 DEBUG_REQ(D_HA, req, "no get_info key");
1313                 RETURN(-EFAULT);
1314         }
1315         keylen = req_capsule_get_size(pill, &RMF_SETINFO_KEY, RCL_CLIENT);
1316
1317         if (KEY_IS(KEY_FIEMAP)) {
1318                 struct ll_fiemap_info_key *fm_key = key;
1319                 int rc;
1320
1321                 rc = ost_validate_obdo(exp, &fm_key->oa, NULL);
1322                 if (rc)
1323                         RETURN(rc);
1324         }
1325
1326         rc = obd_get_info(req->rq_svc_thread->t_env, exp, keylen, key,
1327                           &replylen, NULL, NULL);
1328         if (rc)
1329                 RETURN(rc);
1330
1331         req_capsule_set_size(pill, &RMF_GENERIC_DATA,
1332                              RCL_SERVER, replylen);
1333
1334         rc = req_capsule_server_pack(pill);
1335         if (rc)
1336                 RETURN(rc);
1337
1338         reply = req_capsule_server_get(pill, &RMF_GENERIC_DATA);
1339         if (reply == NULL)
1340                 RETURN(-ENOMEM);
1341
1342         if (KEY_IS(KEY_LAST_FID)) {
1343                 void *val;
1344                 int vallen;
1345
1346                 req_capsule_extend(pill, &RQF_OST_GET_INFO_LAST_FID);
1347                 val = req_capsule_client_get(pill, &RMF_SETINFO_VAL);
1348                 vallen = req_capsule_get_size(pill, &RMF_SETINFO_VAL,
1349                                               RCL_CLIENT);
1350                 if (val != NULL && vallen > 0 && replylen >= vallen) {
1351                         memcpy(reply, val, vallen);
1352                 } else {
1353                         CERROR("%s: invalid req val %p vallen %d replylen %d\n",
1354                                exp->exp_obd->obd_name, val, vallen, replylen);
1355                         GOTO(out, rc = -EINVAL);
1356                 }
1357         }
1358
1359         /* call again to fill in the reply buffer */
1360         rc = obd_get_info(req->rq_svc_thread->t_env, exp, keylen, key,
1361                           &replylen, reply, NULL);
1362 out:
1363         lustre_msg_set_status(req->rq_repmsg, 0);
1364         RETURN(rc);
1365 }
1366
1367 static int ost_handle_quotactl(struct ptlrpc_request *req)
1368 {
1369         struct obd_quotactl *oqctl, *repoqc;
1370         int rc;
1371         ENTRY;
1372
1373         oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1374         if (oqctl == NULL)
1375                 GOTO(out, rc = -EPROTO);
1376
1377         rc = req_capsule_server_pack(&req->rq_pill);
1378         if (rc)
1379                 GOTO(out, rc);
1380
1381         repoqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1382         req->rq_status = obd_quotactl(req->rq_export, oqctl);
1383         *repoqc = *oqctl;
1384
1385 out:
1386         RETURN(rc);
1387 }
1388
1389 static int ost_handle_quotacheck(struct ptlrpc_request *req)
1390 {
1391         struct obd_quotactl *oqctl;
1392         int rc;
1393         ENTRY;
1394
1395         oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1396         if (oqctl == NULL)
1397                 RETURN(-EPROTO);
1398
1399         rc = req_capsule_server_pack(&req->rq_pill);
1400         if (rc)
1401                 RETURN(-ENOMEM);
1402
1403         /* deprecated, not used any more */
1404         req->rq_status = -EOPNOTSUPP;
1405         RETURN(-EOPNOTSUPP);
1406 }
1407
1408 static int ost_llog_handle_connect(struct obd_export *exp,
1409                                    struct ptlrpc_request *req)
1410 {
1411         struct llogd_conn_body *body;
1412         int rc;
1413         ENTRY;
1414
1415         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_CONN_BODY);
1416         rc = obd_llog_connect(exp, body);
1417         RETURN(rc);
1418 }
1419
1420 #define ost_init_sec_none(reply, exp)                                   \
1421 do {                                                                    \
1422         reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |          \
1423                                       OBD_CONNECT_RMT_CLIENT_FORCE |    \
1424                                       OBD_CONNECT_OSS_CAPA);            \
1425         spin_lock(&exp->exp_lock);                                      \
1426         *exp_connect_flags_ptr(exp) = reply->ocd_connect_flags;         \
1427         spin_unlock(&exp->exp_lock);                                    \
1428 } while (0)
1429
1430 static int ost_init_sec_level(struct ptlrpc_request *req)
1431 {
1432         struct obd_export *exp = req->rq_export;
1433         struct req_capsule *pill = &req->rq_pill;
1434         struct obd_device *obd = exp->exp_obd;
1435         struct filter_obd *filter = &obd->u.filter;
1436         char *client = libcfs_nid2str(req->rq_peer.nid);
1437         struct obd_connect_data *data, *reply;
1438         int rc = 0, remote;
1439         ENTRY;
1440
1441         data = req_capsule_client_get(pill, &RMF_CONNECT_DATA);
1442         reply = req_capsule_server_get(pill, &RMF_CONNECT_DATA);
1443         if (data == NULL || reply == NULL)
1444                 RETURN(-EFAULT);
1445
1446         /* connection from MDT is always trusted */
1447         if (req->rq_auth_usr_mdt) {
1448                 ost_init_sec_none(reply, exp);
1449                 RETURN(0);
1450         }
1451
1452         /* no GSS support case */
1453         if (!req->rq_auth_gss) {
1454                 if (filter->fo_sec_level > LUSTRE_SEC_NONE) {
1455                         CWARN("client %s -> target %s does not user GSS, "
1456                               "can not run under security level %d.\n",
1457                               client, obd->obd_name, filter->fo_sec_level);
1458                         RETURN(-EACCES);
1459                 } else {
1460                         ost_init_sec_none(reply, exp);
1461                         RETURN(0);
1462                 }
1463         }
1464
1465         /* old version case */
1466         if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
1467                      !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
1468                 if (filter->fo_sec_level > LUSTRE_SEC_NONE) {
1469                         CWARN("client %s -> target %s uses old version, "
1470                               "can not run under security level %d.\n",
1471                               client, obd->obd_name, filter->fo_sec_level);
1472                         RETURN(-EACCES);
1473                 } else {
1474                         CWARN("client %s -> target %s uses old version, "
1475                               "run under security level %d.\n",
1476                               client, obd->obd_name, filter->fo_sec_level);
1477                         ost_init_sec_none(reply, exp);
1478                         RETURN(0);
1479                 }
1480         }
1481
1482         remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
1483         if (remote) {
1484                 if (!req->rq_auth_remote)
1485                         CDEBUG(D_SEC, "client (local realm) %s -> target %s "
1486                                "asked to be remote.\n", client, obd->obd_name);
1487         } else if (req->rq_auth_remote) {
1488                 remote = 1;
1489                 CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
1490                        "as remote by default.\n", client, obd->obd_name);
1491         }
1492
1493         if (remote) {
1494                 if (!filter->fo_fl_oss_capa) {
1495                         CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
1496                                " but OSS capabilities are not enabled: %d.\n",
1497                                client, obd->obd_name, filter->fo_fl_oss_capa);
1498                         RETURN(-EACCES);
1499                 }
1500         }
1501
1502         switch (filter->fo_sec_level) {
1503         case LUSTRE_SEC_NONE:
1504                 if (!remote) {
1505                         ost_init_sec_none(reply, exp);
1506                         break;
1507                 } else {
1508                         CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
1509                                "can not run under security level %d.\n",
1510                                client, obd->obd_name, filter->fo_sec_level);
1511                         RETURN(-EACCES);
1512                 }
1513         case LUSTRE_SEC_REMOTE:
1514                 if (!remote)
1515                         ost_init_sec_none(reply, exp);
1516                 break;
1517         case LUSTRE_SEC_ALL:
1518                 if (!remote) {
1519                         reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |
1520                                                       OBD_CONNECT_RMT_CLIENT_FORCE);
1521                         if (!filter->fo_fl_oss_capa)
1522                                 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
1523
1524                         spin_lock(&exp->exp_lock);
1525                         *exp_connect_flags_ptr(exp) = reply->ocd_connect_flags;
1526                         spin_unlock(&exp->exp_lock);
1527                 }
1528                 break;
1529         default:
1530                 RETURN(-EINVAL);
1531         }
1532
1533         RETURN(rc);
1534 }
1535
1536 /*
1537  * FIXME
1538  * this should be done in filter_connect()/filter_reconnect(), but
1539  * we can't obtain information like NID, which stored in incoming
1540  * request, thus can't decide what flavor to use. so we do it here.
1541  *
1542  * This hack should be removed after the OST stack be rewritten, just
1543  * like what we are doing in mdt_obd_connect()/mdt_obd_reconnect().
1544  */
1545 static int ost_connect_check_sptlrpc(struct ptlrpc_request *req)
1546 {
1547         struct obd_export     *exp = req->rq_export;
1548         struct filter_obd     *filter = &exp->exp_obd->u.filter;
1549         struct sptlrpc_flavor  flvr;
1550         int                    rc = 0;
1551
1552         if (unlikely(strcmp(exp->exp_obd->obd_type->typ_name,
1553                             LUSTRE_ECHO_NAME) == 0)) {
1554                 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_ANY;
1555                 return 0;
1556         }
1557
1558         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
1559                 read_lock(&filter->fo_sptlrpc_lock);
1560                 sptlrpc_target_choose_flavor(&filter->fo_sptlrpc_rset,
1561                                              req->rq_sp_from,
1562                                              req->rq_peer.nid,
1563                                              &flvr);
1564                 read_unlock(&filter->fo_sptlrpc_lock);
1565
1566                 spin_lock(&exp->exp_lock);
1567
1568                 exp->exp_sp_peer = req->rq_sp_from;
1569                 exp->exp_flvr = flvr;
1570
1571                 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
1572                     exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
1573                         CERROR("unauthorized rpc flavor %x from %s, "
1574                                "expect %x\n", req->rq_flvr.sf_rpc,
1575                                libcfs_nid2str(req->rq_peer.nid),
1576                                exp->exp_flvr.sf_rpc);
1577                         rc = -EACCES;
1578                 }
1579
1580                 spin_unlock(&exp->exp_lock);
1581         } else {
1582                 if (exp->exp_sp_peer != req->rq_sp_from) {
1583                         CERROR("RPC source %s doesn't match %s\n",
1584                                sptlrpc_part2name(req->rq_sp_from),
1585                                sptlrpc_part2name(exp->exp_sp_peer));
1586                         rc = -EACCES;
1587                 } else {
1588                         rc = sptlrpc_target_export_check(exp, req);
1589                 }
1590         }
1591
1592         return rc;
1593 }
1594
1595 /* Ensure that data and metadata are synced to the disk when lock is cancelled
1596  * (if requested) */
1597 int ost_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
1598                      void *data, int flag)
1599 {
1600         struct lu_env   env;
1601         __u32           sync_lock_cancel = 0;
1602         __u32           len = sizeof(sync_lock_cancel);
1603         int             rc = 0;
1604
1605         ENTRY;
1606
1607         rc = lu_env_init(&env, LCT_DT_THREAD);
1608         if (unlikely(rc != 0))
1609                 RETURN(rc);
1610
1611         rc = obd_get_info(&env, lock->l_export, sizeof(KEY_SYNC_LOCK_CANCEL),
1612                           KEY_SYNC_LOCK_CANCEL, &len, &sync_lock_cancel, NULL);
1613         if (rc == 0 && flag == LDLM_CB_CANCELING &&
1614             (lock->l_granted_mode & (LCK_PW|LCK_GROUP)) &&
1615             (sync_lock_cancel == ALWAYS_SYNC_ON_CANCEL ||
1616              (sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL &&
1617               lock->l_flags & LDLM_FL_CBPENDING))) {
1618                 struct obd_info *oinfo;
1619                 struct obdo     *oa;
1620                 int              rc;
1621
1622                 OBD_ALLOC_PTR(oinfo);
1623                 if (!oinfo)
1624                         GOTO(out_env, rc = -ENOMEM);
1625                 OBDO_ALLOC(oa);
1626                 if (!oa) {
1627                         OBD_FREE_PTR(oinfo);
1628                         GOTO(out_env, rc = -ENOMEM);
1629                 }
1630
1631                 ostid_res_name_to_id(&oa->o_oi, &lock->l_resource->lr_name);
1632                 oa->o_valid = OBD_MD_FLID|OBD_MD_FLGROUP;
1633                 oinfo->oi_oa = oa;
1634                 oinfo->oi_capa = BYPASS_CAPA;
1635
1636                 rc = obd_sync(&env, lock->l_export, oinfo,
1637                               lock->l_policy_data.l_extent.start,
1638                               lock->l_policy_data.l_extent.end, NULL);
1639                 if (rc)
1640                         CERROR("Error %d syncing data on lock cancel\n", rc);
1641
1642                 OBDO_FREE(oa);
1643                 OBD_FREE_PTR(oinfo);
1644         }
1645
1646         rc = ldlm_server_blocking_ast(lock, desc, data, flag);
1647 out_env:
1648         lu_env_fini(&env);
1649         RETURN(rc);
1650 }
1651
1652 static int ost_filter_recovery_request(struct ptlrpc_request *req,
1653                                        struct obd_device *obd, int *process)
1654 {
1655         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1656         case OST_CONNECT: /* This will never get here, but for completeness. */
1657         case OST_DISCONNECT:
1658                *process = 1;
1659                RETURN(0);
1660
1661         case OBD_PING:
1662         case OST_CREATE:
1663         case OST_DESTROY:
1664         case OST_PUNCH:
1665         case OST_SETATTR:
1666         case OST_SYNC:
1667         case OST_WRITE:
1668         case OBD_LOG_CANCEL:
1669         case LDLM_ENQUEUE:
1670                 *process = target_queue_recovery_request(req, obd);
1671                 RETURN(0);
1672
1673         default:
1674                 DEBUG_REQ(D_WARNING, req, "not permitted during recovery");
1675                 *process = -EAGAIN;
1676                 RETURN(0);
1677         }
1678 }
1679
1680 int ost_msg_check_version(struct lustre_msg *msg)
1681 {
1682         int rc;
1683
1684         switch(lustre_msg_get_opc(msg)) {
1685         case OST_CONNECT:
1686         case OST_DISCONNECT:
1687         case OBD_PING:
1688         case SEC_CTX_INIT:
1689         case SEC_CTX_INIT_CONT:
1690         case SEC_CTX_FINI:
1691                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1692                 if (rc)
1693                         CERROR("bad opc %u version %08x, expecting %08x\n",
1694                                lustre_msg_get_opc(msg),
1695                                lustre_msg_get_version(msg),
1696                                LUSTRE_OBD_VERSION);
1697                 break;
1698         case SEQ_QUERY:
1699                 /* Note: client always use MDS_VERSION for FID request */
1700                 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
1701                 if (rc)
1702                         CERROR("bad opc %u version %08x, expecting %08x\n",
1703                                lustre_msg_get_opc(msg),
1704                                lustre_msg_get_version(msg),
1705                                LUSTRE_MDS_VERSION);
1706                 break;
1707         case OST_CREATE:
1708         case OST_DESTROY:
1709         case OST_GETATTR:
1710         case OST_SETATTR:
1711         case OST_WRITE:
1712         case OST_READ:
1713         case OST_PUNCH:
1714         case OST_STATFS:
1715         case OST_SYNC:
1716         case OST_SET_INFO:
1717         case OST_GET_INFO:
1718         case OST_QUOTACHECK:
1719         case OST_QUOTACTL:
1720                 rc = lustre_msg_check_version(msg, LUSTRE_OST_VERSION);
1721                 if (rc)
1722                         CERROR("bad opc %u version %08x, expecting %08x\n",
1723                                lustre_msg_get_opc(msg),
1724                                lustre_msg_get_version(msg),
1725                                LUSTRE_OST_VERSION);
1726                 break;
1727         case LDLM_ENQUEUE:
1728         case LDLM_CONVERT:
1729         case LDLM_CANCEL:
1730         case LDLM_BL_CALLBACK:
1731         case LDLM_CP_CALLBACK:
1732                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1733                 if (rc)
1734                         CERROR("bad opc %u version %08x, expecting %08x\n",
1735                                lustre_msg_get_opc(msg),
1736                                lustre_msg_get_version(msg),
1737                                LUSTRE_DLM_VERSION);
1738                 break;
1739         case LLOG_ORIGIN_CONNECT:
1740         case OBD_LOG_CANCEL:
1741                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1742                 if (rc)
1743                         CERROR("bad opc %u version %08x, expecting %08x\n",
1744                                lustre_msg_get_opc(msg),
1745                                lustre_msg_get_version(msg),
1746                                LUSTRE_LOG_VERSION);
1747                 break;
1748         case OST_QUOTA_ADJUST_QUNIT:
1749                 rc = -ENOTSUPP;
1750                 CERROR("Quota adjust is deprecated as of 2.4.0\n");
1751                 break;
1752         default:
1753                 CERROR("Unexpected opcode %d\n", lustre_msg_get_opc(msg));
1754                 rc = -ENOTSUPP;
1755         }
1756         return rc;
1757 }
1758
1759 struct ost_prolong_data {
1760         struct ptlrpc_request *opd_req;
1761         struct obd_export     *opd_exp;
1762         struct obdo           *opd_oa;
1763         struct ldlm_res_id     opd_resid;
1764         struct ldlm_extent     opd_extent;
1765         ldlm_mode_t            opd_mode;
1766         unsigned int           opd_locks;
1767         int                    opd_timeout;
1768 };
1769
1770 /* prolong locks for the current service time of the corresponding
1771  * portal (= OST_IO_PORTAL)
1772  */
1773 static inline int prolong_timeout(struct ptlrpc_request *req)
1774 {
1775         struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1776
1777         if (AT_OFF)
1778                 return obd_timeout / 2;
1779
1780         return max(at_est2timeout(at_get(&svcpt->scp_at_estimate)),
1781                    ldlm_timeout);
1782 }
1783
1784 static void ost_prolong_lock_one(struct ost_prolong_data *opd,
1785                                  struct ldlm_lock *lock)
1786 {
1787         LASSERT(lock->l_export == opd->opd_exp);
1788
1789         if (lock->l_destroyed) /* lock already cancelled */
1790                 return;
1791
1792         /* XXX: never try to grab resource lock here because we're inside
1793          * exp_bl_list_lock; in ldlm_lockd.c to handle waiting list we take
1794          * res lock and then exp_bl_list_lock. */
1795
1796         if (!(lock->l_flags & LDLM_FL_AST_SENT))
1797                 /* ignore locks not being cancelled */
1798                 return;
1799
1800         LDLM_DEBUG(lock,
1801                    "refreshed for req x"LPU64" ext("LPU64"->"LPU64") to %ds.\n",
1802                    opd->opd_req->rq_xid, opd->opd_extent.start,
1803                    opd->opd_extent.end, opd->opd_timeout);
1804
1805         /* OK. this is a possible lock the user holds doing I/O
1806          * let's refresh eviction timer for it */
1807         ldlm_refresh_waiting_lock(lock, opd->opd_timeout);
1808         ++opd->opd_locks;
1809 }
1810
1811 static void ost_prolong_locks(struct ost_prolong_data *data)
1812 {
1813         struct obd_export *exp = data->opd_exp;
1814         struct obdo       *oa  = data->opd_oa;
1815         struct ldlm_lock  *lock;
1816         ENTRY;
1817
1818         if (oa->o_valid & OBD_MD_FLHANDLE) {
1819                 /* mostly a request should be covered by only one lock, try
1820                  * fast path. */
1821                 lock = ldlm_handle2lock(&oa->o_handle);
1822                 if (lock != NULL) {
1823                         /* Fast path to check if the lock covers the whole IO
1824                          * region exclusively. */
1825                         if (lock->l_granted_mode == LCK_PW &&
1826                             ldlm_extent_contain(&lock->l_policy_data.l_extent,
1827                                                 &data->opd_extent)) {
1828                                 /* bingo */
1829                                 ost_prolong_lock_one(data, lock);
1830                                 LDLM_LOCK_PUT(lock);
1831                                 RETURN_EXIT;
1832                         }
1833                         LDLM_LOCK_PUT(lock);
1834                 }
1835         }
1836
1837
1838         spin_lock_bh(&exp->exp_bl_list_lock);
1839         cfs_list_for_each_entry(lock, &exp->exp_bl_list, l_exp_list) {
1840                 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1841                 LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
1842
1843                 if (!ldlm_res_eq(&data->opd_resid, &lock->l_resource->lr_name))
1844                         continue;
1845
1846                 if (!ldlm_extent_overlap(&lock->l_policy_data.l_extent,
1847                                          &data->opd_extent))
1848                         continue;
1849
1850                 ost_prolong_lock_one(data, lock);
1851         }
1852         spin_unlock_bh(&exp->exp_bl_list_lock);
1853
1854         EXIT;
1855 }
1856
1857 /**
1858  * Returns 1 if the given PTLRPC matches the given LDLM locks, or 0 if it does
1859  * not.
1860  */
1861 static int ost_rw_hpreq_lock_match(struct ptlrpc_request *req,
1862                                    struct ldlm_lock *lock)
1863 {
1864         struct niobuf_remote *nb;
1865         struct obd_ioobj *ioo;
1866         int mode, opc;
1867         struct ldlm_extent ext;
1868         ENTRY;
1869
1870         opc = lustre_msg_get_opc(req->rq_reqmsg);
1871         LASSERT(opc == OST_READ || opc == OST_WRITE);
1872
1873         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1874         LASSERT(ioo != NULL);
1875
1876         nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1877         LASSERT(nb != NULL);
1878
1879         ext.start = nb->offset;
1880         nb += ioo->ioo_bufcnt - 1;
1881         ext.end = nb->offset + nb->len - 1;
1882
1883         LASSERT(lock->l_resource != NULL);
1884         if (!ostid_res_name_eq(&ioo->ioo_oid, &lock->l_resource->lr_name))
1885                 RETURN(0);
1886
1887         mode = LCK_PW;
1888         if (opc == OST_READ)
1889                 mode |= LCK_PR;
1890         if (!(lock->l_granted_mode & mode))
1891                 RETURN(0);
1892
1893         RETURN(ldlm_extent_overlap(&lock->l_policy_data.l_extent, &ext));
1894 }
1895
1896 /**
1897  * High-priority queue request check for whether the given PTLRPC request (\a
1898  * req) is blocking an LDLM lock cancel.
1899  *
1900  * Returns 1 if the given given PTLRPC request (\a req) is blocking an LDLM lock
1901  * cancel, 0 if it is not, and -EFAULT if the request is malformed.
1902  *
1903  * Only OST_READs, OST_WRITEs and OST_PUNCHes go on the h-p RPC queue.  This
1904  * function looks only at OST_READs and OST_WRITEs.
1905  */
1906 static int ost_rw_hpreq_check(struct ptlrpc_request *req)
1907 {
1908         struct obd_device *obd = req->rq_export->exp_obd;
1909         struct ost_body *body;
1910         struct obd_ioobj *ioo;
1911         struct niobuf_remote *nb;
1912         struct ost_prolong_data opd = { 0 };
1913         int mode, opc;
1914         ENTRY;
1915
1916         /*
1917          * Use LASSERT to do sanity check because malformed RPCs should have
1918          * been filtered out in ost_hpreq_handler().
1919          */
1920         opc = lustre_msg_get_opc(req->rq_reqmsg);
1921         LASSERT(opc == OST_READ || opc == OST_WRITE);
1922
1923         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1924         LASSERT(body != NULL);
1925
1926         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1927         LASSERT(ioo != NULL);
1928
1929         nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1930         LASSERT(nb != NULL);
1931         LASSERT(!(nb->flags & OBD_BRW_SRVLOCK));
1932
1933         ostid_build_res_name(&ioo->ioo_oid, &opd.opd_resid);
1934
1935         opd.opd_req = req;
1936         mode = LCK_PW;
1937         if (opc == OST_READ)
1938                 mode |= LCK_PR;
1939         opd.opd_mode = mode;
1940         opd.opd_exp = req->rq_export;
1941         opd.opd_oa  = &body->oa;
1942         opd.opd_extent.start = nb->offset;
1943         nb += ioo->ioo_bufcnt - 1;
1944         opd.opd_extent.end = nb->offset + nb->len - 1;
1945         opd.opd_timeout = prolong_timeout(req);
1946
1947         DEBUG_REQ(D_RPCTRACE, req,
1948                "%s %s: refresh rw locks: " LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
1949                obd->obd_name, cfs_current()->comm,
1950                opd.opd_resid.name[0], opd.opd_resid.name[1],
1951                opd.opd_extent.start, opd.opd_extent.end);
1952
1953         ost_prolong_locks(&opd);
1954
1955         CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
1956                obd->obd_name, opd.opd_locks, req);
1957
1958         RETURN(opd.opd_locks > 0);
1959 }
1960
1961 static void ost_rw_hpreq_fini(struct ptlrpc_request *req)
1962 {
1963         (void)ost_rw_hpreq_check(req);
1964 }
1965
1966 /**
1967  * Like ost_rw_hpreq_lock_match(), but for OST_PUNCH RPCs.
1968  */
1969 static int ost_punch_hpreq_lock_match(struct ptlrpc_request *req,
1970                                       struct ldlm_lock *lock)
1971 {
1972         struct ost_body *body;
1973         ENTRY;
1974
1975         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1976         LASSERT(body != NULL);
1977
1978         if (body->oa.o_valid & OBD_MD_FLHANDLE &&
1979             body->oa.o_handle.cookie == lock->l_handle.h_cookie)
1980                 RETURN(1);
1981
1982         RETURN(0);
1983 }
1984
1985 /**
1986  * Like ost_rw_hpreq_check(), but for OST_PUNCH RPCs.
1987  */
1988 static int ost_punch_hpreq_check(struct ptlrpc_request *req)
1989 {
1990         struct obd_device *obd = req->rq_export->exp_obd;
1991         struct ost_body *body;
1992         struct obdo *oa;
1993         struct ost_prolong_data opd = { 0 };
1994         __u64 start, end;
1995         ENTRY;
1996
1997         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1998         LASSERT(body != NULL);
1999
2000         oa = &body->oa;
2001         LASSERT(!(oa->o_valid & OBD_MD_FLFLAGS) ||
2002                 !(oa->o_flags & OBD_FL_SRVLOCK));
2003
2004         start = oa->o_size;
2005         end = start + oa->o_blocks;
2006
2007         opd.opd_req = req;
2008         opd.opd_mode = LCK_PW;
2009         opd.opd_exp = req->rq_export;
2010         opd.opd_oa  = oa;
2011         opd.opd_extent.start = start;
2012         opd.opd_extent.end   = end;
2013         if (oa->o_blocks == OBD_OBJECT_EOF)
2014                 opd.opd_extent.end = OBD_OBJECT_EOF;
2015         opd.opd_timeout = prolong_timeout(req);
2016
2017         ostid_build_res_name(&oa->o_oi, &opd.opd_resid);
2018
2019         CDEBUG(D_DLMTRACE,
2020                "%s: refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
2021                obd->obd_name,
2022                opd.opd_resid.name[0], opd.opd_resid.name[1],
2023                opd.opd_extent.start, opd.opd_extent.end);
2024
2025         ost_prolong_locks(&opd);
2026
2027         CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
2028                obd->obd_name, opd.opd_locks, req);
2029
2030         RETURN(opd.opd_locks > 0);
2031 }
2032
2033 static void ost_punch_hpreq_fini(struct ptlrpc_request *req)
2034 {
2035         (void)ost_punch_hpreq_check(req);
2036 }
2037
2038 struct ptlrpc_hpreq_ops ost_hpreq_rw = {
2039         .hpreq_lock_match = ost_rw_hpreq_lock_match,
2040         .hpreq_check      = ost_rw_hpreq_check,
2041         .hpreq_fini       = ost_rw_hpreq_fini
2042 };
2043
2044 struct ptlrpc_hpreq_ops ost_hpreq_punch = {
2045         .hpreq_lock_match = ost_punch_hpreq_lock_match,
2046         .hpreq_check      = ost_punch_hpreq_check,
2047         .hpreq_fini       = ost_punch_hpreq_fini
2048 };
2049
2050 /** Assign high priority operations to the request if needed. */
2051 static int ost_io_hpreq_handler(struct ptlrpc_request *req)
2052 {
2053         ENTRY;
2054         if (req->rq_export) {
2055                 int opc = lustre_msg_get_opc(req->rq_reqmsg);
2056                 struct ost_body *body;
2057
2058                 if (opc == OST_READ || opc == OST_WRITE) {
2059                         struct niobuf_remote *nb;
2060                         struct obd_ioobj *ioo;
2061                         int objcount, niocount;
2062                         int rc;
2063                         int i;
2064
2065                         /* RPCs on the H-P queue can be inspected before
2066                          * ost_handler() initializes their pills, so we
2067                          * initialize that here.  Capsule initialization is
2068                          * idempotent, as is setting the pill's format (provided
2069                          * it doesn't change).
2070                          */
2071                         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2072                         if (opc == OST_READ)
2073                                 req_capsule_set(&req->rq_pill,
2074                                                 &RQF_OST_BRW_READ);
2075                         else
2076                                 req_capsule_set(&req->rq_pill,
2077                                                 &RQF_OST_BRW_WRITE);
2078
2079                         body = req_capsule_client_get(&req->rq_pill,
2080                                                       &RMF_OST_BODY);
2081                         if (body == NULL) {
2082                                 CERROR("Missing/short ost_body\n");
2083                                 RETURN(-EFAULT);
2084                         }
2085
2086                         objcount = req_capsule_get_size(&req->rq_pill,
2087                                                         &RMF_OBD_IOOBJ,
2088                                                         RCL_CLIENT) /
2089                                                         sizeof(*ioo);
2090                         if (objcount == 0) {
2091                                 CERROR("Missing/short ioobj\n");
2092                                 RETURN(-EFAULT);
2093                         }
2094                         if (objcount > 1) {
2095                                 CERROR("too many ioobjs (%d)\n", objcount);
2096                                 RETURN(-EFAULT);
2097                         }
2098
2099                         ioo = req_capsule_client_get(&req->rq_pill,
2100                                                      &RMF_OBD_IOOBJ);
2101                         if (ioo == NULL) {
2102                                 CERROR("Missing/short ioobj\n");
2103                                 RETURN(-EFAULT);
2104                         }
2105
2106                         rc = ost_validate_obdo(req->rq_export, &body->oa, ioo);
2107                         if (rc) {
2108                                 CERROR("invalid object ids\n");
2109                                 RETURN(rc);
2110                         }
2111
2112                         for (niocount = i = 0; i < objcount; i++) {
2113                                 if (ioo[i].ioo_bufcnt == 0) {
2114                                         CERROR("ioo[%d] has zero bufcnt\n", i);
2115                                         RETURN(-EFAULT);
2116                                 }
2117                                 niocount += ioo[i].ioo_bufcnt;
2118                         }
2119                         if (niocount > PTLRPC_MAX_BRW_PAGES) {
2120                                 DEBUG_REQ(D_RPCTRACE, req,
2121                                           "bulk has too many pages (%d)",
2122                                           niocount);
2123                                 RETURN(-EFAULT);
2124                         }
2125
2126                         nb = req_capsule_client_get(&req->rq_pill,
2127                                                     &RMF_NIOBUF_REMOTE);
2128                         if (nb == NULL) {
2129                                 CERROR("Missing/short niobuf\n");
2130                                 RETURN(-EFAULT);
2131                         }
2132
2133                         if (niocount == 0 || !(nb[0].flags & OBD_BRW_SRVLOCK))
2134                                 req->rq_ops = &ost_hpreq_rw;
2135                 } else if (opc == OST_PUNCH) {
2136                         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2137                         req_capsule_set(&req->rq_pill, &RQF_OST_PUNCH);
2138
2139                         body = req_capsule_client_get(&req->rq_pill,
2140                                                       &RMF_OST_BODY);
2141                         if (body == NULL) {
2142                                 CERROR("Missing/short ost_body\n");
2143                                 RETURN(-EFAULT);
2144                         }
2145
2146                         if (!(body->oa.o_valid & OBD_MD_FLFLAGS) ||
2147                             !(body->oa.o_flags & OBD_FL_SRVLOCK))
2148                                 req->rq_ops = &ost_hpreq_punch;
2149                 }
2150         }
2151         RETURN(0);
2152 }
2153
2154 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
2155 int ost_handle(struct ptlrpc_request *req)
2156 {
2157         struct obd_trans_info trans_info = { 0, };
2158         struct obd_trans_info *oti = &trans_info;
2159         int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
2160         struct obd_device *obd = NULL;
2161         ENTRY;
2162
2163         /* OST module is kept between remounts, but the last reference
2164          * to specific module (say, osd or ofd) kills all related keys
2165          * from the environment. so we have to refill it until the root
2166          * cause is fixed properly */
2167         lu_env_refill(req->rq_svc_thread->t_env);
2168
2169         LASSERT(current->journal_info == NULL);
2170
2171         /* primordial rpcs don't affect server recovery */
2172         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2173         case SEC_CTX_INIT:
2174         case SEC_CTX_INIT_CONT:
2175         case SEC_CTX_FINI:
2176                 GOTO(out, rc = 0);
2177         }
2178
2179         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2180
2181         if (lustre_msg_get_opc(req->rq_reqmsg) != OST_CONNECT) {
2182                 if (!class_connected_export(req->rq_export)) {
2183                         CDEBUG(D_HA,"operation %d on unconnected OST from %s\n",
2184                                lustre_msg_get_opc(req->rq_reqmsg),
2185                                libcfs_id2str(req->rq_peer));
2186                         req->rq_status = -ENOTCONN;
2187                         GOTO(out, rc = -ENOTCONN);
2188                 }
2189
2190                 obd = req->rq_export->exp_obd;
2191
2192                 /* Check for aborted recovery. */
2193                 if (obd->obd_recovering) {
2194                         rc = ost_filter_recovery_request(req, obd,
2195                                                          &should_process);
2196                         if (rc || !should_process)
2197                                 RETURN(rc);
2198                         else if (should_process < 0) {
2199                                 req->rq_status = should_process;
2200                                 rc = ptlrpc_error(req);
2201                                 RETURN(rc);
2202                         }
2203                 }
2204         }
2205
2206         oti_init(oti, req);
2207
2208         rc = ost_msg_check_version(req->rq_reqmsg);
2209         if (rc)
2210                 RETURN(rc);
2211
2212         if (req && req->rq_reqmsg && req->rq_export &&
2213             (exp_connect_flags(req->rq_export) & OBD_CONNECT_JOBSTATS))
2214                 oti->oti_jobid = lustre_msg_get_jobid(req->rq_reqmsg);
2215
2216         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2217         case OST_CONNECT: {
2218                 CDEBUG(D_INODE, "connect\n");
2219                 req_capsule_set(&req->rq_pill, &RQF_OST_CONNECT);
2220                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET))
2221                         RETURN(0);
2222                 rc = target_handle_connect(req);
2223                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET2))
2224                         RETURN(0);
2225                 if (!rc) {
2226                         rc = ost_init_sec_level(req);
2227                         if (!rc)
2228                                 rc = ost_connect_check_sptlrpc(req);
2229                 }
2230                 break;
2231         }
2232         case OST_DISCONNECT:
2233                 CDEBUG(D_INODE, "disconnect\n");
2234                 req_capsule_set(&req->rq_pill, &RQF_OST_DISCONNECT);
2235                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DISCONNECT_NET))
2236                         RETURN(0);
2237                 rc = target_handle_disconnect(req);
2238                 break;
2239         case OST_CREATE:
2240                 CDEBUG(D_INODE, "create\n");
2241                 req_capsule_set(&req->rq_pill, &RQF_OST_CREATE);
2242                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CREATE_NET))
2243                         RETURN(0);
2244                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2245                         GOTO(out, rc = -EROFS);
2246                 rc = ost_create(req->rq_export, req, oti);
2247                 break;
2248         case OST_DESTROY:
2249                 CDEBUG(D_INODE, "destroy\n");
2250                 req_capsule_set(&req->rq_pill, &RQF_OST_DESTROY);
2251                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DESTROY_NET))
2252                         RETURN(0);
2253                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2254                         GOTO(out, rc = -EROFS);
2255                 rc = ost_destroy(req->rq_export, req, oti);
2256                 break;
2257         case OST_GETATTR:
2258                 CDEBUG(D_INODE, "getattr\n");
2259                 req_capsule_set(&req->rq_pill, &RQF_OST_GETATTR);
2260                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_GETATTR_NET))
2261                         RETURN(0);
2262                 rc = ost_getattr(req->rq_export, req);
2263                 break;
2264         case OST_SETATTR:
2265                 CDEBUG(D_INODE, "setattr\n");
2266                 req_capsule_set(&req->rq_pill, &RQF_OST_SETATTR);
2267                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SETATTR_NET))
2268                         RETURN(0);
2269                 rc = ost_setattr(req->rq_export, req, oti);
2270                 break;
2271         case OST_WRITE:
2272                 req_capsule_set(&req->rq_pill, &RQF_OST_BRW_WRITE);
2273                 CDEBUG(D_INODE, "write\n");
2274                 /* req->rq_request_portal would be nice, if it was set */
2275                 if (ptlrpc_req2svc(req)->srv_req_portal != OST_IO_PORTAL) {
2276                         CERROR("%s: deny write request from %s to portal %u\n",
2277                                req->rq_export->exp_obd->obd_name,
2278                                obd_export_nid2str(req->rq_export),
2279                                ptlrpc_req2svc(req)->srv_req_portal);
2280                         GOTO(out, rc = -EPROTO);
2281                 }
2282                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
2283                         RETURN(0);
2284                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
2285                         GOTO(out, rc = -ENOSPC);
2286                 if (OBD_FAIL_TIMEOUT(OBD_FAIL_OST_EROFS, 1))
2287                         GOTO(out, rc = -EROFS);
2288                 rc = ost_brw_write(req, oti);
2289                 LASSERT(current->journal_info == NULL);
2290                 /* ost_brw_write sends its own replies */
2291                 RETURN(rc);
2292         case OST_READ:
2293                 req_capsule_set(&req->rq_pill, &RQF_OST_BRW_READ);
2294                 CDEBUG(D_INODE, "read\n");
2295                 /* req->rq_request_portal would be nice, if it was set */
2296                 if (ptlrpc_req2svc(req)->srv_req_portal != OST_IO_PORTAL) {
2297                         CERROR("%s: deny read request from %s to portal %u\n",
2298                                req->rq_export->exp_obd->obd_name,
2299                                obd_export_nid2str(req->rq_export),
2300                                ptlrpc_req2svc(req)->srv_req_portal);
2301                         GOTO(out, rc = -EPROTO);
2302                 }
2303                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
2304                         RETURN(0);
2305                 rc = ost_brw_read(req, oti);
2306                 LASSERT(current->journal_info == NULL);
2307                 /* ost_brw_read sends its own replies */
2308                 RETURN(rc);
2309         case OST_PUNCH:
2310                 CDEBUG(D_INODE, "punch\n");
2311                 req_capsule_set(&req->rq_pill, &RQF_OST_PUNCH);
2312                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_PUNCH_NET))
2313                         RETURN(0);
2314                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2315                         GOTO(out, rc = -EROFS);
2316                 rc = ost_punch(req->rq_export, req, oti);
2317                 break;
2318         case OST_STATFS:
2319                 CDEBUG(D_INODE, "statfs\n");
2320                 req_capsule_set(&req->rq_pill, &RQF_OST_STATFS);
2321                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_NET))
2322                         RETURN(0);
2323                 rc = ost_statfs(req);
2324                 break;
2325         case OST_SYNC:
2326                 CDEBUG(D_INODE, "sync\n");
2327                 req_capsule_set(&req->rq_pill, &RQF_OST_SYNC);
2328                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SYNC_NET))
2329                         RETURN(0);
2330                 rc = ost_sync(req->rq_export, req, oti);
2331                 break;
2332         case OST_SET_INFO:
2333                 DEBUG_REQ(D_INODE, req, "set_info");
2334                 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
2335                 rc = ost_set_info(req->rq_export, req);
2336                 break;
2337         case OST_GET_INFO:
2338                 DEBUG_REQ(D_INODE, req, "get_info");
2339                 req_capsule_set(&req->rq_pill, &RQF_OST_GET_INFO_GENERIC);
2340                 rc = ost_get_info(req->rq_export, req);
2341                 break;
2342         case SEQ_QUERY:
2343                 CDEBUG(D_INODE, "seq\n");
2344                 rc = seq_handle(req);
2345                 break;
2346         case OST_QUOTACHECK:
2347                 CDEBUG(D_INODE, "quotacheck\n");
2348                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACHECK);
2349                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACHECK_NET))
2350                         RETURN(0);
2351                 rc = ost_handle_quotacheck(req);
2352                 break;
2353         case OST_QUOTACTL:
2354                 CDEBUG(D_INODE, "quotactl\n");
2355                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACTL);
2356                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACTL_NET))
2357                         RETURN(0);
2358                 rc = ost_handle_quotactl(req);
2359                 break;
2360         case OBD_PING:
2361                 DEBUG_REQ(D_INODE, req, "ping");
2362                 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
2363                 rc = target_handle_ping(req);
2364                 break;
2365         /* FIXME - just reply status */
2366         case LLOG_ORIGIN_CONNECT:
2367                 DEBUG_REQ(D_INODE, req, "log connect");
2368                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_CONNECT);
2369                 rc = ost_llog_handle_connect(req->rq_export, req);
2370                 req->rq_status = rc;
2371                 rc = req_capsule_server_pack(&req->rq_pill);
2372                 if (rc)
2373                         RETURN(rc);
2374                 RETURN(ptlrpc_reply(req));
2375         case OBD_LOG_CANCEL:
2376                 CDEBUG(D_INODE, "log cancel\n");
2377                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2378                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2379                         RETURN(0);
2380                 rc = llog_origin_handle_cancel(req);
2381                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2382                         RETURN(0);
2383                 req->rq_status = rc;
2384                 rc = req_capsule_server_pack(&req->rq_pill);
2385                 if (rc)
2386                         RETURN(rc);
2387                 RETURN(ptlrpc_reply(req));
2388         case LDLM_ENQUEUE:
2389                 CDEBUG(D_INODE, "enqueue\n");
2390                 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
2391                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_NET))
2392                         RETURN(0);
2393                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
2394                                          ost_blocking_ast,
2395                                          ldlm_server_glimpse_ast);
2396                 fail = OBD_FAIL_OST_LDLM_REPLY_NET;
2397                 break;
2398         case LDLM_CONVERT:
2399                 CDEBUG(D_INODE, "convert\n");
2400                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CONVERT);
2401                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CONVERT_NET))
2402                         RETURN(0);
2403                 rc = ldlm_handle_convert(req);
2404                 break;
2405         case LDLM_CANCEL:
2406                 CDEBUG(D_INODE, "cancel\n");
2407                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2408                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_NET))
2409                         RETURN(0);
2410                 rc = ldlm_handle_cancel(req);
2411                 break;
2412         case LDLM_BL_CALLBACK:
2413         case LDLM_CP_CALLBACK:
2414                 CDEBUG(D_INODE, "callback\n");
2415                 CERROR("callbacks should not happen on OST\n");
2416                 /* fall through */
2417         default:
2418                 CERROR("Unexpected opcode %d\n",
2419                        lustre_msg_get_opc(req->rq_reqmsg));
2420                 req->rq_status = -ENOTSUPP;
2421                 rc = ptlrpc_error(req);
2422                 RETURN(rc);
2423         }
2424
2425         LASSERT(current->journal_info == NULL);
2426
2427         EXIT;
2428         /* If we're DISCONNECTing, the export_data is already freed */
2429         if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != OST_DISCONNECT)
2430                 target_committed_to_req(req);
2431
2432 out:
2433         if (!rc)
2434                 oti_to_request(oti, req);
2435
2436         target_send_reply(req, rc, fail);
2437         return 0;
2438 }
2439 EXPORT_SYMBOL(ost_handle);
2440
2441 /*
2442  * free per-thread pool created by ost_io_thread_init().
2443  */
2444 static void ost_io_thread_done(struct ptlrpc_thread *thread)
2445 {
2446         struct ost_thread_local_cache *tls; /* TLS stands for Thread-Local
2447                                              * Storage */
2448
2449         ENTRY;
2450
2451         LASSERT(thread != NULL);
2452
2453         /*
2454          * be prepared to handle partially-initialized pools (because this is
2455          * called from ost_io_thread_init() for cleanup.
2456          */
2457         tls = thread->t_data;
2458         if (tls != NULL) {
2459                 OBD_FREE_PTR(tls);
2460                 thread->t_data = NULL;
2461         }
2462         EXIT;
2463 }
2464
2465 /*
2466  * initialize per-thread page pool (bug 5137).
2467  */
2468 static int ost_io_thread_init(struct ptlrpc_thread *thread)
2469 {
2470         struct ost_thread_local_cache *tls;
2471
2472         ENTRY;
2473
2474         LASSERT(thread != NULL);
2475         LASSERT(thread->t_data == NULL);
2476
2477         OBD_ALLOC_PTR(tls);
2478         if (tls == NULL)
2479                 RETURN(-ENOMEM);
2480         thread->t_data = tls;
2481         RETURN(0);
2482 }
2483
2484 #define OST_WATCHDOG_TIMEOUT (obd_timeout * 1000)
2485
2486 static struct cfs_cpt_table     *ost_io_cptable;
2487
2488 /* Sigh - really, this is an OSS, the _server_, not the _target_ */
2489 static int ost_setup(struct obd_device *obd, struct lustre_cfg* lcfg)
2490 {
2491         static struct ptlrpc_service_conf       svc_conf;
2492         struct ost_obd *ost = &obd->u.ost;
2493         struct lprocfs_static_vars lvars;
2494         nodemask_t              *mask;
2495         int rc;
2496         ENTRY;
2497
2498         rc = cfs_cleanup_group_info();
2499         if (rc)
2500                 RETURN(rc);
2501
2502         lprocfs_ost_init_vars(&lvars);
2503         lprocfs_obd_setup(obd, lvars.obd_vars);
2504
2505         mutex_init(&ost->ost_health_mutex);
2506
2507         svc_conf = (typeof(svc_conf)) {
2508                 .psc_name               = LUSTRE_OSS_NAME,
2509                 .psc_watchdog_factor    = OSS_SERVICE_WATCHDOG_FACTOR,
2510                 .psc_buf                = {
2511                         .bc_nbufs               = OST_NBUFS,
2512                         .bc_buf_size            = OST_BUFSIZE,
2513                         .bc_req_max_size        = OST_MAXREQSIZE,
2514                         .bc_rep_max_size        = OST_MAXREPSIZE,
2515                         .bc_req_portal          = OST_REQUEST_PORTAL,
2516                         .bc_rep_portal          = OSC_REPLY_PORTAL,
2517                 },
2518                 .psc_thr                = {
2519                         .tc_thr_name            = "ll_ost",
2520                         .tc_thr_factor          = OSS_THR_FACTOR,
2521                         .tc_nthrs_init          = OSS_NTHRS_INIT,
2522                         .tc_nthrs_base          = OSS_NTHRS_BASE,
2523                         .tc_nthrs_max           = OSS_NTHRS_MAX,
2524                         .tc_nthrs_user          = oss_num_threads,
2525                         .tc_cpu_affinity        = 1,
2526                         .tc_ctx_tags            = LCT_DT_THREAD,
2527                 },
2528                 .psc_cpt                = {
2529                         .cc_pattern             = oss_cpts,
2530                 },
2531                 .psc_ops                = {
2532                         .so_req_handler         = ost_handle,
2533                         .so_req_printer         = target_print_req,
2534                         .so_hpreq_handler       = ptlrpc_hpreq_handler,
2535                 },
2536         };
2537         ost->ost_service = ptlrpc_register_service(&svc_conf,
2538                                                    obd->obd_proc_entry);
2539         if (IS_ERR(ost->ost_service)) {
2540                 rc = PTR_ERR(ost->ost_service);
2541                 CERROR("failed to start service: %d\n", rc);
2542                 GOTO(out_lprocfs, rc);
2543         }
2544
2545         memset(&svc_conf, 0, sizeof(svc_conf));
2546         svc_conf = (typeof(svc_conf)) {
2547                 .psc_name               = "ost_create",
2548                 .psc_watchdog_factor    = OSS_SERVICE_WATCHDOG_FACTOR,
2549                 .psc_buf                = {
2550                         .bc_nbufs               = OST_NBUFS,
2551                         .bc_buf_size            = OST_BUFSIZE,
2552                         .bc_req_max_size        = OST_MAXREQSIZE,
2553                         .bc_rep_max_size        = OST_MAXREPSIZE,
2554                         .bc_req_portal          = OST_CREATE_PORTAL,
2555                         .bc_rep_portal          = OSC_REPLY_PORTAL,
2556                 },
2557                 .psc_thr                = {
2558                         .tc_thr_name            = "ll_ost_create",
2559                         .tc_thr_factor          = OSS_CR_THR_FACTOR,
2560                         .tc_nthrs_init          = OSS_CR_NTHRS_INIT,
2561                         .tc_nthrs_base          = OSS_CR_NTHRS_BASE,
2562                         .tc_nthrs_max           = OSS_CR_NTHRS_MAX,
2563                         .tc_nthrs_user          = oss_num_create_threads,
2564                         .tc_cpu_affinity        = 1,
2565                         .tc_ctx_tags            = LCT_DT_THREAD,
2566                 },
2567                 .psc_cpt                = {
2568                         .cc_pattern             = oss_cpts,
2569                 },
2570                 .psc_ops                = {
2571                         .so_req_handler         = ost_handle,
2572                         .so_req_printer         = target_print_req,
2573                 },
2574         };
2575         ost->ost_create_service = ptlrpc_register_service(&svc_conf,
2576                                                           obd->obd_proc_entry);
2577         if (IS_ERR(ost->ost_create_service)) {
2578                 rc = PTR_ERR(ost->ost_create_service);
2579                 CERROR("failed to start OST create service: %d\n", rc);
2580                 GOTO(out_service, rc);
2581         }
2582
2583         mask = cfs_cpt_table->ctb_nodemask;
2584         /* event CPT feature is disabled in libcfs level by set partition
2585          * number to 1, we still want to set node affinity for io service */
2586         if (cfs_cpt_number(cfs_cpt_table) == 1 && nodes_weight(*mask) > 1) {
2587                 int     cpt = 0;
2588                 int     i;
2589
2590                 ost_io_cptable = cfs_cpt_table_alloc(nodes_weight(*mask));
2591                 for_each_node_mask(i, *mask) {
2592                         if (ost_io_cptable == NULL) {
2593                                 CWARN("OSS failed to create CPT table\n");
2594                                 break;
2595                         }
2596
2597                         rc = cfs_cpt_set_node(ost_io_cptable, cpt++, i);
2598                         if (!rc) {
2599                                 CWARN("OSS Failed to set node %d for"
2600                                       "IO CPT table\n", i);
2601                                 cfs_cpt_table_free(ost_io_cptable);
2602                                 ost_io_cptable = NULL;
2603                                 break;
2604                         }
2605                 }
2606         }
2607
2608         memset(&svc_conf, 0, sizeof(svc_conf));
2609         svc_conf = (typeof(svc_conf)) {
2610                 .psc_name               = "ost_io",
2611                 .psc_watchdog_factor    = OSS_SERVICE_WATCHDOG_FACTOR,
2612                 .psc_buf                = {
2613                         .bc_nbufs               = OST_NBUFS,
2614                         .bc_buf_size            = OST_IO_BUFSIZE,
2615                         .bc_req_max_size        = OST_IO_MAXREQSIZE,
2616                         .bc_rep_max_size        = OST_IO_MAXREPSIZE,
2617                         .bc_req_portal          = OST_IO_PORTAL,
2618                         .bc_rep_portal          = OSC_REPLY_PORTAL,
2619                 },
2620                 .psc_thr                = {
2621                         .tc_thr_name            = "ll_ost_io",
2622                         .tc_thr_factor          = OSS_THR_FACTOR,
2623                         .tc_nthrs_init          = OSS_NTHRS_INIT,
2624                         .tc_nthrs_base          = OSS_NTHRS_BASE,
2625                         .tc_nthrs_max           = OSS_NTHRS_MAX,
2626                         .tc_nthrs_user          = oss_num_threads,
2627                         .tc_cpu_affinity        = 1,
2628                         .tc_ctx_tags            = LCT_DT_THREAD,
2629                 },
2630                 .psc_cpt                = {
2631                         .cc_cptable             = ost_io_cptable,
2632                         .cc_pattern             = ost_io_cptable == NULL ?
2633                                                   oss_io_cpts : NULL,
2634                 },
2635                 .psc_ops                = {
2636                         .so_thr_init            = ost_io_thread_init,
2637                         .so_thr_done            = ost_io_thread_done,
2638                         .so_req_handler         = ost_handle,
2639                         .so_hpreq_handler       = ost_io_hpreq_handler,
2640                         .so_req_printer         = target_print_req,
2641                 },
2642         };
2643         ost->ost_io_service = ptlrpc_register_service(&svc_conf,
2644                                                       obd->obd_proc_entry);
2645         if (IS_ERR(ost->ost_io_service)) {
2646                 rc = PTR_ERR(ost->ost_io_service);
2647                 CERROR("failed to start OST I/O service: %d\n", rc);
2648                 ost->ost_io_service = NULL;
2649                 GOTO(out_create, rc);
2650         }
2651
2652         memset(&svc_conf, 0, sizeof(svc_conf));
2653         svc_conf = (typeof(svc_conf)) {
2654                 .psc_name               = "ost_seq",
2655                 .psc_watchdog_factor    = OSS_SERVICE_WATCHDOG_FACTOR,
2656                 .psc_buf                = {
2657                         .bc_nbufs               = OST_NBUFS,
2658                         .bc_buf_size            = OST_BUFSIZE,
2659                         .bc_req_max_size        = OST_MAXREQSIZE,
2660                         .bc_rep_max_size        = OST_MAXREPSIZE,
2661                         .bc_req_portal          = SEQ_DATA_PORTAL,
2662                         .bc_rep_portal          = OSC_REPLY_PORTAL,
2663                 },
2664                 .psc_thr                = {
2665                         .tc_thr_name            = "ll_ost_seq",
2666                         .tc_thr_factor          = OSS_CR_THR_FACTOR,
2667                         .tc_nthrs_init          = OSS_CR_NTHRS_INIT,
2668                         .tc_nthrs_base          = OSS_CR_NTHRS_BASE,
2669                         .tc_nthrs_max           = OSS_CR_NTHRS_MAX,
2670                         .tc_nthrs_user          = oss_num_create_threads,
2671                         .tc_cpu_affinity        = 1,
2672                         .tc_ctx_tags            = LCT_DT_THREAD,
2673                 },
2674
2675                 .psc_cpt                = {
2676                         .cc_pattern          = oss_cpts,
2677                 },
2678                 .psc_ops                = {
2679                         .so_req_handler         = ost_handle,
2680                         .so_req_printer         = target_print_req,
2681                         .so_hpreq_handler       = NULL,
2682                 },
2683         };
2684         ost->ost_seq_service = ptlrpc_register_service(&svc_conf,
2685                                                       obd->obd_proc_entry);
2686         if (IS_ERR(ost->ost_seq_service)) {
2687                 rc = PTR_ERR(ost->ost_seq_service);
2688                 CERROR("failed to start OST seq service: %d\n", rc);
2689                 ost->ost_seq_service = NULL;
2690                 GOTO(out_io, rc);
2691         }
2692
2693         ping_evictor_start();
2694
2695         RETURN(0);
2696 out_io:
2697         ptlrpc_unregister_service(ost->ost_io_service);
2698         ost->ost_io_service = NULL;
2699 out_create:
2700         ptlrpc_unregister_service(ost->ost_create_service);
2701         ost->ost_create_service = NULL;
2702 out_service:
2703         ptlrpc_unregister_service(ost->ost_service);
2704         ost->ost_service = NULL;
2705 out_lprocfs:
2706         lprocfs_obd_cleanup(obd);
2707         RETURN(rc);
2708 }
2709
2710 static int ost_cleanup(struct obd_device *obd)
2711 {
2712         struct ost_obd *ost = &obd->u.ost;
2713         int err = 0;
2714         ENTRY;
2715
2716         ping_evictor_stop();
2717
2718         /* there is no recovery for OST OBD, all recovery is controlled by
2719          * obdfilter OBD */
2720         LASSERT(obd->obd_recovering == 0);
2721         mutex_lock(&ost->ost_health_mutex);
2722         ptlrpc_unregister_service(ost->ost_service);
2723         ptlrpc_unregister_service(ost->ost_create_service);
2724         ptlrpc_unregister_service(ost->ost_io_service);
2725         ptlrpc_unregister_service(ost->ost_seq_service);
2726         ost->ost_service = NULL;
2727         ost->ost_create_service = NULL;
2728         ost->ost_io_service = NULL;
2729         ost->ost_seq_service = NULL;
2730
2731         mutex_unlock(&ost->ost_health_mutex);
2732
2733         lprocfs_obd_cleanup(obd);
2734
2735         if (ost_io_cptable != NULL) {
2736                 cfs_cpt_table_free(ost_io_cptable);
2737                 ost_io_cptable = NULL;
2738         }
2739
2740         RETURN(err);
2741 }
2742
2743 static int ost_health_check(const struct lu_env *env, struct obd_device *obd)
2744 {
2745         struct ost_obd *ost = &obd->u.ost;
2746         int rc = 0;
2747
2748         mutex_lock(&ost->ost_health_mutex);
2749         rc |= ptlrpc_service_health_check(ost->ost_service);
2750         rc |= ptlrpc_service_health_check(ost->ost_create_service);
2751         rc |= ptlrpc_service_health_check(ost->ost_io_service);
2752         mutex_unlock(&ost->ost_health_mutex);
2753
2754         /*
2755          * health_check to return 0 on healthy
2756          * and 1 on unhealthy.
2757          */
2758         if( rc != 0)
2759                 rc = 1;
2760
2761         return rc;
2762 }
2763
2764 struct ost_thread_local_cache *ost_tls(struct ptlrpc_request *r)
2765 {
2766         return (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
2767 }
2768
2769 /* use obd ops to offer management infrastructure */
2770 static struct obd_ops ost_obd_ops = {
2771         .o_owner        = THIS_MODULE,
2772         .o_setup        = ost_setup,
2773         .o_cleanup      = ost_cleanup,
2774         .o_health_check = ost_health_check,
2775 };
2776
2777
2778 static int __init ost_init(void)
2779 {
2780         struct lprocfs_static_vars lvars;
2781         int rc;
2782         ENTRY;
2783
2784         ost_page_to_corrupt = cfs_alloc_page(CFS_ALLOC_STD);
2785
2786         lprocfs_ost_init_vars(&lvars);
2787         rc = class_register_type(&ost_obd_ops, NULL, lvars.module_vars,
2788                                  LUSTRE_OSS_NAME, NULL);
2789
2790         if (ost_num_threads != 0 && oss_num_threads == 0) {
2791                 LCONSOLE_INFO("ost_num_threads module parameter is deprecated, "
2792                               "use oss_num_threads instead or unset both for "
2793                               "dynamic thread startup\n");
2794                 oss_num_threads = ost_num_threads;
2795         }
2796
2797         RETURN(rc);
2798 }
2799
2800 static void /*__exit*/ ost_exit(void)
2801 {
2802         if (ost_page_to_corrupt)
2803                 page_cache_release(ost_page_to_corrupt);
2804
2805         class_unregister_type(LUSTRE_OSS_NAME);
2806 }
2807
2808 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2809 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
2810 MODULE_LICENSE("GPL");
2811
2812 module_init(ost_init);
2813 module_exit(ost_exit);