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