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