1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/pack_generic.c
38 * (Un)packing of OST requests
40 * Author: Peter J. Braam <braam@clusterfs.com>
41 * Author: Phil Schwan <phil@clusterfs.com>
42 * Author: Eric Barton <eeb@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_RPC
47 # include <liblustre.h>
50 #include <libcfs/libcfs.h>
52 #include <obd_support.h>
53 #include <obd_class.h>
54 #include <lustre_net.h>
55 #include <lustre/ll_fiemap.h>
57 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(1,8,99,0)
58 #error "lustre_msg_v1 has been deprecated since 1.6.0, please remove it"
61 static inline int lustre_msg_hdr_size_v1(int count)
63 return size_round(offsetof(struct lustre_msg_v1, lm_buflens[count]));
66 static inline int lustre_msg_hdr_size_v2(int count)
68 return size_round(offsetof(struct lustre_msg_v2, lm_buflens[count]));
71 int lustre_msg_need_swab(struct lustre_msg *msg)
73 return (msg->lm_magic == LUSTRE_MSG_MAGIC_V1_SWABBED) ||
74 (msg->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED);
77 int lustre_msg_check_version_v2(struct lustre_msg_v2 *msg, __u32 version)
79 __u32 ver = lustre_msg_get_version(msg);
80 return (ver & LUSTRE_VERSION_MASK) != version;
83 int lustre_msg_check_version(struct lustre_msg *msg, __u32 version)
85 switch (msg->lm_magic) {
86 case LUSTRE_MSG_MAGIC_V1:
88 case LUSTRE_MSG_MAGIC_V2:
89 return lustre_msg_check_version_v2(msg, version);
91 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
96 static int ptlrpc_reqbuf_need_swab(struct ptlrpc_request *req, int index)
100 swabb = (!lustre_req_need_swab(req)) ||
101 lustre_req_swabbed(req, index);
106 static int ptlrpc_repbuf_need_swab(struct ptlrpc_request *req, int index)
110 swabb = (!lustre_rep_need_swab(req)) ||
111 lustre_rep_swabbed(req, index);
117 /* early reply size */
118 int lustre_msg_early_size(struct ptlrpc_request *req) {
120 /* For b1_6 interoperability */
121 if (req->rq_reqmsg &&
122 req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2) {
123 __u32 pb_len = lustre_msg_buflen(req->rq_reqmsg,
124 MSG_PTLRPC_BODY_OFF);
125 return lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, &pb_len);
129 size = lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
132 EXPORT_SYMBOL(lustre_msg_early_size);
134 static inline int lustre_msg_size_v1(int count, __u32 *lengths)
140 size = lustre_msg_hdr_size_v1(count);
141 for (i = 0; i < count; i++)
142 size += size_round(lengths[i]);
147 static inline int lustre_msg_size_v2(int count, __u32 *lengths)
152 size = lustre_msg_hdr_size_v2(count);
153 for (i = 0; i < count; i++)
154 size += size_round(lengths[i]);
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 "
581 "(required %d, opc=%d)\n",
582 m, n, buflen, min_size, lustre_msg_get_opc(m));
586 offset = lustre_msg_hdr_size_v2(bufcount);
587 for (i = 0; i < n; i++)
588 offset += size_round(m->lm_buflens[i]);
590 return (char *)m + offset;
593 void *lustre_msg_buf(struct lustre_msg *m, int n, int min_size)
595 switch (m->lm_magic) {
596 case LUSTRE_MSG_MAGIC_V1:
597 return lustre_msg_buf_v1(m, n - 1, min_size);
598 case LUSTRE_MSG_MAGIC_V2:
599 return lustre_msg_buf_v2(m, n, min_size);
601 CERROR("incorrect message magic: %08x\n", m->lm_magic);
606 void lustre_shrink_reply_v1(struct ptlrpc_request *req, int segment,
607 unsigned int newlen, int move_data)
609 struct lustre_msg_v1 *msg = (struct lustre_msg_v1 *)req->rq_repmsg;
610 char *tail = NULL, *newpos;
613 LASSERT(req->rq_reply_state);
615 LASSERT(segment >= 0);
616 LASSERT(msg->lm_bufcount > segment);
617 LASSERT(msg->lm_buflens[segment] >= newlen);
619 if (msg->lm_buflens[segment] == newlen)
622 if (move_data && msg->lm_bufcount > segment + 1) {
623 tail = lustre_msg_buf_v1(msg, segment + 1, 0);
624 for (n = segment + 1; n < msg->lm_bufcount; n++)
625 tail_len += size_round(msg->lm_buflens[n]);
628 msg->lm_buflens[segment] = newlen;
630 if (tail && tail_len) {
631 newpos = lustre_msg_buf_v1(msg, segment + 1, 0);
632 LASSERT(newpos <= tail);
634 memcpy(newpos, tail, tail_len);
637 if (newlen == 0 && msg->lm_bufcount > segment + 1) {
638 memmove(&msg->lm_buflens[segment], &msg->lm_buflens[segment + 1],
639 (msg->lm_bufcount - segment - 1) * sizeof(__u32));
640 msg->lm_buflens[msg->lm_bufcount - 1] = 0;
643 req->rq_replen = lustre_msg_size_v1(msg->lm_bufcount, msg->lm_buflens);
646 void lustre_shrink_reply_v2(struct ptlrpc_request *req, int segment,
647 unsigned int newlen, int move_data)
649 struct lustre_msg_v2 *msg = req->rq_repmsg;
650 char *tail = NULL, *newpos;
653 LASSERT(req->rq_reply_state);
655 LASSERT(msg->lm_bufcount > segment);
656 LASSERT(msg->lm_buflens[segment] >= newlen);
658 if (msg->lm_buflens[segment] == newlen)
661 if (move_data && msg->lm_bufcount > segment + 1) {
662 tail = lustre_msg_buf_v2(msg, segment + 1, 0);
663 for (n = segment + 1; n < msg->lm_bufcount; n++)
664 tail_len += size_round(msg->lm_buflens[n]);
667 msg->lm_buflens[segment] = newlen;
669 if (tail && tail_len) {
670 newpos = lustre_msg_buf_v2(msg, segment + 1, 0);
671 LASSERT(newpos <= tail);
673 memcpy(newpos, tail, tail_len);
676 if (newlen == 0 && msg->lm_bufcount > segment + 1) {
677 memmove(&msg->lm_buflens[segment], &msg->lm_buflens[segment + 1],
678 (msg->lm_bufcount - segment - 1) * sizeof(__u32));
679 msg->lm_buflens[msg->lm_bufcount - 1] = 0;
682 req->rq_replen = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
686 * shrink @segment to size @newlen. if @move_data is non-zero, we also move
687 * data forward from @segment + 1.
689 * if @newlen == 0, we remove the segment completely, but we still keep the
690 * totally bufcount the same to save possible data moving. this will leave a
691 * unused segment with size 0 at the tail, but that's ok.
694 * + if any buffers higher than @segment has been filled in, must call shrink
695 * with non-zero @move_data.
696 * + caller should NOT keep pointers to msg buffers which higher than @segment
699 void lustre_shrink_reply(struct ptlrpc_request *req, int segment,
700 unsigned int newlen, int move_data)
702 switch (req->rq_repmsg->lm_magic) {
703 case LUSTRE_MSG_MAGIC_V1:
704 lustre_shrink_reply_v1(req, segment - 1, newlen, move_data);
706 case LUSTRE_MSG_MAGIC_V2:
707 lustre_shrink_reply_v2(req, segment, newlen, move_data);
710 LASSERTF(0, "incorrect message magic: %08x\n",
711 req->rq_repmsg->lm_magic);
715 void lustre_free_reply_state(struct ptlrpc_reply_state *rs)
717 PTLRPC_RS_DEBUG_LRU_DEL(rs);
719 LASSERT (atomic_read(&rs->rs_refcount) == 0);
720 LASSERT (!rs->rs_difficult || rs->rs_handled);
721 LASSERT (!rs->rs_on_net);
722 LASSERT (!rs->rs_scheduled);
723 LASSERT (rs->rs_export == NULL);
724 LASSERT (rs->rs_nlocks == 0);
725 LASSERT (list_empty(&rs->rs_exp_list));
726 LASSERT (list_empty(&rs->rs_obd_list));
728 if (unlikely(rs->rs_prealloc)) {
729 struct ptlrpc_service *svc = rs->rs_service;
731 spin_lock(&svc->srv_lock);
732 list_add(&rs->rs_list,
733 &svc->srv_free_rs_list);
734 spin_unlock(&svc->srv_lock);
735 cfs_waitq_signal(&svc->srv_free_rs_waitq);
737 OBD_FREE(rs, rs->rs_size);
741 int lustre_unpack_msg_v1(void *msg, int len)
743 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
744 int flipped, required_len, i;
747 /* Now we know the sender speaks my language. */
748 required_len = lustre_msg_hdr_size_v1(0);
749 if (len < required_len) {
750 /* can't even look inside the message */
751 CERROR("message length %d too small for lustre_msg\n", len);
755 flipped = m->lm_magic == LUSTRE_MSG_MAGIC_V1_SWABBED;
757 __swab32s(&m->lm_magic);
758 __swab32s(&m->lm_type);
759 __swab32s(&m->lm_version);
760 __swab32s(&m->lm_opc);
761 __swab64s(&m->lm_last_xid);
762 __swab64s(&m->lm_last_committed);
763 __swab64s(&m->lm_transno);
764 __swab32s(&m->lm_status);
765 __swab32s(&m->lm_flags);
766 __swab32s(&m->lm_conn_cnt);
767 __swab32s(&m->lm_bufcount);
770 if (m->lm_version != PTLRPC_MSG_VERSION) {
771 CERROR("wrong lustre_msg version %08x\n", m->lm_version);
775 required_len = lustre_msg_hdr_size_v1(m->lm_bufcount);
776 if (len < required_len) {
777 /* didn't receive all the buffer lengths */
778 CERROR("message length %d too small for %d buflens\n",
779 len, m->lm_bufcount);
783 for (i = 0; i < m->lm_bufcount; i++) {
785 __swab32s (&m->lm_buflens[i]);
786 required_len += size_round(m->lm_buflens[i]);
789 if (len < required_len) {
790 CERROR("len: %d, required_len %d\n", len, required_len);
791 CERROR("bufcount: %d\n", m->lm_bufcount);
792 for (i = 0; i < m->lm_bufcount; i++)
793 CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
800 static int lustre_unpack_msg_v2(struct lustre_msg_v2 *m, int len)
802 int flipped, required_len, i;
804 /* Now we know the sender speaks my language. */
805 required_len = lustre_msg_hdr_size_v2(0);
806 if (len < required_len) {
807 /* can't even look inside the message */
808 CERROR("message length %d too small for lustre_msg\n", len);
812 flipped = m->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED;
814 __swab32s(&m->lm_magic);
815 __swab32s(&m->lm_bufcount);
816 __swab32s(&m->lm_secflvr);
817 __swab32s(&m->lm_repsize);
818 __swab32s(&m->lm_cksum);
819 __swab32s(&m->lm_flags);
820 CLASSERT(offsetof(typeof(*m), lm_padding_2) != 0);
821 CLASSERT(offsetof(typeof(*m), lm_padding_3) != 0);
824 required_len = lustre_msg_hdr_size_v2(m->lm_bufcount);
825 if (len < required_len) {
826 /* didn't receive all the buffer lengths */
827 CERROR ("message length %d too small for %d buflens\n",
828 len, m->lm_bufcount);
832 for (i = 0; i < m->lm_bufcount; i++) {
834 __swab32s(&m->lm_buflens[i]);
835 required_len += size_round(m->lm_buflens[i]);
838 if (len < required_len) {
839 CERROR("len: %d, required_len %d\n", len, required_len);
840 CERROR("bufcount: %d\n", m->lm_bufcount);
841 for (i = 0; i < m->lm_bufcount; i++)
842 CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
850 * return 1 if some endianness conversions are needed for the req buffers,
852 * -EINVAL if message has wrong magic
854 int lustre_unpack_msg(struct lustre_msg *m, int len)
856 int required_len, rc;
860 /* We can provide a slightly better error log, if we check the
861 * message magic and version first. In the future, struct
862 * lustre_msg may grow, and we'd like to log a version mismatch,
863 * rather than a short message.
866 required_len = offsetof(struct lustre_msg, lm_magic) +
868 if (len < required_len) {
869 /* can't even look inside the message */
870 CERROR("message length %d too small for magic/version check\n",
875 swab_needed = lustre_msg_need_swab(m);
877 switch (m->lm_magic) {
878 case LUSTRE_MSG_MAGIC_V1:
879 case LUSTRE_MSG_MAGIC_V1_SWABBED:
880 rc = lustre_unpack_msg_v1(m, len);
882 case LUSTRE_MSG_MAGIC_V2:
883 case LUSTRE_MSG_MAGIC_V2_SWABBED:
884 rc = lustre_unpack_msg_v2(m, len);
887 CERROR("incorrect message magic: %08x\n", m->lm_magic);
897 static inline int lustre_unpack_ptlrpc_body_v2(struct lustre_msg_v2 *m,
898 int offset, int swab_needed)
900 struct ptlrpc_body *pb;
902 pb = lustre_msg_buf_v2(m, offset, PTLRPC_BODY_MIN_SIZE);
904 CERROR("error unpacking ptlrpc body\n");
908 lustre_swab_ptlrpc_body(pb, lustre_msg_buflen(m, offset));
910 if ((pb->pb_version & ~LUSTRE_VERSION_MASK) != PTLRPC_MSG_VERSION) {
911 CERROR("wrong lustre_msg version %08x\n", pb->pb_version);
918 int lustre_unpack_msg_ptlrpc_body(struct lustre_msg *msg,
919 int offset, int swab_needed)
921 switch (msg->lm_magic) {
922 case LUSTRE_MSG_MAGIC_V1:
924 case LUSTRE_MSG_MAGIC_V2:
925 return lustre_unpack_ptlrpc_body_v2(msg, offset, swab_needed);
927 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
932 int lustre_unpack_req_ptlrpc_body(struct ptlrpc_request *req, int offset)
934 switch (req->rq_reqmsg->lm_magic) {
935 case LUSTRE_MSG_MAGIC_V1:
937 case LUSTRE_MSG_MAGIC_V2: {
938 int swab_needed = ptlrpc_reqbuf_need_swab(req, offset);
939 lustre_set_req_swabbed(req, offset);
940 return lustre_unpack_ptlrpc_body_v2(req->rq_reqmsg, offset,
944 CERROR("incorrect message magic: %08x\n",
945 req->rq_reqmsg->lm_magic);
950 int lustre_unpack_rep_ptlrpc_body(struct ptlrpc_request *req, int offset)
952 switch (req->rq_repmsg->lm_magic) {
953 case LUSTRE_MSG_MAGIC_V1:
955 case LUSTRE_MSG_MAGIC_V2:{
956 int swab_needed = ptlrpc_repbuf_need_swab(req, offset);
957 lustre_set_rep_swabbed(req, offset);
958 return lustre_unpack_ptlrpc_body_v2(req->rq_repmsg, offset,
962 CERROR("incorrect message magic: %08x\n",
963 req->rq_repmsg->lm_magic);
968 static inline int lustre_msg_buflen_v1(void *msg, int n)
970 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
973 if (n >= m->lm_bufcount)
976 return m->lm_buflens[n];
979 static inline int lustre_msg_buflen_v2(struct lustre_msg_v2 *m, int n)
981 if (n >= m->lm_bufcount)
984 return m->lm_buflens[n];
988 * lustre_msg_buflen - return the length of buffer @n in message @m
989 * @m - lustre_msg (request or reply) to look at
990 * @n - message index (base 0)
992 * returns zero for non-existent message indices
994 int lustre_msg_buflen(struct lustre_msg *m, int n)
996 switch (m->lm_magic) {
997 case LUSTRE_MSG_MAGIC_V1:
998 return lustre_msg_buflen_v1(m, n - 1);
999 case LUSTRE_MSG_MAGIC_V2:
1000 return lustre_msg_buflen_v2(m, n);
1002 CERROR("incorrect message magic: %08x\n", m->lm_magic);
1006 EXPORT_SYMBOL(lustre_msg_buflen);
1008 static inline void lustre_msg_set_buflen_v1(void *msg, int n, int len)
1010 struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;
1013 if (n >= m->lm_bufcount)
1016 m->lm_buflens[n] = len;
1020 lustre_msg_set_buflen_v2(struct lustre_msg_v2 *m, int n, int len)
1022 if (n >= m->lm_bufcount)
1025 m->lm_buflens[n] = len;
1028 void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len)
1030 switch (m->lm_magic) {
1031 case LUSTRE_MSG_MAGIC_V1:
1032 lustre_msg_set_buflen_v1(m, n - 1, len);
1034 case LUSTRE_MSG_MAGIC_V2:
1035 lustre_msg_set_buflen_v2(m, n, len);
1038 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
1042 EXPORT_SYMBOL(lustre_msg_set_buflen);
1044 /* NB return the bufcount for lustre_msg_v2 format, so if message is packed
1045 * in V1 format, the result is one bigger. (add struct ptlrpc_body). */
1046 int lustre_msg_bufcount(struct lustre_msg *m)
1048 switch (m->lm_magic) {
1049 case LUSTRE_MSG_MAGIC_V1:
1050 return ((struct lustre_msg_v1 *)m)->lm_bufcount + 1;
1051 case LUSTRE_MSG_MAGIC_V2:
1052 return m->lm_bufcount;
1054 CERROR("incorrect message magic: %08x\n", m->lm_magic);
1058 EXPORT_SYMBOL(lustre_msg_bufcount);
1060 char *lustre_msg_string(struct lustre_msg *m, int index, int max_len)
1062 /* max_len == 0 means the string should fill the buffer */
1066 switch (m->lm_magic) {
1067 case LUSTRE_MSG_MAGIC_V1:
1068 str = lustre_msg_buf_v1(m, index - 1, 0);
1069 blen = lustre_msg_buflen_v1(m, index - 1);
1071 case LUSTRE_MSG_MAGIC_V2:
1072 str = lustre_msg_buf_v2(m, index, 0);
1073 blen = lustre_msg_buflen_v2(m, index);
1076 LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
1080 CERROR ("can't unpack string in msg %p buffer[%d]\n", m, index);
1084 slen = strnlen(str, blen);
1086 if (slen == blen) { /* not NULL terminated */
1087 CERROR("can't unpack non-NULL terminated string in "
1088 "msg %p buffer[%d] len %d\n", m, index, blen);
1093 if (slen != blen - 1) {
1094 CERROR("can't unpack short string in msg %p "
1095 "buffer[%d] len %d: strlen %d\n",
1096 m, index, blen, slen);
1099 } else if (slen > max_len) {
1100 CERROR("can't unpack oversized string in msg %p "
1101 "buffer[%d] len %d strlen %d: max %d expected\n",
1102 m, index, blen, slen, max_len);
1109 /* Wrap up the normal fixed length cases */
1110 void *lustre_swab_buf(struct lustre_msg *msg, int index, int min_size,
1115 LASSERT(msg != NULL);
1116 switch (msg->lm_magic) {
1117 case LUSTRE_MSG_MAGIC_V1:
1118 ptr = lustre_msg_buf_v1(msg, index - 1, min_size);
1120 case LUSTRE_MSG_MAGIC_V2:
1121 ptr = lustre_msg_buf_v2(msg, index, min_size);
1124 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1129 if (swabber != NULL) {
1130 CDEBUG(D_NET, "Swab buffer %d\n", index);
1131 ((void (*)(void *))swabber)(ptr);
1137 void *lustre_swab_reqbuf(struct ptlrpc_request *req, int index, int min_size,
1140 if (lustre_req_swabbed(req, index))
1141 return lustre_msg_buf(req->rq_reqmsg, index, min_size);
1143 if (!lustre_req_need_swab(req))
1146 lustre_set_req_swabbed(req, index);
1147 return lustre_swab_buf(req->rq_reqmsg, index, min_size, swabber);
1150 void *lustre_swab_repbuf(struct ptlrpc_request *req, int index, int min_size,
1153 if (lustre_rep_swabbed(req, index))
1154 return lustre_msg_buf(req->rq_repmsg, index, min_size);
1156 if (!lustre_rep_need_swab(req))
1159 lustre_set_rep_swabbed(req, index);
1160 return lustre_swab_buf(req->rq_repmsg, index, min_size, swabber);
1163 static inline struct ptlrpc_body *lustre_msg_ptlrpc_body(struct lustre_msg *msg)
1165 return lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1166 PTLRPC_BODY_MIN_SIZE);
1169 __u32 lustre_msghdr_get_flags(struct lustre_msg *msg)
1171 switch (msg->lm_magic) {
1172 case LUSTRE_MSG_MAGIC_V1:
1174 case LUSTRE_MSG_MAGIC_V2:
1175 /* already in host endian */
1176 return msg->lm_flags;
1178 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1183 void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags)
1185 switch (msg->lm_magic) {
1186 case LUSTRE_MSG_MAGIC_V1:
1188 case LUSTRE_MSG_MAGIC_V2:
1189 msg->lm_flags = flags;
1192 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1196 __u32 lustre_msg_get_flags(struct lustre_msg *msg)
1198 switch (msg->lm_magic) {
1199 case LUSTRE_MSG_MAGIC_V1:
1200 return ((struct lustre_msg_v1 *)msg)->lm_flags &
1202 case LUSTRE_MSG_MAGIC_V2: {
1203 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1205 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1208 return pb->pb_flags;
1211 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1212 /* flags might be printed in debug code while message
1218 void lustre_msg_add_flags(struct lustre_msg *msg, int flags)
1220 switch (msg->lm_magic) {
1221 case LUSTRE_MSG_MAGIC_V1:
1222 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1223 MSG_GEN_FLAG_MASK & flags;
1225 case LUSTRE_MSG_MAGIC_V2: {
1226 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1227 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1228 pb->pb_flags |= flags;
1232 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1236 void lustre_msg_set_flags(struct lustre_msg *msg, int flags)
1238 switch (msg->lm_magic) {
1239 case LUSTRE_MSG_MAGIC_V1:
1240 ((struct lustre_msg_v1 *)msg)->lm_flags &= ~MSG_GEN_FLAG_MASK;
1241 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1242 MSG_GEN_FLAG_MASK & flags;
1244 case LUSTRE_MSG_MAGIC_V2: {
1245 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1246 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1247 pb->pb_flags = flags;
1251 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1255 void lustre_msg_clear_flags(struct lustre_msg *msg, int flags)
1257 switch (msg->lm_magic) {
1258 case LUSTRE_MSG_MAGIC_V1:
1259 ((struct lustre_msg_v1 *)msg)->lm_flags &=
1260 ~(MSG_GEN_FLAG_MASK & flags);
1262 case LUSTRE_MSG_MAGIC_V2: {
1263 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1264 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1265 pb->pb_flags &= ~(MSG_GEN_FLAG_MASK & flags);
1269 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1273 __u32 lustre_msg_get_op_flags(struct lustre_msg *msg)
1275 switch (msg->lm_magic) {
1276 case LUSTRE_MSG_MAGIC_V1:
1277 return ((struct lustre_msg_v1 *)msg)->lm_flags >>
1279 case LUSTRE_MSG_MAGIC_V2: {
1280 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1282 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1285 return pb->pb_op_flags;
1288 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1293 void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags)
1295 switch (msg->lm_magic) {
1296 case LUSTRE_MSG_MAGIC_V1:
1297 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1298 (flags & MSG_GEN_FLAG_MASK) << MSG_OP_FLAG_SHIFT;
1300 case LUSTRE_MSG_MAGIC_V2: {
1301 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1302 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1303 pb->pb_op_flags |= flags;
1307 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1311 void lustre_msg_set_op_flags(struct lustre_msg *msg, int flags)
1313 switch (msg->lm_magic) {
1314 case LUSTRE_MSG_MAGIC_V1:
1315 ((struct lustre_msg_v1 *)msg)->lm_flags &= ~MSG_OP_FLAG_MASK;
1316 ((struct lustre_msg_v1 *)msg)->lm_flags |=
1317 ((flags & MSG_GEN_FLAG_MASK) <<MSG_OP_FLAG_SHIFT);
1319 case LUSTRE_MSG_MAGIC_V2: {
1320 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1321 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1322 pb->pb_op_flags |= flags;
1326 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1330 struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg)
1332 switch (msg->lm_magic) {
1333 case LUSTRE_MSG_MAGIC_V1:
1334 return &((struct lustre_msg_v1 *)msg)->lm_handle;
1335 case LUSTRE_MSG_MAGIC_V2: {
1336 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1338 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1341 return &pb->pb_handle;
1344 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1349 __u32 lustre_msg_get_type(struct lustre_msg *msg)
1351 switch (msg->lm_magic) {
1352 case LUSTRE_MSG_MAGIC_V1:
1353 return ((struct lustre_msg_v1 *)msg)->lm_type;
1354 case LUSTRE_MSG_MAGIC_V2: {
1355 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1357 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1358 return PTL_RPC_MSG_ERR;
1363 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1364 return PTL_RPC_MSG_ERR;
1368 __u32 lustre_msg_get_version(struct lustre_msg *msg)
1370 switch (msg->lm_magic) {
1371 case LUSTRE_MSG_MAGIC_V1:
1372 return ((struct lustre_msg_v1 *)msg)->lm_version;
1373 case LUSTRE_MSG_MAGIC_V2: {
1374 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1376 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1379 return pb->pb_version;
1382 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1387 void lustre_msg_add_version(struct lustre_msg *msg, int version)
1389 switch (msg->lm_magic) {
1390 case LUSTRE_MSG_MAGIC_V1:
1392 case LUSTRE_MSG_MAGIC_V2: {
1393 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1394 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1395 pb->pb_version |= version;
1399 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1403 __u32 lustre_msg_get_opc(struct lustre_msg *msg)
1405 switch (msg->lm_magic) {
1406 case LUSTRE_MSG_MAGIC_V1:
1407 return ((struct lustre_msg_v1 *)msg)->lm_opc;
1408 case LUSTRE_MSG_MAGIC_V2: {
1409 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1411 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1417 CERROR( "incorrect message magic: %08x\n", msg->lm_magic);
1422 __u64 lustre_msg_get_last_xid(struct lustre_msg *msg)
1424 switch (msg->lm_magic) {
1425 case LUSTRE_MSG_MAGIC_V1:
1426 return ((struct lustre_msg_v1 *)msg)->lm_last_xid;
1427 case LUSTRE_MSG_MAGIC_V2: {
1428 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1430 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1433 return pb->pb_last_xid;
1436 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1441 __u64 lustre_msg_get_last_committed(struct lustre_msg *msg)
1443 switch (msg->lm_magic) {
1444 case LUSTRE_MSG_MAGIC_V1:
1445 return ((struct lustre_msg_v1 *)msg)->lm_last_committed;
1446 case LUSTRE_MSG_MAGIC_V2: {
1447 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1449 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1452 return pb->pb_last_committed;
1455 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1460 __u64 *lustre_msg_get_versions(struct lustre_msg *msg)
1462 switch (msg->lm_magic) {
1463 case LUSTRE_MSG_MAGIC_V1:
1465 case LUSTRE_MSG_MAGIC_V2: {
1466 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1468 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1471 #ifdef PTLRPC_INTEROP_1_6
1472 if (lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF) < sizeof (*pb))
1475 return pb->pb_pre_versions;
1478 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1483 __u64 lustre_msg_get_transno(struct lustre_msg *msg)
1485 switch (msg->lm_magic) {
1486 case LUSTRE_MSG_MAGIC_V1:
1487 return ((struct lustre_msg_v1 *)msg)->lm_transno;
1488 case LUSTRE_MSG_MAGIC_V2: {
1489 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1491 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1494 return pb->pb_transno;
1497 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1502 int lustre_msg_get_status(struct lustre_msg *msg)
1504 switch (msg->lm_magic) {
1505 case LUSTRE_MSG_MAGIC_V1:
1506 return ((struct lustre_msg_v1 *)msg)->lm_status;
1507 case LUSTRE_MSG_MAGIC_V2: {
1508 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1510 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1513 return pb->pb_status;
1516 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1517 /* status might be printed in debug code while message
1523 __u64 lustre_msg_get_slv(struct lustre_msg *msg)
1525 switch (msg->lm_magic) {
1526 case LUSTRE_MSG_MAGIC_V1:
1528 case LUSTRE_MSG_MAGIC_V2: {
1529 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1531 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1537 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1543 void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv)
1545 switch (msg->lm_magic) {
1546 case LUSTRE_MSG_MAGIC_V1:
1548 case LUSTRE_MSG_MAGIC_V2: {
1549 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1551 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1558 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1563 __u32 lustre_msg_get_limit(struct lustre_msg *msg)
1565 switch (msg->lm_magic) {
1566 case LUSTRE_MSG_MAGIC_V1:
1568 case LUSTRE_MSG_MAGIC_V2: {
1569 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1571 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1574 return pb->pb_limit;
1577 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1583 void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit)
1585 switch (msg->lm_magic) {
1586 case LUSTRE_MSG_MAGIC_V1:
1588 case LUSTRE_MSG_MAGIC_V2: {
1589 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1591 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1594 pb->pb_limit = limit;
1598 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1603 __u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg)
1605 switch (msg->lm_magic) {
1606 case LUSTRE_MSG_MAGIC_V1:
1607 return ((struct lustre_msg_v1 *)msg)->lm_conn_cnt;
1608 case LUSTRE_MSG_MAGIC_V2: {
1609 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1611 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1614 return pb->pb_conn_cnt;
1617 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1622 int lustre_msg_is_v1(struct lustre_msg *msg)
1624 switch (msg->lm_magic) {
1625 case LUSTRE_MSG_MAGIC_V1:
1626 case LUSTRE_MSG_MAGIC_V1_SWABBED:
1633 __u32 lustre_msg_get_magic(struct lustre_msg *msg)
1635 switch (msg->lm_magic) {
1636 case LUSTRE_MSG_MAGIC_V1:
1637 case LUSTRE_MSG_MAGIC_V1_SWABBED:
1638 case LUSTRE_MSG_MAGIC_V2:
1639 case LUSTRE_MSG_MAGIC_V2_SWABBED:
1640 return msg->lm_magic;
1642 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1647 __u32 lustre_msg_get_timeout(struct lustre_msg *msg)
1649 switch (msg->lm_magic) {
1650 case LUSTRE_MSG_MAGIC_V1:
1652 case LUSTRE_MSG_MAGIC_V2: {
1653 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1655 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1658 return pb->pb_timeout;
1661 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1666 __u32 lustre_msg_get_service_time(struct lustre_msg *msg)
1668 switch (msg->lm_magic) {
1669 case LUSTRE_MSG_MAGIC_V1:
1671 case LUSTRE_MSG_MAGIC_V2: {
1672 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1674 CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1677 return pb->pb_service_time;
1680 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1685 __u32 lustre_msg_get_cksum(struct lustre_msg *msg)
1687 switch (msg->lm_magic) {
1688 case LUSTRE_MSG_MAGIC_V1:
1690 case LUSTRE_MSG_MAGIC_V2:
1691 return msg->lm_cksum;
1693 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1699 * the sizeof ptlrpc_body in 1.6 is 88 bytes (64 bytes shorter than current
1700 * size), to be able to interoperate with 1.6 we only calculate checksum
1701 * aginst first 88 bytes of ptlrpc_body.
1703 static const int ptlrpc_body_size_16 = 88;
1705 __u32 lustre_msg_calc_cksum(struct lustre_msg *msg)
1707 switch (msg->lm_magic) {
1708 case LUSTRE_MSG_MAGIC_V1:
1710 case LUSTRE_MSG_MAGIC_V2: {
1711 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1712 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1713 return crc32_le(~(__u32)0, (unsigned char *)pb,
1714 ptlrpc_body_size_16);
1717 CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1722 void lustre_msg_set_handle(struct lustre_msg *msg, struct lustre_handle *handle)
1724 switch (msg->lm_magic) {
1725 case LUSTRE_MSG_MAGIC_V1:
1726 ((struct lustre_msg_v1 *)msg)->lm_handle = *handle;
1728 case LUSTRE_MSG_MAGIC_V2: {
1729 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1730 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1731 pb->pb_handle = *handle;
1735 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1739 void lustre_msg_set_type(struct lustre_msg *msg, __u32 type)
1741 switch (msg->lm_magic) {
1742 case LUSTRE_MSG_MAGIC_V1:
1743 ((struct lustre_msg_v1 *)msg)->lm_type = type;
1745 case LUSTRE_MSG_MAGIC_V2: {
1746 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1747 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1752 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1756 void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc)
1758 switch (msg->lm_magic) {
1759 case LUSTRE_MSG_MAGIC_V1:
1760 ((struct lustre_msg_v1 *)msg)->lm_opc = opc;
1762 case LUSTRE_MSG_MAGIC_V2: {
1763 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1764 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1769 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1773 void lustre_msg_set_last_xid(struct lustre_msg *msg, __u64 last_xid)
1775 switch (msg->lm_magic) {
1776 case LUSTRE_MSG_MAGIC_V1:
1777 ((struct lustre_msg_v1 *)msg)->lm_last_xid = last_xid;
1779 case LUSTRE_MSG_MAGIC_V2: {
1780 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1781 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1782 pb->pb_last_xid = last_xid;
1786 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1790 void lustre_msg_set_last_committed(struct lustre_msg *msg, __u64 last_committed)
1792 switch (msg->lm_magic) {
1793 case LUSTRE_MSG_MAGIC_V1:
1794 ((struct lustre_msg_v1 *)msg)->lm_last_committed=last_committed;
1796 case LUSTRE_MSG_MAGIC_V2: {
1797 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1798 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1799 pb->pb_last_committed = last_committed;
1803 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1807 void lustre_msg_set_versions(struct lustre_msg *msg, __u64 *versions)
1809 switch (msg->lm_magic) {
1810 case LUSTRE_MSG_MAGIC_V1:
1812 case LUSTRE_MSG_MAGIC_V2: {
1813 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1814 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1815 #ifdef PTLRPC_INTEROP_1_6
1816 /* do nothing for old clients */
1817 if (lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF) < sizeof (*pb))
1820 pb->pb_pre_versions[0] = versions[0];
1821 pb->pb_pre_versions[1] = versions[1];
1822 pb->pb_pre_versions[2] = versions[2];
1823 pb->pb_pre_versions[3] = versions[3];
1827 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1832 void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno)
1834 switch (msg->lm_magic) {
1835 case LUSTRE_MSG_MAGIC_V1:
1836 ((struct lustre_msg_v1 *)msg)->lm_transno = transno;
1838 case LUSTRE_MSG_MAGIC_V2: {
1839 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1840 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1841 pb->pb_transno = transno;
1845 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1849 void lustre_msg_set_status(struct lustre_msg *msg, __u32 status)
1851 switch (msg->lm_magic) {
1852 case LUSTRE_MSG_MAGIC_V1:
1853 ((struct lustre_msg_v1 *)msg)->lm_status = status;
1855 case LUSTRE_MSG_MAGIC_V2: {
1856 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1857 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1858 pb->pb_status = status;
1862 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1866 void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt)
1868 switch (msg->lm_magic) {
1869 case LUSTRE_MSG_MAGIC_V1:
1870 ((struct lustre_msg_v1 *)msg)->lm_conn_cnt = conn_cnt;
1872 case LUSTRE_MSG_MAGIC_V2: {
1873 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1874 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1875 pb->pb_conn_cnt = conn_cnt;
1879 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1883 void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout)
1885 switch (msg->lm_magic) {
1886 case LUSTRE_MSG_MAGIC_V1:
1888 case LUSTRE_MSG_MAGIC_V2: {
1889 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1890 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1891 pb->pb_timeout = timeout;
1895 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1899 void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time)
1901 switch (msg->lm_magic) {
1902 case LUSTRE_MSG_MAGIC_V1:
1904 case LUSTRE_MSG_MAGIC_V2: {
1905 struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1906 LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1907 pb->pb_service_time = service_time;
1911 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1915 void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum)
1917 switch (msg->lm_magic) {
1918 case LUSTRE_MSG_MAGIC_V1:
1920 case LUSTRE_MSG_MAGIC_V2:
1921 msg->lm_cksum = cksum;
1924 LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1929 /* byte flipping routines for all wire types declared in
1930 * lustre_idl.h implemented here.
1932 void lustre_swab_ptlrpc_body(struct ptlrpc_body *b, int msgsize)
1934 __swab32s (&b->pb_type);
1935 __swab32s (&b->pb_version);
1936 __swab32s (&b->pb_opc);
1937 __swab32s (&b->pb_status);
1938 __swab64s (&b->pb_last_xid);
1939 __swab64s (&b->pb_last_seen);
1940 __swab64s (&b->pb_last_committed);
1941 __swab64s (&b->pb_transno);
1942 __swab32s (&b->pb_flags);
1943 __swab32s (&b->pb_op_flags);
1944 __swab32s (&b->pb_conn_cnt);
1945 __swab32s (&b->pb_timeout);
1946 __swab32s (&b->pb_service_time);
1947 __swab64s (&b->pb_slv);
1948 __swab32s (&b->pb_limit);
1949 if (msgsize < offsetof(struct ptlrpc_body, pb_pre_versions[4]))
1951 __swab64s (&b->pb_pre_versions[0]);
1952 __swab64s (&b->pb_pre_versions[1]);
1953 __swab64s (&b->pb_pre_versions[2]);
1954 __swab64s (&b->pb_pre_versions[3]);
1955 CLASSERT(offsetof(typeof(*b), pb_padding) != 0);
1958 void lustre_swab_connect(struct obd_connect_data *ocd)
1960 __swab64s(&ocd->ocd_connect_flags);
1961 __swab32s(&ocd->ocd_version);
1962 __swab32s(&ocd->ocd_grant);
1963 __swab32s(&ocd->ocd_index);
1964 __swab32s(&ocd->ocd_brw_size);
1965 __swab64s(&ocd->ocd_ibits_known);
1966 __swab32s(&ocd->ocd_nllu);
1967 __swab32s(&ocd->ocd_nllg);
1968 __swab64s(&ocd->ocd_transno);
1969 __swab32s(&ocd->ocd_group);
1970 __swab32s(&ocd->ocd_cksum_types);
1971 CLASSERT(offsetof(typeof(*ocd), padding1) != 0);
1972 CLASSERT(offsetof(typeof(*ocd), padding2) != 0);
1975 void lustre_swab_obdo (struct obdo *o)
1977 __swab64s (&o->o_valid);
1978 __swab64s (&o->o_id);
1979 __swab64s (&o->o_gr);
1980 __swab64s (&o->o_fid);
1981 __swab64s (&o->o_size);
1982 __swab64s (&o->o_mtime);
1983 __swab64s (&o->o_atime);
1984 __swab64s (&o->o_ctime);
1985 __swab64s (&o->o_blocks);
1986 __swab64s (&o->o_grant);
1987 __swab32s (&o->o_blksize);
1988 __swab32s (&o->o_mode);
1989 __swab32s (&o->o_uid);
1990 __swab32s (&o->o_gid);
1991 __swab32s (&o->o_flags);
1992 __swab32s (&o->o_nlink);
1993 __swab32s (&o->o_generation);
1994 __swab32s (&o->o_misc);
1995 __swab32s (&o->o_easize);
1996 __swab32s (&o->o_mds);
1997 __swab32s (&o->o_stripe_idx);
1998 __swab32s (&o->o_padding_1);
1999 /* o_inline is opaque */
2002 void lustre_swab_obd_statfs (struct obd_statfs *os)
2004 __swab64s (&os->os_type);
2005 __swab64s (&os->os_blocks);
2006 __swab64s (&os->os_bfree);
2007 __swab64s (&os->os_bavail);
2008 __swab64s (&os->os_files);
2009 __swab64s (&os->os_ffree);
2010 /* no need to swab os_fsid */
2011 __swab32s (&os->os_bsize);
2012 __swab32s (&os->os_namelen);
2013 __swab64s (&os->os_maxbytes);
2014 __swab32s (&os->os_state);
2015 /* no need to swap os_spare */
2018 void lustre_swab_obd_ioobj (struct obd_ioobj *ioo)
2020 __swab64s (&ioo->ioo_id);
2021 __swab64s (&ioo->ioo_gr);
2022 __swab32s (&ioo->ioo_type);
2023 __swab32s (&ioo->ioo_bufcnt);
2026 void lustre_swab_niobuf_remote (struct niobuf_remote *nbr)
2028 __swab64s (&nbr->offset);
2029 __swab32s (&nbr->len);
2030 __swab32s (&nbr->flags);
2033 void lustre_swab_ost_body (struct ost_body *b)
2035 lustre_swab_obdo (&b->oa);
2038 void lustre_swab_ost_last_id(obd_id *id)
2043 void lustre_swab_ost_lvb(struct ost_lvb *lvb)
2045 __swab64s(&lvb->lvb_size);
2046 __swab64s(&lvb->lvb_mtime);
2047 __swab64s(&lvb->lvb_atime);
2048 __swab64s(&lvb->lvb_ctime);
2049 __swab64s(&lvb->lvb_blocks);
2052 void lustre_swab_mds_status_req (struct mds_status_req *r)
2054 __swab32s (&r->flags);
2055 __swab32s (&r->repbuf);
2058 void lustre_swab_mds_body (struct mds_body *b)
2060 lustre_swab_ll_fid (&b->fid1);
2061 lustre_swab_ll_fid (&b->fid2);
2062 /* handle is opaque */
2063 __swab64s (&b->valid);
2064 __swab64s (&b->size);
2065 __swab64s (&b->mtime);
2066 __swab64s (&b->atime);
2067 __swab64s (&b->ctime);
2068 __swab64s (&b->blocks);
2069 __swab64s (&b->io_epoch);
2070 __swab64s (&b->ino);
2071 __swab32s (&b->fsuid);
2072 __swab32s (&b->fsgid);
2073 __swab32s (&b->capability);
2074 __swab32s (&b->mode);
2075 __swab32s (&b->uid);
2076 __swab32s (&b->gid);
2077 __swab32s (&b->flags);
2078 __swab32s (&b->rdev);
2079 __swab32s (&b->nlink);
2080 __swab32s (&b->generation);
2081 __swab32s (&b->suppgid);
2082 __swab32s (&b->eadatasize);
2083 __swab32s (&b->aclsize);
2084 __swab32s (&b->max_mdsize);
2085 __swab32s (&b->max_cookiesize);
2086 __swab32s (&b->padding_4);
2089 void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
2092 __swab32s(&mti->mti_lustre_ver);
2093 __swab32s(&mti->mti_stripe_index);
2094 __swab32s(&mti->mti_config_ver);
2095 __swab32s(&mti->mti_flags);
2096 __swab32s(&mti->mti_nid_count);
2097 CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
2098 for (i = 0; i < MTI_NIDS_MAX; i++)
2099 __swab64s(&mti->mti_nids[i]);
2102 static void lustre_swab_obd_dqinfo (struct obd_dqinfo *i)
2104 __swab64s (&i->dqi_bgrace);
2105 __swab64s (&i->dqi_igrace);
2106 __swab32s (&i->dqi_flags);
2107 __swab32s (&i->dqi_valid);
2110 static void lustre_swab_obd_dqblk (struct obd_dqblk *b)
2112 __swab64s (&b->dqb_ihardlimit);
2113 __swab64s (&b->dqb_isoftlimit);
2114 __swab64s (&b->dqb_curinodes);
2115 __swab64s (&b->dqb_bhardlimit);
2116 __swab64s (&b->dqb_bsoftlimit);
2117 __swab64s (&b->dqb_curspace);
2118 __swab64s (&b->dqb_btime);
2119 __swab64s (&b->dqb_itime);
2120 __swab32s (&b->dqb_valid);
2121 CLASSERT(offsetof(typeof(*b), padding) != 0);
2124 void lustre_swab_obd_quotactl (struct obd_quotactl *q)
2126 __swab32s (&q->qc_cmd);
2127 __swab32s (&q->qc_type);
2128 __swab32s (&q->qc_id);
2129 __swab32s (&q->qc_stat);
2130 lustre_swab_obd_dqinfo (&q->qc_dqinfo);
2131 lustre_swab_obd_dqblk (&q->qc_dqblk);
2134 void lustre_swab_quota_adjust_qunit (struct quota_adjust_qunit *q)
2136 __swab32s (&q->qaq_flags);
2137 __swab32s (&q->qaq_id);
2138 __swab64s (&q->qaq_bunit_sz);
2139 __swab64s (&q->qaq_iunit_sz);
2142 void lustre_swab_mds_rec_setattr (struct mds_rec_setattr *sa)
2144 __swab32s (&sa->sa_opcode);
2145 __swab32s (&sa->sa_fsuid);
2146 __swab32s (&sa->sa_fsgid);
2147 __swab32s (&sa->sa_cap);
2148 __swab32s (&sa->sa_suppgid);
2149 __swab32s (&sa->sa_mode);
2150 lustre_swab_ll_fid (&sa->sa_fid);
2151 __swab64s (&sa->sa_valid);
2152 __swab64s (&sa->sa_size);
2153 __swab64s (&sa->sa_mtime);
2154 __swab64s (&sa->sa_atime);
2155 __swab64s (&sa->sa_ctime);
2156 __swab32s (&sa->sa_uid);
2157 __swab32s (&sa->sa_gid);
2158 __swab32s (&sa->sa_attr_flags);
2159 CLASSERT(offsetof(typeof(*sa), sa_padding) != 0);
2162 void lustre_swab_mds_rec_join (struct mds_rec_join *jr)
2164 __swab64s(&jr->jr_headsize);
2165 lustre_swab_ll_fid(&jr->jr_fid);
2168 void lustre_swab_mds_rec_create (struct mds_rec_create *cr)
2170 __swab32s (&cr->cr_opcode);
2171 __swab32s (&cr->cr_fsuid);
2172 __swab32s (&cr->cr_fsgid);
2173 __swab32s (&cr->cr_cap);
2174 __swab32s (&cr->cr_flags); /* for use with open */
2175 __swab32s (&cr->cr_mode);
2176 lustre_swab_ll_fid (&cr->cr_fid);
2177 lustre_swab_ll_fid (&cr->cr_replayfid);
2178 __swab64s (&cr->cr_time);
2179 __swab64s (&cr->cr_rdev);
2180 __swab32s (&cr->cr_suppgid);
2181 CLASSERT(offsetof(typeof(*cr), cr_padding_1) != 0);
2182 CLASSERT(offsetof(typeof(*cr), cr_padding_2) != 0);
2183 CLASSERT(offsetof(typeof(*cr), cr_padding_3) != 0);
2184 CLASSERT(offsetof(typeof(*cr), cr_padding_4) != 0);
2185 CLASSERT(offsetof(typeof(*cr), cr_padding_5) != 0);
2188 void lustre_swab_mds_rec_link (struct mds_rec_link *lk)
2190 __swab32s (&lk->lk_opcode);
2191 __swab32s (&lk->lk_fsuid);
2192 __swab32s (&lk->lk_fsgid);
2193 __swab32s (&lk->lk_cap);
2194 __swab32s (&lk->lk_suppgid1);
2195 __swab32s (&lk->lk_suppgid2);
2196 lustre_swab_ll_fid (&lk->lk_fid1);
2197 lustre_swab_ll_fid (&lk->lk_fid2);
2198 __swab64s (&lk->lk_time);
2199 CLASSERT(offsetof(typeof(*lk), lk_padding_1) != 0);
2200 CLASSERT(offsetof(typeof(*lk), lk_padding_2) != 0);
2201 CLASSERT(offsetof(typeof(*lk), lk_padding_3) != 0);
2202 CLASSERT(offsetof(typeof(*lk), lk_padding_4) != 0);
2205 void lustre_swab_mds_rec_unlink (struct mds_rec_unlink *ul)
2207 __swab32s (&ul->ul_opcode);
2208 __swab32s (&ul->ul_fsuid);
2209 __swab32s (&ul->ul_fsgid);
2210 __swab32s (&ul->ul_cap);
2211 __swab32s (&ul->ul_suppgid);
2212 __swab32s (&ul->ul_mode);
2213 lustre_swab_ll_fid (&ul->ul_fid1);
2214 lustre_swab_ll_fid (&ul->ul_fid2);
2215 __swab64s (&ul->ul_time);
2216 CLASSERT(offsetof(typeof(*ul), ul_padding_1) != 0);
2217 CLASSERT(offsetof(typeof(*ul), ul_padding_2) != 0);
2218 CLASSERT(offsetof(typeof(*ul), ul_padding_3) != 0);
2219 CLASSERT(offsetof(typeof(*ul), ul_padding_4) != 0);
2222 void lustre_swab_fiemap_extent(struct ll_fiemap_extent *fm_extent)
2224 __swab64s(&fm_extent->fe_logical);
2225 __swab64s(&fm_extent->fe_physical);
2226 __swab64s(&fm_extent->fe_length);
2227 __swab32s(&fm_extent->fe_flags);
2228 __swab32s(&fm_extent->fe_device);
2231 void lustre_swab_fiemap(struct ll_user_fiemap *fiemap)
2235 __swab64s(&fiemap->fm_start);
2236 __swab64s(&fiemap->fm_length);
2237 __swab32s(&fiemap->fm_flags);
2238 __swab32s(&fiemap->fm_mapped_extents);
2239 __swab32s(&fiemap->fm_extent_count);
2240 __swab32s(&fiemap->fm_reserved);
2242 for (i = 0; i < fiemap->fm_mapped_extents; i++)
2243 lustre_swab_fiemap_extent(&fiemap->fm_extents[i]);
2246 void lustre_swab_mds_rec_rename (struct mds_rec_rename *rn)
2248 __swab32s (&rn->rn_opcode);
2249 __swab32s (&rn->rn_fsuid);
2250 __swab32s (&rn->rn_fsgid);
2251 __swab32s (&rn->rn_cap);
2252 __swab32s (&rn->rn_suppgid1);
2253 __swab32s (&rn->rn_suppgid2);
2254 lustre_swab_ll_fid (&rn->rn_fid1);
2255 lustre_swab_ll_fid (&rn->rn_fid2);
2256 __swab64s (&rn->rn_time);
2257 CLASSERT(offsetof(typeof(*rn), rn_padding_1) != 0);
2258 CLASSERT(offsetof(typeof(*rn), rn_padding_2) != 0);
2259 CLASSERT(offsetof(typeof(*rn), rn_padding_3) != 0);
2260 CLASSERT(offsetof(typeof(*rn), rn_padding_4) != 0);
2263 void lustre_swab_lov_desc (struct lov_desc *ld)
2265 __swab32s (&ld->ld_tgt_count);
2266 __swab32s (&ld->ld_active_tgt_count);
2267 __swab32s (&ld->ld_default_stripe_count);
2268 __swab32s (&ld->ld_pattern);
2269 __swab64s (&ld->ld_default_stripe_size);
2270 __swab64s (&ld->ld_default_stripe_offset);
2271 __swab32s (&ld->ld_qos_maxage);
2272 /* uuid endian insensitive */
2276 static void lustre_swab_lov_user_md_common(struct lov_user_md_v1 *lum)
2279 __swab32s(&lum->lmm_magic);
2280 __swab32s(&lum->lmm_pattern);
2281 __swab64s(&lum->lmm_object_id);
2282 __swab64s(&lum->lmm_object_gr);
2283 __swab32s(&lum->lmm_stripe_size);
2284 __swab16s(&lum->lmm_stripe_count);
2285 __swab16s(&lum->lmm_stripe_offset);
2289 void lustre_swab_lov_user_md_v1(struct lov_user_md_v1 *lum)
2292 CDEBUG(D_IOCTL, "swabbing lov_user_md v1\n");
2293 lustre_swab_lov_user_md_common(lum);
2297 void lustre_swab_lov_user_md_v3(struct lov_user_md_v3 *lum)
2300 CDEBUG(D_IOCTL, "swabbing lov_user_md v3\n");
2301 lustre_swab_lov_user_md_common((struct lov_user_md_v1 *)lum);
2302 /* lmm_pool_name nothing to do with char */
2306 void lustre_swab_lov_mds_md(struct lov_mds_md *lmm)
2309 CDEBUG(D_IOCTL, "swabbing lov_mds_md\n");
2310 __swab32s(&lmm->lmm_magic);
2311 __swab32s(&lmm->lmm_pattern);
2312 __swab64s(&lmm->lmm_object_id);
2313 __swab64s(&lmm->lmm_object_gr);
2314 __swab32s(&lmm->lmm_stripe_size);
2315 __swab32s(&lmm->lmm_stripe_count);
2319 void lustre_swab_lov_user_md_join(struct lov_user_md_join *lumj)
2322 CDEBUG(D_IOCTL, "swabbing lov_user_md_join\n");
2323 __swab32s(&lumj->lmm_magic);
2324 __swab32s(&lumj->lmm_pattern);
2325 __swab64s(&lumj->lmm_object_id);
2326 __swab64s(&lumj->lmm_object_gr);
2327 __swab32s(&lumj->lmm_stripe_size);
2328 __swab32s(&lumj->lmm_stripe_count);
2329 __swab32s(&lumj->lmm_extent_count);
2333 void lustre_swab_lov_user_md_objects(struct lov_user_ost_data *lod,
2338 for (i = 0; i < stripe_count; i++) {
2339 __swab64s(&(lod[i].l_object_id));
2340 __swab64s(&(lod[i].l_object_gr));
2341 __swab32s(&(lod[i].l_ost_gen));
2342 __swab32s(&(lod[i].l_ost_idx));
2347 void lustre_swab_ldlm_res_id (struct ldlm_res_id *id)
2351 for (i = 0; i < RES_NAME_SIZE; i++)
2352 __swab64s (&id->name[i]);
2355 void lustre_swab_ldlm_policy_data (ldlm_policy_data_t *d)
2357 /* the lock data is a union and the first two fields are always an
2358 * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
2359 * data the same way. */
2360 __swab64s(&d->l_extent.start);
2361 __swab64s(&d->l_extent.end);
2362 __swab64s(&d->l_extent.gid);
2363 __swab32s(&d->l_flock.pid);
2366 void lustre_swab_ldlm_intent (struct ldlm_intent *i)
2368 __swab64s (&i->opc);
2371 void lustre_swab_ldlm_resource_desc (struct ldlm_resource_desc *r)
2373 __swab32s (&r->lr_type);
2374 CLASSERT(offsetof(typeof(*r), lr_padding) != 0);
2375 lustre_swab_ldlm_res_id (&r->lr_name);
2378 void lustre_swab_ldlm_lock_desc (struct ldlm_lock_desc *l)
2380 lustre_swab_ldlm_resource_desc (&l->l_resource);
2381 __swab32s (&l->l_req_mode);
2382 __swab32s (&l->l_granted_mode);
2383 lustre_swab_ldlm_policy_data (&l->l_policy_data);
2386 void lustre_swab_ldlm_request (struct ldlm_request *rq)
2388 __swab32s (&rq->lock_flags);
2389 lustre_swab_ldlm_lock_desc (&rq->lock_desc);
2390 __swab32s (&rq->lock_count);
2391 /* lock_handle[] opaque */
2394 void lustre_swab_ldlm_reply (struct ldlm_reply *r)
2396 __swab32s (&r->lock_flags);
2397 CLASSERT(offsetof(typeof(*r), lock_padding) != 0);
2398 lustre_swab_ldlm_lock_desc (&r->lock_desc);
2399 /* lock_handle opaque */
2400 __swab64s (&r->lock_policy_res1);
2401 __swab64s (&r->lock_policy_res2);
2404 /* no one calls this */
2405 int llog_log_swabbed(struct llog_log_hdr *hdr)
2407 if (hdr->llh_hdr.lrh_type == __swab32(LLOG_HDR_MAGIC))
2409 if (hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC)
2414 void lustre_swab_qdata(struct qunit_data *d)
2416 __swab32s (&d->qd_id);
2417 __swab32s (&d->qd_flags);
2418 __swab64s (&d->qd_count);
2419 __swab64s (&d->qd_qunit);
2422 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2423 void lustre_swab_qdata_old2(struct qunit_data_old2 *d)
2425 __swab32s (&d->qd_id);
2426 __swab32s (&d->qd_flags);
2427 __swab64s (&d->qd_count);
2430 #warning "remove quota code above for format absolete in new release"
2435 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2436 void qdata_v2_to_v3(struct qunit_data_old2 *d,
2437 struct qunit_data *qdata)
2442 qdata->qd_id = d->qd_id;
2443 qdata->qd_flags = d->qd_flags;
2444 qdata->qd_count = d->qd_count;
2447 struct qunit_data_old2 *qdata_v3_to_v2(struct qunit_data *d)
2449 struct qunit_data tmp;
2450 struct qunit_data_old2 *ret;
2457 ret = (struct qunit_data_old2 *)d;
2458 ret->qd_id = tmp.qd_id;
2459 ret->qd_flags = tmp.qd_flags & LQUOTA_QUNIT_FLAGS;
2460 ret->qd_count = tmp.qd_count;
2464 #warning "remove quota code above for format absolete in new release"
2467 /* got qdata from request(req/rep) */
2468 int quota_get_qdata(void *request, struct qunit_data *qdata,
2469 int is_req, int is_exp)
2471 struct ptlrpc_request *req = (struct ptlrpc_request *)request;
2472 struct qunit_data *new;
2473 struct qunit_data_old2 *old2;
2474 int size2 = sizeof(struct qunit_data_old2);
2475 __u64 flags = is_exp ? req->rq_export->exp_connect_flags :
2476 req->rq_import->imp_connect_data.ocd_connect_flags;
2482 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2483 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS))
2484 goto without_change_qs;
2486 #warning "remove quota code above for format absolete in new release"
2489 /* support for quota64 and change_qs */
2490 if (flags & OBD_CONNECT_CHANGE_QS) {
2491 if (!(flags & OBD_CONNECT_QUOTA64)) {
2492 CDEBUG(D_ERROR, "Wire protocol for qunit is broken!\n");
2495 if (is_req == QUOTA_REQUEST)
2496 new = lustre_swab_reqbuf(req, REQ_REC_OFF,
2497 sizeof(struct qunit_data),
2500 new = lustre_swab_repbuf(req, REPLY_REC_OFF,
2501 sizeof(struct qunit_data),
2504 GOTO(out, rc = -EPROTO);
2506 QDATA_SET_CHANGE_QS(qdata);
2509 QDATA_CLR_CHANGE_QS(qdata);
2512 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2514 /* only support for quota64 */
2515 if (flags & OBD_CONNECT_QUOTA64) {
2517 if (is_req == QUOTA_REQUEST)
2518 old2 = lustre_swab_reqbuf(req, REQ_REC_OFF, size2,
2519 lustre_swab_qdata_old2);
2521 old2 = lustre_swab_repbuf(req, REPLY_REC_OFF, size2,
2522 lustre_swab_qdata_old2);
2524 GOTO(out, rc = -EPROTO);
2525 qdata_v2_to_v3(old2, qdata);
2530 #warning "remove quota code above for format absolete in new release"
2535 EXPORT_SYMBOL(quota_get_qdata);
2537 /* copy qdata to request(req/rep) */
2538 int quota_copy_qdata(void *request, struct qunit_data *qdata,
2539 int is_req, int is_exp)
2541 struct ptlrpc_request *req = (struct ptlrpc_request *)request;
2543 struct qunit_data_old2 *old2;
2544 __u64 flags = is_exp ? req->rq_export->exp_connect_flags :
2545 req->rq_import->imp_connect_data.ocd_connect_flags;
2551 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2552 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_WITHOUT_CHANGE_QS))
2553 goto without_change_qs;
2555 #warning "remove quota code above for format absolete in new release"
2558 /* support for quota64 and change_qs */
2559 if (flags & OBD_CONNECT_CHANGE_QS) {
2560 if (!(flags & OBD_CONNECT_QUOTA64)) {
2561 CERROR("Wire protocol for qunit is broken!\n");
2564 if (is_req == QUOTA_REQUEST)
2565 target = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
2566 sizeof(struct qunit_data));
2568 target = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
2569 sizeof(struct qunit_data));
2571 GOTO(out, rc = -EPROTO);
2572 memcpy(target, qdata, sizeof(*qdata));
2576 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(1, 9, 0, 0)
2578 /* only support for quota64 */
2579 if (flags & OBD_CONNECT_QUOTA64) {
2580 if (is_req == QUOTA_REQUEST)
2581 target = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
2582 sizeof(struct qunit_data_old2));
2584 target = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
2585 sizeof(struct qunit_data_old2));
2587 GOTO(out, rc = -EPROTO);
2588 old2 = qdata_v3_to_v2(qdata);
2589 memcpy(target, old2, sizeof(*old2));
2593 #warning "remove quota code above for format absolete in new release"
2598 EXPORT_SYMBOL(quota_copy_qdata);
2600 int quota_get_qunit_data_size(__u64 flag)
2604 if (flag & OBD_CONNECT_CHANGE_QS)
2605 size = sizeof(struct qunit_data);
2607 size = sizeof(struct qunit_data_old2);
2611 EXPORT_SYMBOL(quota_get_qunit_data_size);
2612 #endif /* __KERNEL__ */
2614 static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2616 LASSERT(req->rq_reqmsg);
2618 switch (req->rq_reqmsg->lm_magic) {
2619 case LUSTRE_MSG_MAGIC_V1:
2621 case LUSTRE_MSG_MAGIC_V2:
2622 return lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
2624 CERROR("incorrect message magic: %08x\n",
2625 req->rq_reqmsg->lm_magic);
2630 static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2632 LASSERT(req->rq_repmsg);
2634 switch (req->rq_repmsg->lm_magic) {
2635 case LUSTRE_MSG_MAGIC_V1:
2637 case LUSTRE_MSG_MAGIC_V2:
2638 return lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
2640 CERROR("incorrect message magic: %08x\n",
2641 req->rq_repmsg->lm_magic);
2646 void _debug_req(struct ptlrpc_request *req, __u32 mask,
2647 struct libcfs_debug_msg_data *data, const char *fmt, ... )
2656 /* Caller is responsible holding a reference on the request */
2657 LASSERT(req && atomic_read(&req->rq_refcount) > 0);
2659 if (req->rq_reqmsg &&
2660 (!lustre_msg_need_swab(req->rq_reqmsg) ||
2661 (lustre_req_need_swab(req) &&
2662 lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF)))) {
2663 opc = lustre_msg_get_opc(req->rq_reqmsg);
2664 req_fl = lustre_msg_get_flags(req->rq_reqmsg);
2667 if (req->rq_repmsg &&
2668 (!lustre_msg_need_swab(req->rq_repmsg) ||
2669 (lustre_rep_need_swab(req) &&
2670 lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF)))) {
2671 rep_fl = lustre_msg_get_flags(req->rq_repmsg);
2672 rep_status = lustre_msg_get_status(req->rq_repmsg);
2675 va_start(args, fmt);
2676 libcfs_debug_vmsg2(data->msg_cdls, data->msg_subsys, mask,
2677 data->msg_file, data->msg_fn, data->msg_line, fmt, args,
2678 " req@%p x"LPD64"/t"LPD64" o%d->%s@%s:%d/%d lens %d/%d e %d "
2679 "to %d dl %ld ref %d fl "REQ_FLAGS_FMT"/%x/%x rc %d/%d\n",
2680 req, req->rq_xid, req->rq_transno, opc,
2681 req->rq_import ? obd2cli_tgt(req->rq_import->imp_obd) :
2683 (char*)req->rq_export->exp_client_uuid.uuid : "<?>",
2685 (char *)req->rq_import->imp_connection->c_remote_uuid.uuid :
2687 (char *)req->rq_export->exp_connection->c_remote_uuid.uuid :
2688 "<?>", req->rq_request_portal, req->rq_reply_portal,
2689 req->rq_reqlen, req->rq_replen,
2690 req->rq_early_count, !!req->rq_timeout, req->rq_deadline,
2691 atomic_read(&req->rq_refcount), DEBUG_REQ_FLAGS(req),
2692 req_fl, rep_fl, req->rq_status, rep_status);
2697 EXPORT_SYMBOL(_debug_req);