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,0,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]);
159 /* This returns the size of the buffer that is required to hold a lustre_msg
160 * with the given sub-buffer lengths.
161 * NOTE: this should only be used for NEW requests, and should always be
162 * in the form of a v2 request. If this is a connection to a v1
163 * target then the first buffer will be stripped because the ptlrpc
164 * data is part of the lustre_msg_v1 header. b=14043 */
165 int lustre_msg_size(__u32 magic, int count, __u32 *lens)
167 __u32 size[] = { sizeof(struct ptlrpc_body) };
175 #ifdef PTLRPC_INTEROP_1_6
176 LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body) ||
177 lens[MSG_PTLRPC_BODY_OFF] == PTLRPC_BODY_MIN_SIZE);
179 LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
182 case LUSTRE_MSG_MAGIC_V1:
183 return lustre_msg_size_v1(count - 1, lens + 1);
184 case LUSTRE_MSG_MAGIC_V2:
185 return lustre_msg_size_v2(count, lens);
187 CERROR("incorrect message magic: %08x\n", magic);
192 /* This is used to determine the size of a buffer that was already packed
193 * and will correctly handle the different message formats. */
194 int lustre_packed_msg_size(struct lustre_msg *msg)
196 switch (msg->lm_magic) {
197 case LUSTRE_MSG_MAGIC_V1: {
198 struct lustre_msg_v1 *v1_msg = (struct lustre_msg_v1 *)msg;
199 return lustre_msg_size_v1(v1_msg->lm_bufcount,
202 case LUSTRE_MSG_MAGIC_V2:
203 return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
205 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
211 lustre_init_msg_v1(void *m, int count, __u32 *lens, char **bufs)
213 struct lustre_msg_v1 *msg = (struct lustre_msg_v1 *)m;
218 msg->lm_magic = LUSTRE_MSG_MAGIC_V1;
219 msg->lm_version = PTLRPC_MSG_VERSION;
220 msg->lm_bufcount = count;
222 for (i = 0; i < count; i++)
223 msg->lm_buflens[i] = lens[i];
228 ptr = (char *)msg + lustre_msg_hdr_size_v1(count);
229 for (i = 0; i < count; i++) {
231 LOGL(tmp, lens[i], ptr);
236 lustre_init_msg_v2(struct lustre_msg_v2 *msg, int count, __u32 *lens,
242 msg->lm_bufcount = count;
243 /* XXX: lm_secflvr uninitialized here */
244 msg->lm_magic = LUSTRE_MSG_MAGIC_V2;
246 for (i = 0; i < count; i++)
247 msg->lm_buflens[i] = lens[i];
252 ptr = (char *)msg + lustre_msg_hdr_size_v2(count);
253 for (i = 0; i < count; i++) {
255 LOGL(tmp, lens[i], ptr);
259 static int lustre_pack_request_v1(struct ptlrpc_request *req,
260 int count, __u32 *lens, char **bufs)
264 reqlen = lustre_msg_size_v1(count, lens);
266 /* See if we got it from prealloc pool */
267 if (req->rq_reqmsg) {
268 /* Cannot return error here, that would create
269 infinite loop in ptlrpc_prep_req_pool */
270 /* In this case ptlrpc_prep_req_from_pool sets req->rq_reqlen
271 to maximum size that would fit into this preallocated
273 LASSERTF(req->rq_reqlen >= reqlen, "req->rq_reqlen %d, "
274 "reqlen %d\n",req->rq_reqlen,
276 memset(req->rq_reqmsg, 0, reqlen);
278 OBD_ALLOC(req->rq_reqmsg, reqlen);
279 if (req->rq_reqmsg == NULL) {
280 CERROR("alloc reqmsg (len %d) failed\n", reqlen);
285 req->rq_reqlen = reqlen;
287 lustre_init_msg_v1(req->rq_reqmsg, count, lens, bufs);
291 static int lustre_pack_request_v2(struct ptlrpc_request *req,
292 int count, __u32 *lens, char **bufs)
296 reqlen = lustre_msg_size_v2(count, lens);
298 /* See if we got it from prealloc pool */
299 if (req->rq_reqmsg) {
300 /* Cannot return error here, that would create
301 infinite loop in ptlrpc_prep_req_pool */
302 /* In this case ptlrpc_prep_req_from_pool sets req->rq_reqlen
303 to maximum size that would fit into this preallocated
305 LASSERTF(req->rq_reqlen >= reqlen, "req->rq_reqlen %d, "
306 "reqlen %d\n",req->rq_reqlen,
308 memset(req->rq_reqmsg, 0, reqlen);
310 OBD_ALLOC(req->rq_reqmsg, reqlen);
311 if (req->rq_reqmsg == NULL) {
312 CERROR("alloc reqmsg (len %d) failed\n", reqlen);
317 req->rq_reqlen = reqlen;
319 lustre_init_msg_v2(req->rq_reqmsg, count, lens, bufs);
320 lustre_msg_add_version(req->rq_reqmsg, PTLRPC_MSG_VERSION);
321 lustre_set_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
326 int lustre_pack_request(struct ptlrpc_request *req, __u32 magic, int count,
327 __u32 *lens, char **bufs)
329 __u32 size[] = { sizeof(struct ptlrpc_body) };
337 LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
340 case LUSTRE_MSG_MAGIC_V1:
341 return lustre_pack_request_v1(req, count - 1, lens + 1,
342 bufs ? bufs + 1 : NULL);
343 case LUSTRE_MSG_MAGIC_V2:
344 return lustre_pack_request_v2(req, count, lens, bufs);
346 LASSERTF(0, "incorrect message magic: %08x\n", magic);
352 CFS_LIST_HEAD(ptlrpc_rs_debug_lru);
353 spinlock_t ptlrpc_rs_debug_lock;
355 #define PTLRPC_RS_DEBUG_LRU_ADD(rs) \
357 spin_lock(&ptlrpc_rs_debug_lock); \
358 list_add_tail(&(rs)->rs_debug_list, &ptlrpc_rs_debug_lru); \
359 spin_unlock(&ptlrpc_rs_debug_lock); \
362 #define PTLRPC_RS_DEBUG_LRU_DEL(rs) \
364 spin_lock(&ptlrpc_rs_debug_lock); \
365 list_del(&(rs)->rs_debug_list); \
366 spin_unlock(&ptlrpc_rs_debug_lock); \
369 # define PTLRPC_RS_DEBUG_LRU_ADD(rs) do {} while(0)
370 # define PTLRPC_RS_DEBUG_LRU_DEL(rs) do {} while(0)
373 static struct ptlrpc_reply_state *lustre_get_emerg_rs(struct ptlrpc_service *svc,
376 struct ptlrpc_reply_state *rs = NULL;
378 spin_lock(&svc->srv_lock);
379 /* See if we have anything in a pool, and wait if nothing */
380 while (list_empty(&svc->srv_free_rs_list)) {
381 struct l_wait_info lwi;
383 spin_unlock(&svc->srv_lock);
384 /* If we cannot get anything for some long time, we better
385 bail out instead of waiting infinitely */
386 lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
387 rc = l_wait_event(svc->srv_free_rs_waitq,
388 !list_empty(&svc->srv_free_rs_list), &lwi);
391 spin_lock(&svc->srv_lock);
394 rs = list_entry(svc->srv_free_rs_list.next, struct ptlrpc_reply_state,
396 list_del(&rs->rs_list);
397 spin_unlock(&svc->srv_lock);
399 LASSERTF(svc->srv_max_reply_size > size, "Want %d, prealloc %d\n", size,
400 svc->srv_max_reply_size);
407 static int lustre_pack_reply_v1(struct ptlrpc_request *req, int count,
408 __u32 *lens, char **bufs, int flags)
410 struct ptlrpc_reply_state *rs;
415 LASSERT(req->rq_reply_state == NULL);
417 if ((flags & LPRFL_EARLY_REPLY) == 0)
418 req->rq_packed_final = 1;
420 msg_len = lustre_msg_size_v1(count, lens);
421 size = sizeof(struct ptlrpc_reply_state) + msg_len;
423 if (unlikely(rs == NULL)) {
424 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service, size);
428 atomic_set(&rs->rs_refcount, 1); /* 1 ref for rq_reply_state */
429 rs->rs_cb_id.cbid_fn = reply_out_callback;
430 rs->rs_cb_id.cbid_arg = rs;
431 rs->rs_service = req->rq_rqbd->rqbd_service;
433 CFS_INIT_LIST_HEAD(&rs->rs_exp_list);
434 CFS_INIT_LIST_HEAD(&rs->rs_obd_list);
435 rs->rs_msg = (struct lustre_msg *)(rs + 1);
437 req->rq_replen = msg_len;
438 req->rq_reply_state = rs;
439 req->rq_repmsg = rs->rs_msg;
441 lustre_init_msg_v1(rs->rs_msg, count, lens, bufs);
443 PTLRPC_RS_DEBUG_LRU_ADD(rs);
448 static int lustre_pack_reply_v2(struct ptlrpc_request *req, int count,
449 __u32 *lens, char **bufs, int flags)
451 struct ptlrpc_reply_state *rs;
456 LASSERT(req->rq_reply_state == NULL);
458 if ((flags & LPRFL_EARLY_REPLY) == 0)
459 req->rq_packed_final = 1;
461 /* use the same size of ptlrpc_body as client requested for
462 * interoperability cases */
463 LASSERT(req->rq_reqmsg);
464 lens[MSG_PTLRPC_BODY_OFF] = lustre_msg_buflen(req->rq_reqmsg,
465 MSG_PTLRPC_BODY_OFF);
467 msg_len = lustre_msg_size_v2(count, lens);
468 size = sizeof(struct ptlrpc_reply_state) + msg_len;
470 if (unlikely(rs == NULL)) {
471 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service, size);
475 atomic_set(&rs->rs_refcount, 1); /* 1 ref for rq_reply_state */
476 rs->rs_cb_id.cbid_fn = reply_out_callback;
477 rs->rs_cb_id.cbid_arg = rs;
478 rs->rs_service = req->rq_rqbd->rqbd_service;
480 CFS_INIT_LIST_HEAD(&rs->rs_exp_list);
481 CFS_INIT_LIST_HEAD(&rs->rs_obd_list);
482 rs->rs_msg = (struct lustre_msg *)(rs + 1);
484 req->rq_replen = msg_len;
485 req->rq_reply_state = rs;
486 req->rq_repmsg = rs->rs_msg;
487 /* server side, no rq_repbuf */
488 lustre_init_msg_v2(rs->rs_msg, count, lens, bufs);
489 lustre_msg_add_version(rs->rs_msg, PTLRPC_MSG_VERSION);
490 lustre_set_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
492 PTLRPC_RS_DEBUG_LRU_ADD(rs);
497 int lustre_pack_reply_flags(struct ptlrpc_request *req, int count, __u32 *lens,
498 char **bufs, int flags)
500 __u32 size[] = { sizeof(struct ptlrpc_body) };
508 LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
510 switch (req->rq_reqmsg->lm_magic) {
511 case LUSTRE_MSG_MAGIC_V1:
512 return lustre_pack_reply_v1(req, count - 1, lens + 1,
513 bufs ? bufs + 1 : NULL, flags);
514 case LUSTRE_MSG_MAGIC_V2:
515 return lustre_pack_reply_v2(req, count, lens, bufs, flags);
517 CERROR("incorrect message magic: %08x\n",
518 req->rq_reqmsg->lm_magic);
523 int lustre_pack_reply(struct ptlrpc_request *req, int count, __u32 *lens,
526 int rc = lustre_pack_reply_flags(req, count, lens, bufs, 0);
528 CERROR("lustre_pack_reply failed: rc=%d size=%d\n", rc,
529 lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens));
534 void *lustre_msg_buf_v1(void *msg, int n, int min_size)
536 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
537 int i, offset, buflen, bufcount;
542 bufcount = m->lm_bufcount;
544 CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
549 buflen = m->lm_buflens[n];
550 if (buflen < min_size) {
551 CERROR("msg %p buffer[%d] size %d too small (required %d)\n",
552 m, n, buflen, min_size);
557 offset = lustre_msg_hdr_size_v1(bufcount);
558 for (i = 0; i < n; i++)
559 offset += size_round(m->lm_buflens[i]);
561 return (char *)m + offset;
564 void *lustre_msg_buf_v2(struct lustre_msg_v2 *m, int n, int min_size)
566 int i, offset, buflen, bufcount;
571 bufcount = m->lm_bufcount;
573 CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
578 buflen = m->lm_buflens[n];
579 if (buflen < min_size) {
580 CERROR("msg %p buffer[%d] size %d too small (required %d)\n",
581 m, n, buflen, min_size);
585 offset = lustre_msg_hdr_size_v2(bufcount);
586 for (i = 0; i < n; i++)
587 offset += size_round(m->lm_buflens[i]);
589 return (char *)m + offset;
592 void *lustre_msg_buf(struct lustre_msg *m, int n, int min_size)
594 switch (m->lm_magic) {
595 case LUSTRE_MSG_MAGIC_V1:
596 return lustre_msg_buf_v1(m, n - 1, min_size);
597 case LUSTRE_MSG_MAGIC_V2:
598 return lustre_msg_buf_v2(m, n, min_size);
600 CERROR("incorrect message magic: %08x\n", m->lm_magic);
605 void lustre_shrink_reply_v1(struct ptlrpc_request *req, int segment,
606 unsigned int newlen, int move_data)
608 struct lustre_msg_v1 *msg = (struct lustre_msg_v1 *)req->rq_repmsg;
609 char *tail = NULL, *newpos;
612 LASSERT(req->rq_reply_state);
614 LASSERT(segment >= 0);
615 LASSERT(msg->lm_bufcount > segment);
616 LASSERT(msg->lm_buflens[segment] >= newlen);
618 if (msg->lm_buflens[segment] == newlen)
621 if (move_data && msg->lm_bufcount > segment + 1) {
622 tail = lustre_msg_buf_v1(msg, segment + 1, 0);
623 for (n = segment + 1; n < msg->lm_bufcount; n++)
624 tail_len += size_round(msg->lm_buflens[n]);
627 msg->lm_buflens[segment] = newlen;
629 if (tail && tail_len) {
630 newpos = lustre_msg_buf_v1(msg, segment + 1, 0);
631 LASSERT(newpos <= tail);
633 memcpy(newpos, tail, tail_len);
636 if (newlen == 0 && msg->lm_bufcount > segment + 1) {
637 memmove(&msg->lm_buflens[segment], &msg->lm_buflens[segment + 1],
638 (msg->lm_bufcount - segment - 1) * sizeof(__u32));
639 msg->lm_buflens[msg->lm_bufcount - 1] = 0;
642 req->rq_replen = lustre_msg_size_v1(msg->lm_bufcount, msg->lm_buflens);
645 void lustre_shrink_reply_v2(struct ptlrpc_request *req, int segment,
646 unsigned int newlen, int move_data)
648 struct lustre_msg_v2 *msg = req->rq_repmsg;
649 char *tail = NULL, *newpos;
652 LASSERT(req->rq_reply_state);
654 LASSERT(msg->lm_bufcount > segment);
655 LASSERT(msg->lm_buflens[segment] >= newlen);
657 if (msg->lm_buflens[segment] == newlen)
660 if (move_data && msg->lm_bufcount > segment + 1) {
661 tail = lustre_msg_buf_v2(msg, segment + 1, 0);
662 for (n = segment + 1; n < msg->lm_bufcount; n++)
663 tail_len += size_round(msg->lm_buflens[n]);
666 msg->lm_buflens[segment] = newlen;
668 if (tail && tail_len) {
669 newpos = lustre_msg_buf_v2(msg, segment + 1, 0);
670 LASSERT(newpos <= tail);
672 memcpy(newpos, tail, tail_len);
675 if (newlen == 0 && msg->lm_bufcount > segment + 1) {
676 memmove(&msg->lm_buflens[segment], &msg->lm_buflens[segment + 1],
677 (msg->lm_bufcount - segment - 1) * sizeof(__u32));
678 msg->lm_buflens[msg->lm_bufcount - 1] = 0;
681 req->rq_replen = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
685 * shrink @segment to size @newlen. if @move_data is non-zero, we also move
686 * data forward from @segment + 1.
688 * if @newlen == 0, we remove the segment completely, but we still keep the
689 * totally bufcount the same to save possible data moving. this will leave a
690 * unused segment with size 0 at the tail, but that's ok.
693 * + if any buffers higher than @segment has been filled in, must call shrink
694 * with non-zero @move_data.
695 * + caller should NOT keep pointers to msg buffers which higher than @segment
698 void lustre_shrink_reply(struct ptlrpc_request *req, int segment,
699 unsigned int newlen, int move_data)
701 switch (req->rq_repmsg->lm_magic) {
702 case LUSTRE_MSG_MAGIC_V1:
703 lustre_shrink_reply_v1(req, segment - 1, newlen, move_data);
705 case LUSTRE_MSG_MAGIC_V2:
706 lustre_shrink_reply_v2(req, segment, newlen, move_data);
709 LASSERTF(0, "incorrect message magic: %08x\n",
710 req->rq_repmsg->lm_magic);
714 void lustre_free_reply_state(struct ptlrpc_reply_state *rs)
716 PTLRPC_RS_DEBUG_LRU_DEL(rs);
718 LASSERT (atomic_read(&rs->rs_refcount) == 0);
719 LASSERT (!rs->rs_difficult || rs->rs_handled);
720 LASSERT (!rs->rs_on_net);
721 LASSERT (!rs->rs_scheduled);
722 LASSERT (rs->rs_export == NULL);
723 LASSERT (rs->rs_nlocks == 0);
724 LASSERT (list_empty(&rs->rs_exp_list));
725 LASSERT (list_empty(&rs->rs_obd_list));
727 if (unlikely(rs->rs_prealloc)) {
728 struct ptlrpc_service *svc = rs->rs_service;
730 spin_lock(&svc->srv_lock);
731 list_add(&rs->rs_list,
732 &svc->srv_free_rs_list);
733 spin_unlock(&svc->srv_lock);
734 cfs_waitq_signal(&svc->srv_free_rs_waitq);
736 OBD_FREE(rs, rs->rs_size);
740 int lustre_unpack_msg_v1(void *msg, int len)
742 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
743 int flipped, required_len, i;
746 /* Now we know the sender speaks my language. */
747 required_len = lustre_msg_hdr_size_v1(0);
748 if (len < required_len) {
749 /* can't even look inside the message */
750 CERROR("message length %d too small for lustre_msg\n", len);
754 flipped = m->lm_magic == LUSTRE_MSG_MAGIC_V1_SWABBED;
756 __swab32s(&m->lm_magic);
757 __swab32s(&m->lm_type);
758 __swab32s(&m->lm_version);
759 __swab32s(&m->lm_opc);
760 __swab64s(&m->lm_last_xid);
761 __swab64s(&m->lm_last_committed);
762 __swab64s(&m->lm_transno);
763 __swab32s(&m->lm_status);
764 __swab32s(&m->lm_flags);
765 __swab32s(&m->lm_conn_cnt);
766 __swab32s(&m->lm_bufcount);
769 if (m->lm_version != PTLRPC_MSG_VERSION) {
770 CERROR("wrong lustre_msg version %08x\n", m->lm_version);
774 required_len = lustre_msg_hdr_size_v1(m->lm_bufcount);
775 if (len < required_len) {
776 /* didn't receive all the buffer lengths */
777 CERROR("message length %d too small for %d buflens\n",
778 len, m->lm_bufcount);
782 for (i = 0; i < m->lm_bufcount; i++) {
784 __swab32s (&m->lm_buflens[i]);
785 required_len += size_round(m->lm_buflens[i]);
788 if (len < required_len) {
789 CERROR("len: %d, required_len %d\n", len, required_len);
790 CERROR("bufcount: %d\n", m->lm_bufcount);
791 for (i = 0; i < m->lm_bufcount; i++)
792 CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
799 static int lustre_unpack_msg_v2(struct lustre_msg_v2 *m, int len)
801 int flipped, required_len, i;
803 /* Now we know the sender speaks my language. */
804 required_len = lustre_msg_hdr_size_v2(0);
805 if (len < required_len) {
806 /* can't even look inside the message */
807 CERROR("message length %d too small for lustre_msg\n", len);
811 flipped = m->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED;
813 __swab32s(&m->lm_magic);
814 __swab32s(&m->lm_bufcount);
815 __swab32s(&m->lm_secflvr);
816 __swab32s(&m->lm_repsize);
817 __swab32s(&m->lm_cksum);
818 __swab32s(&m->lm_flags);
819 CLASSERT(offsetof(typeof(*m), lm_padding_2) != 0);
820 CLASSERT(offsetof(typeof(*m), lm_padding_3) != 0);
823 required_len = lustre_msg_hdr_size_v2(m->lm_bufcount);
824 if (len < required_len) {
825 /* didn't receive all the buffer lengths */
826 CERROR ("message length %d too small for %d buflens\n",
827 len, m->lm_bufcount);
831 for (i = 0; i < m->lm_bufcount; i++) {
833 __swab32s(&m->lm_buflens[i]);
834 required_len += size_round(m->lm_buflens[i]);
837 if (len < required_len) {
838 CERROR("len: %d, required_len %d\n", len, required_len);
839 CERROR("bufcount: %d\n", m->lm_bufcount);
840 for (i = 0; i < m->lm_bufcount; i++)
841 CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
849 * return 1 if some endianness conversions are needed for the req buffers,
851 * -EINVAL if message has wrong magic
853 int lustre_unpack_msg(struct lustre_msg *m, int len)
855 int required_len, rc;
859 /* We can provide a slightly better error log, if we check the
860 * message magic and version first. In the future, struct
861 * lustre_msg may grow, and we'd like to log a version mismatch,
862 * rather than a short message.
865 required_len = offsetof(struct lustre_msg, lm_magic) +
867 if (len < required_len) {
868 /* can't even look inside the message */
869 CERROR("message length %d too small for magic/version check\n",
874 swab_needed = lustre_msg_need_swab(m);
876 switch (m->lm_magic) {
877 case LUSTRE_MSG_MAGIC_V1:
878 case LUSTRE_MSG_MAGIC_V1_SWABBED:
879 rc = lustre_unpack_msg_v1(m, len);
881 case LUSTRE_MSG_MAGIC_V2:
882 case LUSTRE_MSG_MAGIC_V2_SWABBED:
883 rc = lustre_unpack_msg_v2(m, len);
886 CERROR("incorrect message magic: %08x\n", m->lm_magic);
896 static inline int lustre_unpack_ptlrpc_body_v2(struct lustre_msg_v2 *m,
897 int offset, int swab_needed)
899 struct ptlrpc_body *pb;
901 pb = lustre_msg_buf_v2(m, offset, PTLRPC_BODY_MIN_SIZE);
903 CERROR("error unpacking ptlrpc body\n");
907 lustre_swab_ptlrpc_body(pb, lustre_msg_buflen(m, offset));
909 if ((pb->pb_version & ~LUSTRE_VERSION_MASK) != PTLRPC_MSG_VERSION) {
910 CERROR("wrong lustre_msg version %08x\n", pb->pb_version);
917 int lustre_unpack_msg_ptlrpc_body(struct lustre_msg *msg,
918 int offset, int swab_needed)
920 switch (msg->lm_magic) {
921 case LUSTRE_MSG_MAGIC_V1:
923 case LUSTRE_MSG_MAGIC_V2:
924 return lustre_unpack_ptlrpc_body_v2(msg, offset, swab_needed);
926 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
931 int lustre_unpack_req_ptlrpc_body(struct ptlrpc_request *req, int offset)
933 switch (req->rq_reqmsg->lm_magic) {
934 case LUSTRE_MSG_MAGIC_V1:
936 case LUSTRE_MSG_MAGIC_V2: {
937 int swab_needed = ptlrpc_reqbuf_need_swab(req, offset);
938 lustre_set_req_swabbed(req, offset);
939 return lustre_unpack_ptlrpc_body_v2(req->rq_reqmsg, offset,
943 CERROR("incorrect message magic: %08x\n",
944 req->rq_reqmsg->lm_magic);
949 int lustre_unpack_rep_ptlrpc_body(struct ptlrpc_request *req, int offset)
951 switch (req->rq_repmsg->lm_magic) {
952 case LUSTRE_MSG_MAGIC_V1:
954 case LUSTRE_MSG_MAGIC_V2:{
955 int swab_needed = ptlrpc_repbuf_need_swab(req, offset);
956 lustre_set_rep_swabbed(req, offset);
957 return lustre_unpack_ptlrpc_body_v2(req->rq_repmsg, offset,
961 CERROR("incorrect message magic: %08x\n",
962 req->rq_repmsg->lm_magic);
967 static inline int lustre_msg_buflen_v1(void *msg, int n)
969 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
972 if (n >= m->lm_bufcount)
975 return m->lm_buflens[n];
978 static inline int lustre_msg_buflen_v2(struct lustre_msg_v2 *m, int n)
980 if (n >= m->lm_bufcount)
983 return m->lm_buflens[n];
987 * lustre_msg_buflen - return the length of buffer @n in message @m
988 * @m - lustre_msg (request or reply) to look at
989 * @n - message index (base 0)
991 * returns zero for non-existent message indices
993 int lustre_msg_buflen(struct lustre_msg *m, int n)
995 switch (m->lm_magic) {
996 case LUSTRE_MSG_MAGIC_V1:
997 return lustre_msg_buflen_v1(m, n - 1);
998 case LUSTRE_MSG_MAGIC_V2:
999 return lustre_msg_buflen_v2(m, n);
1001 CERROR("incorrect message magic: %08x\n", m->lm_magic);
1005 EXPORT_SYMBOL(lustre_msg_buflen);
1007 static inline void lustre_msg_set_buflen_v1(void *msg, int n, int len)
1009 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
1012 if (n >= m->lm_bufcount)
1015 m->lm_buflens[n] = len;
1019 lustre_msg_set_buflen_v2(struct lustre_msg_v2 *m, int n, int len)
1021 if (n >= m->lm_bufcount)
1024 m->lm_buflens[n] = len;
1027 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len)
1029 switch (m->lm_magic) {
1030 case LUSTRE_MSG_MAGIC_V1:
1031 lustre_msg_set_buflen_v1(m, n - 1, len);
1033 case LUSTRE_MSG_MAGIC_V2:
1034 lustre_msg_set_buflen_v2(m, n, len);
1037 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
1041 EXPORT_SYMBOL(lustre_msg_set_buflen);
1043 /* NB return the bufcount for lustre_msg_v2 format, so if message is packed
1044 * in V1 format, the result is one bigger. (add struct ptlrpc_body). */
1045 int lustre_msg_bufcount(struct lustre_msg *m)
1047 switch (m->lm_magic) {
1048 case LUSTRE_MSG_MAGIC_V1:
1049 return ((struct lustre_msg_v1 *)m)->lm_bufcount + 1;
1050 case LUSTRE_MSG_MAGIC_V2:
1051 return m->lm_bufcount;
1053 CERROR("incorrect message magic: %08x\n", m->lm_magic);
1057 EXPORT_SYMBOL(lustre_msg_bufcount);
1059 char *lustre_msg_string(struct lustre_msg *m, int index, int max_len)
1061 /* max_len == 0 means the string should fill the buffer */
1065 switch (m->lm_magic) {
1066 case LUSTRE_MSG_MAGIC_V1:
1067 str = lustre_msg_buf_v1(m, index - 1, 0);
1068 blen = lustre_msg_buflen_v1(m, index - 1);
1070 case LUSTRE_MSG_MAGIC_V2:
1071 str = lustre_msg_buf_v2(m, index, 0);
1072 blen = lustre_msg_buflen_v2(m, index);
1075 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
1079 CERROR ("can't unpack string in msg %p buffer[%d]\n", m, index);
1083 slen = strnlen(str, blen);
1085 if (slen == blen) { /* not NULL terminated */
1086 CERROR("can't unpack non-NULL terminated string in "
1087 "msg %p buffer[%d] len %d\n", m, index, blen);
1092 if (slen != blen - 1) {
1093 CERROR("can't unpack short string in msg %p "
1094 "buffer[%d] len %d: strlen %d\n",
1095 m, index, blen, slen);
1098 } else if (slen > max_len) {
1099 CERROR("can't unpack oversized string in msg %p "
1100 "buffer[%d] len %d strlen %d: max %d expected\n",
1101 m, index, blen, slen, max_len);
1108 /* Wrap up the normal fixed length cases */
1109 void *lustre_swab_buf(struct lustre_msg *msg, int index, int min_size,
1114 LASSERT(msg != NULL);
1115 switch (msg->lm_magic) {
1116 case LUSTRE_MSG_MAGIC_V1:
1117 ptr = lustre_msg_buf_v1(msg, index - 1, min_size);
1119 case LUSTRE_MSG_MAGIC_V2:
1120 ptr = lustre_msg_buf_v2(msg, index, min_size);
1123 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1128 if (swabber != NULL) {
1129 CDEBUG(D_NET, "Swab buffer %d\n", index);
1130 ((void (*)(void *))swabber)(ptr);
1136 void *lustre_swab_reqbuf(struct ptlrpc_request *req, int index, int min_size,
1139 if (lustre_req_swabbed(req, index))
1140 return lustre_msg_buf(req->rq_reqmsg, index, min_size);
1142 if (!lustre_req_need_swab(req))
1145 lustre_set_req_swabbed(req, index);
1146 return lustre_swab_buf(req->rq_reqmsg, index, min_size, swabber);
1149 void *lustre_swab_repbuf(struct ptlrpc_request *req, int index, int min_size,
1152 if (lustre_rep_swabbed(req, index))
1153 return lustre_msg_buf(req->rq_repmsg, index, min_size);
1155 if (!lustre_rep_need_swab(req))
1158 lustre_set_rep_swabbed(req, index);
1159 return lustre_swab_buf(req->rq_repmsg, index, min_size, swabber);
1162 static inline struct ptlrpc_body *lustre_msg_ptlrpc_body(struct lustre_msg *msg)
1164 return lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1165 PTLRPC_BODY_MIN_SIZE);
1168 __u32 lustre_msghdr_get_flags(struct lustre_msg *msg)
1170 switch (msg->lm_magic) {
1171 case LUSTRE_MSG_MAGIC_V1:
1173 case LUSTRE_MSG_MAGIC_V2:
1174 /* already in host endian */
1175 return msg->lm_flags;
1177 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1182 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags)
1184 switch (msg->lm_magic) {
1185 case LUSTRE_MSG_MAGIC_V1:
1187 case LUSTRE_MSG_MAGIC_V2:
1188 msg->lm_flags = flags;
1191 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1195 __u32 lustre_msg_get_flags(struct lustre_msg *msg)
1197 switch (msg->lm_magic) {
1198 case LUSTRE_MSG_MAGIC_V1:
1199 return ((struct lustre_msg_v1 *)msg)->lm_flags &
1201 case LUSTRE_MSG_MAGIC_V2: {
1202 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1204 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1207 return pb->pb_flags;
1210 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1211 /* flags might be printed in debug code while message
1217 void lustre_msg_add_flags(struct lustre_msg *msg, int flags)
1219 switch (msg->lm_magic) {
1220 case LUSTRE_MSG_MAGIC_V1:
1221 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1222 MSG_GEN_FLAG_MASK & flags;
1224 case LUSTRE_MSG_MAGIC_V2: {
1225 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1226 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1227 pb->pb_flags |= flags;
1231 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1235 void lustre_msg_set_flags(struct lustre_msg *msg, int flags)
1237 switch (msg->lm_magic) {
1238 case LUSTRE_MSG_MAGIC_V1:
1239 ((struct lustre_msg_v1 *)msg)->lm_flags &= ~MSG_GEN_FLAG_MASK;
1240 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1241 MSG_GEN_FLAG_MASK & flags;
1243 case LUSTRE_MSG_MAGIC_V2: {
1244 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1245 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1246 pb->pb_flags = flags;
1250 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1254 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags)
1256 switch (msg->lm_magic) {
1257 case LUSTRE_MSG_MAGIC_V1:
1258 ((struct lustre_msg_v1 *)msg)->lm_flags &=
1259 ~(MSG_GEN_FLAG_MASK & flags);
1261 case LUSTRE_MSG_MAGIC_V2: {
1262 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1263 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1264 pb->pb_flags &= ~(MSG_GEN_FLAG_MASK & flags);
1268 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1272 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg)
1274 switch (msg->lm_magic) {
1275 case LUSTRE_MSG_MAGIC_V1:
1276 return ((struct lustre_msg_v1 *)msg)->lm_flags >>
1278 case LUSTRE_MSG_MAGIC_V2: {
1279 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1281 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1284 return pb->pb_op_flags;
1287 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1292 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags)
1294 switch (msg->lm_magic) {
1295 case LUSTRE_MSG_MAGIC_V1:
1296 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1297 (flags & MSG_GEN_FLAG_MASK) << MSG_OP_FLAG_SHIFT;
1299 case LUSTRE_MSG_MAGIC_V2: {
1300 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1301 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1302 pb->pb_op_flags |= flags;
1306 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1310 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags)
1312 switch (msg->lm_magic) {
1313 case LUSTRE_MSG_MAGIC_V1:
1314 ((struct lustre_msg_v1 *)msg)->lm_flags &= ~MSG_OP_FLAG_MASK;
1315 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1316 ((flags & MSG_GEN_FLAG_MASK) <<MSG_OP_FLAG_SHIFT);
1318 case LUSTRE_MSG_MAGIC_V2: {
1319 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1320 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1321 pb->pb_op_flags |= flags;
1325 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1329 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg)
1331 switch (msg->lm_magic) {
1332 case LUSTRE_MSG_MAGIC_V1:
1333 return &((struct lustre_msg_v1 *)msg)->lm_handle;
1334 case LUSTRE_MSG_MAGIC_V2: {
1335 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1337 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1340 return &pb->pb_handle;
1343 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1348 __u32 lustre_msg_get_type(struct lustre_msg *msg)
1350 switch (msg->lm_magic) {
1351 case LUSTRE_MSG_MAGIC_V1:
1352 return ((struct lustre_msg_v1 *)msg)->lm_type;
1353 case LUSTRE_MSG_MAGIC_V2: {
1354 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1356 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1357 return PTL_RPC_MSG_ERR;
1362 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1363 return PTL_RPC_MSG_ERR;
1367 __u32 lustre_msg_get_version(struct lustre_msg *msg)
1369 switch (msg->lm_magic) {
1370 case LUSTRE_MSG_MAGIC_V1:
1371 return ((struct lustre_msg_v1 *)msg)->lm_version;
1372 case LUSTRE_MSG_MAGIC_V2: {
1373 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1375 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1378 return pb->pb_version;
1381 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1386 void lustre_msg_add_version(struct lustre_msg *msg, int version)
1388 switch (msg->lm_magic) {
1389 case LUSTRE_MSG_MAGIC_V1:
1391 case LUSTRE_MSG_MAGIC_V2: {
1392 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1393 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1394 pb->pb_version |= version;
1398 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1402 __u32 lustre_msg_get_opc(struct lustre_msg *msg)
1404 switch (msg->lm_magic) {
1405 case LUSTRE_MSG_MAGIC_V1:
1406 return ((struct lustre_msg_v1 *)msg)->lm_opc;
1407 case LUSTRE_MSG_MAGIC_V2: {
1408 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1410 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1416 CERROR( "incorrect message magic: %08x\n", msg->lm_magic);
1421 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg)
1423 switch (msg->lm_magic) {
1424 case LUSTRE_MSG_MAGIC_V1:
1425 return ((struct lustre_msg_v1 *)msg)->lm_last_xid;
1426 case LUSTRE_MSG_MAGIC_V2: {
1427 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1429 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1432 return pb->pb_last_xid;
1435 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1440 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg)
1442 switch (msg->lm_magic) {
1443 case LUSTRE_MSG_MAGIC_V1:
1444 return ((struct lustre_msg_v1 *)msg)->lm_last_committed;
1445 case LUSTRE_MSG_MAGIC_V2: {
1446 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1448 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1451 return pb->pb_last_committed;
1454 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1459 __u64 *lustre_msg_get_versions(struct lustre_msg *msg)
1461 switch (msg->lm_magic) {
1462 case LUSTRE_MSG_MAGIC_V1:
1464 case LUSTRE_MSG_MAGIC_V2: {
1465 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1467 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1470 #ifdef PTLRPC_INTEROP_1_6
1471 if (lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF) < sizeof (*pb))
1474 return pb->pb_pre_versions;
1477 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1482 __u64 lustre_msg_get_transno(struct lustre_msg *msg)
1484 switch (msg->lm_magic) {
1485 case LUSTRE_MSG_MAGIC_V1:
1486 return ((struct lustre_msg_v1 *)msg)->lm_transno;
1487 case LUSTRE_MSG_MAGIC_V2: {
1488 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1490 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1493 return pb->pb_transno;
1496 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1501 int lustre_msg_get_status(struct lustre_msg *msg)
1503 switch (msg->lm_magic) {
1504 case LUSTRE_MSG_MAGIC_V1:
1505 return ((struct lustre_msg_v1 *)msg)->lm_status;
1506 case LUSTRE_MSG_MAGIC_V2: {
1507 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1509 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1512 return pb->pb_status;
1515 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1516 /* status might be printed in debug code while message
1522 __u64 lustre_msg_get_slv(struct lustre_msg *msg)
1524 switch (msg->lm_magic) {
1525 case LUSTRE_MSG_MAGIC_V1:
1527 case LUSTRE_MSG_MAGIC_V2: {
1528 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1530 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1536 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1542 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv)
1544 switch (msg->lm_magic) {
1545 case LUSTRE_MSG_MAGIC_V1:
1547 case LUSTRE_MSG_MAGIC_V2: {
1548 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1550 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1557 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1562 __u32 lustre_msg_get_limit(struct lustre_msg *msg)
1564 switch (msg->lm_magic) {
1565 case LUSTRE_MSG_MAGIC_V1:
1567 case LUSTRE_MSG_MAGIC_V2: {
1568 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1570 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1573 return pb->pb_limit;
1576 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1582 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit)
1584 switch (msg->lm_magic) {
1585 case LUSTRE_MSG_MAGIC_V1:
1587 case LUSTRE_MSG_MAGIC_V2: {
1588 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1590 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1593 pb->pb_limit = limit;
1597 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1602 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg)
1604 switch (msg->lm_magic) {
1605 case LUSTRE_MSG_MAGIC_V1:
1606 return ((struct lustre_msg_v1 *)msg)->lm_conn_cnt;
1607 case LUSTRE_MSG_MAGIC_V2: {
1608 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1610 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1613 return pb->pb_conn_cnt;
1616 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1621 int lustre_msg_is_v1(struct lustre_msg *msg)
1623 switch (msg->lm_magic) {
1624 case LUSTRE_MSG_MAGIC_V1:
1625 case LUSTRE_MSG_MAGIC_V1_SWABBED:
1632 __u32 lustre_msg_get_magic(struct lustre_msg *msg)
1634 switch (msg->lm_magic) {
1635 case LUSTRE_MSG_MAGIC_V1:
1636 case LUSTRE_MSG_MAGIC_V1_SWABBED:
1637 case LUSTRE_MSG_MAGIC_V2:
1638 case LUSTRE_MSG_MAGIC_V2_SWABBED:
1639 return msg->lm_magic;
1641 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1646 __u32 lustre_msg_get_timeout(struct lustre_msg *msg)
1648 switch (msg->lm_magic) {
1649 case LUSTRE_MSG_MAGIC_V1:
1651 case LUSTRE_MSG_MAGIC_V2: {
1652 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1654 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1657 return pb->pb_timeout;
1660 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1665 __u32 lustre_msg_get_service_time(struct lustre_msg *msg)
1667 switch (msg->lm_magic) {
1668 case LUSTRE_MSG_MAGIC_V1:
1670 case LUSTRE_MSG_MAGIC_V2: {
1671 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1673 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1676 return pb->pb_service_time;
1679 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1684 __u32 lustre_msg_get_cksum(struct lustre_msg *msg)
1686 switch (msg->lm_magic) {
1687 case LUSTRE_MSG_MAGIC_V1:
1689 case LUSTRE_MSG_MAGIC_V2:
1690 return msg->lm_cksum;
1692 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1698 * the sizeof ptlrpc_body in 1.6 is 88 bytes (64 bytes shorter than current
1699 * size), to be able to interoperate with 1.6 we only calculate checksum
1700 * aginst first 88 bytes of ptlrpc_body.
1702 static const int ptlrpc_body_size_16 = 88;
1704 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg)
1706 switch (msg->lm_magic) {
1707 case LUSTRE_MSG_MAGIC_V1:
1709 case LUSTRE_MSG_MAGIC_V2: {
1710 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1711 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1712 return crc32_le(~(__u32)0, (unsigned char *)pb,
1713 ptlrpc_body_size_16);
1716 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1721 void lustre_msg_set_handle(struct lustre_msg *msg, struct lustre_handle *handle)
1723 switch (msg->lm_magic) {
1724 case LUSTRE_MSG_MAGIC_V1:
1725 ((struct lustre_msg_v1 *)msg)->lm_handle = *handle;
1727 case LUSTRE_MSG_MAGIC_V2: {
1728 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1729 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1730 pb->pb_handle = *handle;
1734 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1738 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type)
1740 switch (msg->lm_magic) {
1741 case LUSTRE_MSG_MAGIC_V1:
1742 ((struct lustre_msg_v1 *)msg)->lm_type = type;
1744 case LUSTRE_MSG_MAGIC_V2: {
1745 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1746 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1751 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1755 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc)
1757 switch (msg->lm_magic) {
1758 case LUSTRE_MSG_MAGIC_V1:
1759 ((struct lustre_msg_v1 *)msg)->lm_opc = opc;
1761 case LUSTRE_MSG_MAGIC_V2: {
1762 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1763 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1768 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1772 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid)
1774 switch (msg->lm_magic) {
1775 case LUSTRE_MSG_MAGIC_V1:
1776 ((struct lustre_msg_v1 *)msg)->lm_last_xid = last_xid;
1778 case LUSTRE_MSG_MAGIC_V2: {
1779 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1780 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1781 pb->pb_last_xid = last_xid;
1785 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1789 void lustre_msg_set_last_committed(struct lustre_msg *msg, __u64 last_committed)
1791 switch (msg->lm_magic) {
1792 case LUSTRE_MSG_MAGIC_V1:
1793 ((struct lustre_msg_v1 *)msg)->lm_last_committed=last_committed;
1795 case LUSTRE_MSG_MAGIC_V2: {
1796 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1797 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1798 pb->pb_last_committed = last_committed;
1802 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1806 void lustre_msg_set_versions(struct lustre_msg *msg, __u64 *versions)
1808 switch (msg->lm_magic) {
1809 case LUSTRE_MSG_MAGIC_V1:
1811 case LUSTRE_MSG_MAGIC_V2: {
1812 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1813 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1814 #ifdef PTLRPC_INTEROP_1_6
1815 /* do nothing for old clients */
1816 if (lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF) < sizeof (*pb))
1819 pb->pb_pre_versions[0] = versions[0];
1820 pb->pb_pre_versions[1] = versions[1];
1821 pb->pb_pre_versions[2] = versions[2];
1822 pb->pb_pre_versions[3] = versions[3];
1826 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1831 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno)
1833 switch (msg->lm_magic) {
1834 case LUSTRE_MSG_MAGIC_V1:
1835 ((struct lustre_msg_v1 *)msg)->lm_transno = transno;
1837 case LUSTRE_MSG_MAGIC_V2: {
1838 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1839 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1840 pb->pb_transno = transno;
1844 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1848 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status)
1850 switch (msg->lm_magic) {
1851 case LUSTRE_MSG_MAGIC_V1:
1852 ((struct lustre_msg_v1 *)msg)->lm_status = status;
1854 case LUSTRE_MSG_MAGIC_V2: {
1855 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1856 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1857 pb->pb_status = status;
1861 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1865 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt)
1867 switch (msg->lm_magic) {
1868 case LUSTRE_MSG_MAGIC_V1:
1869 ((struct lustre_msg_v1 *)msg)->lm_conn_cnt = conn_cnt;
1871 case LUSTRE_MSG_MAGIC_V2: {
1872 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1873 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1874 pb->pb_conn_cnt = conn_cnt;
1878 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1882 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout)
1884 switch (msg->lm_magic) {
1885 case LUSTRE_MSG_MAGIC_V1:
1887 case LUSTRE_MSG_MAGIC_V2: {
1888 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1889 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1890 pb->pb_timeout = timeout;
1894 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1898 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time)
1900 switch (msg->lm_magic) {
1901 case LUSTRE_MSG_MAGIC_V1:
1903 case LUSTRE_MSG_MAGIC_V2: {
1904 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1905 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1906 pb->pb_service_time = service_time;
1910 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1914 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum)
1916 switch (msg->lm_magic) {
1917 case LUSTRE_MSG_MAGIC_V1:
1919 case LUSTRE_MSG_MAGIC_V2:
1920 msg->lm_cksum = cksum;
1923 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1928 /* byte flipping routines for all wire types declared in
1929 * lustre_idl.h implemented here.
1931 void lustre_swab_ptlrpc_body(struct ptlrpc_body *b, int msgsize)
1933 __swab32s (&b->pb_type);
1934 __swab32s (&b->pb_version);
1935 __swab32s (&b->pb_opc);
1936 __swab32s (&b->pb_status);
1937 __swab64s (&b->pb_last_xid);
1938 __swab64s (&b->pb_last_seen);
1939 __swab64s (&b->pb_last_committed);
1940 __swab64s (&b->pb_transno);
1941 __swab32s (&b->pb_flags);
1942 __swab32s (&b->pb_op_flags);
1943 __swab32s (&b->pb_conn_cnt);
1944 __swab32s (&b->pb_timeout);
1945 __swab32s (&b->pb_service_time);
1946 __swab64s (&b->pb_slv);
1947 __swab32s (&b->pb_limit);
1948 if (msgsize < offsetof(struct ptlrpc_body, pb_pre_versions[4]))
1950 __swab64s (&b->pb_pre_versions[0]);
1951 __swab64s (&b->pb_pre_versions[1]);
1952 __swab64s (&b->pb_pre_versions[2]);
1953 __swab64s (&b->pb_pre_versions[3]);
1954 CLASSERT(offsetof(typeof(*b), pb_padding) != 0);
1957 void lustre_swab_connect(struct obd_connect_data *ocd)
1959 __swab64s(&ocd->ocd_connect_flags);
1960 __swab32s(&ocd->ocd_version);
1961 __swab32s(&ocd->ocd_grant);
1962 __swab32s(&ocd->ocd_index);
1963 __swab32s(&ocd->ocd_brw_size);
1964 __swab64s(&ocd->ocd_ibits_known);
1965 __swab32s(&ocd->ocd_nllu);
1966 __swab32s(&ocd->ocd_nllg);
1967 __swab64s(&ocd->ocd_transno);
1968 __swab32s(&ocd->ocd_group);
1969 __swab32s(&ocd->ocd_cksum_types);
1970 CLASSERT(offsetof(typeof(*ocd), padding1) != 0);
1971 CLASSERT(offsetof(typeof(*ocd), padding2) != 0);
1974 void lustre_swab_obdo (struct obdo *o)
1976 __swab64s (&o->o_valid);
1977 __swab64s (&o->o_id);
1978 __swab64s (&o->o_gr);
1979 __swab64s (&o->o_fid);
1980 __swab64s (&o->o_size);
1981 __swab64s (&o->o_mtime);
1982 __swab64s (&o->o_atime);
1983 __swab64s (&o->o_ctime);
1984 __swab64s (&o->o_blocks);
1985 __swab64s (&o->o_grant);
1986 __swab32s (&o->o_blksize);
1987 __swab32s (&o->o_mode);
1988 __swab32s (&o->o_uid);
1989 __swab32s (&o->o_gid);
1990 __swab32s (&o->o_flags);
1991 __swab32s (&o->o_nlink);
1992 __swab32s (&o->o_generation);
1993 __swab32s (&o->o_misc);
1994 __swab32s (&o->o_easize);
1995 __swab32s (&o->o_mds);
1996 __swab32s (&o->o_stripe_idx);
1997 __swab32s (&o->o_padding_1);
1998 /* o_inline is opaque */
2001 void lustre_swab_obd_statfs (struct obd_statfs *os)
2003 __swab64s (&os->os_type);
2004 __swab64s (&os->os_blocks);
2005 __swab64s (&os->os_bfree);
2006 __swab64s (&os->os_bavail);
2007 __swab64s (&os->os_files);
2008 __swab64s (&os->os_ffree);
2009 /* no need to swab os_fsid */
2010 __swab32s (&os->os_bsize);
2011 __swab32s (&os->os_namelen);
2012 __swab64s (&os->os_maxbytes);
2013 __swab32s (&os->os_state);
2014 /* no need to swap os_spare */
2017 void lustre_swab_obd_ioobj (struct obd_ioobj *ioo)
2019 __swab64s (&ioo->ioo_id);
2020 __swab64s (&ioo->ioo_gr);
2021 __swab32s (&ioo->ioo_type);
2022 __swab32s (&ioo->ioo_bufcnt);
2025 void lustre_swab_niobuf_remote (struct niobuf_remote *nbr)
2027 __swab64s (&nbr->offset);
2028 __swab32s (&nbr->len);
2029 __swab32s (&nbr->flags);
2032 void lustre_swab_ost_body (struct ost_body *b)
2034 lustre_swab_obdo (&b->oa);
2037 void lustre_swab_ost_last_id(obd_id *id)
2042 void lustre_swab_ost_lvb(struct ost_lvb *lvb)
2044 __swab64s(&lvb->lvb_size);
2045 __swab64s(&lvb->lvb_mtime);
2046 __swab64s(&lvb->lvb_atime);
2047 __swab64s(&lvb->lvb_ctime);
2048 __swab64s(&lvb->lvb_blocks);
2051 void lustre_swab_mds_status_req (struct mds_status_req *r)
2053 __swab32s (&r->flags);
2054 __swab32s (&r->repbuf);
2057 void lustre_swab_mds_body (struct mds_body *b)
2059 lustre_swab_ll_fid (&b->fid1);
2060 lustre_swab_ll_fid (&b->fid2);
2061 /* handle is opaque */
2062 __swab64s (&b->valid);
2063 __swab64s (&b->size);
2064 __swab64s (&b->mtime);
2065 __swab64s (&b->atime);
2066 __swab64s (&b->ctime);
2067 __swab64s (&b->blocks);
2068 __swab64s (&b->io_epoch);
2069 __swab64s (&b->ino);
2070 __swab32s (&b->fsuid);
2071 __swab32s (&b->fsgid);
2072 __swab32s (&b->capability);
2073 __swab32s (&b->mode);
2074 __swab32s (&b->uid);
2075 __swab32s (&b->gid);
2076 __swab32s (&b->flags);
2077 __swab32s (&b->rdev);
2078 __swab32s (&b->nlink);
2079 __swab32s (&b->generation);
2080 __swab32s (&b->suppgid);
2081 __swab32s (&b->eadatasize);
2082 __swab32s (&b->aclsize);
2083 __swab32s (&b->max_mdsize);
2084 __swab32s (&b->max_cookiesize);
2085 __swab32s (&b->padding_4);
2088 void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
2091 __swab32s(&mti->mti_lustre_ver);
2092 __swab32s(&mti->mti_stripe_index);
2093 __swab32s(&mti->mti_config_ver);
2094 __swab32s(&mti->mti_flags);
2095 __swab32s(&mti->mti_nid_count);
2096 CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
2097 for (i = 0; i < MTI_NIDS_MAX; i++)
2098 __swab64s(&mti->mti_nids[i]);
2101 static void lustre_swab_obd_dqinfo (struct obd_dqinfo *i)
2103 __swab64s (&i->dqi_bgrace);
2104 __swab64s (&i->dqi_igrace);
2105 __swab32s (&i->dqi_flags);
2106 __swab32s (&i->dqi_valid);
2109 static void lustre_swab_obd_dqblk (struct obd_dqblk *b)
2111 __swab64s (&b->dqb_ihardlimit);
2112 __swab64s (&b->dqb_isoftlimit);
2113 __swab64s (&b->dqb_curinodes);
2114 __swab64s (&b->dqb_bhardlimit);
2115 __swab64s (&b->dqb_bsoftlimit);
2116 __swab64s (&b->dqb_curspace);
2117 __swab64s (&b->dqb_btime);
2118 __swab64s (&b->dqb_itime);
2119 __swab32s (&b->dqb_valid);
2120 CLASSERT(offsetof(typeof(*b), padding) != 0);
2123 void lustre_swab_obd_quotactl (struct obd_quotactl *q)
2125 __swab32s (&q->qc_cmd);
2126 __swab32s (&q->qc_type);
2127 __swab32s (&q->qc_id);
2128 __swab32s (&q->qc_stat);
2129 lustre_swab_obd_dqinfo (&q->qc_dqinfo);
2130 lustre_swab_obd_dqblk (&q->qc_dqblk);
2133 void lustre_swab_quota_adjust_qunit (struct quota_adjust_qunit *q)
2135 __swab32s (&q->qaq_flags);
2136 __swab32s (&q->qaq_id);
2137 __swab64s (&q->qaq_bunit_sz);
2138 __swab64s (&q->qaq_iunit_sz);
2141 void lustre_swab_mds_rec_setattr (struct mds_rec_setattr *sa)
2143 __swab32s (&sa->sa_opcode);
2144 __swab32s (&sa->sa_fsuid);
2145 __swab32s (&sa->sa_fsgid);
2146 __swab32s (&sa->sa_cap);
2147 __swab32s (&sa->sa_suppgid);
2148 __swab32s (&sa->sa_mode);
2149 lustre_swab_ll_fid (&sa->sa_fid);
2150 __swab64s (&sa->sa_valid);
2151 __swab64s (&sa->sa_size);
2152 __swab64s (&sa->sa_mtime);
2153 __swab64s (&sa->sa_atime);
2154 __swab64s (&sa->sa_ctime);
2155 __swab32s (&sa->sa_uid);
2156 __swab32s (&sa->sa_gid);
2157 __swab32s (&sa->sa_attr_flags);
2158 CLASSERT(offsetof(typeof(*sa), sa_padding) != 0);
2161 void lustre_swab_mds_rec_join (struct mds_rec_join *jr)
2163 __swab64s(&jr->jr_headsize);
2164 lustre_swab_ll_fid(&jr->jr_fid);
2167 void lustre_swab_mds_rec_create (struct mds_rec_create *cr)
2169 __swab32s (&cr->cr_opcode);
2170 __swab32s (&cr->cr_fsuid);
2171 __swab32s (&cr->cr_fsgid);
2172 __swab32s (&cr->cr_cap);
2173 __swab32s (&cr->cr_flags); /* for use with open */
2174 __swab32s (&cr->cr_mode);
2175 lustre_swab_ll_fid (&cr->cr_fid);
2176 lustre_swab_ll_fid (&cr->cr_replayfid);
2177 __swab64s (&cr->cr_time);
2178 __swab64s (&cr->cr_rdev);
2179 __swab32s (&cr->cr_suppgid);
2180 CLASSERT(offsetof(typeof(*cr), cr_padding_1) != 0);
2181 CLASSERT(offsetof(typeof(*cr), cr_padding_2) != 0);
2182 CLASSERT(offsetof(typeof(*cr), cr_padding_3) != 0);
2183 CLASSERT(offsetof(typeof(*cr), cr_padding_4) != 0);
2184 CLASSERT(offsetof(typeof(*cr), cr_padding_5) != 0);
2187 void lustre_swab_mds_rec_link (struct mds_rec_link *lk)
2189 __swab32s (&lk->lk_opcode);
2190 __swab32s (&lk->lk_fsuid);
2191 __swab32s (&lk->lk_fsgid);
2192 __swab32s (&lk->lk_cap);
2193 __swab32s (&lk->lk_suppgid1);
2194 __swab32s (&lk->lk_suppgid2);
2195 lustre_swab_ll_fid (&lk->lk_fid1);
2196 lustre_swab_ll_fid (&lk->lk_fid2);
2197 __swab64s (&lk->lk_time);
2198 CLASSERT(offsetof(typeof(*lk), lk_padding_1) != 0);
2199 CLASSERT(offsetof(typeof(*lk), lk_padding_2) != 0);
2200 CLASSERT(offsetof(typeof(*lk), lk_padding_3) != 0);
2201 CLASSERT(offsetof(typeof(*lk), lk_padding_4) != 0);
2204 void lustre_swab_mds_rec_unlink (struct mds_rec_unlink *ul)
2206 __swab32s (&ul->ul_opcode);
2207 __swab32s (&ul->ul_fsuid);
2208 __swab32s (&ul->ul_fsgid);
2209 __swab32s (&ul->ul_cap);
2210 __swab32s (&ul->ul_suppgid);
2211 __swab32s (&ul->ul_mode);
2212 lustre_swab_ll_fid (&ul->ul_fid1);
2213 lustre_swab_ll_fid (&ul->ul_fid2);
2214 __swab64s (&ul->ul_time);
2215 CLASSERT(offsetof(typeof(*ul), ul_padding_1) != 0);
2216 CLASSERT(offsetof(typeof(*ul), ul_padding_2) != 0);
2217 CLASSERT(offsetof(typeof(*ul), ul_padding_3) != 0);
2218 CLASSERT(offsetof(typeof(*ul), ul_padding_4) != 0);
2221 void lustre_swab_fiemap_extent(struct ll_fiemap_extent *fm_extent)
2223 __swab64s(&fm_extent->fe_logical);
2224 __swab64s(&fm_extent->fe_physical);
2225 __swab64s(&fm_extent->fe_length);
2226 __swab32s(&fm_extent->fe_flags);
2227 __swab32s(&fm_extent->fe_device);
2230 void lustre_swab_fiemap(struct ll_user_fiemap *fiemap)
2234 __swab64s(&fiemap->fm_start);
2235 __swab64s(&fiemap->fm_length);
2236 __swab32s(&fiemap->fm_flags);
2237 __swab32s(&fiemap->fm_mapped_extents);
2238 __swab32s(&fiemap->fm_extent_count);
2239 __swab32s(&fiemap->fm_reserved);
2241 for (i = 0; i < fiemap->fm_mapped_extents; i++)
2242 lustre_swab_fiemap_extent(&fiemap->fm_extents[i]);
2245 void lustre_swab_mds_rec_rename (struct mds_rec_rename *rn)
2247 __swab32s (&rn->rn_opcode);
2248 __swab32s (&rn->rn_fsuid);
2249 __swab32s (&rn->rn_fsgid);
2250 __swab32s (&rn->rn_cap);
2251 __swab32s (&rn->rn_suppgid1);
2252 __swab32s (&rn->rn_suppgid2);
2253 lustre_swab_ll_fid (&rn->rn_fid1);
2254 lustre_swab_ll_fid (&rn->rn_fid2);
2255 __swab64s (&rn->rn_time);
2256 CLASSERT(offsetof(typeof(*rn), rn_padding_1) != 0);
2257 CLASSERT(offsetof(typeof(*rn), rn_padding_2) != 0);
2258 CLASSERT(offsetof(typeof(*rn), rn_padding_3) != 0);
2259 CLASSERT(offsetof(typeof(*rn), rn_padding_4) != 0);
2262 void lustre_swab_lov_desc (struct lov_desc *ld)
2264 __swab32s (&ld->ld_tgt_count);
2265 __swab32s (&ld->ld_active_tgt_count);
2266 __swab32s (&ld->ld_default_stripe_count);
2267 __swab32s (&ld->ld_pattern);
2268 __swab64s (&ld->ld_default_stripe_size);
2269 __swab64s (&ld->ld_default_stripe_offset);
2270 __swab32s (&ld->ld_qos_maxage);
2271 /* uuid endian insensitive */
2275 static void lustre_swab_lov_user_md_common(struct lov_user_md_v1 *lum)
2278 __swab32s(&lum->lmm_magic);
2279 __swab32s(&lum->lmm_pattern);
2280 __swab64s(&lum->lmm_object_id);
2281 __swab64s(&lum->lmm_object_gr);
2282 __swab32s(&lum->lmm_stripe_size);
2283 __swab16s(&lum->lmm_stripe_count);
2284 __swab16s(&lum->lmm_stripe_offset);
2288 void lustre_swab_lov_user_md_v1(struct lov_user_md_v1 *lum)
2291 CDEBUG(D_IOCTL, "swabbing lov_user_md v1\n");
2292 lustre_swab_lov_user_md_common(lum);
2296 void lustre_swab_lov_user_md_v3(struct lov_user_md_v3 *lum)
2299 CDEBUG(D_IOCTL, "swabbing lov_user_md v3\n");
2300 lustre_swab_lov_user_md_common((struct lov_user_md_v1 *)lum);
2301 /* lmm_pool_name nothing to do with char */
2305 void lustre_swab_lov_user_md_join(struct lov_user_md_join *lumj)
2308 CDEBUG(D_IOCTL, "swabbing lov_user_md_join\n");
2309 __swab32s(&lumj->lmm_magic);
2310 __swab32s(&lumj->lmm_pattern);
2311 __swab64s(&lumj->lmm_object_id);
2312 __swab64s(&lumj->lmm_object_gr);
2313 __swab32s(&lumj->lmm_stripe_size);
2314 __swab32s(&lumj->lmm_stripe_count);
2315 __swab32s(&lumj->lmm_extent_count);
2319 void lustre_swab_lov_user_md_objects(struct lov_user_ost_data *lod,
2324 for (i = 0; i < stripe_count; i++) {
2325 __swab64s(&(lod[i].l_object_id));
2326 __swab64s(&(lod[i].l_object_gr));
2327 __swab32s(&(lod[i].l_ost_gen));
2328 __swab32s(&(lod[i].l_ost_idx));
2333 void lustre_swab_ldlm_res_id (struct ldlm_res_id *id)
2337 for (i = 0; i < RES_NAME_SIZE; i++)
2338 __swab64s (&id->name[i]);
2341 void lustre_swab_ldlm_policy_data (ldlm_policy_data_t *d)
2343 /* the lock data is a union and the first two fields are always an
2344 * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
2345 * data the same way. */
2346 __swab64s(&d->l_extent.start);
2347 __swab64s(&d->l_extent.end);
2348 __swab64s(&d->l_extent.gid);
2349 __swab32s(&d->l_flock.pid);
2352 void lustre_swab_ldlm_intent (struct ldlm_intent *i)
2354 __swab64s (&i->opc);
2357 void lustre_swab_ldlm_resource_desc (struct ldlm_resource_desc *r)
2359 __swab32s (&r->lr_type);
2360 CLASSERT(offsetof(typeof(*r), lr_padding) != 0);
2361 lustre_swab_ldlm_res_id (&r->lr_name);
2364 void lustre_swab_ldlm_lock_desc (struct ldlm_lock_desc *l)
2366 lustre_swab_ldlm_resource_desc (&l->l_resource);
2367 __swab32s (&l->l_req_mode);
2368 __swab32s (&l->l_granted_mode);
2369 lustre_swab_ldlm_policy_data (&l->l_policy_data);
2372 void lustre_swab_ldlm_request (struct ldlm_request *rq)
2374 __swab32s (&rq->lock_flags);
2375 lustre_swab_ldlm_lock_desc (&rq->lock_desc);
2376 __swab32s (&rq->lock_count);
2377 /* lock_handle[] opaque */
2380 void lustre_swab_ldlm_reply (struct ldlm_reply *r)
2382 __swab32s (&r->lock_flags);
2383 CLASSERT(offsetof(typeof(*r), lock_padding) != 0);
2384 lustre_swab_ldlm_lock_desc (&r->lock_desc);
2385 /* lock_handle opaque */
2386 __swab64s (&r->lock_policy_res1);
2387 __swab64s (&r->lock_policy_res2);
2390 /* no one calls this */
2391 int llog_log_swabbed(struct llog_log_hdr *hdr)
2393 if (hdr->llh_hdr.lrh_type == __swab32(LLOG_HDR_MAGIC))
2395 if (hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC)
2400 void lustre_swab_qdata(struct qunit_data *d)
2402 __swab32s (&d->qd_id);
2403 __swab32s (&d->qd_flags);
2404 __swab64s (&d->qd_count);
2405 __swab64s (&d->qd_qunit);
2408 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2409 void lustre_swab_qdata_old2(struct qunit_data_old2 *d)
2411 __swab32s (&d->qd_id);
2412 __swab32s (&d->qd_flags);
2413 __swab64s (&d->qd_count);
2416 #warning "remove quota code above for format absolete in new release"
2421 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2422 void qdata_v2_to_v3(struct qunit_data_old2 *d,
2423 struct qunit_data *qdata)
2428 qdata->qd_id = d->qd_id;
2429 qdata->qd_flags = d->qd_flags;
2430 qdata->qd_count = d->qd_count;
2433 struct qunit_data_old2 *qdata_v3_to_v2(struct qunit_data *d)
2435 struct qunit_data tmp;
2436 struct qunit_data_old2 *ret;
2443 ret = (struct qunit_data_old2 *)d;
2444 ret->qd_id = tmp.qd_id;
2445 ret->qd_flags = tmp.qd_flags & LQUOTA_QUNIT_FLAGS;
2446 ret->qd_count = tmp.qd_count;
2450 #warning "remove quota code above for format absolete in new release"
2453 /* got qdata from request(req/rep) */
2454 int quota_get_qdata(void *request, struct qunit_data *qdata,
2455 int is_req, int is_exp)
2457 struct ptlrpc_request *req = (struct ptlrpc_request *)request;
2458 struct qunit_data *new;
2459 struct qunit_data_old2 *old2;
2460 int size2 = sizeof(struct qunit_data_old2);
2461 __u64 flags = is_exp ? req->rq_export->exp_connect_flags :
2462 req->rq_import->imp_connect_data.ocd_connect_flags;
2468 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2469 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS))
2470 goto without_change_qs;
2472 #warning "remove quota code above for format absolete in new release"
2475 /* support for quota64 and change_qs */
2476 if (flags & OBD_CONNECT_CHANGE_QS) {
2477 if (!(flags & OBD_CONNECT_QUOTA64)) {
2478 CDEBUG(D_ERROR, "Wire protocol for qunit is broken!\n");
2481 if (is_req == QUOTA_REQUEST)
2482 new = lustre_swab_reqbuf(req, REQ_REC_OFF,
2483 sizeof(struct qunit_data),
2486 new = lustre_swab_repbuf(req, REPLY_REC_OFF,
2487 sizeof(struct qunit_data),
2490 GOTO(out, rc = -EPROTO);
2492 QDATA_SET_CHANGE_QS(qdata);
2495 QDATA_CLR_CHANGE_QS(qdata);
2498 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2500 /* only support for quota64 */
2501 if (flags & OBD_CONNECT_QUOTA64) {
2503 if (is_req == QUOTA_REQUEST)
2504 old2 = lustre_swab_reqbuf(req, REQ_REC_OFF, size2,
2505 lustre_swab_qdata_old2);
2507 old2 = lustre_swab_repbuf(req, REPLY_REC_OFF, size2,
2508 lustre_swab_qdata_old2);
2510 GOTO(out, rc = -EPROTO);
2511 qdata_v2_to_v3(old2, qdata);
2516 #warning "remove quota code above for format absolete in new release"
2521 EXPORT_SYMBOL(quota_get_qdata);
2523 /* copy qdata to request(req/rep) */
2524 int quota_copy_qdata(void *request, struct qunit_data *qdata,
2525 int is_req, int is_exp)
2527 struct ptlrpc_request *req = (struct ptlrpc_request *)request;
2529 struct qunit_data_old2 *old2;
2530 __u64 flags = is_exp ? req->rq_export->exp_connect_flags :
2531 req->rq_import->imp_connect_data.ocd_connect_flags;
2537 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2538 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS))
2539 goto without_change_qs;
2541 #warning "remove quota code above for format absolete in new release"
2544 /* support for quota64 and change_qs */
2545 if (flags & OBD_CONNECT_CHANGE_QS) {
2546 if (!(flags & OBD_CONNECT_QUOTA64)) {
2547 CERROR("Wire protocol for qunit is broken!\n");
2550 if (is_req == QUOTA_REQUEST)
2551 target = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
2552 sizeof(struct qunit_data));
2554 target = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
2555 sizeof(struct qunit_data));
2557 GOTO(out, rc = -EPROTO);
2558 memcpy(target, qdata, sizeof(*qdata));
2562 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2564 /* only support for quota64 */
2565 if (flags & OBD_CONNECT_QUOTA64) {
2566 if (is_req == QUOTA_REQUEST)
2567 target = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
2568 sizeof(struct qunit_data_old2));
2570 target = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
2571 sizeof(struct qunit_data_old2));
2573 GOTO(out, rc = -EPROTO);
2574 old2 = qdata_v3_to_v2(qdata);
2575 memcpy(target, old2, sizeof(*old2));
2579 #warning "remove quota code above for format absolete in new release"
2584 EXPORT_SYMBOL(quota_copy_qdata);
2586 int quota_get_qunit_data_size(__u64 flag)
2590 if (flag & OBD_CONNECT_CHANGE_QS)
2591 size = sizeof(struct qunit_data);
2593 size = sizeof(struct qunit_data_old2);
2597 EXPORT_SYMBOL(quota_get_qunit_data_size);
2598 #endif /* __KERNEL__ */
2600 static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2602 LASSERT(req->rq_reqmsg);
2604 switch (req->rq_reqmsg->lm_magic) {
2605 case LUSTRE_MSG_MAGIC_V1:
2607 case LUSTRE_MSG_MAGIC_V2:
2608 return lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
2610 CERROR("incorrect message magic: %08x\n",
2611 req->rq_reqmsg->lm_magic);
2616 static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2618 LASSERT(req->rq_repmsg);
2620 switch (req->rq_repmsg->lm_magic) {
2621 case LUSTRE_MSG_MAGIC_V1:
2623 case LUSTRE_MSG_MAGIC_V2:
2624 return lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
2626 CERROR("incorrect message magic: %08x\n",
2627 req->rq_repmsg->lm_magic);
2632 void _debug_req(struct ptlrpc_request *req, __u32 mask,
2633 struct libcfs_debug_msg_data *data, const char *fmt, ... )
2642 /* Caller is responsible holding a reference on the request */
2643 LASSERT(req && atomic_read(&req->rq_refcount) > 0);
2645 if (req->rq_reqmsg &&
2646 (!lustre_msg_need_swab(req->rq_reqmsg) ||
2647 (lustre_req_need_swab(req) &&
2648 lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF)))) {
2649 opc = lustre_msg_get_opc(req->rq_reqmsg);
2650 req_fl = lustre_msg_get_flags(req->rq_reqmsg);
2653 if (req->rq_repmsg &&
2654 (!lustre_msg_need_swab(req->rq_repmsg) ||
2655 (lustre_rep_need_swab(req) &&
2656 lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF)))) {
2657 rep_fl = lustre_msg_get_flags(req->rq_repmsg);
2658 rep_status = lustre_msg_get_status(req->rq_repmsg);
2661 va_start(args, fmt);
2662 libcfs_debug_vmsg2(data->msg_cdls, data->msg_subsys, mask,
2663 data->msg_file, data->msg_fn, data->msg_line, fmt, args,
2664 " req@%p x"LPD64"/t"LPD64" o%d->%s@%s:%d/%d lens %d/%d e %d "
2665 "to %d dl %ld ref %d fl "REQ_FLAGS_FMT"/%x/%x rc %d/%d\n",
2666 req, req->rq_xid, req->rq_transno, opc,
2667 req->rq_import ? obd2cli_tgt(req->rq_import->imp_obd) :
2669 (char*)req->rq_export->exp_client_uuid.uuid : "<?>",
2671 (char *)req->rq_import->imp_connection->c_remote_uuid.uuid :
2673 (char *)req->rq_export->exp_connection->c_remote_uuid.uuid :
2674 "<?>", req->rq_request_portal, req->rq_reply_portal,
2675 req->rq_reqlen, req->rq_replen,
2676 req->rq_early_count, !!req->rq_timeout, req->rq_deadline,
2677 atomic_read(&req->rq_refcount), DEBUG_REQ_FLAGS(req),
2678 req_fl, rep_fl, req->rq_status, rep_status);
2683 EXPORT_SYMBOL(_debug_req);