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