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