Whamcloud - gitweb
Merged branch 'peter' with the tip. Pre-merge tag is 't_20020302_networking'.
[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 #define DEBUG_SUBSYSTEM S_MDS
48
49 #include <linux/obd_support.h>
50 #include <linux/obd_class.h>
51 #include <linux/lustre_lib.h>
52 #include <linux/lustre_idl.h>
53 #include <linux/lustre_mds.h>
54
55 int mds_pack_req(char *name, int namelen, char *tgt, int tgtlen, 
56                  struct ptlreq_hdr **hdr, union ptl_req *r,
57                  int *len, char **buf)
58 {
59         struct mds_req *req;
60         char *ptr;
61
62         *len = sizeof(**hdr) + size_round(namelen) + size_round(tgtlen) + 
63                 sizeof(*req); 
64
65         OBD_ALLOC(*buf, *len);
66         if (!*buf) {
67                 EXIT;
68                 return -ENOMEM;
69         }
70
71         memset(*buf, 0, *len); 
72         *hdr = (struct ptlreq_hdr *)(*buf);
73         req = (struct mds_req *)(*buf + sizeof(**hdr));
74         r->mds = req;
75
76         ptr = *buf + sizeof(**hdr) + sizeof(*req);
77
78         (*hdr)->type =  MDS_TYPE_REQ;
79
80         req->namelen = NTOH__u32(namelen);
81         if (name) { 
82                 LOGL(name, namelen, ptr); 
83         } 
84
85         req->tgtlen = NTOH__u32(tgtlen);
86         if (tgt) {
87                 LOGL(tgt, tgtlen, ptr);
88         }
89         return 0;
90 }
91
92
93 int mds_unpack_req(char *buf, int len, 
94                    struct ptlreq_hdr **hdr, union ptl_req *r)
95 {
96         struct mds_req *req;
97         char *name, *tgt;
98
99         if (len < sizeof(**hdr) + sizeof(*req)) { 
100                 EXIT;
101                 return -EINVAL;
102         }
103
104         *hdr = (struct ptlreq_hdr *) (buf);
105         req = (struct mds_req *) (buf + sizeof(**hdr));
106         r->mds = req;
107
108         req->namelen = NTOH__u32(req->namelen); 
109         req->tgtlen = NTOH__u32(req->tgtlen); 
110
111         if (len < sizeof(**hdr) + sizeof(*req) +
112             size_round(req->namelen) + size_round(req->tgtlen) ) { 
113                 EXIT;
114                 return -EINVAL;
115         }
116
117         if (req->namelen) { 
118                 name = buf + sizeof(**hdr) + sizeof(*req);
119         } else { 
120                 name = NULL;
121         }
122
123         if (req->tgtlen) { 
124                 tgt = buf + sizeof(**hdr) + sizeof(*req) + 
125                         size_round(req->namelen);
126         } else { 
127                 tgt = NULL;
128         }
129
130         EXIT;
131         return 0;
132 }
133
134 void *mds_req_tgt(struct mds_req *req)
135 {
136         if (!req->tgtlen) 
137                 return NULL;
138         return (void *)((char *)req + sizeof(*req) + 
139                         size_round(req->namelen)); 
140 }
141
142 void *mds_req_name(struct mds_req *req)
143 {
144         if (!req->namelen) 
145                 return NULL;
146         return (void *)((char *)req + sizeof(*req));
147 }
148
149 int mds_pack_rep(char *name, int namelen, char *tgt, int tgtlen, 
150                  struct ptlrep_hdr **hdr, union ptl_rep *r,
151                  int *len, char **buf)
152 {
153         struct mds_rep *rep;
154         char *ptr;
155
156         *len = sizeof(**hdr) + size_round(namelen) + size_round(tgtlen) + 
157                 sizeof(*rep); 
158
159         OBD_ALLOC(*buf, *len);
160         if (!*buf) {
161                 EXIT;
162                 return -ENOMEM;
163         }
164
165         memset(*buf, 0, *len); 
166         *hdr = (struct ptlrep_hdr *)(*buf);
167         rep = (struct mds_rep *)(*buf + sizeof(**hdr));
168         r->mds = rep;
169
170         ptr = *buf + sizeof(**hdr) + sizeof(*rep);
171
172         (*hdr)->type =  MDS_TYPE_REP;
173
174         rep->namelen = NTOH__u32(namelen);
175         if (name) { 
176                 LOGL(name, namelen, ptr); 
177         } 
178
179         rep->tgtlen = NTOH__u32(tgtlen);
180         if (tgt) { 
181                 LOGL(tgt, tgtlen, ptr);
182         }
183         return 0;
184 }
185
186 int mds_unpack_rep(char *buf, int len, 
187                    struct ptlrep_hdr **hdr, union ptl_rep *r)
188 {
189         struct mds_rep *rep;
190         if (len < sizeof(**hdr)) { 
191                 EXIT;
192                 return -EINVAL;
193         }
194         *hdr = (struct ptlrep_hdr *) (buf);
195
196         if (len < sizeof(**hdr) + sizeof(*rep)) { 
197                 EXIT;
198                 return -EINVAL;
199         }
200
201         rep = (struct mds_rep *) (buf + sizeof(**hdr));
202         r->mds = rep;
203         rep->namelen = NTOH__u32(rep->namelen); 
204         rep->tgtlen = NTOH__u32(rep->namelen); 
205
206         if (len < sizeof(**hdr) + sizeof(*rep) 
207             + size_round(rep->namelen) + size_round(rep->tgtlen) ) { 
208                 EXIT;
209                 return -EINVAL;
210         }
211
212         EXIT;
213         return 0;
214 }
215
216 void *mds_rep_name(struct mds_rep *rep)
217 {
218         if (!rep->namelen) 
219                 return NULL;
220         return (void *)((char *)rep + sizeof(*rep));
221 }
222
223 void *mds_rep_tgt(struct mds_rep *rep)
224 {
225         if (!rep->tgtlen) 
226                 return NULL;
227         return (void *)((char *)rep + sizeof(*rep) + size_round(rep->namelen)); 
228 }
229