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