Whamcloud - gitweb
Changes to request processing:
[fs/lustre-release.git] / lustre / lib / obd_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 OST requests
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/obd_class.h>
50 #include <linux/obd_ost.h>
51 #include <linux/lustre_lib.h>
52 #include <linux/lustre_idl.h>
53
54
55 int ost_pack_req(char *buf1, int buflen1, char *buf2, int buflen2, 
56                  struct ptlreq_hdr **hdr, struct ost_req **req, 
57                  int *len, char **buf)
58 {
59         char *ptr;
60
61         *len = sizeof(**hdr) + size_round(buflen1) + size_round(buflen2) + 
62                 sizeof(**req); 
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 ptlreq_hdr *)(*buf);
72         *req = (struct ost_req *)(*buf + sizeof(**hdr));
73
74         ptr = *buf + sizeof(**hdr) + sizeof(**req);
75
76         (*hdr)->type =  OST_TYPE_REQ;
77
78         (*req)->buflen1 = NTOH__u32(buflen1);
79         if (buf1) { 
80                 LOGL(buf1, buflen1, ptr); 
81         } 
82
83         (*req)->buflen2 = NTOH__u32(buflen2);
84         if (buf2) { 
85                 LOGL(buf2, buflen2, ptr);
86         }
87         return 0;
88 }
89
90 int ost_unpack_req(char *buf, int len, 
91                    struct ptlreq_hdr **hdr, struct ost_req **req)
92 {
93
94         if (len < sizeof(**hdr) + sizeof(**req)) { 
95                 EXIT;
96                 return -EINVAL;
97         }
98
99         *hdr = (struct ptlreq_hdr *) (buf);
100         *req = (struct ost_req *) (buf + sizeof(**hdr));
101
102         (*req)->buflen1 = NTOH__u32((*req)->buflen1); 
103         (*req)->buflen2 = NTOH__u32((*req)->buflen2); 
104
105         if (len < sizeof(**hdr) + sizeof(**req) + 
106             size_round((*req)->buflen1) + size_round((*req)->buflen2) ) { 
107                 EXIT;
108                 return -EINVAL;
109         }
110
111         EXIT;
112         return 0;
113 }
114
115
116 void *ost_req_buf1(struct ost_req *req)
117 {
118         if (!req->buflen1) 
119                 return NULL;
120         return (void *)((char *)req + sizeof(*req));
121 }
122
123 void *ost_req_buf2(struct ost_req *req)
124 {
125         if (!req->buflen2) 
126                 return NULL;
127         return (void *)((char *)req + sizeof(*req) + 
128                         size_round(req->buflen1)); 
129 }
130
131 int ost_pack_rep(void *buf1, __u32 buflen1, void *buf2, __u32 buflen2,
132                  struct ptlrep_hdr **hdr, struct ost_rep **rep, 
133                  int *len, char **buf)
134 {
135         char *ptr;
136
137         *len = sizeof(**hdr) + size_round(buflen1) + size_round(buflen2) + 
138                 sizeof(**rep); 
139
140         *buf = kmalloc(*len, GFP_KERNEL);
141         if (!*buf) {
142                 EXIT;
143                 return -ENOMEM;
144         }
145
146         memset(*buf, 0, *len); 
147         *hdr = (struct ptlrep_hdr *)(*buf);
148         *rep = (struct ost_rep *)(*buf + sizeof(**hdr));
149         ptr = *buf + sizeof(**hdr) + sizeof(**rep);
150
151         (*rep)->buflen1 = NTOH__u32(buflen1);
152         if (buf1) { 
153                 LOGL(buf1, buflen1, ptr); 
154         } 
155
156         (*rep)->buflen2 = NTOH__u32(buflen2);
157         if (buf2) { 
158                 LOGL(buf2, buflen2, ptr);
159         }
160         return 0;
161 }
162
163
164 int ost_unpack_rep(char *buf, int len, 
165                    struct ptlrep_hdr **hdr, struct ost_rep **rep)
166 {
167         if (len < sizeof(**hdr) + sizeof(**rep)) { 
168                 EXIT;
169                 return -EINVAL;
170         }
171
172         *hdr = (struct ptlrep_hdr *) (buf);
173         *rep = (struct ost_rep *) (buf + sizeof(**hdr));
174
175         (*rep)->buflen1 = NTOH__u32((*rep)->buflen1); 
176         (*rep)->buflen2 = NTOH__u32((*rep)->buflen2); 
177
178         if (len < sizeof(**hdr) + sizeof(**rep) + 
179             size_round((*rep)->buflen1) + size_round((*rep)->buflen2) ) { 
180                 EXIT;
181                 return -EINVAL;
182         }
183
184         EXIT;
185         return 0;
186 }
187
188 void *ost_rep_buf1(struct ost_rep *rep)
189 {
190         if (!rep->buflen1) 
191                 return NULL;
192         return (void *)((char *)rep + sizeof(*rep));
193 }
194
195 void *ost_rep_buf2(struct ost_rep *rep)
196 {
197         if (!rep->buflen2) 
198                 return NULL;
199         return (void *)((char *)rep + sizeof(*rep) + 
200                         size_round(rep->buflen1)); 
201 }
202
203 void ost_pack_ioo(void **tmp, struct obdo *oa, int bufcnt)
204 {
205         struct obd_ioobj *ioo = *tmp;
206         char *c = *tmp;
207         
208         ioo->ioo_id = NTOH__u64(oa->o_id); 
209         ioo->ioo_gr = NTOH__u64(oa->o_gr); 
210         ioo->ioo_type = NTOH__u64(oa->o_mode); 
211         ioo->ioo_bufcnt = NTOH__u32(bufcnt); 
212         *tmp = c + sizeof(*ioo); 
213 }
214
215 void ost_unpack_ioo(void **tmp, struct obd_ioobj **ioop)
216 {
217         char *c = *tmp;
218         struct obd_ioobj *ioo = *tmp;
219         *ioop = *tmp;
220         
221         ioo->ioo_id = NTOH__u64(ioo->ioo_id); 
222         ioo->ioo_gr = NTOH__u64(ioo->ioo_gr); 
223         ioo->ioo_type = NTOH__u64(ioo->ioo_type); 
224         ioo->ioo_bufcnt = NTOH__u32(ioo->ioo_bufcnt); 
225         *tmp = c + sizeof(*ioo); 
226 }
227
228 void ost_pack_niobuf(void **tmp, void *addr, __u64 offset, __u32 len, 
229                    __u32 flags)
230 {
231         struct niobuf *ioo = *tmp;
232         char *c = *tmp;
233
234         ioo->addr = NTOH__u64((__u64)(unsigned long)addr); 
235         ioo->offset = NTOH__u64(offset); 
236         ioo->len = NTOH__u32(len); 
237         ioo->flags = NTOH__u32(flags); 
238         *tmp = c + sizeof(*ioo); 
239 }
240
241 void ost_unpack_niobuf(void **tmp, struct niobuf **nbp)
242 {
243         char *c = *tmp;
244         struct niobuf *nb = *tmp;
245
246         *nbp = *tmp;
247
248         nb->addr = NTOH__u64((__u64)(unsigned long)nb->addr); 
249         nb->offset = NTOH__u64(nb->offset); 
250         nb->len = NTOH__u32(nb->len); 
251         nb->flags = NTOH__u32(nb->flags); 
252
253         *tmp = c + sizeof(*nb); 
254 }