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