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