Whamcloud - gitweb
545d767b75e356b2358cc16555db795ab8cb0136
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/pack_generic.c
37  *
38  * (Un)packing of OST requests
39  *
40  * Author: Peter J. Braam <braam@clusterfs.com>
41  * Author: Phil Schwan <phil@clusterfs.com>
42  * Author: Eric Barton <eeb@clusterfs.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_RPC
46 #ifndef __KERNEL__
47 # include <liblustre.h>
48 #endif
49
50 #include <libcfs/libcfs.h>
51
52 #include <obd_support.h>
53 #include <obd_class.h>
54 #include <lustre_net.h>
55 #include <obd_cksum.h>
56 #include <lustre/ll_fiemap.h>
57
58 static inline int lustre_msg_hdr_size_v2(int count)
59 {
60         return cfs_size_round(offsetof(struct lustre_msg_v2,
61                                        lm_buflens[count]));
62 }
63
64 int lustre_msg_hdr_size(__u32 magic, int count)
65 {
66         switch (magic) {
67         case LUSTRE_MSG_MAGIC_V2:
68                 return lustre_msg_hdr_size_v2(count);
69         default:
70                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
71                 return -EINVAL;
72         }
73 }
74 EXPORT_SYMBOL(lustre_msg_hdr_size);
75
76 void ptlrpc_buf_set_swabbed(struct ptlrpc_request *req, const int inout,
77                             int index)
78 {
79         if (inout)
80                 lustre_set_req_swabbed(req, index);
81         else
82                 lustre_set_rep_swabbed(req, index);
83 }
84
85 int ptlrpc_buf_need_swab(struct ptlrpc_request *req, const int inout,
86                          int index)
87 {
88         if (inout)
89                 return (ptlrpc_req_need_swab(req) &&
90                         !lustre_req_swabbed(req, index));
91         else
92                 return (ptlrpc_rep_need_swab(req) &&
93                         !lustre_rep_swabbed(req, index));
94 }
95
96 static inline int lustre_msg_check_version_v2(struct lustre_msg_v2 *msg,
97                                               __u32 version)
98 {
99         __u32 ver = lustre_msg_get_version(msg);
100         return (ver & LUSTRE_VERSION_MASK) != version;
101 }
102
103 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version)
104 {
105         switch (msg->lm_magic) {
106         case LUSTRE_MSG_MAGIC_V1:
107                 CERROR("msg v1 not supported - please upgrade you system\n");
108                 return -EINVAL;
109         case LUSTRE_MSG_MAGIC_V2:
110                 return lustre_msg_check_version_v2(msg, version);
111         default:
112                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
113                 return 0;
114         }
115 }
116
117 /* early reply size */
118 int lustre_msg_early_size()
119 {
120         static int size = 0;
121         if (!size) {
122                 /* Always reply old ptlrpc_body_v2 to keep interoprability
123                  * with the old client (< 2.3) which doesn't have pb_jobid
124                  * in the ptlrpc_body.
125                  *
126                  * XXX Remove this whenever we dorp interoprability with such
127                  *     client.
128                  */
129                 __u32 pblen = sizeof(struct ptlrpc_body_v2);
130                 size = lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, &pblen);
131         }
132         return size;
133 }
134 EXPORT_SYMBOL(lustre_msg_early_size);
135
136 int lustre_msg_size_v2(int count, __u32 *lengths)
137 {
138         int size;
139         int i;
140
141         size = lustre_msg_hdr_size_v2(count);
142         for (i = 0; i < count; i++)
143                 size += cfs_size_round(lengths[i]);
144
145         return size;
146 }
147 EXPORT_SYMBOL(lustre_msg_size_v2);
148
149 /* This returns the size of the buffer that is required to hold a lustre_msg
150  * with the given sub-buffer lengths.
151  * NOTE: this should only be used for NEW requests, and should always be
152  *       in the form of a v2 request.  If this is a connection to a v1
153  *       target then the first buffer will be stripped because the ptlrpc
154  *       data is part of the lustre_msg_v1 header. b=14043 */
155 int lustre_msg_size(__u32 magic, int count, __u32 *lens)
156 {
157         __u32 size[] = { sizeof(struct ptlrpc_body) };
158
159         if (!lens) {
160                 LASSERT(count == 1);
161                 lens = size;
162         }
163
164         LASSERT(count > 0);
165         LASSERT(lens[MSG_PTLRPC_BODY_OFF] >= sizeof(struct ptlrpc_body_v2));
166
167         switch (magic) {
168         case LUSTRE_MSG_MAGIC_V2:
169                 return lustre_msg_size_v2(count, lens);
170         default:
171                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
172                 return -EINVAL;
173         }
174 }
175
176 /* This is used to determine the size of a buffer that was already packed
177  * and will correctly handle the different message formats. */
178 int lustre_packed_msg_size(struct lustre_msg *msg)
179 {
180         switch (msg->lm_magic) {
181         case LUSTRE_MSG_MAGIC_V2:
182                 return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
183         default:
184                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
185                 return 0;
186         }
187 }
188
189 void lustre_init_msg_v2(struct lustre_msg_v2 *msg, int count, __u32 *lens,
190                         char **bufs)
191 {
192         char *ptr;
193         int i;
194
195         msg->lm_bufcount = count;
196         /* XXX: lm_secflvr uninitialized here */
197         msg->lm_magic = LUSTRE_MSG_MAGIC_V2;
198
199         for (i = 0; i < count; i++)
200                 msg->lm_buflens[i] = lens[i];
201
202         if (bufs == NULL)
203                 return;
204
205         ptr = (char *)msg + lustre_msg_hdr_size_v2(count);
206         for (i = 0; i < count; i++) {
207                 char *tmp = bufs[i];
208                 LOGL(tmp, lens[i], ptr);
209         }
210 }
211 EXPORT_SYMBOL(lustre_init_msg_v2);
212
213 static int lustre_pack_request_v2(struct ptlrpc_request *req,
214                                   int count, __u32 *lens, char **bufs)
215 {
216         int reqlen, rc;
217
218         reqlen = lustre_msg_size_v2(count, lens);
219
220         rc = sptlrpc_cli_alloc_reqbuf(req, reqlen);
221         if (rc)
222                 return rc;
223
224         req->rq_reqlen = reqlen;
225
226         lustre_init_msg_v2(req->rq_reqmsg, count, lens, bufs);
227         lustre_msg_add_version(req->rq_reqmsg, PTLRPC_MSG_VERSION);
228         return 0;
229 }
230
231 int lustre_pack_request(struct ptlrpc_request *req, __u32 magic, int count,
232                         __u32 *lens, char **bufs)
233 {
234         __u32 size[] = { sizeof(struct ptlrpc_body) };
235
236         if (!lens) {
237                 LASSERT(count == 1);
238                 lens = size;
239         }
240
241         LASSERT(count > 0);
242         LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
243
244         /* only use new format, we don't need to be compatible with 1.4 */
245         magic = LUSTRE_MSG_MAGIC_V2;
246
247         switch (magic) {
248         case LUSTRE_MSG_MAGIC_V2:
249                 return lustre_pack_request_v2(req, count, lens, bufs);
250         default:
251                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
252                 return -EINVAL;
253         }
254 }
255
256 #if RS_DEBUG
257 CFS_LIST_HEAD(ptlrpc_rs_debug_lru);
258 cfs_spinlock_t ptlrpc_rs_debug_lock;
259
260 #define PTLRPC_RS_DEBUG_LRU_ADD(rs)                                     \
261 do {                                                                    \
262         cfs_spin_lock(&ptlrpc_rs_debug_lock);                           \
263         cfs_list_add_tail(&(rs)->rs_debug_list, &ptlrpc_rs_debug_lru);  \
264         cfs_spin_unlock(&ptlrpc_rs_debug_lock);                         \
265 } while (0)
266
267 #define PTLRPC_RS_DEBUG_LRU_DEL(rs)             \
268 do {                                            \
269         cfs_spin_lock(&ptlrpc_rs_debug_lock);   \
270         cfs_list_del(&(rs)->rs_debug_list);     \
271         cfs_spin_unlock(&ptlrpc_rs_debug_lock); \
272 } while (0)
273 #else
274 # define PTLRPC_RS_DEBUG_LRU_ADD(rs) do {} while(0)
275 # define PTLRPC_RS_DEBUG_LRU_DEL(rs) do {} while(0)
276 #endif
277
278 struct ptlrpc_reply_state *lustre_get_emerg_rs(struct ptlrpc_service *svc)
279 {
280         struct ptlrpc_reply_state *rs = NULL;
281
282         cfs_spin_lock(&svc->srv_rs_lock);
283         /* See if we have anything in a pool, and wait if nothing */
284         while (cfs_list_empty(&svc->srv_free_rs_list)) {
285                 struct l_wait_info lwi;
286                 int rc;
287                 cfs_spin_unlock(&svc->srv_rs_lock);
288                 /* If we cannot get anything for some long time, we better
289                    bail out instead of waiting infinitely */
290                 lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
291                 rc = l_wait_event(svc->srv_free_rs_waitq,
292                                   !cfs_list_empty(&svc->srv_free_rs_list),
293                                   &lwi);
294                 if (rc)
295                         goto out;
296                 cfs_spin_lock(&svc->srv_rs_lock);
297         }
298
299         rs = cfs_list_entry(svc->srv_free_rs_list.next,
300                             struct ptlrpc_reply_state, rs_list);
301         cfs_list_del(&rs->rs_list);
302         cfs_spin_unlock(&svc->srv_rs_lock);
303         LASSERT(rs);
304         memset(rs, 0, svc->srv_max_reply_size);
305         rs->rs_service = svc;
306         rs->rs_prealloc = 1;
307 out:
308         return rs;
309 }
310
311 void lustre_put_emerg_rs(struct ptlrpc_reply_state *rs)
312 {
313         struct ptlrpc_service *svc = rs->rs_service;
314
315         LASSERT(svc);
316
317         cfs_spin_lock(&svc->srv_rs_lock);
318         cfs_list_add(&rs->rs_list, &svc->srv_free_rs_list);
319         cfs_spin_unlock(&svc->srv_rs_lock);
320         cfs_waitq_signal(&svc->srv_free_rs_waitq);
321 }
322
323 int lustre_pack_reply_v2(struct ptlrpc_request *req, int count,
324                          __u32 *lens, char **bufs, int flags)
325 {
326         struct ptlrpc_reply_state *rs;
327         int                        msg_len, rc;
328         ENTRY;
329
330         LASSERT(req->rq_reply_state == NULL);
331
332         if ((flags & LPRFL_EARLY_REPLY) == 0) {
333                 cfs_spin_lock(&req->rq_lock);
334                 req->rq_packed_final = 1;
335                 cfs_spin_unlock(&req->rq_lock);
336         }
337
338         msg_len = lustre_msg_size_v2(count, lens);
339         rc = sptlrpc_svc_alloc_rs(req, msg_len);
340         if (rc)
341                 RETURN(rc);
342
343         rs = req->rq_reply_state;
344         cfs_atomic_set(&rs->rs_refcount, 1);    /* 1 ref for rq_reply_state */
345         rs->rs_cb_id.cbid_fn = reply_out_callback;
346         rs->rs_cb_id.cbid_arg = rs;
347         rs->rs_service = req->rq_rqbd->rqbd_service;
348         CFS_INIT_LIST_HEAD(&rs->rs_exp_list);
349         CFS_INIT_LIST_HEAD(&rs->rs_obd_list);
350         CFS_INIT_LIST_HEAD(&rs->rs_list);
351         cfs_spin_lock_init(&rs->rs_lock);
352
353         req->rq_replen = msg_len;
354         req->rq_reply_state = rs;
355         req->rq_repmsg = rs->rs_msg;
356
357         lustre_init_msg_v2(rs->rs_msg, count, lens, bufs);
358         lustre_msg_add_version(rs->rs_msg, PTLRPC_MSG_VERSION);
359
360         PTLRPC_RS_DEBUG_LRU_ADD(rs);
361
362         RETURN(0);
363 }
364 EXPORT_SYMBOL(lustre_pack_reply_v2);
365
366 int lustre_pack_reply_flags(struct ptlrpc_request *req, int count, __u32 *lens,
367                             char **bufs, int flags)
368 {
369         int rc = 0;
370         __u32 size[] = { sizeof(struct ptlrpc_body) };
371
372         if (!lens) {
373                 LASSERT(count == 1);
374                 lens = size;
375         }
376
377         LASSERT(count > 0);
378         LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
379
380         switch (req->rq_reqmsg->lm_magic) {
381         case LUSTRE_MSG_MAGIC_V2:
382                 rc = lustre_pack_reply_v2(req, count, lens, bufs, flags);
383                 break;
384         default:
385                 LASSERTF(0, "incorrect message magic: %08x\n",
386                          req->rq_reqmsg->lm_magic);
387                 rc = -EINVAL;
388         }
389         if (rc != 0)
390                 CERROR("lustre_pack_reply failed: rc=%d size=%d\n", rc,
391                        lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens));
392         return rc;
393 }
394
395 int lustre_pack_reply(struct ptlrpc_request *req, int count, __u32 *lens,
396                       char **bufs)
397 {
398         return lustre_pack_reply_flags(req, count, lens, bufs, 0);
399 }
400
401 void *lustre_msg_buf_v2(struct lustre_msg_v2 *m, int n, int min_size)
402 {
403         int i, offset, buflen, bufcount;
404
405         LASSERT(m != NULL);
406         LASSERT(n >= 0);
407
408         bufcount = m->lm_bufcount;
409         if (unlikely(n >= bufcount)) {
410                 CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
411                        m, n, bufcount);
412                 return NULL;
413         }
414
415         buflen = m->lm_buflens[n];
416         if (unlikely(buflen < min_size)) {
417                 CERROR("msg %p buffer[%d] size %d too small "
418                        "(required %d, opc=%d)\n", m, n, buflen, min_size,
419                        n == MSG_PTLRPC_BODY_OFF ? -1 : lustre_msg_get_opc(m));
420                 return NULL;
421         }
422
423         offset = lustre_msg_hdr_size_v2(bufcount);
424         for (i = 0; i < n; i++)
425                 offset += cfs_size_round(m->lm_buflens[i]);
426
427         return (char *)m + offset;
428 }
429
430 void *lustre_msg_buf(struct lustre_msg *m, int n, int min_size)
431 {
432         switch (m->lm_magic) {
433         case LUSTRE_MSG_MAGIC_V2:
434                 return lustre_msg_buf_v2(m, n, min_size);
435         default:
436                 LASSERTF(0, "incorrect message magic: %08x(msg:%p)\n", m->lm_magic, m);
437                 return NULL;
438         }
439 }
440
441 int lustre_shrink_msg_v2(struct lustre_msg_v2 *msg, int segment,
442                          unsigned int newlen, int move_data)
443 {
444         char   *tail = NULL, *newpos;
445         int     tail_len = 0, n;
446
447         LASSERT(msg);
448         LASSERT(msg->lm_bufcount > segment);
449         LASSERT(msg->lm_buflens[segment] >= newlen);
450
451         if (msg->lm_buflens[segment] == newlen)
452                 goto out;
453
454         if (move_data && msg->lm_bufcount > segment + 1) {
455                 tail = lustre_msg_buf_v2(msg, segment + 1, 0);
456                 for (n = segment + 1; n < msg->lm_bufcount; n++)
457                         tail_len += cfs_size_round(msg->lm_buflens[n]);
458         }
459
460         msg->lm_buflens[segment] = newlen;
461
462         if (tail && tail_len) {
463                 newpos = lustre_msg_buf_v2(msg, segment + 1, 0);
464                 LASSERT(newpos <= tail);
465                 if (newpos != tail)
466                         memmove(newpos, tail, tail_len);
467         }
468 out:
469         return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
470 }
471
472 /*
473  * for @msg, shrink @segment to size @newlen. if @move_data is non-zero,
474  * we also move data forward from @segment + 1.
475  *
476  * if @newlen == 0, we remove the segment completely, but we still keep the
477  * totally bufcount the same to save possible data moving. this will leave a
478  * unused segment with size 0 at the tail, but that's ok.
479  *
480  * return new msg size after shrinking.
481  *
482  * CAUTION:
483  * + if any buffers higher than @segment has been filled in, must call shrink
484  *   with non-zero @move_data.
485  * + caller should NOT keep pointers to msg buffers which higher than @segment
486  *   after call shrink.
487  */
488 int lustre_shrink_msg(struct lustre_msg *msg, int segment,
489                       unsigned int newlen, int move_data)
490 {
491         switch (msg->lm_magic) {
492         case LUSTRE_MSG_MAGIC_V2:
493                 return lustre_shrink_msg_v2(msg, segment, newlen, move_data);
494         default:
495                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
496         }
497 }
498
499 void lustre_free_reply_state(struct ptlrpc_reply_state *rs)
500 {
501         PTLRPC_RS_DEBUG_LRU_DEL(rs);
502
503         LASSERT (cfs_atomic_read(&rs->rs_refcount) == 0);
504         LASSERT (!rs->rs_difficult || rs->rs_handled);
505         LASSERT (!rs->rs_on_net);
506         LASSERT (!rs->rs_scheduled);
507         LASSERT (rs->rs_export == NULL);
508         LASSERT (rs->rs_nlocks == 0);
509         LASSERT (cfs_list_empty(&rs->rs_exp_list));
510         LASSERT (cfs_list_empty(&rs->rs_obd_list));
511
512         sptlrpc_svc_free_rs(rs);
513 }
514
515 static int lustre_unpack_msg_v2(struct lustre_msg_v2 *m, int len)
516 {
517         int swabbed, required_len, i;
518
519         /* Now we know the sender speaks my language. */
520         required_len = lustre_msg_hdr_size_v2(0);
521         if (len < required_len) {
522                 /* can't even look inside the message */
523                 CERROR("message length %d too small for lustre_msg\n", len);
524                 return -EINVAL;
525         }
526
527         swabbed = (m->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED);
528
529         if (swabbed) {
530                 __swab32s(&m->lm_magic);
531                 __swab32s(&m->lm_bufcount);
532                 __swab32s(&m->lm_secflvr);
533                 __swab32s(&m->lm_repsize);
534                 __swab32s(&m->lm_cksum);
535                 __swab32s(&m->lm_flags);
536                 CLASSERT(offsetof(typeof(*m), lm_padding_2) != 0);
537                 CLASSERT(offsetof(typeof(*m), lm_padding_3) != 0);
538         }
539
540         required_len = lustre_msg_hdr_size_v2(m->lm_bufcount);
541         if (len < required_len) {
542                 /* didn't receive all the buffer lengths */
543                 CERROR ("message length %d too small for %d buflens\n",
544                         len, m->lm_bufcount);
545                 return -EINVAL;
546         }
547
548         for (i = 0; i < m->lm_bufcount; i++) {
549                 if (swabbed)
550                         __swab32s(&m->lm_buflens[i]);
551                 required_len += cfs_size_round(m->lm_buflens[i]);
552         }
553
554         if (len < required_len) {
555                 CERROR("len: %d, required_len %d\n", len, required_len);
556                 CERROR("bufcount: %d\n", m->lm_bufcount);
557                 for (i = 0; i < m->lm_bufcount; i++)
558                         CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
559                 return -EINVAL;
560         }
561
562         return swabbed;
563 }
564
565 int __lustre_unpack_msg(struct lustre_msg *m, int len)
566 {
567         int required_len, rc;
568         ENTRY;
569
570         /* We can provide a slightly better error log, if we check the
571          * message magic and version first.  In the future, struct
572          * lustre_msg may grow, and we'd like to log a version mismatch,
573          * rather than a short message.
574          *
575          */
576         required_len = offsetof(struct lustre_msg, lm_magic) +
577                        sizeof(m->lm_magic);
578         if (len < required_len) {
579                 /* can't even look inside the message */
580                 CERROR("message length %d too small for magic/version check\n",
581                        len);
582                 RETURN(-EINVAL);
583         }
584
585         rc = lustre_unpack_msg_v2(m, len);
586
587         RETURN(rc);
588 }
589 EXPORT_SYMBOL(__lustre_unpack_msg);
590
591 int ptlrpc_unpack_req_msg(struct ptlrpc_request *req, int len)
592 {
593         int rc;
594         rc = __lustre_unpack_msg(req->rq_reqmsg, len);
595         if (rc == 1) {
596                 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
597                 rc = 0;
598         }
599         return rc;
600 }
601
602 int ptlrpc_unpack_rep_msg(struct ptlrpc_request *req, int len)
603 {
604         int rc;
605         rc = __lustre_unpack_msg(req->rq_repmsg, len);
606         if (rc == 1) {
607                 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
608                 rc = 0;
609         }
610         return rc;
611 }
612
613 static inline int lustre_unpack_ptlrpc_body_v2(struct ptlrpc_request *req,
614                                                const int inout, int offset)
615 {
616         struct ptlrpc_body *pb;
617         struct lustre_msg_v2 *m = inout ? req->rq_reqmsg : req->rq_repmsg;
618
619         pb = lustre_msg_buf_v2(m, offset, sizeof(struct ptlrpc_body_v2));
620         if (!pb) {
621                 CERROR("error unpacking ptlrpc body\n");
622                 return -EFAULT;
623         }
624         if (ptlrpc_buf_need_swab(req, inout, offset)) {
625                 lustre_swab_ptlrpc_body(pb);
626                 ptlrpc_buf_set_swabbed(req, inout, offset);
627         }
628
629         if ((pb->pb_version & ~LUSTRE_VERSION_MASK) != PTLRPC_MSG_VERSION) {
630                  CERROR("wrong lustre_msg version %08x\n", pb->pb_version);
631                  return -EINVAL;
632         }
633
634         return 0;
635 }
636
637 int lustre_unpack_req_ptlrpc_body(struct ptlrpc_request *req, int offset)
638 {
639         switch (req->rq_reqmsg->lm_magic) {
640         case LUSTRE_MSG_MAGIC_V2:
641                 return lustre_unpack_ptlrpc_body_v2(req, 1, offset);
642         default:
643                 CERROR("bad lustre msg magic: %08x\n",
644                        req->rq_reqmsg->lm_magic);
645                 return -EINVAL;
646         }
647 }
648
649 int lustre_unpack_rep_ptlrpc_body(struct ptlrpc_request *req, int offset)
650 {
651         switch (req->rq_repmsg->lm_magic) {
652         case LUSTRE_MSG_MAGIC_V2:
653                 return lustre_unpack_ptlrpc_body_v2(req, 0, offset);
654         default:
655                 CERROR("bad lustre msg magic: %08x\n",
656                        req->rq_repmsg->lm_magic);
657                 return -EINVAL;
658         }
659 }
660
661 static inline int lustre_msg_buflen_v2(struct lustre_msg_v2 *m, int n)
662 {
663         if (n >= m->lm_bufcount)
664                 return 0;
665
666         return m->lm_buflens[n];
667 }
668
669 /**
670  * lustre_msg_buflen - return the length of buffer \a n in message \a m
671  * \param m lustre_msg (request or reply) to look at
672  * \param n message index (base 0)
673  *
674  * returns zero for non-existent message indices
675  */
676 int lustre_msg_buflen(struct lustre_msg *m, int n)
677 {
678         switch (m->lm_magic) {
679         case LUSTRE_MSG_MAGIC_V2:
680                 return lustre_msg_buflen_v2(m, n);
681         default:
682                 CERROR("incorrect message magic: %08x\n", m->lm_magic);
683                 return -EINVAL;
684         }
685 }
686 EXPORT_SYMBOL(lustre_msg_buflen);
687
688 static inline void
689 lustre_msg_set_buflen_v2(struct lustre_msg_v2 *m, int n, int len)
690 {
691         if (n >= m->lm_bufcount)
692                 LBUG();
693
694         m->lm_buflens[n] = len;
695 }
696
697 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len)
698 {
699         switch (m->lm_magic) {
700         case LUSTRE_MSG_MAGIC_V2:
701                 lustre_msg_set_buflen_v2(m, n, len);
702                 return;
703         default:
704                 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
705         }
706 }
707
708 EXPORT_SYMBOL(lustre_msg_set_buflen);
709
710 /* NB return the bufcount for lustre_msg_v2 format, so if message is packed
711  * in V1 format, the result is one bigger. (add struct ptlrpc_body). */
712 int lustre_msg_bufcount(struct lustre_msg *m)
713 {
714         switch (m->lm_magic) {
715         case LUSTRE_MSG_MAGIC_V2:
716                 return m->lm_bufcount;
717         default:
718                 CERROR("incorrect message magic: %08x\n", m->lm_magic);
719                 return -EINVAL;
720         }
721 }
722 EXPORT_SYMBOL(lustre_msg_bufcount);
723
724 char *lustre_msg_string(struct lustre_msg *m, int index, int max_len)
725 {
726         /* max_len == 0 means the string should fill the buffer */
727         char *str;
728         int slen, blen;
729
730         switch (m->lm_magic) {
731         case LUSTRE_MSG_MAGIC_V2:
732                 str = lustre_msg_buf_v2(m, index, 0);
733                 blen = lustre_msg_buflen_v2(m, index);
734                 break;
735         default:
736                 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
737         }
738
739         if (str == NULL) {
740                 CERROR ("can't unpack string in msg %p buffer[%d]\n", m, index);
741                 return NULL;
742         }
743
744         slen = strnlen(str, blen);
745
746         if (slen == blen) {                     /* not NULL terminated */
747                 CERROR("can't unpack non-NULL terminated string in "
748                         "msg %p buffer[%d] len %d\n", m, index, blen);
749                 return NULL;
750         }
751
752         if (max_len == 0) {
753                 if (slen != blen - 1) {
754                         CERROR("can't unpack short string in msg %p "
755                                "buffer[%d] len %d: strlen %d\n",
756                                m, index, blen, slen);
757                         return NULL;
758                 }
759         } else if (slen > max_len) {
760                 CERROR("can't unpack oversized string in msg %p "
761                        "buffer[%d] len %d strlen %d: max %d expected\n",
762                        m, index, blen, slen, max_len);
763                 return NULL;
764         }
765
766         return str;
767 }
768
769 /* Wrap up the normal fixed length cases */
770 static inline void *__lustre_swab_buf(struct lustre_msg *msg, int index,
771                                       int min_size, void *swabber)
772 {
773         void *ptr = NULL;
774
775         LASSERT(msg != NULL);
776         switch (msg->lm_magic) {
777         case LUSTRE_MSG_MAGIC_V2:
778                 ptr = lustre_msg_buf_v2(msg, index, min_size);
779                 break;
780         default:
781                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
782         }
783
784         if (ptr && swabber)
785                 ((void (*)(void *))swabber)(ptr);
786
787         return ptr;
788 }
789
790 static inline struct ptlrpc_body *lustre_msg_ptlrpc_body(struct lustre_msg *msg)
791 {
792         return lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
793                                  sizeof(struct ptlrpc_body_v2));
794 }
795
796 __u32 lustre_msghdr_get_flags(struct lustre_msg *msg)
797 {
798         switch (msg->lm_magic) {
799         case LUSTRE_MSG_MAGIC_V1:
800         case LUSTRE_MSG_MAGIC_V1_SWABBED:
801                 return 0;
802         case LUSTRE_MSG_MAGIC_V2:
803                 /* already in host endian */
804                 return msg->lm_flags;
805         default:
806                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
807                 return 0;
808         }
809 }
810 EXPORT_SYMBOL(lustre_msghdr_get_flags);
811
812 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags)
813 {
814         switch (msg->lm_magic) {
815         case LUSTRE_MSG_MAGIC_V1:
816                 return;
817         case LUSTRE_MSG_MAGIC_V2:
818                 msg->lm_flags = flags;
819                 return;
820         default:
821                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
822         }
823 }
824
825 __u32 lustre_msg_get_flags(struct lustre_msg *msg)
826 {
827         switch (msg->lm_magic) {
828         case LUSTRE_MSG_MAGIC_V2: {
829                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
830                 if (!pb) {
831                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
832                         return 0;
833                 }
834                 return pb->pb_flags;
835         }
836         default:
837                 /* flags might be printed in debug code while message
838                  * uninitialized */
839                 return 0;
840         }
841 }
842
843 void lustre_msg_add_flags(struct lustre_msg *msg, int flags)
844 {
845         switch (msg->lm_magic) {
846         case LUSTRE_MSG_MAGIC_V2: {
847                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
848                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
849                 pb->pb_flags |= flags;
850                 return;
851         }
852         default:
853                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
854         }
855 }
856
857 void lustre_msg_set_flags(struct lustre_msg *msg, int flags)
858 {
859         switch (msg->lm_magic) {
860         case LUSTRE_MSG_MAGIC_V2: {
861                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
862                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
863                 pb->pb_flags = flags;
864                 return;
865         }
866         default:
867                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
868         }
869 }
870
871 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags)
872 {
873         switch (msg->lm_magic) {
874         case LUSTRE_MSG_MAGIC_V2: {
875                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
876                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
877                 pb->pb_flags &= ~(MSG_GEN_FLAG_MASK & flags);
878                 return;
879         }
880         default:
881                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
882         }
883 }
884
885 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg)
886 {
887         switch (msg->lm_magic) {
888         case LUSTRE_MSG_MAGIC_V2: {
889                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
890                 if (!pb) {
891                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
892                         return 0;
893                 }
894                 return pb->pb_op_flags;
895         }
896         default:
897                 return 0;
898         }
899 }
900
901 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags)
902 {
903         switch (msg->lm_magic) {
904         case LUSTRE_MSG_MAGIC_V2: {
905                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
906                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
907                 pb->pb_op_flags |= flags;
908                 return;
909         }
910         default:
911                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
912         }
913 }
914
915 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags)
916 {
917         switch (msg->lm_magic) {
918         case LUSTRE_MSG_MAGIC_V2: {
919                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
920                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
921                 pb->pb_op_flags |= flags;
922                 return;
923         }
924         default:
925                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
926         }
927 }
928
929 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg)
930 {
931         switch (msg->lm_magic) {
932         case LUSTRE_MSG_MAGIC_V2: {
933                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
934                 if (!pb) {
935                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
936                         return NULL;
937                 }
938                 return &pb->pb_handle;
939         }
940         default:
941                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
942                 return NULL;
943         }
944 }
945
946 __u32 lustre_msg_get_type(struct lustre_msg *msg)
947 {
948         switch (msg->lm_magic) {
949         case LUSTRE_MSG_MAGIC_V2: {
950                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
951                 if (!pb) {
952                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
953                         return PTL_RPC_MSG_ERR;
954                 }
955                 return pb->pb_type;
956         }
957         default:
958                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
959                 return PTL_RPC_MSG_ERR;
960         }
961 }
962
963 __u32 lustre_msg_get_version(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) {
969                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
970                         return 0;
971                 }
972                 return pb->pb_version;
973         }
974         default:
975                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
976                 return 0;
977         }
978 }
979
980 void lustre_msg_add_version(struct lustre_msg *msg, int version)
981 {
982         switch (msg->lm_magic) {
983         case LUSTRE_MSG_MAGIC_V2: {
984                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
985                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
986                 pb->pb_version |= version;
987                 return;
988         }
989         default:
990                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
991         }
992 }
993
994 __u32 lustre_msg_get_opc(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) {
1000                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1001                         return 0;
1002                 }
1003                 return pb->pb_opc;
1004         }
1005         default:
1006                 CERROR("incorrect message magic: %08x(msg:%p)\n", msg->lm_magic, msg);
1007                 LBUG();
1008                 return 0;
1009         }
1010 }
1011
1012 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg)
1013 {
1014         switch (msg->lm_magic) {
1015         case LUSTRE_MSG_MAGIC_V2: {
1016                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1017                 if (!pb) {
1018                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1019                         return 0;
1020                 }
1021                 return pb->pb_last_xid;
1022         }
1023         default:
1024                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1025                 return 0;
1026         }
1027 }
1028
1029 __u64 lustre_msg_get_last_committed(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) {
1035                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1036                         return 0;
1037                 }
1038                 return pb->pb_last_committed;
1039         }
1040         default:
1041                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1042                 return 0;
1043         }
1044 }
1045
1046 __u64 *lustre_msg_get_versions(struct lustre_msg *msg)
1047 {
1048         switch (msg->lm_magic) {
1049         case LUSTRE_MSG_MAGIC_V1:
1050                 return NULL;
1051         case LUSTRE_MSG_MAGIC_V2: {
1052                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1053                 if (!pb) {
1054                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1055                         return NULL;
1056                 }
1057                 return pb->pb_pre_versions;
1058         }
1059         default:
1060                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1061                 return NULL;
1062         }
1063 }
1064
1065 __u64 lustre_msg_get_transno(struct lustre_msg *msg)
1066 {
1067         switch (msg->lm_magic) {
1068         case LUSTRE_MSG_MAGIC_V2: {
1069                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1070                 if (!pb) {
1071                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1072                         return 0;
1073                 }
1074                 return pb->pb_transno;
1075         }
1076         default:
1077                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1078                 return 0;
1079         }
1080 }
1081
1082 int lustre_msg_get_status(struct lustre_msg *msg)
1083 {
1084         switch (msg->lm_magic) {
1085         case LUSTRE_MSG_MAGIC_V2: {
1086                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1087                 if (!pb) {
1088                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1089                         return -EINVAL;
1090                 }
1091                 return pb->pb_status;
1092         }
1093         default:
1094                 /* status might be printed in debug code while message
1095                  * uninitialized */
1096                 return -EINVAL;
1097         }
1098 }
1099
1100 __u64 lustre_msg_get_slv(struct lustre_msg *msg)
1101 {
1102         switch (msg->lm_magic) {
1103         case LUSTRE_MSG_MAGIC_V2: {
1104                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1105                 if (!pb) {
1106                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1107                         return -EINVAL;
1108                 }
1109                 return pb->pb_slv;
1110         }
1111         default:
1112                 CERROR("invalid msg magic %08x\n", msg->lm_magic);
1113                 return -EINVAL;
1114         }
1115 }
1116
1117
1118 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv)
1119 {
1120         switch (msg->lm_magic) {
1121         case LUSTRE_MSG_MAGIC_V2: {
1122                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1123                 if (!pb) {
1124                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1125                         return;
1126                 }
1127                 pb->pb_slv = slv;
1128                 return;
1129         }
1130         default:
1131                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1132                 return;
1133         }
1134 }
1135
1136 __u32 lustre_msg_get_limit(struct lustre_msg *msg)
1137 {
1138         switch (msg->lm_magic) {
1139         case LUSTRE_MSG_MAGIC_V2: {
1140                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1141                 if (!pb) {
1142                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1143                         return -EINVAL;
1144                 }
1145                 return pb->pb_limit;
1146         }
1147         default:
1148                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1149                 return -EINVAL;
1150         }
1151 }
1152
1153
1154 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit)
1155 {
1156         switch (msg->lm_magic) {
1157         case LUSTRE_MSG_MAGIC_V2: {
1158                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1159                 if (!pb) {
1160                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1161                         return;
1162                 }
1163                 pb->pb_limit = limit;
1164                 return;
1165         }
1166         default:
1167                 CERROR("invalid msg magic %08x\n", msg->lm_magic);
1168                 return;
1169         }
1170 }
1171
1172 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg)
1173 {
1174         switch (msg->lm_magic) {
1175         case LUSTRE_MSG_MAGIC_V2: {
1176                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1177                 if (!pb) {
1178                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1179                         return 0;
1180                 }
1181                 return pb->pb_conn_cnt;
1182         }
1183         default:
1184                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1185                 return 0;
1186         }
1187 }
1188
1189 int lustre_msg_is_v1(struct lustre_msg *msg)
1190 {
1191         switch (msg->lm_magic) {
1192         case LUSTRE_MSG_MAGIC_V1:
1193         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1194                 return 1;
1195         default:
1196                 return 0;
1197         }
1198 }
1199
1200 __u32 lustre_msg_get_magic(struct lustre_msg *msg)
1201 {
1202         switch (msg->lm_magic) {
1203         case LUSTRE_MSG_MAGIC_V2:
1204                 return msg->lm_magic;
1205         default:
1206                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1207                 return 0;
1208         }
1209 }
1210
1211 __u32 lustre_msg_get_timeout(struct lustre_msg *msg)
1212 {
1213         switch (msg->lm_magic) {
1214         case LUSTRE_MSG_MAGIC_V1:
1215         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1216                 return 0;
1217         case LUSTRE_MSG_MAGIC_V2: {
1218                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1219                 if (!pb) {
1220                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1221                         return 0;
1222
1223                 }
1224                 return pb->pb_timeout;
1225         }
1226         default:
1227                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1228                 return 0;
1229         }
1230 }
1231
1232 __u32 lustre_msg_get_service_time(struct lustre_msg *msg)
1233 {
1234         switch (msg->lm_magic) {
1235         case LUSTRE_MSG_MAGIC_V1:
1236         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1237                 return 0;
1238         case LUSTRE_MSG_MAGIC_V2: {
1239                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1240                 if (!pb) {
1241                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1242                         return 0;
1243
1244                 }
1245                 return pb->pb_service_time;
1246         }
1247         default:
1248                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1249                 return 0;
1250         }
1251 }
1252
1253 char *lustre_msg_get_jobid(struct lustre_msg *msg)
1254 {
1255         switch (msg->lm_magic) {
1256         case LUSTRE_MSG_MAGIC_V1:
1257         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1258                 return NULL;
1259         case LUSTRE_MSG_MAGIC_V2: {
1260                 struct ptlrpc_body *pb =
1261                         lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1262                                           sizeof(struct ptlrpc_body));
1263                 if (!pb)
1264                         return NULL;
1265
1266                 return pb->pb_jobid;
1267         }
1268         default:
1269                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1270                 return NULL;
1271         }
1272 }
1273 EXPORT_SYMBOL(lustre_msg_get_jobid);
1274
1275 __u32 lustre_msg_get_cksum(struct lustre_msg *msg)
1276 {
1277         switch (msg->lm_magic) {
1278         case LUSTRE_MSG_MAGIC_V2:
1279                 return msg->lm_cksum;
1280         default:
1281                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1282                 return 0;
1283         }
1284 }
1285
1286 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 0, 0)
1287 /*
1288  * In 1.6 and 1.8 the checksum was computed only on struct ptlrpc_body as
1289  * it was in 1.6 (88 bytes, smaller than the full size in 1.8).  It makes
1290  * more sense to compute the checksum on the full ptlrpc_body, regardless
1291  * of what size it is, but in order to keep interoperability with 1.8 we
1292  * can optionally also checksum only the first 88 bytes (caller decides). */
1293 # define ptlrpc_body_cksum_size_compat18         88
1294
1295 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg, int compat18)
1296 #else
1297 # warning "remove checksum compatibility support for b1_8"
1298 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg)
1299 #endif
1300 {
1301         switch (msg->lm_magic) {
1302         case LUSTRE_MSG_MAGIC_V2: {
1303                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1304 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 0, 0)
1305                 __u32 len = compat18 ? ptlrpc_body_cksum_size_compat18 :
1306                             lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF);
1307                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1308                 return crc32_le(~(__u32)0, (unsigned char *)pb, len);
1309 #else
1310 # warning "remove checksum compatibility support for b1_8"
1311                 return crc32_le(~(__u32)0, (unsigned char *)pb,
1312                                 lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF));
1313 #endif
1314         }
1315         default:
1316                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1317                 return 0;
1318         }
1319 }
1320
1321 void lustre_msg_set_handle(struct lustre_msg *msg, struct lustre_handle *handle)
1322 {
1323         switch (msg->lm_magic) {
1324         case LUSTRE_MSG_MAGIC_V2: {
1325                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1326                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1327                 pb->pb_handle = *handle;
1328                 return;
1329         }
1330         default:
1331                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1332         }
1333 }
1334
1335 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type)
1336 {
1337         switch (msg->lm_magic) {
1338         case LUSTRE_MSG_MAGIC_V2: {
1339                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1340                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1341                 pb->pb_type = type;
1342                 return;
1343         }
1344         default:
1345                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1346         }
1347 }
1348
1349 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc)
1350 {
1351         switch (msg->lm_magic) {
1352         case LUSTRE_MSG_MAGIC_V2: {
1353                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1354                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1355                 pb->pb_opc = opc;
1356                 return;
1357         }
1358         default:
1359                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1360         }
1361 }
1362
1363 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid)
1364 {
1365         switch (msg->lm_magic) {
1366         case LUSTRE_MSG_MAGIC_V2: {
1367                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1368                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1369                 pb->pb_last_xid = last_xid;
1370                 return;
1371         }
1372         default:
1373                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1374         }
1375 }
1376
1377 void lustre_msg_set_last_committed(struct lustre_msg *msg, __u64 last_committed)
1378 {
1379         switch (msg->lm_magic) {
1380         case LUSTRE_MSG_MAGIC_V2: {
1381                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1382                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1383                 pb->pb_last_committed = last_committed;
1384                 return;
1385         }
1386         default:
1387                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1388         }
1389 }
1390
1391 void lustre_msg_set_versions(struct lustre_msg *msg, __u64 *versions)
1392 {
1393         switch (msg->lm_magic) {
1394         case LUSTRE_MSG_MAGIC_V1:
1395                 return;
1396         case LUSTRE_MSG_MAGIC_V2: {
1397                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1398                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1399                 pb->pb_pre_versions[0] = versions[0];
1400                 pb->pb_pre_versions[1] = versions[1];
1401                 pb->pb_pre_versions[2] = versions[2];
1402                 pb->pb_pre_versions[3] = versions[3];
1403                 return;
1404         }
1405         default:
1406                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1407         }
1408 }
1409
1410 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno)
1411 {
1412         switch (msg->lm_magic) {
1413         case LUSTRE_MSG_MAGIC_V2: {
1414                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1415                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1416                 pb->pb_transno = transno;
1417                 return;
1418         }
1419         default:
1420                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1421         }
1422 }
1423
1424 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status)
1425 {
1426         switch (msg->lm_magic) {
1427         case LUSTRE_MSG_MAGIC_V2: {
1428                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1429                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1430                 pb->pb_status = status;
1431                 return;
1432         }
1433         default:
1434                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1435         }
1436 }
1437
1438 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt)
1439 {
1440         switch (msg->lm_magic) {
1441         case LUSTRE_MSG_MAGIC_V2: {
1442                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1443                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1444                 pb->pb_conn_cnt = conn_cnt;
1445                 return;
1446         }
1447         default:
1448                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1449         }
1450 }
1451
1452 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout)
1453 {
1454         switch (msg->lm_magic) {
1455         case LUSTRE_MSG_MAGIC_V1:
1456                 return;
1457         case LUSTRE_MSG_MAGIC_V2: {
1458                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1459                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1460                 pb->pb_timeout = timeout;
1461                 return;
1462         }
1463         default:
1464                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1465         }
1466 }
1467
1468 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time)
1469 {
1470         switch (msg->lm_magic) {
1471         case LUSTRE_MSG_MAGIC_V1:
1472                 return;
1473         case LUSTRE_MSG_MAGIC_V2: {
1474                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1475                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1476                 pb->pb_service_time = service_time;
1477                 return;
1478         }
1479         default:
1480                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1481         }
1482 }
1483
1484 void lustre_msg_set_jobid(struct lustre_msg *msg, char *jobid)
1485 {
1486         switch (msg->lm_magic) {
1487         case LUSTRE_MSG_MAGIC_V1:
1488                 return;
1489         case LUSTRE_MSG_MAGIC_V2: {
1490                 __u32 opc = lustre_msg_get_opc(msg);
1491                 struct ptlrpc_body *pb;
1492
1493                 /* Don't set jobid for ldlm ast RPCs, they've been shrinked.
1494                  * See the comment in ptlrpc_request_pack(). */
1495                 if (!opc || opc == LDLM_BL_CALLBACK ||
1496                     opc == LDLM_CP_CALLBACK || opc == LDLM_GL_CALLBACK)
1497                         return;
1498
1499                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1500                                        sizeof(struct ptlrpc_body));
1501                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1502                 memcpy(pb->pb_jobid, jobid, JOBSTATS_JOBID_SIZE);
1503                 return;
1504         }
1505         default:
1506                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1507         }
1508 }
1509 EXPORT_SYMBOL(lustre_msg_set_jobid);
1510
1511 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum)
1512 {
1513         switch (msg->lm_magic) {
1514         case LUSTRE_MSG_MAGIC_V1:
1515                 return;
1516         case LUSTRE_MSG_MAGIC_V2:
1517                 msg->lm_cksum = cksum;
1518                 return;
1519         default:
1520                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1521         }
1522 }
1523
1524
1525 void ptlrpc_request_set_replen(struct ptlrpc_request *req)
1526 {
1527         int count = req_capsule_filled_sizes(&req->rq_pill, RCL_SERVER);
1528
1529         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count,
1530                                          req->rq_pill.rc_area[RCL_SERVER]);
1531         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1532                 req->rq_reqmsg->lm_repsize = req->rq_replen;
1533 }
1534
1535 void ptlrpc_req_set_repsize(struct ptlrpc_request *req, int count, __u32 *lens)
1536 {
1537         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens);
1538         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1539                 req->rq_reqmsg->lm_repsize = req->rq_replen;
1540 }
1541
1542 /**
1543  * Send a remote set_info_async.
1544  *
1545  * This may go from client to server or server to client.
1546  */
1547 int do_set_info_async(struct obd_import *imp,
1548                       int opcode, int version,
1549                       obd_count keylen, void *key,
1550                       obd_count vallen, void *val,
1551                       struct ptlrpc_request_set *set)
1552 {
1553         struct ptlrpc_request *req;
1554         char                  *tmp;
1555         int                    rc;
1556         ENTRY;
1557
1558         req = ptlrpc_request_alloc(imp, &RQF_OBD_SET_INFO);
1559         if (req == NULL)
1560                 RETURN(-ENOMEM);
1561
1562         req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_KEY,
1563                              RCL_CLIENT, keylen);
1564         req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_VAL,
1565                              RCL_CLIENT, vallen);
1566         rc = ptlrpc_request_pack(req, version, opcode);
1567         if (rc) {
1568                 ptlrpc_request_free(req);
1569                 RETURN(rc);
1570         }
1571
1572         tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1573         memcpy(tmp, key, keylen);
1574         tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1575         memcpy(tmp, val, vallen);
1576
1577         ptlrpc_request_set_replen(req);
1578
1579         if (set) {
1580                 ptlrpc_set_add_req(set, req);
1581                 ptlrpc_check_set(NULL, set);
1582         } else {
1583                 rc = ptlrpc_queue_wait(req);
1584                 ptlrpc_req_finished(req);
1585         }
1586
1587         RETURN(rc);
1588 }
1589 EXPORT_SYMBOL(do_set_info_async);
1590
1591 /* byte flipping routines for all wire types declared in
1592  * lustre_idl.h implemented here.
1593  */
1594 void lustre_swab_ptlrpc_body(struct ptlrpc_body *b)
1595 {
1596         __swab32s (&b->pb_type);
1597         __swab32s (&b->pb_version);
1598         __swab32s (&b->pb_opc);
1599         __swab32s (&b->pb_status);
1600         __swab64s (&b->pb_last_xid);
1601         __swab64s (&b->pb_last_seen);
1602         __swab64s (&b->pb_last_committed);
1603         __swab64s (&b->pb_transno);
1604         __swab32s (&b->pb_flags);
1605         __swab32s (&b->pb_op_flags);
1606         __swab32s (&b->pb_conn_cnt);
1607         __swab32s (&b->pb_timeout);
1608         __swab32s (&b->pb_service_time);
1609         __swab32s (&b->pb_limit);
1610         __swab64s (&b->pb_slv);
1611         __swab64s (&b->pb_pre_versions[0]);
1612         __swab64s (&b->pb_pre_versions[1]);
1613         __swab64s (&b->pb_pre_versions[2]);
1614         __swab64s (&b->pb_pre_versions[3]);
1615         CLASSERT(offsetof(typeof(*b), pb_padding) != 0);
1616         /* While we need to maintain compatibility between
1617          * clients and servers without ptlrpc_body_v2 (< 2.3)
1618          * do not swab any fields beyond pb_jobid, as we are
1619          * using this swab function for both ptlrpc_body
1620          * and ptlrpc_body_v2. */
1621         CLASSERT(offsetof(typeof(*b), pb_jobid) != 0);
1622 }
1623
1624 void lustre_swab_connect(struct obd_connect_data *ocd)
1625 {
1626         __swab64s(&ocd->ocd_connect_flags);
1627         __swab32s(&ocd->ocd_version);
1628         __swab32s(&ocd->ocd_grant);
1629         __swab64s(&ocd->ocd_ibits_known);
1630         __swab32s(&ocd->ocd_index);
1631         __swab32s(&ocd->ocd_brw_size);
1632         /* ocd_blocksize and ocd_inodespace don't need to be swabbed because
1633          * they are 8-byte values */
1634         __swab16s(&ocd->ocd_grant_extent);
1635         __swab32s(&ocd->ocd_unused);
1636         __swab64s(&ocd->ocd_transno);
1637         __swab32s(&ocd->ocd_group);
1638         __swab32s(&ocd->ocd_cksum_types);
1639         __swab32s(&ocd->ocd_instance);
1640         /* Fields after ocd_cksum_types are only accessible by the receiver
1641          * if the corresponding flag in ocd_connect_flags is set. Accessing
1642          * any field after ocd_maxbytes on the receiver without a valid flag
1643          * may result in out-of-bound memory access and kernel oops. */
1644         if (ocd->ocd_connect_flags & OBD_CONNECT_MAX_EASIZE)
1645                 __swab32s(&ocd->ocd_max_easize);
1646         if (ocd->ocd_connect_flags & OBD_CONNECT_MAXBYTES)
1647                 __swab64s(&ocd->ocd_maxbytes);
1648         CLASSERT(offsetof(typeof(*ocd), padding1) != 0);
1649         CLASSERT(offsetof(typeof(*ocd), padding2) != 0);
1650         CLASSERT(offsetof(typeof(*ocd), padding3) != 0);
1651         CLASSERT(offsetof(typeof(*ocd), padding4) != 0);
1652         CLASSERT(offsetof(typeof(*ocd), padding5) != 0);
1653         CLASSERT(offsetof(typeof(*ocd), padding6) != 0);
1654         CLASSERT(offsetof(typeof(*ocd), padding7) != 0);
1655         CLASSERT(offsetof(typeof(*ocd), padding8) != 0);
1656         CLASSERT(offsetof(typeof(*ocd), padding9) != 0);
1657         CLASSERT(offsetof(typeof(*ocd), paddingA) != 0);
1658         CLASSERT(offsetof(typeof(*ocd), paddingB) != 0);
1659         CLASSERT(offsetof(typeof(*ocd), paddingC) != 0);
1660         CLASSERT(offsetof(typeof(*ocd), paddingD) != 0);
1661         CLASSERT(offsetof(typeof(*ocd), paddingE) != 0);
1662         CLASSERT(offsetof(typeof(*ocd), paddingF) != 0);
1663 }
1664
1665 void lustre_swab_obdo (struct obdo  *o)
1666 {
1667         __swab64s (&o->o_valid);
1668         __swab64s (&o->o_id);
1669         __swab64s (&o->o_seq);
1670         __swab64s (&o->o_parent_seq);
1671         __swab64s (&o->o_size);
1672         __swab64s (&o->o_mtime);
1673         __swab64s (&o->o_atime);
1674         __swab64s (&o->o_ctime);
1675         __swab64s (&o->o_blocks);
1676         __swab64s (&o->o_grant);
1677         __swab32s (&o->o_blksize);
1678         __swab32s (&o->o_mode);
1679         __swab32s (&o->o_uid);
1680         __swab32s (&o->o_gid);
1681         __swab32s (&o->o_flags);
1682         __swab32s (&o->o_nlink);
1683         __swab32s (&o->o_parent_oid);
1684         __swab32s (&o->o_misc);
1685         __swab64s (&o->o_ioepoch);
1686         __swab32s (&o->o_stripe_idx);
1687         __swab32s (&o->o_parent_ver);
1688         /* o_handle is opaque */
1689         /* o_lcookie is swabbed elsewhere */
1690         __swab32s (&o->o_uid_h);
1691         __swab32s (&o->o_gid_h);
1692         __swab64s (&o->o_data_version);
1693         CLASSERT(offsetof(typeof(*o), o_padding_4) != 0);
1694         CLASSERT(offsetof(typeof(*o), o_padding_5) != 0);
1695         CLASSERT(offsetof(typeof(*o), o_padding_6) != 0);
1696
1697 }
1698
1699 void lustre_swab_obd_statfs (struct obd_statfs *os)
1700 {
1701         __swab64s (&os->os_type);
1702         __swab64s (&os->os_blocks);
1703         __swab64s (&os->os_bfree);
1704         __swab64s (&os->os_bavail);
1705         __swab64s (&os->os_files);
1706         __swab64s (&os->os_ffree);
1707         /* no need to swab os_fsid */
1708         __swab32s (&os->os_bsize);
1709         __swab32s (&os->os_namelen);
1710         __swab64s (&os->os_maxbytes);
1711         __swab32s (&os->os_state);
1712         CLASSERT(offsetof(typeof(*os), os_spare1) != 0);
1713         CLASSERT(offsetof(typeof(*os), os_spare2) != 0);
1714         CLASSERT(offsetof(typeof(*os), os_spare3) != 0);
1715         CLASSERT(offsetof(typeof(*os), os_spare4) != 0);
1716         CLASSERT(offsetof(typeof(*os), os_spare5) != 0);
1717         CLASSERT(offsetof(typeof(*os), os_spare6) != 0);
1718         CLASSERT(offsetof(typeof(*os), os_spare7) != 0);
1719         CLASSERT(offsetof(typeof(*os), os_spare8) != 0);
1720         CLASSERT(offsetof(typeof(*os), os_spare9) != 0);
1721 }
1722
1723 void lustre_swab_obd_ioobj (struct obd_ioobj *ioo)
1724 {
1725         __swab64s (&ioo->ioo_id);
1726         __swab64s (&ioo->ioo_seq);
1727         __swab32s (&ioo->ioo_type);
1728         __swab32s (&ioo->ioo_bufcnt);
1729 }
1730
1731 void lustre_swab_niobuf_remote (struct niobuf_remote *nbr)
1732 {
1733         __swab64s (&nbr->offset);
1734         __swab32s (&nbr->len);
1735         __swab32s (&nbr->flags);
1736 }
1737
1738 void lustre_swab_ost_body (struct ost_body *b)
1739 {
1740         lustre_swab_obdo (&b->oa);
1741 }
1742
1743 void lustre_swab_ost_last_id(obd_id *id)
1744 {
1745         __swab64s(id);
1746 }
1747
1748 void lustre_swab_generic_32s(__u32 *val)
1749 {
1750         __swab32s(val);
1751 }
1752
1753 void lustre_swab_lvb(union ldlm_wire_lvb *lvb)
1754 {
1755         /* The ldlm_wire_lvb union represents all the possible LVB types.
1756          * Unfortunately, there is no way to know what member of the union we
1757          * are dealing with at this point. Therefore, all LVB structures must
1758          * have fields of the same types, although used for different purposes
1759          */
1760         __swab64s(&lvb->l_ost.lvb_size);
1761         __swab64s(&lvb->l_ost.lvb_mtime);
1762         __swab64s(&lvb->l_ost.lvb_atime);
1763         __swab64s(&lvb->l_ost.lvb_ctime);
1764         __swab64s(&lvb->l_ost.lvb_blocks);
1765 }
1766
1767 void lustre_swab_mdt_body (struct mdt_body *b)
1768 {
1769         lustre_swab_lu_fid (&b->fid1);
1770         lustre_swab_lu_fid (&b->fid2);
1771         /* handle is opaque */
1772         __swab64s (&b->valid);
1773         __swab64s (&b->size);
1774         __swab64s (&b->mtime);
1775         __swab64s (&b->atime);
1776         __swab64s (&b->ctime);
1777         __swab64s (&b->blocks);
1778         __swab64s (&b->ioepoch);
1779         __swab64s (&b->ino);
1780         __swab32s (&b->fsuid);
1781         __swab32s (&b->fsgid);
1782         __swab32s (&b->capability);
1783         __swab32s (&b->mode);
1784         __swab32s (&b->uid);
1785         __swab32s (&b->gid);
1786         __swab32s (&b->flags);
1787         __swab32s (&b->rdev);
1788         __swab32s (&b->nlink);
1789         __swab32s (&b->generation);
1790         __swab32s (&b->suppgid);
1791         __swab32s (&b->eadatasize);
1792         __swab32s (&b->aclsize);
1793         __swab32s (&b->max_mdsize);
1794         __swab32s (&b->max_cookiesize);
1795         __swab32s (&b->uid_h);
1796         __swab32s (&b->gid_h);
1797         CLASSERT(offsetof(typeof(*b), padding_5) != 0);
1798 }
1799
1800 void lustre_swab_mdt_ioepoch (struct mdt_ioepoch *b)
1801 {
1802         /* handle is opaque */
1803          __swab64s (&b->ioepoch);
1804          __swab32s (&b->flags);
1805          CLASSERT(offsetof(typeof(*b), padding) != 0);
1806 }
1807
1808 void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
1809 {
1810         int i;
1811         __swab32s(&mti->mti_lustre_ver);
1812         __swab32s(&mti->mti_stripe_index);
1813         __swab32s(&mti->mti_config_ver);
1814         __swab32s(&mti->mti_flags);
1815         __swab32s(&mti->mti_instance);
1816         __swab32s(&mti->mti_nid_count);
1817         CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
1818         for (i = 0; i < MTI_NIDS_MAX; i++)
1819                 __swab64s(&mti->mti_nids[i]);
1820 }
1821
1822 void lustre_swab_mgs_nidtbl_entry(struct mgs_nidtbl_entry *entry)
1823 {
1824         int i;
1825
1826         __swab64s(&entry->mne_version);
1827         __swab32s(&entry->mne_instance);
1828         __swab32s(&entry->mne_index);
1829         __swab32s(&entry->mne_length);
1830
1831         /* mne_nid_(count|type) must be one byte size because we're gonna
1832          * access it w/o swapping. */
1833         CLASSERT(sizeof(entry->mne_nid_count) == sizeof(__u8));
1834         CLASSERT(sizeof(entry->mne_nid_type) == sizeof(__u8));
1835
1836         /* remove this assertion if ipv6 is supported. */
1837         LASSERT(entry->mne_nid_type == 0);
1838         for (i = 0; i < entry->mne_nid_count; i++) {
1839                 CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
1840                 __swab64s(&entry->u.nids[i]);
1841         }
1842 }
1843 EXPORT_SYMBOL(lustre_swab_mgs_nidtbl_entry);
1844
1845 void lustre_swab_mgs_config_body(struct mgs_config_body *body)
1846 {
1847         __swab64s(&body->mcb_offset);
1848         __swab32s(&body->mcb_units);
1849         __swab16s(&body->mcb_type);
1850 }
1851 EXPORT_SYMBOL(lustre_swab_mgs_config_body);
1852
1853 void lustre_swab_mgs_config_res(struct mgs_config_res *body)
1854 {
1855         __swab64s(&body->mcr_offset);
1856         __swab64s(&body->mcr_size);
1857 }
1858 EXPORT_SYMBOL(lustre_swab_mgs_config_res);
1859
1860 static void lustre_swab_obd_dqinfo (struct obd_dqinfo *i)
1861 {
1862         __swab64s (&i->dqi_bgrace);
1863         __swab64s (&i->dqi_igrace);
1864         __swab32s (&i->dqi_flags);
1865         __swab32s (&i->dqi_valid);
1866 }
1867
1868 static void lustre_swab_obd_dqblk (struct obd_dqblk *b)
1869 {
1870         __swab64s (&b->dqb_ihardlimit);
1871         __swab64s (&b->dqb_isoftlimit);
1872         __swab64s (&b->dqb_curinodes);
1873         __swab64s (&b->dqb_bhardlimit);
1874         __swab64s (&b->dqb_bsoftlimit);
1875         __swab64s (&b->dqb_curspace);
1876         __swab64s (&b->dqb_btime);
1877         __swab64s (&b->dqb_itime);
1878         __swab32s (&b->dqb_valid);
1879         CLASSERT(offsetof(typeof(*b), dqb_padding) != 0);
1880 }
1881
1882 void lustre_swab_obd_quotactl (struct obd_quotactl *q)
1883 {
1884         __swab32s (&q->qc_cmd);
1885         __swab32s (&q->qc_type);
1886         __swab32s (&q->qc_id);
1887         __swab32s (&q->qc_stat);
1888         lustre_swab_obd_dqinfo (&q->qc_dqinfo);
1889         lustre_swab_obd_dqblk (&q->qc_dqblk);
1890 }
1891
1892 void lustre_swab_quota_adjust_qunit (struct quota_adjust_qunit *q)
1893 {
1894         __swab32s (&q->qaq_flags);
1895         __swab32s (&q->qaq_id);
1896         __swab64s (&q->qaq_bunit_sz);
1897         __swab64s (&q->qaq_iunit_sz);
1898         __swab64s (&q->padding1);
1899 }
1900
1901 void lustre_swab_mdt_remote_perm (struct mdt_remote_perm *p)
1902 {
1903         __swab32s (&p->rp_uid);
1904         __swab32s (&p->rp_gid);
1905         __swab32s (&p->rp_fsuid);
1906         __swab32s (&p->rp_fsuid_h);
1907         __swab32s (&p->rp_fsgid);
1908         __swab32s (&p->rp_fsgid_h);
1909         __swab32s (&p->rp_access_perm);
1910         __swab32s (&p->rp_padding);
1911 };
1912
1913 void lustre_swab_fid2path(struct getinfo_fid2path *gf)
1914 {
1915         lustre_swab_lu_fid(&gf->gf_fid);
1916         __swab64s(&gf->gf_recno);
1917         __swab32s(&gf->gf_linkno);
1918         __swab32s(&gf->gf_pathlen);
1919 }
1920 EXPORT_SYMBOL(lustre_swab_fid2path);
1921
1922 void lustre_swab_fiemap_extent(struct ll_fiemap_extent *fm_extent)
1923 {
1924         __swab64s(&fm_extent->fe_logical);
1925         __swab64s(&fm_extent->fe_physical);
1926         __swab64s(&fm_extent->fe_length);
1927         __swab32s(&fm_extent->fe_flags);
1928         __swab32s(&fm_extent->fe_device);
1929 }
1930
1931 void lustre_swab_fiemap(struct ll_user_fiemap *fiemap)
1932 {
1933         int i;
1934
1935         __swab64s(&fiemap->fm_start);
1936         __swab64s(&fiemap->fm_length);
1937         __swab32s(&fiemap->fm_flags);
1938         __swab32s(&fiemap->fm_mapped_extents);
1939         __swab32s(&fiemap->fm_extent_count);
1940         __swab32s(&fiemap->fm_reserved);
1941
1942         for (i = 0; i < fiemap->fm_mapped_extents; i++)
1943                 lustre_swab_fiemap_extent(&fiemap->fm_extents[i]);
1944 }
1945
1946 void lustre_swab_mdt_rec_reint (struct mdt_rec_reint *rr)
1947 {
1948         __swab32s (&rr->rr_opcode);
1949         __swab32s (&rr->rr_cap);
1950         __swab32s (&rr->rr_fsuid);
1951         /* rr_fsuid_h is unused */
1952         __swab32s (&rr->rr_fsgid);
1953         /* rr_fsgid_h is unused */
1954         __swab32s (&rr->rr_suppgid1);
1955         /* rr_suppgid1_h is unused */
1956         __swab32s (&rr->rr_suppgid2);
1957         /* rr_suppgid2_h is unused */
1958         lustre_swab_lu_fid (&rr->rr_fid1);
1959         lustre_swab_lu_fid (&rr->rr_fid2);
1960         __swab64s (&rr->rr_mtime);
1961         __swab64s (&rr->rr_atime);
1962         __swab64s (&rr->rr_ctime);
1963         __swab64s (&rr->rr_size);
1964         __swab64s (&rr->rr_blocks);
1965         __swab32s (&rr->rr_bias);
1966         __swab32s (&rr->rr_mode);
1967         __swab32s (&rr->rr_flags);
1968
1969         CLASSERT(offsetof(typeof(*rr), rr_padding_2) != 0);
1970         CLASSERT(offsetof(typeof(*rr), rr_padding_3) != 0);
1971         CLASSERT(offsetof(typeof(*rr), rr_padding_4) != 0);
1972 };
1973
1974 void lustre_swab_lov_desc (struct lov_desc *ld)
1975 {
1976         __swab32s (&ld->ld_tgt_count);
1977         __swab32s (&ld->ld_active_tgt_count);
1978         __swab32s (&ld->ld_default_stripe_count);
1979         __swab32s (&ld->ld_pattern);
1980         __swab64s (&ld->ld_default_stripe_size);
1981         __swab64s (&ld->ld_default_stripe_offset);
1982         __swab32s (&ld->ld_qos_maxage);
1983         /* uuid endian insensitive */
1984 }
1985
1986 void lustre_swab_lmv_desc (struct lmv_desc *ld)
1987 {
1988         __swab32s (&ld->ld_tgt_count);
1989         __swab32s (&ld->ld_active_tgt_count);
1990         __swab32s (&ld->ld_default_stripe_count);
1991         __swab32s (&ld->ld_pattern);
1992         __swab64s (&ld->ld_default_hash_size);
1993         __swab32s (&ld->ld_qos_maxage);
1994         /* uuid endian insensitive */
1995 }
1996
1997 void lustre_swab_lmv_stripe_md (struct lmv_stripe_md *mea)
1998 {
1999         __swab32s(&mea->mea_magic);
2000         __swab32s(&mea->mea_count);
2001         __swab32s(&mea->mea_master);
2002         CLASSERT(offsetof(typeof(*mea), mea_padding) != 0);
2003 }
2004
2005
2006 static void print_lum (struct lov_user_md *lum)
2007 {
2008         CDEBUG(D_OTHER, "lov_user_md %p:\n", lum);
2009         CDEBUG(D_OTHER, "\tlmm_magic: %#x\n", lum->lmm_magic);
2010         CDEBUG(D_OTHER, "\tlmm_pattern: %#x\n", lum->lmm_pattern);
2011         CDEBUG(D_OTHER, "\tlmm_object_id: "LPU64"\n", lum->lmm_object_id);
2012         CDEBUG(D_OTHER, "\tlmm_object_gr: "LPU64"\n", lum->lmm_object_seq);
2013         CDEBUG(D_OTHER, "\tlmm_stripe_size: %#x\n", lum->lmm_stripe_size);
2014         CDEBUG(D_OTHER, "\tlmm_stripe_count: %#x\n", lum->lmm_stripe_count);
2015         CDEBUG(D_OTHER, "\tlmm_stripe_offset/lmm_layout_gen: %#x\n",
2016                lum->u.lum_stripe_offset);
2017 }
2018
2019 static void lustre_swab_lov_user_md_common(struct lov_user_md_v1 *lum)
2020 {
2021         ENTRY;
2022         __swab32s(&lum->lmm_magic);
2023         __swab32s(&lum->lmm_pattern);
2024         __swab64s(&lum->lmm_object_id);
2025         __swab64s(&lum->lmm_object_seq);
2026         __swab32s(&lum->lmm_stripe_size);
2027         __swab16s(&lum->lmm_stripe_count);
2028         __swab16s(&lum->u.lum_stripe_offset);
2029         print_lum(lum);
2030         EXIT;
2031 }
2032
2033 void lustre_swab_lov_user_md_v1(struct lov_user_md_v1 *lum)
2034 {
2035         ENTRY;
2036         CDEBUG(D_IOCTL, "swabbing lov_user_md v1\n");
2037         lustre_swab_lov_user_md_common(lum);
2038         EXIT;
2039 }
2040
2041 void lustre_swab_lov_user_md_v3(struct lov_user_md_v3 *lum)
2042 {
2043         ENTRY;
2044         CDEBUG(D_IOCTL, "swabbing lov_user_md v3\n");
2045         lustre_swab_lov_user_md_common((struct lov_user_md_v1 *)lum);
2046         /* lmm_pool_name nothing to do with char */
2047         EXIT;
2048 }
2049
2050 void lustre_swab_lov_mds_md(struct lov_mds_md *lmm)
2051 {
2052         ENTRY;
2053         CDEBUG(D_IOCTL, "swabbing lov_mds_md\n");
2054         __swab32s(&lmm->lmm_magic);
2055         __swab32s(&lmm->lmm_pattern);
2056         __swab64s(&lmm->lmm_object_id);
2057         __swab64s(&lmm->lmm_object_seq);
2058         __swab32s(&lmm->lmm_stripe_size);
2059         __swab16s(&lmm->lmm_stripe_count);
2060         __swab16s(&lmm->lmm_layout_gen);
2061         EXIT;
2062 }
2063
2064 void lustre_swab_lov_user_md_objects(struct lov_user_ost_data *lod,
2065                                      int stripe_count)
2066 {
2067         int i;
2068         ENTRY;
2069         for (i = 0; i < stripe_count; i++) {
2070                 __swab64s(&(lod[i].l_object_id));
2071                 __swab64s(&(lod[i].l_object_seq));
2072                 __swab32s(&(lod[i].l_ost_gen));
2073                 __swab32s(&(lod[i].l_ost_idx));
2074         }
2075         EXIT;
2076 }
2077
2078
2079 void lustre_swab_ldlm_res_id (struct ldlm_res_id *id)
2080 {
2081         int  i;
2082
2083         for (i = 0; i < RES_NAME_SIZE; i++)
2084                 __swab64s (&id->name[i]);
2085 }
2086
2087 void lustre_swab_ldlm_policy_data (ldlm_wire_policy_data_t *d)
2088 {
2089         /* the lock data is a union and the first two fields are always an
2090          * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
2091          * data the same way. */
2092         __swab64s(&d->l_extent.start);
2093         __swab64s(&d->l_extent.end);
2094         __swab64s(&d->l_extent.gid);
2095         __swab64s(&d->l_flock.lfw_owner);
2096         __swab32s(&d->l_flock.lfw_pid);
2097 }
2098
2099 void lustre_swab_ldlm_intent (struct ldlm_intent *i)
2100 {
2101         __swab64s (&i->opc);
2102 }
2103
2104 void lustre_swab_ldlm_resource_desc (struct ldlm_resource_desc *r)
2105 {
2106         __swab32s (&r->lr_type);
2107         CLASSERT(offsetof(typeof(*r), lr_padding) != 0);
2108         lustre_swab_ldlm_res_id (&r->lr_name);
2109 }
2110
2111 void lustre_swab_ldlm_lock_desc (struct ldlm_lock_desc *l)
2112 {
2113         lustre_swab_ldlm_resource_desc (&l->l_resource);
2114         __swab32s (&l->l_req_mode);
2115         __swab32s (&l->l_granted_mode);
2116         lustre_swab_ldlm_policy_data (&l->l_policy_data);
2117 }
2118
2119 void lustre_swab_ldlm_request (struct ldlm_request *rq)
2120 {
2121         __swab32s (&rq->lock_flags);
2122         lustre_swab_ldlm_lock_desc (&rq->lock_desc);
2123         __swab32s (&rq->lock_count);
2124         /* lock_handle[] opaque */
2125 }
2126
2127 void lustre_swab_ldlm_reply (struct ldlm_reply *r)
2128 {
2129         __swab32s (&r->lock_flags);
2130         CLASSERT(offsetof(typeof(*r), lock_padding) != 0);
2131         lustre_swab_ldlm_lock_desc (&r->lock_desc);
2132         /* lock_handle opaque */
2133         __swab64s (&r->lock_policy_res1);
2134         __swab64s (&r->lock_policy_res2);
2135 }
2136
2137 /* no one calls this */
2138 int llog_log_swabbed(struct llog_log_hdr *hdr)
2139 {
2140         if (hdr->llh_hdr.lrh_type == __swab32(LLOG_HDR_MAGIC))
2141                 return 1;
2142         if (hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC)
2143                 return 0;
2144         return -1;
2145 }
2146
2147 void lustre_swab_qdata(struct qunit_data *d)
2148 {
2149         __swab32s (&d->qd_id);
2150         __swab32s (&d->qd_flags);
2151         __swab64s (&d->qd_count);
2152         __swab64s (&d->qd_qunit);
2153         CLASSERT(offsetof(typeof(*d), padding) != 0);
2154 }
2155
2156 /* Dump functions */
2157 void dump_ioo(struct obd_ioobj *ioo)
2158 {
2159         CDEBUG(D_RPCTRACE,
2160                "obd_ioobj: ioo_id="LPD64", ioo_seq="LPD64", ioo_type=%d, "
2161                "ioo_bufct=%d\n", ioo->ioo_id, ioo->ioo_seq, ioo->ioo_type,
2162                ioo->ioo_bufcnt);
2163 }
2164
2165 void dump_rniobuf(struct niobuf_remote *nb)
2166 {
2167         CDEBUG(D_RPCTRACE, "niobuf_remote: offset="LPU64", len=%d, flags=%x\n",
2168                nb->offset, nb->len, nb->flags);
2169 }
2170
2171 void dump_obdo(struct obdo *oa)
2172 {
2173         __u32 valid = oa->o_valid;
2174
2175         CDEBUG(D_RPCTRACE, "obdo: o_valid = %08x\n", valid);
2176         if (valid & OBD_MD_FLID)
2177                 CDEBUG(D_RPCTRACE, "obdo: o_id = "LPD64"\n", oa->o_id);
2178         if (valid & OBD_MD_FLGROUP)
2179                 CDEBUG(D_RPCTRACE, "obdo: o_seq = "LPD64"\n", oa->o_seq);
2180         if (valid & OBD_MD_FLFID)
2181                 CDEBUG(D_RPCTRACE, "obdo: o_parent_seq = "LPX64"\n",
2182                        oa->o_parent_seq);
2183         if (valid & OBD_MD_FLSIZE)
2184                 CDEBUG(D_RPCTRACE, "obdo: o_size = "LPD64"\n", oa->o_size);
2185         if (valid & OBD_MD_FLMTIME)
2186                 CDEBUG(D_RPCTRACE, "obdo: o_mtime = "LPD64"\n", oa->o_mtime);
2187         if (valid & OBD_MD_FLATIME)
2188                 CDEBUG(D_RPCTRACE, "obdo: o_atime = "LPD64"\n", oa->o_atime);
2189         if (valid & OBD_MD_FLCTIME)
2190                 CDEBUG(D_RPCTRACE, "obdo: o_ctime = "LPD64"\n", oa->o_ctime);
2191         if (valid & OBD_MD_FLBLOCKS)   /* allocation of space */
2192                 CDEBUG(D_RPCTRACE, "obdo: o_blocks = "LPD64"\n", oa->o_blocks);
2193         if (valid & OBD_MD_FLGRANT)
2194                 CDEBUG(D_RPCTRACE, "obdo: o_grant = "LPD64"\n", oa->o_grant);
2195         if (valid & OBD_MD_FLBLKSZ)
2196                 CDEBUG(D_RPCTRACE, "obdo: o_blksize = %d\n", oa->o_blksize);
2197         if (valid & (OBD_MD_FLTYPE | OBD_MD_FLMODE))
2198                 CDEBUG(D_RPCTRACE, "obdo: o_mode = %o\n",
2199                        oa->o_mode & ((valid & OBD_MD_FLTYPE ?  S_IFMT : 0) |
2200                                      (valid & OBD_MD_FLMODE ? ~S_IFMT : 0)));
2201         if (valid & OBD_MD_FLUID)
2202                 CDEBUG(D_RPCTRACE, "obdo: o_uid = %u\n", oa->o_uid);
2203         if (valid & OBD_MD_FLUID)
2204                 CDEBUG(D_RPCTRACE, "obdo: o_uid_h = %u\n", oa->o_uid_h);
2205         if (valid & OBD_MD_FLGID)
2206                 CDEBUG(D_RPCTRACE, "obdo: o_gid = %u\n", oa->o_gid);
2207         if (valid & OBD_MD_FLGID)
2208                 CDEBUG(D_RPCTRACE, "obdo: o_gid_h = %u\n", oa->o_gid_h);
2209         if (valid & OBD_MD_FLFLAGS)
2210                 CDEBUG(D_RPCTRACE, "obdo: o_flags = %x\n", oa->o_flags);
2211         if (valid & OBD_MD_FLNLINK)
2212                 CDEBUG(D_RPCTRACE, "obdo: o_nlink = %u\n", oa->o_nlink);
2213         else if (valid & OBD_MD_FLCKSUM)
2214                 CDEBUG(D_RPCTRACE, "obdo: o_checksum (o_nlink) = %u\n",
2215                        oa->o_nlink);
2216         if (valid & OBD_MD_FLGENER)
2217                 CDEBUG(D_RPCTRACE, "obdo: o_parent_oid = %x\n",
2218                        oa->o_parent_oid);
2219         if (valid & OBD_MD_FLEPOCH)
2220                 CDEBUG(D_RPCTRACE, "obdo: o_ioepoch = "LPD64"\n",
2221                        oa->o_ioepoch);
2222         if (valid & OBD_MD_FLFID) {
2223                 CDEBUG(D_RPCTRACE, "obdo: o_stripe_idx = %u\n",
2224                        oa->o_stripe_idx);
2225                 CDEBUG(D_RPCTRACE, "obdo: o_parent_ver = %x\n",
2226                        oa->o_parent_ver);
2227         }
2228         if (valid & OBD_MD_FLHANDLE)
2229                 CDEBUG(D_RPCTRACE, "obdo: o_handle = "LPD64"\n",
2230                        oa->o_handle.cookie);
2231         if (valid & OBD_MD_FLCOOKIE)
2232                 CDEBUG(D_RPCTRACE, "obdo: o_lcookie = "
2233                        "(llog_cookie dumping not yet implemented)\n");
2234 }
2235
2236 void dump_ost_body(struct ost_body *ob)
2237 {
2238         dump_obdo(&ob->oa);
2239 }
2240
2241 void dump_rcs(__u32 *rc)
2242 {
2243         CDEBUG(D_RPCTRACE, "rmf_rcs: %d\n", *rc);
2244 }
2245
2246 #ifdef __KERNEL__
2247
2248 /**
2249  * got qdata from request(req/rep)
2250  */
2251 struct qunit_data *quota_get_qdata(void *r, int is_req, int is_exp)
2252 {
2253         struct ptlrpc_request *req = (struct ptlrpc_request *)r;
2254         struct qunit_data *qdata;
2255         __u64  flags = is_exp ? req->rq_export->exp_connect_flags :
2256                        req->rq_import->imp_connect_data.ocd_connect_flags;
2257
2258         LASSERT(req);
2259         /* support for quota64 */
2260         LASSERT(flags & OBD_CONNECT_QUOTA64);
2261         /* support for change_qs */
2262         LASSERT(flags & OBD_CONNECT_CHANGE_QS);
2263
2264         if (is_req == QUOTA_REQUEST)
2265                 qdata = req_capsule_client_get(&req->rq_pill, &RMF_QUNIT_DATA);
2266         else
2267                 qdata = req_capsule_server_get(&req->rq_pill, &RMF_QUNIT_DATA);
2268         if (qdata == NULL)
2269                 return ERR_PTR(-EPROTO);
2270
2271         QDATA_SET_CHANGE_QS(qdata);
2272         return qdata;
2273 }
2274 EXPORT_SYMBOL(quota_get_qdata);
2275
2276 /**
2277  * copy qdata to request(req/rep)
2278  */
2279 int quota_copy_qdata(void *r, struct qunit_data *qdata, int is_req,
2280                      int is_exp)
2281 {
2282         struct ptlrpc_request *req = (struct ptlrpc_request *)r;
2283         void *target;
2284         __u64  flags = is_exp ? req->rq_export->exp_connect_flags :
2285                 req->rq_import->imp_connect_data.ocd_connect_flags;
2286
2287         LASSERT(req);
2288         LASSERT(qdata);
2289         /* support for quota64 */
2290         LASSERT(flags & OBD_CONNECT_QUOTA64);
2291         /* support for change_qs */
2292         LASSERT(flags & OBD_CONNECT_CHANGE_QS);
2293
2294         if (is_req == QUOTA_REQUEST)
2295                 target = req_capsule_client_get(&req->rq_pill, &RMF_QUNIT_DATA);
2296         else
2297                 target = req_capsule_server_get(&req->rq_pill, &RMF_QUNIT_DATA);
2298         if (target == NULL)
2299                 return -EPROTO;
2300
2301         LASSERT(target != qdata);
2302         memcpy(target, qdata, sizeof(*qdata));
2303         return 0;
2304 }
2305 EXPORT_SYMBOL(quota_copy_qdata);
2306 #endif /* __KERNEL__ */
2307
2308 static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2309 {
2310         LASSERT(req->rq_reqmsg);
2311
2312         switch (req->rq_reqmsg->lm_magic) {
2313         case LUSTRE_MSG_MAGIC_V2:
2314                 return lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
2315         default:
2316                 CERROR("bad lustre msg magic: %#08X\n",
2317                        req->rq_reqmsg->lm_magic);
2318         }
2319         return 0;
2320 }
2321
2322 static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2323 {
2324         LASSERT(req->rq_repmsg);
2325
2326         switch (req->rq_repmsg->lm_magic) {
2327         case LUSTRE_MSG_MAGIC_V2:
2328                 return lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
2329         default:
2330                 /* uninitialized yet */
2331                 return 0;
2332         }
2333 }
2334
2335 void _debug_req(struct ptlrpc_request *req,
2336                 struct libcfs_debug_msg_data *msgdata,
2337                 const char *fmt, ... )
2338 {
2339         int req_ok = req->rq_reqmsg != NULL;
2340         int rep_ok = req->rq_repmsg != NULL;
2341         lnet_nid_t nid = LNET_NID_ANY;
2342         va_list args;
2343
2344         if (ptlrpc_req_need_swab(req)) {
2345                 req_ok = req_ok && req_ptlrpc_body_swabbed(req);
2346                 rep_ok = rep_ok && rep_ptlrpc_body_swabbed(req);
2347         }
2348
2349         if (req->rq_import && req->rq_import->imp_connection)
2350                 nid = req->rq_import->imp_connection->c_peer.nid;
2351         else if (req->rq_export && req->rq_export->exp_connection)
2352                 nid = req->rq_export->exp_connection->c_peer.nid;
2353
2354         va_start(args, fmt);
2355         libcfs_debug_vmsg2(msgdata, fmt, args,
2356                            " req@%p x"LPU64"/t"LPD64"("LPD64") o%d->%s@%s:%d/%d"
2357                            " lens %d/%d e %d to %d dl "CFS_TIME_T" ref %d "
2358                            "fl "REQ_FLAGS_FMT"/%x/%x rc %d/%d\n",
2359                            req, req->rq_xid, req->rq_transno,
2360                            req_ok ? lustre_msg_get_transno(req->rq_reqmsg) : 0,
2361                            req_ok ? lustre_msg_get_opc(req->rq_reqmsg) : -1,
2362                            req->rq_import ?
2363                                 req->rq_import->imp_obd->obd_name :
2364                                 req->rq_export ?
2365                                      req->rq_export->exp_client_uuid.uuid :
2366                                      "<?>",
2367                            libcfs_nid2str(nid),
2368                            req->rq_request_portal, req->rq_reply_portal,
2369                            req->rq_reqlen, req->rq_replen,
2370                            req->rq_early_count, req->rq_timedout,
2371                            req->rq_deadline,
2372                            cfs_atomic_read(&req->rq_refcount),
2373                            DEBUG_REQ_FLAGS(req),
2374                            req_ok ? lustre_msg_get_flags(req->rq_reqmsg) : -1,
2375                            rep_ok ? lustre_msg_get_flags(req->rq_repmsg) : -1,
2376                            req->rq_status,
2377                            rep_ok ? lustre_msg_get_status(req->rq_repmsg) : -1);
2378 }
2379 EXPORT_SYMBOL(_debug_req);
2380
2381 void lustre_swab_lustre_capa(struct lustre_capa *c)
2382 {
2383         lustre_swab_lu_fid(&c->lc_fid);
2384         __swab64s (&c->lc_opc);
2385         __swab64s (&c->lc_uid);
2386         __swab64s (&c->lc_gid);
2387         __swab32s (&c->lc_flags);
2388         __swab32s (&c->lc_keyid);
2389         __swab32s (&c->lc_timeout);
2390         __swab32s (&c->lc_expiry);
2391 }
2392
2393 void lustre_swab_lustre_capa_key(struct lustre_capa_key *k)
2394 {
2395         __swab64s (&k->lk_seq);
2396         __swab32s (&k->lk_keyid);
2397         CLASSERT(offsetof(typeof(*k), lk_padding) != 0);
2398 }
2399
2400 void lustre_swab_hsm_state(struct hsm_state_set_ioc *hssi)
2401 {
2402         lustre_swab_lu_fid(&hssi->hssi_fid);
2403         __swab64s(&hssi->hssi_setmask);
2404         __swab64s(&hssi->hssi_clearmask);
2405 }
2406 EXPORT_SYMBOL(lustre_swab_hsm_state);
2407
2408 void lustre_swab_hsm_user_request(struct hsm_user_request *hur)
2409 {
2410         int i;
2411
2412         __swab32s(&hur->hur_action);
2413         __swab32s(&hur->hur_itemcount);
2414         __swab32s(&hur->hur_data_len);
2415         for (i = 0; i < hur->hur_itemcount; i++) {
2416                 struct hsm_user_item *hui = &hur->hur_user_item[i];
2417                 lustre_swab_lu_fid(&hui->hui_fid);
2418                 __swab64s(&hui->hui_extent.offset);
2419                 __swab64s(&hui->hui_extent.length);
2420         }
2421         /* Note: data blob is not swabbed here */
2422 }
2423 EXPORT_SYMBOL(lustre_swab_hsm_user_request);
2424
2425 void lustre_swab_hsm_progress(struct hsm_progress *hp)
2426 {
2427         lustre_swab_lu_fid(&hp->hp_fid);
2428         __swab64s(&hp->hp_cookie);
2429         __swab64s(&hp->hp_extent.offset);
2430         __swab64s(&hp->hp_extent.length);
2431         __swab16s(&hp->hp_flags);
2432         __swab16s(&hp->hp_errval);
2433 }
2434 EXPORT_SYMBOL(lustre_swab_hsm_progress);
2435
2436