nfs: fix sparse warnings
[linux-2.6-block.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>
a5694255 18#include <linux/exportfs.h>
1da177e4 19#include <linux/mount.h>
1da177e4 20
ad06e4bd 21#include <linux/sunrpc/clnt.h>
1da177e4 22#include <linux/sunrpc/svc.h>
32c1eb0c 23#include <linux/sunrpc/svcauth_gss.h>
1da177e4 24#include <linux/nfsd/nfsd.h>
2e8138a2 25#include "auth.h"
1da177e4
LT
26
27#define NFSDDBG_FACILITY NFSDDBG_FH
1da177e4
LT
28
29
30static int nfsd_nr_verified;
31static int nfsd_nr_put;
32
1da177e4
LT
33/*
34 * our acceptability function.
35 * if NOSUBTREECHECK, accept anything
36 * if not, require that we can walk up to exp->ex_dentry
37 * doing some checks on the 'x' bits
38 */
39static int nfsd_acceptable(void *expv, struct dentry *dentry)
40{
41 struct svc_export *exp = expv;
42 int rv;
43 struct dentry *tdentry;
44 struct dentry *parent;
45
46 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
47 return 1;
48
49 tdentry = dget(dentry);
54775491 50 while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
1da177e4
LT
51 /* make sure parents give x permission to user */
52 int err;
53 parent = dget_parent(tdentry);
54 err = permission(parent->d_inode, MAY_EXEC, NULL);
55 if (err < 0) {
56 dput(parent);
57 break;
58 }
59 dput(tdentry);
60 tdentry = parent;
61 }
54775491 62 if (tdentry != exp->ex_path.dentry)
1da177e4 63 dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
54775491 64 rv = (tdentry == exp->ex_path.dentry);
1da177e4
LT
65 dput(tdentry);
66 return rv;
67}
68
69/* Type check. The correct error return for type mismatches does not seem to be
70 * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
71 * comment in the NFSv3 spec says this is incorrect (implementation notes for
72 * the write call).
73 */
83b11340 74static inline __be32
1da177e4
LT
75nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
76{
77 /* Type can be negative when creating hardlinks - not to a dir */
78 if (type > 0 && (mode & S_IFMT) != type) {
79 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
80 return nfserr_symlink;
81 else if (type == S_IFDIR)
82 return nfserr_notdir;
83 else if ((mode & S_IFMT) == S_IFDIR)
84 return nfserr_isdir;
85 else
86 return nfserr_inval;
87 }
88 if (type < 0 && (mode & S_IFMT) == -type) {
89 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
90 return nfserr_symlink;
91 else if (type == -S_IFDIR)
92 return nfserr_isdir;
93 else
94 return nfserr_notdir;
95 }
96 return 0;
97}
98
6fa02839
BF
99static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
100 struct svc_export *exp)
101{
102 /* Check if the request originated from a secure port. */
103 if (!rqstp->rq_secure && EX_SECURE(exp)) {
104 char buf[RPC_MAX_ADDRBUFLEN];
105 dprintk(KERN_WARNING
106 "nfsd: request from insecure port %s!\n",
107 svc_print_addr(rqstp, buf, sizeof(buf)));
108 return nfserr_perm;
109 }
110
111 /* Set user creds for this exportpoint */
112 return nfserrno(nfsd_setuser(rqstp, exp));
113}
114
1da177e4
LT
115/*
116 * Perform sanity checks on the dentry in a client's file handle.
117 *
118 * Note that the file handle dentry may need to be freed even after
119 * an error return.
120 *
121 * This is only called at the start of an nfsproc call, so fhp points to
122 * a svc_fh which is all 0 except for the over-the-wire file handle.
123 */
83b11340 124__be32
1da177e4
LT
125fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
126{
127 struct knfsd_fh *fh = &fhp->fh_handle;
128 struct svc_export *exp = NULL;
129 struct dentry *dentry;
83b11340 130 __be32 error = 0;
1da177e4
LT
131
132 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
133
1da177e4 134 if (!fhp->fh_dentry) {
6e91ea2b 135 struct fid *fid = NULL, sfid;
1da177e4
LT
136 int fileid_type;
137 int data_left = fh->fh_size/4;
138
139 error = nfserr_stale;
1da177e4
LT
140 if (rqstp->rq_vers > 2)
141 error = nfserr_badhandle;
142 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
143 return nfserr_nofilehandle;
144
145 if (fh->fh_version == 1) {
146 int len;
1da177e4
LT
147 if (--data_left<0) goto out;
148 switch (fh->fh_auth_type) {
149 case 0: break;
150 default: goto out;
151 }
152 len = key_len(fh->fh_fsid_type) / 4;
153 if (len == 0) goto out;
af6a4e28 154 if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
1da177e4 155 /* deprecated, convert to type 3 */
af6a4e28
N
156 len = key_len(FSID_ENCODE_DEV)/4;
157 fh->fh_fsid_type = FSID_ENCODE_DEV;
1da177e4
LT
158 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
159 fh->fh_fsid[1] = fh->fh_fsid[2];
160 }
161 if ((data_left -= len)<0) goto out;
6e91ea2b
CH
162 exp = rqst_exp_find(rqstp, fh->fh_fsid_type,
163 fh->fh_auth);
164 fid = (struct fid *)(fh->fh_auth + len);
1da177e4 165 } else {
6e91ea2b 166 __u32 tfh[2];
1da177e4
LT
167 dev_t xdev;
168 ino_t xino;
169 if (fh->fh_size != NFS_FHSIZE)
170 goto out;
171 /* assume old filehandle format */
172 xdev = old_decode_dev(fh->ofh_xdev);
173 xino = u32_to_ino_t(fh->ofh_xino);
af6a4e28 174 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
0989a788 175 exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
1da177e4
LT
176 }
177
2d3bb252
BF
178 error = nfserr_stale;
179 if (PTR_ERR(exp) == -ENOENT)
1da177e4
LT
180 goto out;
181
2d3bb252
BF
182 if (IS_ERR(exp)) {
183 error = nfserrno(PTR_ERR(exp));
1da177e4 184 goto out;
2d3bb252 185 }
1da177e4 186
6fa02839 187 error = nfsd_setuser_and_check_port(rqstp, exp);
d1bbf14f
N
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) {
6e91ea2b
CH
199 sfid.i32.ino = fh->ofh_ino;
200 sfid.i32.gen = fh->ofh_generation;
201 sfid.i32.parent_ino = fh->ofh_dirino;
202 fid = &sfid;
1da177e4
LT
203 data_left = 3;
204 if (fh->ofh_dirino == 0)
6e91ea2b 205 fileid_type = FILEID_INO32_GEN;
1da177e4 206 else
6e91ea2b 207 fileid_type = FILEID_INO32_GEN_PARENT;
1da177e4
LT
208 } else
209 fileid_type = fh->fh_fileid_type;
982aedfd 210
6e91ea2b 211 if (fileid_type == FILEID_ROOT)
54775491 212 dentry = dget(exp->ex_path.dentry);
1da177e4 213 else {
54775491 214 dentry = exportfs_decode_fh(exp->ex_path.mnt, fid,
d37065cd
CH
215 data_left, fileid_type,
216 nfsd_acceptable, exp);
1da177e4
LT
217 }
218 if (dentry == NULL)
219 goto out;
220 if (IS_ERR(dentry)) {
221 if (PTR_ERR(dentry) != -EINVAL)
222 error = nfserrno(PTR_ERR(dentry));
223 goto out;
224 }
34e9a63b 225
1da177e4
LT
226 if (S_ISDIR(dentry->d_inode->i_mode) &&
227 (dentry->d_flags & DCACHE_DISCONNECTED)) {
228 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
229 dentry->d_parent->d_name.name, dentry->d_name.name);
230 }
1da177e4
LT
231
232 fhp->fh_dentry = dentry;
233 fhp->fh_export = exp;
234 nfsd_nr_verified++;
235 } else {
6fa02839
BF
236 /*
237 * just rechecking permissions
238 * (e.g. nfsproc_create calls fh_verify, then nfsd_create
239 * does as well)
1da177e4
LT
240 */
241 dprintk("nfsd: fh_verify - just checking\n");
242 dentry = fhp->fh_dentry;
243 exp = fhp->fh_export;
6fa02839
BF
244 /*
245 * Set user creds for this exportpoint; necessary even
d1bbf14f
N
246 * in the "just checking" case because this may be a
247 * filehandle that was created by fh_compose, and that
248 * is about to be used in another nfsv4 compound
6fa02839
BF
249 * operation.
250 */
251 error = nfsd_setuser_and_check_port(rqstp, exp);
d1bbf14f
N
252 if (error)
253 goto out;
1da177e4
LT
254 }
255 cache_get(&exp->h);
256
7fc90ec9 257
1da177e4
LT
258 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
259 if (error)
260 goto out;
261
9091224f
BF
262 if (!(access & MAY_LOCK)) {
263 /*
264 * pseudoflavor restrictions are not enforced on NLM,
265 * which clients virtually always use auth_sys for,
266 * even while using RPCSEC_GSS for NFS.
267 */
268 error = check_nfsd_access(exp, rqstp);
269 if (error)
270 goto out;
271 }
32c1eb0c 272
1da177e4 273 /* Finally, check access permissions. */
0ec757df 274 error = nfsd_permission(rqstp, exp, dentry, access);
1da177e4 275
1da177e4 276 if (error) {
34e9a63b
N
277 dprintk("fh_verify: %s/%s permission failure, "
278 "acc=%x, error=%d\n",
279 dentry->d_parent->d_name.name,
280 dentry->d_name.name,
fc2dd2e5 281 access, ntohl(error));
1da177e4 282 }
1da177e4
LT
283out:
284 if (exp && !IS_ERR(exp))
285 exp_put(exp);
286 if (error == nfserr_stale)
287 nfsdstats.fh_stale++;
288 return error;
289}
290
291
292/*
293 * Compose a file handle for an NFS reply.
294 *
295 * Note that when first composed, the dentry may not yet have
296 * an inode. In this case a call to fh_update should be made
297 * before the fh goes out on the wire ...
298 */
6e91ea2b
CH
299static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
300 struct dentry *dentry)
1da177e4 301{
54775491 302 if (dentry != exp->ex_path.dentry) {
6e91ea2b
CH
303 struct fid *fid = (struct fid *)
304 (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1);
305 int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
306 int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
1da177e4 307
6e91ea2b
CH
308 fhp->fh_handle.fh_fileid_type =
309 exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
310 fhp->fh_handle.fh_size += maxsize * 4;
311 } else {
312 fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
313 }
1da177e4
LT
314}
315
316/*
317 * for composing old style file handles
318 */
319static inline void _fh_update_old(struct dentry *dentry,
320 struct svc_export *exp,
321 struct knfsd_fh *fh)
322{
323 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
324 fh->ofh_generation = dentry->d_inode->i_generation;
325 if (S_ISDIR(dentry->d_inode->i_mode) ||
326 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
327 fh->ofh_dirino = 0;
328}
329
83b11340 330__be32
982aedfd
N
331fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
332 struct svc_fh *ref_fh)
1da177e4
LT
333{
334 /* ref_fh is a reference file handle.
7e405364
N
335 * if it is non-null and for the same filesystem, then we should compose
336 * a filehandle which is of the same version, where possible.
1da177e4
LT
337 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
338 * Then create a 32byte filehandle using nfs_fhbase_old
339 *
340 */
341
b41eeef1 342 u8 version;
982aedfd 343 u8 fsid_type = 0;
1da177e4
LT
344 struct inode * inode = dentry->d_inode;
345 struct dentry *parent = dentry->d_parent;
346 __u32 *datap;
54775491
JB
347 dev_t ex_dev = exp->ex_path.dentry->d_inode->i_sb->s_dev;
348 int root_export = (exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root);
1da177e4
LT
349
350 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
351 MAJOR(ex_dev), MINOR(ex_dev),
54775491 352 (long) exp->ex_path.dentry->d_inode->i_ino,
1da177e4
LT
353 parent->d_name.name, dentry->d_name.name,
354 (inode ? inode->i_ino : 0));
355
982aedfd
N
356 /* Choose filehandle version and fsid type based on
357 * the reference filehandle (if it is in the same export)
358 * or the export options.
359 */
b41eeef1
N
360 retry:
361 version = 1;
7e405364 362 if (ref_fh && ref_fh->fh_export == exp) {
982aedfd 363 version = ref_fh->fh_handle.fh_version;
b41eeef1
N
364 fsid_type = ref_fh->fh_handle.fh_fsid_type;
365
366 if (ref_fh == fhp)
367 fh_put(ref_fh);
368 ref_fh = NULL;
369
370 switch (version) {
371 case 0xca:
af6a4e28 372 fsid_type = FSID_DEV;
b41eeef1
N
373 break;
374 case 1:
375 break;
376 default:
377 goto retry;
378 }
379
380 /* Need to check that this type works for this
381 * export point. As the fsid -> filesystem mapping
382 * was guided by user-space, there is no guarantee
383 * that the filesystem actually supports that fsid
384 * type. If it doesn't we loop around again without
385 * ref_fh set.
982aedfd 386 */
b41eeef1
N
387 switch(fsid_type) {
388 case FSID_DEV:
389 if (!old_valid_dev(ex_dev))
390 goto retry;
391 /* FALL THROUGH */
392 case FSID_MAJOR_MINOR:
393 case FSID_ENCODE_DEV:
54775491 394 if (!(exp->ex_path.dentry->d_inode->i_sb->s_type->fs_flags
b41eeef1
N
395 & FS_REQUIRES_DEV))
396 goto retry;
397 break;
398 case FSID_NUM:
399 if (! (exp->ex_flags & NFSEXP_FSID))
400 goto retry;
401 break;
402 case FSID_UUID8:
403 case FSID_UUID16:
404 if (!root_export)
405 goto retry;
406 /* fall through */
407 case FSID_UUID4_INUM:
408 case FSID_UUID16_INUM:
409 if (exp->ex_uuid == NULL)
410 goto retry;
411 break;
412 }
af6a4e28
N
413 } else if (exp->ex_uuid) {
414 if (fhp->fh_maxsize >= 64) {
415 if (root_export)
416 fsid_type = FSID_UUID16;
417 else
418 fsid_type = FSID_UUID16_INUM;
419 } else {
420 if (root_export)
421 fsid_type = FSID_UUID8;
422 else
423 fsid_type = FSID_UUID4_INUM;
424 }
1da177e4 425 } else if (exp->ex_flags & NFSEXP_FSID)
af6a4e28 426 fsid_type = FSID_NUM;
982aedfd 427 else if (!old_valid_dev(ex_dev))
1da177e4 428 /* for newer device numbers, we must use a newer fsid format */
af6a4e28 429 fsid_type = FSID_ENCODE_DEV;
982aedfd 430 else
af6a4e28 431 fsid_type = FSID_DEV;
1da177e4
LT
432
433 if (ref_fh == fhp)
434 fh_put(ref_fh);
435
436 if (fhp->fh_locked || fhp->fh_dentry) {
437 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
982aedfd 438 parent->d_name.name, dentry->d_name.name);
1da177e4
LT
439 }
440 if (fhp->fh_maxsize < NFS_FHSIZE)
441 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
982aedfd
N
442 fhp->fh_maxsize,
443 parent->d_name.name, dentry->d_name.name);
1da177e4
LT
444
445 fhp->fh_dentry = dget(dentry); /* our internal copy */
446 fhp->fh_export = exp;
447 cache_get(&exp->h);
448
982aedfd 449 if (version == 0xca) {
1da177e4
LT
450 /* old style filehandle please */
451 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
452 fhp->fh_handle.fh_size = NFS_FHSIZE;
453 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
454 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
455 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
982aedfd 456 fhp->fh_handle.ofh_xino =
54775491 457 ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino);
1da177e4
LT
458 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
459 if (inode)
460 _fh_update_old(dentry, exp, &fhp->fh_handle);
461 } else {
462 int len;
463 fhp->fh_handle.fh_version = 1;
464 fhp->fh_handle.fh_auth_type = 0;
465 datap = fhp->fh_handle.fh_auth+0;
982aedfd 466 fhp->fh_handle.fh_fsid_type = fsid_type;
af6a4e28 467 mk_fsid(fsid_type, datap, ex_dev,
54775491 468 exp->ex_path.dentry->d_inode->i_ino,
af6a4e28
N
469 exp->ex_fsid, exp->ex_uuid);
470
982aedfd 471 len = key_len(fsid_type);
1da177e4
LT
472 datap += len/4;
473 fhp->fh_handle.fh_size = 4 + len;
474
6e91ea2b
CH
475 if (inode)
476 _fh_update(fhp, exp, dentry);
1da177e4
LT
477 if (fhp->fh_handle.fh_fileid_type == 255)
478 return nfserr_opnotsupp;
479 }
480
481 nfsd_nr_verified++;
482 return 0;
483}
484
485/*
486 * Update file handle information after changing a dentry.
487 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
488 */
83b11340 489__be32
1da177e4
LT
490fh_update(struct svc_fh *fhp)
491{
492 struct dentry *dentry;
982aedfd 493
1da177e4
LT
494 if (!fhp->fh_dentry)
495 goto out_bad;
496
497 dentry = fhp->fh_dentry;
498 if (!dentry->d_inode)
499 goto out_negative;
500 if (fhp->fh_handle.fh_version != 1) {
501 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
502 } else {
6e91ea2b 503 if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
4c9608b2 504 goto out;
6e91ea2b
CH
505
506 _fh_update(fhp, fhp->fh_export, dentry);
1da177e4
LT
507 if (fhp->fh_handle.fh_fileid_type == 255)
508 return nfserr_opnotsupp;
509 }
510out:
511 return 0;
512
513out_bad:
514 printk(KERN_ERR "fh_update: fh not verified!\n");
515 goto out;
516out_negative:
517 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
518 dentry->d_parent->d_name.name, dentry->d_name.name);
519 goto out;
1da177e4
LT
520}
521
522/*
523 * Release a file handle.
524 */
525void
526fh_put(struct svc_fh *fhp)
527{
528 struct dentry * dentry = fhp->fh_dentry;
529 struct svc_export * exp = fhp->fh_export;
530 if (dentry) {
531 fh_unlock(fhp);
532 fhp->fh_dentry = NULL;
533 dput(dentry);
534#ifdef CONFIG_NFSD_V3
535 fhp->fh_pre_saved = 0;
536 fhp->fh_post_saved = 0;
537#endif
538 nfsd_nr_put++;
539 }
540 if (exp) {
baab935f 541 cache_put(&exp->h, &svc_export_cache);
1da177e4
LT
542 fhp->fh_export = NULL;
543 }
544 return;
545}
546
547/*
548 * Shorthand for dprintk()'s
549 */
550char * SVCFH_fmt(struct svc_fh *fhp)
551{
552 struct knfsd_fh *fh = &fhp->fh_handle;
553
554 static char buf[80];
555 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
556 fh->fh_size,
557 fh->fh_base.fh_pad[0],
558 fh->fh_base.fh_pad[1],
559 fh->fh_base.fh_pad[2],
560 fh->fh_base.fh_pad[3],
561 fh->fh_base.fh_pad[4],
562 fh->fh_base.fh_pad[5]);
563 return buf;
564}
af6a4e28
N
565
566enum fsid_source fsid_source(struct svc_fh *fhp)
567{
568 if (fhp->fh_handle.fh_version != 1)
569 return FSIDSOURCE_DEV;
570 switch(fhp->fh_handle.fh_fsid_type) {
571 case FSID_DEV:
572 case FSID_ENCODE_DEV:
573 case FSID_MAJOR_MINOR:
54775491 574 if (fhp->fh_export->ex_path.dentry->d_inode->i_sb->s_type->fs_flags
b8da0d1c
NB
575 & FS_REQUIRES_DEV)
576 return FSIDSOURCE_DEV;
577 break;
af6a4e28 578 case FSID_NUM:
af6a4e28
N
579 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
580 return FSIDSOURCE_FSID;
b8da0d1c
NB
581 break;
582 default:
583 break;
af6a4e28 584 }
b8da0d1c
NB
585 /* either a UUID type filehandle, or the filehandle doesn't
586 * match the export.
587 */
588 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
589 return FSIDSOURCE_FSID;
590 if (fhp->fh_export->ex_uuid)
591 return FSIDSOURCE_UUID;
592 return FSIDSOURCE_DEV;
af6a4e28 593}