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