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