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