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