1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/pack_generic.c
38 * (Un)packing of OST requests
40 * Author: Peter J. Braam <braam@clusterfs.com>
41 * Author: Phil Schwan <phil@clusterfs.com>
42 * Author: Eric Barton <eeb@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_RPC
47 # include <liblustre.h>
50 #include <libcfs/libcfs.h>
52 #include <obd_support.h>
53 #include <obd_class.h>
54 #include <lustre_net.h>
55 #include <lustre/ll_fiemap.h>
57 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(1,8,99,0)
58 #error "lustre_msg_v1 has been deprecated since 1.6.0, please remove it"
61 static inline int lustre_msg_hdr_size_v1(int count)
63 return size_round(offsetof(struct lustre_msg_v1, lm_buflens[count]));
66 static inline int lustre_msg_hdr_size_v2(int count)
68 return size_round(offsetof(struct lustre_msg_v2, lm_buflens[count]));
71 int lustre_msg_need_swab(struct lustre_msg *msg)
73 return (msg->lm_magic == LUSTRE_MSG_MAGIC_V1_SWABBED) ||
74 (msg->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED);
77 int lustre_msg_check_version_v2(struct lustre_msg_v2 *msg, __u32 version)
79 __u32 ver = lustre_msg_get_version(msg);
80 return (ver & LUSTRE_VERSION_MASK) != version;
83 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version)
85 switch (msg->lm_magic) {
86 case LUSTRE_MSG_MAGIC_V1:
88 case LUSTRE_MSG_MAGIC_V2:
89 return lustre_msg_check_version_v2(msg, version);
91 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
96 static int ptlrpc_reqbuf_need_swab(struct ptlrpc_request *req, int index)
100 swabb = (!lustre_req_need_swab(req)) ||
101 lustre_req_swabbed(req, index);
106 static int ptlrpc_repbuf_need_swab(struct ptlrpc_request *req, int index)
110 swabb = (!lustre_rep_need_swab(req)) ||
111 lustre_rep_swabbed(req, index);
117 /* early reply size */
118 int lustre_msg_early_size(struct ptlrpc_request *req) {
120 /* For b1_6 interoperability */
121 if (req->rq_reqmsg &&
122 req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2) {
123 __u32 pb_len = lustre_msg_buflen(req->rq_reqmsg,
124 MSG_PTLRPC_BODY_OFF);
125 return lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, &pb_len);
129 size = lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
132 EXPORT_SYMBOL(lustre_msg_early_size);
134 static inline int lustre_msg_size_v1(int count, __u32 *lengths)
140 size = lustre_msg_hdr_size_v1(count);
141 for (i = 0; i < count; i++)
142 size += size_round(lengths[i]);
147 static inline int lustre_msg_size_v2(int count, __u32 *lengths)
152 size = lustre_msg_hdr_size_v2(count);
153 for (i = 0; i < count; i++) {
154 size += size_round(lengths[i]);
155 CDEBUG(D_INFO, "size %d - len %d\n", size, lengths[i]);
161 /* This returns the size of the buffer that is required to hold a lustre_msg
162 * with the given sub-buffer lengths.
163 * NOTE: this should only be used for NEW requests, and should always be
164 * in the form of a v2 request. If this is a connection to a v1
165 * target then the first buffer will be stripped because the ptlrpc
166 * data is part of the lustre_msg_v1 header. b=14043 */
167 int lustre_msg_size(__u32 magic, int count, __u32 *lens)
169 __u32 size[] = { sizeof(struct ptlrpc_body) };
177 #ifdef PTLRPC_INTEROP_1_6
178 LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body) ||
179 lens[MSG_PTLRPC_BODY_OFF] == PTLRPC_BODY_MIN_SIZE);
181 LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
184 case LUSTRE_MSG_MAGIC_V1:
185 return lustre_msg_size_v1(count - 1, lens + 1);
186 case LUSTRE_MSG_MAGIC_V2:
187 return lustre_msg_size_v2(count, lens);
189 CERROR("incorrect message magic: %08x\n", magic);
194 /* This is used to determine the size of a buffer that was already packed
195 * and will correctly handle the different message formats. */
196 int lustre_packed_msg_size(struct lustre_msg *msg)
198 switch (msg->lm_magic) {
199 case LUSTRE_MSG_MAGIC_V1: {
200 struct lustre_msg_v1 *v1_msg = (struct lustre_msg_v1 *)msg;
201 return lustre_msg_size_v1(v1_msg->lm_bufcount,
204 case LUSTRE_MSG_MAGIC_V2:
205 return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
207 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
213 lustre_init_msg_v1(void *m, int count, __u32 *lens, char **bufs)
215 struct lustre_msg_v1 *msg = (struct lustre_msg_v1 *)m;
220 msg->lm_magic = LUSTRE_MSG_MAGIC_V1;
221 msg->lm_version = PTLRPC_MSG_VERSION;
222 msg->lm_bufcount = count;
224 for (i = 0; i < count; i++)
225 msg->lm_buflens[i] = lens[i];
230 ptr = (char *)msg + lustre_msg_hdr_size_v1(count);
231 for (i = 0; i < count; i++) {
233 LOGL(tmp, lens[i], ptr);
238 lustre_init_msg_v2(struct lustre_msg_v2 *msg, int count, __u32 *lens,
244 msg->lm_bufcount = count;
245 /* XXX: lm_secflvr uninitialized here */
246 msg->lm_magic = LUSTRE_MSG_MAGIC_V2;
248 for (i = 0; i < count; i++)
249 msg->lm_buflens[i] = lens[i];
254 ptr = (char *)msg + lustre_msg_hdr_size_v2(count);
255 for (i = 0; i < count; i++) {
257 LOGL(tmp, lens[i], ptr);
261 static int lustre_pack_request_v1(struct ptlrpc_request *req,
262 int count, __u32 *lens, char **bufs)
266 reqlen = lustre_msg_size_v1(count, lens);
268 /* See if we got it from prealloc pool */
269 if (req->rq_reqmsg) {
270 /* Cannot return error here, that would create
271 infinite loop in ptlrpc_prep_req_pool */
272 /* In this case ptlrpc_prep_req_from_pool sets req->rq_reqlen
273 to maximum size that would fit into this preallocated
275 LASSERTF(req->rq_reqlen >= reqlen, "req->rq_reqlen %d, "
276 "reqlen %d\n",req->rq_reqlen,
278 memset(req->rq_reqmsg, 0, reqlen);
280 OBD_ALLOC(req->rq_reqmsg, reqlen);
281 if (req->rq_reqmsg == NULL) {
282 CERROR("alloc reqmsg (len %d) failed\n", reqlen);
287 req->rq_reqlen = reqlen;
289 lustre_init_msg_v1(req->rq_reqmsg, count, lens, bufs);
293 static int lustre_pack_request_v2(struct ptlrpc_request *req,
294 int count, __u32 *lens, char **bufs)
298 reqlen = lustre_msg_size_v2(count, lens);
300 /* See if we got it from prealloc pool */
301 if (req->rq_reqmsg) {
302 /* Cannot return error here, that would create
303 infinite loop in ptlrpc_prep_req_pool */
304 /* In this case ptlrpc_prep_req_from_pool sets req->rq_reqlen
305 to maximum size that would fit into this preallocated
307 LASSERTF(req->rq_reqlen >= reqlen, "req->rq_reqlen %d, "
308 "reqlen %d\n",req->rq_reqlen,
310 memset(req->rq_reqmsg, 0, reqlen);
312 OBD_ALLOC(req->rq_reqmsg, reqlen);
313 if (req->rq_reqmsg == NULL) {
314 CERROR("alloc reqmsg (len %d) failed\n", reqlen);
319 req->rq_reqlen = reqlen;
321 lustre_init_msg_v2(req->rq_reqmsg, count, lens, bufs);
322 lustre_msg_add_version(req->rq_reqmsg, PTLRPC_MSG_VERSION);
323 lustre_set_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
328 int lustre_pack_request(struct ptlrpc_request *req, __u32 magic, int count,
329 __u32 *lens, char **bufs)
331 __u32 size[] = { sizeof(struct ptlrpc_body) };
339 LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
342 case LUSTRE_MSG_MAGIC_V1:
343 return lustre_pack_request_v1(req, count - 1, lens + 1,
344 bufs ? bufs + 1 : NULL);
345 case LUSTRE_MSG_MAGIC_V2:
346 return lustre_pack_request_v2(req, count, lens, bufs);
348 LASSERTF(0, "incorrect message magic: %08x\n", magic);
354 CFS_LIST_HEAD(ptlrpc_rs_debug_lru);
355 spinlock_t ptlrpc_rs_debug_lock;
357 #define PTLRPC_RS_DEBUG_LRU_ADD(rs) \
359 spin_lock(&ptlrpc_rs_debug_lock); \
360 list_add_tail(&(rs)->rs_debug_list, &ptlrpc_rs_debug_lru); \
361 spin_unlock(&ptlrpc_rs_debug_lock); \
364 #define PTLRPC_RS_DEBUG_LRU_DEL(rs) \
366 spin_lock(&ptlrpc_rs_debug_lock); \
367 list_del(&(rs)->rs_debug_list); \
368 spin_unlock(&ptlrpc_rs_debug_lock); \
371 # define PTLRPC_RS_DEBUG_LRU_ADD(rs) do {} while(0)
372 # define PTLRPC_RS_DEBUG_LRU_DEL(rs) do {} while(0)
375 static struct ptlrpc_reply_state *lustre_get_emerg_rs(struct ptlrpc_service *svc,
378 struct ptlrpc_reply_state *rs = NULL;
380 spin_lock(&svc->srv_lock);
381 /* See if we have anything in a pool, and wait if nothing */
382 while (list_empty(&svc->srv_free_rs_list)) {
383 struct l_wait_info lwi;
385 spin_unlock(&svc->srv_lock);
386 /* If we cannot get anything for some long time, we better
387 bail out instead of waiting infinitely */
388 lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
389 rc = l_wait_event(svc->srv_free_rs_waitq,
390 !list_empty(&svc->srv_free_rs_list), &lwi);
393 spin_lock(&svc->srv_lock);
396 rs = list_entry(svc->srv_free_rs_list.next, struct ptlrpc_reply_state,
398 list_del(&rs->rs_list);
399 spin_unlock(&svc->srv_lock);
401 LASSERTF(svc->srv_max_reply_size > size, "Want %d, prealloc %d\n", size,
402 svc->srv_max_reply_size);
409 static int lustre_pack_reply_v1(struct ptlrpc_request *req, int count,
410 __u32 *lens, char **bufs, int flags)
412 struct ptlrpc_reply_state *rs;
417 LASSERT(req->rq_reply_state == NULL);
419 if ((flags & LPRFL_EARLY_REPLY) == 0)
420 req->rq_packed_final = 1;
422 msg_len = lustre_msg_size_v1(count, lens);
423 size = sizeof(struct ptlrpc_reply_state) + msg_len;
425 if (unlikely(rs == NULL)) {
426 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service, size);
430 atomic_set(&rs->rs_refcount, 1); /* 1 ref for rq_reply_state */
431 rs->rs_cb_id.cbid_fn = reply_out_callback;
432 rs->rs_cb_id.cbid_arg = rs;
433 rs->rs_service = req->rq_rqbd->rqbd_service;
435 CFS_INIT_LIST_HEAD(&rs->rs_exp_list);
436 CFS_INIT_LIST_HEAD(&rs->rs_obd_list);
437 rs->rs_msg = (struct lustre_msg *)(rs + 1);
439 req->rq_replen = msg_len;
440 req->rq_reply_state = rs;
441 req->rq_repmsg = rs->rs_msg;
443 lustre_init_msg_v1(rs->rs_msg, count, lens, bufs);
445 PTLRPC_RS_DEBUG_LRU_ADD(rs);
450 static int lustre_pack_reply_v2(struct ptlrpc_request *req, int count,
451 __u32 *lens, char **bufs, int flags)
453 struct ptlrpc_reply_state *rs;
458 LASSERT(req->rq_reply_state == NULL);
460 if ((flags & LPRFL_EARLY_REPLY) == 0)
461 req->rq_packed_final = 1;
463 /* use the same size of ptlrpc_body as client requested for
464 * interoperability cases */
465 LASSERT(req->rq_reqmsg);
466 lens[MSG_PTLRPC_BODY_OFF] = lustre_msg_buflen(req->rq_reqmsg,
467 MSG_PTLRPC_BODY_OFF);
469 msg_len = lustre_msg_size_v2(count, lens);
470 size = sizeof(struct ptlrpc_reply_state) + msg_len;
472 if (unlikely(rs == NULL)) {
473 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service, size);
477 atomic_set(&rs->rs_refcount, 1); /* 1 ref for rq_reply_state */
478 rs->rs_cb_id.cbid_fn = reply_out_callback;
479 rs->rs_cb_id.cbid_arg = rs;
480 rs->rs_service = req->rq_rqbd->rqbd_service;
482 CFS_INIT_LIST_HEAD(&rs->rs_exp_list);
483 CFS_INIT_LIST_HEAD(&rs->rs_obd_list);
484 rs->rs_msg = (struct lustre_msg *)(rs + 1);
486 req->rq_replen = msg_len;
487 req->rq_reply_state = rs;
488 req->rq_repmsg = rs->rs_msg;
489 /* server side, no rq_repbuf */
490 lustre_init_msg_v2(rs->rs_msg, count, lens, bufs);
491 lustre_msg_add_version(rs->rs_msg, PTLRPC_MSG_VERSION);
492 lustre_set_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
494 PTLRPC_RS_DEBUG_LRU_ADD(rs);
499 int lustre_pack_reply_flags(struct ptlrpc_request *req, int count, __u32 *lens,
500 char **bufs, int flags)
502 __u32 size[] = { sizeof(struct ptlrpc_body) };
510 LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
512 switch (req->rq_reqmsg->lm_magic) {
513 case LUSTRE_MSG_MAGIC_V1:
514 return lustre_pack_reply_v1(req, count - 1, lens + 1,
515 bufs ? bufs + 1 : NULL, flags);
516 case LUSTRE_MSG_MAGIC_V2:
517 return lustre_pack_reply_v2(req, count, lens, bufs, flags);
519 CERROR("incorrect message magic: %08x\n",
520 req->rq_reqmsg->lm_magic);
525 int lustre_pack_reply(struct ptlrpc_request *req, int count, __u32 *lens,
528 int rc = lustre_pack_reply_flags(req, count, lens, bufs, 0);
530 CERROR("lustre_pack_reply failed: rc=%d size=%d\n", rc,
531 lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens));
536 void *lustre_msg_buf_v1(void *msg, int n, int min_size)
538 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
539 int i, offset, buflen, bufcount;
544 bufcount = m->lm_bufcount;
546 CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
551 buflen = m->lm_buflens[n];
552 if (buflen < min_size) {
553 CERROR("msg %p buffer[%d] size %d too small (required %d)\n",
554 m, n, buflen, min_size);
559 offset = lustre_msg_hdr_size_v1(bufcount);
560 for (i = 0; i < n; i++)
561 offset += size_round(m->lm_buflens[i]);
563 return (char *)m + offset;
566 void *lustre_msg_buf_v2(struct lustre_msg_v2 *m, int n, int min_size)
568 int i, offset, buflen, bufcount;
573 bufcount = m->lm_bufcount;
575 CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
580 buflen = m->lm_buflens[n];
581 if (buflen < min_size) {
582 CERROR("msg %p buffer[%d] size %d too small "
583 "(required %d, opc=%d)\n",
584 m, n, buflen, min_size, lustre_msg_get_opc(m));
588 offset = lustre_msg_hdr_size_v2(bufcount);
589 for (i = 0; i < n; i++)
590 offset += size_round(m->lm_buflens[i]);
592 return (char *)m + offset;
595 void *lustre_msg_buf(struct lustre_msg *m, int n, int min_size)
597 switch (m->lm_magic) {
598 case LUSTRE_MSG_MAGIC_V1:
599 return lustre_msg_buf_v1(m, n - 1, min_size);
600 case LUSTRE_MSG_MAGIC_V2:
601 return lustre_msg_buf_v2(m, n, min_size);
603 CERROR("incorrect message magic: %08x\n", m->lm_magic);
608 void lustre_shrink_reply_v1(struct ptlrpc_request *req, int segment,
609 unsigned int newlen, int move_data)
611 struct lustre_msg_v1 *msg = (struct lustre_msg_v1 *)req->rq_repmsg;
612 char *tail = NULL, *newpos;
615 LASSERT(req->rq_reply_state);
617 LASSERT(segment >= 0);
618 LASSERTF(msg->lm_bufcount > segment, "message have %d - requested %d\n",
619 msg->lm_bufcount,segment);
620 LASSERT(msg->lm_buflens[segment] >= newlen);
622 if (msg->lm_buflens[segment] == newlen)
625 if (move_data && msg->lm_bufcount > segment + 1) {
626 tail = lustre_msg_buf_v1(msg, segment + 1, 0);
627 for (n = segment + 1; n < msg->lm_bufcount; n++)
628 tail_len += size_round(msg->lm_buflens[n]);
631 msg->lm_buflens[segment] = newlen;
633 if (tail && tail_len) {
634 newpos = lustre_msg_buf_v1(msg, segment + 1, 0);
635 LASSERT(newpos <= tail);
637 memcpy(newpos, tail, tail_len);
640 if (newlen == 0 && msg->lm_bufcount > segment + 1) {
641 memmove(&msg->lm_buflens[segment], &msg->lm_buflens[segment + 1],
642 (msg->lm_bufcount - segment - 1) * sizeof(__u32));
643 msg->lm_buflens[msg->lm_bufcount - 1] = 0;
646 req->rq_replen = lustre_msg_size_v1(msg->lm_bufcount, msg->lm_buflens);
649 void lustre_shrink_reply_v2(struct ptlrpc_request *req, int segment,
650 unsigned int newlen, int move_data)
652 struct lustre_msg_v2 *msg = req->rq_repmsg;
653 char *tail = NULL, *newpos;
656 CDEBUG(D_INFO, "shrink req %p seg %d - len %d\n", req, segment, newlen);
657 LASSERT(req->rq_reply_state);
659 LASSERTF(msg->lm_bufcount > segment, "message have %d - requested %d\n",
660 msg->lm_bufcount,segment);
661 LASSERT(msg->lm_buflens[segment] >= newlen);
663 if (msg->lm_buflens[segment] == newlen)
666 if (move_data && msg->lm_bufcount > segment + 1) {
667 tail = lustre_msg_buf_v2(msg, segment + 1, 0);
668 for (n = segment + 1; n < msg->lm_bufcount; n++)
669 tail_len += size_round(msg->lm_buflens[n]);
672 msg->lm_buflens[segment] = newlen;
674 if (tail && tail_len) {
675 newpos = lustre_msg_buf_v2(msg, segment + 1, 0);
676 LASSERT(newpos <= tail);
678 memmove(newpos, tail, tail_len);
681 if (newlen == 0 && msg->lm_bufcount > segment + 1) {
682 memmove(&msg->lm_buflens[segment], &msg->lm_buflens[segment + 1],
683 (msg->lm_bufcount - segment - 1) * sizeof(__u32));
684 msg->lm_buflens[msg->lm_bufcount - 1] = 0;
687 req->rq_replen = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
691 * shrink @segment to size @newlen. if @move_data is non-zero, we also move
692 * data forward from @segment + 1.
694 * if @newlen == 0, we remove the segment completely, but we still keep the
695 * totally bufcount the same to save possible data moving. this will leave a
696 * unused segment with size 0 at the tail, but that's ok.
699 * + if any buffers higher than @segment has been filled in, must call shrink
700 * with non-zero @move_data.
701 * + caller should NOT keep pointers to msg buffers which higher than @segment
704 void lustre_shrink_reply(struct ptlrpc_request *req, int segment,
705 unsigned int newlen, int move_data)
707 switch (req->rq_repmsg->lm_magic) {
708 case LUSTRE_MSG_MAGIC_V1:
709 lustre_shrink_reply_v1(req, segment - 1, newlen, move_data);
711 case LUSTRE_MSG_MAGIC_V2:
712 lustre_shrink_reply_v2(req, segment, newlen, move_data);
715 LASSERTF(0, "incorrect message magic: %08x\n",
716 req->rq_repmsg->lm_magic);
720 void lustre_free_reply_state(struct ptlrpc_reply_state *rs)
722 PTLRPC_RS_DEBUG_LRU_DEL(rs);
724 LASSERT (atomic_read(&rs->rs_refcount) == 0);
725 LASSERT (!rs->rs_difficult || rs->rs_handled);
726 LASSERT (!rs->rs_on_net);
727 LASSERT (!rs->rs_scheduled);
728 LASSERT (rs->rs_export == NULL);
729 LASSERT (rs->rs_nlocks == 0);
730 LASSERT (list_empty(&rs->rs_exp_list));
731 LASSERT (list_empty(&rs->rs_obd_list));
733 if (unlikely(rs->rs_prealloc)) {
734 struct ptlrpc_service *svc = rs->rs_service;
736 spin_lock(&svc->srv_lock);
737 list_add(&rs->rs_list,
738 &svc->srv_free_rs_list);
739 spin_unlock(&svc->srv_lock);
740 cfs_waitq_signal(&svc->srv_free_rs_waitq);
742 OBD_FREE(rs, rs->rs_size);
746 int lustre_unpack_msg_v1(void *msg, int len)
748 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
749 int flipped, required_len, i;
752 /* Now we know the sender speaks my language. */
753 required_len = lustre_msg_hdr_size_v1(0);
754 if (len < required_len) {
755 /* can't even look inside the message */
756 CERROR("message length %d too small for lustre_msg\n", len);
760 flipped = m->lm_magic == LUSTRE_MSG_MAGIC_V1_SWABBED;
762 __swab32s(&m->lm_magic);
763 __swab32s(&m->lm_type);
764 __swab32s(&m->lm_version);
765 __swab32s(&m->lm_opc);
766 __swab64s(&m->lm_last_xid);
767 __swab64s(&m->lm_last_committed);
768 __swab64s(&m->lm_transno);
769 __swab32s(&m->lm_status);
770 __swab32s(&m->lm_flags);
771 __swab32s(&m->lm_conn_cnt);
772 __swab32s(&m->lm_bufcount);
775 if (m->lm_version != PTLRPC_MSG_VERSION) {
776 CERROR("wrong lustre_msg version %08x\n", m->lm_version);
780 required_len = lustre_msg_hdr_size_v1(m->lm_bufcount);
781 if (len < required_len) {
782 /* didn't receive all the buffer lengths */
783 CERROR("message length %d too small for %d buflens\n",
784 len, m->lm_bufcount);
788 for (i = 0; i < m->lm_bufcount; i++) {
790 __swab32s (&m->lm_buflens[i]);
791 required_len += size_round(m->lm_buflens[i]);
794 if (len < required_len) {
795 CERROR("len: %d, required_len %d\n", len, required_len);
796 CERROR("bufcount: %d\n", m->lm_bufcount);
797 for (i = 0; i < m->lm_bufcount; i++)
798 CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
805 static int lustre_unpack_msg_v2(struct lustre_msg_v2 *m, int len)
807 int flipped, required_len, i;
809 /* Now we know the sender speaks my language. */
810 required_len = lustre_msg_hdr_size_v2(0);
811 if (len < required_len) {
812 /* can't even look inside the message */
813 CERROR("message length %d too small for lustre_msg\n", len);
817 flipped = m->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED;
819 __swab32s(&m->lm_magic);
820 __swab32s(&m->lm_bufcount);
821 __swab32s(&m->lm_secflvr);
822 __swab32s(&m->lm_repsize);
823 __swab32s(&m->lm_cksum);
824 __swab32s(&m->lm_flags);
825 CLASSERT(offsetof(typeof(*m), lm_padding_2) != 0);
826 CLASSERT(offsetof(typeof(*m), lm_padding_3) != 0);
829 required_len = lustre_msg_hdr_size_v2(m->lm_bufcount);
830 if (len < required_len) {
831 /* didn't receive all the buffer lengths */
832 CERROR ("message length %d too small for %d buflens\n",
833 len, m->lm_bufcount);
837 for (i = 0; i < m->lm_bufcount; i++) {
839 __swab32s(&m->lm_buflens[i]);
840 required_len += size_round(m->lm_buflens[i]);
843 if (len < required_len) {
844 CERROR("len: %d, required_len %d\n", len, required_len);
845 CERROR("bufcount: %d\n", m->lm_bufcount);
846 for (i = 0; i < m->lm_bufcount; i++)
847 CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
855 * return 1 if some endianness conversions are needed for the req buffers,
857 * -EINVAL if message has wrong magic
859 int lustre_unpack_msg(struct lustre_msg *m, int len)
861 int required_len, rc;
865 /* We can provide a slightly better error log, if we check the
866 * message magic and version first. In the future, struct
867 * lustre_msg may grow, and we'd like to log a version mismatch,
868 * rather than a short message.
871 required_len = offsetof(struct lustre_msg, lm_magic) +
873 if (len < required_len) {
874 /* can't even look inside the message */
875 CERROR("message length %d too small for magic/version check\n",
880 swab_needed = lustre_msg_need_swab(m);
882 switch (m->lm_magic) {
883 case LUSTRE_MSG_MAGIC_V1:
884 case LUSTRE_MSG_MAGIC_V1_SWABBED:
885 rc = lustre_unpack_msg_v1(m, len);
887 case LUSTRE_MSG_MAGIC_V2:
888 case LUSTRE_MSG_MAGIC_V2_SWABBED:
889 rc = lustre_unpack_msg_v2(m, len);
892 CERROR("incorrect message magic: %08x\n", m->lm_magic);
902 static inline int lustre_unpack_ptlrpc_body_v2(struct lustre_msg_v2 *m,
903 int offset, int swab_needed)
905 struct ptlrpc_body *pb;
907 pb = lustre_msg_buf_v2(m, offset, PTLRPC_BODY_MIN_SIZE);
909 CERROR("error unpacking ptlrpc body\n");
913 lustre_swab_ptlrpc_body(pb, lustre_msg_buflen(m, offset));
915 if ((pb->pb_version & ~LUSTRE_VERSION_MASK) != PTLRPC_MSG_VERSION) {
916 CERROR("wrong lustre_msg version %08x\n", pb->pb_version);
923 int lustre_unpack_msg_ptlrpc_body(struct lustre_msg *msg,
924 int offset, int swab_needed)
926 switch (msg->lm_magic) {
927 case LUSTRE_MSG_MAGIC_V1:
929 case LUSTRE_MSG_MAGIC_V2:
930 return lustre_unpack_ptlrpc_body_v2(msg, offset, swab_needed);
932 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
937 int lustre_unpack_req_ptlrpc_body(struct ptlrpc_request *req, int offset)
939 switch (req->rq_reqmsg->lm_magic) {
940 case LUSTRE_MSG_MAGIC_V1:
942 case LUSTRE_MSG_MAGIC_V2: {
943 int swab_needed = ptlrpc_reqbuf_need_swab(req, offset);
944 lustre_set_req_swabbed(req, offset);
945 return lustre_unpack_ptlrpc_body_v2(req->rq_reqmsg, offset,
949 CERROR("incorrect message magic: %08x\n",
950 req->rq_reqmsg->lm_magic);
955 int lustre_unpack_rep_ptlrpc_body(struct ptlrpc_request *req, int offset)
957 switch (req->rq_repmsg->lm_magic) {
958 case LUSTRE_MSG_MAGIC_V1:
960 case LUSTRE_MSG_MAGIC_V2:{
961 int swab_needed = ptlrpc_repbuf_need_swab(req, offset);
962 lustre_set_rep_swabbed(req, offset);
963 return lustre_unpack_ptlrpc_body_v2(req->rq_repmsg, offset,
967 CERROR("incorrect message magic: %08x\n",
968 req->rq_repmsg->lm_magic);
973 static inline int lustre_msg_buflen_v1(void *msg, int n)
975 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
978 if (n >= m->lm_bufcount)
981 return m->lm_buflens[n];
984 static inline int lustre_msg_buflen_v2(struct lustre_msg_v2 *m, int n)
986 if (n >= m->lm_bufcount)
989 return m->lm_buflens[n];
993 * lustre_msg_buflen - return the length of buffer @n in message @m
994 * @m - lustre_msg (request or reply) to look at
995 * @n - message index (base 0)
997 * returns zero for non-existent message indices
999 int lustre_msg_buflen(struct lustre_msg *m, int n)
1001 switch (m->lm_magic) {
1002 case LUSTRE_MSG_MAGIC_V1:
1003 return lustre_msg_buflen_v1(m, n - 1);
1004 case LUSTRE_MSG_MAGIC_V2:
1005 return lustre_msg_buflen_v2(m, n);
1007 CERROR("incorrect message magic: %08x\n", m->lm_magic);
1011 EXPORT_SYMBOL(lustre_msg_buflen);
1013 static inline void lustre_msg_set_buflen_v1(void *msg, int n, int len)
1015 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
1018 if (n >= m->lm_bufcount)
1021 m->lm_buflens[n] = len;
1025 lustre_msg_set_buflen_v2(struct lustre_msg_v2 *m, int n, int len)
1027 if (n >= m->lm_bufcount)
1030 m->lm_buflens[n] = len;
1033 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len)
1035 switch (m->lm_magic) {
1036 case LUSTRE_MSG_MAGIC_V1:
1037 lustre_msg_set_buflen_v1(m, n - 1, len);
1039 case LUSTRE_MSG_MAGIC_V2:
1040 lustre_msg_set_buflen_v2(m, n, len);
1043 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
1047 EXPORT_SYMBOL(lustre_msg_set_buflen);
1049 /* NB return the bufcount for lustre_msg_v2 format, so if message is packed
1050 * in V1 format, the result is one bigger. (add struct ptlrpc_body). */
1051 int lustre_msg_bufcount(struct lustre_msg *m)
1053 switch (m->lm_magic) {
1054 case LUSTRE_MSG_MAGIC_V1:
1055 return ((struct lustre_msg_v1 *)m)->lm_bufcount + 1;
1056 case LUSTRE_MSG_MAGIC_V2:
1057 return m->lm_bufcount;
1059 CERROR("incorrect message magic: %08x\n", m->lm_magic);
1063 EXPORT_SYMBOL(lustre_msg_bufcount);
1065 char *lustre_msg_string(struct lustre_msg *m, int index, int max_len)
1067 /* max_len == 0 means the string should fill the buffer */
1071 switch (m->lm_magic) {
1072 case LUSTRE_MSG_MAGIC_V1:
1073 str = lustre_msg_buf_v1(m, index - 1, 0);
1074 blen = lustre_msg_buflen_v1(m, index - 1);
1076 case LUSTRE_MSG_MAGIC_V2:
1077 str = lustre_msg_buf_v2(m, index, 0);
1078 blen = lustre_msg_buflen_v2(m, index);
1081 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
1085 CERROR ("can't unpack string in msg %p buffer[%d]\n", m, index);
1089 slen = strnlen(str, blen);
1091 if (slen == blen) { /* not NULL terminated */
1092 CERROR("can't unpack non-NULL terminated string in "
1093 "msg %p buffer[%d] len %d\n", m, index, blen);
1098 if (slen != blen - 1) {
1099 CERROR("can't unpack short string in msg %p "
1100 "buffer[%d] len %d: strlen %d\n",
1101 m, index, blen, slen);
1104 } else if (slen > max_len) {
1105 CERROR("can't unpack oversized string in msg %p "
1106 "buffer[%d] len %d strlen %d: max %d expected\n",
1107 m, index, blen, slen, max_len);
1114 /* Wrap up the normal fixed length cases */
1115 void *lustre_swab_buf(struct lustre_msg *msg, int index, int min_size,
1120 LASSERT(msg != NULL);
1121 switch (msg->lm_magic) {
1122 case LUSTRE_MSG_MAGIC_V1:
1123 ptr = lustre_msg_buf_v1(msg, index - 1, min_size);
1125 case LUSTRE_MSG_MAGIC_V2:
1126 ptr = lustre_msg_buf_v2(msg, index, min_size);
1129 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1134 if (swabber != NULL) {
1135 CDEBUG(D_NET, "Swab buffer %d\n", index);
1136 ((void (*)(void *))swabber)(ptr);
1142 void *lustre_swab_reqbuf(struct ptlrpc_request *req, int index, int min_size,
1145 if (lustre_req_swabbed(req, index))
1146 return lustre_msg_buf(req->rq_reqmsg, index, min_size);
1148 if (!lustre_req_need_swab(req))
1151 lustre_set_req_swabbed(req, index);
1152 return lustre_swab_buf(req->rq_reqmsg, index, min_size, swabber);
1155 void *lustre_swab_repbuf(struct ptlrpc_request *req, int index, int min_size,
1158 if (lustre_rep_swabbed(req, index))
1159 return lustre_msg_buf(req->rq_repmsg, index, min_size);
1161 if (!lustre_rep_need_swab(req))
1164 lustre_set_rep_swabbed(req, index);
1165 return lustre_swab_buf(req->rq_repmsg, index, min_size, swabber);
1168 static inline struct ptlrpc_body *lustre_msg_ptlrpc_body(struct lustre_msg *msg)
1170 return lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1171 PTLRPC_BODY_MIN_SIZE);
1174 __u32 lustre_msghdr_get_flags(struct lustre_msg *msg)
1176 switch (msg->lm_magic) {
1177 case LUSTRE_MSG_MAGIC_V1:
1179 case LUSTRE_MSG_MAGIC_V2:
1180 /* already in host endian */
1181 return msg->lm_flags;
1183 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1188 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags)
1190 switch (msg->lm_magic) {
1191 case LUSTRE_MSG_MAGIC_V1:
1193 case LUSTRE_MSG_MAGIC_V2:
1194 msg->lm_flags = flags;
1197 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1201 __u32 lustre_msg_get_flags(struct lustre_msg *msg)
1203 switch (msg->lm_magic) {
1204 case LUSTRE_MSG_MAGIC_V1:
1205 return ((struct lustre_msg_v1 *)msg)->lm_flags &
1207 case LUSTRE_MSG_MAGIC_V2: {
1208 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1210 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1213 return pb->pb_flags;
1216 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1217 /* flags might be printed in debug code while message
1223 void lustre_msg_add_flags(struct lustre_msg *msg, int flags)
1225 switch (msg->lm_magic) {
1226 case LUSTRE_MSG_MAGIC_V1:
1227 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1228 MSG_GEN_FLAG_MASK & flags;
1230 case LUSTRE_MSG_MAGIC_V2: {
1231 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1232 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1233 pb->pb_flags |= flags;
1237 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1241 void lustre_msg_set_flags(struct lustre_msg *msg, int flags)
1243 switch (msg->lm_magic) {
1244 case LUSTRE_MSG_MAGIC_V1:
1245 ((struct lustre_msg_v1 *)msg)->lm_flags &= ~MSG_GEN_FLAG_MASK;
1246 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1247 MSG_GEN_FLAG_MASK & flags;
1249 case LUSTRE_MSG_MAGIC_V2: {
1250 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1251 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1252 pb->pb_flags = flags;
1256 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1260 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags)
1262 switch (msg->lm_magic) {
1263 case LUSTRE_MSG_MAGIC_V1:
1264 ((struct lustre_msg_v1 *)msg)->lm_flags &=
1265 ~(MSG_GEN_FLAG_MASK & flags);
1267 case LUSTRE_MSG_MAGIC_V2: {
1268 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1269 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1270 pb->pb_flags &= ~(MSG_GEN_FLAG_MASK & flags);
1274 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1278 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg)
1280 switch (msg->lm_magic) {
1281 case LUSTRE_MSG_MAGIC_V1:
1282 return ((struct lustre_msg_v1 *)msg)->lm_flags >>
1284 case LUSTRE_MSG_MAGIC_V2: {
1285 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1287 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1290 return pb->pb_op_flags;
1293 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1298 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags)
1300 switch (msg->lm_magic) {
1301 case LUSTRE_MSG_MAGIC_V1:
1302 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1303 (flags & MSG_GEN_FLAG_MASK) << MSG_OP_FLAG_SHIFT;
1305 case LUSTRE_MSG_MAGIC_V2: {
1306 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1307 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1308 pb->pb_op_flags |= flags;
1312 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1316 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags)
1318 switch (msg->lm_magic) {
1319 case LUSTRE_MSG_MAGIC_V1:
1320 ((struct lustre_msg_v1 *)msg)->lm_flags &= ~MSG_OP_FLAG_MASK;
1321 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1322 ((flags & MSG_GEN_FLAG_MASK) <<MSG_OP_FLAG_SHIFT);
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_op_flags |= flags;
1331 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1335 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg)
1337 switch (msg->lm_magic) {
1338 case LUSTRE_MSG_MAGIC_V1:
1339 return &((struct lustre_msg_v1 *)msg)->lm_handle;
1340 case LUSTRE_MSG_MAGIC_V2: {
1341 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1343 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1346 return &pb->pb_handle;
1349 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1354 __u32 lustre_msg_get_type(struct lustre_msg *msg)
1356 switch (msg->lm_magic) {
1357 case LUSTRE_MSG_MAGIC_V1:
1358 return ((struct lustre_msg_v1 *)msg)->lm_type;
1359 case LUSTRE_MSG_MAGIC_V2: {
1360 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1362 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1363 return PTL_RPC_MSG_ERR;
1368 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1369 return PTL_RPC_MSG_ERR;
1373 __u32 lustre_msg_get_version(struct lustre_msg *msg)
1375 switch (msg->lm_magic) {
1376 case LUSTRE_MSG_MAGIC_V1:
1377 return ((struct lustre_msg_v1 *)msg)->lm_version;
1378 case LUSTRE_MSG_MAGIC_V2: {
1379 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1381 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1384 return pb->pb_version;
1387 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1392 void lustre_msg_add_version(struct lustre_msg *msg, int version)
1394 switch (msg->lm_magic) {
1395 case LUSTRE_MSG_MAGIC_V1:
1397 case LUSTRE_MSG_MAGIC_V2: {
1398 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1399 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1400 pb->pb_version |= version;
1404 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1408 __u32 lustre_msg_get_opc(struct lustre_msg *msg)
1410 switch (msg->lm_magic) {
1411 case LUSTRE_MSG_MAGIC_V1:
1412 return ((struct lustre_msg_v1 *)msg)->lm_opc;
1413 case LUSTRE_MSG_MAGIC_V2: {
1414 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1416 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1422 CERROR( "incorrect message magic: %08x\n", msg->lm_magic);
1427 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg)
1429 switch (msg->lm_magic) {
1430 case LUSTRE_MSG_MAGIC_V1:
1431 return ((struct lustre_msg_v1 *)msg)->lm_last_xid;
1432 case LUSTRE_MSG_MAGIC_V2: {
1433 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1435 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1438 return pb->pb_last_xid;
1441 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1446 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg)
1448 switch (msg->lm_magic) {
1449 case LUSTRE_MSG_MAGIC_V1:
1450 return ((struct lustre_msg_v1 *)msg)->lm_last_committed;
1451 case LUSTRE_MSG_MAGIC_V2: {
1452 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1454 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1457 return pb->pb_last_committed;
1460 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1465 __u64 *lustre_msg_get_versions(struct lustre_msg *msg)
1467 switch (msg->lm_magic) {
1468 case LUSTRE_MSG_MAGIC_V1:
1470 case LUSTRE_MSG_MAGIC_V2: {
1471 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1473 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1476 #ifdef PTLRPC_INTEROP_1_6
1477 if (lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF) < sizeof (*pb))
1480 return pb->pb_pre_versions;
1483 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1488 __u64 lustre_msg_get_transno(struct lustre_msg *msg)
1490 switch (msg->lm_magic) {
1491 case LUSTRE_MSG_MAGIC_V1:
1492 return ((struct lustre_msg_v1 *)msg)->lm_transno;
1493 case LUSTRE_MSG_MAGIC_V2: {
1494 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1496 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1499 return pb->pb_transno;
1502 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1507 int lustre_msg_get_status(struct lustre_msg *msg)
1509 switch (msg->lm_magic) {
1510 case LUSTRE_MSG_MAGIC_V1:
1511 return ((struct lustre_msg_v1 *)msg)->lm_status;
1512 case LUSTRE_MSG_MAGIC_V2: {
1513 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1515 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1518 return pb->pb_status;
1521 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1522 /* status might be printed in debug code while message
1528 __u64 lustre_msg_get_slv(struct lustre_msg *msg)
1530 switch (msg->lm_magic) {
1531 case LUSTRE_MSG_MAGIC_V1:
1533 case LUSTRE_MSG_MAGIC_V2: {
1534 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1536 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1542 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1548 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv)
1550 switch (msg->lm_magic) {
1551 case LUSTRE_MSG_MAGIC_V1:
1553 case LUSTRE_MSG_MAGIC_V2: {
1554 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1556 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1563 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1568 __u32 lustre_msg_get_limit(struct lustre_msg *msg)
1570 switch (msg->lm_magic) {
1571 case LUSTRE_MSG_MAGIC_V1:
1573 case LUSTRE_MSG_MAGIC_V2: {
1574 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1576 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1579 return pb->pb_limit;
1582 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1588 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit)
1590 switch (msg->lm_magic) {
1591 case LUSTRE_MSG_MAGIC_V1:
1593 case LUSTRE_MSG_MAGIC_V2: {
1594 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1596 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1599 pb->pb_limit = limit;
1603 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1608 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg)
1610 switch (msg->lm_magic) {
1611 case LUSTRE_MSG_MAGIC_V1:
1612 return ((struct lustre_msg_v1 *)msg)->lm_conn_cnt;
1613 case LUSTRE_MSG_MAGIC_V2: {
1614 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1616 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1619 return pb->pb_conn_cnt;
1622 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1627 int lustre_msg_is_v1(struct lustre_msg *msg)
1629 switch (msg->lm_magic) {
1630 case LUSTRE_MSG_MAGIC_V1:
1631 case LUSTRE_MSG_MAGIC_V1_SWABBED:
1638 __u32 lustre_msg_get_magic(struct lustre_msg *msg)
1640 switch (msg->lm_magic) {
1641 case LUSTRE_MSG_MAGIC_V1:
1642 case LUSTRE_MSG_MAGIC_V1_SWABBED:
1643 case LUSTRE_MSG_MAGIC_V2:
1644 case LUSTRE_MSG_MAGIC_V2_SWABBED:
1645 return msg->lm_magic;
1647 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1652 __u32 lustre_msg_get_timeout(struct lustre_msg *msg)
1654 switch (msg->lm_magic) {
1655 case LUSTRE_MSG_MAGIC_V1:
1657 case LUSTRE_MSG_MAGIC_V2: {
1658 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1660 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1663 return pb->pb_timeout;
1666 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1671 __u32 lustre_msg_get_service_time(struct lustre_msg *msg)
1673 switch (msg->lm_magic) {
1674 case LUSTRE_MSG_MAGIC_V1:
1676 case LUSTRE_MSG_MAGIC_V2: {
1677 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1679 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1682 return pb->pb_service_time;
1685 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1690 __u32 lustre_msg_get_cksum(struct lustre_msg *msg)
1692 switch (msg->lm_magic) {
1693 case LUSTRE_MSG_MAGIC_V1:
1695 case LUSTRE_MSG_MAGIC_V2:
1696 return msg->lm_cksum;
1698 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1704 * the sizeof ptlrpc_body in 1.6 is 88 bytes (64 bytes shorter than current
1705 * size), to be able to interoperate with 1.6 we only calculate checksum
1706 * aginst first 88 bytes of ptlrpc_body.
1708 static const int ptlrpc_body_size_16 = 88;
1710 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg)
1712 switch (msg->lm_magic) {
1713 case LUSTRE_MSG_MAGIC_V1:
1715 case LUSTRE_MSG_MAGIC_V2: {
1716 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1717 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1718 return crc32_le(~(__u32)0, (unsigned char *)pb,
1719 ptlrpc_body_size_16);
1722 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1727 void lustre_msg_set_handle(struct lustre_msg *msg, struct lustre_handle *handle)
1729 switch (msg->lm_magic) {
1730 case LUSTRE_MSG_MAGIC_V1:
1731 ((struct lustre_msg_v1 *)msg)->lm_handle = *handle;
1733 case LUSTRE_MSG_MAGIC_V2: {
1734 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1735 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1736 pb->pb_handle = *handle;
1740 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1744 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type)
1746 switch (msg->lm_magic) {
1747 case LUSTRE_MSG_MAGIC_V1:
1748 ((struct lustre_msg_v1 *)msg)->lm_type = type;
1750 case LUSTRE_MSG_MAGIC_V2: {
1751 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1752 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1757 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1761 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc)
1763 switch (msg->lm_magic) {
1764 case LUSTRE_MSG_MAGIC_V1:
1765 ((struct lustre_msg_v1 *)msg)->lm_opc = opc;
1767 case LUSTRE_MSG_MAGIC_V2: {
1768 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1769 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1774 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1778 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid)
1780 switch (msg->lm_magic) {
1781 case LUSTRE_MSG_MAGIC_V1:
1782 ((struct lustre_msg_v1 *)msg)->lm_last_xid = last_xid;
1784 case LUSTRE_MSG_MAGIC_V2: {
1785 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1786 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1787 pb->pb_last_xid = last_xid;
1791 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1795 void lustre_msg_set_last_committed(struct lustre_msg *msg, __u64 last_committed)
1797 switch (msg->lm_magic) {
1798 case LUSTRE_MSG_MAGIC_V1:
1799 ((struct lustre_msg_v1 *)msg)->lm_last_committed=last_committed;
1801 case LUSTRE_MSG_MAGIC_V2: {
1802 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1803 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1804 pb->pb_last_committed = last_committed;
1808 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1812 void lustre_msg_set_versions(struct lustre_msg *msg, __u64 *versions)
1814 switch (msg->lm_magic) {
1815 case LUSTRE_MSG_MAGIC_V1:
1817 case LUSTRE_MSG_MAGIC_V2: {
1818 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1819 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1820 #ifdef PTLRPC_INTEROP_1_6
1821 /* do nothing for old clients */
1822 if (lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF) < sizeof (*pb))
1825 pb->pb_pre_versions[0] = versions[0];
1826 pb->pb_pre_versions[1] = versions[1];
1827 pb->pb_pre_versions[2] = versions[2];
1828 pb->pb_pre_versions[3] = versions[3];
1832 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1837 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno)
1839 switch (msg->lm_magic) {
1840 case LUSTRE_MSG_MAGIC_V1:
1841 ((struct lustre_msg_v1 *)msg)->lm_transno = transno;
1843 case LUSTRE_MSG_MAGIC_V2: {
1844 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1845 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1846 pb->pb_transno = transno;
1850 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1854 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status)
1856 switch (msg->lm_magic) {
1857 case LUSTRE_MSG_MAGIC_V1:
1858 ((struct lustre_msg_v1 *)msg)->lm_status = status;
1860 case LUSTRE_MSG_MAGIC_V2: {
1861 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1862 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1863 pb->pb_status = status;
1867 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1871 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt)
1873 switch (msg->lm_magic) {
1874 case LUSTRE_MSG_MAGIC_V1:
1875 ((struct lustre_msg_v1 *)msg)->lm_conn_cnt = conn_cnt;
1877 case LUSTRE_MSG_MAGIC_V2: {
1878 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1879 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1880 pb->pb_conn_cnt = conn_cnt;
1884 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1888 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout)
1890 switch (msg->lm_magic) {
1891 case LUSTRE_MSG_MAGIC_V1:
1893 case LUSTRE_MSG_MAGIC_V2: {
1894 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1895 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1896 pb->pb_timeout = timeout;
1900 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1904 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time)
1906 switch (msg->lm_magic) {
1907 case LUSTRE_MSG_MAGIC_V1:
1909 case LUSTRE_MSG_MAGIC_V2: {
1910 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1911 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1912 pb->pb_service_time = service_time;
1916 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1920 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum)
1922 switch (msg->lm_magic) {
1923 case LUSTRE_MSG_MAGIC_V1:
1925 case LUSTRE_MSG_MAGIC_V2:
1926 msg->lm_cksum = cksum;
1929 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1934 /* byte flipping routines for all wire types declared in
1935 * lustre_idl.h implemented here.
1937 void lustre_swab_ptlrpc_body(struct ptlrpc_body *b, int msgsize)
1939 __swab32s (&b->pb_type);
1940 __swab32s (&b->pb_version);
1941 __swab32s (&b->pb_opc);
1942 __swab32s (&b->pb_status);
1943 __swab64s (&b->pb_last_xid);
1944 __swab64s (&b->pb_last_seen);
1945 __swab64s (&b->pb_last_committed);
1946 __swab64s (&b->pb_transno);
1947 __swab32s (&b->pb_flags);
1948 __swab32s (&b->pb_op_flags);
1949 __swab32s (&b->pb_conn_cnt);
1950 __swab32s (&b->pb_timeout);
1951 __swab32s (&b->pb_service_time);
1952 __swab64s (&b->pb_slv);
1953 __swab32s (&b->pb_limit);
1954 if (msgsize < offsetof(struct ptlrpc_body, pb_pre_versions[4]))
1956 __swab64s (&b->pb_pre_versions[0]);
1957 __swab64s (&b->pb_pre_versions[1]);
1958 __swab64s (&b->pb_pre_versions[2]);
1959 __swab64s (&b->pb_pre_versions[3]);
1960 CLASSERT(offsetof(typeof(*b), pb_padding) != 0);
1963 void lustre_swab_connect(struct obd_connect_data *ocd)
1965 __swab64s(&ocd->ocd_connect_flags);
1966 __swab32s(&ocd->ocd_version);
1967 __swab32s(&ocd->ocd_grant);
1968 __swab32s(&ocd->ocd_index);
1969 __swab32s(&ocd->ocd_brw_size);
1970 __swab64s(&ocd->ocd_ibits_known);
1971 __swab32s(&ocd->ocd_nllu);
1972 __swab32s(&ocd->ocd_nllg);
1973 __swab64s(&ocd->ocd_transno);
1974 __swab32s(&ocd->ocd_group);
1975 __swab32s(&ocd->ocd_cksum_types);
1976 CLASSERT(offsetof(typeof(*ocd), padding1) != 0);
1977 CLASSERT(offsetof(typeof(*ocd), padding2) != 0);
1980 void lustre_swab_obdo (struct obdo *o)
1982 __swab64s (&o->o_valid);
1983 __swab64s (&o->o_id);
1984 __swab64s (&o->o_gr);
1985 __swab64s (&o->o_fid);
1986 __swab64s (&o->o_size);
1987 __swab64s (&o->o_mtime);
1988 __swab64s (&o->o_atime);
1989 __swab64s (&o->o_ctime);
1990 __swab64s (&o->o_blocks);
1991 __swab64s (&o->o_grant);
1992 __swab32s (&o->o_blksize);
1993 __swab32s (&o->o_mode);
1994 __swab32s (&o->o_uid);
1995 __swab32s (&o->o_gid);
1996 __swab32s (&o->o_flags);
1997 __swab32s (&o->o_nlink);
1998 __swab32s (&o->o_generation);
1999 __swab32s (&o->o_misc);
2000 __swab32s (&o->o_easize);
2001 __swab32s (&o->o_mds);
2002 __swab32s (&o->o_stripe_idx);
2003 __swab32s (&o->o_padding_1);
2004 /* o_inline is opaque */
2007 void lustre_swab_obd_statfs (struct obd_statfs *os)
2009 __swab64s (&os->os_type);
2010 __swab64s (&os->os_blocks);
2011 __swab64s (&os->os_bfree);
2012 __swab64s (&os->os_bavail);
2013 __swab64s (&os->os_files);
2014 __swab64s (&os->os_ffree);
2015 /* no need to swab os_fsid */
2016 __swab32s (&os->os_bsize);
2017 __swab32s (&os->os_namelen);
2018 __swab64s (&os->os_maxbytes);
2019 __swab32s (&os->os_state);
2020 /* no need to swap os_spare */
2023 void lustre_swab_obd_ioobj (struct obd_ioobj *ioo)
2025 __swab64s (&ioo->ioo_id);
2026 __swab64s (&ioo->ioo_gr);
2027 __swab32s (&ioo->ioo_type);
2028 __swab32s (&ioo->ioo_bufcnt);
2031 void lustre_swab_niobuf_remote (struct niobuf_remote *nbr)
2033 __swab64s (&nbr->offset);
2034 __swab32s (&nbr->len);
2035 __swab32s (&nbr->flags);
2038 void lustre_swab_ost_body (struct ost_body *b)
2040 lustre_swab_obdo (&b->oa);
2043 void lustre_swab_ost_last_id(obd_id *id)
2048 void lustre_swab_ost_lvb(struct ost_lvb *lvb)
2050 __swab64s(&lvb->lvb_size);
2051 __swab64s(&lvb->lvb_mtime);
2052 __swab64s(&lvb->lvb_atime);
2053 __swab64s(&lvb->lvb_ctime);
2054 __swab64s(&lvb->lvb_blocks);
2057 void lustre_swab_mds_status_req (struct mds_status_req *r)
2059 __swab32s (&r->flags);
2060 __swab32s (&r->repbuf);
2063 void lustre_swab_mds_body (struct mds_body *b)
2065 lustre_swab_ll_fid (&b->fid1);
2066 lustre_swab_ll_fid (&b->fid2);
2067 /* handle is opaque */
2068 __swab64s (&b->valid);
2069 __swab64s (&b->size);
2070 __swab64s (&b->mtime);
2071 __swab64s (&b->atime);
2072 __swab64s (&b->ctime);
2073 __swab64s (&b->blocks);
2074 __swab64s (&b->io_epoch);
2075 __swab64s (&b->ino);
2076 __swab32s (&b->fsuid);
2077 __swab32s (&b->fsgid);
2078 __swab32s (&b->capability);
2079 __swab32s (&b->mode);
2080 __swab32s (&b->uid);
2081 __swab32s (&b->gid);
2082 __swab32s (&b->flags);
2083 __swab32s (&b->rdev);
2084 __swab32s (&b->nlink);
2085 __swab32s (&b->generation);
2086 __swab32s (&b->suppgid);
2087 __swab32s (&b->eadatasize);
2088 __swab32s (&b->aclsize);
2089 __swab32s (&b->max_mdsize);
2090 __swab32s (&b->max_cookiesize);
2091 __swab32s (&b->padding_4);
2094 void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
2097 __swab32s(&mti->mti_lustre_ver);
2098 __swab32s(&mti->mti_stripe_index);
2099 __swab32s(&mti->mti_config_ver);
2100 __swab32s(&mti->mti_flags);
2101 __swab32s(&mti->mti_nid_count);
2102 CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
2103 for (i = 0; i < MTI_NIDS_MAX; i++)
2104 __swab64s(&mti->mti_nids[i]);
2107 static void lustre_swab_obd_dqinfo (struct obd_dqinfo *i)
2109 __swab64s (&i->dqi_bgrace);
2110 __swab64s (&i->dqi_igrace);
2111 __swab32s (&i->dqi_flags);
2112 __swab32s (&i->dqi_valid);
2115 static void lustre_swab_obd_dqblk (struct obd_dqblk *b)
2117 __swab64s (&b->dqb_ihardlimit);
2118 __swab64s (&b->dqb_isoftlimit);
2119 __swab64s (&b->dqb_curinodes);
2120 __swab64s (&b->dqb_bhardlimit);
2121 __swab64s (&b->dqb_bsoftlimit);
2122 __swab64s (&b->dqb_curspace);
2123 __swab64s (&b->dqb_btime);
2124 __swab64s (&b->dqb_itime);
2125 __swab32s (&b->dqb_valid);
2126 CLASSERT(offsetof(typeof(*b), padding) != 0);
2129 void lustre_swab_obd_quotactl (struct obd_quotactl *q)
2131 __swab32s (&q->qc_cmd);
2132 __swab32s (&q->qc_type);
2133 __swab32s (&q->qc_id);
2134 __swab32s (&q->qc_stat);
2135 lustre_swab_obd_dqinfo (&q->qc_dqinfo);
2136 lustre_swab_obd_dqblk (&q->qc_dqblk);
2139 void lustre_swab_quota_adjust_qunit (struct quota_adjust_qunit *q)
2141 __swab32s (&q->qaq_flags);
2142 __swab32s (&q->qaq_id);
2143 __swab64s (&q->qaq_bunit_sz);
2144 __swab64s (&q->qaq_iunit_sz);
2147 void lustre_swab_mds_rec_setattr (struct mds_rec_setattr *sa)
2149 __swab32s (&sa->sa_opcode);
2150 __swab32s (&sa->sa_fsuid);
2151 __swab32s (&sa->sa_fsgid);
2152 __swab32s (&sa->sa_cap);
2153 __swab32s (&sa->sa_suppgid);
2154 __swab32s (&sa->sa_mode);
2155 lustre_swab_ll_fid (&sa->sa_fid);
2156 __swab64s (&sa->sa_valid);
2157 __swab64s (&sa->sa_size);
2158 __swab64s (&sa->sa_mtime);
2159 __swab64s (&sa->sa_atime);
2160 __swab64s (&sa->sa_ctime);
2161 __swab32s (&sa->sa_uid);
2162 __swab32s (&sa->sa_gid);
2163 __swab32s (&sa->sa_attr_flags);
2164 CLASSERT(offsetof(typeof(*sa), sa_padding) != 0);
2167 void lustre_swab_mds_rec_join (struct mds_rec_join *jr)
2169 __swab64s(&jr->jr_headsize);
2170 lustre_swab_ll_fid(&jr->jr_fid);
2173 void lustre_swab_mds_rec_create (struct mds_rec_create *cr)
2175 __swab32s (&cr->cr_opcode);
2176 __swab32s (&cr->cr_fsuid);
2177 __swab32s (&cr->cr_fsgid);
2178 __swab32s (&cr->cr_cap);
2179 __swab32s (&cr->cr_flags); /* for use with open */
2180 __swab32s (&cr->cr_mode);
2181 lustre_swab_ll_fid (&cr->cr_fid);
2182 lustre_swab_ll_fid (&cr->cr_replayfid);
2183 __swab64s (&cr->cr_time);
2184 __swab64s (&cr->cr_rdev);
2185 __swab32s (&cr->cr_suppgid);
2186 CLASSERT(offsetof(typeof(*cr), cr_padding_1) != 0);
2187 CLASSERT(offsetof(typeof(*cr), cr_padding_2) != 0);
2188 CLASSERT(offsetof(typeof(*cr), cr_padding_3) != 0);
2189 CLASSERT(offsetof(typeof(*cr), cr_padding_4) != 0);
2190 CLASSERT(offsetof(typeof(*cr), cr_padding_5) != 0);
2193 void lustre_swab_mds_rec_link (struct mds_rec_link *lk)
2195 __swab32s (&lk->lk_opcode);
2196 __swab32s (&lk->lk_fsuid);
2197 __swab32s (&lk->lk_fsgid);
2198 __swab32s (&lk->lk_cap);
2199 __swab32s (&lk->lk_suppgid1);
2200 __swab32s (&lk->lk_suppgid2);
2201 lustre_swab_ll_fid (&lk->lk_fid1);
2202 lustre_swab_ll_fid (&lk->lk_fid2);
2203 __swab64s (&lk->lk_time);
2204 CLASSERT(offsetof(typeof(*lk), lk_padding_1) != 0);
2205 CLASSERT(offsetof(typeof(*lk), lk_padding_2) != 0);
2206 CLASSERT(offsetof(typeof(*lk), lk_padding_3) != 0);
2207 CLASSERT(offsetof(typeof(*lk), lk_padding_4) != 0);
2210 void lustre_swab_mds_rec_unlink (struct mds_rec_unlink *ul)
2212 __swab32s (&ul->ul_opcode);
2213 __swab32s (&ul->ul_fsuid);
2214 __swab32s (&ul->ul_fsgid);
2215 __swab32s (&ul->ul_cap);
2216 __swab32s (&ul->ul_suppgid);
2217 __swab32s (&ul->ul_mode);
2218 lustre_swab_ll_fid (&ul->ul_fid1);
2219 lustre_swab_ll_fid (&ul->ul_fid2);
2220 __swab64s (&ul->ul_time);
2221 CLASSERT(offsetof(typeof(*ul), ul_padding_1) != 0);
2222 CLASSERT(offsetof(typeof(*ul), ul_padding_2) != 0);
2223 CLASSERT(offsetof(typeof(*ul), ul_padding_3) != 0);
2224 CLASSERT(offsetof(typeof(*ul), ul_padding_4) != 0);
2227 void lustre_swab_fiemap_extent(struct ll_fiemap_extent *fm_extent)
2229 __swab64s(&fm_extent->fe_logical);
2230 __swab64s(&fm_extent->fe_physical);
2231 __swab64s(&fm_extent->fe_length);
2232 __swab32s(&fm_extent->fe_flags);
2233 __swab32s(&fm_extent->fe_device);
2236 void lustre_swab_fiemap(struct ll_user_fiemap *fiemap)
2240 __swab64s(&fiemap->fm_start);
2241 __swab64s(&fiemap->fm_length);
2242 __swab32s(&fiemap->fm_flags);
2243 __swab32s(&fiemap->fm_mapped_extents);
2244 __swab32s(&fiemap->fm_extent_count);
2245 __swab32s(&fiemap->fm_reserved);
2247 for (i = 0; i < fiemap->fm_mapped_extents; i++)
2248 lustre_swab_fiemap_extent(&fiemap->fm_extents[i]);
2251 void lustre_swab_mds_rec_rename (struct mds_rec_rename *rn)
2253 __swab32s (&rn->rn_opcode);
2254 __swab32s (&rn->rn_fsuid);
2255 __swab32s (&rn->rn_fsgid);
2256 __swab32s (&rn->rn_cap);
2257 __swab32s (&rn->rn_suppgid1);
2258 __swab32s (&rn->rn_suppgid2);
2259 lustre_swab_ll_fid (&rn->rn_fid1);
2260 lustre_swab_ll_fid (&rn->rn_fid2);
2261 __swab64s (&rn->rn_time);
2262 CLASSERT(offsetof(typeof(*rn), rn_padding_1) != 0);
2263 CLASSERT(offsetof(typeof(*rn), rn_padding_2) != 0);
2264 CLASSERT(offsetof(typeof(*rn), rn_padding_3) != 0);
2265 CLASSERT(offsetof(typeof(*rn), rn_padding_4) != 0);
2268 void lustre_swab_lov_desc (struct lov_desc *ld)
2270 __swab32s (&ld->ld_tgt_count);
2271 __swab32s (&ld->ld_active_tgt_count);
2272 __swab32s (&ld->ld_default_stripe_count);
2273 __swab32s (&ld->ld_pattern);
2274 __swab64s (&ld->ld_default_stripe_size);
2275 __swab64s (&ld->ld_default_stripe_offset);
2276 __swab32s (&ld->ld_qos_maxage);
2277 /* uuid endian insensitive */
2281 static void lustre_swab_lov_user_md_common(struct lov_user_md_v1 *lum)
2284 __swab32s(&lum->lmm_magic);
2285 __swab32s(&lum->lmm_pattern);
2286 __swab64s(&lum->lmm_object_id);
2287 __swab64s(&lum->lmm_object_gr);
2288 __swab32s(&lum->lmm_stripe_size);
2289 __swab16s(&lum->lmm_stripe_count);
2290 __swab16s(&lum->lmm_stripe_offset);
2294 static void lustre_swab_lov_user_md_v1(struct lov_user_md_v1 *lum)
2297 CDEBUG(D_IOCTL, "swabbing lov_user_md v1\n");
2298 lustre_swab_lov_user_md_common(lum);
2302 static void lustre_swab_lov_user_md_v3(struct lov_user_md_v3 *lum)
2305 CDEBUG(D_IOCTL, "swabbing lov_user_md v3\n");
2306 lustre_swab_lov_user_md_common((struct lov_user_md_v1 *)lum);
2307 /* lmm_pool_name nothing to do with char */
2311 int lustre_swab_lov_user_md(struct lov_user_md_v1 *lum)
2314 switch (lum->lmm_magic) {
2315 case LOV_USER_MAGIC_V1:
2316 case LOV_USER_MAGIC_V1_SWABBED:
2317 lustre_swab_lov_user_md_v1(lum);
2319 case LOV_USER_MAGIC_V3:
2320 case LOV_USER_MAGIC_V3_SWABBED:
2321 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lum);
2324 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
2325 " %#08x != %#08x nor %#08x\n",
2326 lum->lmm_magic, LOV_USER_MAGIC_V1,
2333 void lustre_swab_lov_mds_md(struct lov_mds_md *lmm)
2336 CDEBUG(D_IOCTL, "swabbing lov_mds_md\n");
2337 __swab32s(&lmm->lmm_magic);
2338 __swab32s(&lmm->lmm_pattern);
2339 __swab64s(&lmm->lmm_object_id);
2340 __swab64s(&lmm->lmm_object_gr);
2341 __swab32s(&lmm->lmm_stripe_size);
2342 __swab32s(&lmm->lmm_stripe_count);
2346 void lustre_swab_lov_user_md_join(struct lov_user_md_join *lumj)
2349 CDEBUG(D_IOCTL, "swabbing lov_user_md_join\n");
2350 __swab32s(&lumj->lmm_magic);
2351 __swab32s(&lumj->lmm_pattern);
2352 __swab64s(&lumj->lmm_object_id);
2353 __swab64s(&lumj->lmm_object_gr);
2354 __swab32s(&lumj->lmm_stripe_size);
2355 __swab32s(&lumj->lmm_stripe_count);
2356 __swab32s(&lumj->lmm_extent_count);
2360 int lustre_swab_lov_user_md_objects(struct lov_user_md *lum)
2363 __u16 stripe_count = lum->lmm_stripe_count;
2364 struct lov_user_ost_data *lod;
2366 switch (lum->lmm_magic) {
2367 case LOV_USER_MAGIC_V1_SWABBED:
2368 __swab16s(&stripe_count);
2369 case LOV_USER_MAGIC_V1:
2370 lod = lum->lmm_objects;
2372 case LOV_USER_MAGIC_V3_SWABBED:
2373 __swab16s(&stripe_count);
2374 case LOV_USER_MAGIC_V3:
2375 lod = ((struct lov_user_md_v3 *)lum)->lmm_objects;
2378 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
2379 " %#08x != %#08x nor %#08x\n",
2380 lum->lmm_magic, LOV_USER_MAGIC_V1,
2384 for (i = 0; i < stripe_count; i++) {
2385 __swab64s(&(lod[i].l_object_id));
2386 __swab64s(&(lod[i].l_object_gr));
2387 __swab32s(&(lod[i].l_ost_gen));
2388 __swab32s(&(lod[i].l_ost_idx));
2393 void lustre_swab_ldlm_res_id (struct ldlm_res_id *id)
2397 for (i = 0; i < RES_NAME_SIZE; i++)
2398 __swab64s (&id->name[i]);
2401 void lustre_swab_ldlm_policy_data (ldlm_policy_data_t *d)
2403 /* the lock data is a union and the first two fields are always an
2404 * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
2405 * data the same way. */
2406 __swab64s(&d->l_extent.start);
2407 __swab64s(&d->l_extent.end);
2408 __swab64s(&d->l_extent.gid);
2409 __swab32s(&d->l_flock.pid);
2412 void lustre_swab_ldlm_intent (struct ldlm_intent *i)
2414 __swab64s (&i->opc);
2417 void lustre_swab_ldlm_resource_desc (struct ldlm_resource_desc *r)
2419 __swab32s (&r->lr_type);
2420 CLASSERT(offsetof(typeof(*r), lr_padding) != 0);
2421 lustre_swab_ldlm_res_id (&r->lr_name);
2424 void lustre_swab_ldlm_lock_desc (struct ldlm_lock_desc *l)
2426 lustre_swab_ldlm_resource_desc (&l->l_resource);
2427 __swab32s (&l->l_req_mode);
2428 __swab32s (&l->l_granted_mode);
2429 lustre_swab_ldlm_policy_data (&l->l_policy_data);
2432 void lustre_swab_ldlm_request (struct ldlm_request *rq)
2434 __swab32s (&rq->lock_flags);
2435 lustre_swab_ldlm_lock_desc (&rq->lock_desc);
2436 __swab32s (&rq->lock_count);
2437 /* lock_handle[] opaque */
2440 void lustre_swab_ldlm_reply (struct ldlm_reply *r)
2442 __swab32s (&r->lock_flags);
2443 CLASSERT(offsetof(typeof(*r), lock_padding) != 0);
2444 lustre_swab_ldlm_lock_desc (&r->lock_desc);
2445 /* lock_handle opaque */
2446 __swab64s (&r->lock_policy_res1);
2447 __swab64s (&r->lock_policy_res2);
2450 /* no one calls this */
2451 int llog_log_swabbed(struct llog_log_hdr *hdr)
2453 if (hdr->llh_hdr.lrh_type == __swab32(LLOG_HDR_MAGIC))
2455 if (hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC)
2460 void lustre_swab_qdata(struct qunit_data *d)
2462 __swab32s (&d->qd_id);
2463 __swab32s (&d->qd_flags);
2464 __swab64s (&d->qd_count);
2465 __swab64s (&d->qd_qunit);
2468 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2469 void lustre_swab_qdata_old2(struct qunit_data_old2 *d)
2471 __swab32s (&d->qd_id);
2472 __swab32s (&d->qd_flags);
2473 __swab64s (&d->qd_count);
2476 #warning "remove quota code above for format absolete in new release"
2481 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2482 void qdata_v2_to_v3(struct qunit_data_old2 *d,
2483 struct qunit_data *qdata)
2488 qdata->qd_id = d->qd_id;
2489 qdata->qd_flags = d->qd_flags;
2490 qdata->qd_count = d->qd_count;
2493 struct qunit_data_old2 *qdata_v3_to_v2(struct qunit_data *d)
2495 struct qunit_data tmp;
2496 struct qunit_data_old2 *ret;
2503 ret = (struct qunit_data_old2 *)d;
2504 ret->qd_id = tmp.qd_id;
2505 ret->qd_flags = tmp.qd_flags & LQUOTA_QUNIT_FLAGS;
2506 ret->qd_count = tmp.qd_count;
2510 #warning "remove quota code above for format absolete in new release"
2513 /* got qdata from request(req/rep) */
2514 int quota_get_qdata(void *request, struct qunit_data *qdata,
2515 int is_req, int is_exp)
2517 struct ptlrpc_request *req = (struct ptlrpc_request *)request;
2518 struct qunit_data *new;
2519 struct qunit_data_old2 *old2;
2520 int size2 = sizeof(struct qunit_data_old2);
2521 __u64 flags = is_exp ? req->rq_export->exp_connect_flags :
2522 req->rq_import->imp_connect_data.ocd_connect_flags;
2528 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2529 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS))
2530 goto without_change_qs;
2532 #warning "remove quota code above for format absolete in new release"
2535 /* support for quota64 and change_qs */
2536 if (flags & OBD_CONNECT_CHANGE_QS) {
2537 if (!(flags & OBD_CONNECT_QUOTA64)) {
2538 CDEBUG(D_ERROR, "Wire protocol for qunit is broken!\n");
2541 if (is_req == QUOTA_REQUEST)
2542 new = lustre_swab_reqbuf(req, REQ_REC_OFF,
2543 sizeof(struct qunit_data),
2546 new = lustre_swab_repbuf(req, REPLY_REC_OFF,
2547 sizeof(struct qunit_data),
2550 GOTO(out, rc = -EPROTO);
2552 QDATA_SET_CHANGE_QS(qdata);
2555 QDATA_CLR_CHANGE_QS(qdata);
2558 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2560 /* only support for quota64 */
2561 if (flags & OBD_CONNECT_QUOTA64) {
2563 if (is_req == QUOTA_REQUEST)
2564 old2 = lustre_swab_reqbuf(req, REQ_REC_OFF, size2,
2565 lustre_swab_qdata_old2);
2567 old2 = lustre_swab_repbuf(req, REPLY_REC_OFF, size2,
2568 lustre_swab_qdata_old2);
2570 GOTO(out, rc = -EPROTO);
2571 qdata_v2_to_v3(old2, qdata);
2576 #warning "remove quota code above for format absolete in new release"
2581 EXPORT_SYMBOL(quota_get_qdata);
2583 /* copy qdata to request(req/rep) */
2584 int quota_copy_qdata(void *request, struct qunit_data *qdata,
2585 int is_req, int is_exp)
2587 struct ptlrpc_request *req = (struct ptlrpc_request *)request;
2589 struct qunit_data_old2 *old2;
2590 __u64 flags = is_exp ? req->rq_export->exp_connect_flags :
2591 req->rq_import->imp_connect_data.ocd_connect_flags;
2597 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2598 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS))
2599 goto without_change_qs;
2601 #warning "remove quota code above for format absolete in new release"
2604 /* support for quota64 and change_qs */
2605 if (flags & OBD_CONNECT_CHANGE_QS) {
2606 if (!(flags & OBD_CONNECT_QUOTA64)) {
2607 CERROR("Wire protocol for qunit is broken!\n");
2610 if (is_req == QUOTA_REQUEST)
2611 target = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
2612 sizeof(struct qunit_data));
2614 target = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
2615 sizeof(struct qunit_data));
2617 GOTO(out, rc = -EPROTO);
2618 memcpy(target, qdata, sizeof(*qdata));
2622 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2624 /* only support for quota64 */
2625 if (flags & OBD_CONNECT_QUOTA64) {
2626 if (is_req == QUOTA_REQUEST)
2627 target = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
2628 sizeof(struct qunit_data_old2));
2630 target = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
2631 sizeof(struct qunit_data_old2));
2633 GOTO(out, rc = -EPROTO);
2634 old2 = qdata_v3_to_v2(qdata);
2635 memcpy(target, old2, sizeof(*old2));
2639 #warning "remove quota code above for format absolete in new release"
2644 EXPORT_SYMBOL(quota_copy_qdata);
2646 int quota_get_qunit_data_size(__u64 flag)
2650 if (flag & OBD_CONNECT_CHANGE_QS)
2651 size = sizeof(struct qunit_data);
2653 size = sizeof(struct qunit_data_old2);
2657 EXPORT_SYMBOL(quota_get_qunit_data_size);
2658 #endif /* __KERNEL__ */
2660 static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2662 LASSERT(req->rq_reqmsg);
2664 switch (req->rq_reqmsg->lm_magic) {
2665 case LUSTRE_MSG_MAGIC_V1:
2667 case LUSTRE_MSG_MAGIC_V2:
2668 return lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
2670 CERROR("incorrect message magic: %08x\n",
2671 req->rq_reqmsg->lm_magic);
2676 static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2678 LASSERT(req->rq_repmsg);
2680 switch (req->rq_repmsg->lm_magic) {
2681 case LUSTRE_MSG_MAGIC_V1:
2683 case LUSTRE_MSG_MAGIC_V2:
2684 return lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
2686 CERROR("incorrect message magic: %08x\n",
2687 req->rq_repmsg->lm_magic);
2692 void _debug_req(struct ptlrpc_request *req, __u32 mask,
2693 struct libcfs_debug_msg_data *data, const char *fmt, ... )
2702 /* Caller is responsible holding a reference on the request */
2703 LASSERT(req && atomic_read(&req->rq_refcount) > 0);
2705 if (req->rq_reqmsg &&
2706 (!lustre_msg_need_swab(req->rq_reqmsg) ||
2707 (lustre_req_need_swab(req) &&
2708 lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF)))) {
2709 opc = lustre_msg_get_opc(req->rq_reqmsg);
2710 req_fl = lustre_msg_get_flags(req->rq_reqmsg);
2713 if (req->rq_repmsg &&
2714 (!lustre_msg_need_swab(req->rq_repmsg) ||
2715 (lustre_rep_need_swab(req) &&
2716 lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF)))) {
2717 rep_fl = lustre_msg_get_flags(req->rq_repmsg);
2718 rep_status = lustre_msg_get_status(req->rq_repmsg);
2721 va_start(args, fmt);
2722 libcfs_debug_vmsg2(data->msg_cdls, data->msg_subsys, mask,
2723 data->msg_file, data->msg_fn, data->msg_line, fmt, args,
2724 " req@%p x"LPU64"/t"LPD64" o%d->%s@%s:%d/%d lens %d/%d e %d "
2725 "to %d dl %ld ref %d fl "REQ_FLAGS_FMT"/%x/%x rc %d/%d\n",
2726 req, req->rq_xid, req->rq_transno, opc,
2727 req->rq_import ? obd2cli_tgt(req->rq_import->imp_obd) :
2729 (char*)req->rq_export->exp_client_uuid.uuid : "<?>",
2731 (char *)req->rq_import->imp_connection->c_remote_uuid.uuid :
2733 (char *)req->rq_export->exp_connection->c_remote_uuid.uuid :
2734 "<?>", req->rq_request_portal, req->rq_reply_portal,
2735 req->rq_reqlen, req->rq_replen,
2736 req->rq_early_count, !!req->rq_timeout, req->rq_deadline,
2737 atomic_read(&req->rq_refcount), DEBUG_REQ_FLAGS(req),
2738 req_fl, rep_fl, req->rq_status, rep_status);
2743 EXPORT_SYMBOL(_debug_req);