Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #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 Hellwig | a569425 | 2007-07-17 04:04:28 -0700 | [diff] [blame] | 18 | #include <linux/exportfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 21 | #include <linux/sunrpc/clnt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/sunrpc/svc.h> |
Andy Adamson | 32c1eb0 | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 23 | #include <linux/sunrpc/svcauth_gss.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/nfsd/nfsd.h> |
J. Bruce Fields | 2e8138a | 2007-11-15 17:05:43 -0500 | [diff] [blame] | 25 | #include "auth.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #define NFSDDBG_FACILITY NFSDDBG_FH |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | |
| 30 | static int nfsd_nr_verified; |
| 31 | static int nfsd_nr_put; |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | /* |
| 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 | */ |
| 39 | static 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 Blunck | 5477549 | 2008-02-14 19:38:39 -0800 | [diff] [blame] | 50 | while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /* make sure parents give x permission to user */ |
| 52 | int err; |
| 53 | parent = dget_parent(tdentry); |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 54 | err = inode_permission(parent->d_inode, MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | if (err < 0) { |
| 56 | dput(parent); |
| 57 | break; |
| 58 | } |
| 59 | dput(tdentry); |
| 60 | tdentry = parent; |
| 61 | } |
Jan Blunck | 5477549 | 2008-02-14 19:38:39 -0800 | [diff] [blame] | 62 | if (tdentry != exp->ex_path.dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name); |
Jan Blunck | 5477549 | 2008-02-14 19:38:39 -0800 | [diff] [blame] | 64 | rv = (tdentry == exp->ex_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | 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 Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 74 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | nfsd_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 Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 99 | static __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 Emelyanov | 5216a8e | 2008-02-21 10:57:45 +0300 | [diff] [blame] | 104 | RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); |
J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | /* |
J. Bruce Fields | 03550fa | 2008-03-14 17:51:12 -0400 | [diff] [blame] | 116 | * 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 | */ |
| 120 | static __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 | |
Neil Brown | 496d6c3 | 2008-05-08 13:03:09 +1000 | [diff] [blame] | 179 | if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) { |
| 180 | /* Elevate privileges so that the lack of 'r' or 'x' |
| 181 | * permission on some parent directory will |
| 182 | * not stop exportfs_decode_fh from being able |
| 183 | * to reconnect a directory into the dentry cache. |
| 184 | * The same problem can affect "SUBTREECHECK" exports, |
| 185 | * but as nfsd_acceptable depends on correct |
| 186 | * access control settings being in effect, we cannot |
| 187 | * fix that case easily. |
| 188 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 189 | struct cred *new = prepare_creds(); |
| 190 | if (!new) |
| 191 | return nfserrno(-ENOMEM); |
| 192 | new->cap_effective = |
| 193 | cap_raise_nfsd_set(new->cap_effective, |
| 194 | new->cap_permitted); |
| 195 | put_cred(override_creds(new)); |
| 196 | put_cred(new); |
Neil Brown | 496d6c3 | 2008-05-08 13:03:09 +1000 | [diff] [blame] | 197 | } else { |
| 198 | error = nfsd_setuser_and_check_port(rqstp, exp); |
| 199 | if (error) |
| 200 | goto out; |
| 201 | } |
J. Bruce Fields | 03550fa | 2008-03-14 17:51:12 -0400 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * Look up the dentry using the NFS file handle. |
| 205 | */ |
| 206 | error = nfserr_stale; |
| 207 | if (rqstp->rq_vers > 2) |
| 208 | error = nfserr_badhandle; |
| 209 | |
| 210 | if (fh->fh_version != 1) { |
| 211 | sfid.i32.ino = fh->ofh_ino; |
| 212 | sfid.i32.gen = fh->ofh_generation; |
| 213 | sfid.i32.parent_ino = fh->ofh_dirino; |
| 214 | fid = &sfid; |
| 215 | data_left = 3; |
| 216 | if (fh->ofh_dirino == 0) |
| 217 | fileid_type = FILEID_INO32_GEN; |
| 218 | else |
| 219 | fileid_type = FILEID_INO32_GEN_PARENT; |
| 220 | } else |
| 221 | fileid_type = fh->fh_fileid_type; |
| 222 | |
| 223 | if (fileid_type == FILEID_ROOT) |
| 224 | dentry = dget(exp->ex_path.dentry); |
| 225 | else { |
| 226 | dentry = exportfs_decode_fh(exp->ex_path.mnt, fid, |
| 227 | data_left, fileid_type, |
| 228 | nfsd_acceptable, exp); |
| 229 | } |
| 230 | if (dentry == NULL) |
| 231 | goto out; |
| 232 | if (IS_ERR(dentry)) { |
| 233 | if (PTR_ERR(dentry) != -EINVAL) |
| 234 | error = nfserrno(PTR_ERR(dentry)); |
| 235 | goto out; |
| 236 | } |
| 237 | |
Neil Brown | 496d6c3 | 2008-05-08 13:03:09 +1000 | [diff] [blame] | 238 | if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) { |
| 239 | error = nfsd_setuser_and_check_port(rqstp, exp); |
| 240 | if (error) { |
| 241 | dput(dentry); |
| 242 | goto out; |
| 243 | } |
| 244 | } |
| 245 | |
J. Bruce Fields | 03550fa | 2008-03-14 17:51:12 -0400 | [diff] [blame] | 246 | if (S_ISDIR(dentry->d_inode->i_mode) && |
| 247 | (dentry->d_flags & DCACHE_DISCONNECTED)) { |
| 248 | printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n", |
| 249 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 250 | } |
| 251 | |
| 252 | fhp->fh_dentry = dentry; |
| 253 | fhp->fh_export = exp; |
| 254 | nfsd_nr_verified++; |
| 255 | return 0; |
| 256 | out: |
| 257 | exp_put(exp); |
| 258 | return error; |
| 259 | } |
| 260 | |
J. Bruce Fields | b3d47676 | 2008-10-20 13:01:59 -0400 | [diff] [blame] | 261 | /** |
| 262 | * fh_verify - filehandle lookup and access checking |
| 263 | * @rqstp: pointer to current rpc request |
| 264 | * @fhp: filehandle to be verified |
| 265 | * @type: expected type of object pointed to by filehandle |
| 266 | * @access: type of access needed to object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | * |
J. Bruce Fields | b3d47676 | 2008-10-20 13:01:59 -0400 | [diff] [blame] | 268 | * Look up a dentry from the on-the-wire filehandle, check the client's |
| 269 | * access to the export, and set the current task's credentials. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | * |
J. Bruce Fields | b3d47676 | 2008-10-20 13:01:59 -0400 | [diff] [blame] | 271 | * Regardless of success or failure of fh_verify(), fh_put() should be |
| 272 | * called on @fhp when the caller is finished with the filehandle. |
| 273 | * |
| 274 | * fh_verify() may be called multiple times on a given filehandle, for |
| 275 | * example, when processing an NFSv4 compound. The first call will look |
| 276 | * up a dentry using the on-the-wire filehandle. Subsequent calls will |
| 277 | * skip the lookup and just perform the other checks and possibly change |
| 278 | * the current task's credentials. |
| 279 | * |
| 280 | * @type specifies the type of object expected using one of the S_IF* |
| 281 | * constants defined in include/linux/stat.h. The caller may use zero |
| 282 | * to indicate that it doesn't care, or a negative integer to indicate |
| 283 | * that it expects something not of the given type. |
| 284 | * |
| 285 | * @access is formed from the NFSD_MAY_* constants defined in |
| 286 | * include/linux/nfsd/nfsd.h. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | */ |
Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 288 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access) |
| 290 | { |
J. Bruce Fields | 03550fa | 2008-03-14 17:51:12 -0400 | [diff] [blame] | 291 | struct svc_export *exp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | struct dentry *dentry; |
J. Bruce Fields | 03550fa | 2008-03-14 17:51:12 -0400 | [diff] [blame] | 293 | __be32 error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 295 | dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp)); |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | if (!fhp->fh_dentry) { |
J. Bruce Fields | 03550fa | 2008-03-14 17:51:12 -0400 | [diff] [blame] | 298 | error = nfsd_set_fh_dentry(rqstp, fhp); |
NeilBrown | d1bbf14 | 2006-07-30 03:03:16 -0700 | [diff] [blame] | 299 | if (error) |
| 300 | goto out; |
J. Bruce Fields | 03550fa | 2008-03-14 17:51:12 -0400 | [diff] [blame] | 301 | dentry = fhp->fh_dentry; |
| 302 | exp = fhp->fh_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } else { |
J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 304 | /* |
| 305 | * just rechecking permissions |
| 306 | * (e.g. nfsproc_create calls fh_verify, then nfsd_create |
| 307 | * does as well) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | */ |
| 309 | dprintk("nfsd: fh_verify - just checking\n"); |
| 310 | dentry = fhp->fh_dentry; |
| 311 | exp = fhp->fh_export; |
J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 312 | /* |
| 313 | * Set user creds for this exportpoint; necessary even |
NeilBrown | d1bbf14 | 2006-07-30 03:03:16 -0700 | [diff] [blame] | 314 | * in the "just checking" case because this may be a |
| 315 | * filehandle that was created by fh_compose, and that |
| 316 | * is about to be used in another nfsv4 compound |
J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 317 | * operation. |
| 318 | */ |
| 319 | error = nfsd_setuser_and_check_port(rqstp, exp); |
NeilBrown | d1bbf14 | 2006-07-30 03:03:16 -0700 | [diff] [blame] | 320 | if (error) |
| 321 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
J. Bruce Fields | 7fc90ec | 2006-06-30 01:56:14 -0700 | [diff] [blame] | 323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type); |
| 325 | if (error) |
| 326 | goto out; |
| 327 | |
J. Bruce Fields | 04716e6 | 2008-08-07 13:00:20 -0400 | [diff] [blame] | 328 | /* |
| 329 | * pseudoflavor restrictions are not enforced on NLM, |
| 330 | * which clients virtually always use auth_sys for, |
| 331 | * even while using RPCSEC_GSS for NFS. |
| 332 | */ |
| 333 | if (access & NFSD_MAY_LOCK) |
| 334 | goto skip_pseudoflavor_check; |
| 335 | /* |
| 336 | * Clients may expect to be able to use auth_sys during mount, |
| 337 | * even if they use gss for everything else; see section 2.3.2 |
| 338 | * of rfc 2623. |
| 339 | */ |
| 340 | if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT |
| 341 | && exp->ex_path.dentry == dentry) |
| 342 | goto skip_pseudoflavor_check; |
Andy Adamson | 32c1eb0 | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 343 | |
J. Bruce Fields | 04716e6 | 2008-08-07 13:00:20 -0400 | [diff] [blame] | 344 | error = check_nfsd_access(exp, rqstp); |
| 345 | if (error) |
| 346 | goto out; |
| 347 | |
| 348 | skip_pseudoflavor_check: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | /* Finally, check access permissions. */ |
J. Bruce Fields | 0ec757d | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 350 | error = nfsd_permission(rqstp, exp, dentry, access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | if (error) { |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 353 | dprintk("fh_verify: %s/%s permission failure, " |
| 354 | "acc=%x, error=%d\n", |
| 355 | dentry->d_parent->d_name.name, |
| 356 | dentry->d_name.name, |
Al Viro | fc2dd2e | 2007-02-01 13:52:43 +0000 | [diff] [blame] | 357 | access, ntohl(error)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | if (error == nfserr_stale) |
| 361 | nfsdstats.fh_stale++; |
| 362 | return error; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | /* |
| 367 | * Compose a file handle for an NFS reply. |
| 368 | * |
| 369 | * Note that when first composed, the dentry may not yet have |
| 370 | * an inode. In this case a call to fh_update should be made |
| 371 | * before the fh goes out on the wire ... |
| 372 | */ |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 373 | static void _fh_update(struct svc_fh *fhp, struct svc_export *exp, |
| 374 | struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
Jan Blunck | 5477549 | 2008-02-14 19:38:39 -0800 | [diff] [blame] | 376 | if (dentry != exp->ex_path.dentry) { |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 377 | struct fid *fid = (struct fid *) |
| 378 | (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1); |
| 379 | int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4; |
| 380 | int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 382 | fhp->fh_handle.fh_fileid_type = |
| 383 | exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck); |
| 384 | fhp->fh_handle.fh_size += maxsize * 4; |
| 385 | } else { |
| 386 | fhp->fh_handle.fh_fileid_type = FILEID_ROOT; |
| 387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* |
| 391 | * for composing old style file handles |
| 392 | */ |
| 393 | static inline void _fh_update_old(struct dentry *dentry, |
| 394 | struct svc_export *exp, |
| 395 | struct knfsd_fh *fh) |
| 396 | { |
| 397 | fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino); |
| 398 | fh->ofh_generation = dentry->d_inode->i_generation; |
| 399 | if (S_ISDIR(dentry->d_inode->i_mode) || |
| 400 | (exp->ex_flags & NFSEXP_NOSUBTREECHECK)) |
| 401 | fh->ofh_dirino = 0; |
| 402 | } |
| 403 | |
Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 404 | __be32 |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 405 | fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry, |
| 406 | struct svc_fh *ref_fh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
| 408 | /* ref_fh is a reference file handle. |
NeilBrown | 7e40536 | 2006-06-30 01:56:12 -0700 | [diff] [blame] | 409 | * if it is non-null and for the same filesystem, then we should compose |
| 410 | * a filehandle which is of the same version, where possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca |
| 412 | * Then create a 32byte filehandle using nfs_fhbase_old |
| 413 | * |
| 414 | */ |
| 415 | |
NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 416 | u8 version; |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 417 | u8 fsid_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | struct inode * inode = dentry->d_inode; |
| 419 | struct dentry *parent = dentry->d_parent; |
| 420 | __u32 *datap; |
Jan Blunck | 5477549 | 2008-02-14 19:38:39 -0800 | [diff] [blame] | 421 | dev_t ex_dev = exp->ex_path.dentry->d_inode->i_sb->s_dev; |
| 422 | int root_export = (exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
| 424 | dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n", |
| 425 | MAJOR(ex_dev), MINOR(ex_dev), |
Jan Blunck | 5477549 | 2008-02-14 19:38:39 -0800 | [diff] [blame] | 426 | (long) exp->ex_path.dentry->d_inode->i_ino, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | parent->d_name.name, dentry->d_name.name, |
| 428 | (inode ? inode->i_ino : 0)); |
| 429 | |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 430 | /* Choose filehandle version and fsid type based on |
| 431 | * the reference filehandle (if it is in the same export) |
| 432 | * or the export options. |
| 433 | */ |
NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 434 | retry: |
| 435 | version = 1; |
NeilBrown | 7e40536 | 2006-06-30 01:56:12 -0700 | [diff] [blame] | 436 | if (ref_fh && ref_fh->fh_export == exp) { |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 437 | version = ref_fh->fh_handle.fh_version; |
NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 438 | fsid_type = ref_fh->fh_handle.fh_fsid_type; |
| 439 | |
| 440 | if (ref_fh == fhp) |
| 441 | fh_put(ref_fh); |
| 442 | ref_fh = NULL; |
| 443 | |
| 444 | switch (version) { |
| 445 | case 0xca: |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 446 | fsid_type = FSID_DEV; |
NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 447 | break; |
| 448 | case 1: |
| 449 | break; |
| 450 | default: |
| 451 | goto retry; |
| 452 | } |
| 453 | |
| 454 | /* Need to check that this type works for this |
| 455 | * export point. As the fsid -> filesystem mapping |
| 456 | * was guided by user-space, there is no guarantee |
| 457 | * that the filesystem actually supports that fsid |
| 458 | * type. If it doesn't we loop around again without |
| 459 | * ref_fh set. |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 460 | */ |
NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 461 | switch(fsid_type) { |
| 462 | case FSID_DEV: |
| 463 | if (!old_valid_dev(ex_dev)) |
| 464 | goto retry; |
| 465 | /* FALL THROUGH */ |
| 466 | case FSID_MAJOR_MINOR: |
| 467 | case FSID_ENCODE_DEV: |
Jan Blunck | 5477549 | 2008-02-14 19:38:39 -0800 | [diff] [blame] | 468 | if (!(exp->ex_path.dentry->d_inode->i_sb->s_type->fs_flags |
NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 469 | & FS_REQUIRES_DEV)) |
| 470 | goto retry; |
| 471 | break; |
| 472 | case FSID_NUM: |
| 473 | if (! (exp->ex_flags & NFSEXP_FSID)) |
| 474 | goto retry; |
| 475 | break; |
| 476 | case FSID_UUID8: |
| 477 | case FSID_UUID16: |
| 478 | if (!root_export) |
| 479 | goto retry; |
| 480 | /* fall through */ |
| 481 | case FSID_UUID4_INUM: |
| 482 | case FSID_UUID16_INUM: |
| 483 | if (exp->ex_uuid == NULL) |
| 484 | goto retry; |
| 485 | break; |
| 486 | } |
Steve Dickson | 30fa8c0 | 2009-01-07 16:54:30 -0500 | [diff] [blame] | 487 | } else if (exp->ex_flags & NFSEXP_FSID) { |
| 488 | fsid_type = FSID_NUM; |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 489 | } else if (exp->ex_uuid) { |
| 490 | if (fhp->fh_maxsize >= 64) { |
| 491 | if (root_export) |
| 492 | fsid_type = FSID_UUID16; |
| 493 | else |
| 494 | fsid_type = FSID_UUID16_INUM; |
| 495 | } else { |
| 496 | if (root_export) |
| 497 | fsid_type = FSID_UUID8; |
| 498 | else |
| 499 | fsid_type = FSID_UUID4_INUM; |
| 500 | } |
Steve Dickson | 30fa8c0 | 2009-01-07 16:54:30 -0500 | [diff] [blame] | 501 | } else if (!old_valid_dev(ex_dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | /* for newer device numbers, we must use a newer fsid format */ |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 503 | fsid_type = FSID_ENCODE_DEV; |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 504 | else |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 505 | fsid_type = FSID_DEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
| 507 | if (ref_fh == fhp) |
| 508 | fh_put(ref_fh); |
| 509 | |
| 510 | if (fhp->fh_locked || fhp->fh_dentry) { |
| 511 | printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n", |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 512 | parent->d_name.name, dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | if (fhp->fh_maxsize < NFS_FHSIZE) |
| 515 | printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n", |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 516 | fhp->fh_maxsize, |
| 517 | parent->d_name.name, dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
| 519 | fhp->fh_dentry = dget(dentry); /* our internal copy */ |
| 520 | fhp->fh_export = exp; |
| 521 | cache_get(&exp->h); |
| 522 | |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 523 | if (version == 0xca) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | /* old style filehandle please */ |
| 525 | memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE); |
| 526 | fhp->fh_handle.fh_size = NFS_FHSIZE; |
| 527 | fhp->fh_handle.ofh_dcookie = 0xfeebbaca; |
| 528 | fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev); |
| 529 | fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev; |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 530 | fhp->fh_handle.ofh_xino = |
Jan Blunck | 5477549 | 2008-02-14 19:38:39 -0800 | [diff] [blame] | 531 | ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry)); |
| 533 | if (inode) |
| 534 | _fh_update_old(dentry, exp, &fhp->fh_handle); |
| 535 | } else { |
| 536 | int len; |
| 537 | fhp->fh_handle.fh_version = 1; |
| 538 | fhp->fh_handle.fh_auth_type = 0; |
| 539 | datap = fhp->fh_handle.fh_auth+0; |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 540 | fhp->fh_handle.fh_fsid_type = fsid_type; |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 541 | mk_fsid(fsid_type, datap, ex_dev, |
Jan Blunck | 5477549 | 2008-02-14 19:38:39 -0800 | [diff] [blame] | 542 | exp->ex_path.dentry->d_inode->i_ino, |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 543 | exp->ex_fsid, exp->ex_uuid); |
| 544 | |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 545 | len = key_len(fsid_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | datap += len/4; |
| 547 | fhp->fh_handle.fh_size = 4 + len; |
| 548 | |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 549 | if (inode) |
| 550 | _fh_update(fhp, exp, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | if (fhp->fh_handle.fh_fileid_type == 255) |
| 552 | return nfserr_opnotsupp; |
| 553 | } |
| 554 | |
| 555 | nfsd_nr_verified++; |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Update file handle information after changing a dentry. |
| 561 | * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create |
| 562 | */ |
Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 563 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | fh_update(struct svc_fh *fhp) |
| 565 | { |
| 566 | struct dentry *dentry; |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | if (!fhp->fh_dentry) |
| 569 | goto out_bad; |
| 570 | |
| 571 | dentry = fhp->fh_dentry; |
| 572 | if (!dentry->d_inode) |
| 573 | goto out_negative; |
| 574 | if (fhp->fh_handle.fh_version != 1) { |
| 575 | _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle); |
| 576 | } else { |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 577 | if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT) |
NeilBrown | 4c9608b | 2006-06-30 01:56:11 -0700 | [diff] [blame] | 578 | goto out; |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 579 | |
| 580 | _fh_update(fhp, fhp->fh_export, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | if (fhp->fh_handle.fh_fileid_type == 255) |
| 582 | return nfserr_opnotsupp; |
| 583 | } |
| 584 | out: |
| 585 | return 0; |
| 586 | |
| 587 | out_bad: |
| 588 | printk(KERN_ERR "fh_update: fh not verified!\n"); |
| 589 | goto out; |
| 590 | out_negative: |
| 591 | printk(KERN_ERR "fh_update: %s/%s still negative!\n", |
| 592 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 593 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Release a file handle. |
| 598 | */ |
| 599 | void |
| 600 | fh_put(struct svc_fh *fhp) |
| 601 | { |
| 602 | struct dentry * dentry = fhp->fh_dentry; |
| 603 | struct svc_export * exp = fhp->fh_export; |
| 604 | if (dentry) { |
| 605 | fh_unlock(fhp); |
| 606 | fhp->fh_dentry = NULL; |
| 607 | dput(dentry); |
| 608 | #ifdef CONFIG_NFSD_V3 |
| 609 | fhp->fh_pre_saved = 0; |
| 610 | fhp->fh_post_saved = 0; |
| 611 | #endif |
| 612 | nfsd_nr_put++; |
| 613 | } |
| 614 | if (exp) { |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 615 | cache_put(&exp->h, &svc_export_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | fhp->fh_export = NULL; |
| 617 | } |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | * Shorthand for dprintk()'s |
| 623 | */ |
| 624 | char * SVCFH_fmt(struct svc_fh *fhp) |
| 625 | { |
| 626 | struct knfsd_fh *fh = &fhp->fh_handle; |
| 627 | |
| 628 | static char buf[80]; |
| 629 | sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x", |
| 630 | fh->fh_size, |
| 631 | fh->fh_base.fh_pad[0], |
| 632 | fh->fh_base.fh_pad[1], |
| 633 | fh->fh_base.fh_pad[2], |
| 634 | fh->fh_base.fh_pad[3], |
| 635 | fh->fh_base.fh_pad[4], |
| 636 | fh->fh_base.fh_pad[5]); |
| 637 | return buf; |
| 638 | } |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 639 | |
| 640 | enum fsid_source fsid_source(struct svc_fh *fhp) |
| 641 | { |
| 642 | if (fhp->fh_handle.fh_version != 1) |
| 643 | return FSIDSOURCE_DEV; |
| 644 | switch(fhp->fh_handle.fh_fsid_type) { |
| 645 | case FSID_DEV: |
| 646 | case FSID_ENCODE_DEV: |
| 647 | case FSID_MAJOR_MINOR: |
Jan Blunck | 5477549 | 2008-02-14 19:38:39 -0800 | [diff] [blame] | 648 | if (fhp->fh_export->ex_path.dentry->d_inode->i_sb->s_type->fs_flags |
Neil Brown | b8da0d1 | 2007-09-05 17:22:13 -0400 | [diff] [blame] | 649 | & FS_REQUIRES_DEV) |
| 650 | return FSIDSOURCE_DEV; |
| 651 | break; |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 652 | case FSID_NUM: |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 653 | if (fhp->fh_export->ex_flags & NFSEXP_FSID) |
| 654 | return FSIDSOURCE_FSID; |
Neil Brown | b8da0d1 | 2007-09-05 17:22:13 -0400 | [diff] [blame] | 655 | break; |
| 656 | default: |
| 657 | break; |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 658 | } |
Neil Brown | b8da0d1 | 2007-09-05 17:22:13 -0400 | [diff] [blame] | 659 | /* either a UUID type filehandle, or the filehandle doesn't |
| 660 | * match the export. |
| 661 | */ |
| 662 | if (fhp->fh_export->ex_flags & NFSEXP_FSID) |
| 663 | return FSIDSOURCE_FSID; |
| 664 | if (fhp->fh_export->ex_uuid) |
| 665 | return FSIDSOURCE_UUID; |
| 666 | return FSIDSOURCE_DEV; |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 667 | } |