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