Whamcloud - gitweb
b=22417 remove obd arg from ptlrpc_start_thread
[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_commitrw, 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 out_commitrw:
950         /* Must commit after prep above in all cases */
951         rc = obd_commitrw(OBD_BRW_READ, exp, &body->oa, 1, ioo,
952                           remote_nb, npages, local_nb, oti, rc);
953
954         if (rc == 0) {
955                 repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
956                 memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
957                 ost_drop_id(exp, &repbody->oa);
958         }
959
960 out_lock:
961         ost_brw_lock_put(LCK_PR, ioo, remote_nb, &lockh);
962 out_tls:
963         ost_tls_put(req);
964 out_bulk:
965         if (desc)
966                 ptlrpc_free_bulk(desc);
967 out:
968         LASSERT(rc <= 0);
969         if (rc == 0) {
970                 req->rq_status = nob;
971                 ptlrpc_lprocfs_brw(req, nob);
972                 target_committed_to_req(req);
973                 ptlrpc_reply(req);
974         } else if (!no_reply) {
975                 /* Only reply if there was no comms problem with bulk */
976                 target_committed_to_req(req);
977                 req->rq_status = rc;
978                 ptlrpc_error(req);
979         } else {
980                 /* reply out callback would free */
981                 ptlrpc_req_drop_rs(req);
982                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
983                       "client will retry\n",
984                       exp->exp_obd->obd_name,
985                       exp->exp_client_uuid.uuid,
986                       exp->exp_connection->c_remote_uuid.uuid,
987                       libcfs_id2str(req->rq_peer));
988         }
989
990         RETURN(rc);
991 }
992
993 static int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
994 {
995         struct ptlrpc_bulk_desc *desc = NULL;
996         struct obd_export       *exp = req->rq_export;
997         struct niobuf_remote    *remote_nb;
998         struct niobuf_local     *local_nb;
999         struct obd_ioobj        *ioo;
1000         struct ost_body         *body, *repbody;
1001         struct l_wait_info       lwi;
1002         struct lustre_handle     lockh = {0};
1003         struct lustre_capa      *capa = NULL;
1004         __u32                   *rcs;
1005         int objcount, niocount, npages;
1006         int rc, i, j;
1007         obd_count                client_cksum = 0, server_cksum = 0;
1008         cksum_type_t             cksum_type = OBD_CKSUM_CRC32;
1009         int                      no_reply = 0, mmap = 0;
1010         __u32                    o_uid = 0, o_gid = 0;
1011         struct ost_thread_local_cache *tls;
1012         ENTRY;
1013
1014         req->rq_bulk_write = 1;
1015
1016         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
1017                 GOTO(out, rc = -EIO);
1018         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK2))
1019                 GOTO(out, rc = -EFAULT);
1020
1021         /* pause before transaction has been started */
1022         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, (obd_timeout + 1) / 4);
1023
1024         /* Check if there is eviction in progress, and if so, wait for it to
1025          * finish */
1026         if (unlikely(cfs_atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
1027                 lwi = LWI_INTR(NULL, NULL); // We do not care how long it takes
1028                 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
1029                         !cfs_atomic_read(&exp->exp_obd->obd_evict_inprogress),
1030                         &lwi);
1031         }
1032         if (exp->exp_failed)
1033                 GOTO(out, rc = -ENOTCONN);
1034
1035         /* ost_body, ioobj & noibuf_remote are verified and swabbed in
1036          * ost_rw_hpreq_check(). */
1037         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1038         if (body == NULL)
1039                 GOTO(out, rc = -EFAULT);
1040
1041         objcount = req_capsule_get_size(&req->rq_pill, &RMF_OBD_IOOBJ,
1042                                         RCL_CLIENT) / sizeof(*ioo);
1043         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1044         if (ioo == NULL)
1045                 GOTO(out, rc = -EFAULT);
1046
1047         rc = ost_validate_obdo(exp, &body->oa, ioo);
1048         if (rc)
1049                 RETURN(rc);
1050
1051         for (niocount = i = 0; i < objcount; i++)
1052                 niocount += ioo[i].ioo_bufcnt;
1053
1054         /*
1055          * It'd be nice to have a capsule function to indicate how many elements
1056          * there were in a buffer for an RMF that's declared to be an array.
1057          * It's easy enough to compute the number of elements here though.
1058          */
1059         remote_nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1060         if (remote_nb == NULL || niocount != (req_capsule_get_size(&req->rq_pill,
1061             &RMF_NIOBUF_REMOTE, RCL_CLIENT) / sizeof(*remote_nb)))
1062                 GOTO(out, rc = -EFAULT);
1063
1064         if ((remote_nb[0].flags & OBD_BRW_MEMALLOC) &&
1065             (exp->exp_connection->c_peer.nid == exp->exp_connection->c_self))
1066                 cfs_memory_pressure_set();
1067
1068         if (body->oa.o_valid & OBD_MD_FLOSSCAPA) {
1069                 capa = req_capsule_client_get(&req->rq_pill, &RMF_CAPA1);
1070                 if (capa == NULL) {
1071                         CERROR("Missing capability for OST BRW WRITE");
1072                         GOTO(out, rc = -EFAULT);
1073                 }
1074         }
1075
1076         req_capsule_set_size(&req->rq_pill, &RMF_RCS, RCL_SERVER,
1077                              niocount * sizeof(*rcs));
1078         rc = req_capsule_server_pack(&req->rq_pill);
1079         if (rc != 0)
1080                 GOTO(out, rc);
1081         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_PACK, obd_fail_val);
1082         rcs = req_capsule_server_get(&req->rq_pill, &RMF_RCS);
1083
1084         tls = ost_tls_get(req);
1085         if (tls == NULL)
1086                 GOTO(out_bulk, rc = -ENOMEM);
1087         local_nb = tls->local;
1088
1089         rc = ost_brw_lock_get(LCK_PW, exp, ioo, remote_nb, &lockh);
1090         if (rc != 0)
1091                 GOTO(out_tls, rc);
1092
1093         /*
1094          * If getting the lock took more time than
1095          * client was willing to wait, drop it. b=11330
1096          */
1097         if (cfs_time_current_sec() > req->rq_deadline ||
1098             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
1099                 no_reply = 1;
1100                 CERROR("Dropping timed-out write from %s because locking "
1101                        "object "LPX64" took %ld seconds (limit was %ld).\n",
1102                        libcfs_id2str(req->rq_peer), ioo->ioo_id,
1103                        cfs_time_current_sec() - req->rq_arrival_time.tv_sec,
1104                        req->rq_deadline - req->rq_arrival_time.tv_sec);
1105                 GOTO(out_lock, rc = -ETIMEDOUT);
1106         }
1107
1108         if (!lustre_handle_is_used(&lockh))
1109                 /* no needs to try to prolong lock if server is asked
1110                  * to handle locking (= OBD_BRW_SRVLOCK) */
1111                 ost_rw_prolong_locks(req, ioo, remote_nb,&body->oa,  LCK_PW);
1112
1113         /* obd_preprw clobbers oa->valid, so save what we need */
1114         if (body->oa.o_valid & OBD_MD_FLCKSUM) {
1115                 client_cksum = body->oa.o_cksum;
1116                 if (body->oa.o_valid & OBD_MD_FLFLAGS)
1117                         cksum_type = cksum_type_unpack(body->oa.o_flags);
1118         }
1119         if (body->oa.o_valid & OBD_MD_FLFLAGS && body->oa.o_flags & OBD_FL_MMAP)
1120                 mmap = 1;
1121
1122         /* Because we already sync grant info with client when reconnect,
1123          * grant info will be cleared for resent req, then fed_grant and
1124          * total_grant will not be modified in following preprw_write */
1125         if (lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY)) {
1126                 DEBUG_REQ(D_CACHE, req, "clear resent/replay req grant info");
1127                 body->oa.o_valid &= ~OBD_MD_FLGRANT;
1128         }
1129
1130         if (exp_connect_rmtclient(exp)) {
1131                 o_uid = body->oa.o_uid;
1132                 o_gid = body->oa.o_gid;
1133         }
1134         npages = OST_THREAD_POOL_SIZE;
1135         rc = obd_preprw(OBD_BRW_WRITE, exp, &body->oa, objcount,
1136                         ioo, remote_nb, &npages, local_nb, oti, capa);
1137         if (rc != 0)
1138                 GOTO(out_lock, rc);
1139
1140         desc = ptlrpc_prep_bulk_exp(req, npages,
1141                                      BULK_GET_SINK, OST_BULK_PORTAL);
1142         if (desc == NULL)
1143                 GOTO(skip_transfer, rc = -ENOMEM);
1144
1145         /* NB Having prepped, we must commit... */
1146
1147         for (i = 0; i < npages; i++)
1148                 ptlrpc_prep_bulk_page(desc, local_nb[i].page,
1149                                       local_nb[i].offset & ~CFS_PAGE_MASK,
1150                                       local_nb[i].len);
1151
1152         rc = sptlrpc_svc_prep_bulk(req, desc);
1153         if (rc != 0)
1154                 GOTO(out_lock, rc);
1155
1156         /* Check if client was evicted or tried to reconnect while we
1157          * were doing i/o before touching network */
1158         if (desc->bd_export->exp_failed ||
1159             desc->bd_export->exp_abort_active_req)
1160                 rc = -ENOTCONN;
1161         else
1162                 rc = ptlrpc_start_bulk_transfer(desc);
1163         if (rc == 0) {
1164                 time_t start = cfs_time_current_sec();
1165                 do {
1166                         long timeoutl = req->rq_deadline -
1167                                 cfs_time_current_sec();
1168                         cfs_duration_t timeout = timeoutl <= 0 ?
1169                                 CFS_TICK : cfs_time_seconds(timeoutl);
1170                         lwi = LWI_TIMEOUT_INTERVAL(timeout, cfs_time_seconds(1),
1171                                                    ost_bulk_timeout, desc);
1172                         rc = l_wait_event(desc->bd_waitq,
1173                                           !ptlrpc_server_bulk_active(desc) ||
1174                                           desc->bd_export->exp_failed ||
1175                                           desc->bd_export->exp_abort_active_req,
1176                                           &lwi);
1177                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
1178                         /* Wait again if we changed deadline */
1179                 } while ((rc == -ETIMEDOUT) &&
1180                          (req->rq_deadline > cfs_time_current_sec()));
1181
1182                 if (rc == -ETIMEDOUT) {
1183                         DEBUG_REQ(D_ERROR, req,
1184                                   "timeout on bulk GET after %ld%+lds",
1185                                   req->rq_deadline - start,
1186                                   cfs_time_current_sec() -
1187                                   req->rq_deadline);
1188                         ptlrpc_abort_bulk(desc);
1189                 } else if (desc->bd_export->exp_failed) {
1190                         DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1191                         rc = -ENOTCONN;
1192                         ptlrpc_abort_bulk(desc);
1193                 } else if (desc->bd_export->exp_abort_active_req) {
1194                         DEBUG_REQ(D_ERROR, req, "Reconnect on bulk GET");
1195                         /* we don't reply anyway */
1196                         rc = -ETIMEDOUT;
1197                         ptlrpc_abort_bulk(desc);
1198                 } else if (!desc->bd_success) {
1199                         DEBUG_REQ(D_ERROR, req, "network error on bulk GET");
1200                         /* XXX should this be a different errno? */
1201                         rc = -ETIMEDOUT;
1202                 } else {
1203                         rc = sptlrpc_svc_unwrap_bulk(req, desc);
1204                 }
1205         } else {
1206                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1207         }
1208         no_reply = rc != 0;
1209
1210 skip_transfer:
1211         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
1212         memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
1213
1214         if (unlikely(client_cksum != 0 && rc == 0)) {
1215                 static int cksum_counter;
1216                 repbody->oa.o_valid |= OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
1217                 repbody->oa.o_flags &= ~OBD_FL_CKSUM_ALL;
1218                 repbody->oa.o_flags |= cksum_type_pack(cksum_type);
1219                 server_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
1220                 repbody->oa.o_cksum = server_cksum;
1221                 cksum_counter++;
1222                 if (unlikely(client_cksum != server_cksum)) {
1223                         CDEBUG_LIMIT(mmap ? D_INFO : D_ERROR,
1224                                      "client csum %x, server csum %x\n",
1225                                      client_cksum, server_cksum);
1226                         cksum_counter = 0;
1227                 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
1228                         CDEBUG(D_INFO, "Checksum %u from %s OK: %x\n",
1229                                cksum_counter, libcfs_id2str(req->rq_peer),
1230                                server_cksum);
1231                 }
1232         }
1233
1234         /* Must commit after prep above in all cases */
1235         rc = obd_commitrw(OBD_BRW_WRITE, exp, &repbody->oa, objcount, ioo,
1236                           remote_nb, npages, local_nb, oti, rc);
1237         if (rc == -ENOTCONN)
1238                 /* quota acquire process has been given up because
1239                  * either the client has been evicted or the client
1240                  * has timed out the request already */
1241                 no_reply = 1;
1242
1243         if (exp_connect_rmtclient(exp)) {
1244                 repbody->oa.o_uid = o_uid;
1245                 repbody->oa.o_gid = o_gid;
1246         }
1247
1248         /*
1249          * Disable sending mtime back to the client. If the client locked the
1250          * whole object, then it has already updated the mtime on its side,
1251          * otherwise it will have to glimpse anyway (see bug 21489, comment 32)
1252          */
1253         repbody->oa.o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLATIME);
1254
1255         if (unlikely(client_cksum != server_cksum && rc == 0 &&  !mmap)) {
1256                 int  new_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
1257                 char *msg;
1258                 char *via;
1259                 char *router;
1260
1261                 if (new_cksum == server_cksum)
1262                         msg = "changed in transit before arrival at OST";
1263                 else if (new_cksum == client_cksum)
1264                         msg = "initial checksum before message complete";
1265                 else
1266                         msg = "changed in transit AND after initial checksum";
1267
1268                 if (req->rq_peer.nid == desc->bd_sender) {
1269                         via = router = "";
1270                 } else {
1271                         via = " via ";
1272                         router = libcfs_nid2str(desc->bd_sender);
1273                 }
1274
1275                 LCONSOLE_ERROR_MSG(0x168, "%s: BAD WRITE CHECKSUM: %s from "
1276                                    "%s%s%s inode "DFID" object "
1277                                    LPU64"/"LPU64" extent ["LPU64"-"LPU64"]\n",
1278                                    exp->exp_obd->obd_name, msg,
1279                                    libcfs_id2str(req->rq_peer),
1280                                    via, router,
1281                                    body->oa.o_valid & OBD_MD_FLFID ?
1282                                                 body->oa.o_parent_seq : (__u64)0,
1283                                    body->oa.o_valid & OBD_MD_FLFID ?
1284                                                 body->oa.o_parent_oid : 0,
1285                                    body->oa.o_valid & OBD_MD_FLFID ?
1286                                                 body->oa.o_parent_ver : 0,
1287                                    body->oa.o_id,
1288                                    body->oa.o_valid & OBD_MD_FLGROUP ?
1289                                                 body->oa.o_seq : (__u64)0,
1290                                    local_nb[0].offset,
1291                                    local_nb[npages-1].offset +
1292                                    local_nb[npages-1].len - 1 );
1293                 CERROR("client csum %x, original server csum %x, "
1294                        "server csum now %x\n",
1295                        client_cksum, server_cksum, new_cksum);
1296         }
1297
1298         if (rc == 0) {
1299                 int nob = 0;
1300
1301                 /* set per-requested niobuf return codes */
1302                 for (i = j = 0; i < niocount; i++) {
1303                         int len = remote_nb[i].len;
1304
1305                         nob += len;
1306                         rcs[i] = 0;
1307                         do {
1308                                 LASSERT(j < npages);
1309                                 if (local_nb[j].rc < 0)
1310                                         rcs[i] = local_nb[j].rc;
1311                                 len -= local_nb[j].len;
1312                                 j++;
1313                         } while (len > 0);
1314                         LASSERT(len == 0);
1315                 }
1316                 LASSERT(j == npages);
1317                 ptlrpc_lprocfs_brw(req, nob);
1318         }
1319
1320 out_lock:
1321         ost_brw_lock_put(LCK_PW, ioo, remote_nb, &lockh);
1322 out_tls:
1323         ost_tls_put(req);
1324 out_bulk:
1325         if (desc)
1326                 ptlrpc_free_bulk(desc);
1327 out:
1328         if (rc == 0) {
1329                 oti_to_request(oti, req);
1330                 target_committed_to_req(req);
1331                 rc = ptlrpc_reply(req);
1332         } else if (!no_reply) {
1333                 /* Only reply if there was no comms problem with bulk */
1334                 target_committed_to_req(req);
1335                 req->rq_status = rc;
1336                 ptlrpc_error(req);
1337         } else {
1338                 /* reply out callback would free */
1339                 ptlrpc_req_drop_rs(req);
1340                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
1341                       "client will retry\n",
1342                       exp->exp_obd->obd_name,
1343                       exp->exp_client_uuid.uuid,
1344                       exp->exp_connection->c_remote_uuid.uuid,
1345                       libcfs_id2str(req->rq_peer));
1346         }
1347         cfs_memory_pressure_clr();
1348         RETURN(rc);
1349 }
1350
1351 /**
1352  * Implementation of OST_SET_INFO.
1353  *
1354  * OST_SET_INFO is like ioctl(): heavily overloaded.  Specifically, it takes a
1355  * "key" and a value RPC buffers as arguments, with the value's contents
1356  * interpreted according to the key.
1357  *
1358  * Value types that need swabbing have swabbing done explicitly, either here or
1359  * in functions called from here.  This should be corrected: all swabbing should
1360  * be done in the capsule abstraction, as that will then allow us to move
1361  * swabbing exclusively to the client without having to modify server code
1362  * outside the capsule abstraction's implementation itself.  To correct this
1363  * will require minor changes to the capsule abstraction; see the comments for
1364  * req_capsule_extend() in layout.c.
1365  */
1366 static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
1367 {
1368         struct ost_body *body = NULL, *repbody;
1369         char *key, *val = NULL;
1370         int keylen, vallen, rc = 0;
1371         int is_grant_shrink = 0;
1372         ENTRY;
1373
1374         key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1375         if (key == NULL) {
1376                 DEBUG_REQ(D_HA, req, "no set_info key");
1377                 RETURN(-EFAULT);
1378         }
1379         keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
1380                                       RCL_CLIENT);
1381
1382         vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
1383                                       RCL_CLIENT);
1384
1385         if ((is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK)))
1386                 /* In this case the value is actually an RMF_OST_BODY, so we
1387                  * transmutate the type of this PTLRPC */
1388                 req_capsule_extend(&req->rq_pill, &RQF_OST_SET_GRANT_INFO);
1389
1390         rc = req_capsule_server_pack(&req->rq_pill);
1391         if (rc)
1392                 RETURN(rc);
1393
1394         if (vallen) {
1395                 if (is_grant_shrink) {
1396                         body = req_capsule_client_get(&req->rq_pill,
1397                                                       &RMF_OST_BODY);
1398                         if (!body)
1399                                 RETURN(-EFAULT);
1400
1401                         repbody = req_capsule_server_get(&req->rq_pill,
1402                                                          &RMF_OST_BODY);
1403                         memcpy(repbody, body, sizeof(*body));
1404                         val = (char*)repbody;
1405                 } else {
1406                         val = req_capsule_client_get(&req->rq_pill,
1407                                                      &RMF_SETINFO_VAL);
1408                 }
1409         }
1410
1411         if (KEY_IS(KEY_EVICT_BY_NID)) {
1412                 if (val && vallen)
1413                         obd_export_evict_by_nid(exp->exp_obd, val);
1414                 GOTO(out, rc = 0);
1415         } else if (KEY_IS(KEY_MDS_CONN) && ptlrpc_req_need_swab(req)) {
1416                 if (vallen < sizeof(__u32))
1417                         RETURN(-EFAULT);
1418                 __swab32s((__u32 *)val);
1419         }
1420
1421         /* OBD will also check if KEY_IS(KEY_GRANT_SHRINK), and will cast val to
1422          * a struct ost_body * value */
1423         rc = obd_set_info_async(exp, keylen, key, vallen, val, NULL);
1424 out:
1425         lustre_msg_set_status(req->rq_repmsg, 0);
1426         RETURN(rc);
1427 }
1428
1429 static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
1430 {
1431         void *key, *reply;
1432         int keylen, replylen, rc = 0;
1433         struct req_capsule *pill = &req->rq_pill;
1434         ENTRY;
1435
1436         /* this common part for get_info rpc */
1437         key = req_capsule_client_get(pill, &RMF_SETINFO_KEY);
1438         if (key == NULL) {
1439                 DEBUG_REQ(D_HA, req, "no get_info key");
1440                 RETURN(-EFAULT);
1441         }
1442         keylen = req_capsule_get_size(pill, &RMF_SETINFO_KEY, RCL_CLIENT);
1443
1444         if (KEY_IS(KEY_FIEMAP)) {
1445                 struct ll_fiemap_info_key *fm_key = key;
1446                 int rc;
1447
1448                 rc = ost_validate_obdo(exp, &fm_key->oa, NULL);
1449                 if (rc)
1450                         RETURN(rc);
1451         }
1452
1453         rc = obd_get_info(exp, keylen, key, &replylen, NULL, NULL);
1454         if (rc)
1455                 RETURN(rc);
1456
1457         req_capsule_set_size(pill, &RMF_GENERIC_DATA,
1458                              RCL_SERVER, replylen);
1459
1460         rc = req_capsule_server_pack(pill);
1461         if (rc)
1462                 RETURN(rc);
1463
1464         reply = req_capsule_server_get(pill, &RMF_GENERIC_DATA);
1465         if (reply == NULL)
1466                 RETURN(-ENOMEM);
1467
1468         /* call again to fill in the reply buffer */
1469         rc = obd_get_info(exp, keylen, key, &replylen, reply, NULL);
1470
1471         lustre_msg_set_status(req->rq_repmsg, 0);
1472         RETURN(rc);
1473 }
1474
1475 #ifdef HAVE_QUOTA_SUPPORT
1476 static int ost_handle_quotactl(struct ptlrpc_request *req)
1477 {
1478         struct obd_quotactl *oqctl, *repoqc;
1479         int rc;
1480         ENTRY;
1481
1482         oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1483         if (oqctl == NULL)
1484                 GOTO(out, rc = -EPROTO);
1485
1486         rc = req_capsule_server_pack(&req->rq_pill);
1487         if (rc)
1488                 GOTO(out, rc);
1489
1490         repoqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1491         req->rq_status = obd_quotactl(req->rq_export, oqctl);
1492         *repoqc = *oqctl;
1493
1494 out:
1495         RETURN(rc);
1496 }
1497
1498 static int ost_handle_quotacheck(struct ptlrpc_request *req)
1499 {
1500         struct obd_quotactl *oqctl;
1501         int rc;
1502         ENTRY;
1503
1504         oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1505         if (oqctl == NULL)
1506                 RETURN(-EPROTO);
1507
1508         rc = req_capsule_server_pack(&req->rq_pill);
1509         if (rc)
1510                 RETURN(-ENOMEM);
1511
1512         req->rq_status = obd_quotacheck(req->rq_export, oqctl);
1513         RETURN(0);
1514 }
1515
1516 static int ost_handle_quota_adjust_qunit(struct ptlrpc_request *req)
1517 {
1518         struct quota_adjust_qunit *oqaq, *repoqa;
1519         struct lustre_quota_ctxt *qctxt;
1520         int rc;
1521         ENTRY;
1522
1523         qctxt = &req->rq_export->exp_obd->u.obt.obt_qctxt;
1524         oqaq = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
1525         if (oqaq == NULL)
1526                 GOTO(out, rc = -EPROTO);
1527
1528         rc = req_capsule_server_pack(&req->rq_pill);
1529         if (rc)
1530                 GOTO(out, rc);
1531
1532         repoqa = req_capsule_server_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
1533         req->rq_status = obd_quota_adjust_qunit(req->rq_export, oqaq, qctxt, NULL);
1534         *repoqa = *oqaq;
1535
1536  out:
1537         RETURN(rc);
1538 }
1539 #endif
1540
1541 static int ost_llog_handle_connect(struct obd_export *exp,
1542                                    struct ptlrpc_request *req)
1543 {
1544         struct llogd_conn_body *body;
1545         int rc;
1546         ENTRY;
1547
1548         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_CONN_BODY);
1549         rc = obd_llog_connect(exp, body);
1550         RETURN(rc);
1551 }
1552
1553 #define ost_init_sec_none(reply, exp)                                   \
1554 do {                                                                    \
1555         reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |          \
1556                                       OBD_CONNECT_RMT_CLIENT_FORCE |    \
1557                                       OBD_CONNECT_OSS_CAPA);            \
1558         cfs_spin_lock(&exp->exp_lock);                                  \
1559         exp->exp_connect_flags = reply->ocd_connect_flags;              \
1560         cfs_spin_unlock(&exp->exp_lock);                                \
1561 } while (0)
1562
1563 static int ost_init_sec_level(struct ptlrpc_request *req)
1564 {
1565         struct obd_export *exp = req->rq_export;
1566         struct req_capsule *pill = &req->rq_pill;
1567         struct obd_device *obd = exp->exp_obd;
1568         struct filter_obd *filter = &obd->u.filter;
1569         char *client = libcfs_nid2str(req->rq_peer.nid);
1570         struct obd_connect_data *data, *reply;
1571         int rc = 0, remote;
1572         ENTRY;
1573
1574         data = req_capsule_client_get(pill, &RMF_CONNECT_DATA);
1575         reply = req_capsule_server_get(pill, &RMF_CONNECT_DATA);
1576         if (data == NULL || reply == NULL)
1577                 RETURN(-EFAULT);
1578
1579         /* connection from MDT is always trusted */
1580         if (req->rq_auth_usr_mdt) {
1581                 ost_init_sec_none(reply, exp);
1582                 RETURN(0);
1583         }
1584
1585         /* no GSS support case */
1586         if (!req->rq_auth_gss) {
1587                 if (filter->fo_sec_level > LUSTRE_SEC_NONE) {
1588                         CWARN("client %s -> target %s does not user GSS, "
1589                               "can not run under security level %d.\n",
1590                               client, obd->obd_name, filter->fo_sec_level);
1591                         RETURN(-EACCES);
1592                 } else {
1593                         ost_init_sec_none(reply, exp);
1594                         RETURN(0);
1595                 }
1596         }
1597
1598         /* old version case */
1599         if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
1600                      !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
1601                 if (filter->fo_sec_level > LUSTRE_SEC_NONE) {
1602                         CWARN("client %s -> target %s uses old version, "
1603                               "can not run under security level %d.\n",
1604                               client, obd->obd_name, filter->fo_sec_level);
1605                         RETURN(-EACCES);
1606                 } else {
1607                         CWARN("client %s -> target %s uses old version, "
1608                               "run under security level %d.\n",
1609                               client, obd->obd_name, filter->fo_sec_level);
1610                         ost_init_sec_none(reply, exp);
1611                         RETURN(0);
1612                 }
1613         }
1614
1615         remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
1616         if (remote) {
1617                 if (!req->rq_auth_remote)
1618                         CDEBUG(D_SEC, "client (local realm) %s -> target %s "
1619                                "asked to be remote.\n", client, obd->obd_name);
1620         } else if (req->rq_auth_remote) {
1621                 remote = 1;
1622                 CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
1623                        "as remote by default.\n", client, obd->obd_name);
1624         }
1625
1626         if (remote) {
1627                 if (!filter->fo_fl_oss_capa) {
1628                         CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
1629                                " but OSS capabilities are not enabled: %d.\n",
1630                                client, obd->obd_name, filter->fo_fl_oss_capa);
1631                         RETURN(-EACCES);
1632                 }
1633         }
1634
1635         switch (filter->fo_sec_level) {
1636         case LUSTRE_SEC_NONE:
1637                 if (!remote) {
1638                         ost_init_sec_none(reply, exp);
1639                         break;
1640                 } else {
1641                         CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
1642                                "can not run under security level %d.\n",
1643                                client, obd->obd_name, filter->fo_sec_level);
1644                         RETURN(-EACCES);
1645                 }
1646         case LUSTRE_SEC_REMOTE:
1647                 if (!remote)
1648                         ost_init_sec_none(reply, exp);
1649                 break;
1650         case LUSTRE_SEC_ALL:
1651                 if (!remote) {
1652                         reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |
1653                                                       OBD_CONNECT_RMT_CLIENT_FORCE);
1654                         if (!filter->fo_fl_oss_capa)
1655                                 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
1656
1657                         cfs_spin_lock(&exp->exp_lock);
1658                         exp->exp_connect_flags = reply->ocd_connect_flags;
1659                         cfs_spin_unlock(&exp->exp_lock);
1660                 }
1661                 break;
1662         default:
1663                 RETURN(-EINVAL);
1664         }
1665
1666         RETURN(rc);
1667 }
1668
1669 /*
1670  * FIXME
1671  * this should be done in filter_connect()/filter_reconnect(), but
1672  * we can't obtain information like NID, which stored in incoming
1673  * request, thus can't decide what flavor to use. so we do it here.
1674  *
1675  * This hack should be removed after the OST stack be rewritten, just
1676  * like what we are doing in mdt_obd_connect()/mdt_obd_reconnect().
1677  */
1678 static int ost_connect_check_sptlrpc(struct ptlrpc_request *req)
1679 {
1680         struct obd_export     *exp = req->rq_export;
1681         struct filter_obd     *filter = &exp->exp_obd->u.filter;
1682         struct sptlrpc_flavor  flvr;
1683         int                    rc = 0;
1684
1685         if (unlikely(strcmp(exp->exp_obd->obd_type->typ_name,
1686                             LUSTRE_ECHO_NAME) == 0)) {
1687                 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_ANY;
1688                 return 0;
1689         }
1690
1691         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
1692                 cfs_read_lock(&filter->fo_sptlrpc_lock);
1693                 sptlrpc_target_choose_flavor(&filter->fo_sptlrpc_rset,
1694                                              req->rq_sp_from,
1695                                              req->rq_peer.nid,
1696                                              &flvr);
1697                 cfs_read_unlock(&filter->fo_sptlrpc_lock);
1698
1699                 cfs_spin_lock(&exp->exp_lock);
1700
1701                 exp->exp_sp_peer = req->rq_sp_from;
1702                 exp->exp_flvr = flvr;
1703
1704                 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
1705                     exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
1706                         CERROR("unauthorized rpc flavor %x from %s, "
1707                                "expect %x\n", req->rq_flvr.sf_rpc,
1708                                libcfs_nid2str(req->rq_peer.nid),
1709                                exp->exp_flvr.sf_rpc);
1710                         rc = -EACCES;
1711                 }
1712
1713                 cfs_spin_unlock(&exp->exp_lock);
1714         } else {
1715                 if (exp->exp_sp_peer != req->rq_sp_from) {
1716                         CERROR("RPC source %s doesn't match %s\n",
1717                                sptlrpc_part2name(req->rq_sp_from),
1718                                sptlrpc_part2name(exp->exp_sp_peer));
1719                         rc = -EACCES;
1720                 } else {
1721                         rc = sptlrpc_target_export_check(exp, req);
1722                 }
1723         }
1724
1725         return rc;
1726 }
1727
1728 /* Ensure that data and metadata are synced to the disk when lock is cancelled
1729  * (if requested) */
1730 int ost_blocking_ast(struct ldlm_lock *lock,
1731                              struct ldlm_lock_desc *desc,
1732                              void *data, int flag)
1733 {
1734         __u32 sync_lock_cancel = 0;
1735         __u32 len = sizeof(sync_lock_cancel);
1736         int rc = 0;
1737         ENTRY;
1738
1739         rc = obd_get_info(lock->l_export, sizeof(KEY_SYNC_LOCK_CANCEL),
1740                           KEY_SYNC_LOCK_CANCEL, &len, &sync_lock_cancel, NULL);
1741
1742         if (!rc && flag == LDLM_CB_CANCELING &&
1743             (lock->l_granted_mode & (LCK_PW|LCK_GROUP)) &&
1744             (sync_lock_cancel == ALWAYS_SYNC_ON_CANCEL ||
1745              (sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL &&
1746               lock->l_flags & LDLM_FL_CBPENDING))) {
1747                 struct obdo *oa;
1748                 int rc;
1749
1750                 OBDO_ALLOC(oa);
1751                 oa->o_id = lock->l_resource->lr_name.name[0];
1752                 oa->o_seq = lock->l_resource->lr_name.name[1];
1753                 oa->o_valid = OBD_MD_FLID|OBD_MD_FLGROUP;
1754
1755                 rc = obd_sync(lock->l_export, oa, NULL,
1756                               lock->l_policy_data.l_extent.start,
1757                               lock->l_policy_data.l_extent.end, NULL);
1758                 if (rc)
1759                         CERROR("Error %d syncing data on lock cancel\n", rc);
1760
1761                 OBDO_FREE(oa);
1762         }
1763
1764         return ldlm_server_blocking_ast(lock, desc, data, flag);
1765 }
1766
1767 static int ost_filter_recovery_request(struct ptlrpc_request *req,
1768                                        struct obd_device *obd, int *process)
1769 {
1770         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1771         case OST_CONNECT: /* This will never get here, but for completeness. */
1772         case OST_DISCONNECT:
1773                *process = 1;
1774                RETURN(0);
1775
1776         case OBD_PING:
1777         case OST_CREATE:
1778         case OST_DESTROY:
1779         case OST_PUNCH:
1780         case OST_SETATTR:
1781         case OST_SYNC:
1782         case OST_WRITE:
1783         case OBD_LOG_CANCEL:
1784         case LDLM_ENQUEUE:
1785                 *process = target_queue_recovery_request(req, obd);
1786                 RETURN(0);
1787
1788         default:
1789                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1790                 *process = -EAGAIN;
1791                 RETURN(0);
1792         }
1793 }
1794
1795 int ost_msg_check_version(struct lustre_msg *msg)
1796 {
1797         int rc;
1798
1799         switch(lustre_msg_get_opc(msg)) {
1800         case OST_CONNECT:
1801         case OST_DISCONNECT:
1802         case OBD_PING:
1803         case SEC_CTX_INIT:
1804         case SEC_CTX_INIT_CONT:
1805         case SEC_CTX_FINI:
1806                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1807                 if (rc)
1808                         CERROR("bad opc %u version %08x, expecting %08x\n",
1809                                lustre_msg_get_opc(msg),
1810                                lustre_msg_get_version(msg),
1811                                LUSTRE_OBD_VERSION);
1812                 break;
1813         case OST_CREATE:
1814         case OST_DESTROY:
1815         case OST_GETATTR:
1816         case OST_SETATTR:
1817         case OST_WRITE:
1818         case OST_READ:
1819         case OST_PUNCH:
1820         case OST_STATFS:
1821         case OST_SYNC:
1822         case OST_SET_INFO:
1823         case OST_GET_INFO:
1824 #ifdef HAVE_QUOTA_SUPPORT
1825         case OST_QUOTACHECK:
1826         case OST_QUOTACTL:
1827         case OST_QUOTA_ADJUST_QUNIT:
1828 #endif
1829                 rc = lustre_msg_check_version(msg, LUSTRE_OST_VERSION);
1830                 if (rc)
1831                         CERROR("bad opc %u version %08x, expecting %08x\n",
1832                                lustre_msg_get_opc(msg),
1833                                lustre_msg_get_version(msg),
1834                                LUSTRE_OST_VERSION);
1835                 break;
1836         case LDLM_ENQUEUE:
1837         case LDLM_CONVERT:
1838         case LDLM_CANCEL:
1839         case LDLM_BL_CALLBACK:
1840         case LDLM_CP_CALLBACK:
1841                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1842                 if (rc)
1843                         CERROR("bad opc %u version %08x, expecting %08x\n",
1844                                lustre_msg_get_opc(msg),
1845                                lustre_msg_get_version(msg),
1846                                LUSTRE_DLM_VERSION);
1847                 break;
1848         case LLOG_ORIGIN_CONNECT:
1849         case OBD_LOG_CANCEL:
1850                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1851                 if (rc)
1852                         CERROR("bad opc %u version %08x, expecting %08x\n",
1853                                lustre_msg_get_opc(msg),
1854                                lustre_msg_get_version(msg),
1855                                LUSTRE_LOG_VERSION);
1856                 break;
1857         default:
1858                 CERROR("Unexpected opcode %d\n", lustre_msg_get_opc(msg));
1859                 rc = -ENOTSUPP;
1860         }
1861         return rc;
1862 }
1863
1864 /**
1865  * Returns 1 if the given PTLRPC matches the given LDLM locks, or 0 if it does
1866  * not.
1867  */
1868 static int ost_rw_hpreq_lock_match(struct ptlrpc_request *req,
1869                                    struct ldlm_lock *lock)
1870 {
1871         struct niobuf_remote *nb;
1872         struct obd_ioobj *ioo;
1873         struct ost_body *body;
1874         int objcount, niocount;
1875         int mode, opc, i, rc;
1876         __u64 start, end;
1877         ENTRY;
1878
1879         opc = lustre_msg_get_opc(req->rq_reqmsg);
1880         LASSERT(opc == OST_READ || opc == OST_WRITE);
1881
1882         /* As the request may be covered by several locks, do not look at
1883          * o_handle, look at the RPC IO region. */
1884         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1885         if (body == NULL)
1886                 RETURN(0);
1887
1888         objcount = req_capsule_get_size(&req->rq_pill, &RMF_OBD_IOOBJ,
1889                                         RCL_CLIENT) / sizeof(*ioo);
1890         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1891         if (ioo == NULL)
1892                 RETURN(0);
1893
1894         rc = ost_validate_obdo(req->rq_export, &body->oa, ioo);
1895         if (rc)
1896                 RETURN(rc);
1897
1898         for (niocount = i = 0; i < objcount; i++)
1899                 niocount += ioo[i].ioo_bufcnt;
1900
1901         nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1902         if (nb == NULL ||
1903             niocount != (req_capsule_get_size(&req->rq_pill, &RMF_NIOBUF_REMOTE,
1904             RCL_CLIENT) / sizeof(*nb)))
1905                 RETURN(0);
1906
1907         mode = LCK_PW;
1908         if (opc == OST_READ)
1909                 mode |= LCK_PR;
1910
1911         start = nb[0].offset & CFS_PAGE_MASK;
1912         end = (nb[ioo->ioo_bufcnt - 1].offset +
1913                nb[ioo->ioo_bufcnt - 1].len - 1) | ~CFS_PAGE_MASK;
1914
1915         LASSERT(lock->l_resource != NULL);
1916         if (!osc_res_name_eq(ioo->ioo_id, ioo->ioo_seq,
1917                              &lock->l_resource->lr_name))
1918                 RETURN(0);
1919
1920         if (!(lock->l_granted_mode & mode))
1921                 RETURN(0);
1922
1923         if (lock->l_policy_data.l_extent.end < start ||
1924             lock->l_policy_data.l_extent.start > end)
1925                 RETURN(0);
1926
1927         RETURN(1);
1928 }
1929
1930 /**
1931  * High-priority queue request check for whether the given PTLRPC request (\a
1932  * req) is blocking an LDLM lock cancel.
1933  *
1934  * Returns 1 if the given given PTLRPC request (\a req) is blocking an LDLM lock
1935  * cancel, 0 if it is not, and -EFAULT if the request is malformed.
1936  *
1937  * Only OST_READs, OST_WRITEs and OST_PUNCHes go on the h-p RPC queue.  This
1938  * function looks only at OST_READs and OST_WRITEs.
1939  */
1940 static int ost_rw_hpreq_check(struct ptlrpc_request *req)
1941 {
1942         struct niobuf_remote *nb;
1943         struct obd_ioobj *ioo;
1944         struct ost_body *body;
1945         int objcount, niocount;
1946         int mode, opc, i, rc;
1947         ENTRY;
1948
1949         opc = lustre_msg_get_opc(req->rq_reqmsg);
1950         LASSERT(opc == OST_READ || opc == OST_WRITE);
1951
1952         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1953         if (body == NULL)
1954                 RETURN(-EFAULT);
1955
1956         objcount = req_capsule_get_size(&req->rq_pill, &RMF_OBD_IOOBJ,
1957                                         RCL_CLIENT) / sizeof(*ioo);
1958         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1959         if (ioo == NULL)
1960                 RETURN(-EFAULT);
1961
1962         rc = ost_validate_obdo(req->rq_export, &body->oa, ioo);
1963         if (rc)
1964                 RETURN(rc);
1965
1966         for (niocount = i = 0; i < objcount; i++)
1967                 niocount += ioo[i].ioo_bufcnt;
1968         nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1969         if (nb == NULL ||
1970             niocount != (req_capsule_get_size(&req->rq_pill, &RMF_NIOBUF_REMOTE,
1971             RCL_CLIENT) / sizeof(*nb)))
1972                 RETURN(-EFAULT);
1973         if (niocount != 0 && (nb[0].flags & OBD_BRW_SRVLOCK))
1974                 RETURN(-EFAULT);
1975
1976         mode = LCK_PW;
1977         if (opc == OST_READ)
1978                 mode |= LCK_PR;
1979         RETURN(ost_rw_prolong_locks(req, ioo, nb, &body->oa, mode));
1980 }
1981
1982 static int ost_punch_prolong_locks(struct ptlrpc_request *req, struct obdo *oa)
1983 {
1984         struct ldlm_res_id res_id = { .name = { oa->o_id } };
1985         struct ost_prolong_data opd = { 0 };
1986         __u64 start, end;
1987         ENTRY;
1988
1989         start = oa->o_size;
1990         end = start + oa->o_blocks;
1991
1992         opd.opd_mode = LCK_PW;
1993         opd.opd_exp = req->rq_export;
1994         opd.opd_policy.l_extent.start = start & CFS_PAGE_MASK;
1995         if (oa->o_blocks == OBD_OBJECT_EOF || end < start)
1996                 opd.opd_policy.l_extent.end = OBD_OBJECT_EOF;
1997         else
1998                 opd.opd_policy.l_extent.end = end | ~CFS_PAGE_MASK;
1999
2000         /* prolong locks for the current service time of the corresponding
2001          * portal (= OST_IO_PORTAL) */
2002         opd.opd_timeout = AT_OFF ? obd_timeout / 2:
2003                           max(at_est2timeout(at_get(&req->rq_rqbd->
2004                               rqbd_service->srv_at_estimate)), ldlm_timeout);
2005
2006         CDEBUG(D_DLMTRACE,"refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
2007                res_id.name[0], res_id.name[1], opd.opd_policy.l_extent.start,
2008                opd.opd_policy.l_extent.end);
2009
2010         opd.opd_oa = oa;
2011         ldlm_resource_iterate(req->rq_export->exp_obd->obd_namespace, &res_id,
2012                               ost_prolong_locks_iter, &opd);
2013         RETURN(opd.opd_lock_match);
2014 }
2015
2016 /**
2017  * Like ost_rw_hpreq_lock_match(), but for OST_PUNCH RPCs.
2018  */
2019 static int ost_punch_hpreq_lock_match(struct ptlrpc_request *req,
2020                                       struct ldlm_lock *lock)
2021 {
2022         struct ost_body *body;
2023         int rc;
2024         ENTRY;
2025
2026         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
2027         if (body == NULL)
2028                 RETURN(0);  /* can't return -EFAULT here */
2029
2030         rc = ost_validate_obdo(req->rq_export, &body->oa, NULL);
2031         if (rc)
2032                 RETURN(rc);
2033
2034         if (body->oa.o_valid & OBD_MD_FLHANDLE &&
2035             body->oa.o_handle.cookie == lock->l_handle.h_cookie)
2036                 RETURN(1);
2037         RETURN(0);
2038 }
2039
2040 /**
2041  * Like ost_rw_hpreq_check(), but for OST_PUNCH RPCs.
2042  */
2043 static int ost_punch_hpreq_check(struct ptlrpc_request *req)
2044 {
2045         struct ost_body *body;
2046         int rc;
2047
2048         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
2049         if (body == NULL)
2050                 RETURN(-EFAULT);
2051
2052         rc = ost_validate_obdo(req->rq_export, &body->oa, NULL);
2053         if (rc)
2054                 RETURN(rc);
2055
2056         LASSERT(!(body->oa.o_valid & OBD_MD_FLFLAGS) ||
2057                 !(body->oa.o_flags & OBD_FL_SRVLOCK));
2058
2059         RETURN(ost_punch_prolong_locks(req, &body->oa));
2060 }
2061
2062 struct ptlrpc_hpreq_ops ost_hpreq_rw = {
2063         .hpreq_lock_match  = ost_rw_hpreq_lock_match,
2064         .hpreq_check       = ost_rw_hpreq_check,
2065 };
2066
2067 struct ptlrpc_hpreq_ops ost_hpreq_punch = {
2068         .hpreq_lock_match  = ost_punch_hpreq_lock_match,
2069         .hpreq_check       = ost_punch_hpreq_check,
2070 };
2071
2072 /** Assign high priority operations to the request if needed. */
2073 static int ost_hpreq_handler(struct ptlrpc_request *req)
2074 {
2075         ENTRY;
2076         if (req->rq_export) {
2077                 int opc = lustre_msg_get_opc(req->rq_reqmsg);
2078                 struct ost_body *body;
2079
2080                 if (opc == OST_READ || opc == OST_WRITE) {
2081                         struct niobuf_remote *nb;
2082                         struct obd_ioobj *ioo;
2083                         int objcount, niocount;
2084                         int i;
2085
2086                         /* RPCs on the H-P queue can be inspected before
2087                          * ost_handler() initializes their pills, so we
2088                          * initialize that here.  Capsule initialization is
2089                          * idempotent, as is setting the pill's format (provided
2090                          * it doesn't change).
2091                          */
2092                         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2093                         if (opc == OST_READ)
2094                                 req_capsule_set(&req->rq_pill,
2095                                                 &RQF_OST_BRW_READ);
2096                         else
2097                                 req_capsule_set(&req->rq_pill,
2098                                                 &RQF_OST_BRW_WRITE);
2099
2100                         body = req_capsule_client_get(&req->rq_pill,
2101                                                       &RMF_OST_BODY);
2102                         if (body == NULL) {
2103                                 CERROR("Missing/short ost_body\n");
2104                                 RETURN(-EFAULT);
2105                         }
2106
2107                         objcount = req_capsule_get_size(&req->rq_pill,
2108                                                         &RMF_OBD_IOOBJ,
2109                                                         RCL_CLIENT) /
2110                                                         sizeof(*ioo);
2111                         if (objcount == 0) {
2112                                 CERROR("Missing/short ioobj\n");
2113                                 RETURN(-EFAULT);
2114                         }
2115                         if (objcount > 1) {
2116                                 CERROR("too many ioobjs (%d)\n", objcount);
2117                                 RETURN(-EFAULT);
2118                         }
2119
2120                         ioo = req_capsule_client_get(&req->rq_pill,
2121                                                      &RMF_OBD_IOOBJ);
2122                         if (ioo == NULL) {
2123                                 CERROR("Missing/short ioobj\n");
2124                                 RETURN(-EFAULT);
2125                         }
2126
2127                         for (niocount = i = 0; i < objcount; i++) {
2128                                 if (ioo[i].ioo_bufcnt == 0) {
2129                                         CERROR("ioo[%d] has zero bufcnt\n", i);
2130                                         RETURN(-EFAULT);
2131                                 }
2132                                 niocount += ioo[i].ioo_bufcnt;
2133                         }
2134                         if (niocount > PTLRPC_MAX_BRW_PAGES) {
2135                                 DEBUG_REQ(D_RPCTRACE, req,
2136                                           "bulk has too many pages (%d)",
2137                                           niocount);
2138                                 RETURN(-EFAULT);
2139                         }
2140
2141                         nb = req_capsule_client_get(&req->rq_pill,
2142                                                     &RMF_NIOBUF_REMOTE);
2143                         if (nb == NULL) {
2144                                 CERROR("Missing/short niobuf\n");
2145                                 RETURN(-EFAULT);
2146                         }
2147
2148                         if (niocount == 0 || !(nb[0].flags & OBD_BRW_SRVLOCK))
2149                                 req->rq_ops = &ost_hpreq_rw;
2150                 } else if (opc == OST_PUNCH) {
2151                         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2152                         req_capsule_set(&req->rq_pill, &RQF_OST_PUNCH);
2153
2154                         body = req_capsule_client_get(&req->rq_pill,
2155                                                       &RMF_OST_BODY);
2156                         if (body == NULL) {
2157                                 CERROR("Missing/short ost_body\n");
2158                                 RETURN(-EFAULT);
2159                         }
2160
2161                         if (!(body->oa.o_valid & OBD_MD_FLFLAGS) ||
2162                             !(body->oa.o_flags & OBD_FL_SRVLOCK))
2163                                 req->rq_ops = &ost_hpreq_punch;
2164                 }
2165         }
2166         RETURN(0);
2167 }
2168
2169 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
2170 int ost_handle(struct ptlrpc_request *req)
2171 {
2172         struct obd_trans_info trans_info = { 0, };
2173         struct obd_trans_info *oti = &trans_info;
2174         int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
2175         struct obd_device *obd = NULL;
2176         ENTRY;
2177
2178         LASSERT(current->journal_info == NULL);
2179
2180         /* primordial rpcs don't affect server recovery */
2181         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2182         case SEC_CTX_INIT:
2183         case SEC_CTX_INIT_CONT:
2184         case SEC_CTX_FINI:
2185                 GOTO(out, rc = 0);
2186         }
2187
2188         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2189
2190         if (lustre_msg_get_opc(req->rq_reqmsg) != OST_CONNECT) {
2191                 if (!class_connected_export(req->rq_export)) {
2192                         CDEBUG(D_HA,"operation %d on unconnected OST from %s\n",
2193                                lustre_msg_get_opc(req->rq_reqmsg),
2194                                libcfs_id2str(req->rq_peer));
2195                         req->rq_status = -ENOTCONN;
2196                         GOTO(out, rc = -ENOTCONN);
2197                 }
2198
2199                 obd = req->rq_export->exp_obd;
2200
2201                 /* Check for aborted recovery. */
2202                 if (obd->obd_recovering) {
2203                         rc = ost_filter_recovery_request(req, obd,
2204                                                          &should_process);
2205                         if (rc || !should_process)
2206                                 RETURN(rc);
2207                         else if (should_process < 0) {
2208                                 req->rq_status = should_process;
2209                                 rc = ptlrpc_error(req);
2210                                 RETURN(rc);
2211                         }
2212                 }
2213         }
2214
2215         oti_init(oti, req);
2216
2217         rc = ost_msg_check_version(req->rq_reqmsg);
2218         if (rc)
2219                 RETURN(rc);
2220
2221         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2222         case OST_CONNECT: {
2223                 CDEBUG(D_INODE, "connect\n");
2224                 req_capsule_set(&req->rq_pill, &RQF_OST_CONNECT);
2225                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET))
2226                         RETURN(0);
2227                 rc = target_handle_connect(req);
2228                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET2))
2229                         RETURN(0);
2230                 if (!rc) {
2231                         rc = ost_init_sec_level(req);
2232                         if (!rc)
2233                                 rc = ost_connect_check_sptlrpc(req);
2234                 }
2235                 break;
2236         }
2237         case OST_DISCONNECT:
2238                 CDEBUG(D_INODE, "disconnect\n");
2239                 req_capsule_set(&req->rq_pill, &RQF_OST_DISCONNECT);
2240                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DISCONNECT_NET))
2241                         RETURN(0);
2242                 rc = target_handle_disconnect(req);
2243                 break;
2244         case OST_CREATE:
2245                 CDEBUG(D_INODE, "create\n");
2246                 req_capsule_set(&req->rq_pill, &RQF_OST_CREATE);
2247                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CREATE_NET))
2248                         RETURN(0);
2249                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2250                         GOTO(out, rc = -EROFS);
2251                 rc = ost_create(req->rq_export, req, oti);
2252                 break;
2253         case OST_DESTROY:
2254                 CDEBUG(D_INODE, "destroy\n");
2255                 req_capsule_set(&req->rq_pill, &RQF_OST_DESTROY);
2256                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DESTROY_NET))
2257                         RETURN(0);
2258                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2259                         GOTO(out, rc = -EROFS);
2260                 rc = ost_destroy(req->rq_export, req, oti);
2261                 break;
2262         case OST_GETATTR:
2263                 CDEBUG(D_INODE, "getattr\n");
2264                 req_capsule_set(&req->rq_pill, &RQF_OST_GETATTR);
2265                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_GETATTR_NET))
2266                         RETURN(0);
2267                 rc = ost_getattr(req->rq_export, req);
2268                 break;
2269         case OST_SETATTR:
2270                 CDEBUG(D_INODE, "setattr\n");
2271                 req_capsule_set(&req->rq_pill, &RQF_OST_SETATTR);
2272                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SETATTR_NET))
2273                         RETURN(0);
2274                 rc = ost_setattr(req->rq_export, req, oti);
2275                 break;
2276         case OST_WRITE:
2277                 req_capsule_set(&req->rq_pill, &RQF_OST_BRW_WRITE);
2278                 CDEBUG(D_INODE, "write\n");
2279                 /* req->rq_request_portal would be nice, if it was set */
2280                 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
2281                         CERROR("%s: deny write request from %s to portal %u\n",
2282                                req->rq_export->exp_obd->obd_name,
2283                                obd_export_nid2str(req->rq_export),
2284                                req->rq_rqbd->rqbd_service->srv_req_portal);
2285                         GOTO(out, rc = -EPROTO);
2286                 }
2287                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
2288                         RETURN(0);
2289                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
2290                         GOTO(out, rc = -ENOSPC);
2291                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2292                         GOTO(out, rc = -EROFS);
2293                 rc = ost_brw_write(req, oti);
2294                 LASSERT(current->journal_info == NULL);
2295                 /* ost_brw_write sends its own replies */
2296                 RETURN(rc);
2297         case OST_READ:
2298                 req_capsule_set(&req->rq_pill, &RQF_OST_BRW_READ);
2299                 CDEBUG(D_INODE, "read\n");
2300                 /* req->rq_request_portal would be nice, if it was set */
2301                 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
2302                         CERROR("%s: deny read request from %s to portal %u\n",
2303                                req->rq_export->exp_obd->obd_name,
2304                                obd_export_nid2str(req->rq_export),
2305                                req->rq_rqbd->rqbd_service->srv_req_portal);
2306                         GOTO(out, rc = -EPROTO);
2307                 }
2308                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
2309                         RETURN(0);
2310                 rc = ost_brw_read(req, oti);
2311                 LASSERT(current->journal_info == NULL);
2312                 /* ost_brw_read sends its own replies */
2313                 RETURN(rc);
2314         case OST_PUNCH:
2315                 CDEBUG(D_INODE, "punch\n");
2316                 req_capsule_set(&req->rq_pill, &RQF_OST_PUNCH);
2317                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_PUNCH_NET))
2318                         RETURN(0);
2319                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2320                         GOTO(out, rc = -EROFS);
2321                 rc = ost_punch(req->rq_export, req, oti);
2322                 break;
2323         case OST_STATFS:
2324                 CDEBUG(D_INODE, "statfs\n");
2325                 req_capsule_set(&req->rq_pill, &RQF_OST_STATFS);
2326                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_NET))
2327                         RETURN(0);
2328                 rc = ost_statfs(req);
2329                 break;
2330         case OST_SYNC:
2331                 CDEBUG(D_INODE, "sync\n");
2332                 req_capsule_set(&req->rq_pill, &RQF_OST_SYNC);
2333                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SYNC_NET))
2334                         RETURN(0);
2335                 rc = ost_sync(req->rq_export, req);
2336                 break;
2337         case OST_SET_INFO:
2338                 DEBUG_REQ(D_INODE, req, "set_info");
2339                 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
2340                 rc = ost_set_info(req->rq_export, req);
2341                 break;
2342         case OST_GET_INFO:
2343                 DEBUG_REQ(D_INODE, req, "get_info");
2344                 req_capsule_set(&req->rq_pill, &RQF_OST_GET_INFO_GENERIC);
2345                 rc = ost_get_info(req->rq_export, req);
2346                 break;
2347 #ifdef HAVE_QUOTA_SUPPORT
2348         case OST_QUOTACHECK:
2349                 CDEBUG(D_INODE, "quotacheck\n");
2350                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACHECK);
2351                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACHECK_NET))
2352                         RETURN(0);
2353                 rc = ost_handle_quotacheck(req);
2354                 break;
2355         case OST_QUOTACTL:
2356                 CDEBUG(D_INODE, "quotactl\n");
2357                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACTL);
2358                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACTL_NET))
2359                         RETURN(0);
2360                 rc = ost_handle_quotactl(req);
2361                 break;
2362         case OST_QUOTA_ADJUST_QUNIT:
2363                 CDEBUG(D_INODE, "quota_adjust_qunit\n");
2364                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTA_ADJUST_QUNIT);
2365                 rc = ost_handle_quota_adjust_qunit(req);
2366                 break;
2367 #endif
2368         case OBD_PING:
2369                 DEBUG_REQ(D_INODE, req, "ping");
2370                 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
2371                 rc = target_handle_ping(req);
2372                 break;
2373         /* FIXME - just reply status */
2374         case LLOG_ORIGIN_CONNECT:
2375                 DEBUG_REQ(D_INODE, req, "log connect");
2376                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_CONNECT);
2377                 rc = ost_llog_handle_connect(req->rq_export, req);
2378                 req->rq_status = rc;
2379                 rc = req_capsule_server_pack(&req->rq_pill);
2380                 if (rc)
2381                         RETURN(rc);
2382                 RETURN(ptlrpc_reply(req));
2383         case OBD_LOG_CANCEL:
2384                 CDEBUG(D_INODE, "log cancel\n");
2385                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2386                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2387                         RETURN(0);
2388                 rc = llog_origin_handle_cancel(req);
2389                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2390                         RETURN(0);
2391                 req->rq_status = rc;
2392                 rc = req_capsule_server_pack(&req->rq_pill);
2393                 if (rc)
2394                         RETURN(rc);
2395                 RETURN(ptlrpc_reply(req));
2396         case LDLM_ENQUEUE:
2397                 CDEBUG(D_INODE, "enqueue\n");
2398                 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
2399                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE))
2400                         RETURN(0);
2401                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
2402                                          ost_blocking_ast,
2403                                          ldlm_server_glimpse_ast);
2404                 fail = OBD_FAIL_OST_LDLM_REPLY_NET;
2405                 break;
2406         case LDLM_CONVERT:
2407                 CDEBUG(D_INODE, "convert\n");
2408                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CONVERT);
2409                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CONVERT))
2410                         RETURN(0);
2411                 rc = ldlm_handle_convert(req);
2412                 break;
2413         case LDLM_CANCEL:
2414                 CDEBUG(D_INODE, "cancel\n");
2415                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2416                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
2417                         RETURN(0);
2418                 rc = ldlm_handle_cancel(req);
2419                 break;
2420         case LDLM_BL_CALLBACK:
2421         case LDLM_CP_CALLBACK:
2422                 CDEBUG(D_INODE, "callback\n");
2423                 CERROR("callbacks should not happen on OST\n");
2424                 /* fall through */
2425         default:
2426                 CERROR("Unexpected opcode %d\n",
2427                        lustre_msg_get_opc(req->rq_reqmsg));
2428                 req->rq_status = -ENOTSUPP;
2429                 rc = ptlrpc_error(req);
2430                 RETURN(rc);
2431         }
2432
2433         LASSERT(current->journal_info == NULL);
2434
2435         EXIT;
2436         /* If we're DISCONNECTing, the export_data is already freed */
2437         if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != OST_DISCONNECT)
2438                 target_committed_to_req(req);
2439
2440 out:
2441         if (!rc)
2442                 oti_to_request(oti, req);
2443
2444         target_send_reply(req, rc, fail);
2445         return 0;
2446 }
2447 EXPORT_SYMBOL(ost_handle);
2448 /*
2449  * free per-thread pool created by ost_thread_init().
2450  */
2451 static void ost_thread_done(struct ptlrpc_thread *thread)
2452 {
2453         struct ost_thread_local_cache *tls; /* TLS stands for Thread-Local
2454                                              * Storage */
2455
2456         ENTRY;
2457
2458         LASSERT(thread != NULL);
2459
2460         /*
2461          * be prepared to handle partially-initialized pools (because this is
2462          * called from ost_thread_init() for cleanup.
2463          */
2464         tls = thread->t_data;
2465         if (tls != NULL) {
2466                 OBD_FREE_PTR(tls);
2467                 thread->t_data = NULL;
2468         }
2469         EXIT;
2470 }
2471
2472 /*
2473  * initialize per-thread page pool (bug 5137).
2474  */
2475 static int ost_thread_init(struct ptlrpc_thread *thread)
2476 {
2477         struct ost_thread_local_cache *tls;
2478
2479         ENTRY;
2480
2481         LASSERT(thread != NULL);
2482         LASSERT(thread->t_data == NULL);
2483         LASSERTF(thread->t_id <= OSS_THREADS_MAX, "%u\n", thread->t_id);
2484
2485         OBD_ALLOC_PTR(tls);
2486         if (tls == NULL)
2487                 RETURN(-ENOMEM);
2488         thread->t_data = tls;
2489         RETURN(0);
2490 }
2491
2492 #define OST_WATCHDOG_TIMEOUT (obd_timeout * 1000)
2493
2494 /* Sigh - really, this is an OSS, the _server_, not the _target_ */
2495 static int ost_setup(struct obd_device *obd, struct lustre_cfg* lcfg)
2496 {
2497         struct ost_obd *ost = &obd->u.ost;
2498         struct lprocfs_static_vars lvars;
2499         int oss_min_threads;
2500         int oss_max_threads;
2501         int oss_min_create_threads;
2502         int oss_max_create_threads;
2503         int rc;
2504         ENTRY;
2505
2506         rc = cfs_cleanup_group_info();
2507         if (rc)
2508                 RETURN(rc);
2509
2510         lprocfs_ost_init_vars(&lvars);
2511         lprocfs_obd_setup(obd, lvars.obd_vars);
2512
2513         cfs_sema_init(&ost->ost_health_sem, 1);
2514
2515         if (oss_num_threads) {
2516                 /* If oss_num_threads is set, it is the min and the max. */
2517                 if (oss_num_threads > OSS_THREADS_MAX)
2518                         oss_num_threads = OSS_THREADS_MAX;
2519                 if (oss_num_threads < OSS_THREADS_MIN)
2520                         oss_num_threads = OSS_THREADS_MIN;
2521                 oss_max_threads = oss_min_threads = oss_num_threads;
2522         } else {
2523                 /* Base min threads on memory and cpus */
2524                 oss_min_threads =
2525                         cfs_num_possible_cpus() * CFS_NUM_CACHEPAGES >>
2526                         (27 - CFS_PAGE_SHIFT);
2527                 if (oss_min_threads < OSS_THREADS_MIN)
2528                         oss_min_threads = OSS_THREADS_MIN;
2529                 /* Insure a 4x range for dynamic threads */
2530                 if (oss_min_threads > OSS_THREADS_MAX / 4)
2531                         oss_min_threads = OSS_THREADS_MAX / 4;
2532                 oss_max_threads = min(OSS_THREADS_MAX, oss_min_threads * 4 + 1);
2533         }
2534
2535         ost->ost_service =
2536                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
2537                                 OST_MAXREPSIZE, OST_REQUEST_PORTAL,
2538                                 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
2539                                 ost_handle, LUSTRE_OSS_NAME,
2540                                 obd->obd_proc_entry, target_print_req,
2541                                 oss_min_threads, oss_max_threads,
2542                                 "ll_ost", LCT_DT_THREAD, NULL);
2543         if (ost->ost_service == NULL) {
2544                 CERROR("failed to start service\n");
2545                 GOTO(out_lprocfs, rc = -ENOMEM);
2546         }
2547
2548         rc = ptlrpc_start_threads(ost->ost_service);
2549         if (rc)
2550                 GOTO(out_service, rc = -EINVAL);
2551
2552         if (oss_num_create_threads) {
2553                 if (oss_num_create_threads > OSS_MAX_CREATE_THREADS)
2554                         oss_num_create_threads = OSS_MAX_CREATE_THREADS;
2555                 if (oss_num_create_threads < OSS_MIN_CREATE_THREADS)
2556                         oss_num_create_threads = OSS_MIN_CREATE_THREADS;
2557                 oss_min_create_threads = oss_max_create_threads =
2558                         oss_num_create_threads;
2559         } else {
2560                 oss_min_create_threads = OSS_MIN_CREATE_THREADS;
2561                 oss_max_create_threads = OSS_MAX_CREATE_THREADS;
2562         }
2563
2564         ost->ost_create_service =
2565                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
2566                                 OST_MAXREPSIZE, OST_CREATE_PORTAL,
2567                                 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
2568                                 ost_handle, "ost_create",
2569                                 obd->obd_proc_entry, target_print_req,
2570                                 oss_min_create_threads, oss_max_create_threads,
2571                                 "ll_ost_creat", LCT_DT_THREAD, NULL);
2572         if (ost->ost_create_service == NULL) {
2573                 CERROR("failed to start OST create service\n");
2574                 GOTO(out_service, rc = -ENOMEM);
2575         }
2576
2577         rc = ptlrpc_start_threads(ost->ost_create_service);
2578         if (rc)
2579                 GOTO(out_create, rc = -EINVAL);
2580
2581         ost->ost_io_service =
2582                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
2583                                 OST_MAXREPSIZE, OST_IO_PORTAL,
2584                                 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
2585                                 ost_handle, "ost_io",
2586                                 obd->obd_proc_entry, target_print_req,
2587                                 oss_min_threads, oss_max_threads,
2588                                 "ll_ost_io", LCT_DT_THREAD, ost_hpreq_handler);
2589         if (ost->ost_io_service == NULL) {
2590                 CERROR("failed to start OST I/O service\n");
2591                 GOTO(out_create, rc = -ENOMEM);
2592         }
2593
2594         ost->ost_io_service->srv_init = ost_thread_init;
2595         ost->ost_io_service->srv_done = ost_thread_done;
2596         ost->ost_io_service->srv_cpu_affinity = 1;
2597         rc = ptlrpc_start_threads(ost->ost_io_service);
2598         if (rc)
2599                 GOTO(out_io, rc = -EINVAL);
2600
2601         ping_evictor_start();
2602
2603         RETURN(0);
2604
2605 out_io:
2606         ptlrpc_unregister_service(ost->ost_io_service);
2607         ost->ost_io_service = NULL;
2608 out_create:
2609         ptlrpc_unregister_service(ost->ost_create_service);
2610         ost->ost_create_service = NULL;
2611 out_service:
2612         ptlrpc_unregister_service(ost->ost_service);
2613         ost->ost_service = NULL;
2614 out_lprocfs:
2615         lprocfs_obd_cleanup(obd);
2616         RETURN(rc);
2617 }
2618
2619 static int ost_cleanup(struct obd_device *obd)
2620 {
2621         struct ost_obd *ost = &obd->u.ost;
2622         int err = 0;
2623         ENTRY;
2624
2625         ping_evictor_stop();
2626
2627         /* there is no recovery for OST OBD, all recovery is controlled by
2628          * obdfilter OBD */
2629         LASSERT(obd->obd_recovering == 0);
2630         cfs_down(&ost->ost_health_sem);
2631         ptlrpc_unregister_service(ost->ost_service);
2632         ptlrpc_unregister_service(ost->ost_create_service);
2633         ptlrpc_unregister_service(ost->ost_io_service);
2634         ost->ost_service = NULL;
2635         ost->ost_create_service = NULL;
2636         cfs_up(&ost->ost_health_sem);
2637
2638         lprocfs_obd_cleanup(obd);
2639
2640         RETURN(err);
2641 }
2642
2643 static int ost_health_check(struct obd_device *obd)
2644 {
2645         struct ost_obd *ost = &obd->u.ost;
2646         int rc = 0;
2647
2648         cfs_down(&ost->ost_health_sem);
2649         rc |= ptlrpc_service_health_check(ost->ost_service);
2650         rc |= ptlrpc_service_health_check(ost->ost_create_service);
2651         rc |= ptlrpc_service_health_check(ost->ost_io_service);
2652         cfs_up(&ost->ost_health_sem);
2653
2654         /*
2655          * health_check to return 0 on healthy
2656          * and 1 on unhealthy.
2657          */
2658         if( rc != 0)
2659                 rc = 1;
2660
2661         return rc;
2662 }
2663
2664 struct ost_thread_local_cache *ost_tls(struct ptlrpc_request *r)
2665 {
2666         return (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
2667 }
2668
2669 /* use obd ops to offer management infrastructure */
2670 static struct obd_ops ost_obd_ops = {
2671         .o_owner        = THIS_MODULE,
2672         .o_setup        = ost_setup,
2673         .o_cleanup      = ost_cleanup,
2674         .o_health_check = ost_health_check,
2675 };
2676
2677
2678 static int __init ost_init(void)
2679 {
2680         struct lprocfs_static_vars lvars;
2681         int rc;
2682         ENTRY;
2683
2684         lprocfs_ost_init_vars(&lvars);
2685         rc = class_register_type(&ost_obd_ops, NULL, lvars.module_vars,
2686                                  LUSTRE_OSS_NAME, NULL);
2687
2688         if (ost_num_threads != 0 && oss_num_threads == 0) {
2689                 LCONSOLE_INFO("ost_num_threads module parameter is deprecated, "
2690                               "use oss_num_threads instead or unset both for "
2691                               "dynamic thread startup\n");
2692                 oss_num_threads = ost_num_threads;
2693         }
2694
2695         RETURN(rc);
2696 }
2697
2698 static void /*__exit*/ ost_exit(void)
2699 {
2700         class_unregister_type(LUSTRE_OSS_NAME);
2701 }
2702
2703 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2704 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
2705 MODULE_LICENSE("GPL");
2706
2707 module_init(ost_init);
2708 module_exit(ost_exit);