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