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