Whamcloud - gitweb
Small fixes to the 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 ost_req_hdr **hdr, struct ost_req **req, 
57                  int *len, char **buf)
58 {
59         char *ptr;
60         struct ost_req_packed *preq;
61
62         *len = sizeof(**hdr) + size_round(buflen1) + size_round(buflen2) + 
63                 sizeof(*preq); 
64
65         *buf = kmalloc(*len, GFP_KERNEL);
66         if (!*buf) {
67                 EXIT;
68                 return -ENOMEM;
69         }
70
71         memset(*buf, 0, *len); 
72         *hdr = (struct ost_req_hdr *)(*buf);
73
74         preq = (struct ost_req_packed *)(*buf + sizeof(**hdr));
75         ptr = *buf + sizeof(**hdr) + sizeof(*preq);
76         *req = (struct ost_req *)(*buf + sizeof(**hdr));
77
78         (*hdr)->type =  OST_TYPE_REQ;
79
80         (*req)->buflen1 = NTOH__u32(buflen1);
81         if (buf1) { 
82                 preq->bufoffset1 = (__u32)(ptr - (char *)preq);
83                 LOGL(buf1, buflen1, ptr); 
84         } 
85
86         (*req)->buflen2 = NTOH__u32(buflen2);
87         if (buf2) { 
88                 preq->bufoffset2 = (__u32)(ptr - (char *)preq);
89                 LOGL(buf2, buflen2, ptr);
90         }
91         return 0;
92 }
93
94 int ost_unpack_req(char *buf, int len, 
95                    struct ost_req_hdr **hdr, struct ost_req **req)
96 {
97         struct ost_req_packed *reqp;
98         __u32 off1, off2;
99
100         if (len < sizeof(**hdr) + sizeof(*reqp)) { 
101                 EXIT;
102                 return -EINVAL;
103         }
104
105         *hdr = (struct ost_req_hdr *) (buf);
106         reqp = (struct ost_req_packed *) (buf + sizeof(**hdr));
107         *req = (struct ost_req *) (buf + sizeof(**hdr));
108
109         (*req)->buflen1 = NTOH__u32(reqp->buflen1); 
110         (*req)->buflen2 = NTOH__u32(reqp->buflen2); 
111         off1 = NTOH__u32(reqp->bufoffset1); 
112         off2 = NTOH__u32(reqp->bufoffset2); 
113
114         if (len < sizeof(**hdr) + sizeof(*reqp) + size_round(reqp->buflen1) + 
115             size_round(reqp->buflen2) ) { 
116                 EXIT;
117                 return -EINVAL;
118         }
119
120         if ((*req)->buflen1) { 
121                 (*req)->buf1 = (buf + sizeof(**hdr) + off1);
122         } else { 
123                 (*req)->buf1 = 0;
124         }
125         if ((*req)->buflen2) { 
126                 (*req)->buf2 = (buf + sizeof(**hdr) + off2);
127         } else { 
128                 (*req)->buf2 = 0;
129         }
130
131         EXIT;
132         return 0;
133 }
134
135 int ost_pack_rep(void *buf1, __u32 buflen1, void *buf2, __u32 buflen2,
136                  struct ost_rep_hdr **hdr, struct ost_rep **rep, 
137                  int *len, char **buf)
138 {
139         char *ptr;
140
141         *len = sizeof(**hdr) + size_round(buflen1) + size_round(buflen2) + 
142                 sizeof(**rep); 
143
144         *buf = kmalloc(*len, GFP_KERNEL);
145         if (!*buf) {
146                 EXIT;
147                 return -ENOMEM;
148         }
149
150         memset(*buf, 0, *len); 
151         *hdr = (struct ost_rep_hdr *)(*buf);
152         *rep = (struct ost_rep *)(*buf + sizeof(**hdr));
153         ptr = *buf + sizeof(**hdr) + sizeof(**rep);
154
155         (*rep)->buflen1 = NTOH__u32(buflen1);
156         if (buf1) { 
157                 LOGL(buf1, buflen1, ptr); 
158         } 
159
160         (*rep)->buflen2 = NTOH__u32(buflen2);
161         if (buf2) { 
162                 LOGL(buf2, buflen2, ptr);
163         }
164         return 0;
165 }
166
167
168 int ost_unpack_rep(char *buf, int len, 
169                    struct ost_rep_hdr **hdr, struct ost_rep **rep)
170 {
171         struct ost_rep_packed *prep;
172         __u32 off1, off2;
173
174         if (len < sizeof(**hdr) + sizeof(**rep)) { 
175                 EXIT;
176                 return -EINVAL;
177         }
178
179         *hdr = (struct ost_rep_hdr *) (buf);
180         *rep = (struct ost_rep *) (buf + sizeof(**hdr));
181         prep = (struct ost_rep_packed *) (buf + sizeof(**hdr));
182         (*rep)->buflen1 = NTOH__u32(prep->buflen1); 
183         (*rep)->buflen2 = NTOH__u32(prep->buflen2); 
184         off1 = prep->bufoffset1;
185         off2 = prep->bufoffset2;
186
187         if (len < sizeof(**hdr) + sizeof(*prep) + size_round((*rep)->buflen1) + 
188             size_round((*rep)->buflen2) ) { 
189                 EXIT;
190                 return -EINVAL;
191         }
192
193         if ((*rep)->buflen1) { 
194                 (*rep)->buf1 = (buf + sizeof(**hdr) + off1);
195         } else { 
196                 (*rep)->buf1 = 0;
197         }
198         if ((*rep)->buflen2) { 
199                 (*rep)->buf2 = (buf + sizeof(**hdr) + off2);
200         } else { 
201                 (*rep)->buf2 = 0;
202         }
203
204         EXIT;
205         return 0;
206 }
207