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