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