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