Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / ptlrpc / pack_generic.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 size_round(offsetof(struct lustre_msg_v2, lm_buflens[count]));
61 }
62
63 int lustre_msg_hdr_size(__u32 magic, int count)
64 {
65         switch (magic) {
66         case LUSTRE_MSG_MAGIC_V2:
67                 return lustre_msg_hdr_size_v2(count);
68         default:
69                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
70                 return -EINVAL;
71         }
72 }
73 EXPORT_SYMBOL(lustre_msg_hdr_size);
74
75 int lustre_msg_need_swab(struct lustre_msg *msg)
76 {
77         return (msg->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED);
78 }
79
80 /**
81  * \ret 0 - if need swabbed
82  * \ret 1 - otherwise
83  */
84 static int ptlrpc_reqbuf_need_swab(struct ptlrpc_request *req, int index)
85 {
86         return (lustre_req_need_swab(req) && !lustre_req_swabbed(req, index));
87 }
88
89 /**
90  *  \ret 0 - if need swabbed
91  *  \ret 1 - otherwise
92  */
93 static int ptlrpc_repbuf_need_swab(struct ptlrpc_request *req, int index)
94 {
95         return (lustre_rep_need_swab(req) && !lustre_rep_swabbed(req, index));
96 }
97
98 /**
99  * ptlrpc_req_need_swab - check the @req if need swab.
100  * @req   - ptlrpc_request need to look at.
101  * @inout - =1 reqbuf, =0 repbuf.
102  * @index - message offset
103  *
104  * \ret 0 - swabbed
105  * \ret 1 - need swab
106  */
107 int ptlrpc_req_need_swab(struct ptlrpc_request *req, int inout, int index)
108 {
109         if (inout)
110                 return ptlrpc_reqbuf_need_swab(req, index);
111         else
112                 return ptlrpc_repbuf_need_swab(req, index);
113 }
114
115 void ptlrpc_req_set_swabbed(struct ptlrpc_request *req, int inout, int index)
116 {
117        if(inout)
118                 lustre_set_req_swabbed(req, index);
119         else
120                 lustre_set_rep_swabbed(req, index);
121 }
122
123
124 static inline int
125 lustre_msg_check_version_v2(struct lustre_msg_v2 *msg, __u32 version)
126 {
127         __u32 ver = lustre_msg_get_version(msg);
128         return (ver & LUSTRE_VERSION_MASK) != version;
129 }
130
131 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version)
132 {
133         switch (msg->lm_magic) {
134         case LUSTRE_MSG_MAGIC_V1:
135                 CERROR("msg v1 not supported - please upgrade you system\n");
136                 return -EINVAL; 
137         case LUSTRE_MSG_MAGIC_V2:
138                 return lustre_msg_check_version_v2(msg, version);
139         default:
140                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
141                 return 0;
142         }
143 }
144
145 /* early reply size */
146 int lustre_msg_early_size() {
147         static int size = 0;
148         if (!size)
149                 size = lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
150         return size;
151 }
152 EXPORT_SYMBOL(lustre_msg_early_size);
153
154 int lustre_msg_size_v2(int count, __u32 *lengths)
155 {
156         int size;
157         int i;
158
159         size = lustre_msg_hdr_size_v2(count);
160         for (i = 0; i < count; i++)
161                 size += size_round(lengths[i]);
162
163         return size;
164 }
165 EXPORT_SYMBOL(lustre_msg_size_v2);
166
167 /* This returns the size of the buffer that is required to hold a lustre_msg
168  * with the given sub-buffer lengths.
169  * NOTE: this should only be used for NEW requests, and should always be
170  *       in the form of a v2 request.  If this is a connection to a v1
171  *       target then the first buffer will be stripped because the ptlrpc
172  *       data is part of the lustre_msg_v1 header. b=14043 */
173 int lustre_msg_size(__u32 magic, int count, __u32 *lens)
174 {
175         __u32 size[] = { sizeof(struct ptlrpc_body) };
176
177         if (!lens) {
178                 LASSERT(count == 1);
179                 lens = size;
180         }
181
182         LASSERT(count > 0);
183         LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
184
185         switch (magic) {
186         case LUSTRE_MSG_MAGIC_V2:
187                 return lustre_msg_size_v2(count, lens);
188         default:
189                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
190                 return -EINVAL;
191         }
192 }
193
194 /* This is used to determine the size of a buffer that was already packed
195  * and will correctly handle the different message formats. */
196 int lustre_packed_msg_size(struct lustre_msg *msg)
197 {
198         switch (msg->lm_magic) {
199         case LUSTRE_MSG_MAGIC_V2:
200                 return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
201         default:
202                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
203                 return 0;
204         }
205 }
206
207 void lustre_init_msg_v2(struct lustre_msg_v2 *msg, int count, __u32 *lens,
208                         char **bufs)
209 {
210         char *ptr;
211         int i;
212
213         msg->lm_bufcount = count;
214         /* XXX: lm_secflvr uninitialized here */
215         msg->lm_magic = LUSTRE_MSG_MAGIC_V2;
216
217         for (i = 0; i < count; i++)
218                 msg->lm_buflens[i] = lens[i];
219
220         if (bufs == NULL)
221                 return;
222
223         ptr = (char *)msg + lustre_msg_hdr_size_v2(count);
224         for (i = 0; i < count; i++) {
225                 char *tmp = bufs[i];
226                 LOGL(tmp, lens[i], ptr);
227         }
228 }
229 EXPORT_SYMBOL(lustre_init_msg_v2);
230
231 static int lustre_pack_request_v2(struct ptlrpc_request *req,
232                                   int count, __u32 *lens, char **bufs)
233 {
234         int reqlen, rc;
235
236         reqlen = lustre_msg_size_v2(count, lens);
237
238         rc = sptlrpc_cli_alloc_reqbuf(req, reqlen);
239         if (rc)
240                 return rc;
241
242         req->rq_reqlen = reqlen;
243
244         lustre_init_msg_v2(req->rq_reqmsg, count, lens, bufs);
245         lustre_msg_add_version(req->rq_reqmsg, PTLRPC_MSG_VERSION);
246         lustre_set_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
247         return 0;
248 }
249
250 int lustre_pack_request(struct ptlrpc_request *req, __u32 magic, int count,
251                         __u32 *lens, char **bufs)
252 {
253         __u32 size[] = { sizeof(struct ptlrpc_body) };
254
255         if (!lens) {
256                 LASSERT(count == 1);
257                 lens = size;
258         }
259
260         LASSERT(count > 0);
261         LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
262
263         /* only use new format, we don't need to be compatible with 1.4 */
264         magic = LUSTRE_MSG_MAGIC_V2;
265
266         switch (magic) {
267         case LUSTRE_MSG_MAGIC_V2:
268                 return lustre_pack_request_v2(req, count, lens, bufs);
269         default:
270                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
271                 return -EINVAL;
272         }
273 }
274
275 #if RS_DEBUG
276 CFS_LIST_HEAD(ptlrpc_rs_debug_lru);
277 spinlock_t ptlrpc_rs_debug_lock;
278
279 #define PTLRPC_RS_DEBUG_LRU_ADD(rs)                                     \
280 do {                                                                    \
281         spin_lock(&ptlrpc_rs_debug_lock);                               \
282         list_add_tail(&(rs)->rs_debug_list, &ptlrpc_rs_debug_lru);      \
283         spin_unlock(&ptlrpc_rs_debug_lock);                             \
284 } while (0)
285
286 #define PTLRPC_RS_DEBUG_LRU_DEL(rs)             \
287 do {                                            \
288         spin_lock(&ptlrpc_rs_debug_lock);       \
289         list_del(&(rs)->rs_debug_list);         \
290         spin_unlock(&ptlrpc_rs_debug_lock);     \
291 } while (0)
292 #else
293 # define PTLRPC_RS_DEBUG_LRU_ADD(rs) do {} while(0)
294 # define PTLRPC_RS_DEBUG_LRU_DEL(rs) do {} while(0)
295 #endif
296
297 struct ptlrpc_reply_state *lustre_get_emerg_rs(struct ptlrpc_service *svc)
298 {
299         struct ptlrpc_reply_state *rs = NULL;
300
301         spin_lock(&svc->srv_lock);
302         /* See if we have anything in a pool, and wait if nothing */
303         while (list_empty(&svc->srv_free_rs_list)) {
304                 struct l_wait_info lwi;
305                 int rc;
306                 spin_unlock(&svc->srv_lock);
307                 /* If we cannot get anything for some long time, we better
308                    bail out instead of waiting infinitely */
309                 lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
310                 rc = l_wait_event(svc->srv_free_rs_waitq,
311                                   !list_empty(&svc->srv_free_rs_list), &lwi);
312                 if (rc)
313                         goto out;
314                 spin_lock(&svc->srv_lock);
315         }
316
317         rs = list_entry(svc->srv_free_rs_list.next, struct ptlrpc_reply_state,
318                         rs_list);
319         list_del(&rs->rs_list);
320         spin_unlock(&svc->srv_lock);
321         LASSERT(rs);
322         memset(rs, 0, svc->srv_max_reply_size);
323         rs->rs_service = svc;
324         rs->rs_prealloc = 1;
325 out:
326         return rs;
327 }
328
329 void lustre_put_emerg_rs(struct ptlrpc_reply_state *rs)
330 {
331         struct ptlrpc_service *svc = rs->rs_service;
332
333         LASSERT(svc);
334
335         spin_lock(&svc->srv_lock);
336         list_add(&rs->rs_list, &svc->srv_free_rs_list);
337         spin_unlock(&svc->srv_lock);
338         cfs_waitq_signal(&svc->srv_free_rs_waitq);
339 }
340
341 int lustre_pack_reply_v2(struct ptlrpc_request *req, int count,
342                          __u32 *lens, char **bufs, int flags)
343 {
344         struct ptlrpc_reply_state *rs;
345         int                        msg_len, rc;
346         ENTRY;
347
348         LASSERT(req->rq_reply_state == NULL);
349
350         if ((flags & LPRFL_EARLY_REPLY) == 0)
351                 req->rq_packed_final = 1;
352
353         msg_len = lustre_msg_size_v2(count, lens);
354         rc = sptlrpc_svc_alloc_rs(req, msg_len);
355         if (rc)
356                 RETURN(rc);
357
358         rs = req->rq_reply_state;
359         atomic_set(&rs->rs_refcount, 1);        /* 1 ref for rq_reply_state */
360         rs->rs_cb_id.cbid_fn = reply_out_callback;
361         rs->rs_cb_id.cbid_arg = rs;
362         rs->rs_service = req->rq_rqbd->rqbd_service;
363         CFS_INIT_LIST_HEAD(&rs->rs_exp_list);
364         CFS_INIT_LIST_HEAD(&rs->rs_obd_list);
365
366         req->rq_replen = msg_len;
367         req->rq_reply_state = rs;
368         req->rq_repmsg = rs->rs_msg;
369
370         lustre_init_msg_v2(rs->rs_msg, count, lens, bufs);
371         lustre_msg_add_version(rs->rs_msg, PTLRPC_MSG_VERSION);
372         lustre_set_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
373
374         PTLRPC_RS_DEBUG_LRU_ADD(rs);
375
376         RETURN(0);
377 }
378 EXPORT_SYMBOL(lustre_pack_reply_v2);
379
380 int lustre_pack_reply_flags(struct ptlrpc_request *req, int count, __u32 *lens,
381                             char **bufs, int flags)
382 {
383         int rc = 0;
384         __u32 size[] = { sizeof(struct ptlrpc_body) };
385
386         if (!lens) {
387                 LASSERT(count == 1);
388                 lens = size;
389         }
390
391         LASSERT(count > 0);
392         LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
393
394         switch (req->rq_reqmsg->lm_magic) {
395         case LUSTRE_MSG_MAGIC_V2:
396         case LUSTRE_MSG_MAGIC_V2_SWABBED:
397                 rc = lustre_pack_reply_v2(req, count, lens, bufs, flags);
398                 break;
399         default:
400                 LASSERTF(0, "incorrect message magic: %08x\n",
401                          req->rq_reqmsg->lm_magic);
402                 rc = -EINVAL;
403         }
404         if (rc != 0)
405                 CERROR("lustre_pack_reply failed: rc=%d size=%d\n", rc,
406                        lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens));
407         return rc;
408 }
409
410 int lustre_pack_reply(struct ptlrpc_request *req, int count, __u32 *lens,
411                       char **bufs)
412 {
413         return lustre_pack_reply_flags(req, count, lens, bufs, 0);
414 }
415
416 void *lustre_msg_buf_v2(struct lustre_msg_v2 *m, int n, int min_size)
417 {
418         int i, offset, buflen, bufcount;
419
420         LASSERT(m != NULL);
421         LASSERT(n >= 0);
422
423         bufcount = m->lm_bufcount;
424         if (unlikely(n >= bufcount)) {
425                 CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
426                        m, n, bufcount);
427                 return NULL;
428         }
429
430         buflen = m->lm_buflens[n];
431         if (unlikely(buflen < min_size)) {
432                 CERROR("msg %p buffer[%d] size %d too small (required %d)\n",
433                        m, n, buflen, min_size);
434                 return NULL;
435         }
436
437         offset = lustre_msg_hdr_size_v2(bufcount);
438         for (i = 0; i < n; i++)
439                 offset += size_round(m->lm_buflens[i]);
440
441         return (char *)m + offset;
442 }
443
444 void *lustre_msg_buf(struct lustre_msg *m, int n, int min_size)
445 {
446         switch (m->lm_magic) {
447         case LUSTRE_MSG_MAGIC_V2:
448         case LUSTRE_MSG_MAGIC_V2_SWABBED:
449                 return lustre_msg_buf_v2(m, n, min_size);
450         default:
451                 LASSERTF(0, "incorrect message magic: %08x(msg:%p)\n", m->lm_magic, m);
452                 return NULL;
453         }
454 }
455
456 int lustre_shrink_msg_v2(struct lustre_msg_v2 *msg, int segment,
457                          unsigned int newlen, int move_data)
458 {
459         char   *tail = NULL, *newpos;
460         int     tail_len = 0, n;
461
462         LASSERT(msg);
463         LASSERT(msg->lm_bufcount > segment);
464         LASSERT(msg->lm_buflens[segment] >= newlen);
465
466         if (msg->lm_buflens[segment] == newlen)
467                 goto out;
468
469         if (move_data && msg->lm_bufcount > segment + 1) {
470                 tail = lustre_msg_buf_v2(msg, segment + 1, 0);
471                 for (n = segment + 1; n < msg->lm_bufcount; n++)
472                         tail_len += size_round(msg->lm_buflens[n]);
473         }
474
475         msg->lm_buflens[segment] = newlen;
476
477         if (tail && tail_len) {
478                 newpos = lustre_msg_buf_v2(msg, segment + 1, 0);
479                 LASSERT(newpos <= tail);
480                 if (newpos != tail)
481                         memcpy(newpos, tail, tail_len);
482         }
483 out:
484         return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
485 }
486
487 /*
488  * for @msg, shrink @segment to size @newlen. if @move_data is non-zero,
489  * we also move data forward from @segment + 1.
490  *
491  * if @newlen == 0, we remove the segment completely, but we still keep the
492  * totally bufcount the same to save possible data moving. this will leave a
493  * unused segment with size 0 at the tail, but that's ok.
494  *
495  * return new msg size after shrinking.
496  *
497  * CAUTION:
498  * + if any buffers higher than @segment has been filled in, must call shrink
499  *   with non-zero @move_data.
500  * + caller should NOT keep pointers to msg buffers which higher than @segment
501  *   after call shrink.
502  */
503 int lustre_shrink_msg(struct lustre_msg *msg, int segment,
504                       unsigned int newlen, int move_data)
505 {
506         switch (msg->lm_magic) {
507         case LUSTRE_MSG_MAGIC_V2:
508                 return lustre_shrink_msg_v2(msg, segment, newlen, move_data);
509         default:
510                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
511         }
512 }
513
514 void lustre_free_reply_state(struct ptlrpc_reply_state *rs)
515 {
516         PTLRPC_RS_DEBUG_LRU_DEL(rs);
517
518         LASSERT (atomic_read(&rs->rs_refcount) == 0);
519         LASSERT (!rs->rs_difficult || rs->rs_handled);
520         LASSERT (!rs->rs_on_net);
521         LASSERT (!rs->rs_scheduled);
522         LASSERT (rs->rs_export == NULL);
523         LASSERT (rs->rs_nlocks == 0);
524         LASSERT (list_empty(&rs->rs_exp_list));
525         LASSERT (list_empty(&rs->rs_obd_list));
526
527         sptlrpc_svc_free_rs(rs);
528 }
529
530 static int lustre_unpack_msg_v2(struct lustre_msg_v2 *m, int len)
531 {
532         int flipped, required_len, i;
533
534         /* Now we know the sender speaks my language. */
535         required_len = lustre_msg_hdr_size_v2(0);
536         if (len < required_len) {
537                 /* can't even look inside the message */
538                 CERROR("message length %d too small for lustre_msg\n", len);
539                 return -EINVAL;
540         }
541
542         flipped = lustre_msg_need_swab(m);
543         if (flipped) {
544                 __swab32s(&m->lm_magic);
545                 __swab32s(&m->lm_bufcount);
546                 __swab32s(&m->lm_secflvr);
547                 __swab32s(&m->lm_repsize);
548                 __swab32s(&m->lm_cksum);
549                 __swab32s(&m->lm_flags);
550                 CLASSERT(offsetof(typeof(*m), lm_padding_2) != 0);
551                 CLASSERT(offsetof(typeof(*m), lm_padding_3) != 0);
552         }
553
554         required_len = lustre_msg_hdr_size_v2(m->lm_bufcount);
555         if (len < required_len) {
556                 /* didn't receive all the buffer lengths */
557                 CERROR ("message length %d too small for %d buflens\n",
558                         len, m->lm_bufcount);
559                 return -EINVAL;
560         }
561
562         for (i = 0; i < m->lm_bufcount; i++) {
563                 if (flipped)
564                         __swab32s(&m->lm_buflens[i]);
565                 required_len += size_round(m->lm_buflens[i]);
566         }
567
568         if (len < required_len) {
569                 CERROR("len: %d, required_len %d\n", len, required_len);
570                 CERROR("bufcount: %d\n", m->lm_bufcount);
571                 for (i = 0; i < m->lm_bufcount; i++)
572                         CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
573                 return -EINVAL;
574         }
575
576         return flipped;
577 }
578
579 int lustre_unpack_msg(struct lustre_msg *m, int len)
580 {
581         int required_len, rc;
582         ENTRY;
583
584         /* We can provide a slightly better error log, if we check the
585          * message magic and version first.  In the future, struct
586          * lustre_msg may grow, and we'd like to log a version mismatch,
587          * rather than a short message.
588          *
589          */
590         required_len = offsetof(struct lustre_msg, lm_magic) +
591                        sizeof(m->lm_magic);
592         if (len < required_len) {
593                 /* can't even look inside the message */
594                 CERROR("message length %d too small for magic/version check\n",
595                        len);
596                 RETURN(-EINVAL);
597         }
598
599         switch (m->lm_magic) {
600         case LUSTRE_MSG_MAGIC_V2:
601         case LUSTRE_MSG_MAGIC_V2_SWABBED:
602                 rc = lustre_unpack_msg_v2(m, len);
603                 break;
604         default:
605                 CERROR("bad lustre msg magic: %#08X\n", m->lm_magic);
606                 return -EINVAL;
607         }
608
609         RETURN(rc);
610 }
611
612 static inline int lustre_unpack_ptlrpc_body_v2(struct ptlrpc_request *req,
613                                                int inout,
614                                                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(*pb));
620         if (!pb) {
621                 CERROR("error unpacking ptlrpc body\n");
622                 return -EFAULT;
623         }
624         if (ptlrpc_req_need_swab(req, inout, offset)) {
625                 lustre_swab_ptlrpc_body(pb);
626                 ptlrpc_req_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 @n in message @m
671  * @m - lustre_msg (request or reply) to look at
672  * @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         case LUSTRE_MSG_MAGIC_V2_SWABBED:
681                 return lustre_msg_buflen_v2(m, n);
682         default:
683                 CERROR("incorrect message magic: %08x\n", m->lm_magic);
684                 return -EINVAL;
685         }
686 }
687 EXPORT_SYMBOL(lustre_msg_buflen);
688
689 static inline void
690 lustre_msg_set_buflen_v2(struct lustre_msg_v2 *m, int n, int len)
691 {
692         if (n >= m->lm_bufcount)
693                 LBUG();
694
695         m->lm_buflens[n] = len;
696 }
697
698 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len)
699 {
700         switch (m->lm_magic) {
701         case LUSTRE_MSG_MAGIC_V2:
702                 lustre_msg_set_buflen_v2(m, n, len);
703                 return;
704         default:
705                 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
706         }
707 }
708
709 EXPORT_SYMBOL(lustre_msg_set_buflen);
710
711 /* NB return the bufcount for lustre_msg_v2 format, so if message is packed
712  * in V1 format, the result is one bigger. (add struct ptlrpc_body). */
713 int lustre_msg_bufcount(struct lustre_msg *m)
714 {
715         switch (m->lm_magic) {
716         case LUSTRE_MSG_MAGIC_V2:
717         case LUSTRE_MSG_MAGIC_V2_SWABBED:
718                 return m->lm_bufcount;
719         default:
720                 CERROR("incorrect message magic: %08x\n", m->lm_magic);
721                 return -EINVAL;
722         }
723 }
724 EXPORT_SYMBOL(lustre_msg_bufcount);
725
726 char *lustre_msg_string(struct lustre_msg *m, int index, int max_len)
727 {
728         /* max_len == 0 means the string should fill the buffer */
729         char *str;
730         int slen, blen;
731
732         switch (m->lm_magic) {
733         case LUSTRE_MSG_MAGIC_V2:
734         case LUSTRE_MSG_MAGIC_V2_SWABBED:
735                 str = lustre_msg_buf_v2(m, index, 0);
736                 blen = lustre_msg_buflen_v2(m, index);
737                 break;
738         default:
739                 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
740         }
741
742         if (str == NULL) {
743                 CERROR ("can't unpack string in msg %p buffer[%d]\n", m, index);
744                 return NULL;
745         }
746
747         slen = strnlen(str, blen);
748
749         if (slen == blen) {                     /* not NULL terminated */
750                 CERROR("can't unpack non-NULL terminated string in "
751                         "msg %p buffer[%d] len %d\n", m, index, blen);
752                 return NULL;
753         }
754
755         if (max_len == 0) {
756                 if (slen != blen - 1) {
757                         CERROR("can't unpack short string in msg %p "
758                                "buffer[%d] len %d: strlen %d\n",
759                                m, index, blen, slen);
760                         return NULL;
761                 }
762         } else if (slen > max_len) {
763                 CERROR("can't unpack oversized string in msg %p "
764                        "buffer[%d] len %d strlen %d: max %d expected\n",
765                        m, index, blen, slen, max_len);
766                 return NULL;
767         }
768
769         return str;
770 }
771
772 /* Wrap up the normal fixed length cases */
773 void *lustre_swab_buf(struct lustre_msg *msg, int index, int min_size,
774                       void *swabber)
775 {
776         void *ptr = NULL;
777
778         switch (msg->lm_magic) {
779         case LUSTRE_MSG_MAGIC_V2:
780         case LUSTRE_MSG_MAGIC_V2_SWABBED:
781                 ptr = lustre_msg_buf_v2(msg, index, min_size);
782                 break;
783         default:
784                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
785         }
786         if (ptr == NULL)
787                 return NULL;
788
789         if (swabber != NULL)
790                 ((void (*)(void *))swabber)(ptr);
791
792         return ptr;
793 }
794
795 void *lustre_swab_reqbuf(struct ptlrpc_request *req, int index, int min_size,
796                          void *swabber)
797 {
798         if (!ptlrpc_reqbuf_need_swab(req, index))
799                  swabber = NULL;
800
801         lustre_set_req_swabbed(req, index);
802         return lustre_swab_buf(req->rq_reqmsg, index, min_size, swabber);
803 }
804
805 void *lustre_swab_repbuf(struct ptlrpc_request *req, int index, int min_size,
806                          void *swabber)
807 {
808         if (!ptlrpc_repbuf_need_swab(req, index))
809                  swabber = NULL;
810
811         lustre_set_rep_swabbed(req, index);
812         return lustre_swab_buf(req->rq_repmsg, index, min_size, swabber);
813 }
814
815 __u32 lustre_msghdr_get_flags(struct lustre_msg *msg)
816 {
817         switch (msg->lm_magic) {
818         case LUSTRE_MSG_MAGIC_V1:
819         case LUSTRE_MSG_MAGIC_V1_SWABBED:
820                 return 0;
821         case LUSTRE_MSG_MAGIC_V2:
822         case LUSTRE_MSG_MAGIC_V2_SWABBED:
823                 /* already in host endian */
824                 return msg->lm_flags;
825         default:
826                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
827                 return 0;
828         }
829 }
830 EXPORT_SYMBOL(lustre_msghdr_get_flags);
831
832 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags)
833 {
834         switch (msg->lm_magic) {
835         case LUSTRE_MSG_MAGIC_V1:
836                 return;
837         case LUSTRE_MSG_MAGIC_V2:
838                 msg->lm_flags = flags;
839                 return;
840         default:
841                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
842         }
843 }
844
845 __u32 lustre_msg_get_flags(struct lustre_msg *msg)
846 {
847         switch (msg->lm_magic) {
848         case LUSTRE_MSG_MAGIC_V2:
849         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
850                 struct ptlrpc_body *pb;
851
852                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
853                 if (!pb) {
854                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
855                         return 0;
856                 }
857                 return pb->pb_flags;
858         }
859         default:
860                 /* flags might be printed in debug code while message
861                  * uninitialized */
862                 return 0;
863         }
864 }
865
866 void lustre_msg_add_flags(struct lustre_msg *msg, int flags)
867 {
868         switch (msg->lm_magic) {
869         case LUSTRE_MSG_MAGIC_V2: {
870                 struct ptlrpc_body *pb;
871
872                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
873                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
874                 pb->pb_flags |= flags;
875                 return;
876         }
877         default:
878                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
879         }
880 }
881
882 void lustre_msg_set_flags(struct lustre_msg *msg, int flags)
883 {
884         switch (msg->lm_magic) {
885         case LUSTRE_MSG_MAGIC_V2: {
886                 struct ptlrpc_body *pb;
887
888                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
889                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
890                 pb->pb_flags = flags;
891                 return;
892         }
893         default:
894                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
895         }
896 }
897
898 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags)
899 {
900         switch (msg->lm_magic) {
901         case LUSTRE_MSG_MAGIC_V2:
902         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
903                 struct ptlrpc_body *pb;
904
905                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
906                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
907                 pb->pb_flags &= ~(MSG_GEN_FLAG_MASK & flags);
908                 return;
909         }
910         default:
911                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
912         }
913 }
914
915 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg)
916 {
917         switch (msg->lm_magic) {
918         case LUSTRE_MSG_MAGIC_V2:
919         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
920                 struct ptlrpc_body *pb;
921
922                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
923                 if (!pb) {
924                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
925                         return 0;
926                 }
927                 return pb->pb_op_flags;
928         }
929         default:
930                 return 0;
931         }
932 }
933
934 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags)
935 {
936         switch (msg->lm_magic) {
937         case LUSTRE_MSG_MAGIC_V2: {
938                 struct ptlrpc_body *pb;
939
940                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
941                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
942                 pb->pb_op_flags |= flags;
943                 return;
944         }
945         default:
946                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
947         }
948 }
949
950 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags)
951 {
952         switch (msg->lm_magic) {
953         case LUSTRE_MSG_MAGIC_V2: {
954                 struct ptlrpc_body *pb;
955
956                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
957                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
958                 pb->pb_op_flags |= flags;
959                 return;
960         }
961         default:
962                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
963         }
964 }
965
966 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg)
967 {
968         switch (msg->lm_magic) {
969         case LUSTRE_MSG_MAGIC_V2:
970         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
971                 struct ptlrpc_body *pb;
972
973                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
974                 if (!pb) {
975                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
976                         return NULL;
977                 }
978                 return &pb->pb_handle;
979         }
980         default:
981                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
982                 return NULL;
983         }
984 }
985
986 __u32 lustre_msg_get_type(struct lustre_msg *msg)
987 {
988         switch (msg->lm_magic) {
989         case LUSTRE_MSG_MAGIC_V2:
990         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
991                 struct ptlrpc_body *pb;
992
993                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
994                 if (!pb) {
995                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
996                         return PTL_RPC_MSG_ERR;
997                 }
998                 return pb->pb_type;
999         }
1000         default:
1001                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1002                 return PTL_RPC_MSG_ERR;
1003         }
1004 }
1005
1006 __u32 lustre_msg_get_version(struct lustre_msg *msg)
1007 {
1008         switch (msg->lm_magic) {
1009         case LUSTRE_MSG_MAGIC_V2:
1010         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1011                 struct ptlrpc_body *pb;
1012
1013                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1014                 if (!pb) {
1015                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1016                         return 0;
1017                 }
1018                 return pb->pb_version;
1019         }
1020         default:
1021                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1022                 return 0;
1023         }
1024 }
1025
1026 void lustre_msg_add_version(struct lustre_msg *msg, int version)
1027 {
1028         switch (msg->lm_magic) {
1029         case LUSTRE_MSG_MAGIC_V2:
1030         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1031                 struct ptlrpc_body *pb;
1032
1033                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1034                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1035                 pb->pb_version |= version;
1036                 return;
1037         }
1038         default:
1039                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1040         }
1041 }
1042
1043 __u32 lustre_msg_get_opc(struct lustre_msg *msg)
1044 {
1045         switch (msg->lm_magic) {
1046         case LUSTRE_MSG_MAGIC_V2:
1047         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1048                 struct ptlrpc_body *pb;
1049
1050                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1051                 if (!pb) {
1052                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1053                         return 0;
1054                 }
1055                 return pb->pb_opc;
1056         }
1057         default:
1058                 CERROR("incorrect message magic: %08x(msg:%p)\n", msg->lm_magic, msg);
1059                 return 0;
1060         }
1061 }
1062
1063 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg)
1064 {
1065         switch (msg->lm_magic) {
1066         case LUSTRE_MSG_MAGIC_V2:
1067         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1068                 struct ptlrpc_body *pb;
1069
1070                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1071                 if (!pb) {
1072                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1073                         return 0;
1074                 }
1075                 return pb->pb_last_xid;
1076         }
1077         default:
1078                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1079                 return 0;
1080         }
1081 }
1082
1083 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg)
1084 {
1085         switch (msg->lm_magic) {
1086         case LUSTRE_MSG_MAGIC_V2:
1087         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1088                 struct ptlrpc_body *pb;
1089
1090                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1091                 if (!pb) {
1092                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1093                         return 0;
1094                 }
1095                 return pb->pb_last_committed;
1096         }
1097         default:
1098                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1099                 return 0;
1100         }
1101 }
1102
1103 __u64 lustre_msg_get_transno(struct lustre_msg *msg)
1104 {
1105         switch (msg->lm_magic) {
1106         case LUSTRE_MSG_MAGIC_V2:
1107         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1108                 struct ptlrpc_body *pb;
1109
1110                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1111                 if (!pb) {
1112                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1113                         return 0;
1114                 }
1115                 return pb->pb_transno;
1116         }
1117         default:
1118                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1119                 return 0;
1120         }
1121 }
1122
1123 int lustre_msg_get_status(struct lustre_msg *msg)
1124 {
1125         switch (msg->lm_magic) {
1126         case LUSTRE_MSG_MAGIC_V2:
1127         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1128                 struct ptlrpc_body *pb;
1129
1130                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1131                 if (!pb) {
1132                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1133                         return -EINVAL;
1134                 }
1135                 return pb->pb_status;
1136         }
1137         default:
1138                 /* status might be printed in debug code while message
1139                  * uninitialized */
1140                 return -EINVAL;
1141         }
1142 }
1143
1144 __u64 lustre_msg_get_slv(struct lustre_msg *msg)
1145 {
1146         switch (msg->lm_magic) {
1147         case LUSTRE_MSG_MAGIC_V2:
1148         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1149                 struct ptlrpc_body *pb;
1150
1151                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1152                 if (!pb) {
1153                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1154                         return -EINVAL;
1155                 }
1156                 return pb->pb_slv;
1157         }
1158         default:
1159                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1160                 return -EINVAL;
1161         }
1162 }
1163
1164
1165 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv)
1166 {
1167         switch (msg->lm_magic) {
1168         case LUSTRE_MSG_MAGIC_V2:
1169         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1170                 struct ptlrpc_body *pb;
1171
1172                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1173                 if (!pb) {
1174                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1175                         return;
1176                 }
1177                 pb->pb_slv = slv;
1178                 return;
1179         }
1180         default:
1181                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1182                 return;
1183         }
1184 }
1185
1186 __u32 lustre_msg_get_limit(struct lustre_msg *msg)
1187 {
1188         switch (msg->lm_magic) {
1189         case LUSTRE_MSG_MAGIC_V2:
1190         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1191                 struct ptlrpc_body *pb;
1192
1193                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1194                 if (!pb) {
1195                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1196                         return -EINVAL;
1197                 }
1198                 return pb->pb_limit;
1199         }
1200         default:
1201                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1202                 return -EINVAL;
1203         }
1204 }
1205
1206
1207 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit)
1208 {
1209         switch (msg->lm_magic) {
1210         case LUSTRE_MSG_MAGIC_V2:
1211         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1212                 struct ptlrpc_body *pb;
1213
1214                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1215                 if (!pb) {
1216                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1217                         return;
1218                 }
1219                 pb->pb_limit = limit;
1220                 return;
1221         }
1222         default:
1223                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1224                 return;
1225         }
1226 }
1227
1228 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg)
1229 {
1230         switch (msg->lm_magic) {
1231         case LUSTRE_MSG_MAGIC_V2:
1232         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1233                 struct ptlrpc_body *pb;
1234
1235                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1236                 if (!pb) {
1237                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1238                         return 0;
1239                 }
1240                 return pb->pb_conn_cnt;
1241         }
1242         default:
1243                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1244                 return 0;
1245         }
1246 }
1247
1248 int lustre_msg_is_v1(struct lustre_msg *msg)
1249 {
1250         switch (msg->lm_magic) {
1251         case LUSTRE_MSG_MAGIC_V1:
1252         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1253                 return 1;
1254         default:
1255                 return 0;
1256         }
1257 }
1258
1259 __u32 lustre_msg_get_magic(struct lustre_msg *msg)
1260 {
1261         switch (msg->lm_magic) {
1262         case LUSTRE_MSG_MAGIC_V2:
1263         case LUSTRE_MSG_MAGIC_V2_SWABBED:
1264                 return msg->lm_magic;
1265         default:
1266                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1267                 return 0;
1268         }
1269 }
1270
1271 __u32 lustre_msg_get_timeout(struct lustre_msg *msg)
1272 {
1273         switch (msg->lm_magic) {
1274         case LUSTRE_MSG_MAGIC_V1:
1275         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1276                 return 0;
1277         case LUSTRE_MSG_MAGIC_V2:
1278         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1279                 struct ptlrpc_body *pb;
1280
1281                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1282                 if (!pb) {
1283                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1284                         return 0;
1285
1286                 }
1287                 return pb->pb_timeout;
1288         }
1289         default:
1290                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1291                 return 0;
1292         }
1293 }
1294
1295 __u32 lustre_msg_get_service_time(struct lustre_msg *msg)
1296 {
1297         switch (msg->lm_magic) {
1298         case LUSTRE_MSG_MAGIC_V1:
1299         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1300                 return 0;
1301         case LUSTRE_MSG_MAGIC_V2:
1302         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1303                 struct ptlrpc_body *pb;
1304
1305                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1306                 if (!pb) {
1307                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1308                         return 0;
1309
1310                 }
1311                 return pb->pb_service_time;
1312         }
1313         default:
1314                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1315                 return 0;
1316         }
1317 }
1318
1319 __u32 lustre_msg_get_cksum(struct lustre_msg *msg)
1320 {
1321         switch (msg->lm_magic) {
1322         case LUSTRE_MSG_MAGIC_V1:
1323         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1324                 return 0;
1325         case LUSTRE_MSG_MAGIC_V2:
1326         case LUSTRE_MSG_MAGIC_V2_SWABBED:
1327                 return msg->lm_cksum;
1328         default:
1329                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1330                 return 0;
1331         }
1332 }
1333
1334 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg)
1335 {
1336         switch (msg->lm_magic) {
1337         case LUSTRE_MSG_MAGIC_V1:
1338         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1339                 return 0;
1340         case LUSTRE_MSG_MAGIC_V2:
1341         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1342                 struct ptlrpc_body *pb;
1343                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1344                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1345                 return crc32_le(~(__u32)0, (unsigned char *)pb, sizeof(*pb));
1346         }
1347         default:
1348                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1349                 return 0;
1350         }
1351 }
1352
1353 void lustre_msg_set_handle(struct lustre_msg *msg, struct lustre_handle *handle)
1354 {
1355         switch (msg->lm_magic) {
1356         case LUSTRE_MSG_MAGIC_V2: {
1357                 struct ptlrpc_body *pb;
1358
1359                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1360                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1361                 pb->pb_handle = *handle;
1362                 return;
1363         }
1364         default:
1365                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1366         }
1367 }
1368
1369 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type)
1370 {
1371         switch (msg->lm_magic) {
1372         case LUSTRE_MSG_MAGIC_V2: {
1373                 struct ptlrpc_body *pb;
1374
1375                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1376                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1377                 pb->pb_type = type;
1378                 return;
1379         }
1380         default:
1381                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1382         }
1383 }
1384
1385 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc)
1386 {
1387         switch (msg->lm_magic) {
1388         case LUSTRE_MSG_MAGIC_V2: {
1389                 struct ptlrpc_body *pb;
1390
1391                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1392                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1393                 pb->pb_opc = opc;
1394                 return;
1395         }
1396         default:
1397                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1398         }
1399 }
1400
1401 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid)
1402 {
1403         switch (msg->lm_magic) {
1404         case LUSTRE_MSG_MAGIC_V2: {
1405                 struct ptlrpc_body *pb;
1406
1407                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1408                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1409                 pb->pb_last_xid = last_xid;
1410                 return;
1411         }
1412         default:
1413                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1414         }
1415 }
1416
1417 void lustre_msg_set_last_committed(struct lustre_msg *msg, __u64 last_committed)
1418 {
1419         switch (msg->lm_magic) {
1420         case LUSTRE_MSG_MAGIC_V2: {
1421                 struct ptlrpc_body *pb;
1422
1423                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1424                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1425                 pb->pb_last_committed = last_committed;
1426                 return;
1427         }
1428         default:
1429                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1430         }
1431 }
1432
1433 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno)
1434 {
1435         switch (msg->lm_magic) {
1436         case LUSTRE_MSG_MAGIC_V2: {
1437                 struct ptlrpc_body *pb;
1438
1439                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1440                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1441                 pb->pb_transno = transno;
1442                 return;
1443         }
1444         default:
1445                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1446         }
1447 }
1448
1449 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status)
1450 {
1451         switch (msg->lm_magic) {
1452         case LUSTRE_MSG_MAGIC_V2: {
1453                 struct ptlrpc_body *pb;
1454
1455                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1456                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1457                 pb->pb_status = status;
1458                 return;
1459         }
1460         default:
1461                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1462         }
1463 }
1464
1465 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt)
1466 {
1467         switch (msg->lm_magic) {
1468         case LUSTRE_MSG_MAGIC_V2: {
1469                 struct ptlrpc_body *pb;
1470
1471                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1472                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1473                 pb->pb_conn_cnt = conn_cnt;
1474                 return;
1475         }
1476         default:
1477                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1478         }
1479 }
1480
1481 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout)
1482 {
1483         switch (msg->lm_magic) {
1484         case LUSTRE_MSG_MAGIC_V1:
1485                 return;
1486         case LUSTRE_MSG_MAGIC_V2: {
1487                 struct ptlrpc_body *pb;
1488
1489                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1490                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1491                 pb->pb_timeout = timeout;
1492                 return;
1493         }
1494         default:
1495                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1496         }
1497 }
1498
1499 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time)
1500 {
1501         switch (msg->lm_magic) {
1502         case LUSTRE_MSG_MAGIC_V1:
1503                 return;
1504         case LUSTRE_MSG_MAGIC_V2: {
1505                 struct ptlrpc_body *pb;
1506
1507                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1508                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1509                 pb->pb_service_time = service_time;
1510                 return;
1511         }
1512         default:
1513                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1514         }
1515 }
1516
1517 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum)
1518 {
1519         switch (msg->lm_magic) {
1520         case LUSTRE_MSG_MAGIC_V1:
1521                 return;
1522         case LUSTRE_MSG_MAGIC_V2:
1523                 msg->lm_cksum = cksum;
1524                 return;
1525         default:
1526                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1527         }
1528 }
1529
1530
1531 void ptlrpc_request_set_replen(struct ptlrpc_request *req)
1532 {
1533         int count = req_capsule_filled_sizes(&req->rq_pill, RCL_SERVER);
1534
1535         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count,
1536                                          req->rq_pill.rc_area[RCL_SERVER]);
1537         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1538                 req->rq_reqmsg->lm_repsize = req->rq_replen;
1539 }
1540
1541 void ptlrpc_req_set_repsize(struct ptlrpc_request *req, int count, __u32 *lens)
1542 {
1543         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens);
1544         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1545                 req->rq_reqmsg->lm_repsize = req->rq_replen;
1546 }
1547
1548 /* byte flipping routines for all wire types declared in
1549  * lustre_idl.h implemented here.
1550  */
1551 void lustre_swab_ptlrpc_body(struct ptlrpc_body *b)
1552 {
1553         __swab32s (&b->pb_type);
1554         __swab32s (&b->pb_version);
1555         __swab32s (&b->pb_opc);
1556         __swab32s (&b->pb_status);
1557         __swab64s (&b->pb_last_xid);
1558         __swab64s (&b->pb_last_seen);
1559         __swab64s (&b->pb_last_committed);
1560         __swab64s (&b->pb_transno);
1561         __swab32s (&b->pb_flags);
1562         __swab32s (&b->pb_op_flags);
1563         __swab32s (&b->pb_conn_cnt);
1564         __swab32s (&b->pb_timeout);
1565         __swab32s (&b->pb_service_time);
1566         __swab32s (&b->pb_limit);
1567         __swab64s (&b->pb_slv);
1568         __swab64s (&b->pb_pre_versions[0]);
1569         __swab64s (&b->pb_pre_versions[1]);
1570         __swab64s (&b->pb_pre_versions[2]);
1571         __swab64s (&b->pb_pre_versions[3]);
1572         CLASSERT(offsetof(typeof(*b), pb_padding) != 0);
1573 }
1574
1575 void lustre_swab_connect(struct obd_connect_data *ocd)
1576 {
1577         __swab64s(&ocd->ocd_connect_flags);
1578         __swab32s(&ocd->ocd_version);
1579         __swab32s(&ocd->ocd_grant);
1580         __swab64s(&ocd->ocd_ibits_known);
1581         __swab32s(&ocd->ocd_index);
1582         __swab32s(&ocd->ocd_brw_size);
1583         __swab32s(&ocd->ocd_nllu);
1584         __swab32s(&ocd->ocd_nllg);
1585         __swab64s(&ocd->ocd_transno);
1586         __swab32s(&ocd->ocd_group);
1587         __swab32s(&ocd->ocd_cksum_types);
1588         CLASSERT(offsetof(typeof(*ocd), padding1) != 0);
1589         CLASSERT(offsetof(typeof(*ocd), padding2) != 0);
1590 }
1591
1592 void lustre_swab_obdo (struct obdo  *o)
1593 {
1594         __swab64s (&o->o_valid);
1595         __swab64s (&o->o_id);
1596         __swab64s (&o->o_gr);
1597         __swab64s (&o->o_fid);
1598         __swab64s (&o->o_size);
1599         __swab64s (&o->o_mtime);
1600         __swab64s (&o->o_atime);
1601         __swab64s (&o->o_ctime);
1602         __swab64s (&o->o_blocks);
1603         __swab64s (&o->o_grant);
1604         __swab32s (&o->o_blksize);
1605         __swab32s (&o->o_mode);
1606         __swab32s (&o->o_uid);
1607         __swab32s (&o->o_gid);
1608         __swab32s (&o->o_flags);
1609         __swab32s (&o->o_nlink);
1610         __swab32s (&o->o_generation);
1611         __swab32s (&o->o_misc);
1612         __swab32s (&o->o_easize);
1613         __swab32s (&o->o_mds);
1614         __swab32s (&o->o_stripe_idx);
1615         __swab32s (&o->o_padding_1);
1616         /* o_inline is opaque */
1617 }
1618
1619 void lustre_swab_obd_statfs (struct obd_statfs *os)
1620 {
1621         __swab64s (&os->os_type);
1622         __swab64s (&os->os_blocks);
1623         __swab64s (&os->os_bfree);
1624         __swab64s (&os->os_bavail);
1625         __swab64s (&os->os_files);
1626         __swab64s (&os->os_ffree);
1627         /* no need to swab os_fsid */
1628         __swab32s (&os->os_bsize);
1629         __swab32s (&os->os_namelen);
1630         __swab64s (&os->os_maxbytes);
1631         __swab32s (&os->os_state);
1632         /* no need to swap os_spare */
1633 }
1634
1635 void lustre_swab_obd_ioobj (struct obd_ioobj *ioo)
1636 {
1637         __swab64s (&ioo->ioo_id);
1638         __swab64s (&ioo->ioo_gr);
1639         __swab32s (&ioo->ioo_type);
1640         __swab32s (&ioo->ioo_bufcnt);
1641 }
1642
1643 void lustre_swab_niobuf_remote (struct niobuf_remote *nbr)
1644 {
1645         __swab64s (&nbr->offset);
1646         __swab32s (&nbr->len);
1647         __swab32s (&nbr->flags);
1648 }
1649
1650 void lustre_swab_ost_body (struct ost_body *b)
1651 {
1652         lustre_swab_obdo (&b->oa);
1653 }
1654
1655 void lustre_swab_ost_last_id(obd_id *id)
1656 {
1657         __swab64s(id);
1658 }
1659
1660 void lustre_swab_generic_32s(__u32 *val)
1661 {
1662         __swab32s(val);
1663 }
1664
1665 void lustre_swab_ost_lvb(struct ost_lvb *lvb)
1666 {
1667         __swab64s(&lvb->lvb_size);
1668         __swab64s(&lvb->lvb_mtime);
1669         __swab64s(&lvb->lvb_atime);
1670         __swab64s(&lvb->lvb_ctime);
1671         __swab64s(&lvb->lvb_blocks);
1672 }
1673
1674 void lustre_swab_mds_status_req (struct mds_status_req *r)
1675 {
1676         __swab32s (&r->flags);
1677         __swab32s (&r->repbuf);
1678 }
1679
1680 void lustre_swab_mds_body (struct mds_body *b)
1681 {
1682         lustre_swab_ll_fid (&b->fid1);
1683         lustre_swab_ll_fid (&b->fid2);
1684         /* handle is opaque */
1685         __swab64s (&b->valid);
1686         __swab64s (&b->size);
1687         __swab64s (&b->mtime);
1688         __swab64s (&b->atime);
1689         __swab64s (&b->ctime);
1690         __swab64s (&b->blocks);
1691         __swab64s (&b->io_epoch);
1692         __swab64s (&b->ino);
1693         __swab32s (&b->fsuid);
1694         __swab32s (&b->fsgid);
1695         __swab32s (&b->capability);
1696         __swab32s (&b->mode);
1697         __swab32s (&b->uid);
1698         __swab32s (&b->gid);
1699         __swab32s (&b->flags);
1700         __swab32s (&b->rdev);
1701         __swab32s (&b->nlink);
1702         __swab32s (&b->generation);
1703         __swab32s (&b->suppgid);
1704         __swab32s (&b->eadatasize);
1705         __swab32s (&b->aclsize);
1706         __swab32s (&b->max_mdsize);
1707         __swab32s (&b->max_cookiesize);
1708         __swab32s (&b->padding_4);
1709 }
1710
1711 void lustre_swab_mdt_body (struct mdt_body *b)
1712 {
1713         lustre_swab_lu_fid (&b->fid1);
1714         lustre_swab_lu_fid (&b->fid2);
1715         /* handle is opaque */
1716         __swab64s (&b->valid);
1717         __swab64s (&b->size);
1718         __swab64s (&b->mtime);
1719         __swab64s (&b->atime);
1720         __swab64s (&b->ctime);
1721         __swab64s (&b->blocks);
1722         __swab64s (&b->ioepoch);
1723         __swab64s (&b->ino);
1724         __swab32s (&b->fsuid);
1725         __swab32s (&b->fsgid);
1726         __swab32s (&b->capability);
1727         __swab32s (&b->mode);
1728         __swab32s (&b->uid);
1729         __swab32s (&b->gid);
1730         __swab32s (&b->flags);
1731         __swab32s (&b->rdev);
1732         __swab32s (&b->nlink);
1733         __swab32s (&b->generation);
1734         __swab32s (&b->suppgid);
1735         __swab32s (&b->eadatasize);
1736         __swab32s (&b->aclsize);
1737         __swab32s (&b->max_mdsize);
1738         __swab32s (&b->max_cookiesize);
1739         __swab32s (&b->padding_4);
1740 }
1741
1742 void lustre_swab_mdt_epoch (struct mdt_epoch *b)
1743 {
1744         /* handle is opaque */
1745          __swab64s (&b->ioepoch);
1746          __swab32s (&b->flags);
1747          CLASSERT(offsetof(typeof(*b), padding) != 0);
1748 }
1749
1750 void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
1751 {
1752         int i;
1753         __swab32s(&mti->mti_lustre_ver);
1754         __swab32s(&mti->mti_stripe_index);
1755         __swab32s(&mti->mti_config_ver);
1756         __swab32s(&mti->mti_flags);
1757         __swab32s(&mti->mti_nid_count);
1758         CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
1759         for (i = 0; i < MTI_NIDS_MAX; i++)
1760                 __swab64s(&mti->mti_nids[i]);
1761 }
1762
1763 static void lustre_swab_obd_dqinfo (struct obd_dqinfo *i)
1764 {
1765         __swab64s (&i->dqi_bgrace);
1766         __swab64s (&i->dqi_igrace);
1767         __swab32s (&i->dqi_flags);
1768         __swab32s (&i->dqi_valid);
1769 }
1770
1771 static void lustre_swab_obd_dqblk (struct obd_dqblk *b)
1772 {
1773         __swab64s (&b->dqb_ihardlimit);
1774         __swab64s (&b->dqb_isoftlimit);
1775         __swab64s (&b->dqb_curinodes);
1776         __swab64s (&b->dqb_bhardlimit);
1777         __swab64s (&b->dqb_bsoftlimit);
1778         __swab64s (&b->dqb_curspace);
1779         __swab64s (&b->dqb_btime);
1780         __swab64s (&b->dqb_itime);
1781         __swab32s (&b->dqb_valid);
1782         CLASSERT(offsetof(typeof(*b), padding) != 0);
1783 }
1784
1785 void lustre_swab_obd_quotactl (struct obd_quotactl *q)
1786 {
1787         __swab32s (&q->qc_cmd);
1788         __swab32s (&q->qc_type);
1789         __swab32s (&q->qc_id);
1790         __swab32s (&q->qc_stat);
1791         lustre_swab_obd_dqinfo (&q->qc_dqinfo);
1792         lustre_swab_obd_dqblk (&q->qc_dqblk);
1793 }
1794
1795 void lustre_swab_mds_remote_perm (struct mds_remote_perm *p)
1796 {
1797         __swab32s (&p->rp_uid);
1798         __swab32s (&p->rp_gid);
1799         __swab32s (&p->rp_fsuid);
1800         __swab32s (&p->rp_fsgid);
1801         __swab32s (&p->rp_access_perm);
1802 };
1803
1804 void lustre_swab_mdt_remote_perm (struct mdt_remote_perm *p)
1805 {
1806         __swab32s (&p->rp_uid);
1807         __swab32s (&p->rp_gid);
1808         __swab32s (&p->rp_fsuid);
1809         __swab32s (&p->rp_fsgid);
1810         __swab32s (&p->rp_access_perm);
1811 };
1812
1813 void lustre_swab_mds_rec_setattr (struct mds_rec_setattr *sa)
1814 {
1815         __swab32s (&sa->sa_opcode);
1816         __swab32s (&sa->sa_fsuid);
1817         __swab32s (&sa->sa_fsgid);
1818         __swab32s (&sa->sa_cap);
1819         __swab32s (&sa->sa_suppgid);
1820         __swab32s (&sa->sa_mode);
1821         lustre_swab_ll_fid (&sa->sa_fid);
1822         __swab64s (&sa->sa_valid);
1823         __swab64s (&sa->sa_size);
1824         __swab64s (&sa->sa_mtime);
1825         __swab64s (&sa->sa_atime);
1826         __swab64s (&sa->sa_ctime);
1827         __swab32s (&sa->sa_uid);
1828         __swab32s (&sa->sa_gid);
1829         __swab32s (&sa->sa_attr_flags);
1830         CLASSERT(offsetof(typeof(*sa), sa_padding) != 0);
1831 }
1832
1833 void lustre_swab_mds_rec_join (struct mds_rec_join *jr)
1834 {
1835         __swab64s(&jr->jr_headsize);
1836         lustre_swab_ll_fid(&jr->jr_fid);
1837 }
1838
1839 void lustre_swab_mdt_rec_join (struct mdt_rec_join *jr)
1840 {
1841         __swab64s(&jr->jr_headsize);
1842         lustre_swab_lu_fid(&jr->jr_fid);
1843 }
1844
1845 void lustre_swab_mds_rec_create (struct mds_rec_create *cr)
1846 {
1847         __swab32s (&cr->cr_opcode);
1848         __swab32s (&cr->cr_fsuid);
1849         __swab32s (&cr->cr_fsgid);
1850         __swab32s (&cr->cr_cap);
1851         __swab32s (&cr->cr_flags); /* for use with open */
1852         __swab32s (&cr->cr_mode);
1853         lustre_swab_ll_fid (&cr->cr_fid);
1854         lustre_swab_ll_fid (&cr->cr_replayfid);
1855         __swab64s (&cr->cr_time);
1856         __swab64s (&cr->cr_rdev);
1857         __swab32s (&cr->cr_suppgid);
1858         CLASSERT(offsetof(typeof(*cr), cr_padding_1) != 0);
1859         CLASSERT(offsetof(typeof(*cr), cr_padding_2) != 0);
1860         CLASSERT(offsetof(typeof(*cr), cr_padding_3) != 0);
1861         CLASSERT(offsetof(typeof(*cr), cr_padding_4) != 0);
1862         CLASSERT(offsetof(typeof(*cr), cr_padding_5) != 0);
1863 }
1864
1865 void lustre_swab_mds_rec_link (struct mds_rec_link *lk)
1866 {
1867         __swab32s (&lk->lk_opcode);
1868         __swab32s (&lk->lk_fsuid);
1869         __swab32s (&lk->lk_fsgid);
1870         __swab32s (&lk->lk_cap);
1871         __swab32s (&lk->lk_suppgid1);
1872         __swab32s (&lk->lk_suppgid2);
1873         lustre_swab_ll_fid (&lk->lk_fid1);
1874         lustre_swab_ll_fid (&lk->lk_fid2);
1875         __swab64s (&lk->lk_time);
1876         CLASSERT(offsetof(typeof(*lk), lk_padding_1) != 0);
1877         CLASSERT(offsetof(typeof(*lk), lk_padding_2) != 0);
1878         CLASSERT(offsetof(typeof(*lk), lk_padding_3) != 0);
1879         CLASSERT(offsetof(typeof(*lk), lk_padding_4) != 0);
1880 }
1881
1882 void lustre_swab_mds_rec_unlink (struct mds_rec_unlink *ul)
1883 {
1884         __swab32s (&ul->ul_opcode);
1885         __swab32s (&ul->ul_fsuid);
1886         __swab32s (&ul->ul_fsgid);
1887         __swab32s (&ul->ul_cap);
1888         __swab32s (&ul->ul_suppgid);
1889         __swab32s (&ul->ul_mode);
1890         lustre_swab_ll_fid (&ul->ul_fid1);
1891         lustre_swab_ll_fid (&ul->ul_fid2);
1892         __swab64s (&ul->ul_time);
1893         CLASSERT(offsetof(typeof(*ul), ul_padding_1) != 0);
1894         CLASSERT(offsetof(typeof(*ul), ul_padding_2) != 0);
1895         CLASSERT(offsetof(typeof(*ul), ul_padding_3) != 0);
1896         CLASSERT(offsetof(typeof(*ul), ul_padding_4) != 0);
1897 }
1898
1899 void lustre_swab_fiemap_extent(struct ll_fiemap_extent *fm_extent)
1900 {
1901         __swab64s(&fm_extent->fe_logical);
1902         __swab64s(&fm_extent->fe_physical);
1903         __swab64s(&fm_extent->fe_length);
1904         __swab32s(&fm_extent->fe_flags);
1905         __swab32s(&fm_extent->fe_device);
1906 }
1907
1908 void lustre_swab_fiemap(struct ll_user_fiemap *fiemap)
1909 {
1910         int i;
1911
1912         __swab64s(&fiemap->fm_start);
1913         __swab64s(&fiemap->fm_length);
1914         __swab32s(&fiemap->fm_flags);
1915         __swab32s(&fiemap->fm_mapped_extents);
1916         __swab32s(&fiemap->fm_extent_count);
1917         __swab32s(&fiemap->fm_reserved);
1918
1919         for (i = 0; i < fiemap->fm_mapped_extents; i++)
1920                 lustre_swab_fiemap_extent(&fiemap->fm_extents[i]);
1921 }
1922
1923 void lustre_swab_mds_rec_rename (struct mds_rec_rename *rn)
1924 {
1925         __swab32s (&rn->rn_opcode);
1926         __swab32s (&rn->rn_fsuid);
1927         __swab32s (&rn->rn_fsgid);
1928         __swab32s (&rn->rn_cap);
1929         __swab32s (&rn->rn_suppgid1);
1930         __swab32s (&rn->rn_suppgid2);
1931         lustre_swab_ll_fid (&rn->rn_fid1);
1932         lustre_swab_ll_fid (&rn->rn_fid2);
1933         __swab64s (&rn->rn_time);
1934         CLASSERT(offsetof(typeof(*rn), rn_padding_1) != 0);
1935         CLASSERT(offsetof(typeof(*rn), rn_padding_2) != 0);
1936         CLASSERT(offsetof(typeof(*rn), rn_padding_3) != 0);
1937         CLASSERT(offsetof(typeof(*rn), rn_padding_4) != 0);
1938 }
1939
1940 void lustre_swab_mdt_rec_reint (struct mdt_rec_reint *rr)
1941 {
1942         __swab32s (&rr->rr_opcode);
1943         __swab32s (&rr->rr_fsuid);
1944         __swab32s (&rr->rr_fsgid);
1945         __swab32s (&rr->rr_cap);
1946         __swab32s (&rr->rr_suppgid1);
1947         __swab32s (&rr->rr_suppgid2);
1948         /* handle is opaque */
1949         lustre_swab_lu_fid (&rr->rr_fid1);
1950         lustre_swab_lu_fid (&rr->rr_fid2);
1951         __swab64s (&rr->rr_mtime);
1952         __swab64s (&rr->rr_atime);
1953         __swab64s (&rr->rr_ctime);
1954         __swab64s (&rr->rr_size);
1955         __swab64s (&rr->rr_blocks);
1956         __swab32s (&rr->rr_bias);
1957         __swab32s (&rr->rr_mode);
1958         __swab32s (&rr->rr_padding_1);
1959         __swab32s (&rr->rr_padding_2);
1960         __swab32s (&rr->rr_padding_3);
1961         __swab32s (&rr->rr_padding_4);
1962
1963         CLASSERT(offsetof(typeof(*rr), rr_padding_1) != 0);
1964         CLASSERT(offsetof(typeof(*rr), rr_padding_2) != 0);
1965         CLASSERT(offsetof(typeof(*rr), rr_padding_3) != 0);
1966         CLASSERT(offsetof(typeof(*rr), rr_padding_4) != 0);
1967 };
1968
1969 void lustre_swab_lov_desc (struct lov_desc *ld)
1970 {
1971         __swab32s (&ld->ld_tgt_count);
1972         __swab32s (&ld->ld_active_tgt_count);
1973         __swab32s (&ld->ld_default_stripe_count);
1974         __swab64s (&ld->ld_default_stripe_size);
1975         __swab64s (&ld->ld_default_stripe_offset);
1976         __swab32s (&ld->ld_pattern);
1977         __swab32s (&ld->ld_qos_maxage);
1978         /* uuid endian insensitive */
1979 }
1980
1981 /*begin adding MDT by huanghua@clusterfs.com*/
1982 void lustre_swab_lmv_desc (struct lmv_desc *ld)
1983 {
1984         __swab32s (&ld->ld_tgt_count);
1985         __swab32s (&ld->ld_active_tgt_count);
1986         /* uuid endian insensitive */
1987 }
1988 /*end adding MDT by huanghua@clusterfs.com*/
1989 void lustre_swab_md_fld (struct md_fld *mf)
1990 {
1991         __swab64s(&mf->mf_seq);
1992         __swab64s(&mf->mf_mds);
1993 }
1994
1995 static void print_lum (struct lov_user_md *lum)
1996 {
1997         CDEBUG(D_OTHER, "lov_user_md %p:\n", lum);
1998         CDEBUG(D_OTHER, "\tlmm_magic: %#x\n", lum->lmm_magic);
1999         CDEBUG(D_OTHER, "\tlmm_pattern: %#x\n", lum->lmm_pattern);
2000         CDEBUG(D_OTHER, "\tlmm_object_id: "LPU64"\n", lum->lmm_object_id);
2001         CDEBUG(D_OTHER, "\tlmm_object_gr: "LPU64"\n", lum->lmm_object_gr);
2002         CDEBUG(D_OTHER, "\tlmm_stripe_size: %#x\n", lum->lmm_stripe_size);
2003         CDEBUG(D_OTHER, "\tlmm_stripe_count: %#x\n", lum->lmm_stripe_count);
2004         CDEBUG(D_OTHER, "\tlmm_stripe_offset: %#x\n", lum->lmm_stripe_offset);
2005 }
2006
2007 static void lustre_swab_lov_user_md_common(struct lov_user_md_v1 *lum)
2008 {
2009         ENTRY;
2010         __swab32s(&lum->lmm_magic);
2011         __swab32s(&lum->lmm_pattern);
2012         __swab64s(&lum->lmm_object_id);
2013         __swab64s(&lum->lmm_object_gr);
2014         __swab32s(&lum->lmm_stripe_size);
2015         __swab16s(&lum->lmm_stripe_count);
2016         __swab16s(&lum->lmm_stripe_offset);
2017         print_lum(lum);
2018         EXIT;
2019 }
2020
2021 static void print_lumj (struct lov_user_md_join *lumj)
2022 {
2023         CDEBUG(D_OTHER, "lov_user_md %p:\n", lumj);
2024         CDEBUG(D_OTHER, "\tlmm_magic: %#x\n", lumj->lmm_magic);
2025         CDEBUG(D_OTHER, "\tlmm_pattern: %#x\n", lumj->lmm_pattern);
2026         CDEBUG(D_OTHER, "\tlmm_object_id: "LPU64"\n", lumj->lmm_object_id);
2027         CDEBUG(D_OTHER, "\tlmm_object_gr: "LPU64"\n", lumj->lmm_object_gr);
2028         CDEBUG(D_OTHER, "\tlmm_stripe_size: %#x\n", lumj->lmm_stripe_size);
2029         CDEBUG(D_OTHER, "\tlmm_stripe_count: %#x\n", lumj->lmm_stripe_count);
2030         CDEBUG(D_OTHER, "\tlmm_extent_count: %#x\n", lumj->lmm_extent_count);
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_user_md_join(struct lov_user_md_join *lumj)
2051 {
2052         ENTRY;
2053         CDEBUG(D_IOCTL, "swabbing lov_user_md_join\n");
2054         __swab32s(&lumj->lmm_magic);
2055         __swab32s(&lumj->lmm_pattern);
2056         __swab64s(&lumj->lmm_object_id);
2057         __swab64s(&lumj->lmm_object_gr);
2058         __swab32s(&lumj->lmm_stripe_size);
2059         __swab32s(&lumj->lmm_stripe_count);
2060         __swab32s(&lumj->lmm_extent_count);
2061         print_lumj(lumj);
2062         EXIT;
2063 }
2064
2065 void lustre_swab_lov_user_md_objects(struct lov_user_ost_data *lod,
2066                                      int stripe_count)
2067 {
2068         int i;
2069         ENTRY;
2070         for (i = 0; i < stripe_count; i++) {
2071                 __swab64s(&(lod[i].l_object_id));
2072                 __swab64s(&(lod[i].l_object_gr));
2073                 __swab32s(&(lod[i].l_ost_gen));
2074                 __swab32s(&(lod[i].l_ost_idx));
2075         }
2076         EXIT;
2077 }
2078
2079
2080 void lustre_swab_ldlm_res_id (struct ldlm_res_id *id)
2081 {
2082         int  i;
2083
2084         for (i = 0; i < RES_NAME_SIZE; i++)
2085                 __swab64s (&id->name[i]);
2086 }
2087
2088 void lustre_swab_ldlm_policy_data (ldlm_policy_data_t *d)
2089 {
2090         /* the lock data is a union and the first two fields are always an
2091          * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
2092          * data the same way. */
2093         __swab64s(&d->l_extent.start);
2094         __swab64s(&d->l_extent.end);
2095         __swab64s(&d->l_extent.gid);
2096         __swab32s(&d->l_flock.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 }
2153
2154 void lustre_swab_qdata_old(struct qunit_data_old *d)
2155 {
2156         __swab32s (&d->qd_id);
2157         __swab32s (&d->qd_type);
2158         __swab32s (&d->qd_count);
2159         __swab32s (&d->qd_isblk);
2160 }
2161
2162 #ifdef __KERNEL__
2163 struct qunit_data *lustre_quota_old_to_new(struct qunit_data_old *d)
2164 {
2165         struct qunit_data_old tmp;
2166         struct qunit_data *ret;
2167         ENTRY;
2168
2169         if (!d)
2170                 return NULL;
2171
2172         tmp = *d;
2173         ret = (struct qunit_data *)d;
2174         ret->qd_id = tmp.qd_id;
2175         ret->qd_flags = (tmp.qd_type ? QUOTA_IS_GRP : 0) | (tmp.qd_isblk ? QUOTA_IS_BLOCK : 0);
2176         ret->qd_count = tmp.qd_count;
2177         RETURN(ret);
2178
2179 }
2180 EXPORT_SYMBOL(lustre_quota_old_to_new);
2181
2182 struct qunit_data_old *lustre_quota_new_to_old(struct qunit_data *d)
2183 {
2184         struct qunit_data tmp;
2185         struct qunit_data_old *ret;
2186         ENTRY;
2187
2188         if (!d)
2189                 return NULL;
2190
2191         tmp = *d;
2192         ret = (struct qunit_data_old *)d;
2193         ret->qd_id = tmp.qd_id;
2194         ret->qd_type = ((tmp.qd_flags & QUOTA_IS_GRP) ? GRPQUOTA : USRQUOTA);
2195         ret->qd_count = (__u32)tmp.qd_count;
2196         ret->qd_isblk = ((tmp.qd_flags & QUOTA_IS_BLOCK) ? 1 : 0);
2197         RETURN(ret);
2198 }
2199 EXPORT_SYMBOL(lustre_quota_new_to_old);
2200 #endif /* __KERNEL__ */
2201
2202 static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2203 {
2204         LASSERT(req->rq_reqmsg);
2205
2206         switch (req->rq_reqmsg->lm_magic) {
2207         case LUSTRE_MSG_MAGIC_V2:
2208         case LUSTRE_MSG_MAGIC_V2_SWABBED:
2209                 return lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
2210         default:
2211                 CERROR("bad lustre msg magic: %#08X\n",
2212                        req->rq_reqmsg->lm_magic);
2213         }
2214         return 0;
2215 }
2216
2217 static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2218 {
2219         LASSERT(req->rq_repmsg);
2220
2221         switch (req->rq_repmsg->lm_magic) {
2222         case LUSTRE_MSG_MAGIC_V2:
2223         case LUSTRE_MSG_MAGIC_V2_SWABBED:
2224                 return lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
2225         default:
2226                 /* uninitialized yet */
2227                 return 0;
2228         }
2229 }
2230
2231 void _debug_req(struct ptlrpc_request *req, __u32 mask,
2232                 struct libcfs_debug_msg_data *data, const char *fmt, ... )
2233 {
2234         va_list args;
2235
2236         va_start(args, fmt);
2237         libcfs_debug_vmsg2(data->msg_cdls, data->msg_subsys, mask, data->msg_file,
2238                            data->msg_fn, data->msg_line, fmt, args,
2239                            " req@%p x"LPD64"/t"LPD64"("LPD64") o%d->%s@%s:%d/%d"
2240                            " lens %d/%d e %d to %d dl "CFS_TIME_T" ref %d "
2241                            "fl "REQ_FLAGS_FMT"/%x/%x rc %d/%d\n",
2242                            req, req->rq_xid, req->rq_transno,
2243                            req->rq_reqmsg ? lustre_msg_get_transno(req->rq_reqmsg) : 0,
2244                            req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : -1,
2245                            req->rq_import ? obd2cli_tgt(req->rq_import->imp_obd) :
2246                            req->rq_export ?
2247                            (char*)req->rq_export->exp_client_uuid.uuid : "<?>",
2248                            req->rq_import ?
2249                            (char *)req->rq_import->imp_connection->c_remote_uuid.uuid :
2250                            req->rq_export ?
2251                            (char *)req->rq_export->exp_connection->c_remote_uuid.uuid : "<?>",
2252                            req->rq_request_portal, req->rq_reply_portal,
2253                            req->rq_reqlen, req->rq_replen,
2254                            req->rq_early_count, req->rq_timeout, req->rq_deadline,
2255                            atomic_read(&req->rq_refcount), DEBUG_REQ_FLAGS(req),
2256                            req->rq_reqmsg && req_ptlrpc_body_swabbed(req) ?
2257                            lustre_msg_get_flags(req->rq_reqmsg) : -1,
2258                            req->rq_repmsg && rep_ptlrpc_body_swabbed(req) ?
2259                            lustre_msg_get_flags(req->rq_repmsg) : -1,
2260                            req->rq_status,
2261                            req->rq_repmsg && rep_ptlrpc_body_swabbed(req) ?
2262                            lustre_msg_get_status(req->rq_repmsg) : -1);
2263 }
2264 EXPORT_SYMBOL(_debug_req);
2265
2266 void lustre_swab_lustre_capa(struct lustre_capa *c)
2267 {
2268         lustre_swab_lu_fid(&c->lc_fid);
2269         __swab64s (&c->lc_opc);
2270         __swab32s (&c->lc_uid);
2271         __swab32s (&c->lc_flags);
2272         __swab32s (&c->lc_keyid);
2273         __swab32s (&c->lc_timeout);
2274         __swab64s (&c->lc_expiry);
2275 }
2276
2277 void lustre_swab_lustre_capa_key (struct lustre_capa_key *k)
2278 {
2279         __swab64s (&k->lk_mdsid);
2280         __swab32s (&k->lk_keyid);
2281         __swab32s (&k->lk_padding);
2282 }