blob: 0eb1c59f5ab835c1983267d3f7e026ce77c69565 [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
Christoph Hellwiga5694252007-07-17 04:04:28 -070012#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Andy Adamson32c1eb02007-07-17 04:04:48 -070014#include <linux/sunrpc/svcauth_gss.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020015#include "nfsd.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050016#include "vfs.h"
J. Bruce Fields2e8138a2007-11-15 17:05:43 -050017#include "auth.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#define NFSDDBG_FACILITY NFSDDBG_FH
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/*
23 * our acceptability function.
24 * if NOSUBTREECHECK, accept anything
25 * if not, require that we can walk up to exp->ex_dentry
26 * doing some checks on the 'x' bits
27 */
28static int nfsd_acceptable(void *expv, struct dentry *dentry)
29{
30 struct svc_export *exp = expv;
31 int rv;
32 struct dentry *tdentry;
33 struct dentry *parent;
34
35 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
36 return 1;
37
38 tdentry = dget(dentry);
Jan Blunck54775492008-02-14 19:38:39 -080039 while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 /* make sure parents give x permission to user */
41 int err;
42 parent = dget_parent(tdentry);
Al Virof419a2e2008-07-22 00:07:17 -040043 err = inode_permission(parent->d_inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 if (err < 0) {
45 dput(parent);
46 break;
47 }
48 dput(tdentry);
49 tdentry = parent;
50 }
Jan Blunck54775492008-02-14 19:38:39 -080051 if (tdentry != exp->ex_path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
Jan Blunck54775492008-02-14 19:38:39 -080053 rv = (tdentry == exp->ex_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 dput(tdentry);
55 return rv;
56}
57
58/* Type check. The correct error return for type mismatches does not seem to be
59 * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
60 * comment in the NFSv3 spec says this is incorrect (implementation notes for
61 * the write call).
62 */
Al Viro83b11342006-10-19 23:28:55 -070063static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070064nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
65{
66 /* Type can be negative when creating hardlinks - not to a dir */
67 if (type > 0 && (mode & S_IFMT) != type) {
68 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
69 return nfserr_symlink;
70 else if (type == S_IFDIR)
71 return nfserr_notdir;
72 else if ((mode & S_IFMT) == S_IFDIR)
73 return nfserr_isdir;
74 else
75 return nfserr_inval;
76 }
77 if (type < 0 && (mode & S_IFMT) == -type) {
78 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
79 return nfserr_symlink;
80 else if (type == -S_IFDIR)
81 return nfserr_isdir;
82 else
83 return nfserr_notdir;
84 }
85 return 0;
86}
87
J. Bruce Fields6fa02832007-11-12 16:05:03 -050088static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
89 struct svc_export *exp)
90{
91 /* Check if the request originated from a secure port. */
92 if (!rqstp->rq_secure && EX_SECURE(exp)) {
Pavel Emelyanov5216a8e2008-02-21 10:57:45 +030093 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
J. Bruce Fields6fa02832007-11-12 16:05:03 -050094 dprintk(KERN_WARNING
95 "nfsd: request from insecure port %s!\n",
96 svc_print_addr(rqstp, buf, sizeof(buf)));
97 return nfserr_perm;
98 }
99
100 /* Set user creds for this exportpoint */
101 return nfserrno(nfsd_setuser(rqstp, exp));
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
J. Bruce Fields03550fa2008-03-14 17:51:12 -0400105 * Use the given filehandle to look up the corresponding export and
106 * dentry. On success, the results are used to set fh_export and
107 * fh_dentry.
108 */
109static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
110{
111 struct knfsd_fh *fh = &fhp->fh_handle;
112 struct fid *fid = NULL, sfid;
113 struct svc_export *exp;
114 struct dentry *dentry;
115 int fileid_type;
116 int data_left = fh->fh_size/4;
117 __be32 error;
118
119 error = nfserr_stale;
120 if (rqstp->rq_vers > 2)
121 error = nfserr_badhandle;
122 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
123 return nfserr_nofilehandle;
124
125 if (fh->fh_version == 1) {
126 int len;
127
128 if (--data_left < 0)
129 return error;
130 if (fh->fh_auth_type != 0)
131 return error;
132 len = key_len(fh->fh_fsid_type) / 4;
133 if (len == 0)
134 return error;
135 if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
136 /* deprecated, convert to type 3 */
137 len = key_len(FSID_ENCODE_DEV)/4;
138 fh->fh_fsid_type = FSID_ENCODE_DEV;
139 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
140 fh->fh_fsid[1] = fh->fh_fsid[2];
141 }
142 data_left -= len;
143 if (data_left < 0)
144 return error;
145 exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_auth);
146 fid = (struct fid *)(fh->fh_auth + len);
147 } else {
148 __u32 tfh[2];
149 dev_t xdev;
150 ino_t xino;
151
152 if (fh->fh_size != NFS_FHSIZE)
153 return error;
154 /* assume old filehandle format */
155 xdev = old_decode_dev(fh->ofh_xdev);
156 xino = u32_to_ino_t(fh->ofh_xino);
157 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
158 exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
159 }
160
161 error = nfserr_stale;
162 if (PTR_ERR(exp) == -ENOENT)
163 return error;
164
165 if (IS_ERR(exp))
166 return nfserrno(PTR_ERR(exp));
167
Neil Brown496d6c32008-05-08 13:03:09 +1000168 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
169 /* Elevate privileges so that the lack of 'r' or 'x'
170 * permission on some parent directory will
171 * not stop exportfs_decode_fh from being able
172 * to reconnect a directory into the dentry cache.
173 * The same problem can affect "SUBTREECHECK" exports,
174 * but as nfsd_acceptable depends on correct
175 * access control settings being in effect, we cannot
176 * fix that case easily.
177 */
David Howellsd84f4f92008-11-14 10:39:23 +1100178 struct cred *new = prepare_creds();
179 if (!new)
180 return nfserrno(-ENOMEM);
181 new->cap_effective =
182 cap_raise_nfsd_set(new->cap_effective,
183 new->cap_permitted);
184 put_cred(override_creds(new));
185 put_cred(new);
Neil Brown496d6c32008-05-08 13:03:09 +1000186 } else {
187 error = nfsd_setuser_and_check_port(rqstp, exp);
188 if (error)
189 goto out;
190 }
J. Bruce Fields03550fa2008-03-14 17:51:12 -0400191
192 /*
193 * Look up the dentry using the NFS file handle.
194 */
195 error = nfserr_stale;
196 if (rqstp->rq_vers > 2)
197 error = nfserr_badhandle;
198
199 if (fh->fh_version != 1) {
200 sfid.i32.ino = fh->ofh_ino;
201 sfid.i32.gen = fh->ofh_generation;
202 sfid.i32.parent_ino = fh->ofh_dirino;
203 fid = &sfid;
204 data_left = 3;
205 if (fh->ofh_dirino == 0)
206 fileid_type = FILEID_INO32_GEN;
207 else
208 fileid_type = FILEID_INO32_GEN_PARENT;
209 } else
210 fileid_type = fh->fh_fileid_type;
211
212 if (fileid_type == FILEID_ROOT)
213 dentry = dget(exp->ex_path.dentry);
214 else {
215 dentry = exportfs_decode_fh(exp->ex_path.mnt, fid,
216 data_left, fileid_type,
217 nfsd_acceptable, exp);
218 }
219 if (dentry == NULL)
220 goto out;
221 if (IS_ERR(dentry)) {
222 if (PTR_ERR(dentry) != -EINVAL)
223 error = nfserrno(PTR_ERR(dentry));
224 goto out;
225 }
226
227 if (S_ISDIR(dentry->d_inode->i_mode) &&
228 (dentry->d_flags & DCACHE_DISCONNECTED)) {
229 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
230 dentry->d_parent->d_name.name, dentry->d_name.name);
231 }
232
233 fhp->fh_dentry = dentry;
234 fhp->fh_export = exp;
J. Bruce Fields03550fa2008-03-14 17:51:12 -0400235 return 0;
236out:
237 exp_put(exp);
238 return error;
239}
240
J. Bruce Fieldsb3d476762008-10-20 13:01:59 -0400241/**
242 * fh_verify - filehandle lookup and access checking
243 * @rqstp: pointer to current rpc request
244 * @fhp: filehandle to be verified
245 * @type: expected type of object pointed to by filehandle
246 * @access: type of access needed to object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 *
J. Bruce Fieldsb3d476762008-10-20 13:01:59 -0400248 * Look up a dentry from the on-the-wire filehandle, check the client's
249 * access to the export, and set the current task's credentials.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 *
J. Bruce Fieldsb3d476762008-10-20 13:01:59 -0400251 * Regardless of success or failure of fh_verify(), fh_put() should be
252 * called on @fhp when the caller is finished with the filehandle.
253 *
254 * fh_verify() may be called multiple times on a given filehandle, for
255 * example, when processing an NFSv4 compound. The first call will look
256 * up a dentry using the on-the-wire filehandle. Subsequent calls will
257 * skip the lookup and just perform the other checks and possibly change
258 * the current task's credentials.
259 *
260 * @type specifies the type of object expected using one of the S_IF*
261 * constants defined in include/linux/stat.h. The caller may use zero
262 * to indicate that it doesn't care, or a negative integer to indicate
263 * that it expects something not of the given type.
264 *
265 * @access is formed from the NFSD_MAY_* constants defined in
266 * include/linux/nfsd/nfsd.h.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Al Viro83b11342006-10-19 23:28:55 -0700268__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
270{
J. Bruce Fields03550fa2008-03-14 17:51:12 -0400271 struct svc_export *exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct dentry *dentry;
J. Bruce Fields03550fa2008-03-14 17:51:12 -0400273 __be32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (!fhp->fh_dentry) {
J. Bruce Fields03550fa2008-03-14 17:51:12 -0400278 error = nfsd_set_fh_dentry(rqstp, fhp);
NeilBrownd1bbf142006-07-30 03:03:16 -0700279 if (error)
280 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
J. Bruce Fields864f0f62009-11-25 17:42:05 -0500282 dentry = fhp->fh_dentry;
283 exp = fhp->fh_export;
284 /*
285 * We still have to do all these permission checks, even when
286 * fh_dentry is already set:
287 * - fh_verify may be called multiple times with different
288 * "access" arguments (e.g. nfsd_proc_create calls
289 * fh_verify(...,NFSD_MAY_EXEC) first, then later (in
290 * nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE).
291 * - in the NFSv4 case, the filehandle may have been filled
292 * in by fh_compose, and given a dentry, but further
293 * compound operations performed with that filehandle
294 * still need permissions checks. In the worst case, a
295 * mountpoint crossing may have changed the export
296 * options, and we may now need to use a different uid
297 * (for example, if different id-squashing options are in
298 * effect on the new filesystem).
299 */
300 error = nfsd_setuser_and_check_port(rqstp, exp);
301 if (error)
302 goto out;
J. Bruce Fields7fc90ec2006-06-30 01:56:14 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
305 if (error)
306 goto out;
307
J. Bruce Fields04716e62008-08-07 13:00:20 -0400308 /*
309 * pseudoflavor restrictions are not enforced on NLM,
310 * which clients virtually always use auth_sys for,
311 * even while using RPCSEC_GSS for NFS.
312 */
313 if (access & NFSD_MAY_LOCK)
314 goto skip_pseudoflavor_check;
315 /*
316 * Clients may expect to be able to use auth_sys during mount,
317 * even if they use gss for everything else; see section 2.3.2
318 * of rfc 2623.
319 */
320 if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
321 && exp->ex_path.dentry == dentry)
322 goto skip_pseudoflavor_check;
Andy Adamson32c1eb02007-07-17 04:04:48 -0700323
J. Bruce Fields04716e62008-08-07 13:00:20 -0400324 error = check_nfsd_access(exp, rqstp);
325 if (error)
326 goto out;
327
328skip_pseudoflavor_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /* Finally, check access permissions. */
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700330 error = nfsd_permission(rqstp, exp, dentry, access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (error) {
NeilBrown34e9a632007-01-29 13:19:52 -0800333 dprintk("fh_verify: %s/%s permission failure, "
334 "acc=%x, error=%d\n",
335 dentry->d_parent->d_name.name,
336 dentry->d_name.name,
Al Virofc2dd2e2007-02-01 13:52:43 +0000337 access, ntohl(error));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (error == nfserr_stale)
341 nfsdstats.fh_stale++;
342 return error;
343}
344
345
346/*
347 * Compose a file handle for an NFS reply.
348 *
349 * Note that when first composed, the dentry may not yet have
350 * an inode. In this case a call to fh_update should be made
351 * before the fh goes out on the wire ...
352 */
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700353static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
354 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Jan Blunck54775492008-02-14 19:38:39 -0800356 if (dentry != exp->ex_path.dentry) {
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700357 struct fid *fid = (struct fid *)
358 (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1);
359 int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
360 int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700362 fhp->fh_handle.fh_fileid_type =
363 exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
364 fhp->fh_handle.fh_size += maxsize * 4;
365 } else {
366 fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370/*
371 * for composing old style file handles
372 */
373static inline void _fh_update_old(struct dentry *dentry,
374 struct svc_export *exp,
375 struct knfsd_fh *fh)
376{
377 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
378 fh->ofh_generation = dentry->d_inode->i_generation;
379 if (S_ISDIR(dentry->d_inode->i_mode) ||
380 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
381 fh->ofh_dirino = 0;
382}
383
J. Bruce Fields8e498752009-09-02 19:31:32 -0400384static bool is_root_export(struct svc_export *exp)
385{
386 return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
387}
388
389static struct super_block *exp_sb(struct svc_export *exp)
390{
391 return exp->ex_path.dentry->d_inode->i_sb;
392}
393
394static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
395{
396 switch (fsid_type) {
397 case FSID_DEV:
398 if (!old_valid_dev(exp_sb(exp)->s_dev))
399 return 0;
400 /* FALL THROUGH */
401 case FSID_MAJOR_MINOR:
402 case FSID_ENCODE_DEV:
403 return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
404 case FSID_NUM:
405 return exp->ex_flags & NFSEXP_FSID;
406 case FSID_UUID8:
407 case FSID_UUID16:
408 if (!is_root_export(exp))
409 return 0;
410 /* fall through */
411 case FSID_UUID4_INUM:
412 case FSID_UUID16_INUM:
413 return exp->ex_uuid != NULL;
414 }
415 return 1;
416}
417
J. Bruce Fieldsbc6c53d2009-09-02 19:50:40 -0400418
419static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
NeilBrownb41eeef2007-05-09 02:34:57 -0700421 u8 version;
J. Bruce Fieldsbc6c53d2009-09-02 19:50:40 -0400422 u8 fsid_type;
423retry:
NeilBrownb41eeef2007-05-09 02:34:57 -0700424 version = 1;
NeilBrown7e405362006-06-30 01:56:12 -0700425 if (ref_fh && ref_fh->fh_export == exp) {
NeilBrown982aedf2007-02-14 00:33:11 -0800426 version = ref_fh->fh_handle.fh_version;
NeilBrownb41eeef2007-05-09 02:34:57 -0700427 fsid_type = ref_fh->fh_handle.fh_fsid_type;
428
NeilBrownb41eeef2007-05-09 02:34:57 -0700429 ref_fh = NULL;
430
431 switch (version) {
432 case 0xca:
NeilBrownaf6a4e22007-02-14 00:33:12 -0800433 fsid_type = FSID_DEV;
NeilBrownb41eeef2007-05-09 02:34:57 -0700434 break;
435 case 1:
436 break;
437 default:
438 goto retry;
439 }
440
J. Bruce Fields8e498752009-09-02 19:31:32 -0400441 /*
442 * As the fsid -> filesystem mapping was guided by
443 * user-space, there is no guarantee that the filesystem
444 * actually supports that fsid type. If it doesn't we
445 * loop around again without ref_fh set.
NeilBrown982aedf2007-02-14 00:33:11 -0800446 */
J. Bruce Fields8e498752009-09-02 19:31:32 -0400447 if (!fsid_type_ok_for_exp(fsid_type, exp))
448 goto retry;
Steve Dickson30fa8c02009-01-07 16:54:30 -0500449 } else if (exp->ex_flags & NFSEXP_FSID) {
450 fsid_type = FSID_NUM;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800451 } else if (exp->ex_uuid) {
452 if (fhp->fh_maxsize >= 64) {
J. Bruce Fields8e498752009-09-02 19:31:32 -0400453 if (is_root_export(exp))
NeilBrownaf6a4e22007-02-14 00:33:12 -0800454 fsid_type = FSID_UUID16;
455 else
456 fsid_type = FSID_UUID16_INUM;
457 } else {
J. Bruce Fields8e498752009-09-02 19:31:32 -0400458 if (is_root_export(exp))
NeilBrownaf6a4e22007-02-14 00:33:12 -0800459 fsid_type = FSID_UUID8;
460 else
461 fsid_type = FSID_UUID4_INUM;
462 }
J. Bruce Fieldsbc6c53d2009-09-02 19:50:40 -0400463 } else if (!old_valid_dev(exp_sb(exp)->s_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* for newer device numbers, we must use a newer fsid format */
NeilBrownaf6a4e22007-02-14 00:33:12 -0800465 fsid_type = FSID_ENCODE_DEV;
NeilBrown982aedf2007-02-14 00:33:11 -0800466 else
NeilBrownaf6a4e22007-02-14 00:33:12 -0800467 fsid_type = FSID_DEV;
J. Bruce Fieldsbc6c53d2009-09-02 19:50:40 -0400468 fhp->fh_handle.fh_version = version;
469 if (version)
470 fhp->fh_handle.fh_fsid_type = fsid_type;
471}
472
473__be32
474fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
475 struct svc_fh *ref_fh)
476{
477 /* ref_fh is a reference file handle.
478 * if it is non-null and for the same filesystem, then we should compose
479 * a filehandle which is of the same version, where possible.
480 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
481 * Then create a 32byte filehandle using nfs_fhbase_old
482 *
483 */
484
485 struct inode * inode = dentry->d_inode;
486 struct dentry *parent = dentry->d_parent;
487 __u32 *datap;
488 dev_t ex_dev = exp_sb(exp)->s_dev;
489
490 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
491 MAJOR(ex_dev), MINOR(ex_dev),
492 (long) exp->ex_path.dentry->d_inode->i_ino,
493 parent->d_name.name, dentry->d_name.name,
494 (inode ? inode->i_ino : 0));
495
496 /* Choose filehandle version and fsid type based on
497 * the reference filehandle (if it is in the same export)
498 * or the export options.
499 */
500 set_version_and_fsid_type(fhp, exp, ref_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (ref_fh == fhp)
503 fh_put(ref_fh);
504
505 if (fhp->fh_locked || fhp->fh_dentry) {
506 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
NeilBrown982aedf2007-02-14 00:33:11 -0800507 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 if (fhp->fh_maxsize < NFS_FHSIZE)
510 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
NeilBrown982aedf2007-02-14 00:33:11 -0800511 fhp->fh_maxsize,
512 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 fhp->fh_dentry = dget(dentry); /* our internal copy */
515 fhp->fh_export = exp;
516 cache_get(&exp->h);
517
J. Bruce Fieldsbc6c53d2009-09-02 19:50:40 -0400518 if (fhp->fh_handle.fh_version == 0xca) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* old style filehandle please */
520 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
521 fhp->fh_handle.fh_size = NFS_FHSIZE;
522 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
523 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
524 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
NeilBrown982aedf2007-02-14 00:33:11 -0800525 fhp->fh_handle.ofh_xino =
Jan Blunck54775492008-02-14 19:38:39 -0800526 ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
528 if (inode)
529 _fh_update_old(dentry, exp, &fhp->fh_handle);
530 } else {
531 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 fhp->fh_handle.fh_auth_type = 0;
533 datap = fhp->fh_handle.fh_auth+0;
J. Bruce Fieldsbc6c53d2009-09-02 19:50:40 -0400534 mk_fsid(fhp->fh_handle.fh_fsid_type, datap, ex_dev,
Jan Blunck54775492008-02-14 19:38:39 -0800535 exp->ex_path.dentry->d_inode->i_ino,
NeilBrownaf6a4e22007-02-14 00:33:12 -0800536 exp->ex_fsid, exp->ex_uuid);
537
J. Bruce Fieldsbc6c53d2009-09-02 19:50:40 -0400538 len = key_len(fhp->fh_handle.fh_fsid_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 datap += len/4;
540 fhp->fh_handle.fh_size = 4 + len;
541
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700542 if (inode)
543 _fh_update(fhp, exp, dentry);
J. Bruce Fields1be10a82009-09-04 11:59:32 -0400544 if (fhp->fh_handle.fh_fileid_type == 255) {
545 fh_put(fhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return nfserr_opnotsupp;
J. Bruce Fields1be10a82009-09-04 11:59:32 -0400547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 0;
551}
552
553/*
554 * Update file handle information after changing a dentry.
555 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
556 */
Al Viro83b11342006-10-19 23:28:55 -0700557__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558fh_update(struct svc_fh *fhp)
559{
560 struct dentry *dentry;
NeilBrown982aedf2007-02-14 00:33:11 -0800561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (!fhp->fh_dentry)
563 goto out_bad;
564
565 dentry = fhp->fh_dentry;
566 if (!dentry->d_inode)
567 goto out_negative;
568 if (fhp->fh_handle.fh_version != 1) {
569 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
570 } else {
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700571 if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
NeilBrown4c9608b2006-06-30 01:56:11 -0700572 goto out;
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700573
574 _fh_update(fhp, fhp->fh_export, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (fhp->fh_handle.fh_fileid_type == 255)
576 return nfserr_opnotsupp;
577 }
578out:
579 return 0;
580
581out_bad:
582 printk(KERN_ERR "fh_update: fh not verified!\n");
583 goto out;
584out_negative:
585 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
586 dentry->d_parent->d_name.name, dentry->d_name.name);
587 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590/*
591 * Release a file handle.
592 */
593void
594fh_put(struct svc_fh *fhp)
595{
596 struct dentry * dentry = fhp->fh_dentry;
597 struct svc_export * exp = fhp->fh_export;
598 if (dentry) {
599 fh_unlock(fhp);
600 fhp->fh_dentry = NULL;
601 dput(dentry);
602#ifdef CONFIG_NFSD_V3
603 fhp->fh_pre_saved = 0;
604 fhp->fh_post_saved = 0;
605#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 if (exp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800608 cache_put(&exp->h, &svc_export_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 fhp->fh_export = NULL;
610 }
611 return;
612}
613
614/*
615 * Shorthand for dprintk()'s
616 */
617char * SVCFH_fmt(struct svc_fh *fhp)
618{
619 struct knfsd_fh *fh = &fhp->fh_handle;
620
621 static char buf[80];
622 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
623 fh->fh_size,
624 fh->fh_base.fh_pad[0],
625 fh->fh_base.fh_pad[1],
626 fh->fh_base.fh_pad[2],
627 fh->fh_base.fh_pad[3],
628 fh->fh_base.fh_pad[4],
629 fh->fh_base.fh_pad[5]);
630 return buf;
631}
NeilBrownaf6a4e22007-02-14 00:33:12 -0800632
633enum fsid_source fsid_source(struct svc_fh *fhp)
634{
635 if (fhp->fh_handle.fh_version != 1)
636 return FSIDSOURCE_DEV;
637 switch(fhp->fh_handle.fh_fsid_type) {
638 case FSID_DEV:
639 case FSID_ENCODE_DEV:
640 case FSID_MAJOR_MINOR:
J. Bruce Fields8e498752009-09-02 19:31:32 -0400641 if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
Neil Brownb8da0d12007-09-05 17:22:13 -0400642 return FSIDSOURCE_DEV;
643 break;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800644 case FSID_NUM:
NeilBrownaf6a4e22007-02-14 00:33:12 -0800645 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
646 return FSIDSOURCE_FSID;
Neil Brownb8da0d12007-09-05 17:22:13 -0400647 break;
648 default:
649 break;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800650 }
Neil Brownb8da0d12007-09-05 17:22:13 -0400651 /* either a UUID type filehandle, or the filehandle doesn't
652 * match the export.
653 */
654 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
655 return FSIDSOURCE_FSID;
656 if (fhp->fh_export->ex_uuid)
657 return FSIDSOURCE_UUID;
658 return FSIDSOURCE_DEV;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800659}