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