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