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