Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * File operations used by nfsd. Some of these have been ripped from |
| 3 | * other parts of the kernel because they weren't exported, others |
| 4 | * are partial duplicates with added or changed functionality. |
| 5 | * |
| 6 | * Note that several functions dget() the dentry upon which they want |
| 7 | * to act, most notably those that create directory entries. Response |
| 8 | * dentry's are dput()'d if necessary in the release callback. |
| 9 | * So if you notice code paths that apparently fail to dput() the |
| 10 | * dentry, don't worry--they have been taken care of. |
| 11 | * |
| 12 | * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de> |
| 13 | * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp> |
| 14 | */ |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/fs.h> |
| 17 | #include <linux/file.h> |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 18 | #include <linux/splice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/fcntl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/delay.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 22 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/posix_acl_xattr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/xattr.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 25 | #include <linux/jhash.h> |
| 26 | #include <linux/ima.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 28 | #include <asm/uaccess.h> |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 29 | #include <linux/exportfs.h> |
| 30 | #include <linux/writeback.h> |
David Quigley | 18032ca | 2013-05-02 13:19:10 -0400 | [diff] [blame] | 31 | #include <linux/security.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 32 | |
| 33 | #ifdef CONFIG_NFSD_V3 |
| 34 | #include "xdr3.h" |
| 35 | #endif /* CONFIG_NFSD_V3 */ |
| 36 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 37 | #ifdef CONFIG_NFSD_V4 |
J. Bruce Fields | 2ca72e1 | 2011-01-04 17:37:15 -0500 | [diff] [blame] | 38 | #include "acl.h" |
| 39 | #include "idmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #endif /* CONFIG_NFSD_V4 */ |
| 41 | |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 42 | #include "nfsd.h" |
| 43 | #include "vfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | #define NFSDDBG_FACILITY NFSDDBG_FILEOP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* |
| 49 | * This is a cache of readahead params that help us choose the proper |
| 50 | * readahead strategy. Initially, we set all readahead parameters to 0 |
| 51 | * and let the VFS handle things. |
| 52 | * If you increase the number of cached files very much, you'll need to |
| 53 | * add a hash table here. |
| 54 | */ |
| 55 | struct raparms { |
| 56 | struct raparms *p_next; |
| 57 | unsigned int p_count; |
| 58 | ino_t p_ino; |
| 59 | dev_t p_dev; |
| 60 | int p_set; |
| 61 | struct file_ra_state p_ra; |
Greg Banks | fce1456 | 2006-10-04 02:15:49 -0700 | [diff] [blame] | 62 | unsigned int p_hindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Greg Banks | fce1456 | 2006-10-04 02:15:49 -0700 | [diff] [blame] | 65 | struct raparm_hbucket { |
| 66 | struct raparms *pb_head; |
| 67 | spinlock_t pb_lock; |
| 68 | } ____cacheline_aligned_in_smp; |
| 69 | |
Greg Banks | fce1456 | 2006-10-04 02:15:49 -0700 | [diff] [blame] | 70 | #define RAPARM_HASH_BITS 4 |
| 71 | #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS) |
| 72 | #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1) |
| 73 | static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Called from nfsd_lookup and encode_dirent. Check if we have crossed |
| 77 | * a mount point. |
J.Bruce Fields | e0bb89e | 2006-12-13 00:35:25 -0800 | [diff] [blame] | 78 | * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | * or nfs_ok having possibly changed *dpp and *expp |
| 80 | */ |
| 81 | int |
| 82 | nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp, |
| 83 | struct svc_export **expp) |
| 84 | { |
| 85 | struct svc_export *exp = *expp, *exp2 = NULL; |
| 86 | struct dentry *dentry = *dpp; |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 87 | struct path path = {.mnt = mntget(exp->ex_path.mnt), |
| 88 | .dentry = dget(dentry)}; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 89 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Al Viro | 7cc90cc | 2011-03-18 09:04:20 -0400 | [diff] [blame] | 91 | err = follow_down(&path); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 92 | if (err < 0) |
| 93 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 95 | exp2 = rqst_exp_get_by_name(rqstp, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | if (IS_ERR(exp2)) { |
J. Bruce Fields | 3b6cee7bc | 2009-10-25 21:18:19 -0400 | [diff] [blame] | 97 | err = PTR_ERR(exp2); |
| 98 | /* |
| 99 | * We normally allow NFS clients to continue |
| 100 | * "underneath" a mountpoint that is not exported. |
| 101 | * The exception is V4ROOT, where no traversal is ever |
| 102 | * allowed without an explicit export of the new |
| 103 | * directory. |
| 104 | */ |
| 105 | if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT)) |
| 106 | err = 0; |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 107 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | goto out; |
| 109 | } |
Steve Dickson | 3c394dd | 2009-09-09 15:02:40 -0400 | [diff] [blame] | 110 | if (nfsd_v4client(rqstp) || |
| 111 | (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /* successfully crossed mount point */ |
Al Viro | 1644ccc | 2009-04-18 02:32:31 -0400 | [diff] [blame] | 113 | /* |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 114 | * This is subtle: path.dentry is *not* on path.mnt |
| 115 | * at this point. The only reason we are safe is that |
| 116 | * original mnt is pinned down by exp, so we should |
| 117 | * put path *before* putting exp |
Al Viro | 1644ccc | 2009-04-18 02:32:31 -0400 | [diff] [blame] | 118 | */ |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 119 | *dpp = path.dentry; |
| 120 | path.dentry = dentry; |
Al Viro | 1644ccc | 2009-04-18 02:32:31 -0400 | [diff] [blame] | 121 | *expp = exp2; |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 122 | exp2 = exp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 124 | path_put(&path); |
| 125 | exp_put(exp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | out: |
| 127 | return err; |
| 128 | } |
| 129 | |
J. Bruce Fields | 289ede4 | 2009-09-26 20:32:24 -0400 | [diff] [blame] | 130 | static void follow_to_parent(struct path *path) |
| 131 | { |
| 132 | struct dentry *dp; |
| 133 | |
| 134 | while (path->dentry == path->mnt->mnt_root && follow_up(path)) |
| 135 | ; |
| 136 | dp = dget_parent(path->dentry); |
| 137 | dput(path->dentry); |
| 138 | path->dentry = dp; |
| 139 | } |
| 140 | |
| 141 | static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp) |
| 142 | { |
| 143 | struct svc_export *exp2; |
| 144 | struct path path = {.mnt = mntget((*exp)->ex_path.mnt), |
| 145 | .dentry = dget(dparent)}; |
| 146 | |
| 147 | follow_to_parent(&path); |
| 148 | |
| 149 | exp2 = rqst_exp_parent(rqstp, &path); |
| 150 | if (PTR_ERR(exp2) == -ENOENT) { |
| 151 | *dentryp = dget(dparent); |
| 152 | } else if (IS_ERR(exp2)) { |
| 153 | path_put(&path); |
| 154 | return PTR_ERR(exp2); |
| 155 | } else { |
| 156 | *dentryp = dget(path.dentry); |
| 157 | exp_put(*exp); |
| 158 | *exp = exp2; |
| 159 | } |
| 160 | path_put(&path); |
| 161 | return 0; |
| 162 | } |
| 163 | |
J. Bruce Fields | 82ead7f | 2009-10-25 21:33:15 -0400 | [diff] [blame] | 164 | /* |
| 165 | * For nfsd purposes, we treat V4ROOT exports as though there was an |
| 166 | * export at *every* directory. |
| 167 | */ |
J. Bruce Fields | 3227fa4 | 2009-10-25 21:43:01 -0400 | [diff] [blame] | 168 | int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp) |
J. Bruce Fields | 82ead7f | 2009-10-25 21:33:15 -0400 | [diff] [blame] | 169 | { |
| 170 | if (d_mountpoint(dentry)) |
| 171 | return 1; |
Trond Myklebust | 11fcee02 | 2011-09-12 19:37:26 -0400 | [diff] [blame] | 172 | if (nfsd4_is_junction(dentry)) |
| 173 | return 1; |
J. Bruce Fields | 82ead7f | 2009-10-25 21:33:15 -0400 | [diff] [blame] | 174 | if (!(exp->ex_flags & NFSEXP_V4ROOT)) |
| 175 | return 0; |
| 176 | return dentry->d_inode != NULL; |
| 177 | } |
| 178 | |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 179 | __be32 |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 180 | nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp, |
Chuck Lever | 5a022fc | 2007-11-01 16:57:09 -0400 | [diff] [blame] | 181 | const char *name, unsigned int len, |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 182 | struct svc_export **exp_ret, struct dentry **dentry_ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
| 184 | struct svc_export *exp; |
| 185 | struct dentry *dparent; |
| 186 | struct dentry *dentry; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 187 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name); |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | dparent = fhp->fh_dentry; |
Kinglong Mee | bf18f16 | 2014-06-10 22:06:44 +0800 | [diff] [blame] | 192 | exp = exp_get(fhp->fh_export); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | /* Lookup the name, but don't follow links */ |
| 195 | if (isdotent(name, len)) { |
| 196 | if (len==1) |
| 197 | dentry = dget(dparent); |
Jan Blunck | 5477549 | 2008-02-14 19:38:39 -0800 | [diff] [blame] | 198 | else if (dparent != exp->ex_path.dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | dentry = dget_parent(dparent); |
J. Bruce Fields | fed8381 | 2009-09-26 16:53:01 -0400 | [diff] [blame] | 200 | else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | dentry = dget(dparent); /* .. == . just like at / */ |
| 202 | else { |
| 203 | /* checking mountpoint crossing is very different when stepping up */ |
J. Bruce Fields | 289ede4 | 2009-09-26 20:32:24 -0400 | [diff] [blame] | 204 | host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry); |
| 205 | if (host_err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | goto out_nfserr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | } else { |
J. Bruce Fields | 4335723 | 2014-01-24 18:04:40 -0500 | [diff] [blame] | 209 | /* |
| 210 | * In the nfsd4_open() case, this may be held across |
| 211 | * subsequent open and delegation acquisition which may |
| 212 | * need to take the child's i_mutex: |
| 213 | */ |
| 214 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | dentry = lookup_one_len(name, dparent, len); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 216 | host_err = PTR_ERR(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (IS_ERR(dentry)) |
| 218 | goto out_nfserr; |
| 219 | /* |
| 220 | * check if we have crossed a mount point ... |
| 221 | */ |
J. Bruce Fields | 82ead7f | 2009-10-25 21:33:15 -0400 | [diff] [blame] | 222 | if (nfsd_mountpoint(dentry, exp)) { |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 223 | if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | dput(dentry); |
| 225 | goto out_nfserr; |
| 226 | } |
| 227 | } |
| 228 | } |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 229 | *dentry_ret = dentry; |
| 230 | *exp_ret = exp; |
| 231 | return 0; |
| 232 | |
| 233 | out_nfserr: |
| 234 | exp_put(exp); |
| 235 | return nfserrno(host_err); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Look up one component of a pathname. |
| 240 | * N.B. After this call _both_ fhp and resfh need an fh_put |
| 241 | * |
| 242 | * If the lookup would cross a mountpoint, and the mounted filesystem |
| 243 | * is exported to the client with NFSEXP_NOHIDE, then the lookup is |
| 244 | * accepted as it stands and the mounted directory is |
| 245 | * returned. Otherwise the covered directory is returned. |
| 246 | * NOTE: this mountpoint crossing is not supported properly by all |
| 247 | * clients and is explicitly disallowed for NFSv3 |
| 248 | * NeilBrown <neilb@cse.unsw.edu.au> |
| 249 | */ |
| 250 | __be32 |
| 251 | nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name, |
Chuck Lever | 5a022fc | 2007-11-01 16:57:09 -0400 | [diff] [blame] | 252 | unsigned int len, struct svc_fh *resfh) |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 253 | { |
| 254 | struct svc_export *exp; |
| 255 | struct dentry *dentry; |
| 256 | __be32 err; |
| 257 | |
J. Bruce Fields | 29a78a3 | 2011-04-09 11:28:53 -0400 | [diff] [blame] | 258 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC); |
| 259 | if (err) |
| 260 | return err; |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 261 | err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry); |
| 262 | if (err) |
| 263 | return err; |
Andy Adamson | 32c1eb0 | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 264 | err = check_nfsd_access(exp, rqstp); |
| 265 | if (err) |
| 266 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | /* |
| 268 | * Note: we compose the file handle now, but as the |
| 269 | * dentry may be negative, it may need to be updated. |
| 270 | */ |
| 271 | err = fh_compose(resfh, exp, dentry, fhp); |
| 272 | if (!err && !dentry->d_inode) |
| 273 | err = nfserr_noent; |
Andy Adamson | 32c1eb0 | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 274 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | exp_put(exp); |
| 277 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 280 | /* |
| 281 | * Commit metadata changes to stable storage. |
| 282 | */ |
| 283 | static int |
| 284 | commit_metadata(struct svc_fh *fhp) |
| 285 | { |
| 286 | struct inode *inode = fhp->fh_dentry->d_inode; |
| 287 | const struct export_operations *export_ops = inode->i_sb->s_export_op; |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 288 | |
| 289 | if (!EX_ISSYNC(fhp->fh_export)) |
| 290 | return 0; |
| 291 | |
Christoph Hellwig | c3765016 | 2010-10-06 10:48:20 +0200 | [diff] [blame] | 292 | if (export_ops->commit_metadata) |
| 293 | return export_ops->commit_metadata(inode); |
| 294 | return sync_inode_metadata(inode, 1); |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 295 | } |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | /* |
Christoph Hellwig | 818e5a2 | 2013-11-18 05:07:30 -0800 | [diff] [blame] | 298 | * Go over the attributes and take care of the small differences between |
| 299 | * NFS semantics and what Linux expects. |
| 300 | */ |
| 301 | static void |
| 302 | nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap) |
| 303 | { |
| 304 | /* |
| 305 | * NFSv2 does not differentiate between "set-[ac]time-to-now" |
| 306 | * which only requires access, and "set-[ac]time-to-X" which |
| 307 | * requires ownership. |
| 308 | * So if it looks like it might be "set both to the same time which |
| 309 | * is close to now", and if inode_change_ok fails, then we |
| 310 | * convert to "set to now" instead of "set to explicit time" |
| 311 | * |
| 312 | * We only call inode_change_ok as the last test as technically |
| 313 | * it is not an interface that we should be using. |
| 314 | */ |
| 315 | #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET) |
| 316 | #define MAX_TOUCH_TIME_ERROR (30*60) |
| 317 | if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET && |
| 318 | iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) { |
| 319 | /* |
| 320 | * Looks probable. |
| 321 | * |
| 322 | * Now just make sure time is in the right ballpark. |
| 323 | * Solaris, at least, doesn't seem to care what the time |
| 324 | * request is. We require it be within 30 minutes of now. |
| 325 | */ |
| 326 | time_t delta = iap->ia_atime.tv_sec - get_seconds(); |
| 327 | if (delta < 0) |
| 328 | delta = -delta; |
| 329 | if (delta < MAX_TOUCH_TIME_ERROR && |
| 330 | inode_change_ok(inode, iap) != 0) { |
| 331 | /* |
| 332 | * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME. |
| 333 | * This will cause notify_change to set these times |
| 334 | * to "now" |
| 335 | */ |
| 336 | iap->ia_valid &= ~BOTH_TIME_SET; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /* sanitize the mode change */ |
| 341 | if (iap->ia_valid & ATTR_MODE) { |
| 342 | iap->ia_mode &= S_IALLUGO; |
| 343 | iap->ia_mode |= (inode->i_mode & ~S_IALLUGO); |
| 344 | } |
| 345 | |
| 346 | /* Revoke setuid/setgid on chown */ |
| 347 | if (!S_ISDIR(inode->i_mode) && |
Stanislav Kholmanskikh | c4fa6d7 | 2013-12-11 14:16:36 +0400 | [diff] [blame] | 348 | ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) { |
Christoph Hellwig | 818e5a2 | 2013-11-18 05:07:30 -0800 | [diff] [blame] | 349 | iap->ia_valid |= ATTR_KILL_PRIV; |
| 350 | if (iap->ia_valid & ATTR_MODE) { |
| 351 | /* we're setting mode too, just clear the s*id bits */ |
| 352 | iap->ia_mode &= ~S_ISUID; |
| 353 | if (iap->ia_mode & S_IXGRP) |
| 354 | iap->ia_mode &= ~S_ISGID; |
| 355 | } else { |
| 356 | /* set ATTR_KILL_* bits and let VFS handle it */ |
| 357 | iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | static __be32 |
| 363 | nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 364 | struct iattr *iap) |
| 365 | { |
| 366 | struct inode *inode = fhp->fh_dentry->d_inode; |
| 367 | int host_err; |
| 368 | |
| 369 | if (iap->ia_size < inode->i_size) { |
| 370 | __be32 err; |
| 371 | |
| 372 | err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
| 373 | NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE); |
| 374 | if (err) |
| 375 | return err; |
| 376 | } |
| 377 | |
| 378 | host_err = get_write_access(inode); |
| 379 | if (host_err) |
| 380 | goto out_nfserrno; |
| 381 | |
| 382 | host_err = locks_verify_truncate(inode, NULL, iap->ia_size); |
| 383 | if (host_err) |
| 384 | goto out_put_write_access; |
| 385 | return 0; |
| 386 | |
| 387 | out_put_write_access: |
| 388 | put_write_access(inode); |
| 389 | out_nfserrno: |
| 390 | return nfserrno(host_err); |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Set various file attributes. After this call fhp needs an fh_put. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 396 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap, |
| 398 | int check_guard, time_t guardtime) |
| 399 | { |
| 400 | struct dentry *dentry; |
| 401 | struct inode *inode; |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 402 | int accmode = NFSD_MAY_SATTR; |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 403 | umode_t ftype = 0; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 404 | __be32 err; |
| 405 | int host_err; |
J. Bruce Fields | 9f67f18 | 2014-02-24 14:59:47 -0500 | [diff] [blame] | 406 | bool get_write_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | int size_change = 0; |
| 408 | |
| 409 | if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE)) |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 410 | accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | if (iap->ia_valid & ATTR_SIZE) |
| 412 | ftype = S_IFREG; |
| 413 | |
J. Bruce Fields | 9f67f18 | 2014-02-24 14:59:47 -0500 | [diff] [blame] | 414 | /* Callers that do fh_verify should do the fh_want_write: */ |
| 415 | get_write_count = !fhp->fh_dentry; |
| 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | /* Get inode */ |
| 418 | err = fh_verify(rqstp, fhp, ftype, accmode); |
NeilBrown | 15b7a1b | 2005-11-07 01:00:23 -0800 | [diff] [blame] | 419 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | goto out; |
J. Bruce Fields | 9f67f18 | 2014-02-24 14:59:47 -0500 | [diff] [blame] | 421 | if (get_write_count) { |
| 422 | host_err = fh_want_write(fhp); |
| 423 | if (host_err) |
| 424 | return nfserrno(host_err); |
| 425 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | dentry = fhp->fh_dentry; |
| 428 | inode = dentry->d_inode; |
| 429 | |
NeilBrown | 15b7a1b | 2005-11-07 01:00:23 -0800 | [diff] [blame] | 430 | /* Ignore any mode updates on symlinks */ |
| 431 | if (S_ISLNK(inode->i_mode)) |
| 432 | iap->ia_valid &= ~ATTR_MODE; |
| 433 | |
| 434 | if (!iap->ia_valid) |
| 435 | goto out; |
| 436 | |
Christoph Hellwig | 818e5a2 | 2013-11-18 05:07:30 -0800 | [diff] [blame] | 437 | nfsd_sanitize_attrs(inode, iap); |
| 438 | |
Christoph Hellwig | 9c85fca | 2007-03-07 15:26:25 +0000 | [diff] [blame] | 439 | /* |
Christoph Hellwig | 818e5a2 | 2013-11-18 05:07:30 -0800 | [diff] [blame] | 440 | * The size case is special, it changes the file in addition to the |
| 441 | * attributes. |
Christoph Hellwig | 9c85fca | 2007-03-07 15:26:25 +0000 | [diff] [blame] | 442 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | if (iap->ia_valid & ATTR_SIZE) { |
Christoph Hellwig | 818e5a2 | 2013-11-18 05:07:30 -0800 | [diff] [blame] | 444 | err = nfsd_get_write_access(rqstp, fhp, iap); |
| 445 | if (err) |
| 446 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | size_change = 1; |
Christoph Hellwig | f0c6312 | 2014-09-07 12:15:52 -0700 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * RFC5661, Section 18.30.4: |
| 451 | * Changing the size of a file with SETATTR indirectly |
| 452 | * changes the time_modify and change attributes. |
| 453 | * |
| 454 | * (and similar for the older RFCs) |
| 455 | */ |
| 456 | if (iap->ia_size != i_size_read(inode)) |
| 457 | iap->ia_valid |= ATTR_MTIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | iap->ia_valid |= ATTR_CTIME; |
| 461 | |
Christoph Hellwig | 987da47 | 2013-11-18 05:07:47 -0800 | [diff] [blame] | 462 | if (check_guard && guardtime != inode->i_ctime.tv_sec) { |
| 463 | err = nfserr_notsync; |
| 464 | goto out_put_write_access; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
Christoph Hellwig | 987da47 | 2013-11-18 05:07:47 -0800 | [diff] [blame] | 466 | |
Christoph Hellwig | 987da47 | 2013-11-18 05:07:47 -0800 | [diff] [blame] | 467 | fh_lock(fhp); |
| 468 | host_err = notify_change(dentry, iap, NULL); |
| 469 | fh_unlock(fhp); |
J. R. Okajima | 1406b91 | 2014-02-19 00:27:53 +0900 | [diff] [blame] | 470 | err = nfserrno(host_err); |
Christoph Hellwig | 987da47 | 2013-11-18 05:07:47 -0800 | [diff] [blame] | 471 | |
Christoph Hellwig | 987da47 | 2013-11-18 05:07:47 -0800 | [diff] [blame] | 472 | out_put_write_access: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | if (size_change) |
| 474 | put_write_access(inode); |
| 475 | if (!err) |
Jeff Layton | 722b620 | 2014-07-03 07:54:19 -0400 | [diff] [blame] | 476 | err = nfserrno(commit_metadata(fhp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | out: |
| 478 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 481 | #if defined(CONFIG_NFSD_V4) |
Chuck Lever | 9b4146e | 2012-01-04 16:26:43 -0500 | [diff] [blame] | 482 | /* |
| 483 | * NFS junction information is stored in an extended attribute. |
| 484 | */ |
| 485 | #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs" |
| 486 | |
| 487 | /** |
| 488 | * nfsd4_is_junction - Test if an object could be an NFS junction |
| 489 | * |
| 490 | * @dentry: object to test |
| 491 | * |
| 492 | * Returns 1 if "dentry" appears to contain NFS junction information. |
| 493 | * Otherwise 0 is returned. |
| 494 | */ |
Trond Myklebust | 11fcee02 | 2011-09-12 19:37:26 -0400 | [diff] [blame] | 495 | int nfsd4_is_junction(struct dentry *dentry) |
| 496 | { |
| 497 | struct inode *inode = dentry->d_inode; |
| 498 | |
| 499 | if (inode == NULL) |
| 500 | return 0; |
| 501 | if (inode->i_mode & S_IXUGO) |
| 502 | return 0; |
| 503 | if (!(inode->i_mode & S_ISVTX)) |
| 504 | return 0; |
Chuck Lever | 9b4146e | 2012-01-04 16:26:43 -0500 | [diff] [blame] | 505 | if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0) |
Trond Myklebust | 11fcee02 | 2011-09-12 19:37:26 -0400 | [diff] [blame] | 506 | return 0; |
| 507 | return 1; |
| 508 | } |
David Quigley | 18032ca | 2013-05-02 13:19:10 -0400 | [diff] [blame] | 509 | #ifdef CONFIG_NFSD_V4_SECURITY_LABEL |
| 510 | __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 511 | struct xdr_netobj *label) |
| 512 | { |
| 513 | __be32 error; |
| 514 | int host_error; |
| 515 | struct dentry *dentry; |
| 516 | |
| 517 | error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR); |
| 518 | if (error) |
| 519 | return error; |
| 520 | |
| 521 | dentry = fhp->fh_dentry; |
| 522 | |
| 523 | mutex_lock(&dentry->d_inode->i_mutex); |
| 524 | host_error = security_inode_setsecctx(dentry, label->data, label->len); |
| 525 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 526 | return nfserrno(host_error); |
| 527 | } |
| 528 | #else |
| 529 | __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 530 | struct xdr_netobj *label) |
| 531 | { |
| 532 | return nfserr_notsupp; |
| 533 | } |
| 534 | #endif |
| 535 | |
J. Bruce Fields | 6a85d6c | 2010-07-06 12:39:12 -0400 | [diff] [blame] | 536 | #endif /* defined(CONFIG_NFSD_V4) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | #ifdef CONFIG_NFSD_V3 |
| 539 | /* |
| 540 | * Check server access rights to a file system object |
| 541 | */ |
| 542 | struct accessmap { |
| 543 | u32 access; |
| 544 | int how; |
| 545 | }; |
| 546 | static struct accessmap nfs3_regaccess[] = { |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 547 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
| 548 | { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC }, |
| 549 | { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC }, |
| 550 | { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
| 552 | { 0, 0 } |
| 553 | }; |
| 554 | |
| 555 | static struct accessmap nfs3_diraccess[] = { |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 556 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
| 557 | { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC }, |
| 558 | { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC}, |
| 559 | { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE }, |
| 560 | { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
| 562 | { 0, 0 } |
| 563 | }; |
| 564 | |
| 565 | static struct accessmap nfs3_anyaccess[] = { |
| 566 | /* Some clients - Solaris 2.6 at least, make an access call |
| 567 | * to the server to check for access for things like /dev/null |
| 568 | * (which really, the server doesn't care about). So |
| 569 | * We provide simple access checking for them, looking |
| 570 | * mainly at mode bits, and we make sure to ignore read-only |
| 571 | * filesystem checks |
| 572 | */ |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 573 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
| 574 | { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC }, |
| 575 | { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS }, |
| 576 | { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
| 578 | { 0, 0 } |
| 579 | }; |
| 580 | |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 581 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported) |
| 583 | { |
| 584 | struct accessmap *map; |
| 585 | struct svc_export *export; |
| 586 | struct dentry *dentry; |
| 587 | u32 query, result = 0, sresult = 0; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 588 | __be32 error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 590 | error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | if (error) |
| 592 | goto out; |
| 593 | |
| 594 | export = fhp->fh_export; |
| 595 | dentry = fhp->fh_dentry; |
| 596 | |
| 597 | if (S_ISREG(dentry->d_inode->i_mode)) |
| 598 | map = nfs3_regaccess; |
| 599 | else if (S_ISDIR(dentry->d_inode->i_mode)) |
| 600 | map = nfs3_diraccess; |
| 601 | else |
| 602 | map = nfs3_anyaccess; |
| 603 | |
| 604 | |
| 605 | query = *access; |
| 606 | for (; map->access; map++) { |
| 607 | if (map->access & query) { |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 608 | __be32 err2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
| 610 | sresult |= map->access; |
| 611 | |
J. Bruce Fields | 0ec757d | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 612 | err2 = nfsd_permission(rqstp, export, dentry, map->how); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | switch (err2) { |
| 614 | case nfs_ok: |
| 615 | result |= map->access; |
| 616 | break; |
| 617 | |
| 618 | /* the following error codes just mean the access was not allowed, |
| 619 | * rather than an error occurred */ |
| 620 | case nfserr_rofs: |
| 621 | case nfserr_acces: |
| 622 | case nfserr_perm: |
| 623 | /* simply don't "or" in the access bit. */ |
| 624 | break; |
| 625 | default: |
| 626 | error = err2; |
| 627 | goto out; |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | *access = result; |
| 632 | if (supported) |
| 633 | *supported = sresult; |
| 634 | |
| 635 | out: |
| 636 | return error; |
| 637 | } |
| 638 | #endif /* CONFIG_NFSD_V3 */ |
| 639 | |
J. Bruce Fields | 105f462 | 2011-06-07 11:50:23 -0400 | [diff] [blame] | 640 | static int nfsd_open_break_lease(struct inode *inode, int access) |
| 641 | { |
| 642 | unsigned int mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
J. Bruce Fields | 105f462 | 2011-06-07 11:50:23 -0400 | [diff] [blame] | 644 | if (access & NFSD_MAY_NOT_BREAK_LEASE) |
| 645 | return 0; |
| 646 | mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY; |
| 647 | return break_lease(inode, mode | O_NONBLOCK); |
| 648 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
| 650 | /* |
| 651 | * Open an existing file or directory. |
Bernd Schubert | 999448a | 2012-03-18 22:44:49 -0400 | [diff] [blame] | 652 | * The may_flags argument indicates the type of open (read/write/lock) |
| 653 | * and additional flags. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | * N.B. After this call fhp needs an fh_put |
| 655 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 656 | __be32 |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 657 | nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, |
Bernd Schubert | 999448a | 2012-03-18 22:44:49 -0400 | [diff] [blame] | 658 | int may_flags, struct file **filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
Al Viro | 765927b | 2012-06-26 21:58:53 +0400 | [diff] [blame] | 660 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | struct inode *inode; |
Kinglong Mee | 8519f99 | 2014-09-03 08:14:06 +0800 | [diff] [blame] | 662 | struct file *file; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 663 | int flags = O_RDONLY|O_LARGEFILE; |
| 664 | __be32 err; |
Jeff Layton | 9188525 | 2010-03-19 08:06:28 -0400 | [diff] [blame] | 665 | int host_err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 667 | validate_process_creds(); |
| 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | /* |
| 670 | * If we get here, then the client has already done an "open", |
| 671 | * and (hopefully) checked permission - so allow OWNER_OVERRIDE |
| 672 | * in case a chmod has now revoked permission. |
J. Bruce Fields | d91d0b5 | 2012-06-06 12:12:57 -0400 | [diff] [blame] | 673 | * |
| 674 | * Arguably we should also allow the owner override for |
| 675 | * directories, but we never have and it doesn't seem to have |
| 676 | * caused anyone a problem. If we were to change this, note |
| 677 | * also that our filldir callbacks would need a variant of |
| 678 | * lookup_one_len that doesn't check permissions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | */ |
J. Bruce Fields | d91d0b5 | 2012-06-06 12:12:57 -0400 | [diff] [blame] | 680 | if (type == S_IFREG) |
| 681 | may_flags |= NFSD_MAY_OWNER_OVERRIDE; |
| 682 | err = fh_verify(rqstp, fhp, type, may_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | if (err) |
| 684 | goto out; |
| 685 | |
Al Viro | 765927b | 2012-06-26 21:58:53 +0400 | [diff] [blame] | 686 | path.mnt = fhp->fh_export->ex_path.mnt; |
| 687 | path.dentry = fhp->fh_dentry; |
| 688 | inode = path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
| 690 | /* Disallow write access to files with the append-only bit set |
| 691 | * or any access when mandatory locking enabled |
| 692 | */ |
| 693 | err = nfserr_perm; |
Bernd Schubert | 999448a | 2012-03-18 22:44:49 -0400 | [diff] [blame] | 694 | if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | goto out; |
J. Bruce Fields | 5e7fc43 | 2007-10-02 14:18:12 -0400 | [diff] [blame] | 696 | /* |
| 697 | * We must ignore files (but only files) which might have mandatory |
| 698 | * locks on them because there is no way to know if the accesser has |
| 699 | * the lock. |
| 700 | */ |
| 701 | if (S_ISREG((inode)->i_mode) && mandatory_lock(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | goto out; |
| 703 | |
| 704 | if (!inode->i_fop) |
| 705 | goto out; |
| 706 | |
Bernd Schubert | 999448a | 2012-03-18 22:44:49 -0400 | [diff] [blame] | 707 | host_err = nfsd_open_break_lease(inode, may_flags); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 708 | if (host_err) /* NOMEM or WOULDBLOCK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | goto out_nfserr; |
| 710 | |
Bernd Schubert | 999448a | 2012-03-18 22:44:49 -0400 | [diff] [blame] | 711 | if (may_flags & NFSD_MAY_WRITE) { |
| 712 | if (may_flags & NFSD_MAY_READ) |
J. Bruce Fields | 9ecb6a0 | 2006-06-30 01:56:17 -0700 | [diff] [blame] | 713 | flags = O_RDWR|O_LARGEFILE; |
| 714 | else |
| 715 | flags = O_WRONLY|O_LARGEFILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | } |
Bernd Schubert | 999448a | 2012-03-18 22:44:49 -0400 | [diff] [blame] | 717 | |
Kinglong Mee | 8519f99 | 2014-09-03 08:14:06 +0800 | [diff] [blame] | 718 | file = dentry_open(&path, flags, current_cred()); |
| 719 | if (IS_ERR(file)) { |
| 720 | host_err = PTR_ERR(file); |
| 721 | goto out_nfserr; |
Bernd Schubert | 06effdb | 2012-03-18 22:44:50 -0400 | [diff] [blame] | 722 | } |
| 723 | |
Linus Torvalds | 5e40d33 | 2014-10-12 10:13:55 -0400 | [diff] [blame] | 724 | host_err = ima_file_check(file, may_flags, 0); |
Kinglong Mee | 8519f99 | 2014-09-03 08:14:06 +0800 | [diff] [blame] | 725 | if (host_err) { |
| 726 | nfsd_close(file); |
| 727 | goto out_nfserr; |
| 728 | } |
| 729 | |
| 730 | if (may_flags & NFSD_MAY_64BIT_COOKIE) |
| 731 | file->f_mode |= FMODE_64BITHASH; |
| 732 | else |
| 733 | file->f_mode |= FMODE_32BITHASH; |
| 734 | |
| 735 | *filp = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 737 | err = nfserrno(host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | out: |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 739 | validate_process_creds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | return err; |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Close a file. |
| 745 | */ |
| 746 | void |
| 747 | nfsd_close(struct file *filp) |
| 748 | { |
| 749 | fput(filp); |
| 750 | } |
| 751 | |
J. Bruce Fields | 9a8d248 | 2009-01-06 13:37:03 -0500 | [diff] [blame] | 752 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | * Obtain the readahead parameters for the file |
| 754 | * specified by (dev, ino). |
| 755 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
| 757 | static inline struct raparms * |
| 758 | nfsd_get_raparms(dev_t dev, ino_t ino) |
| 759 | { |
| 760 | struct raparms *ra, **rap, **frap = NULL; |
| 761 | int depth = 0; |
Greg Banks | fce1456 | 2006-10-04 02:15:49 -0700 | [diff] [blame] | 762 | unsigned int hash; |
| 763 | struct raparm_hbucket *rab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
Greg Banks | fce1456 | 2006-10-04 02:15:49 -0700 | [diff] [blame] | 765 | hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK; |
| 766 | rab = &raparm_hash[hash]; |
| 767 | |
| 768 | spin_lock(&rab->pb_lock); |
| 769 | for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | if (ra->p_ino == ino && ra->p_dev == dev) |
| 771 | goto found; |
| 772 | depth++; |
| 773 | if (ra->p_count == 0) |
| 774 | frap = rap; |
| 775 | } |
Konstantin Khorenko | 3aa6e0a | 2011-02-01 17:16:29 +0300 | [diff] [blame] | 776 | depth = nfsdstats.ra_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | if (!frap) { |
Greg Banks | fce1456 | 2006-10-04 02:15:49 -0700 | [diff] [blame] | 778 | spin_unlock(&rab->pb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | return NULL; |
| 780 | } |
| 781 | rap = frap; |
| 782 | ra = *frap; |
| 783 | ra->p_dev = dev; |
| 784 | ra->p_ino = ino; |
| 785 | ra->p_set = 0; |
Greg Banks | fce1456 | 2006-10-04 02:15:49 -0700 | [diff] [blame] | 786 | ra->p_hindex = hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | found: |
Greg Banks | fce1456 | 2006-10-04 02:15:49 -0700 | [diff] [blame] | 788 | if (rap != &rab->pb_head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | *rap = ra->p_next; |
Greg Banks | fce1456 | 2006-10-04 02:15:49 -0700 | [diff] [blame] | 790 | ra->p_next = rab->pb_head; |
| 791 | rab->pb_head = ra; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | ra->p_count++; |
| 794 | nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++; |
Greg Banks | fce1456 | 2006-10-04 02:15:49 -0700 | [diff] [blame] | 795 | spin_unlock(&rab->pb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | return ra; |
| 797 | } |
| 798 | |
| 799 | /* |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 800 | * Grab and keep cached pages associated with a file in the svc_rqst |
| 801 | * so that they can be passed to the network sendmsg/sendpage routines |
| 802 | * directly. They will be released after the sending has completed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | */ |
| 804 | static int |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 805 | nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, |
| 806 | struct splice_desc *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 808 | struct svc_rqst *rqstp = sd->u.data; |
J. Bruce Fields | afc5940 | 2012-12-10 18:01:37 -0500 | [diff] [blame] | 809 | struct page **pp = rqstp->rq_next_page; |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 810 | struct page *page = buf->page; |
| 811 | size_t size; |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 812 | |
| 813 | size = sd->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
| 815 | if (rqstp->rq_res.page_len == 0) { |
| 816 | get_page(page); |
J. Bruce Fields | afc5940 | 2012-12-10 18:01:37 -0500 | [diff] [blame] | 817 | put_page(*rqstp->rq_next_page); |
| 818 | *(rqstp->rq_next_page++) = page; |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 819 | rqstp->rq_res.page_base = buf->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | rqstp->rq_res.page_len = size; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 821 | } else if (page != pp[-1]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | get_page(page); |
J. Bruce Fields | afc5940 | 2012-12-10 18:01:37 -0500 | [diff] [blame] | 823 | if (*rqstp->rq_next_page) |
| 824 | put_page(*rqstp->rq_next_page); |
| 825 | *(rqstp->rq_next_page++) = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | rqstp->rq_res.page_len += size; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 827 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | rqstp->rq_res.page_len += size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | return size; |
| 831 | } |
| 832 | |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 833 | static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe, |
| 834 | struct splice_desc *sd) |
| 835 | { |
| 836 | return __splice_from_pipe(pipe, sd, nfsd_splice_actor); |
| 837 | } |
| 838 | |
Jeff Layton | e2afc81 | 2014-06-17 07:44:13 -0400 | [diff] [blame] | 839 | static __be32 |
| 840 | nfsd_finish_read(struct file *file, unsigned long *count, int host_err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | { |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 842 | if (host_err >= 0) { |
| 843 | nfsdstats.io_read += host_err; |
| 844 | *count = host_err; |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 845 | fsnotify_access(file); |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 846 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } else |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 848 | return nfserrno(host_err); |
| 849 | } |
| 850 | |
Jeff Layton | e2afc81 | 2014-06-17 07:44:13 -0400 | [diff] [blame] | 851 | __be32 nfsd_splice_read(struct svc_rqst *rqstp, |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 852 | struct file *file, loff_t offset, unsigned long *count) |
| 853 | { |
| 854 | struct splice_desc sd = { |
| 855 | .len = 0, |
| 856 | .total_len = *count, |
| 857 | .pos = offset, |
| 858 | .u.data = rqstp, |
| 859 | }; |
| 860 | int host_err; |
| 861 | |
| 862 | rqstp->rq_next_page = rqstp->rq_respages + 1; |
| 863 | host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor); |
| 864 | return nfsd_finish_read(file, count, host_err); |
| 865 | } |
| 866 | |
Jeff Layton | e2afc81 | 2014-06-17 07:44:13 -0400 | [diff] [blame] | 867 | __be32 nfsd_readv(struct file *file, loff_t offset, struct kvec *vec, int vlen, |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 868 | unsigned long *count) |
| 869 | { |
| 870 | mm_segment_t oldfs; |
| 871 | int host_err; |
| 872 | |
| 873 | oldfs = get_fs(); |
| 874 | set_fs(KERNEL_DS); |
| 875 | host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset); |
| 876 | set_fs(oldfs); |
| 877 | return nfsd_finish_read(file, count, host_err); |
| 878 | } |
| 879 | |
| 880 | static __be32 |
| 881 | nfsd_vfs_read(struct svc_rqst *rqstp, struct file *file, |
| 882 | loff_t offset, struct kvec *vec, int vlen, unsigned long *count) |
| 883 | { |
| 884 | if (file->f_op->splice_read && rqstp->rq_splice_ok) |
| 885 | return nfsd_splice_read(rqstp, file, offset, count); |
| 886 | else |
| 887 | return nfsd_readv(file, offset, vec, vlen, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | } |
| 889 | |
J. Bruce Fields | d911df7 | 2009-06-15 16:03:53 -0700 | [diff] [blame] | 890 | /* |
| 891 | * Gathered writes: If another process is currently writing to the file, |
| 892 | * there's a high chance this is another nfsd (triggered by a bulk write |
| 893 | * from a client's biod). Rather than syncing the file with each write |
| 894 | * request, we sleep for 10 msec. |
| 895 | * |
| 896 | * I don't know if this roughly approximates C. Juszak's idea of |
| 897 | * gathered writes, but it's a nice and simple solution (IMHO), and it |
| 898 | * seems to work:-) |
| 899 | * |
| 900 | * Note: we do this only in the NFSv2 case, since v3 and higher have a |
| 901 | * better tool (separate unstable writes and commits) for solving this |
| 902 | * problem. |
| 903 | */ |
| 904 | static int wait_for_concurrent_writes(struct file *file) |
| 905 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 906 | struct inode *inode = file_inode(file); |
J. Bruce Fields | d911df7 | 2009-06-15 16:03:53 -0700 | [diff] [blame] | 907 | static ino_t last_ino; |
| 908 | static dev_t last_dev; |
| 909 | int err = 0; |
| 910 | |
| 911 | if (atomic_read(&inode->i_writecount) > 1 |
| 912 | || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) { |
| 913 | dprintk("nfsd: write defer %d\n", task_pid_nr(current)); |
| 914 | msleep(10); |
| 915 | dprintk("nfsd: write resume %d\n", task_pid_nr(current)); |
| 916 | } |
| 917 | |
| 918 | if (inode->i_state & I_DIRTY) { |
| 919 | dprintk("nfsd: write sync %d\n", task_pid_nr(current)); |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 920 | err = vfs_fsync(file, 0); |
J. Bruce Fields | d911df7 | 2009-06-15 16:03:53 -0700 | [diff] [blame] | 921 | } |
| 922 | last_ino = inode->i_ino; |
| 923 | last_dev = inode->i_sb->s_dev; |
| 924 | return err; |
| 925 | } |
| 926 | |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 927 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file, |
| 929 | loff_t offset, struct kvec *vec, int vlen, |
David Shaw | 31dec25 | 2009-03-05 20:16:14 -0500 | [diff] [blame] | 930 | unsigned long *cnt, int *stablep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | { |
| 932 | struct svc_export *exp; |
| 933 | struct dentry *dentry; |
| 934 | struct inode *inode; |
| 935 | mm_segment_t oldfs; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 936 | __be32 err = 0; |
| 937 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | int stable = *stablep; |
Trond Myklebust | 48e03bc | 2009-06-05 12:35:15 -0400 | [diff] [blame] | 939 | int use_wgather; |
Kent Overstreet | e49dbbf | 2013-03-22 11:18:24 -0700 | [diff] [blame] | 940 | loff_t pos = offset; |
Zach Brown | e77a7b4 | 2014-10-06 16:42:52 -0700 | [diff] [blame^] | 941 | loff_t end = LLONG_MAX; |
NeilBrown | 8658452 | 2014-05-12 11:22:47 +1000 | [diff] [blame] | 942 | unsigned int pflags = current->flags; |
| 943 | |
| 944 | if (rqstp->rq_local) |
| 945 | /* |
| 946 | * We want less throttling in balance_dirty_pages() |
| 947 | * and shrink_inactive_list() so that nfs to |
| 948 | * localhost doesn't cause nfsd to lock up due to all |
| 949 | * the client's dirty pages or its congested queue. |
| 950 | */ |
| 951 | current->flags |= PF_LESS_THROTTLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
Josef "Jeff" Sipek | 7eaa36e | 2006-12-08 02:36:41 -0800 | [diff] [blame] | 953 | dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | inode = dentry->d_inode; |
| 955 | exp = fhp->fh_export; |
| 956 | |
Trond Myklebust | 48e03bc | 2009-06-05 12:35:15 -0400 | [diff] [blame] | 957 | use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | if (!EX_ISSYNC(exp)) |
| 960 | stable = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
| 962 | /* Write the data. */ |
| 963 | oldfs = get_fs(); set_fs(KERNEL_DS); |
Kent Overstreet | e49dbbf | 2013-03-22 11:18:24 -0700 | [diff] [blame] | 964 | host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | set_fs(oldfs); |
J. Bruce Fields | e4636d5 | 2009-06-15 19:07:13 -0700 | [diff] [blame] | 966 | if (host_err < 0) |
| 967 | goto out_nfserr; |
| 968 | *cnt = host_err; |
| 969 | nfsdstats.io_write += host_err; |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 970 | fsnotify_modify(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | |
J. Bruce Fields | face150 | 2012-10-26 16:12:31 -0400 | [diff] [blame] | 972 | if (stable) { |
Zach Brown | e77a7b4 | 2014-10-06 16:42:52 -0700 | [diff] [blame^] | 973 | if (use_wgather) { |
J. Bruce Fields | face150 | 2012-10-26 16:12:31 -0400 | [diff] [blame] | 974 | host_err = wait_for_concurrent_writes(file); |
Zach Brown | e77a7b4 | 2014-10-06 16:42:52 -0700 | [diff] [blame^] | 975 | } else { |
| 976 | if (*cnt) |
| 977 | end = offset + *cnt - 1; |
| 978 | host_err = vfs_fsync_range(file, offset, end, 0); |
| 979 | } |
J. Bruce Fields | face150 | 2012-10-26 16:12:31 -0400 | [diff] [blame] | 980 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
J. Bruce Fields | e4636d5 | 2009-06-15 19:07:13 -0700 | [diff] [blame] | 982 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 983 | dprintk("nfsd: write complete host_err=%d\n", host_err); |
Wei Yongjun | a0d24b2 | 2009-05-19 12:03:15 +0800 | [diff] [blame] | 984 | if (host_err >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | err = 0; |
Wei Yongjun | a0d24b2 | 2009-05-19 12:03:15 +0800 | [diff] [blame] | 986 | else |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 987 | err = nfserrno(host_err); |
NeilBrown | 8658452 | 2014-05-12 11:22:47 +1000 | [diff] [blame] | 988 | if (rqstp->rq_local) |
| 989 | tsk_restore_flags(current, pflags, PF_LESS_THROTTLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | return err; |
| 991 | } |
| 992 | |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 993 | __be32 nfsd_get_tmp_read_open(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 994 | struct file **file, struct raparms **ra) |
J. Bruce Fields | fa0a212 | 2010-07-27 16:48:54 -0400 | [diff] [blame] | 995 | { |
J. Bruce Fields | fa0a212 | 2010-07-27 16:48:54 -0400 | [diff] [blame] | 996 | struct inode *inode; |
J. Bruce Fields | fa0a212 | 2010-07-27 16:48:54 -0400 | [diff] [blame] | 997 | __be32 err; |
| 998 | |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 999 | err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, file); |
J. Bruce Fields | fa0a212 | 2010-07-27 16:48:54 -0400 | [diff] [blame] | 1000 | if (err) |
| 1001 | return err; |
| 1002 | |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 1003 | inode = file_inode(*file); |
J. Bruce Fields | fa0a212 | 2010-07-27 16:48:54 -0400 | [diff] [blame] | 1004 | |
| 1005 | /* Get readahead parameters */ |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 1006 | *ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino); |
J. Bruce Fields | fa0a212 | 2010-07-27 16:48:54 -0400 | [diff] [blame] | 1007 | |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 1008 | if (*ra && (*ra)->p_set) |
| 1009 | (*file)->f_ra = (*ra)->p_ra; |
| 1010 | return nfs_ok; |
| 1011 | } |
J. Bruce Fields | fa0a212 | 2010-07-27 16:48:54 -0400 | [diff] [blame] | 1012 | |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 1013 | void nfsd_put_tmp_read_open(struct file *file, struct raparms *ra) |
| 1014 | { |
J. Bruce Fields | fa0a212 | 2010-07-27 16:48:54 -0400 | [diff] [blame] | 1015 | /* Write back readahead params */ |
| 1016 | if (ra) { |
| 1017 | struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex]; |
| 1018 | spin_lock(&rab->pb_lock); |
| 1019 | ra->p_ra = file->f_ra; |
| 1020 | ra->p_set = 1; |
| 1021 | ra->p_count--; |
| 1022 | spin_unlock(&rab->pb_lock); |
| 1023 | } |
J. Bruce Fields | fa0a212 | 2010-07-27 16:48:54 -0400 | [diff] [blame] | 1024 | nfsd_close(file); |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * Read data from a file. count must contain the requested read count |
| 1029 | * on entry. On return, *count contains the number of bytes actually read. |
| 1030 | * N.B. After this call fhp needs an fh_put |
| 1031 | */ |
| 1032 | __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 1033 | loff_t offset, struct kvec *vec, int vlen, unsigned long *count) |
| 1034 | { |
| 1035 | struct file *file; |
| 1036 | struct raparms *ra; |
| 1037 | __be32 err; |
| 1038 | |
| 1039 | err = nfsd_get_tmp_read_open(rqstp, fhp, &file, &ra); |
| 1040 | if (err) |
| 1041 | return err; |
| 1042 | |
| 1043 | err = nfsd_vfs_read(rqstp, file, offset, vec, vlen, count); |
| 1044 | |
| 1045 | nfsd_put_tmp_read_open(file, ra); |
| 1046 | |
J. Bruce Fields | fa0a212 | 2010-07-27 16:48:54 -0400 | [diff] [blame] | 1047 | return err; |
| 1048 | } |
| 1049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | /* |
| 1051 | * Write data to a file. |
| 1052 | * The stable flag requests synchronous writes. |
| 1053 | * N.B. After this call fhp needs an fh_put |
| 1054 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1055 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file, |
David Shaw | 31dec25 | 2009-03-05 20:16:14 -0500 | [diff] [blame] | 1057 | loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | int *stablep) |
| 1059 | { |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1060 | __be32 err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
| 1062 | if (file) { |
J. Bruce Fields | 0ec757d | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 1063 | err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1064 | NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | if (err) |
| 1066 | goto out; |
| 1067 | err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt, |
| 1068 | stablep); |
| 1069 | } else { |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1070 | err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | if (err) |
| 1072 | goto out; |
| 1073 | |
| 1074 | if (cnt) |
| 1075 | err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, |
| 1076 | cnt, stablep); |
| 1077 | nfsd_close(file); |
| 1078 | } |
| 1079 | out: |
| 1080 | return err; |
| 1081 | } |
| 1082 | |
| 1083 | #ifdef CONFIG_NFSD_V3 |
| 1084 | /* |
| 1085 | * Commit all pending writes to stable storage. |
Trond Myklebust | aa696a6 | 2010-01-29 16:44:25 -0500 | [diff] [blame] | 1086 | * |
| 1087 | * Note: we only guarantee that data that lies within the range specified |
| 1088 | * by the 'offset' and 'count' parameters will be synced. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | * |
| 1090 | * Unfortunately we cannot lock the file to make sure we return full WCC |
| 1091 | * data to the client, as locking happens lower down in the filesystem. |
| 1092 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1093 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 1095 | loff_t offset, unsigned long count) |
| 1096 | { |
| 1097 | struct file *file; |
Trond Myklebust | aa696a6 | 2010-01-29 16:44:25 -0500 | [diff] [blame] | 1098 | loff_t end = LLONG_MAX; |
| 1099 | __be32 err = nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
Trond Myklebust | aa696a6 | 2010-01-29 16:44:25 -0500 | [diff] [blame] | 1101 | if (offset < 0) |
| 1102 | goto out; |
| 1103 | if (count != 0) { |
| 1104 | end = offset + (loff_t)count - 1; |
| 1105 | if (end < offset) |
| 1106 | goto out; |
| 1107 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
Jeff Layton | 9188525 | 2010-03-19 08:06:28 -0400 | [diff] [blame] | 1109 | err = nfsd_open(rqstp, fhp, S_IFREG, |
| 1110 | NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file); |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1111 | if (err) |
Trond Myklebust | aa696a6 | 2010-01-29 16:44:25 -0500 | [diff] [blame] | 1112 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | if (EX_ISSYNC(fhp->fh_export)) { |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 1114 | int err2 = vfs_fsync_range(file, offset, end, 0); |
Trond Myklebust | aa696a6 | 2010-01-29 16:44:25 -0500 | [diff] [blame] | 1115 | |
| 1116 | if (err2 != -EINVAL) |
| 1117 | err = nfserrno(err2); |
| 1118 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | err = nfserr_notsupp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | nfsd_close(file); |
Trond Myklebust | aa696a6 | 2010-01-29 16:44:25 -0500 | [diff] [blame] | 1123 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | return err; |
| 1125 | } |
| 1126 | #endif /* CONFIG_NFSD_V3 */ |
| 1127 | |
Adrian Bunk | f2b0dee | 2008-02-13 23:30:26 +0200 | [diff] [blame] | 1128 | static __be32 |
J. Bruce Fields | 5c002b3 | 2007-11-30 16:55:23 -0500 | [diff] [blame] | 1129 | nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp, |
| 1130 | struct iattr *iap) |
| 1131 | { |
| 1132 | /* |
| 1133 | * Mode has already been set earlier in create: |
| 1134 | */ |
| 1135 | iap->ia_valid &= ~ATTR_MODE; |
| 1136 | /* |
| 1137 | * Setting uid/gid works only for root. Irix appears to |
| 1138 | * send along the gid on create when it tries to implement |
| 1139 | * setgid directories via NFS: |
| 1140 | */ |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1141 | if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) |
J. Bruce Fields | 5c002b3 | 2007-11-30 16:55:23 -0500 | [diff] [blame] | 1142 | iap->ia_valid &= ~(ATTR_UID|ATTR_GID); |
| 1143 | if (iap->ia_valid) |
| 1144 | return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0); |
Trond Myklebust | 0f3a24b | 2014-07-01 18:27:53 -0400 | [diff] [blame] | 1145 | /* Callers expect file metadata to be committed here */ |
Jeff Layton | 722b620 | 2014-07-03 07:54:19 -0400 | [diff] [blame] | 1146 | return nfserrno(commit_metadata(resfhp)); |
J. Bruce Fields | 5c002b3 | 2007-11-30 16:55:23 -0500 | [diff] [blame] | 1147 | } |
| 1148 | |
wengang wang | 4ac35c2 | 2009-02-10 11:27:51 +0800 | [diff] [blame] | 1149 | /* HPUX client sometimes creates a file in mode 000, and sets size to 0. |
| 1150 | * setting size to 0 may fail for some specific file systems by the permission |
| 1151 | * checking which requires WRITE permission but the mode is 000. |
| 1152 | * we ignore the resizing(to 0) on the just new created file, since the size is |
| 1153 | * 0 after file created. |
| 1154 | * |
| 1155 | * call this only after vfs_create() is called. |
| 1156 | * */ |
| 1157 | static void |
| 1158 | nfsd_check_ignore_resizing(struct iattr *iap) |
| 1159 | { |
| 1160 | if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0)) |
| 1161 | iap->ia_valid &= ~ATTR_SIZE; |
| 1162 | } |
| 1163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | /* |
| 1165 | * Create a file (regular, directory, device, fifo); UNIX sockets |
| 1166 | * not yet implemented. |
| 1167 | * If the response fh has been verified, the parent directory should |
| 1168 | * already be locked. Note that the parent directory is left locked. |
| 1169 | * |
| 1170 | * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp |
| 1171 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1172 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 1174 | char *fname, int flen, struct iattr *iap, |
| 1175 | int type, dev_t rdev, struct svc_fh *resfhp) |
| 1176 | { |
| 1177 | struct dentry *dentry, *dchild = NULL; |
| 1178 | struct inode *dirp; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1179 | __be32 err; |
J. Bruce Fields | 5c002b3 | 2007-11-30 16:55:23 -0500 | [diff] [blame] | 1180 | __be32 err2; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1181 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | |
| 1183 | err = nfserr_perm; |
| 1184 | if (!flen) |
| 1185 | goto out; |
| 1186 | err = nfserr_exist; |
| 1187 | if (isdotent(fname, flen)) |
| 1188 | goto out; |
| 1189 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1190 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | if (err) |
| 1192 | goto out; |
| 1193 | |
| 1194 | dentry = fhp->fh_dentry; |
| 1195 | dirp = dentry->d_inode; |
| 1196 | |
| 1197 | err = nfserr_notdir; |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 1198 | if (!dirp->i_op->lookup) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | goto out; |
| 1200 | /* |
| 1201 | * Check whether the response file handle has been verified yet. |
| 1202 | * If it has, the parent directory should already be locked. |
| 1203 | */ |
| 1204 | if (!resfhp->fh_dentry) { |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1205 | host_err = fh_want_write(fhp); |
| 1206 | if (host_err) |
| 1207 | goto out_nfserr; |
| 1208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */ |
Peter Zijlstra | 12fd352 | 2006-10-02 02:18:03 -0700 | [diff] [blame] | 1210 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | dchild = lookup_one_len(fname, dentry, flen); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1212 | host_err = PTR_ERR(dchild); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | if (IS_ERR(dchild)) |
| 1214 | goto out_nfserr; |
| 1215 | err = fh_compose(resfhp, fhp->fh_export, dchild, fhp); |
| 1216 | if (err) |
| 1217 | goto out; |
| 1218 | } else { |
| 1219 | /* called from nfsd_proc_create */ |
| 1220 | dchild = dget(resfhp->fh_dentry); |
| 1221 | if (!fhp->fh_locked) { |
| 1222 | /* not actually possible */ |
| 1223 | printk(KERN_ERR |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 1224 | "nfsd_create: parent %pd2 not locked!\n", |
| 1225 | dentry); |
Al Viro | d75f2b9 | 2006-01-18 17:43:44 -0800 | [diff] [blame] | 1226 | err = nfserr_io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | goto out; |
| 1228 | } |
| 1229 | } |
| 1230 | /* |
| 1231 | * Make sure the child dentry is still negative ... |
| 1232 | */ |
| 1233 | err = nfserr_exist; |
| 1234 | if (dchild->d_inode) { |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 1235 | dprintk("nfsd_create: dentry %pd/%pd not negative!\n", |
| 1236 | dentry, dchild); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | goto out; |
| 1238 | } |
| 1239 | |
| 1240 | if (!(iap->ia_valid & ATTR_MODE)) |
| 1241 | iap->ia_mode = 0; |
| 1242 | iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type; |
| 1243 | |
Miklos Szeredi | 07cad1d | 2008-07-01 15:38:35 +0200 | [diff] [blame] | 1244 | err = nfserr_inval; |
| 1245 | if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) { |
| 1246 | printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n", |
| 1247 | type); |
| 1248 | goto out; |
| 1249 | } |
| 1250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | /* |
| 1252 | * Get the dir op function pointer. |
| 1253 | */ |
J. Bruce Fields | 088406b | 2006-11-08 17:44:59 -0800 | [diff] [blame] | 1254 | err = 0; |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1255 | host_err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | switch (type) { |
| 1257 | case S_IFREG: |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 1258 | host_err = vfs_create(dirp, dchild, iap->ia_mode, true); |
wengang wang | 4ac35c2 | 2009-02-10 11:27:51 +0800 | [diff] [blame] | 1259 | if (!host_err) |
| 1260 | nfsd_check_ignore_resizing(iap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | break; |
| 1262 | case S_IFDIR: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1263 | host_err = vfs_mkdir(dirp, dchild, iap->ia_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | break; |
| 1265 | case S_IFCHR: |
| 1266 | case S_IFBLK: |
| 1267 | case S_IFIFO: |
| 1268 | case S_IFSOCK: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1269 | host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | break; |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 1271 | } |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1272 | if (host_err < 0) |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 1273 | goto out_nfserr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1275 | err = nfsd_create_setattr(rqstp, resfhp, iap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1277 | /* |
Trond Myklebust | 0f3a24b | 2014-07-01 18:27:53 -0400 | [diff] [blame] | 1278 | * nfsd_create_setattr already committed the child. Transactional |
| 1279 | * filesystems had a chance to commit changes for both parent and |
| 1280 | * child * simultaneously making the following commit_metadata a |
| 1281 | * noop. |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1282 | */ |
| 1283 | err2 = nfserrno(commit_metadata(fhp)); |
J. Bruce Fields | 5c002b3 | 2007-11-30 16:55:23 -0500 | [diff] [blame] | 1284 | if (err2) |
| 1285 | err = err2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | /* |
| 1287 | * Update the file handle to get the new inode info. |
| 1288 | */ |
| 1289 | if (!err) |
| 1290 | err = fh_update(resfhp); |
| 1291 | out: |
| 1292 | if (dchild && !IS_ERR(dchild)) |
| 1293 | dput(dchild); |
| 1294 | return err; |
| 1295 | |
| 1296 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1297 | err = nfserrno(host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | goto out; |
| 1299 | } |
| 1300 | |
| 1301 | #ifdef CONFIG_NFSD_V3 |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1302 | |
| 1303 | static inline int nfsd_create_is_exclusive(int createmode) |
| 1304 | { |
| 1305 | return createmode == NFS3_CREATE_EXCLUSIVE |
| 1306 | || createmode == NFS4_CREATE_EXCLUSIVE4_1; |
| 1307 | } |
| 1308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | /* |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1310 | * NFSv3 and NFSv4 version of nfsd_create |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1312 | __be32 |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1313 | do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | char *fname, int flen, struct iattr *iap, |
| 1315 | struct svc_fh *resfhp, int createmode, u32 *verifier, |
J. Bruce Fields | 856121b | 2011-10-13 11:37:11 -0400 | [diff] [blame] | 1316 | bool *truncp, bool *created) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | { |
| 1318 | struct dentry *dentry, *dchild = NULL; |
| 1319 | struct inode *dirp; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1320 | __be32 err; |
| 1321 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | __u32 v_mtime=0, v_atime=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | |
| 1324 | err = nfserr_perm; |
| 1325 | if (!flen) |
| 1326 | goto out; |
| 1327 | err = nfserr_exist; |
| 1328 | if (isdotent(fname, flen)) |
| 1329 | goto out; |
| 1330 | if (!(iap->ia_valid & ATTR_MODE)) |
| 1331 | iap->ia_mode = 0; |
Sachin Prabhu | 1574dff | 2011-04-20 13:09:35 +0100 | [diff] [blame] | 1332 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | if (err) |
| 1334 | goto out; |
| 1335 | |
| 1336 | dentry = fhp->fh_dentry; |
| 1337 | dirp = dentry->d_inode; |
| 1338 | |
| 1339 | /* Get all the sanity checks out of the way before |
| 1340 | * we lock the parent. */ |
| 1341 | err = nfserr_notdir; |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 1342 | if (!dirp->i_op->lookup) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | goto out; |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1344 | |
| 1345 | host_err = fh_want_write(fhp); |
| 1346 | if (host_err) |
| 1347 | goto out_nfserr; |
| 1348 | |
Peter Zijlstra | 12fd352 | 2006-10-02 02:18:03 -0700 | [diff] [blame] | 1349 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
| 1351 | /* |
| 1352 | * Compose the response file handle. |
| 1353 | */ |
| 1354 | dchild = lookup_one_len(fname, dentry, flen); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1355 | host_err = PTR_ERR(dchild); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | if (IS_ERR(dchild)) |
| 1357 | goto out_nfserr; |
| 1358 | |
Sachin Prabhu | 1574dff | 2011-04-20 13:09:35 +0100 | [diff] [blame] | 1359 | /* If file doesn't exist, check for permissions to create one */ |
| 1360 | if (!dchild->d_inode) { |
| 1361 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE); |
| 1362 | if (err) |
| 1363 | goto out; |
| 1364 | } |
| 1365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | err = fh_compose(resfhp, fhp->fh_export, dchild, fhp); |
| 1367 | if (err) |
| 1368 | goto out; |
| 1369 | |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1370 | if (nfsd_create_is_exclusive(createmode)) { |
Peter Staubach | c397852 | 2007-01-26 00:57:00 -0800 | [diff] [blame] | 1371 | /* solaris7 gets confused (bugid 4218508) if these have |
Jeff Layton | 749997e | 2007-07-31 00:37:51 -0700 | [diff] [blame] | 1372 | * the high bit set, so just clear the high bits. If this is |
| 1373 | * ever changed to use different attrs for storing the |
| 1374 | * verifier, then do_open_lookup() will also need to be fixed |
| 1375 | * accordingly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | */ |
| 1377 | v_mtime = verifier[0]&0x7fffffff; |
| 1378 | v_atime = verifier[1]&0x7fffffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | if (dchild->d_inode) { |
| 1382 | err = 0; |
| 1383 | |
| 1384 | switch (createmode) { |
| 1385 | case NFS3_CREATE_UNCHECKED: |
| 1386 | if (! S_ISREG(dchild->d_inode->i_mode)) |
J. Bruce Fields | 9dc4e6c | 2012-04-09 18:06:49 -0400 | [diff] [blame] | 1387 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | else if (truncp) { |
| 1389 | /* in nfsv4, we need to treat this case a little |
| 1390 | * differently. we don't want to truncate the |
| 1391 | * file now; this would be wrong if the OPEN |
| 1392 | * fails for some other reason. furthermore, |
| 1393 | * if the size is nonzero, we should ignore it |
| 1394 | * according to spec! |
| 1395 | */ |
| 1396 | *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size; |
| 1397 | } |
| 1398 | else { |
| 1399 | iap->ia_valid &= ATTR_SIZE; |
| 1400 | goto set_attr; |
| 1401 | } |
| 1402 | break; |
| 1403 | case NFS3_CREATE_EXCLUSIVE: |
| 1404 | if ( dchild->d_inode->i_mtime.tv_sec == v_mtime |
| 1405 | && dchild->d_inode->i_atime.tv_sec == v_atime |
Neil Brown | 7007c90 | 2012-12-07 15:40:55 -0500 | [diff] [blame] | 1406 | && dchild->d_inode->i_size == 0 ) { |
| 1407 | if (created) |
| 1408 | *created = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | break; |
Neil Brown | 7007c90 | 2012-12-07 15:40:55 -0500 | [diff] [blame] | 1410 | } |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1411 | case NFS4_CREATE_EXCLUSIVE4_1: |
| 1412 | if ( dchild->d_inode->i_mtime.tv_sec == v_mtime |
| 1413 | && dchild->d_inode->i_atime.tv_sec == v_atime |
Neil Brown | 7007c90 | 2012-12-07 15:40:55 -0500 | [diff] [blame] | 1414 | && dchild->d_inode->i_size == 0 ) { |
| 1415 | if (created) |
| 1416 | *created = 1; |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1417 | goto set_attr; |
Neil Brown | 7007c90 | 2012-12-07 15:40:55 -0500 | [diff] [blame] | 1418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | /* fallthru */ |
| 1420 | case NFS3_CREATE_GUARDED: |
| 1421 | err = nfserr_exist; |
| 1422 | } |
Al Viro | bad0dcf | 2011-11-23 12:03:18 -0500 | [diff] [blame] | 1423 | fh_drop_write(fhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | goto out; |
| 1425 | } |
| 1426 | |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 1427 | host_err = vfs_create(dirp, dchild, iap->ia_mode, true); |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 1428 | if (host_err < 0) { |
Al Viro | bad0dcf | 2011-11-23 12:03:18 -0500 | [diff] [blame] | 1429 | fh_drop_write(fhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | goto out_nfserr; |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 1431 | } |
J. Bruce Fields | 81ac95c | 2006-11-08 17:44:40 -0800 | [diff] [blame] | 1432 | if (created) |
| 1433 | *created = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | |
wengang wang | 4ac35c2 | 2009-02-10 11:27:51 +0800 | [diff] [blame] | 1435 | nfsd_check_ignore_resizing(iap); |
| 1436 | |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1437 | if (nfsd_create_is_exclusive(createmode)) { |
Peter Staubach | c397852 | 2007-01-26 00:57:00 -0800 | [diff] [blame] | 1438 | /* Cram the verifier into atime/mtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | iap->ia_valid = ATTR_MTIME|ATTR_ATIME |
Peter Staubach | c397852 | 2007-01-26 00:57:00 -0800 | [diff] [blame] | 1440 | | ATTR_MTIME_SET|ATTR_ATIME_SET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | /* XXX someone who knows this better please fix it for nsec */ |
| 1442 | iap->ia_mtime.tv_sec = v_mtime; |
| 1443 | iap->ia_atime.tv_sec = v_atime; |
| 1444 | iap->ia_mtime.tv_nsec = 0; |
| 1445 | iap->ia_atime.tv_nsec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | set_attr: |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1449 | err = nfsd_create_setattr(rqstp, resfhp, iap); |
| 1450 | |
| 1451 | /* |
Trond Myklebust | 0f3a24b | 2014-07-01 18:27:53 -0400 | [diff] [blame] | 1452 | * nfsd_create_setattr already committed the child |
| 1453 | * (and possibly also the parent). |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1454 | */ |
| 1455 | if (!err) |
| 1456 | err = nfserrno(commit_metadata(fhp)); |
YAMAMOTO Takashi | f193fba | 2006-01-18 17:43:13 -0800 | [diff] [blame] | 1457 | |
| 1458 | /* |
| 1459 | * Update the filehandle to get the new inode info. |
| 1460 | */ |
| 1461 | if (!err) |
| 1462 | err = fh_update(resfhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | |
| 1464 | out: |
| 1465 | fh_unlock(fhp); |
| 1466 | if (dchild && !IS_ERR(dchild)) |
| 1467 | dput(dchild); |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1468 | fh_drop_write(fhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | return err; |
| 1470 | |
| 1471 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1472 | err = nfserrno(host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | goto out; |
| 1474 | } |
| 1475 | #endif /* CONFIG_NFSD_V3 */ |
| 1476 | |
| 1477 | /* |
| 1478 | * Read a symlink. On entry, *lenp must contain the maximum path length that |
| 1479 | * fits into the buffer. On return, it contains the true length. |
| 1480 | * N.B. After this call fhp needs an fh_put |
| 1481 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1482 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp) |
| 1484 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | struct inode *inode; |
| 1486 | mm_segment_t oldfs; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1487 | __be32 err; |
| 1488 | int host_err; |
Al Viro | 68ac123 | 2012-03-15 08:21:57 -0400 | [diff] [blame] | 1489 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1491 | err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | if (err) |
| 1493 | goto out; |
| 1494 | |
Al Viro | 68ac123 | 2012-03-15 08:21:57 -0400 | [diff] [blame] | 1495 | path.mnt = fhp->fh_export->ex_path.mnt; |
| 1496 | path.dentry = fhp->fh_dentry; |
| 1497 | inode = path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | |
| 1499 | err = nfserr_inval; |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 1500 | if (!inode->i_op->readlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | goto out; |
| 1502 | |
Al Viro | 68ac123 | 2012-03-15 08:21:57 -0400 | [diff] [blame] | 1503 | touch_atime(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | /* N.B. Why does this call need a get_fs()?? |
| 1505 | * Remove the set_fs and watch the fireworks:-) --okir |
| 1506 | */ |
| 1507 | |
| 1508 | oldfs = get_fs(); set_fs(KERNEL_DS); |
J. Bruce Fields | fac7a17 | 2012-07-27 14:16:55 -0400 | [diff] [blame] | 1509 | host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | set_fs(oldfs); |
| 1511 | |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1512 | if (host_err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | goto out_nfserr; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1514 | *lenp = host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | err = 0; |
| 1516 | out: |
| 1517 | return err; |
| 1518 | |
| 1519 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1520 | err = nfserrno(host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | goto out; |
| 1522 | } |
| 1523 | |
| 1524 | /* |
| 1525 | * Create a symlink and look up its inode |
| 1526 | * N.B. After this call _both_ fhp and resfhp need an fh_put |
| 1527 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1528 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 1530 | char *fname, int flen, |
J. Bruce Fields | 52ee043 | 2014-06-20 11:52:21 -0400 | [diff] [blame] | 1531 | char *path, |
Kinglong Mee | 1e444f5 | 2014-07-01 17:48:02 +0800 | [diff] [blame] | 1532 | struct svc_fh *resfhp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | { |
| 1534 | struct dentry *dentry, *dnew; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1535 | __be32 err, cerr; |
| 1536 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
| 1538 | err = nfserr_noent; |
J. Bruce Fields | 52ee043 | 2014-06-20 11:52:21 -0400 | [diff] [blame] | 1539 | if (!flen || path[0] == '\0') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | goto out; |
| 1541 | err = nfserr_exist; |
| 1542 | if (isdotent(fname, flen)) |
| 1543 | goto out; |
| 1544 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1545 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | if (err) |
| 1547 | goto out; |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1548 | |
| 1549 | host_err = fh_want_write(fhp); |
| 1550 | if (host_err) |
| 1551 | goto out_nfserr; |
| 1552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | fh_lock(fhp); |
| 1554 | dentry = fhp->fh_dentry; |
| 1555 | dnew = lookup_one_len(fname, dentry, flen); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1556 | host_err = PTR_ERR(dnew); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | if (IS_ERR(dnew)) |
| 1558 | goto out_nfserr; |
| 1559 | |
J. Bruce Fields | 52ee043 | 2014-06-20 11:52:21 -0400 | [diff] [blame] | 1560 | host_err = vfs_symlink(dentry->d_inode, dnew, path); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1561 | err = nfserrno(host_err); |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1562 | if (!err) |
| 1563 | err = nfserrno(commit_metadata(fhp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | fh_unlock(fhp); |
| 1565 | |
Al Viro | bad0dcf | 2011-11-23 12:03:18 -0500 | [diff] [blame] | 1566 | fh_drop_write(fhp); |
Dave Hansen | 75c3f29 | 2008-02-15 14:37:45 -0800 | [diff] [blame] | 1567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp); |
| 1569 | dput(dnew); |
| 1570 | if (err==0) err = cerr; |
| 1571 | out: |
| 1572 | return err; |
| 1573 | |
| 1574 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1575 | err = nfserrno(host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | goto out; |
| 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | * Create a hardlink |
| 1581 | * N.B. After this call _both_ ffhp and tfhp need an fh_put |
| 1582 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1583 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp, |
| 1585 | char *name, int len, struct svc_fh *tfhp) |
| 1586 | { |
| 1587 | struct dentry *ddir, *dnew, *dold; |
J. Bruce Fields | 55b1335 | 2010-07-19 16:38:24 -0400 | [diff] [blame] | 1588 | struct inode *dirp; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1589 | __be32 err; |
| 1590 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1592 | err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | if (err) |
| 1594 | goto out; |
J. Bruce Fields | 7d818a7 | 2011-08-15 16:59:55 -0400 | [diff] [blame] | 1595 | err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | if (err) |
| 1597 | goto out; |
J. Bruce Fields | 7d818a7 | 2011-08-15 16:59:55 -0400 | [diff] [blame] | 1598 | err = nfserr_isdir; |
| 1599 | if (S_ISDIR(tfhp->fh_dentry->d_inode->i_mode)) |
| 1600 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | err = nfserr_perm; |
| 1602 | if (!len) |
| 1603 | goto out; |
| 1604 | err = nfserr_exist; |
| 1605 | if (isdotent(name, len)) |
| 1606 | goto out; |
| 1607 | |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1608 | host_err = fh_want_write(tfhp); |
| 1609 | if (host_err) { |
| 1610 | err = nfserrno(host_err); |
| 1611 | goto out; |
| 1612 | } |
| 1613 | |
Peter Zijlstra | 12fd352 | 2006-10-02 02:18:03 -0700 | [diff] [blame] | 1614 | fh_lock_nested(ffhp, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | ddir = ffhp->fh_dentry; |
| 1616 | dirp = ddir->d_inode; |
| 1617 | |
| 1618 | dnew = lookup_one_len(name, ddir, len); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1619 | host_err = PTR_ERR(dnew); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | if (IS_ERR(dnew)) |
| 1621 | goto out_nfserr; |
| 1622 | |
| 1623 | dold = tfhp->fh_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | |
J. Bruce Fields | 4795bb3 | 2011-01-11 13:55:46 -0500 | [diff] [blame] | 1625 | err = nfserr_noent; |
| 1626 | if (!dold->d_inode) |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1627 | goto out_dput; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 1628 | host_err = vfs_link(dold, dirp, dnew, NULL); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1629 | if (!host_err) { |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1630 | err = nfserrno(commit_metadata(ffhp)); |
| 1631 | if (!err) |
| 1632 | err = nfserrno(commit_metadata(tfhp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | } else { |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1634 | if (host_err == -EXDEV && rqstp->rq_vers == 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | err = nfserr_acces; |
| 1636 | else |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1637 | err = nfserrno(host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | } |
Dave Hansen | 75c3f29 | 2008-02-15 14:37:45 -0800 | [diff] [blame] | 1639 | out_dput: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | dput(dnew); |
David M. Richter | 270d56e | 2006-06-30 01:56:15 -0700 | [diff] [blame] | 1641 | out_unlock: |
| 1642 | fh_unlock(ffhp); |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1643 | fh_drop_write(tfhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | out: |
| 1645 | return err; |
| 1646 | |
| 1647 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1648 | err = nfserrno(host_err); |
David M. Richter | 270d56e | 2006-06-30 01:56:15 -0700 | [diff] [blame] | 1649 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | /* |
| 1653 | * Rename a file |
| 1654 | * N.B. After this call _both_ ffhp and tfhp need an fh_put |
| 1655 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1656 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, |
| 1658 | struct svc_fh *tfhp, char *tname, int tlen) |
| 1659 | { |
| 1660 | struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap; |
| 1661 | struct inode *fdir, *tdir; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1662 | __be32 err; |
| 1663 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1665 | err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | if (err) |
| 1667 | goto out; |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1668 | err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | if (err) |
| 1670 | goto out; |
| 1671 | |
| 1672 | fdentry = ffhp->fh_dentry; |
| 1673 | fdir = fdentry->d_inode; |
| 1674 | |
| 1675 | tdentry = tfhp->fh_dentry; |
| 1676 | tdir = tdentry->d_inode; |
| 1677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | err = nfserr_perm; |
| 1679 | if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen)) |
| 1680 | goto out; |
| 1681 | |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1682 | host_err = fh_want_write(ffhp); |
| 1683 | if (host_err) { |
| 1684 | err = nfserrno(host_err); |
| 1685 | goto out; |
| 1686 | } |
| 1687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | /* cannot use fh_lock as we need deadlock protective ordering |
| 1689 | * so do it by hand */ |
| 1690 | trap = lock_rename(tdentry, fdentry); |
| 1691 | ffhp->fh_locked = tfhp->fh_locked = 1; |
| 1692 | fill_pre_wcc(ffhp); |
| 1693 | fill_pre_wcc(tfhp); |
| 1694 | |
| 1695 | odentry = lookup_one_len(fname, fdentry, flen); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1696 | host_err = PTR_ERR(odentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | if (IS_ERR(odentry)) |
| 1698 | goto out_nfserr; |
| 1699 | |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1700 | host_err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | if (!odentry->d_inode) |
| 1702 | goto out_dput_old; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1703 | host_err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | if (odentry == trap) |
| 1705 | goto out_dput_old; |
| 1706 | |
| 1707 | ndentry = lookup_one_len(tname, tdentry, tlen); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1708 | host_err = PTR_ERR(ndentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | if (IS_ERR(ndentry)) |
| 1710 | goto out_dput_old; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1711 | host_err = -ENOTEMPTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | if (ndentry == trap) |
| 1713 | goto out_dput_new; |
| 1714 | |
Dave Hansen | 9079b1e | 2008-02-15 14:37:49 -0800 | [diff] [blame] | 1715 | host_err = -EXDEV; |
| 1716 | if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt) |
| 1717 | goto out_dput_new; |
J. Bruce Fields | aa387d6c | 2013-04-15 16:03:46 -0400 | [diff] [blame] | 1718 | if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry) |
| 1719 | goto out_dput_new; |
Dave Hansen | 9079b1e | 2008-02-15 14:37:49 -0800 | [diff] [blame] | 1720 | |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 1721 | host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0); |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1722 | if (!host_err) { |
| 1723 | host_err = commit_metadata(tfhp); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1724 | if (!host_err) |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1725 | host_err = commit_metadata(ffhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | out_dput_new: |
| 1728 | dput(ndentry); |
| 1729 | out_dput_old: |
| 1730 | dput(odentry); |
| 1731 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1732 | err = nfserrno(host_err); |
J. Bruce Fields | fbb74a3 | 2014-03-28 16:43:17 -0400 | [diff] [blame] | 1733 | /* |
| 1734 | * We cannot rely on fh_unlock on the two filehandles, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | * as that would do the wrong thing if the two directories |
J. Bruce Fields | fbb74a3 | 2014-03-28 16:43:17 -0400 | [diff] [blame] | 1736 | * were the same, so again we do it by hand. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | */ |
| 1738 | fill_post_wcc(ffhp); |
| 1739 | fill_post_wcc(tfhp); |
| 1740 | unlock_rename(tdentry, fdentry); |
| 1741 | ffhp->fh_locked = tfhp->fh_locked = 0; |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1742 | fh_drop_write(ffhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | |
| 1744 | out: |
| 1745 | return err; |
| 1746 | } |
| 1747 | |
| 1748 | /* |
| 1749 | * Unlink a file or directory |
| 1750 | * N.B. After this call fhp needs an fh_put |
| 1751 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1752 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, |
| 1754 | char *fname, int flen) |
| 1755 | { |
| 1756 | struct dentry *dentry, *rdentry; |
| 1757 | struct inode *dirp; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1758 | __be32 err; |
| 1759 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | |
| 1761 | err = nfserr_acces; |
| 1762 | if (!flen || isdotent(fname, flen)) |
| 1763 | goto out; |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1764 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | if (err) |
| 1766 | goto out; |
| 1767 | |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1768 | host_err = fh_want_write(fhp); |
| 1769 | if (host_err) |
| 1770 | goto out_nfserr; |
| 1771 | |
Peter Zijlstra | 12fd352 | 2006-10-02 02:18:03 -0700 | [diff] [blame] | 1772 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | dentry = fhp->fh_dentry; |
| 1774 | dirp = dentry->d_inode; |
| 1775 | |
| 1776 | rdentry = lookup_one_len(fname, dentry, flen); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1777 | host_err = PTR_ERR(rdentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | if (IS_ERR(rdentry)) |
| 1779 | goto out_nfserr; |
| 1780 | |
| 1781 | if (!rdentry->d_inode) { |
| 1782 | dput(rdentry); |
| 1783 | err = nfserr_noent; |
| 1784 | goto out; |
| 1785 | } |
| 1786 | |
| 1787 | if (!type) |
| 1788 | type = rdentry->d_inode->i_mode & S_IFMT; |
| 1789 | |
J. Bruce Fields | 9ce137e | 2011-01-11 14:07:12 -0500 | [diff] [blame] | 1790 | if (type != S_IFDIR) |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 1791 | host_err = vfs_unlink(dirp, rdentry, NULL); |
J. Bruce Fields | 9ce137e | 2011-01-11 14:07:12 -0500 | [diff] [blame] | 1792 | else |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1793 | host_err = vfs_rmdir(dirp, rdentry); |
J. Bruce Fields | 541ce98 | 2011-01-14 20:00:02 -0500 | [diff] [blame] | 1794 | if (!host_err) |
| 1795 | host_err = commit_metadata(fhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | dput(rdentry); |
| 1797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1799 | err = nfserrno(host_err); |
YAMAMOTO Takashi | f193fba | 2006-01-18 17:43:13 -0800 | [diff] [blame] | 1800 | out: |
| 1801 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | /* |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1805 | * We do this buffering because we must not call back into the file |
| 1806 | * system's ->lookup() method from the filldir callback. That may well |
| 1807 | * deadlock a number of file systems. |
| 1808 | * |
| 1809 | * This is based heavily on the implementation of same in XFS. |
| 1810 | */ |
| 1811 | struct buffered_dirent { |
| 1812 | u64 ino; |
| 1813 | loff_t offset; |
| 1814 | int namlen; |
| 1815 | unsigned int d_type; |
| 1816 | char name[]; |
| 1817 | }; |
| 1818 | |
| 1819 | struct readdir_data { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 1820 | struct dir_context ctx; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1821 | char *dirent; |
| 1822 | size_t used; |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 1823 | int full; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1824 | }; |
| 1825 | |
| 1826 | static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen, |
| 1827 | loff_t offset, u64 ino, unsigned int d_type) |
David Woodhouse | 2628b76 | 2008-07-31 17:16:51 +0100 | [diff] [blame] | 1828 | { |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1829 | struct readdir_data *buf = __buf; |
| 1830 | struct buffered_dirent *de = (void *)(buf->dirent + buf->used); |
| 1831 | unsigned int reclen; |
| 1832 | |
| 1833 | reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64)); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 1834 | if (buf->used + reclen > PAGE_SIZE) { |
| 1835 | buf->full = 1; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1836 | return -EINVAL; |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 1837 | } |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1838 | |
| 1839 | de->namlen = namlen; |
| 1840 | de->offset = offset; |
| 1841 | de->ino = ino; |
| 1842 | de->d_type = d_type; |
| 1843 | memcpy(de->name, name, namlen); |
| 1844 | buf->used += reclen; |
| 1845 | |
| 1846 | return 0; |
| 1847 | } |
| 1848 | |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 1849 | static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func, |
| 1850 | struct readdir_cd *cdp, loff_t *offsetp) |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1851 | { |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1852 | struct buffered_dirent *de; |
David Woodhouse | 2628b76 | 2008-07-31 17:16:51 +0100 | [diff] [blame] | 1853 | int host_err; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1854 | int size; |
| 1855 | loff_t offset; |
Al Viro | ac6614b | 2013-05-22 22:22:04 -0400 | [diff] [blame] | 1856 | struct readdir_data buf = { |
| 1857 | .ctx.actor = nfsd_buffered_filldir, |
| 1858 | .dirent = (void *)__get_free_page(GFP_KERNEL) |
| 1859 | }; |
David Woodhouse | 2628b76 | 2008-07-31 17:16:51 +0100 | [diff] [blame] | 1860 | |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1861 | if (!buf.dirent) |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 1862 | return nfserrno(-ENOMEM); |
David Woodhouse | 2628b76 | 2008-07-31 17:16:51 +0100 | [diff] [blame] | 1863 | |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1864 | offset = *offsetp; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1865 | |
| 1866 | while (1) { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1867 | struct inode *dir_inode = file_inode(file); |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1868 | unsigned int reclen; |
| 1869 | |
Doug Nazar | b726e92 | 2008-11-05 06:16:28 -0500 | [diff] [blame] | 1870 | cdp->err = nfserr_eof; /* will be cleared on successful read */ |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1871 | buf.used = 0; |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 1872 | buf.full = 0; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1873 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 1874 | host_err = iterate_dir(file, &buf.ctx); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 1875 | if (buf.full) |
| 1876 | host_err = 0; |
| 1877 | |
| 1878 | if (host_err < 0) |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1879 | break; |
| 1880 | |
| 1881 | size = buf.used; |
| 1882 | |
| 1883 | if (!size) |
| 1884 | break; |
| 1885 | |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 1886 | /* |
| 1887 | * Various filldir functions may end up calling back into |
| 1888 | * lookup_one_len() and the file system's ->lookup() method. |
| 1889 | * These expect i_mutex to be held, as it would within readdir. |
| 1890 | */ |
| 1891 | host_err = mutex_lock_killable(&dir_inode->i_mutex); |
| 1892 | if (host_err) |
| 1893 | break; |
| 1894 | |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1895 | de = (struct buffered_dirent *)buf.dirent; |
| 1896 | while (size > 0) { |
| 1897 | offset = de->offset; |
| 1898 | |
| 1899 | if (func(cdp, de->name, de->namlen, de->offset, |
| 1900 | de->ino, de->d_type)) |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 1901 | break; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1902 | |
| 1903 | if (cdp->err != nfs_ok) |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 1904 | break; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1905 | |
| 1906 | reclen = ALIGN(sizeof(*de) + de->namlen, |
| 1907 | sizeof(u64)); |
| 1908 | size -= reclen; |
| 1909 | de = (struct buffered_dirent *)((char *)de + reclen); |
| 1910 | } |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 1911 | mutex_unlock(&dir_inode->i_mutex); |
| 1912 | if (size > 0) /* We bailed out early */ |
| 1913 | break; |
| 1914 | |
David Woodhouse | c002a6c | 2008-08-17 17:21:18 +0100 | [diff] [blame] | 1915 | offset = vfs_llseek(file, 0, SEEK_CUR); |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1916 | } |
| 1917 | |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1918 | free_page((unsigned long)(buf.dirent)); |
David Woodhouse | 2628b76 | 2008-07-31 17:16:51 +0100 | [diff] [blame] | 1919 | |
| 1920 | if (host_err) |
| 1921 | return nfserrno(host_err); |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1922 | |
| 1923 | *offsetp = offset; |
| 1924 | return cdp->err; |
David Woodhouse | 2628b76 | 2008-07-31 17:16:51 +0100 | [diff] [blame] | 1925 | } |
| 1926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | /* |
| 1928 | * Read entries from a directory. |
| 1929 | * The NFSv3/4 verifier we ignore for now. |
| 1930 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1931 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp, |
NeilBrown | a0ad13e | 2007-01-26 00:57:10 -0800 | [diff] [blame] | 1933 | struct readdir_cd *cdp, filldir_t func) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | { |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1935 | __be32 err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | struct file *file; |
| 1937 | loff_t offset = *offsetp; |
Bernd Schubert | 06effdb | 2012-03-18 22:44:50 -0400 | [diff] [blame] | 1938 | int may_flags = NFSD_MAY_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | |
Bernd Schubert | 06effdb | 2012-03-18 22:44:50 -0400 | [diff] [blame] | 1940 | /* NFSv2 only supports 32 bit cookies */ |
| 1941 | if (rqstp->rq_vers > 2) |
| 1942 | may_flags |= NFSD_MAY_64BIT_COOKIE; |
| 1943 | |
| 1944 | err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | if (err) |
| 1946 | goto out; |
| 1947 | |
Jeff Layton | b108fe6 | 2012-04-25 15:30:00 -0400 | [diff] [blame] | 1948 | offset = vfs_llseek(file, offset, SEEK_SET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | if (offset < 0) { |
| 1950 | err = nfserrno((int)offset); |
| 1951 | goto out_close; |
| 1952 | } |
| 1953 | |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1954 | err = nfsd_buffered_readdir(file, func, cdp, offsetp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | |
| 1956 | if (err == nfserr_eof || err == nfserr_toosmall) |
| 1957 | err = nfs_ok; /* can still be found in ->err */ |
| 1958 | out_close: |
| 1959 | nfsd_close(file); |
| 1960 | out: |
| 1961 | return err; |
| 1962 | } |
| 1963 | |
| 1964 | /* |
| 1965 | * Get file system stats |
| 1966 | * N.B. After this call fhp needs an fh_put |
| 1967 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1968 | __be32 |
J. Bruce Fields | 04716e6 | 2008-08-07 13:00:20 -0400 | [diff] [blame] | 1969 | nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | { |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame] | 1971 | __be32 err; |
| 1972 | |
| 1973 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access); |
Takashi Iwai | f6360ef | 2010-08-13 15:53:49 +0200 | [diff] [blame] | 1974 | if (!err) { |
| 1975 | struct path path = { |
| 1976 | .mnt = fhp->fh_export->ex_path.mnt, |
| 1977 | .dentry = fhp->fh_dentry, |
| 1978 | }; |
| 1979 | if (vfs_statfs(&path, stat)) |
| 1980 | err = nfserr_io; |
| 1981 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | return err; |
| 1983 | } |
| 1984 | |
J. Bruce Fields | c7d5140 | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 1985 | static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp) |
J. Bruce Fields | e22841c | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 1986 | { |
J. Bruce Fields | c7d5140 | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 1987 | return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY; |
J. Bruce Fields | e22841c | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | /* |
| 1991 | * Check for a user's access permissions to this inode. |
| 1992 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1993 | __be32 |
J. Bruce Fields | 0ec757d | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 1994 | nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp, |
| 1995 | struct dentry *dentry, int acc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | { |
| 1997 | struct inode *inode = dentry->d_inode; |
| 1998 | int err; |
| 1999 | |
J. Bruce Fields | aea9339 | 2011-04-10 10:35:12 -0400 | [diff] [blame] | 2000 | if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | return 0; |
| 2002 | #if 0 |
| 2003 | dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n", |
| 2004 | acc, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2005 | (acc & NFSD_MAY_READ)? " read" : "", |
| 2006 | (acc & NFSD_MAY_WRITE)? " write" : "", |
| 2007 | (acc & NFSD_MAY_EXEC)? " exec" : "", |
| 2008 | (acc & NFSD_MAY_SATTR)? " sattr" : "", |
| 2009 | (acc & NFSD_MAY_TRUNC)? " trunc" : "", |
| 2010 | (acc & NFSD_MAY_LOCK)? " lock" : "", |
| 2011 | (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | inode->i_mode, |
| 2013 | IS_IMMUTABLE(inode)? " immut" : "", |
| 2014 | IS_APPEND(inode)? " append" : "", |
Dave Hansen | 2c463e9 | 2008-02-15 14:37:56 -0800 | [diff] [blame] | 2015 | __mnt_is_readonly(exp->ex_path.mnt)? " ro" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | dprintk(" owner %d/%d user %d/%d\n", |
David Howells | 5cc0a84 | 2008-11-14 10:38:58 +1100 | [diff] [blame] | 2017 | inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | #endif |
| 2019 | |
| 2020 | /* Normally we reject any write/sattr etc access on a read-only file |
| 2021 | * system. But if it is IRIX doing check on write-access for a |
| 2022 | * device special file, we ignore rofs. |
| 2023 | */ |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2024 | if (!(acc & NFSD_MAY_LOCAL_ACCESS)) |
| 2025 | if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) { |
Dave Hansen | 2c463e9 | 2008-02-15 14:37:56 -0800 | [diff] [blame] | 2026 | if (exp_rdonly(rqstp, exp) || |
| 2027 | __mnt_is_readonly(exp->ex_path.mnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | return nfserr_rofs; |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2029 | if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | return nfserr_perm; |
| 2031 | } |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2032 | if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | return nfserr_perm; |
| 2034 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2035 | if (acc & NFSD_MAY_LOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | /* If we cannot rely on authentication in NLM requests, |
| 2037 | * just allow locks, otherwise require read permission, or |
| 2038 | * ownership |
| 2039 | */ |
| 2040 | if (exp->ex_flags & NFSEXP_NOAUTHNLM) |
| 2041 | return 0; |
| 2042 | else |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2043 | acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | } |
| 2045 | /* |
| 2046 | * The file owner always gets access permission for accesses that |
| 2047 | * would normally be checked at open time. This is to make |
| 2048 | * file access work even when the client has done a fchmod(fd, 0). |
| 2049 | * |
| 2050 | * However, `cp foo bar' should fail nevertheless when bar is |
| 2051 | * readonly. A sensible way to do this might be to reject all |
| 2052 | * attempts to truncate a read-only file, because a creat() call |
| 2053 | * always implies file truncation. |
| 2054 | * ... but this isn't really fair. A process may reasonably call |
| 2055 | * ftruncate on an open file descriptor on a file with perm 000. |
| 2056 | * We must trust the client to do permission checking - using "ACCESS" |
| 2057 | * with NFSv3. |
| 2058 | */ |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2059 | if ((acc & NFSD_MAY_OWNER_OVERRIDE) && |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 2060 | uid_eq(inode->i_uid, current_fsuid())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | return 0; |
| 2062 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2063 | /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */ |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 2064 | err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | |
| 2066 | /* Allow read access to binaries even when mode 111 */ |
| 2067 | if (err == -EACCES && S_ISREG(inode->i_mode) && |
J. Bruce Fields | a043226 | 2011-08-25 10:48:39 -0400 | [diff] [blame] | 2068 | (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) || |
| 2069 | acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC))) |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 2070 | err = inode_permission(inode, MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | |
| 2072 | return err? nfserrno(err) : 0; |
| 2073 | } |
| 2074 | |
| 2075 | void |
| 2076 | nfsd_racache_shutdown(void) |
| 2077 | { |
Jeff Layton | 54a66e5 | 2008-08-13 22:03:27 -0400 | [diff] [blame] | 2078 | struct raparms *raparm, *last_raparm; |
| 2079 | unsigned int i; |
| 2080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | dprintk("nfsd: freeing readahead buffers.\n"); |
Jeff Layton | 54a66e5 | 2008-08-13 22:03:27 -0400 | [diff] [blame] | 2082 | |
| 2083 | for (i = 0; i < RAPARM_HASH_SIZE; i++) { |
| 2084 | raparm = raparm_hash[i].pb_head; |
| 2085 | while(raparm) { |
| 2086 | last_raparm = raparm; |
| 2087 | raparm = raparm->p_next; |
| 2088 | kfree(last_raparm); |
| 2089 | } |
| 2090 | raparm_hash[i].pb_head = NULL; |
| 2091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | } |
| 2093 | /* |
| 2094 | * Initialize readahead param cache |
| 2095 | */ |
| 2096 | int |
| 2097 | nfsd_racache_init(int cache_size) |
| 2098 | { |
| 2099 | int i; |
Greg Banks | fce1456 | 2006-10-04 02:15:49 -0700 | [diff] [blame] | 2100 | int j = 0; |
| 2101 | int nperbucket; |
Jeff Layton | 54a66e5 | 2008-08-13 22:03:27 -0400 | [diff] [blame] | 2102 | struct raparms **raparm = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | |
Greg Banks | fce1456 | 2006-10-04 02:15:49 -0700 | [diff] [blame] | 2104 | |
Jeff Layton | 54a66e5 | 2008-08-13 22:03:27 -0400 | [diff] [blame] | 2105 | if (raparm_hash[0].pb_head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | return 0; |
Jeff Layton | 54a66e5 | 2008-08-13 22:03:27 -0400 | [diff] [blame] | 2107 | nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE); |
Kinglong Mee | 3c7aa15 | 2014-06-10 18:08:19 +0800 | [diff] [blame] | 2108 | nperbucket = max(2, nperbucket); |
Jeff Layton | 54a66e5 | 2008-08-13 22:03:27 -0400 | [diff] [blame] | 2109 | cache_size = nperbucket * RAPARM_HASH_SIZE; |
Yan Burman | 4b3bb06 | 2006-12-08 02:39:41 -0800 | [diff] [blame] | 2110 | |
| 2111 | dprintk("nfsd: allocating %d readahead buffers.\n", cache_size); |
Jeff Layton | 54a66e5 | 2008-08-13 22:03:27 -0400 | [diff] [blame] | 2112 | |
| 2113 | for (i = 0; i < RAPARM_HASH_SIZE; i++) { |
Yan Burman | 4b3bb06 | 2006-12-08 02:39:41 -0800 | [diff] [blame] | 2114 | spin_lock_init(&raparm_hash[i].pb_lock); |
Jeff Layton | 54a66e5 | 2008-08-13 22:03:27 -0400 | [diff] [blame] | 2115 | |
| 2116 | raparm = &raparm_hash[i].pb_head; |
| 2117 | for (j = 0; j < nperbucket; j++) { |
| 2118 | *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL); |
| 2119 | if (!*raparm) |
| 2120 | goto out_nomem; |
| 2121 | raparm = &(*raparm)->p_next; |
| 2122 | } |
| 2123 | *raparm = NULL; |
Yan Burman | 4b3bb06 | 2006-12-08 02:39:41 -0800 | [diff] [blame] | 2124 | } |
| 2125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | nfsdstats.ra_size = cache_size; |
| 2127 | return 0; |
Jeff Layton | 54a66e5 | 2008-08-13 22:03:27 -0400 | [diff] [blame] | 2128 | |
| 2129 | out_nomem: |
| 2130 | dprintk("nfsd: kmalloc failed, freeing readahead buffers\n"); |
| 2131 | nfsd_racache_shutdown(); |
| 2132 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | } |