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