Whamcloud - gitweb
da1c3ddef6c0df52dc35afbff947b34880480f06
[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 InterMezzo, http://www.inter-mezzo.org.
7  *
8  *   InterMezzo 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  *   InterMezzo 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 InterMezzo; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * Unpacking of KML 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
52
53 int mds_pack_req(char *name, int namelen, char *tgt, int tgtlen, 
54                  struct mds_req_hdr **hdr, struct mds_req **req, 
55                  int *len, char **buf)
56 {
57         char *ptr;
58
59         *len = sizeof(**hdr) + size_round(namelen) + sizeround(tgtlen) + 
60                 sizeof(**req); 
61
62         *buf = kmalloc(*len, GFP_KERNEL);
63         if (!*buf) {
64                 EXIT;
65                 return -ENOMEM;
66         }
67
68         memset(*buf, 0, *len); 
69         *hdr = (struct mds_req_hdr *)(*buf);
70         *req = (struct mds_req *)(*buf + sizeof(**hdr));
71         ptr = *buf + sizeof(**hdr) + sizeof(**req);
72
73         (*hdr)->type =  MDS_TYPE_REQ;
74
75         (*req)->namelen = NTOH_u32(namelen);
76         if (name) { 
77                 LOGL(name, namelen, ptr); 
78         } 
79
80         (*req)->tgtlen = NTOH_u32(tgtlen);
81         if (tgt) { 
82                 LOGL(tgt, tgtlen, ptr);
83         }
84         return 0;
85 }
86
87
88 int mds_unpack_req(char *buf, int len, 
89                    struct mds_req_hdr **hdr, struct mds_req **req)
90 {
91         if (len < sizeof(**hdr) + sizeof(**req)) { 
92                 EXIT;
93                 return -EINVAL;
94         }
95
96         *hdr = (struct mds_req_hdr *) (buf);
97         *req = (struct mds_req *) (buf + sizeof(**hdr));
98         (*req)->namelen = NTOH_u32((*req)->namelen); 
99         (*req)->tgtlen = NTOH_u32((*req)->namelen); 
100
101         if (len < sizeof(**hdr) + sizeof(**req) + (*req)->namelen + 
102             (*req)->tgtlen ) { 
103                 EXIT;
104                 return -EINVAL;
105         }
106
107         if ((*req)->namelen) { 
108                 (*req)->name = buf + sizeof(**hdr) + sizeof(**req);
109         } else { 
110                 (*req)->name = NULL;
111         }
112
113         if ((*req)->tgtlen) { 
114                 (*req)->tgt = buf + sizeof(**hdr) + sizeof(**req) + 
115                         sizerount((*req)->namelen);
116         } else { 
117                 (*req)->tgt = NULL;
118         }
119
120         EXIT;
121         return 0;
122 }
123
124 int mds_pack_rep(char *name, int namelen, char *tgt, int tgtlen, 
125                  struct mds_rep_hdr **hdr, struct mds_rep **rep, 
126                  int *len, char **buf)
127 {
128         char *ptr;
129
130         *len = sizeof(**hdr) + size_round(namelen) + sizeround(tgtlen) + 
131                 sizeof(**rep); 
132
133         *buf = kmalloc(*len, GFP_KERNEL);
134         if (!*buf) {
135                 EXIT;
136                 return -ENOMEM;
137         }
138
139         memset(*buf, 0, *len); 
140         *hdr = (struct mds_rep_hdr *)(*buf);
141         *rep = (struct mds_rep *)(*buf + sizeof(**hdr));
142         ptr = *buf + sizeof(**hdr) + sizeof(**rep);
143
144         (*rep)->namelen = NTOH_u32(namelen);
145         if (name) { 
146                 LOGL(name, namelen, ptr); 
147         } 
148
149         (*rep)->tgtlen = NTOH_u32(tgtlen);
150         if (tgt) { 
151                 LOGL(tgt, tgtlen, ptr);
152         }
153         return 0;
154 }
155
156
157 int mds_unpack_rep(char *buf, int len, 
158                    struct mds_rep_hdr **hdr, struct mds_rep **rep)
159 {
160         if (len < sizeof(**hdr) + sizeof(**rep)) { 
161                 EXIT;
162                 return -EINVAL;
163         }
164
165         *hdr = (struct mds_rep_hdr *) (buf);
166         *rep = (struct mds_rep *) (buf + sizeof(**hdr));
167         (*rep)->namelen = NTOH_u32((*rep)->namelen); 
168         (*rep)->tgtlen = NTOH_u32((*rep)->namelen); 
169
170         if (len < sizeof(**hdr) + sizeof(**rep) + (*rep)->namelen + 
171             (*rep)->tgtlen ) { 
172                 EXIT;
173                 return -EINVAL;
174         }
175
176         if ((*rep)->namelen) { 
177                 (*rep)->name = buf + sizeof(**hdr) + sizeof(**rep);
178         } else { 
179                 (*rep)->name = NULL;
180         }
181
182         if ((*rep)->tgtlen) { 
183                 (*rep)->tgt = buf + sizeof(**hdr) + sizeof(**rep) + 
184                         sizerount((*rep)->namelen);
185         } else { 
186                 (*rep)->tgt = NULL;
187         }
188
189         EXIT;
190         return 0;
191 }