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