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