Whamcloud - gitweb
6ec0e5b12c37f9486faee5d763312b9049bd401c
[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 static inline struct ptlrpc_body *lustre_msg_ptlrpc_body(struct lustre_msg *msg)
777 {
778         return lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
779                                  sizeof(struct ptlrpc_body));
780 }
781
782 __u32 lustre_msghdr_get_flags(struct lustre_msg *msg)
783 {
784         switch (msg->lm_magic) {
785         case LUSTRE_MSG_MAGIC_V1:
786         case LUSTRE_MSG_MAGIC_V1_SWABBED:
787                 return 0;
788         case LUSTRE_MSG_MAGIC_V2:
789                 /* already in host endian */
790                 return msg->lm_flags;
791         default:
792                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
793                 return 0;
794         }
795 }
796 EXPORT_SYMBOL(lustre_msghdr_get_flags);
797
798 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags)
799 {
800         switch (msg->lm_magic) {
801         case LUSTRE_MSG_MAGIC_V1:
802                 return;
803         case LUSTRE_MSG_MAGIC_V2:
804                 msg->lm_flags = flags;
805                 return;
806         default:
807                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
808         }
809 }
810
811 __u32 lustre_msg_get_flags(struct lustre_msg *msg)
812 {
813         switch (msg->lm_magic) {
814         case LUSTRE_MSG_MAGIC_V2: {
815                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
816                 if (!pb) {
817                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
818                         return 0;
819                 }
820                 return pb->pb_flags;
821         }
822         default:
823                 /* flags might be printed in debug code while message
824                  * uninitialized */
825                 return 0;
826         }
827 }
828
829 void lustre_msg_add_flags(struct lustre_msg *msg, int flags)
830 {
831         switch (msg->lm_magic) {
832         case LUSTRE_MSG_MAGIC_V2: {
833                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
834                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
835                 pb->pb_flags |= flags;
836                 return;
837         }
838         default:
839                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
840         }
841 }
842
843 void lustre_msg_set_flags(struct lustre_msg *msg, int flags)
844 {
845         switch (msg->lm_magic) {
846         case LUSTRE_MSG_MAGIC_V2: {
847                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
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                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
862                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
863                 pb->pb_flags &= ~(MSG_GEN_FLAG_MASK & flags);
864                 return;
865         }
866         default:
867                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
868         }
869 }
870
871 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg)
872 {
873         switch (msg->lm_magic) {
874         case LUSTRE_MSG_MAGIC_V2: {
875                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
876                 if (!pb) {
877                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
878                         return 0;
879                 }
880                 return pb->pb_op_flags;
881         }
882         default:
883                 return 0;
884         }
885 }
886
887 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags)
888 {
889         switch (msg->lm_magic) {
890         case LUSTRE_MSG_MAGIC_V2: {
891                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
892                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
893                 pb->pb_op_flags |= flags;
894                 return;
895         }
896         default:
897                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
898         }
899 }
900
901 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags)
902 {
903         switch (msg->lm_magic) {
904         case LUSTRE_MSG_MAGIC_V2: {
905                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
906                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
907                 pb->pb_op_flags |= flags;
908                 return;
909         }
910         default:
911                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
912         }
913 }
914
915 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg)
916 {
917         switch (msg->lm_magic) {
918         case LUSTRE_MSG_MAGIC_V2: {
919                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
920                 if (!pb) {
921                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
922                         return NULL;
923                 }
924                 return &pb->pb_handle;
925         }
926         default:
927                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
928                 return NULL;
929         }
930 }
931
932 __u32 lustre_msg_get_type(struct lustre_msg *msg)
933 {
934         switch (msg->lm_magic) {
935         case LUSTRE_MSG_MAGIC_V2: {
936                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
937                 if (!pb) {
938                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
939                         return PTL_RPC_MSG_ERR;
940                 }
941                 return pb->pb_type;
942         }
943         default:
944                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
945                 return PTL_RPC_MSG_ERR;
946         }
947 }
948
949 __u32 lustre_msg_get_version(struct lustre_msg *msg)
950 {
951         switch (msg->lm_magic) {
952         case LUSTRE_MSG_MAGIC_V2: {
953                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
954                 if (!pb) {
955                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
956                         return 0;
957                 }
958                 return pb->pb_version;
959         }
960         default:
961                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
962                 return 0;
963         }
964 }
965
966 void lustre_msg_add_version(struct lustre_msg *msg, int version)
967 {
968         switch (msg->lm_magic) {
969         case LUSTRE_MSG_MAGIC_V2: {
970                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
971                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
972                 pb->pb_version |= version;
973                 return;
974         }
975         default:
976                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
977         }
978 }
979
980 __u32 lustre_msg_get_opc(struct lustre_msg *msg)
981 {
982         switch (msg->lm_magic) {
983         case LUSTRE_MSG_MAGIC_V2: {
984                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
985                 if (!pb) {
986                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
987                         return 0;
988                 }
989                 return pb->pb_opc;
990         }
991         default:
992                 CERROR("incorrect message magic: %08x(msg:%p)\n", msg->lm_magic, msg);
993                 LBUG();
994                 return 0;
995         }
996 }
997
998 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg)
999 {
1000         switch (msg->lm_magic) {
1001         case LUSTRE_MSG_MAGIC_V2: {
1002                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1003                 if (!pb) {
1004                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1005                         return 0;
1006                 }
1007                 return pb->pb_last_xid;
1008         }
1009         default:
1010                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1011                 return 0;
1012         }
1013 }
1014
1015 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg)
1016 {
1017         switch (msg->lm_magic) {
1018         case LUSTRE_MSG_MAGIC_V2: {
1019                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1020                 if (!pb) {
1021                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1022                         return 0;
1023                 }
1024                 return pb->pb_last_committed;
1025         }
1026         default:
1027                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1028                 return 0;
1029         }
1030 }
1031
1032 __u64 *lustre_msg_get_versions(struct lustre_msg *msg)
1033 {
1034         switch (msg->lm_magic) {
1035         case LUSTRE_MSG_MAGIC_V1:
1036                 return NULL;
1037         case LUSTRE_MSG_MAGIC_V2: {
1038                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1039                 if (!pb) {
1040                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1041                         return NULL;
1042                 }
1043                 return pb->pb_pre_versions;
1044         }
1045         default:
1046                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1047                 return NULL;
1048         }
1049 }
1050
1051 __u64 lustre_msg_get_transno(struct lustre_msg *msg)
1052 {
1053         switch (msg->lm_magic) {
1054         case LUSTRE_MSG_MAGIC_V2: {
1055                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1056                 if (!pb) {
1057                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1058                         return 0;
1059                 }
1060                 return pb->pb_transno;
1061         }
1062         default:
1063                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1064                 return 0;
1065         }
1066 }
1067
1068 int lustre_msg_get_status(struct lustre_msg *msg)
1069 {
1070         switch (msg->lm_magic) {
1071         case LUSTRE_MSG_MAGIC_V2: {
1072                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1073                 if (!pb) {
1074                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1075                         return -EINVAL;
1076                 }
1077                 return pb->pb_status;
1078         }
1079         default:
1080                 /* status might be printed in debug code while message
1081                  * uninitialized */
1082                 return -EINVAL;
1083         }
1084 }
1085
1086 __u64 lustre_msg_get_slv(struct lustre_msg *msg)
1087 {
1088         switch (msg->lm_magic) {
1089         case LUSTRE_MSG_MAGIC_V2: {
1090                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1091                 if (!pb) {
1092                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1093                         return -EINVAL;
1094                 }
1095                 return pb->pb_slv;
1096         }
1097         default:
1098                 CERROR("invalid msg magic %08x\n", msg->lm_magic);
1099                 return -EINVAL;
1100         }
1101 }
1102
1103
1104 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv)
1105 {
1106         switch (msg->lm_magic) {
1107         case LUSTRE_MSG_MAGIC_V2: {
1108                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1109                 if (!pb) {
1110                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1111                         return;
1112                 }
1113                 pb->pb_slv = slv;
1114                 return;
1115         }
1116         default:
1117                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1118                 return;
1119         }
1120 }
1121
1122 __u32 lustre_msg_get_limit(struct lustre_msg *msg)
1123 {
1124         switch (msg->lm_magic) {
1125         case LUSTRE_MSG_MAGIC_V2: {
1126                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1127                 if (!pb) {
1128                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1129                         return -EINVAL;
1130                 }
1131                 return pb->pb_limit;
1132         }
1133         default:
1134                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1135                 return -EINVAL;
1136         }
1137 }
1138
1139
1140 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit)
1141 {
1142         switch (msg->lm_magic) {
1143         case LUSTRE_MSG_MAGIC_V2: {
1144                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1145                 if (!pb) {
1146                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1147                         return;
1148                 }
1149                 pb->pb_limit = limit;
1150                 return;
1151         }
1152         default:
1153                 CERROR("invalid msg magic %08x\n", msg->lm_magic);
1154                 return;
1155         }
1156 }
1157
1158 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg)
1159 {
1160         switch (msg->lm_magic) {
1161         case LUSTRE_MSG_MAGIC_V2: {
1162                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1163                 if (!pb) {
1164                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1165                         return 0;
1166                 }
1167                 return pb->pb_conn_cnt;
1168         }
1169         default:
1170                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1171                 return 0;
1172         }
1173 }
1174
1175 int lustre_msg_is_v1(struct lustre_msg *msg)
1176 {
1177         switch (msg->lm_magic) {
1178         case LUSTRE_MSG_MAGIC_V1:
1179         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1180                 return 1;
1181         default:
1182                 return 0;
1183         }
1184 }
1185
1186 __u32 lustre_msg_get_magic(struct lustre_msg *msg)
1187 {
1188         switch (msg->lm_magic) {
1189         case LUSTRE_MSG_MAGIC_V2:
1190                 return msg->lm_magic;
1191         default:
1192                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1193                 return 0;
1194         }
1195 }
1196
1197 __u32 lustre_msg_get_timeout(struct lustre_msg *msg)
1198 {
1199         switch (msg->lm_magic) {
1200         case LUSTRE_MSG_MAGIC_V1:
1201         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1202                 return 0;
1203         case LUSTRE_MSG_MAGIC_V2: {
1204                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1205                 if (!pb) {
1206                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1207                         return 0;
1208
1209                 }
1210                 return pb->pb_timeout;
1211         }
1212         default:
1213                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1214                 return 0;
1215         }
1216 }
1217
1218 __u32 lustre_msg_get_service_time(struct lustre_msg *msg)
1219 {
1220         switch (msg->lm_magic) {
1221         case LUSTRE_MSG_MAGIC_V1:
1222         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1223                 return 0;
1224         case LUSTRE_MSG_MAGIC_V2: {
1225                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1226                 if (!pb) {
1227                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1228                         return 0;
1229
1230                 }
1231                 return pb->pb_service_time;
1232         }
1233         default:
1234                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1235                 return 0;
1236         }
1237 }
1238
1239 __u32 lustre_msg_get_cksum(struct lustre_msg *msg)
1240 {
1241         switch (msg->lm_magic) {
1242         case LUSTRE_MSG_MAGIC_V2:
1243                 return msg->lm_cksum;
1244         default:
1245                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1246                 return 0;
1247         }
1248 }
1249
1250 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg)
1251 {
1252         switch (msg->lm_magic) {
1253         case LUSTRE_MSG_MAGIC_V2: {
1254                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1255                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1256                 return crc32_le(~(__u32)0, (unsigned char *)pb, sizeof(*pb));
1257         }
1258         default:
1259                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1260                 return 0;
1261         }
1262 }
1263
1264 void lustre_msg_set_handle(struct lustre_msg *msg, struct lustre_handle *handle)
1265 {
1266         switch (msg->lm_magic) {
1267         case LUSTRE_MSG_MAGIC_V2: {
1268                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1269                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1270                 pb->pb_handle = *handle;
1271                 return;
1272         }
1273         default:
1274                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1275         }
1276 }
1277
1278 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type)
1279 {
1280         switch (msg->lm_magic) {
1281         case LUSTRE_MSG_MAGIC_V2: {
1282                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1283                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1284                 pb->pb_type = type;
1285                 return;
1286         }
1287         default:
1288                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1289         }
1290 }
1291
1292 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc)
1293 {
1294         switch (msg->lm_magic) {
1295         case LUSTRE_MSG_MAGIC_V2: {
1296                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1297                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1298                 pb->pb_opc = opc;
1299                 return;
1300         }
1301         default:
1302                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1303         }
1304 }
1305
1306 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid)
1307 {
1308         switch (msg->lm_magic) {
1309         case LUSTRE_MSG_MAGIC_V2: {
1310                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1311                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1312                 pb->pb_last_xid = last_xid;
1313                 return;
1314         }
1315         default:
1316                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1317         }
1318 }
1319
1320 void lustre_msg_set_last_committed(struct lustre_msg *msg, __u64 last_committed)
1321 {
1322         switch (msg->lm_magic) {
1323         case LUSTRE_MSG_MAGIC_V2: {
1324                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1325                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1326                 pb->pb_last_committed = last_committed;
1327                 return;
1328         }
1329         default:
1330                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1331         }
1332 }
1333
1334 void lustre_msg_set_versions(struct lustre_msg *msg, __u64 *versions)
1335 {
1336         switch (msg->lm_magic) {
1337         case LUSTRE_MSG_MAGIC_V1:
1338                 return;
1339         case LUSTRE_MSG_MAGIC_V2: {
1340                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1341                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1342                 pb->pb_pre_versions[0] = versions[0];
1343                 pb->pb_pre_versions[1] = versions[1];
1344                 pb->pb_pre_versions[2] = versions[2];
1345                 pb->pb_pre_versions[3] = versions[3];
1346                 return;
1347         }
1348         default:
1349                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1350         }
1351 }
1352
1353 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno)
1354 {
1355         switch (msg->lm_magic) {
1356         case LUSTRE_MSG_MAGIC_V2: {
1357                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1358                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1359                 pb->pb_transno = transno;
1360                 return;
1361         }
1362         default:
1363                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1364         }
1365 }
1366
1367 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status)
1368 {
1369         switch (msg->lm_magic) {
1370         case LUSTRE_MSG_MAGIC_V2: {
1371                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1372                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1373                 pb->pb_status = status;
1374                 return;
1375         }
1376         default:
1377                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1378         }
1379 }
1380
1381 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt)
1382 {
1383         switch (msg->lm_magic) {
1384         case LUSTRE_MSG_MAGIC_V2: {
1385                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1386                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1387                 pb->pb_conn_cnt = conn_cnt;
1388                 return;
1389         }
1390         default:
1391                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1392         }
1393 }
1394
1395 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout)
1396 {
1397         switch (msg->lm_magic) {
1398         case LUSTRE_MSG_MAGIC_V1:
1399                 return;
1400         case LUSTRE_MSG_MAGIC_V2: {
1401                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1402                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1403                 pb->pb_timeout = timeout;
1404                 return;
1405         }
1406         default:
1407                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1408         }
1409 }
1410
1411 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time)
1412 {
1413         switch (msg->lm_magic) {
1414         case LUSTRE_MSG_MAGIC_V1:
1415                 return;
1416         case LUSTRE_MSG_MAGIC_V2: {
1417                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1418                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1419                 pb->pb_service_time = service_time;
1420                 return;
1421         }
1422         default:
1423                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1424         }
1425 }
1426
1427 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum)
1428 {
1429         switch (msg->lm_magic) {
1430         case LUSTRE_MSG_MAGIC_V1:
1431                 return;
1432         case LUSTRE_MSG_MAGIC_V2:
1433                 msg->lm_cksum = cksum;
1434                 return;
1435         default:
1436                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1437         }
1438 }
1439
1440
1441 void ptlrpc_request_set_replen(struct ptlrpc_request *req)
1442 {
1443         int count = req_capsule_filled_sizes(&req->rq_pill, RCL_SERVER);
1444
1445         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count,
1446                                          req->rq_pill.rc_area[RCL_SERVER]);
1447         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1448                 req->rq_reqmsg->lm_repsize = req->rq_replen;
1449 }
1450
1451 void ptlrpc_req_set_repsize(struct ptlrpc_request *req, int count, __u32 *lens)
1452 {
1453         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens);
1454         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1455                 req->rq_reqmsg->lm_repsize = req->rq_replen;
1456 }
1457
1458 /* byte flipping routines for all wire types declared in
1459  * lustre_idl.h implemented here.
1460  */
1461 void lustre_swab_ptlrpc_body(struct ptlrpc_body *b)
1462 {
1463         __swab32s (&b->pb_type);
1464         __swab32s (&b->pb_version);
1465         __swab32s (&b->pb_opc);
1466         __swab32s (&b->pb_status);
1467         __swab64s (&b->pb_last_xid);
1468         __swab64s (&b->pb_last_seen);
1469         __swab64s (&b->pb_last_committed);
1470         __swab64s (&b->pb_transno);
1471         __swab32s (&b->pb_flags);
1472         __swab32s (&b->pb_op_flags);
1473         __swab32s (&b->pb_conn_cnt);
1474         __swab32s (&b->pb_timeout);
1475         __swab32s (&b->pb_service_time);
1476         __swab32s (&b->pb_limit);
1477         __swab64s (&b->pb_slv);
1478         __swab64s (&b->pb_pre_versions[0]);
1479         __swab64s (&b->pb_pre_versions[1]);
1480         __swab64s (&b->pb_pre_versions[2]);
1481         __swab64s (&b->pb_pre_versions[3]);
1482         CLASSERT(offsetof(typeof(*b), pb_padding) != 0);
1483 }
1484
1485 void lustre_swab_connect(struct obd_connect_data *ocd)
1486 {
1487         __swab64s(&ocd->ocd_connect_flags);
1488         __swab32s(&ocd->ocd_version);
1489         __swab32s(&ocd->ocd_grant);
1490         __swab64s(&ocd->ocd_ibits_known);
1491         __swab32s(&ocd->ocd_index);
1492         __swab32s(&ocd->ocd_brw_size);
1493         __swab32s(&ocd->ocd_nllu);
1494         __swab32s(&ocd->ocd_nllg);
1495         __swab64s(&ocd->ocd_transno);
1496         __swab32s(&ocd->ocd_group);
1497         __swab32s(&ocd->ocd_cksum_types);
1498         CLASSERT(offsetof(typeof(*ocd), padding1) != 0);
1499         CLASSERT(offsetof(typeof(*ocd), padding2) != 0);
1500 }
1501
1502 void lustre_swab_obdo (struct obdo  *o)
1503 {
1504         __swab64s (&o->o_valid);
1505         __swab64s (&o->o_id);
1506         __swab64s (&o->o_gr);
1507         __swab64s (&o->o_fid);
1508         __swab64s (&o->o_size);
1509         __swab64s (&o->o_mtime);
1510         __swab64s (&o->o_atime);
1511         __swab64s (&o->o_ctime);
1512         __swab64s (&o->o_blocks);
1513         __swab64s (&o->o_grant);
1514         __swab32s (&o->o_blksize);
1515         __swab32s (&o->o_mode);
1516         __swab32s (&o->o_uid);
1517         __swab32s (&o->o_gid);
1518         __swab32s (&o->o_flags);
1519         __swab32s (&o->o_nlink);
1520         __swab32s (&o->o_generation);
1521         __swab32s (&o->o_misc);
1522         __swab32s (&o->o_easize);
1523         __swab32s (&o->o_mds);
1524         __swab32s (&o->o_stripe_idx);
1525         __swab32s (&o->o_padding_1);
1526         /* o_inline is opaque */
1527 }
1528
1529 void lustre_swab_obd_statfs (struct obd_statfs *os)
1530 {
1531         __swab64s (&os->os_type);
1532         __swab64s (&os->os_blocks);
1533         __swab64s (&os->os_bfree);
1534         __swab64s (&os->os_bavail);
1535         __swab64s (&os->os_files);
1536         __swab64s (&os->os_ffree);
1537         /* no need to swab os_fsid */
1538         __swab32s (&os->os_bsize);
1539         __swab32s (&os->os_namelen);
1540         __swab64s (&os->os_maxbytes);
1541         __swab32s (&os->os_state);
1542         /* no need to swap os_spare */
1543 }
1544
1545 void lustre_swab_obd_ioobj (struct obd_ioobj *ioo)
1546 {
1547         __swab64s (&ioo->ioo_id);
1548         __swab64s (&ioo->ioo_gr);
1549         __swab32s (&ioo->ioo_type);
1550         __swab32s (&ioo->ioo_bufcnt);
1551 }
1552
1553 void lustre_swab_niobuf_remote (struct niobuf_remote *nbr)
1554 {
1555         __swab64s (&nbr->offset);
1556         __swab32s (&nbr->len);
1557         __swab32s (&nbr->flags);
1558 }
1559
1560 void lustre_swab_ost_body (struct ost_body *b)
1561 {
1562         lustre_swab_obdo (&b->oa);
1563 }
1564
1565 void lustre_swab_ost_last_id(obd_id *id)
1566 {
1567         __swab64s(id);
1568 }
1569
1570 void lustre_swab_generic_32s(__u32 *val)
1571 {
1572         __swab32s(val);
1573 }
1574
1575 void lustre_swab_ost_lvb(struct ost_lvb *lvb)
1576 {
1577         __swab64s(&lvb->lvb_size);
1578         __swab64s(&lvb->lvb_mtime);
1579         __swab64s(&lvb->lvb_atime);
1580         __swab64s(&lvb->lvb_ctime);
1581         __swab64s(&lvb->lvb_blocks);
1582 }
1583
1584 void lustre_swab_mds_status_req (struct mds_status_req *r)
1585 {
1586         __swab32s (&r->flags);
1587         __swab32s (&r->repbuf);
1588 }
1589
1590 void lustre_swab_mds_body (struct mds_body *b)
1591 {
1592         lustre_swab_ll_fid (&b->fid1);
1593         lustre_swab_ll_fid (&b->fid2);
1594         /* handle is opaque */
1595         __swab64s (&b->valid);
1596         __swab64s (&b->size);
1597         __swab64s (&b->mtime);
1598         __swab64s (&b->atime);
1599         __swab64s (&b->ctime);
1600         __swab64s (&b->blocks);
1601         __swab64s (&b->io_epoch);
1602         __swab64s (&b->ino);
1603         __swab32s (&b->fsuid);
1604         __swab32s (&b->fsgid);
1605         __swab32s (&b->capability);
1606         __swab32s (&b->mode);
1607         __swab32s (&b->uid);
1608         __swab32s (&b->gid);
1609         __swab32s (&b->flags);
1610         __swab32s (&b->rdev);
1611         __swab32s (&b->nlink);
1612         __swab32s (&b->generation);
1613         __swab32s (&b->suppgid);
1614         __swab32s (&b->eadatasize);
1615         __swab32s (&b->aclsize);
1616         __swab32s (&b->max_mdsize);
1617         __swab32s (&b->max_cookiesize);
1618         __swab32s (&b->padding_4);
1619 }
1620
1621 void lustre_swab_mdt_body (struct mdt_body *b)
1622 {
1623         lustre_swab_lu_fid (&b->fid1);
1624         lustre_swab_lu_fid (&b->fid2);
1625         /* handle is opaque */
1626         __swab64s (&b->valid);
1627         __swab64s (&b->size);
1628         __swab64s (&b->mtime);
1629         __swab64s (&b->atime);
1630         __swab64s (&b->ctime);
1631         __swab64s (&b->blocks);
1632         __swab64s (&b->ioepoch);
1633         __swab64s (&b->ino);
1634         __swab32s (&b->fsuid);
1635         __swab32s (&b->fsgid);
1636         __swab32s (&b->capability);
1637         __swab32s (&b->mode);
1638         __swab32s (&b->uid);
1639         __swab32s (&b->gid);
1640         __swab32s (&b->flags);
1641         __swab32s (&b->rdev);
1642         __swab32s (&b->nlink);
1643         __swab32s (&b->generation);
1644         __swab32s (&b->suppgid);
1645         __swab32s (&b->eadatasize);
1646         __swab32s (&b->aclsize);
1647         __swab32s (&b->max_mdsize);
1648         __swab32s (&b->max_cookiesize);
1649         __swab32s (&b->padding_4);
1650 }
1651
1652 void lustre_swab_mdt_epoch (struct mdt_epoch *b)
1653 {
1654         /* handle is opaque */
1655          __swab64s (&b->ioepoch);
1656          __swab32s (&b->flags);
1657          CLASSERT(offsetof(typeof(*b), padding) != 0);
1658 }
1659
1660 void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
1661 {
1662         int i;
1663         __swab32s(&mti->mti_lustre_ver);
1664         __swab32s(&mti->mti_stripe_index);
1665         __swab32s(&mti->mti_config_ver);
1666         __swab32s(&mti->mti_flags);
1667         __swab32s(&mti->mti_nid_count);
1668         CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
1669         for (i = 0; i < MTI_NIDS_MAX; i++)
1670                 __swab64s(&mti->mti_nids[i]);
1671 }
1672
1673 static void lustre_swab_obd_dqinfo (struct obd_dqinfo *i)
1674 {
1675         __swab64s (&i->dqi_bgrace);
1676         __swab64s (&i->dqi_igrace);
1677         __swab32s (&i->dqi_flags);
1678         __swab32s (&i->dqi_valid);
1679 }
1680
1681 static void lustre_swab_obd_dqblk (struct obd_dqblk *b)
1682 {
1683         __swab64s (&b->dqb_ihardlimit);
1684         __swab64s (&b->dqb_isoftlimit);
1685         __swab64s (&b->dqb_curinodes);
1686         __swab64s (&b->dqb_bhardlimit);
1687         __swab64s (&b->dqb_bsoftlimit);
1688         __swab64s (&b->dqb_curspace);
1689         __swab64s (&b->dqb_btime);
1690         __swab64s (&b->dqb_itime);
1691         __swab32s (&b->dqb_valid);
1692         CLASSERT(offsetof(typeof(*b), padding) != 0);
1693 }
1694
1695 void lustre_swab_obd_quotactl (struct obd_quotactl *q)
1696 {
1697         __swab32s (&q->qc_cmd);
1698         __swab32s (&q->qc_type);
1699         __swab32s (&q->qc_id);
1700         __swab32s (&q->qc_stat);
1701         lustre_swab_obd_dqinfo (&q->qc_dqinfo);
1702         lustre_swab_obd_dqblk (&q->qc_dqblk);
1703 }
1704
1705 void lustre_swab_quota_adjust_qunit (struct quota_adjust_qunit *q)
1706 {
1707         __swab32s (&q->qaq_flags);
1708         __swab32s (&q->qaq_id);
1709         __swab64s (&q->qaq_bunit_sz);
1710         __swab64s (&q->qaq_iunit_sz);
1711         __swab64s (&q->padding1);
1712 }
1713
1714 void lustre_swab_mds_remote_perm (struct mds_remote_perm *p)
1715 {
1716         __swab32s (&p->rp_uid);
1717         __swab32s (&p->rp_gid);
1718         __swab32s (&p->rp_fsuid);
1719         __swab32s (&p->rp_fsgid);
1720         __swab32s (&p->rp_access_perm);
1721 };
1722
1723 void lustre_swab_mdt_remote_perm (struct mdt_remote_perm *p)
1724 {
1725         __swab32s (&p->rp_uid);
1726         __swab32s (&p->rp_gid);
1727         __swab32s (&p->rp_fsuid);
1728         __swab32s (&p->rp_fsgid);
1729         __swab32s (&p->rp_access_perm);
1730 };
1731
1732 void lustre_swab_mds_rec_setattr (struct mds_rec_setattr *sa)
1733 {
1734         __swab32s (&sa->sa_opcode);
1735         __swab32s (&sa->sa_fsuid);
1736         __swab32s (&sa->sa_fsgid);
1737         __swab32s (&sa->sa_cap);
1738         __swab32s (&sa->sa_suppgid);
1739         __swab32s (&sa->sa_mode);
1740         lustre_swab_ll_fid (&sa->sa_fid);
1741         __swab64s (&sa->sa_valid);
1742         __swab64s (&sa->sa_size);
1743         __swab64s (&sa->sa_mtime);
1744         __swab64s (&sa->sa_atime);
1745         __swab64s (&sa->sa_ctime);
1746         __swab32s (&sa->sa_uid);
1747         __swab32s (&sa->sa_gid);
1748         __swab32s (&sa->sa_attr_flags);
1749         CLASSERT(offsetof(typeof(*sa), sa_padding) != 0);
1750 }
1751
1752 void lustre_swab_fid2path(struct getinfo_fid2path *gf)
1753 {
1754         lustre_swab_lu_fid(&gf->gf_fid);
1755         __swab64s(&gf->gf_recno);
1756         __swab32s(&gf->gf_linkno);
1757         __swab32s(&gf->gf_pathlen);
1758 }
1759 EXPORT_SYMBOL(lustre_swab_fid2path);
1760
1761 void lustre_swab_mds_rec_create (struct mds_rec_create *cr)
1762 {
1763         __swab32s (&cr->cr_opcode);
1764         __swab32s (&cr->cr_fsuid);
1765         __swab32s (&cr->cr_fsgid);
1766         __swab32s (&cr->cr_cap);
1767         __swab32s (&cr->cr_flags); /* for use with open */
1768         __swab32s (&cr->cr_mode);
1769         lustre_swab_ll_fid (&cr->cr_fid);
1770         lustre_swab_ll_fid (&cr->cr_replayfid);
1771         __swab64s (&cr->cr_time);
1772         __swab64s (&cr->cr_rdev);
1773         __swab32s (&cr->cr_suppgid);
1774         CLASSERT(offsetof(typeof(*cr), cr_padding_1) != 0);
1775         CLASSERT(offsetof(typeof(*cr), cr_padding_2) != 0);
1776         CLASSERT(offsetof(typeof(*cr), cr_padding_3) != 0);
1777         CLASSERT(offsetof(typeof(*cr), cr_padding_4) != 0);
1778         CLASSERT(offsetof(typeof(*cr), cr_padding_5) != 0);
1779 }
1780
1781 void lustre_swab_mds_rec_link (struct mds_rec_link *lk)
1782 {
1783         __swab32s (&lk->lk_opcode);
1784         __swab32s (&lk->lk_fsuid);
1785         __swab32s (&lk->lk_fsgid);
1786         __swab32s (&lk->lk_cap);
1787         __swab32s (&lk->lk_suppgid1);
1788         __swab32s (&lk->lk_suppgid2);
1789         lustre_swab_ll_fid (&lk->lk_fid1);
1790         lustre_swab_ll_fid (&lk->lk_fid2);
1791         __swab64s (&lk->lk_time);
1792         CLASSERT(offsetof(typeof(*lk), lk_padding_1) != 0);
1793         CLASSERT(offsetof(typeof(*lk), lk_padding_2) != 0);
1794         CLASSERT(offsetof(typeof(*lk), lk_padding_3) != 0);
1795         CLASSERT(offsetof(typeof(*lk), lk_padding_4) != 0);
1796 }
1797
1798 void lustre_swab_mds_rec_unlink (struct mds_rec_unlink *ul)
1799 {
1800         __swab32s (&ul->ul_opcode);
1801         __swab32s (&ul->ul_fsuid);
1802         __swab32s (&ul->ul_fsgid);
1803         __swab32s (&ul->ul_cap);
1804         __swab32s (&ul->ul_suppgid);
1805         __swab32s (&ul->ul_mode);
1806         lustre_swab_ll_fid (&ul->ul_fid1);
1807         lustre_swab_ll_fid (&ul->ul_fid2);
1808         __swab64s (&ul->ul_time);
1809         CLASSERT(offsetof(typeof(*ul), ul_padding_1) != 0);
1810         CLASSERT(offsetof(typeof(*ul), ul_padding_2) != 0);
1811         CLASSERT(offsetof(typeof(*ul), ul_padding_3) != 0);
1812         CLASSERT(offsetof(typeof(*ul), ul_padding_4) != 0);
1813 }
1814
1815 void lustre_swab_fiemap_extent(struct ll_fiemap_extent *fm_extent)
1816 {
1817         __swab64s(&fm_extent->fe_logical);
1818         __swab64s(&fm_extent->fe_physical);
1819         __swab64s(&fm_extent->fe_length);
1820         __swab32s(&fm_extent->fe_flags);
1821         __swab32s(&fm_extent->fe_device);
1822 }
1823
1824 void lustre_swab_fiemap(struct ll_user_fiemap *fiemap)
1825 {
1826         int i;
1827
1828         __swab64s(&fiemap->fm_start);
1829         __swab64s(&fiemap->fm_length);
1830         __swab32s(&fiemap->fm_flags);
1831         __swab32s(&fiemap->fm_mapped_extents);
1832         __swab32s(&fiemap->fm_extent_count);
1833         __swab32s(&fiemap->fm_reserved);
1834
1835         for (i = 0; i < fiemap->fm_mapped_extents; i++)
1836                 lustre_swab_fiemap_extent(&fiemap->fm_extents[i]);
1837 }
1838
1839 void lustre_swab_mds_rec_rename (struct mds_rec_rename *rn)
1840 {
1841         __swab32s (&rn->rn_opcode);
1842         __swab32s (&rn->rn_fsuid);
1843         __swab32s (&rn->rn_fsgid);
1844         __swab32s (&rn->rn_cap);
1845         __swab32s (&rn->rn_suppgid1);
1846         __swab32s (&rn->rn_suppgid2);
1847         lustre_swab_ll_fid (&rn->rn_fid1);
1848         lustre_swab_ll_fid (&rn->rn_fid2);
1849         __swab64s (&rn->rn_time);
1850         CLASSERT(offsetof(typeof(*rn), rn_padding_1) != 0);
1851         CLASSERT(offsetof(typeof(*rn), rn_padding_2) != 0);
1852         CLASSERT(offsetof(typeof(*rn), rn_padding_3) != 0);
1853         CLASSERT(offsetof(typeof(*rn), rn_padding_4) != 0);
1854 }
1855
1856 void lustre_swab_mdt_rec_reint (struct mdt_rec_reint *rr)
1857 {
1858         __swab32s (&rr->rr_opcode);
1859         __swab32s (&rr->rr_cap);
1860         __swab32s (&rr->rr_fsuid);
1861         /* rr_fsuid_h is unused */
1862         __swab32s (&rr->rr_fsgid);
1863         /* rr_fsgid_h is unused */
1864         __swab32s (&rr->rr_suppgid1);
1865         /* rr_suppgid1_h is unused */
1866         __swab32s (&rr->rr_suppgid2);
1867         /* rr_suppgid2_h is unused */
1868         lustre_swab_lu_fid (&rr->rr_fid1);
1869         lustre_swab_lu_fid (&rr->rr_fid2);
1870         __swab64s (&rr->rr_mtime);
1871         __swab64s (&rr->rr_atime);
1872         __swab64s (&rr->rr_ctime);
1873         __swab64s (&rr->rr_size);
1874         __swab64s (&rr->rr_blocks);
1875         __swab32s (&rr->rr_bias);
1876         __swab32s (&rr->rr_mode);
1877         __swab32s (&rr->rr_padding_1);
1878         __swab32s (&rr->rr_padding_2);
1879         __swab32s (&rr->rr_padding_3);
1880         __swab32s (&rr->rr_padding_4);
1881
1882         CLASSERT(offsetof(typeof(*rr), rr_padding_1) != 0);
1883         CLASSERT(offsetof(typeof(*rr), rr_padding_2) != 0);
1884         CLASSERT(offsetof(typeof(*rr), rr_padding_3) != 0);
1885         CLASSERT(offsetof(typeof(*rr), rr_padding_4) != 0);
1886 };
1887
1888 void lustre_swab_lov_desc (struct lov_desc *ld)
1889 {
1890         __swab32s (&ld->ld_tgt_count);
1891         __swab32s (&ld->ld_active_tgt_count);
1892         __swab32s (&ld->ld_default_stripe_count);
1893         __swab32s (&ld->ld_pattern);
1894         __swab64s (&ld->ld_default_stripe_size);
1895         __swab64s (&ld->ld_default_stripe_offset);
1896         __swab32s (&ld->ld_qos_maxage);
1897         /* uuid endian insensitive */
1898 }
1899
1900 void lustre_swab_lmv_desc (struct lmv_desc *ld)
1901 {
1902         __swab32s (&ld->ld_tgt_count);
1903         __swab32s (&ld->ld_active_tgt_count);
1904         __swab32s (&ld->ld_default_stripe_count);
1905         __swab32s (&ld->ld_pattern);
1906         __swab64s (&ld->ld_default_hash_size);
1907         __swab32s (&ld->ld_qos_maxage);
1908         /* uuid endian insensitive */
1909 }
1910
1911 void lustre_swab_lmv_stripe_md (struct lmv_stripe_md *mea)
1912 {
1913         __swab32s(&mea->mea_magic);
1914         __swab32s(&mea->mea_count);
1915         __swab32s(&mea->mea_master);
1916         CLASSERT(offsetof(typeof(*mea), mea_padding) != 0);
1917 }
1918
1919
1920 static void print_lum (struct lov_user_md *lum)
1921 {
1922         CDEBUG(D_OTHER, "lov_user_md %p:\n", lum);
1923         CDEBUG(D_OTHER, "\tlmm_magic: %#x\n", lum->lmm_magic);
1924         CDEBUG(D_OTHER, "\tlmm_pattern: %#x\n", lum->lmm_pattern);
1925         CDEBUG(D_OTHER, "\tlmm_object_id: "LPU64"\n", lum->lmm_object_id);
1926         CDEBUG(D_OTHER, "\tlmm_object_gr: "LPU64"\n", lum->lmm_object_gr);
1927         CDEBUG(D_OTHER, "\tlmm_stripe_size: %#x\n", lum->lmm_stripe_size);
1928         CDEBUG(D_OTHER, "\tlmm_stripe_count: %#x\n", lum->lmm_stripe_count);
1929         CDEBUG(D_OTHER, "\tlmm_stripe_offset: %#x\n", lum->lmm_stripe_offset);
1930 }
1931
1932 static void lustre_swab_lov_user_md_common(struct lov_user_md_v1 *lum)
1933 {
1934         ENTRY;
1935         __swab32s(&lum->lmm_magic);
1936         __swab32s(&lum->lmm_pattern);
1937         __swab64s(&lum->lmm_object_id);
1938         __swab64s(&lum->lmm_object_gr);
1939         __swab32s(&lum->lmm_stripe_size);
1940         __swab16s(&lum->lmm_stripe_count);
1941         __swab16s(&lum->lmm_stripe_offset);
1942         print_lum(lum);
1943         EXIT;
1944 }
1945
1946 void lustre_swab_lov_user_md_v1(struct lov_user_md_v1 *lum)
1947 {
1948         ENTRY;
1949         CDEBUG(D_IOCTL, "swabbing lov_user_md v1\n");
1950         lustre_swab_lov_user_md_common(lum);
1951         EXIT;
1952 }
1953
1954 void lustre_swab_lov_user_md_v3(struct lov_user_md_v3 *lum)
1955 {
1956         ENTRY;
1957         CDEBUG(D_IOCTL, "swabbing lov_user_md v3\n");
1958         lustre_swab_lov_user_md_common((struct lov_user_md_v1 *)lum);
1959         /* lmm_pool_name nothing to do with char */
1960         EXIT;
1961 }
1962
1963 void lustre_swab_lov_mds_md(struct lov_mds_md *lmm)
1964 {
1965         ENTRY;
1966         CDEBUG(D_IOCTL, "swabbing lov_mds_md\n");
1967         __swab32s(&lmm->lmm_magic);
1968         __swab32s(&lmm->lmm_pattern);
1969         __swab64s(&lmm->lmm_object_id);
1970         __swab64s(&lmm->lmm_object_gr);
1971         __swab32s(&lmm->lmm_stripe_size);
1972         __swab32s(&lmm->lmm_stripe_count);
1973         EXIT;
1974 }
1975
1976 void lustre_swab_lov_user_md_objects(struct lov_user_ost_data *lod,
1977                                      int stripe_count)
1978 {
1979         int i;
1980         ENTRY;
1981         for (i = 0; i < stripe_count; i++) {
1982                 __swab64s(&(lod[i].l_object_id));
1983                 __swab64s(&(lod[i].l_object_gr));
1984                 __swab32s(&(lod[i].l_ost_gen));
1985                 __swab32s(&(lod[i].l_ost_idx));
1986         }
1987         EXIT;
1988 }
1989
1990
1991 void lustre_swab_ldlm_res_id (struct ldlm_res_id *id)
1992 {
1993         int  i;
1994
1995         for (i = 0; i < RES_NAME_SIZE; i++)
1996                 __swab64s (&id->name[i]);
1997 }
1998
1999 void lustre_swab_ldlm_policy_data (ldlm_policy_data_t *d)
2000 {
2001         /* the lock data is a union and the first two fields are always an
2002          * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
2003          * data the same way. */
2004         __swab64s(&d->l_extent.start);
2005         __swab64s(&d->l_extent.end);
2006         __swab64s(&d->l_extent.gid);
2007         __swab32s(&d->l_flock.pid);
2008 }
2009
2010 void lustre_swab_ldlm_intent (struct ldlm_intent *i)
2011 {
2012         __swab64s (&i->opc);
2013 }
2014
2015 void lustre_swab_ldlm_resource_desc (struct ldlm_resource_desc *r)
2016 {
2017         __swab32s (&r->lr_type);
2018         CLASSERT(offsetof(typeof(*r), lr_padding) != 0);
2019         lustre_swab_ldlm_res_id (&r->lr_name);
2020 }
2021
2022 void lustre_swab_ldlm_lock_desc (struct ldlm_lock_desc *l)
2023 {
2024         lustre_swab_ldlm_resource_desc (&l->l_resource);
2025         __swab32s (&l->l_req_mode);
2026         __swab32s (&l->l_granted_mode);
2027         lustre_swab_ldlm_policy_data (&l->l_policy_data);
2028 }
2029
2030 void lustre_swab_ldlm_request (struct ldlm_request *rq)
2031 {
2032         __swab32s (&rq->lock_flags);
2033         lustre_swab_ldlm_lock_desc (&rq->lock_desc);
2034         __swab32s (&rq->lock_count);
2035         /* lock_handle[] opaque */
2036 }
2037
2038 void lustre_swab_ldlm_reply (struct ldlm_reply *r)
2039 {
2040         __swab32s (&r->lock_flags);
2041         CLASSERT(offsetof(typeof(*r), lock_padding) != 0);
2042         lustre_swab_ldlm_lock_desc (&r->lock_desc);
2043         /* lock_handle opaque */
2044         __swab64s (&r->lock_policy_res1);
2045         __swab64s (&r->lock_policy_res2);
2046 }
2047
2048 /* no one calls this */
2049 int llog_log_swabbed(struct llog_log_hdr *hdr)
2050 {
2051         if (hdr->llh_hdr.lrh_type == __swab32(LLOG_HDR_MAGIC))
2052                 return 1;
2053         if (hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC)
2054                 return 0;
2055         return -1;
2056 }
2057
2058 void lustre_swab_qdata(struct qunit_data *d)
2059 {
2060         __swab32s (&d->qd_id);
2061         __swab32s (&d->qd_flags);
2062         __swab64s (&d->qd_count);
2063         __swab64s (&d->qd_qunit);
2064         __swab64s (&d->padding);
2065 }
2066
2067 /* Dump functions */
2068 void dump_ioo(struct obd_ioobj *ioo)
2069 {
2070         CDEBUG(D_RPCTRACE,
2071                "obd_ioobj: ioo_id="LPD64", ioo_gr="LPD64", ioo_type=%d, "
2072                "ioo_bufct=%d\n", ioo->ioo_id, ioo->ioo_gr, ioo->ioo_type,
2073                ioo->ioo_bufcnt);
2074 }
2075
2076 void dump_rniobuf(struct niobuf_remote *nb)
2077 {
2078         CDEBUG(D_RPCTRACE, "niobuf_remote: offset="LPU64", len=%d, flags=%x\n",
2079                nb->offset, nb->len, nb->flags);
2080 }
2081
2082 void dump_obdo(struct obdo *oa)
2083 {
2084         __u32 valid = oa->o_valid;
2085
2086         CDEBUG(D_RPCTRACE, "obdo: o_valid = %08x\n", valid);
2087         if (valid & OBD_MD_FLID)
2088                 CDEBUG(D_RPCTRACE, "obdo: o_id = "LPD64"\n", oa->o_id);
2089         if (valid & OBD_MD_FLGROUP)
2090                 CDEBUG(D_RPCTRACE, "obdo: o_gr = "LPD64"\n", oa->o_gr);
2091         if (valid & OBD_MD_FLFID)
2092                 CDEBUG(D_RPCTRACE, "obdo: o_fid = "LPD64"\n", oa->o_fid);
2093         if (valid & OBD_MD_FLSIZE)
2094                 CDEBUG(D_RPCTRACE, "obdo: o_size = "LPD64"\n", oa->o_size);
2095         if (valid & OBD_MD_FLMTIME)
2096                 CDEBUG(D_RPCTRACE, "obdo: o_mtime = "LPD64"\n", oa->o_mtime);
2097         if (valid & OBD_MD_FLATIME)
2098                 CDEBUG(D_RPCTRACE, "obdo: o_atime = "LPD64"\n", oa->o_atime);
2099         if (valid & OBD_MD_FLCTIME)
2100                 CDEBUG(D_RPCTRACE, "obdo: o_ctime = "LPD64"\n", oa->o_ctime);
2101         if (valid & OBD_MD_FLBLOCKS)   /* allocation of space */
2102                 CDEBUG(D_RPCTRACE, "obdo: o_blocks = "LPD64"\n", oa->o_blocks);
2103         if (valid & OBD_MD_FLGRANT)
2104                 CDEBUG(D_RPCTRACE, "obdo: o_grant = "LPD64"\n", oa->o_grant);
2105         if (valid & OBD_MD_FLBLKSZ)
2106                 CDEBUG(D_RPCTRACE, "obdo: o_blksize = %d\n", oa->o_blksize);
2107         if (valid & (OBD_MD_FLTYPE | OBD_MD_FLMODE))
2108                 CDEBUG(D_RPCTRACE, "obdo: o_mode = %o\n",
2109                        oa->o_mode & ((valid & OBD_MD_FLTYPE ?  S_IFMT : 0) |
2110                                      (valid & OBD_MD_FLMODE ? ~S_IFMT : 0)));
2111         if (valid & OBD_MD_FLUID)
2112                 CDEBUG(D_RPCTRACE, "obdo: o_uid = %u\n", oa->o_uid);
2113         if (valid & OBD_MD_FLGID)
2114                 CDEBUG(D_RPCTRACE, "obdo: o_gid = %u\n", oa->o_gid);
2115         if (valid & OBD_MD_FLFLAGS)
2116                 CDEBUG(D_RPCTRACE, "obdo: o_flags = %x\n", oa->o_flags);
2117         if (valid & OBD_MD_FLNLINK)
2118                 CDEBUG(D_RPCTRACE, "obdo: o_nlink = %u\n", oa->o_nlink);
2119         else if (valid & OBD_MD_FLCKSUM)
2120                 CDEBUG(D_RPCTRACE, "obdo: o_checksum (o_nlink) = %u\n", oa->o_nlink);
2121         if (valid & OBD_MD_FLGENER)
2122                 CDEBUG(D_RPCTRACE, "obdo: o_generation = %u\n",
2123                        oa->o_generation);
2124         if (valid & OBD_MD_FLEASIZE)
2125                 CDEBUG(D_RPCTRACE, "obdo: o_easize = %u\n", oa->o_easize);
2126         else if (valid & OBD_MD_FLEPOCH)
2127                 CDEBUG(D_RPCTRACE, "obdo: o_epoc (o_easize) = %u\n", oa->o_easize);
2128         if (valid & OBD_MD_FLID)
2129                 CDEBUG(D_RPCTRACE, "obdo: o_stripe_idx = %u\n", oa->o_stripe_idx);
2130         if (valid & OBD_MD_FLHANDLE)
2131                 CDEBUG(D_RPCTRACE, "obdo: o_handle = "LPD64"\n", oa->o_handle.cookie);
2132         if (valid & OBD_MD_FLCOOKIE)
2133                 CDEBUG(D_RPCTRACE, "obdo: o_lcookie = "
2134                        "(llog_cookie dumping not yet implemented)\n");
2135 }
2136
2137 void dump_ost_body(struct ost_body *ob)
2138 {
2139         dump_obdo(&ob->oa);
2140 }
2141
2142 void dump_rcs(__u32 *rc)
2143 {
2144         CDEBUG(D_RPCTRACE, "rmf_rcs: %d\n", *rc);
2145 }
2146
2147 #ifdef __KERNEL__
2148
2149 /**
2150  * got qdata from request(req/rep)
2151  */
2152 struct qunit_data *quota_get_qdata(void *r, int is_req, int is_exp)
2153 {
2154         struct ptlrpc_request *req = (struct ptlrpc_request *)r;
2155         struct qunit_data *qdata;
2156         __u64  flags = is_exp ? req->rq_export->exp_connect_flags :
2157                        req->rq_import->imp_connect_data.ocd_connect_flags;
2158
2159         LASSERT(req);
2160         /* support for quota64 */
2161         LASSERT(flags & OBD_CONNECT_QUOTA64);
2162         /* support for change_qs */
2163         LASSERT(flags & OBD_CONNECT_CHANGE_QS);
2164
2165         if (is_req == QUOTA_REQUEST)
2166                 qdata = req_capsule_client_get(&req->rq_pill, &RMF_QUNIT_DATA);
2167         else
2168                 qdata = req_capsule_server_get(&req->rq_pill, &RMF_QUNIT_DATA);
2169         if (qdata == NULL)
2170                 return ERR_PTR(-EPROTO);
2171
2172         QDATA_SET_CHANGE_QS(qdata);
2173         return qdata;
2174 }
2175 EXPORT_SYMBOL(quota_get_qdata);
2176
2177 /**
2178  * copy qdata to request(req/rep)
2179  */
2180 int quota_copy_qdata(void *r, struct qunit_data *qdata, int is_req,
2181                      int is_exp)
2182 {
2183         struct ptlrpc_request *req = (struct ptlrpc_request *)r;
2184         void *target;
2185         __u64  flags = is_exp ? req->rq_export->exp_connect_flags :
2186                 req->rq_import->imp_connect_data.ocd_connect_flags;
2187
2188         LASSERT(req);
2189         LASSERT(qdata);
2190         /* support for quota64 */
2191         LASSERT(flags & OBD_CONNECT_QUOTA64);
2192         /* support for change_qs */
2193         LASSERT(flags & OBD_CONNECT_CHANGE_QS);
2194
2195         if (is_req == QUOTA_REQUEST)
2196                 target = req_capsule_client_get(&req->rq_pill, &RMF_QUNIT_DATA);
2197         else
2198                 target = req_capsule_server_get(&req->rq_pill, &RMF_QUNIT_DATA);
2199         if (target == NULL)
2200                 return -EPROTO;
2201
2202         LASSERT(target != qdata);
2203         memcpy(target, qdata, sizeof(*qdata));
2204         return 0;
2205 }
2206 EXPORT_SYMBOL(quota_copy_qdata);
2207 #endif /* __KERNEL__ */
2208
2209 static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2210 {
2211         LASSERT(req->rq_reqmsg);
2212
2213         switch (req->rq_reqmsg->lm_magic) {
2214         case LUSTRE_MSG_MAGIC_V2:
2215                 return lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
2216         default:
2217                 CERROR("bad lustre msg magic: %#08X\n",
2218                        req->rq_reqmsg->lm_magic);
2219         }
2220         return 0;
2221 }
2222
2223 static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2224 {
2225         LASSERT(req->rq_repmsg);
2226
2227         switch (req->rq_repmsg->lm_magic) {
2228         case LUSTRE_MSG_MAGIC_V2:
2229                 return lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
2230         default:
2231                 /* uninitialized yet */
2232                 return 0;
2233         }
2234 }
2235
2236 void _debug_req(struct ptlrpc_request *req, __u32 mask,
2237                 struct libcfs_debug_msg_data *data, const char *fmt, ... )
2238 {
2239         va_list args;
2240         va_start(args, fmt);
2241         libcfs_debug_vmsg2(data->msg_cdls, data->msg_subsys, mask, data->msg_file,
2242                            data->msg_fn, data->msg_line, fmt, args,
2243                            " req@%p x"LPU64"/t"LPD64"("LPD64") o%d->%s@%s:%d/%d"
2244                            " lens %d/%d e %d to %d dl "CFS_TIME_T" ref %d "
2245                            "fl "REQ_FLAGS_FMT"/%x/%x rc %d/%d\n",
2246                            req, req->rq_xid, req->rq_transno,
2247                            req->rq_reqmsg ? lustre_msg_get_transno(req->rq_reqmsg) : 0,
2248                            req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : -1,
2249                            req->rq_import ? obd2cli_tgt(req->rq_import->imp_obd) :
2250                            req->rq_export ?
2251                            (char*)req->rq_export->exp_client_uuid.uuid : "<?>",
2252                            req->rq_import ?
2253                            (char *)req->rq_import->imp_connection->c_remote_uuid.uuid :
2254                            req->rq_export ?
2255                            (char *)req->rq_export->exp_connection->c_remote_uuid.uuid : "<?>",
2256                            req->rq_request_portal, req->rq_reply_portal,
2257                            req->rq_reqlen, req->rq_replen,
2258                            req->rq_early_count, req->rq_timedout, req->rq_deadline,
2259                            atomic_read(&req->rq_refcount), DEBUG_REQ_FLAGS(req),
2260                            req->rq_reqmsg && req_ptlrpc_body_swabbed(req) ?
2261                            lustre_msg_get_flags(req->rq_reqmsg) : -1,
2262                            req->rq_repmsg && rep_ptlrpc_body_swabbed(req) ?
2263                            lustre_msg_get_flags(req->rq_repmsg) : -1,
2264                            req->rq_status,
2265                            req->rq_repmsg && rep_ptlrpc_body_swabbed(req) ?
2266                            lustre_msg_get_status(req->rq_repmsg) : -1);
2267 }
2268 EXPORT_SYMBOL(_debug_req);
2269
2270 void lustre_swab_lustre_capa(struct lustre_capa *c)
2271 {
2272         lustre_swab_lu_fid(&c->lc_fid);
2273         __swab64s (&c->lc_opc);
2274         __swab64s (&c->lc_uid);
2275         __swab64s (&c->lc_gid);
2276         __swab32s (&c->lc_flags);
2277         __swab32s (&c->lc_keyid);
2278         __swab32s (&c->lc_timeout);
2279         __swab32s (&c->lc_expiry);
2280 }
2281
2282 void lustre_swab_lustre_capa_key(struct lustre_capa_key *k)
2283 {
2284         __swab64s (&k->lk_mdsid);
2285         __swab32s (&k->lk_keyid);
2286         __swab32s (&k->lk_padding);
2287 }
2288
2289 void lustre_swab_lnlh(struct lnl_hdr *l)
2290 {
2291         __swab16s(&l->lnl_magic);
2292         /* __u8 l->lnl_transport */
2293         __swab16s(&l->lnl_msgtype);
2294         __swab16s(&l->lnl_msglen);
2295 }
2296 EXPORT_SYMBOL(lustre_swab_lnlh);
2297