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