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