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