Whamcloud - gitweb
af4ab6863206fc7d3f7dd6674abed74e52329357
[fs/lustre-release.git] / lustre / lib / mds_pack.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 MDS and OST request records
22  *
23  */
24
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/major.h>
29 #include <linux/sched.h>
30 #include <linux/lp.h>
31 #include <linux/slab.h>
32 #include <linux/ioport.h>
33 #include <linux/fcntl.h>
34 #include <linux/delay.h>
35 #include <linux/skbuff.h>
36 #include <linux/proc_fs.h>
37 #include <linux/vmalloc.h>
38 #include <linux/fs.h>
39 #include <linux/poll.h>
40 #include <linux/init.h>
41 #include <linux/list.h>
42 #include <asm/io.h>
43 #include <asm/segment.h>
44 #include <asm/system.h>
45 #include <asm/poll.h>
46 #include <asm/uaccess.h>
47
48 #include <linux/obd_support.h>
49 #include <linux/lustre_lib.h>
50 #include <linux/lustre_idl.h>
51 #include <linux/lustre_mds.h>
52
53
54 int mds_pack_req(char *name, int namelen, char *tgt, int tgtlen, 
55                  struct mds_req_hdr **hdr, struct mds_req **req, 
56                  int *len, char **buf)
57 {
58         char *ptr;
59         struct mds_req_packed *preq;
60
61         *len = sizeof(**hdr) + size_round(namelen) + size_round(tgtlen) + 
62                 sizeof(*preq); 
63
64         *buf = kmalloc(*len, GFP_KERNEL);
65         if (!*buf) {
66                 EXIT;
67                 return -ENOMEM;
68         }
69
70         memset(*buf, 0, *len); 
71         *hdr = (struct mds_req_hdr *)(*buf);
72         *req = (struct mds_req *)(*buf + sizeof(**hdr));
73
74         preq = (struct mds_req_packed *)(*buf + sizeof(**hdr));
75         ptr = *buf + sizeof(**hdr) + sizeof(*preq);
76
77         (*hdr)->type =  MDS_TYPE_REQ;
78
79         (*req)->namelen = NTOH__u32(namelen);
80         if (name) { 
81                 LOGL(name, namelen, ptr); 
82         } 
83
84         (*req)->tgtlen = NTOH__u32(tgtlen);
85         if (tgt) {
86                 LOGL(tgt, tgtlen, ptr);
87         }
88         return 0;
89 }
90
91
92 int mds_unpack_req(char *buf, int len, 
93                    struct mds_req_hdr **hdr, struct mds_req **req)
94 {
95         struct mds_req_packed *preq;
96         char *name, *tgt;
97
98         if (len < sizeof(**hdr) + sizeof(**req)) { 
99                 EXIT;
100                 return -EINVAL;
101         }
102
103         *hdr = (struct mds_req_hdr *) (buf);
104         preq = (struct mds_req_packed *) (buf + sizeof(**hdr));
105
106         *req = (struct mds_req *) (buf + sizeof(**hdr));
107         (*req)->namelen = NTOH__u32((*req)->namelen); 
108         (*req)->tgtlen = NTOH__u32((*req)->tgtlen); 
109
110         if (len < sizeof(**hdr) + sizeof(**req) + (*req)->namelen + 
111             (*req)->tgtlen ) { 
112                 EXIT;
113                 return -EINVAL;
114         }
115
116         if ((*req)->namelen) { 
117                 name = buf + sizeof(**hdr) + sizeof(*preq);
118         } else { 
119                 name = NULL;
120         }
121
122         if ((*req)->tgtlen) { 
123                 tgt = buf + sizeof(**hdr) + sizeof(*preq) + 
124                         size_round((*req)->namelen);
125         } else { 
126                 tgt = NULL;
127         }
128
129         EXIT;
130         return 0;
131 }
132
133 void *mds_req_tgt(struct mds_req *req)
134 {
135         if (!req->tgtlen) 
136                 return NULL;
137         return (void *)((char *)req + sizeof(*req) + size_round(req->namelen)); 
138 }
139
140 int mds_pack_rep(char *name, int namelen, char *tgt, int tgtlen, 
141                  struct mds_rep_hdr **hdr, struct mds_rep **rep, 
142                  int *len, char **buf)
143 {
144         char *ptr;
145         struct mds_rep_packed *prep;
146
147         *len = sizeof(**hdr) + size_round(namelen) + size_round(tgtlen) + 
148                 sizeof(*prep); 
149
150         *buf = kmalloc(*len, GFP_KERNEL);
151         if (!*buf) {
152                 EXIT;
153                 return -ENOMEM;
154         }
155
156         memset(*buf, 0, *len); 
157         *hdr = (struct mds_rep_hdr *)(*buf);
158         *rep = (struct mds_rep *)(*buf + sizeof(**hdr));
159
160         prep = (struct mds_rep_packed *)(*buf + sizeof(**hdr));
161         ptr = *buf + sizeof(**hdr) + sizeof(*prep);
162
163         (*hdr)->type =  MDS_TYPE_REP;
164
165         (*rep)->namelen = NTOH__u32(namelen);
166         if (name) { 
167                 LOGL(name, namelen, ptr); 
168         } 
169
170         (*rep)->tgtlen = NTOH__u32(tgtlen);
171         if (tgt) { 
172                 LOGL(tgt, tgtlen, ptr);
173         }
174         return 0;
175 }
176
177
178 int mds_unpack_rep(char *buf, int len, 
179                    struct mds_rep_hdr **hdr, struct mds_rep **rep)
180 {
181         struct mds_rep_packed *prep;
182
183         if (len < sizeof(**hdr)) { 
184                 EXIT;
185                 return -EINVAL;
186         }
187         *hdr = (struct mds_rep_hdr *) (buf);
188
189         if (len < sizeof(**hdr) + sizeof(**rep)) { 
190                 EXIT;
191                 return -EINVAL;
192         }
193
194         prep = (struct mds_rep_packed *) (buf + sizeof(**hdr));
195
196         *rep = (struct mds_rep *) (buf + sizeof(**hdr));
197         (*rep)->namelen = NTOH__u32((*rep)->namelen); 
198         (*rep)->tgtlen = NTOH__u32((*rep)->namelen); 
199
200         if (len < sizeof(**hdr) + sizeof(**rep) + (*rep)->namelen + 
201             (*rep)->tgtlen ) { 
202                 EXIT;
203                 return -EINVAL;
204         }
205
206         EXIT;
207         return 0;
208 }
209
210 void *mds_rep_tgt(struct mds_rep *rep)
211 {
212         if (!rep->tgtlen) 
213                 return NULL;
214         return (void *)((char *)rep + sizeof(*rep) + size_round(rep->namelen)); 
215 }
216
217 #if 0
218 int mds_pack_rep(char *name, int namelen, char *tgt, int tgtlen, 
219                  struct mds_rep_hdr **hdr, struct mds_rep **rep, 
220                  int *len, char **buf)
221 {
222         char *ptr;
223
224         *len = sizeof(**hdr) + size_round(namelen) + size_round(tgtlen) + 
225                 sizeof(**rep); 
226
227         *buf = kmalloc(*len, GFP_KERNEL);
228         if (!*buf) {
229                 EXIT;
230                 return -ENOMEM;
231         }
232
233         memset(*buf, 0, *len); 
234         *hdr = (struct mds_rep_hdr *)(*buf);
235         *rep = (struct mds_rep *)(*buf + sizeof(**hdr));
236         ptr = *buf + sizeof(**hdr) + sizeof(**rep);
237
238         (*rep)->namelen = NTOH__u32(namelen);
239         if (name) { 
240                 LOGL(name, namelen, ptr); 
241         } 
242
243         (*rep)->tgtlen = NTOH__u32(tgtlen);
244         if (tgt) { 
245                 LOGL(tgt, tgtlen, ptr);
246         }
247         return 0;
248 }
249
250
251 int mds_unpack_rep(char *buf, int len, 
252                    struct mds_rep_hdr **hdr, struct mds_rep **rep)
253 {
254         if (len < sizeof(**hdr) + sizeof(**rep)) { 
255                 EXIT;
256                 return -EINVAL;
257         }
258
259         *hdr = (struct mds_rep_hdr *) (buf);
260         *rep = (struct mds_rep *) (buf + sizeof(**hdr));
261         (*rep)->namelen = NTOH__u32((*rep)->namelen); 
262         (*rep)->tgtlen = NTOH__u32((*rep)->namelen); 
263
264         if (len < sizeof(**hdr) + sizeof(**rep) + (*rep)->namelen + 
265             (*rep)->tgtlen ) { 
266                 EXIT;
267                 return -EINVAL;
268         }
269
270         if ((*rep)->namelen) { 
271                 (*rep)->name = buf + sizeof(**hdr) + sizeof(**rep);
272         } else { 
273                 (*rep)->name = NULL;
274         }
275
276         if ((*rep)->tgtlen) { 
277                 (*rep)->tgt = buf + sizeof(**hdr) + sizeof(**rep) + 
278                         size_round((*rep)->namelen);
279         } else { 
280                 (*rep)->tgt = NULL;
281         }
282
283         EXIT;
284         return 0;
285 }
286 #endif