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