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