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