Whamcloud - gitweb
- Added DEBUG_SUBSYSTEMs
[fs/lustre-release.git] / lustre / lib / mds_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 MDS and OST request records
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_MDS
48
49 #include <linux/obd_support.h>
50 #include <linux/lustre_lib.h>
51 #include <linux/lustre_idl.h>
52 #include <linux/lustre_mds.h>
53
54 int mds_pack_req(char *name, int namelen, char *tgt, int tgtlen, 
55                  struct ptlreq_hdr **hdr, struct mds_req **req, 
56                  int *len, char **buf)
57 {
58         char *ptr;
59
60         *len = sizeof(**hdr) + size_round(namelen) + size_round(tgtlen) + 
61                 sizeof(**req); 
62
63         OBD_ALLOC(*buf, *len);
64         if (!*buf) {
65                 EXIT;
66                 return -ENOMEM;
67         }
68
69         memset(*buf, 0, *len); 
70         *hdr = (struct ptlreq_hdr *)(*buf);
71         *req = (struct mds_req *)(*buf + sizeof(**hdr));
72         ptr = *buf + sizeof(**hdr) + sizeof(**req);
73
74         (*hdr)->type =  MDS_TYPE_REQ;
75
76         (*req)->namelen = NTOH__u32(namelen);
77         if (name) { 
78                 LOGL(name, namelen, ptr); 
79         } 
80
81         (*req)->tgtlen = NTOH__u32(tgtlen);
82         if (tgt) {
83                 LOGL(tgt, tgtlen, ptr);
84         }
85         return 0;
86 }
87
88
89 int mds_unpack_req(char *buf, int len, 
90                    struct ptlreq_hdr **hdr, struct mds_req **req)
91 {
92         char *name, *tgt;
93
94         if (len < sizeof(**hdr) + sizeof(**req)) { 
95                 EXIT;
96                 return -EINVAL;
97         }
98
99         *hdr = (struct ptlreq_hdr *) (buf);
100         *req = (struct mds_req *) (buf + sizeof(**hdr));
101
102         (*req)->namelen = NTOH__u32((*req)->namelen); 
103         (*req)->tgtlen = NTOH__u32((*req)->tgtlen); 
104
105         if (len < sizeof(**hdr) + sizeof(**req) +
106             size_round((*req)->namelen) + size_round((*req)->tgtlen) ) { 
107                 EXIT;
108                 return -EINVAL;
109         }
110
111         if ((*req)->namelen) { 
112                 name = buf + sizeof(**hdr) + sizeof(**req);
113         } else { 
114                 name = NULL;
115         }
116
117         if ((*req)->tgtlen) { 
118                 tgt = buf + sizeof(**hdr) + sizeof(**req) + 
119                         size_round((*req)->namelen);
120         } else { 
121                 tgt = NULL;
122         }
123
124         EXIT;
125         return 0;
126 }
127
128 void *mds_req_tgt(struct mds_req *req)
129 {
130         if (!req->tgtlen) 
131                 return NULL;
132         return (void *)((char *)req + sizeof(*req) + 
133                         size_round(req->namelen)); 
134 }
135
136 void *mds_req_name(struct mds_req *req)
137 {
138         if (!req->namelen) 
139                 return NULL;
140         return (void *)((char *)req + sizeof(*req));
141 }
142
143 int mds_pack_rep(char *name, int namelen, char *tgt, int tgtlen, 
144                  struct ptlrep_hdr **hdr, struct mds_rep **rep, 
145                  int *len, char **buf)
146 {
147         char *ptr;
148
149         *len = sizeof(**hdr) + size_round(namelen) + size_round(tgtlen) + 
150                 sizeof(**rep); 
151
152         OBD_ALLOC(*buf, *len);
153         if (!*buf) {
154                 EXIT;
155                 return -ENOMEM;
156         }
157
158         memset(*buf, 0, *len); 
159         *hdr = (struct ptlrep_hdr *)(*buf);
160         *rep = (struct mds_rep *)(*buf + sizeof(**hdr));
161
162         ptr = *buf + sizeof(**hdr) + sizeof(**rep);
163
164         (*hdr)->type =  MDS_TYPE_REP;
165
166         (*rep)->namelen = NTOH__u32(namelen);
167         if (name) { 
168                 LOGL(name, namelen, ptr); 
169         } 
170
171         (*rep)->tgtlen = NTOH__u32(tgtlen);
172         if (tgt) { 
173                 LOGL(tgt, tgtlen, ptr);
174         }
175         return 0;
176 }
177
178 int mds_unpack_rep(char *buf, int len, 
179                    struct ptlrep_hdr **hdr, struct mds_rep **rep)
180 {
181
182         if (len < sizeof(**hdr)) { 
183                 EXIT;
184                 return -EINVAL;
185         }
186         *hdr = (struct ptlrep_hdr *) (buf);
187
188         if (len < sizeof(**hdr) + sizeof(**rep)) { 
189                 EXIT;
190                 return -EINVAL;
191         }
192
193         *rep = (struct mds_rep *) (buf + sizeof(**hdr));
194         (*rep)->namelen = NTOH__u32((*rep)->namelen); 
195         (*rep)->tgtlen = NTOH__u32((*rep)->namelen); 
196
197         if (len < sizeof(**hdr) + sizeof(**rep) 
198             + size_round((*rep)->namelen) + size_round((*rep)->tgtlen) ) { 
199                 EXIT;
200                 return -EINVAL;
201         }
202
203         EXIT;
204         return 0;
205 }
206
207 void *mds_rep_name(struct mds_rep *rep)
208 {
209         if (!rep->namelen) 
210                 return NULL;
211         return (void *)((char *)rep + sizeof(*rep));
212 }
213
214 void *mds_rep_tgt(struct mds_rep *rep)
215 {
216         if (!rep->tgtlen) 
217                 return NULL;
218         return (void *)((char *)rep + sizeof(*rep) + size_round(rep->namelen)); 
219 }
220