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