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