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