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