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