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