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