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