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