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