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