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