Whamcloud - gitweb
LU-5349 include: use __vmalloc() to avoid __GFP_FS default
[fs/lustre-release.git] / lustre / ptlrpc / pack_generic.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
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 #include <lustre_update.h>
58
59 static inline int lustre_msg_hdr_size_v2(int count)
60 {
61         return cfs_size_round(offsetof(struct lustre_msg_v2,
62                                        lm_buflens[count]));
63 }
64
65 int lustre_msg_hdr_size(__u32 magic, int count)
66 {
67         switch (magic) {
68         case LUSTRE_MSG_MAGIC_V2:
69                 return lustre_msg_hdr_size_v2(count);
70         default:
71                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
72                 return -EINVAL;
73         }
74 }
75 EXPORT_SYMBOL(lustre_msg_hdr_size);
76
77 void ptlrpc_buf_set_swabbed(struct ptlrpc_request *req, const int inout,
78                             int index)
79 {
80         if (inout)
81                 lustre_set_req_swabbed(req, index);
82         else
83                 lustre_set_rep_swabbed(req, index);
84 }
85 EXPORT_SYMBOL(ptlrpc_buf_set_swabbed);
86
87 int ptlrpc_buf_need_swab(struct ptlrpc_request *req, const int inout,
88                          int index)
89 {
90         if (inout)
91                 return (ptlrpc_req_need_swab(req) &&
92                         !lustre_req_swabbed(req, index));
93         else
94                 return (ptlrpc_rep_need_swab(req) &&
95                         !lustre_rep_swabbed(req, index));
96 }
97 EXPORT_SYMBOL(ptlrpc_buf_need_swab);
98
99 static inline int lustre_msg_check_version_v2(struct lustre_msg_v2 *msg,
100                                               __u32 version)
101 {
102         __u32 ver = lustre_msg_get_version(msg);
103         return (ver & LUSTRE_VERSION_MASK) != version;
104 }
105
106 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version)
107 {
108         switch (msg->lm_magic) {
109         case LUSTRE_MSG_MAGIC_V1:
110                 CERROR("msg v1 not supported - please upgrade you system\n");
111                 return -EINVAL;
112         case LUSTRE_MSG_MAGIC_V2:
113                 return lustre_msg_check_version_v2(msg, version);
114         default:
115                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
116                 return 0;
117         }
118 }
119 EXPORT_SYMBOL(lustre_msg_check_version);
120
121 /* early reply size */
122 int lustre_msg_early_size()
123 {
124         static int size = 0;
125         if (!size) {
126                 /* Always reply old ptlrpc_body_v2 to keep interoprability
127                  * with the old client (< 2.3) which doesn't have pb_jobid
128                  * in the ptlrpc_body.
129                  *
130                  * XXX Remove this whenever we dorp interoprability with such
131                  *     client.
132                  */
133                 __u32 pblen = sizeof(struct ptlrpc_body_v2);
134                 size = lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, &pblen);
135         }
136         return size;
137 }
138 EXPORT_SYMBOL(lustre_msg_early_size);
139
140 int lustre_msg_size_v2(int count, __u32 *lengths)
141 {
142         int size;
143         int i;
144
145         size = lustre_msg_hdr_size_v2(count);
146         for (i = 0; i < count; i++)
147                 size += cfs_size_round(lengths[i]);
148
149         return size;
150 }
151 EXPORT_SYMBOL(lustre_msg_size_v2);
152
153 /* This returns the size of the buffer that is required to hold a lustre_msg
154  * with the given sub-buffer lengths.
155  * NOTE: this should only be used for NEW requests, and should always be
156  *       in the form of a v2 request.  If this is a connection to a v1
157  *       target then the first buffer will be stripped because the ptlrpc
158  *       data is part of the lustre_msg_v1 header. b=14043 */
159 int lustre_msg_size(__u32 magic, int count, __u32 *lens)
160 {
161         __u32 size[] = { sizeof(struct ptlrpc_body) };
162
163         if (!lens) {
164                 LASSERT(count == 1);
165                 lens = size;
166         }
167
168         LASSERT(count > 0);
169         LASSERT(lens[MSG_PTLRPC_BODY_OFF] >= sizeof(struct ptlrpc_body_v2));
170
171         switch (magic) {
172         case LUSTRE_MSG_MAGIC_V2:
173                 return lustre_msg_size_v2(count, lens);
174         default:
175                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
176                 return -EINVAL;
177         }
178 }
179 EXPORT_SYMBOL(lustre_msg_size);
180
181 /* This is used to determine the size of a buffer that was already packed
182  * and will correctly handle the different message formats. */
183 int lustre_packed_msg_size(struct lustre_msg *msg)
184 {
185         switch (msg->lm_magic) {
186         case LUSTRE_MSG_MAGIC_V2:
187                 return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
188         default:
189                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
190                 return 0;
191         }
192 }
193 EXPORT_SYMBOL(lustre_packed_msg_size);
194
195 void lustre_init_msg_v2(struct lustre_msg_v2 *msg, int count, __u32 *lens,
196                         char **bufs)
197 {
198         char *ptr;
199         int i;
200
201         msg->lm_bufcount = count;
202         /* XXX: lm_secflvr uninitialized here */
203         msg->lm_magic = LUSTRE_MSG_MAGIC_V2;
204
205         for (i = 0; i < count; i++)
206                 msg->lm_buflens[i] = lens[i];
207
208         if (bufs == NULL)
209                 return;
210
211         ptr = (char *)msg + lustre_msg_hdr_size_v2(count);
212         for (i = 0; i < count; i++) {
213                 char *tmp = bufs[i];
214                 LOGL(tmp, lens[i], ptr);
215         }
216 }
217 EXPORT_SYMBOL(lustre_init_msg_v2);
218
219 static int lustre_pack_request_v2(struct ptlrpc_request *req,
220                                   int count, __u32 *lens, char **bufs)
221 {
222         int reqlen, rc;
223
224         reqlen = lustre_msg_size_v2(count, lens);
225
226         rc = sptlrpc_cli_alloc_reqbuf(req, reqlen);
227         if (rc)
228                 return rc;
229
230         req->rq_reqlen = reqlen;
231
232         lustre_init_msg_v2(req->rq_reqmsg, count, lens, bufs);
233         lustre_msg_add_version(req->rq_reqmsg, PTLRPC_MSG_VERSION);
234         return 0;
235 }
236
237 int lustre_pack_request(struct ptlrpc_request *req, __u32 magic, int count,
238                         __u32 *lens, char **bufs)
239 {
240         __u32 size[] = { sizeof(struct ptlrpc_body) };
241
242         if (!lens) {
243                 LASSERT(count == 1);
244                 lens = size;
245         }
246
247         LASSERT(count > 0);
248         LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
249
250         /* only use new format, we don't need to be compatible with 1.4 */
251         magic = LUSTRE_MSG_MAGIC_V2;
252
253         switch (magic) {
254         case LUSTRE_MSG_MAGIC_V2:
255                 return lustre_pack_request_v2(req, count, lens, bufs);
256         default:
257                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
258                 return -EINVAL;
259         }
260 }
261 EXPORT_SYMBOL(lustre_pack_request);
262
263 #if RS_DEBUG
264 struct list_head ptlrpc_rs_debug_lru =
265         LIST_HEAD_INIT(ptlrpc_rs_debug_lru);
266 spinlock_t ptlrpc_rs_debug_lock;
267
268 #define PTLRPC_RS_DEBUG_LRU_ADD(rs)                                     \
269 do {                                                                    \
270         spin_lock(&ptlrpc_rs_debug_lock);                               \
271         list_add_tail(&(rs)->rs_debug_list, &ptlrpc_rs_debug_lru);      \
272         spin_unlock(&ptlrpc_rs_debug_lock);                             \
273 } while (0)
274
275 #define PTLRPC_RS_DEBUG_LRU_DEL(rs)                                     \
276 do {                                                                    \
277         spin_lock(&ptlrpc_rs_debug_lock);                               \
278         list_del(&(rs)->rs_debug_list);                         \
279         spin_unlock(&ptlrpc_rs_debug_lock);                             \
280 } while (0)
281 #else
282 # define PTLRPC_RS_DEBUG_LRU_ADD(rs) do {} while(0)
283 # define PTLRPC_RS_DEBUG_LRU_DEL(rs) do {} while(0)
284 #endif
285
286 struct ptlrpc_reply_state *
287 lustre_get_emerg_rs(struct ptlrpc_service_part *svcpt)
288 {
289         struct ptlrpc_reply_state *rs = NULL;
290
291         spin_lock(&svcpt->scp_rep_lock);
292
293         /* See if we have anything in a pool, and wait if nothing */
294         while (list_empty(&svcpt->scp_rep_idle)) {
295                 struct l_wait_info      lwi;
296                 int                     rc;
297
298                 spin_unlock(&svcpt->scp_rep_lock);
299                 /* If we cannot get anything for some long time, we better
300                  * bail out instead of waiting infinitely */
301                 lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
302                 rc = l_wait_event(svcpt->scp_rep_waitq,
303                                   !list_empty(&svcpt->scp_rep_idle), &lwi);
304                 if (rc != 0)
305                         goto out;
306                 spin_lock(&svcpt->scp_rep_lock);
307         }
308
309         rs = list_entry(svcpt->scp_rep_idle.next,
310                             struct ptlrpc_reply_state, rs_list);
311         list_del(&rs->rs_list);
312
313         spin_unlock(&svcpt->scp_rep_lock);
314
315         memset(rs, 0, svcpt->scp_service->srv_max_reply_size);
316         rs->rs_size = svcpt->scp_service->srv_max_reply_size;
317         rs->rs_svcpt = svcpt;
318         rs->rs_prealloc = 1;
319 out:
320         return rs;
321 }
322
323 void lustre_put_emerg_rs(struct ptlrpc_reply_state *rs)
324 {
325         struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
326
327         spin_lock(&svcpt->scp_rep_lock);
328         list_add(&rs->rs_list, &svcpt->scp_rep_idle);
329         spin_unlock(&svcpt->scp_rep_lock);
330         wake_up(&svcpt->scp_rep_waitq);
331 }
332
333 int lustre_pack_reply_v2(struct ptlrpc_request *req, int count,
334                          __u32 *lens, char **bufs, int flags)
335 {
336         struct ptlrpc_reply_state *rs;
337         int                        msg_len, rc;
338         ENTRY;
339
340         LASSERT(req->rq_reply_state == NULL);
341
342         if ((flags & LPRFL_EARLY_REPLY) == 0) {
343                 spin_lock(&req->rq_lock);
344                 req->rq_packed_final = 1;
345                 spin_unlock(&req->rq_lock);
346         }
347
348         msg_len = lustre_msg_size_v2(count, lens);
349         rc = sptlrpc_svc_alloc_rs(req, msg_len);
350         if (rc)
351                 RETURN(rc);
352
353         rs = req->rq_reply_state;
354         atomic_set(&rs->rs_refcount, 1);        /* 1 ref for rq_reply_state */
355         rs->rs_cb_id.cbid_fn = reply_out_callback;
356         rs->rs_cb_id.cbid_arg = rs;
357         rs->rs_svcpt = req->rq_rqbd->rqbd_svcpt;
358         INIT_LIST_HEAD(&rs->rs_exp_list);
359         INIT_LIST_HEAD(&rs->rs_obd_list);
360         INIT_LIST_HEAD(&rs->rs_list);
361         spin_lock_init(&rs->rs_lock);
362
363         req->rq_replen = msg_len;
364         req->rq_reply_state = rs;
365         req->rq_repmsg = rs->rs_msg;
366
367         lustre_init_msg_v2(rs->rs_msg, count, lens, bufs);
368         lustre_msg_add_version(rs->rs_msg, PTLRPC_MSG_VERSION);
369
370         PTLRPC_RS_DEBUG_LRU_ADD(rs);
371
372         RETURN(0);
373 }
374 EXPORT_SYMBOL(lustre_pack_reply_v2);
375
376 int lustre_pack_reply_flags(struct ptlrpc_request *req, int count, __u32 *lens,
377                             char **bufs, int flags)
378 {
379         int rc = 0;
380         __u32 size[] = { sizeof(struct ptlrpc_body) };
381
382         if (!lens) {
383                 LASSERT(count == 1);
384                 lens = size;
385         }
386
387         LASSERT(count > 0);
388         LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
389
390         switch (req->rq_reqmsg->lm_magic) {
391         case LUSTRE_MSG_MAGIC_V2:
392                 rc = lustre_pack_reply_v2(req, count, lens, bufs, flags);
393                 break;
394         default:
395                 LASSERTF(0, "incorrect message magic: %08x\n",
396                          req->rq_reqmsg->lm_magic);
397                 rc = -EINVAL;
398         }
399         if (rc != 0)
400                 CERROR("lustre_pack_reply failed: rc=%d size=%d\n", rc,
401                        lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens));
402         return rc;
403 }
404 EXPORT_SYMBOL(lustre_pack_reply_flags);
405
406 int lustre_pack_reply(struct ptlrpc_request *req, int count, __u32 *lens,
407                       char **bufs)
408 {
409         return lustre_pack_reply_flags(req, count, lens, bufs, 0);
410 }
411 EXPORT_SYMBOL(lustre_pack_reply);
412
413 void *lustre_msg_buf_v2(struct lustre_msg_v2 *m, int n, int min_size)
414 {
415         int i, offset, buflen, bufcount;
416
417         LASSERT(m != NULL);
418         LASSERT(n >= 0);
419
420         bufcount = m->lm_bufcount;
421         if (unlikely(n >= bufcount)) {
422                 CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
423                        m, n, bufcount);
424                 return NULL;
425         }
426
427         buflen = m->lm_buflens[n];
428         if (unlikely(buflen < min_size)) {
429                 CERROR("msg %p buffer[%d] size %d too small "
430                        "(required %d, opc=%d)\n", m, n, buflen, min_size,
431                        n == MSG_PTLRPC_BODY_OFF ? -1 : lustre_msg_get_opc(m));
432                 return NULL;
433         }
434
435         offset = lustre_msg_hdr_size_v2(bufcount);
436         for (i = 0; i < n; i++)
437                 offset += cfs_size_round(m->lm_buflens[i]);
438
439         return (char *)m + offset;
440 }
441
442 void *lustre_msg_buf(struct lustre_msg *m, int n, int min_size)
443 {
444         switch (m->lm_magic) {
445         case LUSTRE_MSG_MAGIC_V2:
446                 return lustre_msg_buf_v2(m, n, min_size);
447         default:
448                 LASSERTF(0, "incorrect message magic: %08x(msg:%p)\n", m->lm_magic, m);
449                 return NULL;
450         }
451 }
452 EXPORT_SYMBOL(lustre_msg_buf);
453
454 int lustre_shrink_msg_v2(struct lustre_msg_v2 *msg, int segment,
455                          unsigned int newlen, int move_data)
456 {
457         char   *tail = NULL, *newpos;
458         int     tail_len = 0, n;
459
460         LASSERT(msg);
461         LASSERT(msg->lm_bufcount > segment);
462         LASSERT(msg->lm_buflens[segment] >= newlen);
463
464         if (msg->lm_buflens[segment] == newlen)
465                 goto out;
466
467         if (move_data && msg->lm_bufcount > segment + 1) {
468                 tail = lustre_msg_buf_v2(msg, segment + 1, 0);
469                 for (n = segment + 1; n < msg->lm_bufcount; n++)
470                         tail_len += cfs_size_round(msg->lm_buflens[n]);
471         }
472
473         msg->lm_buflens[segment] = newlen;
474
475         if (tail && tail_len) {
476                 newpos = lustre_msg_buf_v2(msg, segment + 1, 0);
477                 LASSERT(newpos <= tail);
478                 if (newpos != tail)
479                         memmove(newpos, tail, tail_len);
480         }
481 out:
482         return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
483 }
484
485 /*
486  * for @msg, shrink @segment to size @newlen. if @move_data is non-zero,
487  * we also move data forward from @segment + 1.
488  *
489  * if @newlen == 0, we remove the segment completely, but we still keep the
490  * totally bufcount the same to save possible data moving. this will leave a
491  * unused segment with size 0 at the tail, but that's ok.
492  *
493  * return new msg size after shrinking.
494  *
495  * CAUTION:
496  * + if any buffers higher than @segment has been filled in, must call shrink
497  *   with non-zero @move_data.
498  * + caller should NOT keep pointers to msg buffers which higher than @segment
499  *   after call shrink.
500  */
501 int lustre_shrink_msg(struct lustre_msg *msg, int segment,
502                       unsigned int newlen, int move_data)
503 {
504         switch (msg->lm_magic) {
505         case LUSTRE_MSG_MAGIC_V2:
506                 return lustre_shrink_msg_v2(msg, segment, newlen, move_data);
507         default:
508                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
509         }
510 }
511 EXPORT_SYMBOL(lustre_shrink_msg);
512
513 void lustre_free_reply_state(struct ptlrpc_reply_state *rs)
514 {
515         PTLRPC_RS_DEBUG_LRU_DEL(rs);
516
517         LASSERT(atomic_read(&rs->rs_refcount) == 0);
518         LASSERT(!rs->rs_difficult || rs->rs_handled);
519         LASSERT(!rs->rs_on_net);
520         LASSERT(!rs->rs_scheduled);
521         LASSERT(rs->rs_export == NULL);
522         LASSERT(rs->rs_nlocks == 0);
523         LASSERT(list_empty(&rs->rs_exp_list));
524         LASSERT(list_empty(&rs->rs_obd_list));
525
526         sptlrpc_svc_free_rs(rs);
527 }
528 EXPORT_SYMBOL(lustre_free_reply_state);
529
530 static int lustre_unpack_msg_v2(struct lustre_msg_v2 *m, int len)
531 {
532         int swabbed, required_len, i;
533
534         /* Now we know the sender speaks my language. */
535         required_len = lustre_msg_hdr_size_v2(0);
536         if (len < required_len) {
537                 /* can't even look inside the message */
538                 CERROR("message length %d too small for lustre_msg\n", len);
539                 return -EINVAL;
540         }
541
542         swabbed = (m->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED);
543
544         if (swabbed) {
545                 __swab32s(&m->lm_magic);
546                 __swab32s(&m->lm_bufcount);
547                 __swab32s(&m->lm_secflvr);
548                 __swab32s(&m->lm_repsize);
549                 __swab32s(&m->lm_cksum);
550                 __swab32s(&m->lm_flags);
551                 CLASSERT(offsetof(typeof(*m), lm_padding_2) != 0);
552                 CLASSERT(offsetof(typeof(*m), lm_padding_3) != 0);
553         }
554
555         required_len = lustre_msg_hdr_size_v2(m->lm_bufcount);
556         if (len < required_len) {
557                 /* didn't receive all the buffer lengths */
558                 CERROR ("message length %d too small for %d buflens\n",
559                         len, m->lm_bufcount);
560                 return -EINVAL;
561         }
562
563         for (i = 0; i < m->lm_bufcount; i++) {
564                 if (swabbed)
565                         __swab32s(&m->lm_buflens[i]);
566                 required_len += cfs_size_round(m->lm_buflens[i]);
567         }
568
569         if (len < required_len) {
570                 CERROR("len: %d, required_len %d\n", len, required_len);
571                 CERROR("bufcount: %d\n", m->lm_bufcount);
572                 for (i = 0; i < m->lm_bufcount; i++)
573                         CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
574                 return -EINVAL;
575         }
576
577         return swabbed;
578 }
579
580 int __lustre_unpack_msg(struct lustre_msg *m, int len)
581 {
582         int required_len, rc;
583         ENTRY;
584
585         /* We can provide a slightly better error log, if we check the
586          * message magic and version first.  In the future, struct
587          * lustre_msg may grow, and we'd like to log a version mismatch,
588          * rather than a short message.
589          *
590          */
591         required_len = offsetof(struct lustre_msg, lm_magic) +
592                        sizeof(m->lm_magic);
593         if (len < required_len) {
594                 /* can't even look inside the message */
595                 CERROR("message length %d too small for magic/version check\n",
596                        len);
597                 RETURN(-EINVAL);
598         }
599
600         rc = lustre_unpack_msg_v2(m, len);
601
602         RETURN(rc);
603 }
604 EXPORT_SYMBOL(__lustre_unpack_msg);
605
606 int ptlrpc_unpack_req_msg(struct ptlrpc_request *req, int len)
607 {
608         int rc;
609         rc = __lustre_unpack_msg(req->rq_reqmsg, len);
610         if (rc == 1) {
611                 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
612                 rc = 0;
613         }
614         return rc;
615 }
616 EXPORT_SYMBOL(ptlrpc_unpack_req_msg);
617
618 int ptlrpc_unpack_rep_msg(struct ptlrpc_request *req, int len)
619 {
620         int rc;
621         rc = __lustre_unpack_msg(req->rq_repmsg, len);
622         if (rc == 1) {
623                 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
624                 rc = 0;
625         }
626         return rc;
627 }
628 EXPORT_SYMBOL(ptlrpc_unpack_rep_msg);
629
630 static inline int lustre_unpack_ptlrpc_body_v2(struct ptlrpc_request *req,
631                                                const int inout, int offset)
632 {
633         struct ptlrpc_body *pb;
634         struct lustre_msg_v2 *m = inout ? req->rq_reqmsg : req->rq_repmsg;
635
636         pb = lustre_msg_buf_v2(m, offset, sizeof(struct ptlrpc_body_v2));
637         if (!pb) {
638                 CERROR("error unpacking ptlrpc body\n");
639                 return -EFAULT;
640         }
641         if (ptlrpc_buf_need_swab(req, inout, offset)) {
642                 lustre_swab_ptlrpc_body(pb);
643                 ptlrpc_buf_set_swabbed(req, inout, offset);
644         }
645
646         if ((pb->pb_version & ~LUSTRE_VERSION_MASK) != PTLRPC_MSG_VERSION) {
647                  CERROR("wrong lustre_msg version %08x\n", pb->pb_version);
648                  return -EINVAL;
649         }
650
651         if (!inout)
652                 pb->pb_status = ptlrpc_status_ntoh(pb->pb_status);
653
654         return 0;
655 }
656
657 int lustre_unpack_req_ptlrpc_body(struct ptlrpc_request *req, int offset)
658 {
659         switch (req->rq_reqmsg->lm_magic) {
660         case LUSTRE_MSG_MAGIC_V2:
661                 return lustre_unpack_ptlrpc_body_v2(req, 1, offset);
662         default:
663                 CERROR("bad lustre msg magic: %08x\n",
664                        req->rq_reqmsg->lm_magic);
665                 return -EINVAL;
666         }
667 }
668
669 int lustre_unpack_rep_ptlrpc_body(struct ptlrpc_request *req, int offset)
670 {
671         switch (req->rq_repmsg->lm_magic) {
672         case LUSTRE_MSG_MAGIC_V2:
673                 return lustre_unpack_ptlrpc_body_v2(req, 0, offset);
674         default:
675                 CERROR("bad lustre msg magic: %08x\n",
676                        req->rq_repmsg->lm_magic);
677                 return -EINVAL;
678         }
679 }
680
681 static inline int lustre_msg_buflen_v2(struct lustre_msg_v2 *m, int n)
682 {
683         if (n >= m->lm_bufcount)
684                 return 0;
685
686         return m->lm_buflens[n];
687 }
688
689 /**
690  * lustre_msg_buflen - return the length of buffer \a n in message \a m
691  * \param m lustre_msg (request or reply) to look at
692  * \param n message index (base 0)
693  *
694  * returns zero for non-existent message indices
695  */
696 int lustre_msg_buflen(struct lustre_msg *m, int n)
697 {
698         switch (m->lm_magic) {
699         case LUSTRE_MSG_MAGIC_V2:
700                 return lustre_msg_buflen_v2(m, n);
701         default:
702                 CERROR("incorrect message magic: %08x\n", m->lm_magic);
703                 return -EINVAL;
704         }
705 }
706 EXPORT_SYMBOL(lustre_msg_buflen);
707
708 static inline void
709 lustre_msg_set_buflen_v2(struct lustre_msg_v2 *m, int n, int len)
710 {
711         if (n >= m->lm_bufcount)
712                 LBUG();
713
714         m->lm_buflens[n] = len;
715 }
716
717 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len)
718 {
719         switch (m->lm_magic) {
720         case LUSTRE_MSG_MAGIC_V2:
721                 lustre_msg_set_buflen_v2(m, n, len);
722                 return;
723         default:
724                 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
725         }
726 }
727
728 EXPORT_SYMBOL(lustre_msg_set_buflen);
729
730 /* NB return the bufcount for lustre_msg_v2 format, so if message is packed
731  * in V1 format, the result is one bigger. (add struct ptlrpc_body). */
732 int lustre_msg_bufcount(struct lustre_msg *m)
733 {
734         switch (m->lm_magic) {
735         case LUSTRE_MSG_MAGIC_V2:
736                 return m->lm_bufcount;
737         default:
738                 CERROR("incorrect message magic: %08x\n", m->lm_magic);
739                 return -EINVAL;
740         }
741 }
742 EXPORT_SYMBOL(lustre_msg_bufcount);
743
744 char *lustre_msg_string(struct lustre_msg *m, int index, int max_len)
745 {
746         /* max_len == 0 means the string should fill the buffer */
747         char *str;
748         int slen, blen;
749
750         switch (m->lm_magic) {
751         case LUSTRE_MSG_MAGIC_V2:
752                 str = lustre_msg_buf_v2(m, index, 0);
753                 blen = lustre_msg_buflen_v2(m, index);
754                 break;
755         default:
756                 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
757         }
758
759         if (str == NULL) {
760                 CERROR ("can't unpack string in msg %p buffer[%d]\n", m, index);
761                 return NULL;
762         }
763
764         slen = strnlen(str, blen);
765
766         if (slen == blen) {                     /* not NULL terminated */
767                 CERROR("can't unpack non-NULL terminated string in "
768                         "msg %p buffer[%d] len %d\n", m, index, blen);
769                 return NULL;
770         }
771
772         if (max_len == 0) {
773                 if (slen != blen - 1) {
774                         CERROR("can't unpack short string in msg %p "
775                                "buffer[%d] len %d: strlen %d\n",
776                                m, index, blen, slen);
777                         return NULL;
778                 }
779         } else if (slen > max_len) {
780                 CERROR("can't unpack oversized string in msg %p "
781                        "buffer[%d] len %d strlen %d: max %d expected\n",
782                        m, index, blen, slen, max_len);
783                 return NULL;
784         }
785
786         return str;
787 }
788 EXPORT_SYMBOL(lustre_msg_string);
789
790 /* Wrap up the normal fixed length cases */
791 static inline void *__lustre_swab_buf(struct lustre_msg *msg, int index,
792                                       int min_size, void *swabber)
793 {
794         void *ptr = NULL;
795
796         LASSERT(msg != NULL);
797         switch (msg->lm_magic) {
798         case LUSTRE_MSG_MAGIC_V2:
799                 ptr = lustre_msg_buf_v2(msg, index, min_size);
800                 break;
801         default:
802                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
803         }
804
805         if (ptr && swabber)
806                 ((void (*)(void *))swabber)(ptr);
807
808         return ptr;
809 }
810
811 static inline struct ptlrpc_body *lustre_msg_ptlrpc_body(struct lustre_msg *msg)
812 {
813         return lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
814                                  sizeof(struct ptlrpc_body_v2));
815 }
816
817 __u32 lustre_msghdr_get_flags(struct lustre_msg *msg)
818 {
819         switch (msg->lm_magic) {
820         case LUSTRE_MSG_MAGIC_V1:
821         case LUSTRE_MSG_MAGIC_V1_SWABBED:
822                 return 0;
823         case LUSTRE_MSG_MAGIC_V2:
824                 /* already in host endian */
825                 return msg->lm_flags;
826         default:
827                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
828                 return 0;
829         }
830 }
831 EXPORT_SYMBOL(lustre_msghdr_get_flags);
832
833 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags)
834 {
835         switch (msg->lm_magic) {
836         case LUSTRE_MSG_MAGIC_V1:
837                 return;
838         case LUSTRE_MSG_MAGIC_V2:
839                 msg->lm_flags = flags;
840                 return;
841         default:
842                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
843         }
844 }
845
846 __u32 lustre_msg_get_flags(struct lustre_msg *msg)
847 {
848         switch (msg->lm_magic) {
849         case LUSTRE_MSG_MAGIC_V2: {
850                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
851                 if (!pb) {
852                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
853                         return 0;
854                 }
855                 return pb->pb_flags;
856         }
857         default:
858                 /* flags might be printed in debug code while message
859                  * uninitialized */
860                 return 0;
861         }
862 }
863 EXPORT_SYMBOL(lustre_msg_get_flags);
864
865 void lustre_msg_add_flags(struct lustre_msg *msg, int flags)
866 {
867         switch (msg->lm_magic) {
868         case LUSTRE_MSG_MAGIC_V2: {
869                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
870                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
871                 pb->pb_flags |= flags;
872                 return;
873         }
874         default:
875                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
876         }
877 }
878 EXPORT_SYMBOL(lustre_msg_add_flags);
879
880 void lustre_msg_set_flags(struct lustre_msg *msg, int flags)
881 {
882         switch (msg->lm_magic) {
883         case LUSTRE_MSG_MAGIC_V2: {
884                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
885                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
886                 pb->pb_flags = flags;
887                 return;
888         }
889         default:
890                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
891         }
892 }
893 EXPORT_SYMBOL(lustre_msg_set_flags);
894
895 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags)
896 {
897         switch (msg->lm_magic) {
898         case LUSTRE_MSG_MAGIC_V2: {
899                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
900                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
901                 pb->pb_flags &= ~(MSG_GEN_FLAG_MASK & flags);
902                 return;
903         }
904         default:
905                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
906         }
907 }
908 EXPORT_SYMBOL(lustre_msg_clear_flags);
909
910 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg)
911 {
912         switch (msg->lm_magic) {
913         case LUSTRE_MSG_MAGIC_V2: {
914                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
915                 if (!pb) {
916                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
917                         return 0;
918                 }
919                 return pb->pb_op_flags;
920         }
921         default:
922                 return 0;
923         }
924 }
925 EXPORT_SYMBOL(lustre_msg_get_op_flags);
926
927 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags)
928 {
929         switch (msg->lm_magic) {
930         case LUSTRE_MSG_MAGIC_V2: {
931                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
932                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
933                 pb->pb_op_flags |= flags;
934                 return;
935         }
936         default:
937                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
938         }
939 }
940 EXPORT_SYMBOL(lustre_msg_add_op_flags);
941
942 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags)
943 {
944         switch (msg->lm_magic) {
945         case LUSTRE_MSG_MAGIC_V2: {
946                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
947                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
948                 pb->pb_op_flags |= flags;
949                 return;
950         }
951         default:
952                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
953         }
954 }
955 EXPORT_SYMBOL(lustre_msg_set_op_flags);
956
957 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg)
958 {
959         switch (msg->lm_magic) {
960         case LUSTRE_MSG_MAGIC_V2: {
961                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
962                 if (!pb) {
963                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
964                         return NULL;
965                 }
966                 return &pb->pb_handle;
967         }
968         default:
969                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
970                 return NULL;
971         }
972 }
973 EXPORT_SYMBOL(lustre_msg_get_handle);
974
975 __u32 lustre_msg_get_type(struct lustre_msg *msg)
976 {
977         switch (msg->lm_magic) {
978         case LUSTRE_MSG_MAGIC_V2: {
979                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
980                 if (!pb) {
981                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
982                         return PTL_RPC_MSG_ERR;
983                 }
984                 return pb->pb_type;
985         }
986         default:
987                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
988                 return PTL_RPC_MSG_ERR;
989         }
990 }
991 EXPORT_SYMBOL(lustre_msg_get_type);
992
993 __u32 lustre_msg_get_version(struct lustre_msg *msg)
994 {
995         switch (msg->lm_magic) {
996         case LUSTRE_MSG_MAGIC_V2: {
997                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
998                 if (!pb) {
999                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1000                         return 0;
1001                 }
1002                 return pb->pb_version;
1003         }
1004         default:
1005                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1006                 return 0;
1007         }
1008 }
1009 EXPORT_SYMBOL(lustre_msg_get_version);
1010
1011 void lustre_msg_add_version(struct lustre_msg *msg, int version)
1012 {
1013         switch (msg->lm_magic) {
1014         case LUSTRE_MSG_MAGIC_V2: {
1015                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1016                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1017                 pb->pb_version |= version;
1018                 return;
1019         }
1020         default:
1021                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1022         }
1023 }
1024 EXPORT_SYMBOL(lustre_msg_add_version);
1025
1026 __u32 lustre_msg_get_opc(struct lustre_msg *msg)
1027 {
1028         switch (msg->lm_magic) {
1029         case LUSTRE_MSG_MAGIC_V2: {
1030                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1031                 if (!pb) {
1032                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1033                         return 0;
1034                 }
1035                 return pb->pb_opc;
1036         }
1037         default:
1038                 CERROR("incorrect message magic: %08x(msg:%p)\n", msg->lm_magic, msg);
1039                 LBUG();
1040                 return 0;
1041         }
1042 }
1043 EXPORT_SYMBOL(lustre_msg_get_opc);
1044
1045 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg)
1046 {
1047         switch (msg->lm_magic) {
1048         case LUSTRE_MSG_MAGIC_V2: {
1049                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1050                 if (!pb) {
1051                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1052                         return 0;
1053                 }
1054                 return pb->pb_last_xid;
1055         }
1056         default:
1057                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1058                 return 0;
1059         }
1060 }
1061 EXPORT_SYMBOL(lustre_msg_get_last_xid);
1062
1063 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg)
1064 {
1065         switch (msg->lm_magic) {
1066         case LUSTRE_MSG_MAGIC_V2: {
1067                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1068                 if (!pb) {
1069                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1070                         return 0;
1071                 }
1072                 return pb->pb_last_committed;
1073         }
1074         default:
1075                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1076                 return 0;
1077         }
1078 }
1079 EXPORT_SYMBOL(lustre_msg_get_last_committed);
1080
1081 __u64 *lustre_msg_get_versions(struct lustre_msg *msg)
1082 {
1083         switch (msg->lm_magic) {
1084         case LUSTRE_MSG_MAGIC_V1:
1085                 return NULL;
1086         case LUSTRE_MSG_MAGIC_V2: {
1087                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1088                 if (!pb) {
1089                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1090                         return NULL;
1091                 }
1092                 return pb->pb_pre_versions;
1093         }
1094         default:
1095                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1096                 return NULL;
1097         }
1098 }
1099 EXPORT_SYMBOL(lustre_msg_get_versions);
1100
1101 __u64 lustre_msg_get_transno(struct lustre_msg *msg)
1102 {
1103         switch (msg->lm_magic) {
1104         case LUSTRE_MSG_MAGIC_V2: {
1105                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1106                 if (!pb) {
1107                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1108                         return 0;
1109                 }
1110                 return pb->pb_transno;
1111         }
1112         default:
1113                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1114                 return 0;
1115         }
1116 }
1117 EXPORT_SYMBOL(lustre_msg_get_transno);
1118
1119 int lustre_msg_get_status(struct lustre_msg *msg)
1120 {
1121         switch (msg->lm_magic) {
1122         case LUSTRE_MSG_MAGIC_V2: {
1123                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1124                 if (!pb) {
1125                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1126                         return -EINVAL;
1127                 }
1128                 return pb->pb_status;
1129         }
1130         default:
1131                 /* status might be printed in debug code while message
1132                  * uninitialized */
1133                 return -EINVAL;
1134         }
1135 }
1136 EXPORT_SYMBOL(lustre_msg_get_status);
1137
1138 __u64 lustre_msg_get_slv(struct lustre_msg *msg)
1139 {
1140         switch (msg->lm_magic) {
1141         case LUSTRE_MSG_MAGIC_V2: {
1142                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1143                 if (!pb) {
1144                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1145                         return -EINVAL;
1146                 }
1147                 return pb->pb_slv;
1148         }
1149         default:
1150                 CERROR("invalid msg magic %08x\n", msg->lm_magic);
1151                 return -EINVAL;
1152         }
1153 }
1154 EXPORT_SYMBOL(lustre_msg_get_slv);
1155
1156
1157 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv)
1158 {
1159         switch (msg->lm_magic) {
1160         case LUSTRE_MSG_MAGIC_V2: {
1161                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1162                 if (!pb) {
1163                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1164                         return;
1165                 }
1166                 pb->pb_slv = slv;
1167                 return;
1168         }
1169         default:
1170                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1171                 return;
1172         }
1173 }
1174 EXPORT_SYMBOL(lustre_msg_set_slv);
1175
1176 __u32 lustre_msg_get_limit(struct lustre_msg *msg)
1177 {
1178         switch (msg->lm_magic) {
1179         case LUSTRE_MSG_MAGIC_V2: {
1180                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1181                 if (!pb) {
1182                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1183                         return -EINVAL;
1184                 }
1185                 return pb->pb_limit;
1186         }
1187         default:
1188                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1189                 return -EINVAL;
1190         }
1191 }
1192 EXPORT_SYMBOL(lustre_msg_get_limit);
1193
1194
1195 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit)
1196 {
1197         switch (msg->lm_magic) {
1198         case LUSTRE_MSG_MAGIC_V2: {
1199                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1200                 if (!pb) {
1201                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1202                         return;
1203                 }
1204                 pb->pb_limit = limit;
1205                 return;
1206         }
1207         default:
1208                 CERROR("invalid msg magic %08x\n", msg->lm_magic);
1209                 return;
1210         }
1211 }
1212 EXPORT_SYMBOL(lustre_msg_set_limit);
1213
1214 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg)
1215 {
1216         switch (msg->lm_magic) {
1217         case LUSTRE_MSG_MAGIC_V2: {
1218                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1219                 if (!pb) {
1220                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1221                         return 0;
1222                 }
1223                 return pb->pb_conn_cnt;
1224         }
1225         default:
1226                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1227                 return 0;
1228         }
1229 }
1230 EXPORT_SYMBOL(lustre_msg_get_conn_cnt);
1231
1232 int lustre_msg_is_v1(struct lustre_msg *msg)
1233 {
1234         switch (msg->lm_magic) {
1235         case LUSTRE_MSG_MAGIC_V1:
1236         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1237                 return 1;
1238         default:
1239                 return 0;
1240         }
1241 }
1242 EXPORT_SYMBOL(lustre_msg_is_v1);
1243
1244 __u32 lustre_msg_get_magic(struct lustre_msg *msg)
1245 {
1246         switch (msg->lm_magic) {
1247         case LUSTRE_MSG_MAGIC_V2:
1248                 return msg->lm_magic;
1249         default:
1250                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1251                 return 0;
1252         }
1253 }
1254 EXPORT_SYMBOL(lustre_msg_get_magic);
1255
1256 __u32 lustre_msg_get_timeout(struct lustre_msg *msg)
1257 {
1258         switch (msg->lm_magic) {
1259         case LUSTRE_MSG_MAGIC_V1:
1260         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1261                 return 0;
1262         case LUSTRE_MSG_MAGIC_V2: {
1263                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1264                 if (!pb) {
1265                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1266                         return 0;
1267
1268                 }
1269                 return pb->pb_timeout;
1270         }
1271         default:
1272                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1273                 return 0;
1274         }
1275 }
1276
1277 __u32 lustre_msg_get_service_time(struct lustre_msg *msg)
1278 {
1279         switch (msg->lm_magic) {
1280         case LUSTRE_MSG_MAGIC_V1:
1281         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1282                 return 0;
1283         case LUSTRE_MSG_MAGIC_V2: {
1284                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1285                 if (!pb) {
1286                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1287                         return 0;
1288
1289                 }
1290                 return pb->pb_service_time;
1291         }
1292         default:
1293                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1294                 return 0;
1295         }
1296 }
1297
1298 char *lustre_msg_get_jobid(struct lustre_msg *msg)
1299 {
1300         switch (msg->lm_magic) {
1301         case LUSTRE_MSG_MAGIC_V1:
1302         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1303                 return NULL;
1304         case LUSTRE_MSG_MAGIC_V2: {
1305                 struct ptlrpc_body *pb =
1306                         lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1307                                           sizeof(struct ptlrpc_body));
1308                 if (!pb)
1309                         return NULL;
1310
1311                 return pb->pb_jobid;
1312         }
1313         default:
1314                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1315                 return NULL;
1316         }
1317 }
1318 EXPORT_SYMBOL(lustre_msg_get_jobid);
1319
1320 __u32 lustre_msg_get_cksum(struct lustre_msg *msg)
1321 {
1322         switch (msg->lm_magic) {
1323         case LUSTRE_MSG_MAGIC_V2:
1324                 return msg->lm_cksum;
1325         default:
1326                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1327                 return 0;
1328         }
1329 }
1330
1331 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
1332 /*
1333  * In 1.6 and 1.8 the checksum was computed only on struct ptlrpc_body as
1334  * it was in 1.6 (88 bytes, smaller than the full size in 1.8).  It makes
1335  * more sense to compute the checksum on the full ptlrpc_body, regardless
1336  * of what size it is, but in order to keep interoperability with 1.8 we
1337  * can optionally also checksum only the first 88 bytes (caller decides). */
1338 # define ptlrpc_body_cksum_size_compat18         88
1339
1340 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg, int compat18)
1341 #else
1342 # warning "remove checksum compatibility support for b1_8"
1343 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg)
1344 #endif
1345 {
1346         switch (msg->lm_magic) {
1347         case LUSTRE_MSG_MAGIC_V2: {
1348                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1349 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
1350                 __u32 crc;
1351                 unsigned int hsize = 4;
1352                 __u32 len = compat18 ? ptlrpc_body_cksum_size_compat18 :
1353                             lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF);
1354                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1355                 cfs_crypto_hash_digest(CFS_HASH_ALG_CRC32, (unsigned char *)pb,
1356                                        len, NULL, 0, (unsigned char *)&crc,
1357                                        &hsize);
1358                 return crc;
1359 #else
1360 # warning "remove checksum compatibility support for b1_8"
1361                 __u32 crc;
1362                 unsigned int hsize = 4;
1363                 cfs_crypto_hash_digest(CFS_HASH_ALG_CRC32, (unsigned char *)pb,
1364                                    lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF),
1365                                    NULL, 0, (unsigned char *)&crc, &hsize);
1366                 return crc;
1367 #endif
1368         }
1369         default:
1370                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1371                 return 0;
1372         }
1373 }
1374
1375 void lustre_msg_set_handle(struct lustre_msg *msg, struct lustre_handle *handle)
1376 {
1377         switch (msg->lm_magic) {
1378         case LUSTRE_MSG_MAGIC_V2: {
1379                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1380                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1381                 pb->pb_handle = *handle;
1382                 return;
1383         }
1384         default:
1385                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1386         }
1387 }
1388 EXPORT_SYMBOL(lustre_msg_set_handle);
1389
1390 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type)
1391 {
1392         switch (msg->lm_magic) {
1393         case LUSTRE_MSG_MAGIC_V2: {
1394                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1395                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1396                 pb->pb_type = type;
1397                 return;
1398         }
1399         default:
1400                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1401         }
1402 }
1403 EXPORT_SYMBOL(lustre_msg_set_type);
1404
1405 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc)
1406 {
1407         switch (msg->lm_magic) {
1408         case LUSTRE_MSG_MAGIC_V2: {
1409                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1410                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1411                 pb->pb_opc = opc;
1412                 return;
1413         }
1414         default:
1415                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1416         }
1417 }
1418 EXPORT_SYMBOL(lustre_msg_set_opc);
1419
1420 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid)
1421 {
1422         switch (msg->lm_magic) {
1423         case LUSTRE_MSG_MAGIC_V2: {
1424                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1425                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1426                 pb->pb_last_xid = last_xid;
1427                 return;
1428         }
1429         default:
1430                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1431         }
1432 }
1433 EXPORT_SYMBOL(lustre_msg_set_last_xid);
1434
1435 void lustre_msg_set_last_committed(struct lustre_msg *msg, __u64 last_committed)
1436 {
1437         switch (msg->lm_magic) {
1438         case LUSTRE_MSG_MAGIC_V2: {
1439                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1440                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1441                 pb->pb_last_committed = last_committed;
1442                 return;
1443         }
1444         default:
1445                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1446         }
1447 }
1448 EXPORT_SYMBOL(lustre_msg_set_last_committed);
1449
1450 void lustre_msg_set_versions(struct lustre_msg *msg, __u64 *versions)
1451 {
1452         switch (msg->lm_magic) {
1453         case LUSTRE_MSG_MAGIC_V1:
1454                 return;
1455         case LUSTRE_MSG_MAGIC_V2: {
1456                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1457                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1458                 pb->pb_pre_versions[0] = versions[0];
1459                 pb->pb_pre_versions[1] = versions[1];
1460                 pb->pb_pre_versions[2] = versions[2];
1461                 pb->pb_pre_versions[3] = versions[3];
1462                 return;
1463         }
1464         default:
1465                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1466         }
1467 }
1468 EXPORT_SYMBOL(lustre_msg_set_versions);
1469
1470 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno)
1471 {
1472         switch (msg->lm_magic) {
1473         case LUSTRE_MSG_MAGIC_V2: {
1474                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1475                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1476                 pb->pb_transno = transno;
1477                 return;
1478         }
1479         default:
1480                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1481         }
1482 }
1483 EXPORT_SYMBOL(lustre_msg_set_transno);
1484
1485 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status)
1486 {
1487         switch (msg->lm_magic) {
1488         case LUSTRE_MSG_MAGIC_V2: {
1489                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1490                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1491                 pb->pb_status = status;
1492                 return;
1493         }
1494         default:
1495                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1496         }
1497 }
1498 EXPORT_SYMBOL(lustre_msg_set_status);
1499
1500 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt)
1501 {
1502         switch (msg->lm_magic) {
1503         case LUSTRE_MSG_MAGIC_V2: {
1504                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1505                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1506                 pb->pb_conn_cnt = conn_cnt;
1507                 return;
1508         }
1509         default:
1510                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1511         }
1512 }
1513 EXPORT_SYMBOL(lustre_msg_set_conn_cnt);
1514
1515 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout)
1516 {
1517         switch (msg->lm_magic) {
1518         case LUSTRE_MSG_MAGIC_V1:
1519                 return;
1520         case LUSTRE_MSG_MAGIC_V2: {
1521                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1522                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1523                 pb->pb_timeout = timeout;
1524                 return;
1525         }
1526         default:
1527                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1528         }
1529 }
1530
1531 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time)
1532 {
1533         switch (msg->lm_magic) {
1534         case LUSTRE_MSG_MAGIC_V1:
1535                 return;
1536         case LUSTRE_MSG_MAGIC_V2: {
1537                 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1538                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1539                 pb->pb_service_time = service_time;
1540                 return;
1541         }
1542         default:
1543                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1544         }
1545 }
1546
1547 void lustre_msg_set_jobid(struct lustre_msg *msg, char *jobid)
1548 {
1549         switch (msg->lm_magic) {
1550         case LUSTRE_MSG_MAGIC_V1:
1551                 return;
1552         case LUSTRE_MSG_MAGIC_V2: {
1553                 __u32 opc = lustre_msg_get_opc(msg);
1554                 struct ptlrpc_body *pb;
1555
1556                 /* Don't set jobid for ldlm ast RPCs, they've been shrinked.
1557                  * See the comment in ptlrpc_request_pack(). */
1558                 if (!opc || opc == LDLM_BL_CALLBACK ||
1559                     opc == LDLM_CP_CALLBACK || opc == LDLM_GL_CALLBACK)
1560                         return;
1561
1562                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1563                                        sizeof(struct ptlrpc_body));
1564                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1565
1566                 if (jobid != NULL)
1567                         memcpy(pb->pb_jobid, jobid, JOBSTATS_JOBID_SIZE);
1568                 else if (pb->pb_jobid[0] == '\0')
1569                         lustre_get_jobid(pb->pb_jobid);
1570                 return;
1571         }
1572         default:
1573                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1574         }
1575 }
1576 EXPORT_SYMBOL(lustre_msg_set_jobid);
1577
1578 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum)
1579 {
1580         switch (msg->lm_magic) {
1581         case LUSTRE_MSG_MAGIC_V1:
1582                 return;
1583         case LUSTRE_MSG_MAGIC_V2:
1584                 msg->lm_cksum = cksum;
1585                 return;
1586         default:
1587                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1588         }
1589 }
1590
1591
1592 void ptlrpc_request_set_replen(struct ptlrpc_request *req)
1593 {
1594         int count = req_capsule_filled_sizes(&req->rq_pill, RCL_SERVER);
1595
1596         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count,
1597                                          req->rq_pill.rc_area[RCL_SERVER]);
1598         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1599                 req->rq_reqmsg->lm_repsize = req->rq_replen;
1600 }
1601 EXPORT_SYMBOL(ptlrpc_request_set_replen);
1602
1603 void ptlrpc_req_set_repsize(struct ptlrpc_request *req, int count, __u32 *lens)
1604 {
1605         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens);
1606         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1607                 req->rq_reqmsg->lm_repsize = req->rq_replen;
1608 }
1609 EXPORT_SYMBOL(ptlrpc_req_set_repsize);
1610
1611 /**
1612  * Send a remote set_info_async.
1613  *
1614  * This may go from client to server or server to client.
1615  */
1616 int do_set_info_async(struct obd_import *imp,
1617                       int opcode, int version,
1618                       obd_count keylen, void *key,
1619                       obd_count vallen, void *val,
1620                       struct ptlrpc_request_set *set)
1621 {
1622         struct ptlrpc_request *req;
1623         char                  *tmp;
1624         int                    rc;
1625         ENTRY;
1626
1627         req = ptlrpc_request_alloc(imp, &RQF_OBD_SET_INFO);
1628         if (req == NULL)
1629                 RETURN(-ENOMEM);
1630
1631         req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_KEY,
1632                              RCL_CLIENT, keylen);
1633         req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_VAL,
1634                              RCL_CLIENT, vallen);
1635         rc = ptlrpc_request_pack(req, version, opcode);
1636         if (rc) {
1637                 ptlrpc_request_free(req);
1638                 RETURN(rc);
1639         }
1640
1641         tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1642         memcpy(tmp, key, keylen);
1643         tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1644         memcpy(tmp, val, vallen);
1645
1646         ptlrpc_request_set_replen(req);
1647
1648         if (set) {
1649                 ptlrpc_set_add_req(set, req);
1650                 ptlrpc_check_set(NULL, set);
1651         } else {
1652                 rc = ptlrpc_queue_wait(req);
1653                 ptlrpc_req_finished(req);
1654         }
1655
1656         RETURN(rc);
1657 }
1658 EXPORT_SYMBOL(do_set_info_async);
1659
1660 /* byte flipping routines for all wire types declared in
1661  * lustre_idl.h implemented here.
1662  */
1663 void lustre_swab_ptlrpc_body(struct ptlrpc_body *b)
1664 {
1665         __swab32s (&b->pb_type);
1666         __swab32s (&b->pb_version);
1667         __swab32s (&b->pb_opc);
1668         __swab32s (&b->pb_status);
1669         __swab64s (&b->pb_last_xid);
1670         __swab64s (&b->pb_last_seen);
1671         __swab64s (&b->pb_last_committed);
1672         __swab64s (&b->pb_transno);
1673         __swab32s (&b->pb_flags);
1674         __swab32s (&b->pb_op_flags);
1675         __swab32s (&b->pb_conn_cnt);
1676         __swab32s (&b->pb_timeout);
1677         __swab32s (&b->pb_service_time);
1678         __swab32s (&b->pb_limit);
1679         __swab64s (&b->pb_slv);
1680         __swab64s (&b->pb_pre_versions[0]);
1681         __swab64s (&b->pb_pre_versions[1]);
1682         __swab64s (&b->pb_pre_versions[2]);
1683         __swab64s (&b->pb_pre_versions[3]);
1684         CLASSERT(offsetof(typeof(*b), pb_padding) != 0);
1685         /* While we need to maintain compatibility between
1686          * clients and servers without ptlrpc_body_v2 (< 2.3)
1687          * do not swab any fields beyond pb_jobid, as we are
1688          * using this swab function for both ptlrpc_body
1689          * and ptlrpc_body_v2. */
1690         CLASSERT(offsetof(typeof(*b), pb_jobid) != 0);
1691 }
1692 EXPORT_SYMBOL(lustre_swab_ptlrpc_body);
1693
1694 void lustre_swab_connect(struct obd_connect_data *ocd)
1695 {
1696         __swab64s(&ocd->ocd_connect_flags);
1697         __swab32s(&ocd->ocd_version);
1698         __swab32s(&ocd->ocd_grant);
1699         __swab64s(&ocd->ocd_ibits_known);
1700         __swab32s(&ocd->ocd_index);
1701         __swab32s(&ocd->ocd_brw_size);
1702         /* ocd_blocksize and ocd_inodespace don't need to be swabbed because
1703          * they are 8-byte values */
1704         __swab16s(&ocd->ocd_grant_extent);
1705         __swab32s(&ocd->ocd_unused);
1706         __swab64s(&ocd->ocd_transno);
1707         __swab32s(&ocd->ocd_group);
1708         __swab32s(&ocd->ocd_cksum_types);
1709         __swab32s(&ocd->ocd_instance);
1710         /* Fields after ocd_cksum_types are only accessible by the receiver
1711          * if the corresponding flag in ocd_connect_flags is set. Accessing
1712          * any field after ocd_maxbytes on the receiver without a valid flag
1713          * may result in out-of-bound memory access and kernel oops. */
1714         if (ocd->ocd_connect_flags & OBD_CONNECT_MAX_EASIZE)
1715                 __swab32s(&ocd->ocd_max_easize);
1716         if (ocd->ocd_connect_flags & OBD_CONNECT_MAXBYTES)
1717                 __swab64s(&ocd->ocd_maxbytes);
1718         CLASSERT(offsetof(typeof(*ocd), padding1) != 0);
1719         CLASSERT(offsetof(typeof(*ocd), padding2) != 0);
1720         CLASSERT(offsetof(typeof(*ocd), padding3) != 0);
1721         CLASSERT(offsetof(typeof(*ocd), padding4) != 0);
1722         CLASSERT(offsetof(typeof(*ocd), padding5) != 0);
1723         CLASSERT(offsetof(typeof(*ocd), padding6) != 0);
1724         CLASSERT(offsetof(typeof(*ocd), padding7) != 0);
1725         CLASSERT(offsetof(typeof(*ocd), padding8) != 0);
1726         CLASSERT(offsetof(typeof(*ocd), padding9) != 0);
1727         CLASSERT(offsetof(typeof(*ocd), paddingA) != 0);
1728         CLASSERT(offsetof(typeof(*ocd), paddingB) != 0);
1729         CLASSERT(offsetof(typeof(*ocd), paddingC) != 0);
1730         CLASSERT(offsetof(typeof(*ocd), paddingD) != 0);
1731         CLASSERT(offsetof(typeof(*ocd), paddingE) != 0);
1732         CLASSERT(offsetof(typeof(*ocd), paddingF) != 0);
1733 }
1734
1735 void lustre_swab_obdo (struct obdo  *o)
1736 {
1737         __swab64s (&o->o_valid);
1738         lustre_swab_ost_id(&o->o_oi);
1739         __swab64s (&o->o_parent_seq);
1740         __swab64s (&o->o_size);
1741         __swab64s (&o->o_mtime);
1742         __swab64s (&o->o_atime);
1743         __swab64s (&o->o_ctime);
1744         __swab64s (&o->o_blocks);
1745         __swab64s (&o->o_grant);
1746         __swab32s (&o->o_blksize);
1747         __swab32s (&o->o_mode);
1748         __swab32s (&o->o_uid);
1749         __swab32s (&o->o_gid);
1750         __swab32s (&o->o_flags);
1751         __swab32s (&o->o_nlink);
1752         __swab32s (&o->o_parent_oid);
1753         __swab32s (&o->o_misc);
1754         __swab64s (&o->o_ioepoch);
1755         __swab32s (&o->o_stripe_idx);
1756         __swab32s (&o->o_parent_ver);
1757         /* o_handle is opaque */
1758         /* o_lcookie is swabbed elsewhere */
1759         __swab32s (&o->o_uid_h);
1760         __swab32s (&o->o_gid_h);
1761         __swab64s (&o->o_data_version);
1762         CLASSERT(offsetof(typeof(*o), o_padding_4) != 0);
1763         CLASSERT(offsetof(typeof(*o), o_padding_5) != 0);
1764         CLASSERT(offsetof(typeof(*o), o_padding_6) != 0);
1765
1766 }
1767 EXPORT_SYMBOL(lustre_swab_obdo);
1768
1769 void lustre_swab_obd_statfs (struct obd_statfs *os)
1770 {
1771         __swab64s (&os->os_type);
1772         __swab64s (&os->os_blocks);
1773         __swab64s (&os->os_bfree);
1774         __swab64s (&os->os_bavail);
1775         __swab64s (&os->os_files);
1776         __swab64s (&os->os_ffree);
1777         /* no need to swab os_fsid */
1778         __swab32s (&os->os_bsize);
1779         __swab32s (&os->os_namelen);
1780         __swab64s (&os->os_maxbytes);
1781         __swab32s (&os->os_state);
1782         CLASSERT(offsetof(typeof(*os), os_fprecreated) != 0);
1783         CLASSERT(offsetof(typeof(*os), os_spare2) != 0);
1784         CLASSERT(offsetof(typeof(*os), os_spare3) != 0);
1785         CLASSERT(offsetof(typeof(*os), os_spare4) != 0);
1786         CLASSERT(offsetof(typeof(*os), os_spare5) != 0);
1787         CLASSERT(offsetof(typeof(*os), os_spare6) != 0);
1788         CLASSERT(offsetof(typeof(*os), os_spare7) != 0);
1789         CLASSERT(offsetof(typeof(*os), os_spare8) != 0);
1790         CLASSERT(offsetof(typeof(*os), os_spare9) != 0);
1791 }
1792 EXPORT_SYMBOL(lustre_swab_obd_statfs);
1793
1794 void lustre_swab_obd_ioobj(struct obd_ioobj *ioo)
1795 {
1796         lustre_swab_ost_id(&ioo->ioo_oid);
1797         __swab32s(&ioo->ioo_max_brw);
1798         __swab32s(&ioo->ioo_bufcnt);
1799 }
1800 EXPORT_SYMBOL(lustre_swab_obd_ioobj);
1801
1802 void lustre_swab_niobuf_remote(struct niobuf_remote *nbr)
1803 {
1804         __swab64s(&nbr->rnb_offset);
1805         __swab32s(&nbr->rnb_len);
1806         __swab32s(&nbr->rnb_flags);
1807 }
1808 EXPORT_SYMBOL(lustre_swab_niobuf_remote);
1809
1810 void lustre_swab_ost_body (struct ost_body *b)
1811 {
1812         lustre_swab_obdo (&b->oa);
1813 }
1814 EXPORT_SYMBOL(lustre_swab_ost_body);
1815
1816 void lustre_swab_ost_last_id(obd_id *id)
1817 {
1818         __swab64s(id);
1819 }
1820 EXPORT_SYMBOL(lustre_swab_ost_last_id);
1821
1822 void lustre_swab_generic_32s(__u32 *val)
1823 {
1824         __swab32s(val);
1825 }
1826 EXPORT_SYMBOL(lustre_swab_generic_32s);
1827
1828 void lustre_swab_gl_desc(union ldlm_gl_desc *desc)
1829 {
1830         lustre_swab_lu_fid(&desc->lquota_desc.gl_id.qid_fid);
1831         __swab64s(&desc->lquota_desc.gl_flags);
1832         __swab64s(&desc->lquota_desc.gl_ver);
1833         __swab64s(&desc->lquota_desc.gl_hardlimit);
1834         __swab64s(&desc->lquota_desc.gl_softlimit);
1835         __swab64s(&desc->lquota_desc.gl_time);
1836         CLASSERT(offsetof(typeof(desc->lquota_desc), gl_pad2) != 0);
1837 }
1838
1839 void lustre_swab_ost_lvb_v1(struct ost_lvb_v1 *lvb)
1840 {
1841         __swab64s(&lvb->lvb_size);
1842         __swab64s(&lvb->lvb_mtime);
1843         __swab64s(&lvb->lvb_atime);
1844         __swab64s(&lvb->lvb_ctime);
1845         __swab64s(&lvb->lvb_blocks);
1846 }
1847 EXPORT_SYMBOL(lustre_swab_ost_lvb_v1);
1848
1849 void lustre_swab_ost_lvb(struct ost_lvb *lvb)
1850 {
1851         __swab64s(&lvb->lvb_size);
1852         __swab64s(&lvb->lvb_mtime);
1853         __swab64s(&lvb->lvb_atime);
1854         __swab64s(&lvb->lvb_ctime);
1855         __swab64s(&lvb->lvb_blocks);
1856         __swab32s(&lvb->lvb_mtime_ns);
1857         __swab32s(&lvb->lvb_atime_ns);
1858         __swab32s(&lvb->lvb_ctime_ns);
1859         __swab32s(&lvb->lvb_padding);
1860 }
1861 EXPORT_SYMBOL(lustre_swab_ost_lvb);
1862
1863 void lustre_swab_lquota_lvb(struct lquota_lvb *lvb)
1864 {
1865         __swab64s(&lvb->lvb_flags);
1866         __swab64s(&lvb->lvb_id_may_rel);
1867         __swab64s(&lvb->lvb_id_rel);
1868         __swab64s(&lvb->lvb_id_qunit);
1869         __swab64s(&lvb->lvb_pad1);
1870 }
1871 EXPORT_SYMBOL(lustre_swab_lquota_lvb);
1872
1873 void lustre_swab_mdt_body (struct mdt_body *b)
1874 {
1875         lustre_swab_lu_fid(&b->mbo_fid1);
1876         lustre_swab_lu_fid(&b->mbo_fid2);
1877         /* handle is opaque */
1878         __swab64s(&b->mbo_valid);
1879         __swab64s(&b->mbo_size);
1880         __swab64s(&b->mbo_mtime);
1881         __swab64s(&b->mbo_atime);
1882         __swab64s(&b->mbo_ctime);
1883         __swab64s(&b->mbo_blocks);
1884         __swab64s(&b->mbo_ioepoch);
1885         __swab64s(&b->mbo_t_state);
1886         __swab32s(&b->mbo_fsuid);
1887         __swab32s(&b->mbo_fsgid);
1888         __swab32s(&b->mbo_capability);
1889         __swab32s(&b->mbo_mode);
1890         __swab32s(&b->mbo_uid);
1891         __swab32s(&b->mbo_gid);
1892         __swab32s(&b->mbo_flags);
1893         __swab32s(&b->mbo_rdev);
1894         __swab32s(&b->mbo_nlink);
1895         CLASSERT(offsetof(typeof(*b), mbo_unused2) != 0);
1896         __swab32s(&b->mbo_suppgid);
1897         __swab32s(&b->mbo_eadatasize);
1898         __swab32s(&b->mbo_aclsize);
1899         __swab32s(&b->mbo_max_mdsize);
1900         __swab32s(&b->mbo_max_cookiesize);
1901         __swab32s(&b->mbo_uid_h);
1902         __swab32s(&b->mbo_gid_h);
1903         CLASSERT(offsetof(typeof(*b), mbo_padding_5) != 0);
1904 }
1905 EXPORT_SYMBOL(lustre_swab_mdt_body);
1906
1907 void lustre_swab_mdt_ioepoch (struct mdt_ioepoch *b)
1908 {
1909         /* handle is opaque */
1910          __swab64s (&b->ioepoch);
1911          __swab32s (&b->flags);
1912          CLASSERT(offsetof(typeof(*b), padding) != 0);
1913 }
1914 EXPORT_SYMBOL(lustre_swab_mdt_ioepoch);
1915
1916 void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
1917 {
1918         int i;
1919         __swab32s(&mti->mti_lustre_ver);
1920         __swab32s(&mti->mti_stripe_index);
1921         __swab32s(&mti->mti_config_ver);
1922         __swab32s(&mti->mti_flags);
1923         __swab32s(&mti->mti_instance);
1924         __swab32s(&mti->mti_nid_count);
1925         CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
1926         for (i = 0; i < MTI_NIDS_MAX; i++)
1927                 __swab64s(&mti->mti_nids[i]);
1928 }
1929 EXPORT_SYMBOL(lustre_swab_mgs_target_info);
1930
1931 void lustre_swab_mgs_nidtbl_entry(struct mgs_nidtbl_entry *entry)
1932 {
1933         int i;
1934
1935         __swab64s(&entry->mne_version);
1936         __swab32s(&entry->mne_instance);
1937         __swab32s(&entry->mne_index);
1938         __swab32s(&entry->mne_length);
1939
1940         /* mne_nid_(count|type) must be one byte size because we're gonna
1941          * access it w/o swapping. */
1942         CLASSERT(sizeof(entry->mne_nid_count) == sizeof(__u8));
1943         CLASSERT(sizeof(entry->mne_nid_type) == sizeof(__u8));
1944
1945         /* remove this assertion if ipv6 is supported. */
1946         LASSERT(entry->mne_nid_type == 0);
1947         for (i = 0; i < entry->mne_nid_count; i++) {
1948                 CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
1949                 __swab64s(&entry->u.nids[i]);
1950         }
1951 }
1952 EXPORT_SYMBOL(lustre_swab_mgs_nidtbl_entry);
1953
1954 void lustre_swab_mgs_config_body(struct mgs_config_body *body)
1955 {
1956         __swab64s(&body->mcb_offset);
1957         __swab32s(&body->mcb_units);
1958         __swab16s(&body->mcb_type);
1959 }
1960 EXPORT_SYMBOL(lustre_swab_mgs_config_body);
1961
1962 void lustre_swab_mgs_config_res(struct mgs_config_res *body)
1963 {
1964         __swab64s(&body->mcr_offset);
1965         __swab64s(&body->mcr_size);
1966 }
1967 EXPORT_SYMBOL(lustre_swab_mgs_config_res);
1968
1969 static void lustre_swab_obd_dqinfo (struct obd_dqinfo *i)
1970 {
1971         __swab64s (&i->dqi_bgrace);
1972         __swab64s (&i->dqi_igrace);
1973         __swab32s (&i->dqi_flags);
1974         __swab32s (&i->dqi_valid);
1975 }
1976
1977 static void lustre_swab_obd_dqblk (struct obd_dqblk *b)
1978 {
1979         __swab64s (&b->dqb_ihardlimit);
1980         __swab64s (&b->dqb_isoftlimit);
1981         __swab64s (&b->dqb_curinodes);
1982         __swab64s (&b->dqb_bhardlimit);
1983         __swab64s (&b->dqb_bsoftlimit);
1984         __swab64s (&b->dqb_curspace);
1985         __swab64s (&b->dqb_btime);
1986         __swab64s (&b->dqb_itime);
1987         __swab32s (&b->dqb_valid);
1988         CLASSERT(offsetof(typeof(*b), dqb_padding) != 0);
1989 }
1990
1991 void lustre_swab_obd_quotactl (struct obd_quotactl *q)
1992 {
1993         __swab32s (&q->qc_cmd);
1994         __swab32s (&q->qc_type);
1995         __swab32s (&q->qc_id);
1996         __swab32s (&q->qc_stat);
1997         lustre_swab_obd_dqinfo (&q->qc_dqinfo);
1998         lustre_swab_obd_dqblk (&q->qc_dqblk);
1999 }
2000 EXPORT_SYMBOL(lustre_swab_obd_quotactl);
2001
2002 void lustre_swab_mdt_remote_perm (struct mdt_remote_perm *p)
2003 {
2004         __swab32s (&p->rp_uid);
2005         __swab32s (&p->rp_gid);
2006         __swab32s (&p->rp_fsuid);
2007         __swab32s (&p->rp_fsuid_h);
2008         __swab32s (&p->rp_fsgid);
2009         __swab32s (&p->rp_fsgid_h);
2010         __swab32s (&p->rp_access_perm);
2011         __swab32s (&p->rp_padding);
2012 };
2013 EXPORT_SYMBOL(lustre_swab_mdt_remote_perm);
2014
2015 void lustre_swab_fid2path(struct getinfo_fid2path *gf)
2016 {
2017         lustre_swab_lu_fid(&gf->gf_fid);
2018         __swab64s(&gf->gf_recno);
2019         __swab32s(&gf->gf_linkno);
2020         __swab32s(&gf->gf_pathlen);
2021 }
2022 EXPORT_SYMBOL(lustre_swab_fid2path);
2023
2024 void lustre_swab_fiemap_extent(struct ll_fiemap_extent *fm_extent)
2025 {
2026         __swab64s(&fm_extent->fe_logical);
2027         __swab64s(&fm_extent->fe_physical);
2028         __swab64s(&fm_extent->fe_length);
2029         __swab32s(&fm_extent->fe_flags);
2030         __swab32s(&fm_extent->fe_device);
2031 }
2032
2033 void lustre_swab_fiemap(struct ll_user_fiemap *fiemap)
2034 {
2035         int i;
2036
2037         __swab64s(&fiemap->fm_start);
2038         __swab64s(&fiemap->fm_length);
2039         __swab32s(&fiemap->fm_flags);
2040         __swab32s(&fiemap->fm_mapped_extents);
2041         __swab32s(&fiemap->fm_extent_count);
2042         __swab32s(&fiemap->fm_reserved);
2043
2044         for (i = 0; i < fiemap->fm_mapped_extents; i++)
2045                 lustre_swab_fiemap_extent(&fiemap->fm_extents[i]);
2046 }
2047 EXPORT_SYMBOL(lustre_swab_fiemap);
2048
2049 void lustre_swab_idx_info(struct idx_info *ii)
2050 {
2051         __swab32s(&ii->ii_magic);
2052         __swab32s(&ii->ii_flags);
2053         __swab16s(&ii->ii_count);
2054         __swab32s(&ii->ii_attrs);
2055         lustre_swab_lu_fid(&ii->ii_fid);
2056         __swab64s(&ii->ii_version);
2057         __swab64s(&ii->ii_hash_start);
2058         __swab64s(&ii->ii_hash_end);
2059         __swab16s(&ii->ii_keysize);
2060         __swab16s(&ii->ii_recsize);
2061 }
2062
2063 void lustre_swab_lip_header(struct lu_idxpage *lip)
2064 {
2065         /* swab header */
2066         __swab32s(&lip->lip_magic);
2067         __swab16s(&lip->lip_flags);
2068         __swab16s(&lip->lip_nr);
2069 }
2070 EXPORT_SYMBOL(lustre_swab_lip_header);
2071
2072 void lustre_swab_mdt_rec_reint (struct mdt_rec_reint *rr)
2073 {
2074         __swab32s(&rr->rr_opcode);
2075         __swab32s(&rr->rr_cap);
2076         __swab32s(&rr->rr_fsuid);
2077         /* rr_fsuid_h is unused */
2078         __swab32s(&rr->rr_fsgid);
2079         /* rr_fsgid_h is unused */
2080         __swab32s(&rr->rr_suppgid1);
2081         /* rr_suppgid1_h is unused */
2082         __swab32s(&rr->rr_suppgid2);
2083         /* rr_suppgid2_h is unused */
2084         lustre_swab_lu_fid(&rr->rr_fid1);
2085         lustre_swab_lu_fid(&rr->rr_fid2);
2086         __swab64s(&rr->rr_mtime);
2087         __swab64s(&rr->rr_atime);
2088         __swab64s(&rr->rr_ctime);
2089         __swab64s(&rr->rr_size);
2090         __swab64s(&rr->rr_blocks);
2091         __swab32s(&rr->rr_bias);
2092         __swab32s(&rr->rr_mode);
2093         __swab32s(&rr->rr_flags);
2094         __swab32s(&rr->rr_flags_h);
2095         __swab32s(&rr->rr_umask);
2096
2097         CLASSERT(offsetof(typeof(*rr), rr_padding_4) != 0);
2098 };
2099 EXPORT_SYMBOL(lustre_swab_mdt_rec_reint);
2100
2101 void lustre_swab_lov_desc (struct lov_desc *ld)
2102 {
2103         __swab32s (&ld->ld_tgt_count);
2104         __swab32s (&ld->ld_active_tgt_count);
2105         __swab32s (&ld->ld_default_stripe_count);
2106         __swab32s (&ld->ld_pattern);
2107         __swab64s (&ld->ld_default_stripe_size);
2108         __swab64s (&ld->ld_default_stripe_offset);
2109         __swab32s (&ld->ld_qos_maxage);
2110         /* uuid endian insensitive */
2111 }
2112 EXPORT_SYMBOL(lustre_swab_lov_desc);
2113
2114 void lustre_swab_lmv_desc (struct lmv_desc *ld)
2115 {
2116         __swab32s (&ld->ld_tgt_count);
2117         __swab32s (&ld->ld_active_tgt_count);
2118         __swab32s (&ld->ld_default_stripe_count);
2119         __swab32s (&ld->ld_pattern);
2120         __swab64s (&ld->ld_default_hash_size);
2121         __swab32s (&ld->ld_qos_maxage);
2122         /* uuid endian insensitive */
2123 }
2124
2125 /* This structure is always in little-endian */
2126 static void lustre_swab_lmv_mds_md_v1(struct lmv_mds_md_v1 *lmm1)
2127 {
2128         int i;
2129
2130         __swab32s(&lmm1->lmv_magic);
2131         __swab32s(&lmm1->lmv_stripe_count);
2132         __swab32s(&lmm1->lmv_master_mdt_index);
2133         __swab32s(&lmm1->lmv_hash_type);
2134         __swab32s(&lmm1->lmv_layout_version);
2135         for (i = 0; i < lmm1->lmv_stripe_count; i++)
2136                 lustre_swab_lu_fid(&lmm1->lmv_stripe_fids[i]);
2137 }
2138
2139 void lustre_swab_lmv_mds_md(union lmv_mds_md *lmm)
2140 {
2141         switch (lmm->lmv_magic) {
2142         case LMV_MAGIC_V1:
2143                 lustre_swab_lmv_mds_md_v1(&lmm->lmv_md_v1);
2144                 break;
2145         default:
2146                 break;
2147         }
2148 }
2149 EXPORT_SYMBOL(lustre_swab_lmv_mds_md);
2150
2151 void lustre_swab_lmv_user_md(struct lmv_user_md *lum)
2152 {
2153         int i;
2154
2155         __swab32s(&lum->lum_magic);
2156         __swab32s(&lum->lum_stripe_count);
2157         __swab32s(&lum->lum_stripe_offset);
2158         __swab32s(&lum->lum_hash_type);
2159         __swab32s(&lum->lum_type);
2160         CLASSERT(offsetof(typeof(*lum), lum_padding1) != 0);
2161         for (i = 0; i < lum->lum_stripe_count; i++) {
2162                 __swab32s(&lum->lum_objects[i].lum_mds);
2163                 lustre_swab_lu_fid(&lum->lum_objects[i].lum_fid);
2164         }
2165
2166 }
2167 EXPORT_SYMBOL(lustre_swab_lmv_user_md);
2168
2169 static void print_lum (struct lov_user_md *lum)
2170 {
2171         CDEBUG(D_OTHER, "lov_user_md %p:\n", lum);
2172         CDEBUG(D_OTHER, "\tlmm_magic: %#x\n", lum->lmm_magic);
2173         CDEBUG(D_OTHER, "\tlmm_pattern: %#x\n", lum->lmm_pattern);
2174         CDEBUG(D_OTHER, "\tlmm_object_id: "LPU64"\n", lmm_oi_id(&lum->lmm_oi));
2175         CDEBUG(D_OTHER, "\tlmm_object_gr: "LPU64"\n", lmm_oi_seq(&lum->lmm_oi));
2176         CDEBUG(D_OTHER, "\tlmm_stripe_size: %#x\n", lum->lmm_stripe_size);
2177         CDEBUG(D_OTHER, "\tlmm_stripe_count: %#x\n", lum->lmm_stripe_count);
2178         CDEBUG(D_OTHER, "\tlmm_stripe_offset/lmm_layout_gen: %#x\n",
2179                         lum->lmm_stripe_offset);
2180 }
2181
2182 static void lustre_swab_lmm_oi(struct ost_id *oi)
2183 {
2184         __swab64s(&oi->oi.oi_id);
2185         __swab64s(&oi->oi.oi_seq);
2186 }
2187
2188 static void lustre_swab_lov_user_md_common(struct lov_user_md_v1 *lum)
2189 {
2190         ENTRY;
2191         __swab32s(&lum->lmm_magic);
2192         __swab32s(&lum->lmm_pattern);
2193         lustre_swab_lmm_oi(&lum->lmm_oi);
2194         __swab32s(&lum->lmm_stripe_size);
2195         __swab16s(&lum->lmm_stripe_count);
2196         __swab16s(&lum->lmm_stripe_offset);
2197         print_lum(lum);
2198         EXIT;
2199 }
2200
2201 void lustre_swab_lov_user_md_v1(struct lov_user_md_v1 *lum)
2202 {
2203         ENTRY;
2204         CDEBUG(D_IOCTL, "swabbing lov_user_md v1\n");
2205         lustre_swab_lov_user_md_common(lum);
2206         EXIT;
2207 }
2208 EXPORT_SYMBOL(lustre_swab_lov_user_md_v1);
2209
2210 void lustre_swab_lov_user_md_v3(struct lov_user_md_v3 *lum)
2211 {
2212         ENTRY;
2213         CDEBUG(D_IOCTL, "swabbing lov_user_md v3\n");
2214         lustre_swab_lov_user_md_common((struct lov_user_md_v1 *)lum);
2215         /* lmm_pool_name nothing to do with char */
2216         EXIT;
2217 }
2218 EXPORT_SYMBOL(lustre_swab_lov_user_md_v3);
2219
2220 void lustre_swab_lov_mds_md(struct lov_mds_md *lmm)
2221 {
2222         ENTRY;
2223         CDEBUG(D_IOCTL, "swabbing lov_mds_md\n");
2224         __swab32s(&lmm->lmm_magic);
2225         __swab32s(&lmm->lmm_pattern);
2226         lustre_swab_lmm_oi(&lmm->lmm_oi);
2227         __swab32s(&lmm->lmm_stripe_size);
2228         __swab16s(&lmm->lmm_stripe_count);
2229         __swab16s(&lmm->lmm_layout_gen);
2230         EXIT;
2231 }
2232 EXPORT_SYMBOL(lustre_swab_lov_mds_md);
2233
2234 void lustre_swab_lov_user_md_objects(struct lov_user_ost_data *lod,
2235                                      int stripe_count)
2236 {
2237         int i;
2238         ENTRY;
2239         for (i = 0; i < stripe_count; i++) {
2240                 lustre_swab_ost_id(&(lod[i].l_ost_oi));
2241                 __swab32s(&(lod[i].l_ost_gen));
2242                 __swab32s(&(lod[i].l_ost_idx));
2243         }
2244         EXIT;
2245 }
2246 EXPORT_SYMBOL(lustre_swab_lov_user_md_objects);
2247
2248 void lustre_swab_ldlm_res_id (struct ldlm_res_id *id)
2249 {
2250         int  i;
2251
2252         for (i = 0; i < RES_NAME_SIZE; i++)
2253                 __swab64s (&id->name[i]);
2254 }
2255 EXPORT_SYMBOL(lustre_swab_ldlm_res_id);
2256
2257 void lustre_swab_ldlm_policy_data (ldlm_wire_policy_data_t *d)
2258 {
2259         /* the lock data is a union and the first two fields are always an
2260          * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
2261          * data the same way. */
2262         __swab64s(&d->l_extent.start);
2263         __swab64s(&d->l_extent.end);
2264         __swab64s(&d->l_extent.gid);
2265         __swab64s(&d->l_flock.lfw_owner);
2266         __swab32s(&d->l_flock.lfw_pid);
2267 }
2268 EXPORT_SYMBOL(lustre_swab_ldlm_policy_data);
2269
2270 void lustre_swab_ldlm_intent (struct ldlm_intent *i)
2271 {
2272         __swab64s (&i->opc);
2273 }
2274 EXPORT_SYMBOL(lustre_swab_ldlm_intent);
2275
2276 void lustre_swab_ldlm_resource_desc (struct ldlm_resource_desc *r)
2277 {
2278         __swab32s (&r->lr_type);
2279         CLASSERT(offsetof(typeof(*r), lr_padding) != 0);
2280         lustre_swab_ldlm_res_id (&r->lr_name);
2281 }
2282 EXPORT_SYMBOL(lustre_swab_ldlm_resource_desc);
2283
2284 void lustre_swab_ldlm_lock_desc (struct ldlm_lock_desc *l)
2285 {
2286         lustre_swab_ldlm_resource_desc (&l->l_resource);
2287         __swab32s (&l->l_req_mode);
2288         __swab32s (&l->l_granted_mode);
2289         lustre_swab_ldlm_policy_data (&l->l_policy_data);
2290 }
2291 EXPORT_SYMBOL(lustre_swab_ldlm_lock_desc);
2292
2293 void lustre_swab_ldlm_request (struct ldlm_request *rq)
2294 {
2295         __swab32s (&rq->lock_flags);
2296         lustre_swab_ldlm_lock_desc (&rq->lock_desc);
2297         __swab32s (&rq->lock_count);
2298         /* lock_handle[] opaque */
2299 }
2300 EXPORT_SYMBOL(lustre_swab_ldlm_request);
2301
2302 void lustre_swab_ldlm_reply (struct ldlm_reply *r)
2303 {
2304         __swab32s (&r->lock_flags);
2305         CLASSERT(offsetof(typeof(*r), lock_padding) != 0);
2306         lustre_swab_ldlm_lock_desc (&r->lock_desc);
2307         /* lock_handle opaque */
2308         __swab64s (&r->lock_policy_res1);
2309         __swab64s (&r->lock_policy_res2);
2310 }
2311 EXPORT_SYMBOL(lustre_swab_ldlm_reply);
2312
2313 void lustre_swab_quota_body(struct quota_body *b)
2314 {
2315         lustre_swab_lu_fid(&b->qb_fid);
2316         lustre_swab_lu_fid((struct lu_fid *)&b->qb_id);
2317         __swab32s(&b->qb_flags);
2318         __swab64s(&b->qb_count);
2319         __swab64s(&b->qb_usage);
2320         __swab64s(&b->qb_slv_ver);
2321 }
2322
2323 /* Dump functions */
2324 void dump_ioo(struct obd_ioobj *ioo)
2325 {
2326         CDEBUG(D_RPCTRACE,
2327                "obd_ioobj: ioo_oid="DOSTID", ioo_max_brw=%#x, "
2328                "ioo_bufct=%d\n", POSTID(&ioo->ioo_oid), ioo->ioo_max_brw,
2329                ioo->ioo_bufcnt);
2330 }
2331 EXPORT_SYMBOL(dump_ioo);
2332
2333 void dump_rniobuf(struct niobuf_remote *nb)
2334 {
2335         CDEBUG(D_RPCTRACE, "niobuf_remote: offset="LPU64", len=%d, flags=%x\n",
2336                nb->rnb_offset, nb->rnb_len, nb->rnb_flags);
2337 }
2338 EXPORT_SYMBOL(dump_rniobuf);
2339
2340 void dump_obdo(struct obdo *oa)
2341 {
2342         __u32 valid = oa->o_valid;
2343
2344         CDEBUG(D_RPCTRACE, "obdo: o_valid = %08x\n", valid);
2345         if (valid & OBD_MD_FLID)
2346                 CDEBUG(D_RPCTRACE, "obdo: id = "DOSTID"\n", POSTID(&oa->o_oi));
2347         if (valid & OBD_MD_FLFID)
2348                 CDEBUG(D_RPCTRACE, "obdo: o_parent_seq = "LPX64"\n",
2349                        oa->o_parent_seq);
2350         if (valid & OBD_MD_FLSIZE)
2351                 CDEBUG(D_RPCTRACE, "obdo: o_size = "LPD64"\n", oa->o_size);
2352         if (valid & OBD_MD_FLMTIME)
2353                 CDEBUG(D_RPCTRACE, "obdo: o_mtime = "LPD64"\n", oa->o_mtime);
2354         if (valid & OBD_MD_FLATIME)
2355                 CDEBUG(D_RPCTRACE, "obdo: o_atime = "LPD64"\n", oa->o_atime);
2356         if (valid & OBD_MD_FLCTIME)
2357                 CDEBUG(D_RPCTRACE, "obdo: o_ctime = "LPD64"\n", oa->o_ctime);
2358         if (valid & OBD_MD_FLBLOCKS)   /* allocation of space */
2359                 CDEBUG(D_RPCTRACE, "obdo: o_blocks = "LPD64"\n", oa->o_blocks);
2360         if (valid & OBD_MD_FLGRANT)
2361                 CDEBUG(D_RPCTRACE, "obdo: o_grant = "LPD64"\n", oa->o_grant);
2362         if (valid & OBD_MD_FLBLKSZ)
2363                 CDEBUG(D_RPCTRACE, "obdo: o_blksize = %d\n", oa->o_blksize);
2364         if (valid & (OBD_MD_FLTYPE | OBD_MD_FLMODE))
2365                 CDEBUG(D_RPCTRACE, "obdo: o_mode = %o\n",
2366                        oa->o_mode & ((valid & OBD_MD_FLTYPE ?  S_IFMT : 0) |
2367                                      (valid & OBD_MD_FLMODE ? ~S_IFMT : 0)));
2368         if (valid & OBD_MD_FLUID)
2369                 CDEBUG(D_RPCTRACE, "obdo: o_uid = %u\n", oa->o_uid);
2370         if (valid & OBD_MD_FLUID)
2371                 CDEBUG(D_RPCTRACE, "obdo: o_uid_h = %u\n", oa->o_uid_h);
2372         if (valid & OBD_MD_FLGID)
2373                 CDEBUG(D_RPCTRACE, "obdo: o_gid = %u\n", oa->o_gid);
2374         if (valid & OBD_MD_FLGID)
2375                 CDEBUG(D_RPCTRACE, "obdo: o_gid_h = %u\n", oa->o_gid_h);
2376         if (valid & OBD_MD_FLFLAGS)
2377                 CDEBUG(D_RPCTRACE, "obdo: o_flags = %x\n", oa->o_flags);
2378         if (valid & OBD_MD_FLNLINK)
2379                 CDEBUG(D_RPCTRACE, "obdo: o_nlink = %u\n", oa->o_nlink);
2380         else if (valid & OBD_MD_FLCKSUM)
2381                 CDEBUG(D_RPCTRACE, "obdo: o_checksum (o_nlink) = %u\n",
2382                        oa->o_nlink);
2383         if (valid & OBD_MD_FLGENER)
2384                 CDEBUG(D_RPCTRACE, "obdo: o_parent_oid = %x\n",
2385                        oa->o_parent_oid);
2386         if (valid & OBD_MD_FLEPOCH)
2387                 CDEBUG(D_RPCTRACE, "obdo: o_ioepoch = "LPD64"\n",
2388                        oa->o_ioepoch);
2389         if (valid & OBD_MD_FLFID) {
2390                 CDEBUG(D_RPCTRACE, "obdo: o_stripe_idx = %u\n",
2391                        oa->o_stripe_idx);
2392                 CDEBUG(D_RPCTRACE, "obdo: o_parent_ver = %x\n",
2393                        oa->o_parent_ver);
2394         }
2395         if (valid & OBD_MD_FLHANDLE)
2396                 CDEBUG(D_RPCTRACE, "obdo: o_handle = "LPD64"\n",
2397                        oa->o_handle.cookie);
2398         if (valid & OBD_MD_FLCOOKIE)
2399                 CDEBUG(D_RPCTRACE, "obdo: o_lcookie = "
2400                        "(llog_cookie dumping not yet implemented)\n");
2401 }
2402 EXPORT_SYMBOL(dump_obdo);
2403
2404 void dump_ost_body(struct ost_body *ob)
2405 {
2406         dump_obdo(&ob->oa);
2407 }
2408 EXPORT_SYMBOL(dump_ost_body);
2409
2410 void dump_rcs(__u32 *rc)
2411 {
2412         CDEBUG(D_RPCTRACE, "rmf_rcs: %d\n", *rc);
2413 }
2414 EXPORT_SYMBOL(dump_rcs);
2415
2416 static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2417 {
2418         LASSERT(req->rq_reqmsg);
2419
2420         switch (req->rq_reqmsg->lm_magic) {
2421         case LUSTRE_MSG_MAGIC_V2:
2422                 return lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
2423         default:
2424                 CERROR("bad lustre msg magic: %#08X\n",
2425                        req->rq_reqmsg->lm_magic);
2426         }
2427         return 0;
2428 }
2429
2430 static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2431 {
2432         LASSERT(req->rq_repmsg);
2433
2434         switch (req->rq_repmsg->lm_magic) {
2435         case LUSTRE_MSG_MAGIC_V2:
2436                 return lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
2437         default:
2438                 /* uninitialized yet */
2439                 return 0;
2440         }
2441 }
2442
2443 void _debug_req(struct ptlrpc_request *req,
2444                 struct libcfs_debug_msg_data *msgdata,
2445                 const char *fmt, ... )
2446 {
2447         int req_ok = req->rq_reqmsg != NULL;
2448         int rep_ok = req->rq_repmsg != NULL;
2449         lnet_nid_t nid = LNET_NID_ANY;
2450         va_list args;
2451
2452         if (ptlrpc_req_need_swab(req)) {
2453                 req_ok = req_ok && req_ptlrpc_body_swabbed(req);
2454                 rep_ok = rep_ok && rep_ptlrpc_body_swabbed(req);
2455         }
2456
2457         if (req->rq_import && req->rq_import->imp_connection)
2458                 nid = req->rq_import->imp_connection->c_peer.nid;
2459         else if (req->rq_export && req->rq_export->exp_connection)
2460                 nid = req->rq_export->exp_connection->c_peer.nid;
2461
2462         va_start(args, fmt);
2463         libcfs_debug_vmsg2(msgdata, fmt, args,
2464                            " req@%p x"LPU64"/t"LPD64"("LPD64") o%d->%s@%s:%d/%d"
2465                            " lens %d/%d e %d to %d dl "CFS_TIME_T" ref %d "
2466                            "fl "REQ_FLAGS_FMT"/%x/%x rc %d/%d\n",
2467                            req, req->rq_xid, req->rq_transno,
2468                            req_ok ? lustre_msg_get_transno(req->rq_reqmsg) : 0,
2469                            req_ok ? lustre_msg_get_opc(req->rq_reqmsg) : -1,
2470                            req->rq_import ?
2471                                 req->rq_import->imp_obd->obd_name :
2472                                 req->rq_export ?
2473                                         req->rq_export->exp_client_uuid.uuid :
2474                                         "<?>",
2475                            libcfs_nid2str(nid),
2476                            req->rq_request_portal, req->rq_reply_portal,
2477                            req->rq_reqlen, req->rq_replen,
2478                            req->rq_early_count, req->rq_timedout,
2479                            req->rq_deadline,
2480                            atomic_read(&req->rq_refcount),
2481                            DEBUG_REQ_FLAGS(req),
2482                            req_ok ? lustre_msg_get_flags(req->rq_reqmsg) : -1,
2483                            rep_ok ? lustre_msg_get_flags(req->rq_repmsg) : -1,
2484                            req->rq_status,
2485                            rep_ok ? lustre_msg_get_status(req->rq_repmsg) : -1);
2486         va_end(args);
2487 }
2488 EXPORT_SYMBOL(_debug_req);
2489
2490 void lustre_swab_lustre_capa(struct lustre_capa *c)
2491 {
2492         lustre_swab_lu_fid(&c->lc_fid);
2493         __swab64s (&c->lc_opc);
2494         __swab64s (&c->lc_uid);
2495         __swab64s (&c->lc_gid);
2496         __swab32s (&c->lc_flags);
2497         __swab32s (&c->lc_keyid);
2498         __swab32s (&c->lc_timeout);
2499         __swab32s (&c->lc_expiry);
2500 }
2501 EXPORT_SYMBOL(lustre_swab_lustre_capa);
2502
2503 void lustre_swab_lustre_capa_key(struct lustre_capa_key *k)
2504 {
2505         __swab64s (&k->lk_seq);
2506         __swab32s (&k->lk_keyid);
2507         CLASSERT(offsetof(typeof(*k), lk_padding) != 0);
2508 }
2509 EXPORT_SYMBOL(lustre_swab_lustre_capa_key);
2510
2511 void lustre_swab_hsm_user_state(struct hsm_user_state *state)
2512 {
2513         __swab32s(&state->hus_states);
2514         __swab32s(&state->hus_archive_id);
2515 }
2516 EXPORT_SYMBOL(lustre_swab_hsm_user_state);
2517
2518 void lustre_swab_hsm_state_set(struct hsm_state_set *hss)
2519 {
2520         __swab32s(&hss->hss_valid);
2521         __swab64s(&hss->hss_setmask);
2522         __swab64s(&hss->hss_clearmask);
2523         __swab32s(&hss->hss_archive_id);
2524 }
2525 EXPORT_SYMBOL(lustre_swab_hsm_state_set);
2526
2527 void lustre_swab_hsm_extent(struct hsm_extent *extent)
2528 {
2529         __swab64s(&extent->offset);
2530         __swab64s(&extent->length);
2531 }
2532
2533 void lustre_swab_hsm_current_action(struct hsm_current_action *action)
2534 {
2535         __swab32s(&action->hca_state);
2536         __swab32s(&action->hca_action);
2537         lustre_swab_hsm_extent(&action->hca_location);
2538 }
2539 EXPORT_SYMBOL(lustre_swab_hsm_current_action);
2540
2541 void lustre_swab_hsm_user_item(struct hsm_user_item *hui)
2542 {
2543         lustre_swab_lu_fid(&hui->hui_fid);
2544         lustre_swab_hsm_extent(&hui->hui_extent);
2545 }
2546 EXPORT_SYMBOL(lustre_swab_hsm_user_item);
2547
2548 void lustre_swab_layout_intent(struct layout_intent *li)
2549 {
2550         __swab32s(&li->li_opc);
2551         __swab32s(&li->li_flags);
2552         __swab64s(&li->li_start);
2553         __swab64s(&li->li_end);
2554 }
2555 EXPORT_SYMBOL(lustre_swab_layout_intent);
2556
2557 void lustre_swab_hsm_progress_kernel(struct hsm_progress_kernel *hpk)
2558 {
2559         lustre_swab_lu_fid(&hpk->hpk_fid);
2560         __swab64s(&hpk->hpk_cookie);
2561         __swab64s(&hpk->hpk_extent.offset);
2562         __swab64s(&hpk->hpk_extent.length);
2563         __swab16s(&hpk->hpk_flags);
2564         __swab16s(&hpk->hpk_errval);
2565 }
2566 EXPORT_SYMBOL(lustre_swab_hsm_progress_kernel);
2567
2568 void lustre_swab_hsm_request(struct hsm_request *hr)
2569 {
2570         __swab32s(&hr->hr_action);
2571         __swab32s(&hr->hr_archive_id);
2572         __swab64s(&hr->hr_flags);
2573         __swab32s(&hr->hr_itemcount);
2574         __swab32s(&hr->hr_data_len);
2575 }
2576 EXPORT_SYMBOL(lustre_swab_hsm_request);
2577
2578 void lustre_swab_object_update(struct object_update *ou)
2579 {
2580         struct object_update_param *param;
2581         int     i;
2582
2583         __swab16s(&ou->ou_type);
2584         __swab16s(&ou->ou_params_count);
2585         __swab32s(&ou->ou_master_index);
2586         __swab32s(&ou->ou_flags);
2587         __swab32s(&ou->ou_padding1);
2588         __swab64s(&ou->ou_batchid);
2589         lustre_swab_lu_fid(&ou->ou_fid);
2590         param = &ou->ou_params[0];
2591         for (i = 0; i < ou->ou_params_count; i++) {
2592                 __swab16s(&param->oup_len);
2593                 __swab16s(&param->oup_padding);
2594                 __swab32s(&param->oup_padding2);
2595                 param = (struct object_update_param *)((char *)param +
2596                          object_update_param_size(param));
2597         }
2598 }
2599 EXPORT_SYMBOL(lustre_swab_object_update);
2600
2601 void lustre_swab_object_update_request(struct object_update_request *our)
2602 {
2603         int i;
2604         __swab32s(&our->ourq_magic);
2605         __swab16s(&our->ourq_count);
2606         __swab16s(&our->ourq_padding);
2607         for (i = 0; i < our->ourq_count; i++) {
2608                 struct object_update *ou;
2609
2610                 ou = object_update_request_get(our, i, NULL);
2611                 if (ou == NULL)
2612                         return;
2613                 lustre_swab_object_update(ou);
2614         }
2615 }
2616 EXPORT_SYMBOL(lustre_swab_object_update_request);
2617
2618 void lustre_swab_object_update_result(struct object_update_result *our)
2619 {
2620         __swab32s(&our->our_rc);
2621         __swab16s(&our->our_datalen);
2622         __swab16s(&our->our_padding);
2623 }
2624 EXPORT_SYMBOL(lustre_swab_object_update_result);
2625
2626 void lustre_swab_object_update_reply(struct object_update_reply *our)
2627 {
2628         int i;
2629
2630         __swab32s(&our->ourp_magic);
2631         __swab16s(&our->ourp_count);
2632         __swab16s(&our->ourp_padding);
2633         for (i = 0; i < our->ourp_count; i++) {
2634                 struct object_update_result *ourp;
2635
2636                 __swab16s(&our->ourp_lens[i]);
2637                 ourp = object_update_result_get(our, i, NULL);
2638                 if (ourp == NULL)
2639                         return;
2640                 lustre_swab_object_update_result(ourp);
2641         }
2642 }
2643 EXPORT_SYMBOL(lustre_swab_object_update_reply);
2644
2645 void lustre_swab_swap_layouts(struct mdc_swap_layouts *msl)
2646 {
2647         __swab64s(&msl->msl_flags);
2648 }
2649 EXPORT_SYMBOL(lustre_swab_swap_layouts);
2650
2651 void lustre_swab_close_data(struct close_data *cd)
2652 {
2653         lustre_swab_lu_fid(&cd->cd_fid);
2654         __swab64s(&cd->cd_data_version);
2655 }
2656 EXPORT_SYMBOL(lustre_swab_close_data);
2657
2658 void lustre_swab_lfsck_request(struct lfsck_request *lr)
2659 {
2660         __swab32s(&lr->lr_event);
2661         __swab32s(&lr->lr_index);
2662         __swab32s(&lr->lr_flags);
2663         __swab32s(&lr->lr_valid);
2664         __swab32s(&lr->lr_speed);
2665         __swab16s(&lr->lr_version);
2666         __swab16s(&lr->lr_active);
2667         __swab16s(&lr->lr_param);
2668         __swab16s(&lr->lr_async_windows);
2669         CLASSERT(offsetof(typeof(*lr), lr_padding_1) != 0);
2670         lustre_swab_lu_fid(&lr->lr_fid);
2671         lustre_swab_lu_fid(&lr->lr_fid2);
2672         lustre_swab_lu_fid(&lr->lr_fid3);
2673         CLASSERT(offsetof(typeof(*lr), lr_padding_2) != 0);
2674         CLASSERT(offsetof(typeof(*lr), lr_padding_3) != 0);
2675 }
2676 EXPORT_SYMBOL(lustre_swab_lfsck_request);
2677
2678 void lustre_swab_lfsck_reply(struct lfsck_reply *lr)
2679 {
2680         __swab32s(&lr->lr_status);
2681         CLASSERT(offsetof(typeof(*lr), lr_padding_1) != 0);
2682         CLASSERT(offsetof(typeof(*lr), lr_padding_2) != 0);
2683 }
2684 EXPORT_SYMBOL(lustre_swab_lfsck_reply);
2685
2686 void lustre_swab_orphan_ent(struct lu_orphan_ent *ent)
2687 {
2688         lustre_swab_lu_fid(&ent->loe_key);
2689         lustre_swab_lu_fid(&ent->loe_rec.lor_fid);
2690         __swab32s(&ent->loe_rec.lor_uid);
2691         __swab32s(&ent->loe_rec.lor_gid);
2692 }
2693 EXPORT_SYMBOL(lustre_swab_orphan_ent);