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