Whamcloud - gitweb
Remove yet another extraneous BUG()
[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 = HTON__u32(buflen1);
81         if (buf1) { 
82                 LOGL(buf1, buflen1, ptr); 
83         } 
84
85         req->buflen2 = HTON__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 = HTON__u32(buflen1);
159         if (buf1) { 
160                 LOGL(buf1, buflen1, ptr); 
161         } else {
162                 ptr += size_round(buflen1);
163         }
164
165         rep->buflen2 = HTON__u32(buflen2);
166         if (buf2) { 
167                 LOGL(buf2, buflen2, ptr);
168         }
169         return 0;
170 }
171
172
173 int ost_unpack_rep(char *buf, int len, 
174                    struct ptlrep_hdr **hdr, union ptl_rep *r)
175 {
176         struct ost_rep *rep;
177
178         if (len < sizeof(**hdr) + sizeof(*rep)) { 
179                 EXIT;
180                 return -EINVAL;
181         }
182
183         *hdr = (struct ptlrep_hdr *) (buf);
184         rep = (struct ost_rep *) (buf + sizeof(**hdr));
185         r->ost = rep;
186
187         rep->buflen1 = NTOH__u32(rep->buflen1); 
188         rep->buflen2 = NTOH__u32(rep->buflen2); 
189
190         if (len < sizeof(**hdr) + sizeof(*rep) + 
191             size_round(rep->buflen1) + size_round(rep->buflen2) ) { 
192                 EXIT;
193                 return -EINVAL;
194         }
195
196         EXIT;
197         return 0;
198 }
199
200 void *ost_rep_buf1(struct ost_rep *rep)
201 {
202         if (!rep->buflen1) 
203                 return NULL;
204         return (void *)((char *)rep + sizeof(*rep));
205 }
206
207 void *ost_rep_buf2(struct ost_rep *rep)
208 {
209         if (!rep->buflen2) 
210                 return NULL;
211         return (void *)((char *)rep + sizeof(*rep) + 
212                         size_round(rep->buflen1)); 
213 }
214
215 void ost_pack_ioo(void **tmp, struct obdo *oa, int bufcnt)
216 {
217         struct obd_ioobj *ioo = *tmp;
218         char *c = *tmp;
219
220         ioo->ioo_id = HTON__u64(oa->o_id); 
221         ioo->ioo_gr = HTON__u64(oa->o_gr); 
222         ioo->ioo_type = HTON__u64(oa->o_mode); 
223         ioo->ioo_bufcnt = HTON__u32(bufcnt); 
224         *tmp = c + sizeof(*ioo); 
225 }
226
227 void ost_unpack_ioo(void **tmp, struct obd_ioobj **ioop)
228 {
229         char *c = *tmp;
230         struct obd_ioobj *ioo = *tmp;
231         *ioop = *tmp;
232
233         ioo->ioo_id = NTOH__u64(ioo->ioo_id); 
234         ioo->ioo_gr = NTOH__u64(ioo->ioo_gr); 
235         ioo->ioo_type = NTOH__u64(ioo->ioo_type); 
236         ioo->ioo_bufcnt = NTOH__u32(ioo->ioo_bufcnt); 
237         *tmp = c + sizeof(*ioo); 
238 }
239
240 void ost_pack_niobuf(void **tmp, void *addr, __u64 offset, __u32 len, 
241                      __u32 flags, __u32 xid)
242 {
243         struct niobuf *ioo = *tmp;
244         char *c = *tmp;
245
246         ioo->addr = HTON__u64((__u64)(unsigned long)addr); 
247         ioo->offset = HTON__u64(offset); 
248         ioo->len = HTON__u32(len); 
249         ioo->flags = HTON__u32(flags); 
250         ioo->xid = HTON__u32(xid);
251         *tmp = c + sizeof(*ioo); 
252 }
253
254 void ost_unpack_niobuf(void **tmp, struct niobuf **nbp)
255 {
256         char *c = *tmp;
257         struct niobuf *nb = *tmp;
258
259         *nbp = *tmp;
260
261         nb->addr = NTOH__u64(nb->addr); 
262         nb->offset = NTOH__u64(nb->offset); 
263         nb->len = NTOH__u32(nb->len); 
264         nb->flags = NTOH__u32(nb->flags); 
265
266         *tmp = c + sizeof(*nb); 
267 }