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