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