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