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