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