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