pack_generic.c

来自「lustre 1.6.5 source code」· C语言 代码 · 共 1,984 行 · 第 1/5 页

C
1,984
字号
                for (i = 0; i < m->lm_bufcount; i++)                        CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);                return -EINVAL;        }        return 0;}int lustre_unpack_msg(struct lustre_msg *m, int len){        int required_len, rc;        ENTRY;        /* We can provide a slightly better error log, if we check the         * message magic and version first.  In the future, struct         * lustre_msg may grow, and we'd like to log a version mismatch,         * rather than a short message.         *         */        required_len = offsetof(struct lustre_msg, lm_magic) +                       sizeof(m->lm_magic);        if (len < required_len) {                /* can't even look inside the message */                CERROR("message length %d too small for magic/version check\n",                       len);                RETURN(-EINVAL);        }        switch (m->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:        case LUSTRE_MSG_MAGIC_V1_SWABBED:                rc = lustre_unpack_msg_v1(m, len);                break;        case LUSTRE_MSG_MAGIC_V2:        case LUSTRE_MSG_MAGIC_V2_SWABBED:                rc = lustre_unpack_msg_v2(m, len);                break;        default:                CERROR("bad lustre msg magic: %#08X\n", m->lm_magic);                return -EINVAL;        }        RETURN(rc);}static inline int lustre_unpack_ptlrpc_body_v2(struct lustre_msg_v2 *m,                                               int offset){        struct ptlrpc_body *pb;        pb = lustre_msg_buf_v2(m, offset, sizeof(*pb));        if (!pb) {                CERROR("error unpacking ptlrpc body\n");                return -EFAULT;        }        if (lustre_msg_swabbed(m))                lustre_swab_ptlrpc_body(pb);        if ((pb->pb_version & ~LUSTRE_VERSION_MASK) != PTLRPC_MSG_VERSION) {                 CERROR("wrong lustre_msg version %08x\n", pb->pb_version);                 return -EINVAL;        }        return 0;}int lustre_unpack_req_ptlrpc_body(struct ptlrpc_request *req, int offset){        switch (req->rq_reqmsg->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:        case LUSTRE_MSG_MAGIC_V1_SWABBED:                return 0;        case LUSTRE_MSG_MAGIC_V2:        case LUSTRE_MSG_MAGIC_V2_SWABBED:                lustre_set_req_swabbed(req, offset);                return lustre_unpack_ptlrpc_body_v2(req->rq_reqmsg, offset);        default:                CERROR("bad lustre msg magic: %#08X\n",                       req->rq_reqmsg->lm_magic);                return -EINVAL;        }}int lustre_unpack_rep_ptlrpc_body(struct ptlrpc_request *req, int offset){        switch (req->rq_repmsg->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:        case LUSTRE_MSG_MAGIC_V1_SWABBED:                return 0;        case LUSTRE_MSG_MAGIC_V2:        case LUSTRE_MSG_MAGIC_V2_SWABBED:                lustre_set_rep_swabbed(req, offset);                return lustre_unpack_ptlrpc_body_v2(req->rq_repmsg, offset);        default:                CERROR("bad lustre msg magic: %#08X\n",                       req->rq_repmsg->lm_magic);                return -EINVAL;        }}static inline int lustre_msg_buflen_v1(void *msg, int n){        struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;        LASSERT(n >= 0);        if (n >= m->lm_bufcount)                return 0;        return m->lm_buflens[n];}static inline int lustre_msg_buflen_v2(struct lustre_msg_v2 *m, int n){        if (n >= m->lm_bufcount)                return 0;        return m->lm_buflens[n];}/** * lustre_msg_buflen - return the length of buffer @n in message @m * @m - lustre_msg (request or reply) to look at * @n - message index (base 0) * * returns zero for non-existent message indices */int lustre_msg_buflen(struct lustre_msg *m, int n){        switch (m->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:        case LUSTRE_MSG_MAGIC_V1_SWABBED:                return lustre_msg_buflen_v1(m, n - 1);        case LUSTRE_MSG_MAGIC_V2:        case LUSTRE_MSG_MAGIC_V2_SWABBED:                return lustre_msg_buflen_v2(m, n);        default:                CERROR("incorrect message magic: %08x\n", m->lm_magic);                return -EINVAL;        }}EXPORT_SYMBOL(lustre_msg_buflen);static inline void lustre_msg_set_buflen_v1(void *msg, int n, int len){        struct lustre_msg_v1 *m = (struct lustre_msg_v1 *)msg;        LASSERT(n >= 0);        if (n >= m->lm_bufcount)                LBUG();        m->lm_buflens[n] = len;}static inline voidlustre_msg_set_buflen_v2(struct lustre_msg_v2 *m, int n, int len){        if (n >= m->lm_bufcount)                LBUG();        m->lm_buflens[n] = len;}void lustre_msg_set_buflen(struct lustre_msg *m, int n, int len){        switch (m->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:                lustre_msg_set_buflen_v1(m, n - 1, len);                return;        case LUSTRE_MSG_MAGIC_V2:                lustre_msg_set_buflen_v2(m, n, len);                return;        default:                LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);        }}EXPORT_SYMBOL(lustre_msg_set_buflen);/* NB return the bufcount for lustre_msg_v2 format, so if message is packed * in V1 format, the result is one bigger. (add struct ptlrpc_body). */int lustre_msg_bufcount(struct lustre_msg *m){        switch (m->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:        case LUSTRE_MSG_MAGIC_V1_SWABBED:                return ((struct lustre_msg_v1 *)m)->lm_bufcount + 1;        case LUSTRE_MSG_MAGIC_V2:        case LUSTRE_MSG_MAGIC_V2_SWABBED:                return m->lm_bufcount;        default:                CERROR("incorrect message magic: %08x\n", m->lm_magic);                return -EINVAL;        }}EXPORT_SYMBOL(lustre_msg_bufcount);char *lustre_msg_string(struct lustre_msg *m, int index, int max_len){        /* max_len == 0 means the string should fill the buffer */        char *str;        int slen, blen;        switch (m->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:        case LUSTRE_MSG_MAGIC_V1_SWABBED:                str = lustre_msg_buf_v1(m, index - 1, 0);                blen = lustre_msg_buflen_v1(m, index - 1);                break;        case LUSTRE_MSG_MAGIC_V2:        case LUSTRE_MSG_MAGIC_V2_SWABBED:                str = lustre_msg_buf_v2(m, index, 0);                blen = lustre_msg_buflen_v2(m, index);                break;        default:                LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);        }        if (str == NULL) {                CERROR ("can't unpack string in msg %p buffer[%d]\n", m, index);                return NULL;        }        slen = strnlen(str, blen);        if (slen == blen) {                     /* not NULL terminated */                CERROR("can't unpack non-NULL terminated string in "                        "msg %p buffer[%d] len %d\n", m, index, blen);                return NULL;        }        if (max_len == 0) {                if (slen != blen - 1) {                        CERROR("can't unpack short string in msg %p "                               "buffer[%d] len %d: strlen %d\n",                               m, index, blen, slen);                        return NULL;                }        } else if (slen > max_len) {                CERROR("can't unpack oversized string in msg %p "                       "buffer[%d] len %d strlen %d: max %d expected\n",                       m, index, blen, slen, max_len);                return NULL;        }        return str;}/* Wrap up the normal fixed length cases */void *lustre_swab_buf(struct lustre_msg *msg, int index, int min_size,                      void *swabber){        void *ptr = NULL;        switch (msg->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:        case LUSTRE_MSG_MAGIC_V1_SWABBED:                ptr = lustre_msg_buf_v1(msg, index - 1, min_size);                break;        case LUSTRE_MSG_MAGIC_V2:        case LUSTRE_MSG_MAGIC_V2_SWABBED:                ptr = lustre_msg_buf_v2(msg, index, min_size);                break;        default:                CERROR("incorrect message magic: %08x\n", msg->lm_magic);        }        if (ptr == NULL)                return NULL;        if (swabber != NULL && lustre_msg_swabbed(msg))                ((void (*)(void *))swabber)(ptr);        return ptr;}void *lustre_swab_reqbuf(struct ptlrpc_request *req, int index, int min_size,                         void *swabber){        lustre_set_req_swabbed(req, index);        return lustre_swab_buf(req->rq_reqmsg, index, min_size, swabber);}void *lustre_swab_repbuf(struct ptlrpc_request *req, int index, int min_size,                         void *swabber){        lustre_set_rep_swabbed(req, index);        return lustre_swab_buf(req->rq_repmsg, index, min_size, swabber);}__u32 lustre_msghdr_get_flags(struct lustre_msg *msg){        switch (msg->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:        case LUSTRE_MSG_MAGIC_V1_SWABBED:                return 0;        case LUSTRE_MSG_MAGIC_V2:        case LUSTRE_MSG_MAGIC_V2_SWABBED:                /* already in host endian */                return msg->lm_flags;        default:                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);                return 0;        }}void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags){        switch (msg->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:                return;        case LUSTRE_MSG_MAGIC_V2:                msg->lm_flags = flags;                return;        default:                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);        }}__u32 lustre_msg_get_flags(struct lustre_msg *msg){        switch (msg->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:        case LUSTRE_MSG_MAGIC_V1_SWABBED:                return ((struct lustre_msg_v1 *)msg)->lm_flags &                       MSG_GEN_FLAG_MASK;        case LUSTRE_MSG_MAGIC_V2:        case LUSTRE_MSG_MAGIC_V2_SWABBED: {                struct ptlrpc_body *pb;                pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));                if (!pb) {                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);                        return 0;                }                return pb->pb_flags;        }        default:                /* flags might be printed in debug code while message                 * uninitialized */                return 0;        }}void lustre_msg_add_flags(struct lustre_msg *msg, int flags){        switch (msg->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:                ((struct lustre_msg_v1 *)msg)->lm_flags |=                                        MSG_GEN_FLAG_MASK & flags;                return;        case LUSTRE_MSG_MAGIC_V2: {                struct ptlrpc_body *pb;                pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);                pb->pb_flags |= flags;                return;        }        default:                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);        }}void lustre_msg_set_flags(struct lustre_msg *msg, int flags){        switch (msg->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:                ((struct lustre_msg_v1 *)msg)->lm_flags &= ~MSG_GEN_FLAG_MASK;                ((struct lustre_msg_v1 *)msg)->lm_flags |=                                        MSG_GEN_FLAG_MASK & flags;                return;        case LUSTRE_MSG_MAGIC_V2: {                struct ptlrpc_body *pb;                pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);                pb->pb_flags = flags;                return;        }        default:                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);        }}void lustre_msg_clear_flags(struct lustre_msg *msg, int flags){        switch (msg->lm_magic) {        case LUSTRE_MSG_MAGIC_V1:        case LUSTRE_MSG_MAGIC_V1_SWABBED:                ((struct lustre_msg_v1 *)msg)->lm_flags &=                                        ~(MSG_GEN_FLAG_MASK & flags);                return;        case LUSTRE_MSG_MAGIC_V2:        case LUSTRE_MSG_MAGIC_V2_SWABBED: {                struct ptlrpc_body *pb;                pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF, sizeof(*pb));                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?