]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/nfsd/nfsfh.c
knfsd: various nfsd xdr cleanups
[net-next-2.6.git] / fs / nfsd / nfsfh.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfsd/nfsfh.c
3 *
4 * NFS server file handle treatment.
5 *
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org>
8 * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999
9 * ... and again Southern-Winter 2001 to support export_operations
10 */
11
1da177e4 12#include <linux/slab.h>
1da177e4
LT
13#include <linux/fs.h>
14#include <linux/unistd.h>
15#include <linux/string.h>
16#include <linux/stat.h>
17#include <linux/dcache.h>
18#include <linux/mount.h>
1da177e4 19
ad06e4bd 20#include <linux/sunrpc/clnt.h>
1da177e4
LT
21#include <linux/sunrpc/svc.h>
22#include <linux/nfsd/nfsd.h>
23
24#define NFSDDBG_FACILITY NFSDDBG_FH
1da177e4
LT
25
26
27static int nfsd_nr_verified;
28static int nfsd_nr_put;
29
30extern struct export_operations export_op_default;
31
32#define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
33
34/*
35 * our acceptability function.
36 * if NOSUBTREECHECK, accept anything
37 * if not, require that we can walk up to exp->ex_dentry
38 * doing some checks on the 'x' bits
39 */
40static int nfsd_acceptable(void *expv, struct dentry *dentry)
41{
42 struct svc_export *exp = expv;
43 int rv;
44 struct dentry *tdentry;
45 struct dentry *parent;
46
47 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
48 return 1;
49
50 tdentry = dget(dentry);
51 while (tdentry != exp->ex_dentry && ! IS_ROOT(tdentry)) {
52 /* make sure parents give x permission to user */
53 int err;
54 parent = dget_parent(tdentry);
55 err = permission(parent->d_inode, MAY_EXEC, NULL);
56 if (err < 0) {
57 dput(parent);
58 break;
59 }
60 dput(tdentry);
61 tdentry = parent;
62 }
63 if (tdentry != exp->ex_dentry)
64 dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
65 rv = (tdentry == exp->ex_dentry);
66 dput(tdentry);
67 return rv;
68}
69
70/* Type check. The correct error return for type mismatches does not seem to be
71 * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
72 * comment in the NFSv3 spec says this is incorrect (implementation notes for
73 * the write call).
74 */
83b11340 75static inline __be32
1da177e4
LT
76nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
77{
78 /* Type can be negative when creating hardlinks - not to a dir */
79 if (type > 0 && (mode & S_IFMT) != type) {
80 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
81 return nfserr_symlink;
82 else if (type == S_IFDIR)
83 return nfserr_notdir;
84 else if ((mode & S_IFMT) == S_IFDIR)
85 return nfserr_isdir;
86 else
87 return nfserr_inval;
88 }
89 if (type < 0 && (mode & S_IFMT) == -type) {
90 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
91 return nfserr_symlink;
92 else if (type == -S_IFDIR)
93 return nfserr_isdir;
94 else
95 return nfserr_notdir;
96 }
97 return 0;
98}
99
100/*
101 * Perform sanity checks on the dentry in a client's file handle.
102 *
103 * Note that the file handle dentry may need to be freed even after
104 * an error return.
105 *
106 * This is only called at the start of an nfsproc call, so fhp points to
107 * a svc_fh which is all 0 except for the over-the-wire file handle.
108 */
83b11340 109__be32
1da177e4
LT
110fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
111{
112 struct knfsd_fh *fh = &fhp->fh_handle;
113 struct svc_export *exp = NULL;
114 struct dentry *dentry;
83b11340 115 __be32 error = 0;
1da177e4
LT
116
117 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
118
1da177e4
LT
119 if (!fhp->fh_dentry) {
120 __u32 *datap=NULL;
121 __u32 tfh[3]; /* filehandle fragment for oldstyle filehandles */
122 int fileid_type;
123 int data_left = fh->fh_size/4;
124
125 error = nfserr_stale;
126 if (rqstp->rq_client == NULL)
127 goto out;
128 if (rqstp->rq_vers > 2)
129 error = nfserr_badhandle;
130 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
131 return nfserr_nofilehandle;
132
133 if (fh->fh_version == 1) {
134 int len;
135 datap = fh->fh_auth;
136 if (--data_left<0) goto out;
137 switch (fh->fh_auth_type) {
138 case 0: break;
139 default: goto out;
140 }
141 len = key_len(fh->fh_fsid_type) / 4;
142 if (len == 0) goto out;
af6a4e28 143 if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
1da177e4 144 /* deprecated, convert to type 3 */
af6a4e28
N
145 len = key_len(FSID_ENCODE_DEV)/4;
146 fh->fh_fsid_type = FSID_ENCODE_DEV;
1da177e4
LT
147 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
148 fh->fh_fsid[1] = fh->fh_fsid[2];
149 }
150 if ((data_left -= len)<0) goto out;
151 exp = exp_find(rqstp->rq_client, fh->fh_fsid_type, datap, &rqstp->rq_chandle);
152 datap += len;
153 } else {
154 dev_t xdev;
155 ino_t xino;
156 if (fh->fh_size != NFS_FHSIZE)
157 goto out;
158 /* assume old filehandle format */
159 xdev = old_decode_dev(fh->ofh_xdev);
160 xino = u32_to_ino_t(fh->ofh_xino);
af6a4e28
N
161 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
162 exp = exp_find(rqstp->rq_client, FSID_DEV, tfh,
163 &rqstp->rq_chandle);
1da177e4
LT
164 }
165
e0bb89ef
BF
166 if (IS_ERR(exp) && (PTR_ERR(exp) == -EAGAIN
167 || PTR_ERR(exp) == -ETIMEDOUT)) {
168 error = nfserrno(PTR_ERR(exp));
1da177e4 169 goto out;
e0bb89ef 170 }
1da177e4
LT
171
172 error = nfserr_stale;
173 if (!exp || IS_ERR(exp))
174 goto out;
175
176 /* Check if the request originated from a secure port. */
177 error = nfserr_perm;
178 if (!rqstp->rq_secure && EX_SECURE(exp)) {
ad06e4bd 179 char buf[RPC_MAX_ADDRBUFLEN];
1da177e4 180 printk(KERN_WARNING
ad06e4bd
CL
181 "nfsd: request from insecure port %s!\n",
182 svc_print_addr(rqstp, buf, sizeof(buf)));
1da177e4
LT
183 goto out;
184 }
185
d1bbf14f
N
186 /* Set user creds for this exportpoint */
187 error = nfserrno(nfsd_setuser(rqstp, exp));
188 if (error)
189 goto out;
190
1da177e4
LT
191 /*
192 * Look up the dentry using the NFS file handle.
193 */
194 error = nfserr_stale;
195 if (rqstp->rq_vers > 2)
196 error = nfserr_badhandle;
197
198 if (fh->fh_version != 1) {
199 tfh[0] = fh->ofh_ino;
200 tfh[1] = fh->ofh_generation;
201 tfh[2] = fh->ofh_dirino;
202 datap = tfh;
203 data_left = 3;
204 if (fh->ofh_dirino == 0)
205 fileid_type = 1;
206 else
207 fileid_type = 2;
208 } else
209 fileid_type = fh->fh_fileid_type;
982aedfd 210
1da177e4
LT
211 if (fileid_type == 0)
212 dentry = dget(exp->ex_dentry);
213 else {
214 struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
215 dentry = CALL(nop,decode_fh)(exp->ex_mnt->mnt_sb,
216 datap, data_left,
217 fileid_type,
218 nfsd_acceptable, exp);
219 }
220 if (dentry == NULL)
221 goto out;
222 if (IS_ERR(dentry)) {
223 if (PTR_ERR(dentry) != -EINVAL)
224 error = nfserrno(PTR_ERR(dentry));
225 goto out;
226 }
34e9a63b 227
1da177e4
LT
228 if (S_ISDIR(dentry->d_inode->i_mode) &&
229 (dentry->d_flags & DCACHE_DISCONNECTED)) {
230 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
231 dentry->d_parent->d_name.name, dentry->d_name.name);
232 }
1da177e4
LT
233
234 fhp->fh_dentry = dentry;
235 fhp->fh_export = exp;
236 nfsd_nr_verified++;
237 } else {
238 /* just rechecking permissions
239 * (e.g. nfsproc_create calls fh_verify, then nfsd_create does as well)
240 */
241 dprintk("nfsd: fh_verify - just checking\n");
242 dentry = fhp->fh_dentry;
243 exp = fhp->fh_export;
d1bbf14f
N
244 /* Set user creds for this exportpoint; necessary even
245 * in the "just checking" case because this may be a
246 * filehandle that was created by fh_compose, and that
247 * is about to be used in another nfsv4 compound
248 * operation */
249 error = nfserrno(nfsd_setuser(rqstp, exp));
250 if (error)
251 goto out;
1da177e4
LT
252 }
253 cache_get(&exp->h);
254
7fc90ec9 255
1da177e4
LT
256 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
257 if (error)
258 goto out;
259
260 /* Finally, check access permissions. */
261 error = nfsd_permission(exp, dentry, access);
262
1da177e4 263 if (error) {
34e9a63b
N
264 dprintk("fh_verify: %s/%s permission failure, "
265 "acc=%x, error=%d\n",
266 dentry->d_parent->d_name.name,
267 dentry->d_name.name,
fc2dd2e5 268 access, ntohl(error));
1da177e4 269 }
1da177e4
LT
270out:
271 if (exp && !IS_ERR(exp))
272 exp_put(exp);
273 if (error == nfserr_stale)
274 nfsdstats.fh_stale++;
275 return error;
276}
277
278
279/*
280 * Compose a file handle for an NFS reply.
281 *
282 * Note that when first composed, the dentry may not yet have
283 * an inode. In this case a call to fh_update should be made
284 * before the fh goes out on the wire ...
285 */
286static inline int _fh_update(struct dentry *dentry, struct svc_export *exp,
287 __u32 *datap, int *maxsize)
288{
289 struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
982aedfd 290
1da177e4
LT
291 if (dentry == exp->ex_dentry) {
292 *maxsize = 0;
293 return 0;
294 }
295
296 return CALL(nop,encode_fh)(dentry, datap, maxsize,
297 !(exp->ex_flags&NFSEXP_NOSUBTREECHECK));
298}
299
300/*
301 * for composing old style file handles
302 */
303static inline void _fh_update_old(struct dentry *dentry,
304 struct svc_export *exp,
305 struct knfsd_fh *fh)
306{
307 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
308 fh->ofh_generation = dentry->d_inode->i_generation;
309 if (S_ISDIR(dentry->d_inode->i_mode) ||
310 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
311 fh->ofh_dirino = 0;
312}
313
83b11340 314__be32
982aedfd
N
315fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
316 struct svc_fh *ref_fh)
1da177e4
LT
317{
318 /* ref_fh is a reference file handle.
7e405364
N
319 * if it is non-null and for the same filesystem, then we should compose
320 * a filehandle which is of the same version, where possible.
1da177e4
LT
321 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
322 * Then create a 32byte filehandle using nfs_fhbase_old
323 *
324 */
325
982aedfd
N
326 u8 version = 1;
327 u8 fsid_type = 0;
1da177e4
LT
328 struct inode * inode = dentry->d_inode;
329 struct dentry *parent = dentry->d_parent;
330 __u32 *datap;
331 dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev;
af6a4e28 332 int root_export = (exp->ex_dentry == exp->ex_dentry->d_sb->s_root);
1da177e4
LT
333
334 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
335 MAJOR(ex_dev), MINOR(ex_dev),
336 (long) exp->ex_dentry->d_inode->i_ino,
337 parent->d_name.name, dentry->d_name.name,
338 (inode ? inode->i_ino : 0));
339
982aedfd
N
340 /* Choose filehandle version and fsid type based on
341 * the reference filehandle (if it is in the same export)
342 * or the export options.
343 */
7e405364 344 if (ref_fh && ref_fh->fh_export == exp) {
982aedfd
N
345 version = ref_fh->fh_handle.fh_version;
346 if (version == 0xca)
af6a4e28 347 fsid_type = FSID_DEV;
1da177e4 348 else
982aedfd
N
349 fsid_type = ref_fh->fh_handle.fh_fsid_type;
350 /* We know this version/type works for this export
351 * so there is no need for further checks.
352 */
af6a4e28
N
353 } else if (exp->ex_uuid) {
354 if (fhp->fh_maxsize >= 64) {
355 if (root_export)
356 fsid_type = FSID_UUID16;
357 else
358 fsid_type = FSID_UUID16_INUM;
359 } else {
360 if (root_export)
361 fsid_type = FSID_UUID8;
362 else
363 fsid_type = FSID_UUID4_INUM;
364 }
1da177e4 365 } else if (exp->ex_flags & NFSEXP_FSID)
af6a4e28 366 fsid_type = FSID_NUM;
982aedfd 367 else if (!old_valid_dev(ex_dev))
1da177e4 368 /* for newer device numbers, we must use a newer fsid format */
af6a4e28 369 fsid_type = FSID_ENCODE_DEV;
982aedfd 370 else
af6a4e28 371 fsid_type = FSID_DEV;
1da177e4
LT
372
373 if (ref_fh == fhp)
374 fh_put(ref_fh);
375
376 if (fhp->fh_locked || fhp->fh_dentry) {
377 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
982aedfd 378 parent->d_name.name, dentry->d_name.name);
1da177e4
LT
379 }
380 if (fhp->fh_maxsize < NFS_FHSIZE)
381 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
982aedfd
N
382 fhp->fh_maxsize,
383 parent->d_name.name, dentry->d_name.name);
1da177e4
LT
384
385 fhp->fh_dentry = dget(dentry); /* our internal copy */
386 fhp->fh_export = exp;
387 cache_get(&exp->h);
388
982aedfd 389 if (version == 0xca) {
1da177e4
LT
390 /* old style filehandle please */
391 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
392 fhp->fh_handle.fh_size = NFS_FHSIZE;
393 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
394 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
395 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
982aedfd
N
396 fhp->fh_handle.ofh_xino =
397 ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
1da177e4
LT
398 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
399 if (inode)
400 _fh_update_old(dentry, exp, &fhp->fh_handle);
401 } else {
402 int len;
403 fhp->fh_handle.fh_version = 1;
404 fhp->fh_handle.fh_auth_type = 0;
405 datap = fhp->fh_handle.fh_auth+0;
982aedfd 406 fhp->fh_handle.fh_fsid_type = fsid_type;
af6a4e28
N
407 mk_fsid(fsid_type, datap, ex_dev,
408 exp->ex_dentry->d_inode->i_ino,
409 exp->ex_fsid, exp->ex_uuid);
410
982aedfd 411 len = key_len(fsid_type);
1da177e4
LT
412 datap += len/4;
413 fhp->fh_handle.fh_size = 4 + len;
414
415 if (inode) {
416 int size = (fhp->fh_maxsize-len-4)/4;
417 fhp->fh_handle.fh_fileid_type =
418 _fh_update(dentry, exp, datap, &size);
419 fhp->fh_handle.fh_size += size*4;
420 }
421 if (fhp->fh_handle.fh_fileid_type == 255)
422 return nfserr_opnotsupp;
423 }
424
425 nfsd_nr_verified++;
426 return 0;
427}
428
429/*
430 * Update file handle information after changing a dentry.
431 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
432 */
83b11340 433__be32
1da177e4
LT
434fh_update(struct svc_fh *fhp)
435{
436 struct dentry *dentry;
437 __u32 *datap;
982aedfd 438
1da177e4
LT
439 if (!fhp->fh_dentry)
440 goto out_bad;
441
442 dentry = fhp->fh_dentry;
443 if (!dentry->d_inode)
444 goto out_negative;
445 if (fhp->fh_handle.fh_version != 1) {
446 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
447 } else {
448 int size;
449 if (fhp->fh_handle.fh_fileid_type != 0)
4c9608b2 450 goto out;
1da177e4
LT
451 datap = fhp->fh_handle.fh_auth+
452 fhp->fh_handle.fh_size/4 -1;
453 size = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
454 fhp->fh_handle.fh_fileid_type =
455 _fh_update(dentry, fhp->fh_export, datap, &size);
456 fhp->fh_handle.fh_size += size*4;
457 if (fhp->fh_handle.fh_fileid_type == 255)
458 return nfserr_opnotsupp;
459 }
460out:
461 return 0;
462
463out_bad:
464 printk(KERN_ERR "fh_update: fh not verified!\n");
465 goto out;
466out_negative:
467 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
468 dentry->d_parent->d_name.name, dentry->d_name.name);
469 goto out;
1da177e4
LT
470}
471
472/*
473 * Release a file handle.
474 */
475void
476fh_put(struct svc_fh *fhp)
477{
478 struct dentry * dentry = fhp->fh_dentry;
479 struct svc_export * exp = fhp->fh_export;
480 if (dentry) {
481 fh_unlock(fhp);
482 fhp->fh_dentry = NULL;
483 dput(dentry);
484#ifdef CONFIG_NFSD_V3
485 fhp->fh_pre_saved = 0;
486 fhp->fh_post_saved = 0;
487#endif
488 nfsd_nr_put++;
489 }
490 if (exp) {
baab935f 491 cache_put(&exp->h, &svc_export_cache);
1da177e4
LT
492 fhp->fh_export = NULL;
493 }
494 return;
495}
496
497/*
498 * Shorthand for dprintk()'s
499 */
500char * SVCFH_fmt(struct svc_fh *fhp)
501{
502 struct knfsd_fh *fh = &fhp->fh_handle;
503
504 static char buf[80];
505 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
506 fh->fh_size,
507 fh->fh_base.fh_pad[0],
508 fh->fh_base.fh_pad[1],
509 fh->fh_base.fh_pad[2],
510 fh->fh_base.fh_pad[3],
511 fh->fh_base.fh_pad[4],
512 fh->fh_base.fh_pad[5]);
513 return buf;
514}
af6a4e28
N
515
516enum fsid_source fsid_source(struct svc_fh *fhp)
517{
518 if (fhp->fh_handle.fh_version != 1)
519 return FSIDSOURCE_DEV;
520 switch(fhp->fh_handle.fh_fsid_type) {
521 case FSID_DEV:
522 case FSID_ENCODE_DEV:
523 case FSID_MAJOR_MINOR:
524 return FSIDSOURCE_DEV;
525 case FSID_NUM:
526 return FSIDSOURCE_FSID;
527 default:
528 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
529 return FSIDSOURCE_FSID;
530 else
531 return FSIDSOURCE_UUID;
532 }
533}