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