Whamcloud - gitweb
Removes all traces of mds_req, mds_rep, ost_req, and ost_rep. All subsystems
[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 EXPORT_SYMTAB
26
27 #define DEBUG_SUBSYSTEM S_CLASS
28
29 #include <linux/obd_class.h>
30 #include <linux/lustre_net.h>
31
32 int lustre_pack_msg(int count, int *lens, char **bufs, int *len, char **buf)
33 {
34         char *ptr;
35         struct lustre_msg *m;
36         int size = 0, i;
37
38         for (i = 0; i < count; i++)
39                 size += size_round(lens[i]);
40
41         *len = sizeof(*m) + count * sizeof(__u32) + size;
42
43         OBD_ALLOC(*buf, *len);
44         if (!*buf)
45                 RETURN(-ENOMEM);
46
47         m = (struct lustre_msg *)(*buf);
48         m->bufcount = HTON__u32(count);
49         for (i = 0; i < count; i++)
50                 m->buflens[i] = HTON__u32(lens[i]);
51
52         ptr = *buf + sizeof(*m) + sizeof(__u32) * count;
53         for (i = 0; i < count; i++) {
54                 char *tmp = NULL;
55                 if (bufs)
56                         tmp = bufs[i];
57                 LOGL(tmp, lens[i], ptr);
58         }
59
60         return 0;
61 }
62
63 /* This returns the size of the buffer that is required to hold a lustre_msg
64  * with the given sub-buffer lengths. */
65 int lustre_msg_size(int count, int *lengths)
66 {
67         int size = sizeof(struct lustre_msg), i;
68
69         for (i = 0; i < count; i++)
70                 size += size_round(lengths[i]);
71
72         size += count * sizeof(__u32);
73
74         return size;
75 }
76
77 int lustre_unpack_msg(char *buf, int len)
78 {
79         struct lustre_msg *m = (struct lustre_msg *)buf;
80         int required_len, i;
81
82         required_len = sizeof(*m);
83         if (len < required_len)
84                 RETURN(-EINVAL);
85
86         m->opc = NTOH__u32(m->opc);
87         m->xid = NTOH__u32(m->xid);
88         m->status = NTOH__u32(m->status);
89         m->type = NTOH__u32(m->type);
90         m->connid = NTOH__u32(m->connid);
91         m->bufcount = NTOH__u32(m->bufcount);
92
93         required_len += m->bufcount * sizeof(__u32);
94         if (len < required_len)
95                 RETURN(-EINVAL);
96
97         for (i = 0; i < m->bufcount; i++) {
98                 m->buflens[i] = NTOH__u32(m->buflens[i]);
99                 required_len += size_round(m->buflens[i]);
100         }
101
102         if (len < required_len) {
103                 CERROR("len: %d, required_len %d\n", len, required_len);
104                 RETURN(-EINVAL);
105         }
106
107         RETURN(0);
108 }
109
110 void *lustre_msg_buf(struct lustre_msg *m, int n)
111 {
112         int i, offset;
113
114         if (n < 0 || n >= m->bufcount) {
115                 CERROR("referencing bad sub buffer!\n");
116                 LBUG();
117                 return NULL;
118         }
119
120         if (m->buflens[n] == 0)
121                 return NULL;
122
123         offset = sizeof(*m) + m->bufcount * sizeof(__u32);
124
125         for (i = 0; i < n; i++)
126                 offset += size_round(m->buflens[i]);
127
128         return (char *)m + offset;
129 }