Whamcloud - gitweb
638536a81d4c2f2f320bbdf9b1d5e3d4613f845e
[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         rc = req_capsule_server_pack(&req->rq_pill);
685         if (rc)
686                 GOTO(out, rc);
687
688         /*
689          * Per-thread array of struct niobuf_{local,remote}'s was allocated by
690          * ost_thread_init().
691          */
692         local_nb = ost_tls(req)->local;
693
694         rc = ost_brw_lock_get(LCK_PR, exp, ioo, remote_nb, &lockh);
695         if (rc != 0)
696                 GOTO(out_bulk, rc);
697
698         /*
699          * If getting the lock took more time than
700          * client was willing to wait, drop it. b=11330
701          */
702         if (cfs_time_current_sec() > req->rq_deadline ||
703             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
704                 no_reply = 1;
705                 CERROR("Dropping timed-out read from %s because locking"
706                        "object "LPX64" took %ld seconds (limit was %ld).\n",
707                        libcfs_id2str(req->rq_peer), ioo->ioo_id,
708                        cfs_time_current_sec() - req->rq_arrival_time.tv_sec,
709                        req->rq_deadline - req->rq_arrival_time.tv_sec);
710                 GOTO(out_lock, rc = -ETIMEDOUT);
711         }
712
713         npages = OST_THREAD_POOL_SIZE;
714         rc = obd_preprw(OBD_BRW_READ, exp, &body->oa, 1, ioo,
715                         remote_nb, &npages, local_nb, oti, capa);
716         if (rc != 0)
717                 GOTO(out_lock, rc);
718
719         desc = ptlrpc_prep_bulk_exp(req, npages,
720                                      BULK_PUT_SOURCE, OST_BULK_PORTAL);
721         if (desc == NULL)
722                 GOTO(out_lock, rc = -ENOMEM);
723
724         if (!lustre_handle_is_used(&lockh))
725                 /* no needs to try to prolong lock if server is asked
726                  * to handle locking (= OBD_BRW_SRVLOCK) */
727                 ost_rw_prolong_locks(req, ioo, remote_nb, &body->oa,
728                                      LCK_PW | LCK_PR);
729
730         nob = 0;
731         for (i = 0; i < npages; i++) {
732                 int page_rc = local_nb[i].rc;
733
734                 if (page_rc < 0) {              /* error */
735                         rc = page_rc;
736                         break;
737                 }
738
739                 nob += page_rc;
740                 if (page_rc != 0) {             /* some data! */
741                         LASSERT (local_nb[i].page != NULL);
742                         ptlrpc_prep_bulk_page(desc, local_nb[i].page,
743                                               local_nb[i].offset & ~CFS_PAGE_MASK,
744                                               page_rc);
745                 }
746
747                 if (page_rc != local_nb[i].len) { /* short read */
748                         /* All subsequent pages should be 0 */
749                         while(++i < npages)
750                                 LASSERT(local_nb[i].rc == 0);
751                         break;
752                 }
753         }
754
755         if (body->oa.o_valid & OBD_MD_FLCKSUM) {
756                 cksum_type_t cksum_type = OBD_CKSUM_CRC32;
757
758                 if (body->oa.o_valid & OBD_MD_FLFLAGS)
759                         cksum_type = cksum_type_unpack(body->oa.o_flags);
760                 body->oa.o_flags = cksum_type_pack(cksum_type);
761                 body->oa.o_valid = OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
762                 body->oa.o_cksum = ost_checksum_bulk(desc, OST_READ, cksum_type);
763                 CDEBUG(D_PAGE,"checksum at read origin: %x\n",body->oa.o_cksum);
764         } else {
765                 body->oa.o_valid = 0;
766         }
767         /* We're finishing using body->oa as an input variable */
768
769         /* Check if client was evicted while we were doing i/o before touching
770            network */
771         if (rc == 0) {
772                 /* Check if there is eviction in progress, and if so, wait for
773                  * it to finish */
774                 if (unlikely(cfs_atomic_read(&exp->exp_obd->
775                                              obd_evict_inprogress))) {
776                         lwi = LWI_INTR(NULL, NULL);
777                         rc = l_wait_event(exp->exp_obd->
778                                           obd_evict_inprogress_waitq,
779                                           !cfs_atomic_read(&exp->exp_obd->
780                                           obd_evict_inprogress),
781                                           &lwi);
782                 }
783                 /* Check if client was evicted or tried to reconnect already */
784                 if (exp->exp_failed || exp->exp_abort_active_req)
785                         rc = -ENOTCONN;
786                 else {
787                         rc = sptlrpc_svc_wrap_bulk(req, desc);
788                         if (rc == 0)
789                                 rc = ptlrpc_start_bulk_transfer(desc);
790                 }
791
792                 if (rc == 0) {
793                         time_t start = cfs_time_current_sec();
794                         do {
795                                 long timeoutl = req->rq_deadline -
796                                         cfs_time_current_sec();
797                                 cfs_duration_t timeout = timeoutl <= 0 ?
798                                         CFS_TICK : cfs_time_seconds(timeoutl);
799                                 lwi = LWI_TIMEOUT_INTERVAL(timeout,
800                                                            cfs_time_seconds(1),
801                                                            ost_bulk_timeout,
802                                                            desc);
803                                 rc = l_wait_event(desc->bd_waitq,
804                                                   !ptlrpc_server_bulk_active(desc) ||
805                                                   exp->exp_failed ||
806                                                   exp->exp_abort_active_req,
807                                                   &lwi);
808                                 LASSERT(rc == 0 || rc == -ETIMEDOUT);
809                                 /* Wait again if we changed deadline */
810                         } while ((rc == -ETIMEDOUT) &&
811                                  (req->rq_deadline > cfs_time_current_sec()));
812
813                         if (rc == -ETIMEDOUT) {
814                                 DEBUG_REQ(D_ERROR, req,
815                                           "timeout on bulk PUT after %ld%+lds",
816                                           req->rq_deadline - start,
817                                           cfs_time_current_sec() -
818                                           req->rq_deadline);
819                                 ptlrpc_abort_bulk(desc);
820                         } else if (exp->exp_failed) {
821                                 DEBUG_REQ(D_ERROR, req, "Eviction on bulk PUT");
822                                 rc = -ENOTCONN;
823                                 ptlrpc_abort_bulk(desc);
824                         } else if (exp->exp_abort_active_req) {
825                                 DEBUG_REQ(D_ERROR, req, "Reconnect on bulk PUT");
826                                 /* we don't reply anyway */
827                                 rc = -ETIMEDOUT;
828                                 ptlrpc_abort_bulk(desc);
829                         } else if (!desc->bd_success ||
830                                    desc->bd_nob_transferred != desc->bd_nob) {
831                                 DEBUG_REQ(D_ERROR, req, "%s bulk PUT %d(%d)",
832                                           desc->bd_success ?
833                                           "truncated" : "network error on",
834                                           desc->bd_nob_transferred,
835                                           desc->bd_nob);
836                                 /* XXX should this be a different errno? */
837                                 rc = -ETIMEDOUT;
838                         }
839                 } else {
840                         DEBUG_REQ(D_ERROR, req, "bulk PUT failed: rc %d", rc);
841                 }
842                 no_reply = rc != 0;
843         }
844
845         /* Must commit after prep above in all cases */
846         rc = obd_commitrw(OBD_BRW_READ, exp, &body->oa, 1, ioo,
847                           remote_nb, npages, local_nb, oti, rc);
848
849         if (rc == 0) {
850                 repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
851                 memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
852                 ost_drop_id(exp, &repbody->oa);
853         }
854
855 out_lock:
856         ost_brw_lock_put(LCK_PR, ioo, remote_nb, &lockh);
857 out_bulk:
858         if (desc)
859                 ptlrpc_free_bulk(desc);
860 out:
861         LASSERT(rc <= 0);
862         if (rc == 0) {
863                 req->rq_status = nob;
864                 ptlrpc_lprocfs_brw(req, nob);
865                 target_committed_to_req(req);
866                 ptlrpc_reply(req);
867         } else if (!no_reply) {
868                 /* Only reply if there was no comms problem with bulk */
869                 target_committed_to_req(req);
870                 req->rq_status = rc;
871                 ptlrpc_error(req);
872         } else {
873                 /* reply out callback would free */
874                 ptlrpc_req_drop_rs(req);
875                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
876                       "client will retry\n",
877                       exp->exp_obd->obd_name,
878                       exp->exp_client_uuid.uuid,
879                       exp->exp_connection->c_remote_uuid.uuid,
880                       libcfs_id2str(req->rq_peer));
881         }
882
883         RETURN(rc);
884 }
885
886 static int ost_brw_write(struct ptlrpc_request *req, struct obd_trans_info *oti)
887 {
888         struct ptlrpc_bulk_desc *desc = NULL;
889         struct obd_export       *exp = req->rq_export;
890         struct niobuf_remote    *remote_nb;
891         struct niobuf_local     *local_nb;
892         struct obd_ioobj        *ioo;
893         struct ost_body         *body, *repbody;
894         struct l_wait_info       lwi;
895         struct lustre_handle     lockh = {0};
896         struct lustre_capa      *capa = NULL;
897         __u32                   *rcs;
898         int objcount, niocount, npages;
899         int rc, i, j;
900         obd_count                client_cksum = 0, server_cksum = 0;
901         cksum_type_t             cksum_type = OBD_CKSUM_CRC32;
902         int                      no_reply = 0;
903         __u32                    o_uid = 0, o_gid = 0;
904         ENTRY;
905
906         req->rq_bulk_write = 1;
907
908         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK))
909                 GOTO(out, rc = -EIO);
910         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_WRITE_BULK2))
911                 GOTO(out, rc = -EFAULT);
912
913         /* pause before transaction has been started */
914         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK, (obd_timeout + 1) / 4);
915
916         /* Check if there is eviction in progress, and if so, wait for it to
917          * finish */
918         if (unlikely(cfs_atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
919                 lwi = LWI_INTR(NULL, NULL); // We do not care how long it takes
920                 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
921                         !cfs_atomic_read(&exp->exp_obd->obd_evict_inprogress),
922                         &lwi);
923         }
924         if (exp->exp_failed)
925                 GOTO(out, rc = -ENOTCONN);
926
927         /* ost_body, ioobj & noibuf_remote are verified and swabbed in
928          * ost_rw_hpreq_check(). */
929         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
930         if (body == NULL)
931                 GOTO(out, rc = -EFAULT);
932
933         if ((body->oa.o_flags & OBD_BRW_MEMALLOC) &&
934             (exp->exp_connection->c_peer.nid == exp->exp_connection->c_self))
935                 libcfs_memory_pressure_set();
936
937         objcount = req_capsule_get_size(&req->rq_pill, &RMF_OBD_IOOBJ,
938                                         RCL_CLIENT) / sizeof(*ioo);
939         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
940         if (ioo == NULL)
941                 GOTO(out, rc = -EFAULT);
942         for (niocount = i = 0; i < objcount; i++)
943                 niocount += ioo[i].ioo_bufcnt;
944
945         /*
946          * It'd be nice to have a capsule function to indicate how many elements
947          * there were in a buffer for an RMF that's declared to be an array.
948          * It's easy enough to compute the number of elements here though.
949          */
950         remote_nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
951         if (remote_nb == NULL || niocount != (req_capsule_get_size(&req->rq_pill,
952             &RMF_NIOBUF_REMOTE, RCL_CLIENT) / sizeof(*remote_nb)))
953                 GOTO(out, rc = -EFAULT);
954
955         if (body->oa.o_valid & OBD_MD_FLOSSCAPA) {
956                 capa = req_capsule_client_get(&req->rq_pill, &RMF_CAPA1);
957                 if (capa == NULL) {
958                         CERROR("Missing capability for OST BRW WRITE");
959                         GOTO(out, rc = -EFAULT);
960                 }
961         }
962
963         req_capsule_set_size(&req->rq_pill, &RMF_RCS, RCL_SERVER,
964                              niocount * sizeof(*rcs));
965         rc = req_capsule_server_pack(&req->rq_pill);
966         if (rc != 0)
967                 GOTO(out, rc);
968         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_PACK, obd_fail_val);
969         rcs = req_capsule_server_get(&req->rq_pill, &RMF_RCS);
970
971         /*
972          * Per-thread array of struct niobuf_{local,remote}'s was allocated by
973          * ost_thread_init().
974          */
975         local_nb = ost_tls(req)->local;
976
977         rc = ost_brw_lock_get(LCK_PW, exp, ioo, remote_nb, &lockh);
978         if (rc != 0)
979                 GOTO(out_bulk, rc);
980
981         /*
982          * If getting the lock took more time than
983          * client was willing to wait, drop it. b=11330
984          */
985         if (cfs_time_current_sec() > req->rq_deadline ||
986             OBD_FAIL_CHECK(OBD_FAIL_OST_DROP_REQ)) {
987                 no_reply = 1;
988                 CERROR("Dropping timed-out write from %s because locking "
989                        "object "LPX64" took %ld seconds (limit was %ld).\n",
990                        libcfs_id2str(req->rq_peer), ioo->ioo_id,
991                        cfs_time_current_sec() - req->rq_arrival_time.tv_sec,
992                        req->rq_deadline - req->rq_arrival_time.tv_sec);
993                 GOTO(out_lock, rc = -ETIMEDOUT);
994         }
995
996         if (!lustre_handle_is_used(&lockh))
997                 /* no needs to try to prolong lock if server is asked
998                  * to handle locking (= OBD_BRW_SRVLOCK) */
999                 ost_rw_prolong_locks(req, ioo, remote_nb,&body->oa,  LCK_PW);
1000
1001         /* obd_preprw clobbers oa->valid, so save what we need */
1002         if (body->oa.o_valid & OBD_MD_FLCKSUM) {
1003                 client_cksum = body->oa.o_cksum;
1004                 if (body->oa.o_valid & OBD_MD_FLFLAGS)
1005                         cksum_type = cksum_type_unpack(body->oa.o_flags);
1006         }
1007
1008         /* Because we already sync grant info with client when reconnect,
1009          * grant info will be cleared for resent req, then fed_grant and
1010          * total_grant will not be modified in following preprw_write */
1011         if (lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY)) {
1012                 DEBUG_REQ(D_CACHE, req, "clear resent/replay req grant info");
1013                 body->oa.o_valid &= ~OBD_MD_FLGRANT;
1014         }
1015
1016         if (exp_connect_rmtclient(exp)) {
1017                 o_uid = body->oa.o_uid;
1018                 o_gid = body->oa.o_gid;
1019         }
1020         npages = OST_THREAD_POOL_SIZE;
1021         rc = obd_preprw(OBD_BRW_WRITE, exp, &body->oa, objcount,
1022                         ioo, remote_nb, &npages, local_nb, oti, capa);
1023         if (rc != 0)
1024                 GOTO(out_lock, rc);
1025
1026         desc = ptlrpc_prep_bulk_exp(req, npages,
1027                                      BULK_GET_SINK, OST_BULK_PORTAL);
1028         if (desc == NULL)
1029                 GOTO(out_lock, rc = -ENOMEM);
1030
1031         /* NB Having prepped, we must commit... */
1032
1033         for (i = 0; i < npages; i++)
1034                 ptlrpc_prep_bulk_page(desc, local_nb[i].page,
1035                                       local_nb[i].offset & ~CFS_PAGE_MASK,
1036                                       local_nb[i].len);
1037
1038         rc = sptlrpc_svc_prep_bulk(req, desc);
1039         if (rc != 0)
1040                 GOTO(out_lock, rc);
1041
1042         /* Check if client was evicted or tried to reconnect while we
1043          * were doing i/o before touching network */
1044         if (desc->bd_export->exp_failed ||
1045             desc->bd_export->exp_abort_active_req)
1046                 rc = -ENOTCONN;
1047         else
1048                 rc = ptlrpc_start_bulk_transfer(desc);
1049         if (rc == 0) {
1050                 time_t start = cfs_time_current_sec();
1051                 do {
1052                         long timeoutl = req->rq_deadline -
1053                                 cfs_time_current_sec();
1054                         cfs_duration_t timeout = timeoutl <= 0 ?
1055                                 CFS_TICK : cfs_time_seconds(timeoutl);
1056                         lwi = LWI_TIMEOUT_INTERVAL(timeout, cfs_time_seconds(1),
1057                                                    ost_bulk_timeout, desc);
1058                         rc = l_wait_event(desc->bd_waitq,
1059                                           !ptlrpc_server_bulk_active(desc) ||
1060                                           desc->bd_export->exp_failed ||
1061                                           desc->bd_export->exp_abort_active_req,
1062                                           &lwi);
1063                         LASSERT(rc == 0 || rc == -ETIMEDOUT);
1064                         /* Wait again if we changed deadline */
1065                 } while ((rc == -ETIMEDOUT) &&
1066                          (req->rq_deadline > cfs_time_current_sec()));
1067
1068                 if (rc == -ETIMEDOUT) {
1069                         DEBUG_REQ(D_ERROR, req,
1070                                   "timeout on bulk GET after %ld%+lds",
1071                                   req->rq_deadline - start,
1072                                   cfs_time_current_sec() -
1073                                   req->rq_deadline);
1074                         ptlrpc_abort_bulk(desc);
1075                 } else if (desc->bd_export->exp_failed) {
1076                         DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1077                         rc = -ENOTCONN;
1078                         ptlrpc_abort_bulk(desc);
1079                 } else if (desc->bd_export->exp_abort_active_req) {
1080                         DEBUG_REQ(D_ERROR, req, "Reconnect on bulk GET");
1081                         /* we don't reply anyway */
1082                         rc = -ETIMEDOUT;
1083                         ptlrpc_abort_bulk(desc);
1084                 } else if (!desc->bd_success) {
1085                         DEBUG_REQ(D_ERROR, req, "network error on bulk GET");
1086                         /* XXX should this be a different errno? */
1087                         rc = -ETIMEDOUT;
1088                 } else {
1089                         rc = sptlrpc_svc_unwrap_bulk(req, desc);
1090                 }
1091         } else {
1092                 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1093         }
1094         no_reply = rc != 0;
1095
1096         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
1097         memcpy(&repbody->oa, &body->oa, sizeof(repbody->oa));
1098
1099         if (unlikely(client_cksum != 0 && rc == 0)) {
1100                 static int cksum_counter;
1101                 repbody->oa.o_valid |= OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
1102                 repbody->oa.o_flags &= ~OBD_FL_CKSUM_ALL;
1103                 repbody->oa.o_flags |= cksum_type_pack(cksum_type);
1104                 server_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
1105                 repbody->oa.o_cksum = server_cksum;
1106                 cksum_counter++;
1107                 if (unlikely(client_cksum != server_cksum)) {
1108                         CERROR("client csum %x, server csum %x\n",
1109                                client_cksum, server_cksum);
1110                         cksum_counter = 0;
1111                 } else if ((cksum_counter & (-cksum_counter)) == cksum_counter){
1112                         CDEBUG(D_INFO, "Checksum %u from %s OK: %x\n",
1113                                cksum_counter, libcfs_id2str(req->rq_peer),
1114                                server_cksum);
1115                 }
1116         }
1117
1118         /* Must commit after prep above in all cases */
1119         rc = obd_commitrw(OBD_BRW_WRITE, exp, &repbody->oa, objcount, ioo,
1120                           remote_nb, npages, local_nb, oti, rc);
1121         if (rc == -ENOTCONN)
1122                 /* quota acquire process has been given up because
1123                  * either the client has been evicted or the client
1124                  * has timed out the request already */
1125                 no_reply = 1;
1126
1127         if (exp_connect_rmtclient(exp)) {
1128                 repbody->oa.o_uid = o_uid;
1129                 repbody->oa.o_gid = o_gid;
1130         }
1131
1132         /*
1133          * Disable sending mtime back to the client. If the client locked the
1134          * whole object, then it has already updated the mtime on its side,
1135          * otherwise it will have to glimpse anyway (see bug 21489, comment 32)
1136          */
1137         repbody->oa.o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLATIME);
1138
1139         if (unlikely(client_cksum != server_cksum && rc == 0)) {
1140                 int  new_cksum = ost_checksum_bulk(desc, OST_WRITE, cksum_type);
1141                 char *msg;
1142                 char *via;
1143                 char *router;
1144
1145                 if (new_cksum == server_cksum)
1146                         msg = "changed in transit before arrival at OST";
1147                 else if (new_cksum == client_cksum)
1148                         msg = "initial checksum before message complete";
1149                 else
1150                         msg = "changed in transit AND after initial checksum";
1151
1152                 if (req->rq_peer.nid == desc->bd_sender) {
1153                         via = router = "";
1154                 } else {
1155                         via = " via ";
1156                         router = libcfs_nid2str(desc->bd_sender);
1157                 }
1158
1159                 LCONSOLE_ERROR_MSG(0x168, "%s: BAD WRITE CHECKSUM: %s from "
1160                                    "%s%s%s inum "LPU64"/"LPU64" object "
1161                                    LPU64"/"LPU64" extent ["LPU64"-"LPU64"]\n",
1162                                    exp->exp_obd->obd_name, msg,
1163                                    libcfs_id2str(req->rq_peer),
1164                                    via, router,
1165                                    body->oa.o_valid & OBD_MD_FLFID ?
1166                                                 body->oa.o_fid : (__u64)0,
1167                                    body->oa.o_valid & OBD_MD_FLFID ?
1168                                                 body->oa.o_generation :(__u64)0,
1169                                    body->oa.o_id,
1170                                    body->oa.o_valid & OBD_MD_FLGROUP ?
1171                                                 body->oa.o_gr : (__u64)0,
1172                                    local_nb[0].offset,
1173                                    local_nb[npages-1].offset +
1174                                    local_nb[npages-1].len - 1 );
1175                 CERROR("client csum %x, original server csum %x, "
1176                        "server csum now %x\n",
1177                        client_cksum, server_cksum, new_cksum);
1178         }
1179
1180         if (rc == 0) {
1181                 int nob = 0;
1182
1183                 /* set per-requested niobuf return codes */
1184                 for (i = j = 0; i < niocount; i++) {
1185                         int len = remote_nb[i].len;
1186
1187                         nob += len;
1188                         rcs[i] = 0;
1189                         do {
1190                                 LASSERT(j < npages);
1191                                 if (local_nb[j].rc < 0)
1192                                         rcs[i] = local_nb[j].rc;
1193                                 len -= local_nb[j].len;
1194                                 j++;
1195                         } while (len > 0);
1196                         LASSERT(len == 0);
1197                 }
1198                 LASSERT(j == npages);
1199                 ptlrpc_lprocfs_brw(req, nob);
1200         }
1201
1202 out_lock:
1203         ost_brw_lock_put(LCK_PW, ioo, remote_nb, &lockh);
1204 out_bulk:
1205         if (desc)
1206                 ptlrpc_free_bulk(desc);
1207 out:
1208        /* XXX: don't send reply if obd rdonly mode, this can cause data loss
1209         * on client, see bug 22190. Remove this when async bulk will be done.
1210         * Meanwhile, if this is umount then don't reply anything. */
1211         if (req->rq_export->exp_obd->obd_no_transno) {
1212                 no_reply = req->rq_export->exp_obd->obd_stopping;
1213                 rc = -EIO;
1214         }
1215
1216         if (rc == 0) {
1217                 oti_to_request(oti, req);
1218                 target_committed_to_req(req);
1219                 rc = ptlrpc_reply(req);
1220         } else if (!no_reply) {
1221                 /* Only reply if there was no comms problem with bulk */
1222                 target_committed_to_req(req);
1223                 req->rq_status = rc;
1224                 ptlrpc_error(req);
1225         } else {
1226                 /* reply out callback would free */
1227                 ptlrpc_req_drop_rs(req);
1228                 CWARN("%s: ignoring bulk IO comm error with %s@%s id %s - "
1229                       "client will retry\n",
1230                       exp->exp_obd->obd_name,
1231                       exp->exp_client_uuid.uuid,
1232                       exp->exp_connection->c_remote_uuid.uuid,
1233                       libcfs_id2str(req->rq_peer));
1234         }
1235         libcfs_memory_pressure_clr();
1236         RETURN(rc);
1237 }
1238
1239 /**
1240  * Implementation of OST_SET_INFO.
1241  *
1242  * OST_SET_INFO is like ioctl(): heavily overloaded.  Specifically, it takes a
1243  * "key" and a value RPC buffers as arguments, with the value's contents
1244  * interpreted according to the key.
1245  *
1246  * Value types that need swabbing have swabbing done explicitly, either here or
1247  * in functions called from here.  This should be corrected: all swabbing should
1248  * be done in the capsule abstraction, as that will then allow us to move
1249  * swabbing exclusively to the client without having to modify server code
1250  * outside the capsule abstraction's implementation itself.  To correct this
1251  * will require minor changes to the capsule abstraction; see the comments for
1252  * req_capsule_extend() in layout.c.
1253  */
1254 static int ost_set_info(struct obd_export *exp, struct ptlrpc_request *req)
1255 {
1256         struct ost_body *body = NULL, *repbody;
1257         char *key, *val = NULL;
1258         int keylen, vallen, rc = 0;
1259         int is_grant_shrink = 0;
1260         ENTRY;
1261
1262         key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1263         if (key == NULL) {
1264                 DEBUG_REQ(D_HA, req, "no set_info key");
1265                 RETURN(-EFAULT);
1266         }
1267         keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
1268                                       RCL_CLIENT);
1269
1270         vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
1271                                       RCL_CLIENT);
1272
1273         if ((is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK)))
1274                 /* In this case the value is actually an RMF_OST_BODY, so we
1275                  * transmutate the type of this PTLRPC */
1276                 req_capsule_extend(&req->rq_pill, &RQF_OST_SET_GRANT_INFO);
1277
1278         rc = req_capsule_server_pack(&req->rq_pill);
1279         if (rc)
1280                 RETURN(rc);
1281
1282         if (vallen) {
1283                 if (is_grant_shrink) {
1284                         body = req_capsule_client_get(&req->rq_pill,
1285                                                       &RMF_OST_BODY);
1286                         if (!body)
1287                                 RETURN(-EFAULT);
1288
1289                         repbody = req_capsule_server_get(&req->rq_pill,
1290                                                          &RMF_OST_BODY);
1291                         memcpy(repbody, body, sizeof(*body));
1292                         val = (char*)repbody;
1293                 } else {
1294                         val = req_capsule_client_get(&req->rq_pill,
1295                                                      &RMF_SETINFO_VAL);
1296                 }
1297         }
1298
1299         if (KEY_IS(KEY_EVICT_BY_NID)) {
1300                 if (val && vallen)
1301                         obd_export_evict_by_nid(exp->exp_obd, val);
1302                 GOTO(out, rc = 0);
1303         } else if (KEY_IS(KEY_MDS_CONN) && ptlrpc_req_need_swab(req)) {
1304                 if (vallen < sizeof(__u32))
1305                         RETURN(-EFAULT);
1306                 __swab32s((__u32 *)val);
1307         }
1308
1309         /* OBD will also check if KEY_IS(KEY_GRANT_SHRINK), and will cast val to
1310          * a struct ost_body * value */
1311         rc = obd_set_info_async(exp, keylen, key, vallen, val, NULL);
1312 out:
1313         lustre_msg_set_status(req->rq_repmsg, 0);
1314         RETURN(rc);
1315 }
1316
1317 static int ost_get_info(struct obd_export *exp, struct ptlrpc_request *req)
1318 {
1319         void *key, *reply;
1320         int keylen, replylen, rc = 0;
1321         struct req_capsule *pill = &req->rq_pill;
1322         ENTRY;
1323
1324         /* this common part for get_info rpc */
1325         key = req_capsule_client_get(pill, &RMF_SETINFO_KEY);
1326         if (key == NULL) {
1327                 DEBUG_REQ(D_HA, req, "no get_info key");
1328                 RETURN(-EFAULT);
1329         }
1330         keylen = req_capsule_get_size(pill, &RMF_SETINFO_KEY, RCL_CLIENT);
1331
1332         rc = obd_get_info(exp, keylen, key, &replylen, NULL, NULL);
1333         if (rc)
1334                 RETURN(rc);
1335
1336         req_capsule_set_size(pill, &RMF_GENERIC_DATA,
1337                              RCL_SERVER, replylen);
1338
1339         rc = req_capsule_server_pack(pill);
1340         if (rc)
1341                 RETURN(rc);
1342
1343         reply = req_capsule_server_get(pill, &RMF_GENERIC_DATA);
1344         if (reply == NULL)
1345                 RETURN(-ENOMEM);
1346
1347         /* call again to fill in the reply buffer */
1348         rc = obd_get_info(exp, keylen, key, &replylen, reply, NULL);
1349
1350         lustre_msg_set_status(req->rq_repmsg, 0);
1351         RETURN(rc);
1352 }
1353
1354 #ifdef HAVE_QUOTA_SUPPORT
1355 static int ost_handle_quotactl(struct ptlrpc_request *req)
1356 {
1357         struct obd_quotactl *oqctl, *repoqc;
1358         int rc;
1359         ENTRY;
1360
1361         oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1362         if (oqctl == NULL)
1363                 GOTO(out, rc = -EPROTO);
1364
1365         rc = req_capsule_server_pack(&req->rq_pill);
1366         if (rc)
1367                 GOTO(out, rc);
1368
1369         repoqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1370         req->rq_status = obd_quotactl(req->rq_export, oqctl);
1371         *repoqc = *oqctl;
1372
1373 out:
1374         RETURN(rc);
1375 }
1376
1377 static int ost_handle_quotacheck(struct ptlrpc_request *req)
1378 {
1379         struct obd_quotactl *oqctl;
1380         int rc;
1381         ENTRY;
1382
1383         oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1384         if (oqctl == NULL)
1385                 RETURN(-EPROTO);
1386
1387         rc = req_capsule_server_pack(&req->rq_pill);
1388         if (rc)
1389                 RETURN(-ENOMEM);
1390
1391         req->rq_status = obd_quotacheck(req->rq_export, oqctl);
1392         RETURN(0);
1393 }
1394
1395 static int ost_handle_quota_adjust_qunit(struct ptlrpc_request *req)
1396 {
1397         struct quota_adjust_qunit *oqaq, *repoqa;
1398         struct lustre_quota_ctxt *qctxt;
1399         int rc;
1400         ENTRY;
1401
1402         qctxt = &req->rq_export->exp_obd->u.obt.obt_qctxt;
1403         oqaq = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
1404         if (oqaq == NULL)
1405                 GOTO(out, rc = -EPROTO);
1406
1407         rc = req_capsule_server_pack(&req->rq_pill);
1408         if (rc)
1409                 GOTO(out, rc);
1410
1411         repoqa = req_capsule_server_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
1412         req->rq_status = obd_quota_adjust_qunit(req->rq_export, oqaq, qctxt);
1413         *repoqa = *oqaq;
1414
1415  out:
1416         RETURN(rc);
1417 }
1418 #endif
1419
1420 static int ost_llog_handle_connect(struct obd_export *exp,
1421                                    struct ptlrpc_request *req)
1422 {
1423         struct llogd_conn_body *body;
1424         int rc;
1425         ENTRY;
1426
1427         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_CONN_BODY);
1428         rc = obd_llog_connect(exp, body);
1429         RETURN(rc);
1430 }
1431
1432 #define ost_init_sec_none(reply, exp)                                   \
1433 do {                                                                    \
1434         reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |          \
1435                                       OBD_CONNECT_RMT_CLIENT_FORCE |    \
1436                                       OBD_CONNECT_OSS_CAPA);            \
1437         cfs_spin_lock(&exp->exp_lock);                                  \
1438         exp->exp_connect_flags = reply->ocd_connect_flags;              \
1439         cfs_spin_unlock(&exp->exp_lock);                                \
1440 } while (0)
1441
1442 static int ost_init_sec_level(struct ptlrpc_request *req)
1443 {
1444         struct obd_export *exp = req->rq_export;
1445         struct req_capsule *pill = &req->rq_pill;
1446         struct obd_device *obd = exp->exp_obd;
1447         struct filter_obd *filter = &obd->u.filter;
1448         char *client = libcfs_nid2str(req->rq_peer.nid);
1449         struct obd_connect_data *data, *reply;
1450         int rc = 0, remote;
1451         ENTRY;
1452
1453         data = req_capsule_client_get(pill, &RMF_CONNECT_DATA);
1454         reply = req_capsule_server_get(pill, &RMF_CONNECT_DATA);
1455         if (data == NULL || reply == NULL)
1456                 RETURN(-EFAULT);
1457
1458         /* connection from MDT is always trusted */
1459         if (req->rq_auth_usr_mdt) {
1460                 ost_init_sec_none(reply, exp);
1461                 RETURN(0);
1462         }
1463
1464         /* no GSS support case */
1465         if (!req->rq_auth_gss) {
1466                 if (filter->fo_sec_level > LUSTRE_SEC_NONE) {
1467                         CWARN("client %s -> target %s does not user GSS, "
1468                               "can not run under security level %d.\n",
1469                               client, obd->obd_name, filter->fo_sec_level);
1470                         RETURN(-EACCES);
1471                 } else {
1472                         ost_init_sec_none(reply, exp);
1473                         RETURN(0);
1474                 }
1475         }
1476
1477         /* old version case */
1478         if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
1479                      !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
1480                 if (filter->fo_sec_level > LUSTRE_SEC_NONE) {
1481                         CWARN("client %s -> target %s uses old version, "
1482                               "can not run under security level %d.\n",
1483                               client, obd->obd_name, filter->fo_sec_level);
1484                         RETURN(-EACCES);
1485                 } else {
1486                         CWARN("client %s -> target %s uses old version, "
1487                               "run under security level %d.\n",
1488                               client, obd->obd_name, filter->fo_sec_level);
1489                         ost_init_sec_none(reply, exp);
1490                         RETURN(0);
1491                 }
1492         }
1493
1494         remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
1495         if (remote) {
1496                 if (!req->rq_auth_remote)
1497                         CDEBUG(D_SEC, "client (local realm) %s -> target %s "
1498                                "asked to be remote.\n", client, obd->obd_name);
1499         } else if (req->rq_auth_remote) {
1500                 remote = 1;
1501                 CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
1502                        "as remote by default.\n", client, obd->obd_name);
1503         }
1504
1505         if (remote) {
1506                 if (!filter->fo_fl_oss_capa) {
1507                         CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
1508                                " but OSS capabilities are not enabled: %d.\n",
1509                                client, obd->obd_name, filter->fo_fl_oss_capa);
1510                         RETURN(-EACCES);
1511                 }
1512         }
1513
1514         switch (filter->fo_sec_level) {
1515         case LUSTRE_SEC_NONE:
1516                 if (!remote) {
1517                         ost_init_sec_none(reply, exp);
1518                         break;
1519                 } else {
1520                         CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
1521                                "can not run under security level %d.\n",
1522                                client, obd->obd_name, filter->fo_sec_level);
1523                         RETURN(-EACCES);
1524                 }
1525         case LUSTRE_SEC_REMOTE:
1526                 if (!remote)
1527                         ost_init_sec_none(reply, exp);
1528                 break;
1529         case LUSTRE_SEC_ALL:
1530                 if (!remote) {
1531                         reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |
1532                                                       OBD_CONNECT_RMT_CLIENT_FORCE);
1533                         if (!filter->fo_fl_oss_capa)
1534                                 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
1535
1536                         cfs_spin_lock(&exp->exp_lock);
1537                         exp->exp_connect_flags = reply->ocd_connect_flags;
1538                         cfs_spin_unlock(&exp->exp_lock);
1539                 }
1540                 break;
1541         default:
1542                 RETURN(-EINVAL);
1543         }
1544
1545         RETURN(rc);
1546 }
1547
1548 /*
1549  * FIXME
1550  * this should be done in filter_connect()/filter_reconnect(), but
1551  * we can't obtain information like NID, which stored in incoming
1552  * request, thus can't decide what flavor to use. so we do it here.
1553  *
1554  * This hack should be removed after the OST stack be rewritten, just
1555  * like what we are doing in mdt_obd_connect()/mdt_obd_reconnect().
1556  */
1557 static int ost_connect_check_sptlrpc(struct ptlrpc_request *req)
1558 {
1559         struct obd_export     *exp = req->rq_export;
1560         struct filter_obd     *filter = &exp->exp_obd->u.filter;
1561         struct sptlrpc_flavor  flvr;
1562         int                    rc = 0;
1563
1564         if (unlikely(strcmp(exp->exp_obd->obd_type->typ_name,
1565                             LUSTRE_ECHO_NAME) == 0)) {
1566                 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_ANY;
1567                 return 0;
1568         }
1569
1570         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
1571                 cfs_read_lock(&filter->fo_sptlrpc_lock);
1572                 sptlrpc_target_choose_flavor(&filter->fo_sptlrpc_rset,
1573                                              req->rq_sp_from,
1574                                              req->rq_peer.nid,
1575                                              &flvr);
1576                 cfs_read_unlock(&filter->fo_sptlrpc_lock);
1577
1578                 cfs_spin_lock(&exp->exp_lock);
1579
1580                 exp->exp_sp_peer = req->rq_sp_from;
1581                 exp->exp_flvr = flvr;
1582
1583                 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
1584                     exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
1585                         CERROR("unauthorized rpc flavor %x from %s, "
1586                                "expect %x\n", req->rq_flvr.sf_rpc,
1587                                libcfs_nid2str(req->rq_peer.nid),
1588                                exp->exp_flvr.sf_rpc);
1589                         rc = -EACCES;
1590                 }
1591
1592                 cfs_spin_unlock(&exp->exp_lock);
1593         } else {
1594                 if (exp->exp_sp_peer != req->rq_sp_from) {
1595                         CERROR("RPC source %s doesn't match %s\n",
1596                                sptlrpc_part2name(req->rq_sp_from),
1597                                sptlrpc_part2name(exp->exp_sp_peer));
1598                         rc = -EACCES;
1599                 } else {
1600                         rc = sptlrpc_target_export_check(exp, req);
1601                 }
1602         }
1603
1604         return rc;
1605 }
1606
1607 static int ost_filter_recovery_request(struct ptlrpc_request *req,
1608                                        struct obd_device *obd, int *process)
1609 {
1610         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1611         case OST_CONNECT: /* This will never get here, but for completeness. */
1612         case OST_DISCONNECT:
1613                *process = 1;
1614                RETURN(0);
1615
1616         case OBD_PING:
1617         case OST_CREATE:
1618         case OST_DESTROY:
1619         case OST_PUNCH:
1620         case OST_SETATTR:
1621         case OST_SYNC:
1622         case OST_WRITE:
1623         case OBD_LOG_CANCEL:
1624         case LDLM_ENQUEUE:
1625                 *process = target_queue_recovery_request(req, obd);
1626                 RETURN(0);
1627
1628         default:
1629                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1630                 *process = -EAGAIN;
1631                 RETURN(0);
1632         }
1633 }
1634
1635 int ost_msg_check_version(struct lustre_msg *msg)
1636 {
1637         int rc;
1638
1639         switch(lustre_msg_get_opc(msg)) {
1640         case OST_CONNECT:
1641         case OST_DISCONNECT:
1642         case OBD_PING:
1643         case SEC_CTX_INIT:
1644         case SEC_CTX_INIT_CONT:
1645         case SEC_CTX_FINI:
1646                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1647                 if (rc)
1648                         CERROR("bad opc %u version %08x, expecting %08x\n",
1649                                lustre_msg_get_opc(msg),
1650                                lustre_msg_get_version(msg),
1651                                LUSTRE_OBD_VERSION);
1652                 break;
1653         case OST_CREATE:
1654         case OST_DESTROY:
1655         case OST_GETATTR:
1656         case OST_SETATTR:
1657         case OST_WRITE:
1658         case OST_READ:
1659         case OST_PUNCH:
1660         case OST_STATFS:
1661         case OST_SYNC:
1662         case OST_SET_INFO:
1663         case OST_GET_INFO:
1664 #ifdef HAVE_QUOTA_SUPPORT
1665         case OST_QUOTACHECK:
1666         case OST_QUOTACTL:
1667         case OST_QUOTA_ADJUST_QUNIT:
1668 #endif
1669                 rc = lustre_msg_check_version(msg, LUSTRE_OST_VERSION);
1670                 if (rc)
1671                         CERROR("bad opc %u version %08x, expecting %08x\n",
1672                                lustre_msg_get_opc(msg),
1673                                lustre_msg_get_version(msg),
1674                                LUSTRE_OST_VERSION);
1675                 break;
1676         case LDLM_ENQUEUE:
1677         case LDLM_CONVERT:
1678         case LDLM_CANCEL:
1679         case LDLM_BL_CALLBACK:
1680         case LDLM_CP_CALLBACK:
1681                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1682                 if (rc)
1683                         CERROR("bad opc %u version %08x, expecting %08x\n",
1684                                lustre_msg_get_opc(msg),
1685                                lustre_msg_get_version(msg),
1686                                LUSTRE_DLM_VERSION);
1687                 break;
1688         case LLOG_ORIGIN_CONNECT:
1689         case OBD_LOG_CANCEL:
1690                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1691                 if (rc)
1692                         CERROR("bad opc %u version %08x, expecting %08x\n",
1693                                lustre_msg_get_opc(msg),
1694                                lustre_msg_get_version(msg),
1695                                LUSTRE_LOG_VERSION);
1696                 break;
1697         default:
1698                 CERROR("Unexpected opcode %d\n", lustre_msg_get_opc(msg));
1699                 rc = -ENOTSUPP;
1700         }
1701         return rc;
1702 }
1703
1704 /**
1705  * Returns 1 if the given PTLRPC matches the given LDLM locks, or 0 if it does
1706  * not.
1707  */
1708 static int ost_rw_hpreq_lock_match(struct ptlrpc_request *req,
1709                                    struct ldlm_lock *lock)
1710 {
1711         struct niobuf_remote *nb;
1712         struct obd_ioobj *ioo;
1713         struct ost_body *body;
1714         int objcount, niocount;
1715         int mode, opc, i;
1716         __u64 start, end;
1717         ENTRY;
1718
1719         opc = lustre_msg_get_opc(req->rq_reqmsg);
1720         LASSERT(opc == OST_READ || opc == OST_WRITE);
1721
1722         /* As the request may be covered by several locks, do not look at
1723          * o_handle, look at the RPC IO region. */
1724         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1725         if (body == NULL)
1726                 RETURN(0);
1727
1728         objcount = req_capsule_get_size(&req->rq_pill, &RMF_OBD_IOOBJ,
1729                                         RCL_CLIENT) / sizeof(*ioo);
1730         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1731         if (ioo == NULL)
1732                 RETURN(0);
1733
1734         for (niocount = i = 0; i < objcount; i++)
1735                 niocount += ioo[i].ioo_bufcnt;
1736
1737         nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1738         if (nb == NULL ||
1739             niocount != (req_capsule_get_size(&req->rq_pill, &RMF_NIOBUF_REMOTE,
1740             RCL_CLIENT) / sizeof(*nb)))
1741                 RETURN(0);
1742
1743         mode = LCK_PW;
1744         if (opc == OST_READ)
1745                 mode |= LCK_PR;
1746
1747         start = nb[0].offset & CFS_PAGE_MASK;
1748         end = (nb[ioo->ioo_bufcnt - 1].offset +
1749                nb[ioo->ioo_bufcnt - 1].len - 1) | ~CFS_PAGE_MASK;
1750
1751         LASSERT(lock->l_resource != NULL);
1752         if (!osc_res_name_eq(ioo->ioo_id, ioo->ioo_gr,
1753                              &lock->l_resource->lr_name))
1754                 RETURN(0);
1755
1756         if (!(lock->l_granted_mode & mode))
1757                 RETURN(0);
1758
1759         if (lock->l_policy_data.l_extent.end < start ||
1760             lock->l_policy_data.l_extent.start > end)
1761                 RETURN(0);
1762
1763         RETURN(1);
1764 }
1765
1766 /**
1767  * High-priority queue request check for whether the given PTLRPC request (\a
1768  * req) is blocking an LDLM lock cancel.
1769  *
1770  * Returns 1 if the given given PTLRPC request (\a req) is blocking an LDLM lock
1771  * cancel, 0 if it is not, and -EFAULT if the request is malformed.
1772  *
1773  * Only OST_READs, OST_WRITEs and OST_PUNCHes go on the h-p RPC queue.  This
1774  * function looks only at OST_READs and OST_WRITEs.
1775  */
1776 static int ost_rw_hpreq_check(struct ptlrpc_request *req)
1777 {
1778         struct niobuf_remote *nb;
1779         struct obd_ioobj *ioo;
1780         struct ost_body *body;
1781         int objcount, niocount;
1782         int mode, opc, i;
1783         ENTRY;
1784
1785         opc = lustre_msg_get_opc(req->rq_reqmsg);
1786         LASSERT(opc == OST_READ || opc == OST_WRITE);
1787
1788         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1789         if (body == NULL)
1790                 RETURN(-EFAULT);
1791
1792         objcount = req_capsule_get_size(&req->rq_pill, &RMF_OBD_IOOBJ,
1793                                         RCL_CLIENT) / sizeof(*ioo);
1794         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1795         if (ioo == NULL)
1796                 RETURN(-EFAULT);
1797
1798         for (niocount = i = 0; i < objcount; i++)
1799                 niocount += ioo[i].ioo_bufcnt;
1800         nb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1801         if (nb == NULL ||
1802             niocount != (req_capsule_get_size(&req->rq_pill, &RMF_NIOBUF_REMOTE,
1803             RCL_CLIENT) / sizeof(*nb)))
1804                 RETURN(-EFAULT);
1805         if (niocount != 0 && (nb[0].flags & OBD_BRW_SRVLOCK))
1806                 RETURN(-EFAULT);
1807
1808         mode = LCK_PW;
1809         if (opc == OST_READ)
1810                 mode |= LCK_PR;
1811         RETURN(ost_rw_prolong_locks(req, ioo, nb, &body->oa, mode));
1812 }
1813
1814 static int ost_punch_prolong_locks(struct ptlrpc_request *req, struct obdo *oa)
1815 {
1816         struct ldlm_res_id res_id = { .name = { oa->o_id } };
1817         struct ost_prolong_data opd = { 0 };
1818         __u64 start, end;
1819         ENTRY;
1820
1821         start = oa->o_size;
1822         end = start + oa->o_blocks;
1823
1824         opd.opd_mode = LCK_PW;
1825         opd.opd_exp = req->rq_export;
1826         opd.opd_policy.l_extent.start = start & CFS_PAGE_MASK;
1827         if (oa->o_blocks == OBD_OBJECT_EOF || end < start)
1828                 opd.opd_policy.l_extent.end = OBD_OBJECT_EOF;
1829         else
1830                 opd.opd_policy.l_extent.end = end | ~CFS_PAGE_MASK;
1831
1832         /* prolong locks for the current service time of the corresponding
1833          * portal (= OST_IO_PORTAL) */
1834         opd.opd_timeout = AT_OFF ? obd_timeout / 2:
1835                           max(at_est2timeout(at_get(&req->rq_rqbd->
1836                               rqbd_service->srv_at_estimate)), ldlm_timeout);
1837
1838         CDEBUG(D_DLMTRACE,"refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
1839                res_id.name[0], res_id.name[1], opd.opd_policy.l_extent.start,
1840                opd.opd_policy.l_extent.end);
1841
1842         opd.opd_oa = oa;
1843         ldlm_resource_iterate(req->rq_export->exp_obd->obd_namespace, &res_id,
1844                               ost_prolong_locks_iter, &opd);
1845         RETURN(opd.opd_lock_match);
1846 }
1847
1848 /**
1849  * Like ost_rw_hpreq_lock_match(), but for OST_PUNCH RPCs.
1850  */
1851 static int ost_punch_hpreq_lock_match(struct ptlrpc_request *req,
1852                                       struct ldlm_lock *lock)
1853 {
1854         struct ost_body *body;
1855         ENTRY;
1856
1857         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1858         if (body == NULL)
1859                 RETURN(0);  /* can't return -EFAULT here */
1860
1861         if (body->oa.o_valid & OBD_MD_FLHANDLE &&
1862             body->oa.o_handle.cookie == lock->l_handle.h_cookie)
1863                 RETURN(1);
1864         RETURN(0);
1865 }
1866
1867 /**
1868  * Like ost_rw_hpreq_check(), but for OST_PUNCH RPCs.
1869  */
1870 static int ost_punch_hpreq_check(struct ptlrpc_request *req)
1871 {
1872         struct ost_body *body;
1873
1874         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1875         if (body == NULL)
1876                 RETURN(-EFAULT);
1877
1878         LASSERT(!(body->oa.o_valid & OBD_MD_FLFLAGS) ||
1879                 !(body->oa.o_flags & OBD_FL_SRVLOCK));
1880
1881         RETURN(ost_punch_prolong_locks(req, &body->oa));
1882 }
1883
1884 struct ptlrpc_hpreq_ops ost_hpreq_rw = {
1885         .hpreq_lock_match  = ost_rw_hpreq_lock_match,
1886         .hpreq_check       = ost_rw_hpreq_check,
1887 };
1888
1889 struct ptlrpc_hpreq_ops ost_hpreq_punch = {
1890         .hpreq_lock_match  = ost_punch_hpreq_lock_match,
1891         .hpreq_check       = ost_punch_hpreq_check,
1892 };
1893
1894 /** Assign high priority operations to the request if needed. */
1895 static int ost_hpreq_handler(struct ptlrpc_request *req)
1896 {
1897         ENTRY;
1898         if (req->rq_export) {
1899                 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1900                 struct ost_body *body;
1901
1902                 if (opc == OST_READ || opc == OST_WRITE) {
1903                         struct niobuf_remote *nb;
1904                         struct obd_ioobj *ioo;
1905                         int objcount, niocount;
1906                         int i;
1907
1908                         /* RPCs on the H-P queue can be inspected before
1909                          * ost_handler() initializes their pills, so we
1910                          * initialize that here.  Capsule initialization is
1911                          * idempotent, as is setting the pill's format (provided
1912                          * it doesn't change).
1913                          */
1914                         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1915                         if (opc == OST_READ)
1916                                 req_capsule_set(&req->rq_pill,
1917                                                 &RQF_OST_BRW_READ);
1918                         else
1919                                 req_capsule_set(&req->rq_pill,
1920                                                 &RQF_OST_BRW_WRITE);
1921
1922                         body = req_capsule_client_get(&req->rq_pill,
1923                                                       &RMF_OST_BODY);
1924                         if (body == NULL) {
1925                                 CERROR("Missing/short ost_body\n");
1926                                 RETURN(-EFAULT);
1927                         }
1928                         objcount = req_capsule_get_size(&req->rq_pill,
1929                                                         &RMF_OBD_IOOBJ,
1930                                                         RCL_CLIENT) /
1931                                                         sizeof(*ioo);
1932                         if (objcount == 0) {
1933                                 CERROR("Missing/short ioobj\n");
1934                                 RETURN(-EFAULT);
1935                         }
1936                         if (objcount > 1) {
1937                                 CERROR("too many ioobjs (%d)\n", objcount);
1938                                 RETURN(-EFAULT);
1939                         }
1940
1941                         ioo = req_capsule_client_get(&req->rq_pill,
1942                                                      &RMF_OBD_IOOBJ);
1943                         if (ioo == NULL) {
1944                                 CERROR("Missing/short ioobj\n");
1945                                 RETURN(-EFAULT);
1946                         }
1947
1948                         for (niocount = i = 0; i < objcount; i++) {
1949                                 if (ioo[i].ioo_bufcnt == 0) {
1950                                         CERROR("ioo[%d] has zero bufcnt\n", i);
1951                                         RETURN(-EFAULT);
1952                                 }
1953                                 niocount += ioo[i].ioo_bufcnt;
1954                         }
1955                         if (niocount > PTLRPC_MAX_BRW_PAGES) {
1956                                 DEBUG_REQ(D_RPCTRACE, req,
1957                                           "bulk has too many pages (%d)",
1958                                           niocount);
1959                                 RETURN(-EFAULT);
1960                         }
1961
1962                         nb = req_capsule_client_get(&req->rq_pill,
1963                                                     &RMF_NIOBUF_REMOTE);
1964                         if (nb == NULL) {
1965                                 CERROR("Missing/short niobuf\n");
1966                                 RETURN(-EFAULT);
1967                         }
1968
1969                         if (niocount == 0 || !(nb[0].flags & OBD_BRW_SRVLOCK))
1970                                 req->rq_ops = &ost_hpreq_rw;
1971                 } else if (opc == OST_PUNCH) {
1972                         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1973                         req_capsule_set(&req->rq_pill, &RQF_OST_PUNCH);
1974
1975                         body = req_capsule_client_get(&req->rq_pill,
1976                                                       &RMF_OST_BODY);
1977                         if (body == NULL) {
1978                                 CERROR("Missing/short ost_body\n");
1979                                 RETURN(-EFAULT);
1980                         }
1981
1982                         if (!(body->oa.o_valid & OBD_MD_FLFLAGS) ||
1983                             !(body->oa.o_flags & OBD_FL_SRVLOCK))
1984                                 req->rq_ops = &ost_hpreq_punch;
1985                 }
1986         }
1987         RETURN(0);
1988 }
1989
1990 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1991 int ost_handle(struct ptlrpc_request *req)
1992 {
1993         struct obd_trans_info trans_info = { 0, };
1994         struct obd_trans_info *oti = &trans_info;
1995         int should_process, fail = OBD_FAIL_OST_ALL_REPLY_NET, rc = 0;
1996         struct obd_device *obd = NULL;
1997         ENTRY;
1998
1999         LASSERT(current->journal_info == NULL);
2000
2001         /* primordial rpcs don't affect server recovery */
2002         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2003         case SEC_CTX_INIT:
2004         case SEC_CTX_INIT_CONT:
2005         case SEC_CTX_FINI:
2006                 GOTO(out, rc = 0);
2007         }
2008
2009         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2010
2011         if (lustre_msg_get_opc(req->rq_reqmsg) != OST_CONNECT) {
2012                 if (!class_connected_export(req->rq_export)) {
2013                         CDEBUG(D_HA,"operation %d on unconnected OST from %s\n",
2014                                lustre_msg_get_opc(req->rq_reqmsg),
2015                                libcfs_id2str(req->rq_peer));
2016                         req->rq_status = -ENOTCONN;
2017                         GOTO(out, rc = -ENOTCONN);
2018                 }
2019
2020                 obd = req->rq_export->exp_obd;
2021
2022                 /* Check for aborted recovery. */
2023                 if (obd->obd_recovering) {
2024                         rc = ost_filter_recovery_request(req, obd,
2025                                                          &should_process);
2026                         if (rc || !should_process)
2027                                 RETURN(rc);
2028                         else if (should_process < 0) {
2029                                 req->rq_status = should_process;
2030                                 rc = ptlrpc_error(req);
2031                                 RETURN(rc);
2032                         }
2033                 }
2034         }
2035
2036         oti_init(oti, req);
2037
2038         rc = ost_msg_check_version(req->rq_reqmsg);
2039         if (rc)
2040                 RETURN(rc);
2041
2042         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2043         case OST_CONNECT: {
2044                 CDEBUG(D_INODE, "connect\n");
2045                 req_capsule_set(&req->rq_pill, &RQF_OST_CONNECT);
2046                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET))
2047                         RETURN(0);
2048                 rc = target_handle_connect(req);
2049                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CONNECT_NET2))
2050                         RETURN(0);
2051                 if (!rc) {
2052                         rc = ost_init_sec_level(req);
2053                         if (!rc)
2054                                 rc = ost_connect_check_sptlrpc(req);
2055                 }
2056                 break;
2057         }
2058         case OST_DISCONNECT:
2059                 CDEBUG(D_INODE, "disconnect\n");
2060                 req_capsule_set(&req->rq_pill, &RQF_OST_DISCONNECT);
2061                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DISCONNECT_NET))
2062                         RETURN(0);
2063                 rc = target_handle_disconnect(req);
2064                 break;
2065         case OST_CREATE:
2066                 CDEBUG(D_INODE, "create\n");
2067                 req_capsule_set(&req->rq_pill, &RQF_OST_CREATE);
2068                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_CREATE_NET))
2069                         RETURN(0);
2070                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2071                         GOTO(out, rc = -EROFS);
2072                 rc = ost_create(req->rq_export, req, oti);
2073                 break;
2074         case OST_DESTROY:
2075                 CDEBUG(D_INODE, "destroy\n");
2076                 req_capsule_set(&req->rq_pill, &RQF_OST_DESTROY);
2077                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DESTROY_NET))
2078                         RETURN(0);
2079                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2080                         GOTO(out, rc = -EROFS);
2081                 rc = ost_destroy(req->rq_export, req, oti);
2082                 break;
2083         case OST_GETATTR:
2084                 CDEBUG(D_INODE, "getattr\n");
2085                 req_capsule_set(&req->rq_pill, &RQF_OST_GETATTR);
2086                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_GETATTR_NET))
2087                         RETURN(0);
2088                 rc = ost_getattr(req->rq_export, req);
2089                 break;
2090         case OST_SETATTR:
2091                 CDEBUG(D_INODE, "setattr\n");
2092                 req_capsule_set(&req->rq_pill, &RQF_OST_SETATTR);
2093                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SETATTR_NET))
2094                         RETURN(0);
2095                 rc = ost_setattr(req->rq_export, req, oti);
2096                 break;
2097         case OST_WRITE:
2098                 req_capsule_set(&req->rq_pill, &RQF_OST_BRW_WRITE);
2099                 CDEBUG(D_INODE, "write\n");
2100                 /* req->rq_request_portal would be nice, if it was set */
2101                 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
2102                         CERROR("%s: deny write request from %s to portal %u\n",
2103                                req->rq_export->exp_obd->obd_name,
2104                                obd_export_nid2str(req->rq_export),
2105                                req->rq_rqbd->rqbd_service->srv_req_portal);
2106                         GOTO(out, rc = -EPROTO);
2107                 }
2108                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
2109                         RETURN(0);
2110                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC))
2111                         GOTO(out, rc = -ENOSPC);
2112                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2113                         GOTO(out, rc = -EROFS);
2114                 rc = ost_brw_write(req, oti);
2115                 LASSERT(current->journal_info == NULL);
2116                 /* ost_brw_write sends its own replies */
2117                 RETURN(rc);
2118         case OST_READ:
2119                 req_capsule_set(&req->rq_pill, &RQF_OST_BRW_READ);
2120                 CDEBUG(D_INODE, "read\n");
2121                 /* req->rq_request_portal would be nice, if it was set */
2122                 if (req->rq_rqbd->rqbd_service->srv_req_portal !=OST_IO_PORTAL){
2123                         CERROR("%s: deny read request from %s to portal %u\n",
2124                                req->rq_export->exp_obd->obd_name,
2125                                obd_export_nid2str(req->rq_export),
2126                                req->rq_rqbd->rqbd_service->srv_req_portal);
2127                         GOTO(out, rc = -EPROTO);
2128                 }
2129                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_NET))
2130                         RETURN(0);
2131                 rc = ost_brw_read(req, oti);
2132                 LASSERT(current->journal_info == NULL);
2133                 /* ost_brw_read sends its own replies */
2134                 RETURN(rc);
2135         case OST_PUNCH:
2136                 CDEBUG(D_INODE, "punch\n");
2137                 req_capsule_set(&req->rq_pill, &RQF_OST_PUNCH);
2138                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_PUNCH_NET))
2139                         RETURN(0);
2140                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
2141                         GOTO(out, rc = -EROFS);
2142                 rc = ost_punch(req->rq_export, req, oti);
2143                 break;
2144         case OST_STATFS:
2145                 CDEBUG(D_INODE, "statfs\n");
2146                 req_capsule_set(&req->rq_pill, &RQF_OST_STATFS);
2147                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_NET))
2148                         RETURN(0);
2149                 rc = ost_statfs(req);
2150                 break;
2151         case OST_SYNC:
2152                 CDEBUG(D_INODE, "sync\n");
2153                 req_capsule_set(&req->rq_pill, &RQF_OST_SYNC);
2154                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_SYNC_NET))
2155                         RETURN(0);
2156                 rc = ost_sync(req->rq_export, req);
2157                 break;
2158         case OST_SET_INFO:
2159                 DEBUG_REQ(D_INODE, req, "set_info");
2160                 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
2161                 rc = ost_set_info(req->rq_export, req);
2162                 break;
2163         case OST_GET_INFO:
2164                 DEBUG_REQ(D_INODE, req, "get_info");
2165                 req_capsule_set(&req->rq_pill, &RQF_OST_GET_INFO_GENERIC);
2166                 rc = ost_get_info(req->rq_export, req);
2167                 break;
2168 #ifdef HAVE_QUOTA_SUPPORT
2169         case OST_QUOTACHECK:
2170                 CDEBUG(D_INODE, "quotacheck\n");
2171                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACHECK);
2172                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACHECK_NET))
2173                         RETURN(0);
2174                 rc = ost_handle_quotacheck(req);
2175                 break;
2176         case OST_QUOTACTL:
2177                 CDEBUG(D_INODE, "quotactl\n");
2178                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTACTL);
2179                 if (OBD_FAIL_CHECK(OBD_FAIL_OST_QUOTACTL_NET))
2180                         RETURN(0);
2181                 rc = ost_handle_quotactl(req);
2182                 break;
2183         case OST_QUOTA_ADJUST_QUNIT:
2184                 CDEBUG(D_INODE, "quota_adjust_qunit\n");
2185                 req_capsule_set(&req->rq_pill, &RQF_OST_QUOTA_ADJUST_QUNIT);
2186                 rc = ost_handle_quota_adjust_qunit(req);
2187                 break;
2188 #endif
2189         case OBD_PING:
2190                 DEBUG_REQ(D_INODE, req, "ping");
2191                 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
2192                 rc = target_handle_ping(req);
2193                 break;
2194         /* FIXME - just reply status */
2195         case LLOG_ORIGIN_CONNECT:
2196                 DEBUG_REQ(D_INODE, req, "log connect");
2197                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_CONNECT);
2198                 rc = ost_llog_handle_connect(req->rq_export, req);
2199                 req->rq_status = rc;
2200                 rc = req_capsule_server_pack(&req->rq_pill);
2201                 if (rc)
2202                         RETURN(rc);
2203                 RETURN(ptlrpc_reply(req));
2204         case OBD_LOG_CANCEL:
2205                 CDEBUG(D_INODE, "log cancel\n");
2206                 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2207                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2208                         RETURN(0);
2209                 rc = llog_origin_handle_cancel(req);
2210                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2211                         RETURN(0);
2212                 req->rq_status = rc;
2213                 rc = req_capsule_server_pack(&req->rq_pill);
2214                 if (rc)
2215                         RETURN(rc);
2216                 RETURN(ptlrpc_reply(req));
2217         case LDLM_ENQUEUE:
2218                 CDEBUG(D_INODE, "enqueue\n");
2219                 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
2220                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE))
2221                         RETURN(0);
2222                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
2223                                          ldlm_server_blocking_ast,
2224                                          ldlm_server_glimpse_ast);
2225                 fail = OBD_FAIL_OST_LDLM_REPLY_NET;
2226                 break;
2227         case LDLM_CONVERT:
2228                 CDEBUG(D_INODE, "convert\n");
2229                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CONVERT);
2230                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CONVERT))
2231                         RETURN(0);
2232                 rc = ldlm_handle_convert(req);
2233                 break;
2234         case LDLM_CANCEL:
2235                 CDEBUG(D_INODE, "cancel\n");
2236                 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2237                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
2238                         RETURN(0);
2239                 rc = ldlm_handle_cancel(req);
2240                 break;
2241         case LDLM_BL_CALLBACK:
2242         case LDLM_CP_CALLBACK:
2243                 CDEBUG(D_INODE, "callback\n");
2244                 CERROR("callbacks should not happen on OST\n");
2245                 /* fall through */
2246         default:
2247                 CERROR("Unexpected opcode %d\n",
2248                        lustre_msg_get_opc(req->rq_reqmsg));
2249                 req->rq_status = -ENOTSUPP;
2250                 rc = ptlrpc_error(req);
2251                 RETURN(rc);
2252         }
2253
2254         LASSERT(current->journal_info == NULL);
2255
2256         EXIT;
2257         /* If we're DISCONNECTing, the export_data is already freed */
2258         if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != OST_DISCONNECT)
2259                 target_committed_to_req(req);
2260
2261 out:
2262         if (!rc)
2263                 oti_to_request(oti, req);
2264
2265         target_send_reply(req, rc, fail);
2266         return 0;
2267 }
2268 EXPORT_SYMBOL(ost_handle);
2269 /*
2270  * free per-thread pool created by ost_thread_init().
2271  */
2272 static void ost_thread_done(struct ptlrpc_thread *thread)
2273 {
2274         struct ost_thread_local_cache *tls; /* TLS stands for Thread-Local
2275                                              * Storage */
2276
2277         ENTRY;
2278
2279         LASSERT(thread != NULL);
2280
2281         /*
2282          * be prepared to handle partially-initialized pools (because this is
2283          * called from ost_thread_init() for cleanup.
2284          */
2285         tls = thread->t_data;
2286         if (tls != NULL) {
2287                 OBD_FREE_PTR(tls);
2288                 thread->t_data = NULL;
2289         }
2290         EXIT;
2291 }
2292
2293 /*
2294  * initialize per-thread page pool (bug 5137).
2295  */
2296 static int ost_thread_init(struct ptlrpc_thread *thread)
2297 {
2298         struct ost_thread_local_cache *tls;
2299
2300         ENTRY;
2301
2302         LASSERT(thread != NULL);
2303         LASSERT(thread->t_data == NULL);
2304         LASSERTF(thread->t_id <= OSS_THREADS_MAX, "%u\n", thread->t_id);
2305
2306         OBD_ALLOC_PTR(tls);
2307         if (tls == NULL)
2308                 RETURN(-ENOMEM);
2309         thread->t_data = tls;
2310         RETURN(0);
2311 }
2312
2313 #define OST_WATCHDOG_TIMEOUT (obd_timeout * 1000)
2314
2315 /* Sigh - really, this is an OSS, the _server_, not the _target_ */
2316 static int ost_setup(struct obd_device *obd, struct lustre_cfg* lcfg)
2317 {
2318         struct ost_obd *ost = &obd->u.ost;
2319         struct lprocfs_static_vars lvars;
2320         int oss_min_threads;
2321         int oss_max_threads;
2322         int oss_min_create_threads;
2323         int oss_max_create_threads;
2324         int rc;
2325         ENTRY;
2326
2327         rc = cfs_cleanup_group_info();
2328         if (rc)
2329                 RETURN(rc);
2330
2331         lprocfs_ost_init_vars(&lvars);
2332         lprocfs_obd_setup(obd, lvars.obd_vars);
2333
2334         cfs_sema_init(&ost->ost_health_sem, 1);
2335
2336         if (oss_num_threads) {
2337                 /* If oss_num_threads is set, it is the min and the max. */
2338                 if (oss_num_threads > OSS_THREADS_MAX)
2339                         oss_num_threads = OSS_THREADS_MAX;
2340                 if (oss_num_threads < OSS_THREADS_MIN)
2341                         oss_num_threads = OSS_THREADS_MIN;
2342                 oss_max_threads = oss_min_threads = oss_num_threads;
2343         } else {
2344                 /* Base min threads on memory and cpus */
2345                 oss_min_threads =
2346                         cfs_num_possible_cpus() * CFS_NUM_CACHEPAGES >>
2347                         (27 - CFS_PAGE_SHIFT);
2348                 if (oss_min_threads < OSS_THREADS_MIN)
2349                         oss_min_threads = OSS_THREADS_MIN;
2350                 /* Insure a 4x range for dynamic threads */
2351                 if (oss_min_threads > OSS_THREADS_MAX / 4)
2352                         oss_min_threads = OSS_THREADS_MAX / 4;
2353                 oss_max_threads = min(OSS_THREADS_MAX, oss_min_threads * 4 + 1);
2354         }
2355
2356         ost->ost_service =
2357                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
2358                                 OST_MAXREPSIZE, OST_REQUEST_PORTAL,
2359                                 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
2360                                 ost_handle, LUSTRE_OSS_NAME,
2361                                 obd->obd_proc_entry, target_print_req,
2362                                 oss_min_threads, oss_max_threads,
2363                                 "ll_ost", LCT_DT_THREAD, NULL);
2364         if (ost->ost_service == NULL) {
2365                 CERROR("failed to start service\n");
2366                 GOTO(out_lprocfs, rc = -ENOMEM);
2367         }
2368
2369         rc = ptlrpc_start_threads(obd, ost->ost_service);
2370         if (rc)
2371                 GOTO(out_service, rc = -EINVAL);
2372
2373         if (oss_num_create_threads) {
2374                 if (oss_num_create_threads > OSS_MAX_CREATE_THREADS)
2375                         oss_num_create_threads = OSS_MAX_CREATE_THREADS;
2376                 if (oss_num_create_threads < OSS_MIN_CREATE_THREADS)
2377                         oss_num_create_threads = OSS_MIN_CREATE_THREADS;
2378                 oss_min_create_threads = oss_max_create_threads =
2379                         oss_num_create_threads;
2380         } else {
2381                 oss_min_create_threads = OSS_MIN_CREATE_THREADS;
2382                 oss_max_create_threads = OSS_MAX_CREATE_THREADS;
2383         }
2384
2385         ost->ost_create_service =
2386                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
2387                                 OST_MAXREPSIZE, OST_CREATE_PORTAL,
2388                                 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
2389                                 ost_handle, "ost_create",
2390                                 obd->obd_proc_entry, target_print_req,
2391                                 oss_min_create_threads, oss_max_create_threads,
2392                                 "ll_ost_creat", LCT_DT_THREAD, NULL);
2393         if (ost->ost_create_service == NULL) {
2394                 CERROR("failed to start OST create service\n");
2395                 GOTO(out_service, rc = -ENOMEM);
2396         }
2397
2398         rc = ptlrpc_start_threads(obd, ost->ost_create_service);
2399         if (rc)
2400                 GOTO(out_create, rc = -EINVAL);
2401
2402         ost->ost_io_service =
2403                 ptlrpc_init_svc(OST_NBUFS, OST_BUFSIZE, OST_MAXREQSIZE,
2404                                 OST_MAXREPSIZE, OST_IO_PORTAL,
2405                                 OSC_REPLY_PORTAL, OSS_SERVICE_WATCHDOG_FACTOR,
2406                                 ost_handle, "ost_io",
2407                                 obd->obd_proc_entry, target_print_req,
2408                                 oss_min_threads, oss_max_threads,
2409                                 "ll_ost_io", LCT_DT_THREAD, ost_hpreq_handler);
2410         if (ost->ost_io_service == NULL) {
2411                 CERROR("failed to start OST I/O service\n");
2412                 GOTO(out_create, rc = -ENOMEM);
2413         }
2414
2415         ost->ost_io_service->srv_init = ost_thread_init;
2416         ost->ost_io_service->srv_done = ost_thread_done;
2417         ost->ost_io_service->srv_cpu_affinity = 1;
2418         rc = ptlrpc_start_threads(obd, ost->ost_io_service);
2419         if (rc)
2420                 GOTO(out_io, rc = -EINVAL);
2421
2422         ping_evictor_start();
2423
2424         RETURN(0);
2425
2426 out_io:
2427         ptlrpc_unregister_service(ost->ost_io_service);
2428         ost->ost_io_service = NULL;
2429 out_create:
2430         ptlrpc_unregister_service(ost->ost_create_service);
2431         ost->ost_create_service = NULL;
2432 out_service:
2433         ptlrpc_unregister_service(ost->ost_service);
2434         ost->ost_service = NULL;
2435 out_lprocfs:
2436         lprocfs_obd_cleanup(obd);
2437         RETURN(rc);
2438 }
2439
2440 static int ost_cleanup(struct obd_device *obd)
2441 {
2442         struct ost_obd *ost = &obd->u.ost;
2443         int err = 0;
2444         ENTRY;
2445
2446         ping_evictor_stop();
2447
2448         cfs_spin_lock_bh(&obd->obd_processing_task_lock);
2449         if (obd->obd_recovering) {
2450                 target_cancel_recovery_timer(obd);
2451                 obd->obd_recovering = 0;
2452         }
2453         cfs_spin_unlock_bh(&obd->obd_processing_task_lock);
2454
2455         cfs_down(&ost->ost_health_sem);
2456         ptlrpc_unregister_service(ost->ost_service);
2457         ptlrpc_unregister_service(ost->ost_create_service);
2458         ptlrpc_unregister_service(ost->ost_io_service);
2459         ost->ost_service = NULL;
2460         ost->ost_create_service = NULL;
2461         cfs_up(&ost->ost_health_sem);
2462
2463         lprocfs_obd_cleanup(obd);
2464
2465         RETURN(err);
2466 }
2467
2468 static int ost_health_check(struct obd_device *obd)
2469 {
2470         struct ost_obd *ost = &obd->u.ost;
2471         int rc = 0;
2472
2473         cfs_down(&ost->ost_health_sem);
2474         rc |= ptlrpc_service_health_check(ost->ost_service);
2475         rc |= ptlrpc_service_health_check(ost->ost_create_service);
2476         rc |= ptlrpc_service_health_check(ost->ost_io_service);
2477         cfs_up(&ost->ost_health_sem);
2478
2479         /*
2480          * health_check to return 0 on healthy
2481          * and 1 on unhealthy.
2482          */
2483         if( rc != 0)
2484                 rc = 1;
2485
2486         return rc;
2487 }
2488
2489 struct ost_thread_local_cache *ost_tls(struct ptlrpc_request *r)
2490 {
2491         return (struct ost_thread_local_cache *)(r->rq_svc_thread->t_data);
2492 }
2493
2494 /* use obd ops to offer management infrastructure */
2495 static struct obd_ops ost_obd_ops = {
2496         .o_owner        = THIS_MODULE,
2497         .o_setup        = ost_setup,
2498         .o_cleanup      = ost_cleanup,
2499         .o_health_check = ost_health_check,
2500 };
2501
2502
2503 static int __init ost_init(void)
2504 {
2505         struct lprocfs_static_vars lvars;
2506         int rc;
2507         ENTRY;
2508
2509         lprocfs_ost_init_vars(&lvars);
2510         rc = class_register_type(&ost_obd_ops, NULL, lvars.module_vars,
2511                                  LUSTRE_OSS_NAME, NULL);
2512
2513         if (ost_num_threads != 0 && oss_num_threads == 0) {
2514                 LCONSOLE_INFO("ost_num_threads module parameter is deprecated, "
2515                               "use oss_num_threads instead or unset both for "
2516                               "dynamic thread startup\n");
2517                 oss_num_threads = ost_num_threads;
2518         }
2519
2520         RETURN(rc);
2521 }
2522
2523 static void /*__exit*/ ost_exit(void)
2524 {
2525         class_unregister_type(LUSTRE_OSS_NAME);
2526 }
2527
2528 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2529 MODULE_DESCRIPTION("Lustre Object Storage Target (OST) v0.01");
2530 MODULE_LICENSE("GPL");
2531
2532 module_init(ost_init);
2533 module_exit(ost_exit);