Whamcloud - gitweb
acl includes and time_t formats cleanup.
[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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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         __swab64s (&b->pb_pre_versions[0]);
1519         __swab64s (&b->pb_pre_versions[1]);
1520         __swab64s (&b->pb_pre_versions[2]);
1521         __swab64s (&b->pb_pre_versions[3]);
1522         CLASSERT(offsetof(typeof(*b), pb_padding) != 0);
1523 }
1524
1525 void lustre_swab_connect(struct obd_connect_data *ocd)
1526 {
1527         __swab64s(&ocd->ocd_connect_flags);
1528         __swab32s(&ocd->ocd_version);
1529         __swab32s(&ocd->ocd_grant);
1530         __swab64s(&ocd->ocd_ibits_known);
1531         __swab32s(&ocd->ocd_index);
1532         __swab32s(&ocd->ocd_brw_size);
1533         __swab32s(&ocd->ocd_nllu);
1534         __swab32s(&ocd->ocd_nllg);
1535         __swab64s(&ocd->ocd_transno);
1536         __swab32s(&ocd->ocd_group);
1537         __swab32s(&ocd->ocd_cksum_types);
1538         CLASSERT(offsetof(typeof(*ocd), padding1) != 0);
1539         CLASSERT(offsetof(typeof(*ocd), padding2) != 0);
1540 }
1541
1542 void lustre_swab_obdo (struct obdo  *o)
1543 {
1544         __swab64s (&o->o_valid);
1545         __swab64s (&o->o_id);
1546         __swab64s (&o->o_gr);
1547         __swab64s (&o->o_fid);
1548         __swab64s (&o->o_size);
1549         __swab64s (&o->o_mtime);
1550         __swab64s (&o->o_atime);
1551         __swab64s (&o->o_ctime);
1552         __swab64s (&o->o_blocks);
1553         __swab64s (&o->o_grant);
1554         __swab32s (&o->o_blksize);
1555         __swab32s (&o->o_mode);
1556         __swab32s (&o->o_uid);
1557         __swab32s (&o->o_gid);
1558         __swab32s (&o->o_flags);
1559         __swab32s (&o->o_nlink);
1560         __swab32s (&o->o_generation);
1561         __swab32s (&o->o_misc);
1562         __swab32s (&o->o_easize);
1563         __swab32s (&o->o_mds);
1564         __swab32s (&o->o_stripe_idx);
1565         __swab32s (&o->o_padding_1);
1566         /* o_inline is opaque */
1567 }
1568
1569 void lustre_swab_obd_statfs (struct obd_statfs *os)
1570 {
1571         __swab64s (&os->os_type);
1572         __swab64s (&os->os_blocks);
1573         __swab64s (&os->os_bfree);
1574         __swab64s (&os->os_bavail);
1575         __swab64s (&os->os_files);
1576         __swab64s (&os->os_ffree);
1577         /* no need to swab os_fsid */
1578         __swab32s (&os->os_bsize);
1579         __swab32s (&os->os_namelen);
1580         __swab64s (&os->os_maxbytes);
1581         __swab32s (&os->os_state);
1582         /* no need to swap os_spare */
1583 }
1584
1585 void lustre_swab_obd_ioobj (struct obd_ioobj *ioo)
1586 {
1587         __swab64s (&ioo->ioo_id);
1588         __swab64s (&ioo->ioo_gr);
1589         __swab32s (&ioo->ioo_type);
1590         __swab32s (&ioo->ioo_bufcnt);
1591 }
1592
1593 void lustre_swab_niobuf_remote (struct niobuf_remote *nbr)
1594 {
1595         __swab64s (&nbr->offset);
1596         __swab32s (&nbr->len);
1597         __swab32s (&nbr->flags);
1598 }
1599
1600 void lustre_swab_ost_body (struct ost_body *b)
1601 {
1602         lustre_swab_obdo (&b->oa);
1603 }
1604
1605 void lustre_swab_ost_last_id(obd_id *id)
1606 {
1607         __swab64s(id);
1608 }
1609
1610 void lustre_swab_generic_32s(__u32 *val)
1611 {
1612         __swab32s(val);
1613 }
1614
1615 void lustre_swab_ost_lvb(struct ost_lvb *lvb)
1616 {
1617         __swab64s(&lvb->lvb_size);
1618         __swab64s(&lvb->lvb_mtime);
1619         __swab64s(&lvb->lvb_atime);
1620         __swab64s(&lvb->lvb_ctime);
1621         __swab64s(&lvb->lvb_blocks);
1622 }
1623
1624 void lustre_swab_mds_status_req (struct mds_status_req *r)
1625 {
1626         __swab32s (&r->flags);
1627         __swab32s (&r->repbuf);
1628 }
1629
1630 void lustre_swab_mds_body (struct mds_body *b)
1631 {
1632         lustre_swab_ll_fid (&b->fid1);
1633         lustre_swab_ll_fid (&b->fid2);
1634         /* handle is opaque */
1635         __swab64s (&b->valid);
1636         __swab64s (&b->size);
1637         __swab64s (&b->mtime);
1638         __swab64s (&b->atime);
1639         __swab64s (&b->ctime);
1640         __swab64s (&b->blocks);
1641         __swab64s (&b->io_epoch);
1642         __swab64s (&b->ino);
1643         __swab32s (&b->fsuid);
1644         __swab32s (&b->fsgid);
1645         __swab32s (&b->capability);
1646         __swab32s (&b->mode);
1647         __swab32s (&b->uid);
1648         __swab32s (&b->gid);
1649         __swab32s (&b->flags);
1650         __swab32s (&b->rdev);
1651         __swab32s (&b->nlink);
1652         __swab32s (&b->generation);
1653         __swab32s (&b->suppgid);
1654         __swab32s (&b->eadatasize);
1655         __swab32s (&b->aclsize);
1656         __swab32s (&b->max_mdsize);
1657         __swab32s (&b->max_cookiesize);
1658         __swab32s (&b->padding_4);
1659 }
1660
1661 void lustre_swab_mdt_body (struct mdt_body *b)
1662 {
1663         lustre_swab_lu_fid (&b->fid1);
1664         lustre_swab_lu_fid (&b->fid2);
1665         /* handle is opaque */
1666         __swab64s (&b->valid);
1667         __swab64s (&b->size);
1668         __swab64s (&b->mtime);
1669         __swab64s (&b->atime);
1670         __swab64s (&b->ctime);
1671         __swab64s (&b->blocks);
1672         __swab64s (&b->ioepoch);
1673         __swab64s (&b->ino);
1674         __swab32s (&b->fsuid);
1675         __swab32s (&b->fsgid);
1676         __swab32s (&b->capability);
1677         __swab32s (&b->mode);
1678         __swab32s (&b->uid);
1679         __swab32s (&b->gid);
1680         __swab32s (&b->flags);
1681         __swab32s (&b->rdev);
1682         __swab32s (&b->nlink);
1683         __swab32s (&b->generation);
1684         __swab32s (&b->suppgid);
1685         __swab32s (&b->eadatasize);
1686         __swab32s (&b->aclsize);
1687         __swab32s (&b->max_mdsize);
1688         __swab32s (&b->max_cookiesize);
1689         __swab32s (&b->padding_4);
1690 }
1691
1692 void lustre_swab_mdt_epoch (struct mdt_epoch *b)
1693 {
1694         /* handle is opaque */
1695          __swab64s (&b->ioepoch);
1696          __swab32s (&b->flags);
1697          CLASSERT(offsetof(typeof(*b), padding) != 0);
1698 }
1699
1700 void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
1701 {
1702         int i;
1703         __swab32s(&mti->mti_lustre_ver);
1704         __swab32s(&mti->mti_stripe_index);
1705         __swab32s(&mti->mti_config_ver);
1706         __swab32s(&mti->mti_flags);
1707         __swab32s(&mti->mti_nid_count);
1708         CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
1709         for (i = 0; i < MTI_NIDS_MAX; i++)
1710                 __swab64s(&mti->mti_nids[i]);
1711 }
1712
1713 static void lustre_swab_obd_dqinfo (struct obd_dqinfo *i)
1714 {
1715         __swab64s (&i->dqi_bgrace);
1716         __swab64s (&i->dqi_igrace);
1717         __swab32s (&i->dqi_flags);
1718         __swab32s (&i->dqi_valid);
1719 }
1720
1721 static void lustre_swab_obd_dqblk (struct obd_dqblk *b)
1722 {
1723         __swab64s (&b->dqb_ihardlimit);
1724         __swab64s (&b->dqb_isoftlimit);
1725         __swab64s (&b->dqb_curinodes);
1726         __swab64s (&b->dqb_bhardlimit);
1727         __swab64s (&b->dqb_bsoftlimit);
1728         __swab64s (&b->dqb_curspace);
1729         __swab64s (&b->dqb_btime);
1730         __swab64s (&b->dqb_itime);
1731         __swab32s (&b->dqb_valid);
1732         CLASSERT(offsetof(typeof(*b), padding) != 0);
1733 }
1734
1735 void lustre_swab_obd_quotactl (struct obd_quotactl *q)
1736 {
1737         __swab32s (&q->qc_cmd);
1738         __swab32s (&q->qc_type);
1739         __swab32s (&q->qc_id);
1740         __swab32s (&q->qc_stat);
1741         lustre_swab_obd_dqinfo (&q->qc_dqinfo);
1742         lustre_swab_obd_dqblk (&q->qc_dqblk);
1743 }
1744
1745 void lustre_swab_mds_remote_perm (struct mds_remote_perm *p)
1746 {
1747         __swab32s (&p->rp_uid);
1748         __swab32s (&p->rp_gid);
1749         __swab32s (&p->rp_fsuid);
1750         __swab32s (&p->rp_fsgid);
1751         __swab32s (&p->rp_access_perm);
1752 };
1753
1754 void lustre_swab_mdt_remote_perm (struct mdt_remote_perm *p)
1755 {
1756         __swab32s (&p->rp_uid);
1757         __swab32s (&p->rp_gid);
1758         __swab32s (&p->rp_fsuid);
1759         __swab32s (&p->rp_fsgid);
1760         __swab32s (&p->rp_access_perm);
1761 };
1762
1763 void lustre_swab_mds_rec_setattr (struct mds_rec_setattr *sa)
1764 {
1765         __swab32s (&sa->sa_opcode);
1766         __swab32s (&sa->sa_fsuid);
1767         __swab32s (&sa->sa_fsgid);
1768         __swab32s (&sa->sa_cap);
1769         __swab32s (&sa->sa_suppgid);
1770         __swab32s (&sa->sa_mode);
1771         lustre_swab_ll_fid (&sa->sa_fid);
1772         __swab64s (&sa->sa_valid);
1773         __swab64s (&sa->sa_size);
1774         __swab64s (&sa->sa_mtime);
1775         __swab64s (&sa->sa_atime);
1776         __swab64s (&sa->sa_ctime);
1777         __swab32s (&sa->sa_uid);
1778         __swab32s (&sa->sa_gid);
1779         __swab32s (&sa->sa_attr_flags);
1780         CLASSERT(offsetof(typeof(*sa), sa_padding) != 0);
1781 }
1782
1783 void lustre_swab_mds_rec_join (struct mds_rec_join *jr)
1784 {
1785         __swab64s(&jr->jr_headsize);
1786         lustre_swab_ll_fid(&jr->jr_fid);
1787 }
1788
1789 void lustre_swab_mdt_rec_join (struct mdt_rec_join *jr)
1790 {
1791         __swab64s(&jr->jr_headsize);
1792         lustre_swab_lu_fid(&jr->jr_fid);
1793 }
1794
1795 void lustre_swab_mds_rec_create (struct mds_rec_create *cr)
1796 {
1797         __swab32s (&cr->cr_opcode);
1798         __swab32s (&cr->cr_fsuid);
1799         __swab32s (&cr->cr_fsgid);
1800         __swab32s (&cr->cr_cap);
1801         __swab32s (&cr->cr_flags); /* for use with open */
1802         __swab32s (&cr->cr_mode);
1803         lustre_swab_ll_fid (&cr->cr_fid);
1804         lustre_swab_ll_fid (&cr->cr_replayfid);
1805         __swab64s (&cr->cr_time);
1806         __swab64s (&cr->cr_rdev);
1807         __swab32s (&cr->cr_suppgid);
1808         CLASSERT(offsetof(typeof(*cr), cr_padding_1) != 0);
1809         CLASSERT(offsetof(typeof(*cr), cr_padding_2) != 0);
1810         CLASSERT(offsetof(typeof(*cr), cr_padding_3) != 0);
1811         CLASSERT(offsetof(typeof(*cr), cr_padding_4) != 0);
1812         CLASSERT(offsetof(typeof(*cr), cr_padding_5) != 0);
1813 }
1814
1815 void lustre_swab_mds_rec_link (struct mds_rec_link *lk)
1816 {
1817         __swab32s (&lk->lk_opcode);
1818         __swab32s (&lk->lk_fsuid);
1819         __swab32s (&lk->lk_fsgid);
1820         __swab32s (&lk->lk_cap);
1821         __swab32s (&lk->lk_suppgid1);
1822         __swab32s (&lk->lk_suppgid2);
1823         lustre_swab_ll_fid (&lk->lk_fid1);
1824         lustre_swab_ll_fid (&lk->lk_fid2);
1825         __swab64s (&lk->lk_time);
1826         CLASSERT(offsetof(typeof(*lk), lk_padding_1) != 0);
1827         CLASSERT(offsetof(typeof(*lk), lk_padding_2) != 0);
1828         CLASSERT(offsetof(typeof(*lk), lk_padding_3) != 0);
1829         CLASSERT(offsetof(typeof(*lk), lk_padding_4) != 0);
1830 }
1831
1832 void lustre_swab_mds_rec_unlink (struct mds_rec_unlink *ul)
1833 {
1834         __swab32s (&ul->ul_opcode);
1835         __swab32s (&ul->ul_fsuid);
1836         __swab32s (&ul->ul_fsgid);
1837         __swab32s (&ul->ul_cap);
1838         __swab32s (&ul->ul_suppgid);
1839         __swab32s (&ul->ul_mode);
1840         lustre_swab_ll_fid (&ul->ul_fid1);
1841         lustre_swab_ll_fid (&ul->ul_fid2);
1842         __swab64s (&ul->ul_time);
1843         CLASSERT(offsetof(typeof(*ul), ul_padding_1) != 0);
1844         CLASSERT(offsetof(typeof(*ul), ul_padding_2) != 0);
1845         CLASSERT(offsetof(typeof(*ul), ul_padding_3) != 0);
1846         CLASSERT(offsetof(typeof(*ul), ul_padding_4) != 0);
1847 }
1848
1849 void lustre_swab_mds_rec_rename (struct mds_rec_rename *rn)
1850 {
1851         __swab32s (&rn->rn_opcode);
1852         __swab32s (&rn->rn_fsuid);
1853         __swab32s (&rn->rn_fsgid);
1854         __swab32s (&rn->rn_cap);
1855         __swab32s (&rn->rn_suppgid1);
1856         __swab32s (&rn->rn_suppgid2);
1857         lustre_swab_ll_fid (&rn->rn_fid1);
1858         lustre_swab_ll_fid (&rn->rn_fid2);
1859         __swab64s (&rn->rn_time);
1860         CLASSERT(offsetof(typeof(*rn), rn_padding_1) != 0);
1861         CLASSERT(offsetof(typeof(*rn), rn_padding_2) != 0);
1862         CLASSERT(offsetof(typeof(*rn), rn_padding_3) != 0);
1863         CLASSERT(offsetof(typeof(*rn), rn_padding_4) != 0);
1864 }
1865
1866 void lustre_swab_mdt_rec_reint (struct mdt_rec_reint *rr)
1867 {
1868         __swab32s (&rr->rr_opcode);
1869         __swab32s (&rr->rr_fsuid);
1870         __swab32s (&rr->rr_fsgid);
1871         __swab32s (&rr->rr_cap);
1872         __swab32s (&rr->rr_suppgid1);
1873         __swab32s (&rr->rr_suppgid2);
1874         /* handle is opaque */
1875         lustre_swab_lu_fid (&rr->rr_fid1);
1876         lustre_swab_lu_fid (&rr->rr_fid2);
1877         __swab64s (&rr->rr_mtime);
1878         __swab64s (&rr->rr_atime);
1879         __swab64s (&rr->rr_ctime);
1880         __swab64s (&rr->rr_size);
1881         __swab64s (&rr->rr_blocks);
1882         __swab32s (&rr->rr_bias);
1883         __swab32s (&rr->rr_mode);
1884         __swab32s (&rr->rr_padding_1);
1885         __swab32s (&rr->rr_padding_2);
1886         __swab32s (&rr->rr_padding_3);
1887         __swab32s (&rr->rr_padding_4);
1888
1889         CLASSERT(offsetof(typeof(*rr), rr_padding_1) != 0);
1890         CLASSERT(offsetof(typeof(*rr), rr_padding_2) != 0);
1891         CLASSERT(offsetof(typeof(*rr), rr_padding_3) != 0);
1892         CLASSERT(offsetof(typeof(*rr), rr_padding_4) != 0);
1893 };
1894
1895 void lustre_swab_lov_desc (struct lov_desc *ld)
1896 {
1897         __swab32s (&ld->ld_tgt_count);
1898         __swab32s (&ld->ld_active_tgt_count);
1899         __swab32s (&ld->ld_default_stripe_count);
1900         __swab64s (&ld->ld_default_stripe_size);
1901         __swab64s (&ld->ld_default_stripe_offset);
1902         __swab32s (&ld->ld_pattern);
1903         __swab32s (&ld->ld_qos_maxage);
1904         /* uuid endian insensitive */
1905 }
1906
1907 /*begin adding MDT by huanghua@clusterfs.com*/
1908 void lustre_swab_lmv_desc (struct lmv_desc *ld)
1909 {
1910         __swab32s (&ld->ld_tgt_count);
1911         __swab32s (&ld->ld_active_tgt_count);
1912         /* uuid endian insensitive */
1913 }
1914 /*end adding MDT by huanghua@clusterfs.com*/
1915 void lustre_swab_md_fld (struct md_fld *mf)
1916 {
1917         __swab64s(&mf->mf_seq);
1918         __swab64s(&mf->mf_mds);
1919 }
1920
1921 static void print_lum (struct lov_user_md *lum)
1922 {
1923         CDEBUG(D_OTHER, "lov_user_md %p:\n", lum);
1924         CDEBUG(D_OTHER, "\tlmm_magic: %#x\n", lum->lmm_magic);
1925         CDEBUG(D_OTHER, "\tlmm_pattern: %#x\n", lum->lmm_pattern);
1926         CDEBUG(D_OTHER, "\tlmm_object_id: "LPU64"\n", lum->lmm_object_id);
1927         CDEBUG(D_OTHER, "\tlmm_object_gr: "LPU64"\n", lum->lmm_object_gr);
1928         CDEBUG(D_OTHER, "\tlmm_stripe_size: %#x\n", lum->lmm_stripe_size);
1929         CDEBUG(D_OTHER, "\tlmm_stripe_count: %#x\n", lum->lmm_stripe_count);
1930         CDEBUG(D_OTHER, "\tlmm_stripe_offset: %#x\n", lum->lmm_stripe_offset);
1931 }
1932
1933 void lustre_swab_lov_user_md(struct lov_user_md *lum)
1934 {
1935         ENTRY;
1936         CDEBUG(D_IOCTL, "swabbing lov_user_md\n");
1937         __swab32s(&lum->lmm_magic);
1938         __swab32s(&lum->lmm_pattern);
1939         __swab64s(&lum->lmm_object_id);
1940         __swab64s(&lum->lmm_object_gr);
1941         __swab32s(&lum->lmm_stripe_size);
1942         __swab16s(&lum->lmm_stripe_count);
1943         __swab16s(&lum->lmm_stripe_offset);
1944         print_lum(lum);
1945         EXIT;
1946 }
1947
1948 static void print_lumj (struct lov_user_md_join *lumj)
1949 {
1950         CDEBUG(D_OTHER, "lov_user_md %p:\n", lumj);
1951         CDEBUG(D_OTHER, "\tlmm_magic: %#x\n", lumj->lmm_magic);
1952         CDEBUG(D_OTHER, "\tlmm_pattern: %#x\n", lumj->lmm_pattern);
1953         CDEBUG(D_OTHER, "\tlmm_object_id: "LPU64"\n", lumj->lmm_object_id);
1954         CDEBUG(D_OTHER, "\tlmm_object_gr: "LPU64"\n", lumj->lmm_object_gr);
1955         CDEBUG(D_OTHER, "\tlmm_stripe_size: %#x\n", lumj->lmm_stripe_size);
1956         CDEBUG(D_OTHER, "\tlmm_stripe_count: %#x\n", lumj->lmm_stripe_count);
1957         CDEBUG(D_OTHER, "\tlmm_extent_count: %#x\n", lumj->lmm_extent_count);
1958 }
1959
1960 void lustre_swab_lov_user_md_join(struct lov_user_md_join *lumj)
1961 {
1962         ENTRY;
1963         CDEBUG(D_IOCTL, "swabbing lov_user_md_join\n");
1964         __swab32s(&lumj->lmm_magic);
1965         __swab32s(&lumj->lmm_pattern);
1966         __swab64s(&lumj->lmm_object_id);
1967         __swab64s(&lumj->lmm_object_gr);
1968         __swab32s(&lumj->lmm_stripe_size);
1969         __swab32s(&lumj->lmm_stripe_count);
1970         __swab32s(&lumj->lmm_extent_count);
1971         print_lumj(lumj);
1972         EXIT;
1973 }
1974
1975 static void print_lum_objs(struct lov_user_md *lum)
1976 {
1977         struct lov_user_ost_data *lod;
1978         int i;
1979         ENTRY;
1980         if (!(libcfs_debug & D_OTHER)) /* don't loop on nothing */
1981                 return;
1982         CDEBUG(D_OTHER, "lov_user_md_objects: %p\n", lum);
1983         for (i = 0; i < lum->lmm_stripe_count; i++) {
1984                 lod = &lum->lmm_objects[i];
1985                 CDEBUG(D_OTHER, "(%i) lod->l_object_id: "LPX64"\n", i, lod->l_object_id);
1986                 CDEBUG(D_OTHER, "(%i) lod->l_object_gr: "LPX64"\n", i, lod->l_object_gr);
1987                 CDEBUG(D_OTHER, "(%i) lod->l_ost_gen: %#x\n", i, lod->l_ost_gen);
1988                 CDEBUG(D_OTHER, "(%i) lod->l_ost_idx: %#x\n", i, lod->l_ost_idx);
1989         }
1990         EXIT;
1991 }
1992
1993 void lustre_swab_lov_user_md_objects(struct lov_user_md *lum)
1994 {
1995         struct lov_user_ost_data *lod;
1996         int i;
1997         ENTRY;
1998         for (i = 0; i < lum->lmm_stripe_count; i++) {
1999                 lod = &lum->lmm_objects[i];
2000                 __swab64s(&lod->l_object_id);
2001                 __swab64s(&lod->l_object_gr);
2002                 __swab32s(&lod->l_ost_gen);
2003                 __swab32s(&lod->l_ost_idx);
2004         }
2005         print_lum_objs(lum);
2006         EXIT;
2007 }
2008
2009
2010 void lustre_swab_lov_mds_md(struct lov_mds_md *lmm)
2011 {
2012         struct lov_ost_data *lod;
2013         int i;
2014         ENTRY;
2015         for (i = 0; i < lmm->lmm_stripe_count; i++) {
2016                 lod = &lmm->lmm_objects[i];
2017                 __swab64s(&lod->l_object_id);
2018                 __swab64s(&lod->l_object_gr);
2019                 __swab32s(&lod->l_ost_gen);
2020                 __swab32s(&lod->l_ost_idx);
2021         }
2022         __swab32s(&lmm->lmm_magic);
2023         __swab32s(&lmm->lmm_pattern);
2024         __swab64s(&lmm->lmm_object_id);
2025         __swab64s(&lmm->lmm_object_gr);
2026         __swab32s(&lmm->lmm_stripe_size);
2027         __swab32s(&lmm->lmm_stripe_count);
2028
2029         EXIT;
2030 }
2031
2032
2033 void lustre_swab_ldlm_res_id (struct ldlm_res_id *id)
2034 {
2035         int  i;
2036
2037         for (i = 0; i < RES_NAME_SIZE; i++)
2038                 __swab64s (&id->name[i]);
2039 }
2040
2041 void lustre_swab_ldlm_policy_data (ldlm_policy_data_t *d)
2042 {
2043         /* the lock data is a union and the first two fields are always an
2044          * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
2045          * data the same way. */
2046         __swab64s(&d->l_extent.start);
2047         __swab64s(&d->l_extent.end);
2048         __swab64s(&d->l_extent.gid);
2049         __swab32s(&d->l_flock.pid);
2050 }
2051
2052 void lustre_swab_ldlm_intent (struct ldlm_intent *i)
2053 {
2054         __swab64s (&i->opc);
2055 }
2056
2057 void lustre_swab_ldlm_resource_desc (struct ldlm_resource_desc *r)
2058 {
2059         __swab32s (&r->lr_type);
2060         CLASSERT(offsetof(typeof(*r), lr_padding) != 0);
2061         lustre_swab_ldlm_res_id (&r->lr_name);
2062 }
2063
2064 void lustre_swab_ldlm_lock_desc (struct ldlm_lock_desc *l)
2065 {
2066         lustre_swab_ldlm_resource_desc (&l->l_resource);
2067         __swab32s (&l->l_req_mode);
2068         __swab32s (&l->l_granted_mode);
2069         lustre_swab_ldlm_policy_data (&l->l_policy_data);
2070 }
2071
2072 void lustre_swab_ldlm_request (struct ldlm_request *rq)
2073 {
2074         __swab32s (&rq->lock_flags);
2075         lustre_swab_ldlm_lock_desc (&rq->lock_desc);
2076         __swab32s (&rq->lock_count);
2077         /* lock_handle[] opaque */
2078 }
2079
2080 void lustre_swab_ldlm_reply (struct ldlm_reply *r)
2081 {
2082         __swab32s (&r->lock_flags);
2083         CLASSERT(offsetof(typeof(*r), lock_padding) != 0);
2084         lustre_swab_ldlm_lock_desc (&r->lock_desc);
2085         /* lock_handle opaque */
2086         __swab64s (&r->lock_policy_res1);
2087         __swab64s (&r->lock_policy_res2);
2088 }
2089
2090 /* no one calls this */
2091 int llog_log_swabbed(struct llog_log_hdr *hdr)
2092 {
2093         if (hdr->llh_hdr.lrh_type == __swab32(LLOG_HDR_MAGIC))
2094                 return 1;
2095         if (hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC)
2096                 return 0;
2097         return -1;
2098 }
2099
2100 void lustre_swab_qdata(struct qunit_data *d)
2101 {
2102         __swab32s (&d->qd_id);
2103         __swab32s (&d->qd_flags);
2104         __swab64s (&d->qd_count);
2105 }
2106
2107 void lustre_swab_qdata_old(struct qunit_data_old *d)
2108 {
2109         __swab32s (&d->qd_id);
2110         __swab32s (&d->qd_type);
2111         __swab32s (&d->qd_count);
2112         __swab32s (&d->qd_isblk);
2113 }
2114
2115 #ifdef __KERNEL__
2116 struct qunit_data *lustre_quota_old_to_new(struct qunit_data_old *d)
2117 {
2118         struct qunit_data_old tmp;
2119         struct qunit_data *ret;
2120         ENTRY;
2121
2122         if (!d)
2123                 return NULL;
2124
2125         tmp = *d;
2126         ret = (struct qunit_data *)d;
2127         ret->qd_id = tmp.qd_id;
2128         ret->qd_flags = (tmp.qd_type ? QUOTA_IS_GRP : 0) | (tmp.qd_isblk ? QUOTA_IS_BLOCK : 0);
2129         ret->qd_count = tmp.qd_count;
2130         RETURN(ret);
2131
2132 }
2133 EXPORT_SYMBOL(lustre_quota_old_to_new);
2134
2135 struct qunit_data_old *lustre_quota_new_to_old(struct qunit_data *d)
2136 {
2137         struct qunit_data tmp;
2138         struct qunit_data_old *ret;
2139         ENTRY;
2140
2141         if (!d)
2142                 return NULL;
2143
2144         tmp = *d;
2145         ret = (struct qunit_data_old *)d;
2146         ret->qd_id = tmp.qd_id;
2147         ret->qd_type = ((tmp.qd_flags & QUOTA_IS_GRP) ? GRPQUOTA : USRQUOTA);
2148         ret->qd_count = (__u32)tmp.qd_count;
2149         ret->qd_isblk = ((tmp.qd_flags & QUOTA_IS_BLOCK) ? 1 : 0);
2150         RETURN(ret);
2151 }
2152 EXPORT_SYMBOL(lustre_quota_new_to_old);
2153 #endif /* __KERNEL__ */
2154
2155 static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2156 {
2157         LASSERT(req->rq_reqmsg);
2158
2159         switch (req->rq_reqmsg->lm_magic) {
2160         case LUSTRE_MSG_MAGIC_V2:
2161         case LUSTRE_MSG_MAGIC_V2_SWABBED:
2162                 return lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
2163         default:
2164                 CERROR("bad lustre msg magic: %#08X\n",
2165                        req->rq_reqmsg->lm_magic);
2166         }
2167         return 0;
2168 }
2169
2170 static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2171 {
2172         LASSERT(req->rq_repmsg);
2173
2174         switch (req->rq_repmsg->lm_magic) {
2175         case LUSTRE_MSG_MAGIC_V2:
2176         case LUSTRE_MSG_MAGIC_V2_SWABBED:
2177                 return lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
2178         default:
2179                 /* uninitialized yet */
2180                 return 0;
2181         }
2182 }
2183
2184 void _debug_req(struct ptlrpc_request *req, __u32 mask,
2185                 struct libcfs_debug_msg_data *data, const char *fmt, ... )
2186 {
2187         va_list args;
2188
2189         va_start(args, fmt);
2190         libcfs_debug_vmsg2(data->msg_cdls, data->msg_subsys, mask, data->msg_file,
2191                            data->msg_fn, data->msg_line, fmt, args,
2192                            " req@%p x"LPD64"/t"LPD64"("LPD64") o%d->%s@%s:%d/%d"
2193                            " lens %d/%d e %d to %d dl "CFS_TIME_T" ref %d "
2194                            "fl "REQ_FLAGS_FMT"/%x/%x rc %d/%d\n",
2195                            req, req->rq_xid, req->rq_transno,
2196                            req->rq_reqmsg ? lustre_msg_get_transno(req->rq_reqmsg) : 0,
2197                            req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : -1,
2198                            req->rq_import ? obd2cli_tgt(req->rq_import->imp_obd) :
2199                            req->rq_export ?
2200                            (char*)req->rq_export->exp_client_uuid.uuid : "<?>",
2201                            req->rq_import ?
2202                            (char *)req->rq_import->imp_connection->c_remote_uuid.uuid :
2203                            req->rq_export ?
2204                            (char *)req->rq_export->exp_connection->c_remote_uuid.uuid : "<?>",
2205                            req->rq_request_portal, req->rq_reply_portal,
2206                            req->rq_reqlen, req->rq_replen,
2207                            req->rq_early_count, req->rq_timeout, req->rq_deadline,
2208                            atomic_read(&req->rq_refcount), DEBUG_REQ_FLAGS(req),
2209                            req->rq_reqmsg && req_ptlrpc_body_swabbed(req) ?
2210                            lustre_msg_get_flags(req->rq_reqmsg) : -1,
2211                            req->rq_repmsg && rep_ptlrpc_body_swabbed(req) ?
2212                            lustre_msg_get_flags(req->rq_repmsg) : -1,
2213                            req->rq_status,
2214                            req->rq_repmsg && rep_ptlrpc_body_swabbed(req) ?
2215                            lustre_msg_get_status(req->rq_repmsg) : -1);
2216 }
2217 EXPORT_SYMBOL(_debug_req);
2218
2219 void lustre_swab_lustre_capa(struct lustre_capa *c)
2220 {
2221         lustre_swab_lu_fid(&c->lc_fid);
2222         __swab64s (&c->lc_opc);
2223         __swab32s (&c->lc_uid);
2224         __swab32s (&c->lc_flags);
2225         __swab32s (&c->lc_keyid);
2226         __swab32s (&c->lc_timeout);
2227         __swab64s (&c->lc_expiry);
2228 }
2229
2230 void lustre_swab_lustre_capa_key (struct lustre_capa_key *k)
2231 {
2232         __swab64s (&k->lk_mdsid);
2233         __swab32s (&k->lk_keyid);
2234         __swab32s (&k->lk_padding);
2235 }