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