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> |
| 25 | |
| 26 | #define NFSDDBG_FACILITY NFSDDBG_FH |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | static int nfsd_nr_verified; |
| 30 | static int nfsd_nr_put; |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /* |
| 33 | * our acceptability function. |
| 34 | * if NOSUBTREECHECK, accept anything |
| 35 | * if not, require that we can walk up to exp->ex_dentry |
| 36 | * doing some checks on the 'x' bits |
| 37 | */ |
| 38 | static int nfsd_acceptable(void *expv, struct dentry *dentry) |
| 39 | { |
| 40 | struct svc_export *exp = expv; |
| 41 | int rv; |
| 42 | struct dentry *tdentry; |
| 43 | struct dentry *parent; |
| 44 | |
| 45 | if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) |
| 46 | return 1; |
| 47 | |
| 48 | tdentry = dget(dentry); |
| 49 | while (tdentry != exp->ex_dentry && ! IS_ROOT(tdentry)) { |
| 50 | /* make sure parents give x permission to user */ |
| 51 | int err; |
| 52 | parent = dget_parent(tdentry); |
| 53 | err = permission(parent->d_inode, MAY_EXEC, NULL); |
| 54 | if (err < 0) { |
| 55 | dput(parent); |
| 56 | break; |
| 57 | } |
| 58 | dput(tdentry); |
| 59 | tdentry = parent; |
| 60 | } |
| 61 | if (tdentry != exp->ex_dentry) |
| 62 | dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name); |
| 63 | rv = (tdentry == exp->ex_dentry); |
| 64 | dput(tdentry); |
| 65 | return rv; |
| 66 | } |
| 67 | |
| 68 | /* Type check. The correct error return for type mismatches does not seem to be |
| 69 | * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a |
| 70 | * comment in the NFSv3 spec says this is incorrect (implementation notes for |
| 71 | * the write call). |
| 72 | */ |
Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 73 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type) |
| 75 | { |
| 76 | /* Type can be negative when creating hardlinks - not to a dir */ |
| 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_notdir; |
| 82 | else if ((mode & S_IFMT) == S_IFDIR) |
| 83 | return nfserr_isdir; |
| 84 | else |
| 85 | return nfserr_inval; |
| 86 | } |
| 87 | if (type < 0 && (mode & S_IFMT) == -type) { |
| 88 | if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK) |
| 89 | return nfserr_symlink; |
| 90 | else if (type == -S_IFDIR) |
| 91 | return nfserr_isdir; |
| 92 | else |
| 93 | return nfserr_notdir; |
| 94 | } |
| 95 | return 0; |
| 96 | } |
| 97 | |
J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 98 | static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp, |
| 99 | struct svc_export *exp) |
| 100 | { |
| 101 | /* Check if the request originated from a secure port. */ |
| 102 | if (!rqstp->rq_secure && EX_SECURE(exp)) { |
| 103 | char buf[RPC_MAX_ADDRBUFLEN]; |
| 104 | dprintk(KERN_WARNING |
| 105 | "nfsd: request from insecure port %s!\n", |
| 106 | svc_print_addr(rqstp, buf, sizeof(buf))); |
| 107 | return nfserr_perm; |
| 108 | } |
| 109 | |
| 110 | /* Set user creds for this exportpoint */ |
| 111 | return nfserrno(nfsd_setuser(rqstp, exp)); |
| 112 | } |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | /* |
| 115 | * Perform sanity checks on the dentry in a client's file handle. |
| 116 | * |
| 117 | * Note that the file handle dentry may need to be freed even after |
| 118 | * an error return. |
| 119 | * |
| 120 | * This is only called at the start of an nfsproc call, so fhp points to |
| 121 | * a svc_fh which is all 0 except for the over-the-wire file handle. |
| 122 | */ |
Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 123 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access) |
| 125 | { |
| 126 | struct knfsd_fh *fh = &fhp->fh_handle; |
| 127 | struct svc_export *exp = NULL; |
| 128 | struct dentry *dentry; |
Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 129 | __be32 error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp)); |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | if (!fhp->fh_dentry) { |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 134 | struct fid *fid = NULL, sfid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | int fileid_type; |
| 136 | int data_left = fh->fh_size/4; |
| 137 | |
| 138 | error = nfserr_stale; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | if (rqstp->rq_vers > 2) |
| 140 | error = nfserr_badhandle; |
| 141 | if (rqstp->rq_vers == 4 && fh->fh_size == 0) |
| 142 | return nfserr_nofilehandle; |
| 143 | |
| 144 | if (fh->fh_version == 1) { |
| 145 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | if (--data_left<0) goto out; |
| 147 | switch (fh->fh_auth_type) { |
| 148 | case 0: break; |
| 149 | default: goto out; |
| 150 | } |
| 151 | len = key_len(fh->fh_fsid_type) / 4; |
| 152 | if (len == 0) goto out; |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 153 | if (fh->fh_fsid_type == FSID_MAJOR_MINOR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /* deprecated, convert to type 3 */ |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 155 | len = key_len(FSID_ENCODE_DEV)/4; |
| 156 | fh->fh_fsid_type = FSID_ENCODE_DEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1]))); |
| 158 | fh->fh_fsid[1] = fh->fh_fsid[2]; |
| 159 | } |
| 160 | if ((data_left -= len)<0) goto out; |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 161 | exp = rqst_exp_find(rqstp, fh->fh_fsid_type, |
| 162 | fh->fh_auth); |
| 163 | fid = (struct fid *)(fh->fh_auth + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } else { |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 165 | __u32 tfh[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | dev_t xdev; |
| 167 | ino_t xino; |
| 168 | if (fh->fh_size != NFS_FHSIZE) |
| 169 | goto out; |
| 170 | /* assume old filehandle format */ |
| 171 | xdev = old_decode_dev(fh->ofh_xdev); |
| 172 | xino = u32_to_ino_t(fh->ofh_xino); |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 173 | mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL); |
J. Bruce Fields | 0989a78 | 2007-07-17 04:04:44 -0700 | [diff] [blame] | 174 | exp = rqst_exp_find(rqstp, FSID_DEV, tfh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
J. Bruce Fields | 2d3bb25 | 2007-07-17 04:04:40 -0700 | [diff] [blame] | 177 | error = nfserr_stale; |
| 178 | if (PTR_ERR(exp) == -ENOENT) |
| 179 | goto out; |
| 180 | |
| 181 | if (IS_ERR(exp)) { |
J.Bruce Fields | e0bb89e | 2006-12-13 00:35:25 -0800 | [diff] [blame] | 182 | error = nfserrno(PTR_ERR(exp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | goto out; |
J.Bruce Fields | e0bb89e | 2006-12-13 00:35:25 -0800 | [diff] [blame] | 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 186 | error = nfsd_setuser_and_check_port(rqstp, exp); |
NeilBrown | d1bbf14 | 2006-07-30 03:03:16 -0700 | [diff] [blame] | 187 | if (error) |
| 188 | goto out; |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* |
| 191 | * Look up the dentry using the NFS file handle. |
| 192 | */ |
| 193 | error = nfserr_stale; |
| 194 | if (rqstp->rq_vers > 2) |
| 195 | error = nfserr_badhandle; |
| 196 | |
| 197 | if (fh->fh_version != 1) { |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 198 | sfid.i32.ino = fh->ofh_ino; |
| 199 | sfid.i32.gen = fh->ofh_generation; |
| 200 | sfid.i32.parent_ino = fh->ofh_dirino; |
| 201 | fid = &sfid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | data_left = 3; |
| 203 | if (fh->ofh_dirino == 0) |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 204 | fileid_type = FILEID_INO32_GEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | else |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 206 | fileid_type = FILEID_INO32_GEN_PARENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } else |
| 208 | fileid_type = fh->fh_fileid_type; |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 209 | |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 210 | if (fileid_type == FILEID_ROOT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | dentry = dget(exp->ex_dentry); |
| 212 | else { |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 213 | dentry = exportfs_decode_fh(exp->ex_mnt, fid, |
Christoph Hellwig | d37065c | 2007-07-17 04:04:30 -0700 | [diff] [blame] | 214 | data_left, fileid_type, |
| 215 | nfsd_acceptable, exp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | if (dentry == NULL) |
| 218 | goto out; |
| 219 | if (IS_ERR(dentry)) { |
| 220 | if (PTR_ERR(dentry) != -EINVAL) |
| 221 | error = nfserrno(PTR_ERR(dentry)); |
| 222 | goto out; |
| 223 | } |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | if (S_ISDIR(dentry->d_inode->i_mode) && |
| 226 | (dentry->d_flags & DCACHE_DISCONNECTED)) { |
| 227 | printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n", |
| 228 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | fhp->fh_dentry = dentry; |
| 232 | fhp->fh_export = exp; |
| 233 | nfsd_nr_verified++; |
| 234 | } else { |
J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 235 | /* |
| 236 | * just rechecking permissions |
| 237 | * (e.g. nfsproc_create calls fh_verify, then nfsd_create |
| 238 | * does as well) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | */ |
| 240 | dprintk("nfsd: fh_verify - just checking\n"); |
| 241 | dentry = fhp->fh_dentry; |
| 242 | exp = fhp->fh_export; |
J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 243 | /* |
| 244 | * Set user creds for this exportpoint; necessary even |
NeilBrown | d1bbf14 | 2006-07-30 03:03:16 -0700 | [diff] [blame] | 245 | * in the "just checking" case because this may be a |
| 246 | * filehandle that was created by fh_compose, and that |
| 247 | * is about to be used in another nfsv4 compound |
J. Bruce Fields | 6fa0283 | 2007-11-12 16:05:03 -0500 | [diff] [blame] | 248 | * operation. |
| 249 | */ |
| 250 | error = nfsd_setuser_and_check_port(rqstp, exp); |
NeilBrown | d1bbf14 | 2006-07-30 03:03:16 -0700 | [diff] [blame] | 251 | if (error) |
| 252 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | cache_get(&exp->h); |
| 255 | |
J. Bruce Fields | 7fc90ec | 2006-06-30 01:56:14 -0700 | [diff] [blame] | 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type); |
| 258 | if (error) |
| 259 | goto out; |
| 260 | |
J. Bruce Fields | 9091224 | 2007-07-17 04:04:52 -0700 | [diff] [blame] | 261 | if (!(access & MAY_LOCK)) { |
| 262 | /* |
| 263 | * pseudoflavor restrictions are not enforced on NLM, |
| 264 | * which clients virtually always use auth_sys for, |
| 265 | * even while using RPCSEC_GSS for NFS. |
| 266 | */ |
| 267 | error = check_nfsd_access(exp, rqstp); |
| 268 | if (error) |
| 269 | goto out; |
| 270 | } |
Andy Adamson | 32c1eb0 | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | /* Finally, check access permissions. */ |
J. Bruce Fields | 0ec757d | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 273 | error = nfsd_permission(rqstp, exp, dentry, access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | if (error) { |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 276 | dprintk("fh_verify: %s/%s permission failure, " |
| 277 | "acc=%x, error=%d\n", |
| 278 | dentry->d_parent->d_name.name, |
| 279 | dentry->d_name.name, |
Al Viro | fc2dd2e | 2007-02-01 13:52:43 +0000 | [diff] [blame] | 280 | access, ntohl(error)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | out: |
| 283 | if (exp && !IS_ERR(exp)) |
| 284 | exp_put(exp); |
| 285 | if (error == nfserr_stale) |
| 286 | nfsdstats.fh_stale++; |
| 287 | return error; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | /* |
| 292 | * Compose a file handle for an NFS reply. |
| 293 | * |
| 294 | * Note that when first composed, the dentry may not yet have |
| 295 | * an inode. In this case a call to fh_update should be made |
| 296 | * before the fh goes out on the wire ... |
| 297 | */ |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 298 | static void _fh_update(struct svc_fh *fhp, struct svc_export *exp, |
| 299 | struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 301 | if (dentry != exp->ex_dentry) { |
| 302 | struct fid *fid = (struct fid *) |
| 303 | (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1); |
| 304 | int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4; |
| 305 | int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 307 | fhp->fh_handle.fh_fileid_type = |
| 308 | exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck); |
| 309 | fhp->fh_handle.fh_size += maxsize * 4; |
| 310 | } else { |
| 311 | fhp->fh_handle.fh_fileid_type = FILEID_ROOT; |
| 312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* |
| 316 | * for composing old style file handles |
| 317 | */ |
| 318 | static inline void _fh_update_old(struct dentry *dentry, |
| 319 | struct svc_export *exp, |
| 320 | struct knfsd_fh *fh) |
| 321 | { |
| 322 | fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino); |
| 323 | fh->ofh_generation = dentry->d_inode->i_generation; |
| 324 | if (S_ISDIR(dentry->d_inode->i_mode) || |
| 325 | (exp->ex_flags & NFSEXP_NOSUBTREECHECK)) |
| 326 | fh->ofh_dirino = 0; |
| 327 | } |
| 328 | |
Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 329 | __be32 |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 330 | fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry, |
| 331 | struct svc_fh *ref_fh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
| 333 | /* ref_fh is a reference file handle. |
NeilBrown | 7e40536 | 2006-06-30 01:56:12 -0700 | [diff] [blame] | 334 | * if it is non-null and for the same filesystem, then we should compose |
| 335 | * a filehandle which is of the same version, where possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca |
| 337 | * Then create a 32byte filehandle using nfs_fhbase_old |
| 338 | * |
| 339 | */ |
| 340 | |
NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 341 | u8 version; |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 342 | u8 fsid_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | struct inode * inode = dentry->d_inode; |
| 344 | struct dentry *parent = dentry->d_parent; |
| 345 | __u32 *datap; |
| 346 | dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev; |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 347 | int root_export = (exp->ex_dentry == exp->ex_dentry->d_sb->s_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n", |
| 350 | MAJOR(ex_dev), MINOR(ex_dev), |
| 351 | (long) exp->ex_dentry->d_inode->i_ino, |
| 352 | parent->d_name.name, dentry->d_name.name, |
| 353 | (inode ? inode->i_ino : 0)); |
| 354 | |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 355 | /* Choose filehandle version and fsid type based on |
| 356 | * the reference filehandle (if it is in the same export) |
| 357 | * or the export options. |
| 358 | */ |
NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 359 | retry: |
| 360 | version = 1; |
NeilBrown | 7e40536 | 2006-06-30 01:56:12 -0700 | [diff] [blame] | 361 | if (ref_fh && ref_fh->fh_export == exp) { |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 362 | version = ref_fh->fh_handle.fh_version; |
NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 363 | fsid_type = ref_fh->fh_handle.fh_fsid_type; |
| 364 | |
| 365 | if (ref_fh == fhp) |
| 366 | fh_put(ref_fh); |
| 367 | ref_fh = NULL; |
| 368 | |
| 369 | switch (version) { |
| 370 | case 0xca: |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 371 | fsid_type = FSID_DEV; |
NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 372 | break; |
| 373 | case 1: |
| 374 | break; |
| 375 | default: |
| 376 | goto retry; |
| 377 | } |
| 378 | |
| 379 | /* Need to check that this type works for this |
| 380 | * export point. As the fsid -> filesystem mapping |
| 381 | * was guided by user-space, there is no guarantee |
| 382 | * that the filesystem actually supports that fsid |
| 383 | * type. If it doesn't we loop around again without |
| 384 | * ref_fh set. |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 385 | */ |
NeilBrown | b41eeef | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 386 | switch(fsid_type) { |
| 387 | case FSID_DEV: |
| 388 | if (!old_valid_dev(ex_dev)) |
| 389 | goto retry; |
| 390 | /* FALL THROUGH */ |
| 391 | case FSID_MAJOR_MINOR: |
| 392 | case FSID_ENCODE_DEV: |
| 393 | if (!(exp->ex_dentry->d_inode->i_sb->s_type->fs_flags |
| 394 | & FS_REQUIRES_DEV)) |
| 395 | goto retry; |
| 396 | break; |
| 397 | case FSID_NUM: |
| 398 | if (! (exp->ex_flags & NFSEXP_FSID)) |
| 399 | goto retry; |
| 400 | break; |
| 401 | case FSID_UUID8: |
| 402 | case FSID_UUID16: |
| 403 | if (!root_export) |
| 404 | goto retry; |
| 405 | /* fall through */ |
| 406 | case FSID_UUID4_INUM: |
| 407 | case FSID_UUID16_INUM: |
| 408 | if (exp->ex_uuid == NULL) |
| 409 | goto retry; |
| 410 | break; |
| 411 | } |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 412 | } else if (exp->ex_uuid) { |
| 413 | if (fhp->fh_maxsize >= 64) { |
| 414 | if (root_export) |
| 415 | fsid_type = FSID_UUID16; |
| 416 | else |
| 417 | fsid_type = FSID_UUID16_INUM; |
| 418 | } else { |
| 419 | if (root_export) |
| 420 | fsid_type = FSID_UUID8; |
| 421 | else |
| 422 | fsid_type = FSID_UUID4_INUM; |
| 423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } else if (exp->ex_flags & NFSEXP_FSID) |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 425 | fsid_type = FSID_NUM; |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 426 | else if (!old_valid_dev(ex_dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | /* for newer device numbers, we must use a newer fsid format */ |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 428 | fsid_type = FSID_ENCODE_DEV; |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 429 | else |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 430 | fsid_type = FSID_DEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | if (ref_fh == fhp) |
| 433 | fh_put(ref_fh); |
| 434 | |
| 435 | if (fhp->fh_locked || fhp->fh_dentry) { |
| 436 | printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n", |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 437 | parent->d_name.name, dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | if (fhp->fh_maxsize < NFS_FHSIZE) |
| 440 | printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n", |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 441 | fhp->fh_maxsize, |
| 442 | parent->d_name.name, dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
| 444 | fhp->fh_dentry = dget(dentry); /* our internal copy */ |
| 445 | fhp->fh_export = exp; |
| 446 | cache_get(&exp->h); |
| 447 | |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 448 | if (version == 0xca) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | /* old style filehandle please */ |
| 450 | memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE); |
| 451 | fhp->fh_handle.fh_size = NFS_FHSIZE; |
| 452 | fhp->fh_handle.ofh_dcookie = 0xfeebbaca; |
| 453 | fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev); |
| 454 | fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev; |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 455 | fhp->fh_handle.ofh_xino = |
| 456 | ino_t_to_u32(exp->ex_dentry->d_inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry)); |
| 458 | if (inode) |
| 459 | _fh_update_old(dentry, exp, &fhp->fh_handle); |
| 460 | } else { |
| 461 | int len; |
| 462 | fhp->fh_handle.fh_version = 1; |
| 463 | fhp->fh_handle.fh_auth_type = 0; |
| 464 | datap = fhp->fh_handle.fh_auth+0; |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 465 | fhp->fh_handle.fh_fsid_type = fsid_type; |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 466 | mk_fsid(fsid_type, datap, ex_dev, |
| 467 | exp->ex_dentry->d_inode->i_ino, |
| 468 | exp->ex_fsid, exp->ex_uuid); |
| 469 | |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 470 | len = key_len(fsid_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | datap += len/4; |
| 472 | fhp->fh_handle.fh_size = 4 + len; |
| 473 | |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 474 | if (inode) |
| 475 | _fh_update(fhp, exp, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | if (fhp->fh_handle.fh_fileid_type == 255) |
| 477 | return nfserr_opnotsupp; |
| 478 | } |
| 479 | |
| 480 | nfsd_nr_verified++; |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Update file handle information after changing a dentry. |
| 486 | * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create |
| 487 | */ |
Al Viro | 83b1134 | 2006-10-19 23:28:55 -0700 | [diff] [blame] | 488 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | fh_update(struct svc_fh *fhp) |
| 490 | { |
| 491 | struct dentry *dentry; |
NeilBrown | 982aedf | 2007-02-14 00:33:11 -0800 | [diff] [blame] | 492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | if (!fhp->fh_dentry) |
| 494 | goto out_bad; |
| 495 | |
| 496 | dentry = fhp->fh_dentry; |
| 497 | if (!dentry->d_inode) |
| 498 | goto out_negative; |
| 499 | if (fhp->fh_handle.fh_version != 1) { |
| 500 | _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle); |
| 501 | } else { |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 502 | if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT) |
NeilBrown | 4c9608b | 2006-06-30 01:56:11 -0700 | [diff] [blame] | 503 | goto out; |
Christoph Hellwig | 6e91ea2 | 2007-10-21 16:42:03 -0700 | [diff] [blame] | 504 | |
| 505 | _fh_update(fhp, fhp->fh_export, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | if (fhp->fh_handle.fh_fileid_type == 255) |
| 507 | return nfserr_opnotsupp; |
| 508 | } |
| 509 | out: |
| 510 | return 0; |
| 511 | |
| 512 | out_bad: |
| 513 | printk(KERN_ERR "fh_update: fh not verified!\n"); |
| 514 | goto out; |
| 515 | out_negative: |
| 516 | printk(KERN_ERR "fh_update: %s/%s still negative!\n", |
| 517 | dentry->d_parent->d_name.name, dentry->d_name.name); |
| 518 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Release a file handle. |
| 523 | */ |
| 524 | void |
| 525 | fh_put(struct svc_fh *fhp) |
| 526 | { |
| 527 | struct dentry * dentry = fhp->fh_dentry; |
| 528 | struct svc_export * exp = fhp->fh_export; |
| 529 | if (dentry) { |
| 530 | fh_unlock(fhp); |
| 531 | fhp->fh_dentry = NULL; |
| 532 | dput(dentry); |
| 533 | #ifdef CONFIG_NFSD_V3 |
| 534 | fhp->fh_pre_saved = 0; |
| 535 | fhp->fh_post_saved = 0; |
| 536 | #endif |
| 537 | nfsd_nr_put++; |
| 538 | } |
| 539 | if (exp) { |
NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 540 | cache_put(&exp->h, &svc_export_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | fhp->fh_export = NULL; |
| 542 | } |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | * Shorthand for dprintk()'s |
| 548 | */ |
| 549 | char * SVCFH_fmt(struct svc_fh *fhp) |
| 550 | { |
| 551 | struct knfsd_fh *fh = &fhp->fh_handle; |
| 552 | |
| 553 | static char buf[80]; |
| 554 | sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x", |
| 555 | fh->fh_size, |
| 556 | fh->fh_base.fh_pad[0], |
| 557 | fh->fh_base.fh_pad[1], |
| 558 | fh->fh_base.fh_pad[2], |
| 559 | fh->fh_base.fh_pad[3], |
| 560 | fh->fh_base.fh_pad[4], |
| 561 | fh->fh_base.fh_pad[5]); |
| 562 | return buf; |
| 563 | } |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 564 | |
| 565 | enum fsid_source fsid_source(struct svc_fh *fhp) |
| 566 | { |
| 567 | if (fhp->fh_handle.fh_version != 1) |
| 568 | return FSIDSOURCE_DEV; |
| 569 | switch(fhp->fh_handle.fh_fsid_type) { |
| 570 | case FSID_DEV: |
| 571 | case FSID_ENCODE_DEV: |
| 572 | case FSID_MAJOR_MINOR: |
Neil Brown | b8da0d1 | 2007-09-05 17:22:13 -0400 | [diff] [blame] | 573 | if (fhp->fh_export->ex_dentry->d_inode->i_sb->s_type->fs_flags |
| 574 | & FS_REQUIRES_DEV) |
| 575 | return FSIDSOURCE_DEV; |
| 576 | break; |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 577 | case FSID_NUM: |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 578 | if (fhp->fh_export->ex_flags & NFSEXP_FSID) |
| 579 | return FSIDSOURCE_FSID; |
Neil Brown | b8da0d1 | 2007-09-05 17:22:13 -0400 | [diff] [blame] | 580 | break; |
| 581 | default: |
| 582 | break; |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 583 | } |
Neil Brown | b8da0d1 | 2007-09-05 17:22:13 -0400 | [diff] [blame] | 584 | /* either a UUID type filehandle, or the filehandle doesn't |
| 585 | * match the export. |
| 586 | */ |
| 587 | if (fhp->fh_export->ex_flags & NFSEXP_FSID) |
| 588 | return FSIDSOURCE_FSID; |
| 589 | if (fhp->fh_export->ex_uuid) |
| 590 | return FSIDSOURCE_UUID; |
| 591 | return FSIDSOURCE_DEV; |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 592 | } |