Whamcloud - gitweb
LU-17662 osd-zfs: Support for ZFS 2.2.3
[fs/lustre-release.git] / lustre / ptlrpc / pack_generic.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/ptlrpc/pack_generic.c
32  *
33  * (Un)packing of OST requests
34  *
35  * Author: Peter J. Braam <braam@clusterfs.com>
36  * Author: Phil Schwan <phil@clusterfs.com>
37  * Author: Eric Barton <eeb@clusterfs.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_RPC
41
42 #include <linux/crc32.h>
43
44 #include <libcfs/libcfs.h>
45
46 #include <llog_swab.h>
47 #include <lustre_disk.h>
48 #include <lustre_net.h>
49 #include <lustre_swab.h>
50 #include <obd_cksum.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include "ptlrpc_internal.h"
54
55 static inline __u32 lustre_msg_hdr_size_v2(__u32 count)
56 {
57         return round_up(offsetof(struct lustre_msg_v2, lm_buflens[count]), 8);
58 }
59
60 __u32 lustre_msg_hdr_size(__u32 magic, __u32 count)
61 {
62         LASSERT(count > 0);
63
64         switch (magic) {
65         case LUSTRE_MSG_MAGIC_V2:
66                 return lustre_msg_hdr_size_v2(count);
67         default:
68                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
69                 return 0;
70         }
71 }
72
73 static inline int lustre_msg_check_version_v2(struct lustre_msg_v2 *msg,
74                                               enum lustre_msg_version version)
75 {
76         enum lustre_msg_version ver = lustre_msg_get_version(msg);
77
78         return (ver & LUSTRE_VERSION_MASK) != version;
79 }
80
81 int lustre_msg_check_version(struct lustre_msg *msg,
82                              enum lustre_msg_version version)
83 {
84 #define LUSTRE_MSG_MAGIC_V1 0x0BD00BD0
85         switch (msg->lm_magic) {
86         case LUSTRE_MSG_MAGIC_V1:
87                 CERROR("msg v1 not supported - please upgrade you system\n");
88                 return -EINVAL;
89         case LUSTRE_MSG_MAGIC_V2:
90                 return lustre_msg_check_version_v2(msg, version);
91         default:
92                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
93                 return -EPROTO;
94         }
95 #undef LUSTRE_MSG_MAGIC_V1
96 }
97
98 __u32 lustre_msg_early_size;
99 EXPORT_SYMBOL(lustre_msg_early_size);
100
101 /* early reply size */
102 void lustre_msg_early_size_init(void)
103 {
104         __u32 pblen = sizeof(struct ptlrpc_body);
105
106         lustre_msg_early_size = lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, &pblen);
107 }
108
109 __u32 lustre_msg_size_v2(int count, __u32 *lengths)
110 {
111         __u32 size;
112         int i;
113
114         LASSERT(count > 0);
115         size = lustre_msg_hdr_size_v2(count);
116         for (i = 0; i < count; i++)
117                 size += round_up(lengths[i], 8);
118
119         return size;
120 }
121 EXPORT_SYMBOL(lustre_msg_size_v2);
122
123 /*
124  * This returns the size of the buffer that is required to hold a lustre_msg
125  * with the given sub-buffer lengths.
126  * NOTE: this should only be used for NEW requests, and should always be
127  *       in the form of a v2 request.  If this is a connection to a v1
128  *       target then the first buffer will be stripped because the ptlrpc
129  *       data is part of the lustre_msg_v1 header. b=14043
130  */
131 __u32 lustre_msg_size(__u32 magic, int count, __u32 *lens)
132 {
133         __u32 size[] = { sizeof(struct ptlrpc_body) };
134
135         if (!lens) {
136                 LASSERT(count == 1);
137                 lens = size;
138         }
139
140         LASSERT(count > 0);
141         LASSERT(lens[MSG_PTLRPC_BODY_OFF] >= sizeof(struct ptlrpc_body_v2));
142
143         switch (magic) {
144         case LUSTRE_MSG_MAGIC_V2:
145                 return lustre_msg_size_v2(count, lens);
146         default:
147                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
148                 return 0;
149         }
150 }
151
152 /*
153  * This is used to determine the size of a buffer that was already packed
154  * and will correctly handle the different message formats.
155  */
156 __u32 lustre_packed_msg_size(struct lustre_msg *msg)
157 {
158         switch (msg->lm_magic) {
159         case LUSTRE_MSG_MAGIC_V2:
160                 return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
161         default:
162                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
163                 return 0;
164         }
165 }
166 EXPORT_SYMBOL(lustre_packed_msg_size);
167
168 void lustre_init_msg_v2(struct lustre_msg_v2 *msg, int count, __u32 *lens,
169                         char **bufs)
170 {
171         char *ptr;
172         int i;
173
174         LASSERT(count > 0);
175
176         msg->lm_bufcount = count;
177         /* XXX: lm_secflvr uninitialized here */
178         msg->lm_magic = LUSTRE_MSG_MAGIC_V2;
179
180         for (i = 0; i < count; i++)
181                 msg->lm_buflens[i] = lens[i];
182
183         if (bufs == NULL)
184                 return;
185
186         ptr = (char *)msg + lustre_msg_hdr_size_v2(count);
187         for (i = 0; i < count; i++) {
188                 char *tmp = bufs[i];
189
190                 if (tmp)
191                         memcpy(ptr, tmp, lens[i]);
192                 ptr += round_up(lens[i], 8);
193         }
194 }
195 EXPORT_SYMBOL(lustre_init_msg_v2);
196
197 static int lustre_pack_request_v2(struct ptlrpc_request *req,
198                                   int count, __u32 *lens, char **bufs)
199 {
200         int reqlen, rc;
201
202         reqlen = lustre_msg_size_v2(count, lens);
203
204         rc = sptlrpc_cli_alloc_reqbuf(req, reqlen);
205         if (rc)
206                 return rc;
207
208         req->rq_reqlen = reqlen;
209
210         lustre_init_msg_v2(req->rq_reqmsg, count, lens, bufs);
211         lustre_msg_add_version(req->rq_reqmsg, PTLRPC_MSG_VERSION);
212         return 0;
213 }
214
215 int lustre_pack_request(struct ptlrpc_request *req, __u32 magic, int count,
216                         __u32 *lens, char **bufs)
217 {
218         __u32 size[] = { sizeof(struct ptlrpc_body) };
219
220         if (!lens) {
221                 LASSERT(count == 1);
222                 lens = size;
223         }
224
225         LASSERT(count > 0);
226         LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
227
228         /* only use new format, we don't need to be compatible with 1.4 */
229         magic = LUSTRE_MSG_MAGIC_V2;
230
231         switch (magic) {
232         case LUSTRE_MSG_MAGIC_V2:
233                 return lustre_pack_request_v2(req, count, lens, bufs);
234         default:
235                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
236                 return -EINVAL;
237         }
238 }
239
240 #if RS_DEBUG
241 struct list_head ptlrpc_rs_debug_lru =
242         LIST_HEAD_INIT(ptlrpc_rs_debug_lru);
243 spinlock_t ptlrpc_rs_debug_lock;
244
245 #define PTLRPC_RS_DEBUG_LRU_ADD(rs)                                     \
246 do {                                                                    \
247         spin_lock(&ptlrpc_rs_debug_lock);                               \
248         list_add_tail(&(rs)->rs_debug_list, &ptlrpc_rs_debug_lru);      \
249         spin_unlock(&ptlrpc_rs_debug_lock);                             \
250 } while (0)
251
252 #define PTLRPC_RS_DEBUG_LRU_DEL(rs)                                     \
253 do {                                                                    \
254         spin_lock(&ptlrpc_rs_debug_lock);                               \
255         list_del(&(rs)->rs_debug_list);                         \
256         spin_unlock(&ptlrpc_rs_debug_lock);                             \
257 } while (0)
258 #else
259 # define PTLRPC_RS_DEBUG_LRU_ADD(rs) do {} while(0)
260 # define PTLRPC_RS_DEBUG_LRU_DEL(rs) do {} while(0)
261 #endif
262
263 struct ptlrpc_reply_state *
264 lustre_get_emerg_rs(struct ptlrpc_service_part *svcpt)
265 {
266         struct ptlrpc_reply_state *rs = NULL;
267
268         spin_lock(&svcpt->scp_rep_lock);
269
270         /* See if we have anything in a pool, and wait if nothing */
271         while (list_empty(&svcpt->scp_rep_idle)) {
272                 int                     rc;
273
274                 spin_unlock(&svcpt->scp_rep_lock);
275                 /* If we cannot get anything for some long time, we better
276                  * bail out instead of waiting infinitely */
277                 rc = wait_event_idle_timeout(svcpt->scp_rep_waitq,
278                                              !list_empty(&svcpt->scp_rep_idle),
279                                              cfs_time_seconds(10));
280                 if (rc <= 0)
281                         goto out;
282                 spin_lock(&svcpt->scp_rep_lock);
283         }
284
285         rs = list_first_entry(&svcpt->scp_rep_idle,
286                               struct ptlrpc_reply_state, rs_list);
287         list_del(&rs->rs_list);
288
289         spin_unlock(&svcpt->scp_rep_lock);
290
291         memset(rs, 0, svcpt->scp_service->srv_max_reply_size);
292         rs->rs_size = svcpt->scp_service->srv_max_reply_size;
293         rs->rs_svcpt = svcpt;
294         rs->rs_prealloc = 1;
295 out:
296         return rs;
297 }
298
299 void lustre_put_emerg_rs(struct ptlrpc_reply_state *rs)
300 {
301         struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
302
303         spin_lock(&svcpt->scp_rep_lock);
304         list_add(&rs->rs_list, &svcpt->scp_rep_idle);
305         spin_unlock(&svcpt->scp_rep_lock);
306         wake_up(&svcpt->scp_rep_waitq);
307 }
308
309 int lustre_pack_reply_v2(struct ptlrpc_request *req, int count,
310                          __u32 *lens, char **bufs, int flags)
311 {
312         struct ptlrpc_reply_state *rs;
313         int msg_len, rc;
314         ENTRY;
315
316         LASSERT(req->rq_reply_state == NULL);
317         LASSERT(count > 0);
318
319         if ((flags & LPRFL_EARLY_REPLY) == 0) {
320                 spin_lock(&req->rq_lock);
321                 req->rq_packed_final = 1;
322                 spin_unlock(&req->rq_lock);
323         }
324
325         msg_len = lustre_msg_size_v2(count, lens);
326         rc = sptlrpc_svc_alloc_rs(req, msg_len);
327         if (rc)
328                 RETURN(rc);
329
330         rs = req->rq_reply_state;
331         atomic_set(&rs->rs_refcount, 1); /* 1 ref for rq_reply_state */
332         rs->rs_cb_id.cbid_fn = reply_out_callback;
333         rs->rs_cb_id.cbid_arg = rs;
334         rs->rs_svcpt = req->rq_rqbd->rqbd_svcpt;
335         INIT_LIST_HEAD(&rs->rs_exp_list);
336         INIT_LIST_HEAD(&rs->rs_obd_list);
337         INIT_LIST_HEAD(&rs->rs_list);
338         spin_lock_init(&rs->rs_lock);
339
340         req->rq_replen = msg_len;
341         req->rq_reply_state = rs;
342         req->rq_repmsg = rs->rs_msg;
343
344         lustre_init_msg_v2(rs->rs_msg, count, lens, bufs);
345         lustre_msg_add_version(rs->rs_msg, PTLRPC_MSG_VERSION);
346
347         PTLRPC_RS_DEBUG_LRU_ADD(rs);
348
349         RETURN(0);
350 }
351 EXPORT_SYMBOL(lustre_pack_reply_v2);
352
353 int lustre_pack_reply_flags(struct ptlrpc_request *req, int count, __u32 *lens,
354                             char **bufs, int flags)
355 {
356         int rc = 0;
357         __u32 size[] = { sizeof(struct ptlrpc_body) };
358
359         if (!lens) {
360                 LASSERT(count == 1);
361                 lens = size;
362         }
363
364         LASSERT(count > 0);
365         LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
366
367         switch (req->rq_reqmsg->lm_magic) {
368         case LUSTRE_MSG_MAGIC_V2:
369                 rc = lustre_pack_reply_v2(req, count, lens, bufs, flags);
370                 break;
371         default:
372                 LASSERTF(0, "incorrect message magic: %08x\n",
373                          req->rq_reqmsg->lm_magic);
374                 rc = -EINVAL;
375         }
376         if (rc != 0)
377                 CERROR("lustre_pack_reply failed: rc=%d size=%d\n", rc,
378                        lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens));
379         return rc;
380 }
381
382 int lustre_pack_reply(struct ptlrpc_request *req, int count, __u32 *lens,
383                       char **bufs)
384 {
385         return lustre_pack_reply_flags(req, count, lens, bufs, 0);
386 }
387 EXPORT_SYMBOL(lustre_pack_reply);
388
389 void *lustre_msg_buf_v2(struct lustre_msg_v2 *m, __u32 n, __u32 min_size)
390 {
391         __u32 i, offset, buflen, bufcount;
392
393         LASSERT(m != NULL);
394         LASSERT(m->lm_bufcount > 0);
395
396         bufcount = m->lm_bufcount;
397         if (unlikely(n >= bufcount)) {
398                 CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
399                        m, n, bufcount);
400                 return NULL;
401         }
402
403         buflen = m->lm_buflens[n];
404         if (unlikely(buflen < min_size)) {
405                 CERROR("msg %p buffer[%d] size %d too small "
406                        "(required %d, opc=%d)\n", m, n, buflen, min_size,
407                        n == MSG_PTLRPC_BODY_OFF ? -1 : lustre_msg_get_opc(m));
408                 return NULL;
409         }
410
411         offset = lustre_msg_hdr_size_v2(bufcount);
412         for (i = 0; i < n; i++)
413                 offset += round_up(m->lm_buflens[i], 8);
414
415         return (char *)m + offset;
416 }
417
418 void *lustre_msg_buf(struct lustre_msg *m, __u32 n, __u32 min_size)
419 {
420         switch (m->lm_magic) {
421         case LUSTRE_MSG_MAGIC_V2:
422                 return lustre_msg_buf_v2(m, n, min_size);
423         default:
424                 LASSERTF(0, "incorrect message magic: %08x (msg:%px)\n",
425                          m->lm_magic, m);
426                 return NULL;
427         }
428 }
429 EXPORT_SYMBOL(lustre_msg_buf);
430
431 static int lustre_shrink_msg_v2(struct lustre_msg_v2 *msg, __u32 segment,
432                                 unsigned int newlen, int move_data)
433 {
434         char *tail = NULL, *newpos;
435         int tail_len = 0, n;
436
437         LASSERT(msg);
438         LASSERT(msg->lm_bufcount > segment);
439         LASSERT(msg->lm_buflens[segment] >= newlen);
440
441         if (msg->lm_buflens[segment] == newlen)
442                 goto out;
443
444         if (move_data && msg->lm_bufcount > segment + 1) {
445                 tail = lustre_msg_buf_v2(msg, segment + 1, 0);
446                 for (n = segment + 1; n < msg->lm_bufcount; n++)
447                         tail_len += round_up(msg->lm_buflens[n], 8);
448         }
449
450         msg->lm_buflens[segment] = newlen;
451
452         if (tail && tail_len) {
453                 newpos = lustre_msg_buf_v2(msg, segment + 1, 0);
454                 LASSERT(newpos <= tail);
455                 if (newpos != tail)
456                         memmove(newpos, tail, tail_len);
457         }
458 out:
459         return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
460 }
461
462 /*
463  * for @msg, shrink @segment to size @newlen. if @move_data is non-zero,
464  * we also move data forward from @segment + 1.
465  *
466  * if @newlen == 0, we remove the segment completely, but we still keep the
467  * totally bufcount the same to save possible data moving. this will leave a
468  * unused segment with size 0 at the tail, but that's ok.
469  *
470  * return new msg size after shrinking.
471  *
472  * CAUTION:
473  * + if any buffers higher than @segment has been filled in, must call shrink
474  *   with non-zero @move_data.
475  * + caller should NOT keep pointers to msg buffers which higher than @segment
476  *   after call shrink.
477  */
478 int lustre_shrink_msg(struct lustre_msg *msg, int segment,
479                       unsigned int newlen, int move_data)
480 {
481         switch (msg->lm_magic) {
482         case LUSTRE_MSG_MAGIC_V2:
483                 return lustre_shrink_msg_v2(msg, segment, newlen, move_data);
484         default:
485                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
486         }
487 }
488 EXPORT_SYMBOL(lustre_shrink_msg);
489
490 static int lustre_grow_msg_v2(struct lustre_msg_v2 *msg, __u32 segment,
491                               unsigned int newlen)
492 {
493         char *tail = NULL, *newpos;
494         int tail_len = 0, n;
495
496         LASSERT(msg);
497         LASSERT(msg->lm_bufcount > segment);
498         LASSERT(msg->lm_buflens[segment] <= newlen);
499
500         if (msg->lm_buflens[segment] == newlen)
501                 goto out;
502
503         if (msg->lm_bufcount > segment + 1) {
504                 tail = lustre_msg_buf_v2(msg, segment + 1, 0);
505                 for (n = segment + 1; n < msg->lm_bufcount; n++)
506                         tail_len += round_up(msg->lm_buflens[n], 8);
507         }
508
509         msg->lm_buflens[segment] = newlen;
510
511         if (tail && tail_len) {
512                 newpos = lustre_msg_buf_v2(msg, segment + 1, 0);
513                 memmove(newpos, tail, tail_len);
514         }
515 out:
516         return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
517 }
518
519 /*
520  * for @msg, grow @segment to size @newlen.
521  * Always move higher buffer forward.
522  *
523  * return new msg size after growing.
524  *
525  * CAUTION:
526  * - caller must make sure there is enough space in allocated message buffer
527  * - caller should NOT keep pointers to msg buffers which higher than @segment
528  *   after call shrink.
529  */
530 int lustre_grow_msg(struct lustre_msg *msg, int segment, unsigned int newlen)
531 {
532         switch (msg->lm_magic) {
533         case LUSTRE_MSG_MAGIC_V2:
534                 return lustre_grow_msg_v2(msg, segment, newlen);
535         default:
536                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
537         }
538 }
539 EXPORT_SYMBOL(lustre_grow_msg);
540
541 void lustre_free_reply_state(struct ptlrpc_reply_state *rs)
542 {
543         PTLRPC_RS_DEBUG_LRU_DEL(rs);
544
545         LASSERT(atomic_read(&rs->rs_refcount) == 0);
546         LASSERT(!rs->rs_difficult || rs->rs_handled);
547         LASSERT(!rs->rs_difficult || rs->rs_unlinked);
548         LASSERT(!rs->rs_scheduled);
549         LASSERT(rs->rs_export == NULL);
550         LASSERT(rs->rs_nlocks == 0);
551         LASSERT(list_empty(&rs->rs_exp_list));
552         LASSERT(list_empty(&rs->rs_obd_list));
553
554         sptlrpc_svc_free_rs(rs);
555 }
556
557 static int lustre_unpack_msg_v2(struct lustre_msg_v2 *m, int len)
558 {
559         int swabbed, required_len, i, buflen;
560
561         /* Now we know the sender speaks my language. */
562         required_len = lustre_msg_hdr_size_v2(0);
563         if (len < required_len) {
564                 /* can't even look inside the message */
565                 CERROR("message length %d too small for lustre_msg\n", len);
566                 return -EINVAL;
567         }
568
569         swabbed = (m->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED);
570
571         if (swabbed) {
572                 __swab32s(&m->lm_magic);
573                 __swab32s(&m->lm_bufcount);
574                 __swab32s(&m->lm_secflvr);
575                 __swab32s(&m->lm_repsize);
576                 __swab32s(&m->lm_cksum);
577                 __swab32s(&m->lm_flags);
578                 __swab32s(&m->lm_opc);
579                 BUILD_BUG_ON(offsetof(typeof(*m), lm_padding_3) == 0);
580         }
581
582         if (m->lm_bufcount == 0 || m->lm_bufcount > PTLRPC_MAX_BUFCOUNT) {
583                 CERROR("message bufcount %d is not valid\n", m->lm_bufcount);
584                 return -EINVAL;
585         }
586         required_len = lustre_msg_hdr_size_v2(m->lm_bufcount);
587         if (len < required_len) {
588                 /* didn't receive all the buffer lengths */
589                 CERROR("message length %d too small for %d buflens\n",
590                        len, m->lm_bufcount);
591                 return -EINVAL;
592         }
593
594         for (i = 0; i < m->lm_bufcount; i++) {
595                 if (swabbed)
596                         __swab32s(&m->lm_buflens[i]);
597                 buflen = round_up(m->lm_buflens[i], 8);
598                 if (buflen < 0 || buflen > PTLRPC_MAX_BUFLEN) {
599                         CERROR("buffer %d length %d is not valid\n", i, buflen);
600                         return -EINVAL;
601                 }
602                 required_len += buflen;
603         }
604         if (len < required_len || required_len > PTLRPC_MAX_BUFLEN) {
605                 CERROR("len: %d, required_len %d, bufcount: %d\n",
606                        len, required_len, m->lm_bufcount);
607                 for (i = 0; i < m->lm_bufcount; i++)
608                         CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
609                 return -EINVAL;
610         }
611
612         return swabbed;
613 }
614
615 int __lustre_unpack_msg(struct lustre_msg *m, int len)
616 {
617         int required_len, rc;
618
619         ENTRY;
620         /*
621          * We can provide a slightly better error log, if we check the
622          * message magic and version first.  In the future, struct
623          * lustre_msg may grow, and we'd like to log a version mismatch,
624          * rather than a short message.
625          */
626         required_len = offsetof(struct lustre_msg, lm_magic) +
627                                 sizeof(m->lm_magic);
628         if (len < required_len) {
629                 /* can't even look inside the message */
630                 CERROR("message length %d too small for magic/version check\n",
631                        len);
632                 RETURN(-EINVAL);
633         }
634
635         rc = lustre_unpack_msg_v2(m, len);
636
637         RETURN(rc);
638 }
639 EXPORT_SYMBOL(__lustre_unpack_msg);
640
641 int ptlrpc_unpack_req_msg(struct ptlrpc_request *req, int len)
642 {
643         int rc;
644
645         rc = __lustre_unpack_msg(req->rq_reqmsg, len);
646         if (rc == 1) {
647                 req_capsule_set_req_swabbed(&req->rq_pill,
648                                             MSG_PTLRPC_HEADER_OFF);
649                 rc = 0;
650         }
651         return rc;
652 }
653
654 int ptlrpc_unpack_rep_msg(struct ptlrpc_request *req, int len)
655 {
656         int rc;
657
658         rc = __lustre_unpack_msg(req->rq_repmsg, len);
659         if (rc == 1) {
660                 req_capsule_set_rep_swabbed(&req->rq_pill,
661                                             MSG_PTLRPC_HEADER_OFF);
662                 rc = 0;
663         }
664         return rc;
665 }
666
667 static inline int
668 lustre_unpack_ptlrpc_body_v2(struct ptlrpc_request *req,
669                              enum req_location loc, int offset)
670 {
671         struct ptlrpc_body *pb;
672         struct lustre_msg_v2 *m;
673
674         m = loc == RCL_CLIENT ? req->rq_reqmsg : req->rq_repmsg;
675
676         pb = lustre_msg_buf_v2(m, offset, sizeof(struct ptlrpc_body_v2));
677         if (!pb) {
678                 CERROR("error unpacking ptlrpc body\n");
679                 return -EFAULT;
680         }
681         if (req_capsule_need_swab(&req->rq_pill, loc, offset)) {
682                 lustre_swab_ptlrpc_body(pb);
683                 req_capsule_set_swabbed(&req->rq_pill, loc, offset);
684         }
685
686         if ((pb->pb_version & ~LUSTRE_VERSION_MASK) != PTLRPC_MSG_VERSION) {
687                 CERROR("wrong lustre_msg version %08x\n", pb->pb_version);
688                 return -EINVAL;
689         }
690
691         if (loc == RCL_SERVER)
692                 pb->pb_status = ptlrpc_status_ntoh(pb->pb_status);
693
694         return 0;
695 }
696
697 int lustre_unpack_req_ptlrpc_body(struct ptlrpc_request *req, int offset)
698 {
699         switch (req->rq_reqmsg->lm_magic) {
700         case LUSTRE_MSG_MAGIC_V2:
701                 return lustre_unpack_ptlrpc_body_v2(req, RCL_CLIENT, offset);
702         default:
703                 CERROR("bad lustre msg magic: %08x\n",
704                        req->rq_reqmsg->lm_magic);
705                 return -EINVAL;
706         }
707 }
708
709 int lustre_unpack_rep_ptlrpc_body(struct ptlrpc_request *req, int offset)
710 {
711         switch (req->rq_repmsg->lm_magic) {
712         case LUSTRE_MSG_MAGIC_V2:
713                 return lustre_unpack_ptlrpc_body_v2(req, RCL_SERVER, offset);
714         default:
715                 CERROR("bad lustre msg magic: %08x\n",
716                        req->rq_repmsg->lm_magic);
717                 return -EINVAL;
718         }
719 }
720
721 static inline __u32 lustre_msg_buflen_v2(struct lustre_msg_v2 *m, __u32 n)
722 {
723         if (n >= m->lm_bufcount)
724                 return 0;
725
726         return m->lm_buflens[n];
727 }
728
729 /**
730  * lustre_msg_buflen - return the length of buffer \a n in message \a m
731  * \param m lustre_msg (request or reply) to look at
732  * \param n message index (base 0)
733  *
734  * returns zero for non-existent message indices
735  */
736 __u32 lustre_msg_buflen(struct lustre_msg *m, __u32 n)
737 {
738         switch (m->lm_magic) {
739         case LUSTRE_MSG_MAGIC_V2:
740                 return lustre_msg_buflen_v2(m, n);
741         default:
742                 CERROR("incorrect message magic: %08x\n", m->lm_magic);
743                 return 0;
744         }
745 }
746 EXPORT_SYMBOL(lustre_msg_buflen);
747
748 static inline void
749 lustre_msg_set_buflen_v2(struct lustre_msg_v2 *m, __u32 n, __u32 len)
750 {
751         if (n >= m->lm_bufcount)
752                 LBUG();
753
754         m->lm_buflens[n] = len;
755 }
756
757 void lustre_msg_set_buflen(struct lustre_msg *m, __u32 n, __u32 len)
758 {
759         switch (m->lm_magic) {
760         case LUSTRE_MSG_MAGIC_V2:
761                 lustre_msg_set_buflen_v2(m, n, len);
762                 return;
763         default:
764                 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
765         }
766 }
767
768 /*
769  * NB return the bufcount for lustre_msg_v2 format, so if message is packed
770  * in V1 format, the result is one bigger. (add struct ptlrpc_body).
771  */
772 __u32 lustre_msg_bufcount(struct lustre_msg *m)
773 {
774         switch (m->lm_magic) {
775         case LUSTRE_MSG_MAGIC_V2:
776                 return m->lm_bufcount;
777         default:
778                 CERROR("incorrect message magic: %08x\n", m->lm_magic);
779                 return 0;
780         }
781 }
782
783 char *lustre_msg_string(struct lustre_msg *m, __u32 index, __u32 max_len)
784 {
785         /* max_len == 0 means the string should fill the buffer */
786         char *str;
787         __u32 slen, blen;
788
789         switch (m->lm_magic) {
790         case LUSTRE_MSG_MAGIC_V2:
791                 str = lustre_msg_buf_v2(m, index, 0);
792                 blen = lustre_msg_buflen_v2(m, index);
793                 break;
794         default:
795                 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
796         }
797
798         if (str == NULL) {
799                 CERROR("can't unpack string in msg %p buffer[%d]\n", m, index);
800                 return NULL;
801         }
802
803         slen = strnlen(str, blen);
804
805         if (slen == blen) { /* not NULL terminated */
806                 CERROR("can't unpack non-NULL terminated string in msg %p buffer[%d] len %d\n",
807                        m, index, blen);
808                 return NULL;
809         }
810         if (blen > PTLRPC_MAX_BUFLEN) {
811                 CERROR("buffer length of msg %p buffer[%d] is invalid(%d)\n",
812                        m, index, blen);
813                 return NULL;
814         }
815
816         if (max_len == 0) {
817                 if (slen != blen - 1) {
818                         CERROR("can't unpack short string in msg %p buffer[%d] len %d: strlen %d\n",
819                                m, index, blen, slen);
820                         return NULL;
821                 }
822         } else if (slen > max_len) {
823                 CERROR("can't unpack oversized string in msg %p buffer[%d] len %d strlen %d: max %d expected\n",
824                        m, index, blen, slen, max_len);
825                 return NULL;
826         }
827
828         return str;
829 }
830
831 /* Wrap up the normal fixed length cases */
832 static inline void *__lustre_swab_buf(struct lustre_msg *msg, __u32 index,
833                                       __u32 min_size, void *swabber)
834 {
835         void *ptr = NULL;
836
837         LASSERT(msg != NULL);
838         switch (msg->lm_magic) {
839         case LUSTRE_MSG_MAGIC_V2:
840                 ptr = lustre_msg_buf_v2(msg, index, min_size);
841                 break;
842         default:
843                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
844         }
845
846         if (ptr != NULL && swabber != NULL)
847                 ((void (*)(void *))swabber)(ptr);
848
849         return ptr;
850 }
851
852 static inline struct ptlrpc_body *lustre_msg_ptlrpc_body(struct lustre_msg *msg)
853 {
854         return lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
855                                  sizeof(struct ptlrpc_body_v2));
856 }
857
858 enum lustre_msghdr lustre_msghdr_get_flags(struct lustre_msg *msg)
859 {
860         switch (msg->lm_magic) {
861         case LUSTRE_MSG_MAGIC_V2:
862                 /* already in host endian */
863                 return msg->lm_flags;
864         default:
865                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
866                 return 0;
867         }
868 }
869 EXPORT_SYMBOL(lustre_msghdr_get_flags);
870
871 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags)
872 {
873         switch (msg->lm_magic) {
874         case LUSTRE_MSG_MAGIC_V2:
875                 msg->lm_flags = flags;
876                 return;
877         default:
878                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
879         }
880 }
881
882 __u32 lustre_msg_get_flags(struct lustre_msg *msg)
883 {
884         switch (msg->lm_magic) {
885         case LUSTRE_MSG_MAGIC_V2: {
886                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
887                 if (pb != NULL)
888                         return pb->pb_flags;
889
890                 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
891         }
892         fallthrough;
893         default:
894                 /*
895                  * flags might be printed in debug code while message
896                  * uninitialized
897                  */
898                 return 0;
899         }
900 }
901 EXPORT_SYMBOL(lustre_msg_get_flags);
902
903 void lustre_msg_add_flags(struct lustre_msg *msg, __u32 flags)
904 {
905         switch (msg->lm_magic) {
906         case LUSTRE_MSG_MAGIC_V2: {
907                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
908                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
909                 pb->pb_flags |= flags;
910                 return;
911         }
912         default:
913                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
914         }
915 }
916 EXPORT_SYMBOL(lustre_msg_add_flags);
917
918 void lustre_msg_set_flags(struct lustre_msg *msg, __u32 flags)
919 {
920         switch (msg->lm_magic) {
921         case LUSTRE_MSG_MAGIC_V2: {
922                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
923                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
924                 pb->pb_flags = flags;
925                 return;
926         }
927         default:
928                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
929         }
930 }
931
932 void lustre_msg_clear_flags(struct lustre_msg *msg, __u32 flags)
933 {
934         switch (msg->lm_magic) {
935         case LUSTRE_MSG_MAGIC_V2: {
936                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
937                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
938                 pb->pb_flags &= ~flags;
939
940                 return;
941         }
942         default:
943                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
944         }
945 }
946 EXPORT_SYMBOL(lustre_msg_clear_flags);
947
948 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg)
949 {
950         switch (msg->lm_magic) {
951         case LUSTRE_MSG_MAGIC_V2: {
952                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
953                 if (pb != NULL)
954                         return pb->pb_op_flags;
955
956                 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
957         }
958         fallthrough;
959         default:
960                 return 0;
961         }
962 }
963
964 void lustre_msg_add_op_flags(struct lustre_msg *msg, __u32 flags)
965 {
966         switch (msg->lm_magic) {
967         case LUSTRE_MSG_MAGIC_V2: {
968                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
969                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
970                 pb->pb_op_flags |= flags;
971                 return;
972         }
973         default:
974                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
975         }
976 }
977 EXPORT_SYMBOL(lustre_msg_add_op_flags);
978
979 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg)
980 {
981         switch (msg->lm_magic) {
982         case LUSTRE_MSG_MAGIC_V2: {
983                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
984                 if (pb == NULL) {
985                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
986                         return NULL;
987                 }
988                 return &pb->pb_handle;
989         }
990         default:
991                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
992                 return NULL;
993         }
994 }
995
996 __u32 lustre_msg_get_type(struct lustre_msg *msg)
997 {
998         switch (msg->lm_magic) {
999         case LUSTRE_MSG_MAGIC_V2: {
1000                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1001                 if (pb == NULL) {
1002                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1003                         return PTL_RPC_MSG_ERR;
1004                 }
1005                 return pb->pb_type;
1006         }
1007         default:
1008                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1009                 return PTL_RPC_MSG_ERR;
1010         }
1011 }
1012 EXPORT_SYMBOL(lustre_msg_get_type);
1013
1014 enum lustre_msg_version lustre_msg_get_version(struct lustre_msg *msg)
1015 {
1016         switch (msg->lm_magic) {
1017         case LUSTRE_MSG_MAGIC_V2: {
1018                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1019                 if (pb == NULL) {
1020                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1021                         return 0;
1022                 }
1023                 return pb->pb_version;
1024         }
1025         default:
1026                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1027                 return 0;
1028         }
1029 }
1030
1031 void lustre_msg_add_version(struct lustre_msg *msg, __u32 version)
1032 {
1033         switch (msg->lm_magic) {
1034         case LUSTRE_MSG_MAGIC_V2: {
1035                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1036                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
1037                 pb->pb_version |= version;
1038                 return;
1039         }
1040         default:
1041                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1042         }
1043 }
1044
1045 __u32 lustre_msg_get_opc(struct lustre_msg *msg)
1046 {
1047         switch (msg->lm_magic) {
1048         case LUSTRE_MSG_MAGIC_V2: {
1049                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1050                 if (pb == NULL) {
1051                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1052                         return 0;
1053                 }
1054                 return pb->pb_opc;
1055         }
1056         default:
1057                 CERROR("incorrect message magic: %08x (msg:%p)\n",
1058                        msg->lm_magic, msg);
1059                 return 0;
1060         }
1061 }
1062 EXPORT_SYMBOL(lustre_msg_get_opc);
1063
1064 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg)
1065 {
1066         switch (msg->lm_magic) {
1067         case LUSTRE_MSG_MAGIC_V2: {
1068                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1069                 if (pb == NULL) {
1070                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1071                         return 0;
1072                 }
1073                 return pb->pb_last_xid;
1074         }
1075         default:
1076                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1077                 return 0;
1078         }
1079 }
1080 EXPORT_SYMBOL(lustre_msg_get_last_xid);
1081
1082 __u16 lustre_msg_get_tag(struct lustre_msg *msg)
1083 {
1084         switch (msg->lm_magic) {
1085         case LUSTRE_MSG_MAGIC_V2: {
1086                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1087                 if (!pb) {
1088                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1089                         return 0;
1090                 }
1091                 return pb->pb_tag;
1092         }
1093         default:
1094                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1095                 return 0;
1096         }
1097 }
1098 EXPORT_SYMBOL(lustre_msg_get_tag);
1099
1100 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg)
1101 {
1102         switch (msg->lm_magic) {
1103         case LUSTRE_MSG_MAGIC_V2: {
1104                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1105                 if (pb == NULL) {
1106                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1107                         return 0;
1108                 }
1109                 return pb->pb_last_committed;
1110         }
1111         default:
1112                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1113                 return 0;
1114         }
1115 }
1116 EXPORT_SYMBOL(lustre_msg_get_last_committed);
1117
1118 __u64 *lustre_msg_get_versions(struct lustre_msg *msg)
1119 {
1120         switch (msg->lm_magic) {
1121         case LUSTRE_MSG_MAGIC_V2: {
1122                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1123                 if (pb == NULL) {
1124                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1125                         return NULL;
1126                 }
1127                 return pb->pb_pre_versions;
1128         }
1129         default:
1130                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1131                 return NULL;
1132         }
1133 }
1134 EXPORT_SYMBOL(lustre_msg_get_versions);
1135
1136 __u64 lustre_msg_get_transno(struct lustre_msg *msg)
1137 {
1138         switch (msg->lm_magic) {
1139         case LUSTRE_MSG_MAGIC_V2: {
1140                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1141                 if (pb == NULL) {
1142                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1143                         return 0;
1144                 }
1145                 return pb->pb_transno;
1146         }
1147         default:
1148                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1149                 return 0;
1150         }
1151 }
1152 EXPORT_SYMBOL(lustre_msg_get_transno);
1153
1154 int lustre_msg_get_status(struct lustre_msg *msg)
1155 {
1156         switch (msg->lm_magic) {
1157         case LUSTRE_MSG_MAGIC_V2: {
1158                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1159                 if (pb != NULL)
1160                         return pb->pb_status;
1161                 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1162         }
1163         fallthrough;
1164         default:
1165                 /*
1166                  * status might be printed in debug code while message
1167                  * uninitialized
1168                  */
1169                 return -EINVAL;
1170         }
1171 }
1172 EXPORT_SYMBOL(lustre_msg_get_status);
1173
1174 __u64 lustre_msg_get_slv(struct lustre_msg *msg)
1175 {
1176         switch (msg->lm_magic) {
1177         case LUSTRE_MSG_MAGIC_V2: {
1178                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1179                 if (pb == NULL) {
1180                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1181                         return -EINVAL;
1182                 }
1183                 return pb->pb_slv;
1184         }
1185         default:
1186                 CERROR("invalid msg magic %08x\n", msg->lm_magic);
1187                 return -EINVAL;
1188         }
1189 }
1190
1191
1192 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv)
1193 {
1194         switch (msg->lm_magic) {
1195         case LUSTRE_MSG_MAGIC_V2: {
1196                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1197                 if (pb == NULL) {
1198                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1199                         return;
1200                 }
1201                 pb->pb_slv = slv;
1202                 return;
1203         }
1204         default:
1205                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1206                 return;
1207         }
1208 }
1209
1210 __u32 lustre_msg_get_limit(struct lustre_msg *msg)
1211 {
1212         switch (msg->lm_magic) {
1213         case LUSTRE_MSG_MAGIC_V2: {
1214                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1215                 if (pb == NULL) {
1216                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1217                         return -EINVAL;
1218                 }
1219                 return pb->pb_limit;
1220         }
1221         default:
1222                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1223                 return -EINVAL;
1224         }
1225 }
1226
1227
1228 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit)
1229 {
1230         switch (msg->lm_magic) {
1231         case LUSTRE_MSG_MAGIC_V2: {
1232                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1233                 if (pb == NULL) {
1234                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1235                         return;
1236                 }
1237                 pb->pb_limit = limit;
1238                 return;
1239         }
1240         default:
1241                 CERROR("invalid msg magic %08x\n", msg->lm_magic);
1242                 return;
1243         }
1244 }
1245
1246 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg)
1247 {
1248         switch (msg->lm_magic) {
1249         case LUSTRE_MSG_MAGIC_V2: {
1250                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1251                 if (pb == NULL) {
1252                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1253                         return 0;
1254                 }
1255                 return pb->pb_conn_cnt;
1256         }
1257         default:
1258                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1259                 return 0;
1260         }
1261 }
1262 EXPORT_SYMBOL(lustre_msg_get_conn_cnt);
1263
1264 __u32 lustre_msg_get_magic(struct lustre_msg *msg)
1265 {
1266         switch (msg->lm_magic) {
1267         case LUSTRE_MSG_MAGIC_V2:
1268                 return msg->lm_magic;
1269         default:
1270                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1271                 return 0;
1272         }
1273 }
1274
1275 timeout_t lustre_msg_get_timeout(struct lustre_msg *msg)
1276 {
1277         switch (msg->lm_magic) {
1278         case LUSTRE_MSG_MAGIC_V2: {
1279                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1280
1281                 if (pb == NULL) {
1282                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1283                         return 0;
1284                 }
1285                 return pb->pb_timeout;
1286         }
1287         default:
1288                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1289                 return 0;
1290         }
1291 }
1292
1293 timeout_t lustre_msg_get_service_timeout(struct lustre_msg *msg)
1294 {
1295         switch (msg->lm_magic) {
1296         case LUSTRE_MSG_MAGIC_V2: {
1297                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1298
1299                 if (pb == NULL) {
1300                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1301                         return 0;
1302                 }
1303                 return pb->pb_service_time;
1304         }
1305         default:
1306                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1307                 return 0;
1308         }
1309 }
1310
1311 int lustre_msg_get_uid_gid(struct lustre_msg *msg, __u32 *uid, __u32 *gid)
1312 {
1313         switch (msg->lm_magic) {
1314         case LUSTRE_MSG_MAGIC_V2: {
1315                 struct ptlrpc_body *pb;
1316
1317                 /* the old pltrpc_body_v2 is smaller; doesn't include uid/gid */
1318                 if (msg->lm_buflens[MSG_PTLRPC_BODY_OFF] <
1319                     sizeof(struct ptlrpc_body))
1320                         return -EOPNOTSUPP;
1321
1322                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1323                                           sizeof(struct ptlrpc_body));
1324
1325                 if (!pb || !(pb->pb_flags & MSG_PACK_UID_GID))
1326                         return -EOPNOTSUPP;
1327
1328                 if (uid)
1329                         *uid = pb->pb_uid;
1330                 if (gid)
1331                         *gid = pb->pb_gid;
1332
1333                 return 0;
1334         }
1335         default:
1336                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1337                 return -EOPNOTSUPP;
1338         }
1339 }
1340 EXPORT_SYMBOL(lustre_msg_get_uid_gid);
1341
1342 char *lustre_msg_get_jobid(struct lustre_msg *msg)
1343 {
1344         switch (msg->lm_magic) {
1345         case LUSTRE_MSG_MAGIC_V2: {
1346                 struct ptlrpc_body *pb;
1347
1348                 /* the old pltrpc_body_v2 is smaller; doesn't include jobid */
1349                 if (msg->lm_buflens[MSG_PTLRPC_BODY_OFF] <
1350                     sizeof(struct ptlrpc_body))
1351                         return NULL;
1352
1353                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1354                                           sizeof(struct ptlrpc_body));
1355                 if (!pb)
1356                         return NULL;
1357
1358                 /* If clients send unterminated jobids, terminate them here
1359                  * so that there is no chance of string overflow later.
1360                  */
1361                 if (unlikely(pb->pb_jobid[LUSTRE_JOBID_SIZE - 1] != '\0'))
1362                         pb->pb_jobid[LUSTRE_JOBID_SIZE - 1] = '\0';
1363
1364                 return pb->pb_jobid;
1365         }
1366         default:
1367                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1368                 return NULL;
1369         }
1370 }
1371 EXPORT_SYMBOL(lustre_msg_get_jobid);
1372
1373 __u32 lustre_msg_get_cksum(struct lustre_msg *msg)
1374 {
1375         switch (msg->lm_magic) {
1376         case LUSTRE_MSG_MAGIC_V2:
1377                 return msg->lm_cksum;
1378         default:
1379                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1380                 return 0;
1381         }
1382 }
1383
1384 __u64 lustre_msg_get_mbits(struct lustre_msg *msg)
1385 {
1386         switch (msg->lm_magic) {
1387         case LUSTRE_MSG_MAGIC_V2: {
1388                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1389                 if (pb == NULL) {
1390                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1391                         return 0;
1392                 }
1393                 return pb->pb_mbits;
1394         }
1395         default:
1396                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1397                 return 0;
1398         }
1399 }
1400
1401 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg, __u32 buf)
1402 {
1403         switch (msg->lm_magic) {
1404         case LUSTRE_MSG_MAGIC_V2: {
1405                 struct ptlrpc_body *pb = lustre_msg_buf_v2(msg, buf, 0);
1406                 __u32 len = lustre_msg_buflen(msg, buf);
1407                 __u32 crc;
1408
1409 #if IS_ENABLED(CONFIG_CRC32)
1410                 /* about 10x faster than crypto_hash for small buffers */
1411                 crc = crc32_le(~(__u32)0, (unsigned char *)pb, len);
1412 #elif IS_ENABLED(CONFIG_CRYPTO_CRC32)
1413                 unsigned int hsize = 4;
1414
1415                 cfs_crypto_hash_digest(CFS_HASH_ALG_CRC32, (unsigned char *)pb,
1416                                        len, NULL, 0, (unsigned char *)&crc,
1417                                        &hsize);
1418 #else
1419 #error "need either CONFIG_CRC32 or CONFIG_CRYPTO_CRC32 enabled in the kernel"
1420 #endif
1421                 return crc;
1422         }
1423         default:
1424                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1425                 return 0;
1426         }
1427 }
1428
1429 void lustre_msg_set_handle(struct lustre_msg *msg, struct lustre_handle *handle)
1430 {
1431         switch (msg->lm_magic) {
1432         case LUSTRE_MSG_MAGIC_V2: {
1433                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1434                 LASSERTF(pb, "invalid msg %px: no ptlrpc body!\n", msg);
1435                 pb->pb_handle = *handle;
1436                 return;
1437         }
1438         default:
1439                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1440         }
1441 }
1442
1443 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type)
1444 {
1445         switch (msg->lm_magic) {
1446         case LUSTRE_MSG_MAGIC_V2: {
1447                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1448                 LASSERTF(pb, "invalid msg %px: no ptlrpc body!\n", msg);
1449                 pb->pb_type = type;
1450                 return;
1451                 }
1452         default:
1453                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1454         }
1455 }
1456
1457 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc)
1458 {
1459         switch (msg->lm_magic) {
1460         case LUSTRE_MSG_MAGIC_V2: {
1461                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1462                 LASSERTF(pb, "invalid msg %px: no ptlrpc body!\n", msg);
1463                 pb->pb_opc = opc;
1464                 return;
1465         }
1466         default:
1467                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1468         }
1469 }
1470
1471 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid)
1472 {
1473         switch (msg->lm_magic) {
1474         case LUSTRE_MSG_MAGIC_V2: {
1475                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1476                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
1477                 pb->pb_last_xid = last_xid;
1478                 return;
1479         }
1480         default:
1481                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1482         }
1483 }
1484 EXPORT_SYMBOL(lustre_msg_set_last_xid);
1485
1486 void lustre_msg_set_tag(struct lustre_msg *msg, __u16 tag)
1487 {
1488         switch (msg->lm_magic) {
1489         case LUSTRE_MSG_MAGIC_V2: {
1490                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1491                 LASSERTF(pb, "invalid msg %px: no ptlrpc body!\n", msg);
1492                 pb->pb_tag = tag;
1493                 return;
1494         }
1495         default:
1496                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1497         }
1498 }
1499 EXPORT_SYMBOL(lustre_msg_set_tag);
1500
1501 void lustre_msg_set_last_committed(struct lustre_msg *msg, __u64 last_committed)
1502 {
1503         switch (msg->lm_magic) {
1504         case LUSTRE_MSG_MAGIC_V2: {
1505                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1506                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
1507                 pb->pb_last_committed = last_committed;
1508                 return;
1509         }
1510         default:
1511                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1512         }
1513 }
1514
1515 void lustre_msg_set_versions(struct lustre_msg *msg, __u64 *versions)
1516 {
1517         switch (msg->lm_magic) {
1518         case LUSTRE_MSG_MAGIC_V2: {
1519                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1520                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
1521                 pb->pb_pre_versions[0] = versions[0];
1522                 pb->pb_pre_versions[1] = versions[1];
1523                 pb->pb_pre_versions[2] = versions[2];
1524                 pb->pb_pre_versions[3] = versions[3];
1525                 return;
1526         }
1527         default:
1528                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1529         }
1530 }
1531 EXPORT_SYMBOL(lustre_msg_set_versions);
1532
1533 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno)
1534 {
1535         switch (msg->lm_magic) {
1536         case LUSTRE_MSG_MAGIC_V2: {
1537                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1538                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
1539                 pb->pb_transno = transno;
1540                 return;
1541         }
1542         default:
1543                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1544         }
1545 }
1546 EXPORT_SYMBOL(lustre_msg_set_transno);
1547
1548 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status)
1549 {
1550         switch (msg->lm_magic) {
1551         case LUSTRE_MSG_MAGIC_V2: {
1552                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1553                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
1554                 pb->pb_status = status;
1555                 return;
1556         }
1557         default:
1558                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1559         }
1560 }
1561 EXPORT_SYMBOL(lustre_msg_set_status);
1562
1563 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt)
1564 {
1565         switch (msg->lm_magic) {
1566         case LUSTRE_MSG_MAGIC_V2: {
1567                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1568                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
1569                 pb->pb_conn_cnt = conn_cnt;
1570                 return;
1571         }
1572         default:
1573                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1574         }
1575 }
1576
1577 void lustre_msg_set_timeout(struct lustre_msg *msg, timeout_t timeout)
1578 {
1579         switch (msg->lm_magic) {
1580         case LUSTRE_MSG_MAGIC_V2: {
1581                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1582
1583                 LASSERT(timeout >= 0);
1584                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
1585                 pb->pb_timeout = timeout;
1586                 return;
1587         }
1588         default:
1589                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1590         }
1591 }
1592
1593 void lustre_msg_set_service_timeout(struct lustre_msg *msg,
1594                                     timeout_t service_timeout)
1595 {
1596         switch (msg->lm_magic) {
1597         case LUSTRE_MSG_MAGIC_V2: {
1598                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1599
1600                 LASSERT(service_timeout >= 0);
1601                 LASSERTF(pb, "invalid msg %px: no ptlrpc body!\n", msg);
1602                 pb->pb_service_time = service_timeout;
1603                 return;
1604         }
1605         default:
1606                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1607         }
1608 }
1609
1610 void lustre_msg_set_uid_gid(struct lustre_msg *msg, __u32 *uid, __u32 *gid)
1611 {
1612         switch (msg->lm_magic) {
1613         case LUSTRE_MSG_MAGIC_V2: {
1614                 __u32 opc = lustre_msg_get_opc(msg);
1615                 struct ptlrpc_body *pb;
1616
1617                 /* Don't set uid/gid for ldlm ast RPCs */
1618                 if (!opc || opc == LDLM_BL_CALLBACK ||
1619                     opc == LDLM_CP_CALLBACK || opc == LDLM_GL_CALLBACK)
1620                         return;
1621
1622                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1623                                        sizeof(struct ptlrpc_body));
1624                 LASSERTF(pb, "invalid msg %px: no ptlrpc body!\n", msg);
1625
1626                 if (uid && gid) {
1627                         pb->pb_uid = *uid;
1628                         pb->pb_gid = *gid;
1629                         pb->pb_flags |= MSG_PACK_UID_GID;
1630                 } else if (!(pb->pb_flags & MSG_PACK_UID_GID)) {
1631                         pb->pb_uid = from_kuid(&init_user_ns, current_uid());
1632                         pb->pb_gid = from_kgid(&init_user_ns, current_gid());
1633                         pb->pb_flags |= MSG_PACK_UID_GID;
1634                 }
1635
1636                 return;
1637         }
1638         default:
1639                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1640         }
1641 }
1642 EXPORT_SYMBOL(lustre_msg_set_uid_gid);
1643
1644 void lustre_msg_set_jobid(struct lustre_msg *msg, char *jobid)
1645 {
1646         switch (msg->lm_magic) {
1647         case LUSTRE_MSG_MAGIC_V2: {
1648                 __u32 opc = lustre_msg_get_opc(msg);
1649                 struct ptlrpc_body *pb;
1650
1651                 /* Don't set jobid for ldlm ast RPCs, they've been shrinked.
1652                  * See the comment in ptlrpc_request_pack(). */
1653                 if (!opc || opc == LDLM_BL_CALLBACK ||
1654                     opc == LDLM_CP_CALLBACK || opc == LDLM_GL_CALLBACK)
1655                         return;
1656
1657                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1658                                        sizeof(struct ptlrpc_body));
1659                 LASSERTF(pb, "invalid msg %px: no ptlrpc body!\n", msg);
1660
1661                 if (jobid != NULL)
1662                         memcpy(pb->pb_jobid, jobid, sizeof(pb->pb_jobid));
1663                 else if (pb->pb_jobid[0] == '\0')
1664                         lustre_get_jobid(pb->pb_jobid, sizeof(pb->pb_jobid));
1665                 return;
1666         }
1667         default:
1668                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1669         }
1670 }
1671 EXPORT_SYMBOL(lustre_msg_set_jobid);
1672
1673 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum)
1674 {
1675         switch (msg->lm_magic) {
1676         case LUSTRE_MSG_MAGIC_V2:
1677                 msg->lm_cksum = cksum;
1678                 return;
1679         default:
1680                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1681         }
1682 }
1683
1684 void lustre_msg_set_mbits(struct lustre_msg *msg, __u64 mbits)
1685 {
1686         switch (msg->lm_magic) {
1687         case LUSTRE_MSG_MAGIC_V2: {
1688                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1689
1690                 LASSERTF(pb != NULL, "invalid msg %px: no ptlrpc body!\n", msg);
1691                 pb->pb_mbits = mbits;
1692                 return;
1693         }
1694         default:
1695                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1696         }
1697 }
1698
1699 void ptlrpc_request_set_replen(struct ptlrpc_request *req)
1700 {
1701         int count = req_capsule_filled_sizes(&req->rq_pill, RCL_SERVER);
1702
1703         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count,
1704                                          req->rq_pill.rc_area[RCL_SERVER]);
1705         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1706                 req->rq_reqmsg->lm_repsize = req->rq_replen;
1707 }
1708 EXPORT_SYMBOL(ptlrpc_request_set_replen);
1709
1710 /**
1711  * Send a remote set_info_async.
1712  *
1713  * This may go from client to server or server to client.
1714  */
1715 int do_set_info_async(struct obd_import *imp,
1716                       int opcode, int version,
1717                       size_t keylen, void *key,
1718                       size_t vallen, void *val,
1719                       struct ptlrpc_request_set *set)
1720 {
1721         struct ptlrpc_request *req;
1722         char *tmp;
1723         int rc;
1724
1725         ENTRY;
1726
1727         req = ptlrpc_request_alloc(imp, KEY_IS(KEY_CHANGELOG_CLEAR) ?
1728                                                 &RQF_MDT_SET_INFO :
1729                                                 &RQF_OBD_SET_INFO);
1730         if (req == NULL)
1731                 RETURN(-ENOMEM);
1732
1733         req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_KEY,
1734                              RCL_CLIENT, keylen);
1735         req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_VAL,
1736                              RCL_CLIENT, vallen);
1737         rc = ptlrpc_request_pack(req, version, opcode);
1738         if (rc) {
1739                 ptlrpc_request_free(req);
1740                 RETURN(rc);
1741         }
1742
1743         if (KEY_IS(KEY_CHANGELOG_CLEAR))
1744                 do_pack_body(req);
1745
1746         tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1747         memcpy(tmp, key, keylen);
1748         tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1749         memcpy(tmp, val, vallen);
1750
1751         ptlrpc_request_set_replen(req);
1752
1753         if (set) {
1754                 ptlrpc_set_add_req(set, req);
1755                 ptlrpc_check_set(NULL, set);
1756         } else {
1757                 rc = ptlrpc_queue_wait(req);
1758                 ptlrpc_req_put(req);
1759         }
1760
1761         RETURN(rc);
1762 }
1763 EXPORT_SYMBOL(do_set_info_async);
1764
1765 /* byte flipping routines for all wire types declared in
1766  * lustre_idl.h implemented here.
1767  */
1768 void lustre_swab_ptlrpc_body(struct ptlrpc_body *body)
1769 {
1770         __swab32s(&body->pb_type);
1771         __swab32s(&body->pb_version);
1772         __swab32s(&body->pb_opc);
1773         __swab32s(&body->pb_status);
1774         __swab64s(&body->pb_last_xid);
1775         __swab16s(&body->pb_tag);
1776         BUILD_BUG_ON(offsetof(typeof(*body), pb_padding0) == 0);
1777         BUILD_BUG_ON(offsetof(typeof(*body), pb_padding1) == 0);
1778         __swab64s(&body->pb_last_committed);
1779         __swab64s(&body->pb_transno);
1780         __swab32s(&body->pb_flags);
1781         __swab32s(&body->pb_op_flags);
1782         __swab32s(&body->pb_conn_cnt);
1783         __swab32s(&body->pb_timeout);
1784         __swab32s(&body->pb_service_time);
1785         __swab32s(&body->pb_limit);
1786         __swab64s(&body->pb_slv);
1787         __swab64s(&body->pb_pre_versions[0]);
1788         __swab64s(&body->pb_pre_versions[1]);
1789         __swab64s(&body->pb_pre_versions[2]);
1790         __swab64s(&body->pb_pre_versions[3]);
1791         __swab64s(&body->pb_mbits);
1792         BUILD_BUG_ON(offsetof(typeof(*body), pb_padding64_0) == 0);
1793         BUILD_BUG_ON(offsetof(typeof(*body), pb_padding64_1) == 0);
1794         __swab32s(&body->pb_uid);
1795         __swab32s(&body->pb_gid);
1796         /*
1797          * While we need to maintain compatibility between
1798          * clients and servers without ptlrpc_body_v2 (< 2.3)
1799          * do not swab any fields beyond pb_jobid, as we are
1800          * using this swab function for both ptlrpc_body
1801          * and ptlrpc_body_v2.
1802          */
1803         /* pb_jobid is an ASCII string and should not be swabbed */
1804         BUILD_BUG_ON(offsetof(typeof(*body), pb_jobid) == 0);
1805 }
1806
1807 void lustre_swab_connect(struct obd_connect_data *ocd)
1808 {
1809         __swab64s(&ocd->ocd_connect_flags);
1810         __swab32s(&ocd->ocd_version);
1811         __swab32s(&ocd->ocd_grant);
1812         __swab64s(&ocd->ocd_ibits_known);
1813         __swab32s(&ocd->ocd_index);
1814         __swab32s(&ocd->ocd_brw_size);
1815         /*
1816          * ocd_blocksize and ocd_inodespace don't need to be swabbed because
1817          * they are 8-byte values
1818          */
1819         __swab16s(&ocd->ocd_grant_tax_kb);
1820         __swab32s(&ocd->ocd_grant_max_blks);
1821         __swab64s(&ocd->ocd_transno);
1822         __swab32s(&ocd->ocd_group);
1823         __swab32s(&ocd->ocd_cksum_types);
1824         __swab32s(&ocd->ocd_instance);
1825         /*
1826          * Fields after ocd_cksum_types are only accessible by the receiver
1827          * if the corresponding flag in ocd_connect_flags is set. Accessing
1828          * any field after ocd_maxbytes on the receiver without a valid flag
1829          * may result in out-of-bound memory access and kernel oops.
1830          */
1831         if (ocd->ocd_connect_flags & OBD_CONNECT_MAX_EASIZE)
1832                 __swab32s(&ocd->ocd_max_easize);
1833         if (ocd->ocd_connect_flags & OBD_CONNECT_MAXBYTES)
1834                 __swab64s(&ocd->ocd_maxbytes);
1835         if (ocd->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS)
1836                 __swab16s(&ocd->ocd_maxmodrpcs);
1837         BUILD_BUG_ON(offsetof(typeof(*ocd), padding0) == 0);
1838         BUILD_BUG_ON(offsetof(typeof(*ocd), padding1) == 0);
1839         if (ocd->ocd_connect_flags & OBD_CONNECT_FLAGS2) {
1840                 __swab64s(&ocd->ocd_connect_flags2);
1841                 if (ocd->ocd_connect_flags2 & OBD_CONNECT2_COMPRESS)
1842                         __swab64s(&ocd->ocd_compr_type);
1843         }
1844         BUILD_BUG_ON(offsetof(typeof(*ocd), padding4) == 0);
1845         BUILD_BUG_ON(offsetof(typeof(*ocd), padding5) == 0);
1846         BUILD_BUG_ON(offsetof(typeof(*ocd), padding6) == 0);
1847         BUILD_BUG_ON(offsetof(typeof(*ocd), padding7) == 0);
1848         BUILD_BUG_ON(offsetof(typeof(*ocd), padding8) == 0);
1849         BUILD_BUG_ON(offsetof(typeof(*ocd), padding9) == 0);
1850         BUILD_BUG_ON(offsetof(typeof(*ocd), paddingA) == 0);
1851         BUILD_BUG_ON(offsetof(typeof(*ocd), paddingB) == 0);
1852         BUILD_BUG_ON(offsetof(typeof(*ocd), paddingC) == 0);
1853         BUILD_BUG_ON(offsetof(typeof(*ocd), paddingD) == 0);
1854         BUILD_BUG_ON(offsetof(typeof(*ocd), paddingE) == 0);
1855         BUILD_BUG_ON(offsetof(typeof(*ocd), paddingF) == 0);
1856 }
1857
1858 static void lustre_swab_ost_layout(struct ost_layout *ol)
1859 {
1860         __swab32s(&ol->ol_stripe_size);
1861         __swab32s(&ol->ol_stripe_count);
1862         __swab64s(&ol->ol_comp_start);
1863         __swab64s(&ol->ol_comp_end);
1864         __swab32s(&ol->ol_comp_id);
1865 }
1866
1867 void lustre_swab_obdo(struct obdo *o)
1868 {
1869         __swab64s(&o->o_valid);
1870         lustre_swab_ost_id(&o->o_oi);
1871         __swab64s(&o->o_parent_seq);
1872         __swab64s(&o->o_size);
1873         __swab64s(&o->o_mtime);
1874         __swab64s(&o->o_atime);
1875         __swab64s(&o->o_ctime);
1876         __swab64s(&o->o_blocks);
1877         __swab64s(&o->o_grant);
1878         __swab32s(&o->o_blksize);
1879         __swab32s(&o->o_mode);
1880         __swab32s(&o->o_uid);
1881         __swab32s(&o->o_gid);
1882         __swab32s(&o->o_flags);
1883         __swab32s(&o->o_nlink);
1884         __swab32s(&o->o_parent_oid);
1885         __swab32s(&o->o_misc);
1886         __swab64s(&o->o_ioepoch);
1887         __swab32s(&o->o_stripe_idx);
1888         __swab32s(&o->o_parent_ver);
1889         lustre_swab_ost_layout(&o->o_layout);
1890         __swab32s(&o->o_layout_version);
1891         __swab32s(&o->o_uid_h);
1892         __swab32s(&o->o_gid_h);
1893         __swab64s(&o->o_data_version);
1894         __swab32s(&o->o_projid);
1895         BUILD_BUG_ON(offsetof(typeof(*o), o_padding_4) == 0);
1896         BUILD_BUG_ON(offsetof(typeof(*o), o_padding_5) == 0);
1897         BUILD_BUG_ON(offsetof(typeof(*o), o_padding_6) == 0);
1898
1899 }
1900 EXPORT_SYMBOL(lustre_swab_obdo);
1901
1902 void lustre_swab_obd_statfs(struct obd_statfs *os)
1903 {
1904         __swab64s(&os->os_type);
1905         __swab64s(&os->os_blocks);
1906         __swab64s(&os->os_bfree);
1907         __swab64s(&os->os_bavail);
1908         __swab64s(&os->os_files);
1909         __swab64s(&os->os_ffree);
1910         /* no need to swab os_fsid */
1911         __swab32s(&os->os_bsize);
1912         __swab32s(&os->os_namelen);
1913         __swab64s(&os->os_maxbytes);
1914         __swab32s(&os->os_state);
1915         __swab32s(&os->os_fprecreated);
1916         __swab32s(&os->os_granted);
1917         BUILD_BUG_ON(offsetof(typeof(*os), os_spare3) == 0);
1918         BUILD_BUG_ON(offsetof(typeof(*os), os_spare4) == 0);
1919         BUILD_BUG_ON(offsetof(typeof(*os), os_spare5) == 0);
1920         BUILD_BUG_ON(offsetof(typeof(*os), os_spare6) == 0);
1921         BUILD_BUG_ON(offsetof(typeof(*os), os_spare7) == 0);
1922         BUILD_BUG_ON(offsetof(typeof(*os), os_spare8) == 0);
1923         BUILD_BUG_ON(offsetof(typeof(*os), os_spare9) == 0);
1924 }
1925
1926 void lustre_swab_obd_ioobj(struct obd_ioobj *ioo)
1927 {
1928         lustre_swab_ost_id(&ioo->ioo_oid);
1929         __swab32s(&ioo->ioo_max_brw);
1930         __swab32s(&ioo->ioo_bufcnt);
1931 }
1932
1933 void lustre_swab_niobuf_remote(struct niobuf_remote *nbr)
1934 {
1935         __swab64s(&nbr->rnb_offset);
1936         __swab32s(&nbr->rnb_len);
1937         __swab32s(&nbr->rnb_flags);
1938 }
1939
1940 void lustre_swab_ost_body(struct ost_body *b)
1941 {
1942         lustre_swab_obdo(&b->oa);
1943 }
1944
1945 void lustre_swab_ost_last_id(u64 *id)
1946 {
1947         __swab64s(id);
1948 }
1949
1950 void lustre_swab_generic_32s(__u32 *val)
1951 {
1952         __swab32s(val);
1953 }
1954
1955 #ifdef HAVE_SERVER_SUPPORT
1956 void lustre_swab_gl_lquota_desc(struct ldlm_gl_lquota_desc *desc)
1957 {
1958         lustre_swab_lu_fid(&desc->gl_id.qid_fid);
1959         __swab64s(&desc->gl_flags);
1960         __swab64s(&desc->gl_ver);
1961         __swab64s(&desc->gl_hardlimit);
1962         __swab64s(&desc->gl_softlimit);
1963         __swab64s(&desc->gl_time);
1964         BUILD_BUG_ON(offsetof(typeof(*desc), gl_pad2) == 0);
1965 }
1966 EXPORT_SYMBOL(lustre_swab_gl_lquota_desc);
1967
1968 void lustre_swab_gl_barrier_desc(struct ldlm_gl_barrier_desc *desc)
1969 {
1970         __swab32s(&desc->lgbd_status);
1971         __swab32s(&desc->lgbd_timeout);
1972         BUILD_BUG_ON(offsetof(typeof(*desc), lgbd_padding) == 0);
1973 }
1974 EXPORT_SYMBOL(lustre_swab_gl_barrier_desc);
1975 #endif /* HAVE_SERVER_SUPPORT */
1976
1977 void lustre_swab_ost_lvb_v1(struct ost_lvb_v1 *lvb)
1978 {
1979         __swab64s(&lvb->lvb_size);
1980         __swab64s(&lvb->lvb_mtime);
1981         __swab64s(&lvb->lvb_atime);
1982         __swab64s(&lvb->lvb_ctime);
1983         __swab64s(&lvb->lvb_blocks);
1984 }
1985 EXPORT_SYMBOL(lustre_swab_ost_lvb_v1);
1986
1987 void lustre_swab_ost_lvb(struct ost_lvb *lvb)
1988 {
1989         __swab64s(&lvb->lvb_size);
1990         __swab64s(&lvb->lvb_mtime);
1991         __swab64s(&lvb->lvb_atime);
1992         __swab64s(&lvb->lvb_ctime);
1993         __swab64s(&lvb->lvb_blocks);
1994         __swab32s(&lvb->lvb_mtime_ns);
1995         __swab32s(&lvb->lvb_atime_ns);
1996         __swab32s(&lvb->lvb_ctime_ns);
1997         __swab32s(&lvb->lvb_padding);
1998 }
1999 EXPORT_SYMBOL(lustre_swab_ost_lvb);
2000
2001 void lustre_swab_lquota_lvb(struct lquota_lvb *lvb)
2002 {
2003         __swab64s(&lvb->lvb_flags);
2004         __swab64s(&lvb->lvb_id_may_rel);
2005         __swab64s(&lvb->lvb_id_rel);
2006         __swab64s(&lvb->lvb_id_qunit);
2007         __swab64s(&lvb->lvb_pad1);
2008 }
2009 EXPORT_SYMBOL(lustre_swab_lquota_lvb);
2010
2011 void lustre_swab_barrier_lvb(struct barrier_lvb *lvb)
2012 {
2013         __swab32s(&lvb->lvb_status);
2014         __swab32s(&lvb->lvb_index);
2015         BUILD_BUG_ON(offsetof(typeof(*lvb), lvb_padding) == 0);
2016 }
2017 EXPORT_SYMBOL(lustre_swab_barrier_lvb);
2018
2019 void lustre_swab_mdt_body(struct mdt_body *b)
2020 {
2021         lustre_swab_lu_fid(&b->mbo_fid1);
2022         lustre_swab_lu_fid(&b->mbo_fid2);
2023         /* handle is opaque */
2024         __swab64s(&b->mbo_valid);
2025         __swab64s(&b->mbo_size);
2026         __swab64s(&b->mbo_mtime);
2027         __swab64s(&b->mbo_atime);
2028         __swab64s(&b->mbo_ctime);
2029         __swab64s(&b->mbo_blocks);
2030         __swab64s(&b->mbo_version);
2031         __swab64s(&b->mbo_t_state);
2032         __swab32s(&b->mbo_fsuid);
2033         __swab32s(&b->mbo_fsgid);
2034         __swab32s(&b->mbo_capability);
2035         __swab32s(&b->mbo_mode);
2036         __swab32s(&b->mbo_uid);
2037         __swab32s(&b->mbo_gid);
2038         __swab32s(&b->mbo_flags);
2039         __swab32s(&b->mbo_rdev);
2040         __swab32s(&b->mbo_nlink);
2041         __swab32s(&b->mbo_layout_gen);
2042         __swab32s(&b->mbo_suppgid);
2043         __swab32s(&b->mbo_eadatasize);
2044         __swab32s(&b->mbo_aclsize);
2045         __swab32s(&b->mbo_max_mdsize);
2046         BUILD_BUG_ON(offsetof(typeof(*b), mbo_unused3) == 0);
2047         __swab32s(&b->mbo_uid_h);
2048         __swab32s(&b->mbo_gid_h);
2049         __swab32s(&b->mbo_projid);
2050         __swab64s(&b->mbo_dom_size);
2051         __swab64s(&b->mbo_dom_blocks);
2052         __swab64s(&b->mbo_btime);
2053         BUILD_BUG_ON(offsetof(typeof(*b), mbo_padding_9) == 0);
2054         BUILD_BUG_ON(offsetof(typeof(*b), mbo_padding_10) == 0);
2055 }
2056
2057 void lustre_swab_mdt_ioepoch(struct mdt_ioepoch *b)
2058 {
2059         /* mio_open_handle is opaque */
2060         BUILD_BUG_ON(offsetof(typeof(*b), mio_unused1) == 0);
2061         BUILD_BUG_ON(offsetof(typeof(*b), mio_unused2) == 0);
2062         BUILD_BUG_ON(offsetof(typeof(*b), mio_padding) == 0);
2063 }
2064
2065 void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
2066 {
2067         int i;
2068
2069         __swab32s(&mti->mti_lustre_ver);
2070         __swab32s(&mti->mti_stripe_index);
2071         __swab32s(&mti->mti_config_ver);
2072         __swab32s(&mti->mti_flags);
2073         __swab32s(&mti->mti_instance);
2074         __swab32s(&mti->mti_nid_count);
2075         BUILD_BUG_ON(sizeof(lnet_nid_t) != sizeof(u64));
2076
2077         /* For NID string we never need to swab */
2078         if (target_supports_large_nid(mti))
2079                 return;
2080
2081         for (i = 0; i < MTI_NIDS_MAX; i++)
2082                 __swab64s(&mti->mti_nids[i]);
2083 }
2084
2085 void lustre_swab_mgs_nidtbl_entry_header(struct mgs_nidtbl_entry *entry)
2086 {
2087         __swab64s(&entry->mne_version);
2088         __swab32s(&entry->mne_instance);
2089         __swab32s(&entry->mne_index);
2090         __swab32s(&entry->mne_length);
2091
2092         /* mne_nid_(count|type) must be one byte size because we're going to
2093          * access it w/o swapping.
2094          */
2095         BUILD_BUG_ON(sizeof(entry->mne_nid_count) != sizeof(u8));
2096         BUILD_BUG_ON(sizeof(entry->mne_nid_type) != sizeof(u8));
2097
2098 }
2099 EXPORT_SYMBOL(lustre_swab_mgs_nidtbl_entry_header);
2100
2101 void lustre_swab_mgs_nidtbl_entry_content(struct mgs_nidtbl_entry *entry)
2102 {
2103         int i;
2104
2105         /* Large NIDs are always big endian so we don't need swapping */
2106         if (entry->mne_nid_type)
2107                 return;
2108
2109         for (i = 0; i < entry->mne_nid_count; i++)
2110                 __swab64s(&entry->u.nids[i]);
2111 }
2112 EXPORT_SYMBOL(lustre_swab_mgs_nidtbl_entry_content);
2113
2114 void lustre_swab_mgs_config_body(struct mgs_config_body *body)
2115 {
2116         __swab64s(&body->mcb_offset);
2117         __swab32s(&body->mcb_units);
2118         __swab16s(&body->mcb_type);
2119 }
2120
2121 void lustre_swab_mgs_config_res(struct mgs_config_res *body)
2122 {
2123         __swab64s(&body->mcr_offset);
2124         __swab64s(&body->mcr_size);
2125 }
2126
2127 static void lustre_swab_obd_dqinfo(struct obd_dqinfo *i)
2128 {
2129         __swab64s(&i->dqi_bgrace);
2130         __swab64s(&i->dqi_igrace);
2131         __swab32s(&i->dqi_flags);
2132         __swab32s(&i->dqi_valid);
2133 }
2134
2135 static void lustre_swab_obd_dqblk(struct obd_dqblk *b)
2136 {
2137         __swab64s(&b->dqb_ihardlimit);
2138         __swab64s(&b->dqb_isoftlimit);
2139         __swab64s(&b->dqb_curinodes);
2140         __swab64s(&b->dqb_bhardlimit);
2141         __swab64s(&b->dqb_bsoftlimit);
2142         __swab64s(&b->dqb_curspace);
2143         __swab64s(&b->dqb_btime);
2144         __swab64s(&b->dqb_itime);
2145         __swab32s(&b->dqb_valid);
2146         BUILD_BUG_ON(offsetof(typeof(*b), dqb_padding) == 0);
2147 }
2148
2149 int lustre_swab_obd_quotactl(struct obd_quotactl *q, __u32 len)
2150 {
2151         if (unlikely(len <= sizeof(struct obd_quotactl)))
2152                 return -EOVERFLOW;
2153
2154         __swab32s(&q->qc_cmd);
2155         __swab32s(&q->qc_type);
2156         __swab32s(&q->qc_id);
2157         __swab32s(&q->qc_stat);
2158         lustre_swab_obd_dqinfo(&q->qc_dqinfo);
2159         lustre_swab_obd_dqblk(&q->qc_dqblk);
2160
2161         return len;
2162 }
2163
2164 void lustre_swab_fid2path(struct getinfo_fid2path *gf)
2165 {
2166         lustre_swab_lu_fid(&gf->gf_fid);
2167         __swab64s(&gf->gf_recno);
2168         __swab32s(&gf->gf_linkno);
2169         __swab32s(&gf->gf_pathlen);
2170 }
2171 EXPORT_SYMBOL(lustre_swab_fid2path);
2172
2173 static void lustre_swab_fiemap_extent(struct fiemap_extent *fm_extent)
2174 {
2175         __swab64s(&fm_extent->fe_logical);
2176         __swab64s(&fm_extent->fe_physical);
2177         __swab64s(&fm_extent->fe_length);
2178         __swab32s(&fm_extent->fe_flags);
2179         __swab32s(&fm_extent->fe_device);
2180 }
2181
2182 static void lustre_swab_fiemap_hdr(struct fiemap *fiemap)
2183 {
2184         __swab64s(&fiemap->fm_start);
2185         __swab64s(&fiemap->fm_length);
2186         __swab32s(&fiemap->fm_flags);
2187         __swab32s(&fiemap->fm_mapped_extents);
2188         __swab32s(&fiemap->fm_extent_count);
2189         __swab32s(&fiemap->fm_reserved);
2190 }
2191
2192 int lustre_swab_fiemap(struct fiemap *fiemap, __u32 len)
2193 {
2194         __u32 i, size, count;
2195
2196         lustre_swab_fiemap_hdr(fiemap);
2197
2198         size = fiemap_count_to_size(fiemap->fm_mapped_extents);
2199         count = fiemap->fm_mapped_extents;
2200         if (unlikely(size > len)) {
2201                 count = (len - sizeof(struct fiemap)) /
2202                         sizeof(struct fiemap_extent);
2203                 fiemap->fm_mapped_extents = count;
2204                 size = -EOVERFLOW;
2205         }
2206         /* still swab extents as we cannot yet pass rc to callers */
2207         for (i = 0; i < count; i++)
2208                 lustre_swab_fiemap_extent(&fiemap->fm_extents[i]);
2209
2210         return size;
2211 }
2212
2213 void lustre_swab_fiemap_info_key(struct ll_fiemap_info_key *fiemap_info)
2214 {
2215         lustre_swab_obdo(&fiemap_info->lfik_oa);
2216         lustre_swab_fiemap_hdr(&fiemap_info->lfik_fiemap);
2217 }
2218
2219 void lustre_swab_idx_info(struct idx_info *ii)
2220 {
2221         __swab32s(&ii->ii_magic);
2222         __swab32s(&ii->ii_flags);
2223         __swab16s(&ii->ii_count);
2224         __swab32s(&ii->ii_attrs);
2225         lustre_swab_lu_fid(&ii->ii_fid);
2226         __swab64s(&ii->ii_version);
2227         __swab64s(&ii->ii_hash_start);
2228         __swab64s(&ii->ii_hash_end);
2229         __swab16s(&ii->ii_keysize);
2230         __swab16s(&ii->ii_recsize);
2231 }
2232
2233 void lustre_swab_lip_header(struct lu_idxpage *lip)
2234 {
2235         /* swab header */
2236         __swab32s(&lip->lip_magic);
2237         __swab16s(&lip->lip_flags);
2238         __swab16s(&lip->lip_nr);
2239 }
2240 EXPORT_SYMBOL(lustre_swab_lip_header);
2241
2242 void lustre_swab_mdt_rec_reint (struct mdt_rec_reint *rr)
2243 {
2244         __swab32s(&rr->rr_opcode);
2245         __swab32s(&rr->rr_cap);
2246         __swab32s(&rr->rr_fsuid);
2247         /* rr_fsuid_h is unused */
2248         __swab32s(&rr->rr_fsgid);
2249         /* rr_fsgid_h is unused */
2250         __swab32s(&rr->rr_suppgid1);
2251         /* rr_suppgid1_h is unused */
2252         __swab32s(&rr->rr_suppgid2);
2253         /* rr_suppgid2_h is unused */
2254         lustre_swab_lu_fid(&rr->rr_fid1);
2255         lustre_swab_lu_fid(&rr->rr_fid2);
2256         __swab64s(&rr->rr_mtime);
2257         __swab64s(&rr->rr_atime);
2258         __swab64s(&rr->rr_ctime);
2259         __swab64s(&rr->rr_size);
2260         __swab64s(&rr->rr_blocks);
2261         __swab32s(&rr->rr_bias);
2262         __swab32s(&rr->rr_mode);
2263         __swab32s(&rr->rr_flags);
2264         __swab32s(&rr->rr_flags_h);
2265         __swab32s(&rr->rr_umask);
2266         __swab16s(&rr->rr_mirror_id);
2267
2268         BUILD_BUG_ON(offsetof(typeof(*rr), rr_padding_4) == 0);
2269 };
2270
2271 void lustre_swab_lov_desc(struct lov_desc *ld)
2272 {
2273         __swab32s(&ld->ld_tgt_count);
2274         __swab32s(&ld->ld_active_tgt_count);
2275         __swab32s(&ld->ld_default_stripe_count);
2276         __swab32s(&ld->ld_pattern);
2277         __swab64s(&ld->ld_default_stripe_size);
2278         __swab64s(&ld->ld_default_stripe_offset);
2279         __swab32s(&ld->ld_qos_maxage);
2280         /* uuid endian insensitive */
2281 }
2282 EXPORT_SYMBOL(lustre_swab_lov_desc);
2283
2284 void lustre_swab_lmv_desc(struct lmv_desc *ld)
2285 {
2286         __swab32s(&ld->ld_tgt_count);
2287         __swab32s(&ld->ld_active_tgt_count);
2288         __swab32s(&ld->ld_default_stripe_count);
2289         __swab32s(&ld->ld_pattern);
2290         __swab64s(&ld->ld_default_hash_size);
2291         __swab32s(&ld->ld_qos_maxage);
2292         /* uuid endian insensitive */
2293 }
2294
2295 /* This structure is always in little-endian */
2296 static void lustre_swab_lmv_mds_md_v1(struct lmv_mds_md_v1 *lmm1)
2297 {
2298         int i;
2299
2300         __swab32s(&lmm1->lmv_magic);
2301         __swab32s(&lmm1->lmv_stripe_count);
2302         __swab32s(&lmm1->lmv_master_mdt_index);
2303         __swab32s(&lmm1->lmv_hash_type);
2304         __swab32s(&lmm1->lmv_layout_version);
2305         for (i = 0; i < lmm1->lmv_stripe_count; i++)
2306                 lustre_swab_lu_fid(&lmm1->lmv_stripe_fids[i]);
2307 }
2308
2309 void lustre_swab_lmv_mds_md(union lmv_mds_md *lmm)
2310 {
2311         switch (lmm->lmv_magic) {
2312         case LMV_MAGIC_V1:
2313                 lustre_swab_lmv_mds_md_v1(&lmm->lmv_md_v1);
2314                 break;
2315         default:
2316                 break;
2317         }
2318 }
2319 EXPORT_SYMBOL(lustre_swab_lmv_mds_md);
2320
2321 void lustre_swab_lmv_user_md_objects(struct lmv_user_mds_data *lmd,
2322                                      int stripe_count)
2323 {
2324         int i;
2325
2326         for (i = 0; i < stripe_count; i++)
2327                 __swab32s(&(lmd[i].lum_mds));
2328 }
2329 EXPORT_SYMBOL(lustre_swab_lmv_user_md_objects);
2330
2331
2332 void lustre_swab_lmv_user_md(struct lmv_user_md *lum)
2333 {
2334         __u32 count;
2335
2336         if (lum->lum_magic == LMV_MAGIC_FOREIGN) {
2337                 __swab32s(&lum->lum_magic);
2338                 __swab32s(&((struct lmv_foreign_md *)lum)->lfm_length);
2339                 __swab32s(&((struct lmv_foreign_md *)lum)->lfm_type);
2340                 __swab32s(&((struct lmv_foreign_md *)lum)->lfm_flags);
2341                 return;
2342         }
2343
2344         count = lum->lum_stripe_count;
2345         __swab32s(&lum->lum_magic);
2346         __swab32s(&lum->lum_stripe_count);
2347         __swab32s(&lum->lum_stripe_offset);
2348         __swab32s(&lum->lum_hash_type);
2349         __swab32s(&lum->lum_type);
2350         /* lum_max_inherit and lum_max_inherit_rr do not need to be swabbed */
2351         BUILD_BUG_ON(offsetof(typeof(*lum), lum_padding1) == 0);
2352         BUILD_BUG_ON(offsetof(typeof(*lum), lum_padding2) == 0);
2353         BUILD_BUG_ON(offsetof(typeof(*lum), lum_padding3) == 0);
2354         switch (lum->lum_magic) {
2355         case LMV_USER_MAGIC_SPECIFIC:
2356                 count = lum->lum_stripe_count;
2357                 fallthrough;
2358         case __swab32(LMV_USER_MAGIC_SPECIFIC):
2359                 lustre_swab_lmv_user_md_objects(lum->lum_objects, count);
2360                 break;
2361         default:
2362                 break;
2363         }
2364 }
2365 EXPORT_SYMBOL(lustre_swab_lmv_user_md);
2366
2367 static void lustre_print_v1v3(unsigned int lvl, struct lov_user_md *lum,
2368                               const char *msg)
2369 {
2370         CDEBUG(lvl, "%s lov_user_md %p:\n", msg, lum);
2371         CDEBUG(lvl, "\tlmm_magic: %#x\n", lum->lmm_magic);
2372         CDEBUG(lvl, "\tlmm_pattern: %#x\n", lum->lmm_pattern);
2373         CDEBUG(lvl, "\tlmm_object_id: %llu\n", lmm_oi_id(&lum->lmm_oi));
2374         CDEBUG(lvl, "\tlmm_object_gr: %llu\n", lmm_oi_seq(&lum->lmm_oi));
2375         CDEBUG(lvl, "\tlmm_stripe_size: %#x\n", lum->lmm_stripe_size);
2376         CDEBUG(lvl, "\tlmm_stripe_count: %#x\n", lum->lmm_stripe_count);
2377         CDEBUG(lvl, "\tlmm_stripe_offset/lmm_layout_gen: %#x\n",
2378                lum->lmm_stripe_offset);
2379         if (lum->lmm_magic == LOV_USER_MAGIC_V3) {
2380                 struct lov_user_md_v3 *v3 = (void *)lum;
2381                 CDEBUG(lvl, "\tlmm_pool_name: %s\n", v3->lmm_pool_name);
2382         }
2383         if (lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2384                 struct lov_user_md_v3 *v3 = (void *)lum;
2385                 int i;
2386
2387                 if (v3->lmm_pool_name[0] != '\0')
2388                         CDEBUG(lvl, "\tlmm_pool_name: %s\n", v3->lmm_pool_name);
2389
2390                 CDEBUG(lvl, "\ttarget list:\n");
2391                 for (i = 0; i < v3->lmm_stripe_count; i++)
2392                         CDEBUG(lvl, "\t\t%u\n", v3->lmm_objects[i].l_ost_idx);
2393         }
2394 }
2395
2396 static void lustre_print_foreign(unsigned int lvl, struct lov_foreign_md *lfm,
2397                                  const char *msg)
2398 {
2399         CDEBUG(lvl, "%s lov_foreign_md %p:\n", msg, lfm);
2400         CDEBUG(lvl, "\tlfm_magic: %#X\n", lfm->lfm_magic);
2401         CDEBUG(lvl, "\tlfm_length: %u\n", lfm->lfm_length);
2402         CDEBUG(lvl, "\tlfm_type: %#X\n", lfm->lfm_type);
2403         CDEBUG(lvl, "\tlfm_flags: %#X\n", lfm->lfm_flags);
2404 }
2405
2406 void lustre_print_user_md(unsigned int lvl, struct lov_user_md *lum,
2407                           const char *msg)
2408 {
2409         struct lov_comp_md_v1   *comp_v1;
2410         int                      i;
2411
2412         if (likely(!cfs_cdebug_show(lvl, DEBUG_SUBSYSTEM)))
2413                 return;
2414
2415         if (lum->lmm_magic == LOV_USER_MAGIC_V1 ||
2416             lum->lmm_magic == LOV_USER_MAGIC_V3) {
2417                 lustre_print_v1v3(lvl, lum, msg);
2418                 return;
2419         }
2420
2421         if (lum->lmm_magic != LOV_USER_MAGIC_COMP_V1) {
2422                 CDEBUG(lvl, "%s: bad magic: %x\n", msg, lum->lmm_magic);
2423                 return;
2424         }
2425
2426         comp_v1 = (struct lov_comp_md_v1 *)lum;
2427         CDEBUG(lvl, "%s: lov_comp_md_v1 %p:\n", msg, lum);
2428         CDEBUG(lvl, "\tlcm_magic: %#x\n", comp_v1->lcm_magic);
2429         CDEBUG(lvl, "\tlcm_size: %#x\n", comp_v1->lcm_size);
2430         CDEBUG(lvl, "\tlcm_layout_gen: %#x\n", comp_v1->lcm_layout_gen);
2431         CDEBUG(lvl, "\tlcm_flags: %#x\n", comp_v1->lcm_flags);
2432         CDEBUG(lvl, "\tlcm_entry_count: %#x\n\n", comp_v1->lcm_entry_count);
2433         CDEBUG(lvl, "\tlcm_mirror_count: %#x\n\n", comp_v1->lcm_mirror_count);
2434
2435         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2436                 struct lov_comp_md_entry_v1 *ent = &comp_v1->lcm_entries[i];
2437                 struct lov_user_md *v1;
2438
2439                 CDEBUG(lvl, "\tentry %d:\n", i);
2440                 CDEBUG(lvl, "\tlcme_id: %#x\n", ent->lcme_id);
2441                 CDEBUG(lvl, "\tlcme_flags: %#x\n", ent->lcme_flags);
2442                 if (ent->lcme_flags & LCME_FL_NOSYNC)
2443                         CDEBUG(lvl, "\tlcme_timestamp: %llu\n",
2444                                         ent->lcme_timestamp);
2445                 CDEBUG(lvl, "\tlcme_extent.e_start: %llu\n",
2446                        ent->lcme_extent.e_start);
2447                 CDEBUG(lvl, "\tlcme_extent.e_end: %llu\n",
2448                        ent->lcme_extent.e_end);
2449                 CDEBUG(lvl, "\tlcme_offset: %#x\n", ent->lcme_offset);
2450                 CDEBUG(lvl, "\tlcme_size: %#x\n\n", ent->lcme_size);
2451
2452                 v1 = (struct lov_user_md *)((char *)comp_v1 +
2453                                 comp_v1->lcm_entries[i].lcme_offset);
2454                 if (v1->lmm_magic == LOV_MAGIC_FOREIGN)
2455                         lustre_print_foreign(lvl, (struct lov_foreign_md *)v1,
2456                                              msg);
2457                 else
2458                         lustre_print_v1v3(lvl, v1, msg);
2459         }
2460 }
2461 EXPORT_SYMBOL(lustre_print_user_md);
2462
2463 static void lustre_swab_lmm_oi(struct ost_id *oi)
2464 {
2465         __swab64s(&oi->oi.oi_id);
2466         __swab64s(&oi->oi.oi_seq);
2467 }
2468
2469 static void lustre_swab_lov_user_md_common(struct lov_user_md_v1 *lum)
2470 {
2471         ENTRY;
2472         __swab32s(&lum->lmm_magic);
2473         __swab32s(&lum->lmm_pattern);
2474         lustre_swab_lmm_oi(&lum->lmm_oi);
2475         __swab32s(&lum->lmm_stripe_size);
2476         __swab16s(&lum->lmm_stripe_count);
2477         __swab16s(&lum->lmm_stripe_offset);
2478         EXIT;
2479 }
2480
2481 void lustre_swab_lov_user_md_v1(struct lov_user_md_v1 *lum)
2482 {
2483         ENTRY;
2484         CDEBUG(D_IOCTL, "swabbing lov_user_md v1\n");
2485         lustre_swab_lov_user_md_common(lum);
2486         EXIT;
2487 }
2488 EXPORT_SYMBOL(lustre_swab_lov_user_md_v1);
2489
2490 void lustre_swab_lov_user_md_v3(struct lov_user_md_v3 *lum)
2491 {
2492         ENTRY;
2493         CDEBUG(D_IOCTL, "swabbing lov_user_md v3\n");
2494         lustre_swab_lov_user_md_common((struct lov_user_md_v1 *)lum);
2495         /* lmm_pool_name nothing to do with char */
2496         EXIT;
2497 }
2498 EXPORT_SYMBOL(lustre_swab_lov_user_md_v3);
2499
2500 static void lustre_swab_lov_hsm_md(struct lov_hsm_md *lhm)
2501 {
2502         ENTRY;
2503         CDEBUG(D_IOCTL, "swabbing lov_hsm_md\n");
2504         __swab32s(&lhm->lhm_magic);
2505         __swab32s(&lhm->lhm_length);
2506         __swab32s(&lhm->lhm_type);
2507         __swab32s(&lhm->lhm_flags);
2508
2509         if (lov_hsm_type_supported(lhm->lhm_type)) {
2510                 __swab64s(&lhm->lhm_archive_id);
2511                 __swab64s(&lhm->lhm_archive_ver);
2512         }
2513         EXIT;
2514 }
2515
2516 void lustre_swab_lov_comp_md_v1(struct lov_comp_md_v1 *lum)
2517 {
2518         struct lov_comp_md_entry_v1     *ent;
2519         struct lov_user_md_v1   *v1;
2520         struct lov_user_md_v3   *v3;
2521         int     i;
2522         bool    cpu_endian;
2523         __u32   off, size;
2524         __u16   ent_count, stripe_count;
2525         ENTRY;
2526
2527         cpu_endian = lum->lcm_magic == LOV_USER_MAGIC_COMP_V1;
2528         ent_count = lum->lcm_entry_count;
2529         if (!cpu_endian)
2530                 __swab16s(&ent_count);
2531
2532         CDEBUG(D_IOCTL, "swabbing lov_user_comp_md v1\n");
2533         __swab32s(&lum->lcm_magic);
2534         __swab32s(&lum->lcm_size);
2535         __swab32s(&lum->lcm_layout_gen);
2536         __swab16s(&lum->lcm_flags);
2537         __swab16s(&lum->lcm_entry_count);
2538         __swab16s(&lum->lcm_mirror_count);
2539         /* no need to swab lcm_ec_count */
2540         BUILD_BUG_ON(offsetof(typeof(*lum), lcm_padding1) == 0);
2541         BUILD_BUG_ON(offsetof(typeof(*lum), lcm_padding2) == 0);
2542         BUILD_BUG_ON(offsetof(typeof(*lum), lcm_padding3) == 0);
2543
2544         for (i = 0; i < ent_count; i++) {
2545                 ent = &lum->lcm_entries[i];
2546                 off = ent->lcme_offset;
2547                 size = ent->lcme_size;
2548
2549                 if (!cpu_endian) {
2550                         __swab32s(&off);
2551                         __swab32s(&size);
2552                 }
2553                 __swab32s(&ent->lcme_id);
2554                 __swab32s(&ent->lcme_flags);
2555                 __swab64s(&ent->lcme_timestamp);
2556                 __swab64s(&ent->lcme_extent.e_start);
2557                 __swab64s(&ent->lcme_extent.e_end);
2558                 __swab32s(&ent->lcme_offset);
2559                 __swab32s(&ent->lcme_size);
2560                 __swab32s(&ent->lcme_layout_gen);
2561                 /* no need to swab lcme_dstripe_count */
2562                 /* no need to swab lcme_cstripe_count */
2563
2564                 v1 = (struct lov_user_md_v1 *)((char *)lum + off);
2565                 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_FOREIGN) ||
2566                     v1->lmm_magic == LOV_USER_MAGIC_FOREIGN) {
2567                         lustre_swab_lov_hsm_md((struct lov_hsm_md *)v1);
2568                         return;
2569                 }
2570
2571                 stripe_count = v1->lmm_stripe_count;
2572                 if (!cpu_endian)
2573                         __swab16s(&stripe_count);
2574
2575                 if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1) ||
2576                     v1->lmm_magic == LOV_USER_MAGIC_V1) {
2577                         lustre_swab_lov_user_md_v1(v1);
2578                         if (size > sizeof(*v1))
2579                                 lustre_swab_lov_user_md_objects(v1->lmm_objects,
2580                                                                 stripe_count);
2581                 } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3) ||
2582                            v1->lmm_magic == LOV_USER_MAGIC_V3 ||
2583                            v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC) ||
2584                            v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
2585                         v3 = (struct lov_user_md_v3 *)v1;
2586                         lustre_swab_lov_user_md_v3(v3);
2587                         if (size > sizeof(*v3))
2588                                 lustre_swab_lov_user_md_objects(v3->lmm_objects,
2589                                                                 stripe_count);
2590                 } else {
2591                         CERROR("Invalid magic %#x\n", v1->lmm_magic);
2592                 }
2593         }
2594 }
2595 EXPORT_SYMBOL(lustre_swab_lov_comp_md_v1);
2596
2597 void lustre_swab_lov_user_md_objects(struct lov_user_ost_data *lod,
2598                                      int stripe_count)
2599 {
2600         int i;
2601
2602         ENTRY;
2603         for (i = 0; i < stripe_count; i++) {
2604                 lustre_swab_ost_id(&(lod[i].l_ost_oi));
2605                 __swab32s(&(lod[i].l_ost_gen));
2606                 __swab32s(&(lod[i].l_ost_idx));
2607         }
2608         EXIT;
2609 }
2610 EXPORT_SYMBOL(lustre_swab_lov_user_md_objects);
2611
2612 void lustre_swab_lov_user_md(struct lov_user_md *lum, size_t size)
2613 {
2614         struct lov_user_md_v1 *v1;
2615         struct lov_user_md_v3 *v3;
2616         struct lov_foreign_md *lfm;
2617         __u16 stripe_count;
2618         ENTRY;
2619
2620         CDEBUG(D_IOCTL, "swabbing lov_user_md\n");
2621         switch (lum->lmm_magic) {
2622         case __swab32(LOV_MAGIC_V1):
2623         case LOV_USER_MAGIC_V1:
2624         {
2625                 v1 = (struct lov_user_md_v1 *)lum;
2626                 stripe_count = v1->lmm_stripe_count;
2627
2628                 if (lum->lmm_magic != LOV_USER_MAGIC_V1)
2629                         __swab16s(&stripe_count);
2630
2631                 lustre_swab_lov_user_md_v1(v1);
2632                 if (size > sizeof(*v1))
2633                         lustre_swab_lov_user_md_objects(v1->lmm_objects,
2634                                                         stripe_count);
2635
2636                 break;
2637         }
2638         case __swab32(LOV_MAGIC_V3):
2639         case LOV_USER_MAGIC_V3:
2640         {
2641                 v3 = (struct lov_user_md_v3 *)lum;
2642                 stripe_count = v3->lmm_stripe_count;
2643
2644                 if (lum->lmm_magic != LOV_USER_MAGIC_V3)
2645                         __swab16s(&stripe_count);
2646
2647                 lustre_swab_lov_user_md_v3(v3);
2648                 if (size > sizeof(*v3))
2649                         lustre_swab_lov_user_md_objects(v3->lmm_objects,
2650                                                         stripe_count);
2651                 break;
2652         }
2653         case __swab32(LOV_USER_MAGIC_SPECIFIC):
2654         case LOV_USER_MAGIC_SPECIFIC:
2655         {
2656                 v3 = (struct lov_user_md_v3 *)lum;
2657                 stripe_count = v3->lmm_stripe_count;
2658
2659                 if (lum->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
2660                         __swab16s(&stripe_count);
2661
2662                 lustre_swab_lov_user_md_v3(v3);
2663                 lustre_swab_lov_user_md_objects(v3->lmm_objects, stripe_count);
2664                 break;
2665         }
2666         case __swab32(LOV_MAGIC_COMP_V1):
2667         case LOV_USER_MAGIC_COMP_V1:
2668                 lustre_swab_lov_comp_md_v1((struct lov_comp_md_v1 *)lum);
2669                 break;
2670         case __swab32(LOV_MAGIC_FOREIGN):
2671         case LOV_USER_MAGIC_FOREIGN:
2672         {
2673                 lfm = (struct lov_foreign_md *)lum;
2674                 __swab32s(&lfm->lfm_magic);
2675                 __swab32s(&lfm->lfm_length);
2676                 __swab32s(&lfm->lfm_type);
2677                 __swab32s(&lfm->lfm_flags);
2678                 break;
2679         }
2680         default:
2681                 CDEBUG(D_IOCTL, "Invalid LOV magic %08x\n", lum->lmm_magic);
2682         }
2683 }
2684 EXPORT_SYMBOL(lustre_swab_lov_user_md);
2685
2686 void lustre_swab_lov_mds_md(struct lov_mds_md *lmm)
2687 {
2688         ENTRY;
2689         CDEBUG(D_IOCTL, "swabbing lov_mds_md\n");
2690         __swab32s(&lmm->lmm_magic);
2691         __swab32s(&lmm->lmm_pattern);
2692         lustre_swab_lmm_oi(&lmm->lmm_oi);
2693         __swab32s(&lmm->lmm_stripe_size);
2694         __swab16s(&lmm->lmm_stripe_count);
2695         __swab16s(&lmm->lmm_layout_gen);
2696         EXIT;
2697 }
2698 EXPORT_SYMBOL(lustre_swab_lov_mds_md);
2699
2700 void lustre_swab_ldlm_res_id(struct ldlm_res_id *id)
2701 {
2702         int i;
2703
2704         for (i = 0; i < RES_NAME_SIZE; i++)
2705                 __swab64s(&id->name[i]);
2706 }
2707
2708 void lustre_swab_ldlm_policy_data(union ldlm_wire_policy_data *d)
2709 {
2710         /* the lock data is a union and the first two fields are always an
2711          * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
2712          * data the same way.
2713          */
2714         __swab64s(&d->l_extent.start);
2715         __swab64s(&d->l_extent.end);
2716         __swab64s(&d->l_extent.gid);
2717         __swab32s(&d->l_flock.lfw_padding);
2718         __swab32s(&d->l_flock.lfw_pid);
2719 }
2720
2721 void lustre_swab_ldlm_intent(struct ldlm_intent *i)
2722 {
2723         __swab64s(&i->opc);
2724 }
2725
2726 void lustre_swab_ldlm_resource_desc(struct ldlm_resource_desc *r)
2727 {
2728         __swab32s(&r->lr_type);
2729         BUILD_BUG_ON(offsetof(typeof(*r), lr_pad) == 0);
2730         lustre_swab_ldlm_res_id(&r->lr_name);
2731 }
2732
2733 void lustre_swab_ldlm_lock_desc(struct ldlm_lock_desc *l)
2734 {
2735         lustre_swab_ldlm_resource_desc(&l->l_resource);
2736         __swab32s(&l->l_req_mode);
2737         __swab32s(&l->l_granted_mode);
2738         lustre_swab_ldlm_policy_data(&l->l_policy_data);
2739 }
2740
2741 void lustre_swab_ldlm_request(struct ldlm_request *rq)
2742 {
2743         __swab32s(&rq->lock_flags);
2744         lustre_swab_ldlm_lock_desc(&rq->lock_desc);
2745         __swab32s(&rq->lock_count);
2746         /* lock_handle[] opaque */
2747 }
2748
2749 void lustre_swab_ldlm_reply(struct ldlm_reply *r)
2750 {
2751         __swab32s(&r->lock_flags);
2752         BUILD_BUG_ON(offsetof(typeof(*r), lock_padding) == 0);
2753         lustre_swab_ldlm_lock_desc(&r->lock_desc);
2754         /* lock_handle opaque */
2755         __swab64s(&r->lock_policy_res1);
2756         __swab64s(&r->lock_policy_res2);
2757 }
2758
2759 void lustre_swab_quota_body(struct quota_body *b)
2760 {
2761         lustre_swab_lu_fid(&b->qb_fid);
2762         lustre_swab_lu_fid((struct lu_fid *)&b->qb_id);
2763         __swab32s(&b->qb_flags);
2764         __swab64s(&b->qb_count);
2765         __swab64s(&b->qb_usage);
2766         __swab64s(&b->qb_slv_ver);
2767 }
2768
2769 /* Dump functions */
2770 void dump_ioo(struct obd_ioobj *ioo)
2771 {
2772         CDEBUG(D_RPCTRACE,
2773                "obd_ioobj: ioo_oid="DOSTID", ioo_max_brw=%#x, "
2774                "ioo_bufct=%d\n", POSTID(&ioo->ioo_oid), ioo->ioo_max_brw,
2775                ioo->ioo_bufcnt);
2776 }
2777
2778 void dump_rniobuf(struct niobuf_remote *nb)
2779 {
2780         CDEBUG(D_RPCTRACE, "niobuf_remote: offset=%llu, len=%d, flags=%x\n",
2781                nb->rnb_offset, nb->rnb_len, nb->rnb_flags);
2782 }
2783
2784 static void dump_obdo(struct obdo *oa)
2785 {
2786         u64 valid = oa->o_valid;
2787
2788         CDEBUG(D_RPCTRACE, "obdo: o_valid = %#llx\n", valid);
2789         if (valid & OBD_MD_FLID)
2790                 CDEBUG(D_RPCTRACE, "obdo: id = "DOSTID"\n", POSTID(&oa->o_oi));
2791         if (valid & OBD_MD_FLFID)
2792                 CDEBUG(D_RPCTRACE, "obdo: o_parent_seq = %#llx\n",
2793                        oa->o_parent_seq);
2794         if (valid & OBD_MD_FLSIZE)
2795                 CDEBUG(D_RPCTRACE, "obdo: o_size = %lld\n", oa->o_size);
2796         if (valid & OBD_MD_FLMTIME)
2797                 CDEBUG(D_RPCTRACE, "obdo: o_mtime = %lld\n", oa->o_mtime);
2798         if (valid & OBD_MD_FLATIME)
2799                 CDEBUG(D_RPCTRACE, "obdo: o_atime = %lld\n", oa->o_atime);
2800         if (valid & OBD_MD_FLCTIME)
2801                 CDEBUG(D_RPCTRACE, "obdo: o_ctime = %lld\n", oa->o_ctime);
2802         if (valid & OBD_MD_FLBLOCKS)   /* allocation of space */
2803                 CDEBUG(D_RPCTRACE, "obdo: o_blocks = %lld\n", oa->o_blocks);
2804         if (valid & OBD_MD_FLGRANT)
2805                 CDEBUG(D_RPCTRACE, "obdo: o_grant = %lld\n", oa->o_grant);
2806         if (valid & OBD_MD_FLBLKSZ)
2807                 CDEBUG(D_RPCTRACE, "obdo: o_blksize = %d\n", oa->o_blksize);
2808         if (valid & (OBD_MD_FLTYPE | OBD_MD_FLMODE))
2809                 CDEBUG(D_RPCTRACE, "obdo: o_mode = %o\n",
2810                        oa->o_mode & ((valid & OBD_MD_FLTYPE ?  S_IFMT : 0) |
2811                                      (valid & OBD_MD_FLMODE ? ~S_IFMT : 0)));
2812         if (valid & OBD_MD_FLUID)
2813                 CDEBUG(D_RPCTRACE, "obdo: o_uid = %u\n", oa->o_uid);
2814         if (valid & OBD_MD_FLUID)
2815                 CDEBUG(D_RPCTRACE, "obdo: o_uid_h = %u\n", oa->o_uid_h);
2816         if (valid & OBD_MD_FLGID)
2817                 CDEBUG(D_RPCTRACE, "obdo: o_gid = %u\n", oa->o_gid);
2818         if (valid & OBD_MD_FLGID)
2819                 CDEBUG(D_RPCTRACE, "obdo: o_gid_h = %u\n", oa->o_gid_h);
2820         if (valid & OBD_MD_FLFLAGS)
2821                 CDEBUG(D_RPCTRACE, "obdo: o_flags = %x\n", oa->o_flags);
2822         if (valid & OBD_MD_FLNLINK)
2823                 CDEBUG(D_RPCTRACE, "obdo: o_nlink = %u\n", oa->o_nlink);
2824         else if (valid & OBD_MD_FLCKSUM)
2825                 CDEBUG(D_RPCTRACE, "obdo: o_checksum (o_nlink) = %u\n",
2826                        oa->o_nlink);
2827         if (valid & OBD_MD_FLPARENT)
2828                 CDEBUG(D_RPCTRACE, "obdo: o_parent_oid = %x\n",
2829                        oa->o_parent_oid);
2830         if (valid & OBD_MD_FLFID) {
2831                 CDEBUG(D_RPCTRACE, "obdo: o_stripe_idx = %u\n",
2832                        oa->o_stripe_idx);
2833                 CDEBUG(D_RPCTRACE, "obdo: o_parent_ver = %x\n",
2834                        oa->o_parent_ver);
2835         }
2836         if (valid & OBD_MD_FLHANDLE)
2837                 CDEBUG(D_RPCTRACE, "obdo: o_handle = %lld\n",
2838                        oa->o_handle.cookie);
2839 }
2840
2841 void dump_ost_body(struct ost_body *ob)
2842 {
2843         dump_obdo(&ob->oa);
2844 }
2845
2846 void dump_rcs(__u32 *rc)
2847 {
2848         CDEBUG(D_RPCTRACE, "rmf_rcs: %d\n", *rc);
2849 }
2850
2851 static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2852 {
2853         LASSERT(req->rq_reqmsg);
2854
2855         switch (req->rq_reqmsg->lm_magic) {
2856         case LUSTRE_MSG_MAGIC_V2:
2857                 return req_capsule_req_swabbed(&req->rq_pill,
2858                                                MSG_PTLRPC_BODY_OFF);
2859         default:
2860                 CERROR("bad lustre msg magic: %#08X\n",
2861                        req->rq_reqmsg->lm_magic);
2862         }
2863         return 0;
2864 }
2865
2866 static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2867 {
2868         if (unlikely(!req->rq_repmsg))
2869                 return 0;
2870
2871         switch (req->rq_repmsg->lm_magic) {
2872         case LUSTRE_MSG_MAGIC_V2:
2873                 return req_capsule_rep_swabbed(&req->rq_pill,
2874                                                MSG_PTLRPC_BODY_OFF);
2875         default:
2876                 /* uninitialized yet */
2877                 return 0;
2878         }
2879 }
2880
2881 void _debug_req(struct ptlrpc_request *req,
2882                 struct libcfs_debug_msg_data *msgdata, const char *fmt, ...)
2883 {
2884         bool req_ok = req->rq_reqmsg != NULL;
2885         bool rep_ok = false;
2886         struct lnet_nid *nid = NULL;
2887         struct va_format vaf;
2888         va_list args;
2889         int rep_flags = -1;
2890         int rep_status = -1;
2891         __u64 req_transno = 0;
2892         int req_opc = -1;
2893         __u32 req_flags =  (__u32) -1;
2894         __u32 req_uid = (__u32) -1;
2895         __u32 req_gid = (__u32) -1;
2896         char *req_jobid = NULL;
2897
2898         spin_lock(&req->rq_early_free_lock);
2899         if (req->rq_repmsg)
2900                 rep_ok = true;
2901
2902         if (req_capsule_req_need_swab(&req->rq_pill)) {
2903                 req_ok = req_ok && req_ptlrpc_body_swabbed(req);
2904                 rep_ok = rep_ok && rep_ptlrpc_body_swabbed(req);
2905         }
2906
2907         if (rep_ok) {
2908                 rep_flags = lustre_msg_get_flags(req->rq_repmsg);
2909                 rep_status = lustre_msg_get_status(req->rq_repmsg);
2910         }
2911         spin_unlock(&req->rq_early_free_lock);
2912
2913         if (req->rq_import && req->rq_import->imp_connection)
2914                 nid = &req->rq_import->imp_connection->c_peer.nid;
2915         else if (req->rq_export && req->rq_export->exp_connection)
2916                 nid = &req->rq_export->exp_connection->c_peer.nid;
2917
2918         if (req_ok) {
2919                 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
2920                 req_opc = lustre_msg_get_opc(req->rq_reqmsg);
2921                 req_jobid = lustre_msg_get_jobid(req->rq_reqmsg);
2922                 lustre_msg_get_uid_gid(req->rq_reqmsg, &req_uid, &req_gid);
2923                 req_flags = lustre_msg_get_flags(req->rq_reqmsg);
2924         }
2925
2926         va_start(args, fmt);
2927         vaf.fmt = fmt;
2928         vaf.va = &args;
2929         libcfs_debug_msg(msgdata,
2930                          "%pV req@%p x%llu/t%lld(%llu) o%d->%s@%s:%d/%d lens %d/%d e %d to %lld dl %lld ref %d fl " REQ_FLAGS_FMT "/%x/%x rc %d/%d job:'%s' uid:%u gid:%u\n",
2931                          &vaf,
2932                          req, req->rq_xid, req->rq_transno, req_transno,
2933                          req_opc,
2934                          req->rq_import ?
2935                          req->rq_import->imp_obd->obd_name :
2936                          req->rq_export ?
2937                          req->rq_export->exp_client_uuid.uuid :
2938                          "<?>",
2939                          nid ? libcfs_nidstr(nid) : "<unknown>",
2940                          req->rq_request_portal, req->rq_reply_portal,
2941                          req->rq_reqlen, req->rq_replen,
2942                          req->rq_early_count, (s64)req->rq_timedout,
2943                          (s64)req->rq_deadline,
2944                          atomic_read(&req->rq_refcount),
2945                          DEBUG_REQ_FLAGS(req), req_flags, rep_flags,
2946                          req->rq_status, rep_status,
2947                          req_jobid ?: "", req_uid, req_gid);
2948         va_end(args);
2949 }
2950 EXPORT_SYMBOL(_debug_req);
2951
2952 void lustre_swab_hsm_user_state(struct hsm_user_state *state)
2953 {
2954         __swab32s(&state->hus_states);
2955         __swab32s(&state->hus_archive_id);
2956 }
2957
2958 void lustre_swab_hsm_state_set(struct hsm_state_set *hss)
2959 {
2960         __swab32s(&hss->hss_valid);
2961         __swab64s(&hss->hss_setmask);
2962         __swab64s(&hss->hss_clearmask);
2963         __swab32s(&hss->hss_archive_id);
2964 }
2965
2966 static void lustre_swab_hsm_extent(struct hsm_extent *extent)
2967 {
2968         __swab64s(&extent->offset);
2969         __swab64s(&extent->length);
2970 }
2971
2972 void lustre_swab_hsm_current_action(struct hsm_current_action *action)
2973 {
2974         __swab32s(&action->hca_state);
2975         __swab32s(&action->hca_action);
2976         lustre_swab_hsm_extent(&action->hca_location);
2977 }
2978
2979 void lustre_swab_hsm_user_item(struct hsm_user_item *hui)
2980 {
2981         lustre_swab_lu_fid(&hui->hui_fid);
2982         lustre_swab_hsm_extent(&hui->hui_extent);
2983 }
2984
2985 static void lustre_swab_lu_extent(struct lu_extent *le)
2986 {
2987         __swab64s(&le->e_start);
2988         __swab64s(&le->e_end);
2989 }
2990
2991 void lustre_swab_layout_intent(struct layout_intent *li)
2992 {
2993         __swab32s(&li->lai_opc);
2994         __swab32s(&li->lai_flags);
2995         lustre_swab_lu_extent(&li->lai_extent);
2996 }
2997
2998 void lustre_swab_hsm_progress_kernel(struct hsm_progress_kernel *hpk)
2999 {
3000         lustre_swab_lu_fid(&hpk->hpk_fid);
3001         __swab64s(&hpk->hpk_cookie);
3002         __swab64s(&hpk->hpk_extent.offset);
3003         __swab64s(&hpk->hpk_extent.length);
3004         __swab16s(&hpk->hpk_flags);
3005         __swab16s(&hpk->hpk_errval);
3006 }
3007
3008 void lustre_swab_hsm_request(struct hsm_request *hr)
3009 {
3010         __swab32s(&hr->hr_action);
3011         __swab32s(&hr->hr_archive_id);
3012         __swab64s(&hr->hr_flags);
3013         __swab32s(&hr->hr_itemcount);
3014         __swab32s(&hr->hr_data_len);
3015 }
3016
3017 /* TODO: swab each sub request message */
3018 void lustre_swab_batch_update_request(struct batch_update_request *bur)
3019 {
3020         __swab32s(&bur->burq_magic);
3021         __swab16s(&bur->burq_count);
3022         __swab16s(&bur->burq_padding);
3023 }
3024
3025 /* TODO: swab each sub reply message. */
3026 void lustre_swab_batch_update_reply(struct batch_update_reply *bur)
3027 {
3028         __swab32s(&bur->burp_magic);
3029         __swab16s(&bur->burp_count);
3030         __swab16s(&bur->burp_padding);
3031 }
3032
3033 void lustre_swab_but_update_header(struct but_update_header *buh)
3034 {
3035         __swab32s(&buh->buh_magic);
3036         __swab32s(&buh->buh_count);
3037         __swab32s(&buh->buh_inline_length);
3038         __swab32s(&buh->buh_reply_size);
3039         __swab32s(&buh->buh_update_count);
3040 }
3041 EXPORT_SYMBOL(lustre_swab_but_update_header);
3042
3043 void lustre_swab_but_update_buffer(struct but_update_buffer *bub)
3044 {
3045         __swab32s(&bub->bub_size);
3046         __swab32s(&bub->bub_padding);
3047 }
3048 EXPORT_SYMBOL(lustre_swab_but_update_buffer);
3049
3050 void lustre_swab_swap_layouts(struct mdc_swap_layouts *msl)
3051 {
3052         __swab64s(&msl->msl_flags);
3053 }
3054
3055 void lustre_swab_close_data(struct close_data *cd)
3056 {
3057
3058         __swab64s(&cd->cd_handle.cookie);
3059         lustre_swab_lu_fid(&cd->cd_fid);
3060         __swab64s(&cd->cd_data_version);
3061 }
3062
3063 void lustre_swab_close_data_resync_done(struct close_data_resync_done *resync)
3064 {
3065         int i;
3066
3067         __swab32s(&resync->resync_count);
3068         /* after swab, resync_count must in CPU endian */
3069         if (resync->resync_count <= INLINE_RESYNC_ARRAY_SIZE) {
3070                 for (i = 0; i < resync->resync_count; i++)
3071                         __swab32s(&resync->resync_ids_inline[i]);
3072         }
3073 }
3074
3075 void lustre_swab_close_data_special(struct close_data *cd, enum mds_op_bias b)
3076 {
3077         if (b & MDS_CLOSE_RESYNC_DONE)
3078                 lustre_swab_close_data_resync_done(&cd->cd_resync);
3079         else if (b & MDS_CLOSE_LAYOUT_SPLIT)
3080                 __swab16s(&cd->cd_mirror_id);
3081         else if (b & MDS_PCC_ATTACH)
3082                 swab32s(&cd->cd_archive_id);
3083         else if (b & MDS_CLOSE_LAYOUT_SWAP)
3084                 swab64s(&cd->cd_data_version2);
3085 }
3086 EXPORT_SYMBOL(lustre_swab_close_data_special);
3087
3088 void lustre_swab_lfsck_request(struct lfsck_request *lr)
3089 {
3090         __swab32s(&lr->lr_event);
3091         __swab32s(&lr->lr_index);
3092         __swab32s(&lr->lr_flags);
3093         __swab32s(&lr->lr_valid);
3094         __swab32s(&lr->lr_speed);
3095         __swab16s(&lr->lr_version);
3096         __swab16s(&lr->lr_active);
3097         __swab16s(&lr->lr_param);
3098         __swab16s(&lr->lr_async_windows);
3099         __swab32s(&lr->lr_flags);
3100         lustre_swab_lu_fid(&lr->lr_fid);
3101         lustre_swab_lu_fid(&lr->lr_fid2);
3102         __swab32s(&lr->lr_comp_id);
3103         BUILD_BUG_ON(offsetof(typeof(*lr), lr_padding_0) == 0);
3104         BUILD_BUG_ON(offsetof(typeof(*lr), lr_padding_1) == 0);
3105         BUILD_BUG_ON(offsetof(typeof(*lr), lr_padding_2) == 0);
3106         BUILD_BUG_ON(offsetof(typeof(*lr), lr_padding_3) == 0);
3107 }
3108
3109 void lustre_swab_lfsck_reply(struct lfsck_reply *lr)
3110 {
3111         __swab32s(&lr->lr_status);
3112         BUILD_BUG_ON(offsetof(typeof(*lr), lr_padding_1) == 0);
3113         __swab64s(&lr->lr_repaired);
3114 }
3115
3116 #ifdef HAVE_SERVER_SUPPORT
3117 static void lustre_swab_orphan_rec(struct lu_orphan_rec *rec)
3118 {
3119         lustre_swab_lu_fid(&rec->lor_fid);
3120         __swab32s(&rec->lor_uid);
3121         __swab32s(&rec->lor_gid);
3122 }
3123
3124 void lustre_swab_orphan_ent(struct lu_orphan_ent *ent)
3125 {
3126         lustre_swab_lu_fid(&ent->loe_key);
3127         lustre_swab_orphan_rec(&ent->loe_rec);
3128 }
3129 EXPORT_SYMBOL(lustre_swab_orphan_ent);
3130
3131 void lustre_swab_orphan_ent_v2(struct lu_orphan_ent_v2 *ent)
3132 {
3133         lustre_swab_lu_fid(&ent->loe_key);
3134         lustre_swab_orphan_rec(&ent->loe_rec.lor_rec);
3135         lustre_swab_ost_layout(&ent->loe_rec.lor_layout);
3136         BUILD_BUG_ON(offsetof(typeof(ent->loe_rec), lor_padding) == 0);
3137 }
3138 EXPORT_SYMBOL(lustre_swab_orphan_ent_v2);
3139
3140 void lustre_swab_orphan_ent_v3(struct lu_orphan_ent_v3 *ent)
3141 {
3142         lustre_swab_lu_fid(&ent->loe_key);
3143         lustre_swab_orphan_rec(&ent->loe_rec.lor_rec);
3144         lustre_swab_ost_layout(&ent->loe_rec.lor_layout);
3145         __swab32s(&ent->loe_rec.lor_layout_version);
3146         __swab32s(&ent->loe_rec.lor_range);
3147         BUILD_BUG_ON(offsetof(typeof(ent->loe_rec), lor_padding_1) == 0);
3148         BUILD_BUG_ON(offsetof(typeof(ent->loe_rec), lor_padding_2) == 0);
3149 }
3150 EXPORT_SYMBOL(lustre_swab_orphan_ent_v3);
3151 #endif /* HAVE_SERVER_SUPPORT */
3152
3153 void lustre_swab_ladvise(struct lu_ladvise *ladvise)
3154 {
3155         __swab16s(&ladvise->lla_advice);
3156         __swab16s(&ladvise->lla_value1);
3157         __swab32s(&ladvise->lla_value2);
3158         __swab64s(&ladvise->lla_start);
3159         __swab64s(&ladvise->lla_end);
3160         __swab32s(&ladvise->lla_value3);
3161         __swab32s(&ladvise->lla_value4);
3162 }
3163 EXPORT_SYMBOL(lustre_swab_ladvise);
3164
3165 void lustre_swab_ladvise_hdr(struct ladvise_hdr *ladvise_hdr)
3166 {
3167         __swab32s(&ladvise_hdr->lah_magic);
3168         __swab32s(&ladvise_hdr->lah_count);
3169         __swab64s(&ladvise_hdr->lah_flags);
3170         __swab32s(&ladvise_hdr->lah_value1);
3171         __swab32s(&ladvise_hdr->lah_value2);
3172         __swab64s(&ladvise_hdr->lah_value3);
3173 }
3174 EXPORT_SYMBOL(lustre_swab_ladvise_hdr);