Whamcloud - gitweb
Pass 1 of debugging and leak cleanup:
[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/fs.h>
38 #include <linux/poll.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
41 #include <asm/io.h>
42 #include <asm/segment.h>
43 #include <asm/system.h>
44 #include <asm/poll.h>
45 #include <asm/uaccess.h>
46
47 #include <linux/obd_support.h>
48 #include <linux/lustre_lib.h>
49 #include <linux/lustre_idl.h>
50 #include <linux/lustre_mds.h>
51
52
53 int mds_pack_req(char *name, int namelen, char *tgt, int tgtlen, 
54                  struct ptlreq_hdr **hdr, struct mds_req **req, 
55                  int *len, char **buf)
56 {
57         char *ptr;
58
59         *len = sizeof(**hdr) + size_round(namelen) + size_round(tgtlen) + 
60                 sizeof(**req); 
61
62         OBD_ALLOC(*buf, *len);
63         if (!*buf) {
64                 EXIT;
65                 return -ENOMEM;
66         }
67
68         memset(*buf, 0, *len); 
69         *hdr = (struct ptlreq_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 ptlreq_hdr **hdr, struct mds_req **req)
90 {
91         char *name, *tgt;
92
93         if (len < sizeof(**hdr) + sizeof(**req)) { 
94                 EXIT;
95                 return -EINVAL;
96         }
97
98         *hdr = (struct ptlreq_hdr *) (buf);
99         *req = (struct mds_req *) (buf + sizeof(**hdr));
100
101         (*req)->namelen = NTOH__u32((*req)->namelen); 
102         (*req)->tgtlen = NTOH__u32((*req)->tgtlen); 
103
104         if (len < sizeof(**hdr) + sizeof(**req) +
105             size_round((*req)->namelen) + size_round((*req)->tgtlen) ) { 
106                 EXIT;
107                 return -EINVAL;
108         }
109
110         if ((*req)->namelen) { 
111                 name = buf + sizeof(**hdr) + sizeof(**req);
112         } else { 
113                 name = NULL;
114         }
115
116         if ((*req)->tgtlen) { 
117                 tgt = buf + sizeof(**hdr) + sizeof(**req) + 
118                         size_round((*req)->namelen);
119         } else { 
120                 tgt = NULL;
121         }
122
123         EXIT;
124         return 0;
125 }
126
127 void *mds_req_tgt(struct mds_req *req)
128 {
129         if (!req->tgtlen) 
130                 return NULL;
131         return (void *)((char *)req + sizeof(*req) + 
132                         size_round(req->namelen)); 
133 }
134
135 void *mds_req_name(struct mds_req *req)
136 {
137         if (!req->namelen) 
138                 return NULL;
139         return (void *)((char *)req + sizeof(*req));
140 }
141
142 int mds_pack_rep(char *name, int namelen, char *tgt, int tgtlen, 
143                  struct ptlrep_hdr **hdr, struct mds_rep **rep, 
144                  int *len, char **buf)
145 {
146         char *ptr;
147
148         *len = sizeof(**hdr) + size_round(namelen) + size_round(tgtlen) + 
149                 sizeof(**rep); 
150
151         OBD_ALLOC(*buf, *len);
152         if (!*buf) {
153                 EXIT;
154                 return -ENOMEM;
155         }
156
157         memset(*buf, 0, *len); 
158         *hdr = (struct ptlrep_hdr *)(*buf);
159         *rep = (struct mds_rep *)(*buf + sizeof(**hdr));
160
161         ptr = *buf + sizeof(**hdr) + sizeof(**rep);
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 int mds_unpack_rep(char *buf, int len, 
178                    struct ptlrep_hdr **hdr, struct mds_rep **rep)
179 {
180
181         if (len < sizeof(**hdr)) { 
182                 EXIT;
183                 return -EINVAL;
184         }
185         *hdr = (struct ptlrep_hdr *) (buf);
186
187         if (len < sizeof(**hdr) + sizeof(**rep)) { 
188                 EXIT;
189                 return -EINVAL;
190         }
191
192         *rep = (struct mds_rep *) (buf + sizeof(**hdr));
193         (*rep)->namelen = NTOH__u32((*rep)->namelen); 
194         (*rep)->tgtlen = NTOH__u32((*rep)->namelen); 
195
196         if (len < sizeof(**hdr) + sizeof(**rep) 
197             + size_round((*rep)->namelen) + size_round((*rep)->tgtlen) ) { 
198                 EXIT;
199                 return -EINVAL;
200         }
201
202         EXIT;
203         return 0;
204 }
205
206 void *mds_rep_name(struct mds_rep *rep)
207 {
208         if (!rep->namelen) 
209                 return NULL;
210         return (void *)((char *)rep + sizeof(*rep));
211 }
212
213 void *mds_rep_tgt(struct mds_rep *rep)
214 {
215         if (!rep->tgtlen) 
216                 return NULL;
217         return (void *)((char *)rep + sizeof(*rep) + size_round(rep->namelen)); 
218 }
219