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