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