blob: 0130b345234dc1a5d29ae21017d8c2cb0a16e897 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/fs.h>
14#include <linux/unistd.h>
15#include <linux/string.h>
16#include <linux/stat.h>
17#include <linux/dcache.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070018#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Chuck Leverad06e4b2007-02-12 00:53:32 -080021#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/sunrpc/svc.h>
Andy Adamson32c1eb02007-07-17 04:04:48 -070023#include <linux/sunrpc/svcauth_gss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/nfsd/nfsd.h>
J. Bruce Fields2e8138a2007-11-15 17:05:43 -050025#include "auth.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define NFSDDBG_FACILITY NFSDDBG_FH
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29
30static int nfsd_nr_verified;
31static int nfsd_nr_put;
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * our acceptability function.
35 * if NOSUBTREECHECK, accept anything
36 * if not, require that we can walk up to exp->ex_dentry
37 * doing some checks on the 'x' bits
38 */
39static int nfsd_acceptable(void *expv, struct dentry *dentry)
40{
41 struct svc_export *exp = expv;
42 int rv;
43 struct dentry *tdentry;
44 struct dentry *parent;
45
46 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
47 return 1;
48
49 tdentry = dget(dentry);
Jan Blunck54775492008-02-14 19:38:39 -080050 while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 /* make sure parents give x permission to user */
52 int err;
53 parent = dget_parent(tdentry);
54 err = permission(parent->d_inode, MAY_EXEC, NULL);
55 if (err < 0) {
56 dput(parent);
57 break;
58 }
59 dput(tdentry);
60 tdentry = parent;
61 }
Jan Blunck54775492008-02-14 19:38:39 -080062 if (tdentry != exp->ex_path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
Jan Blunck54775492008-02-14 19:38:39 -080064 rv = (tdentry == exp->ex_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 dput(tdentry);
66 return rv;
67}
68
69/* Type check. The correct error return for type mismatches does not seem to be
70 * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
71 * comment in the NFSv3 spec says this is incorrect (implementation notes for
72 * the write call).
73 */
Al Viro83b11342006-10-19 23:28:55 -070074static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070075nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
76{
77 /* Type can be negative when creating hardlinks - not to a dir */
78 if (type > 0 && (mode & S_IFMT) != type) {
79 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
80 return nfserr_symlink;
81 else if (type == S_IFDIR)
82 return nfserr_notdir;
83 else if ((mode & S_IFMT) == S_IFDIR)
84 return nfserr_isdir;
85 else
86 return nfserr_inval;
87 }
88 if (type < 0 && (mode & S_IFMT) == -type) {
89 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
90 return nfserr_symlink;
91 else if (type == -S_IFDIR)
92 return nfserr_isdir;
93 else
94 return nfserr_notdir;
95 }
96 return 0;
97}
98
J. Bruce Fields6fa02832007-11-12 16:05:03 -050099static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
100 struct svc_export *exp)
101{
102 /* Check if the request originated from a secure port. */
103 if (!rqstp->rq_secure && EX_SECURE(exp)) {
104 char buf[RPC_MAX_ADDRBUFLEN];
105 dprintk(KERN_WARNING
106 "nfsd: request from insecure port %s!\n",
107 svc_print_addr(rqstp, buf, sizeof(buf)));
108 return nfserr_perm;
109 }
110
111 /* Set user creds for this exportpoint */
112 return nfserrno(nfsd_setuser(rqstp, exp));
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/*
116 * Perform sanity checks on the dentry in a client's file handle.
117 *
118 * Note that the file handle dentry may need to be freed even after
119 * an error return.
120 *
121 * This is only called at the start of an nfsproc call, so fhp points to
122 * a svc_fh which is all 0 except for the over-the-wire file handle.
123 */
Al Viro83b11342006-10-19 23:28:55 -0700124__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
126{
127 struct knfsd_fh *fh = &fhp->fh_handle;
128 struct svc_export *exp = NULL;
129 struct dentry *dentry;
Al Viro83b11342006-10-19 23:28:55 -0700130 __be32 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (!fhp->fh_dentry) {
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700135 struct fid *fid = NULL, sfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int fileid_type;
137 int data_left = fh->fh_size/4;
138
139 error = nfserr_stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (rqstp->rq_vers > 2)
141 error = nfserr_badhandle;
142 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
143 return nfserr_nofilehandle;
144
145 if (fh->fh_version == 1) {
146 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (--data_left<0) goto out;
148 switch (fh->fh_auth_type) {
149 case 0: break;
150 default: goto out;
151 }
152 len = key_len(fh->fh_fsid_type) / 4;
153 if (len == 0) goto out;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800154 if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* deprecated, convert to type 3 */
NeilBrownaf6a4e22007-02-14 00:33:12 -0800156 len = key_len(FSID_ENCODE_DEV)/4;
157 fh->fh_fsid_type = FSID_ENCODE_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
159 fh->fh_fsid[1] = fh->fh_fsid[2];
160 }
161 if ((data_left -= len)<0) goto out;
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700162 exp = rqst_exp_find(rqstp, fh->fh_fsid_type,
163 fh->fh_auth);
164 fid = (struct fid *)(fh->fh_auth + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 } else {
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700166 __u32 tfh[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 dev_t xdev;
168 ino_t xino;
169 if (fh->fh_size != NFS_FHSIZE)
170 goto out;
171 /* assume old filehandle format */
172 xdev = old_decode_dev(fh->ofh_xdev);
173 xino = u32_to_ino_t(fh->ofh_xino);
NeilBrownaf6a4e22007-02-14 00:33:12 -0800174 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
J. Bruce Fields0989a782007-07-17 04:04:44 -0700175 exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
J. Bruce Fields2d3bb252007-07-17 04:04:40 -0700178 error = nfserr_stale;
179 if (PTR_ERR(exp) == -ENOENT)
180 goto out;
181
182 if (IS_ERR(exp)) {
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800183 error = nfserrno(PTR_ERR(exp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto out;
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
J. Bruce Fields6fa02832007-11-12 16:05:03 -0500187 error = nfsd_setuser_and_check_port(rqstp, exp);
NeilBrownd1bbf142006-07-30 03:03:16 -0700188 if (error)
189 goto out;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /*
192 * Look up the dentry using the NFS file handle.
193 */
194 error = nfserr_stale;
195 if (rqstp->rq_vers > 2)
196 error = nfserr_badhandle;
197
198 if (fh->fh_version != 1) {
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700199 sfid.i32.ino = fh->ofh_ino;
200 sfid.i32.gen = fh->ofh_generation;
201 sfid.i32.parent_ino = fh->ofh_dirino;
202 fid = &sfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 data_left = 3;
204 if (fh->ofh_dirino == 0)
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700205 fileid_type = FILEID_INO32_GEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 else
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700207 fileid_type = FILEID_INO32_GEN_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 } else
209 fileid_type = fh->fh_fileid_type;
NeilBrown982aedf2007-02-14 00:33:11 -0800210
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700211 if (fileid_type == FILEID_ROOT)
Jan Blunck54775492008-02-14 19:38:39 -0800212 dentry = dget(exp->ex_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 else {
Jan Blunck54775492008-02-14 19:38:39 -0800214 dentry = exportfs_decode_fh(exp->ex_path.mnt, fid,
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700215 data_left, fileid_type,
216 nfsd_acceptable, exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 if (dentry == NULL)
219 goto out;
220 if (IS_ERR(dentry)) {
221 if (PTR_ERR(dentry) != -EINVAL)
222 error = nfserrno(PTR_ERR(dentry));
223 goto out;
224 }
NeilBrown34e9a632007-01-29 13:19:52 -0800225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (S_ISDIR(dentry->d_inode->i_mode) &&
227 (dentry->d_flags & DCACHE_DISCONNECTED)) {
228 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
229 dentry->d_parent->d_name.name, dentry->d_name.name);
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 fhp->fh_dentry = dentry;
233 fhp->fh_export = exp;
234 nfsd_nr_verified++;
235 } else {
J. Bruce Fields6fa02832007-11-12 16:05:03 -0500236 /*
237 * just rechecking permissions
238 * (e.g. nfsproc_create calls fh_verify, then nfsd_create
239 * does as well)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241 dprintk("nfsd: fh_verify - just checking\n");
242 dentry = fhp->fh_dentry;
243 exp = fhp->fh_export;
J. Bruce Fields6fa02832007-11-12 16:05:03 -0500244 /*
245 * Set user creds for this exportpoint; necessary even
NeilBrownd1bbf142006-07-30 03:03:16 -0700246 * in the "just checking" case because this may be a
247 * filehandle that was created by fh_compose, and that
248 * is about to be used in another nfsv4 compound
J. Bruce Fields6fa02832007-11-12 16:05:03 -0500249 * operation.
250 */
251 error = nfsd_setuser_and_check_port(rqstp, exp);
NeilBrownd1bbf142006-07-30 03:03:16 -0700252 if (error)
253 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255 cache_get(&exp->h);
256
J. Bruce Fields7fc90ec2006-06-30 01:56:14 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
259 if (error)
260 goto out;
261
J. Bruce Fields90912242007-07-17 04:04:52 -0700262 if (!(access & MAY_LOCK)) {
263 /*
264 * pseudoflavor restrictions are not enforced on NLM,
265 * which clients virtually always use auth_sys for,
266 * even while using RPCSEC_GSS for NFS.
267 */
268 error = check_nfsd_access(exp, rqstp);
269 if (error)
270 goto out;
271 }
Andy Adamson32c1eb02007-07-17 04:04:48 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Finally, check access permissions. */
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700274 error = nfsd_permission(rqstp, exp, dentry, access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (error) {
NeilBrown34e9a632007-01-29 13:19:52 -0800277 dprintk("fh_verify: %s/%s permission failure, "
278 "acc=%x, error=%d\n",
279 dentry->d_parent->d_name.name,
280 dentry->d_name.name,
Al Virofc2dd2e2007-02-01 13:52:43 +0000281 access, ntohl(error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283out:
284 if (exp && !IS_ERR(exp))
285 exp_put(exp);
286 if (error == nfserr_stale)
287 nfsdstats.fh_stale++;
288 return error;
289}
290
291
292/*
293 * Compose a file handle for an NFS reply.
294 *
295 * Note that when first composed, the dentry may not yet have
296 * an inode. In this case a call to fh_update should be made
297 * before the fh goes out on the wire ...
298 */
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700299static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
300 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Jan Blunck54775492008-02-14 19:38:39 -0800302 if (dentry != exp->ex_path.dentry) {
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700303 struct fid *fid = (struct fid *)
304 (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1);
305 int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
306 int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700308 fhp->fh_handle.fh_fileid_type =
309 exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
310 fhp->fh_handle.fh_size += maxsize * 4;
311 } else {
312 fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316/*
317 * for composing old style file handles
318 */
319static inline void _fh_update_old(struct dentry *dentry,
320 struct svc_export *exp,
321 struct knfsd_fh *fh)
322{
323 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
324 fh->ofh_generation = dentry->d_inode->i_generation;
325 if (S_ISDIR(dentry->d_inode->i_mode) ||
326 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
327 fh->ofh_dirino = 0;
328}
329
Al Viro83b11342006-10-19 23:28:55 -0700330__be32
NeilBrown982aedf2007-02-14 00:33:11 -0800331fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
332 struct svc_fh *ref_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 /* ref_fh is a reference file handle.
NeilBrown7e405362006-06-30 01:56:12 -0700335 * if it is non-null and for the same filesystem, then we should compose
336 * a filehandle which is of the same version, where possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
338 * Then create a 32byte filehandle using nfs_fhbase_old
339 *
340 */
341
NeilBrownb41eeef2007-05-09 02:34:57 -0700342 u8 version;
NeilBrown982aedf2007-02-14 00:33:11 -0800343 u8 fsid_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct inode * inode = dentry->d_inode;
345 struct dentry *parent = dentry->d_parent;
346 __u32 *datap;
Jan Blunck54775492008-02-14 19:38:39 -0800347 dev_t ex_dev = exp->ex_path.dentry->d_inode->i_sb->s_dev;
348 int root_export = (exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
351 MAJOR(ex_dev), MINOR(ex_dev),
Jan Blunck54775492008-02-14 19:38:39 -0800352 (long) exp->ex_path.dentry->d_inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 parent->d_name.name, dentry->d_name.name,
354 (inode ? inode->i_ino : 0));
355
NeilBrown982aedf2007-02-14 00:33:11 -0800356 /* Choose filehandle version and fsid type based on
357 * the reference filehandle (if it is in the same export)
358 * or the export options.
359 */
NeilBrownb41eeef2007-05-09 02:34:57 -0700360 retry:
361 version = 1;
NeilBrown7e405362006-06-30 01:56:12 -0700362 if (ref_fh && ref_fh->fh_export == exp) {
NeilBrown982aedf2007-02-14 00:33:11 -0800363 version = ref_fh->fh_handle.fh_version;
NeilBrownb41eeef2007-05-09 02:34:57 -0700364 fsid_type = ref_fh->fh_handle.fh_fsid_type;
365
366 if (ref_fh == fhp)
367 fh_put(ref_fh);
368 ref_fh = NULL;
369
370 switch (version) {
371 case 0xca:
NeilBrownaf6a4e22007-02-14 00:33:12 -0800372 fsid_type = FSID_DEV;
NeilBrownb41eeef2007-05-09 02:34:57 -0700373 break;
374 case 1:
375 break;
376 default:
377 goto retry;
378 }
379
380 /* Need to check that this type works for this
381 * export point. As the fsid -> filesystem mapping
382 * was guided by user-space, there is no guarantee
383 * that the filesystem actually supports that fsid
384 * type. If it doesn't we loop around again without
385 * ref_fh set.
NeilBrown982aedf2007-02-14 00:33:11 -0800386 */
NeilBrownb41eeef2007-05-09 02:34:57 -0700387 switch(fsid_type) {
388 case FSID_DEV:
389 if (!old_valid_dev(ex_dev))
390 goto retry;
391 /* FALL THROUGH */
392 case FSID_MAJOR_MINOR:
393 case FSID_ENCODE_DEV:
Jan Blunck54775492008-02-14 19:38:39 -0800394 if (!(exp->ex_path.dentry->d_inode->i_sb->s_type->fs_flags
NeilBrownb41eeef2007-05-09 02:34:57 -0700395 & FS_REQUIRES_DEV))
396 goto retry;
397 break;
398 case FSID_NUM:
399 if (! (exp->ex_flags & NFSEXP_FSID))
400 goto retry;
401 break;
402 case FSID_UUID8:
403 case FSID_UUID16:
404 if (!root_export)
405 goto retry;
406 /* fall through */
407 case FSID_UUID4_INUM:
408 case FSID_UUID16_INUM:
409 if (exp->ex_uuid == NULL)
410 goto retry;
411 break;
412 }
NeilBrownaf6a4e22007-02-14 00:33:12 -0800413 } else if (exp->ex_uuid) {
414 if (fhp->fh_maxsize >= 64) {
415 if (root_export)
416 fsid_type = FSID_UUID16;
417 else
418 fsid_type = FSID_UUID16_INUM;
419 } else {
420 if (root_export)
421 fsid_type = FSID_UUID8;
422 else
423 fsid_type = FSID_UUID4_INUM;
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 } else if (exp->ex_flags & NFSEXP_FSID)
NeilBrownaf6a4e22007-02-14 00:33:12 -0800426 fsid_type = FSID_NUM;
NeilBrown982aedf2007-02-14 00:33:11 -0800427 else if (!old_valid_dev(ex_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* for newer device numbers, we must use a newer fsid format */
NeilBrownaf6a4e22007-02-14 00:33:12 -0800429 fsid_type = FSID_ENCODE_DEV;
NeilBrown982aedf2007-02-14 00:33:11 -0800430 else
NeilBrownaf6a4e22007-02-14 00:33:12 -0800431 fsid_type = FSID_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (ref_fh == fhp)
434 fh_put(ref_fh);
435
436 if (fhp->fh_locked || fhp->fh_dentry) {
437 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
NeilBrown982aedf2007-02-14 00:33:11 -0800438 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 if (fhp->fh_maxsize < NFS_FHSIZE)
441 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
NeilBrown982aedf2007-02-14 00:33:11 -0800442 fhp->fh_maxsize,
443 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 fhp->fh_dentry = dget(dentry); /* our internal copy */
446 fhp->fh_export = exp;
447 cache_get(&exp->h);
448
NeilBrown982aedf2007-02-14 00:33:11 -0800449 if (version == 0xca) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* old style filehandle please */
451 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
452 fhp->fh_handle.fh_size = NFS_FHSIZE;
453 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
454 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
455 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
NeilBrown982aedf2007-02-14 00:33:11 -0800456 fhp->fh_handle.ofh_xino =
Jan Blunck54775492008-02-14 19:38:39 -0800457 ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
459 if (inode)
460 _fh_update_old(dentry, exp, &fhp->fh_handle);
461 } else {
462 int len;
463 fhp->fh_handle.fh_version = 1;
464 fhp->fh_handle.fh_auth_type = 0;
465 datap = fhp->fh_handle.fh_auth+0;
NeilBrown982aedf2007-02-14 00:33:11 -0800466 fhp->fh_handle.fh_fsid_type = fsid_type;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800467 mk_fsid(fsid_type, datap, ex_dev,
Jan Blunck54775492008-02-14 19:38:39 -0800468 exp->ex_path.dentry->d_inode->i_ino,
NeilBrownaf6a4e22007-02-14 00:33:12 -0800469 exp->ex_fsid, exp->ex_uuid);
470
NeilBrown982aedf2007-02-14 00:33:11 -0800471 len = key_len(fsid_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 datap += len/4;
473 fhp->fh_handle.fh_size = 4 + len;
474
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700475 if (inode)
476 _fh_update(fhp, exp, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (fhp->fh_handle.fh_fileid_type == 255)
478 return nfserr_opnotsupp;
479 }
480
481 nfsd_nr_verified++;
482 return 0;
483}
484
485/*
486 * Update file handle information after changing a dentry.
487 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
488 */
Al Viro83b11342006-10-19 23:28:55 -0700489__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490fh_update(struct svc_fh *fhp)
491{
492 struct dentry *dentry;
NeilBrown982aedf2007-02-14 00:33:11 -0800493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!fhp->fh_dentry)
495 goto out_bad;
496
497 dentry = fhp->fh_dentry;
498 if (!dentry->d_inode)
499 goto out_negative;
500 if (fhp->fh_handle.fh_version != 1) {
501 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
502 } else {
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700503 if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
NeilBrown4c9608b2006-06-30 01:56:11 -0700504 goto out;
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700505
506 _fh_update(fhp, fhp->fh_export, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (fhp->fh_handle.fh_fileid_type == 255)
508 return nfserr_opnotsupp;
509 }
510out:
511 return 0;
512
513out_bad:
514 printk(KERN_ERR "fh_update: fh not verified!\n");
515 goto out;
516out_negative:
517 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
518 dentry->d_parent->d_name.name, dentry->d_name.name);
519 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522/*
523 * Release a file handle.
524 */
525void
526fh_put(struct svc_fh *fhp)
527{
528 struct dentry * dentry = fhp->fh_dentry;
529 struct svc_export * exp = fhp->fh_export;
530 if (dentry) {
531 fh_unlock(fhp);
532 fhp->fh_dentry = NULL;
533 dput(dentry);
534#ifdef CONFIG_NFSD_V3
535 fhp->fh_pre_saved = 0;
536 fhp->fh_post_saved = 0;
537#endif
538 nfsd_nr_put++;
539 }
540 if (exp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800541 cache_put(&exp->h, &svc_export_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 fhp->fh_export = NULL;
543 }
544 return;
545}
546
547/*
548 * Shorthand for dprintk()'s
549 */
550char * SVCFH_fmt(struct svc_fh *fhp)
551{
552 struct knfsd_fh *fh = &fhp->fh_handle;
553
554 static char buf[80];
555 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
556 fh->fh_size,
557 fh->fh_base.fh_pad[0],
558 fh->fh_base.fh_pad[1],
559 fh->fh_base.fh_pad[2],
560 fh->fh_base.fh_pad[3],
561 fh->fh_base.fh_pad[4],
562 fh->fh_base.fh_pad[5]);
563 return buf;
564}
NeilBrownaf6a4e22007-02-14 00:33:12 -0800565
566enum fsid_source fsid_source(struct svc_fh *fhp)
567{
568 if (fhp->fh_handle.fh_version != 1)
569 return FSIDSOURCE_DEV;
570 switch(fhp->fh_handle.fh_fsid_type) {
571 case FSID_DEV:
572 case FSID_ENCODE_DEV:
573 case FSID_MAJOR_MINOR:
Jan Blunck54775492008-02-14 19:38:39 -0800574 if (fhp->fh_export->ex_path.dentry->d_inode->i_sb->s_type->fs_flags
Neil Brownb8da0d12007-09-05 17:22:13 -0400575 & FS_REQUIRES_DEV)
576 return FSIDSOURCE_DEV;
577 break;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800578 case FSID_NUM:
NeilBrownaf6a4e22007-02-14 00:33:12 -0800579 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
580 return FSIDSOURCE_FSID;
Neil Brownb8da0d12007-09-05 17:22:13 -0400581 break;
582 default:
583 break;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800584 }
Neil Brownb8da0d12007-09-05 17:22:13 -0400585 /* either a UUID type filehandle, or the filehandle doesn't
586 * match the export.
587 */
588 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
589 return FSIDSOURCE_FSID;
590 if (fhp->fh_export->ex_uuid)
591 return FSIDSOURCE_UUID;
592 return FSIDSOURCE_DEV;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800593}