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