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