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