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