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