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