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