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