Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / ptlrpc / pack_generic.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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
57 static inline int lustre_msg_hdr_size_v2(int count)
58 {
59         return size_round(offsetof(struct lustre_msg_v2, lm_buflens[count]));
60 }
61
62 int lustre_msg_hdr_size(__u32 magic, int count)
63 {
64         switch (magic) {
65         case LUSTRE_MSG_MAGIC_V2:
66                 return lustre_msg_hdr_size_v2(count);
67         default:
68                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
69                 return -EINVAL;
70         }
71 }
72 EXPORT_SYMBOL(lustre_msg_hdr_size);
73
74 int lustre_msg_swabbed(struct lustre_msg *msg)
75 {
76         return (msg->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED);
77 }
78
79 static inline int
80 lustre_msg_check_version_v2(struct lustre_msg_v2 *msg, __u32 version)
81 {
82         __u32 ver = lustre_msg_get_version(msg);
83         return (ver & LUSTRE_VERSION_MASK) != version;
84 }
85
86 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version)
87 {
88         switch (msg->lm_magic) {
89         case LUSTRE_MSG_MAGIC_V1:
90         case LUSTRE_MSG_MAGIC_V1_SWABBED:
91                 CERROR("msg v1 not supported - please upgrade you system\n");
92                 return -EINVAL; 
93         case LUSTRE_MSG_MAGIC_V2:
94         case LUSTRE_MSG_MAGIC_V2_SWABBED:
95                 return lustre_msg_check_version_v2(msg, version);
96         default:
97                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
98                 return 0;
99         }
100 }
101
102 /* early reply size */
103 int lustre_msg_early_size() {
104         static int size = 0;
105         if (!size)
106                 size = lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
107         return size;
108 }
109 EXPORT_SYMBOL(lustre_msg_early_size);
110
111 int lustre_msg_size_v2(int count, __u32 *lengths)
112 {
113         int size;
114         int i;
115
116         size = lustre_msg_hdr_size_v2(count);
117         for (i = 0; i < count; i++)
118                 size += size_round(lengths[i]);
119
120         return size;
121 }
122 EXPORT_SYMBOL(lustre_msg_size_v2);
123
124 /* This returns the size of the buffer that is required to hold a lustre_msg
125  * with the given sub-buffer lengths.
126  * NOTE: this should only be used for NEW requests, and should always be
127  *       in the form of a v2 request.  If this is a connection to a v1
128  *       target then the first buffer will be stripped because the ptlrpc
129  *       data is part of the lustre_msg_v1 header. b=14043 */
130 int lustre_msg_size(__u32 magic, int count, __u32 *lens)
131 {
132         __u32 size[] = { sizeof(struct ptlrpc_body) };
133
134         if (!lens) {
135                 LASSERT(count == 1);
136                 lens = size;
137         }
138
139         LASSERT(count > 0);
140         LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
141
142         switch (magic) {
143         case LUSTRE_MSG_MAGIC_V2:
144                 return lustre_msg_size_v2(count, lens);
145         default:
146                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
147                 return -EINVAL;
148         }
149 }
150
151 /* This is used to determine the size of a buffer that was already packed
152  * and will correctly handle the different message formats. */
153 int lustre_packed_msg_size(struct lustre_msg *msg)
154 {
155         switch (msg->lm_magic) {
156         case LUSTRE_MSG_MAGIC_V2:
157                 return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
158         default:
159                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
160                 return 0;
161         }
162 }
163
164 void lustre_init_msg_v2(struct lustre_msg_v2 *msg, int count, __u32 *lens,
165                         char **bufs)
166 {
167         char *ptr;
168         int i;
169
170         msg->lm_bufcount = count;
171         /* XXX: lm_secflvr uninitialized here */
172         msg->lm_magic = LUSTRE_MSG_MAGIC_V2;
173
174         for (i = 0; i < count; i++)
175                 msg->lm_buflens[i] = lens[i];
176
177         if (bufs == NULL)
178                 return;
179
180         ptr = (char *)msg + lustre_msg_hdr_size_v2(count);
181         for (i = 0; i < count; i++) {
182                 char *tmp = bufs[i];
183                 LOGL(tmp, lens[i], ptr);
184         }
185 }
186 EXPORT_SYMBOL(lustre_init_msg_v2);
187
188 static int lustre_pack_request_v2(struct ptlrpc_request *req,
189                                   int count, __u32 *lens, char **bufs)
190 {
191         int reqlen, rc;
192
193         reqlen = lustre_msg_size_v2(count, lens);
194
195         rc = sptlrpc_cli_alloc_reqbuf(req, reqlen);
196         if (rc)
197                 return rc;
198
199         req->rq_reqlen = reqlen;
200
201         lustre_init_msg_v2(req->rq_reqmsg, count, lens, bufs);
202         lustre_msg_add_version(req->rq_reqmsg, PTLRPC_MSG_VERSION);
203         lustre_set_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
204         return 0;
205 }
206
207 int lustre_pack_request(struct ptlrpc_request *req, __u32 magic, int count,
208                         __u32 *lens, char **bufs)
209 {
210         __u32 size[] = { sizeof(struct ptlrpc_body) };
211
212         if (!lens) {
213                 LASSERT(count == 1);
214                 lens = size;
215         }
216
217         LASSERT(count > 0);
218         LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
219
220         /* only use new format, we don't need to be compatible with 1.4 */
221         magic = LUSTRE_MSG_MAGIC_V2;
222
223         switch (magic) {
224         case LUSTRE_MSG_MAGIC_V2:
225                 return lustre_pack_request_v2(req, count, lens, bufs);
226         default:
227                 LASSERTF(0, "incorrect message magic: %08x\n", magic);
228                 return -EINVAL;
229         }
230 }
231
232 #if RS_DEBUG
233 CFS_LIST_HEAD(ptlrpc_rs_debug_lru);
234 spinlock_t ptlrpc_rs_debug_lock;
235
236 #define PTLRPC_RS_DEBUG_LRU_ADD(rs)                                     \
237 do {                                                                    \
238         spin_lock(&ptlrpc_rs_debug_lock);                               \
239         list_add_tail(&(rs)->rs_debug_list, &ptlrpc_rs_debug_lru);      \
240         spin_unlock(&ptlrpc_rs_debug_lock);                             \
241 } while (0)
242
243 #define PTLRPC_RS_DEBUG_LRU_DEL(rs)             \
244 do {                                            \
245         spin_lock(&ptlrpc_rs_debug_lock);       \
246         list_del(&(rs)->rs_debug_list);         \
247         spin_unlock(&ptlrpc_rs_debug_lock);     \
248 } while (0)
249 #else
250 # define PTLRPC_RS_DEBUG_LRU_ADD(rs) do {} while(0)
251 # define PTLRPC_RS_DEBUG_LRU_DEL(rs) do {} while(0)
252 #endif
253
254 struct ptlrpc_reply_state *lustre_get_emerg_rs(struct ptlrpc_service *svc)
255 {
256         struct ptlrpc_reply_state *rs = NULL;
257
258         spin_lock(&svc->srv_lock);
259         /* See if we have anything in a pool, and wait if nothing */
260         while (list_empty(&svc->srv_free_rs_list)) {
261                 struct l_wait_info lwi;
262                 int rc;
263                 spin_unlock(&svc->srv_lock);
264                 /* If we cannot get anything for some long time, we better
265                    bail out instead of waiting infinitely */
266                 lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
267                 rc = l_wait_event(svc->srv_free_rs_waitq,
268                                   !list_empty(&svc->srv_free_rs_list), &lwi);
269                 if (rc)
270                         goto out;
271                 spin_lock(&svc->srv_lock);
272         }
273
274         rs = list_entry(svc->srv_free_rs_list.next, struct ptlrpc_reply_state,
275                         rs_list);
276         list_del(&rs->rs_list);
277         spin_unlock(&svc->srv_lock);
278         LASSERT(rs);
279         memset(rs, 0, svc->srv_max_reply_size);
280         rs->rs_service = svc;
281         rs->rs_prealloc = 1;
282 out:
283         return rs;
284 }
285
286 void lustre_put_emerg_rs(struct ptlrpc_reply_state *rs)
287 {
288         struct ptlrpc_service *svc = rs->rs_service;
289
290         LASSERT(svc);
291
292         spin_lock(&svc->srv_lock);
293         list_add(&rs->rs_list, &svc->srv_free_rs_list);
294         spin_unlock(&svc->srv_lock);
295         cfs_waitq_signal(&svc->srv_free_rs_waitq);
296 }
297
298 int lustre_pack_reply_v2(struct ptlrpc_request *req, int count,
299                          __u32 *lens, char **bufs, int flags)
300 {
301         struct ptlrpc_reply_state *rs;
302         int                        msg_len, rc;
303         ENTRY;
304
305         LASSERT(req->rq_reply_state == NULL);
306
307         if ((flags & LPRFL_EARLY_REPLY) == 0)
308                 req->rq_packed_final = 1;
309
310         msg_len = lustre_msg_size_v2(count, lens);
311         rc = sptlrpc_svc_alloc_rs(req, msg_len);
312         if (rc)
313                 RETURN(rc);
314
315         rs = req->rq_reply_state;
316         atomic_set(&rs->rs_refcount, 1);        /* 1 ref for rq_reply_state */
317         rs->rs_cb_id.cbid_fn = reply_out_callback;
318         rs->rs_cb_id.cbid_arg = rs;
319         rs->rs_service = req->rq_rqbd->rqbd_service;
320         CFS_INIT_LIST_HEAD(&rs->rs_exp_list);
321         CFS_INIT_LIST_HEAD(&rs->rs_obd_list);
322
323         req->rq_replen = msg_len;
324         req->rq_reply_state = rs;
325         req->rq_repmsg = rs->rs_msg;
326
327         lustre_init_msg_v2(rs->rs_msg, count, lens, bufs);
328         lustre_msg_add_version(rs->rs_msg, PTLRPC_MSG_VERSION);
329         lustre_set_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
330
331         PTLRPC_RS_DEBUG_LRU_ADD(rs);
332
333         RETURN(0);
334 }
335 EXPORT_SYMBOL(lustre_pack_reply_v2);
336
337 int lustre_pack_reply_flags(struct ptlrpc_request *req, int count, __u32 *lens,
338                             char **bufs, int flags)
339 {
340         int rc = 0;
341         __u32 size[] = { sizeof(struct ptlrpc_body) };
342
343         if (!lens) {
344                 LASSERT(count == 1);
345                 lens = size;
346         }
347
348         LASSERT(count > 0);
349         LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
350
351         switch (req->rq_reqmsg->lm_magic) {
352         case LUSTRE_MSG_MAGIC_V2:
353         case LUSTRE_MSG_MAGIC_V2_SWABBED:
354                 rc = lustre_pack_reply_v2(req, count, lens, bufs, flags);
355                 break;
356         default:
357                 LASSERTF(0, "incorrect message magic: %08x\n",
358                          req->rq_reqmsg->lm_magic);
359                 rc = -EINVAL;
360         }
361         if (rc != 0)
362                 CERROR("lustre_pack_reply failed: rc=%d size=%d\n", rc,
363                        lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens));
364         return rc;
365 }
366
367 int lustre_pack_reply(struct ptlrpc_request *req, int count, __u32 *lens,
368                       char **bufs)
369 {
370         return lustre_pack_reply_flags(req, count, lens, bufs, 0);
371 }
372
373 void *lustre_msg_buf_v2(struct lustre_msg_v2 *m, int n, int min_size)
374 {
375         int i, offset, buflen, bufcount;
376
377         LASSERT(m != NULL);
378         LASSERT(n >= 0);
379
380         bufcount = m->lm_bufcount;
381         if (unlikely(n >= bufcount)) {
382                 CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
383                        m, n, bufcount);
384                 return NULL;
385         }
386
387         buflen = m->lm_buflens[n];
388         if (unlikely(buflen < min_size)) {
389                 CERROR("msg %p buffer[%d] size %d too small (required %d)\n",
390                        m, n, buflen, min_size);
391                 return NULL;
392         }
393
394         offset = lustre_msg_hdr_size_v2(bufcount);
395         for (i = 0; i < n; i++)
396                 offset += size_round(m->lm_buflens[i]);
397
398         return (char *)m + offset;
399 }
400
401 void *lustre_msg_buf(struct lustre_msg *m, int n, int min_size)
402 {
403         switch (m->lm_magic) {
404         case LUSTRE_MSG_MAGIC_V2:
405         case LUSTRE_MSG_MAGIC_V2_SWABBED:
406                 return lustre_msg_buf_v2(m, n, min_size);
407         default:
408                 LASSERTF(0, "incorrect message magic: %08x(msg:%p)\n", m->lm_magic, m);
409                 return NULL;
410         }
411 }
412
413 int lustre_shrink_msg_v2(struct lustre_msg_v2 *msg, int segment,
414                          unsigned int newlen, int move_data)
415 {
416         char   *tail = NULL, *newpos;
417         int     tail_len = 0, n;
418
419         LASSERT(msg);
420         LASSERT(msg->lm_bufcount > segment);
421         LASSERT(msg->lm_buflens[segment] >= newlen);
422
423         if (msg->lm_buflens[segment] == newlen)
424                 goto out;
425
426         if (move_data && msg->lm_bufcount > segment + 1) {
427                 tail = lustre_msg_buf_v2(msg, segment + 1, 0);
428                 for (n = segment + 1; n < msg->lm_bufcount; n++)
429                         tail_len += size_round(msg->lm_buflens[n]);
430         }
431
432         msg->lm_buflens[segment] = newlen;
433
434         if (tail && tail_len) {
435                 newpos = lustre_msg_buf_v2(msg, segment + 1, 0);
436                 LASSERT(newpos <= tail);
437                 if (newpos != tail)
438                         memcpy(newpos, tail, tail_len);
439         }
440 out:
441         return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
442 }
443
444 /*
445  * for @msg, shrink @segment to size @newlen. if @move_data is non-zero,
446  * we also move data forward from @segment + 1.
447  *
448  * if @newlen == 0, we remove the segment completely, but we still keep the
449  * totally bufcount the same to save possible data moving. this will leave a
450  * unused segment with size 0 at the tail, but that's ok.
451  *
452  * return new msg size after shrinking.
453  *
454  * CAUTION:
455  * + if any buffers higher than @segment has been filled in, must call shrink
456  *   with non-zero @move_data.
457  * + caller should NOT keep pointers to msg buffers which higher than @segment
458  *   after call shrink.
459  */
460 int lustre_shrink_msg(struct lustre_msg *msg, int segment,
461                       unsigned int newlen, int move_data)
462 {
463         switch (msg->lm_magic) {
464         case LUSTRE_MSG_MAGIC_V2:
465                 return lustre_shrink_msg_v2(msg, segment, newlen, move_data);
466         default:
467                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
468         }
469 }
470
471 void lustre_free_reply_state(struct ptlrpc_reply_state *rs)
472 {
473         PTLRPC_RS_DEBUG_LRU_DEL(rs);
474
475         LASSERT (atomic_read(&rs->rs_refcount) == 0);
476         LASSERT (!rs->rs_difficult || rs->rs_handled);
477         LASSERT (!rs->rs_on_net);
478         LASSERT (!rs->rs_scheduled);
479         LASSERT (rs->rs_export == NULL);
480         LASSERT (rs->rs_nlocks == 0);
481         LASSERT (list_empty(&rs->rs_exp_list));
482         LASSERT (list_empty(&rs->rs_obd_list));
483
484         sptlrpc_svc_free_rs(rs);
485 }
486
487 static int lustre_unpack_msg_v2(struct lustre_msg_v2 *m, int len)
488 {
489         int flipped, required_len, i;
490
491         /* Now we know the sender speaks my language. */
492         required_len = lustre_msg_hdr_size_v2(0);
493         if (len < required_len) {
494                 /* can't even look inside the message */
495                 CERROR("message length %d too small for lustre_msg\n", len);
496                 return -EINVAL;
497         }
498
499         flipped = lustre_msg_swabbed(m);
500
501         if (flipped) {
502                 __swab32s(&m->lm_bufcount);
503                 __swab32s(&m->lm_secflvr);
504                 __swab32s(&m->lm_repsize);
505                 __swab32s(&m->lm_cksum);
506                 __swab32s(&m->lm_flags);
507                 CLASSERT(offsetof(typeof(*m), lm_padding_2) != 0);
508                 CLASSERT(offsetof(typeof(*m), lm_padding_3) != 0);
509         }
510
511         required_len = lustre_msg_hdr_size_v2(m->lm_bufcount);
512         if (len < required_len) {
513                 /* didn't receive all the buffer lengths */
514                 CERROR ("message length %d too small for %d buflens\n",
515                         len, m->lm_bufcount);
516                 return -EINVAL;
517         }
518         
519         for (i = 0; i < m->lm_bufcount; i++) {
520                 if (flipped)
521                         __swab32s(&m->lm_buflens[i]);
522                 required_len += size_round(m->lm_buflens[i]);
523         }
524
525         if (len < required_len) {
526                 CERROR("len: %d, required_len %d\n", len, required_len);
527                 CERROR("bufcount: %d\n", m->lm_bufcount);
528                 for (i = 0; i < m->lm_bufcount; i++)
529                         CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
530                 return -EINVAL;
531         }
532
533         return 0;
534 }
535
536 int lustre_unpack_msg(struct lustre_msg *m, int len)
537 {
538         int required_len, rc;
539         ENTRY;
540
541         /* We can provide a slightly better error log, if we check the
542          * message magic and version first.  In the future, struct
543          * lustre_msg may grow, and we'd like to log a version mismatch,
544          * rather than a short message.
545          *
546          */
547         required_len = offsetof(struct lustre_msg, lm_magic) +
548                        sizeof(m->lm_magic);
549         if (len < required_len) {
550                 /* can't even look inside the message */
551                 CERROR("message length %d too small for magic/version check\n",
552                        len);
553                 RETURN(-EINVAL);
554         }
555
556         switch (m->lm_magic) {
557         case LUSTRE_MSG_MAGIC_V2:
558         case LUSTRE_MSG_MAGIC_V2_SWABBED:
559                 rc = lustre_unpack_msg_v2(m, len);
560                 break;
561         default:
562                 CERROR("bad lustre msg magic: %#08X\n", m->lm_magic);
563                 return -EINVAL;
564         }
565
566         RETURN(rc);
567 }
568
569 static inline int lustre_unpack_ptlrpc_body_v2(struct lustre_msg_v2 *m,
570                                                int offset)
571 {
572         struct ptlrpc_body *pb;
573
574         pb = lustre_msg_buf_v2(m, offset, sizeof(*pb));
575         if (!pb) {
576                 CERROR("error unpacking ptlrpc body\n");
577                 return -EFAULT;
578         }
579         if (lustre_msg_swabbed(m))
580                 lustre_swab_ptlrpc_body(pb);
581
582         if ((pb->pb_version & ~LUSTRE_VERSION_MASK) != PTLRPC_MSG_VERSION) {
583                  CERROR("wrong lustre_msg version %08x\n", pb->pb_version);
584                  return -EINVAL;
585         }
586
587         return 0;
588 }
589
590 int lustre_unpack_req_ptlrpc_body(struct ptlrpc_request *req, int offset)
591 {
592         switch (req->rq_reqmsg->lm_magic) {
593         case LUSTRE_MSG_MAGIC_V2:
594         case LUSTRE_MSG_MAGIC_V2_SWABBED:
595                 lustre_set_req_swabbed(req, offset);
596                 return lustre_unpack_ptlrpc_body_v2(req->rq_reqmsg, offset);
597         default:
598                 CERROR("bad lustre msg magic: %#08X\n",
599                        req->rq_reqmsg->lm_magic);
600                 return -EINVAL;
601         }
602 }
603
604 int lustre_unpack_rep_ptlrpc_body(struct ptlrpc_request *req, int offset)
605 {
606         switch (req->rq_repmsg->lm_magic) {
607         case LUSTRE_MSG_MAGIC_V2:
608         case LUSTRE_MSG_MAGIC_V2_SWABBED:
609                 lustre_set_rep_swabbed(req, offset);
610                 return lustre_unpack_ptlrpc_body_v2(req->rq_repmsg, offset);
611         default:
612                 CERROR("bad lustre msg magic: %#08X\n",
613                        req->rq_repmsg->lm_magic);
614                 return -EINVAL;
615         }
616 }
617
618 static inline int lustre_msg_buflen_v2(struct lustre_msg_v2 *m, int n)
619 {
620         if (n >= m->lm_bufcount)
621                 return 0;
622
623         return m->lm_buflens[n];
624 }
625
626 /**
627  * lustre_msg_buflen - return the length of buffer @n in message @m
628  * @m - lustre_msg (request or reply) to look at
629  * @n - message index (base 0)
630  *
631  * returns zero for non-existent message indices
632  */
633 int lustre_msg_buflen(struct lustre_msg *m, int n)
634 {
635         switch (m->lm_magic) {
636         case LUSTRE_MSG_MAGIC_V2:
637         case LUSTRE_MSG_MAGIC_V2_SWABBED:
638                 return lustre_msg_buflen_v2(m, n);
639         default:
640                 CERROR("incorrect message magic: %08x\n", m->lm_magic);
641                 return -EINVAL;
642         }
643 }
644 EXPORT_SYMBOL(lustre_msg_buflen);
645
646 static inline void
647 lustre_msg_set_buflen_v2(struct lustre_msg_v2 *m, int n, int len)
648 {
649         if (n >= m->lm_bufcount)
650                 LBUG();
651
652         m->lm_buflens[n] = len;
653 }
654
655 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len)
656 {
657         switch (m->lm_magic) {
658         case LUSTRE_MSG_MAGIC_V2:
659                 lustre_msg_set_buflen_v2(m, n, len);
660                 return;
661         default:
662                 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
663         }
664 }
665
666 EXPORT_SYMBOL(lustre_msg_set_buflen);
667
668 /* NB return the bufcount for lustre_msg_v2 format, so if message is packed
669  * in V1 format, the result is one bigger. (add struct ptlrpc_body). */
670 int lustre_msg_bufcount(struct lustre_msg *m)
671 {
672         switch (m->lm_magic) {
673         case LUSTRE_MSG_MAGIC_V2:
674         case LUSTRE_MSG_MAGIC_V2_SWABBED:
675                 return m->lm_bufcount;
676         default:
677                 CERROR("incorrect message magic: %08x\n", m->lm_magic);
678                 return -EINVAL;
679         }
680 }
681 EXPORT_SYMBOL(lustre_msg_bufcount);
682
683 char *lustre_msg_string(struct lustre_msg *m, int index, int max_len)
684 {
685         /* max_len == 0 means the string should fill the buffer */
686         char *str;
687         int slen, blen;
688
689         switch (m->lm_magic) {
690         case LUSTRE_MSG_MAGIC_V2:
691         case LUSTRE_MSG_MAGIC_V2_SWABBED:
692                 str = lustre_msg_buf_v2(m, index, 0);
693                 blen = lustre_msg_buflen_v2(m, index);
694                 break;
695         default:
696                 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
697         }
698
699         if (str == NULL) {
700                 CERROR ("can't unpack string in msg %p buffer[%d]\n", m, index);
701                 return NULL;
702         }
703
704         slen = strnlen(str, blen);
705
706         if (slen == blen) {                     /* not NULL terminated */
707                 CERROR("can't unpack non-NULL terminated string in "
708                         "msg %p buffer[%d] len %d\n", m, index, blen);
709                 return NULL;
710         }
711
712         if (max_len == 0) {
713                 if (slen != blen - 1) {
714                         CERROR("can't unpack short string in msg %p "
715                                "buffer[%d] len %d: strlen %d\n",
716                                m, index, blen, slen);
717                         return NULL;
718                 }
719         } else if (slen > max_len) {
720                 CERROR("can't unpack oversized string in msg %p "
721                        "buffer[%d] len %d strlen %d: max %d expected\n",
722                        m, index, blen, slen, max_len);
723                 return NULL;
724         }
725
726         return str;
727 }
728
729 /* Wrap up the normal fixed length cases */
730 void *lustre_swab_buf(struct lustre_msg *msg, int index, int min_size,
731                       void *swabber)
732 {
733         void *ptr = NULL;
734
735         switch (msg->lm_magic) {
736         case LUSTRE_MSG_MAGIC_V2:
737         case LUSTRE_MSG_MAGIC_V2_SWABBED:
738                 ptr = lustre_msg_buf_v2(msg, index, min_size);
739                 break;
740         default:
741                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
742         }
743         if (ptr == NULL)
744                 return NULL;
745
746         if (swabber != NULL && lustre_msg_swabbed(msg))
747                 ((void (*)(void *))swabber)(ptr);
748
749         return ptr;
750 }
751
752 void *lustre_swab_reqbuf(struct ptlrpc_request *req, int index, int min_size,
753                          void *swabber)
754 {
755         lustre_set_req_swabbed(req, index);
756         return lustre_swab_buf(req->rq_reqmsg, index, min_size, swabber);
757 }
758
759 void *lustre_swab_repbuf(struct ptlrpc_request *req, int index, int min_size,
760                          void *swabber)
761 {
762         lustre_set_rep_swabbed(req, index);
763         return lustre_swab_buf(req->rq_repmsg, index, min_size, swabber);
764 }
765
766 __u32 lustre_msghdr_get_flags(struct lustre_msg *msg)
767 {
768         switch (msg->lm_magic) {
769         case LUSTRE_MSG_MAGIC_V1:
770         case LUSTRE_MSG_MAGIC_V1_SWABBED:
771                 return 0;
772         case LUSTRE_MSG_MAGIC_V2:
773         case LUSTRE_MSG_MAGIC_V2_SWABBED:
774                 /* already in host endian */
775                 return msg->lm_flags;
776         default:
777                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
778                 return 0;
779         }
780 }
781
782 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags)
783 {
784         switch (msg->lm_magic) {
785         case LUSTRE_MSG_MAGIC_V1:
786                 return;
787         case LUSTRE_MSG_MAGIC_V2:
788                 msg->lm_flags = flags;
789                 return;
790         default:
791                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
792         }
793 }
794
795 __u32 lustre_msg_get_flags(struct lustre_msg *msg)
796 {
797         switch (msg->lm_magic) {
798         case LUSTRE_MSG_MAGIC_V2:
799         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
800                 struct ptlrpc_body *pb;
801
802                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
803                 if (!pb) {
804                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
805                         return 0;
806                 }
807                 return pb->pb_flags;
808         }
809         default:
810                 /* flags might be printed in debug code while message
811                  * uninitialized */
812                 return 0;
813         }
814 }
815
816 void lustre_msg_add_flags(struct lustre_msg *msg, int flags)
817 {
818         switch (msg->lm_magic) {
819         case LUSTRE_MSG_MAGIC_V2: {
820                 struct ptlrpc_body *pb;
821
822                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
823                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
824                 pb->pb_flags |= flags;
825                 return;
826         }
827         default:
828                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
829         }
830 }
831
832 void lustre_msg_set_flags(struct lustre_msg *msg, int flags)
833 {
834         switch (msg->lm_magic) {
835         case LUSTRE_MSG_MAGIC_V2: {
836                 struct ptlrpc_body *pb;
837
838                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
839                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
840                 pb->pb_flags = flags;
841                 return;
842         }
843         default:
844                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
845         }
846 }
847
848 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags)
849 {
850         switch (msg->lm_magic) {
851         case LUSTRE_MSG_MAGIC_V2:
852         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
853                 struct ptlrpc_body *pb;
854
855                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
856                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
857                 pb->pb_flags &= ~(MSG_GEN_FLAG_MASK & flags);
858                 return;
859         }
860         default:
861                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
862         }
863 }
864
865 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg)
866 {
867         switch (msg->lm_magic) {
868         case LUSTRE_MSG_MAGIC_V2:
869         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
870                 struct ptlrpc_body *pb;
871
872                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
873                 if (!pb) {
874                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
875                         return 0;
876                 }
877                 return pb->pb_op_flags;
878         }
879         default:
880                 return 0;
881         }
882 }
883
884 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags)
885 {
886         switch (msg->lm_magic) {
887         case LUSTRE_MSG_MAGIC_V2: {
888                 struct ptlrpc_body *pb;
889
890                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
891                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
892                 pb->pb_op_flags |= flags;
893                 return;
894         }
895         default:
896                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
897         }
898 }
899
900 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags)
901 {
902         switch (msg->lm_magic) {
903         case LUSTRE_MSG_MAGIC_V2: {
904                 struct ptlrpc_body *pb;
905
906                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
907                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
908                 pb->pb_op_flags |= flags;
909                 return;
910         }
911         default:
912                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
913         }
914 }
915
916 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg)
917 {
918         switch (msg->lm_magic) {
919         case LUSTRE_MSG_MAGIC_V2:
920         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
921                 struct ptlrpc_body *pb;
922
923                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
924                 if (!pb) {
925                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
926                         return NULL;
927                 }
928                 return &pb->pb_handle;
929         }
930         default:
931                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
932                 return NULL;
933         }
934 }
935
936 __u32 lustre_msg_get_type(struct lustre_msg *msg)
937 {
938         switch (msg->lm_magic) {
939         case LUSTRE_MSG_MAGIC_V2:
940         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
941                 struct ptlrpc_body *pb;
942
943                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
944                 if (!pb) {
945                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
946                         return PTL_RPC_MSG_ERR;
947                 }
948                 return pb->pb_type;
949         }
950         default:
951                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
952                 return PTL_RPC_MSG_ERR;
953         }
954 }
955
956 __u32 lustre_msg_get_version(struct lustre_msg *msg)
957 {
958         switch (msg->lm_magic) {
959         case LUSTRE_MSG_MAGIC_V2:
960         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
961                 struct ptlrpc_body *pb;
962
963                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
964                 if (!pb) {
965                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
966                         return 0;
967                 }
968                 return pb->pb_version;
969         }
970         default:
971                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
972                 return 0;
973         }
974 }
975
976 void lustre_msg_add_version(struct lustre_msg *msg, int version)
977 {
978         switch (msg->lm_magic) {
979         case LUSTRE_MSG_MAGIC_V2:
980         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
981                 struct ptlrpc_body *pb;
982
983                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
984                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
985                 pb->pb_version |= version;
986                 return;
987         }
988         default:
989                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
990         }
991 }
992
993 __u32 lustre_msg_get_opc(struct lustre_msg *msg)
994 {
995         switch (msg->lm_magic) {
996         case LUSTRE_MSG_MAGIC_V2:
997         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
998                 struct ptlrpc_body *pb;
999
1000                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1001                 if (!pb) {
1002                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1003                         return 0;
1004                 }
1005                 return pb->pb_opc;
1006         }
1007         default:
1008                 CERROR("incorrect message magic: %08x(msg:%p)\n", msg->lm_magic, msg);
1009                 return 0;
1010         }
1011 }
1012
1013 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg)
1014 {
1015         switch (msg->lm_magic) {
1016         case LUSTRE_MSG_MAGIC_V2:
1017         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1018                 struct ptlrpc_body *pb;
1019
1020                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1021                 if (!pb) {
1022                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1023                         return 0;
1024                 }
1025                 return pb->pb_last_xid;
1026         }
1027         default:
1028                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1029                 return 0;
1030         }
1031 }
1032
1033 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg)
1034 {
1035         switch (msg->lm_magic) {
1036         case LUSTRE_MSG_MAGIC_V2:
1037         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1038                 struct ptlrpc_body *pb;
1039
1040                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1041                 if (!pb) {
1042                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1043                         return 0;
1044                 }
1045                 return pb->pb_last_committed;
1046         }
1047         default:
1048                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1049                 return 0;
1050         }
1051 }
1052
1053 __u64 lustre_msg_get_transno(struct lustre_msg *msg)
1054 {
1055         switch (msg->lm_magic) {
1056         case LUSTRE_MSG_MAGIC_V2:
1057         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1058                 struct ptlrpc_body *pb;
1059
1060                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1061                 if (!pb) {
1062                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1063                         return 0;
1064                 }
1065                 return pb->pb_transno;
1066         }
1067         default:
1068                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1069                 return 0;
1070         }
1071 }
1072
1073 int lustre_msg_get_status(struct lustre_msg *msg)
1074 {
1075         switch (msg->lm_magic) {
1076         case LUSTRE_MSG_MAGIC_V2:
1077         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1078                 struct ptlrpc_body *pb;
1079
1080                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1081                 if (!pb) {
1082                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1083                         return -EINVAL;
1084                 }
1085                 return pb->pb_status;
1086         }
1087         default:
1088                 /* status might be printed in debug code while message
1089                  * uninitialized */
1090                 return -EINVAL;
1091         }
1092 }
1093
1094 __u64 lustre_msg_get_slv(struct lustre_msg *msg)
1095 {
1096         switch (msg->lm_magic) {
1097         case LUSTRE_MSG_MAGIC_V2:
1098         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1099                 struct ptlrpc_body *pb;
1100
1101                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1102                 if (!pb) {
1103                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1104                         return -EINVAL;
1105                 }
1106                 return pb->pb_slv;
1107         }
1108         default:
1109                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1110                 return -EINVAL;
1111         }
1112 }
1113
1114
1115 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv)
1116 {
1117         switch (msg->lm_magic) {
1118         case LUSTRE_MSG_MAGIC_V2:
1119         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1120                 struct ptlrpc_body *pb;
1121
1122                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1123                 if (!pb) {
1124                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1125                         return;
1126                 }
1127                 pb->pb_slv = slv;
1128                 return;
1129         }
1130         default:
1131                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1132                 return;
1133         }
1134 }
1135
1136 __u32 lustre_msg_get_limit(struct lustre_msg *msg)
1137 {
1138         switch (msg->lm_magic) {
1139         case LUSTRE_MSG_MAGIC_V2:
1140         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1141                 struct ptlrpc_body *pb;
1142
1143                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1144                 if (!pb) {
1145                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1146                         return -EINVAL;
1147                 }
1148                 return pb->pb_limit;
1149         }
1150         default:
1151                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1152                 return -EINVAL;
1153         }
1154 }
1155
1156
1157 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit)
1158 {
1159         switch (msg->lm_magic) {
1160         case LUSTRE_MSG_MAGIC_V2:
1161         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1162                 struct ptlrpc_body *pb;
1163
1164                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1165                 if (!pb) {
1166                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1167                         return;
1168                 }
1169                 pb->pb_limit = limit;
1170                 return;
1171         }
1172         default:
1173                 CERROR("invalid msg magic %x\n", msg->lm_magic);
1174                 return;
1175         }
1176 }
1177
1178 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg)
1179 {
1180         switch (msg->lm_magic) {
1181         case LUSTRE_MSG_MAGIC_V2:
1182         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1183                 struct ptlrpc_body *pb;
1184
1185                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1186                 if (!pb) {
1187                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1188                         return 0;
1189                 }
1190                 return pb->pb_conn_cnt;
1191         }
1192         default:
1193                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1194                 return 0;
1195         }
1196 }
1197
1198 int lustre_msg_is_v1(struct lustre_msg *msg)
1199 {
1200         switch (msg->lm_magic) {
1201         case LUSTRE_MSG_MAGIC_V1:
1202         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1203                 return 1;
1204         default:
1205                 return 0;
1206         }
1207 }
1208
1209 __u32 lustre_msg_get_magic(struct lustre_msg *msg)
1210 {
1211         switch (msg->lm_magic) {
1212         case LUSTRE_MSG_MAGIC_V2:
1213         case LUSTRE_MSG_MAGIC_V2_SWABBED:
1214                 return msg->lm_magic;
1215         default:
1216                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1217                 return 0;
1218         }
1219 }
1220
1221 __u32 lustre_msg_get_timeout(struct lustre_msg *msg)
1222 {
1223         switch (msg->lm_magic) {
1224         case LUSTRE_MSG_MAGIC_V1:
1225         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1226                 return 0;
1227         case LUSTRE_MSG_MAGIC_V2:
1228         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1229                 struct ptlrpc_body *pb;
1230
1231                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1232                 if (!pb) {
1233                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1234                         return 0;
1235
1236                 }
1237                 return pb->pb_timeout;
1238         }
1239         default:
1240                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1241                 return 0;
1242         }
1243 }
1244
1245 __u32 lustre_msg_get_service_time(struct lustre_msg *msg)
1246 {
1247         switch (msg->lm_magic) {
1248         case LUSTRE_MSG_MAGIC_V1:
1249         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1250                 return 0;
1251         case LUSTRE_MSG_MAGIC_V2:
1252         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1253                 struct ptlrpc_body *pb;
1254
1255                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1256                 if (!pb) {
1257                         CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1258                         return 0;
1259
1260                 }
1261                 return pb->pb_service_time;
1262         }
1263         default:
1264                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1265                 return 0;
1266         }
1267 }
1268
1269 __u32 lustre_msg_get_cksum(struct lustre_msg *msg)
1270 {
1271         switch (msg->lm_magic) {
1272         case LUSTRE_MSG_MAGIC_V1:
1273         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1274                 return 0;
1275         case LUSTRE_MSG_MAGIC_V2:
1276         case LUSTRE_MSG_MAGIC_V2_SWABBED:
1277                 return msg->lm_cksum;
1278         default:
1279                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1280                 return 0;
1281         }
1282 }
1283
1284 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg)
1285 {
1286         switch (msg->lm_magic) {
1287         case LUSTRE_MSG_MAGIC_V1:
1288         case LUSTRE_MSG_MAGIC_V1_SWABBED:
1289                 return 0;
1290         case LUSTRE_MSG_MAGIC_V2:
1291         case LUSTRE_MSG_MAGIC_V2_SWABBED: {
1292                 struct ptlrpc_body *pb;
1293                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1294                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1295                 return crc32_le(~(__u32)0, (unsigned char *)pb, sizeof(*pb));
1296         }
1297         default:
1298                 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1299                 return 0;
1300         }
1301 }
1302
1303 void lustre_msg_set_handle(struct lustre_msg *msg, struct lustre_handle *handle)
1304 {
1305         switch (msg->lm_magic) {
1306         case LUSTRE_MSG_MAGIC_V2: {
1307                 struct ptlrpc_body *pb;
1308
1309                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1310                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1311                 pb->pb_handle = *handle;
1312                 return;
1313         }
1314         default:
1315                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1316         }
1317 }
1318
1319 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type)
1320 {
1321         switch (msg->lm_magic) {
1322         case LUSTRE_MSG_MAGIC_V2: {
1323                 struct ptlrpc_body *pb;
1324
1325                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1326                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1327                 pb->pb_type = type;
1328                 return;
1329         }
1330         default:
1331                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1332         }
1333 }
1334
1335 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc)
1336 {
1337         switch (msg->lm_magic) {
1338         case LUSTRE_MSG_MAGIC_V2: {
1339                 struct ptlrpc_body *pb;
1340
1341                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1342                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1343                 pb->pb_opc = opc;
1344                 return;
1345         }
1346         default:
1347                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1348         }
1349 }
1350
1351 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid)
1352 {
1353         switch (msg->lm_magic) {
1354         case LUSTRE_MSG_MAGIC_V2: {
1355                 struct ptlrpc_body *pb;
1356
1357                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1358                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1359                 pb->pb_last_xid = last_xid;
1360                 return;
1361         }
1362         default:
1363                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1364         }
1365 }
1366
1367 void lustre_msg_set_last_committed(struct lustre_msg *msg, __u64 last_committed)
1368 {
1369         switch (msg->lm_magic) {
1370         case LUSTRE_MSG_MAGIC_V2: {
1371                 struct ptlrpc_body *pb;
1372
1373                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1374                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1375                 pb->pb_last_committed = last_committed;
1376                 return;
1377         }
1378         default:
1379                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1380         }
1381 }
1382
1383 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno)
1384 {
1385         switch (msg->lm_magic) {
1386         case LUSTRE_MSG_MAGIC_V2: {
1387                 struct ptlrpc_body *pb;
1388
1389                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1390                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1391                 pb->pb_transno = transno;
1392                 return;
1393         }
1394         default:
1395                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1396         }
1397 }
1398
1399 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status)
1400 {
1401         switch (msg->lm_magic) {
1402         case LUSTRE_MSG_MAGIC_V2: {
1403                 struct ptlrpc_body *pb;
1404
1405                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1406                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1407                 pb->pb_status = status;
1408                 return;
1409         }
1410         default:
1411                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1412         }
1413 }
1414
1415 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt)
1416 {
1417         switch (msg->lm_magic) {
1418         case LUSTRE_MSG_MAGIC_V2: {
1419                 struct ptlrpc_body *pb;
1420
1421                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1422                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1423                 pb->pb_conn_cnt = conn_cnt;
1424                 return;
1425         }
1426         default:
1427                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1428         }
1429 }
1430
1431 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout)
1432 {
1433         switch (msg->lm_magic) {
1434         case LUSTRE_MSG_MAGIC_V1:
1435                 return;
1436         case LUSTRE_MSG_MAGIC_V2: {
1437                 struct ptlrpc_body *pb;
1438
1439                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1440                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1441                 pb->pb_timeout = timeout;
1442                 return;
1443         }
1444         default:
1445                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1446         }
1447 }
1448
1449 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time)
1450 {
1451         switch (msg->lm_magic) {
1452         case LUSTRE_MSG_MAGIC_V1:
1453                 return;
1454         case LUSTRE_MSG_MAGIC_V2: {
1455                 struct ptlrpc_body *pb;
1456
1457                 pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));
1458                 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1459                 pb->pb_service_time = service_time;
1460                 return;
1461         }
1462         default:
1463                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1464         }
1465 }
1466
1467 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum)
1468 {
1469         switch (msg->lm_magic) {
1470         case LUSTRE_MSG_MAGIC_V1:
1471                 return;
1472         case LUSTRE_MSG_MAGIC_V2:
1473                 msg->lm_cksum = cksum;
1474                 return;
1475         default:
1476                 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1477         }
1478 }
1479
1480
1481 void ptlrpc_request_set_replen(struct ptlrpc_request *req)
1482 {
1483         int count = req_capsule_filled_sizes(&req->rq_pill, RCL_SERVER);
1484
1485         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count,
1486                                          req->rq_pill.rc_area[RCL_SERVER]);
1487         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1488                 req->rq_reqmsg->lm_repsize = req->rq_replen;
1489 }
1490
1491 void ptlrpc_req_set_repsize(struct ptlrpc_request *req, int count, __u32 *lens)
1492 {
1493         req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens);
1494         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1495                 req->rq_reqmsg->lm_repsize = req->rq_replen;
1496 }
1497
1498 /* byte flipping routines for all wire types declared in
1499  * lustre_idl.h implemented here.
1500  */
1501 void lustre_swab_ptlrpc_body(struct ptlrpc_body *b)
1502 {
1503         __swab32s (&b->pb_type);
1504         __swab32s (&b->pb_version);
1505         __swab32s (&b->pb_opc);
1506         __swab32s (&b->pb_status);
1507         __swab64s (&b->pb_last_xid);
1508         __swab64s (&b->pb_last_seen);
1509         __swab64s (&b->pb_last_committed);
1510         __swab64s (&b->pb_transno);
1511         __swab32s (&b->pb_flags);
1512         __swab32s (&b->pb_op_flags);
1513         __swab32s (&b->pb_conn_cnt);
1514         __swab32s (&b->pb_timeout);
1515         __swab32s (&b->pb_service_time);
1516         __swab32s (&b->pb_limit);
1517         __swab64s (&b->pb_slv);
1518 }
1519
1520 void lustre_swab_connect(struct obd_connect_data *ocd)
1521 {
1522         __swab64s(&ocd->ocd_connect_flags);
1523         __swab32s(&ocd->ocd_version);
1524         __swab32s(&ocd->ocd_grant);
1525         __swab64s(&ocd->ocd_ibits_known);
1526         __swab32s(&ocd->ocd_index);
1527         __swab32s(&ocd->ocd_brw_size);
1528         __swab32s(&ocd->ocd_nllu);
1529         __swab32s(&ocd->ocd_nllg);
1530         __swab64s(&ocd->ocd_transno);
1531         __swab32s(&ocd->ocd_group);
1532         __swab32s(&ocd->ocd_cksum_types);
1533         CLASSERT(offsetof(typeof(*ocd), padding1) != 0);
1534         CLASSERT(offsetof(typeof(*ocd), padding2) != 0);
1535 }
1536
1537 void lustre_swab_obdo (struct obdo  *o)
1538 {
1539         __swab64s (&o->o_valid);
1540         __swab64s (&o->o_id);
1541         __swab64s (&o->o_gr);
1542         __swab64s (&o->o_fid);
1543         __swab64s (&o->o_size);
1544         __swab64s (&o->o_mtime);
1545         __swab64s (&o->o_atime);
1546         __swab64s (&o->o_ctime);
1547         __swab64s (&o->o_blocks);
1548         __swab64s (&o->o_grant);
1549         __swab32s (&o->o_blksize);
1550         __swab32s (&o->o_mode);
1551         __swab32s (&o->o_uid);
1552         __swab32s (&o->o_gid);
1553         __swab32s (&o->o_flags);
1554         __swab32s (&o->o_nlink);
1555         __swab32s (&o->o_generation);
1556         __swab32s (&o->o_misc);
1557         __swab32s (&o->o_easize);
1558         __swab32s (&o->o_mds);
1559         __swab32s (&o->o_stripe_idx);
1560         __swab32s (&o->o_padding_1);
1561         /* o_inline is opaque */
1562 }
1563
1564 void lustre_swab_obd_statfs (struct obd_statfs *os)
1565 {
1566         __swab64s (&os->os_type);
1567         __swab64s (&os->os_blocks);
1568         __swab64s (&os->os_bfree);
1569         __swab64s (&os->os_bavail);
1570         __swab64s (&os->os_files);
1571         __swab64s (&os->os_ffree);
1572         /* no need to swab os_fsid */
1573         __swab32s (&os->os_bsize);
1574         __swab32s (&os->os_namelen);
1575         __swab64s (&os->os_maxbytes);
1576         __swab32s (&os->os_state);
1577         /* no need to swap os_spare */
1578 }
1579
1580 void lustre_swab_obd_ioobj (struct obd_ioobj *ioo)
1581 {
1582         __swab64s (&ioo->ioo_id);
1583         __swab64s (&ioo->ioo_gr);
1584         __swab32s (&ioo->ioo_type);
1585         __swab32s (&ioo->ioo_bufcnt);
1586 }
1587
1588 void lustre_swab_niobuf_remote (struct niobuf_remote *nbr)
1589 {
1590         __swab64s (&nbr->offset);
1591         __swab32s (&nbr->len);
1592         __swab32s (&nbr->flags);
1593 }
1594
1595 void lustre_swab_ost_body (struct ost_body *b)
1596 {
1597         lustre_swab_obdo (&b->oa);
1598 }
1599
1600 void lustre_swab_ost_last_id(obd_id *id)
1601 {
1602         __swab64s(id);
1603 }
1604
1605 void lustre_swab_generic_32s(__u32 *val)
1606 {
1607         __swab32s(val);
1608 }
1609
1610 void lustre_swab_ost_lvb(struct ost_lvb *lvb)
1611 {
1612         __swab64s(&lvb->lvb_size);
1613         __swab64s(&lvb->lvb_mtime);
1614         __swab64s(&lvb->lvb_atime);
1615         __swab64s(&lvb->lvb_ctime);
1616         __swab64s(&lvb->lvb_blocks);
1617 }
1618
1619 void lustre_swab_mds_status_req (struct mds_status_req *r)
1620 {
1621         __swab32s (&r->flags);
1622         __swab32s (&r->repbuf);
1623 }
1624
1625 void lustre_swab_mds_body (struct mds_body *b)
1626 {
1627         lustre_swab_ll_fid (&b->fid1);
1628         lustre_swab_ll_fid (&b->fid2);
1629         /* handle is opaque */
1630         __swab64s (&b->valid);
1631         __swab64s (&b->size);
1632         __swab64s (&b->mtime);
1633         __swab64s (&b->atime);
1634         __swab64s (&b->ctime);
1635         __swab64s (&b->blocks);
1636         __swab64s (&b->io_epoch);
1637         __swab64s (&b->ino);
1638         __swab32s (&b->fsuid);
1639         __swab32s (&b->fsgid);
1640         __swab32s (&b->capability);
1641         __swab32s (&b->mode);
1642         __swab32s (&b->uid);
1643         __swab32s (&b->gid);
1644         __swab32s (&b->flags);
1645         __swab32s (&b->rdev);
1646         __swab32s (&b->nlink);
1647         __swab32s (&b->generation);
1648         __swab32s (&b->suppgid);
1649         __swab32s (&b->eadatasize);
1650         __swab32s (&b->aclsize);
1651         __swab32s (&b->max_mdsize);
1652         __swab32s (&b->max_cookiesize);
1653         __swab32s (&b->padding_4);
1654 }
1655
1656 void lustre_swab_mdt_body (struct mdt_body *b)
1657 {
1658         lustre_swab_lu_fid (&b->fid1);
1659         lustre_swab_lu_fid (&b->fid2);
1660         /* handle is opaque */
1661         __swab64s (&b->valid);
1662         __swab64s (&b->size);
1663         __swab64s (&b->mtime);
1664         __swab64s (&b->atime);
1665         __swab64s (&b->ctime);
1666         __swab64s (&b->blocks);
1667         __swab64s (&b->ioepoch);
1668         __swab64s (&b->ino);
1669         __swab32s (&b->fsuid);
1670         __swab32s (&b->fsgid);
1671         __swab32s (&b->capability);
1672         __swab32s (&b->mode);
1673         __swab32s (&b->uid);
1674         __swab32s (&b->gid);
1675         __swab32s (&b->flags);
1676         __swab32s (&b->rdev);
1677         __swab32s (&b->nlink);
1678         __swab32s (&b->generation);
1679         __swab32s (&b->suppgid);
1680         __swab32s (&b->eadatasize);
1681         __swab32s (&b->aclsize);
1682         __swab32s (&b->max_mdsize);
1683         __swab32s (&b->max_cookiesize);
1684         __swab32s (&b->padding_4);
1685 }
1686
1687 void lustre_swab_mdt_epoch (struct mdt_epoch *b)
1688 {
1689         /* handle is opaque */
1690          __swab64s (&b->ioepoch);
1691          __swab32s (&b->flags);
1692          CLASSERT(offsetof(typeof(*b), padding) != 0);
1693 }
1694
1695 void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
1696 {
1697         int i;
1698         __swab32s(&mti->mti_lustre_ver);
1699         __swab32s(&mti->mti_stripe_index);
1700         __swab32s(&mti->mti_config_ver);
1701         __swab32s(&mti->mti_flags);
1702         __swab32s(&mti->mti_nid_count);
1703         CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
1704         for (i = 0; i < MTI_NIDS_MAX; i++)
1705                 __swab64s(&mti->mti_nids[i]);
1706 }
1707
1708 static void lustre_swab_obd_dqinfo (struct obd_dqinfo *i)
1709 {
1710         __swab64s (&i->dqi_bgrace);
1711         __swab64s (&i->dqi_igrace);
1712         __swab32s (&i->dqi_flags);
1713         __swab32s (&i->dqi_valid);
1714 }
1715
1716 static void lustre_swab_obd_dqblk (struct obd_dqblk *b)
1717 {
1718         __swab64s (&b->dqb_ihardlimit);
1719         __swab64s (&b->dqb_isoftlimit);
1720         __swab64s (&b->dqb_curinodes);
1721         __swab64s (&b->dqb_bhardlimit);
1722         __swab64s (&b->dqb_bsoftlimit);
1723         __swab64s (&b->dqb_curspace);
1724         __swab64s (&b->dqb_btime);
1725         __swab64s (&b->dqb_itime);
1726         __swab32s (&b->dqb_valid);
1727         CLASSERT(offsetof(typeof(*b), padding) != 0);
1728 }
1729
1730 void lustre_swab_obd_quotactl (struct obd_quotactl *q)
1731 {
1732         __swab32s (&q->qc_cmd);
1733         __swab32s (&q->qc_type);
1734         __swab32s (&q->qc_id);
1735         __swab32s (&q->qc_stat);
1736         lustre_swab_obd_dqinfo (&q->qc_dqinfo);
1737         lustre_swab_obd_dqblk (&q->qc_dqblk);
1738 }
1739
1740 void lustre_swab_mds_remote_perm (struct mds_remote_perm *p)
1741 {
1742         __swab32s (&p->rp_uid);
1743         __swab32s (&p->rp_gid);
1744         __swab32s (&p->rp_fsuid);
1745         __swab32s (&p->rp_fsgid);
1746         __swab32s (&p->rp_access_perm);
1747 };
1748
1749 void lustre_swab_mdt_remote_perm (struct mdt_remote_perm *p)
1750 {
1751         __swab32s (&p->rp_uid);
1752         __swab32s (&p->rp_gid);
1753         __swab32s (&p->rp_fsuid);
1754         __swab32s (&p->rp_fsgid);
1755         __swab32s (&p->rp_access_perm);
1756 };
1757
1758 void lustre_swab_mds_rec_setattr (struct mds_rec_setattr *sa)
1759 {
1760         __swab32s (&sa->sa_opcode);
1761         __swab32s (&sa->sa_fsuid);
1762         __swab32s (&sa->sa_fsgid);
1763         __swab32s (&sa->sa_cap);
1764         __swab32s (&sa->sa_suppgid);
1765         __swab32s (&sa->sa_mode);
1766         lustre_swab_ll_fid (&sa->sa_fid);
1767         __swab64s (&sa->sa_valid);
1768         __swab64s (&sa->sa_size);
1769         __swab64s (&sa->sa_mtime);
1770         __swab64s (&sa->sa_atime);
1771         __swab64s (&sa->sa_ctime);
1772         __swab32s (&sa->sa_uid);
1773         __swab32s (&sa->sa_gid);
1774         __swab32s (&sa->sa_attr_flags);
1775         CLASSERT(offsetof(typeof(*sa), sa_padding) != 0);
1776 }
1777
1778 void lustre_swab_mds_rec_join (struct mds_rec_join *jr)
1779 {
1780         __swab64s(&jr->jr_headsize);
1781         lustre_swab_ll_fid(&jr->jr_fid);
1782 }
1783
1784 void lustre_swab_mdt_rec_join (struct mdt_rec_join *jr)
1785 {
1786         __swab64s(&jr->jr_headsize);
1787         lustre_swab_lu_fid(&jr->jr_fid);
1788 }
1789
1790 void lustre_swab_mds_rec_create (struct mds_rec_create *cr)
1791 {
1792         __swab32s (&cr->cr_opcode);
1793         __swab32s (&cr->cr_fsuid);
1794         __swab32s (&cr->cr_fsgid);
1795         __swab32s (&cr->cr_cap);
1796         __swab32s (&cr->cr_flags); /* for use with open */
1797         __swab32s (&cr->cr_mode);
1798         lustre_swab_ll_fid (&cr->cr_fid);
1799         lustre_swab_ll_fid (&cr->cr_replayfid);
1800         __swab64s (&cr->cr_time);
1801         __swab64s (&cr->cr_rdev);
1802         __swab32s (&cr->cr_suppgid);
1803         CLASSERT(offsetof(typeof(*cr), cr_padding_1) != 0);
1804         CLASSERT(offsetof(typeof(*cr), cr_padding_2) != 0);
1805         CLASSERT(offsetof(typeof(*cr), cr_padding_3) != 0);
1806         CLASSERT(offsetof(typeof(*cr), cr_padding_4) != 0);
1807         CLASSERT(offsetof(typeof(*cr), cr_padding_5) != 0);
1808 }
1809
1810 void lustre_swab_mds_rec_link (struct mds_rec_link *lk)
1811 {
1812         __swab32s (&lk->lk_opcode);
1813         __swab32s (&lk->lk_fsuid);
1814         __swab32s (&lk->lk_fsgid);
1815         __swab32s (&lk->lk_cap);
1816         __swab32s (&lk->lk_suppgid1);
1817         __swab32s (&lk->lk_suppgid2);
1818         lustre_swab_ll_fid (&lk->lk_fid1);
1819         lustre_swab_ll_fid (&lk->lk_fid2);
1820         __swab64s (&lk->lk_time);
1821         CLASSERT(offsetof(typeof(*lk), lk_padding_1) != 0);
1822         CLASSERT(offsetof(typeof(*lk), lk_padding_2) != 0);
1823         CLASSERT(offsetof(typeof(*lk), lk_padding_3) != 0);
1824         CLASSERT(offsetof(typeof(*lk), lk_padding_4) != 0);
1825 }
1826
1827 void lustre_swab_mds_rec_unlink (struct mds_rec_unlink *ul)
1828 {
1829         __swab32s (&ul->ul_opcode);
1830         __swab32s (&ul->ul_fsuid);
1831         __swab32s (&ul->ul_fsgid);
1832         __swab32s (&ul->ul_cap);
1833         __swab32s (&ul->ul_suppgid);
1834         __swab32s (&ul->ul_mode);
1835         lustre_swab_ll_fid (&ul->ul_fid1);
1836         lustre_swab_ll_fid (&ul->ul_fid2);
1837         __swab64s (&ul->ul_time);
1838         CLASSERT(offsetof(typeof(*ul), ul_padding_1) != 0);
1839         CLASSERT(offsetof(typeof(*ul), ul_padding_2) != 0);
1840         CLASSERT(offsetof(typeof(*ul), ul_padding_3) != 0);
1841         CLASSERT(offsetof(typeof(*ul), ul_padding_4) != 0);
1842 }
1843
1844 void lustre_swab_mds_rec_rename (struct mds_rec_rename *rn)
1845 {
1846         __swab32s (&rn->rn_opcode);
1847         __swab32s (&rn->rn_fsuid);
1848         __swab32s (&rn->rn_fsgid);
1849         __swab32s (&rn->rn_cap);
1850         __swab32s (&rn->rn_suppgid1);
1851         __swab32s (&rn->rn_suppgid2);
1852         lustre_swab_ll_fid (&rn->rn_fid1);
1853         lustre_swab_ll_fid (&rn->rn_fid2);
1854         __swab64s (&rn->rn_time);
1855         CLASSERT(offsetof(typeof(*rn), rn_padding_1) != 0);
1856         CLASSERT(offsetof(typeof(*rn), rn_padding_2) != 0);
1857         CLASSERT(offsetof(typeof(*rn), rn_padding_3) != 0);
1858         CLASSERT(offsetof(typeof(*rn), rn_padding_4) != 0);
1859 }
1860
1861 void lustre_swab_mdt_rec_reint (struct mdt_rec_reint *rr)
1862 {
1863         __swab32s (&rr->rr_opcode);
1864         __swab32s (&rr->rr_fsuid);
1865         __swab32s (&rr->rr_fsgid);
1866         __swab32s (&rr->rr_cap);
1867         __swab32s (&rr->rr_suppgid1);
1868         __swab32s (&rr->rr_suppgid2);
1869         /* handle is opaque */
1870         lustre_swab_lu_fid (&rr->rr_fid1);
1871         lustre_swab_lu_fid (&rr->rr_fid2);
1872         __swab64s (&rr->rr_mtime);
1873         __swab64s (&rr->rr_atime);
1874         __swab64s (&rr->rr_ctime);
1875         __swab64s (&rr->rr_size);
1876         __swab64s (&rr->rr_blocks);
1877         __swab32s (&rr->rr_bias);
1878         __swab32s (&rr->rr_mode);
1879         __swab32s (&rr->rr_padding_1);
1880         __swab32s (&rr->rr_padding_2);
1881         __swab32s (&rr->rr_padding_3);
1882         __swab32s (&rr->rr_padding_4);
1883
1884         CLASSERT(offsetof(typeof(*rr), rr_padding_1) != 0);
1885         CLASSERT(offsetof(typeof(*rr), rr_padding_2) != 0);
1886         CLASSERT(offsetof(typeof(*rr), rr_padding_3) != 0);
1887         CLASSERT(offsetof(typeof(*rr), rr_padding_4) != 0);
1888 };
1889
1890 void lustre_swab_lov_desc (struct lov_desc *ld)
1891 {
1892         __swab32s (&ld->ld_tgt_count);
1893         __swab32s (&ld->ld_active_tgt_count);
1894         __swab32s (&ld->ld_default_stripe_count);
1895         __swab64s (&ld->ld_default_stripe_size);
1896         __swab64s (&ld->ld_default_stripe_offset);
1897         __swab32s (&ld->ld_pattern);
1898         __swab32s (&ld->ld_qos_maxage);
1899         /* uuid endian insensitive */
1900 }
1901
1902 /*begin adding MDT by huanghua@clusterfs.com*/
1903 void lustre_swab_lmv_desc (struct lmv_desc *ld)
1904 {
1905         __swab32s (&ld->ld_tgt_count);
1906         __swab32s (&ld->ld_active_tgt_count);
1907         /* uuid endian insensitive */
1908 }
1909 /*end adding MDT by huanghua@clusterfs.com*/
1910 void lustre_swab_md_fld (struct md_fld *mf)
1911 {
1912         __swab64s(&mf->mf_seq);
1913         __swab64s(&mf->mf_mds);
1914 }
1915
1916 static void print_lum (struct lov_user_md *lum)
1917 {
1918         CDEBUG(D_OTHER, "lov_user_md %p:\n", lum);
1919         CDEBUG(D_OTHER, "\tlmm_magic: %#x\n", lum->lmm_magic);
1920         CDEBUG(D_OTHER, "\tlmm_pattern: %#x\n", lum->lmm_pattern);
1921         CDEBUG(D_OTHER, "\tlmm_object_id: "LPU64"\n", lum->lmm_object_id);
1922         CDEBUG(D_OTHER, "\tlmm_object_gr: "LPU64"\n", lum->lmm_object_gr);
1923         CDEBUG(D_OTHER, "\tlmm_stripe_size: %#x\n", lum->lmm_stripe_size);
1924         CDEBUG(D_OTHER, "\tlmm_stripe_count: %#x\n", lum->lmm_stripe_count);
1925         CDEBUG(D_OTHER, "\tlmm_stripe_offset: %#x\n", lum->lmm_stripe_offset);
1926 }
1927
1928 void lustre_swab_lov_user_md(struct lov_user_md *lum)
1929 {
1930         ENTRY;
1931         CDEBUG(D_IOCTL, "swabbing lov_user_md\n");
1932         __swab32s(&lum->lmm_magic);
1933         __swab32s(&lum->lmm_pattern);
1934         __swab64s(&lum->lmm_object_id);
1935         __swab64s(&lum->lmm_object_gr);
1936         __swab32s(&lum->lmm_stripe_size);
1937         __swab16s(&lum->lmm_stripe_count);
1938         __swab16s(&lum->lmm_stripe_offset);
1939         print_lum(lum);
1940         EXIT;
1941 }
1942
1943 static void print_lumj (struct lov_user_md_join *lumj)
1944 {
1945         CDEBUG(D_OTHER, "lov_user_md %p:\n", lumj);
1946         CDEBUG(D_OTHER, "\tlmm_magic: %#x\n", lumj->lmm_magic);
1947         CDEBUG(D_OTHER, "\tlmm_pattern: %#x\n", lumj->lmm_pattern);
1948         CDEBUG(D_OTHER, "\tlmm_object_id: "LPU64"\n", lumj->lmm_object_id);
1949         CDEBUG(D_OTHER, "\tlmm_object_gr: "LPU64"\n", lumj->lmm_object_gr);
1950         CDEBUG(D_OTHER, "\tlmm_stripe_size: %#x\n", lumj->lmm_stripe_size);
1951         CDEBUG(D_OTHER, "\tlmm_stripe_count: %#x\n", lumj->lmm_stripe_count);
1952         CDEBUG(D_OTHER, "\tlmm_extent_count: %#x\n", lumj->lmm_extent_count);
1953 }
1954
1955 void lustre_swab_lov_user_md_join(struct lov_user_md_join *lumj)
1956 {
1957         ENTRY;
1958         CDEBUG(D_IOCTL, "swabbing lov_user_md_join\n");
1959         __swab32s(&lumj->lmm_magic);
1960         __swab32s(&lumj->lmm_pattern);
1961         __swab64s(&lumj->lmm_object_id);
1962         __swab64s(&lumj->lmm_object_gr);
1963         __swab32s(&lumj->lmm_stripe_size);
1964         __swab32s(&lumj->lmm_stripe_count);
1965         __swab32s(&lumj->lmm_extent_count);
1966         print_lumj(lumj);
1967         EXIT;
1968 }
1969
1970 static void print_lum_objs(struct lov_user_md *lum)
1971 {
1972         struct lov_user_ost_data *lod;
1973         int i;
1974         ENTRY;
1975         if (!(libcfs_debug & D_OTHER)) /* don't loop on nothing */
1976                 return;
1977         CDEBUG(D_OTHER, "lov_user_md_objects: %p\n", lum);
1978         for (i = 0; i < lum->lmm_stripe_count; i++) {
1979                 lod = &lum->lmm_objects[i];
1980                 CDEBUG(D_OTHER, "(%i) lod->l_object_id: "LPX64"\n", i, lod->l_object_id);
1981                 CDEBUG(D_OTHER, "(%i) lod->l_object_gr: "LPX64"\n", i, lod->l_object_gr);
1982                 CDEBUG(D_OTHER, "(%i) lod->l_ost_gen: %#x\n", i, lod->l_ost_gen);
1983                 CDEBUG(D_OTHER, "(%i) lod->l_ost_idx: %#x\n", i, lod->l_ost_idx);
1984         }
1985         EXIT;
1986 }
1987
1988 void lustre_swab_lov_user_md_objects(struct lov_user_md *lum)
1989 {
1990         struct lov_user_ost_data *lod;
1991         int i;
1992         ENTRY;
1993         for (i = 0; i < lum->lmm_stripe_count; i++) {
1994                 lod = &lum->lmm_objects[i];
1995                 __swab64s(&lod->l_object_id);
1996                 __swab64s(&lod->l_object_gr);
1997                 __swab32s(&lod->l_ost_gen);
1998                 __swab32s(&lod->l_ost_idx);
1999         }
2000         print_lum_objs(lum);
2001         EXIT;
2002 }
2003
2004
2005 void lustre_swab_lov_mds_md(struct lov_mds_md *lmm)
2006 {
2007         struct lov_ost_data *lod;
2008         int i;
2009         ENTRY;
2010         for (i = 0; i < lmm->lmm_stripe_count; i++) {
2011                 lod = &lmm->lmm_objects[i];
2012                 __swab64s(&lod->l_object_id);
2013                 __swab64s(&lod->l_object_gr);
2014                 __swab32s(&lod->l_ost_gen);
2015                 __swab32s(&lod->l_ost_idx);
2016         }
2017         __swab32s(&lmm->lmm_magic);
2018         __swab32s(&lmm->lmm_pattern);
2019         __swab64s(&lmm->lmm_object_id);
2020         __swab64s(&lmm->lmm_object_gr);
2021         __swab32s(&lmm->lmm_stripe_size);
2022         __swab32s(&lmm->lmm_stripe_count);
2023
2024         EXIT;
2025 }
2026
2027
2028 void lustre_swab_ldlm_res_id (struct ldlm_res_id *id)
2029 {
2030         int  i;
2031
2032         for (i = 0; i < RES_NAME_SIZE; i++)
2033                 __swab64s (&id->name[i]);
2034 }
2035
2036 void lustre_swab_ldlm_policy_data (ldlm_policy_data_t *d)
2037 {
2038         /* the lock data is a union and the first two fields are always an
2039          * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
2040          * data the same way. */
2041         __swab64s(&d->l_extent.start);
2042         __swab64s(&d->l_extent.end);
2043         __swab64s(&d->l_extent.gid);
2044         __swab32s(&d->l_flock.pid);
2045 }
2046
2047 void lustre_swab_ldlm_intent (struct ldlm_intent *i)
2048 {
2049         __swab64s (&i->opc);
2050 }
2051
2052 void lustre_swab_ldlm_resource_desc (struct ldlm_resource_desc *r)
2053 {
2054         __swab32s (&r->lr_type);
2055         CLASSERT(offsetof(typeof(*r), lr_padding) != 0);
2056         lustre_swab_ldlm_res_id (&r->lr_name);
2057 }
2058
2059 void lustre_swab_ldlm_lock_desc (struct ldlm_lock_desc *l)
2060 {
2061         lustre_swab_ldlm_resource_desc (&l->l_resource);
2062         __swab32s (&l->l_req_mode);
2063         __swab32s (&l->l_granted_mode);
2064         lustre_swab_ldlm_policy_data (&l->l_policy_data);
2065 }
2066
2067 void lustre_swab_ldlm_request (struct ldlm_request *rq)
2068 {
2069         __swab32s (&rq->lock_flags);
2070         lustre_swab_ldlm_lock_desc (&rq->lock_desc);
2071         __swab32s (&rq->lock_count);
2072         /* lock_handle[] opaque */
2073 }
2074
2075 void lustre_swab_ldlm_reply (struct ldlm_reply *r)
2076 {
2077         __swab32s (&r->lock_flags);
2078         CLASSERT(offsetof(typeof(*r), lock_padding) != 0);
2079         lustre_swab_ldlm_lock_desc (&r->lock_desc);
2080         /* lock_handle opaque */
2081         __swab64s (&r->lock_policy_res1);
2082         __swab64s (&r->lock_policy_res2);
2083 }
2084
2085 /* no one calls this */
2086 int llog_log_swabbed(struct llog_log_hdr *hdr)
2087 {
2088         if (hdr->llh_hdr.lrh_type == __swab32(LLOG_HDR_MAGIC))
2089                 return 1;
2090         if (hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC)
2091                 return 0;
2092         return -1;
2093 }
2094
2095 void lustre_swab_qdata(struct qunit_data *d)
2096 {
2097         __swab32s (&d->qd_id);
2098         __swab32s (&d->qd_flags);
2099         __swab64s (&d->qd_count);
2100 }
2101
2102 void lustre_swab_qdata_old(struct qunit_data_old *d)
2103 {
2104         __swab32s (&d->qd_id);
2105         __swab32s (&d->qd_type);
2106         __swab32s (&d->qd_count);
2107         __swab32s (&d->qd_isblk);
2108 }
2109
2110 #ifdef __KERNEL__
2111 struct qunit_data *lustre_quota_old_to_new(struct qunit_data_old *d)
2112 {
2113         struct qunit_data_old tmp;
2114         struct qunit_data *ret;
2115         ENTRY;
2116
2117         if (!d)
2118                 return NULL;
2119
2120         tmp = *d;
2121         ret = (struct qunit_data *)d;
2122         ret->qd_id = tmp.qd_id;
2123         ret->qd_flags = (tmp.qd_type ? QUOTA_IS_GRP : 0) | (tmp.qd_isblk ? QUOTA_IS_BLOCK : 0);
2124         ret->qd_count = tmp.qd_count;
2125         RETURN(ret);
2126
2127 }
2128 EXPORT_SYMBOL(lustre_quota_old_to_new);
2129
2130 struct qunit_data_old *lustre_quota_new_to_old(struct qunit_data *d)
2131 {
2132         struct qunit_data tmp;
2133         struct qunit_data_old *ret;
2134         ENTRY;
2135
2136         if (!d)
2137                 return NULL;
2138
2139         tmp = *d;
2140         ret = (struct qunit_data_old *)d;
2141         ret->qd_id = tmp.qd_id;
2142         ret->qd_type = ((tmp.qd_flags & QUOTA_IS_GRP) ? GRPQUOTA : USRQUOTA);
2143         ret->qd_count = (__u32)tmp.qd_count;
2144         ret->qd_isblk = ((tmp.qd_flags & QUOTA_IS_BLOCK) ? 1 : 0);
2145         RETURN(ret);
2146 }
2147 EXPORT_SYMBOL(lustre_quota_new_to_old);
2148 #endif /* __KERNEL__ */
2149
2150 static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2151 {
2152         LASSERT(req->rq_reqmsg);
2153
2154         switch (req->rq_reqmsg->lm_magic) {
2155         case LUSTRE_MSG_MAGIC_V2:
2156         case LUSTRE_MSG_MAGIC_V2_SWABBED:
2157                 return lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
2158         default:
2159                 CERROR("bad lustre msg magic: %#08X\n",
2160                        req->rq_reqmsg->lm_magic);
2161         }
2162         return 0;
2163 }
2164
2165 static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2166 {
2167         LASSERT(req->rq_repmsg);
2168
2169         switch (req->rq_repmsg->lm_magic) {
2170         case LUSTRE_MSG_MAGIC_V2:
2171         case LUSTRE_MSG_MAGIC_V2_SWABBED:
2172                 return lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
2173         default:
2174                 /* uninitialized yet */
2175                 return 0;
2176         }
2177 }
2178
2179 void _debug_req(struct ptlrpc_request *req, __u32 mask,
2180                 struct libcfs_debug_msg_data *data, const char *fmt, ... )
2181 {
2182         va_list args;
2183
2184         va_start(args, fmt);
2185         libcfs_debug_vmsg2(data->msg_cdls, data->msg_subsys, mask, data->msg_file,
2186                            data->msg_fn, data->msg_line, fmt, args,
2187                            " req@%p x"LPD64"/t"LPD64"("LPD64") o%d->%s@%s:%d/%d"
2188                            " lens %d/%d e %d to %d dl %ld ref %d "
2189                            "fl "REQ_FLAGS_FMT"/%x/%x rc %d/%d\n",
2190                            req, req->rq_xid, req->rq_transno,
2191                            req->rq_reqmsg ? lustre_msg_get_transno(req->rq_reqmsg) : 0,
2192                            req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : -1,
2193                            req->rq_import ? obd2cli_tgt(req->rq_import->imp_obd) :
2194                            req->rq_export ?
2195                            (char*)req->rq_export->exp_client_uuid.uuid : "<?>",
2196                            req->rq_import ?
2197                            (char *)req->rq_import->imp_connection->c_remote_uuid.uuid :
2198                            req->rq_export ?
2199                            (char *)req->rq_export->exp_connection->c_remote_uuid.uuid : "<?>",
2200                            req->rq_request_portal, req->rq_reply_portal,
2201                            req->rq_reqlen, req->rq_replen,
2202                            req->rq_early_count, req->rq_timeout, req->rq_deadline,
2203                            atomic_read(&req->rq_refcount), DEBUG_REQ_FLAGS(req),
2204                            req->rq_reqmsg && req_ptlrpc_body_swabbed(req) ?
2205                            lustre_msg_get_flags(req->rq_reqmsg) : -1,
2206                            req->rq_repmsg && rep_ptlrpc_body_swabbed(req) ?
2207                            lustre_msg_get_flags(req->rq_repmsg) : -1,
2208                            req->rq_status,
2209                            req->rq_repmsg && rep_ptlrpc_body_swabbed(req) ?
2210                            lustre_msg_get_status(req->rq_repmsg) : -1);
2211 }
2212 EXPORT_SYMBOL(_debug_req);
2213
2214 void lustre_swab_lustre_capa(struct lustre_capa *c)
2215 {
2216         lustre_swab_lu_fid(&c->lc_fid);
2217         __swab64s (&c->lc_opc);
2218         __swab32s (&c->lc_uid);
2219         __swab32s (&c->lc_flags);
2220         __swab32s (&c->lc_keyid);
2221         __swab32s (&c->lc_timeout);
2222         __swab64s (&c->lc_expiry);
2223 }
2224
2225 void lustre_swab_lustre_capa_key (struct lustre_capa_key *k)
2226 {
2227         __swab64s (&k->lk_mdsid);
2228         __swab32s (&k->lk_keyid);
2229         __swab32s (&k->lk_padding);
2230 }