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