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