Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / fs / nfsd / nfsfh.c
blob1c50df32422b829d01cd7d515adb19b7fa0e1843
1 /*
2 * linux/fs/nfsd/nfsfh.c
4 * NFS server file handle treatment.
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
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>
23 #include <linux/sunrpc/svc.h>
24 #include <linux/nfsd/nfsd.h>
26 #define NFSDDBG_FACILITY NFSDDBG_FH
27 #define NFSD_PARANOIA 1
28 /* #define NFSD_DEBUG_VERBOSE 1 */
31 static int nfsd_nr_verified;
32 static int nfsd_nr_put;
34 extern struct export_operations export_op_default;
36 #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
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
44 int nfsd_acceptable(void *expv, struct dentry *dentry)
46 struct svc_export *exp = expv;
47 int rv;
48 struct dentry *tdentry;
49 struct dentry *parent;
51 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
52 return 1;
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 read_lock(&dparent_lock);
59 parent = dget(tdentry->d_parent);
60 read_unlock(&dparent_lock);
61 err = permission(parent->d_inode, S_IXOTH);
62 if (err < 0) {
63 dput(parent);
64 break;
66 dput(tdentry);
67 tdentry = parent;
69 if (tdentry != exp->ex_dentry)
70 dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
71 rv = (tdentry == exp->ex_dentry);
72 dput(tdentry);
73 return rv;
78 * Perform sanity checks on the dentry in a client's file handle.
80 * Note that the file handle dentry may need to be freed even after
81 * an error return.
83 * This is only called at the start of an nfsproc call, so fhp points to
84 * a svc_fh which is all 0 except for the over-the-wire file handle.
86 u32
87 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
89 struct knfsd_fh *fh = &fhp->fh_handle;
90 struct svc_export *exp = NULL;
91 struct dentry *dentry;
92 struct inode *inode;
93 u32 error = 0;
95 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
97 /* keep this filehandle for possible reference when encoding attributes */
98 rqstp->rq_reffh = fh;
100 if (!fhp->fh_dentry) {
101 __u32 *datap=NULL;
102 __u32 tfh[3]; /* filehandle fragment for oldstyle filehandles */
103 int fileid_type;
104 int data_left = fh->fh_size/4;
106 error = nfserr_stale;
107 if (rqstp->rq_client == NULL)
108 goto out;
109 if (rqstp->rq_vers > 2)
110 error = nfserr_badhandle;
111 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
112 return nfserr_nofilehandle;
114 if (fh->fh_version == 1) {
115 int len;
116 datap = fh->fh_auth;
117 if (--data_left<0) goto out;
118 switch (fh->fh_auth_type) {
119 case 0: break;
120 default: goto out;
123 switch (fh->fh_fsid_type) {
124 case 0:
125 len = 2;
126 break;
127 case 1:
128 len = 1;
129 break;
130 default:
131 goto out;
133 if ((data_left -= len)<0) goto out;
134 exp = exp_find(rqstp->rq_client, fh->fh_fsid_type, datap, &rqstp->rq_chandle);
135 datap += len;
136 } else {
137 dev_t xdev;
138 ino_t xino;
139 if (fh->fh_size != NFS_FHSIZE)
140 goto out;
141 /* assume old filehandle format */
142 xdev = u32_to_dev_t(fh->ofh_xdev);
143 xino = u32_to_ino_t(fh->ofh_xino);
144 mk_fsid_v0(tfh, xdev, xino);
145 exp = exp_find(rqstp->rq_client, 0, tfh, &rqstp->rq_chandle);
148 error = nfserr_dropit;
149 if (IS_ERR(exp))
150 goto out;
152 error = nfserr_stale;
153 if (!exp)
154 goto out;
156 /* Check if the request originated from a secure port. */
157 error = nfserr_perm;
158 if (!rqstp->rq_secure && EX_SECURE(exp)) {
159 printk(KERN_WARNING
160 "nfsd: request from insecure port (%08x:%d)!\n",
161 ntohl(rqstp->rq_addr.sin_addr.s_addr),
162 ntohs(rqstp->rq_addr.sin_port));
163 goto out;
166 /* Set user creds if we haven't done so already. */
167 nfsd_setuser(rqstp, exp);
170 * Look up the dentry using the NFS file handle.
172 error = nfserr_stale;
173 if (rqstp->rq_vers > 2)
174 error = nfserr_badhandle;
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;
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);
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;
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);
211 #endif
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)
220 dprintk("nfsd: fh_verify - just checking\n");
221 dentry = fhp->fh_dentry;
222 exp = fhp->fh_export;
224 cache_get(&exp->h);
226 inode = dentry->d_inode;
228 /* Type check. The correct error return for type mismatches
229 * does not seem to be generally agreed upon. SunOS seems to
230 * use EISDIR if file isn't S_IFREG; a comment in the NFSv3
231 * spec says this is incorrect (implementation notes for the
232 * write call).
235 /* Type can be negative when creating hardlinks - not to a dir */
236 if (type > 0 && (inode->i_mode & S_IFMT) != type) {
237 if (rqstp->rq_vers == 4 && (inode->i_mode & S_IFMT) == S_IFLNK)
238 error = nfserr_symlink;
239 else if (type == S_IFDIR)
240 error = nfserr_notdir;
241 else if ((inode->i_mode & S_IFMT) == S_IFDIR)
242 error = nfserr_isdir;
243 else
244 error = nfserr_inval;
245 goto out;
247 if (type < 0 && (inode->i_mode & S_IFMT) == -type) {
248 if (rqstp->rq_vers == 4 && (inode->i_mode & S_IFMT) == S_IFLNK)
249 error = nfserr_symlink;
250 else if (type == -S_IFDIR)
251 error = nfserr_isdir;
252 else
253 error = nfserr_notdir;
254 goto out;
257 /* Finally, check access permissions. */
258 error = nfsd_permission(exp, dentry, access);
260 #ifdef NFSD_PARANOIA_EXTREME
261 if (error) {
262 printk("fh_verify: %s/%s permission failure, acc=%x, error=%d\n",
263 dentry->d_parent->d_name.name, dentry->d_name.name, access, (error >> 24));
265 #endif
266 out:
267 if (exp && !IS_ERR(exp))
268 exp_put(exp);
269 if (error == nfserr_stale)
270 nfsdstats.fh_stale++;
271 return error;
276 * Compose a file handle for an NFS reply.
278 * Note that when first composed, the dentry may not yet have
279 * an inode. In this case a call to fh_update should be made
280 * before the fh goes out on the wire ...
282 inline int _fh_update(struct dentry *dentry, struct svc_export *exp,
283 __u32 *datap, int *maxsize)
285 struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
287 if (dentry == exp->ex_dentry) {
288 *maxsize = 0;
289 return 0;
292 return CALL(nop,encode_fh)(dentry, datap, maxsize,
293 !(exp->ex_flags&NFSEXP_NOSUBTREECHECK));
297 * for composing old style file handles
299 inline void _fh_update_old(struct dentry *dentry, struct svc_export *exp,
300 struct knfsd_fh *fh)
302 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
303 fh->ofh_generation = dentry->d_inode->i_generation;
304 if (S_ISDIR(dentry->d_inode->i_mode) ||
305 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
306 fh->ofh_dirino = 0;
310 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry, struct svc_fh *ref_fh)
312 /* ref_fh is a reference file handle.
313 * if it is non-null, then we should compose a filehandle which is
314 * of the same version, where possible.
315 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
316 * Then create a 32byte filehandle using nfs_fhbase_old
320 u8 ref_fh_version = 0;
321 u8 ref_fh_fsid_type = 0;
322 struct inode * inode = dentry->d_inode;
323 struct dentry *parent = dentry->d_parent;
324 __u32 *datap;
325 dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev;
327 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
328 MAJOR(ex_dev), MINOR(ex_dev),
329 (long) exp->ex_dentry->d_inode->i_ino,
330 parent->d_name.name, dentry->d_name.name,
331 (inode ? inode->i_ino : 0));
333 if (ref_fh) {
334 ref_fh_version = ref_fh->fh_handle.fh_version;
335 ref_fh_fsid_type = ref_fh->fh_handle.fh_fsid_type;
336 if (ref_fh == fhp)
337 fh_put(ref_fh);
340 if (fhp->fh_locked || fhp->fh_dentry) {
341 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
342 parent->d_name.name, dentry->d_name.name);
344 if (fhp->fh_maxsize < NFS_FHSIZE)
345 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
346 fhp->fh_maxsize, parent->d_name.name, dentry->d_name.name);
348 fhp->fh_dentry = dentry; /* our internal copy */
349 fhp->fh_export = exp;
350 cache_get(&exp->h);
352 if (ref_fh_version == 0xca) {
353 /* old style filehandle please */
354 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
355 fhp->fh_handle.fh_size = NFS_FHSIZE;
356 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
357 fhp->fh_handle.ofh_dev = htonl((MAJOR(ex_dev)<<16)| MINOR(ex_dev));
358 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
359 fhp->fh_handle.ofh_xino = ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
360 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
361 if (inode)
362 _fh_update_old(dentry, exp, &fhp->fh_handle);
363 } else {
364 fhp->fh_handle.fh_version = 1;
365 fhp->fh_handle.fh_auth_type = 0;
366 datap = fhp->fh_handle.fh_auth+0;
367 if ((exp->ex_flags & NFSEXP_FSID) &&
368 (ref_fh_fsid_type == 1)) {
369 fhp->fh_handle.fh_fsid_type = 1;
370 /* fsid_type 1 == 4 bytes filesystem id */
371 mk_fsid_v1(datap, exp->ex_fsid);
372 datap += 1;
373 fhp->fh_handle.fh_size = 2*4;
374 } else {
375 fhp->fh_handle.fh_fsid_type = 0;
376 /* fsid_type 0 == 2byte major, 2byte minor, 4byte inode */
377 mk_fsid_v0(datap, ex_dev, exp->ex_dentry->d_inode->i_ino);
378 datap += 2;
379 fhp->fh_handle.fh_size = 3*4;
381 if (inode) {
382 int size = fhp->fh_maxsize/4 - 3;
383 fhp->fh_handle.fh_fileid_type =
384 _fh_update(dentry, exp, datap, &size);
385 fhp->fh_handle.fh_size += size*4;
387 if (fhp->fh_handle.fh_fileid_type == 255)
388 return nfserr_opnotsupp;
391 nfsd_nr_verified++;
392 return 0;
396 * Update file handle information after changing a dentry.
397 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
400 fh_update(struct svc_fh *fhp)
402 struct dentry *dentry;
403 __u32 *datap;
405 if (!fhp->fh_dentry)
406 goto out_bad;
408 dentry = fhp->fh_dentry;
409 if (!dentry->d_inode)
410 goto out_negative;
411 if (fhp->fh_handle.fh_version != 1) {
412 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
413 } else {
414 int size;
415 if (fhp->fh_handle.fh_fileid_type != 0)
416 goto out_uptodate;
417 datap = fhp->fh_handle.fh_auth+
418 fhp->fh_handle.fh_size/4 -1;
419 size = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
420 fhp->fh_handle.fh_fileid_type =
421 _fh_update(dentry, fhp->fh_export, datap, &size);
422 fhp->fh_handle.fh_size += size*4;
423 if (fhp->fh_handle.fh_fileid_type == 255)
424 return nfserr_opnotsupp;
426 out:
427 return 0;
429 out_bad:
430 printk(KERN_ERR "fh_update: fh not verified!\n");
431 goto out;
432 out_negative:
433 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
434 dentry->d_parent->d_name.name, dentry->d_name.name);
435 goto out;
436 out_uptodate:
437 printk(KERN_ERR "fh_update: %s/%s already up-to-date!\n",
438 dentry->d_parent->d_name.name, dentry->d_name.name);
439 goto out;
443 * Release a file handle.
445 void
446 fh_put(struct svc_fh *fhp)
448 struct dentry * dentry = fhp->fh_dentry;
449 struct svc_export * exp = fhp->fh_export;
450 if (dentry) {
451 fh_unlock(fhp);
452 fhp->fh_dentry = NULL;
453 dput(dentry);
454 #ifdef CONFIG_NFSD_V3
455 fhp->fh_pre_saved = 0;
456 fhp->fh_post_saved = 0;
457 #endif
458 nfsd_nr_put++;
460 if (exp) {
461 svc_export_put(&exp->h, &svc_export_cache);
462 fhp->fh_export = NULL;
464 return;