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