Whamcloud - gitweb
LU-824 corrupted ldiskfs after md rebuild (bz24264)
[fs/lustre-release.git] / lustre / ptlrpc / layout.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * Copyright (c) 2011 Whamcloud, Inc.
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * lustre/ptlrpc/layout.c
40  *
41  * Lustre Metadata Target (mdt) request handler
42  *
43  * Author: Nikita Danilov <nikita@clusterfs.com>
44  */
45 /*
46  * This file contains the "capsule/pill" abstraction layered above PTLRPC.
47  *
48  * Every struct ptlrpc_request contains a "pill", which points to a description
49  * of the format that the request conforms to.
50  */
51
52 #if !defined(__REQ_LAYOUT_USER__)
53
54 #ifndef EXPORT_SYMTAB
55 # define EXPORT_SYMTAB
56 #endif
57 #define DEBUG_SUBSYSTEM S_RPC
58
59 #ifdef __KERNEL__
60 #include <linux/module.h>
61 #else
62 # include <liblustre.h>
63 #endif
64
65 /* LUSTRE_VERSION_CODE */
66 #include <lustre_ver.h>
67
68 #include <obd_support.h>
69 /* lustre_swab_mdt_body */
70 #include <lustre/lustre_idl.h>
71 /* obd2cli_tgt() (required by DEBUG_REQ()) */
72 #include <obd.h>
73
74 /* __REQ_LAYOUT_USER__ */
75 #endif
76 /* struct ptlrpc_request, lustre_msg* */
77 #include <lustre_req_layout.h>
78 #include <lustre_acl.h>
79 #include <lustre_debug.h>
80
81 /*
82  * RQFs (see below) refer to two struct req_msg_field arrays describing the
83  * client request and server reply, respectively.
84  */
85 /* empty set of fields... for suitable definition of emptiness. */
86 static const struct req_msg_field *empty[] = {
87         &RMF_PTLRPC_BODY
88 };
89
90 static const struct req_msg_field *mgs_target_info_only[] = {
91         &RMF_PTLRPC_BODY,
92         &RMF_MGS_TARGET_INFO
93 };
94
95 static const struct req_msg_field *mgs_set_info[] = {
96         &RMF_PTLRPC_BODY,
97         &RMF_MGS_SEND_PARAM
98 };
99
100 static const struct req_msg_field *mgs_config_read_client[] = {
101         &RMF_PTLRPC_BODY,
102         &RMF_MGS_CONFIG_BODY
103 };
104
105 static const struct req_msg_field *mgs_config_read_server[] = {
106         &RMF_PTLRPC_BODY,
107         &RMF_MGS_CONFIG_RES
108 };
109
110 static const struct req_msg_field *log_cancel_client[] = {
111         &RMF_PTLRPC_BODY,
112         &RMF_LOGCOOKIES
113 };
114
115 static const struct req_msg_field *mdt_body_only[] = {
116         &RMF_PTLRPC_BODY,
117         &RMF_MDT_BODY
118 };
119
120 static const struct req_msg_field *mdt_body_capa[] = {
121         &RMF_PTLRPC_BODY,
122         &RMF_MDT_BODY,
123         &RMF_CAPA1
124 };
125
126 static const struct req_msg_field *quotactl_only[] = {
127         &RMF_PTLRPC_BODY,
128         &RMF_OBD_QUOTACTL
129 };
130 static const struct req_msg_field *quota_adjust_qunit_only[] = {
131         &RMF_PTLRPC_BODY,
132         &RMF_QUOTA_ADJUST_QUNIT
133 };
134
135 static const struct req_msg_field *qunit_data_only[] = {
136         &RMF_PTLRPC_BODY,
137         &RMF_QUNIT_DATA
138 };
139
140 static const struct req_msg_field *mdt_close_client[] = {
141         &RMF_PTLRPC_BODY,
142         &RMF_MDT_EPOCH,
143         &RMF_REC_REINT,
144         &RMF_CAPA1
145 };
146
147 static const struct req_msg_field *obd_statfs_server[] = {
148         &RMF_PTLRPC_BODY,
149         &RMF_OBD_STATFS
150 };
151
152 static const struct req_msg_field *seq_query_client[] = {
153         &RMF_PTLRPC_BODY,
154         &RMF_SEQ_OPC,
155         &RMF_SEQ_RANGE
156 };
157
158 static const struct req_msg_field *seq_query_server[] = {
159         &RMF_PTLRPC_BODY,
160         &RMF_SEQ_RANGE
161 };
162
163 static const struct req_msg_field *fld_query_client[] = {
164         &RMF_PTLRPC_BODY,
165         &RMF_FLD_OPC,
166         &RMF_FLD_MDFLD
167 };
168
169 static const struct req_msg_field *fld_query_server[] = {
170         &RMF_PTLRPC_BODY,
171         &RMF_FLD_MDFLD
172 };
173
174 static const struct req_msg_field *mds_getattr_name_client[] = {
175         &RMF_PTLRPC_BODY,
176         &RMF_MDT_BODY,
177         &RMF_CAPA1,
178         &RMF_NAME
179 };
180
181 static const struct req_msg_field *mds_reint_client[] = {
182         &RMF_PTLRPC_BODY,
183         &RMF_REC_REINT
184 };
185
186 static const struct req_msg_field *mds_reint_create_client[] = {
187         &RMF_PTLRPC_BODY,
188         &RMF_REC_REINT,
189         &RMF_CAPA1,
190         &RMF_NAME
191 };
192
193 static const struct req_msg_field *mds_reint_create_slave_client[] = {
194         &RMF_PTLRPC_BODY,
195         &RMF_REC_REINT,
196         &RMF_CAPA1,
197         &RMF_NAME,
198         &RMF_EADATA,
199         &RMF_DLM_REQ
200 };
201
202 static const struct req_msg_field *mds_reint_create_rmt_acl_client[] = {
203         &RMF_PTLRPC_BODY,
204         &RMF_REC_REINT,
205         &RMF_CAPA1,
206         &RMF_NAME,
207         &RMF_EADATA,
208         &RMF_DLM_REQ
209 };
210
211 static const struct req_msg_field *mds_reint_create_sym_client[] = {
212         &RMF_PTLRPC_BODY,
213         &RMF_REC_REINT,
214         &RMF_CAPA1,
215         &RMF_NAME,
216         &RMF_SYMTGT,
217         &RMF_DLM_REQ
218 };
219
220 static const struct req_msg_field *mds_reint_open_client[] = {
221         &RMF_PTLRPC_BODY,
222         &RMF_REC_REINT,
223         &RMF_CAPA1,
224         &RMF_CAPA2,
225         &RMF_NAME,
226         &RMF_EADATA
227 };
228
229 static const struct req_msg_field *mds_reint_open_server[] = {
230         &RMF_PTLRPC_BODY,
231         &RMF_MDT_BODY,
232         &RMF_MDT_MD,
233         &RMF_ACL,
234         &RMF_CAPA1,
235         &RMF_CAPA2
236 };
237
238 static const struct req_msg_field *mds_reint_unlink_client[] = {
239         &RMF_PTLRPC_BODY,
240         &RMF_REC_REINT,
241         &RMF_CAPA1,
242         &RMF_NAME,
243         &RMF_DLM_REQ
244 };
245
246 static const struct req_msg_field *mds_reint_link_client[] = {
247         &RMF_PTLRPC_BODY,
248         &RMF_REC_REINT,
249         &RMF_CAPA1,
250         &RMF_CAPA2,
251         &RMF_NAME,
252         &RMF_DLM_REQ
253 };
254
255 static const struct req_msg_field *mds_reint_rename_client[] = {
256         &RMF_PTLRPC_BODY,
257         &RMF_REC_REINT,
258         &RMF_CAPA1,
259         &RMF_CAPA2,
260         &RMF_NAME,
261         &RMF_SYMTGT,
262         &RMF_DLM_REQ
263 };
264
265 static const struct req_msg_field *mds_last_unlink_server[] = {
266         &RMF_PTLRPC_BODY,
267         &RMF_MDT_BODY,
268         &RMF_MDT_MD,
269         &RMF_LOGCOOKIES,
270         &RMF_CAPA1,
271         &RMF_CAPA2
272 };
273
274 static const struct req_msg_field *mds_reint_setattr_client[] = {
275         &RMF_PTLRPC_BODY,
276         &RMF_REC_REINT,
277         &RMF_CAPA1,
278         &RMF_MDT_EPOCH,
279         &RMF_EADATA,
280         &RMF_LOGCOOKIES,
281         &RMF_DLM_REQ
282 };
283
284 static const struct req_msg_field *mds_reint_setxattr_client[] = {
285         &RMF_PTLRPC_BODY,
286         &RMF_REC_REINT,
287         &RMF_CAPA1,
288         &RMF_NAME,
289         &RMF_EADATA
290 };
291
292 static const struct req_msg_field *obd_connect_client[] = {
293         &RMF_PTLRPC_BODY,
294         &RMF_TGTUUID,
295         &RMF_CLUUID,
296         &RMF_CONN,
297         &RMF_CONNECT_DATA
298 };
299
300 static const struct req_msg_field *obd_connect_server[] = {
301         &RMF_PTLRPC_BODY,
302         &RMF_CONNECT_DATA
303 };
304
305 static const struct req_msg_field *obd_set_info_client[] = {
306         &RMF_PTLRPC_BODY,
307         &RMF_SETINFO_KEY,
308         &RMF_SETINFO_VAL
309 };
310
311 static const struct req_msg_field *ost_grant_shrink_client[] = {
312         &RMF_PTLRPC_BODY,
313         &RMF_SETINFO_KEY,
314         &RMF_OST_BODY
315 };
316
317 static const struct req_msg_field *mds_getinfo_client[] = {
318         &RMF_PTLRPC_BODY,
319         &RMF_GETINFO_KEY,
320         &RMF_GETINFO_VALLEN
321 };
322
323 static const struct req_msg_field *mds_getinfo_server[] = {
324         &RMF_PTLRPC_BODY,
325         &RMF_GETINFO_VAL,
326 };
327
328 static const struct req_msg_field *ldlm_enqueue_client[] = {
329         &RMF_PTLRPC_BODY,
330         &RMF_DLM_REQ
331 };
332
333 static const struct req_msg_field *ldlm_enqueue_server[] = {
334         &RMF_PTLRPC_BODY,
335         &RMF_DLM_REP
336 };
337
338 static const struct req_msg_field *ldlm_enqueue_lvb_server[] = {
339         &RMF_PTLRPC_BODY,
340         &RMF_DLM_REP,
341         &RMF_DLM_LVB
342 };
343
344 static const struct req_msg_field *ldlm_cp_callback_client[] = {
345         &RMF_PTLRPC_BODY,
346         &RMF_DLM_REQ,
347         &RMF_DLM_LVB
348 };
349
350 static const struct req_msg_field *ldlm_gl_callback_server[] = {
351         &RMF_PTLRPC_BODY,
352         &RMF_DLM_LVB
353 };
354
355 static const struct req_msg_field *ldlm_intent_client[] = {
356         &RMF_PTLRPC_BODY,
357         &RMF_DLM_REQ,
358         &RMF_LDLM_INTENT,
359         &RMF_REC_REINT
360 };
361
362 static const struct req_msg_field *ldlm_intent_server[] = {
363         &RMF_PTLRPC_BODY,
364         &RMF_DLM_REP,
365         &RMF_MDT_BODY,
366         &RMF_MDT_MD,
367         &RMF_ACL
368 };
369
370 static const struct req_msg_field *ldlm_intent_open_server[] = {
371         &RMF_PTLRPC_BODY,
372         &RMF_DLM_REP,
373         &RMF_MDT_BODY,
374         &RMF_MDT_MD,
375         &RMF_ACL,
376         &RMF_CAPA1,
377         &RMF_CAPA2
378 };
379
380 static const struct req_msg_field *ldlm_intent_getattr_client[] = {
381         &RMF_PTLRPC_BODY,
382         &RMF_DLM_REQ,
383         &RMF_LDLM_INTENT,
384         &RMF_MDT_BODY,     /* coincides with mds_getattr_name_client[] */
385         &RMF_CAPA1,
386         &RMF_NAME
387 };
388
389 static const struct req_msg_field *ldlm_intent_getattr_server[] = {
390         &RMF_PTLRPC_BODY,
391         &RMF_DLM_REP,
392         &RMF_MDT_BODY,
393         &RMF_MDT_MD,
394         &RMF_ACL,
395         &RMF_CAPA1
396 };
397
398 static const struct req_msg_field *ldlm_intent_create_client[] = {
399         &RMF_PTLRPC_BODY,
400         &RMF_DLM_REQ,
401         &RMF_LDLM_INTENT,
402         &RMF_REC_REINT,    /* coincides with mds_reint_create_client[] */
403         &RMF_CAPA1,
404         &RMF_NAME,
405         &RMF_EADATA
406 };
407
408 static const struct req_msg_field *ldlm_intent_open_client[] = {
409         &RMF_PTLRPC_BODY,
410         &RMF_DLM_REQ,
411         &RMF_LDLM_INTENT,
412         &RMF_REC_REINT,    /* coincides with mds_reint_open_client[] */
413         &RMF_CAPA1,
414         &RMF_CAPA2,
415         &RMF_NAME,
416         &RMF_EADATA
417 };
418
419 static const struct req_msg_field *ldlm_intent_unlink_client[] = {
420         &RMF_PTLRPC_BODY,
421         &RMF_DLM_REQ,
422         &RMF_LDLM_INTENT,
423         &RMF_REC_REINT,    /* coincides with mds_reint_unlink_client[] */
424         &RMF_CAPA1,
425         &RMF_NAME
426 };
427
428 static const struct req_msg_field *mds_getxattr_client[] = {
429         &RMF_PTLRPC_BODY,
430         &RMF_MDT_BODY,
431         &RMF_CAPA1,
432         &RMF_NAME,
433         &RMF_EADATA
434 };
435
436 static const struct req_msg_field *mds_getxattr_server[] = {
437         &RMF_PTLRPC_BODY,
438         &RMF_MDT_BODY,
439         &RMF_EADATA
440 };
441
442 static const struct req_msg_field *mds_getattr_server[] = {
443         &RMF_PTLRPC_BODY,
444         &RMF_MDT_BODY,
445         &RMF_MDT_MD,
446         &RMF_ACL,
447         &RMF_CAPA1,
448         &RMF_CAPA2
449 };
450
451 static const struct req_msg_field *mds_setattr_server[] = {
452         &RMF_PTLRPC_BODY,
453         &RMF_MDT_BODY,
454         &RMF_MDT_MD,
455         &RMF_ACL,
456         &RMF_CAPA1,
457         &RMF_CAPA2
458 };
459
460 static const struct req_msg_field *llog_catinfo_client[] = {
461         &RMF_PTLRPC_BODY,
462         &RMF_NAME,
463         &RMF_STRING
464 };
465
466 static const struct req_msg_field *llog_catinfo_server[] = {
467         &RMF_PTLRPC_BODY,
468         &RMF_STRING
469 };
470
471 static const struct req_msg_field *llog_origin_handle_create_client[] = {
472         &RMF_PTLRPC_BODY,
473         &RMF_LLOGD_BODY,
474         &RMF_NAME
475 };
476
477 static const struct req_msg_field *llogd_body_only[] = {
478         &RMF_PTLRPC_BODY,
479         &RMF_LLOGD_BODY
480 };
481
482 static const struct req_msg_field *llog_log_hdr_only[] = {
483         &RMF_PTLRPC_BODY,
484         &RMF_LLOG_LOG_HDR
485 };
486
487 static const struct req_msg_field *llogd_conn_body_only[] = {
488         &RMF_PTLRPC_BODY,
489         &RMF_LLOGD_CONN_BODY
490 };
491
492 static const struct req_msg_field *llog_origin_handle_next_block_server[] = {
493         &RMF_PTLRPC_BODY,
494         &RMF_LLOGD_BODY,
495         &RMF_EADATA
496 };
497
498 static const struct req_msg_field *ost_body_only[] = {
499         &RMF_PTLRPC_BODY,
500         &RMF_OST_BODY
501 };
502
503 static const struct req_msg_field *ost_body_capa[] = {
504         &RMF_PTLRPC_BODY,
505         &RMF_OST_BODY,
506         &RMF_CAPA1
507 };
508
509 static const struct req_msg_field *ost_destroy_client[] = {
510         &RMF_PTLRPC_BODY,
511         &RMF_OST_BODY,
512         &RMF_DLM_REQ,
513         &RMF_CAPA1
514 };
515
516
517 static const struct req_msg_field *ost_brw_client[] = {
518         &RMF_PTLRPC_BODY,
519         &RMF_OST_BODY,
520         &RMF_OBD_IOOBJ,
521         &RMF_NIOBUF_REMOTE,
522         &RMF_CAPA1
523 };
524
525 static const struct req_msg_field *ost_brw_read_server[] = {
526         &RMF_PTLRPC_BODY,
527         &RMF_OST_BODY
528 };
529
530 static const struct req_msg_field *ost_brw_write_server[] = {
531         &RMF_PTLRPC_BODY,
532         &RMF_OST_BODY,
533         &RMF_RCS
534 };
535
536 static const struct req_msg_field *ost_get_info_generic_server[] = {
537         &RMF_PTLRPC_BODY,
538         &RMF_GENERIC_DATA,
539 };
540
541 static const struct req_msg_field *ost_get_info_generic_client[] = {
542         &RMF_PTLRPC_BODY,
543         &RMF_SETINFO_KEY
544 };
545
546 static const struct req_msg_field *ost_get_last_id_server[] = {
547         &RMF_PTLRPC_BODY,
548         &RMF_OBD_ID
549 };
550
551 static const struct req_msg_field *ost_get_fiemap_client[] = {
552         &RMF_PTLRPC_BODY,
553         &RMF_FIEMAP_KEY,
554         &RMF_FIEMAP_VAL
555 };
556
557 static const struct req_msg_field *ost_get_fiemap_server[] = {
558         &RMF_PTLRPC_BODY,
559         &RMF_FIEMAP_VAL
560 };
561
562 static struct req_format *req_formats[] = {
563         &RQF_OBD_PING,
564         &RQF_OBD_SET_INFO,
565         &RQF_SEC_CTX,
566         &RQF_MGS_TARGET_REG,
567         &RQF_MGS_SET_INFO,
568         &RQF_MGS_CONFIG_READ,
569         &RQF_SEQ_QUERY,
570         &RQF_FLD_QUERY,
571         &RQF_MDS_CONNECT,
572         &RQF_MDS_DISCONNECT,
573         &RQF_MDS_GET_INFO,
574         &RQF_MDS_GETSTATUS,
575         &RQF_MDS_STATFS,
576         &RQF_MDS_GETATTR,
577         &RQF_MDS_GETATTR_NAME,
578         &RQF_MDS_GETXATTR,
579         &RQF_MDS_SYNC,
580         &RQF_MDS_CLOSE,
581         &RQF_MDS_PIN,
582         &RQF_MDS_UNPIN,
583         &RQF_MDS_READPAGE,
584         &RQF_MDS_WRITEPAGE,
585         &RQF_MDS_IS_SUBDIR,
586         &RQF_MDS_DONE_WRITING,
587         &RQF_MDS_REINT,
588         &RQF_MDS_REINT_CREATE,
589         &RQF_MDS_REINT_CREATE_RMT_ACL,
590         &RQF_MDS_REINT_CREATE_SLAVE,
591         &RQF_MDS_REINT_CREATE_SYM,
592         &RQF_MDS_REINT_OPEN,
593         &RQF_MDS_REINT_UNLINK,
594         &RQF_MDS_REINT_LINK,
595         &RQF_MDS_REINT_RENAME,
596         &RQF_MDS_REINT_SETATTR,
597         &RQF_MDS_REINT_SETXATTR,
598         &RQF_MDS_QUOTACHECK,
599         &RQF_MDS_QUOTACTL,
600         &RQF_MDS_QUOTA_DQACQ,
601         &RQF_QC_CALLBACK,
602         &RQF_OST_CONNECT,
603         &RQF_OST_DISCONNECT,
604         &RQF_OST_QUOTACHECK,
605         &RQF_OST_QUOTACTL,
606         &RQF_OST_QUOTA_ADJUST_QUNIT,
607         &RQF_OST_GETATTR,
608         &RQF_OST_SETATTR,
609         &RQF_OST_CREATE,
610         &RQF_OST_PUNCH,
611         &RQF_OST_SYNC,
612         &RQF_OST_DESTROY,
613         &RQF_OST_BRW_READ,
614         &RQF_OST_BRW_WRITE,
615         &RQF_OST_STATFS,
616         &RQF_OST_SET_GRANT_INFO,
617         &RQF_OST_GET_INFO_GENERIC,
618         &RQF_OST_GET_INFO_LAST_ID,
619         &RQF_OST_GET_INFO_FIEMAP,
620         &RQF_LDLM_ENQUEUE,
621         &RQF_LDLM_ENQUEUE_LVB,
622         &RQF_LDLM_CONVERT,
623         &RQF_LDLM_CANCEL,
624         &RQF_LDLM_CALLBACK,
625         &RQF_LDLM_CP_CALLBACK,
626         &RQF_LDLM_BL_CALLBACK,
627         &RQF_LDLM_GL_CALLBACK,
628         &RQF_LDLM_INTENT,
629         &RQF_LDLM_INTENT_GETATTR,
630         &RQF_LDLM_INTENT_OPEN,
631         &RQF_LDLM_INTENT_CREATE,
632         &RQF_LDLM_INTENT_UNLINK,
633         &RQF_LOG_CANCEL,
634         &RQF_LLOG_CATINFO,
635         &RQF_LLOG_ORIGIN_HANDLE_CREATE,
636         &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
637         &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
638         &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
639         &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
640         &RQF_LLOG_ORIGIN_CONNECT
641 };
642
643 struct req_msg_field {
644         const __u32 rmf_flags;
645         const char  *rmf_name;
646         /**
647          * Field length. (-1) means "variable length".  If the
648          * \a RMF_F_STRUCT_ARRAY flag is set the field is also variable-length,
649          * but the actual size must be a whole multiple of \a rmf_size.
650          */
651         const int   rmf_size;
652         void        (*rmf_swabber)(void *);
653         void        (*rmf_dumper)(void *);
654         int         rmf_offset[ARRAY_SIZE(req_formats)][RCL_NR];
655 };
656
657 enum rmf_flags {
658         /**
659          * The field is a string, must be NUL-terminated.
660          */
661         RMF_F_STRING = 1 << 0,
662         /**
663          * The field's buffer size need not match the declared \a rmf_size.
664          */
665         RMF_F_NO_SIZE_CHECK = 1 << 1,
666         /**
667          * The field's buffer size must be a whole multiple of the declared \a
668          * rmf_size and the \a rmf_swabber function must work on the declared \a
669          * rmf_size worth of bytes.
670          */
671         RMF_F_STRUCT_ARRAY = 1 << 2
672 };
673
674 struct req_capsule;
675
676 /*
677  * Request fields.
678  */
679 #define DEFINE_MSGF(name, flags, size, swabber, dumper) {       \
680         .rmf_name    = (name),                                  \
681         .rmf_flags   = (flags),                                 \
682         .rmf_size    = (size),                                  \
683         .rmf_swabber = (void (*)(void*))(swabber),              \
684         .rmf_dumper  = (void (*)(void*))(dumper)                \
685 }
686
687 struct req_msg_field RMF_GENERIC_DATA =
688         DEFINE_MSGF("generic_data", 0,
689                     -1, NULL, NULL);
690 EXPORT_SYMBOL(RMF_GENERIC_DATA);
691
692 struct req_msg_field RMF_MGS_TARGET_INFO =
693         DEFINE_MSGF("mgs_target_info", 0,
694                     sizeof(struct mgs_target_info),
695                     lustre_swab_mgs_target_info, NULL);
696 EXPORT_SYMBOL(RMF_MGS_TARGET_INFO);
697
698 struct req_msg_field RMF_MGS_SEND_PARAM =
699         DEFINE_MSGF("mgs_send_param", 0,
700                     sizeof(struct mgs_send_param),
701                     NULL, NULL);
702 EXPORT_SYMBOL(RMF_MGS_SEND_PARAM);
703
704 struct req_msg_field RMF_MGS_CONFIG_BODY =
705         DEFINE_MSGF("mgs_config_read request", 0,
706                     sizeof(struct mgs_config_body),
707                     lustre_swab_mgs_config_body, NULL);
708 EXPORT_SYMBOL(RMF_MGS_CONFIG_BODY);
709
710 struct req_msg_field RMF_MGS_CONFIG_RES =
711         DEFINE_MSGF("mgs_config_read reply ", 0,
712                     sizeof(struct mgs_config_res),
713                     lustre_swab_mgs_config_res, NULL);
714 EXPORT_SYMBOL(RMF_MGS_CONFIG_RES);
715
716 struct req_msg_field RMF_U32 =
717         DEFINE_MSGF("generic u32", 0,
718                     sizeof(__u32), lustre_swab_generic_32s, NULL);
719 EXPORT_SYMBOL(RMF_U32);
720
721 struct req_msg_field RMF_SETINFO_VAL =
722         DEFINE_MSGF("setinfo_val", 0, -1, NULL, NULL);
723 EXPORT_SYMBOL(RMF_SETINFO_VAL);
724
725 struct req_msg_field RMF_GETINFO_KEY =
726         DEFINE_MSGF("getinfo_key", 0, -1, NULL, NULL);
727 EXPORT_SYMBOL(RMF_GETINFO_KEY);
728
729 struct req_msg_field RMF_GETINFO_VALLEN =
730         DEFINE_MSGF("getinfo_vallen", 0,
731                     sizeof(__u32), lustre_swab_generic_32s, NULL);
732 EXPORT_SYMBOL(RMF_GETINFO_VALLEN);
733
734 struct req_msg_field RMF_GETINFO_VAL =
735         DEFINE_MSGF("getinfo_val", 0, -1, NULL, NULL);
736 EXPORT_SYMBOL(RMF_GETINFO_VAL);
737
738 struct req_msg_field RMF_SEQ_OPC =
739         DEFINE_MSGF("seq_query_opc", 0,
740                     sizeof(__u32), lustre_swab_generic_32s, NULL);
741 EXPORT_SYMBOL(RMF_SEQ_OPC);
742
743 struct req_msg_field RMF_SEQ_RANGE =
744         DEFINE_MSGF("seq_query_range", 0,
745                     sizeof(struct lu_seq_range),
746                     lustre_swab_lu_seq_range, NULL);
747 EXPORT_SYMBOL(RMF_SEQ_RANGE);
748
749 struct req_msg_field RMF_FLD_OPC =
750         DEFINE_MSGF("fld_query_opc", 0,
751                     sizeof(__u32), lustre_swab_generic_32s, NULL);
752 EXPORT_SYMBOL(RMF_FLD_OPC);
753
754 struct req_msg_field RMF_FLD_MDFLD =
755         DEFINE_MSGF("fld_query_mdfld", 0,
756                     sizeof(struct lu_seq_range),
757                     lustre_swab_lu_seq_range, NULL);
758 EXPORT_SYMBOL(RMF_FLD_MDFLD);
759
760 struct req_msg_field RMF_MDT_BODY =
761         DEFINE_MSGF("mdt_body", 0,
762                     sizeof(struct mdt_body), lustre_swab_mdt_body, NULL);
763 EXPORT_SYMBOL(RMF_MDT_BODY);
764
765 struct req_msg_field RMF_OBD_QUOTACTL =
766         DEFINE_MSGF("obd_quotactl", 0,
767                     sizeof(struct obd_quotactl),
768                     lustre_swab_obd_quotactl, NULL);
769 EXPORT_SYMBOL(RMF_OBD_QUOTACTL);
770
771 struct req_msg_field RMF_QUOTA_ADJUST_QUNIT =
772         DEFINE_MSGF("quota_adjust_qunit", 0,
773                     sizeof(struct quota_adjust_qunit),
774                     lustre_swab_quota_adjust_qunit, NULL);
775 EXPORT_SYMBOL(RMF_QUOTA_ADJUST_QUNIT);
776
777 struct req_msg_field RMF_QUNIT_DATA =
778         DEFINE_MSGF("qunit_data", 0,
779                     sizeof(struct qunit_data), lustre_swab_qdata, NULL);
780 EXPORT_SYMBOL(RMF_QUNIT_DATA);
781
782 struct req_msg_field RMF_MDT_EPOCH =
783         DEFINE_MSGF("mdt_ioepoch", 0,
784                     sizeof(struct mdt_ioepoch), lustre_swab_mdt_ioepoch, NULL);
785 EXPORT_SYMBOL(RMF_MDT_EPOCH);
786
787 struct req_msg_field RMF_PTLRPC_BODY =
788         DEFINE_MSGF("ptlrpc_body", 0,
789                     sizeof(struct ptlrpc_body), lustre_swab_ptlrpc_body, NULL);
790 EXPORT_SYMBOL(RMF_PTLRPC_BODY);
791
792 struct req_msg_field RMF_OBD_STATFS =
793         DEFINE_MSGF("obd_statfs", 0,
794                     sizeof(struct obd_statfs), lustre_swab_obd_statfs, NULL);
795 EXPORT_SYMBOL(RMF_OBD_STATFS);
796
797 struct req_msg_field RMF_SETINFO_KEY =
798         DEFINE_MSGF("setinfo_key", 0, -1, NULL, NULL);
799 EXPORT_SYMBOL(RMF_SETINFO_KEY);
800
801 struct req_msg_field RMF_NAME =
802         DEFINE_MSGF("name", RMF_F_STRING, -1, NULL, NULL);
803 EXPORT_SYMBOL(RMF_NAME);
804
805 struct req_msg_field RMF_SYMTGT =
806         DEFINE_MSGF("symtgt", RMF_F_STRING, -1, NULL, NULL);
807 EXPORT_SYMBOL(RMF_SYMTGT);
808
809 struct req_msg_field RMF_TGTUUID =
810         DEFINE_MSGF("tgtuuid", RMF_F_STRING, sizeof(struct obd_uuid) - 1, NULL,
811         NULL);
812 EXPORT_SYMBOL(RMF_TGTUUID);
813
814 struct req_msg_field RMF_CLUUID =
815         DEFINE_MSGF("cluuid", RMF_F_STRING, sizeof(struct obd_uuid) - 1, NULL,
816         NULL);
817 EXPORT_SYMBOL(RMF_CLUUID);
818
819 struct req_msg_field RMF_STRING =
820         DEFINE_MSGF("string", RMF_F_STRING, -1, NULL, NULL);
821 EXPORT_SYMBOL(RMF_STRING);
822
823 struct req_msg_field RMF_LLOGD_BODY =
824         DEFINE_MSGF("llogd_body", 0,
825                     sizeof(struct llogd_body), lustre_swab_llogd_body, NULL);
826 EXPORT_SYMBOL(RMF_LLOGD_BODY);
827
828 struct req_msg_field RMF_LLOG_LOG_HDR =
829         DEFINE_MSGF("llog_log_hdr", 0,
830                     sizeof(struct llog_log_hdr), lustre_swab_llog_hdr, NULL);
831 EXPORT_SYMBOL(RMF_LLOG_LOG_HDR);
832
833 struct req_msg_field RMF_LLOGD_CONN_BODY =
834         DEFINE_MSGF("llogd_conn_body", 0,
835                     sizeof(struct llogd_conn_body),
836                     lustre_swab_llogd_conn_body, NULL);
837 EXPORT_SYMBOL(RMF_LLOGD_CONN_BODY);
838
839 /*
840  * connection handle received in MDS_CONNECT request.
841  *
842  * No swabbing needed because struct lustre_handle contains only a 64-bit cookie
843  * that the client does not interpret at all.
844  */
845 struct req_msg_field RMF_CONN =
846         DEFINE_MSGF("conn", 0, sizeof(struct lustre_handle), NULL, NULL);
847 EXPORT_SYMBOL(RMF_CONN);
848
849 struct req_msg_field RMF_CONNECT_DATA =
850         DEFINE_MSGF("cdata",
851                     RMF_F_NO_SIZE_CHECK /* we allow extra space for interop */,
852 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 9, 0, 0)
853                     sizeof(struct obd_connect_data),
854 #else
855 /* For interoperability with 1.8 and 2.0 clients/servers.
856  * The RPC verification code allows larger RPC buffers, but not
857  * smaller buffers.  Until we no longer need to keep compatibility
858  * with older servers/clients we can only check that the buffer
859  * size is at least as large as obd_connect_data_v1.  That is not
860  * not in itself harmful, since the chance of just corrupting this
861  * field is low.  See JIRA LU-16 for details. */
862                     sizeof(struct obd_connect_data_v1),
863 #endif
864                     lustre_swab_connect, NULL);
865 EXPORT_SYMBOL(RMF_CONNECT_DATA);
866
867 struct req_msg_field RMF_DLM_REQ =
868         DEFINE_MSGF("dlm_req", RMF_F_NO_SIZE_CHECK /* ldlm_request_bufsize */,
869                     sizeof(struct ldlm_request),
870                     lustre_swab_ldlm_request, NULL);
871 EXPORT_SYMBOL(RMF_DLM_REQ);
872
873 struct req_msg_field RMF_DLM_REP =
874         DEFINE_MSGF("dlm_rep", 0,
875                     sizeof(struct ldlm_reply), lustre_swab_ldlm_reply, NULL);
876 EXPORT_SYMBOL(RMF_DLM_REP);
877
878 struct req_msg_field RMF_LDLM_INTENT =
879         DEFINE_MSGF("ldlm_intent", 0,
880                     sizeof(struct ldlm_intent), lustre_swab_ldlm_intent, NULL);
881 EXPORT_SYMBOL(RMF_LDLM_INTENT);
882
883 struct req_msg_field RMF_DLM_LVB =
884         DEFINE_MSGF("dlm_lvb", 0, sizeof(struct ost_lvb), lustre_swab_ost_lvb,
885         NULL);
886 EXPORT_SYMBOL(RMF_DLM_LVB);
887
888 struct req_msg_field RMF_MDT_MD =
889         DEFINE_MSGF("mdt_md", RMF_F_NO_SIZE_CHECK, MIN_MD_SIZE, NULL, NULL);
890 EXPORT_SYMBOL(RMF_MDT_MD);
891
892 struct req_msg_field RMF_REC_REINT =
893         DEFINE_MSGF("rec_reint", 0, sizeof(struct mdt_rec_reint),
894                     lustre_swab_mdt_rec_reint, NULL);
895 EXPORT_SYMBOL(RMF_REC_REINT);
896
897 /* FIXME: this length should be defined as a macro */
898 struct req_msg_field RMF_EADATA = DEFINE_MSGF("eadata", 0, -1,
899                                                     NULL, NULL);
900 EXPORT_SYMBOL(RMF_EADATA);
901
902 struct req_msg_field RMF_ACL =
903         DEFINE_MSGF("acl", RMF_F_NO_SIZE_CHECK,
904                     LUSTRE_POSIX_ACL_MAX_SIZE, NULL, NULL);
905 EXPORT_SYMBOL(RMF_ACL);
906
907 /* FIXME: this should be made to use RMF_F_STRUCT_ARRAY */
908 struct req_msg_field RMF_LOGCOOKIES =
909         DEFINE_MSGF("logcookies", RMF_F_NO_SIZE_CHECK /* multiple cookies */,
910                     sizeof(struct llog_cookie), NULL, NULL);
911 EXPORT_SYMBOL(RMF_LOGCOOKIES);
912
913 struct req_msg_field RMF_CAPA1 =
914         DEFINE_MSGF("capa", 0, sizeof(struct lustre_capa),
915                     lustre_swab_lustre_capa, NULL);
916 EXPORT_SYMBOL(RMF_CAPA1);
917
918 struct req_msg_field RMF_CAPA2 =
919         DEFINE_MSGF("capa", 0, sizeof(struct lustre_capa),
920                     lustre_swab_lustre_capa, NULL);
921 EXPORT_SYMBOL(RMF_CAPA2);
922
923 /*
924  * OST request field.
925  */
926 struct req_msg_field RMF_OST_BODY =
927         DEFINE_MSGF("ost_body", 0,
928                     sizeof(struct ost_body), lustre_swab_ost_body, dump_ost_body);
929 EXPORT_SYMBOL(RMF_OST_BODY);
930
931 struct req_msg_field RMF_OBD_IOOBJ =
932         DEFINE_MSGF("obd_ioobj", RMF_F_STRUCT_ARRAY,
933                     sizeof(struct obd_ioobj), lustre_swab_obd_ioobj, dump_ioo);
934 EXPORT_SYMBOL(RMF_OBD_IOOBJ);
935
936 struct req_msg_field RMF_NIOBUF_REMOTE =
937         DEFINE_MSGF("niobuf_remote", RMF_F_STRUCT_ARRAY,
938                     sizeof(struct niobuf_remote), lustre_swab_niobuf_remote,
939                     dump_rniobuf);
940 EXPORT_SYMBOL(RMF_NIOBUF_REMOTE);
941
942 struct req_msg_field RMF_RCS =
943         DEFINE_MSGF("niobuf_remote", RMF_F_STRUCT_ARRAY, sizeof(__u32),
944                     lustre_swab_generic_32s, dump_rcs);
945 EXPORT_SYMBOL(RMF_RCS);
946
947 struct req_msg_field RMF_OBD_ID =
948         DEFINE_MSGF("obd_id", 0,
949                     sizeof(obd_id), lustre_swab_ost_last_id, NULL);
950 EXPORT_SYMBOL(RMF_OBD_ID);
951
952 struct req_msg_field RMF_FIEMAP_KEY =
953         DEFINE_MSGF("fiemap", 0, sizeof(struct ll_fiemap_info_key),
954                     lustre_swab_fiemap, NULL);
955 EXPORT_SYMBOL(RMF_FIEMAP_KEY);
956
957 struct req_msg_field RMF_FIEMAP_VAL =
958         DEFINE_MSGF("fiemap", 0, -1, lustre_swab_fiemap, NULL);
959 EXPORT_SYMBOL(RMF_FIEMAP_VAL);
960
961 /*
962  * Request formats.
963  */
964
965 struct req_format {
966         const char *rf_name;
967         int         rf_idx;
968         struct {
969                 int                          nr;
970                 const struct req_msg_field **d;
971         } rf_fields[RCL_NR];
972 };
973
974 #define DEFINE_REQ_FMT(name, client, client_nr, server, server_nr) {    \
975         .rf_name   = name,                                              \
976         .rf_fields = {                                                  \
977                 [RCL_CLIENT] = {                                        \
978                         .nr = client_nr,                                \
979                         .d  = client                                    \
980                 },                                                      \
981                 [RCL_SERVER] = {                                        \
982                         .nr = server_nr,                                \
983                         .d  = server                                    \
984                 }                                                       \
985         }                                                               \
986 }
987
988 #define DEFINE_REQ_FMT0(name, client, server)                                  \
989 DEFINE_REQ_FMT(name, client, ARRAY_SIZE(client), server, ARRAY_SIZE(server))
990
991 struct req_format RQF_OBD_PING =
992         DEFINE_REQ_FMT0("OBD_PING", empty, empty);
993 EXPORT_SYMBOL(RQF_OBD_PING);
994
995 struct req_format RQF_OBD_SET_INFO =
996         DEFINE_REQ_FMT0("OBD_SET_INFO", obd_set_info_client, empty);
997 EXPORT_SYMBOL(RQF_OBD_SET_INFO);
998
999 struct req_format RQF_SEC_CTX =
1000         DEFINE_REQ_FMT0("SEC_CTX", empty, empty);
1001 EXPORT_SYMBOL(RQF_SEC_CTX);
1002
1003 struct req_format RQF_MGS_TARGET_REG =
1004         DEFINE_REQ_FMT0("MGS_TARGET_REG", mgs_target_info_only,
1005                          mgs_target_info_only);
1006 EXPORT_SYMBOL(RQF_MGS_TARGET_REG);
1007
1008 struct req_format RQF_MGS_SET_INFO =
1009         DEFINE_REQ_FMT0("MGS_SET_INFO", mgs_set_info,
1010                          mgs_set_info);
1011 EXPORT_SYMBOL(RQF_MGS_SET_INFO);
1012
1013 struct req_format RQF_MGS_CONFIG_READ =
1014         DEFINE_REQ_FMT0("MGS_CONFIG_READ", mgs_config_read_client,
1015                          mgs_config_read_server);
1016 EXPORT_SYMBOL(RQF_MGS_CONFIG_READ);
1017
1018 struct req_format RQF_SEQ_QUERY =
1019         DEFINE_REQ_FMT0("SEQ_QUERY", seq_query_client, seq_query_server);
1020 EXPORT_SYMBOL(RQF_SEQ_QUERY);
1021
1022 struct req_format RQF_FLD_QUERY =
1023         DEFINE_REQ_FMT0("FLD_QUERY", fld_query_client, fld_query_server);
1024 EXPORT_SYMBOL(RQF_FLD_QUERY);
1025
1026 struct req_format RQF_LOG_CANCEL =
1027         DEFINE_REQ_FMT0("OBD_LOG_CANCEL", log_cancel_client, empty);
1028 EXPORT_SYMBOL(RQF_LOG_CANCEL);
1029
1030 struct req_format RQF_MDS_QUOTACHECK =
1031         DEFINE_REQ_FMT0("MDS_QUOTACHECK", quotactl_only, empty);
1032 EXPORT_SYMBOL(RQF_MDS_QUOTACHECK);
1033
1034 struct req_format RQF_OST_QUOTACHECK =
1035         DEFINE_REQ_FMT0("OST_QUOTACHECK", quotactl_only, empty);
1036 EXPORT_SYMBOL(RQF_OST_QUOTACHECK);
1037
1038 struct req_format RQF_MDS_QUOTACTL =
1039         DEFINE_REQ_FMT0("MDS_QUOTACTL", quotactl_only, quotactl_only);
1040 EXPORT_SYMBOL(RQF_MDS_QUOTACTL);
1041
1042 struct req_format RQF_OST_QUOTACTL =
1043         DEFINE_REQ_FMT0("OST_QUOTACTL", quotactl_only, quotactl_only);
1044 EXPORT_SYMBOL(RQF_OST_QUOTACTL);
1045
1046 struct req_format RQF_OST_QUOTA_ADJUST_QUNIT =
1047         DEFINE_REQ_FMT0("OST_QUOTA_ADJUST_QUNIT", quota_adjust_qunit_only,
1048                         quota_adjust_qunit_only);
1049 EXPORT_SYMBOL(RQF_OST_QUOTA_ADJUST_QUNIT);
1050
1051 struct req_format RQF_QC_CALLBACK =
1052         DEFINE_REQ_FMT0("QC_CALLBACK", quotactl_only, empty);
1053 EXPORT_SYMBOL(RQF_QC_CALLBACK);
1054
1055 struct req_format RQF_MDS_QUOTA_DQACQ =
1056         DEFINE_REQ_FMT0("MDS_QUOTA_DQACQ", qunit_data_only, qunit_data_only);
1057 EXPORT_SYMBOL(RQF_MDS_QUOTA_DQACQ);
1058
1059 struct req_format RQF_MDS_GETSTATUS =
1060         DEFINE_REQ_FMT0("MDS_GETSTATUS", mdt_body_only, mdt_body_capa);
1061 EXPORT_SYMBOL(RQF_MDS_GETSTATUS);
1062
1063 struct req_format RQF_MDS_STATFS =
1064         DEFINE_REQ_FMT0("MDS_STATFS", empty, obd_statfs_server);
1065 EXPORT_SYMBOL(RQF_MDS_STATFS);
1066
1067 struct req_format RQF_MDS_SYNC =
1068         DEFINE_REQ_FMT0("MDS_SYNC", mdt_body_capa, mdt_body_only);
1069 EXPORT_SYMBOL(RQF_MDS_SYNC);
1070
1071 struct req_format RQF_MDS_GETATTR =
1072         DEFINE_REQ_FMT0("MDS_GETATTR", mdt_body_capa, mds_getattr_server);
1073 EXPORT_SYMBOL(RQF_MDS_GETATTR);
1074
1075 struct req_format RQF_MDS_GETXATTR =
1076         DEFINE_REQ_FMT0("MDS_GETXATTR",
1077                         mds_getxattr_client, mds_getxattr_server);
1078 EXPORT_SYMBOL(RQF_MDS_GETXATTR);
1079
1080 struct req_format RQF_MDS_GETATTR_NAME =
1081         DEFINE_REQ_FMT0("MDS_GETATTR_NAME",
1082                         mds_getattr_name_client, mds_getattr_server);
1083 EXPORT_SYMBOL(RQF_MDS_GETATTR_NAME);
1084
1085 struct req_format RQF_MDS_REINT =
1086         DEFINE_REQ_FMT0("MDS_REINT", mds_reint_client, mdt_body_only);
1087 EXPORT_SYMBOL(RQF_MDS_REINT);
1088
1089 struct req_format RQF_MDS_REINT_CREATE =
1090         DEFINE_REQ_FMT0("MDS_REINT_CREATE",
1091                         mds_reint_create_client, mdt_body_capa);
1092 EXPORT_SYMBOL(RQF_MDS_REINT_CREATE);
1093
1094 struct req_format RQF_MDS_REINT_CREATE_RMT_ACL =
1095         DEFINE_REQ_FMT0("MDS_REINT_CREATE_RMT_ACL",
1096                         mds_reint_create_rmt_acl_client, mdt_body_capa);
1097 EXPORT_SYMBOL(RQF_MDS_REINT_CREATE_RMT_ACL);
1098
1099 struct req_format RQF_MDS_REINT_CREATE_SLAVE =
1100         DEFINE_REQ_FMT0("MDS_REINT_CREATE_EA",
1101                         mds_reint_create_slave_client, mdt_body_capa);
1102 EXPORT_SYMBOL(RQF_MDS_REINT_CREATE_SLAVE);
1103
1104 struct req_format RQF_MDS_REINT_CREATE_SYM =
1105         DEFINE_REQ_FMT0("MDS_REINT_CREATE_SYM",
1106                         mds_reint_create_sym_client, mdt_body_capa);
1107 EXPORT_SYMBOL(RQF_MDS_REINT_CREATE_SYM);
1108
1109 struct req_format RQF_MDS_REINT_OPEN =
1110         DEFINE_REQ_FMT0("MDS_REINT_OPEN",
1111                         mds_reint_open_client, mds_reint_open_server);
1112 EXPORT_SYMBOL(RQF_MDS_REINT_OPEN);
1113
1114 struct req_format RQF_MDS_REINT_UNLINK =
1115         DEFINE_REQ_FMT0("MDS_REINT_UNLINK", mds_reint_unlink_client,
1116                         mds_last_unlink_server);
1117 EXPORT_SYMBOL(RQF_MDS_REINT_UNLINK);
1118
1119 struct req_format RQF_MDS_REINT_LINK =
1120         DEFINE_REQ_FMT0("MDS_REINT_LINK",
1121                         mds_reint_link_client, mdt_body_only);
1122 EXPORT_SYMBOL(RQF_MDS_REINT_LINK);
1123
1124 struct req_format RQF_MDS_REINT_RENAME =
1125         DEFINE_REQ_FMT0("MDS_REINT_RENAME", mds_reint_rename_client,
1126                         mds_last_unlink_server);
1127 EXPORT_SYMBOL(RQF_MDS_REINT_RENAME);
1128
1129 struct req_format RQF_MDS_REINT_SETATTR =
1130         DEFINE_REQ_FMT0("MDS_REINT_SETATTR",
1131                         mds_reint_setattr_client, mds_setattr_server);
1132 EXPORT_SYMBOL(RQF_MDS_REINT_SETATTR);
1133
1134 struct req_format RQF_MDS_REINT_SETXATTR =
1135         DEFINE_REQ_FMT0("MDS_REINT_SETXATTR",
1136                         mds_reint_setxattr_client, mdt_body_only);
1137 EXPORT_SYMBOL(RQF_MDS_REINT_SETXATTR);
1138
1139 struct req_format RQF_MDS_CONNECT =
1140         DEFINE_REQ_FMT0("MDS_CONNECT",
1141                         obd_connect_client, obd_connect_server);
1142 EXPORT_SYMBOL(RQF_MDS_CONNECT);
1143
1144 struct req_format RQF_MDS_DISCONNECT =
1145         DEFINE_REQ_FMT0("MDS_DISCONNECT", empty, empty);
1146 EXPORT_SYMBOL(RQF_MDS_DISCONNECT);
1147
1148 struct req_format RQF_MDS_GET_INFO =
1149         DEFINE_REQ_FMT0("MDS_GET_INFO", mds_getinfo_client,
1150                         mds_getinfo_server);
1151 EXPORT_SYMBOL(RQF_MDS_GET_INFO);
1152
1153 struct req_format RQF_LDLM_ENQUEUE =
1154         DEFINE_REQ_FMT0("LDLM_ENQUEUE",
1155                         ldlm_enqueue_client, ldlm_enqueue_lvb_server);
1156 EXPORT_SYMBOL(RQF_LDLM_ENQUEUE);
1157
1158 struct req_format RQF_LDLM_ENQUEUE_LVB =
1159         DEFINE_REQ_FMT0("LDLM_ENQUEUE_LVB",
1160                         ldlm_enqueue_client, ldlm_enqueue_lvb_server);
1161 EXPORT_SYMBOL(RQF_LDLM_ENQUEUE_LVB);
1162
1163 struct req_format RQF_LDLM_CONVERT =
1164         DEFINE_REQ_FMT0("LDLM_CONVERT",
1165                         ldlm_enqueue_client, ldlm_enqueue_server);
1166 EXPORT_SYMBOL(RQF_LDLM_CONVERT);
1167
1168 struct req_format RQF_LDLM_CANCEL =
1169         DEFINE_REQ_FMT0("LDLM_CANCEL", ldlm_enqueue_client, empty);
1170 EXPORT_SYMBOL(RQF_LDLM_CANCEL);
1171
1172 struct req_format RQF_LDLM_CALLBACK =
1173         DEFINE_REQ_FMT0("LDLM_CALLBACK", ldlm_enqueue_client, empty);
1174 EXPORT_SYMBOL(RQF_LDLM_CALLBACK);
1175
1176 struct req_format RQF_LDLM_CP_CALLBACK =
1177         DEFINE_REQ_FMT0("LDLM_CP_CALLBACK", ldlm_cp_callback_client, empty);
1178 EXPORT_SYMBOL(RQF_LDLM_CP_CALLBACK);
1179
1180 struct req_format RQF_LDLM_BL_CALLBACK =
1181         DEFINE_REQ_FMT0("LDLM_BL_CALLBACK", ldlm_enqueue_client, empty);
1182 EXPORT_SYMBOL(RQF_LDLM_BL_CALLBACK);
1183
1184 struct req_format RQF_LDLM_GL_CALLBACK =
1185         DEFINE_REQ_FMT0("LDLM_GL_CALLBACK", ldlm_enqueue_client,
1186                         ldlm_gl_callback_server);
1187 EXPORT_SYMBOL(RQF_LDLM_GL_CALLBACK);
1188
1189 struct req_format RQF_LDLM_INTENT =
1190         DEFINE_REQ_FMT0("LDLM_INTENT",
1191                         ldlm_intent_client, ldlm_intent_server);
1192 EXPORT_SYMBOL(RQF_LDLM_INTENT);
1193
1194 struct req_format RQF_LDLM_INTENT_GETATTR =
1195         DEFINE_REQ_FMT0("LDLM_INTENT_GETATTR",
1196                         ldlm_intent_getattr_client, ldlm_intent_getattr_server);
1197 EXPORT_SYMBOL(RQF_LDLM_INTENT_GETATTR);
1198
1199 struct req_format RQF_LDLM_INTENT_OPEN =
1200         DEFINE_REQ_FMT0("LDLM_INTENT_OPEN",
1201                         ldlm_intent_open_client, ldlm_intent_open_server);
1202 EXPORT_SYMBOL(RQF_LDLM_INTENT_OPEN);
1203
1204 struct req_format RQF_LDLM_INTENT_CREATE =
1205         DEFINE_REQ_FMT0("LDLM_INTENT_CREATE",
1206                         ldlm_intent_create_client, ldlm_intent_getattr_server);
1207 EXPORT_SYMBOL(RQF_LDLM_INTENT_CREATE);
1208
1209 struct req_format RQF_LDLM_INTENT_UNLINK =
1210         DEFINE_REQ_FMT0("LDLM_INTENT_UNLINK",
1211                         ldlm_intent_unlink_client, ldlm_intent_server);
1212 EXPORT_SYMBOL(RQF_LDLM_INTENT_UNLINK);
1213
1214 struct req_format RQF_MDS_CLOSE =
1215         DEFINE_REQ_FMT0("MDS_CLOSE",
1216                         mdt_close_client, mds_last_unlink_server);
1217 EXPORT_SYMBOL(RQF_MDS_CLOSE);
1218
1219 struct req_format RQF_MDS_PIN =
1220         DEFINE_REQ_FMT0("MDS_PIN",
1221                         mdt_body_capa, mdt_body_only);
1222 EXPORT_SYMBOL(RQF_MDS_PIN);
1223
1224 struct req_format RQF_MDS_UNPIN =
1225         DEFINE_REQ_FMT0("MDS_UNPIN", mdt_body_only, empty);
1226 EXPORT_SYMBOL(RQF_MDS_UNPIN);
1227
1228 struct req_format RQF_MDS_DONE_WRITING =
1229         DEFINE_REQ_FMT0("MDS_DONE_WRITING",
1230                         mdt_close_client, mdt_body_only);
1231 EXPORT_SYMBOL(RQF_MDS_DONE_WRITING);
1232
1233 struct req_format RQF_MDS_READPAGE =
1234         DEFINE_REQ_FMT0("MDS_READPAGE",
1235                         mdt_body_capa, mdt_body_only);
1236 EXPORT_SYMBOL(RQF_MDS_READPAGE);
1237
1238 /* This is for split */
1239 struct req_format RQF_MDS_WRITEPAGE =
1240         DEFINE_REQ_FMT0("MDS_WRITEPAGE",
1241                         mdt_body_capa, mdt_body_only);
1242 EXPORT_SYMBOL(RQF_MDS_WRITEPAGE);
1243
1244 struct req_format RQF_MDS_IS_SUBDIR =
1245         DEFINE_REQ_FMT0("MDS_IS_SUBDIR",
1246                         mdt_body_only, mdt_body_only);
1247 EXPORT_SYMBOL(RQF_MDS_IS_SUBDIR);
1248
1249 struct req_format RQF_LLOG_CATINFO =
1250         DEFINE_REQ_FMT0("LLOG_CATINFO",
1251                         llog_catinfo_client, llog_catinfo_server);
1252 EXPORT_SYMBOL(RQF_LLOG_CATINFO);
1253
1254 struct req_format RQF_LLOG_ORIGIN_HANDLE_CREATE =
1255         DEFINE_REQ_FMT0("LLOG_ORIGIN_HANDLE_CREATE",
1256                         llog_origin_handle_create_client, llogd_body_only);
1257 EXPORT_SYMBOL(RQF_LLOG_ORIGIN_HANDLE_CREATE);
1258
1259 struct req_format RQF_LLOG_ORIGIN_HANDLE_DESTROY =
1260         DEFINE_REQ_FMT0("LLOG_ORIGIN_HANDLE_DESTROY",
1261                         llogd_body_only, llogd_body_only);
1262 EXPORT_SYMBOL(RQF_LLOG_ORIGIN_HANDLE_DESTROY);
1263
1264 struct req_format RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK =
1265         DEFINE_REQ_FMT0("LLOG_ORIGIN_HANDLE_NEXT_BLOCK",
1266                         llogd_body_only, llog_origin_handle_next_block_server);
1267 EXPORT_SYMBOL(RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1268
1269 struct req_format RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK =
1270         DEFINE_REQ_FMT0("LLOG_ORIGIN_HANDLE_PREV_BLOCK",
1271                         llogd_body_only, llog_origin_handle_next_block_server);
1272 EXPORT_SYMBOL(RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
1273
1274 struct req_format RQF_LLOG_ORIGIN_HANDLE_READ_HEADER =
1275         DEFINE_REQ_FMT0("LLOG_ORIGIN_HANDLE_READ_HEADER",
1276                         llogd_body_only, llog_log_hdr_only);
1277 EXPORT_SYMBOL(RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1278
1279 struct req_format RQF_LLOG_ORIGIN_CONNECT =
1280         DEFINE_REQ_FMT0("LLOG_ORIGIN_CONNECT", llogd_conn_body_only, empty);
1281 EXPORT_SYMBOL(RQF_LLOG_ORIGIN_CONNECT);
1282
1283 struct req_format RQF_OST_CONNECT =
1284         DEFINE_REQ_FMT0("OST_CONNECT",
1285                         obd_connect_client, obd_connect_server);
1286 EXPORT_SYMBOL(RQF_OST_CONNECT);
1287
1288 struct req_format RQF_OST_DISCONNECT =
1289         DEFINE_REQ_FMT0("OST_DISCONNECT", empty, empty);
1290 EXPORT_SYMBOL(RQF_OST_DISCONNECT);
1291
1292 struct req_format RQF_OST_GETATTR =
1293         DEFINE_REQ_FMT0("OST_GETATTR", ost_body_capa, ost_body_only);
1294 EXPORT_SYMBOL(RQF_OST_GETATTR);
1295
1296 struct req_format RQF_OST_SETATTR =
1297         DEFINE_REQ_FMT0("OST_SETATTR", ost_body_capa, ost_body_only);
1298 EXPORT_SYMBOL(RQF_OST_SETATTR);
1299
1300 struct req_format RQF_OST_CREATE =
1301         DEFINE_REQ_FMT0("OST_CREATE", ost_body_only, ost_body_only);
1302 EXPORT_SYMBOL(RQF_OST_CREATE);
1303
1304 struct req_format RQF_OST_PUNCH =
1305         DEFINE_REQ_FMT0("OST_PUNCH", ost_body_capa, ost_body_only);
1306 EXPORT_SYMBOL(RQF_OST_PUNCH);
1307
1308 struct req_format RQF_OST_SYNC =
1309         DEFINE_REQ_FMT0("OST_SYNC", ost_body_capa, ost_body_only);
1310 EXPORT_SYMBOL(RQF_OST_SYNC);
1311
1312 struct req_format RQF_OST_DESTROY =
1313         DEFINE_REQ_FMT0("OST_DESTROY", ost_destroy_client, ost_body_only);
1314 EXPORT_SYMBOL(RQF_OST_DESTROY);
1315
1316 struct req_format RQF_OST_BRW_READ =
1317         DEFINE_REQ_FMT0("OST_BRW_READ", ost_brw_client, ost_brw_read_server);
1318 EXPORT_SYMBOL(RQF_OST_BRW_READ);
1319
1320 struct req_format RQF_OST_BRW_WRITE =
1321         DEFINE_REQ_FMT0("OST_BRW_WRITE", ost_brw_client, ost_brw_write_server);
1322 EXPORT_SYMBOL(RQF_OST_BRW_WRITE);
1323
1324 struct req_format RQF_OST_STATFS =
1325         DEFINE_REQ_FMT0("OST_STATFS", empty, obd_statfs_server);
1326 EXPORT_SYMBOL(RQF_OST_STATFS);
1327
1328 struct req_format RQF_OST_SET_GRANT_INFO =
1329         DEFINE_REQ_FMT0("OST_SET_GRANT_INFO", ost_grant_shrink_client,
1330                          ost_body_only);
1331 EXPORT_SYMBOL(RQF_OST_SET_GRANT_INFO);
1332
1333 struct req_format RQF_OST_GET_INFO_GENERIC =
1334         DEFINE_REQ_FMT0("OST_GET_INFO", ost_get_info_generic_client,
1335                                         ost_get_info_generic_server);
1336 EXPORT_SYMBOL(RQF_OST_GET_INFO_GENERIC);
1337
1338 struct req_format RQF_OST_GET_INFO_LAST_ID =
1339         DEFINE_REQ_FMT0("OST_GET_INFO_LAST_ID", ost_get_info_generic_client,
1340                                                 ost_get_last_id_server);
1341 EXPORT_SYMBOL(RQF_OST_GET_INFO_LAST_ID);
1342
1343 struct req_format RQF_OST_GET_INFO_FIEMAP =
1344         DEFINE_REQ_FMT0("OST_GET_INFO_FIEMAP", ost_get_fiemap_client,
1345                                                ost_get_fiemap_server);
1346 EXPORT_SYMBOL(RQF_OST_GET_INFO_FIEMAP);
1347
1348
1349 #if !defined(__REQ_LAYOUT_USER__)
1350
1351 /* Convenience macro */
1352 #define FMT_FIELD(fmt, i, j) (fmt)->rf_fields[(i)].d[(j)]
1353
1354 /**
1355  * Initializes the capsule abstraction by computing and setting the \a rf_idx
1356  * field of RQFs and the \a rmf_offset field of RMFs.
1357  */
1358 int req_layout_init(void)
1359 {
1360         int i;
1361         int j;
1362         int k;
1363         struct req_format *rf = NULL;
1364
1365         for (i = 0; i < ARRAY_SIZE(req_formats); ++i) {
1366                 rf = req_formats[i];
1367                 rf->rf_idx = i;
1368                 for (j = 0; j < RCL_NR; ++j) {
1369                         LASSERT(rf->rf_fields[j].nr <= REQ_MAX_FIELD_NR);
1370                         for (k = 0; k < rf->rf_fields[j].nr; ++k) {
1371                                 struct req_msg_field *field;
1372
1373                                 field = (typeof(field))rf->rf_fields[j].d[k];
1374                                 LASSERT(!(field->rmf_flags & RMF_F_STRUCT_ARRAY)
1375                                         || field->rmf_size > 0);
1376                                 LASSERT(field->rmf_offset[i][j] == 0);
1377                                 /*
1378                                  * k + 1 to detect unused format/field
1379                                  * combinations.
1380                                  */
1381                                 field->rmf_offset[i][j] = k + 1;
1382                         }
1383                 }
1384         }
1385         return 0;
1386 }
1387 EXPORT_SYMBOL(req_layout_init);
1388
1389 void req_layout_fini(void)
1390 {
1391 }
1392 EXPORT_SYMBOL(req_layout_fini);
1393
1394 /**
1395  * Initializes the expected sizes of each RMF in a \a pill (\a rc_area) to -1.
1396  *
1397  * Actual/expected field sizes are set elsewhere in functions in this file:
1398  * req_capsule_init(), req_capsule_server_pack(), req_capsule_set_size() and
1399  * req_capsule_msg_size().  The \a rc_area information is used by.
1400  * ptlrpc_request_set_replen().
1401  */
1402 void req_capsule_init_area(struct req_capsule *pill)
1403 {
1404         int i;
1405
1406         for (i = 0; i < ARRAY_SIZE(pill->rc_area[RCL_CLIENT]); i++) {
1407                 pill->rc_area[RCL_CLIENT][i] = -1;
1408                 pill->rc_area[RCL_SERVER][i] = -1;
1409         }
1410 }
1411 EXPORT_SYMBOL(req_capsule_init_area);
1412
1413 /**
1414  * Initialize a pill.
1415  *
1416  * The \a location indicates whether the caller is executing on the client side
1417  * (RCL_CLIENT) or server side (RCL_SERVER)..
1418  */
1419 void req_capsule_init(struct req_capsule *pill,
1420                       struct ptlrpc_request *req,
1421                       enum req_location location)
1422 {
1423         LASSERT(location == RCL_SERVER || location == RCL_CLIENT);
1424
1425         /*
1426          * Today all capsules are embedded in ptlrpc_request structs,
1427          * but just in case that ever isn't the case, we don't reach
1428          * into req unless req != NULL and pill is the one embedded in
1429          * the req.
1430          *
1431          * The req->rq_pill_init flag makes it safe to initialize a pill
1432          * twice, which might happen in the OST paths as a result of the
1433          * high-priority RPC queue getting peeked at before ost_handle()
1434          * handles an OST RPC.
1435          */
1436         if (req != NULL && pill == &req->rq_pill && req->rq_pill_init)
1437                 return;
1438
1439         memset(pill, 0, sizeof *pill);
1440         pill->rc_req = req;
1441         pill->rc_loc = location;
1442         req_capsule_init_area(pill);
1443
1444         if (req != NULL && pill == &req->rq_pill)
1445                 req->rq_pill_init = 1;
1446 }
1447 EXPORT_SYMBOL(req_capsule_init);
1448
1449 void req_capsule_fini(struct req_capsule *pill)
1450 {
1451 }
1452 EXPORT_SYMBOL(req_capsule_fini);
1453
1454 static int __req_format_is_sane(const struct req_format *fmt)
1455 {
1456         return
1457                 0 <= fmt->rf_idx && fmt->rf_idx < ARRAY_SIZE(req_formats) &&
1458                 req_formats[fmt->rf_idx] == fmt;
1459 }
1460
1461 static struct lustre_msg *__req_msg(const struct req_capsule *pill,
1462                                     enum req_location loc)
1463 {
1464         struct ptlrpc_request *req;
1465
1466         req = pill->rc_req;
1467         return loc == RCL_CLIENT ? req->rq_reqmsg : req->rq_repmsg;
1468 }
1469
1470 /**
1471  * Set the format (\a fmt) of a \a pill; format changes are not allowed here
1472  * (see req_capsule_extend()).
1473  */
1474 void req_capsule_set(struct req_capsule *pill, const struct req_format *fmt)
1475 {
1476         LASSERT(pill->rc_fmt == NULL || pill->rc_fmt == fmt);
1477         LASSERT(__req_format_is_sane(fmt));
1478
1479         pill->rc_fmt = fmt;
1480 }
1481 EXPORT_SYMBOL(req_capsule_set);
1482
1483 /**
1484  * Fills in any parts of the \a rc_area of a \a pill that haven't been filled in
1485  * yet.
1486
1487  * \a rc_area is an array of REQ_MAX_FIELD_NR elements, used to store sizes of
1488  * variable-sized fields.  The field sizes come from the declared \a rmf_size
1489  * field of a \a pill's \a rc_fmt's RMF's.
1490  */
1491 int req_capsule_filled_sizes(struct req_capsule *pill,
1492                            enum req_location loc)
1493 {
1494         const struct req_format *fmt = pill->rc_fmt;
1495         int                      i;
1496
1497         LASSERT(fmt != NULL);
1498
1499         for (i = 0; i < fmt->rf_fields[loc].nr; ++i) {
1500                 if (pill->rc_area[loc][i] == -1) {
1501                         pill->rc_area[loc][i] =
1502                                             fmt->rf_fields[loc].d[i]->rmf_size;
1503                         if (pill->rc_area[loc][i] == -1) {
1504                                 /*
1505                                  * Skip the following fields.
1506                                  *
1507                                  * If this LASSERT() trips then you're missing a
1508                                  * call to req_capsule_set_size().
1509                                  */
1510                                 LASSERT(loc != RCL_SERVER);
1511                                 break;
1512                         }
1513                 }
1514         }
1515         return i;
1516 }
1517 EXPORT_SYMBOL(req_capsule_filled_sizes);
1518
1519 /**
1520  * Capsule equivalent of lustre_pack_request() and lustre_pack_reply().
1521  *
1522  * This function uses the \a pill's \a rc_area as filled in by
1523  * req_capsule_set_size() or req_capsule_filled_sizes() (the latter is called by
1524  * this function).
1525  */
1526 int req_capsule_server_pack(struct req_capsule *pill)
1527 {
1528         const struct req_format *fmt;
1529         int                      count;
1530         int                      rc;
1531
1532         LASSERT(pill->rc_loc == RCL_SERVER);
1533         fmt = pill->rc_fmt;
1534         LASSERT(fmt != NULL);
1535
1536         count = req_capsule_filled_sizes(pill, RCL_SERVER);
1537         rc = lustre_pack_reply(pill->rc_req, count,
1538                                pill->rc_area[RCL_SERVER], NULL);
1539         if (rc != 0) {
1540                 DEBUG_REQ(D_ERROR, pill->rc_req,
1541                        "Cannot pack %d fields in format `%s': ",
1542                        count, fmt->rf_name);
1543         }
1544         return rc;
1545 }
1546 EXPORT_SYMBOL(req_capsule_server_pack);
1547
1548 /**
1549  * Returns the PTLRPC request or reply (\a loc) buffer offset of a \a pill
1550  * corresponding to the given RMF (\a field).
1551  */
1552 static int __req_capsule_offset(const struct req_capsule *pill,
1553                                 const struct req_msg_field *field,
1554                                 enum req_location loc)
1555 {
1556         int offset;
1557
1558         offset = field->rmf_offset[pill->rc_fmt->rf_idx][loc];
1559         LASSERTF(offset > 0, "%s:%s, off=%d, loc=%d\n",
1560                             pill->rc_fmt->rf_name,
1561                             field->rmf_name, offset, loc);
1562         offset --;
1563
1564         LASSERT(0 <= offset && offset < REQ_MAX_FIELD_NR);
1565         return offset;
1566 }
1567
1568 /**
1569  * Helper for __req_capsule_get(); swabs value / array of values and/or dumps
1570  * them if desired.
1571  */
1572 static
1573 void
1574 swabber_dumper_helper(struct req_capsule *pill,
1575                       const struct req_msg_field *field,
1576                       enum req_location loc,
1577                       int offset,
1578                       void *value, int len, int dump, void (*swabber)( void *))
1579 {
1580         void    *p;
1581         int     i;
1582         int     n;
1583         int     do_swab;
1584         int     inout = loc == RCL_CLIENT;
1585
1586         swabber = swabber ?: field->rmf_swabber;
1587
1588         if (ptlrpc_buf_need_swab(pill->rc_req, inout, offset) &&
1589             swabber != NULL && value != NULL)
1590                 do_swab = 1;
1591         else
1592                 do_swab = 0;
1593
1594         if (!(field->rmf_flags & RMF_F_STRUCT_ARRAY)) {
1595                 if (dump && field->rmf_dumper) {
1596                         CDEBUG(D_RPCTRACE, "Dump of %sfield %s follows\n",
1597                                do_swab ? "unswabbed " : "", field->rmf_name);
1598                         field->rmf_dumper(value);
1599                 }
1600                 if (!do_swab)
1601                         return;
1602                 swabber(value);
1603                 ptlrpc_buf_set_swabbed(pill->rc_req, inout, offset);
1604                 if (dump) {
1605                         CDEBUG(D_RPCTRACE, "Dump of swabbed field %s "
1606                                "follows\n", field->rmf_name);
1607                         field->rmf_dumper(value);
1608                 }
1609
1610                 return;
1611         }
1612
1613         /*
1614          * We're swabbing an array; swabber() swabs a single array element, so
1615          * swab every element.
1616          */
1617         LASSERT((len % field->rmf_size) == 0);
1618         for (p = value, i = 0, n = len / field->rmf_size;
1619              i < n;
1620              i++, p += field->rmf_size) {
1621                 if (dump && field->rmf_dumper) {
1622                         CDEBUG(D_RPCTRACE, "Dump of %sarray field %s, "
1623                                "element %d follows\n",
1624                                do_swab ? "unswabbed " : "", field->rmf_name, i);
1625                         field->rmf_dumper(p);
1626                 }
1627                 if (!do_swab)
1628                         continue;
1629                 swabber(p);
1630                 if (dump && field->rmf_dumper) {
1631                         CDEBUG(D_RPCTRACE, "Dump of swabbed array field %s, "
1632                                "element %d follows\n", field->rmf_name, i);
1633                         field->rmf_dumper(value);
1634                 }
1635         }
1636         if (do_swab)
1637                 ptlrpc_buf_set_swabbed(pill->rc_req, inout, offset);
1638 }
1639
1640 /**
1641  * Returns the pointer to a PTLRPC request or reply (\a loc) buffer of a \a pill
1642  * corresponding to the given RMF (\a field).
1643  *
1644  * The buffer will be swabbed using the given \a swabber.  If \a swabber == NULL
1645  * then the \a rmf_swabber from the RMF will be used.  Soon there will be no
1646  * calls to __req_capsule_get() with a non-NULL \a swabber; \a swabber will then
1647  * be removed.  Fields with the \a RMF_F_STRUCT_ARRAY flag set will have each
1648  * element of the array swabbed.
1649  */
1650 static void *__req_capsule_get(struct req_capsule *pill,
1651                                const struct req_msg_field *field,
1652                                enum req_location loc,
1653                                void (*swabber)( void *),
1654                                int dump)
1655 {
1656         const struct req_format *fmt;
1657         struct lustre_msg       *msg;
1658         void                    *value;
1659         int                      len;
1660         int                      offset;
1661
1662         void *(*getter)(struct lustre_msg *m, int n, int minlen);
1663
1664         static const char *rcl_names[RCL_NR] = {
1665                 [RCL_CLIENT] = "client",
1666                 [RCL_SERVER] = "server"
1667         };
1668
1669         LASSERT(pill != NULL);
1670         LASSERT(pill != LP_POISON);
1671         fmt = pill->rc_fmt;
1672         LASSERT(fmt != NULL);
1673         LASSERT(fmt != LP_POISON);
1674         LASSERT(__req_format_is_sane(fmt));
1675
1676         offset = __req_capsule_offset(pill, field, loc);
1677
1678         msg = __req_msg(pill, loc);
1679         LASSERT(msg != NULL);
1680
1681         getter = (field->rmf_flags & RMF_F_STRING) ?
1682                 (typeof(getter))lustre_msg_string : lustre_msg_buf;
1683
1684         if (field->rmf_flags & RMF_F_STRUCT_ARRAY) {
1685                 /*
1686                  * We've already asserted that field->rmf_size > 0 in
1687                  * req_layout_init().
1688                  */
1689                 len = lustre_msg_buflen(msg, offset);
1690                 if ((len % field->rmf_size) != 0) {
1691                         CERROR("%s: array field size mismatch "
1692                                "%d modulo %d != 0 (%d)\n",
1693                                field->rmf_name, len, field->rmf_size, loc);
1694                         return NULL;
1695                 }
1696         } else if (pill->rc_area[loc][offset] != -1) {
1697                 len = pill->rc_area[loc][offset];
1698         } else {
1699                 len = max(field->rmf_size, 0);
1700         }
1701         value = getter(msg, offset, len);
1702
1703         if (value == NULL) {
1704                 DEBUG_REQ(D_ERROR, pill->rc_req,
1705                           "Wrong buffer for field `%s' (%d of %d) "
1706                           "in format `%s': %d vs. %d (%s)\n",
1707                           field->rmf_name, offset, lustre_msg_bufcount(msg),
1708                           fmt->rf_name, lustre_msg_buflen(msg, offset), len,
1709                           rcl_names[loc]);
1710         } else {
1711                 swabber_dumper_helper(pill, field, loc, offset, value, len,
1712                                       dump, swabber);
1713         }
1714
1715         return value;
1716 }
1717
1718 /**
1719  * Dump a request and/or reply
1720  */
1721 void __req_capsule_dump(struct req_capsule *pill, enum req_location loc)
1722 {
1723         const struct    req_format *fmt;
1724         const struct    req_msg_field *field;
1725         int             len;
1726         int             i;
1727
1728         fmt = pill->rc_fmt;
1729
1730         DEBUG_REQ(D_RPCTRACE, pill->rc_req, "BEGIN REQ CAPSULE DUMP\n");
1731         for (i = 0; i < fmt->rf_fields[loc].nr; ++i) {
1732                 field = FMT_FIELD(fmt, loc, i);
1733                 if (field->rmf_dumper == NULL) {
1734                         /*
1735                          * FIXME Add a default hex dumper for fields that don't
1736                          * have a specific dumper
1737                          */
1738                         len = req_capsule_get_size(pill, field, loc);
1739                         CDEBUG(D_RPCTRACE, "Field %s has no dumper function;"
1740                                "field size is %d\n", field->rmf_name, len);
1741                 } else {
1742                         /* It's the dumping side-effect that we're interested in */
1743                         (void) __req_capsule_get(pill, field, loc, NULL, 1);
1744                 }
1745         }
1746         CDEBUG(D_RPCTRACE, "END REQ CAPSULE DUMP\n");
1747 }
1748
1749 /**
1750  * Dump a request.
1751  */
1752 void req_capsule_client_dump(struct req_capsule *pill)
1753 {
1754         __req_capsule_dump(pill, RCL_CLIENT);
1755 }
1756 EXPORT_SYMBOL(req_capsule_client_dump);
1757
1758 /**
1759  * Dump a reply
1760  */
1761 void req_capsule_server_dump(struct req_capsule *pill)
1762 {
1763         __req_capsule_dump(pill, RCL_SERVER);
1764 }
1765 EXPORT_SYMBOL(req_capsule_server_dump);
1766
1767 /**
1768  * Trivial wrapper around __req_capsule_get(), that returns the PTLRPC request
1769  * buffer corresponding to the given RMF (\a field) of a \a pill.
1770  */
1771 void *req_capsule_client_get(struct req_capsule *pill,
1772                              const struct req_msg_field *field)
1773 {
1774         return __req_capsule_get(pill, field, RCL_CLIENT, NULL, 0);
1775 }
1776 EXPORT_SYMBOL(req_capsule_client_get);
1777
1778 /**
1779  * Same as req_capsule_client_get(), but with a \a swabber argument.
1780  *
1781  * Currently unused; will be removed when req_capsule_server_swab_get() is
1782  * unused too.
1783  */
1784 void *req_capsule_client_swab_get(struct req_capsule *pill,
1785                                   const struct req_msg_field *field,
1786                                   void (*swabber)(void* ))
1787 {
1788         return __req_capsule_get(pill, field, RCL_CLIENT, swabber, 0);
1789 }
1790 EXPORT_SYMBOL(req_capsule_client_swab_get);
1791
1792 /**
1793  * Utility that combines req_capsule_set_size() and req_capsule_client_get().
1794  *
1795  * First the \a pill's request \a field's size is set (\a rc_area) using
1796  * req_capsule_set_size() with the given \a len.  Then the actual buffer is
1797  * returned.
1798  */
1799 void *req_capsule_client_sized_get(struct req_capsule *pill,
1800                                    const struct req_msg_field *field,
1801                                    int len)
1802 {
1803         req_capsule_set_size(pill, field, RCL_CLIENT, len);
1804         return __req_capsule_get(pill, field, RCL_CLIENT, NULL, 0);
1805 }
1806 EXPORT_SYMBOL(req_capsule_client_sized_get);
1807
1808 /**
1809  * Trivial wrapper around __req_capsule_get(), that returns the PTLRPC reply
1810  * buffer corresponding to the given RMF (\a field) of a \a pill.
1811  */
1812 void *req_capsule_server_get(struct req_capsule *pill,
1813                              const struct req_msg_field *field)
1814 {
1815         return __req_capsule_get(pill, field, RCL_SERVER, NULL, 0);
1816 }
1817 EXPORT_SYMBOL(req_capsule_server_get);
1818
1819 /**
1820  * Same as req_capsule_server_get(), but with a \a swabber argument.
1821  *
1822  * Ideally all swabbing should be done pursuant to RMF definitions, with no
1823  * swabbing done outside this capsule abstraction.
1824  */
1825 void *req_capsule_server_swab_get(struct req_capsule *pill,
1826                                   const struct req_msg_field *field,
1827                                   void *swabber)
1828 {
1829         return __req_capsule_get(pill, field, RCL_SERVER, swabber, 0);
1830 }
1831 EXPORT_SYMBOL(req_capsule_server_swab_get);
1832
1833 /**
1834  * Utility that combines req_capsule_set_size() and req_capsule_server_get().
1835  *
1836  * First the \a pill's request \a field's size is set (\a rc_area) using
1837  * req_capsule_set_size() with the given \a len.  Then the actual buffer is
1838  * returned.
1839  */
1840 void *req_capsule_server_sized_get(struct req_capsule *pill,
1841                                    const struct req_msg_field *field,
1842                                    int len)
1843 {
1844         req_capsule_set_size(pill, field, RCL_SERVER, len);
1845         return __req_capsule_get(pill, field, RCL_SERVER, NULL, 0);
1846 }
1847 EXPORT_SYMBOL(req_capsule_server_sized_get);
1848
1849 /**
1850  * Returns the buffer of a \a pill corresponding to the given \a field from the
1851  * request (if the caller is executing on the server-side) or reply (if the
1852  * caller is executing on the client-side).
1853  *
1854  * This function convienient for use is code that could be executed on the
1855  * client and server alike.
1856  */
1857 const void *req_capsule_other_get(struct req_capsule *pill,
1858                                   const struct req_msg_field *field)
1859 {
1860         return __req_capsule_get(pill, field, pill->rc_loc ^ 1, NULL, 0);
1861 }
1862 EXPORT_SYMBOL(req_capsule_other_get);
1863
1864 /**
1865  * Set the size of the PTLRPC request/reply (\a loc) buffer for the given \a
1866  * field of the given \a pill.
1867  *
1868  * This function must be used when constructing variable sized fields of a
1869  * request or reply.
1870  */
1871 void req_capsule_set_size(struct req_capsule *pill,
1872                           const struct req_msg_field *field,
1873                           enum req_location loc, int size)
1874 {
1875         LASSERT(loc == RCL_SERVER || loc == RCL_CLIENT);
1876
1877         if ((size != field->rmf_size) &&
1878             (field->rmf_size != -1) &&
1879             !(field->rmf_flags & RMF_F_NO_SIZE_CHECK) &&
1880             (size > 0)) {
1881                 if ((field->rmf_flags & RMF_F_STRUCT_ARRAY) &&
1882                     (size % field->rmf_size != 0)) {
1883                         CERROR("%s: array field size mismatch "
1884                                "%d %% %d != 0 (%d)\n",
1885                                field->rmf_name, size, field->rmf_size, loc);
1886                         LBUG();
1887                 } else if (!(field->rmf_flags & RMF_F_STRUCT_ARRAY) &&
1888                     size < field->rmf_size) {
1889                         CERROR("%s: field size mismatch %d != %d (%d)\n",
1890                                field->rmf_name, size, field->rmf_size, loc);
1891                         LBUG();
1892                 }
1893         }
1894
1895         pill->rc_area[loc][__req_capsule_offset(pill, field, loc)] = size;
1896 }
1897 EXPORT_SYMBOL(req_capsule_set_size);
1898
1899 /**
1900  * Return the actual PTLRPC buffer length of a request or reply (\a loc)
1901  * for the given \a pill's given \a field.
1902  *
1903  * NB: this function doesn't correspond with req_capsule_set_size(), which
1904  * actually sets the size in pill.rc_area[loc][offset], but this function
1905  * returns the message buflen[offset], maybe we should use another name.
1906  */
1907 int req_capsule_get_size(const struct req_capsule *pill,
1908                          const struct req_msg_field *field,
1909                          enum req_location loc)
1910 {
1911         LASSERT(loc == RCL_SERVER || loc == RCL_CLIENT);
1912
1913         return lustre_msg_buflen(__req_msg(pill, loc),
1914                                  __req_capsule_offset(pill, field, loc));
1915 }
1916 EXPORT_SYMBOL(req_capsule_get_size);
1917
1918 /**
1919  * Wrapper around lustre_msg_size() that returns the PTLRPC size needed for the
1920  * given \a pill's request or reply (\a loc) given the field size recorded in
1921  * the \a pill's rc_area.
1922  *
1923  * See also req_capsule_set_size().
1924  */
1925 int req_capsule_msg_size(struct req_capsule *pill, enum req_location loc)
1926 {
1927         return lustre_msg_size(pill->rc_req->rq_import->imp_msg_magic,
1928                                pill->rc_fmt->rf_fields[loc].nr,
1929                                pill->rc_area[loc]);
1930 }
1931
1932 /**
1933  * While req_capsule_msg_size() computes the size of a PTLRPC request or reply
1934  * (\a loc) given a \a pill's \a rc_area, this function computes the size of a
1935  * PTLRPC request or reply given only an RQF (\a fmt).
1936  *
1937  * This function should not be used for formats which contain variable size
1938  * fields.
1939  */
1940 int req_capsule_fmt_size(__u32 magic, const struct req_format *fmt,
1941                          enum req_location loc)
1942 {
1943         int size, i = 0;
1944
1945         /*
1946          * This function should probably LASSERT() that fmt has no fields with
1947          * RMF_F_STRUCT_ARRAY in rmf_flags, since we can't know here how many
1948          * elements in the array there will ultimately be, but then, we could
1949          * assume that there will be at least one element, and that's just what
1950          * we do.
1951          */
1952         size = lustre_msg_hdr_size(magic, fmt->rf_fields[loc].nr);
1953         if (size < 0)
1954                 return size;
1955
1956         for (; i < fmt->rf_fields[loc].nr; ++i)
1957                 if (fmt->rf_fields[loc].d[i]->rmf_size != -1)
1958                         size += cfs_size_round(fmt->rf_fields[loc].d[i]->
1959                                                rmf_size);
1960         return size;
1961 }
1962
1963 /**
1964  * Changes the format of an RPC.
1965  *
1966  * The pill must already have been initialized, which means that it already has
1967  * a request format.  The new format \a fmt must be an extension of the pill's
1968  * old format.  Specifically: the new format must have as many request and reply
1969  * fields as the old one, and all fields shared by the old and new format must
1970  * be at least as large in the new format.
1971  *
1972  * The new format's fields may be of different "type" than the old format, but
1973  * only for fields that are "opaque" blobs: fields which have a) have no
1974  * \a rmf_swabber, b) \a rmf_flags == 0 or RMF_F_NO_SIZE_CHECK, and c) \a
1975  * rmf_size == -1 or \a rmf_flags == RMF_F_NO_SIZE_CHECK.  For example,
1976  * OBD_SET_INFO has a key field and an opaque value field that gets interpreted
1977  * according to the key field.  When the value, according to the key, contains a
1978  * structure (or array thereof) to be swabbed, the format should be changed to
1979  * one where the value field has \a rmf_size/rmf_flags/rmf_swabber set
1980  * accordingly.
1981  */
1982 void req_capsule_extend(struct req_capsule *pill, const struct req_format *fmt)
1983 {
1984         int i;
1985         int j;
1986
1987         const struct req_format *old;
1988
1989         LASSERT(pill->rc_fmt != NULL);
1990         LASSERT(__req_format_is_sane(fmt));
1991
1992         old = pill->rc_fmt;
1993         /*
1994          * Sanity checking...
1995          */
1996         for (i = 0; i < RCL_NR; ++i) {
1997                 LASSERT(fmt->rf_fields[i].nr >= old->rf_fields[i].nr);
1998                 for (j = 0; j < old->rf_fields[i].nr - 1; ++j) {
1999                         const struct req_msg_field *ofield = FMT_FIELD(old, i, j);
2000
2001                         /* "opaque" fields can be transmogrified */
2002                         if (ofield->rmf_swabber == NULL &&
2003                             (ofield->rmf_flags & ~RMF_F_NO_SIZE_CHECK) == 0 &&
2004                             (ofield->rmf_size == -1 ||
2005                             ofield->rmf_flags == RMF_F_NO_SIZE_CHECK))
2006                                 continue;
2007                         LASSERT(FMT_FIELD(fmt, i, j) == FMT_FIELD(old, i, j));
2008                 }
2009                 /*
2010                  * Last field in old format can be shorter than in new.
2011                  */
2012                 LASSERT(FMT_FIELD(fmt, i, j)->rmf_size >=
2013                         FMT_FIELD(old, i, j)->rmf_size);
2014         }
2015
2016         pill->rc_fmt = fmt;
2017 }
2018 EXPORT_SYMBOL(req_capsule_extend);
2019
2020 /**
2021  * This function returns a non-zero value if the given \a field is present in
2022  * the format (\a rc_fmt) of \a pill's PTLRPC request or reply (\a loc), else it
2023  * returns 0.
2024  */
2025 int req_capsule_has_field(const struct req_capsule *pill,
2026                           const struct req_msg_field *field,
2027                           enum req_location loc)
2028 {
2029         LASSERT(loc == RCL_SERVER || loc == RCL_CLIENT);
2030
2031         return field->rmf_offset[pill->rc_fmt->rf_idx][loc];
2032 }
2033 EXPORT_SYMBOL(req_capsule_has_field);
2034
2035 /**
2036  * Returns a non-zero value if the given \a field is present in the given \a
2037  * pill's PTLRPC request or reply (\a loc), else it returns 0.
2038  */
2039 int req_capsule_field_present(const struct req_capsule *pill,
2040                               const struct req_msg_field *field,
2041                               enum req_location loc)
2042 {
2043         int offset;
2044
2045         LASSERT(loc == RCL_SERVER || loc == RCL_CLIENT);
2046         LASSERT(req_capsule_has_field(pill, field, loc));
2047
2048         offset = __req_capsule_offset(pill, field, loc);
2049         return lustre_msg_bufcount(__req_msg(pill, loc)) > offset;
2050 }
2051 EXPORT_SYMBOL(req_capsule_field_present);
2052
2053 /**
2054  * This function shrinks the size of the _buffer_ of the \a pill's PTLRPC
2055  * request or reply (\a loc).
2056  *
2057  * This is not the opposite of req_capsule_extend().
2058  */
2059 void req_capsule_shrink(struct req_capsule *pill,
2060                         const struct req_msg_field *field,
2061                         unsigned int newlen,
2062                         enum req_location loc)
2063 {
2064         const struct req_format *fmt;
2065         struct lustre_msg       *msg;
2066         int                      len;
2067         int                      offset;
2068
2069         fmt = pill->rc_fmt;
2070         LASSERT(fmt != NULL);
2071         LASSERT(__req_format_is_sane(fmt));
2072         LASSERT(req_capsule_has_field(pill, field, loc));
2073         LASSERT(req_capsule_field_present(pill, field, loc));
2074
2075         offset = __req_capsule_offset(pill, field, loc);
2076
2077         msg = __req_msg(pill, loc);
2078         len = lustre_msg_buflen(msg, offset);
2079         LASSERTF(newlen <= len, "%s:%s, oldlen=%d, newlen=%d\n",
2080                                 fmt->rf_name, field->rmf_name, len, newlen);
2081
2082         if (loc == RCL_CLIENT)
2083                 pill->rc_req->rq_reqlen = lustre_shrink_msg(msg, offset, newlen,
2084                                                             1);
2085         else
2086                 pill->rc_req->rq_replen = lustre_shrink_msg(msg, offset, newlen,
2087                                                             1);
2088 }
2089 EXPORT_SYMBOL(req_capsule_shrink);
2090
2091 /* __REQ_LAYOUT_USER__ */
2092 #endif