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