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