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