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