Whamcloud - gitweb
Fix an error where the garbage may be returned by this function because
[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                 CERROR("incorrect message magic: %08x\n", magic);
132                 return 0;
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                 CERROR("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                 CERROR("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 = NULL;
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                 CERROR("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                 if (!pb) {
1042                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1043                         return 0;
1044                 }
1045                 return pb->pb_flags;
1046         }
1047         default:
1048                 /* flags might be printed in debug code while message
1049                  * uninitialized */
1050                 return 0;
1051         }
1052 }
1053
1054 void lustre_msg_add_flags(struct lustre_msg *msg, int flags)
1055 {
1056         switch (msg->lm_magic) {
1057         case LUSTRE_MSG_MAGIC_V1:
1058                 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1059                                         MSG_GEN_FLAG_MASK & flags;
1060                 return;
1061         case LUSTRE_MSG_MAGIC_V2: {
1062                 struct ptlrpc_body *pb;
1063
1064                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1065                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1066                 pb->pb_flags |= flags;
1067                 return;
1068         }
1069         default:
1070                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1071         }
1072 }
1073
1074 void lustre_msg_set_flags(struct lustre_msg *msg, int flags)
1075 {
1076         switch (msg->lm_magic) {
1077         case LUSTRE_MSG_MAGIC_V1:
1078                 ((struct lustre_msg_v1 *)msg)->lm_flags &= ~MSG_GEN_FLAG_MASK;
1079                 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1080                                         MSG_GEN_FLAG_MASK & flags;
1081                 return;
1082         case LUSTRE_MSG_MAGIC_V2: {
1083                 struct ptlrpc_body *pb;
1084
1085                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1086                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1087                 pb->pb_flags = flags;
1088                 return;
1089         }
1090         default:
1091                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1092         }
1093 }
1094
1095 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags)
1096 {
1097         switch (msg->lm_magic) {
1098         case LUSTRE_MSG_MAGIC_V1:
1099         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1100                 ((struct lustre_msg_v1 *)msg)->lm_flags &=
1101                                         ~(MSG_GEN_FLAG_MASK & flags);
1102                 return;
1103         case LUSTRE_MSG_MAGIC_V2:
1104         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1105                 struct ptlrpc_body *pb;
1106
1107                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1108                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1109                 pb->pb_flags &= ~(MSG_GEN_FLAG_MASK & flags);
1110                 return;
1111         }
1112         default:
1113                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1114         }
1115 }
1116
1117 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg)
1118 {
1119         switch (msg->lm_magic) {
1120         case LUSTRE_MSG_MAGIC_V1:
1121         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1122                 return ((struct lustre_msg_v1 *)msg)->lm_flags >>
1123                        MSG_OP_FLAG_SHIFT;
1124         case LUSTRE_MSG_MAGIC_V2:
1125         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1126                 struct ptlrpc_body *pb;
1127
1128                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1129                 if (!pb) {
1130                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1131                         return 0;
1132                 }
1133                 return pb->pb_op_flags;
1134         }
1135         default:
1136                 return 0;
1137         }
1138 }
1139
1140 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags)
1141 {
1142         switch (msg->lm_magic) {
1143         case LUSTRE_MSG_MAGIC_V1:
1144                 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1145                         (flags & MSG_GEN_FLAG_MASK) << MSG_OP_FLAG_SHIFT;
1146                 return;
1147         case LUSTRE_MSG_MAGIC_V2: {
1148                 struct ptlrpc_body *pb;
1149
1150                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1151                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1152                 pb->pb_op_flags |= flags;
1153                 return;
1154         }
1155         default:
1156                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1157         }
1158 }
1159
1160 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags)
1161 {
1162         switch (msg->lm_magic) {
1163         case LUSTRE_MSG_MAGIC_V1:
1164                 ((struct lustre_msg_v1 *)msg)->lm_flags &= ~MSG_OP_FLAG_MASK;
1165                 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1166                         ((flags & MSG_GEN_FLAG_MASK) <<MSG_OP_FLAG_SHIFT);
1167                 return;
1168         case LUSTRE_MSG_MAGIC_V2: {
1169                 struct ptlrpc_body *pb;
1170
1171                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1172                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1173                 pb->pb_op_flags |= flags;
1174                 return;
1175         }
1176         default:
1177                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1178         }
1179 }
1180
1181 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg)
1182 {
1183         switch (msg->lm_magic) {
1184         case LUSTRE_MSG_MAGIC_V1:
1185         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1186                 return &((struct lustre_msg_v1 *)msg)->lm_handle;
1187         case LUSTRE_MSG_MAGIC_V2:
1188         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1189                 struct ptlrpc_body *pb;
1190
1191                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1192                 if (!pb) {
1193                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1194                         return NULL;
1195                 }
1196                 return &pb->pb_handle;
1197         }
1198         default:
1199                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1200                 return NULL;
1201         }
1202 }
1203
1204 __u32 lustre_msg_get_type(struct lustre_msg *msg)
1205 {
1206         switch (msg->lm_magic) {
1207         case LUSTRE_MSG_MAGIC_V1:
1208         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1209                 return ((struct lustre_msg_v1 *)msg)->lm_type;
1210         case LUSTRE_MSG_MAGIC_V2:
1211         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1212                 struct ptlrpc_body *pb;
1213
1214                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1215                 if (!pb) {
1216                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1217                         return PTL_RPC_MSG_ERR;
1218                 }
1219                 return pb->pb_type;
1220         }
1221         default:
1222                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1223                 return PTL_RPC_MSG_ERR;
1224         }
1225 }
1226
1227 __u32 lustre_msg_get_version(struct lustre_msg *msg)
1228 {
1229         switch (msg->lm_magic) {
1230         case LUSTRE_MSG_MAGIC_V1:
1231         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1232                 return ((struct lustre_msg_v1 *)msg)->lm_version;
1233         case LUSTRE_MSG_MAGIC_V2:
1234         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1235                 struct ptlrpc_body *pb;
1236
1237                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1238                 if (!pb) {
1239                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1240                         return 0;
1241                 }
1242                 return pb->pb_version;
1243         }
1244         default:
1245                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1246                 return 0;
1247         }
1248 }
1249
1250 void lustre_msg_add_version(struct lustre_msg *msg, int version)
1251 {
1252         switch (msg->lm_magic) {
1253         case LUSTRE_MSG_MAGIC_V1:
1254         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1255                 return;
1256         case LUSTRE_MSG_MAGIC_V2:
1257         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1258                 struct ptlrpc_body *pb;
1259
1260                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1261                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1262                 pb->pb_version |= version;
1263                 return;
1264         }
1265         default:
1266                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1267         }
1268 }
1269
1270 __u32 lustre_msg_get_opc(struct lustre_msg *msg)
1271 {
1272         switch (msg->lm_magic) {
1273         case LUSTRE_MSG_MAGIC_V1:
1274         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1275                 return ((struct lustre_msg_v1 *)msg)->lm_opc;
1276         case LUSTRE_MSG_MAGIC_V2:
1277         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1278                 struct ptlrpc_body *pb;
1279
1280                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1281                 if (!pb) {
1282                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1283                         return 0;
1284                 }
1285                 return pb->pb_opc;
1286         }
1287         default:
1288                 CERROR( "incorrect message magic: %08x\n", msg->lm_magic);
1289                 return 0;
1290         }
1291 }
1292
1293 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg)
1294 {
1295         switch (msg->lm_magic) {
1296         case LUSTRE_MSG_MAGIC_V1:
1297         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1298                 return ((struct lustre_msg_v1 *)msg)->lm_last_xid;
1299         case LUSTRE_MSG_MAGIC_V2:
1300         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1301                 struct ptlrpc_body *pb;
1302
1303                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1304                 if (!pb) {
1305                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1306                         return 0;
1307                 }
1308                 return pb->pb_last_xid;
1309         }
1310         default:
1311                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1312                 return 0;
1313         }
1314 }
1315
1316 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg)
1317 {
1318         switch (msg->lm_magic) {
1319         case LUSTRE_MSG_MAGIC_V1:
1320         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1321                 return ((struct lustre_msg_v1 *)msg)->lm_last_committed;
1322         case LUSTRE_MSG_MAGIC_V2:
1323         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1324                 struct ptlrpc_body *pb;
1325
1326                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1327                 if (!pb) {
1328                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1329                         return 0;
1330                 }
1331                 return pb->pb_last_committed;
1332         }
1333         default:
1334                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1335                 return 0;
1336         }
1337 }
1338
1339 __u64 lustre_msg_get_transno(struct lustre_msg *msg)
1340 {
1341         switch (msg->lm_magic) {
1342         case LUSTRE_MSG_MAGIC_V1:
1343         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1344                 return ((struct lustre_msg_v1 *)msg)->lm_transno;
1345         case LUSTRE_MSG_MAGIC_V2:
1346         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1347                 struct ptlrpc_body *pb;
1348
1349                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1350                 if (!pb) {
1351                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1352                         return 0;
1353                 }
1354                 return pb->pb_transno;
1355         }
1356         default:
1357                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1358                 return 0;
1359         }
1360 }
1361
1362 int lustre_msg_get_status(struct lustre_msg *msg)
1363 {
1364         switch (msg->lm_magic) {
1365         case LUSTRE_MSG_MAGIC_V1:
1366         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1367                 return ((struct lustre_msg_v1 *)msg)->lm_status;
1368         case LUSTRE_MSG_MAGIC_V2:
1369         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1370                 struct ptlrpc_body *pb;
1371
1372                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1373                 if (!pb) {
1374                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1375                         return -EINVAL;
1376                 }
1377                 return pb->pb_status;
1378         }
1379         default:
1380                 /* status might be printed in debug code while message
1381                  * uninitialized */
1382                 return -EINVAL;
1383         }
1384 }
1385
1386 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg)
1387 {
1388         switch (msg->lm_magic) {
1389         case LUSTRE_MSG_MAGIC_V1:
1390         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1391                 return ((struct lustre_msg_v1 *)msg)->lm_conn_cnt;
1392         case LUSTRE_MSG_MAGIC_V2:
1393         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1394                 struct ptlrpc_body *pb;
1395
1396                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1397                 if (!pb) {
1398                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1399                         return 0;
1400                 }
1401                 return pb->pb_conn_cnt;
1402         }
1403         default:
1404                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1405                 return 0;
1406         }
1407 }
1408
1409 __u32 lustre_msg_get_magic(struct lustre_msg *msg)
1410 {
1411         switch (msg->lm_magic) {
1412         case LUSTRE_MSG_MAGIC_V1:
1413         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1414         case LUSTRE_MSG_MAGIC_V2:
1415         case LUSTRE_MSG_MAGIC_V2_SWABBED:
1416                 return msg->lm_magic;
1417         default:
1418                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1419                 return 0;
1420         }
1421 }
1422
1423 void lustre_msg_set_handle(struct lustre_msg *msg, struct lustre_handle *handle)
1424 {
1425         switch (msg->lm_magic) {
1426         case LUSTRE_MSG_MAGIC_V1:
1427                 ((struct lustre_msg_v1 *)msg)->lm_handle = *handle;
1428                 return;
1429         case LUSTRE_MSG_MAGIC_V2: {
1430                 struct ptlrpc_body *pb;
1431
1432                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1433                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1434                 pb->pb_handle = *handle;
1435                 return;
1436         }
1437         default:
1438                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1439         }
1440 }
1441
1442 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type)
1443 {
1444         switch (msg->lm_magic) {
1445         case LUSTRE_MSG_MAGIC_V1:
1446                 ((struct lustre_msg_v1 *)msg)->lm_type = type;
1447                 return;
1448         case LUSTRE_MSG_MAGIC_V2: {
1449                 struct ptlrpc_body *pb;
1450
1451                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1452                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1453                 pb->pb_type = type;
1454                 return;
1455         }
1456         default:
1457                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1458         }
1459 }
1460
1461 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc)
1462 {
1463         switch (msg->lm_magic) {
1464         case LUSTRE_MSG_MAGIC_V1:
1465                 ((struct lustre_msg_v1 *)msg)->lm_opc = opc;
1466                 return;
1467         case LUSTRE_MSG_MAGIC_V2: {
1468                 struct ptlrpc_body *pb;
1469
1470                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1471                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1472                 pb->pb_opc = opc;
1473                 return;
1474         }
1475         default:
1476                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1477         }
1478 }
1479
1480 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid)
1481 {
1482         switch (msg->lm_magic) {
1483         case LUSTRE_MSG_MAGIC_V1:
1484                 ((struct lustre_msg_v1 *)msg)->lm_last_xid = last_xid;
1485                 return;
1486         case LUSTRE_MSG_MAGIC_V2: {
1487                 struct ptlrpc_body *pb;
1488
1489                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1490                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1491                 pb->pb_last_xid = last_xid;
1492                 return;
1493         }
1494         default:
1495                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1496         }
1497 }
1498
1499 void lustre_msg_set_last_committed(struct lustre_msg *msg, __u64 last_committed)
1500 {
1501         switch (msg->lm_magic) {
1502         case LUSTRE_MSG_MAGIC_V1:
1503                 ((struct lustre_msg_v1 *)msg)->lm_last_committed=last_committed;
1504                 return;
1505         case LUSTRE_MSG_MAGIC_V2: {
1506                 struct ptlrpc_body *pb;
1507
1508                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1509                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1510                 pb->pb_last_committed = last_committed;
1511                 return;
1512         }
1513         default:
1514                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1515         }
1516 }
1517
1518 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno)
1519 {
1520         switch (msg->lm_magic) {
1521         case LUSTRE_MSG_MAGIC_V1:
1522                 ((struct lustre_msg_v1 *)msg)->lm_transno = transno;
1523                 return;
1524         case LUSTRE_MSG_MAGIC_V2: {
1525                 struct ptlrpc_body *pb;
1526
1527                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1528                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1529                 pb->pb_transno = transno;
1530                 return;
1531         }
1532         default:
1533                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1534         }
1535 }
1536
1537 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status)
1538 {
1539         switch (msg->lm_magic) {
1540         case LUSTRE_MSG_MAGIC_V1:
1541                 ((struct lustre_msg_v1 *)msg)->lm_status = status;
1542                 return;
1543         case LUSTRE_MSG_MAGIC_V2: {
1544                 struct ptlrpc_body *pb;
1545
1546                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1547                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1548                 pb->pb_status = status;
1549                 return;
1550         }
1551         default:
1552                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1553         }
1554 }
1555
1556 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt)
1557 {
1558         switch (msg->lm_magic) {
1559         case LUSTRE_MSG_MAGIC_V1:
1560                 ((struct lustre_msg_v1 *)msg)->lm_conn_cnt = conn_cnt;
1561                 return;
1562         case LUSTRE_MSG_MAGIC_V2: {
1563                 struct ptlrpc_body *pb;
1564
1565                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1566                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1567                 pb->pb_conn_cnt = conn_cnt;
1568                 return;
1569         }
1570         default:
1571                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1572         }
1573 }
1574
1575 /* byte flipping routines for all wire types declared in
1576  * lustre_idl.h implemented here.
1577  */
1578 void lustre_swab_ptlrpc_body(struct ptlrpc_body *b)
1579 {
1580         __swab32s (&b->pb_type);
1581         __swab32s (&b->pb_version);
1582         __swab32s (&b->pb_opc);
1583         __swab32s (&b->pb_status);
1584         __swab64s (&b->pb_last_xid);
1585         __swab64s (&b->pb_last_seen);
1586         __swab64s (&b->pb_last_committed);
1587         __swab64s (&b->pb_transno);
1588         __swab32s (&b->pb_flags);
1589         __swab32s (&b->pb_op_flags);
1590         __swab32s (&b->pb_conn_cnt);
1591         CLASSERT(offsetof(typeof(*b), pb_padding_1) != 0);
1592         CLASSERT(offsetof(typeof(*b), pb_padding_2) != 0);
1593         CLASSERT(offsetof(typeof(*b), pb_padding_3) != 0);
1594         CLASSERT(offsetof(typeof(*b), pb_padding_4) != 0);
1595         CLASSERT(offsetof(typeof(*b), pb_padding_5) != 0);
1596 }
1597
1598 void lustre_swab_connect(struct obd_connect_data *ocd)
1599 {
1600         __swab64s(&ocd->ocd_connect_flags);
1601         __swab32s(&ocd->ocd_version);
1602         __swab32s(&ocd->ocd_grant);
1603         __swab32s(&ocd->ocd_index);
1604         __swab32s(&ocd->ocd_brw_size);
1605         __swab64s(&ocd->ocd_ibits_known);
1606         __swab32s(&ocd->ocd_nllu);
1607         __swab32s(&ocd->ocd_nllg);
1608         CLASSERT(offsetof(typeof(*ocd), padding1) != 0);
1609         CLASSERT(offsetof(typeof(*ocd), padding2) != 0);
1610         CLASSERT(offsetof(typeof(*ocd), padding3) != 0);
1611         CLASSERT(offsetof(typeof(*ocd), padding4) != 0);
1612 }
1613
1614 void lustre_swab_obdo (struct obdo  *o)
1615 {
1616         __swab64s (&o->o_valid);
1617         __swab64s (&o->o_id);
1618         __swab64s (&o->o_gr);
1619         __swab64s (&o->o_fid);
1620         __swab64s (&o->o_size);
1621         __swab64s (&o->o_mtime);
1622         __swab64s (&o->o_atime);
1623         __swab64s (&o->o_ctime);
1624         __swab64s (&o->o_blocks);
1625         __swab64s (&o->o_grant);
1626         __swab32s (&o->o_blksize);
1627         __swab32s (&o->o_mode);
1628         __swab32s (&o->o_uid);
1629         __swab32s (&o->o_gid);
1630         __swab32s (&o->o_flags);
1631         __swab32s (&o->o_nlink);
1632         __swab32s (&o->o_generation);
1633         __swab32s (&o->o_misc);
1634         __swab32s (&o->o_easize);
1635         __swab32s (&o->o_mds);
1636         __swab32s (&o->o_stripe_idx);
1637         __swab32s (&o->o_padding_1);
1638         /* o_inline is opaque */
1639 }
1640
1641 void lustre_swab_obd_statfs (struct obd_statfs *os)
1642 {
1643         __swab64s (&os->os_type);
1644         __swab64s (&os->os_blocks);
1645         __swab64s (&os->os_bfree);
1646         __swab64s (&os->os_bavail);
1647         __swab64s (&os->os_files);
1648         __swab64s (&os->os_ffree);
1649         /* no need to swab os_fsid */
1650         __swab32s (&os->os_bsize);
1651         __swab32s (&os->os_namelen);
1652         __swab64s (&os->os_maxbytes);
1653         __swab32s (&os->os_state);
1654         /* no need to swap os_spare */
1655 }
1656
1657 void lustre_swab_obd_ioobj (struct obd_ioobj *ioo)
1658 {
1659         __swab64s (&ioo->ioo_id);
1660         __swab64s (&ioo->ioo_gr);
1661         __swab32s (&ioo->ioo_type);
1662         __swab32s (&ioo->ioo_bufcnt);
1663 }
1664
1665 void lustre_swab_niobuf_remote (struct niobuf_remote *nbr)
1666 {
1667         __swab64s (&nbr->offset);
1668         __swab32s (&nbr->len);
1669         __swab32s (&nbr->flags);
1670 }
1671
1672 void lustre_swab_ost_body (struct ost_body *b)
1673 {
1674         lustre_swab_obdo (&b->oa);
1675 }
1676
1677 void lustre_swab_ost_last_id(obd_id *id)
1678 {
1679         __swab64s(id);
1680 }
1681
1682 void lustre_swab_ost_lvb(struct ost_lvb *lvb)
1683 {
1684         __swab64s(&lvb->lvb_size);
1685         __swab64s(&lvb->lvb_mtime);
1686         __swab64s(&lvb->lvb_atime);
1687         __swab64s(&lvb->lvb_ctime);
1688         __swab64s(&lvb->lvb_blocks);
1689 }
1690
1691 void lustre_swab_mds_status_req (struct mds_status_req *r)
1692 {
1693         __swab32s (&r->flags);
1694         __swab32s (&r->repbuf);
1695 }
1696
1697 void lustre_swab_mds_body (struct mds_body *b)
1698 {
1699         lustre_swab_ll_fid (&b->fid1);
1700         lustre_swab_ll_fid (&b->fid2);
1701         /* handle is opaque */
1702         __swab64s (&b->valid);
1703         __swab64s (&b->size);
1704         __swab64s (&b->mtime);
1705         __swab64s (&b->atime);
1706         __swab64s (&b->ctime);
1707         __swab64s (&b->blocks);
1708         __swab64s (&b->io_epoch);
1709         __swab64s (&b->ino);
1710         __swab32s (&b->fsuid);
1711         __swab32s (&b->fsgid);
1712         __swab32s (&b->capability);
1713         __swab32s (&b->mode);
1714         __swab32s (&b->uid);
1715         __swab32s (&b->gid);
1716         __swab32s (&b->flags);
1717         __swab32s (&b->rdev);
1718         __swab32s (&b->nlink);
1719         __swab32s (&b->generation);
1720         __swab32s (&b->suppgid);
1721         __swab32s (&b->eadatasize);
1722         __swab32s (&b->aclsize);
1723         __swab32s (&b->max_mdsize);
1724         __swab32s (&b->max_cookiesize);
1725         __swab32s (&b->padding_4);
1726 }
1727
1728 void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
1729 {
1730         int i;
1731         __swab32s(&mti->mti_lustre_ver);
1732         __swab32s(&mti->mti_stripe_index);
1733         __swab32s(&mti->mti_config_ver);
1734         __swab32s(&mti->mti_flags);
1735         __swab32s(&mti->mti_nid_count);
1736         CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
1737         for (i = 0; i < MTI_NIDS_MAX; i++) 
1738                 __swab64s(&mti->mti_nids[i]);
1739 }
1740
1741 static void lustre_swab_obd_dqinfo (struct obd_dqinfo *i)
1742 {
1743         __swab64s (&i->dqi_bgrace);
1744         __swab64s (&i->dqi_igrace);
1745         __swab32s (&i->dqi_flags);
1746         __swab32s (&i->dqi_valid);
1747 }
1748
1749 static void lustre_swab_obd_dqblk (struct obd_dqblk *b)
1750 {
1751         __swab64s (&b->dqb_ihardlimit);
1752         __swab64s (&b->dqb_isoftlimit);
1753         __swab64s (&b->dqb_curinodes);
1754         __swab64s (&b->dqb_bhardlimit);
1755         __swab64s (&b->dqb_bsoftlimit);
1756         __swab64s (&b->dqb_curspace);
1757         __swab64s (&b->dqb_btime);
1758         __swab64s (&b->dqb_itime);
1759         __swab32s (&b->dqb_valid);
1760         CLASSERT(offsetof(typeof(*b), padding) != 0);
1761 }
1762
1763 void lustre_swab_obd_quotactl (struct obd_quotactl *q)
1764 {
1765         __swab32s (&q->qc_cmd);
1766         __swab32s (&q->qc_type);
1767         __swab32s (&q->qc_id);
1768         __swab32s (&q->qc_stat);
1769         lustre_swab_obd_dqinfo (&q->qc_dqinfo);
1770         lustre_swab_obd_dqblk (&q->qc_dqblk);
1771 }
1772
1773 void lustre_swab_mds_rec_setattr (struct mds_rec_setattr *sa)
1774 {
1775         __swab32s (&sa->sa_opcode);
1776         __swab32s (&sa->sa_fsuid);
1777         __swab32s (&sa->sa_fsgid);
1778         __swab32s (&sa->sa_cap);
1779         __swab32s (&sa->sa_suppgid);
1780         __swab32s (&sa->sa_mode);
1781         lustre_swab_ll_fid (&sa->sa_fid);
1782         __swab64s (&sa->sa_valid);
1783         __swab64s (&sa->sa_size);
1784         __swab64s (&sa->sa_mtime);
1785         __swab64s (&sa->sa_atime);
1786         __swab64s (&sa->sa_ctime);
1787         __swab32s (&sa->sa_uid);
1788         __swab32s (&sa->sa_gid);
1789         __swab32s (&sa->sa_attr_flags);
1790         CLASSERT(offsetof(typeof(*sa), sa_padding) != 0);
1791 }
1792
1793 void lustre_swab_mds_rec_join (struct mds_rec_join *jr)
1794 {
1795         __swab64s(&jr->jr_headsize);
1796         lustre_swab_ll_fid(&jr->jr_fid);
1797 }
1798
1799 void lustre_swab_mds_rec_create (struct mds_rec_create *cr)
1800 {
1801         __swab32s (&cr->cr_opcode);
1802         __swab32s (&cr->cr_fsuid);
1803         __swab32s (&cr->cr_fsgid);
1804         __swab32s (&cr->cr_cap);
1805         __swab32s (&cr->cr_flags); /* for use with open */
1806         __swab32s (&cr->cr_mode);
1807         lustre_swab_ll_fid (&cr->cr_fid);
1808         lustre_swab_ll_fid (&cr->cr_replayfid);
1809         __swab64s (&cr->cr_time);
1810         __swab64s (&cr->cr_rdev);
1811         __swab32s (&cr->cr_suppgid);
1812         CLASSERT(offsetof(typeof(*cr), cr_padding_1) != 0);
1813         CLASSERT(offsetof(typeof(*cr), cr_padding_2) != 0);
1814         CLASSERT(offsetof(typeof(*cr), cr_padding_3) != 0);
1815         CLASSERT(offsetof(typeof(*cr), cr_padding_4) != 0);
1816         CLASSERT(offsetof(typeof(*cr), cr_padding_5) != 0);
1817 }
1818
1819 void lustre_swab_mds_rec_link (struct mds_rec_link *lk)
1820 {
1821         __swab32s (&lk->lk_opcode);
1822         __swab32s (&lk->lk_fsuid);
1823         __swab32s (&lk->lk_fsgid);
1824         __swab32s (&lk->lk_cap);
1825         __swab32s (&lk->lk_suppgid1);
1826         __swab32s (&lk->lk_suppgid2);
1827         lustre_swab_ll_fid (&lk->lk_fid1);
1828         lustre_swab_ll_fid (&lk->lk_fid2);
1829         __swab64s (&lk->lk_time);
1830         CLASSERT(offsetof(typeof(*lk), lk_padding_1) != 0);
1831         CLASSERT(offsetof(typeof(*lk), lk_padding_2) != 0);
1832         CLASSERT(offsetof(typeof(*lk), lk_padding_3) != 0);
1833         CLASSERT(offsetof(typeof(*lk), lk_padding_4) != 0);
1834 }
1835
1836 void lustre_swab_mds_rec_unlink (struct mds_rec_unlink *ul)
1837 {
1838         __swab32s (&ul->ul_opcode);
1839         __swab32s (&ul->ul_fsuid);
1840         __swab32s (&ul->ul_fsgid);
1841         __swab32s (&ul->ul_cap);
1842         __swab32s (&ul->ul_suppgid);
1843         __swab32s (&ul->ul_mode);
1844         lustre_swab_ll_fid (&ul->ul_fid1);
1845         lustre_swab_ll_fid (&ul->ul_fid2);
1846         __swab64s (&ul->ul_time);
1847         CLASSERT(offsetof(typeof(*ul), ul_padding_1) != 0);
1848         CLASSERT(offsetof(typeof(*ul), ul_padding_2) != 0);
1849         CLASSERT(offsetof(typeof(*ul), ul_padding_3) != 0);
1850         CLASSERT(offsetof(typeof(*ul), ul_padding_4) != 0);
1851 }
1852
1853 void lustre_swab_mds_rec_rename (struct mds_rec_rename *rn)
1854 {
1855         __swab32s (&rn->rn_opcode);
1856         __swab32s (&rn->rn_fsuid);
1857         __swab32s (&rn->rn_fsgid);
1858         __swab32s (&rn->rn_cap);
1859         __swab32s (&rn->rn_suppgid1);
1860         __swab32s (&rn->rn_suppgid2);
1861         lustre_swab_ll_fid (&rn->rn_fid1);
1862         lustre_swab_ll_fid (&rn->rn_fid2);
1863         __swab64s (&rn->rn_time);
1864         CLASSERT(offsetof(typeof(*rn), rn_padding_1) != 0);
1865         CLASSERT(offsetof(typeof(*rn), rn_padding_2) != 0);
1866         CLASSERT(offsetof(typeof(*rn), rn_padding_3) != 0);
1867         CLASSERT(offsetof(typeof(*rn), rn_padding_4) != 0);
1868 }
1869
1870 void lustre_swab_lov_desc (struct lov_desc *ld)
1871 {
1872         __swab32s (&ld->ld_tgt_count);
1873         __swab32s (&ld->ld_active_tgt_count);
1874         __swab32s (&ld->ld_default_stripe_count);
1875         __swab64s (&ld->ld_default_stripe_size);
1876         __swab64s (&ld->ld_default_stripe_offset);
1877         __swab32s (&ld->ld_pattern);
1878         __swab32s (&ld->ld_qos_maxage);
1879         /* uuid endian insensitive */
1880 }
1881
1882 static void print_lum (struct lov_user_md *lum)
1883 {
1884         CDEBUG(D_OTHER, "lov_user_md %p:\n", lum);
1885         CDEBUG(D_OTHER, "\tlmm_magic: %#x\n", lum->lmm_magic);
1886         CDEBUG(D_OTHER, "\tlmm_pattern: %#x\n", lum->lmm_pattern);
1887         CDEBUG(D_OTHER, "\tlmm_object_id: "LPU64"\n", lum->lmm_object_id);
1888         CDEBUG(D_OTHER, "\tlmm_object_gr: "LPU64"\n", lum->lmm_object_gr);
1889         CDEBUG(D_OTHER, "\tlmm_stripe_size: %#x\n", lum->lmm_stripe_size);
1890         CDEBUG(D_OTHER, "\tlmm_stripe_count: %#x\n", lum->lmm_stripe_count);
1891         CDEBUG(D_OTHER, "\tlmm_stripe_offset: %#x\n", lum->lmm_stripe_offset);
1892 }
1893
1894 void lustre_swab_lov_user_md(struct lov_user_md *lum)
1895 {
1896         ENTRY;
1897         CDEBUG(D_IOCTL, "swabbing lov_user_md\n");
1898         __swab32s(&lum->lmm_magic);
1899         __swab32s(&lum->lmm_pattern);
1900         __swab64s(&lum->lmm_object_id);
1901         __swab64s(&lum->lmm_object_gr);
1902         __swab32s(&lum->lmm_stripe_size);
1903         __swab16s(&lum->lmm_stripe_count);
1904         __swab16s(&lum->lmm_stripe_offset);
1905         print_lum(lum);
1906         EXIT;
1907 }
1908
1909 static void print_lumj (struct lov_user_md_join *lumj)
1910 {
1911         CDEBUG(D_OTHER, "lov_user_md %p:\n", lumj);
1912         CDEBUG(D_OTHER, "\tlmm_magic: %#x\n", lumj->lmm_magic);
1913         CDEBUG(D_OTHER, "\tlmm_pattern: %#x\n", lumj->lmm_pattern);
1914         CDEBUG(D_OTHER, "\tlmm_object_id: "LPU64"\n", lumj->lmm_object_id);
1915         CDEBUG(D_OTHER, "\tlmm_object_gr: "LPU64"\n", lumj->lmm_object_gr);
1916         CDEBUG(D_OTHER, "\tlmm_stripe_size: %#x\n", lumj->lmm_stripe_size);
1917         CDEBUG(D_OTHER, "\tlmm_stripe_count: %#x\n", lumj->lmm_stripe_count);
1918         CDEBUG(D_OTHER, "\tlmm_extent_count: %#x\n", lumj->lmm_extent_count);
1919 }
1920
1921 void lustre_swab_lov_user_md_join(struct lov_user_md_join *lumj)
1922 {
1923         ENTRY;
1924         CDEBUG(D_IOCTL, "swabbing lov_user_md_join\n");
1925         __swab32s(&lumj->lmm_magic);
1926         __swab32s(&lumj->lmm_pattern);
1927         __swab64s(&lumj->lmm_object_id);
1928         __swab64s(&lumj->lmm_object_gr);
1929         __swab32s(&lumj->lmm_stripe_size);
1930         __swab32s(&lumj->lmm_stripe_count);
1931         __swab32s(&lumj->lmm_extent_count);
1932         print_lumj(lumj);
1933         EXIT;
1934 }
1935
1936 static void print_lum_objs(struct lov_user_md *lum)
1937 {
1938         struct lov_user_ost_data *lod;
1939         int i;
1940         ENTRY;
1941         if (!(libcfs_debug & D_OTHER)) /* don't loop on nothing */
1942                 return;
1943         CDEBUG(D_OTHER, "lov_user_md_objects: %p\n", lum);
1944         for (i = 0; i < lum->lmm_stripe_count; i++) {
1945                 lod = &lum->lmm_objects[i];
1946                 CDEBUG(D_OTHER, "(%i) lod->l_object_id: "LPX64"\n", i, lod->l_object_id);
1947                 CDEBUG(D_OTHER, "(%i) lod->l_object_gr: "LPX64"\n", i, lod->l_object_gr);
1948                 CDEBUG(D_OTHER, "(%i) lod->l_ost_gen: %#x\n", i, lod->l_ost_gen);
1949                 CDEBUG(D_OTHER, "(%i) lod->l_ost_idx: %#x\n", i, lod->l_ost_idx);
1950         }
1951         EXIT;
1952 }
1953
1954 void lustre_swab_lov_user_md_objects(struct lov_user_md *lum)
1955 {
1956         struct lov_user_ost_data *lod;
1957         int i;
1958         ENTRY;
1959         for (i = 0; i < lum->lmm_stripe_count; i++) {
1960                 lod = &lum->lmm_objects[i];
1961                 __swab64s(&lod->l_object_id);
1962                 __swab64s(&lod->l_object_gr);
1963                 __swab32s(&lod->l_ost_gen);
1964                 __swab32s(&lod->l_ost_idx);
1965         }
1966         print_lum_objs(lum);
1967         EXIT;
1968 }
1969
1970 void lustre_swab_ldlm_res_id (struct ldlm_res_id *id)
1971 {
1972         int  i;
1973
1974         for (i = 0; i < RES_NAME_SIZE; i++)
1975                 __swab64s (&id->name[i]);
1976 }
1977
1978 void lustre_swab_ldlm_policy_data (ldlm_policy_data_t *d)
1979 {
1980         /* the lock data is a union and the first two fields are always an
1981          * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
1982          * data the same way. */
1983         __swab64s(&d->l_extent.start);
1984         __swab64s(&d->l_extent.end);
1985         __swab64s(&d->l_extent.gid);
1986         __swab32s(&d->l_flock.pid);
1987 }
1988
1989 void lustre_swab_ldlm_intent (struct ldlm_intent *i)
1990 {
1991         __swab64s (&i->opc);
1992 }
1993
1994 void lustre_swab_ldlm_resource_desc (struct ldlm_resource_desc *r)
1995 {
1996         __swab32s (&r->lr_type);
1997         CLASSERT(offsetof(typeof(*r), lr_padding) != 0);
1998         lustre_swab_ldlm_res_id (&r->lr_name);
1999 }
2000
2001 void lustre_swab_ldlm_lock_desc (struct ldlm_lock_desc *l)
2002 {
2003         lustre_swab_ldlm_resource_desc (&l->l_resource);
2004         __swab32s (&l->l_req_mode);
2005         __swab32s (&l->l_granted_mode);
2006         lustre_swab_ldlm_policy_data (&l->l_policy_data);
2007 }
2008
2009 void lustre_swab_ldlm_request (struct ldlm_request *rq)
2010 {
2011         __swab32s (&rq->lock_flags);
2012         CLASSERT(offsetof(typeof(*rq), lock_padding) != 0);
2013         lustre_swab_ldlm_lock_desc (&rq->lock_desc);
2014         /* lock_handle1 opaque */
2015         /* lock_handle2 opaque */
2016 }
2017
2018 void lustre_swab_ldlm_reply (struct ldlm_reply *r)
2019 {
2020         __swab32s (&r->lock_flags);
2021         CLASSERT(offsetof(typeof(*r), lock_padding) != 0);
2022         lustre_swab_ldlm_lock_desc (&r->lock_desc);
2023         /* lock_handle opaque */
2024         __swab64s (&r->lock_policy_res1);
2025         __swab64s (&r->lock_policy_res2);
2026 }
2027
2028 /* no one calls this */
2029 int llog_log_swabbed(struct llog_log_hdr *hdr)
2030 {
2031         if (hdr->llh_hdr.lrh_type == __swab32(LLOG_HDR_MAGIC))
2032                 return 1;
2033         if (hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC)
2034                 return 0;
2035         return -1;
2036 }
2037
2038 void lustre_swab_qdata(struct qunit_data *d)
2039 {
2040         __swab32s (&d->qd_id);
2041         __swab32s (&d->qd_flags);
2042         __swab64s (&d->qd_count);
2043 }
2044
2045 void lustre_swab_qdata_old(struct qunit_data_old *d)
2046 {
2047         __swab32s (&d->qd_id);
2048         __swab32s (&d->qd_type);
2049         __swab32s (&d->qd_count);
2050         __swab32s (&d->qd_isblk);
2051 }
2052
2053 #ifdef __KERNEL__
2054 struct qunit_data *lustre_quota_old_to_new(struct qunit_data_old *d)
2055 {
2056         struct qunit_data_old tmp;
2057         struct qunit_data *ret;
2058         ENTRY;
2059
2060         if (!d)
2061                 return NULL;
2062
2063         tmp = *d;
2064         ret = (struct qunit_data *)d;
2065         ret->qd_id = tmp.qd_id;
2066         ret->qd_flags = (tmp.qd_type ? QUOTA_IS_GRP : 0) | (tmp.qd_isblk ? QUOTA_IS_BLOCK : 0);
2067         ret->qd_count = tmp.qd_count;
2068         RETURN(ret);
2069
2070 }
2071 EXPORT_SYMBOL(lustre_quota_old_to_new);
2072
2073 struct qunit_data_old *lustre_quota_new_to_old(struct qunit_data *d)
2074 {
2075         struct qunit_data tmp;
2076         struct qunit_data_old *ret;
2077         ENTRY;
2078
2079         if (!d)
2080                 return NULL;
2081
2082         LASSERT(d->qd_count <= MAX_QUOTA_COUNT32);
2083         tmp = *d;
2084         ret = (struct qunit_data_old *)d;
2085         ret->qd_id = tmp.qd_id;
2086         ret->qd_type = ((tmp.qd_flags & QUOTA_IS_GRP) ? GRPQUOTA : USRQUOTA);
2087         ret->qd_count = (__u32)tmp.qd_count;
2088         ret->qd_isblk = ((tmp.qd_flags & QUOTA_IS_BLOCK) ? 1 : 0);
2089         RETURN(ret);
2090 }
2091 EXPORT_SYMBOL(lustre_quota_new_to_old);
2092 #endif /* __KERNEL__ */
2093
2094 static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2095 {
2096         LASSERT(req->rq_reqmsg);
2097
2098         switch (req->rq_reqmsg->lm_magic) {
2099         case LUSTRE_MSG_MAGIC_V1:
2100         case LUSTRE_MSG_MAGIC_V1_SWABBED:
2101                 return 1;
2102         case LUSTRE_MSG_MAGIC_V2:
2103         case LUSTRE_MSG_MAGIC_V2_SWABBED:
2104                 return lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
2105         default:
2106                 CERROR("bad lustre msg magic: %#08X\n",
2107                        req->rq_reqmsg->lm_magic);
2108         }
2109         return 0;
2110 }
2111
2112 static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2113 {
2114         LASSERT(req->rq_repmsg);
2115
2116         switch (req->rq_repmsg->lm_magic) {
2117         case LUSTRE_MSG_MAGIC_V1:
2118         case LUSTRE_MSG_MAGIC_V1_SWABBED:
2119                 return 1;
2120         case LUSTRE_MSG_MAGIC_V2:
2121         case LUSTRE_MSG_MAGIC_V2_SWABBED:
2122                 return lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
2123         default:
2124                 /* uninitialized yet */
2125                 return 0;
2126         }
2127 }
2128
2129 void _debug_req(struct ptlrpc_request *req, __u32 mask,
2130                 struct libcfs_debug_msg_data *data, const char *fmt, ... )
2131
2132 {
2133         va_list args;
2134
2135         va_start(args, fmt);
2136         libcfs_debug_vmsg2(data->msg_cdls, data->msg_subsys, mask, data->msg_file,
2137                            data->msg_fn, data->msg_line, fmt, args,
2138                            " req@%p x"LPD64"/t"LPD64" o%d->%s@%s:%d lens %d/%d ref %d fl "
2139                            REQ_FLAGS_FMT"/%x/%x rc %d/%d\n",
2140                            req, req->rq_xid, req->rq_transno,
2141                            req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : -1,
2142                            req->rq_import ? obd2cli_tgt(req->rq_import->imp_obd) :
2143                            req->rq_export ?
2144                                 (char*)req->rq_export->exp_client_uuid.uuid : "<?>",
2145                            req->rq_import ?
2146                                 (char *)req->rq_import->imp_connection->c_remote_uuid.uuid :
2147                            req->rq_export ?
2148                                 (char *)req->rq_export->exp_connection->c_remote_uuid.uuid : "<?>",
2149                            (req->rq_import && req->rq_import->imp_client) ?
2150                                 req->rq_import->imp_client->cli_request_portal : -1,
2151                            req->rq_reqlen, req->rq_replen, atomic_read(&req->rq_refcount),
2152                            DEBUG_REQ_FLAGS(req),
2153                            req->rq_reqmsg ? lustre_msg_get_flags(req->rq_reqmsg) : 0,
2154                            req->rq_repmsg ? lustre_msg_get_flags(req->rq_repmsg) : 0,
2155                            req->rq_status,
2156                            req->rq_repmsg ? lustre_msg_get_status(req->rq_repmsg) : 0);
2157         va_end(args);
2158 }
2159
2160 EXPORT_SYMBOL(_debug_req);