Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_rawobj.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/ptlrpc/gss/gss_rawobj.c
39  *
40  * Author: Eric Mei <ericm@clusterfs.com>
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_SEC
47
48 #include <linux/mutex.h>
49
50 #include <obd.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lustre_sec.h>
54
55 #include "gss_internal.h"
56
57 int rawobj_empty(rawobj_t *obj)
58 {
59         LASSERT(equi(obj->len, obj->data));
60         return (obj->len == 0);
61 }
62
63 int rawobj_alloc(rawobj_t *obj, char *buf, int len)
64 {
65         LASSERT(obj);
66         LASSERT(len >= 0);
67
68         obj->len = len;
69         if (len) {
70                 OBD_ALLOC_LARGE(obj->data, len);
71                 if (!obj->data) {
72                         obj->len = 0;
73                         RETURN(-ENOMEM);
74                 }
75                 memcpy(obj->data, buf, len);
76         } else
77                 obj->data = NULL;
78         return 0;
79 }
80
81 void rawobj_free(rawobj_t *obj)
82 {
83         LASSERT(obj);
84
85         if (obj->len) {
86                 LASSERT(obj->data);
87                 OBD_FREE_LARGE(obj->data, obj->len);
88                 obj->len = 0;
89                 obj->data = NULL;
90         } else
91                 LASSERT(!obj->data);
92 }
93
94 int rawobj_equal(rawobj_t *a, rawobj_t *b)
95 {
96         LASSERT(a && b);
97
98         return (a->len == b->len &&
99                 (!a->len || !memcmp(a->data, b->data, a->len)));
100 }
101
102 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
103 {
104         LASSERT(src && dest);
105
106         dest->len = src->len;
107         if (dest->len) {
108                 OBD_ALLOC_LARGE(dest->data, dest->len);
109                 if (!dest->data) {
110                         dest->len = 0;
111                         return -ENOMEM;
112                 }
113                 memcpy(dest->data, src->data, dest->len);
114         } else
115                 dest->data = NULL;
116         return 0;
117 }
118
119 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
120 {
121         __u32 len;
122
123         LASSERT(obj);
124         LASSERT(buf);
125         LASSERT(buflen);
126
127         len = cfs_size_round4(obj->len);
128
129         if (*buflen < 4 + len) {
130                 CERROR("buflen %u <  %u\n", *buflen, 4 + len);
131                 return -EINVAL;
132         }
133
134         *(*buf)++ = cpu_to_le32(obj->len);
135         memcpy(*buf, obj->data, obj->len);
136         *buf += (len >> 2);
137         *buflen -= (4 + len);
138
139         return 0;
140 }
141
142 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
143                             int alloc, int local)
144 {
145         __u32 len;
146
147         if (*buflen < sizeof(__u32)) {
148                 CERROR("buflen %u\n", *buflen);
149                 return -EINVAL;
150         }
151
152         obj->len = *(*buf)++;
153         if (!local)
154                 obj->len = le32_to_cpu(obj->len);
155         *buflen -= sizeof(__u32);
156
157         if (!obj->len) {
158                 obj->data = NULL;
159                 return 0;
160         }
161
162         len = local ? obj->len : cfs_size_round4(obj->len);
163         if (*buflen < len) {
164                 CERROR("buflen %u < %u\n", *buflen, len);
165                 obj->len = 0;
166                 return -EINVAL;
167         }
168
169         if (!alloc)
170                 obj->data = (__u8 *) *buf;
171         else {
172                 OBD_ALLOC_LARGE(obj->data, obj->len);
173                 if (!obj->data) {
174                         CERROR("fail to alloc %u bytes\n", obj->len);
175                         obj->len = 0;
176                         return -ENOMEM;
177                 }
178                 memcpy(obj->data, *buf, obj->len);
179         }
180
181         *((char **)buf) += len;
182         *buflen -= len;
183
184         return 0;
185 }
186
187 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
188 {
189         return __rawobj_extract(obj, buf, buflen, 0, 0);
190 }
191
192 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
193 {
194         return __rawobj_extract(obj, buf, buflen, 1, 0);
195 }
196
197 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
198 {
199         return __rawobj_extract(obj, buf, buflen, 0, 1);
200 }
201
202 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
203 {
204         return __rawobj_extract(obj, buf, buflen, 1, 1);
205 }
206
207 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj)
208 {
209         rawobj->len = netobj->len;
210         rawobj->data = netobj->data;
211         return 0;
212 }
213
214 int rawobj_from_netobj_alloc(rawobj_t *rawobj, netobj_t *netobj)
215 {
216         rawobj->len = 0;
217         rawobj->data = NULL;
218
219         if (netobj->len == 0)
220                 return 0;
221                 
222         OBD_ALLOC_LARGE(rawobj->data, netobj->len);
223         if (rawobj->data == NULL)
224                 return -ENOMEM;
225
226         rawobj->len = netobj->len;
227         memcpy(rawobj->data, netobj->data, netobj->len);
228         return 0;
229 }
230
231 /****************************************
232  * misc more                            *
233  ****************************************/
234
235 int buffer_extract_bytes(const void **buf, __u32 *buflen,
236                          void *res, __u32 reslen)
237 {
238         if (*buflen < reslen) {
239                 CERROR("buflen %u < %u\n", *buflen, reslen);
240                 return -EINVAL;
241         }
242
243         memcpy(res, *buf, reslen);
244         *buf += reslen;
245         *buflen -= reslen;
246         return 0;
247 }