Merge commit 'v2.6.28-rc2' into x86/pci-ioapic-boot-irq-quirks
[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/sunrpc/svcauth_gss.h>
24 #include <linux/nfsd/nfsd.h>
25 #include "auth.h"
26
27 #define NFSDDBG_FACILITY                NFSDDBG_FH
28
29
30 static int nfsd_nr_verified;
31 static int nfsd_nr_put;
32
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  */
39 static 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);
50         while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
51                 /* make sure parents give x permission to user */
52                 int err;
53                 parent = dget_parent(tdentry);
54                 err = inode_permission(parent->d_inode, MAY_EXEC);
55                 if (err < 0) {
56                         dput(parent);
57                         break;
58                 }
59                 dput(tdentry);
60                 tdentry = parent;
61         }
62         if (tdentry != exp->ex_path.dentry)
63                 dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
64         rv = (tdentry == exp->ex_path.dentry);
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  */
74 static inline __be32
75 nfsd_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
99 static __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                 RPC_IFDEBUG(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
115 /*
116  * Use the given filehandle to look up the corresponding export and
117  * dentry.  On success, the results are used to set fh_export and
118  * fh_dentry.
119  */
120 static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
121 {
122         struct knfsd_fh *fh = &fhp->fh_handle;
123         struct fid *fid = NULL, sfid;
124         struct svc_export *exp;
125         struct dentry *dentry;
126         int fileid_type;
127         int data_left = fh->fh_size/4;
128         __be32 error;
129
130         error = nfserr_stale;
131         if (rqstp->rq_vers > 2)
132                 error = nfserr_badhandle;
133         if (rqstp->rq_vers == 4 && fh->fh_size == 0)
134                 return nfserr_nofilehandle;
135
136         if (fh->fh_version == 1) {
137                 int len;
138
139                 if (--data_left < 0)
140                         return error;
141                 if (fh->fh_auth_type != 0)
142                         return error;
143                 len = key_len(fh->fh_fsid_type) / 4;
144                 if (len == 0)
145                         return error;
146                 if  (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
147                         /* deprecated, convert to type 3 */
148                         len = key_len(FSID_ENCODE_DEV)/4;
149                         fh->fh_fsid_type = FSID_ENCODE_DEV;
150                         fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
151                         fh->fh_fsid[1] = fh->fh_fsid[2];
152                 }
153                 data_left -= len;
154                 if (data_left < 0)
155                         return error;
156                 exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_auth);
157                 fid = (struct fid *)(fh->fh_auth + len);
158         } else {
159                 __u32 tfh[2];
160                 dev_t xdev;
161                 ino_t xino;
162
163                 if (fh->fh_size != NFS_FHSIZE)
164                         return error;
165                 /* assume old filehandle format */
166                 xdev = old_decode_dev(fh->ofh_xdev);
167                 xino = u32_to_ino_t(fh->ofh_xino);
168                 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
169                 exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
170         }
171
172         error = nfserr_stale;
173         if (PTR_ERR(exp) == -ENOENT)
174                 return error;
175
176         if (IS_ERR(exp))
177                 return nfserrno(PTR_ERR(exp));
178
179         if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
180                 /* Elevate privileges so that the lack of 'r' or 'x'
181                  * permission on some parent directory will
182                  * not stop exportfs_decode_fh from being able
183                  * to reconnect a directory into the dentry cache.
184                  * The same problem can affect "SUBTREECHECK" exports,
185                  * but as nfsd_acceptable depends on correct
186                  * access control settings being in effect, we cannot
187                  * fix that case easily.
188                  */
189                 current->cap_effective =
190                         cap_raise_nfsd_set(current->cap_effective,
191                                            current->cap_permitted);
192         } else {
193                 error = nfsd_setuser_and_check_port(rqstp, exp);
194                 if (error)
195                         goto out;
196         }
197
198         /*
199          * Look up the dentry using the NFS file handle.
200          */
201         error = nfserr_stale;
202         if (rqstp->rq_vers > 2)
203                 error = nfserr_badhandle;
204
205         if (fh->fh_version != 1) {
206                 sfid.i32.ino = fh->ofh_ino;
207                 sfid.i32.gen = fh->ofh_generation;
208                 sfid.i32.parent_ino = fh->ofh_dirino;
209                 fid = &sfid;
210                 data_left = 3;
211                 if (fh->ofh_dirino == 0)
212                         fileid_type = FILEID_INO32_GEN;
213                 else
214                         fileid_type = FILEID_INO32_GEN_PARENT;
215         } else
216                 fileid_type = fh->fh_fileid_type;
217
218         if (fileid_type == FILEID_ROOT)
219                 dentry = dget(exp->ex_path.dentry);
220         else {
221                 dentry = exportfs_decode_fh(exp->ex_path.mnt, fid,
222                                 data_left, 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         }
232
233         if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
234                 error = nfsd_setuser_and_check_port(rqstp, exp);
235                 if (error) {
236                         dput(dentry);
237                         goto out;
238                 }
239         }
240
241         if (S_ISDIR(dentry->d_inode->i_mode) &&
242                         (dentry->d_flags & DCACHE_DISCONNECTED)) {
243                 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
244                                 dentry->d_parent->d_name.name, dentry->d_name.name);
245         }
246
247         fhp->fh_dentry = dentry;
248         fhp->fh_export = exp;
249         nfsd_nr_verified++;
250         return 0;
251 out:
252         exp_put(exp);
253         return error;
254 }
255
256 /*
257  * Perform sanity checks on the dentry in a client's file handle.
258  *
259  * Note that the file handle dentry may need to be freed even after
260  * an error return.
261  *
262  * This is only called at the start of an nfsproc call, so fhp points to
263  * a svc_fh which is all 0 except for the over-the-wire file handle.
264  */
265 __be32
266 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
267 {
268         struct svc_export *exp;
269         struct dentry   *dentry;
270         __be32          error;
271
272         dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
273
274         if (!fhp->fh_dentry) {
275                 error = nfsd_set_fh_dentry(rqstp, fhp);
276                 if (error)
277                         goto out;
278                 dentry = fhp->fh_dentry;
279                 exp = fhp->fh_export;
280         } else {
281                 /*
282                  * just rechecking permissions
283                  * (e.g. nfsproc_create calls fh_verify, then nfsd_create
284                  * does as well)
285                  */
286                 dprintk("nfsd: fh_verify - just checking\n");
287                 dentry = fhp->fh_dentry;
288                 exp = fhp->fh_export;
289                 /*
290                  * Set user creds for this exportpoint; necessary even
291                  * in the "just checking" case because this may be a
292                  * filehandle that was created by fh_compose, and that
293                  * is about to be used in another nfsv4 compound
294                  * operation.
295                  */
296                 error = nfsd_setuser_and_check_port(rqstp, exp);
297                 if (error)
298                         goto out;
299         }
300
301         error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
302         if (error)
303                 goto out;
304
305         /*
306          * pseudoflavor restrictions are not enforced on NLM,
307          * which clients virtually always use auth_sys for,
308          * even while using RPCSEC_GSS for NFS.
309          */
310         if (access & NFSD_MAY_LOCK)
311                 goto skip_pseudoflavor_check;
312         /*
313          * Clients may expect to be able to use auth_sys during mount,
314          * even if they use gss for everything else; see section 2.3.2
315          * of rfc 2623.
316          */
317         if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
318                         && exp->ex_path.dentry == dentry)
319                 goto skip_pseudoflavor_check;
320
321         error = check_nfsd_access(exp, rqstp);
322         if (error)
323                 goto out;
324
325 skip_pseudoflavor_check:
326         /* Finally, check access permissions. */
327         error = nfsd_permission(rqstp, exp, dentry, access);
328
329         if (error) {
330                 dprintk("fh_verify: %s/%s permission failure, "
331                         "acc=%x, error=%d\n",
332                         dentry->d_parent->d_name.name,
333                         dentry->d_name.name,
334                         access, ntohl(error));
335         }
336 out:
337         if (error == nfserr_stale)
338                 nfsdstats.fh_stale++;
339         return error;
340 }
341
342
343 /*
344  * Compose a file handle for an NFS reply.
345  *
346  * Note that when first composed, the dentry may not yet have
347  * an inode.  In this case a call to fh_update should be made
348  * before the fh goes out on the wire ...
349  */
350 static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
351                 struct dentry *dentry)
352 {
353         if (dentry != exp->ex_path.dentry) {
354                 struct fid *fid = (struct fid *)
355                         (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1);
356                 int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
357                 int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
358
359                 fhp->fh_handle.fh_fileid_type =
360                         exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
361                 fhp->fh_handle.fh_size += maxsize * 4;
362         } else {
363                 fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
364         }
365 }
366
367 /*
368  * for composing old style file handles
369  */
370 static inline void _fh_update_old(struct dentry *dentry,
371                                   struct svc_export *exp,
372                                   struct knfsd_fh *fh)
373 {
374         fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
375         fh->ofh_generation = dentry->d_inode->i_generation;
376         if (S_ISDIR(dentry->d_inode->i_mode) ||
377             (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
378                 fh->ofh_dirino = 0;
379 }
380
381 __be32
382 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
383            struct svc_fh *ref_fh)
384 {
385         /* ref_fh is a reference file handle.
386          * if it is non-null and for the same filesystem, then we should compose
387          * a filehandle which is of the same version, where possible.
388          * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
389          * Then create a 32byte filehandle using nfs_fhbase_old
390          *
391          */
392
393         u8 version;
394         u8 fsid_type = 0;
395         struct inode * inode = dentry->d_inode;
396         struct dentry *parent = dentry->d_parent;
397         __u32 *datap;
398         dev_t ex_dev = exp->ex_path.dentry->d_inode->i_sb->s_dev;
399         int root_export = (exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root);
400
401         dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
402                 MAJOR(ex_dev), MINOR(ex_dev),
403                 (long) exp->ex_path.dentry->d_inode->i_ino,
404                 parent->d_name.name, dentry->d_name.name,
405                 (inode ? inode->i_ino : 0));
406
407         /* Choose filehandle version and fsid type based on
408          * the reference filehandle (if it is in the same export)
409          * or the export options.
410          */
411  retry:
412         version = 1;
413         if (ref_fh && ref_fh->fh_export == exp) {
414                 version = ref_fh->fh_handle.fh_version;
415                 fsid_type = ref_fh->fh_handle.fh_fsid_type;
416
417                 if (ref_fh == fhp)
418                         fh_put(ref_fh);
419                 ref_fh = NULL;
420
421                 switch (version) {
422                 case 0xca:
423                         fsid_type = FSID_DEV;
424                         break;
425                 case 1:
426                         break;
427                 default:
428                         goto retry;
429                 }
430
431                 /* Need to check that this type works for this
432                  * export point.  As the fsid -> filesystem mapping
433                  * was guided by user-space, there is no guarantee
434                  * that the filesystem actually supports that fsid
435                  * type. If it doesn't we loop around again without
436                  * ref_fh set.
437                  */
438                 switch(fsid_type) {
439                 case FSID_DEV:
440                         if (!old_valid_dev(ex_dev))
441                                 goto retry;
442                         /* FALL THROUGH */
443                 case FSID_MAJOR_MINOR:
444                 case FSID_ENCODE_DEV:
445                         if (!(exp->ex_path.dentry->d_inode->i_sb->s_type->fs_flags
446                               & FS_REQUIRES_DEV))
447                                 goto retry;
448                         break;
449                 case FSID_NUM:
450                         if (! (exp->ex_flags & NFSEXP_FSID))
451                                 goto retry;
452                         break;
453                 case FSID_UUID8:
454                 case FSID_UUID16:
455                         if (!root_export)
456                                 goto retry;
457                         /* fall through */
458                 case FSID_UUID4_INUM:
459                 case FSID_UUID16_INUM:
460                         if (exp->ex_uuid == NULL)
461                                 goto retry;
462                         break;
463                 }
464         } else if (exp->ex_uuid) {
465                 if (fhp->fh_maxsize >= 64) {
466                         if (root_export)
467                                 fsid_type = FSID_UUID16;
468                         else
469                                 fsid_type = FSID_UUID16_INUM;
470                 } else {
471                         if (root_export)
472                                 fsid_type = FSID_UUID8;
473                         else
474                                 fsid_type = FSID_UUID4_INUM;
475                 }
476         } else if (exp->ex_flags & NFSEXP_FSID)
477                 fsid_type = FSID_NUM;
478         else if (!old_valid_dev(ex_dev))
479                 /* for newer device numbers, we must use a newer fsid format */
480                 fsid_type = FSID_ENCODE_DEV;
481         else
482                 fsid_type = FSID_DEV;
483
484         if (ref_fh == fhp)
485                 fh_put(ref_fh);
486
487         if (fhp->fh_locked || fhp->fh_dentry) {
488                 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
489                        parent->d_name.name, dentry->d_name.name);
490         }
491         if (fhp->fh_maxsize < NFS_FHSIZE)
492                 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
493                        fhp->fh_maxsize,
494                        parent->d_name.name, dentry->d_name.name);
495
496         fhp->fh_dentry = dget(dentry); /* our internal copy */
497         fhp->fh_export = exp;
498         cache_get(&exp->h);
499
500         if (version == 0xca) {
501                 /* old style filehandle please */
502                 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
503                 fhp->fh_handle.fh_size = NFS_FHSIZE;
504                 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
505                 fhp->fh_handle.ofh_dev =  old_encode_dev(ex_dev);
506                 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
507                 fhp->fh_handle.ofh_xino =
508                         ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino);
509                 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
510                 if (inode)
511                         _fh_update_old(dentry, exp, &fhp->fh_handle);
512         } else {
513                 int len;
514                 fhp->fh_handle.fh_version = 1;
515                 fhp->fh_handle.fh_auth_type = 0;
516                 datap = fhp->fh_handle.fh_auth+0;
517                 fhp->fh_handle.fh_fsid_type = fsid_type;
518                 mk_fsid(fsid_type, datap, ex_dev,
519                         exp->ex_path.dentry->d_inode->i_ino,
520                         exp->ex_fsid, exp->ex_uuid);
521
522                 len = key_len(fsid_type);
523                 datap += len/4;
524                 fhp->fh_handle.fh_size = 4 + len;
525
526                 if (inode)
527                         _fh_update(fhp, exp, dentry);
528                 if (fhp->fh_handle.fh_fileid_type == 255)
529                         return nfserr_opnotsupp;
530         }
531
532         nfsd_nr_verified++;
533         return 0;
534 }
535
536 /*
537  * Update file handle information after changing a dentry.
538  * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
539  */
540 __be32
541 fh_update(struct svc_fh *fhp)
542 {
543         struct dentry *dentry;
544
545         if (!fhp->fh_dentry)
546                 goto out_bad;
547
548         dentry = fhp->fh_dentry;
549         if (!dentry->d_inode)
550                 goto out_negative;
551         if (fhp->fh_handle.fh_version != 1) {
552                 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
553         } else {
554                 if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
555                         goto out;
556
557                 _fh_update(fhp, fhp->fh_export, dentry);
558                 if (fhp->fh_handle.fh_fileid_type == 255)
559                         return nfserr_opnotsupp;
560         }
561 out:
562         return 0;
563
564 out_bad:
565         printk(KERN_ERR "fh_update: fh not verified!\n");
566         goto out;
567 out_negative:
568         printk(KERN_ERR "fh_update: %s/%s still negative!\n",
569                 dentry->d_parent->d_name.name, dentry->d_name.name);
570         goto out;
571 }
572
573 /*
574  * Release a file handle.
575  */
576 void
577 fh_put(struct svc_fh *fhp)
578 {
579         struct dentry * dentry = fhp->fh_dentry;
580         struct svc_export * exp = fhp->fh_export;
581         if (dentry) {
582                 fh_unlock(fhp);
583                 fhp->fh_dentry = NULL;
584                 dput(dentry);
585 #ifdef CONFIG_NFSD_V3
586                 fhp->fh_pre_saved = 0;
587                 fhp->fh_post_saved = 0;
588 #endif
589                 nfsd_nr_put++;
590         }
591         if (exp) {
592                 cache_put(&exp->h, &svc_export_cache);
593                 fhp->fh_export = NULL;
594         }
595         return;
596 }
597
598 /*
599  * Shorthand for dprintk()'s
600  */
601 char * SVCFH_fmt(struct svc_fh *fhp)
602 {
603         struct knfsd_fh *fh = &fhp->fh_handle;
604
605         static char buf[80];
606         sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
607                 fh->fh_size,
608                 fh->fh_base.fh_pad[0],
609                 fh->fh_base.fh_pad[1],
610                 fh->fh_base.fh_pad[2],
611                 fh->fh_base.fh_pad[3],
612                 fh->fh_base.fh_pad[4],
613                 fh->fh_base.fh_pad[5]);
614         return buf;
615 }
616
617 enum fsid_source fsid_source(struct svc_fh *fhp)
618 {
619         if (fhp->fh_handle.fh_version != 1)
620                 return FSIDSOURCE_DEV;
621         switch(fhp->fh_handle.fh_fsid_type) {
622         case FSID_DEV:
623         case FSID_ENCODE_DEV:
624         case FSID_MAJOR_MINOR:
625                 if (fhp->fh_export->ex_path.dentry->d_inode->i_sb->s_type->fs_flags
626                     & FS_REQUIRES_DEV)
627                         return FSIDSOURCE_DEV;
628                 break;
629         case FSID_NUM:
630                 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
631                         return FSIDSOURCE_FSID;
632                 break;
633         default:
634                 break;
635         }
636         /* either a UUID type filehandle, or the filehandle doesn't
637          * match the export.
638          */
639         if (fhp->fh_export->ex_flags & NFSEXP_FSID)
640                 return FSIDSOURCE_FSID;
641         if (fhp->fh_export->ex_uuid)
642                 return FSIDSOURCE_UUID;
643         return FSIDSOURCE_DEV;
644 }