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