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