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