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