blob: 100ae5641162b97c7fb0e57032a9b30ecc5b899e [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)) {
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +0300104 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
J. Bruce Fields6fa02832007-11-12 16:05:03 -0500105 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/*
J. Bruce Fields03550fa2008-03-14 17:51:12 -0400116 * Use the given filehandle to look up the corresponding export and
117 * dentry. On success, the results are used to set fh_export and
118 * fh_dentry.
119 */
120static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
121{
122 struct knfsd_fh *fh = &fhp->fh_handle;
123 struct fid *fid = NULL, sfid;
124 struct svc_export *exp;
125 struct dentry *dentry;
126 int fileid_type;
127 int data_left = fh->fh_size/4;
128 __be32 error;
129
130 error = nfserr_stale;
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
139 if (--data_left < 0)
140 return error;
141 if (fh->fh_auth_type != 0)
142 return error;
143 len = key_len(fh->fh_fsid_type) / 4;
144 if (len == 0)
145 return error;
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 data_left -= len;
154 if (data_left < 0)
155 return error;
156 exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_auth);
157 fid = (struct fid *)(fh->fh_auth + len);
158 } else {
159 __u32 tfh[2];
160 dev_t xdev;
161 ino_t xino;
162
163 if (fh->fh_size != NFS_FHSIZE)
164 return error;
165 /* assume old filehandle format */
166 xdev = old_decode_dev(fh->ofh_xdev);
167 xino = u32_to_ino_t(fh->ofh_xino);
168 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
169 exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
170 }
171
172 error = nfserr_stale;
173 if (PTR_ERR(exp) == -ENOENT)
174 return error;
175
176 if (IS_ERR(exp))
177 return nfserrno(PTR_ERR(exp));
178
179 error = nfsd_setuser_and_check_port(rqstp, exp);
180 if (error)
181 goto out;
182
183 /*
184 * Look up the dentry using the NFS file handle.
185 */
186 error = nfserr_stale;
187 if (rqstp->rq_vers > 2)
188 error = nfserr_badhandle;
189
190 if (fh->fh_version != 1) {
191 sfid.i32.ino = fh->ofh_ino;
192 sfid.i32.gen = fh->ofh_generation;
193 sfid.i32.parent_ino = fh->ofh_dirino;
194 fid = &sfid;
195 data_left = 3;
196 if (fh->ofh_dirino == 0)
197 fileid_type = FILEID_INO32_GEN;
198 else
199 fileid_type = FILEID_INO32_GEN_PARENT;
200 } else
201 fileid_type = fh->fh_fileid_type;
202
203 if (fileid_type == FILEID_ROOT)
204 dentry = dget(exp->ex_path.dentry);
205 else {
206 dentry = exportfs_decode_fh(exp->ex_path.mnt, fid,
207 data_left, fileid_type,
208 nfsd_acceptable, exp);
209 }
210 if (dentry == NULL)
211 goto out;
212 if (IS_ERR(dentry)) {
213 if (PTR_ERR(dentry) != -EINVAL)
214 error = nfserrno(PTR_ERR(dentry));
215 goto out;
216 }
217
218 if (S_ISDIR(dentry->d_inode->i_mode) &&
219 (dentry->d_flags & DCACHE_DISCONNECTED)) {
220 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
221 dentry->d_parent->d_name.name, dentry->d_name.name);
222 }
223
224 fhp->fh_dentry = dentry;
225 fhp->fh_export = exp;
226 nfsd_nr_verified++;
227 return 0;
228out:
229 exp_put(exp);
230 return error;
231}
232
233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * Perform sanity checks on the dentry in a client's file handle.
235 *
236 * Note that the file handle dentry may need to be freed even after
237 * an error return.
238 *
239 * This is only called at the start of an nfsproc call, so fhp points to
240 * a svc_fh which is all 0 except for the over-the-wire file handle.
241 */
Al Viro83b11342006-10-19 23:28:55 -0700242__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
244{
J. Bruce Fields03550fa2008-03-14 17:51:12 -0400245 struct svc_export *exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct dentry *dentry;
J. Bruce Fields03550fa2008-03-14 17:51:12 -0400247 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (!fhp->fh_dentry) {
J. Bruce Fields03550fa2008-03-14 17:51:12 -0400252 error = nfsd_set_fh_dentry(rqstp, fhp);
NeilBrownd1bbf142006-07-30 03:03:16 -0700253 if (error)
254 goto out;
J. Bruce Fields03550fa2008-03-14 17:51:12 -0400255 dentry = fhp->fh_dentry;
256 exp = fhp->fh_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 } else {
J. Bruce Fields6fa02832007-11-12 16:05:03 -0500258 /*
259 * just rechecking permissions
260 * (e.g. nfsproc_create calls fh_verify, then nfsd_create
261 * does as well)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
263 dprintk("nfsd: fh_verify - just checking\n");
264 dentry = fhp->fh_dentry;
265 exp = fhp->fh_export;
J. Bruce Fields6fa02832007-11-12 16:05:03 -0500266 /*
267 * Set user creds for this exportpoint; necessary even
NeilBrownd1bbf142006-07-30 03:03:16 -0700268 * in the "just checking" case because this may be a
269 * filehandle that was created by fh_compose, and that
270 * is about to be used in another nfsv4 compound
J. Bruce Fields6fa02832007-11-12 16:05:03 -0500271 * operation.
272 */
273 error = nfsd_setuser_and_check_port(rqstp, exp);
NeilBrownd1bbf142006-07-30 03:03:16 -0700274 if (error)
275 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
J. Bruce Fields7fc90ec2006-06-30 01:56:14 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
279 if (error)
280 goto out;
281
J. Bruce Fields90912242007-07-17 04:04:52 -0700282 if (!(access & MAY_LOCK)) {
283 /*
284 * pseudoflavor restrictions are not enforced on NLM,
285 * which clients virtually always use auth_sys for,
286 * even while using RPCSEC_GSS for NFS.
287 */
288 error = check_nfsd_access(exp, rqstp);
289 if (error)
290 goto out;
291 }
Andy Adamson32c1eb02007-07-17 04:04:48 -0700292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* Finally, check access permissions. */
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700294 error = nfsd_permission(rqstp, exp, dentry, access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (error) {
NeilBrown34e9a632007-01-29 13:19:52 -0800297 dprintk("fh_verify: %s/%s permission failure, "
298 "acc=%x, error=%d\n",
299 dentry->d_parent->d_name.name,
300 dentry->d_name.name,
Al Virofc2dd2e2007-02-01 13:52:43 +0000301 access, ntohl(error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (error == nfserr_stale)
305 nfsdstats.fh_stale++;
306 return error;
307}
308
309
310/*
311 * Compose a file handle for an NFS reply.
312 *
313 * Note that when first composed, the dentry may not yet have
314 * an inode. In this case a call to fh_update should be made
315 * before the fh goes out on the wire ...
316 */
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700317static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
318 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Jan Blunck54775492008-02-14 19:38:39 -0800320 if (dentry != exp->ex_path.dentry) {
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700321 struct fid *fid = (struct fid *)
322 (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1);
323 int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
324 int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700326 fhp->fh_handle.fh_fileid_type =
327 exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
328 fhp->fh_handle.fh_size += maxsize * 4;
329 } else {
330 fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334/*
335 * for composing old style file handles
336 */
337static inline void _fh_update_old(struct dentry *dentry,
338 struct svc_export *exp,
339 struct knfsd_fh *fh)
340{
341 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
342 fh->ofh_generation = dentry->d_inode->i_generation;
343 if (S_ISDIR(dentry->d_inode->i_mode) ||
344 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
345 fh->ofh_dirino = 0;
346}
347
Al Viro83b11342006-10-19 23:28:55 -0700348__be32
NeilBrown982aedf2007-02-14 00:33:11 -0800349fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
350 struct svc_fh *ref_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 /* ref_fh is a reference file handle.
NeilBrown7e405362006-06-30 01:56:12 -0700353 * if it is non-null and for the same filesystem, then we should compose
354 * a filehandle which is of the same version, where possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
356 * Then create a 32byte filehandle using nfs_fhbase_old
357 *
358 */
359
NeilBrownb41eeef2007-05-09 02:34:57 -0700360 u8 version;
NeilBrown982aedf2007-02-14 00:33:11 -0800361 u8 fsid_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct inode * inode = dentry->d_inode;
363 struct dentry *parent = dentry->d_parent;
364 __u32 *datap;
Jan Blunck54775492008-02-14 19:38:39 -0800365 dev_t ex_dev = exp->ex_path.dentry->d_inode->i_sb->s_dev;
366 int root_export = (exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
369 MAJOR(ex_dev), MINOR(ex_dev),
Jan Blunck54775492008-02-14 19:38:39 -0800370 (long) exp->ex_path.dentry->d_inode->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 parent->d_name.name, dentry->d_name.name,
372 (inode ? inode->i_ino : 0));
373
NeilBrown982aedf2007-02-14 00:33:11 -0800374 /* Choose filehandle version and fsid type based on
375 * the reference filehandle (if it is in the same export)
376 * or the export options.
377 */
NeilBrownb41eeef2007-05-09 02:34:57 -0700378 retry:
379 version = 1;
NeilBrown7e405362006-06-30 01:56:12 -0700380 if (ref_fh && ref_fh->fh_export == exp) {
NeilBrown982aedf2007-02-14 00:33:11 -0800381 version = ref_fh->fh_handle.fh_version;
NeilBrownb41eeef2007-05-09 02:34:57 -0700382 fsid_type = ref_fh->fh_handle.fh_fsid_type;
383
384 if (ref_fh == fhp)
385 fh_put(ref_fh);
386 ref_fh = NULL;
387
388 switch (version) {
389 case 0xca:
NeilBrownaf6a4e22007-02-14 00:33:12 -0800390 fsid_type = FSID_DEV;
NeilBrownb41eeef2007-05-09 02:34:57 -0700391 break;
392 case 1:
393 break;
394 default:
395 goto retry;
396 }
397
398 /* Need to check that this type works for this
399 * export point. As the fsid -> filesystem mapping
400 * was guided by user-space, there is no guarantee
401 * that the filesystem actually supports that fsid
402 * type. If it doesn't we loop around again without
403 * ref_fh set.
NeilBrown982aedf2007-02-14 00:33:11 -0800404 */
NeilBrownb41eeef2007-05-09 02:34:57 -0700405 switch(fsid_type) {
406 case FSID_DEV:
407 if (!old_valid_dev(ex_dev))
408 goto retry;
409 /* FALL THROUGH */
410 case FSID_MAJOR_MINOR:
411 case FSID_ENCODE_DEV:
Jan Blunck54775492008-02-14 19:38:39 -0800412 if (!(exp->ex_path.dentry->d_inode->i_sb->s_type->fs_flags
NeilBrownb41eeef2007-05-09 02:34:57 -0700413 & FS_REQUIRES_DEV))
414 goto retry;
415 break;
416 case FSID_NUM:
417 if (! (exp->ex_flags & NFSEXP_FSID))
418 goto retry;
419 break;
420 case FSID_UUID8:
421 case FSID_UUID16:
422 if (!root_export)
423 goto retry;
424 /* fall through */
425 case FSID_UUID4_INUM:
426 case FSID_UUID16_INUM:
427 if (exp->ex_uuid == NULL)
428 goto retry;
429 break;
430 }
NeilBrownaf6a4e22007-02-14 00:33:12 -0800431 } else if (exp->ex_uuid) {
432 if (fhp->fh_maxsize >= 64) {
433 if (root_export)
434 fsid_type = FSID_UUID16;
435 else
436 fsid_type = FSID_UUID16_INUM;
437 } else {
438 if (root_export)
439 fsid_type = FSID_UUID8;
440 else
441 fsid_type = FSID_UUID4_INUM;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 } else if (exp->ex_flags & NFSEXP_FSID)
NeilBrownaf6a4e22007-02-14 00:33:12 -0800444 fsid_type = FSID_NUM;
NeilBrown982aedf2007-02-14 00:33:11 -0800445 else if (!old_valid_dev(ex_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /* for newer device numbers, we must use a newer fsid format */
NeilBrownaf6a4e22007-02-14 00:33:12 -0800447 fsid_type = FSID_ENCODE_DEV;
NeilBrown982aedf2007-02-14 00:33:11 -0800448 else
NeilBrownaf6a4e22007-02-14 00:33:12 -0800449 fsid_type = FSID_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (ref_fh == fhp)
452 fh_put(ref_fh);
453
454 if (fhp->fh_locked || fhp->fh_dentry) {
455 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
NeilBrown982aedf2007-02-14 00:33:11 -0800456 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 if (fhp->fh_maxsize < NFS_FHSIZE)
459 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
NeilBrown982aedf2007-02-14 00:33:11 -0800460 fhp->fh_maxsize,
461 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 fhp->fh_dentry = dget(dentry); /* our internal copy */
464 fhp->fh_export = exp;
465 cache_get(&exp->h);
466
NeilBrown982aedf2007-02-14 00:33:11 -0800467 if (version == 0xca) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* old style filehandle please */
469 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
470 fhp->fh_handle.fh_size = NFS_FHSIZE;
471 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
472 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
473 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
NeilBrown982aedf2007-02-14 00:33:11 -0800474 fhp->fh_handle.ofh_xino =
Jan Blunck54775492008-02-14 19:38:39 -0800475 ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
477 if (inode)
478 _fh_update_old(dentry, exp, &fhp->fh_handle);
479 } else {
480 int len;
481 fhp->fh_handle.fh_version = 1;
482 fhp->fh_handle.fh_auth_type = 0;
483 datap = fhp->fh_handle.fh_auth+0;
NeilBrown982aedf2007-02-14 00:33:11 -0800484 fhp->fh_handle.fh_fsid_type = fsid_type;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800485 mk_fsid(fsid_type, datap, ex_dev,
Jan Blunck54775492008-02-14 19:38:39 -0800486 exp->ex_path.dentry->d_inode->i_ino,
NeilBrownaf6a4e22007-02-14 00:33:12 -0800487 exp->ex_fsid, exp->ex_uuid);
488
NeilBrown982aedf2007-02-14 00:33:11 -0800489 len = key_len(fsid_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 datap += len/4;
491 fhp->fh_handle.fh_size = 4 + len;
492
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700493 if (inode)
494 _fh_update(fhp, exp, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (fhp->fh_handle.fh_fileid_type == 255)
496 return nfserr_opnotsupp;
497 }
498
499 nfsd_nr_verified++;
500 return 0;
501}
502
503/*
504 * Update file handle information after changing a dentry.
505 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
506 */
Al Viro83b11342006-10-19 23:28:55 -0700507__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508fh_update(struct svc_fh *fhp)
509{
510 struct dentry *dentry;
NeilBrown982aedf2007-02-14 00:33:11 -0800511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!fhp->fh_dentry)
513 goto out_bad;
514
515 dentry = fhp->fh_dentry;
516 if (!dentry->d_inode)
517 goto out_negative;
518 if (fhp->fh_handle.fh_version != 1) {
519 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
520 } else {
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700521 if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
NeilBrown4c9608b2006-06-30 01:56:11 -0700522 goto out;
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700523
524 _fh_update(fhp, fhp->fh_export, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (fhp->fh_handle.fh_fileid_type == 255)
526 return nfserr_opnotsupp;
527 }
528out:
529 return 0;
530
531out_bad:
532 printk(KERN_ERR "fh_update: fh not verified!\n");
533 goto out;
534out_negative:
535 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
536 dentry->d_parent->d_name.name, dentry->d_name.name);
537 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540/*
541 * Release a file handle.
542 */
543void
544fh_put(struct svc_fh *fhp)
545{
546 struct dentry * dentry = fhp->fh_dentry;
547 struct svc_export * exp = fhp->fh_export;
548 if (dentry) {
549 fh_unlock(fhp);
550 fhp->fh_dentry = NULL;
551 dput(dentry);
552#ifdef CONFIG_NFSD_V3
553 fhp->fh_pre_saved = 0;
554 fhp->fh_post_saved = 0;
555#endif
556 nfsd_nr_put++;
557 }
558 if (exp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800559 cache_put(&exp->h, &svc_export_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 fhp->fh_export = NULL;
561 }
562 return;
563}
564
565/*
566 * Shorthand for dprintk()'s
567 */
568char * SVCFH_fmt(struct svc_fh *fhp)
569{
570 struct knfsd_fh *fh = &fhp->fh_handle;
571
572 static char buf[80];
573 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
574 fh->fh_size,
575 fh->fh_base.fh_pad[0],
576 fh->fh_base.fh_pad[1],
577 fh->fh_base.fh_pad[2],
578 fh->fh_base.fh_pad[3],
579 fh->fh_base.fh_pad[4],
580 fh->fh_base.fh_pad[5]);
581 return buf;
582}
NeilBrownaf6a4e22007-02-14 00:33:12 -0800583
584enum fsid_source fsid_source(struct svc_fh *fhp)
585{
586 if (fhp->fh_handle.fh_version != 1)
587 return FSIDSOURCE_DEV;
588 switch(fhp->fh_handle.fh_fsid_type) {
589 case FSID_DEV:
590 case FSID_ENCODE_DEV:
591 case FSID_MAJOR_MINOR:
Jan Blunck54775492008-02-14 19:38:39 -0800592 if (fhp->fh_export->ex_path.dentry->d_inode->i_sb->s_type->fs_flags
Neil Brownb8da0d12007-09-05 17:22:13 -0400593 & FS_REQUIRES_DEV)
594 return FSIDSOURCE_DEV;
595 break;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800596 case FSID_NUM:
NeilBrownaf6a4e22007-02-14 00:33:12 -0800597 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
598 return FSIDSOURCE_FSID;
Neil Brownb8da0d12007-09-05 17:22:13 -0400599 break;
600 default:
601 break;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800602 }
Neil Brownb8da0d12007-09-05 17:22:13 -0400603 /* either a UUID type filehandle, or the filehandle doesn't
604 * match the export.
605 */
606 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
607 return FSIDSOURCE_FSID;
608 if (fhp->fh_export->ex_uuid)
609 return FSIDSOURCE_UUID;
610 return FSIDSOURCE_DEV;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800611}