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