This commit was manufactured by cvs2svn to create tag
[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                         dprintk("nfsd: fh_verify failed: nfserr_dropit\n");
147                         goto out;
148                 }
149                 error = nfserr_stale; 
150                 if (!exp || IS_ERR(exp)) {
151                         dprintk("nfsd: fh_verify failed: nfserr_stale\n");
152                         goto out;
153                 }
154                 /* Check if the request originated from a secure port. */
155                 error = nfserr_perm;
156                 if (!rqstp->rq_secure && EX_SECURE(exp)) {
157                         printk(KERN_WARNING
158                                "nfsd: request from insecure port (%u.%u.%u.%u:%d)!\n",
159                                NIPQUAD(rqstp->rq_addr.sin_addr.s_addr),
160                                ntohs(rqstp->rq_addr.sin_port));
161                         goto out;
162                 }
163
164                 /* Set user creds for this exportpoint */
165                 error = nfsd_setuser(rqstp, exp);
166                 if (error) {
167                         dprintk("nfsd: nfsd_setuser failed: %d\n", error);
168                         error = nfserrno(error);
169                         goto out;
170                 }
171
172                 /*
173                  * Look up the dentry using the NFS file handle.
174                  */
175                 error = nfserr_stale;
176                 if (rqstp->rq_vers > 2)
177                         error = nfserr_badhandle;
178
179                 if (fh->fh_version != 1) {
180                         tfh[0] = fh->ofh_ino;
181                         tfh[1] = fh->ofh_generation;
182                         tfh[2] = fh->ofh_dirino;
183                         datap = tfh;
184                         data_left = 3;
185                         if (fh->ofh_dirino == 0)
186                                 fileid_type = 1;
187                         else
188                                 fileid_type = 2;
189                 } else
190                         fileid_type = fh->fh_fileid_type;
191                 
192                 if (fileid_type == 0)
193                         dentry = dget(exp->ex_dentry);
194                 else {
195                         struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
196                         dentry = CALL(nop,decode_fh)(exp->ex_mnt->mnt_sb,
197                                                      datap, data_left,
198                                                      fileid_type,
199                                                      nfsd_acceptable, exp);
200                 }
201                 if (dentry == NULL)
202                         goto out;
203                 if (IS_ERR(dentry)) {
204                         dprintk("nfsd: CALL(nop,decode_fh) failed: %ld\n", PTR_ERR(dentry));
205                         if (PTR_ERR(dentry) != -EINVAL)
206                                 error = nfserrno(PTR_ERR(dentry));
207                         goto out;
208                 }
209 #ifdef NFSD_PARANOIA
210                 if (S_ISDIR(dentry->d_inode->i_mode) &&
211                     (dentry->d_flags & DCACHE_DISCONNECTED)) {
212                         printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
213                                dentry->d_parent->d_name.name, dentry->d_name.name);
214                 }
215 #endif
216
217                 fhp->fh_dentry = dentry;
218                 fhp->fh_export = exp;
219                 nfsd_nr_verified++;
220         } else {
221                 /* just rechecking permissions
222                  * (e.g. nfsproc_create calls fh_verify, then nfsd_create does as well)
223                  */
224                 dprintk("nfsd: fh_verify - just checking\n");
225                 dentry = fhp->fh_dentry;
226                 exp = fhp->fh_export;
227         }
228         cache_get(&exp->h);
229
230         inode = dentry->d_inode;
231
232
233         /* Type check. The correct error return for type mismatches
234          * does not seem to be generally agreed upon. SunOS seems to
235          * use EISDIR if file isn't S_IFREG; a comment in the NFSv3
236          * spec says this is incorrect (implementation notes for the
237          * write call).
238          */
239
240         /* Type can be negative when creating hardlinks - not to a dir */
241         if (type > 0 && (inode->i_mode & S_IFMT) != type) {
242                 if (rqstp->rq_vers == 4 && (inode->i_mode & S_IFMT) == S_IFLNK)
243                         error = nfserr_symlink;
244                 else if (type == S_IFDIR)
245                         error = nfserr_notdir;
246                 else if ((inode->i_mode & S_IFMT) == S_IFDIR)
247                         error = nfserr_isdir;
248                 else
249                         error = nfserr_inval;
250                 dprintk("nfsd: bad type: %d\n", ntohl(error));
251                 goto out;
252         }
253         if (type < 0 && (inode->i_mode & S_IFMT) == -type) {
254                 if (rqstp->rq_vers == 4 && (inode->i_mode & S_IFMT) == S_IFLNK)
255                         error = nfserr_symlink;
256                 else if (type == -S_IFDIR)
257                         error = nfserr_isdir;
258                 else
259                         error = nfserr_notdir;
260                 dprintk("nfsd: bad type2: %d\n", ntohl(error));
261                 goto out;
262         }
263
264         /* Finally, check access permissions. */
265         error = nfsd_permission(exp, dentry, access);
266
267 #ifdef NFSD_PARANOIA_EXTREME
268         if (error) {
269                 printk("fh_verify: %s/%s permission failure, acc=%x, error=%d\n",
270                        dentry->d_parent->d_name.name, dentry->d_name.name, access, (error >> 24));
271         }
272 #endif
273 out:
274         if (exp && !IS_ERR(exp))
275                 exp_put(exp);
276         if (error == nfserr_stale)
277                 nfsdstats.fh_stale++;
278         return error;
279 }
280
281
282 /*
283  * Compose a file handle for an NFS reply.
284  *
285  * Note that when first composed, the dentry may not yet have
286  * an inode.  In this case a call to fh_update should be made
287  * before the fh goes out on the wire ...
288  */
289 inline int _fh_update(struct dentry *dentry, struct svc_export *exp,
290                       __u32 *datap, int *maxsize)
291 {
292         struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
293         
294         if (dentry == exp->ex_dentry) {
295                 *maxsize = 0;
296                 return 0;
297         }
298
299         return CALL(nop,encode_fh)(dentry, datap, maxsize,
300                           !(exp->ex_flags&NFSEXP_NOSUBTREECHECK));
301 }
302
303 /*
304  * for composing old style file handles
305  */
306 inline void _fh_update_old(struct dentry *dentry, struct svc_export *exp,
307                            struct knfsd_fh *fh)
308 {
309         fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
310         fh->ofh_generation = dentry->d_inode->i_generation;
311         if (S_ISDIR(dentry->d_inode->i_mode) ||
312             (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
313                 fh->ofh_dirino = 0;
314 }
315
316 int
317 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry, struct svc_fh *ref_fh)
318 {
319         /* ref_fh is a reference file handle.
320          * if it is non-null, then we should compose a filehandle which is
321          * of the same version, where possible.
322          * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
323          * Then create a 32byte filehandle using nfs_fhbase_old
324          *
325          */
326
327         u8 ref_fh_version = 0;
328         u8 ref_fh_fsid_type = 0;
329         struct inode * inode = dentry->d_inode;
330         struct dentry *parent = dentry->d_parent;
331         __u32 *datap;
332         dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev;
333
334         dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
335                 MAJOR(ex_dev), MINOR(ex_dev),
336                 (long) exp->ex_dentry->d_inode->i_ino,
337                 parent->d_name.name, dentry->d_name.name,
338                 (inode ? inode->i_ino : 0));
339
340         if (ref_fh) {
341                 ref_fh_version = ref_fh->fh_handle.fh_version;
342                 if (ref_fh_version == 0xca)
343                         ref_fh_fsid_type = 0;
344                 else
345                         ref_fh_fsid_type = ref_fh->fh_handle.fh_fsid_type;
346                 if (ref_fh_fsid_type > 3)
347                         ref_fh_fsid_type = 0;
348
349                 /* make sure ref_fh type works for given export */
350                 if (ref_fh_fsid_type == 1 &&
351                     !(exp->ex_flags & NFSEXP_FSID)) {
352                         /* if we don't have an fsid, we cannot provide one... */
353                         ref_fh_fsid_type = 0;
354                 }
355         } else if (exp->ex_flags & NFSEXP_FSID)
356                 ref_fh_fsid_type = 1;
357
358         if (!old_valid_dev(ex_dev) && ref_fh_fsid_type == 0) {
359                 /* for newer device numbers, we must use a newer fsid format */
360                 ref_fh_version = 1;
361                 ref_fh_fsid_type = 3;
362         }
363         if (old_valid_dev(ex_dev) &&
364             (ref_fh_fsid_type == 2 || ref_fh_fsid_type == 3))
365                 /* must use type1 for smaller device numbers */
366                 ref_fh_fsid_type = 0;
367
368         if (ref_fh == fhp)
369                 fh_put(ref_fh);
370
371         if (fhp->fh_locked || fhp->fh_dentry) {
372                 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
373                         parent->d_name.name, dentry->d_name.name);
374         }
375         if (fhp->fh_maxsize < NFS_FHSIZE)
376                 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
377                        fhp->fh_maxsize, parent->d_name.name, dentry->d_name.name);
378
379         fhp->fh_dentry = dget(dentry); /* our internal copy */
380         fhp->fh_export = exp;
381         cache_get(&exp->h);
382
383         if (ref_fh_version == 0xca) {
384                 /* old style filehandle please */
385                 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
386                 fhp->fh_handle.fh_size = NFS_FHSIZE;
387                 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
388                 fhp->fh_handle.ofh_dev =  old_encode_dev(ex_dev);
389                 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
390                 fhp->fh_handle.ofh_xino = ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
391                 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
392                 if (inode)
393                         _fh_update_old(dentry, exp, &fhp->fh_handle);
394         } else {
395                 int len;
396                 fhp->fh_handle.fh_version = 1;
397                 fhp->fh_handle.fh_auth_type = 0;
398                 datap = fhp->fh_handle.fh_auth+0;
399                 fhp->fh_handle.fh_fsid_type = ref_fh_fsid_type;
400                 switch (ref_fh_fsid_type) {
401                         case 0:
402                                 /*
403                                  * fsid_type 0:
404                                  * 2byte major, 2byte minor, 4byte inode
405                                  */
406                                 mk_fsid_v0(datap, ex_dev,
407                                         exp->ex_dentry->d_inode->i_ino);
408                                 break;
409                         case 1:
410                                 /* fsid_type 1 == 4 bytes filesystem id */
411                                 mk_fsid_v1(datap, exp->ex_fsid);
412                                 break;
413                         case 2:
414                                 /*
415                                  * fsid_type 2:
416                                  * 4byte major, 4byte minor, 4byte inode
417                                  */
418                                 mk_fsid_v2(datap, ex_dev,
419                                         exp->ex_dentry->d_inode->i_ino);
420                                 break;
421                         case 3:
422                                 /*
423                                  * fsid_type 3:
424                                  * 4byte devicenumber, 4byte inode
425                                  */
426                                 mk_fsid_v3(datap, ex_dev,
427                                         exp->ex_dentry->d_inode->i_ino);
428                                 break;
429                 }
430                 len = key_len(ref_fh_fsid_type);
431                 datap += len/4;
432                 fhp->fh_handle.fh_size = 4 + len;
433
434                 if (inode) {
435                         int size = (fhp->fh_maxsize-len-4)/4;
436                         fhp->fh_handle.fh_fileid_type =
437                                 _fh_update(dentry, exp, datap, &size);
438                         fhp->fh_handle.fh_size += size*4;
439                 }
440                 if (fhp->fh_handle.fh_fileid_type == 255)
441                         return nfserr_opnotsupp;
442         }
443
444         nfsd_nr_verified++;
445         return 0;
446 }
447
448 /*
449  * Update file handle information after changing a dentry.
450  * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
451  */
452 int
453 fh_update(struct svc_fh *fhp)
454 {
455         struct dentry *dentry;
456         __u32 *datap;
457         
458         if (!fhp->fh_dentry)
459                 goto out_bad;
460
461         dentry = fhp->fh_dentry;
462         if (!dentry->d_inode)
463                 goto out_negative;
464         if (fhp->fh_handle.fh_version != 1) {
465                 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
466         } else {
467                 int size;
468                 if (fhp->fh_handle.fh_fileid_type != 0)
469                         goto out_uptodate;
470                 datap = fhp->fh_handle.fh_auth+
471                         fhp->fh_handle.fh_size/4 -1;
472                 size = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
473                 fhp->fh_handle.fh_fileid_type =
474                         _fh_update(dentry, fhp->fh_export, datap, &size);
475                 fhp->fh_handle.fh_size += size*4;
476                 if (fhp->fh_handle.fh_fileid_type == 255)
477                         return nfserr_opnotsupp;
478         }
479 out:
480         return 0;
481
482 out_bad:
483         printk(KERN_ERR "fh_update: fh not verified!\n");
484         goto out;
485 out_negative:
486         printk(KERN_ERR "fh_update: %s/%s still negative!\n",
487                 dentry->d_parent->d_name.name, dentry->d_name.name);
488         goto out;
489 out_uptodate:
490         printk(KERN_ERR "fh_update: %s/%s already up-to-date!\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                 svc_export_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 }