blob: 5b651daad5183c8e7b31c75340bfce42f8ad28d7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/lockd/xdr.c
3 *
4 * XDR support for lockd and the lock client.
5 *
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/types.h>
10#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/nfs.h>
12
13#include <linux/sunrpc/xdr.h>
14#include <linux/sunrpc/clnt.h>
15#include <linux/sunrpc/svc.h>
16#include <linux/sunrpc/stats.h>
17#include <linux/lockd/lockd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Christoph Hellwig9c69de42014-05-06 19:37:13 +020019#include <uapi/linux/nfs2.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define NLMDBG_FACILITY NLMDBG_XDR
22
23
24static inline loff_t
25s32_to_loff_t(__s32 offset)
26{
27 return (loff_t)offset;
28}
29
30static inline __s32
31loff_t_to_s32(loff_t offset)
32{
33 __s32 res;
34 if (offset >= NLM_OFFSET_MAX)
35 res = NLM_OFFSET_MAX;
36 else if (offset <= -NLM_OFFSET_MAX)
37 res = -NLM_OFFSET_MAX;
38 else
39 res = offset;
40 return res;
41}
42
43/*
44 * XDR functions for basic NLM types
45 */
Al Viro52921e02006-10-19 23:28:46 -070046static __be32 *nlm_decode_cookie(__be32 *p, struct nlm_cookie *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 unsigned int len;
49
50 len = ntohl(*p++);
51
52 if(len==0)
53 {
54 c->len=4;
55 memset(c->data, 0, 4); /* hockeypux brain damage */
56 }
57 else if(len<=NLM_MAXCOOKIELEN)
58 {
59 c->len=len;
60 memcpy(c->data, p, len);
61 p+=XDR_QUADLEN(len);
62 }
63 else
64 {
Chuck Levere159a082007-09-11 18:01:15 -040065 dprintk("lockd: bad cookie size %d (only cookies under "
66 "%d bytes are supported.)\n",
67 len, NLM_MAXCOOKIELEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return NULL;
69 }
70 return p;
71}
72
Al Viro52921e02006-10-19 23:28:46 -070073static inline __be32 *
74nlm_encode_cookie(__be32 *p, struct nlm_cookie *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 *p++ = htonl(c->len);
77 memcpy(p, c->data, c->len);
78 p+=XDR_QUADLEN(c->len);
79 return p;
80}
81
Al Viro52921e02006-10-19 23:28:46 -070082static __be32 *
83nlm_decode_fh(__be32 *p, struct nfs_fh *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 unsigned int len;
86
87 if ((len = ntohl(*p++)) != NFS2_FHSIZE) {
Chuck Levere159a082007-09-11 18:01:15 -040088 dprintk("lockd: bad fhandle size %d (should be %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 len, NFS2_FHSIZE);
90 return NULL;
91 }
92 f->size = NFS2_FHSIZE;
93 memset(f->data, 0, sizeof(f->data));
94 memcpy(f->data, p, NFS2_FHSIZE);
95 return p + XDR_QUADLEN(NFS2_FHSIZE);
96}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
99 * Encode and decode owner handle
100 */
Al Viro52921e02006-10-19 23:28:46 -0700101static inline __be32 *
102nlm_decode_oh(__be32 *p, struct xdr_netobj *oh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 return xdr_decode_netobj(p, oh);
105}
106
Al Viro52921e02006-10-19 23:28:46 -0700107static inline __be32 *
108nlm_encode_oh(__be32 *p, struct xdr_netobj *oh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 return xdr_encode_netobj(p, oh);
111}
112
Al Viro52921e02006-10-19 23:28:46 -0700113static __be32 *
114nlm_decode_lock(__be32 *p, struct nlm_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct file_lock *fl = &lock->fl;
117 s32 start, len, end;
118
119 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
120 &lock->len,
121 NLM_MAXSTRLEN))
122 || !(p = nlm_decode_fh(p, &lock->fh))
123 || !(p = nlm_decode_oh(p, &lock->oh)))
124 return NULL;
Trond Myklebust7bab3772006-03-20 13:44:06 -0500125 lock->svid = ntohl(*p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 locks_init_lock(fl);
128 fl->fl_owner = current->files;
Trond Myklebust7bab3772006-03-20 13:44:06 -0500129 fl->fl_pid = (pid_t)lock->svid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 fl->fl_flags = FL_POSIX;
131 fl->fl_type = F_RDLCK; /* as good as anything else */
132 start = ntohl(*p++);
133 len = ntohl(*p++);
134 end = start + len - 1;
135
136 fl->fl_start = s32_to_loff_t(start);
137
138 if (len == 0 || end < 0)
139 fl->fl_end = OFFSET_MAX;
140 else
141 fl->fl_end = s32_to_loff_t(end);
142 return p;
143}
144
145/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * Encode result of a TEST/TEST_MSG call
147 */
Al Viro52921e02006-10-19 23:28:46 -0700148static __be32 *
149nlm_encode_testres(__be32 *p, struct nlm_res *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 s32 start, len;
152
153 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
154 return NULL;
155 *p++ = resp->status;
156
157 if (resp->status == nlm_lck_denied) {
158 struct file_lock *fl = &resp->lock.fl;
159
160 *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
Trond Myklebust7bab3772006-03-20 13:44:06 -0500161 *p++ = htonl(resp->lock.svid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 /* Encode owner handle. */
164 if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
165 return NULL;
166
167 start = loff_t_to_s32(fl->fl_start);
168 if (fl->fl_end == OFFSET_MAX)
169 len = 0;
170 else
171 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
172
173 *p++ = htonl(start);
174 *p++ = htonl(len);
175 }
176
177 return p;
178}
179
180
181/*
182 * First, the server side XDR functions
183 */
184int
Al Viro52921e02006-10-19 23:28:46 -0700185nlmsvc_decode_testargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 u32 exclusive;
188
189 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
190 return 0;
191
192 exclusive = ntohl(*p++);
193 if (!(p = nlm_decode_lock(p, &argp->lock)))
194 return 0;
195 if (exclusive)
196 argp->lock.fl.fl_type = F_WRLCK;
197
198 return xdr_argsize_check(rqstp, p);
199}
200
201int
Al Viro52921e02006-10-19 23:28:46 -0700202nlmsvc_encode_testres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 if (!(p = nlm_encode_testres(p, resp)))
205 return 0;
206 return xdr_ressize_check(rqstp, p);
207}
208
209int
Al Viro52921e02006-10-19 23:28:46 -0700210nlmsvc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 u32 exclusive;
213
214 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
215 return 0;
216 argp->block = ntohl(*p++);
217 exclusive = ntohl(*p++);
218 if (!(p = nlm_decode_lock(p, &argp->lock)))
219 return 0;
220 if (exclusive)
221 argp->lock.fl.fl_type = F_WRLCK;
222 argp->reclaim = ntohl(*p++);
223 argp->state = ntohl(*p++);
224 argp->monitor = 1; /* monitor client by default */
225
226 return xdr_argsize_check(rqstp, p);
227}
228
229int
Al Viro52921e02006-10-19 23:28:46 -0700230nlmsvc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 u32 exclusive;
233
234 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
235 return 0;
236 argp->block = ntohl(*p++);
237 exclusive = ntohl(*p++);
238 if (!(p = nlm_decode_lock(p, &argp->lock)))
239 return 0;
240 if (exclusive)
241 argp->lock.fl.fl_type = F_WRLCK;
242 return xdr_argsize_check(rqstp, p);
243}
244
245int
Al Viro52921e02006-10-19 23:28:46 -0700246nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 if (!(p = nlm_decode_cookie(p, &argp->cookie))
249 || !(p = nlm_decode_lock(p, &argp->lock)))
250 return 0;
251 argp->lock.fl.fl_type = F_UNLCK;
252 return xdr_argsize_check(rqstp, p);
253}
254
255int
Al Viro52921e02006-10-19 23:28:46 -0700256nlmsvc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct nlm_lock *lock = &argp->lock;
259
260 memset(lock, 0, sizeof(*lock));
261 locks_init_lock(&lock->fl);
Trond Myklebust7bab3772006-03-20 13:44:06 -0500262 lock->svid = ~(u32) 0;
263 lock->fl.fl_pid = (pid_t)lock->svid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (!(p = nlm_decode_cookie(p, &argp->cookie))
266 || !(p = xdr_decode_string_inplace(p, &lock->caller,
267 &lock->len, NLM_MAXSTRLEN))
268 || !(p = nlm_decode_fh(p, &lock->fh))
269 || !(p = nlm_decode_oh(p, &lock->oh)))
270 return 0;
271 argp->fsm_mode = ntohl(*p++);
272 argp->fsm_access = ntohl(*p++);
273 return xdr_argsize_check(rqstp, p);
274}
275
276int
Al Viro52921e02006-10-19 23:28:46 -0700277nlmsvc_encode_shareres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
280 return 0;
281 *p++ = resp->status;
282 *p++ = xdr_zero; /* sequence argument */
283 return xdr_ressize_check(rqstp, p);
284}
285
286int
Al Viro52921e02006-10-19 23:28:46 -0700287nlmsvc_encode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
290 return 0;
291 *p++ = resp->status;
292 return xdr_ressize_check(rqstp, p);
293}
294
295int
Al Viro52921e02006-10-19 23:28:46 -0700296nlmsvc_decode_notify(struct svc_rqst *rqstp, __be32 *p, struct nlm_args *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct nlm_lock *lock = &argp->lock;
299
300 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
301 &lock->len, NLM_MAXSTRLEN)))
302 return 0;
303 argp->state = ntohl(*p++);
304 return xdr_argsize_check(rqstp, p);
305}
306
307int
Al Viro52921e02006-10-19 23:28:46 -0700308nlmsvc_decode_reboot(struct svc_rqst *rqstp, __be32 *p, struct nlm_reboot *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
311 return 0;
312 argp->state = ntohl(*p++);
Chuck Lever576df462008-12-05 19:03:39 -0500313 memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
314 p += XDR_QUADLEN(SM_PRIV_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return xdr_argsize_check(rqstp, p);
316}
317
318int
Al Viro52921e02006-10-19 23:28:46 -0700319nlmsvc_decode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
322 return 0;
Al Viroe8c5c042006-12-13 00:35:03 -0800323 resp->status = *p++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return xdr_argsize_check(rqstp, p);
325}
326
327int
Al Viro52921e02006-10-19 23:28:46 -0700328nlmsvc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 return xdr_argsize_check(rqstp, p);
331}
332
333int
Al Viro52921e02006-10-19 23:28:46 -0700334nlmsvc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 return xdr_ressize_check(rqstp, p);
337}