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