Whamcloud - gitweb
Small fixes to the request processing.
[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                 preq->name_offset = (__u32)(ptr - (char *)preq);
82                 LOGL(name, namelen, ptr); 
83         } 
84
85         (*req)->tgtlen = NTOH__u32(tgtlen);
86         if (tgt) { 
87                 preq->tgt_offset = (__u32)(ptr - (char *)preq);
88                 LOGL(tgt, tgtlen, ptr);
89         }
90         return 0;
91 }
92
93
94 int mds_unpack_req(char *buf, int len, 
95                    struct mds_req_hdr **hdr, struct mds_req **req)
96 {
97         struct mds_req_packed *preq;
98         __u32 off1, off2;
99         char *name, *tgt;
100
101         if (len < sizeof(**hdr) + sizeof(**req)) { 
102                 EXIT;
103                 return -EINVAL;
104         }
105
106         *hdr = (struct mds_req_hdr *) (buf);
107         preq = (struct mds_req_packed *) (buf + sizeof(**hdr));
108         off1 = preq->name_offset;
109         off2 = preq->tgt_offset;
110
111         *req = (struct mds_req *) (buf + sizeof(**hdr));
112         (*req)->namelen = NTOH__u32((*req)->namelen); 
113         (*req)->tgtlen = NTOH__u32((*req)->namelen); 
114
115         if (len < sizeof(**hdr) + sizeof(**req) + (*req)->namelen + 
116             (*req)->tgtlen ) { 
117                 EXIT;
118                 return -EINVAL;
119         }
120
121         if ((*req)->namelen) { 
122                 name = buf + sizeof(**hdr) + off1;
123         } else { 
124                 name = NULL;
125         }
126
127         if ((*req)->tgtlen) { 
128                 tgt = buf + sizeof(**hdr) + off2;
129         } else { 
130                 tgt = NULL;
131         }
132         (*req)->name = name;
133         (*req)->tgt = tgt;
134
135         EXIT;
136         return 0;
137 }
138
139 int mds_pack_rep(char *name, int namelen, char *tgt, int tgtlen, 
140                  struct mds_rep_hdr **hdr, struct mds_rep **rep, 
141                  int *len, char **buf)
142 {
143         char *ptr;
144         struct mds_rep_packed *prep;
145
146         *len = sizeof(**hdr) + size_round(namelen) + size_round(tgtlen) + 
147                 sizeof(*prep); 
148
149         *buf = kmalloc(*len, GFP_KERNEL);
150         if (!*buf) {
151                 EXIT;
152                 return -ENOMEM;
153         }
154
155         memset(*buf, 0, *len); 
156         *hdr = (struct mds_rep_hdr *)(*buf);
157         *rep = (struct mds_rep *)(*buf + sizeof(**hdr));
158
159         prep = (struct mds_rep_packed *)(*buf + sizeof(**hdr));
160         ptr = *buf + sizeof(**hdr) + sizeof(*prep);
161
162         (*hdr)->type =  MDS_TYPE_REP;
163
164         (*rep)->namelen = NTOH__u32(namelen);
165         if (name) { 
166                 prep->name_offset = (__u32)(ptr - (char *)prep);
167                 LOGL(name, namelen, ptr); 
168         } 
169
170         (*rep)->tgtlen = NTOH__u32(tgtlen);
171         if (tgt) { 
172                 prep->tgt_offset = (__u32)(ptr - (char *)prep);
173                 LOGL(tgt, tgtlen, ptr);
174         }
175         return 0;
176 }
177
178
179 int mds_unpack_rep(char *buf, int len, 
180                    struct mds_rep_hdr **hdr, struct mds_rep **rep)
181 {
182         struct mds_rep_packed *prep;
183         __u32 off1, off2;
184
185         if (len < sizeof(**hdr) + sizeof(**rep)) { 
186                 EXIT;
187                 return -EINVAL;
188         }
189
190         *hdr = (struct mds_rep_hdr *) (buf);
191         prep = (struct mds_rep_packed *) (buf + sizeof(**hdr));
192         off1 = prep->name_offset;
193         off2 = prep->tgt_offset;
194
195         *rep = (struct mds_rep *) (buf + sizeof(**hdr));
196         (*rep)->namelen = NTOH__u32((*rep)->namelen); 
197         (*rep)->tgtlen = NTOH__u32((*rep)->namelen); 
198
199         if (len < sizeof(**hdr) + sizeof(**rep) + (*rep)->namelen + 
200             (*rep)->tgtlen ) { 
201                 EXIT;
202                 return -EINVAL;
203         }
204
205         if ((*rep)->namelen) { 
206                 (*rep)->name = buf + sizeof(**hdr) + off1;
207         } else { 
208                 (*rep)->name = NULL;
209         }
210
211         if ((*rep)->tgtlen) { 
212                 (*rep)->tgt = buf + sizeof(**hdr) + off2;
213         } else { 
214                 (*rep)->tgt = NULL;
215         }
216         
217
218         EXIT;
219         return 0;
220 }
221
222 #if 0
223 int mds_pack_rep(char *name, int namelen, char *tgt, int tgtlen, 
224                  struct mds_rep_hdr **hdr, struct mds_rep **rep, 
225                  int *len, char **buf)
226 {
227         char *ptr;
228
229         *len = sizeof(**hdr) + size_round(namelen) + size_round(tgtlen) + 
230                 sizeof(**rep); 
231
232         *buf = kmalloc(*len, GFP_KERNEL);
233         if (!*buf) {
234                 EXIT;
235                 return -ENOMEM;
236         }
237
238         memset(*buf, 0, *len); 
239         *hdr = (struct mds_rep_hdr *)(*buf);
240         *rep = (struct mds_rep *)(*buf + sizeof(**hdr));
241         ptr = *buf + sizeof(**hdr) + sizeof(**rep);
242
243         (*rep)->namelen = NTOH__u32(namelen);
244         if (name) { 
245                 LOGL(name, namelen, ptr); 
246         } 
247
248         (*rep)->tgtlen = NTOH__u32(tgtlen);
249         if (tgt) { 
250                 LOGL(tgt, tgtlen, ptr);
251         }
252         return 0;
253 }
254
255
256 int mds_unpack_rep(char *buf, int len, 
257                    struct mds_rep_hdr **hdr, struct mds_rep **rep)
258 {
259         if (len < sizeof(**hdr) + sizeof(**rep)) { 
260                 EXIT;
261                 return -EINVAL;
262         }
263
264         *hdr = (struct mds_rep_hdr *) (buf);
265         *rep = (struct mds_rep *) (buf + sizeof(**hdr));
266         (*rep)->namelen = NTOH__u32((*rep)->namelen); 
267         (*rep)->tgtlen = NTOH__u32((*rep)->namelen); 
268
269         if (len < sizeof(**hdr) + sizeof(**rep) + (*rep)->namelen + 
270             (*rep)->tgtlen ) { 
271                 EXIT;
272                 return -EINVAL;
273         }
274
275         if ((*rep)->namelen) { 
276                 (*rep)->name = buf + sizeof(**hdr) + sizeof(**rep);
277         } else { 
278                 (*rep)->name = NULL;
279         }
280
281         if ((*rep)->tgtlen) { 
282                 (*rep)->tgt = buf + sizeof(**hdr) + sizeof(**rep) + 
283                         size_round((*rep)->namelen);
284         } else { 
285                 (*rep)->tgt = NULL;
286         }
287
288         EXIT;
289         return 0;
290 }
291 #endif