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 (c) 2002, 2010, Oracle and/or its affiliates. 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 spin_lock(&req->rq_lock);
421 req->rq_packed_final = 1;
422 spin_unlock(&req->rq_lock);
425 msg_len = lustre_msg_size_v1(count, lens);
426 size = sizeof(struct ptlrpc_reply_state) + msg_len;
428 if (unlikely(rs == NULL)) {
429 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service, size);
433 atomic_set(&rs->rs_refcount, 1); /* 1 ref for rq_reply_state */
434 rs->rs_cb_id.cbid_fn = reply_out_callback;
435 rs->rs_cb_id.cbid_arg = rs;
436 rs->rs_service = req->rq_rqbd->rqbd_service;
438 CFS_INIT_LIST_HEAD(&rs->rs_exp_list);
439 CFS_INIT_LIST_HEAD(&rs->rs_obd_list);
440 rs->rs_msg = (struct lustre_msg *)(rs + 1);
442 req->rq_replen = msg_len;
443 req->rq_reply_state = rs;
444 req->rq_repmsg = rs->rs_msg;
446 lustre_init_msg_v1(rs->rs_msg, count, lens, bufs);
448 PTLRPC_RS_DEBUG_LRU_ADD(rs);
453 static int lustre_pack_reply_v2(struct ptlrpc_request *req, int count,
454 __u32 *lens, char **bufs, int flags)
456 struct ptlrpc_reply_state *rs;
461 LASSERT(req->rq_reply_state == NULL);
463 if ((flags & LPRFL_EARLY_REPLY) == 0) {
464 spin_lock(&req->rq_lock);
465 req->rq_packed_final = 1;
466 spin_unlock(&req->rq_lock);
469 /* use the same size of ptlrpc_body as client requested for
470 * interoperability cases */
471 LASSERT(req->rq_reqmsg);
472 lens[MSG_PTLRPC_BODY_OFF] = lustre_msg_buflen(req->rq_reqmsg,
473 MSG_PTLRPC_BODY_OFF);
475 msg_len = lustre_msg_size_v2(count, lens);
476 size = sizeof(struct ptlrpc_reply_state) + msg_len;
478 if (unlikely(rs == NULL)) {
479 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service, size);
483 atomic_set(&rs->rs_refcount, 1); /* 1 ref for rq_reply_state */
484 rs->rs_cb_id.cbid_fn = reply_out_callback;
485 rs->rs_cb_id.cbid_arg = rs;
486 rs->rs_service = req->rq_rqbd->rqbd_service;
488 CFS_INIT_LIST_HEAD(&rs->rs_exp_list);
489 CFS_INIT_LIST_HEAD(&rs->rs_obd_list);
490 rs->rs_msg = (struct lustre_msg *)(rs + 1);
492 req->rq_replen = msg_len;
493 req->rq_reply_state = rs;
494 req->rq_repmsg = rs->rs_msg;
495 /* server side, no rq_repbuf */
496 lustre_init_msg_v2(rs->rs_msg, count, lens, bufs);
497 lustre_msg_add_version(rs->rs_msg, PTLRPC_MSG_VERSION);
498 lustre_set_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
500 PTLRPC_RS_DEBUG_LRU_ADD(rs);
505 int lustre_pack_reply_flags(struct ptlrpc_request *req, int count, __u32 *lens,
506 char **bufs, int flags)
508 __u32 size[] = { sizeof(struct ptlrpc_body) };
516 LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
518 switch (req->rq_reqmsg->lm_magic) {
519 case LUSTRE_MSG_MAGIC_V1:
520 return lustre_pack_reply_v1(req, count - 1, lens + 1,
521 bufs ? bufs + 1 : NULL, flags);
522 case LUSTRE_MSG_MAGIC_V2:
523 return lustre_pack_reply_v2(req, count, lens, bufs, flags);
525 CERROR("incorrect message magic: %08x\n",
526 req->rq_reqmsg->lm_magic);
531 int lustre_pack_reply(struct ptlrpc_request *req, int count, __u32 *lens,
534 int rc = lustre_pack_reply_flags(req, count, lens, bufs, 0);
536 CERROR("lustre_pack_reply failed: rc=%d size=%d\n", rc,
537 lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens));
542 void *lustre_msg_buf_v1(void *msg, int n, int min_size)
544 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
545 int i, offset, buflen, bufcount;
550 bufcount = m->lm_bufcount;
552 CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
557 buflen = m->lm_buflens[n];
558 if (buflen < min_size) {
559 CERROR("msg %p buffer[%d] size %d too small (required %d)\n",
560 m, n, buflen, min_size);
565 offset = lustre_msg_hdr_size_v1(bufcount);
566 for (i = 0; i < n; i++)
567 offset += size_round(m->lm_buflens[i]);
569 return (char *)m + offset;
572 void *lustre_msg_buf_v2(struct lustre_msg_v2 *m, int n, int min_size)
574 int i, offset, buflen, bufcount;
579 bufcount = m->lm_bufcount;
581 CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
586 buflen = m->lm_buflens[n];
587 if (buflen < min_size) {
588 CERROR("msg %p buffer[%d] size %d too small "
589 "(required %d, opc=%d)\n",
590 m, n, buflen, min_size, lustre_msg_get_opc(m));
594 offset = lustre_msg_hdr_size_v2(bufcount);
595 for (i = 0; i < n; i++)
596 offset += size_round(m->lm_buflens[i]);
598 return (char *)m + offset;
601 void *lustre_msg_buf(struct lustre_msg *m, int n, int min_size)
603 switch (m->lm_magic) {
604 case LUSTRE_MSG_MAGIC_V1:
605 return lustre_msg_buf_v1(m, n - 1, min_size);
606 case LUSTRE_MSG_MAGIC_V2:
607 return lustre_msg_buf_v2(m, n, min_size);
609 CERROR("incorrect message magic: %08x\n", m->lm_magic);
614 void lustre_shrink_reply_v1(struct ptlrpc_request *req, int segment,
615 unsigned int newlen, int move_data)
617 struct lustre_msg_v1 *msg = (struct lustre_msg_v1 *)req->rq_repmsg;
618 char *tail = NULL, *newpos;
621 LASSERT(req->rq_reply_state);
623 LASSERT(segment >= 0);
624 LASSERTF(msg->lm_bufcount > segment, "message have %d - requested %d\n",
625 msg->lm_bufcount,segment);
626 LASSERT(msg->lm_buflens[segment] >= newlen);
628 if (msg->lm_buflens[segment] == newlen)
631 if (move_data && msg->lm_bufcount > segment + 1) {
632 tail = lustre_msg_buf_v1(msg, segment + 1, 0);
633 for (n = segment + 1; n < msg->lm_bufcount; n++)
634 tail_len += size_round(msg->lm_buflens[n]);
637 msg->lm_buflens[segment] = newlen;
639 if (tail && tail_len) {
640 newpos = lustre_msg_buf_v1(msg, segment + 1, 0);
641 LASSERT(newpos <= tail);
643 memcpy(newpos, tail, tail_len);
646 if (newlen == 0 && msg->lm_bufcount > segment + 1) {
647 memmove(&msg->lm_buflens[segment], &msg->lm_buflens[segment + 1],
648 (msg->lm_bufcount - segment - 1) * sizeof(__u32));
649 msg->lm_buflens[msg->lm_bufcount - 1] = 0;
652 req->rq_replen = lustre_msg_size_v1(msg->lm_bufcount, msg->lm_buflens);
655 void lustre_shrink_reply_v2(struct ptlrpc_request *req, int segment,
656 unsigned int newlen, int move_data)
658 struct lustre_msg_v2 *msg = req->rq_repmsg;
659 char *tail = NULL, *newpos;
662 CDEBUG(D_INFO, "shrink req %p seg %d - len %d\n", req, segment, newlen);
663 LASSERT(req->rq_reply_state);
665 LASSERTF(msg->lm_bufcount > segment, "message have %d - requested %d\n",
666 msg->lm_bufcount,segment);
667 LASSERT(msg->lm_buflens[segment] >= newlen);
669 if (msg->lm_buflens[segment] == newlen)
672 if (move_data && msg->lm_bufcount > segment + 1) {
673 tail = lustre_msg_buf_v2(msg, segment + 1, 0);
674 for (n = segment + 1; n < msg->lm_bufcount; n++)
675 tail_len += size_round(msg->lm_buflens[n]);
678 msg->lm_buflens[segment] = newlen;
680 if (tail && tail_len) {
681 newpos = lustre_msg_buf_v2(msg, segment + 1, 0);
682 LASSERT(newpos <= tail);
684 memmove(newpos, tail, tail_len);
687 if (newlen == 0 && msg->lm_bufcount > segment + 1) {
688 memmove(&msg->lm_buflens[segment], &msg->lm_buflens[segment + 1],
689 (msg->lm_bufcount - segment - 1) * sizeof(__u32));
690 msg->lm_buflens[msg->lm_bufcount - 1] = 0;
693 req->rq_replen = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
697 * shrink @segment to size @newlen. if @move_data is non-zero, we also move
698 * data forward from @segment + 1.
700 * if @newlen == 0, we remove the segment completely, but we still keep the
701 * totally bufcount the same to save possible data moving. this will leave a
702 * unused segment with size 0 at the tail, but that's ok.
705 * + if any buffers higher than @segment has been filled in, must call shrink
706 * with non-zero @move_data.
707 * + caller should NOT keep pointers to msg buffers which higher than @segment
710 void lustre_shrink_reply(struct ptlrpc_request *req, int segment,
711 unsigned int newlen, int move_data)
713 switch (req->rq_repmsg->lm_magic) {
714 case LUSTRE_MSG_MAGIC_V1:
715 lustre_shrink_reply_v1(req, segment - 1, newlen, move_data);
717 case LUSTRE_MSG_MAGIC_V2:
718 lustre_shrink_reply_v2(req, segment, newlen, move_data);
721 LASSERTF(0, "incorrect message magic: %08x\n",
722 req->rq_repmsg->lm_magic);
726 void lustre_free_reply_state(struct ptlrpc_reply_state *rs)
728 PTLRPC_RS_DEBUG_LRU_DEL(rs);
730 LASSERT (atomic_read(&rs->rs_refcount) == 0);
731 LASSERT (!rs->rs_difficult || rs->rs_handled);
732 LASSERT (!rs->rs_on_net);
733 LASSERT (!rs->rs_scheduled);
734 LASSERT (rs->rs_export == NULL);
735 LASSERT (rs->rs_nlocks == 0);
736 LASSERT (list_empty(&rs->rs_exp_list));
737 LASSERT (list_empty(&rs->rs_obd_list));
739 if (unlikely(rs->rs_prealloc)) {
740 struct ptlrpc_service *svc = rs->rs_service;
742 spin_lock(&svc->srv_lock);
743 list_add(&rs->rs_list,
744 &svc->srv_free_rs_list);
745 spin_unlock(&svc->srv_lock);
746 cfs_waitq_signal(&svc->srv_free_rs_waitq);
748 OBD_FREE(rs, rs->rs_size);
752 int lustre_unpack_msg_v1(void *msg, int len)
754 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
755 int flipped, required_len, i;
758 /* Now we know the sender speaks my language. */
759 required_len = lustre_msg_hdr_size_v1(0);
760 if (len < required_len) {
761 /* can't even look inside the message */
762 CERROR("message length %d too small for lustre_msg\n", len);
766 flipped = m->lm_magic == LUSTRE_MSG_MAGIC_V1_SWABBED;
768 __swab32s(&m->lm_magic);
769 __swab32s(&m->lm_type);
770 __swab32s(&m->lm_version);
771 __swab32s(&m->lm_opc);
772 __swab64s(&m->lm_last_xid);
773 __swab64s(&m->lm_last_committed);
774 __swab64s(&m->lm_transno);
775 __swab32s(&m->lm_status);
776 __swab32s(&m->lm_flags);
777 __swab32s(&m->lm_conn_cnt);
778 __swab32s(&m->lm_bufcount);
781 if (m->lm_version != PTLRPC_MSG_VERSION) {
782 CERROR("wrong lustre_msg version %08x\n", m->lm_version);
786 required_len = lustre_msg_hdr_size_v1(m->lm_bufcount);
787 if (len < required_len) {
788 /* didn't receive all the buffer lengths */
789 CERROR("message length %d too small for %d buflens\n",
790 len, m->lm_bufcount);
794 for (i = 0; i < m->lm_bufcount; i++) {
796 __swab32s (&m->lm_buflens[i]);
797 required_len += size_round(m->lm_buflens[i]);
800 if (len < required_len) {
801 CERROR("len: %d, required_len %d\n", len, required_len);
802 CERROR("bufcount: %d\n", m->lm_bufcount);
803 for (i = 0; i < m->lm_bufcount; i++)
804 CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
811 static int lustre_unpack_msg_v2(struct lustre_msg_v2 *m, int len)
813 int flipped, required_len, i;
815 /* Now we know the sender speaks my language. */
816 required_len = lustre_msg_hdr_size_v2(0);
817 if (len < required_len) {
818 /* can't even look inside the message */
819 CERROR("message length %d too small for lustre_msg\n", len);
823 flipped = m->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED;
825 __swab32s(&m->lm_magic);
826 __swab32s(&m->lm_bufcount);
827 __swab32s(&m->lm_secflvr);
828 __swab32s(&m->lm_repsize);
829 __swab32s(&m->lm_cksum);
830 __swab32s(&m->lm_flags);
831 CLASSERT(offsetof(typeof(*m), lm_padding_2) != 0);
832 CLASSERT(offsetof(typeof(*m), lm_padding_3) != 0);
835 required_len = lustre_msg_hdr_size_v2(m->lm_bufcount);
836 if (len < required_len) {
837 /* didn't receive all the buffer lengths */
838 CERROR ("message length %d too small for %d buflens\n",
839 len, m->lm_bufcount);
843 for (i = 0; i < m->lm_bufcount; i++) {
845 __swab32s(&m->lm_buflens[i]);
846 required_len += size_round(m->lm_buflens[i]);
849 if (len < required_len) {
850 CERROR("len: %d, required_len %d\n", len, required_len);
851 CERROR("bufcount: %d\n", m->lm_bufcount);
852 for (i = 0; i < m->lm_bufcount; i++)
853 CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
861 * return 1 if some endianness conversions are needed for the req buffers,
863 * -EINVAL if message has wrong magic
865 int lustre_unpack_msg(struct lustre_msg *m, int len)
867 int required_len, rc;
871 /* We can provide a slightly better error log, if we check the
872 * message magic and version first. In the future, struct
873 * lustre_msg may grow, and we'd like to log a version mismatch,
874 * rather than a short message.
877 required_len = offsetof(struct lustre_msg, lm_magic) +
879 if (len < required_len) {
880 /* can't even look inside the message */
881 CERROR("message length %d too small for magic/version check\n",
886 swab_needed = lustre_msg_need_swab(m);
888 switch (m->lm_magic) {
889 case LUSTRE_MSG_MAGIC_V1:
890 case LUSTRE_MSG_MAGIC_V1_SWABBED:
891 rc = lustre_unpack_msg_v1(m, len);
893 case LUSTRE_MSG_MAGIC_V2:
894 case LUSTRE_MSG_MAGIC_V2_SWABBED:
895 rc = lustre_unpack_msg_v2(m, len);
898 CERROR("incorrect message magic: %08x\n", m->lm_magic);
908 static inline int lustre_unpack_ptlrpc_body_v2(struct lustre_msg_v2 *m,
909 int offset, int swab_needed)
911 struct ptlrpc_body *pb;
913 pb = lustre_msg_buf_v2(m, offset, PTLRPC_BODY_MIN_SIZE);
915 CERROR("error unpacking ptlrpc body\n");
919 lustre_swab_ptlrpc_body(pb, lustre_msg_buflen(m, offset));
921 if ((pb->pb_version & ~LUSTRE_VERSION_MASK) != PTLRPC_MSG_VERSION) {
922 CERROR("wrong lustre_msg version %08x\n", pb->pb_version);
929 int lustre_unpack_msg_ptlrpc_body(struct lustre_msg *msg,
930 int offset, int swab_needed)
932 switch (msg->lm_magic) {
933 case LUSTRE_MSG_MAGIC_V1:
935 case LUSTRE_MSG_MAGIC_V2:
936 return lustre_unpack_ptlrpc_body_v2(msg, offset, swab_needed);
938 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
943 int lustre_unpack_req_ptlrpc_body(struct ptlrpc_request *req, int offset)
945 switch (req->rq_reqmsg->lm_magic) {
946 case LUSTRE_MSG_MAGIC_V1:
948 case LUSTRE_MSG_MAGIC_V2: {
949 int swab_needed = ptlrpc_reqbuf_need_swab(req, offset);
950 lustre_set_req_swabbed(req, offset);
951 return lustre_unpack_ptlrpc_body_v2(req->rq_reqmsg, offset,
955 CERROR("incorrect message magic: %08x\n",
956 req->rq_reqmsg->lm_magic);
961 int lustre_unpack_rep_ptlrpc_body(struct ptlrpc_request *req, int offset)
963 switch (req->rq_repmsg->lm_magic) {
964 case LUSTRE_MSG_MAGIC_V1:
966 case LUSTRE_MSG_MAGIC_V2:{
967 int swab_needed = ptlrpc_repbuf_need_swab(req, offset);
968 lustre_set_rep_swabbed(req, offset);
969 return lustre_unpack_ptlrpc_body_v2(req->rq_repmsg, offset,
973 CERROR("incorrect message magic: %08x\n",
974 req->rq_repmsg->lm_magic);
979 static inline int lustre_msg_buflen_v1(void *msg, int n)
981 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
984 if (n >= m->lm_bufcount)
987 return m->lm_buflens[n];
990 static inline int lustre_msg_buflen_v2(struct lustre_msg_v2 *m, int n)
992 if (n >= m->lm_bufcount)
995 return m->lm_buflens[n];
999 * lustre_msg_buflen - return the length of buffer @n in message @m
1000 * @m - lustre_msg (request or reply) to look at
1001 * @n - message index (base 0)
1003 * returns zero for non-existent message indices
1005 int lustre_msg_buflen(struct lustre_msg *m, int n)
1007 switch (m->lm_magic) {
1008 case LUSTRE_MSG_MAGIC_V1:
1009 return lustre_msg_buflen_v1(m, n - 1);
1010 case LUSTRE_MSG_MAGIC_V2:
1011 return lustre_msg_buflen_v2(m, n);
1013 CERROR("incorrect message magic: %08x\n", m->lm_magic);
1017 EXPORT_SYMBOL(lustre_msg_buflen);
1019 static inline void lustre_msg_set_buflen_v1(void *msg, int n, int len)
1021 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
1024 if (n >= m->lm_bufcount)
1027 m->lm_buflens[n] = len;
1031 lustre_msg_set_buflen_v2(struct lustre_msg_v2 *m, int n, int len)
1033 if (n >= m->lm_bufcount)
1036 m->lm_buflens[n] = len;
1039 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len)
1041 switch (m->lm_magic) {
1042 case LUSTRE_MSG_MAGIC_V1:
1043 lustre_msg_set_buflen_v1(m, n - 1, len);
1045 case LUSTRE_MSG_MAGIC_V2:
1046 lustre_msg_set_buflen_v2(m, n, len);
1049 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
1053 EXPORT_SYMBOL(lustre_msg_set_buflen);
1055 /* NB return the bufcount for lustre_msg_v2 format, so if message is packed
1056 * in V1 format, the result is one bigger. (add struct ptlrpc_body). */
1057 int lustre_msg_bufcount(struct lustre_msg *m)
1059 switch (m->lm_magic) {
1060 case LUSTRE_MSG_MAGIC_V1:
1061 return ((struct lustre_msg_v1 *)m)->lm_bufcount + 1;
1062 case LUSTRE_MSG_MAGIC_V2:
1063 return m->lm_bufcount;
1065 CERROR("incorrect message magic: %08x\n", m->lm_magic);
1069 EXPORT_SYMBOL(lustre_msg_bufcount);
1071 char *lustre_msg_string(struct lustre_msg *m, int index, int max_len)
1073 /* max_len == 0 means the string should fill the buffer */
1077 switch (m->lm_magic) {
1078 case LUSTRE_MSG_MAGIC_V1:
1079 str = lustre_msg_buf_v1(m, index - 1, 0);
1080 blen = lustre_msg_buflen_v1(m, index - 1);
1082 case LUSTRE_MSG_MAGIC_V2:
1083 str = lustre_msg_buf_v2(m, index, 0);
1084 blen = lustre_msg_buflen_v2(m, index);
1087 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
1091 CERROR ("can't unpack string in msg %p buffer[%d]\n", m, index);
1095 slen = strnlen(str, blen);
1097 if (slen == blen) { /* not NULL terminated */
1098 CERROR("can't unpack non-NULL terminated string in "
1099 "msg %p buffer[%d] len %d\n", m, index, blen);
1104 if (slen != blen - 1) {
1105 CERROR("can't unpack short string in msg %p "
1106 "buffer[%d] len %d: strlen %d\n",
1107 m, index, blen, slen);
1110 } else if (slen > max_len) {
1111 CERROR("can't unpack oversized string in msg %p "
1112 "buffer[%d] len %d strlen %d: max %d expected\n",
1113 m, index, blen, slen, max_len);
1120 /* Wrap up the normal fixed length cases */
1121 void *lustre_swab_buf(struct lustre_msg *msg, int index, int min_size,
1126 LASSERT(msg != NULL);
1127 switch (msg->lm_magic) {
1128 case LUSTRE_MSG_MAGIC_V1:
1129 ptr = lustre_msg_buf_v1(msg, index - 1, min_size);
1131 case LUSTRE_MSG_MAGIC_V2:
1132 ptr = lustre_msg_buf_v2(msg, index, min_size);
1135 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1140 if (swabber != NULL) {
1141 CDEBUG(D_NET, "Swab buffer %d\n", index);
1142 ((void (*)(void *))swabber)(ptr);
1148 void *lustre_swab_reqbuf(struct ptlrpc_request *req, int index, int min_size,
1151 if (lustre_req_swabbed(req, index))
1152 return lustre_msg_buf(req->rq_reqmsg, index, min_size);
1154 if (!lustre_req_need_swab(req))
1157 lustre_set_req_swabbed(req, index);
1158 return lustre_swab_buf(req->rq_reqmsg, index, min_size, swabber);
1161 void *lustre_swab_repbuf(struct ptlrpc_request *req, int index, int min_size,
1164 if (lustre_rep_swabbed(req, index))
1165 return lustre_msg_buf(req->rq_repmsg, index, min_size);
1167 if (!lustre_rep_need_swab(req))
1170 lustre_set_rep_swabbed(req, index);
1171 return lustre_swab_buf(req->rq_repmsg, index, min_size, swabber);
1174 static inline struct ptlrpc_body *lustre_msg_ptlrpc_body(struct lustre_msg *msg)
1176 return lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1177 PTLRPC_BODY_MIN_SIZE);
1180 __u32 lustre_msghdr_get_flags(struct lustre_msg *msg)
1182 switch (msg->lm_magic) {
1183 case LUSTRE_MSG_MAGIC_V1:
1185 case LUSTRE_MSG_MAGIC_V2:
1186 /* already in host endian */
1187 return msg->lm_flags;
1189 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1194 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags)
1196 switch (msg->lm_magic) {
1197 case LUSTRE_MSG_MAGIC_V1:
1199 case LUSTRE_MSG_MAGIC_V2:
1200 msg->lm_flags = flags;
1203 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1207 __u32 lustre_msg_get_flags(struct lustre_msg *msg)
1209 switch (msg->lm_magic) {
1210 case LUSTRE_MSG_MAGIC_V1:
1211 return ((struct lustre_msg_v1 *)msg)->lm_flags &
1213 case LUSTRE_MSG_MAGIC_V2: {
1214 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1216 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1219 return pb->pb_flags;
1222 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1223 /* flags might be printed in debug code while message
1229 void lustre_msg_add_flags(struct lustre_msg *msg, int flags)
1231 switch (msg->lm_magic) {
1232 case LUSTRE_MSG_MAGIC_V1:
1233 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1234 MSG_GEN_FLAG_MASK & flags;
1236 case LUSTRE_MSG_MAGIC_V2: {
1237 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1238 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1239 pb->pb_flags |= flags;
1243 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1247 void lustre_msg_set_flags(struct lustre_msg *msg, int flags)
1249 switch (msg->lm_magic) {
1250 case LUSTRE_MSG_MAGIC_V1:
1251 ((struct lustre_msg_v1 *)msg)->lm_flags &= ~MSG_GEN_FLAG_MASK;
1252 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1253 MSG_GEN_FLAG_MASK & flags;
1255 case LUSTRE_MSG_MAGIC_V2: {
1256 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1257 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1258 pb->pb_flags = flags;
1262 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1266 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags)
1268 switch (msg->lm_magic) {
1269 case LUSTRE_MSG_MAGIC_V1:
1270 ((struct lustre_msg_v1 *)msg)->lm_flags &=
1271 ~(MSG_GEN_FLAG_MASK & flags);
1273 case LUSTRE_MSG_MAGIC_V2: {
1274 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1275 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1276 pb->pb_flags &= ~(MSG_GEN_FLAG_MASK & flags);
1280 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1284 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg)
1286 switch (msg->lm_magic) {
1287 case LUSTRE_MSG_MAGIC_V1:
1288 return ((struct lustre_msg_v1 *)msg)->lm_flags >>
1290 case LUSTRE_MSG_MAGIC_V2: {
1291 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1293 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1296 return pb->pb_op_flags;
1299 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1304 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags)
1306 switch (msg->lm_magic) {
1307 case LUSTRE_MSG_MAGIC_V1:
1308 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1309 (flags & MSG_GEN_FLAG_MASK) << MSG_OP_FLAG_SHIFT;
1311 case LUSTRE_MSG_MAGIC_V2: {
1312 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1313 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1314 pb->pb_op_flags |= flags;
1318 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1322 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags)
1324 switch (msg->lm_magic) {
1325 case LUSTRE_MSG_MAGIC_V1:
1326 ((struct lustre_msg_v1 *)msg)->lm_flags &= ~MSG_OP_FLAG_MASK;
1327 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1328 ((flags & MSG_GEN_FLAG_MASK) <<MSG_OP_FLAG_SHIFT);
1330 case LUSTRE_MSG_MAGIC_V2: {
1331 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1332 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1333 pb->pb_op_flags |= flags;
1337 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1341 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg)
1343 switch (msg->lm_magic) {
1344 case LUSTRE_MSG_MAGIC_V1:
1345 return &((struct lustre_msg_v1 *)msg)->lm_handle;
1346 case LUSTRE_MSG_MAGIC_V2: {
1347 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1349 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1352 return &pb->pb_handle;
1355 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1360 __u32 lustre_msg_get_type(struct lustre_msg *msg)
1362 switch (msg->lm_magic) {
1363 case LUSTRE_MSG_MAGIC_V1:
1364 return ((struct lustre_msg_v1 *)msg)->lm_type;
1365 case LUSTRE_MSG_MAGIC_V2: {
1366 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1368 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1369 return PTL_RPC_MSG_ERR;
1374 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1375 return PTL_RPC_MSG_ERR;
1379 __u32 lustre_msg_get_version(struct lustre_msg *msg)
1381 switch (msg->lm_magic) {
1382 case LUSTRE_MSG_MAGIC_V1:
1383 return ((struct lustre_msg_v1 *)msg)->lm_version;
1384 case LUSTRE_MSG_MAGIC_V2: {
1385 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1387 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1390 return pb->pb_version;
1393 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1398 void lustre_msg_add_version(struct lustre_msg *msg, int version)
1400 switch (msg->lm_magic) {
1401 case LUSTRE_MSG_MAGIC_V1:
1403 case LUSTRE_MSG_MAGIC_V2: {
1404 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1405 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1406 pb->pb_version |= version;
1410 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1414 __u32 lustre_msg_get_opc(struct lustre_msg *msg)
1416 switch (msg->lm_magic) {
1417 case LUSTRE_MSG_MAGIC_V1:
1418 return ((struct lustre_msg_v1 *)msg)->lm_opc;
1419 case LUSTRE_MSG_MAGIC_V2: {
1420 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1422 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1428 CERROR( "incorrect message magic: %08x\n", msg->lm_magic);
1433 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg)
1435 switch (msg->lm_magic) {
1436 case LUSTRE_MSG_MAGIC_V1:
1437 return ((struct lustre_msg_v1 *)msg)->lm_last_xid;
1438 case LUSTRE_MSG_MAGIC_V2: {
1439 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1441 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1444 return pb->pb_last_xid;
1447 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1452 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg)
1454 switch (msg->lm_magic) {
1455 case LUSTRE_MSG_MAGIC_V1:
1456 return ((struct lustre_msg_v1 *)msg)->lm_last_committed;
1457 case LUSTRE_MSG_MAGIC_V2: {
1458 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1460 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1463 return pb->pb_last_committed;
1466 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1471 __u64 *lustre_msg_get_versions(struct lustre_msg *msg)
1473 switch (msg->lm_magic) {
1474 case LUSTRE_MSG_MAGIC_V1:
1476 case LUSTRE_MSG_MAGIC_V2: {
1477 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1479 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1482 #ifdef PTLRPC_INTEROP_1_6
1483 if (lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF) < sizeof (*pb))
1486 return pb->pb_pre_versions;
1489 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1494 __u64 lustre_msg_get_transno(struct lustre_msg *msg)
1496 switch (msg->lm_magic) {
1497 case LUSTRE_MSG_MAGIC_V1:
1498 return ((struct lustre_msg_v1 *)msg)->lm_transno;
1499 case LUSTRE_MSG_MAGIC_V2: {
1500 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1502 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1505 return pb->pb_transno;
1508 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1513 int lustre_msg_get_status(struct lustre_msg *msg)
1515 switch (msg->lm_magic) {
1516 case LUSTRE_MSG_MAGIC_V1:
1517 return ((struct lustre_msg_v1 *)msg)->lm_status;
1518 case LUSTRE_MSG_MAGIC_V2: {
1519 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1521 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1524 return pb->pb_status;
1527 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1528 /* status might be printed in debug code while message
1534 __u64 lustre_msg_get_slv(struct lustre_msg *msg)
1536 switch (msg->lm_magic) {
1537 case LUSTRE_MSG_MAGIC_V1:
1539 case LUSTRE_MSG_MAGIC_V2: {
1540 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1542 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1548 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1554 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv)
1556 switch (msg->lm_magic) {
1557 case LUSTRE_MSG_MAGIC_V1:
1559 case LUSTRE_MSG_MAGIC_V2: {
1560 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1562 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1569 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1574 __u32 lustre_msg_get_limit(struct lustre_msg *msg)
1576 switch (msg->lm_magic) {
1577 case LUSTRE_MSG_MAGIC_V1:
1579 case LUSTRE_MSG_MAGIC_V2: {
1580 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1582 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1585 return pb->pb_limit;
1588 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1594 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit)
1596 switch (msg->lm_magic) {
1597 case LUSTRE_MSG_MAGIC_V1:
1599 case LUSTRE_MSG_MAGIC_V2: {
1600 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1602 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1605 pb->pb_limit = limit;
1609 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1614 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg)
1616 switch (msg->lm_magic) {
1617 case LUSTRE_MSG_MAGIC_V1:
1618 return ((struct lustre_msg_v1 *)msg)->lm_conn_cnt;
1619 case LUSTRE_MSG_MAGIC_V2: {
1620 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1622 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1625 return pb->pb_conn_cnt;
1628 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1633 int lustre_msg_is_v1(struct lustre_msg *msg)
1635 switch (msg->lm_magic) {
1636 case LUSTRE_MSG_MAGIC_V1:
1637 case LUSTRE_MSG_MAGIC_V1_SWABBED:
1644 __u32 lustre_msg_get_magic(struct lustre_msg *msg)
1646 switch (msg->lm_magic) {
1647 case LUSTRE_MSG_MAGIC_V1:
1648 case LUSTRE_MSG_MAGIC_V1_SWABBED:
1649 case LUSTRE_MSG_MAGIC_V2:
1650 case LUSTRE_MSG_MAGIC_V2_SWABBED:
1651 return msg->lm_magic;
1653 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1658 __u32 lustre_msg_get_timeout(struct lustre_msg *msg)
1660 switch (msg->lm_magic) {
1661 case LUSTRE_MSG_MAGIC_V1:
1663 case LUSTRE_MSG_MAGIC_V2: {
1664 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1666 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1669 return pb->pb_timeout;
1672 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1677 __u32 lustre_msg_get_service_time(struct lustre_msg *msg)
1679 switch (msg->lm_magic) {
1680 case LUSTRE_MSG_MAGIC_V1:
1682 case LUSTRE_MSG_MAGIC_V2: {
1683 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1685 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1688 return pb->pb_service_time;
1691 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1696 __u32 lustre_msg_get_cksum(struct lustre_msg *msg)
1698 switch (msg->lm_magic) {
1699 case LUSTRE_MSG_MAGIC_V1:
1701 case LUSTRE_MSG_MAGIC_V2:
1702 return msg->lm_cksum;
1704 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1710 * the sizeof ptlrpc_body in 1.6 is 88 bytes (64 bytes shorter than current
1711 * size), to be able to interoperate with 1.6 we only calculate checksum
1712 * aginst first 88 bytes of ptlrpc_body.
1714 static const int ptlrpc_body_size_16 = 88;
1716 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg)
1718 switch (msg->lm_magic) {
1719 case LUSTRE_MSG_MAGIC_V1:
1721 case LUSTRE_MSG_MAGIC_V2: {
1722 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1723 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1724 return crc32_le(~(__u32)0, (unsigned char *)pb,
1725 ptlrpc_body_size_16);
1728 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1733 void lustre_msg_set_handle(struct lustre_msg *msg, struct lustre_handle *handle)
1735 switch (msg->lm_magic) {
1736 case LUSTRE_MSG_MAGIC_V1:
1737 ((struct lustre_msg_v1 *)msg)->lm_handle = *handle;
1739 case LUSTRE_MSG_MAGIC_V2: {
1740 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1741 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1742 pb->pb_handle = *handle;
1746 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1750 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type)
1752 switch (msg->lm_magic) {
1753 case LUSTRE_MSG_MAGIC_V1:
1754 ((struct lustre_msg_v1 *)msg)->lm_type = type;
1756 case LUSTRE_MSG_MAGIC_V2: {
1757 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1758 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1763 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1767 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc)
1769 switch (msg->lm_magic) {
1770 case LUSTRE_MSG_MAGIC_V1:
1771 ((struct lustre_msg_v1 *)msg)->lm_opc = opc;
1773 case LUSTRE_MSG_MAGIC_V2: {
1774 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1775 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1780 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1784 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid)
1786 switch (msg->lm_magic) {
1787 case LUSTRE_MSG_MAGIC_V1:
1788 ((struct lustre_msg_v1 *)msg)->lm_last_xid = last_xid;
1790 case LUSTRE_MSG_MAGIC_V2: {
1791 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1792 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1793 pb->pb_last_xid = last_xid;
1797 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1801 void lustre_msg_set_last_committed(struct lustre_msg *msg, __u64 last_committed)
1803 switch (msg->lm_magic) {
1804 case LUSTRE_MSG_MAGIC_V1:
1805 ((struct lustre_msg_v1 *)msg)->lm_last_committed=last_committed;
1807 case LUSTRE_MSG_MAGIC_V2: {
1808 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1809 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1810 pb->pb_last_committed = last_committed;
1814 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1818 void lustre_msg_set_versions(struct lustre_msg *msg, __u64 *versions)
1820 switch (msg->lm_magic) {
1821 case LUSTRE_MSG_MAGIC_V1:
1823 case LUSTRE_MSG_MAGIC_V2: {
1824 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1825 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1826 #ifdef PTLRPC_INTEROP_1_6
1827 /* do nothing for old clients */
1828 if (lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF) < sizeof (*pb))
1831 pb->pb_pre_versions[0] = versions[0];
1832 pb->pb_pre_versions[1] = versions[1];
1833 pb->pb_pre_versions[2] = versions[2];
1834 pb->pb_pre_versions[3] = versions[3];
1838 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1843 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno)
1845 switch (msg->lm_magic) {
1846 case LUSTRE_MSG_MAGIC_V1:
1847 ((struct lustre_msg_v1 *)msg)->lm_transno = transno;
1849 case LUSTRE_MSG_MAGIC_V2: {
1850 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1851 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1852 pb->pb_transno = transno;
1856 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1860 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status)
1862 switch (msg->lm_magic) {
1863 case LUSTRE_MSG_MAGIC_V1:
1864 ((struct lustre_msg_v1 *)msg)->lm_status = status;
1866 case LUSTRE_MSG_MAGIC_V2: {
1867 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1868 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1869 pb->pb_status = status;
1873 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1877 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt)
1879 switch (msg->lm_magic) {
1880 case LUSTRE_MSG_MAGIC_V1:
1881 ((struct lustre_msg_v1 *)msg)->lm_conn_cnt = conn_cnt;
1883 case LUSTRE_MSG_MAGIC_V2: {
1884 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1885 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1886 pb->pb_conn_cnt = conn_cnt;
1890 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1894 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout)
1896 switch (msg->lm_magic) {
1897 case LUSTRE_MSG_MAGIC_V1:
1899 case LUSTRE_MSG_MAGIC_V2: {
1900 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1901 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1902 pb->pb_timeout = timeout;
1906 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1910 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time)
1912 switch (msg->lm_magic) {
1913 case LUSTRE_MSG_MAGIC_V1:
1915 case LUSTRE_MSG_MAGIC_V2: {
1916 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1917 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1918 pb->pb_service_time = service_time;
1922 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1926 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum)
1928 switch (msg->lm_magic) {
1929 case LUSTRE_MSG_MAGIC_V1:
1931 case LUSTRE_MSG_MAGIC_V2:
1932 msg->lm_cksum = cksum;
1935 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1940 /* byte flipping routines for all wire types declared in
1941 * lustre_idl.h implemented here.
1943 void lustre_swab_ptlrpc_body(struct ptlrpc_body *b, int msgsize)
1945 __swab32s (&b->pb_type);
1946 __swab32s (&b->pb_version);
1947 __swab32s (&b->pb_opc);
1948 __swab32s (&b->pb_status);
1949 __swab64s (&b->pb_last_xid);
1950 __swab64s (&b->pb_last_seen);
1951 __swab64s (&b->pb_last_committed);
1952 __swab64s (&b->pb_transno);
1953 __swab32s (&b->pb_flags);
1954 __swab32s (&b->pb_op_flags);
1955 __swab32s (&b->pb_conn_cnt);
1956 __swab32s (&b->pb_timeout);
1957 __swab32s (&b->pb_service_time);
1958 __swab64s (&b->pb_slv);
1959 __swab32s (&b->pb_limit);
1960 if (msgsize < offsetof(struct ptlrpc_body, pb_pre_versions[4]))
1962 __swab64s (&b->pb_pre_versions[0]);
1963 __swab64s (&b->pb_pre_versions[1]);
1964 __swab64s (&b->pb_pre_versions[2]);
1965 __swab64s (&b->pb_pre_versions[3]);
1966 CLASSERT(offsetof(typeof(*b), pb_padding) != 0);
1969 void lustre_swab_connect(struct obd_connect_data *ocd)
1971 __swab64s(&ocd->ocd_connect_flags);
1972 __swab32s(&ocd->ocd_version);
1973 __swab32s(&ocd->ocd_grant);
1974 __swab32s(&ocd->ocd_index);
1975 __swab32s(&ocd->ocd_brw_size);
1976 __swab64s(&ocd->ocd_ibits_known);
1977 __swab32s(&ocd->ocd_nllu);
1978 __swab32s(&ocd->ocd_nllg);
1979 __swab64s(&ocd->ocd_transno);
1980 __swab32s(&ocd->ocd_group);
1981 __swab32s(&ocd->ocd_cksum_types);
1982 CLASSERT(offsetof(typeof(*ocd), padding1) != 0);
1983 CLASSERT(offsetof(typeof(*ocd), padding2) != 0);
1986 void lustre_swab_obdo (struct obdo *o)
1988 __swab64s (&o->o_valid);
1989 __swab64s (&o->o_id);
1990 __swab64s (&o->o_gr);
1991 __swab64s (&o->o_fid);
1992 __swab64s (&o->o_size);
1993 __swab64s (&o->o_mtime);
1994 __swab64s (&o->o_atime);
1995 __swab64s (&o->o_ctime);
1996 __swab64s (&o->o_blocks);
1997 __swab64s (&o->o_grant);
1998 __swab32s (&o->o_blksize);
1999 __swab32s (&o->o_mode);
2000 __swab32s (&o->o_uid);
2001 __swab32s (&o->o_gid);
2002 __swab32s (&o->o_flags);
2003 __swab32s (&o->o_nlink);
2004 __swab32s (&o->o_generation);
2005 __swab32s (&o->o_misc);
2006 __swab32s (&o->o_easize);
2007 __swab32s (&o->o_mds);
2008 __swab32s (&o->o_stripe_idx);
2009 __swab32s (&o->o_padding_1);
2010 /* o_inline is opaque */
2013 void lustre_swab_obd_statfs (struct obd_statfs *os)
2015 __swab64s (&os->os_type);
2016 __swab64s (&os->os_blocks);
2017 __swab64s (&os->os_bfree);
2018 __swab64s (&os->os_bavail);
2019 __swab64s (&os->os_files);
2020 __swab64s (&os->os_ffree);
2021 /* no need to swab os_fsid */
2022 __swab32s (&os->os_bsize);
2023 __swab32s (&os->os_namelen);
2024 __swab64s (&os->os_maxbytes);
2025 __swab32s (&os->os_state);
2026 /* no need to swap os_spare */
2029 void lustre_swab_obd_ioobj (struct obd_ioobj *ioo)
2031 __swab64s (&ioo->ioo_id);
2032 __swab64s (&ioo->ioo_gr);
2033 __swab32s (&ioo->ioo_type);
2034 __swab32s (&ioo->ioo_bufcnt);
2037 void lustre_swab_niobuf_remote (struct niobuf_remote *nbr)
2039 __swab64s (&nbr->offset);
2040 __swab32s (&nbr->len);
2041 __swab32s (&nbr->flags);
2044 void lustre_swab_ost_body (struct ost_body *b)
2046 lustre_swab_obdo (&b->oa);
2049 void lustre_swab_ost_last_id(obd_id *id)
2054 void lustre_swab_ost_lvb(struct ost_lvb *lvb)
2056 __swab64s(&lvb->lvb_size);
2057 __swab64s(&lvb->lvb_mtime);
2058 __swab64s(&lvb->lvb_atime);
2059 __swab64s(&lvb->lvb_ctime);
2060 __swab64s(&lvb->lvb_blocks);
2063 void lustre_swab_mds_status_req (struct mds_status_req *r)
2065 __swab32s (&r->flags);
2066 __swab32s (&r->repbuf);
2069 void lustre_swab_mds_body (struct mds_body *b)
2071 lustre_swab_ll_fid (&b->fid1);
2072 lustre_swab_ll_fid (&b->fid2);
2073 /* handle is opaque */
2074 __swab64s (&b->valid);
2075 __swab64s (&b->size);
2076 __swab64s (&b->mtime);
2077 __swab64s (&b->atime);
2078 __swab64s (&b->ctime);
2079 __swab64s (&b->blocks);
2080 __swab64s (&b->io_epoch);
2081 __swab64s (&b->ino);
2082 __swab32s (&b->fsuid);
2083 __swab32s (&b->fsgid);
2084 __swab32s (&b->capability);
2085 __swab32s (&b->mode);
2086 __swab32s (&b->uid);
2087 __swab32s (&b->gid);
2088 __swab32s (&b->flags);
2089 __swab32s (&b->rdev);
2090 __swab32s (&b->nlink);
2091 __swab32s (&b->generation);
2092 __swab32s (&b->suppgid);
2093 __swab32s (&b->eadatasize);
2094 __swab32s (&b->aclsize);
2095 __swab32s (&b->max_mdsize);
2096 __swab32s (&b->max_cookiesize);
2097 __swab32s (&b->padding_4);
2100 void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
2103 __swab32s(&mti->mti_lustre_ver);
2104 __swab32s(&mti->mti_stripe_index);
2105 __swab32s(&mti->mti_config_ver);
2106 __swab32s(&mti->mti_flags);
2107 __swab32s(&mti->mti_nid_count);
2108 CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
2109 for (i = 0; i < MTI_NIDS_MAX; i++)
2110 __swab64s(&mti->mti_nids[i]);
2113 static void lustre_swab_obd_dqinfo (struct obd_dqinfo *i)
2115 __swab64s (&i->dqi_bgrace);
2116 __swab64s (&i->dqi_igrace);
2117 __swab32s (&i->dqi_flags);
2118 __swab32s (&i->dqi_valid);
2121 static void lustre_swab_obd_dqblk (struct obd_dqblk *b)
2123 __swab64s (&b->dqb_ihardlimit);
2124 __swab64s (&b->dqb_isoftlimit);
2125 __swab64s (&b->dqb_curinodes);
2126 __swab64s (&b->dqb_bhardlimit);
2127 __swab64s (&b->dqb_bsoftlimit);
2128 __swab64s (&b->dqb_curspace);
2129 __swab64s (&b->dqb_btime);
2130 __swab64s (&b->dqb_itime);
2131 __swab32s (&b->dqb_valid);
2132 CLASSERT(offsetof(typeof(*b), padding) != 0);
2135 void lustre_swab_obd_quotactl (struct obd_quotactl *q)
2137 __swab32s (&q->qc_cmd);
2138 __swab32s (&q->qc_type);
2139 __swab32s (&q->qc_id);
2140 __swab32s (&q->qc_stat);
2141 lustre_swab_obd_dqinfo (&q->qc_dqinfo);
2142 lustre_swab_obd_dqblk (&q->qc_dqblk);
2145 void lustre_swab_quota_adjust_qunit (struct quota_adjust_qunit *q)
2147 __swab32s (&q->qaq_flags);
2148 __swab32s (&q->qaq_id);
2149 __swab64s (&q->qaq_bunit_sz);
2150 __swab64s (&q->qaq_iunit_sz);
2153 void lustre_swab_mds_rec_setattr (struct mds_rec_setattr *sa)
2155 __swab32s (&sa->sa_opcode);
2156 __swab32s (&sa->sa_fsuid);
2157 __swab32s (&sa->sa_fsgid);
2158 __swab32s (&sa->sa_cap);
2159 __swab32s (&sa->sa_suppgid);
2160 __swab32s (&sa->sa_mode);
2161 lustre_swab_ll_fid (&sa->sa_fid);
2162 __swab64s (&sa->sa_valid);
2163 __swab64s (&sa->sa_size);
2164 __swab64s (&sa->sa_mtime);
2165 __swab64s (&sa->sa_atime);
2166 __swab64s (&sa->sa_ctime);
2167 __swab32s (&sa->sa_uid);
2168 __swab32s (&sa->sa_gid);
2169 __swab32s (&sa->sa_attr_flags);
2170 CLASSERT(offsetof(typeof(*sa), sa_padding) != 0);
2173 void lustre_swab_mds_rec_join (struct mds_rec_join *jr)
2175 __swab64s(&jr->jr_headsize);
2176 lustre_swab_ll_fid(&jr->jr_fid);
2179 void lustre_swab_mds_rec_create (struct mds_rec_create *cr)
2181 __swab32s (&cr->cr_opcode);
2182 __swab32s (&cr->cr_fsuid);
2183 __swab32s (&cr->cr_fsgid);
2184 __swab32s (&cr->cr_cap);
2185 __swab32s (&cr->cr_flags); /* for use with open */
2186 __swab32s (&cr->cr_mode);
2187 lustre_swab_ll_fid (&cr->cr_fid);
2188 lustre_swab_ll_fid (&cr->cr_replayfid);
2189 __swab64s (&cr->cr_time);
2190 __swab64s (&cr->cr_rdev);
2191 __swab32s (&cr->cr_suppgid);
2192 CLASSERT(offsetof(typeof(*cr), cr_padding_1) != 0);
2193 CLASSERT(offsetof(typeof(*cr), cr_padding_2) != 0);
2194 CLASSERT(offsetof(typeof(*cr), cr_padding_3) != 0);
2195 CLASSERT(offsetof(typeof(*cr), cr_padding_4) != 0);
2196 CLASSERT(offsetof(typeof(*cr), cr_padding_5) != 0);
2199 void lustre_swab_mds_rec_link (struct mds_rec_link *lk)
2201 __swab32s (&lk->lk_opcode);
2202 __swab32s (&lk->lk_fsuid);
2203 __swab32s (&lk->lk_fsgid);
2204 __swab32s (&lk->lk_cap);
2205 __swab32s (&lk->lk_suppgid1);
2206 __swab32s (&lk->lk_suppgid2);
2207 lustre_swab_ll_fid (&lk->lk_fid1);
2208 lustre_swab_ll_fid (&lk->lk_fid2);
2209 __swab64s (&lk->lk_time);
2210 CLASSERT(offsetof(typeof(*lk), lk_padding_1) != 0);
2211 CLASSERT(offsetof(typeof(*lk), lk_padding_2) != 0);
2212 CLASSERT(offsetof(typeof(*lk), lk_padding_3) != 0);
2213 CLASSERT(offsetof(typeof(*lk), lk_padding_4) != 0);
2216 void lustre_swab_mds_rec_unlink (struct mds_rec_unlink *ul)
2218 __swab32s (&ul->ul_opcode);
2219 __swab32s (&ul->ul_fsuid);
2220 __swab32s (&ul->ul_fsgid);
2221 __swab32s (&ul->ul_cap);
2222 __swab32s (&ul->ul_suppgid);
2223 __swab32s (&ul->ul_mode);
2224 lustre_swab_ll_fid (&ul->ul_fid1);
2225 lustre_swab_ll_fid (&ul->ul_fid2);
2226 __swab64s (&ul->ul_time);
2227 CLASSERT(offsetof(typeof(*ul), ul_padding_1) != 0);
2228 CLASSERT(offsetof(typeof(*ul), ul_padding_2) != 0);
2229 CLASSERT(offsetof(typeof(*ul), ul_padding_3) != 0);
2230 CLASSERT(offsetof(typeof(*ul), ul_padding_4) != 0);
2233 void lustre_swab_fiemap_extent(struct ll_fiemap_extent *fm_extent)
2235 __swab64s(&fm_extent->fe_logical);
2236 __swab64s(&fm_extent->fe_physical);
2237 __swab64s(&fm_extent->fe_length);
2238 __swab32s(&fm_extent->fe_flags);
2239 __swab32s(&fm_extent->fe_device);
2242 void lustre_swab_fiemap(struct ll_user_fiemap *fiemap)
2246 __swab64s(&fiemap->fm_start);
2247 __swab64s(&fiemap->fm_length);
2248 __swab32s(&fiemap->fm_flags);
2249 __swab32s(&fiemap->fm_mapped_extents);
2250 __swab32s(&fiemap->fm_extent_count);
2251 __swab32s(&fiemap->fm_reserved);
2253 for (i = 0; i < fiemap->fm_mapped_extents; i++)
2254 lustre_swab_fiemap_extent(&fiemap->fm_extents[i]);
2257 void lustre_swab_mds_rec_rename (struct mds_rec_rename *rn)
2259 __swab32s (&rn->rn_opcode);
2260 __swab32s (&rn->rn_fsuid);
2261 __swab32s (&rn->rn_fsgid);
2262 __swab32s (&rn->rn_cap);
2263 __swab32s (&rn->rn_suppgid1);
2264 __swab32s (&rn->rn_suppgid2);
2265 lustre_swab_ll_fid (&rn->rn_fid1);
2266 lustre_swab_ll_fid (&rn->rn_fid2);
2267 __swab64s (&rn->rn_time);
2268 CLASSERT(offsetof(typeof(*rn), rn_padding_1) != 0);
2269 CLASSERT(offsetof(typeof(*rn), rn_padding_2) != 0);
2270 CLASSERT(offsetof(typeof(*rn), rn_padding_3) != 0);
2271 CLASSERT(offsetof(typeof(*rn), rn_padding_4) != 0);
2274 void lustre_swab_lov_desc (struct lov_desc *ld)
2276 __swab32s (&ld->ld_tgt_count);
2277 __swab32s (&ld->ld_active_tgt_count);
2278 __swab32s (&ld->ld_default_stripe_count);
2279 __swab32s (&ld->ld_pattern);
2280 __swab64s (&ld->ld_default_stripe_size);
2281 __swab64s (&ld->ld_default_stripe_offset);
2282 __swab32s (&ld->ld_qos_maxage);
2283 /* uuid endian insensitive */
2287 static void lustre_swab_lov_user_md_common(struct lov_user_md_v1 *lum)
2290 __swab32s(&lum->lmm_magic);
2291 __swab32s(&lum->lmm_pattern);
2292 __swab64s(&lum->lmm_object_id);
2293 __swab64s(&lum->lmm_object_gr);
2294 __swab32s(&lum->lmm_stripe_size);
2295 __swab16s(&lum->lmm_stripe_count);
2296 __swab16s(&lum->lmm_stripe_offset);
2300 static void lustre_swab_lov_user_md_v1(struct lov_user_md_v1 *lum)
2303 CDEBUG(D_IOCTL, "swabbing lov_user_md v1\n");
2304 lustre_swab_lov_user_md_common(lum);
2308 static void lustre_swab_lov_user_md_v3(struct lov_user_md_v3 *lum)
2311 CDEBUG(D_IOCTL, "swabbing lov_user_md v3\n");
2312 lustre_swab_lov_user_md_common((struct lov_user_md_v1 *)lum);
2313 /* lmm_pool_name nothing to do with char */
2317 int lustre_swab_lov_user_md(struct lov_user_md_v1 *lum)
2320 switch (lum->lmm_magic) {
2321 case LOV_USER_MAGIC_V1:
2322 case LOV_USER_MAGIC_V1_SWABBED:
2323 lustre_swab_lov_user_md_v1(lum);
2325 case LOV_USER_MAGIC_V3:
2326 case LOV_USER_MAGIC_V3_SWABBED:
2327 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lum);
2330 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
2331 " %#08x != %#08x nor %#08x\n",
2332 lum->lmm_magic, LOV_USER_MAGIC_V1,
2339 void lustre_swab_lov_mds_md(struct lov_mds_md *lmm)
2342 CDEBUG(D_IOCTL, "swabbing lov_mds_md\n");
2343 __swab32s(&lmm->lmm_magic);
2344 __swab32s(&lmm->lmm_pattern);
2345 __swab64s(&lmm->lmm_object_id);
2346 __swab64s(&lmm->lmm_object_gr);
2347 __swab32s(&lmm->lmm_stripe_size);
2348 __swab32s(&lmm->lmm_stripe_count);
2352 void lustre_swab_lov_user_md_join(struct lov_user_md_join *lumj)
2355 CDEBUG(D_IOCTL, "swabbing lov_user_md_join\n");
2356 __swab32s(&lumj->lmm_magic);
2357 __swab32s(&lumj->lmm_pattern);
2358 __swab64s(&lumj->lmm_object_id);
2359 __swab64s(&lumj->lmm_object_gr);
2360 __swab32s(&lumj->lmm_stripe_size);
2361 __swab32s(&lumj->lmm_stripe_count);
2362 __swab32s(&lumj->lmm_extent_count);
2366 int lustre_swab_lov_user_md_objects(struct lov_user_md *lum)
2369 __u16 stripe_count = lum->lmm_stripe_count;
2370 struct lov_user_ost_data *lod;
2372 switch (lum->lmm_magic) {
2373 case LOV_USER_MAGIC_V1_SWABBED:
2374 __swab16s(&stripe_count);
2375 case LOV_USER_MAGIC_V1:
2376 lod = lum->lmm_objects;
2378 case LOV_USER_MAGIC_V3_SWABBED:
2379 __swab16s(&stripe_count);
2380 case LOV_USER_MAGIC_V3:
2381 lod = ((struct lov_user_md_v3 *)lum)->lmm_objects;
2384 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
2385 " %#08x != %#08x nor %#08x\n",
2386 lum->lmm_magic, LOV_USER_MAGIC_V1,
2390 for (i = 0; i < stripe_count; i++) {
2391 __swab64s(&(lod[i].l_object_id));
2392 __swab64s(&(lod[i].l_object_gr));
2393 __swab32s(&(lod[i].l_ost_gen));
2394 __swab32s(&(lod[i].l_ost_idx));
2399 void lustre_swab_ldlm_res_id (struct ldlm_res_id *id)
2403 for (i = 0; i < RES_NAME_SIZE; i++)
2404 __swab64s (&id->name[i]);
2407 void lustre_swab_ldlm_policy_data (ldlm_policy_data_t *d)
2409 /* the lock data is a union and the first two fields are always an
2410 * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
2411 * data the same way. */
2412 __swab64s(&d->l_extent.start);
2413 __swab64s(&d->l_extent.end);
2414 __swab64s(&d->l_extent.gid);
2415 __swab32s(&d->l_flock.pid);
2418 void lustre_swab_ldlm_intent (struct ldlm_intent *i)
2420 __swab64s (&i->opc);
2423 void lustre_swab_ldlm_resource_desc (struct ldlm_resource_desc *r)
2425 __swab32s (&r->lr_type);
2426 CLASSERT(offsetof(typeof(*r), lr_padding) != 0);
2427 lustre_swab_ldlm_res_id (&r->lr_name);
2430 void lustre_swab_ldlm_lock_desc (struct ldlm_lock_desc *l)
2432 lustre_swab_ldlm_resource_desc (&l->l_resource);
2433 __swab32s (&l->l_req_mode);
2434 __swab32s (&l->l_granted_mode);
2435 lustre_swab_ldlm_policy_data (&l->l_policy_data);
2438 void lustre_swab_ldlm_request (struct ldlm_request *rq)
2440 __swab32s (&rq->lock_flags);
2441 lustre_swab_ldlm_lock_desc (&rq->lock_desc);
2442 __swab32s (&rq->lock_count);
2443 /* lock_handle[] opaque */
2446 void lustre_swab_ldlm_reply (struct ldlm_reply *r)
2448 __swab32s (&r->lock_flags);
2449 CLASSERT(offsetof(typeof(*r), lock_padding) != 0);
2450 lustre_swab_ldlm_lock_desc (&r->lock_desc);
2451 /* lock_handle opaque */
2452 __swab64s (&r->lock_policy_res1);
2453 __swab64s (&r->lock_policy_res2);
2456 /* no one calls this */
2457 int llog_log_swabbed(struct llog_log_hdr *hdr)
2459 if (hdr->llh_hdr.lrh_type == __swab32(LLOG_HDR_MAGIC))
2461 if (hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC)
2466 void lustre_swab_qdata(struct qunit_data *d)
2468 __swab32s (&d->qd_id);
2469 __swab32s (&d->qd_flags);
2470 __swab64s (&d->qd_count);
2471 __swab64s (&d->qd_qunit);
2474 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2475 void lustre_swab_qdata_old2(struct qunit_data_old2 *d)
2477 __swab32s (&d->qd_id);
2478 __swab32s (&d->qd_flags);
2479 __swab64s (&d->qd_count);
2482 #warning "remove quota code above for format absolete in new release"
2487 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2488 void qdata_v2_to_v3(struct qunit_data_old2 *d,
2489 struct qunit_data *qdata)
2494 qdata->qd_id = d->qd_id;
2495 qdata->qd_flags = d->qd_flags;
2496 qdata->qd_count = d->qd_count;
2499 struct qunit_data_old2 *qdata_v3_to_v2(struct qunit_data *d)
2501 struct qunit_data tmp;
2502 struct qunit_data_old2 *ret;
2509 ret = (struct qunit_data_old2 *)d;
2510 ret->qd_id = tmp.qd_id;
2511 ret->qd_flags = tmp.qd_flags & LQUOTA_QUNIT_FLAGS;
2512 ret->qd_count = tmp.qd_count;
2516 #warning "remove quota code above for format absolete in new release"
2519 /* got qdata from request(req/rep) */
2520 int quota_get_qdata(void *request, struct qunit_data *qdata,
2521 int is_req, int is_exp)
2523 struct ptlrpc_request *req = (struct ptlrpc_request *)request;
2524 struct qunit_data *new;
2525 struct qunit_data_old2 *old2;
2526 int size2 = sizeof(struct qunit_data_old2);
2527 __u64 flags = is_exp ? req->rq_export->exp_connect_flags :
2528 req->rq_import->imp_connect_data.ocd_connect_flags;
2534 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2535 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS))
2536 goto without_change_qs;
2538 #warning "remove quota code above for format absolete in new release"
2541 /* support for quota64 and change_qs */
2542 if (flags & OBD_CONNECT_CHANGE_QS) {
2543 if (!(flags & OBD_CONNECT_QUOTA64)) {
2544 CDEBUG(D_ERROR, "Wire protocol for qunit is broken!\n");
2547 if (is_req == QUOTA_REQUEST)
2548 new = lustre_swab_reqbuf(req, REQ_REC_OFF,
2549 sizeof(struct qunit_data),
2552 new = lustre_swab_repbuf(req, REPLY_REC_OFF,
2553 sizeof(struct qunit_data),
2556 GOTO(out, rc = -EPROTO);
2558 QDATA_SET_CHANGE_QS(qdata);
2561 QDATA_CLR_CHANGE_QS(qdata);
2564 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2566 /* only support for quota64 */
2567 if (flags & OBD_CONNECT_QUOTA64) {
2569 if (is_req == QUOTA_REQUEST)
2570 old2 = lustre_swab_reqbuf(req, REQ_REC_OFF, size2,
2571 lustre_swab_qdata_old2);
2573 old2 = lustre_swab_repbuf(req, REPLY_REC_OFF, size2,
2574 lustre_swab_qdata_old2);
2576 GOTO(out, rc = -EPROTO);
2577 qdata_v2_to_v3(old2, qdata);
2582 #warning "remove quota code above for format absolete in new release"
2587 EXPORT_SYMBOL(quota_get_qdata);
2589 /* copy qdata to request(req/rep) */
2590 int quota_copy_qdata(void *request, struct qunit_data *qdata,
2591 int is_req, int is_exp)
2593 struct ptlrpc_request *req = (struct ptlrpc_request *)request;
2595 struct qunit_data_old2 *old2;
2596 __u64 flags = is_exp ? req->rq_export->exp_connect_flags :
2597 req->rq_import->imp_connect_data.ocd_connect_flags;
2603 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2604 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS))
2605 goto without_change_qs;
2607 #warning "remove quota code above for format absolete in new release"
2610 /* support for quota64 and change_qs */
2611 if (flags & OBD_CONNECT_CHANGE_QS) {
2612 if (!(flags & OBD_CONNECT_QUOTA64)) {
2613 CERROR("Wire protocol for qunit is broken!\n");
2616 if (is_req == QUOTA_REQUEST)
2617 target = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
2618 sizeof(struct qunit_data));
2620 target = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
2621 sizeof(struct qunit_data));
2623 GOTO(out, rc = -EPROTO);
2624 memcpy(target, qdata, sizeof(*qdata));
2628 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2630 /* only support for quota64 */
2631 if (flags & OBD_CONNECT_QUOTA64) {
2632 if (is_req == QUOTA_REQUEST)
2633 target = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
2634 sizeof(struct qunit_data_old2));
2636 target = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
2637 sizeof(struct qunit_data_old2));
2639 GOTO(out, rc = -EPROTO);
2640 old2 = qdata_v3_to_v2(qdata);
2641 memcpy(target, old2, sizeof(*old2));
2645 #warning "remove quota code above for format absolete in new release"
2650 EXPORT_SYMBOL(quota_copy_qdata);
2652 int quota_get_qunit_data_size(__u64 flag)
2656 if (flag & OBD_CONNECT_CHANGE_QS)
2657 size = sizeof(struct qunit_data);
2659 size = sizeof(struct qunit_data_old2);
2663 EXPORT_SYMBOL(quota_get_qunit_data_size);
2664 #endif /* __KERNEL__ */
2666 static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2668 LASSERT(req->rq_reqmsg);
2670 switch (req->rq_reqmsg->lm_magic) {
2671 case LUSTRE_MSG_MAGIC_V1:
2673 case LUSTRE_MSG_MAGIC_V2:
2674 return lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
2676 CERROR("incorrect message magic: %08x\n",
2677 req->rq_reqmsg->lm_magic);
2682 static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2684 LASSERT(req->rq_repmsg);
2686 switch (req->rq_repmsg->lm_magic) {
2687 case LUSTRE_MSG_MAGIC_V1:
2689 case LUSTRE_MSG_MAGIC_V2:
2690 return lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
2692 CERROR("incorrect message magic: %08x\n",
2693 req->rq_repmsg->lm_magic);
2698 void _debug_req(struct ptlrpc_request *req, __u32 mask,
2699 struct libcfs_debug_msg_data *data, const char *fmt, ... )
2708 /* Caller is responsible holding a reference on the request */
2709 LASSERT(req && atomic_read(&req->rq_refcount) > 0);
2711 if (req->rq_reqmsg &&
2712 (!lustre_msg_need_swab(req->rq_reqmsg) ||
2713 (lustre_req_need_swab(req) &&
2714 lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF)))) {
2715 opc = lustre_msg_get_opc(req->rq_reqmsg);
2716 req_fl = lustre_msg_get_flags(req->rq_reqmsg);
2719 if (req->rq_repmsg &&
2720 (!lustre_msg_need_swab(req->rq_repmsg) ||
2721 (lustre_rep_need_swab(req) &&
2722 lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF)))) {
2723 rep_fl = lustre_msg_get_flags(req->rq_repmsg);
2724 rep_status = lustre_msg_get_status(req->rq_repmsg);
2727 va_start(args, fmt);
2728 libcfs_debug_vmsg2(data->msg_cdls, data->msg_subsys, mask,
2729 data->msg_file, data->msg_fn, data->msg_line, fmt, args,
2730 " req@%p x"LPU64"/t"LPD64" o%d->%s@%s:%d/%d lens %d/%d e %d "
2731 "to %d dl %ld ref %d fl "REQ_FLAGS_FMT"/%x/%x rc %d/%d\n",
2732 req, req->rq_xid, req->rq_transno, opc,
2733 req->rq_import ? obd2cli_tgt(req->rq_import->imp_obd) :
2735 (char*)req->rq_export->exp_client_uuid.uuid : "<?>",
2737 (char *)req->rq_import->imp_connection->c_remote_uuid.uuid :
2739 (char *)req->rq_export->exp_connection->c_remote_uuid.uuid :
2740 "<?>", req->rq_request_portal, req->rq_reply_portal,
2741 req->rq_reqlen, req->rq_replen,
2742 req->rq_early_count, !!req->rq_timeout, req->rq_deadline,
2743 atomic_read(&req->rq_refcount), DEBUG_REQ_FLAGS(req),
2744 req_fl, rep_fl, req->rq_status, rep_status);
2749 EXPORT_SYMBOL(_debug_req);