Whamcloud - gitweb
Merge b_md into HEAD
[fs/lustre-release.git] / lustre / ptlrpc / pack_generic.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * (Un)packing of OST requests
22  *
23  */
24
25 #define DEBUG_SUBSYSTEM S_RPC
26
27 #include <linux/obd_support.h>
28 #include <linux/lustre_net.h>
29
30 int lustre_pack_msg(int count, int *lens, char **bufs, int *len,
31                     struct lustre_msg **msg)
32 {
33         char *ptr;
34         struct lustre_msg *m;
35         int size = 0, i;
36
37         for (i = 0; i < count; i++)
38                 size += size_round(lens[i]);
39
40         *len = size_round(sizeof(*m) + count * sizeof(__u32)) + size;
41
42         OBD_ALLOC(*msg, *len);
43         if (!*msg)
44                 RETURN(-ENOMEM);
45
46         m = *msg;
47         m->bufcount = HTON__u32(count);
48         for (i = 0; i < count; i++)
49                 m->buflens[i] = HTON__u32(lens[i]);
50
51         ptr = (char *)m + size_round(sizeof(*m) + count * sizeof(__u32));
52         for (i = 0; i < count; i++) {
53                 char *tmp = NULL;
54                 if (bufs)
55                         tmp = bufs[i];
56                 LOGL(tmp, lens[i], ptr);
57         }
58
59         return 0;
60 }
61
62 /* This returns the size of the buffer that is required to hold a lustre_msg
63  * with the given sub-buffer lengths. */
64 int lustre_msg_size(int count, int *lengths)
65 {
66         int size = 0, i;
67
68         for (i = 0; i < count; i++)
69                 size += size_round(lengths[i]);
70
71         size += size_round(sizeof(struct lustre_msg) + count * sizeof(__u32));
72
73         return size;
74 }
75
76 int lustre_unpack_msg(struct lustre_msg *m, int len)
77 {
78         int required_len, i;
79         ENTRY;
80
81         required_len = size_round(sizeof(*m));
82         if (len < required_len)
83                 RETURN(-EINVAL);
84
85         m->opc = NTOH__u32(m->opc);
86         m->status = NTOH__u32(m->status);
87         m->type = NTOH__u32(m->type);
88         m->bufcount = NTOH__u32(m->bufcount);
89         m->last_xid = NTOH__u64(m->last_xid);
90         m->last_committed = NTOH__u64(m->last_committed);
91
92         required_len = size_round(sizeof(*m) + m->bufcount * sizeof(__u32));
93         if (len < required_len)
94                 RETURN(-EINVAL);
95
96         for (i = 0; i < m->bufcount; i++) {
97                 m->buflens[i] = NTOH__u32(m->buflens[i]);
98                 required_len += size_round(m->buflens[i]);
99         }
100
101         if (len < required_len) {
102                 CERROR("len: %d, required_len %d\n", len, required_len);
103                 CERROR("bufcount: %d\n", m->bufcount);
104                 for (i = 0; i < m->bufcount; i++)
105                         CERROR("buffer %d length %d\n", i, m->buflens[i]);
106                 RETURN(-EINVAL);
107         }
108
109         RETURN(0);
110 }
111
112 void *lustre_msg_buf(struct lustre_msg *m, int n)
113 {
114         int i, offset;
115
116         if (!m) {
117                 CERROR("no message buffer!\n");
118                 LBUG();
119                 return NULL;
120         }
121
122         if (n < 0 || n >= m->bufcount) {
123                 CERROR("referencing bad sub buffer in %p (want %d, count "
124                        "%d)!\n", m, n, m->bufcount);
125                 LBUG();
126                 return NULL;
127         }
128
129         if (m->buflens[n] == 0) {
130                 CERROR("zero-length buffer requested for buffer %d in %p\n",
131                        n, m);
132                 return NULL;
133         }
134
135         offset = size_round(sizeof(*m) + m->bufcount * sizeof(__u32));
136
137         for (i = 0; i < n; i++)
138                 offset += size_round(m->buflens[i]);
139
140         return (char *)m + offset;
141 }