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