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