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