linux/drivers/staging/lustre/lustre/ptlrpc/pack_generic.c
<<
>>
Prefs
   1/*
   2 * GPL HEADER START
   3 *
   4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 only,
   8 * as published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful, but
  11 * WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13 * General Public License version 2 for more details (a copy is included
  14 * in the LICENSE file that accompanied this code).
  15 *
  16 * You should have received a copy of the GNU General Public License
  17 * version 2 along with this program; If not, see
  18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  19 *
  20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  21 * CA 95054 USA or visit www.sun.com if you need additional information or
  22 * have any questions.
  23 *
  24 * GPL HEADER END
  25 */
  26/*
  27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
  28 * Use is subject to license terms.
  29 *
  30 * Copyright (c) 2011, 2012, Intel Corporation.
  31 */
  32/*
  33 * This file is part of Lustre, http://www.lustre.org/
  34 * Lustre is a trademark of Sun Microsystems, Inc.
  35 *
  36 * lustre/ptlrpc/pack_generic.c
  37 *
  38 * (Un)packing of OST requests
  39 *
  40 * Author: Peter J. Braam <braam@clusterfs.com>
  41 * Author: Phil Schwan <phil@clusterfs.com>
  42 * Author: Eric Barton <eeb@clusterfs.com>
  43 */
  44
  45#define DEBUG_SUBSYSTEM S_RPC
  46
  47#include "../../include/linux/libcfs/libcfs.h"
  48
  49#include "../include/obd_support.h"
  50#include "../include/obd_class.h"
  51#include "../include/lustre_net.h"
  52#include "../include/obd_cksum.h"
  53#include "../include/lustre/ll_fiemap.h"
  54
  55#include "ptlrpc_internal.h"
  56
  57static inline int lustre_msg_hdr_size_v2(int count)
  58{
  59        return cfs_size_round(offsetof(struct lustre_msg_v2,
  60                                       lm_buflens[count]));
  61}
  62
  63int lustre_msg_hdr_size(__u32 magic, int count)
  64{
  65        switch (magic) {
  66        case LUSTRE_MSG_MAGIC_V2:
  67                return lustre_msg_hdr_size_v2(count);
  68        default:
  69                LASSERTF(0, "incorrect message magic: %08x\n", magic);
  70                return -EINVAL;
  71        }
  72}
  73EXPORT_SYMBOL(lustre_msg_hdr_size);
  74
  75void ptlrpc_buf_set_swabbed(struct ptlrpc_request *req, const int inout,
  76                            int index)
  77{
  78        if (inout)
  79                lustre_set_req_swabbed(req, index);
  80        else
  81                lustre_set_rep_swabbed(req, index);
  82}
  83EXPORT_SYMBOL(ptlrpc_buf_set_swabbed);
  84
  85int ptlrpc_buf_need_swab(struct ptlrpc_request *req, const int inout,
  86                         int index)
  87{
  88        if (inout)
  89                return (ptlrpc_req_need_swab(req) &&
  90                        !lustre_req_swabbed(req, index));
  91        else
  92                return (ptlrpc_rep_need_swab(req) &&
  93                        !lustre_rep_swabbed(req, index));
  94}
  95EXPORT_SYMBOL(ptlrpc_buf_need_swab);
  96
  97/* early reply size */
  98int lustre_msg_early_size(void)
  99{
 100        static int size;
 101
 102        if (!size) {
 103                /* Always reply old ptlrpc_body_v2 to keep interoperability
 104                 * with the old client (< 2.3) which doesn't have pb_jobid
 105                 * in the ptlrpc_body.
 106                 *
 107                 * XXX Remove this whenever we drop interoperability with such
 108                 *     client.
 109                 */
 110                __u32 pblen = sizeof(struct ptlrpc_body_v2);
 111
 112                size = lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, &pblen);
 113        }
 114        return size;
 115}
 116EXPORT_SYMBOL(lustre_msg_early_size);
 117
 118int lustre_msg_size_v2(int count, __u32 *lengths)
 119{
 120        int size;
 121        int i;
 122
 123        size = lustre_msg_hdr_size_v2(count);
 124        for (i = 0; i < count; i++)
 125                size += cfs_size_round(lengths[i]);
 126
 127        return size;
 128}
 129EXPORT_SYMBOL(lustre_msg_size_v2);
 130
 131/* This returns the size of the buffer that is required to hold a lustre_msg
 132 * with the given sub-buffer lengths.
 133 * NOTE: this should only be used for NEW requests, and should always be
 134 *       in the form of a v2 request.  If this is a connection to a v1
 135 *       target then the first buffer will be stripped because the ptlrpc
 136 *       data is part of the lustre_msg_v1 header. b=14043
 137 */
 138int lustre_msg_size(__u32 magic, int count, __u32 *lens)
 139{
 140        __u32 size[] = { sizeof(struct ptlrpc_body) };
 141
 142        if (!lens) {
 143                LASSERT(count == 1);
 144                lens = size;
 145        }
 146
 147        LASSERT(count > 0);
 148        LASSERT(lens[MSG_PTLRPC_BODY_OFF] >= sizeof(struct ptlrpc_body_v2));
 149
 150        switch (magic) {
 151        case LUSTRE_MSG_MAGIC_V2:
 152                return lustre_msg_size_v2(count, lens);
 153        default:
 154                LASSERTF(0, "incorrect message magic: %08x\n", magic);
 155                return -EINVAL;
 156        }
 157}
 158EXPORT_SYMBOL(lustre_msg_size);
 159
 160/* This is used to determine the size of a buffer that was already packed
 161 * and will correctly handle the different message formats.
 162 */
 163int lustre_packed_msg_size(struct lustre_msg *msg)
 164{
 165        switch (msg->lm_magic) {
 166        case LUSTRE_MSG_MAGIC_V2:
 167                return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
 168        default:
 169                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
 170                return 0;
 171        }
 172}
 173EXPORT_SYMBOL(lustre_packed_msg_size);
 174
 175void lustre_init_msg_v2(struct lustre_msg_v2 *msg, int count, __u32 *lens,
 176                        char **bufs)
 177{
 178        char *ptr;
 179        int i;
 180
 181        msg->lm_bufcount = count;
 182        /* XXX: lm_secflvr uninitialized here */
 183        msg->lm_magic = LUSTRE_MSG_MAGIC_V2;
 184
 185        for (i = 0; i < count; i++)
 186                msg->lm_buflens[i] = lens[i];
 187
 188        if (!bufs)
 189                return;
 190
 191        ptr = (char *)msg + lustre_msg_hdr_size_v2(count);
 192        for (i = 0; i < count; i++) {
 193                char *tmp = bufs[i];
 194
 195                LOGL(tmp, lens[i], ptr);
 196        }
 197}
 198EXPORT_SYMBOL(lustre_init_msg_v2);
 199
 200static int lustre_pack_request_v2(struct ptlrpc_request *req,
 201                                  int count, __u32 *lens, char **bufs)
 202{
 203        int reqlen, rc;
 204
 205        reqlen = lustre_msg_size_v2(count, lens);
 206
 207        rc = sptlrpc_cli_alloc_reqbuf(req, reqlen);
 208        if (rc)
 209                return rc;
 210
 211        req->rq_reqlen = reqlen;
 212
 213        lustre_init_msg_v2(req->rq_reqmsg, count, lens, bufs);
 214        lustre_msg_add_version(req->rq_reqmsg, PTLRPC_MSG_VERSION);
 215        return 0;
 216}
 217
 218int lustre_pack_request(struct ptlrpc_request *req, __u32 magic, int count,
 219                        __u32 *lens, char **bufs)
 220{
 221        __u32 size[] = { sizeof(struct ptlrpc_body) };
 222
 223        if (!lens) {
 224                LASSERT(count == 1);
 225                lens = size;
 226        }
 227
 228        LASSERT(count > 0);
 229        LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
 230
 231        /* only use new format, we don't need to be compatible with 1.4 */
 232        return lustre_pack_request_v2(req, count, lens, bufs);
 233}
 234EXPORT_SYMBOL(lustre_pack_request);
 235
 236#if RS_DEBUG
 237LIST_HEAD(ptlrpc_rs_debug_lru);
 238spinlock_t ptlrpc_rs_debug_lock;
 239
 240#define PTLRPC_RS_DEBUG_LRU_ADD(rs)                                     \
 241do {                                                                    \
 242        spin_lock(&ptlrpc_rs_debug_lock);                               \
 243        list_add_tail(&(rs)->rs_debug_list, &ptlrpc_rs_debug_lru);      \
 244        spin_unlock(&ptlrpc_rs_debug_lock);                             \
 245} while (0)
 246
 247#define PTLRPC_RS_DEBUG_LRU_DEL(rs)                                     \
 248do {                                                                    \
 249        spin_lock(&ptlrpc_rs_debug_lock);                               \
 250        list_del(&(rs)->rs_debug_list);                         \
 251        spin_unlock(&ptlrpc_rs_debug_lock);                             \
 252} while (0)
 253#else
 254# define PTLRPC_RS_DEBUG_LRU_ADD(rs) do {} while (0)
 255# define PTLRPC_RS_DEBUG_LRU_DEL(rs) do {} while (0)
 256#endif
 257
 258struct ptlrpc_reply_state *
 259lustre_get_emerg_rs(struct ptlrpc_service_part *svcpt)
 260{
 261        struct ptlrpc_reply_state *rs = NULL;
 262
 263        spin_lock(&svcpt->scp_rep_lock);
 264
 265        /* See if we have anything in a pool, and wait if nothing */
 266        while (list_empty(&svcpt->scp_rep_idle)) {
 267                struct l_wait_info lwi;
 268                int rc;
 269
 270                spin_unlock(&svcpt->scp_rep_lock);
 271                /* If we cannot get anything for some long time, we better
 272                 * bail out instead of waiting infinitely
 273                 */
 274                lwi = LWI_TIMEOUT(cfs_time_seconds(10), NULL, NULL);
 275                rc = l_wait_event(svcpt->scp_rep_waitq,
 276                                  !list_empty(&svcpt->scp_rep_idle), &lwi);
 277                if (rc != 0)
 278                        goto out;
 279                spin_lock(&svcpt->scp_rep_lock);
 280        }
 281
 282        rs = list_entry(svcpt->scp_rep_idle.next,
 283                        struct ptlrpc_reply_state, rs_list);
 284        list_del(&rs->rs_list);
 285
 286        spin_unlock(&svcpt->scp_rep_lock);
 287
 288        memset(rs, 0, svcpt->scp_service->srv_max_reply_size);
 289        rs->rs_size = svcpt->scp_service->srv_max_reply_size;
 290        rs->rs_svcpt = svcpt;
 291        rs->rs_prealloc = 1;
 292out:
 293        return rs;
 294}
 295
 296void lustre_put_emerg_rs(struct ptlrpc_reply_state *rs)
 297{
 298        struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
 299
 300        spin_lock(&svcpt->scp_rep_lock);
 301        list_add(&rs->rs_list, &svcpt->scp_rep_idle);
 302        spin_unlock(&svcpt->scp_rep_lock);
 303        wake_up(&svcpt->scp_rep_waitq);
 304}
 305
 306int lustre_pack_reply_v2(struct ptlrpc_request *req, int count,
 307                         __u32 *lens, char **bufs, int flags)
 308{
 309        struct ptlrpc_reply_state *rs;
 310        int msg_len, rc;
 311
 312        LASSERT(!req->rq_reply_state);
 313
 314        if ((flags & LPRFL_EARLY_REPLY) == 0) {
 315                spin_lock(&req->rq_lock);
 316                req->rq_packed_final = 1;
 317                spin_unlock(&req->rq_lock);
 318        }
 319
 320        msg_len = lustre_msg_size_v2(count, lens);
 321        rc = sptlrpc_svc_alloc_rs(req, msg_len);
 322        if (rc)
 323                return rc;
 324
 325        rs = req->rq_reply_state;
 326        atomic_set(&rs->rs_refcount, 1);    /* 1 ref for rq_reply_state */
 327        rs->rs_cb_id.cbid_fn = reply_out_callback;
 328        rs->rs_cb_id.cbid_arg = rs;
 329        rs->rs_svcpt = req->rq_rqbd->rqbd_svcpt;
 330        INIT_LIST_HEAD(&rs->rs_exp_list);
 331        INIT_LIST_HEAD(&rs->rs_obd_list);
 332        INIT_LIST_HEAD(&rs->rs_list);
 333        spin_lock_init(&rs->rs_lock);
 334
 335        req->rq_replen = msg_len;
 336        req->rq_reply_state = rs;
 337        req->rq_repmsg = rs->rs_msg;
 338
 339        lustre_init_msg_v2(rs->rs_msg, count, lens, bufs);
 340        lustre_msg_add_version(rs->rs_msg, PTLRPC_MSG_VERSION);
 341
 342        PTLRPC_RS_DEBUG_LRU_ADD(rs);
 343
 344        return 0;
 345}
 346EXPORT_SYMBOL(lustre_pack_reply_v2);
 347
 348int lustre_pack_reply_flags(struct ptlrpc_request *req, int count, __u32 *lens,
 349                            char **bufs, int flags)
 350{
 351        int rc = 0;
 352        __u32 size[] = { sizeof(struct ptlrpc_body) };
 353
 354        if (!lens) {
 355                LASSERT(count == 1);
 356                lens = size;
 357        }
 358
 359        LASSERT(count > 0);
 360        LASSERT(lens[MSG_PTLRPC_BODY_OFF] == sizeof(struct ptlrpc_body));
 361
 362        switch (req->rq_reqmsg->lm_magic) {
 363        case LUSTRE_MSG_MAGIC_V2:
 364                rc = lustre_pack_reply_v2(req, count, lens, bufs, flags);
 365                break;
 366        default:
 367                LASSERTF(0, "incorrect message magic: %08x\n",
 368                         req->rq_reqmsg->lm_magic);
 369                rc = -EINVAL;
 370        }
 371        if (rc != 0)
 372                CERROR("lustre_pack_reply failed: rc=%d size=%d\n", rc,
 373                       lustre_msg_size(req->rq_reqmsg->lm_magic, count, lens));
 374        return rc;
 375}
 376EXPORT_SYMBOL(lustre_pack_reply_flags);
 377
 378int lustre_pack_reply(struct ptlrpc_request *req, int count, __u32 *lens,
 379                      char **bufs)
 380{
 381        return lustre_pack_reply_flags(req, count, lens, bufs, 0);
 382}
 383EXPORT_SYMBOL(lustre_pack_reply);
 384
 385void *lustre_msg_buf_v2(struct lustre_msg_v2 *m, int n, int min_size)
 386{
 387        int i, offset, buflen, bufcount;
 388
 389        LASSERT(n >= 0);
 390
 391        bufcount = m->lm_bufcount;
 392        if (unlikely(n >= bufcount)) {
 393                CDEBUG(D_INFO, "msg %p buffer[%d] not present (count %d)\n",
 394                       m, n, bufcount);
 395                return NULL;
 396        }
 397
 398        buflen = m->lm_buflens[n];
 399        if (unlikely(buflen < min_size)) {
 400                CERROR("msg %p buffer[%d] size %d too small (required %d, opc=%d)\n",
 401                       m, n, buflen, min_size,
 402                       n == MSG_PTLRPC_BODY_OFF ? -1 : lustre_msg_get_opc(m));
 403                return NULL;
 404        }
 405
 406        offset = lustre_msg_hdr_size_v2(bufcount);
 407        for (i = 0; i < n; i++)
 408                offset += cfs_size_round(m->lm_buflens[i]);
 409
 410        return (char *)m + offset;
 411}
 412
 413void *lustre_msg_buf(struct lustre_msg *m, int n, int min_size)
 414{
 415        switch (m->lm_magic) {
 416        case LUSTRE_MSG_MAGIC_V2:
 417                return lustre_msg_buf_v2(m, n, min_size);
 418        default:
 419                LASSERTF(0, "incorrect message magic: %08x (msg:%p)\n",
 420                         m->lm_magic, m);
 421                return NULL;
 422        }
 423}
 424EXPORT_SYMBOL(lustre_msg_buf);
 425
 426static int lustre_shrink_msg_v2(struct lustre_msg_v2 *msg, int segment,
 427                                unsigned int newlen, int move_data)
 428{
 429        char *tail = NULL, *newpos;
 430        int tail_len = 0, n;
 431
 432        LASSERT(msg);
 433        LASSERT(msg->lm_bufcount > segment);
 434        LASSERT(msg->lm_buflens[segment] >= newlen);
 435
 436        if (msg->lm_buflens[segment] == newlen)
 437                goto out;
 438
 439        if (move_data && msg->lm_bufcount > segment + 1) {
 440                tail = lustre_msg_buf_v2(msg, segment + 1, 0);
 441                for (n = segment + 1; n < msg->lm_bufcount; n++)
 442                        tail_len += cfs_size_round(msg->lm_buflens[n]);
 443        }
 444
 445        msg->lm_buflens[segment] = newlen;
 446
 447        if (tail && tail_len) {
 448                newpos = lustre_msg_buf_v2(msg, segment + 1, 0);
 449                LASSERT(newpos <= tail);
 450                if (newpos != tail)
 451                        memmove(newpos, tail, tail_len);
 452        }
 453out:
 454        return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
 455}
 456
 457/*
 458 * for @msg, shrink @segment to size @newlen. if @move_data is non-zero,
 459 * we also move data forward from @segment + 1.
 460 *
 461 * if @newlen == 0, we remove the segment completely, but we still keep the
 462 * totally bufcount the same to save possible data moving. this will leave a
 463 * unused segment with size 0 at the tail, but that's ok.
 464 *
 465 * return new msg size after shrinking.
 466 *
 467 * CAUTION:
 468 * + if any buffers higher than @segment has been filled in, must call shrink
 469 *   with non-zero @move_data.
 470 * + caller should NOT keep pointers to msg buffers which higher than @segment
 471 *   after call shrink.
 472 */
 473int lustre_shrink_msg(struct lustre_msg *msg, int segment,
 474                      unsigned int newlen, int move_data)
 475{
 476        switch (msg->lm_magic) {
 477        case LUSTRE_MSG_MAGIC_V2:
 478                return lustre_shrink_msg_v2(msg, segment, newlen, move_data);
 479        default:
 480                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
 481        }
 482}
 483EXPORT_SYMBOL(lustre_shrink_msg);
 484
 485void lustre_free_reply_state(struct ptlrpc_reply_state *rs)
 486{
 487        PTLRPC_RS_DEBUG_LRU_DEL(rs);
 488
 489        LASSERT(atomic_read(&rs->rs_refcount) == 0);
 490        LASSERT(!rs->rs_difficult || rs->rs_handled);
 491        LASSERT(!rs->rs_on_net);
 492        LASSERT(!rs->rs_scheduled);
 493        LASSERT(!rs->rs_export);
 494        LASSERT(rs->rs_nlocks == 0);
 495        LASSERT(list_empty(&rs->rs_exp_list));
 496        LASSERT(list_empty(&rs->rs_obd_list));
 497
 498        sptlrpc_svc_free_rs(rs);
 499}
 500EXPORT_SYMBOL(lustre_free_reply_state);
 501
 502static int lustre_unpack_msg_v2(struct lustre_msg_v2 *m, int len)
 503{
 504        int swabbed, required_len, i;
 505
 506        /* Now we know the sender speaks my language. */
 507        required_len = lustre_msg_hdr_size_v2(0);
 508        if (len < required_len) {
 509                /* can't even look inside the message */
 510                CERROR("message length %d too small for lustre_msg\n", len);
 511                return -EINVAL;
 512        }
 513
 514        swabbed = (m->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED);
 515
 516        if (swabbed) {
 517                __swab32s(&m->lm_magic);
 518                __swab32s(&m->lm_bufcount);
 519                __swab32s(&m->lm_secflvr);
 520                __swab32s(&m->lm_repsize);
 521                __swab32s(&m->lm_cksum);
 522                __swab32s(&m->lm_flags);
 523                CLASSERT(offsetof(typeof(*m), lm_padding_2) != 0);
 524                CLASSERT(offsetof(typeof(*m), lm_padding_3) != 0);
 525        }
 526
 527        required_len = lustre_msg_hdr_size_v2(m->lm_bufcount);
 528        if (len < required_len) {
 529                /* didn't receive all the buffer lengths */
 530                CERROR("message length %d too small for %d buflens\n",
 531                       len, m->lm_bufcount);
 532                return -EINVAL;
 533        }
 534
 535        for (i = 0; i < m->lm_bufcount; i++) {
 536                if (swabbed)
 537                        __swab32s(&m->lm_buflens[i]);
 538                required_len += cfs_size_round(m->lm_buflens[i]);
 539        }
 540
 541        if (len < required_len) {
 542                CERROR("len: %d, required_len %d\n", len, required_len);
 543                CERROR("bufcount: %d\n", m->lm_bufcount);
 544                for (i = 0; i < m->lm_bufcount; i++)
 545                        CERROR("buffer %d length %d\n", i, m->lm_buflens[i]);
 546                return -EINVAL;
 547        }
 548
 549        return swabbed;
 550}
 551
 552int __lustre_unpack_msg(struct lustre_msg *m, int len)
 553{
 554        int required_len, rc;
 555
 556        /* We can provide a slightly better error log, if we check the
 557         * message magic and version first.  In the future, struct
 558         * lustre_msg may grow, and we'd like to log a version mismatch,
 559         * rather than a short message.
 560         *
 561         */
 562        required_len = offsetof(struct lustre_msg, lm_magic) +
 563                       sizeof(m->lm_magic);
 564        if (len < required_len) {
 565                /* can't even look inside the message */
 566                CERROR("message length %d too small for magic/version check\n",
 567                       len);
 568                return -EINVAL;
 569        }
 570
 571        rc = lustre_unpack_msg_v2(m, len);
 572
 573        return rc;
 574}
 575EXPORT_SYMBOL(__lustre_unpack_msg);
 576
 577int ptlrpc_unpack_req_msg(struct ptlrpc_request *req, int len)
 578{
 579        int rc;
 580
 581        rc = __lustre_unpack_msg(req->rq_reqmsg, len);
 582        if (rc == 1) {
 583                lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
 584                rc = 0;
 585        }
 586        return rc;
 587}
 588EXPORT_SYMBOL(ptlrpc_unpack_req_msg);
 589
 590int ptlrpc_unpack_rep_msg(struct ptlrpc_request *req, int len)
 591{
 592        int rc;
 593
 594        rc = __lustre_unpack_msg(req->rq_repmsg, len);
 595        if (rc == 1) {
 596                lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
 597                rc = 0;
 598        }
 599        return rc;
 600}
 601EXPORT_SYMBOL(ptlrpc_unpack_rep_msg);
 602
 603static inline int lustre_unpack_ptlrpc_body_v2(struct ptlrpc_request *req,
 604                                               const int inout, int offset)
 605{
 606        struct ptlrpc_body *pb;
 607        struct lustre_msg_v2 *m = inout ? req->rq_reqmsg : req->rq_repmsg;
 608
 609        pb = lustre_msg_buf_v2(m, offset, sizeof(struct ptlrpc_body_v2));
 610        if (!pb) {
 611                CERROR("error unpacking ptlrpc body\n");
 612                return -EFAULT;
 613        }
 614        if (ptlrpc_buf_need_swab(req, inout, offset)) {
 615                lustre_swab_ptlrpc_body(pb);
 616                ptlrpc_buf_set_swabbed(req, inout, offset);
 617        }
 618
 619        if ((pb->pb_version & ~LUSTRE_VERSION_MASK) != PTLRPC_MSG_VERSION) {
 620                CERROR("wrong lustre_msg version %08x\n", pb->pb_version);
 621                return -EINVAL;
 622        }
 623
 624        if (!inout)
 625                pb->pb_status = ptlrpc_status_ntoh(pb->pb_status);
 626
 627        return 0;
 628}
 629
 630int lustre_unpack_req_ptlrpc_body(struct ptlrpc_request *req, int offset)
 631{
 632        switch (req->rq_reqmsg->lm_magic) {
 633        case LUSTRE_MSG_MAGIC_V2:
 634                return lustre_unpack_ptlrpc_body_v2(req, 1, offset);
 635        default:
 636                CERROR("bad lustre msg magic: %08x\n",
 637                       req->rq_reqmsg->lm_magic);
 638                return -EINVAL;
 639        }
 640}
 641
 642int lustre_unpack_rep_ptlrpc_body(struct ptlrpc_request *req, int offset)
 643{
 644        switch (req->rq_repmsg->lm_magic) {
 645        case LUSTRE_MSG_MAGIC_V2:
 646                return lustre_unpack_ptlrpc_body_v2(req, 0, offset);
 647        default:
 648                CERROR("bad lustre msg magic: %08x\n",
 649                       req->rq_repmsg->lm_magic);
 650                return -EINVAL;
 651        }
 652}
 653
 654static inline int lustre_msg_buflen_v2(struct lustre_msg_v2 *m, int n)
 655{
 656        if (n >= m->lm_bufcount)
 657                return 0;
 658
 659        return m->lm_buflens[n];
 660}
 661
 662/**
 663 * lustre_msg_buflen - return the length of buffer \a n in message \a m
 664 * \param m lustre_msg (request or reply) to look at
 665 * \param n message index (base 0)
 666 *
 667 * returns zero for non-existent message indices
 668 */
 669int lustre_msg_buflen(struct lustre_msg *m, int n)
 670{
 671        switch (m->lm_magic) {
 672        case LUSTRE_MSG_MAGIC_V2:
 673                return lustre_msg_buflen_v2(m, n);
 674        default:
 675                CERROR("incorrect message magic: %08x\n", m->lm_magic);
 676                return -EINVAL;
 677        }
 678}
 679EXPORT_SYMBOL(lustre_msg_buflen);
 680
 681/* NB return the bufcount for lustre_msg_v2 format, so if message is packed
 682 * in V1 format, the result is one bigger. (add struct ptlrpc_body).
 683 */
 684int lustre_msg_bufcount(struct lustre_msg *m)
 685{
 686        switch (m->lm_magic) {
 687        case LUSTRE_MSG_MAGIC_V2:
 688                return m->lm_bufcount;
 689        default:
 690                CERROR("incorrect message magic: %08x\n", m->lm_magic);
 691                return -EINVAL;
 692        }
 693}
 694EXPORT_SYMBOL(lustre_msg_bufcount);
 695
 696char *lustre_msg_string(struct lustre_msg *m, int index, int max_len)
 697{
 698        /* max_len == 0 means the string should fill the buffer */
 699        char *str;
 700        int slen, blen;
 701
 702        switch (m->lm_magic) {
 703        case LUSTRE_MSG_MAGIC_V2:
 704                str = lustre_msg_buf_v2(m, index, 0);
 705                blen = lustre_msg_buflen_v2(m, index);
 706                break;
 707        default:
 708                LASSERTF(0, "incorrect message magic: %08x\n", m->lm_magic);
 709        }
 710
 711        if (!str) {
 712                CERROR("can't unpack string in msg %p buffer[%d]\n", m, index);
 713                return NULL;
 714        }
 715
 716        slen = strnlen(str, blen);
 717
 718        if (slen == blen) {                  /* not NULL terminated */
 719                CERROR("can't unpack non-NULL terminated string in msg %p buffer[%d] len %d\n",
 720                       m, index, blen);
 721                return NULL;
 722        }
 723
 724        if (max_len == 0) {
 725                if (slen != blen - 1) {
 726                        CERROR("can't unpack short string in msg %p buffer[%d] len %d: strlen %d\n",
 727                               m, index, blen, slen);
 728                        return NULL;
 729                }
 730        } else if (slen > max_len) {
 731                CERROR("can't unpack oversized string in msg %p buffer[%d] len %d strlen %d: max %d expected\n",
 732                       m, index, blen, slen, max_len);
 733                return NULL;
 734        }
 735
 736        return str;
 737}
 738EXPORT_SYMBOL(lustre_msg_string);
 739
 740/* Wrap up the normal fixed length cases */
 741static inline void *__lustre_swab_buf(struct lustre_msg *msg, int index,
 742                                      int min_size, void *swabber)
 743{
 744        void *ptr = NULL;
 745
 746        switch (msg->lm_magic) {
 747        case LUSTRE_MSG_MAGIC_V2:
 748                ptr = lustre_msg_buf_v2(msg, index, min_size);
 749                break;
 750        default:
 751                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
 752        }
 753
 754        if (ptr && swabber)
 755                ((void (*)(void *))swabber)(ptr);
 756
 757        return ptr;
 758}
 759
 760static inline struct ptlrpc_body *lustre_msg_ptlrpc_body(struct lustre_msg *msg)
 761{
 762        return lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
 763                                 sizeof(struct ptlrpc_body_v2));
 764}
 765
 766__u32 lustre_msghdr_get_flags(struct lustre_msg *msg)
 767{
 768        switch (msg->lm_magic) {
 769        case LUSTRE_MSG_MAGIC_V2:
 770                /* already in host endian */
 771                return msg->lm_flags;
 772        default:
 773                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
 774                return 0;
 775        }
 776}
 777EXPORT_SYMBOL(lustre_msghdr_get_flags);
 778
 779void lustre_msghdr_set_flags(struct lustre_msg *msg, __u32 flags)
 780{
 781        switch (msg->lm_magic) {
 782        case LUSTRE_MSG_MAGIC_V2:
 783                msg->lm_flags = flags;
 784                return;
 785        default:
 786                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
 787        }
 788}
 789
 790__u32 lustre_msg_get_flags(struct lustre_msg *msg)
 791{
 792        switch (msg->lm_magic) {
 793        case LUSTRE_MSG_MAGIC_V2: {
 794                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
 795
 796                if (pb)
 797                        return pb->pb_flags;
 798
 799                CERROR("invalid msg %p: no ptlrpc body!\n", msg);
 800        }
 801        /* no break */
 802        default:
 803                /* flags might be printed in debug code while message
 804                 * uninitialized
 805                 */
 806                return 0;
 807        }
 808}
 809EXPORT_SYMBOL(lustre_msg_get_flags);
 810
 811void lustre_msg_add_flags(struct lustre_msg *msg, int flags)
 812{
 813        switch (msg->lm_magic) {
 814        case LUSTRE_MSG_MAGIC_V2: {
 815                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
 816
 817                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
 818                pb->pb_flags |= flags;
 819                return;
 820        }
 821        default:
 822                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
 823        }
 824}
 825EXPORT_SYMBOL(lustre_msg_add_flags);
 826
 827void lustre_msg_set_flags(struct lustre_msg *msg, int flags)
 828{
 829        switch (msg->lm_magic) {
 830        case LUSTRE_MSG_MAGIC_V2: {
 831                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
 832
 833                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
 834                pb->pb_flags = flags;
 835                return;
 836        }
 837        default:
 838                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
 839        }
 840}
 841EXPORT_SYMBOL(lustre_msg_set_flags);
 842
 843void lustre_msg_clear_flags(struct lustre_msg *msg, int flags)
 844{
 845        switch (msg->lm_magic) {
 846        case LUSTRE_MSG_MAGIC_V2: {
 847                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
 848
 849                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
 850                pb->pb_flags &= ~(flags & MSG_GEN_FLAG_MASK);
 851                return;
 852        }
 853        default:
 854                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
 855        }
 856}
 857EXPORT_SYMBOL(lustre_msg_clear_flags);
 858
 859__u32 lustre_msg_get_op_flags(struct lustre_msg *msg)
 860{
 861        switch (msg->lm_magic) {
 862        case LUSTRE_MSG_MAGIC_V2: {
 863                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
 864
 865                if (pb)
 866                        return pb->pb_op_flags;
 867
 868                CERROR("invalid msg %p: no ptlrpc body!\n", msg);
 869        }
 870        /* no break */
 871        default:
 872                return 0;
 873        }
 874}
 875EXPORT_SYMBOL(lustre_msg_get_op_flags);
 876
 877void lustre_msg_add_op_flags(struct lustre_msg *msg, int flags)
 878{
 879        switch (msg->lm_magic) {
 880        case LUSTRE_MSG_MAGIC_V2: {
 881                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
 882
 883                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
 884                pb->pb_op_flags |= flags;
 885                return;
 886        }
 887        default:
 888                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
 889        }
 890}
 891EXPORT_SYMBOL(lustre_msg_add_op_flags);
 892
 893struct lustre_handle *lustre_msg_get_handle(struct lustre_msg *msg)
 894{
 895        switch (msg->lm_magic) {
 896        case LUSTRE_MSG_MAGIC_V2: {
 897                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
 898
 899                if (!pb) {
 900                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
 901                        return NULL;
 902                }
 903                return &pb->pb_handle;
 904        }
 905        default:
 906                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
 907                return NULL;
 908        }
 909}
 910EXPORT_SYMBOL(lustre_msg_get_handle);
 911
 912__u32 lustre_msg_get_type(struct lustre_msg *msg)
 913{
 914        switch (msg->lm_magic) {
 915        case LUSTRE_MSG_MAGIC_V2: {
 916                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
 917
 918                if (!pb) {
 919                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
 920                        return PTL_RPC_MSG_ERR;
 921                }
 922                return pb->pb_type;
 923        }
 924        default:
 925                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
 926                return PTL_RPC_MSG_ERR;
 927        }
 928}
 929EXPORT_SYMBOL(lustre_msg_get_type);
 930
 931void lustre_msg_add_version(struct lustre_msg *msg, int version)
 932{
 933        switch (msg->lm_magic) {
 934        case LUSTRE_MSG_MAGIC_V2: {
 935                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
 936
 937                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
 938                pb->pb_version |= version;
 939                return;
 940        }
 941        default:
 942                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
 943        }
 944}
 945EXPORT_SYMBOL(lustre_msg_add_version);
 946
 947__u32 lustre_msg_get_opc(struct lustre_msg *msg)
 948{
 949        switch (msg->lm_magic) {
 950        case LUSTRE_MSG_MAGIC_V2: {
 951                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
 952
 953                if (!pb) {
 954                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
 955                        return 0;
 956                }
 957                return pb->pb_opc;
 958        }
 959        default:
 960                CERROR("incorrect message magic: %08x (msg:%p)\n",
 961                       msg->lm_magic, msg);
 962                return 0;
 963        }
 964}
 965EXPORT_SYMBOL(lustre_msg_get_opc);
 966
 967__u64 lustre_msg_get_last_committed(struct lustre_msg *msg)
 968{
 969        switch (msg->lm_magic) {
 970        case LUSTRE_MSG_MAGIC_V2: {
 971                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
 972
 973                if (!pb) {
 974                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
 975                        return 0;
 976                }
 977                return pb->pb_last_committed;
 978        }
 979        default:
 980                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
 981                return 0;
 982        }
 983}
 984EXPORT_SYMBOL(lustre_msg_get_last_committed);
 985
 986__u64 *lustre_msg_get_versions(struct lustre_msg *msg)
 987{
 988        switch (msg->lm_magic) {
 989        case LUSTRE_MSG_MAGIC_V2: {
 990                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
 991
 992                if (!pb) {
 993                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
 994                        return NULL;
 995                }
 996                return pb->pb_pre_versions;
 997        }
 998        default:
 999                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1000                return NULL;
1001        }
1002}
1003EXPORT_SYMBOL(lustre_msg_get_versions);
1004
1005__u64 lustre_msg_get_transno(struct lustre_msg *msg)
1006{
1007        switch (msg->lm_magic) {
1008        case LUSTRE_MSG_MAGIC_V2: {
1009                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1010
1011                if (!pb) {
1012                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1013                        return 0;
1014                }
1015                return pb->pb_transno;
1016        }
1017        default:
1018                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1019                return 0;
1020        }
1021}
1022EXPORT_SYMBOL(lustre_msg_get_transno);
1023
1024int lustre_msg_get_status(struct lustre_msg *msg)
1025{
1026        switch (msg->lm_magic) {
1027        case LUSTRE_MSG_MAGIC_V2: {
1028                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1029
1030                if (pb)
1031                        return pb->pb_status;
1032
1033                CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1034        }
1035        /* no break */
1036        default:
1037                /* status might be printed in debug code while message
1038                 * uninitialized
1039                 */
1040                return -EINVAL;
1041        }
1042}
1043EXPORT_SYMBOL(lustre_msg_get_status);
1044
1045__u64 lustre_msg_get_slv(struct lustre_msg *msg)
1046{
1047        switch (msg->lm_magic) {
1048        case LUSTRE_MSG_MAGIC_V2: {
1049                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1050
1051                if (!pb) {
1052                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1053                        return -EINVAL;
1054                }
1055                return pb->pb_slv;
1056        }
1057        default:
1058                CERROR("invalid msg magic %08x\n", msg->lm_magic);
1059                return -EINVAL;
1060        }
1061}
1062EXPORT_SYMBOL(lustre_msg_get_slv);
1063
1064void lustre_msg_set_slv(struct lustre_msg *msg, __u64 slv)
1065{
1066        switch (msg->lm_magic) {
1067        case LUSTRE_MSG_MAGIC_V2: {
1068                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1069
1070                if (!pb) {
1071                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1072                        return;
1073                }
1074                pb->pb_slv = slv;
1075                return;
1076        }
1077        default:
1078                CERROR("invalid msg magic %x\n", msg->lm_magic);
1079                return;
1080        }
1081}
1082EXPORT_SYMBOL(lustre_msg_set_slv);
1083
1084__u32 lustre_msg_get_limit(struct lustre_msg *msg)
1085{
1086        switch (msg->lm_magic) {
1087        case LUSTRE_MSG_MAGIC_V2: {
1088                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1089
1090                if (!pb) {
1091                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1092                        return -EINVAL;
1093                }
1094                return pb->pb_limit;
1095        }
1096        default:
1097                CERROR("invalid msg magic %x\n", msg->lm_magic);
1098                return -EINVAL;
1099        }
1100}
1101EXPORT_SYMBOL(lustre_msg_get_limit);
1102
1103void lustre_msg_set_limit(struct lustre_msg *msg, __u64 limit)
1104{
1105        switch (msg->lm_magic) {
1106        case LUSTRE_MSG_MAGIC_V2: {
1107                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1108
1109                if (!pb) {
1110                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1111                        return;
1112                }
1113                pb->pb_limit = limit;
1114                return;
1115        }
1116        default:
1117                CERROR("invalid msg magic %08x\n", msg->lm_magic);
1118                return;
1119        }
1120}
1121EXPORT_SYMBOL(lustre_msg_set_limit);
1122
1123__u32 lustre_msg_get_conn_cnt(struct lustre_msg *msg)
1124{
1125        switch (msg->lm_magic) {
1126        case LUSTRE_MSG_MAGIC_V2: {
1127                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1128
1129                if (!pb) {
1130                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1131                        return 0;
1132                }
1133                return pb->pb_conn_cnt;
1134        }
1135        default:
1136                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1137                return 0;
1138        }
1139}
1140EXPORT_SYMBOL(lustre_msg_get_conn_cnt);
1141
1142__u32 lustre_msg_get_magic(struct lustre_msg *msg)
1143{
1144        switch (msg->lm_magic) {
1145        case LUSTRE_MSG_MAGIC_V2:
1146                return msg->lm_magic;
1147        default:
1148                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1149                return 0;
1150        }
1151}
1152EXPORT_SYMBOL(lustre_msg_get_magic);
1153
1154__u32 lustre_msg_get_timeout(struct lustre_msg *msg)
1155{
1156        switch (msg->lm_magic) {
1157        case LUSTRE_MSG_MAGIC_V2: {
1158                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1159
1160                if (!pb) {
1161                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1162                        return 0;
1163
1164                }
1165                return pb->pb_timeout;
1166        }
1167        default:
1168                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1169                return -EPROTO;
1170        }
1171}
1172
1173__u32 lustre_msg_get_service_time(struct lustre_msg *msg)
1174{
1175        switch (msg->lm_magic) {
1176        case LUSTRE_MSG_MAGIC_V2: {
1177                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1178
1179                if (!pb) {
1180                        CERROR("invalid msg %p: no ptlrpc body!\n", msg);
1181                        return 0;
1182
1183                }
1184                return pb->pb_service_time;
1185        }
1186        default:
1187                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1188                return 0;
1189        }
1190}
1191
1192__u32 lustre_msg_get_cksum(struct lustre_msg *msg)
1193{
1194        switch (msg->lm_magic) {
1195        case LUSTRE_MSG_MAGIC_V2:
1196                return msg->lm_cksum;
1197        default:
1198                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1199                return 0;
1200        }
1201}
1202
1203__u32 lustre_msg_calc_cksum(struct lustre_msg *msg)
1204{
1205        switch (msg->lm_magic) {
1206        case LUSTRE_MSG_MAGIC_V2: {
1207                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1208                __u32 crc;
1209                unsigned int hsize = 4;
1210
1211                cfs_crypto_hash_digest(CFS_HASH_ALG_CRC32, (unsigned char *)pb,
1212                                   lustre_msg_buflen(msg, MSG_PTLRPC_BODY_OFF),
1213                                   NULL, 0, (unsigned char *)&crc, &hsize);
1214                return crc;
1215        }
1216        default:
1217                CERROR("incorrect message magic: %08x\n", msg->lm_magic);
1218                return 0;
1219        }
1220}
1221
1222void lustre_msg_set_handle(struct lustre_msg *msg, struct lustre_handle *handle)
1223{
1224        switch (msg->lm_magic) {
1225        case LUSTRE_MSG_MAGIC_V2: {
1226                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1227
1228                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1229                pb->pb_handle = *handle;
1230                return;
1231        }
1232        default:
1233                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1234        }
1235}
1236EXPORT_SYMBOL(lustre_msg_set_handle);
1237
1238void lustre_msg_set_type(struct lustre_msg *msg, __u32 type)
1239{
1240        switch (msg->lm_magic) {
1241        case LUSTRE_MSG_MAGIC_V2: {
1242                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1243
1244                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1245                pb->pb_type = type;
1246                return;
1247        }
1248        default:
1249                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1250        }
1251}
1252EXPORT_SYMBOL(lustre_msg_set_type);
1253
1254void lustre_msg_set_opc(struct lustre_msg *msg, __u32 opc)
1255{
1256        switch (msg->lm_magic) {
1257        case LUSTRE_MSG_MAGIC_V2: {
1258                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1259
1260                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1261                pb->pb_opc = opc;
1262                return;
1263        }
1264        default:
1265                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1266        }
1267}
1268EXPORT_SYMBOL(lustre_msg_set_opc);
1269
1270void lustre_msg_set_versions(struct lustre_msg *msg, __u64 *versions)
1271{
1272        switch (msg->lm_magic) {
1273        case LUSTRE_MSG_MAGIC_V2: {
1274                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1275
1276                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1277                pb->pb_pre_versions[0] = versions[0];
1278                pb->pb_pre_versions[1] = versions[1];
1279                pb->pb_pre_versions[2] = versions[2];
1280                pb->pb_pre_versions[3] = versions[3];
1281                return;
1282        }
1283        default:
1284                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1285        }
1286}
1287EXPORT_SYMBOL(lustre_msg_set_versions);
1288
1289void lustre_msg_set_transno(struct lustre_msg *msg, __u64 transno)
1290{
1291        switch (msg->lm_magic) {
1292        case LUSTRE_MSG_MAGIC_V2: {
1293                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1294
1295                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1296                pb->pb_transno = transno;
1297                return;
1298        }
1299        default:
1300                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1301        }
1302}
1303EXPORT_SYMBOL(lustre_msg_set_transno);
1304
1305void lustre_msg_set_status(struct lustre_msg *msg, __u32 status)
1306{
1307        switch (msg->lm_magic) {
1308        case LUSTRE_MSG_MAGIC_V2: {
1309                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1310
1311                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1312                pb->pb_status = status;
1313                return;
1314        }
1315        default:
1316                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1317        }
1318}
1319EXPORT_SYMBOL(lustre_msg_set_status);
1320
1321void lustre_msg_set_conn_cnt(struct lustre_msg *msg, __u32 conn_cnt)
1322{
1323        switch (msg->lm_magic) {
1324        case LUSTRE_MSG_MAGIC_V2: {
1325                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1326
1327                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1328                pb->pb_conn_cnt = conn_cnt;
1329                return;
1330        }
1331        default:
1332                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1333        }
1334}
1335EXPORT_SYMBOL(lustre_msg_set_conn_cnt);
1336
1337void lustre_msg_set_timeout(struct lustre_msg *msg, __u32 timeout)
1338{
1339        switch (msg->lm_magic) {
1340        case LUSTRE_MSG_MAGIC_V2: {
1341                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1342
1343                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1344                pb->pb_timeout = timeout;
1345                return;
1346        }
1347        default:
1348                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1349        }
1350}
1351
1352void lustre_msg_set_service_time(struct lustre_msg *msg, __u32 service_time)
1353{
1354        switch (msg->lm_magic) {
1355        case LUSTRE_MSG_MAGIC_V2: {
1356                struct ptlrpc_body *pb = lustre_msg_ptlrpc_body(msg);
1357
1358                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1359                pb->pb_service_time = service_time;
1360                return;
1361        }
1362        default:
1363                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1364        }
1365}
1366
1367void lustre_msg_set_jobid(struct lustre_msg *msg, char *jobid)
1368{
1369        switch (msg->lm_magic) {
1370        case LUSTRE_MSG_MAGIC_V2: {
1371                __u32 opc = lustre_msg_get_opc(msg);
1372                struct ptlrpc_body *pb;
1373
1374                /* Don't set jobid for ldlm ast RPCs, they've been shrunk.
1375                 * See the comment in ptlrpc_request_pack().
1376                 */
1377                if (!opc || opc == LDLM_BL_CALLBACK ||
1378                    opc == LDLM_CP_CALLBACK || opc == LDLM_GL_CALLBACK)
1379                        return;
1380
1381                pb = lustre_msg_buf_v2(msg, MSG_PTLRPC_BODY_OFF,
1382                                       sizeof(struct ptlrpc_body));
1383                LASSERTF(pb, "invalid msg %p: no ptlrpc body!\n", msg);
1384
1385                if (jobid)
1386                        memcpy(pb->pb_jobid, jobid, JOBSTATS_JOBID_SIZE);
1387                else if (pb->pb_jobid[0] == '\0')
1388                        lustre_get_jobid(pb->pb_jobid);
1389                return;
1390        }
1391        default:
1392                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1393        }
1394}
1395EXPORT_SYMBOL(lustre_msg_set_jobid);
1396
1397void lustre_msg_set_cksum(struct lustre_msg *msg, __u32 cksum)
1398{
1399        switch (msg->lm_magic) {
1400        case LUSTRE_MSG_MAGIC_V2:
1401                msg->lm_cksum = cksum;
1402                return;
1403        default:
1404                LASSERTF(0, "incorrect message magic: %08x\n", msg->lm_magic);
1405        }
1406}
1407
1408void ptlrpc_request_set_replen(struct ptlrpc_request *req)
1409{
1410        int count = req_capsule_filled_sizes(&req->rq_pill, RCL_SERVER);
1411
1412        req->rq_replen = lustre_msg_size(req->rq_reqmsg->lm_magic, count,
1413                                         req->rq_pill.rc_area[RCL_SERVER]);
1414        if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1415                req->rq_reqmsg->lm_repsize = req->rq_replen;
1416}
1417EXPORT_SYMBOL(ptlrpc_request_set_replen);
1418
1419/**
1420 * Send a remote set_info_async.
1421 *
1422 * This may go from client to server or server to client.
1423 */
1424int do_set_info_async(struct obd_import *imp,
1425                      int opcode, int version,
1426                      u32 keylen, void *key,
1427                      u32 vallen, void *val,
1428                      struct ptlrpc_request_set *set)
1429{
1430        struct ptlrpc_request *req;
1431        char *tmp;
1432        int rc;
1433
1434        req = ptlrpc_request_alloc(imp, &RQF_OBD_SET_INFO);
1435        if (!req)
1436                return -ENOMEM;
1437
1438        req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_KEY,
1439                             RCL_CLIENT, keylen);
1440        req_capsule_set_size(&req->rq_pill, &RMF_SETINFO_VAL,
1441                             RCL_CLIENT, vallen);
1442        rc = ptlrpc_request_pack(req, version, opcode);
1443        if (rc) {
1444                ptlrpc_request_free(req);
1445                return rc;
1446        }
1447
1448        tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1449        memcpy(tmp, key, keylen);
1450        tmp = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1451        memcpy(tmp, val, vallen);
1452
1453        ptlrpc_request_set_replen(req);
1454
1455        if (set) {
1456                ptlrpc_set_add_req(set, req);
1457                ptlrpc_check_set(NULL, set);
1458        } else {
1459                rc = ptlrpc_queue_wait(req);
1460                ptlrpc_req_finished(req);
1461        }
1462
1463        return rc;
1464}
1465EXPORT_SYMBOL(do_set_info_async);
1466
1467/* byte flipping routines for all wire types declared in
1468 * lustre_idl.h implemented here.
1469 */
1470void lustre_swab_ptlrpc_body(struct ptlrpc_body *b)
1471{
1472        __swab32s(&b->pb_type);
1473        __swab32s(&b->pb_version);
1474        __swab32s(&b->pb_opc);
1475        __swab32s(&b->pb_status);
1476        __swab64s(&b->pb_last_xid);
1477        __swab64s(&b->pb_last_seen);
1478        __swab64s(&b->pb_last_committed);
1479        __swab64s(&b->pb_transno);
1480        __swab32s(&b->pb_flags);
1481        __swab32s(&b->pb_op_flags);
1482        __swab32s(&b->pb_conn_cnt);
1483        __swab32s(&b->pb_timeout);
1484        __swab32s(&b->pb_service_time);
1485        __swab32s(&b->pb_limit);
1486        __swab64s(&b->pb_slv);
1487        __swab64s(&b->pb_pre_versions[0]);
1488        __swab64s(&b->pb_pre_versions[1]);
1489        __swab64s(&b->pb_pre_versions[2]);
1490        __swab64s(&b->pb_pre_versions[3]);
1491        CLASSERT(offsetof(typeof(*b), pb_padding) != 0);
1492        /* While we need to maintain compatibility between
1493         * clients and servers without ptlrpc_body_v2 (< 2.3)
1494         * do not swab any fields beyond pb_jobid, as we are
1495         * using this swab function for both ptlrpc_body
1496         * and ptlrpc_body_v2.
1497         */
1498        CLASSERT(offsetof(typeof(*b), pb_jobid) != 0);
1499}
1500EXPORT_SYMBOL(lustre_swab_ptlrpc_body);
1501
1502void lustre_swab_connect(struct obd_connect_data *ocd)
1503{
1504        __swab64s(&ocd->ocd_connect_flags);
1505        __swab32s(&ocd->ocd_version);
1506        __swab32s(&ocd->ocd_grant);
1507        __swab64s(&ocd->ocd_ibits_known);
1508        __swab32s(&ocd->ocd_index);
1509        __swab32s(&ocd->ocd_brw_size);
1510        /* ocd_blocksize and ocd_inodespace don't need to be swabbed because
1511         * they are 8-byte values
1512         */
1513        __swab16s(&ocd->ocd_grant_extent);
1514        __swab32s(&ocd->ocd_unused);
1515        __swab64s(&ocd->ocd_transno);
1516        __swab32s(&ocd->ocd_group);
1517        __swab32s(&ocd->ocd_cksum_types);
1518        __swab32s(&ocd->ocd_instance);
1519        /* Fields after ocd_cksum_types are only accessible by the receiver
1520         * if the corresponding flag in ocd_connect_flags is set. Accessing
1521         * any field after ocd_maxbytes on the receiver without a valid flag
1522         * may result in out-of-bound memory access and kernel oops.
1523         */
1524        if (ocd->ocd_connect_flags & OBD_CONNECT_MAX_EASIZE)
1525                __swab32s(&ocd->ocd_max_easize);
1526        if (ocd->ocd_connect_flags & OBD_CONNECT_MAXBYTES)
1527                __swab64s(&ocd->ocd_maxbytes);
1528        CLASSERT(offsetof(typeof(*ocd), padding1) != 0);
1529        CLASSERT(offsetof(typeof(*ocd), padding2) != 0);
1530        CLASSERT(offsetof(typeof(*ocd), padding3) != 0);
1531        CLASSERT(offsetof(typeof(*ocd), padding4) != 0);
1532        CLASSERT(offsetof(typeof(*ocd), padding5) != 0);
1533        CLASSERT(offsetof(typeof(*ocd), padding6) != 0);
1534        CLASSERT(offsetof(typeof(*ocd), padding7) != 0);
1535        CLASSERT(offsetof(typeof(*ocd), padding8) != 0);
1536        CLASSERT(offsetof(typeof(*ocd), padding9) != 0);
1537        CLASSERT(offsetof(typeof(*ocd), paddingA) != 0);
1538        CLASSERT(offsetof(typeof(*ocd), paddingB) != 0);
1539        CLASSERT(offsetof(typeof(*ocd), paddingC) != 0);
1540        CLASSERT(offsetof(typeof(*ocd), paddingD) != 0);
1541        CLASSERT(offsetof(typeof(*ocd), paddingE) != 0);
1542        CLASSERT(offsetof(typeof(*ocd), paddingF) != 0);
1543}
1544
1545static void lustre_swab_obdo(struct obdo *o)
1546{
1547        __swab64s(&o->o_valid);
1548        lustre_swab_ost_id(&o->o_oi);
1549        __swab64s(&o->o_parent_seq);
1550        __swab64s(&o->o_size);
1551        __swab64s(&o->o_mtime);
1552        __swab64s(&o->o_atime);
1553        __swab64s(&o->o_ctime);
1554        __swab64s(&o->o_blocks);
1555        __swab64s(&o->o_grant);
1556        __swab32s(&o->o_blksize);
1557        __swab32s(&o->o_mode);
1558        __swab32s(&o->o_uid);
1559        __swab32s(&o->o_gid);
1560        __swab32s(&o->o_flags);
1561        __swab32s(&o->o_nlink);
1562        __swab32s(&o->o_parent_oid);
1563        __swab32s(&o->o_misc);
1564        __swab64s(&o->o_ioepoch);
1565        __swab32s(&o->o_stripe_idx);
1566        __swab32s(&o->o_parent_ver);
1567        /* o_handle is opaque */
1568        /* o_lcookie is swabbed elsewhere */
1569        __swab32s(&o->o_uid_h);
1570        __swab32s(&o->o_gid_h);
1571        __swab64s(&o->o_data_version);
1572        CLASSERT(offsetof(typeof(*o), o_padding_4) != 0);
1573        CLASSERT(offsetof(typeof(*o), o_padding_5) != 0);
1574        CLASSERT(offsetof(typeof(*o), o_padding_6) != 0);
1575
1576}
1577
1578void lustre_swab_obd_statfs(struct obd_statfs *os)
1579{
1580        __swab64s(&os->os_type);
1581        __swab64s(&os->os_blocks);
1582        __swab64s(&os->os_bfree);
1583        __swab64s(&os->os_bavail);
1584        __swab64s(&os->os_files);
1585        __swab64s(&os->os_ffree);
1586        /* no need to swab os_fsid */
1587        __swab32s(&os->os_bsize);
1588        __swab32s(&os->os_namelen);
1589        __swab64s(&os->os_maxbytes);
1590        __swab32s(&os->os_state);
1591        CLASSERT(offsetof(typeof(*os), os_fprecreated) != 0);
1592        CLASSERT(offsetof(typeof(*os), os_spare2) != 0);
1593        CLASSERT(offsetof(typeof(*os), os_spare3) != 0);
1594        CLASSERT(offsetof(typeof(*os), os_spare4) != 0);
1595        CLASSERT(offsetof(typeof(*os), os_spare5) != 0);
1596        CLASSERT(offsetof(typeof(*os), os_spare6) != 0);
1597        CLASSERT(offsetof(typeof(*os), os_spare7) != 0);
1598        CLASSERT(offsetof(typeof(*os), os_spare8) != 0);
1599        CLASSERT(offsetof(typeof(*os), os_spare9) != 0);
1600}
1601EXPORT_SYMBOL(lustre_swab_obd_statfs);
1602
1603void lustre_swab_obd_ioobj(struct obd_ioobj *ioo)
1604{
1605        lustre_swab_ost_id(&ioo->ioo_oid);
1606        __swab32s(&ioo->ioo_max_brw);
1607        __swab32s(&ioo->ioo_bufcnt);
1608}
1609EXPORT_SYMBOL(lustre_swab_obd_ioobj);
1610
1611void lustre_swab_niobuf_remote(struct niobuf_remote *nbr)
1612{
1613        __swab64s(&nbr->offset);
1614        __swab32s(&nbr->len);
1615        __swab32s(&nbr->flags);
1616}
1617EXPORT_SYMBOL(lustre_swab_niobuf_remote);
1618
1619void lustre_swab_ost_body(struct ost_body *b)
1620{
1621        lustre_swab_obdo(&b->oa);
1622}
1623EXPORT_SYMBOL(lustre_swab_ost_body);
1624
1625void lustre_swab_ost_last_id(u64 *id)
1626{
1627        __swab64s(id);
1628}
1629EXPORT_SYMBOL(lustre_swab_ost_last_id);
1630
1631void lustre_swab_generic_32s(__u32 *val)
1632{
1633        __swab32s(val);
1634}
1635EXPORT_SYMBOL(lustre_swab_generic_32s);
1636
1637void lustre_swab_gl_desc(union ldlm_gl_desc *desc)
1638{
1639        lustre_swab_lu_fid(&desc->lquota_desc.gl_id.qid_fid);
1640        __swab64s(&desc->lquota_desc.gl_flags);
1641        __swab64s(&desc->lquota_desc.gl_ver);
1642        __swab64s(&desc->lquota_desc.gl_hardlimit);
1643        __swab64s(&desc->lquota_desc.gl_softlimit);
1644        __swab64s(&desc->lquota_desc.gl_time);
1645        CLASSERT(offsetof(typeof(desc->lquota_desc), gl_pad2) != 0);
1646}
1647
1648void lustre_swab_ost_lvb_v1(struct ost_lvb_v1 *lvb)
1649{
1650        __swab64s(&lvb->lvb_size);
1651        __swab64s(&lvb->lvb_mtime);
1652        __swab64s(&lvb->lvb_atime);
1653        __swab64s(&lvb->lvb_ctime);
1654        __swab64s(&lvb->lvb_blocks);
1655}
1656EXPORT_SYMBOL(lustre_swab_ost_lvb_v1);
1657
1658void lustre_swab_ost_lvb(struct ost_lvb *lvb)
1659{
1660        __swab64s(&lvb->lvb_size);
1661        __swab64s(&lvb->lvb_mtime);
1662        __swab64s(&lvb->lvb_atime);
1663        __swab64s(&lvb->lvb_ctime);
1664        __swab64s(&lvb->lvb_blocks);
1665        __swab32s(&lvb->lvb_mtime_ns);
1666        __swab32s(&lvb->lvb_atime_ns);
1667        __swab32s(&lvb->lvb_ctime_ns);
1668        __swab32s(&lvb->lvb_padding);
1669}
1670EXPORT_SYMBOL(lustre_swab_ost_lvb);
1671
1672void lustre_swab_lquota_lvb(struct lquota_lvb *lvb)
1673{
1674        __swab64s(&lvb->lvb_flags);
1675        __swab64s(&lvb->lvb_id_may_rel);
1676        __swab64s(&lvb->lvb_id_rel);
1677        __swab64s(&lvb->lvb_id_qunit);
1678        __swab64s(&lvb->lvb_pad1);
1679}
1680EXPORT_SYMBOL(lustre_swab_lquota_lvb);
1681
1682void lustre_swab_mdt_body(struct mdt_body *b)
1683{
1684        lustre_swab_lu_fid(&b->fid1);
1685        lustre_swab_lu_fid(&b->fid2);
1686        /* handle is opaque */
1687        __swab64s(&b->valid);
1688        __swab64s(&b->size);
1689        __swab64s(&b->mtime);
1690        __swab64s(&b->atime);
1691        __swab64s(&b->ctime);
1692        __swab64s(&b->blocks);
1693        __swab64s(&b->ioepoch);
1694        __swab64s(&b->t_state);
1695        __swab32s(&b->fsuid);
1696        __swab32s(&b->fsgid);
1697        __swab32s(&b->capability);
1698        __swab32s(&b->mode);
1699        __swab32s(&b->uid);
1700        __swab32s(&b->gid);
1701        __swab32s(&b->flags);
1702        __swab32s(&b->rdev);
1703        __swab32s(&b->nlink);
1704        CLASSERT(offsetof(typeof(*b), unused2) != 0);
1705        __swab32s(&b->suppgid);
1706        __swab32s(&b->eadatasize);
1707        __swab32s(&b->aclsize);
1708        __swab32s(&b->max_mdsize);
1709        __swab32s(&b->max_cookiesize);
1710        __swab32s(&b->uid_h);
1711        __swab32s(&b->gid_h);
1712        CLASSERT(offsetof(typeof(*b), padding_5) != 0);
1713}
1714EXPORT_SYMBOL(lustre_swab_mdt_body);
1715
1716void lustre_swab_mdt_ioepoch(struct mdt_ioepoch *b)
1717{
1718        /* handle is opaque */
1719         __swab64s(&b->ioepoch);
1720         __swab32s(&b->flags);
1721         CLASSERT(offsetof(typeof(*b), padding) != 0);
1722}
1723EXPORT_SYMBOL(lustre_swab_mdt_ioepoch);
1724
1725void lustre_swab_mgs_target_info(struct mgs_target_info *mti)
1726{
1727        int i;
1728
1729        __swab32s(&mti->mti_lustre_ver);
1730        __swab32s(&mti->mti_stripe_index);
1731        __swab32s(&mti->mti_config_ver);
1732        __swab32s(&mti->mti_flags);
1733        __swab32s(&mti->mti_instance);
1734        __swab32s(&mti->mti_nid_count);
1735        CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
1736        for (i = 0; i < MTI_NIDS_MAX; i++)
1737                __swab64s(&mti->mti_nids[i]);
1738}
1739EXPORT_SYMBOL(lustre_swab_mgs_target_info);
1740
1741void lustre_swab_mgs_nidtbl_entry(struct mgs_nidtbl_entry *entry)
1742{
1743        int i;
1744
1745        __swab64s(&entry->mne_version);
1746        __swab32s(&entry->mne_instance);
1747        __swab32s(&entry->mne_index);
1748        __swab32s(&entry->mne_length);
1749
1750        /* mne_nid_(count|type) must be one byte size because we're gonna
1751         * access it w/o swapping. */
1752        CLASSERT(sizeof(entry->mne_nid_count) == sizeof(__u8));
1753        CLASSERT(sizeof(entry->mne_nid_type) == sizeof(__u8));
1754
1755        /* remove this assertion if ipv6 is supported. */
1756        LASSERT(entry->mne_nid_type == 0);
1757        for (i = 0; i < entry->mne_nid_count; i++) {
1758                CLASSERT(sizeof(lnet_nid_t) == sizeof(__u64));
1759                __swab64s(&entry->u.nids[i]);
1760        }
1761}
1762EXPORT_SYMBOL(lustre_swab_mgs_nidtbl_entry);
1763
1764void lustre_swab_mgs_config_body(struct mgs_config_body *body)
1765{
1766        __swab64s(&body->mcb_offset);
1767        __swab32s(&body->mcb_units);
1768        __swab16s(&body->mcb_type);
1769}
1770EXPORT_SYMBOL(lustre_swab_mgs_config_body);
1771
1772void lustre_swab_mgs_config_res(struct mgs_config_res *body)
1773{
1774        __swab64s(&body->mcr_offset);
1775        __swab64s(&body->mcr_size);
1776}
1777EXPORT_SYMBOL(lustre_swab_mgs_config_res);
1778
1779static void lustre_swab_obd_dqinfo(struct obd_dqinfo *i)
1780{
1781        __swab64s(&i->dqi_bgrace);
1782        __swab64s(&i->dqi_igrace);
1783        __swab32s(&i->dqi_flags);
1784        __swab32s(&i->dqi_valid);
1785}
1786
1787static void lustre_swab_obd_dqblk(struct obd_dqblk *b)
1788{
1789        __swab64s(&b->dqb_ihardlimit);
1790        __swab64s(&b->dqb_isoftlimit);
1791        __swab64s(&b->dqb_curinodes);
1792        __swab64s(&b->dqb_bhardlimit);
1793        __swab64s(&b->dqb_bsoftlimit);
1794        __swab64s(&b->dqb_curspace);
1795        __swab64s(&b->dqb_btime);
1796        __swab64s(&b->dqb_itime);
1797        __swab32s(&b->dqb_valid);
1798        CLASSERT(offsetof(typeof(*b), dqb_padding) != 0);
1799}
1800
1801void lustre_swab_obd_quotactl(struct obd_quotactl *q)
1802{
1803        __swab32s(&q->qc_cmd);
1804        __swab32s(&q->qc_type);
1805        __swab32s(&q->qc_id);
1806        __swab32s(&q->qc_stat);
1807        lustre_swab_obd_dqinfo(&q->qc_dqinfo);
1808        lustre_swab_obd_dqblk(&q->qc_dqblk);
1809}
1810EXPORT_SYMBOL(lustre_swab_obd_quotactl);
1811
1812void lustre_swab_mdt_remote_perm(struct mdt_remote_perm *p)
1813{
1814        __swab32s(&p->rp_uid);
1815        __swab32s(&p->rp_gid);
1816        __swab32s(&p->rp_fsuid);
1817        __swab32s(&p->rp_fsuid_h);
1818        __swab32s(&p->rp_fsgid);
1819        __swab32s(&p->rp_fsgid_h);
1820        __swab32s(&p->rp_access_perm);
1821        __swab32s(&p->rp_padding);
1822};
1823EXPORT_SYMBOL(lustre_swab_mdt_remote_perm);
1824
1825void lustre_swab_fid2path(struct getinfo_fid2path *gf)
1826{
1827        lustre_swab_lu_fid(&gf->gf_fid);
1828        __swab64s(&gf->gf_recno);
1829        __swab32s(&gf->gf_linkno);
1830        __swab32s(&gf->gf_pathlen);
1831}
1832EXPORT_SYMBOL(lustre_swab_fid2path);
1833
1834static void lustre_swab_fiemap_extent(struct ll_fiemap_extent *fm_extent)
1835{
1836        __swab64s(&fm_extent->fe_logical);
1837        __swab64s(&fm_extent->fe_physical);
1838        __swab64s(&fm_extent->fe_length);
1839        __swab32s(&fm_extent->fe_flags);
1840        __swab32s(&fm_extent->fe_device);
1841}
1842
1843void lustre_swab_fiemap(struct ll_user_fiemap *fiemap)
1844{
1845        int i;
1846
1847        __swab64s(&fiemap->fm_start);
1848        __swab64s(&fiemap->fm_length);
1849        __swab32s(&fiemap->fm_flags);
1850        __swab32s(&fiemap->fm_mapped_extents);
1851        __swab32s(&fiemap->fm_extent_count);
1852        __swab32s(&fiemap->fm_reserved);
1853
1854        for (i = 0; i < fiemap->fm_mapped_extents; i++)
1855                lustre_swab_fiemap_extent(&fiemap->fm_extents[i]);
1856}
1857EXPORT_SYMBOL(lustre_swab_fiemap);
1858
1859void lustre_swab_mdt_rec_reint (struct mdt_rec_reint *rr)
1860{
1861        __swab32s(&rr->rr_opcode);
1862        __swab32s(&rr->rr_cap);
1863        __swab32s(&rr->rr_fsuid);
1864        /* rr_fsuid_h is unused */
1865        __swab32s(&rr->rr_fsgid);
1866        /* rr_fsgid_h is unused */
1867        __swab32s(&rr->rr_suppgid1);
1868        /* rr_suppgid1_h is unused */
1869        __swab32s(&rr->rr_suppgid2);
1870        /* rr_suppgid2_h is unused */
1871        lustre_swab_lu_fid(&rr->rr_fid1);
1872        lustre_swab_lu_fid(&rr->rr_fid2);
1873        __swab64s(&rr->rr_mtime);
1874        __swab64s(&rr->rr_atime);
1875        __swab64s(&rr->rr_ctime);
1876        __swab64s(&rr->rr_size);
1877        __swab64s(&rr->rr_blocks);
1878        __swab32s(&rr->rr_bias);
1879        __swab32s(&rr->rr_mode);
1880        __swab32s(&rr->rr_flags);
1881        __swab32s(&rr->rr_flags_h);
1882        __swab32s(&rr->rr_umask);
1883
1884        CLASSERT(offsetof(typeof(*rr), rr_padding_4) != 0);
1885};
1886EXPORT_SYMBOL(lustre_swab_mdt_rec_reint);
1887
1888void lustre_swab_lov_desc(struct lov_desc *ld)
1889{
1890        __swab32s(&ld->ld_tgt_count);
1891        __swab32s(&ld->ld_active_tgt_count);
1892        __swab32s(&ld->ld_default_stripe_count);
1893        __swab32s(&ld->ld_pattern);
1894        __swab64s(&ld->ld_default_stripe_size);
1895        __swab64s(&ld->ld_default_stripe_offset);
1896        __swab32s(&ld->ld_qos_maxage);
1897        /* uuid endian insensitive */
1898}
1899EXPORT_SYMBOL(lustre_swab_lov_desc);
1900
1901static void print_lum(struct lov_user_md *lum)
1902{
1903        CDEBUG(D_OTHER, "lov_user_md %p:\n", lum);
1904        CDEBUG(D_OTHER, "\tlmm_magic: %#x\n", lum->lmm_magic);
1905        CDEBUG(D_OTHER, "\tlmm_pattern: %#x\n", lum->lmm_pattern);
1906        CDEBUG(D_OTHER, "\tlmm_object_id: %llu\n", lmm_oi_id(&lum->lmm_oi));
1907        CDEBUG(D_OTHER, "\tlmm_object_gr: %llu\n", lmm_oi_seq(&lum->lmm_oi));
1908        CDEBUG(D_OTHER, "\tlmm_stripe_size: %#x\n", lum->lmm_stripe_size);
1909        CDEBUG(D_OTHER, "\tlmm_stripe_count: %#x\n", lum->lmm_stripe_count);
1910        CDEBUG(D_OTHER, "\tlmm_stripe_offset/lmm_layout_gen: %#x\n",
1911               lum->lmm_stripe_offset);
1912}
1913
1914static void lustre_swab_lmm_oi(struct ost_id *oi)
1915{
1916        __swab64s(&oi->oi.oi_id);
1917        __swab64s(&oi->oi.oi_seq);
1918}
1919
1920static void lustre_swab_lov_user_md_common(struct lov_user_md_v1 *lum)
1921{
1922        __swab32s(&lum->lmm_magic);
1923        __swab32s(&lum->lmm_pattern);
1924        lustre_swab_lmm_oi(&lum->lmm_oi);
1925        __swab32s(&lum->lmm_stripe_size);
1926        __swab16s(&lum->lmm_stripe_count);
1927        __swab16s(&lum->lmm_stripe_offset);
1928        print_lum(lum);
1929}
1930
1931void lustre_swab_lov_user_md_v1(struct lov_user_md_v1 *lum)
1932{
1933        CDEBUG(D_IOCTL, "swabbing lov_user_md v1\n");
1934        lustre_swab_lov_user_md_common(lum);
1935}
1936EXPORT_SYMBOL(lustre_swab_lov_user_md_v1);
1937
1938void lustre_swab_lov_user_md_v3(struct lov_user_md_v3 *lum)
1939{
1940        CDEBUG(D_IOCTL, "swabbing lov_user_md v3\n");
1941        lustre_swab_lov_user_md_common((struct lov_user_md_v1 *)lum);
1942        /* lmm_pool_name nothing to do with char */
1943}
1944EXPORT_SYMBOL(lustre_swab_lov_user_md_v3);
1945
1946void lustre_swab_lov_mds_md(struct lov_mds_md *lmm)
1947{
1948        CDEBUG(D_IOCTL, "swabbing lov_mds_md\n");
1949        __swab32s(&lmm->lmm_magic);
1950        __swab32s(&lmm->lmm_pattern);
1951        lustre_swab_lmm_oi(&lmm->lmm_oi);
1952        __swab32s(&lmm->lmm_stripe_size);
1953        __swab16s(&lmm->lmm_stripe_count);
1954        __swab16s(&lmm->lmm_layout_gen);
1955}
1956EXPORT_SYMBOL(lustre_swab_lov_mds_md);
1957
1958void lustre_swab_lov_user_md_objects(struct lov_user_ost_data *lod,
1959                                     int stripe_count)
1960{
1961        int i;
1962
1963        for (i = 0; i < stripe_count; i++) {
1964                lustre_swab_ost_id(&(lod[i].l_ost_oi));
1965                __swab32s(&(lod[i].l_ost_gen));
1966                __swab32s(&(lod[i].l_ost_idx));
1967        }
1968}
1969EXPORT_SYMBOL(lustre_swab_lov_user_md_objects);
1970
1971static void lustre_swab_ldlm_res_id(struct ldlm_res_id *id)
1972{
1973        int i;
1974
1975        for (i = 0; i < RES_NAME_SIZE; i++)
1976                __swab64s(&id->name[i]);
1977}
1978
1979static void lustre_swab_ldlm_policy_data(ldlm_wire_policy_data_t *d)
1980{
1981        /* the lock data is a union and the first two fields are always an
1982         * extent so it's ok to process an LDLM_EXTENT and LDLM_FLOCK lock
1983         * data the same way.
1984         */
1985        __swab64s(&d->l_extent.start);
1986        __swab64s(&d->l_extent.end);
1987        __swab64s(&d->l_extent.gid);
1988        __swab64s(&d->l_flock.lfw_owner);
1989        __swab32s(&d->l_flock.lfw_pid);
1990}
1991
1992void lustre_swab_ldlm_intent(struct ldlm_intent *i)
1993{
1994        __swab64s(&i->opc);
1995}
1996EXPORT_SYMBOL(lustre_swab_ldlm_intent);
1997
1998static void lustre_swab_ldlm_resource_desc(struct ldlm_resource_desc *r)
1999{
2000        __swab32s(&r->lr_type);
2001        CLASSERT(offsetof(typeof(*r), lr_padding) != 0);
2002        lustre_swab_ldlm_res_id(&r->lr_name);
2003}
2004
2005static void lustre_swab_ldlm_lock_desc(struct ldlm_lock_desc *l)
2006{
2007        lustre_swab_ldlm_resource_desc(&l->l_resource);
2008        __swab32s(&l->l_req_mode);
2009        __swab32s(&l->l_granted_mode);
2010        lustre_swab_ldlm_policy_data(&l->l_policy_data);
2011}
2012
2013void lustre_swab_ldlm_request(struct ldlm_request *rq)
2014{
2015        __swab32s(&rq->lock_flags);
2016        lustre_swab_ldlm_lock_desc(&rq->lock_desc);
2017        __swab32s(&rq->lock_count);
2018        /* lock_handle[] opaque */
2019}
2020EXPORT_SYMBOL(lustre_swab_ldlm_request);
2021
2022void lustre_swab_ldlm_reply(struct ldlm_reply *r)
2023{
2024        __swab32s(&r->lock_flags);
2025        CLASSERT(offsetof(typeof(*r), lock_padding) != 0);
2026        lustre_swab_ldlm_lock_desc(&r->lock_desc);
2027        /* lock_handle opaque */
2028        __swab64s(&r->lock_policy_res1);
2029        __swab64s(&r->lock_policy_res2);
2030}
2031EXPORT_SYMBOL(lustre_swab_ldlm_reply);
2032
2033/* Dump functions */
2034void dump_ioo(struct obd_ioobj *ioo)
2035{
2036        CDEBUG(D_RPCTRACE,
2037               "obd_ioobj: ioo_oid=" DOSTID ", ioo_max_brw=%#x, ioo_bufct=%d\n",
2038               POSTID(&ioo->ioo_oid), ioo->ioo_max_brw,
2039               ioo->ioo_bufcnt);
2040}
2041EXPORT_SYMBOL(dump_ioo);
2042
2043void dump_rniobuf(struct niobuf_remote *nb)
2044{
2045        CDEBUG(D_RPCTRACE, "niobuf_remote: offset=%llu, len=%d, flags=%x\n",
2046               nb->offset, nb->len, nb->flags);
2047}
2048EXPORT_SYMBOL(dump_rniobuf);
2049
2050static void dump_obdo(struct obdo *oa)
2051{
2052        __u32 valid = oa->o_valid;
2053
2054        CDEBUG(D_RPCTRACE, "obdo: o_valid = %08x\n", valid);
2055        if (valid & OBD_MD_FLID)
2056                CDEBUG(D_RPCTRACE, "obdo: id = "DOSTID"\n", POSTID(&oa->o_oi));
2057        if (valid & OBD_MD_FLFID)
2058                CDEBUG(D_RPCTRACE, "obdo: o_parent_seq = %#llx\n",
2059                       oa->o_parent_seq);
2060        if (valid & OBD_MD_FLSIZE)
2061                CDEBUG(D_RPCTRACE, "obdo: o_size = %lld\n", oa->o_size);
2062        if (valid & OBD_MD_FLMTIME)
2063                CDEBUG(D_RPCTRACE, "obdo: o_mtime = %lld\n", oa->o_mtime);
2064        if (valid & OBD_MD_FLATIME)
2065                CDEBUG(D_RPCTRACE, "obdo: o_atime = %lld\n", oa->o_atime);
2066        if (valid & OBD_MD_FLCTIME)
2067                CDEBUG(D_RPCTRACE, "obdo: o_ctime = %lld\n", oa->o_ctime);
2068        if (valid & OBD_MD_FLBLOCKS)   /* allocation of space */
2069                CDEBUG(D_RPCTRACE, "obdo: o_blocks = %lld\n", oa->o_blocks);
2070        if (valid & OBD_MD_FLGRANT)
2071                CDEBUG(D_RPCTRACE, "obdo: o_grant = %lld\n", oa->o_grant);
2072        if (valid & OBD_MD_FLBLKSZ)
2073                CDEBUG(D_RPCTRACE, "obdo: o_blksize = %d\n", oa->o_blksize);
2074        if (valid & (OBD_MD_FLTYPE | OBD_MD_FLMODE))
2075                CDEBUG(D_RPCTRACE, "obdo: o_mode = %o\n",
2076                       oa->o_mode & ((valid & OBD_MD_FLTYPE ?  S_IFMT : 0) |
2077                                     (valid & OBD_MD_FLMODE ? ~S_IFMT : 0)));
2078        if (valid & OBD_MD_FLUID)
2079                CDEBUG(D_RPCTRACE, "obdo: o_uid = %u\n", oa->o_uid);
2080        if (valid & OBD_MD_FLUID)
2081                CDEBUG(D_RPCTRACE, "obdo: o_uid_h = %u\n", oa->o_uid_h);
2082        if (valid & OBD_MD_FLGID)
2083                CDEBUG(D_RPCTRACE, "obdo: o_gid = %u\n", oa->o_gid);
2084        if (valid & OBD_MD_FLGID)
2085                CDEBUG(D_RPCTRACE, "obdo: o_gid_h = %u\n", oa->o_gid_h);
2086        if (valid & OBD_MD_FLFLAGS)
2087                CDEBUG(D_RPCTRACE, "obdo: o_flags = %x\n", oa->o_flags);
2088        if (valid & OBD_MD_FLNLINK)
2089                CDEBUG(D_RPCTRACE, "obdo: o_nlink = %u\n", oa->o_nlink);
2090        else if (valid & OBD_MD_FLCKSUM)
2091                CDEBUG(D_RPCTRACE, "obdo: o_checksum (o_nlink) = %u\n",
2092                       oa->o_nlink);
2093        if (valid & OBD_MD_FLGENER)
2094                CDEBUG(D_RPCTRACE, "obdo: o_parent_oid = %x\n",
2095                       oa->o_parent_oid);
2096        if (valid & OBD_MD_FLEPOCH)
2097                CDEBUG(D_RPCTRACE, "obdo: o_ioepoch = %lld\n",
2098                       oa->o_ioepoch);
2099        if (valid & OBD_MD_FLFID) {
2100                CDEBUG(D_RPCTRACE, "obdo: o_stripe_idx = %u\n",
2101                       oa->o_stripe_idx);
2102                CDEBUG(D_RPCTRACE, "obdo: o_parent_ver = %x\n",
2103                       oa->o_parent_ver);
2104        }
2105        if (valid & OBD_MD_FLHANDLE)
2106                CDEBUG(D_RPCTRACE, "obdo: o_handle = %lld\n",
2107                       oa->o_handle.cookie);
2108        if (valid & OBD_MD_FLCOOKIE)
2109                CDEBUG(D_RPCTRACE, "obdo: o_lcookie = (llog_cookie dumping not yet implemented)\n");
2110}
2111
2112void dump_ost_body(struct ost_body *ob)
2113{
2114        dump_obdo(&ob->oa);
2115}
2116EXPORT_SYMBOL(dump_ost_body);
2117
2118void dump_rcs(__u32 *rc)
2119{
2120        CDEBUG(D_RPCTRACE, "rmf_rcs: %d\n", *rc);
2121}
2122EXPORT_SYMBOL(dump_rcs);
2123
2124static inline int req_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2125{
2126        LASSERT(req->rq_reqmsg);
2127
2128        switch (req->rq_reqmsg->lm_magic) {
2129        case LUSTRE_MSG_MAGIC_V2:
2130                return lustre_req_swabbed(req, MSG_PTLRPC_BODY_OFF);
2131        default:
2132                CERROR("bad lustre msg magic: %#08X\n",
2133                       req->rq_reqmsg->lm_magic);
2134        }
2135        return 0;
2136}
2137
2138static inline int rep_ptlrpc_body_swabbed(struct ptlrpc_request *req)
2139{
2140        LASSERT(req->rq_repmsg);
2141
2142        switch (req->rq_repmsg->lm_magic) {
2143        case LUSTRE_MSG_MAGIC_V2:
2144                return lustre_rep_swabbed(req, MSG_PTLRPC_BODY_OFF);
2145        default:
2146                /* uninitialized yet */
2147                return 0;
2148        }
2149}
2150
2151void _debug_req(struct ptlrpc_request *req,
2152                struct libcfs_debug_msg_data *msgdata,
2153                const char *fmt, ...)
2154{
2155        int req_ok = req->rq_reqmsg != NULL;
2156        int rep_ok = req->rq_repmsg != NULL;
2157        lnet_nid_t nid = LNET_NID_ANY;
2158        va_list args;
2159
2160        if (ptlrpc_req_need_swab(req)) {
2161                req_ok = req_ok && req_ptlrpc_body_swabbed(req);
2162                rep_ok = rep_ok && rep_ptlrpc_body_swabbed(req);
2163        }
2164
2165        if (req->rq_import && req->rq_import->imp_connection)
2166                nid = req->rq_import->imp_connection->c_peer.nid;
2167        else if (req->rq_export && req->rq_export->exp_connection)
2168                nid = req->rq_export->exp_connection->c_peer.nid;
2169
2170        va_start(args, fmt);
2171        libcfs_debug_vmsg2(msgdata, fmt, args,
2172                           " req@%p x%llu/t%lld(%lld) o%d->%s@%s:%d/%d lens %d/%d e %d to %lld dl %lld ref %d fl " REQ_FLAGS_FMT "/%x/%x rc %d/%d\n",
2173                           req, req->rq_xid, req->rq_transno,
2174                           req_ok ? lustre_msg_get_transno(req->rq_reqmsg) : 0,
2175                           req_ok ? lustre_msg_get_opc(req->rq_reqmsg) : -1,
2176                           req->rq_import ?
2177                           req->rq_import->imp_obd->obd_name :
2178                           req->rq_export ?
2179                           req->rq_export->exp_client_uuid.uuid :
2180                           "<?>",
2181                           libcfs_nid2str(nid),
2182                           req->rq_request_portal, req->rq_reply_portal,
2183                           req->rq_reqlen, req->rq_replen,
2184                           req->rq_early_count, (s64)req->rq_timedout,
2185                           (s64)req->rq_deadline,
2186                           atomic_read(&req->rq_refcount),
2187                           DEBUG_REQ_FLAGS(req),
2188                           req_ok ? lustre_msg_get_flags(req->rq_reqmsg) : -1,
2189                           rep_ok ? lustre_msg_get_flags(req->rq_repmsg) : -1,
2190                           req->rq_status,
2191                           rep_ok ? lustre_msg_get_status(req->rq_repmsg) : -1);
2192        va_end(args);
2193}
2194EXPORT_SYMBOL(_debug_req);
2195
2196void lustre_swab_lustre_capa(struct lustre_capa *c)
2197{
2198        lustre_swab_lu_fid(&c->lc_fid);
2199        __swab64s(&c->lc_opc);
2200        __swab64s(&c->lc_uid);
2201        __swab64s(&c->lc_gid);
2202        __swab32s(&c->lc_flags);
2203        __swab32s(&c->lc_keyid);
2204        __swab32s(&c->lc_timeout);
2205        __swab32s(&c->lc_expiry);
2206}
2207EXPORT_SYMBOL(lustre_swab_lustre_capa);
2208
2209void lustre_swab_hsm_user_state(struct hsm_user_state *state)
2210{
2211        __swab32s(&state->hus_states);
2212        __swab32s(&state->hus_archive_id);
2213}
2214EXPORT_SYMBOL(lustre_swab_hsm_user_state);
2215
2216void lustre_swab_hsm_state_set(struct hsm_state_set *hss)
2217{
2218        __swab32s(&hss->hss_valid);
2219        __swab64s(&hss->hss_setmask);
2220        __swab64s(&hss->hss_clearmask);
2221        __swab32s(&hss->hss_archive_id);
2222}
2223EXPORT_SYMBOL(lustre_swab_hsm_state_set);
2224
2225static void lustre_swab_hsm_extent(struct hsm_extent *extent)
2226{
2227        __swab64s(&extent->offset);
2228        __swab64s(&extent->length);
2229}
2230
2231void lustre_swab_hsm_current_action(struct hsm_current_action *action)
2232{
2233        __swab32s(&action->hca_state);
2234        __swab32s(&action->hca_action);
2235        lustre_swab_hsm_extent(&action->hca_location);
2236}
2237EXPORT_SYMBOL(lustre_swab_hsm_current_action);
2238
2239void lustre_swab_hsm_user_item(struct hsm_user_item *hui)
2240{
2241        lustre_swab_lu_fid(&hui->hui_fid);
2242        lustre_swab_hsm_extent(&hui->hui_extent);
2243}
2244EXPORT_SYMBOL(lustre_swab_hsm_user_item);
2245
2246void lustre_swab_layout_intent(struct layout_intent *li)
2247{
2248        __swab32s(&li->li_opc);
2249        __swab32s(&li->li_flags);
2250        __swab64s(&li->li_start);
2251        __swab64s(&li->li_end);
2252}
2253EXPORT_SYMBOL(lustre_swab_layout_intent);
2254
2255void lustre_swab_hsm_progress_kernel(struct hsm_progress_kernel *hpk)
2256{
2257        lustre_swab_lu_fid(&hpk->hpk_fid);
2258        __swab64s(&hpk->hpk_cookie);
2259        __swab64s(&hpk->hpk_extent.offset);
2260        __swab64s(&hpk->hpk_extent.length);
2261        __swab16s(&hpk->hpk_flags);
2262        __swab16s(&hpk->hpk_errval);
2263}
2264EXPORT_SYMBOL(lustre_swab_hsm_progress_kernel);
2265
2266void lustre_swab_hsm_request(struct hsm_request *hr)
2267{
2268        __swab32s(&hr->hr_action);
2269        __swab32s(&hr->hr_archive_id);
2270        __swab64s(&hr->hr_flags);
2271        __swab32s(&hr->hr_itemcount);
2272        __swab32s(&hr->hr_data_len);
2273}
2274EXPORT_SYMBOL(lustre_swab_hsm_request);
2275
2276void lustre_swab_swap_layouts(struct mdc_swap_layouts *msl)
2277{
2278        __swab64s(&msl->msl_flags);
2279}
2280EXPORT_SYMBOL(lustre_swab_swap_layouts);
2281
2282void lustre_swab_close_data(struct close_data *cd)
2283{
2284        lustre_swab_lu_fid(&cd->cd_fid);
2285        __swab64s(&cd->cd_data_version);
2286}
2287EXPORT_SYMBOL(lustre_swab_close_data);
2288