Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/nfs/nfs4proc.c |
| 3 | * |
| 4 | * Client-side procedure declarations for NFSv4. |
| 5 | * |
| 6 | * Copyright (c) 2002 The Regents of the University of Michigan. |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Kendrick Smith <kmsmith@umich.edu> |
| 10 | * Andy Adamson <andros@umich.edu> |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or without |
| 13 | * modification, are permitted provided that the following conditions |
| 14 | * are met: |
| 15 | * |
| 16 | * 1. Redistributions of source code must retain the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer. |
| 18 | * 2. Redistributions in binary form must reproduce the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer in the |
| 20 | * documentation and/or other materials provided with the distribution. |
| 21 | * 3. Neither the name of the University nor the names of its |
| 22 | * contributors may be used to endorse or promote products derived |
| 23 | * from this software without specific prior written permission. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 26 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 27 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 28 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 32 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 34 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | */ |
| 37 | |
| 38 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/delay.h> |
| 40 | #include <linux/errno.h> |
| 41 | #include <linux/string.h> |
| 42 | #include <linux/sunrpc/clnt.h> |
| 43 | #include <linux/nfs.h> |
| 44 | #include <linux/nfs4.h> |
| 45 | #include <linux/nfs_fs.h> |
| 46 | #include <linux/nfs_page.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/namei.h> |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 48 | #include <linux/mount.h> |
Benny Halevy | 99fe60d | 2009-04-01 09:22:29 -0400 | [diff] [blame] | 49 | #include <linux/module.h> |
Andy Adamson | 5a0ffe5 | 2009-04-01 09:23:18 -0400 | [diff] [blame] | 50 | #include <linux/sunrpc/bc_xprt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 52 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include "delegation.h" |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 54 | #include "internal.h" |
Chuck Lever | 006ea73 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 55 | #include "iostat.h" |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 56 | #include "callback.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | #define NFSDBG_FACILITY NFSDBG_PROC |
| 59 | |
Trond Myklebust | 2066fe8 | 2006-09-15 08:30:46 -0400 | [diff] [blame] | 60 | #define NFS4_POLL_RETRY_MIN (HZ/10) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #define NFS4_POLL_RETRY_MAX (15*HZ) |
| 62 | |
Trond Myklebust | a78cb57 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 63 | #define NFS4_MAX_LOOP_ON_RECOVER (10) |
| 64 | |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 65 | struct nfs4_opendata; |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 66 | static int _nfs4_proc_open(struct nfs4_opendata *data); |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 67 | static int _nfs4_recover_proc_open(struct nfs4_opendata *data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *); |
Trond Myklebust | 9e33bed | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 69 | static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *); |
Trond Myklebust | 9936781 | 2007-07-17 21:52:41 -0400 | [diff] [blame] | 70 | static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr); |
| 71 | static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /* Prevent leaks of NFSv4 errors into userland */ |
WANG Cong | 46f72f5 | 2008-12-30 16:35:55 -0500 | [diff] [blame] | 74 | static int nfs4_map_errors(int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Trond Myklebust | 52567b0 | 2009-10-23 14:46:42 -0400 | [diff] [blame] | 76 | if (err >= -1000) |
| 77 | return err; |
| 78 | switch (err) { |
| 79 | case -NFS4ERR_RESOURCE: |
| 80 | return -EREMOTEIO; |
| 81 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | dprintk("%s could not handle NFSv4 error %d\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 83 | __func__, -err); |
Trond Myklebust | 52567b0 | 2009-10-23 14:46:42 -0400 | [diff] [blame] | 84 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
Trond Myklebust | 52567b0 | 2009-10-23 14:46:42 -0400 | [diff] [blame] | 86 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* |
| 90 | * This is our standard bitmap for GETATTR requests. |
| 91 | */ |
| 92 | const u32 nfs4_fattr_bitmap[2] = { |
| 93 | FATTR4_WORD0_TYPE |
| 94 | | FATTR4_WORD0_CHANGE |
| 95 | | FATTR4_WORD0_SIZE |
| 96 | | FATTR4_WORD0_FSID |
| 97 | | FATTR4_WORD0_FILEID, |
| 98 | FATTR4_WORD1_MODE |
| 99 | | FATTR4_WORD1_NUMLINKS |
| 100 | | FATTR4_WORD1_OWNER |
| 101 | | FATTR4_WORD1_OWNER_GROUP |
| 102 | | FATTR4_WORD1_RAWDEV |
| 103 | | FATTR4_WORD1_SPACE_USED |
| 104 | | FATTR4_WORD1_TIME_ACCESS |
| 105 | | FATTR4_WORD1_TIME_METADATA |
| 106 | | FATTR4_WORD1_TIME_MODIFY |
| 107 | }; |
| 108 | |
| 109 | const u32 nfs4_statfs_bitmap[2] = { |
| 110 | FATTR4_WORD0_FILES_AVAIL |
| 111 | | FATTR4_WORD0_FILES_FREE |
| 112 | | FATTR4_WORD0_FILES_TOTAL, |
| 113 | FATTR4_WORD1_SPACE_AVAIL |
| 114 | | FATTR4_WORD1_SPACE_FREE |
| 115 | | FATTR4_WORD1_SPACE_TOTAL |
| 116 | }; |
| 117 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 118 | const u32 nfs4_pathconf_bitmap[2] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | FATTR4_WORD0_MAXLINK |
| 120 | | FATTR4_WORD0_MAXNAME, |
| 121 | 0 |
| 122 | }; |
| 123 | |
| 124 | const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE |
| 125 | | FATTR4_WORD0_MAXREAD |
| 126 | | FATTR4_WORD0_MAXWRITE |
| 127 | | FATTR4_WORD0_LEASE_TIME, |
| 128 | 0 |
| 129 | }; |
| 130 | |
Manoj Naik | 830b8e3 | 2006-06-09 09:34:25 -0400 | [diff] [blame] | 131 | const u32 nfs4_fs_locations_bitmap[2] = { |
| 132 | FATTR4_WORD0_TYPE |
| 133 | | FATTR4_WORD0_CHANGE |
| 134 | | FATTR4_WORD0_SIZE |
| 135 | | FATTR4_WORD0_FSID |
| 136 | | FATTR4_WORD0_FILEID |
| 137 | | FATTR4_WORD0_FS_LOCATIONS, |
| 138 | FATTR4_WORD1_MODE |
| 139 | | FATTR4_WORD1_NUMLINKS |
| 140 | | FATTR4_WORD1_OWNER |
| 141 | | FATTR4_WORD1_OWNER_GROUP |
| 142 | | FATTR4_WORD1_RAWDEV |
| 143 | | FATTR4_WORD1_SPACE_USED |
| 144 | | FATTR4_WORD1_TIME_ACCESS |
| 145 | | FATTR4_WORD1_TIME_METADATA |
| 146 | | FATTR4_WORD1_TIME_MODIFY |
| 147 | | FATTR4_WORD1_MOUNTED_ON_FILEID |
| 148 | }; |
| 149 | |
Al Viro | bc4785c | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 150 | static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | struct nfs4_readdir_arg *readdir) |
| 152 | { |
Al Viro | 0dbb4c6 | 2006-10-19 23:28:49 -0700 | [diff] [blame] | 153 | __be32 *start, *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | BUG_ON(readdir->count < 80); |
| 156 | if (cookie > 2) { |
Adrian Bunk | b7ef195 | 2005-06-22 17:16:28 +0000 | [diff] [blame] | 157 | readdir->cookie = cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier)); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | readdir->cookie = 0; |
| 163 | memset(&readdir->verifier, 0, sizeof(readdir->verifier)); |
| 164 | if (cookie == 2) |
| 165 | return; |
| 166 | |
| 167 | /* |
| 168 | * NFSv4 servers do not return entries for '.' and '..' |
| 169 | * Therefore, we fake these entries here. We let '.' |
| 170 | * have cookie 0 and '..' have cookie 1. Note that |
| 171 | * when talking to the server, we always send cookie 0 |
| 172 | * instead of 1 or 2. |
| 173 | */ |
Al Viro | 0dbb4c6 | 2006-10-19 23:28:49 -0700 | [diff] [blame] | 174 | start = p = kmap_atomic(*readdir->pages, KM_USER0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | if (cookie == 0) { |
| 177 | *p++ = xdr_one; /* next */ |
| 178 | *p++ = xdr_zero; /* cookie, first word */ |
| 179 | *p++ = xdr_one; /* cookie, second word */ |
| 180 | *p++ = xdr_one; /* entry len */ |
| 181 | memcpy(p, ".\0\0\0", 4); /* entry */ |
| 182 | p++; |
| 183 | *p++ = xdr_one; /* bitmap length */ |
| 184 | *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */ |
| 185 | *p++ = htonl(8); /* attribute buffer length */ |
Peter Staubach | 4e769b9 | 2007-08-03 15:07:10 -0400 | [diff] [blame] | 186 | p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | *p++ = xdr_one; /* next */ |
| 190 | *p++ = xdr_zero; /* cookie, first word */ |
| 191 | *p++ = xdr_two; /* cookie, second word */ |
| 192 | *p++ = xdr_two; /* entry len */ |
| 193 | memcpy(p, "..\0\0", 4); /* entry */ |
| 194 | p++; |
| 195 | *p++ = xdr_one; /* bitmap length */ |
| 196 | *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */ |
| 197 | *p++ = htonl(8); /* attribute buffer length */ |
Peter Staubach | 4e769b9 | 2007-08-03 15:07:10 -0400 | [diff] [blame] | 198 | p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | readdir->pgbase = (char *)p - (char *)start; |
| 201 | readdir->count -= readdir->pgbase; |
| 202 | kunmap_atomic(start, KM_USER0); |
| 203 | } |
| 204 | |
Trond Myklebust | 65de872 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 205 | static int nfs4_wait_clnt_recover(struct nfs_client *clp) |
| 206 | { |
| 207 | int res; |
| 208 | |
| 209 | might_sleep(); |
| 210 | |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 211 | res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING, |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 212 | nfs_wait_bit_killable, TASK_KILLABLE); |
Trond Myklebust | 65de872 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 213 | return res; |
| 214 | } |
| 215 | |
| 216 | static int nfs4_delay(struct rpc_clnt *clnt, long *timeout) |
| 217 | { |
| 218 | int res = 0; |
| 219 | |
| 220 | might_sleep(); |
| 221 | |
| 222 | if (*timeout <= 0) |
| 223 | *timeout = NFS4_POLL_RETRY_MIN; |
| 224 | if (*timeout > NFS4_POLL_RETRY_MAX) |
| 225 | *timeout = NFS4_POLL_RETRY_MAX; |
| 226 | schedule_timeout_killable(*timeout); |
| 227 | if (fatal_signal_pending(current)) |
| 228 | res = -ERESTARTSYS; |
| 229 | *timeout <<= 1; |
| 230 | return res; |
| 231 | } |
| 232 | |
| 233 | /* This is the error handling routine for processes that are allowed |
| 234 | * to sleep. |
| 235 | */ |
| 236 | static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception) |
| 237 | { |
| 238 | struct nfs_client *clp = server->nfs_client; |
Trond Myklebust | 9e33bed | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 239 | struct nfs4_state *state = exception->state; |
Trond Myklebust | 65de872 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 240 | int ret = errorcode; |
| 241 | |
| 242 | exception->retry = 0; |
| 243 | switch(errorcode) { |
| 244 | case 0: |
| 245 | return 0; |
Trond Myklebust | 9e33bed | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 246 | case -NFS4ERR_ADMIN_REVOKED: |
| 247 | case -NFS4ERR_BAD_STATEID: |
| 248 | case -NFS4ERR_OPENMODE: |
| 249 | if (state == NULL) |
| 250 | break; |
| 251 | nfs4_state_mark_reclaim_nograce(clp, state); |
Trond Myklebust | 65de872 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 252 | case -NFS4ERR_STALE_CLIENTID: |
| 253 | case -NFS4ERR_STALE_STATEID: |
| 254 | case -NFS4ERR_EXPIRED: |
| 255 | nfs4_schedule_state_recovery(clp); |
| 256 | ret = nfs4_wait_clnt_recover(clp); |
| 257 | if (ret == 0) |
| 258 | exception->retry = 1; |
Andy Adamson | 4745e31 | 2009-04-01 09:22:42 -0400 | [diff] [blame] | 259 | #if !defined(CONFIG_NFS_V4_1) |
Trond Myklebust | 65de872 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 260 | break; |
Andy Adamson | 4745e31 | 2009-04-01 09:22:42 -0400 | [diff] [blame] | 261 | #else /* !defined(CONFIG_NFS_V4_1) */ |
| 262 | if (!nfs4_has_session(server->nfs_client)) |
| 263 | break; |
| 264 | /* FALLTHROUGH */ |
| 265 | case -NFS4ERR_BADSESSION: |
| 266 | case -NFS4ERR_BADSLOT: |
| 267 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 268 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
| 269 | case -NFS4ERR_DEADSESSION: |
| 270 | case -NFS4ERR_SEQ_FALSE_RETRY: |
| 271 | case -NFS4ERR_SEQ_MISORDERED: |
| 272 | dprintk("%s ERROR: %d Reset session\n", __func__, |
| 273 | errorcode); |
Andy Adamson | 0b9e2d4 | 2009-12-04 16:02:14 -0500 | [diff] [blame] | 274 | nfs4_schedule_state_recovery(clp); |
Andy Adamson | 4745e31 | 2009-04-01 09:22:42 -0400 | [diff] [blame] | 275 | exception->retry = 1; |
Andy Adamson | b917923 | 2009-12-04 15:55:32 -0500 | [diff] [blame] | 276 | break; |
Andy Adamson | 4745e31 | 2009-04-01 09:22:42 -0400 | [diff] [blame] | 277 | #endif /* !defined(CONFIG_NFS_V4_1) */ |
Trond Myklebust | 65de872 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 278 | case -NFS4ERR_FILE_OPEN: |
NeilBrown | 44ed355 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 279 | if (exception->timeout > HZ) { |
| 280 | /* We have retried a decent amount, time to |
| 281 | * fail |
| 282 | */ |
| 283 | ret = -EBUSY; |
| 284 | break; |
| 285 | } |
Trond Myklebust | 65de872 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 286 | case -NFS4ERR_GRACE: |
| 287 | case -NFS4ERR_DELAY: |
| 288 | ret = nfs4_delay(server->client, &exception->timeout); |
| 289 | if (ret != 0) |
| 290 | break; |
| 291 | case -NFS4ERR_OLD_STATEID: |
| 292 | exception->retry = 1; |
| 293 | } |
| 294 | /* We failed to handle the error */ |
| 295 | return nfs4_map_errors(ret); |
| 296 | } |
| 297 | |
| 298 | |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 299 | static void renew_lease(const struct nfs_server *server, unsigned long timestamp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 301 | struct nfs_client *clp = server->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | spin_lock(&clp->cl_lock); |
| 303 | if (time_before(clp->cl_last_renewal,timestamp)) |
| 304 | clp->cl_last_renewal = timestamp; |
| 305 | spin_unlock(&clp->cl_lock); |
| 306 | } |
| 307 | |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 308 | #if defined(CONFIG_NFS_V4_1) |
| 309 | |
Benny Halevy | 510b817 | 2009-04-01 09:22:14 -0400 | [diff] [blame] | 310 | /* |
Andy Adamson | e2c4ab3 | 2009-04-01 09:22:16 -0400 | [diff] [blame] | 311 | * nfs4_free_slot - free a slot and efficiently update slot table. |
| 312 | * |
| 313 | * freeing a slot is trivially done by clearing its respective bit |
| 314 | * in the bitmap. |
| 315 | * If the freed slotid equals highest_used_slotid we want to update it |
| 316 | * so that the server would be able to size down the slot table if needed, |
| 317 | * otherwise we know that the highest_used_slotid is still in use. |
| 318 | * When updating highest_used_slotid there may be "holes" in the bitmap |
| 319 | * so we need to scan down from highest_used_slotid to 0 looking for the now |
| 320 | * highest slotid in use. |
| 321 | * If none found, highest_used_slotid is set to -1. |
Trond Myklebust | 35dc1d7 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 322 | * |
| 323 | * Must be called while holding tbl->slot_tbl_lock |
Andy Adamson | e2c4ab3 | 2009-04-01 09:22:16 -0400 | [diff] [blame] | 324 | */ |
| 325 | static void |
| 326 | nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid) |
| 327 | { |
| 328 | int slotid = free_slotid; |
| 329 | |
Andy Adamson | e2c4ab3 | 2009-04-01 09:22:16 -0400 | [diff] [blame] | 330 | /* clear used bit in bitmap */ |
| 331 | __clear_bit(slotid, tbl->used_slots); |
| 332 | |
| 333 | /* update highest_used_slotid when it is freed */ |
| 334 | if (slotid == tbl->highest_used_slotid) { |
| 335 | slotid = find_last_bit(tbl->used_slots, tbl->max_slots); |
Trond Myklebust | bcb5616 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 336 | if (slotid < tbl->max_slots) |
Andy Adamson | e2c4ab3 | 2009-04-01 09:22:16 -0400 | [diff] [blame] | 337 | tbl->highest_used_slotid = slotid; |
| 338 | else |
| 339 | tbl->highest_used_slotid = -1; |
| 340 | } |
Andy Adamson | e2c4ab3 | 2009-04-01 09:22:16 -0400 | [diff] [blame] | 341 | dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__, |
| 342 | free_slotid, tbl->highest_used_slotid); |
| 343 | } |
| 344 | |
Alexandros Batsakis | 3bfb0fc | 2009-12-09 01:50:11 -0800 | [diff] [blame] | 345 | /* |
| 346 | * Signal state manager thread if session is drained |
| 347 | */ |
| 348 | static void nfs41_check_drain_session_complete(struct nfs4_session *ses) |
| 349 | { |
Alexandros Batsakis | 689cf5c | 2009-12-14 21:27:56 -0800 | [diff] [blame] | 350 | struct rpc_task *task; |
| 351 | |
Alexandros Batsakis | 3bfb0fc | 2009-12-09 01:50:11 -0800 | [diff] [blame] | 352 | if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) { |
Alexandros Batsakis | 689cf5c | 2009-12-14 21:27:56 -0800 | [diff] [blame] | 353 | task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq); |
| 354 | if (task) |
| 355 | rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED); |
Alexandros Batsakis | 3bfb0fc | 2009-12-09 01:50:11 -0800 | [diff] [blame] | 356 | return; |
| 357 | } |
| 358 | |
| 359 | if (ses->fc_slot_table.highest_used_slotid != -1) |
| 360 | return; |
| 361 | |
| 362 | dprintk("%s COMPLETE: Session Drained\n", __func__); |
| 363 | complete(&ses->complete); |
| 364 | } |
| 365 | |
Trond Myklebust | d61e612 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 366 | static void nfs41_sequence_free_slot(const struct nfs_client *clp, |
Andy Adamson | 1361587 | 2009-04-01 09:22:17 -0400 | [diff] [blame] | 367 | struct nfs4_sequence_res *res) |
| 368 | { |
| 369 | struct nfs4_slot_table *tbl; |
| 370 | |
Andy Adamson | 1361587 | 2009-04-01 09:22:17 -0400 | [diff] [blame] | 371 | tbl = &clp->cl_session->fc_slot_table; |
| 372 | if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) { |
Andy Adamson | 1361587 | 2009-04-01 09:22:17 -0400 | [diff] [blame] | 373 | /* just wake up the next guy waiting since |
| 374 | * we may have not consumed a slot after all */ |
Andy Adamson | 691daf3 | 2009-12-04 15:55:39 -0500 | [diff] [blame] | 375 | dprintk("%s: No slot\n", __func__); |
Trond Myklebust | 35dc1d7 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 376 | return; |
Andy Adamson | 1361587 | 2009-04-01 09:22:17 -0400 | [diff] [blame] | 377 | } |
Andy Adamson | ea028ac | 2009-12-04 15:55:38 -0500 | [diff] [blame] | 378 | |
Trond Myklebust | 35dc1d7 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 379 | spin_lock(&tbl->slot_tbl_lock); |
| 380 | nfs4_free_slot(tbl, res->sr_slotid); |
Alexandros Batsakis | 3bfb0fc | 2009-12-09 01:50:11 -0800 | [diff] [blame] | 381 | nfs41_check_drain_session_complete(clp->cl_session); |
Trond Myklebust | 35dc1d7 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 382 | spin_unlock(&tbl->slot_tbl_lock); |
| 383 | res->sr_slotid = NFS4_MAX_SLOT_TABLE; |
Andy Adamson | 1361587 | 2009-04-01 09:22:17 -0400 | [diff] [blame] | 384 | } |
| 385 | |
Andy Adamson | b0df806 | 2009-04-01 09:22:18 -0400 | [diff] [blame] | 386 | static void nfs41_sequence_done(struct nfs_client *clp, |
| 387 | struct nfs4_sequence_res *res, |
| 388 | int rpc_status) |
| 389 | { |
| 390 | unsigned long timestamp; |
| 391 | struct nfs4_slot_table *tbl; |
| 392 | struct nfs4_slot *slot; |
| 393 | |
| 394 | /* |
| 395 | * sr_status remains 1 if an RPC level error occurred. The server |
| 396 | * may or may not have processed the sequence operation.. |
| 397 | * Proceed as if the server received and processed the sequence |
| 398 | * operation. |
| 399 | */ |
| 400 | if (res->sr_status == 1) |
| 401 | res->sr_status = NFS_OK; |
| 402 | |
| 403 | /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */ |
| 404 | if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) |
| 405 | goto out; |
| 406 | |
Andy Adamson | 691daf3 | 2009-12-04 15:55:39 -0500 | [diff] [blame] | 407 | /* Check the SEQUENCE operation status */ |
Andy Adamson | b0df806 | 2009-04-01 09:22:18 -0400 | [diff] [blame] | 408 | if (res->sr_status == 0) { |
Andy Adamson | 691daf3 | 2009-12-04 15:55:39 -0500 | [diff] [blame] | 409 | tbl = &clp->cl_session->fc_slot_table; |
| 410 | slot = tbl->slots + res->sr_slotid; |
Andy Adamson | b0df806 | 2009-04-01 09:22:18 -0400 | [diff] [blame] | 411 | /* Update the slot's sequence and clientid lease timer */ |
| 412 | ++slot->seq_nr; |
| 413 | timestamp = res->sr_renewal_time; |
| 414 | spin_lock(&clp->cl_lock); |
| 415 | if (time_before(clp->cl_last_renewal, timestamp)) |
| 416 | clp->cl_last_renewal = timestamp; |
| 417 | spin_unlock(&clp->cl_lock); |
Alexandros Batsakis | 0629e37 | 2009-12-05 13:46:14 -0500 | [diff] [blame] | 418 | /* Check sequence flags */ |
| 419 | nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags); |
Andy Adamson | b0df806 | 2009-04-01 09:22:18 -0400 | [diff] [blame] | 420 | } |
| 421 | out: |
| 422 | /* The session may be reset by one of the error handlers. */ |
| 423 | dprintk("%s: Error %d free the slot \n", __func__, res->sr_status); |
| 424 | nfs41_sequence_free_slot(clp, res); |
| 425 | } |
| 426 | |
Andy Adamson | e2c4ab3 | 2009-04-01 09:22:16 -0400 | [diff] [blame] | 427 | /* |
Benny Halevy | 510b817 | 2009-04-01 09:22:14 -0400 | [diff] [blame] | 428 | * nfs4_find_slot - efficiently look for a free slot |
| 429 | * |
| 430 | * nfs4_find_slot looks for an unset bit in the used_slots bitmap. |
| 431 | * If found, we mark the slot as used, update the highest_used_slotid, |
| 432 | * and respectively set up the sequence operation args. |
| 433 | * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise. |
Andy Adamson | fbcd4ab | 2009-04-01 09:22:15 -0400 | [diff] [blame] | 434 | * |
| 435 | * Note: must be called with under the slot_tbl_lock. |
Benny Halevy | 510b817 | 2009-04-01 09:22:14 -0400 | [diff] [blame] | 436 | */ |
| 437 | static u8 |
Alexandros Batsakis | 40ead58 | 2009-12-14 21:27:54 -0800 | [diff] [blame] | 438 | nfs4_find_slot(struct nfs4_slot_table *tbl) |
Benny Halevy | 510b817 | 2009-04-01 09:22:14 -0400 | [diff] [blame] | 439 | { |
| 440 | int slotid; |
| 441 | u8 ret_id = NFS4_MAX_SLOT_TABLE; |
| 442 | BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE); |
| 443 | |
Benny Halevy | 510b817 | 2009-04-01 09:22:14 -0400 | [diff] [blame] | 444 | dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n", |
| 445 | __func__, tbl->used_slots[0], tbl->highest_used_slotid, |
| 446 | tbl->max_slots); |
| 447 | slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots); |
| 448 | if (slotid >= tbl->max_slots) |
| 449 | goto out; |
| 450 | __set_bit(slotid, tbl->used_slots); |
| 451 | if (slotid > tbl->highest_used_slotid) |
| 452 | tbl->highest_used_slotid = slotid; |
| 453 | ret_id = slotid; |
| 454 | out: |
| 455 | dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n", |
| 456 | __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id); |
Benny Halevy | 510b817 | 2009-04-01 09:22:14 -0400 | [diff] [blame] | 457 | return ret_id; |
| 458 | } |
| 459 | |
Andy Adamson | ce5039c | 2009-04-01 09:22:13 -0400 | [diff] [blame] | 460 | static int nfs41_setup_sequence(struct nfs4_session *session, |
| 461 | struct nfs4_sequence_args *args, |
| 462 | struct nfs4_sequence_res *res, |
| 463 | int cache_reply, |
| 464 | struct rpc_task *task) |
| 465 | { |
Andy Adamson | fbcd4ab | 2009-04-01 09:22:15 -0400 | [diff] [blame] | 466 | struct nfs4_slot *slot; |
| 467 | struct nfs4_slot_table *tbl; |
| 468 | u8 slotid; |
| 469 | |
| 470 | dprintk("--> %s\n", __func__); |
Andy Adamson | ce5039c | 2009-04-01 09:22:13 -0400 | [diff] [blame] | 471 | /* slot already allocated? */ |
| 472 | if (res->sr_slotid != NFS4_MAX_SLOT_TABLE) |
| 473 | return 0; |
| 474 | |
| 475 | memset(res, 0, sizeof(*res)); |
| 476 | res->sr_slotid = NFS4_MAX_SLOT_TABLE; |
Andy Adamson | fbcd4ab | 2009-04-01 09:22:15 -0400 | [diff] [blame] | 477 | tbl = &session->fc_slot_table; |
| 478 | |
| 479 | spin_lock(&tbl->slot_tbl_lock); |
Alexandros Batsakis | 5601a00 | 2009-12-14 21:27:58 -0800 | [diff] [blame] | 480 | if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) && |
| 481 | !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) { |
Andy Adamson | ea028ac | 2009-12-04 15:55:38 -0500 | [diff] [blame] | 482 | /* |
| 483 | * The state manager will wait until the slot table is empty. |
| 484 | * Schedule the reset thread |
| 485 | */ |
Andy Adamson | 05f0d23 | 2009-12-04 15:55:37 -0500 | [diff] [blame] | 486 | rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); |
Andy Adamson | b069d94 | 2009-04-01 09:22:43 -0400 | [diff] [blame] | 487 | spin_unlock(&tbl->slot_tbl_lock); |
Trond Myklebust | bcb5616 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 488 | dprintk("%s Schedule Session Reset\n", __func__); |
Andy Adamson | 05f0d23 | 2009-12-04 15:55:37 -0500 | [diff] [blame] | 489 | return -EAGAIN; |
Andy Adamson | b069d94 | 2009-04-01 09:22:43 -0400 | [diff] [blame] | 490 | } |
| 491 | |
Alexandros Batsakis | 689cf5c | 2009-12-14 21:27:56 -0800 | [diff] [blame] | 492 | if (!rpc_queue_empty(&tbl->slot_tbl_waitq) && |
| 493 | !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) { |
| 494 | rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); |
| 495 | spin_unlock(&tbl->slot_tbl_lock); |
| 496 | dprintk("%s enforce FIFO order\n", __func__); |
| 497 | return -EAGAIN; |
| 498 | } |
| 499 | |
Alexandros Batsakis | 40ead58 | 2009-12-14 21:27:54 -0800 | [diff] [blame] | 500 | slotid = nfs4_find_slot(tbl); |
Andy Adamson | fbcd4ab | 2009-04-01 09:22:15 -0400 | [diff] [blame] | 501 | if (slotid == NFS4_MAX_SLOT_TABLE) { |
| 502 | rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); |
| 503 | spin_unlock(&tbl->slot_tbl_lock); |
| 504 | dprintk("<-- %s: no free slots\n", __func__); |
| 505 | return -EAGAIN; |
| 506 | } |
| 507 | spin_unlock(&tbl->slot_tbl_lock); |
| 508 | |
Alexandros Batsakis | 689cf5c | 2009-12-14 21:27:56 -0800 | [diff] [blame] | 509 | rpc_task_set_priority(task, RPC_PRIORITY_NORMAL); |
Andy Adamson | fbcd4ab | 2009-04-01 09:22:15 -0400 | [diff] [blame] | 510 | slot = tbl->slots + slotid; |
Benny Halevy | 99fe60d | 2009-04-01 09:22:29 -0400 | [diff] [blame] | 511 | args->sa_session = session; |
Andy Adamson | fbcd4ab | 2009-04-01 09:22:15 -0400 | [diff] [blame] | 512 | args->sa_slotid = slotid; |
| 513 | args->sa_cache_this = cache_reply; |
| 514 | |
| 515 | dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr); |
| 516 | |
Benny Halevy | 99fe60d | 2009-04-01 09:22:29 -0400 | [diff] [blame] | 517 | res->sr_session = session; |
Andy Adamson | fbcd4ab | 2009-04-01 09:22:15 -0400 | [diff] [blame] | 518 | res->sr_slotid = slotid; |
| 519 | res->sr_renewal_time = jiffies; |
| 520 | /* |
| 521 | * sr_status is only set in decode_sequence, and so will remain |
| 522 | * set to 1 if an rpc level failure occurs. |
| 523 | */ |
| 524 | res->sr_status = 1; |
Andy Adamson | ce5039c | 2009-04-01 09:22:13 -0400 | [diff] [blame] | 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | int nfs4_setup_sequence(struct nfs_client *clp, |
| 529 | struct nfs4_sequence_args *args, |
| 530 | struct nfs4_sequence_res *res, |
| 531 | int cache_reply, |
| 532 | struct rpc_task *task) |
| 533 | { |
| 534 | int ret = 0; |
| 535 | |
| 536 | dprintk("--> %s clp %p session %p sr_slotid %d\n", |
| 537 | __func__, clp, clp->cl_session, res->sr_slotid); |
| 538 | |
| 539 | if (!nfs4_has_session(clp)) |
| 540 | goto out; |
| 541 | ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply, |
| 542 | task); |
Andy Adamson | 2628edd | 2009-12-04 15:55:35 -0500 | [diff] [blame] | 543 | if (ret && ret != -EAGAIN) { |
Andy Adamson | ce5039c | 2009-04-01 09:22:13 -0400 | [diff] [blame] | 544 | /* terminate rpc task */ |
| 545 | task->tk_status = ret; |
| 546 | task->tk_action = NULL; |
| 547 | } |
| 548 | out: |
| 549 | dprintk("<-- %s status=%d\n", __func__, ret); |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | struct nfs41_call_sync_data { |
| 554 | struct nfs_client *clp; |
| 555 | struct nfs4_sequence_args *seq_args; |
| 556 | struct nfs4_sequence_res *seq_res; |
| 557 | int cache_reply; |
| 558 | }; |
| 559 | |
| 560 | static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata) |
| 561 | { |
| 562 | struct nfs41_call_sync_data *data = calldata; |
| 563 | |
| 564 | dprintk("--> %s data->clp->cl_session %p\n", __func__, |
| 565 | data->clp->cl_session); |
| 566 | if (nfs4_setup_sequence(data->clp, data->seq_args, |
| 567 | data->seq_res, data->cache_reply, task)) |
| 568 | return; |
| 569 | rpc_call_start(task); |
| 570 | } |
| 571 | |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 572 | static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata) |
| 573 | { |
| 574 | rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED); |
| 575 | nfs41_call_sync_prepare(task, calldata); |
| 576 | } |
| 577 | |
Andy Adamson | 69ab40c | 2009-04-01 09:22:19 -0400 | [diff] [blame] | 578 | static void nfs41_call_sync_done(struct rpc_task *task, void *calldata) |
| 579 | { |
| 580 | struct nfs41_call_sync_data *data = calldata; |
| 581 | |
| 582 | nfs41_sequence_done(data->clp, data->seq_res, task->tk_status); |
Andy Adamson | 69ab40c | 2009-04-01 09:22:19 -0400 | [diff] [blame] | 583 | } |
| 584 | |
Andy Adamson | ce5039c | 2009-04-01 09:22:13 -0400 | [diff] [blame] | 585 | struct rpc_call_ops nfs41_call_sync_ops = { |
| 586 | .rpc_call_prepare = nfs41_call_sync_prepare, |
Andy Adamson | 69ab40c | 2009-04-01 09:22:19 -0400 | [diff] [blame] | 587 | .rpc_call_done = nfs41_call_sync_done, |
Andy Adamson | ce5039c | 2009-04-01 09:22:13 -0400 | [diff] [blame] | 588 | }; |
| 589 | |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 590 | struct rpc_call_ops nfs41_call_priv_sync_ops = { |
| 591 | .rpc_call_prepare = nfs41_call_priv_sync_prepare, |
| 592 | .rpc_call_done = nfs41_call_sync_done, |
| 593 | }; |
| 594 | |
Andy Adamson | ce5039c | 2009-04-01 09:22:13 -0400 | [diff] [blame] | 595 | static int nfs4_call_sync_sequence(struct nfs_client *clp, |
| 596 | struct rpc_clnt *clnt, |
| 597 | struct rpc_message *msg, |
| 598 | struct nfs4_sequence_args *args, |
| 599 | struct nfs4_sequence_res *res, |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 600 | int cache_reply, |
| 601 | int privileged) |
Andy Adamson | ce5039c | 2009-04-01 09:22:13 -0400 | [diff] [blame] | 602 | { |
| 603 | int ret; |
| 604 | struct rpc_task *task; |
| 605 | struct nfs41_call_sync_data data = { |
| 606 | .clp = clp, |
| 607 | .seq_args = args, |
| 608 | .seq_res = res, |
| 609 | .cache_reply = cache_reply, |
| 610 | }; |
| 611 | struct rpc_task_setup task_setup = { |
| 612 | .rpc_client = clnt, |
| 613 | .rpc_message = msg, |
| 614 | .callback_ops = &nfs41_call_sync_ops, |
| 615 | .callback_data = &data |
| 616 | }; |
| 617 | |
| 618 | res->sr_slotid = NFS4_MAX_SLOT_TABLE; |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 619 | if (privileged) |
| 620 | task_setup.callback_ops = &nfs41_call_priv_sync_ops; |
Andy Adamson | ce5039c | 2009-04-01 09:22:13 -0400 | [diff] [blame] | 621 | task = rpc_run_task(&task_setup); |
| 622 | if (IS_ERR(task)) |
| 623 | ret = PTR_ERR(task); |
| 624 | else { |
| 625 | ret = task->tk_status; |
| 626 | rpc_put_task(task); |
| 627 | } |
| 628 | return ret; |
| 629 | } |
| 630 | |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 631 | int _nfs4_call_sync_session(struct nfs_server *server, |
| 632 | struct rpc_message *msg, |
| 633 | struct nfs4_sequence_args *args, |
| 634 | struct nfs4_sequence_res *res, |
| 635 | int cache_reply) |
| 636 | { |
Andy Adamson | ce5039c | 2009-04-01 09:22:13 -0400 | [diff] [blame] | 637 | return nfs4_call_sync_sequence(server->nfs_client, server->client, |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 638 | msg, args, res, cache_reply, 0); |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | #endif /* CONFIG_NFS_V4_1 */ |
| 642 | |
| 643 | int _nfs4_call_sync(struct nfs_server *server, |
| 644 | struct rpc_message *msg, |
| 645 | struct nfs4_sequence_args *args, |
| 646 | struct nfs4_sequence_res *res, |
| 647 | int cache_reply) |
| 648 | { |
Benny Halevy | f375297 | 2009-04-01 09:22:04 -0400 | [diff] [blame] | 649 | args->sa_session = res->sr_session = NULL; |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 650 | return rpc_call_sync(server->client, msg, 0); |
| 651 | } |
| 652 | |
| 653 | #define nfs4_call_sync(server, msg, args, res, cache_reply) \ |
| 654 | (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \ |
| 655 | &(res)->seq_res, (cache_reply)) |
| 656 | |
Andy Adamson | b0df806 | 2009-04-01 09:22:18 -0400 | [diff] [blame] | 657 | static void nfs4_sequence_done(const struct nfs_server *server, |
| 658 | struct nfs4_sequence_res *res, int rpc_status) |
| 659 | { |
| 660 | #ifdef CONFIG_NFS_V4_1 |
| 661 | if (nfs4_has_session(server->nfs_client)) |
| 662 | nfs41_sequence_done(server->nfs_client, res, rpc_status); |
| 663 | #endif /* CONFIG_NFS_V4_1 */ |
| 664 | } |
| 665 | |
Trond Myklebust | 38478b2 | 2006-05-25 01:40:57 -0400 | [diff] [blame] | 666 | static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
Trond Myklebust | 38478b2 | 2006-05-25 01:40:57 -0400 | [diff] [blame] | 668 | struct nfs_inode *nfsi = NFS_I(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Trond Myklebust | 38478b2 | 2006-05-25 01:40:57 -0400 | [diff] [blame] | 670 | spin_lock(&dir->i_lock); |
Trond Myklebust | 40d2470 | 2007-10-08 09:24:22 -0400 | [diff] [blame] | 671 | nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA; |
| 672 | if (!cinfo->atomic || cinfo->before != nfsi->change_attr) |
Trond Myklebust | bfc69a4 | 2007-10-15 18:18:29 -0400 | [diff] [blame] | 673 | nfs_force_lookup_revalidate(dir); |
Trond Myklebust | 40d2470 | 2007-10-08 09:24:22 -0400 | [diff] [blame] | 674 | nfsi->change_attr = cinfo->after; |
Trond Myklebust | 38478b2 | 2006-05-25 01:40:57 -0400 | [diff] [blame] | 675 | spin_unlock(&dir->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 678 | struct nfs4_opendata { |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 679 | struct kref kref; |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 680 | struct nfs_openargs o_arg; |
| 681 | struct nfs_openres o_res; |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 682 | struct nfs_open_confirmargs c_arg; |
| 683 | struct nfs_open_confirmres c_res; |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 684 | struct nfs_fattr f_attr; |
| 685 | struct nfs_fattr dir_attr; |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 686 | struct path path; |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 687 | struct dentry *dir; |
| 688 | struct nfs4_state_owner *owner; |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 689 | struct nfs4_state *state; |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 690 | struct iattr attrs; |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 691 | unsigned long timestamp; |
Trond Myklebust | 3e30991 | 2007-07-07 13:19:59 -0400 | [diff] [blame] | 692 | unsigned int rpc_done : 1; |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 693 | int rpc_status; |
| 694 | int cancelled; |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 695 | }; |
| 696 | |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 697 | |
| 698 | static void nfs4_init_opendata_res(struct nfs4_opendata *p) |
| 699 | { |
| 700 | p->o_res.f_attr = &p->f_attr; |
| 701 | p->o_res.dir_attr = &p->dir_attr; |
Trond Myklebust | c1d5193 | 2008-04-07 13:20:54 -0400 | [diff] [blame] | 702 | p->o_res.seqid = p->o_arg.seqid; |
| 703 | p->c_res.seqid = p->c_arg.seqid; |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 704 | p->o_res.server = p->o_arg.server; |
| 705 | nfs_fattr_init(&p->f_attr); |
| 706 | nfs_fattr_init(&p->dir_attr); |
Benny Halevy | 578e458 | 2009-06-18 22:01:23 -0400 | [diff] [blame] | 707 | p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 708 | } |
| 709 | |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 710 | static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path, |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 711 | struct nfs4_state_owner *sp, fmode_t fmode, int flags, |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 712 | const struct iattr *attrs) |
| 713 | { |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 714 | struct dentry *parent = dget_parent(path->dentry); |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 715 | struct inode *dir = parent->d_inode; |
| 716 | struct nfs_server *server = NFS_SERVER(dir); |
| 717 | struct nfs4_opendata *p; |
| 718 | |
| 719 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 720 | if (p == NULL) |
| 721 | goto err; |
| 722 | p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid); |
| 723 | if (p->o_arg.seqid == NULL) |
| 724 | goto err_free; |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 725 | p->path.mnt = mntget(path->mnt); |
| 726 | p->path.dentry = dget(path->dentry); |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 727 | p->dir = parent; |
| 728 | p->owner = sp; |
| 729 | atomic_inc(&sp->so_count); |
| 730 | p->o_arg.fh = NFS_FH(dir); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 731 | p->o_arg.open_flags = flags; |
| 732 | p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE); |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 733 | p->o_arg.clientid = server->nfs_client->cl_clientid; |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 734 | p->o_arg.id = sp->so_owner_id.id; |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 735 | p->o_arg.name = &p->path.dentry->d_name; |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 736 | p->o_arg.server = server; |
| 737 | p->o_arg.bitmask = server->attr_bitmask; |
| 738 | p->o_arg.claim = NFS4_OPEN_CLAIM_NULL; |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 739 | if (flags & O_EXCL) { |
Alexandros Batsakis | 4882ef7 | 2009-12-05 13:30:21 -0500 | [diff] [blame] | 740 | if (nfs4_has_persistent_session(server->nfs_client)) { |
| 741 | /* GUARDED */ |
| 742 | p->o_arg.u.attrs = &p->attrs; |
| 743 | memcpy(&p->attrs, attrs, sizeof(p->attrs)); |
| 744 | } else { /* EXCLUSIVE4_1 */ |
| 745 | u32 *s = (u32 *) p->o_arg.u.verifier.data; |
| 746 | s[0] = jiffies; |
| 747 | s[1] = current->pid; |
| 748 | } |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 749 | } else if (flags & O_CREAT) { |
| 750 | p->o_arg.u.attrs = &p->attrs; |
| 751 | memcpy(&p->attrs, attrs, sizeof(p->attrs)); |
| 752 | } |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 753 | p->c_arg.fh = &p->o_res.fh; |
| 754 | p->c_arg.stateid = &p->o_res.stateid; |
| 755 | p->c_arg.seqid = p->o_arg.seqid; |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 756 | nfs4_init_opendata_res(p); |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 757 | kref_init(&p->kref); |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 758 | return p; |
| 759 | err_free: |
| 760 | kfree(p); |
| 761 | err: |
| 762 | dput(parent); |
| 763 | return NULL; |
| 764 | } |
| 765 | |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 766 | static void nfs4_opendata_free(struct kref *kref) |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 767 | { |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 768 | struct nfs4_opendata *p = container_of(kref, |
| 769 | struct nfs4_opendata, kref); |
| 770 | |
| 771 | nfs_free_seqid(p->o_arg.seqid); |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 772 | if (p->state != NULL) |
| 773 | nfs4_put_open_state(p->state); |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 774 | nfs4_put_state_owner(p->owner); |
| 775 | dput(p->dir); |
Jan Blunck | 31f31db1 | 2008-05-02 13:42:45 -0700 | [diff] [blame] | 776 | path_put(&p->path); |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 777 | kfree(p); |
| 778 | } |
| 779 | |
| 780 | static void nfs4_opendata_put(struct nfs4_opendata *p) |
| 781 | { |
| 782 | if (p != NULL) |
| 783 | kref_put(&p->kref, nfs4_opendata_free); |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 784 | } |
| 785 | |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame] | 786 | static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task) |
| 787 | { |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame] | 788 | int ret; |
| 789 | |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame] | 790 | ret = rpc_wait_for_completion_task(task); |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame] | 791 | return ret; |
| 792 | } |
| 793 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 794 | static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode) |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 795 | { |
| 796 | int ret = 0; |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 797 | |
| 798 | if (open_mode & O_EXCL) |
| 799 | goto out; |
| 800 | switch (mode & (FMODE_READ|FMODE_WRITE)) { |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 801 | case FMODE_READ: |
Trond Myklebust | 88069f7 | 2009-12-08 08:33:16 -0500 | [diff] [blame] | 802 | ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 |
| 803 | && state->n_rdonly != 0; |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 804 | break; |
| 805 | case FMODE_WRITE: |
Trond Myklebust | 88069f7 | 2009-12-08 08:33:16 -0500 | [diff] [blame] | 806 | ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 |
| 807 | && state->n_wronly != 0; |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 808 | break; |
| 809 | case FMODE_READ|FMODE_WRITE: |
Trond Myklebust | 88069f7 | 2009-12-08 08:33:16 -0500 | [diff] [blame] | 810 | ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 |
| 811 | && state->n_rdwr != 0; |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 812 | } |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 813 | out: |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 814 | return ret; |
| 815 | } |
| 816 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 817 | static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode) |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 818 | { |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 819 | if ((delegation->type & fmode) != fmode) |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 820 | return 0; |
Trond Myklebust | 15c831b | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 821 | if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags)) |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 822 | return 0; |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 823 | nfs_mark_delegation_referenced(delegation); |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 824 | return 1; |
| 825 | } |
| 826 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 827 | static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode) |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 828 | { |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 829 | switch (fmode) { |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 830 | case FMODE_WRITE: |
| 831 | state->n_wronly++; |
| 832 | break; |
| 833 | case FMODE_READ: |
| 834 | state->n_rdonly++; |
| 835 | break; |
| 836 | case FMODE_READ|FMODE_WRITE: |
| 837 | state->n_rdwr++; |
| 838 | } |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 839 | nfs4_state_set_mode_locked(state, state->state | fmode); |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 840 | } |
| 841 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 842 | static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode) |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 843 | { |
| 844 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
| 845 | memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data)); |
| 846 | memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data)); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 847 | switch (fmode) { |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 848 | case FMODE_READ: |
| 849 | set_bit(NFS_O_RDONLY_STATE, &state->flags); |
| 850 | break; |
| 851 | case FMODE_WRITE: |
| 852 | set_bit(NFS_O_WRONLY_STATE, &state->flags); |
| 853 | break; |
| 854 | case FMODE_READ|FMODE_WRITE: |
| 855 | set_bit(NFS_O_RDWR_STATE, &state->flags); |
| 856 | } |
| 857 | } |
| 858 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 859 | static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode) |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 860 | { |
Trond Myklebust | 8bda4e4 | 2007-07-09 10:45:42 -0400 | [diff] [blame] | 861 | write_seqlock(&state->seqlock); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 862 | nfs_set_open_stateid_locked(state, stateid, fmode); |
Trond Myklebust | 8bda4e4 | 2007-07-09 10:45:42 -0400 | [diff] [blame] | 863 | write_sequnlock(&state->seqlock); |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 864 | } |
| 865 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 866 | static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { |
Trond Myklebust | 8bda4e4 | 2007-07-09 10:45:42 -0400 | [diff] [blame] | 868 | /* |
| 869 | * Protect the call to nfs4_state_set_mode_locked and |
| 870 | * serialise the stateid update |
| 871 | */ |
| 872 | write_seqlock(&state->seqlock); |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 873 | if (deleg_stateid != NULL) { |
| 874 | memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data)); |
| 875 | set_bit(NFS_DELEGATED_STATE, &state->flags); |
| 876 | } |
| 877 | if (open_stateid != NULL) |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 878 | nfs_set_open_stateid_locked(state, open_stateid, fmode); |
Trond Myklebust | 8bda4e4 | 2007-07-09 10:45:42 -0400 | [diff] [blame] | 879 | write_sequnlock(&state->seqlock); |
| 880 | spin_lock(&state->owner->so_lock); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 881 | update_open_stateflags(state, fmode); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 882 | spin_unlock(&state->owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 885 | static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode) |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 886 | { |
| 887 | struct nfs_inode *nfsi = NFS_I(state->inode); |
| 888 | struct nfs_delegation *deleg_cur; |
| 889 | int ret = 0; |
| 890 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 891 | fmode &= (FMODE_READ|FMODE_WRITE); |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 892 | |
| 893 | rcu_read_lock(); |
| 894 | deleg_cur = rcu_dereference(nfsi->delegation); |
| 895 | if (deleg_cur == NULL) |
| 896 | goto no_delegation; |
| 897 | |
| 898 | spin_lock(&deleg_cur->lock); |
| 899 | if (nfsi->delegation != deleg_cur || |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 900 | (deleg_cur->type & fmode) != fmode) |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 901 | goto no_delegation_unlock; |
| 902 | |
| 903 | if (delegation == NULL) |
| 904 | delegation = &deleg_cur->stateid; |
| 905 | else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0) |
| 906 | goto no_delegation_unlock; |
| 907 | |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 908 | nfs_mark_delegation_referenced(deleg_cur); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 909 | __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode); |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 910 | ret = 1; |
| 911 | no_delegation_unlock: |
| 912 | spin_unlock(&deleg_cur->lock); |
| 913 | no_delegation: |
| 914 | rcu_read_unlock(); |
| 915 | |
| 916 | if (!ret && open_stateid != NULL) { |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 917 | __update_open_stateid(state, open_stateid, NULL, fmode); |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 918 | ret = 1; |
| 919 | } |
| 920 | |
| 921 | return ret; |
| 922 | } |
| 923 | |
| 924 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 925 | static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode) |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 926 | { |
| 927 | struct nfs_delegation *delegation; |
| 928 | |
| 929 | rcu_read_lock(); |
| 930 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 931 | if (delegation == NULL || (delegation->type & fmode) == fmode) { |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 932 | rcu_read_unlock(); |
| 933 | return; |
| 934 | } |
| 935 | rcu_read_unlock(); |
| 936 | nfs_inode_return_delegation(inode); |
| 937 | } |
| 938 | |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 939 | static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata) |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 940 | { |
| 941 | struct nfs4_state *state = opendata->state; |
| 942 | struct nfs_inode *nfsi = NFS_I(state->inode); |
| 943 | struct nfs_delegation *delegation; |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 944 | int open_mode = opendata->o_arg.open_flags & O_EXCL; |
| 945 | fmode_t fmode = opendata->o_arg.fmode; |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 946 | nfs4_stateid stateid; |
| 947 | int ret = -EAGAIN; |
| 948 | |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 949 | for (;;) { |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 950 | if (can_open_cached(state, fmode, open_mode)) { |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 951 | spin_lock(&state->owner->so_lock); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 952 | if (can_open_cached(state, fmode, open_mode)) { |
| 953 | update_open_stateflags(state, fmode); |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 954 | spin_unlock(&state->owner->so_lock); |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 955 | goto out_return_state; |
| 956 | } |
| 957 | spin_unlock(&state->owner->so_lock); |
| 958 | } |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 959 | rcu_read_lock(); |
| 960 | delegation = rcu_dereference(nfsi->delegation); |
| 961 | if (delegation == NULL || |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 962 | !can_open_delegated(delegation, fmode)) { |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 963 | rcu_read_unlock(); |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 964 | break; |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 965 | } |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 966 | /* Save the delegation */ |
| 967 | memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data)); |
| 968 | rcu_read_unlock(); |
Trond Myklebust | af22f94 | 2007-08-10 17:45:10 -0400 | [diff] [blame] | 969 | ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode); |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 970 | if (ret != 0) |
| 971 | goto out; |
| 972 | ret = -EAGAIN; |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 973 | |
| 974 | /* Try to update the stateid using the delegation */ |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 975 | if (update_open_stateid(state, NULL, &stateid, fmode)) |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 976 | goto out_return_state; |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 977 | } |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 978 | out: |
| 979 | return ERR_PTR(ret); |
| 980 | out_return_state: |
| 981 | atomic_inc(&state->count); |
| 982 | return state; |
| 983 | } |
| 984 | |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 985 | static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) |
| 986 | { |
| 987 | struct inode *inode; |
| 988 | struct nfs4_state *state = NULL; |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 989 | struct nfs_delegation *delegation; |
Trond Myklebust | 1b370bc | 2007-07-07 08:04:47 -0400 | [diff] [blame] | 990 | int ret; |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 991 | |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 992 | if (!data->rpc_done) { |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 993 | state = nfs4_try_open_cached(data); |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 994 | goto out; |
| 995 | } |
| 996 | |
Trond Myklebust | 1b370bc | 2007-07-07 08:04:47 -0400 | [diff] [blame] | 997 | ret = -EAGAIN; |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 998 | if (!(data->f_attr.valid & NFS_ATTR_FATTR)) |
Trond Myklebust | 1b370bc | 2007-07-07 08:04:47 -0400 | [diff] [blame] | 999 | goto err; |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1000 | inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr); |
Trond Myklebust | 1b370bc | 2007-07-07 08:04:47 -0400 | [diff] [blame] | 1001 | ret = PTR_ERR(inode); |
Trond Myklebust | 03f28e3 | 2006-03-20 13:44:48 -0500 | [diff] [blame] | 1002 | if (IS_ERR(inode)) |
Trond Myklebust | 1b370bc | 2007-07-07 08:04:47 -0400 | [diff] [blame] | 1003 | goto err; |
| 1004 | ret = -ENOMEM; |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1005 | state = nfs4_get_open_state(inode, data->owner); |
| 1006 | if (state == NULL) |
Trond Myklebust | 1b370bc | 2007-07-07 08:04:47 -0400 | [diff] [blame] | 1007 | goto err_put_inode; |
Trond Myklebust | 549d6ed | 2007-07-03 16:42:45 -0400 | [diff] [blame] | 1008 | if (data->o_res.delegation_type != 0) { |
Trond Myklebust | 549d6ed | 2007-07-03 16:42:45 -0400 | [diff] [blame] | 1009 | int delegation_flags = 0; |
| 1010 | |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 1011 | rcu_read_lock(); |
| 1012 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
| 1013 | if (delegation) |
| 1014 | delegation_flags = delegation->flags; |
| 1015 | rcu_read_unlock(); |
Trond Myklebust | 15c831b | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 1016 | if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0) |
Trond Myklebust | 549d6ed | 2007-07-03 16:42:45 -0400 | [diff] [blame] | 1017 | nfs_inode_set_delegation(state->inode, |
| 1018 | data->owner->so_cred, |
| 1019 | &data->o_res); |
| 1020 | else |
| 1021 | nfs_inode_reclaim_delegation(state->inode, |
| 1022 | data->owner->so_cred, |
| 1023 | &data->o_res); |
| 1024 | } |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 1025 | |
| 1026 | update_open_stateid(state, &data->o_res.stateid, NULL, |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1027 | data->o_arg.fmode); |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1028 | iput(inode); |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 1029 | out: |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1030 | return state; |
Trond Myklebust | 1b370bc | 2007-07-07 08:04:47 -0400 | [diff] [blame] | 1031 | err_put_inode: |
| 1032 | iput(inode); |
| 1033 | err: |
| 1034 | return ERR_PTR(ret); |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1035 | } |
| 1036 | |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1037 | static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state) |
| 1038 | { |
| 1039 | struct nfs_inode *nfsi = NFS_I(state->inode); |
| 1040 | struct nfs_open_context *ctx; |
| 1041 | |
| 1042 | spin_lock(&state->inode->i_lock); |
| 1043 | list_for_each_entry(ctx, &nfsi->open_files, list) { |
| 1044 | if (ctx->state != state) |
| 1045 | continue; |
| 1046 | get_nfs_open_context(ctx); |
| 1047 | spin_unlock(&state->inode->i_lock); |
| 1048 | return ctx; |
| 1049 | } |
| 1050 | spin_unlock(&state->inode->i_lock); |
| 1051 | return ERR_PTR(-ENOENT); |
| 1052 | } |
| 1053 | |
Trond Myklebust | 6f220ed | 2007-07-17 21:50:45 -0400 | [diff] [blame] | 1054 | static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state) |
| 1055 | { |
| 1056 | struct nfs4_opendata *opendata; |
| 1057 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1058 | opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL); |
Trond Myklebust | 6f220ed | 2007-07-17 21:50:45 -0400 | [diff] [blame] | 1059 | if (opendata == NULL) |
| 1060 | return ERR_PTR(-ENOMEM); |
| 1061 | opendata->state = state; |
| 1062 | atomic_inc(&state->count); |
| 1063 | return opendata; |
| 1064 | } |
| 1065 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1066 | static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res) |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1067 | { |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 1068 | struct nfs4_state *newstate; |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1069 | int ret; |
| 1070 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1071 | opendata->o_arg.open_flags = 0; |
| 1072 | opendata->o_arg.fmode = fmode; |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 1073 | memset(&opendata->o_res, 0, sizeof(opendata->o_res)); |
| 1074 | memset(&opendata->c_res, 0, sizeof(opendata->c_res)); |
| 1075 | nfs4_init_opendata_res(opendata); |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 1076 | ret = _nfs4_recover_proc_open(opendata); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1077 | if (ret != 0) |
| 1078 | return ret; |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 1079 | newstate = nfs4_opendata_to_nfs4_state(opendata); |
Trond Myklebust | 1b370bc | 2007-07-07 08:04:47 -0400 | [diff] [blame] | 1080 | if (IS_ERR(newstate)) |
| 1081 | return PTR_ERR(newstate); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1082 | nfs4_close_state(&opendata->path, newstate, fmode); |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 1083 | *res = newstate; |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state) |
| 1088 | { |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1089 | struct nfs4_state *newstate; |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1090 | int ret; |
| 1091 | |
| 1092 | /* memory barrier prior to reading state->n_* */ |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 1093 | clear_bit(NFS_DELEGATED_STATE, &state->flags); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1094 | smp_rmb(); |
| 1095 | if (state->n_rdwr != 0) { |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 1096 | ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1097 | if (ret != 0) |
| 1098 | return ret; |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 1099 | if (newstate != state) |
| 1100 | return -ESTALE; |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1101 | } |
| 1102 | if (state->n_wronly != 0) { |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 1103 | ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1104 | if (ret != 0) |
| 1105 | return ret; |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 1106 | if (newstate != state) |
| 1107 | return -ESTALE; |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1108 | } |
| 1109 | if (state->n_rdonly != 0) { |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 1110 | ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1111 | if (ret != 0) |
| 1112 | return ret; |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 1113 | if (newstate != state) |
| 1114 | return -ESTALE; |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1115 | } |
Trond Myklebust | 1ac7e2f | 2007-07-08 21:04:15 -0400 | [diff] [blame] | 1116 | /* |
| 1117 | * We may have performed cached opens for all three recoveries. |
| 1118 | * Check if we need to update the current stateid. |
| 1119 | */ |
| 1120 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 && |
| 1121 | memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) { |
Trond Myklebust | 8bda4e4 | 2007-07-09 10:45:42 -0400 | [diff] [blame] | 1122 | write_seqlock(&state->seqlock); |
Trond Myklebust | 1ac7e2f | 2007-07-08 21:04:15 -0400 | [diff] [blame] | 1123 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
| 1124 | memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)); |
Trond Myklebust | 8bda4e4 | 2007-07-09 10:45:42 -0400 | [diff] [blame] | 1125 | write_sequnlock(&state->seqlock); |
Trond Myklebust | 1ac7e2f | 2007-07-08 21:04:15 -0400 | [diff] [blame] | 1126 | } |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1127 | return 0; |
| 1128 | } |
| 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | /* |
| 1131 | * OPEN_RECLAIM: |
| 1132 | * reclaim state on the server after a reboot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | */ |
Trond Myklebust | 539cd03 | 2007-06-05 11:46:42 -0400 | [diff] [blame] | 1134 | static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | { |
Trond Myklebust | 1ac7e2f | 2007-07-08 21:04:15 -0400 | [diff] [blame] | 1136 | struct nfs_delegation *delegation; |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1137 | struct nfs4_opendata *opendata; |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1138 | fmode_t delegation_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | int status; |
| 1140 | |
Trond Myklebust | 6f220ed | 2007-07-17 21:50:45 -0400 | [diff] [blame] | 1141 | opendata = nfs4_open_recoverdata_alloc(ctx, state); |
| 1142 | if (IS_ERR(opendata)) |
| 1143 | return PTR_ERR(opendata); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1144 | opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS; |
| 1145 | opendata->o_arg.fh = NFS_FH(state->inode); |
Trond Myklebust | 1ac7e2f | 2007-07-08 21:04:15 -0400 | [diff] [blame] | 1146 | rcu_read_lock(); |
| 1147 | delegation = rcu_dereference(NFS_I(state->inode)->delegation); |
Trond Myklebust | 15c831b | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 1148 | if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0) |
Trond Myklebust | 65bbf6b | 2007-08-27 09:57:46 -0400 | [diff] [blame] | 1149 | delegation_type = delegation->type; |
Trond Myklebust | 1ac7e2f | 2007-07-08 21:04:15 -0400 | [diff] [blame] | 1150 | rcu_read_unlock(); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1151 | opendata->o_arg.u.delegation_type = delegation_type; |
| 1152 | status = nfs4_open_recover(opendata, state); |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 1153 | nfs4_opendata_put(opendata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | return status; |
| 1155 | } |
| 1156 | |
Trond Myklebust | 539cd03 | 2007-06-05 11:46:42 -0400 | [diff] [blame] | 1157 | static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | { |
| 1159 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 1160 | struct nfs4_exception exception = { }; |
| 1161 | int err; |
| 1162 | do { |
Trond Myklebust | 539cd03 | 2007-06-05 11:46:42 -0400 | [diff] [blame] | 1163 | err = _nfs4_do_open_reclaim(ctx, state); |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 1164 | if (err != -NFS4ERR_DELAY) |
| 1165 | break; |
| 1166 | nfs4_handle_exception(server, err, &exception); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } while (exception.retry); |
| 1168 | return err; |
| 1169 | } |
| 1170 | |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1171 | static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state) |
| 1172 | { |
| 1173 | struct nfs_open_context *ctx; |
| 1174 | int ret; |
| 1175 | |
| 1176 | ctx = nfs4_state_find_open_context(state); |
| 1177 | if (IS_ERR(ctx)) |
| 1178 | return PTR_ERR(ctx); |
Trond Myklebust | 539cd03 | 2007-06-05 11:46:42 -0400 | [diff] [blame] | 1179 | ret = nfs4_do_open_reclaim(ctx, state); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1180 | put_nfs_open_context(ctx); |
| 1181 | return ret; |
| 1182 | } |
| 1183 | |
Trond Myklebust | 13437e1 | 2007-07-06 15:10:43 -0400 | [diff] [blame] | 1184 | static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | { |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 1186 | struct nfs4_opendata *opendata; |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1187 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | |
Trond Myklebust | 6f220ed | 2007-07-17 21:50:45 -0400 | [diff] [blame] | 1189 | opendata = nfs4_open_recoverdata_alloc(ctx, state); |
| 1190 | if (IS_ERR(opendata)) |
| 1191 | return PTR_ERR(opendata); |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 1192 | opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR; |
Trond Myklebust | 13437e1 | 2007-07-06 15:10:43 -0400 | [diff] [blame] | 1193 | memcpy(opendata->o_arg.u.delegation.data, stateid->data, |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 1194 | sizeof(opendata->o_arg.u.delegation.data)); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1195 | ret = nfs4_open_recover(opendata, state); |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 1196 | nfs4_opendata_put(opendata); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1197 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Trond Myklebust | 13437e1 | 2007-07-06 15:10:43 -0400 | [diff] [blame] | 1200 | int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | { |
| 1202 | struct nfs4_exception exception = { }; |
Trond Myklebust | 539cd03 | 2007-06-05 11:46:42 -0400 | [diff] [blame] | 1203 | struct nfs_server *server = NFS_SERVER(state->inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | int err; |
| 1205 | do { |
Trond Myklebust | 13437e1 | 2007-07-06 15:10:43 -0400 | [diff] [blame] | 1206 | err = _nfs4_open_delegation_recall(ctx, state, stateid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | switch (err) { |
| 1208 | case 0: |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 1209 | case -ENOENT: |
| 1210 | case -ESTALE: |
| 1211 | goto out; |
Ricardo Labiaga | bcfa49f | 2009-12-07 09:22:29 -0500 | [diff] [blame] | 1212 | case -NFS4ERR_BADSESSION: |
| 1213 | case -NFS4ERR_BADSLOT: |
| 1214 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 1215 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
| 1216 | case -NFS4ERR_DEADSESSION: |
| 1217 | nfs4_schedule_state_recovery( |
| 1218 | server->nfs_client); |
| 1219 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | case -NFS4ERR_STALE_CLIENTID: |
| 1221 | case -NFS4ERR_STALE_STATEID: |
| 1222 | case -NFS4ERR_EXPIRED: |
| 1223 | /* Don't recall a delegation if it was lost */ |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 1224 | nfs4_schedule_state_recovery(server->nfs_client); |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 1225 | goto out; |
| 1226 | case -ERESTARTSYS: |
| 1227 | /* |
| 1228 | * The show must go on: exit, but mark the |
| 1229 | * stateid as needing recovery. |
| 1230 | */ |
| 1231 | case -NFS4ERR_ADMIN_REVOKED: |
| 1232 | case -NFS4ERR_BAD_STATEID: |
| 1233 | nfs4_state_mark_reclaim_nograce(server->nfs_client, state); |
| 1234 | case -ENOMEM: |
| 1235 | err = 0; |
| 1236 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | } |
| 1238 | err = nfs4_handle_exception(server, err, &exception); |
| 1239 | } while (exception.retry); |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 1240 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | return err; |
| 1242 | } |
| 1243 | |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 1244 | static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata) |
| 1245 | { |
| 1246 | struct nfs4_opendata *data = calldata; |
| 1247 | |
| 1248 | data->rpc_status = task->tk_status; |
| 1249 | if (RPC_ASSASSINATED(task)) |
| 1250 | return; |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 1251 | if (data->rpc_status == 0) { |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 1252 | memcpy(data->o_res.stateid.data, data->c_res.stateid.data, |
| 1253 | sizeof(data->o_res.stateid.data)); |
Trond Myklebust | bb22629 | 2008-01-02 15:19:18 -0500 | [diff] [blame] | 1254 | nfs_confirm_seqid(&data->owner->so_seqid, 0); |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 1255 | renew_lease(data->o_res.server, data->timestamp); |
Trond Myklebust | 3e30991 | 2007-07-07 13:19:59 -0400 | [diff] [blame] | 1256 | data->rpc_done = 1; |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 1257 | } |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | static void nfs4_open_confirm_release(void *calldata) |
| 1261 | { |
| 1262 | struct nfs4_opendata *data = calldata; |
| 1263 | struct nfs4_state *state = NULL; |
| 1264 | |
| 1265 | /* If this request hasn't been cancelled, do nothing */ |
| 1266 | if (data->cancelled == 0) |
| 1267 | goto out_free; |
| 1268 | /* In case of error, no cleanup! */ |
Trond Myklebust | 3e30991 | 2007-07-07 13:19:59 -0400 | [diff] [blame] | 1269 | if (!data->rpc_done) |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 1270 | goto out_free; |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 1271 | state = nfs4_opendata_to_nfs4_state(data); |
Trond Myklebust | 1b370bc | 2007-07-07 08:04:47 -0400 | [diff] [blame] | 1272 | if (!IS_ERR(state)) |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1273 | nfs4_close_state(&data->path, state, data->o_arg.fmode); |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 1274 | out_free: |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 1275 | nfs4_opendata_put(data); |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | static const struct rpc_call_ops nfs4_open_confirm_ops = { |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 1279 | .rpc_call_done = nfs4_open_confirm_done, |
| 1280 | .rpc_release = nfs4_open_confirm_release, |
| 1281 | }; |
| 1282 | |
| 1283 | /* |
| 1284 | * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata |
| 1285 | */ |
| 1286 | static int _nfs4_proc_open_confirm(struct nfs4_opendata *data) |
| 1287 | { |
| 1288 | struct nfs_server *server = NFS_SERVER(data->dir->d_inode); |
| 1289 | struct rpc_task *task; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 1290 | struct rpc_message msg = { |
| 1291 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM], |
| 1292 | .rpc_argp = &data->c_arg, |
| 1293 | .rpc_resp = &data->c_res, |
| 1294 | .rpc_cred = data->owner->so_cred, |
| 1295 | }; |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1296 | struct rpc_task_setup task_setup_data = { |
| 1297 | .rpc_client = server->client, |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 1298 | .rpc_message = &msg, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1299 | .callback_ops = &nfs4_open_confirm_ops, |
| 1300 | .callback_data = data, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 1301 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1302 | .flags = RPC_TASK_ASYNC, |
| 1303 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | int status; |
| 1305 | |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 1306 | kref_get(&data->kref); |
Trond Myklebust | 3e30991 | 2007-07-07 13:19:59 -0400 | [diff] [blame] | 1307 | data->rpc_done = 0; |
| 1308 | data->rpc_status = 0; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 1309 | data->timestamp = jiffies; |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1310 | task = rpc_run_task(&task_setup_data); |
Trond Myklebust | 7a1218a | 2006-03-20 18:11:10 -0500 | [diff] [blame] | 1311 | if (IS_ERR(task)) |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 1312 | return PTR_ERR(task); |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 1313 | status = nfs4_wait_for_completion_rpc_task(task); |
| 1314 | if (status != 0) { |
| 1315 | data->cancelled = 1; |
| 1316 | smp_wmb(); |
| 1317 | } else |
| 1318 | status = data->rpc_status; |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 1319 | rpc_put_task(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | return status; |
| 1321 | } |
| 1322 | |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1323 | static void nfs4_open_prepare(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | { |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1325 | struct nfs4_opendata *data = calldata; |
| 1326 | struct nfs4_state_owner *sp = data->owner; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 1327 | |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1328 | if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0) |
| 1329 | return; |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 1330 | /* |
| 1331 | * Check if we still need to send an OPEN call, or if we can use |
| 1332 | * a delegation instead. |
| 1333 | */ |
| 1334 | if (data->state != NULL) { |
| 1335 | struct nfs_delegation *delegation; |
| 1336 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1337 | if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags)) |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 1338 | goto out_no_action; |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 1339 | rcu_read_lock(); |
| 1340 | delegation = rcu_dereference(NFS_I(data->state->inode)->delegation); |
| 1341 | if (delegation != NULL && |
Trond Myklebust | 15c831b | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 1342 | test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) { |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 1343 | rcu_read_unlock(); |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 1344 | goto out_no_action; |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 1345 | } |
| 1346 | rcu_read_unlock(); |
| 1347 | } |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1348 | /* Update sequence id. */ |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 1349 | data->o_arg.id = sp->so_owner_id.id; |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1350 | data->o_arg.clientid = sp->so_client->cl_clientid; |
Trond Myklebust | 6f220ed | 2007-07-17 21:50:45 -0400 | [diff] [blame] | 1351 | if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) { |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 1352 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR]; |
Trond Myklebust | 6f220ed | 2007-07-17 21:50:45 -0400 | [diff] [blame] | 1353 | nfs_copy_fh(&data->o_res.fh, data->o_arg.fh); |
| 1354 | } |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 1355 | data->timestamp = jiffies; |
Andy Adamson | d898528 | 2009-04-01 09:22:21 -0400 | [diff] [blame] | 1356 | if (nfs4_setup_sequence(data->o_arg.server->nfs_client, |
| 1357 | &data->o_arg.seq_args, |
| 1358 | &data->o_res.seq_res, 1, task)) |
| 1359 | return; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 1360 | rpc_call_start(task); |
Trond Myklebust | 6ee4126 | 2007-07-08 14:11:36 -0400 | [diff] [blame] | 1361 | return; |
| 1362 | out_no_action: |
| 1363 | task->tk_action = NULL; |
| 1364 | |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1365 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 1367 | static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata) |
| 1368 | { |
| 1369 | rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED); |
| 1370 | nfs4_open_prepare(task, calldata); |
| 1371 | } |
| 1372 | |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1373 | static void nfs4_open_done(struct rpc_task *task, void *calldata) |
| 1374 | { |
| 1375 | struct nfs4_opendata *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1377 | data->rpc_status = task->tk_status; |
Andy Adamson | d898528 | 2009-04-01 09:22:21 -0400 | [diff] [blame] | 1378 | |
Trond Myklebust | d61e612 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 1379 | nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res, |
| 1380 | task->tk_status); |
Andy Adamson | d898528 | 2009-04-01 09:22:21 -0400 | [diff] [blame] | 1381 | |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1382 | if (RPC_ASSASSINATED(task)) |
| 1383 | return; |
| 1384 | if (task->tk_status == 0) { |
| 1385 | switch (data->o_res.f_attr->mode & S_IFMT) { |
Trond Myklebust | 6f926b5 | 2005-10-18 14:20:18 -0700 | [diff] [blame] | 1386 | case S_IFREG: |
| 1387 | break; |
| 1388 | case S_IFLNK: |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1389 | data->rpc_status = -ELOOP; |
Trond Myklebust | 6f926b5 | 2005-10-18 14:20:18 -0700 | [diff] [blame] | 1390 | break; |
| 1391 | case S_IFDIR: |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1392 | data->rpc_status = -EISDIR; |
Trond Myklebust | 6f926b5 | 2005-10-18 14:20:18 -0700 | [diff] [blame] | 1393 | break; |
| 1394 | default: |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1395 | data->rpc_status = -ENOTDIR; |
Trond Myklebust | 6f926b5 | 2005-10-18 14:20:18 -0700 | [diff] [blame] | 1396 | } |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 1397 | renew_lease(data->o_res.server, data->timestamp); |
Trond Myklebust | 0f9f95e | 2007-07-08 16:19:56 -0400 | [diff] [blame] | 1398 | if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)) |
| 1399 | nfs_confirm_seqid(&data->owner->so_seqid, 0); |
Trond Myklebust | 6f926b5 | 2005-10-18 14:20:18 -0700 | [diff] [blame] | 1400 | } |
Trond Myklebust | 3e30991 | 2007-07-07 13:19:59 -0400 | [diff] [blame] | 1401 | data->rpc_done = 1; |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1402 | } |
Trond Myklebust | 6f926b5 | 2005-10-18 14:20:18 -0700 | [diff] [blame] | 1403 | |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1404 | static void nfs4_open_release(void *calldata) |
| 1405 | { |
| 1406 | struct nfs4_opendata *data = calldata; |
| 1407 | struct nfs4_state *state = NULL; |
| 1408 | |
| 1409 | /* If this request hasn't been cancelled, do nothing */ |
| 1410 | if (data->cancelled == 0) |
| 1411 | goto out_free; |
| 1412 | /* In case of error, no cleanup! */ |
Trond Myklebust | 3e30991 | 2007-07-07 13:19:59 -0400 | [diff] [blame] | 1413 | if (data->rpc_status != 0 || !data->rpc_done) |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1414 | goto out_free; |
| 1415 | /* In case we need an open_confirm, no cleanup! */ |
| 1416 | if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) |
| 1417 | goto out_free; |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1418 | state = nfs4_opendata_to_nfs4_state(data); |
Trond Myklebust | 1b370bc | 2007-07-07 08:04:47 -0400 | [diff] [blame] | 1419 | if (!IS_ERR(state)) |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1420 | nfs4_close_state(&data->path, state, data->o_arg.fmode); |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1421 | out_free: |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 1422 | nfs4_opendata_put(data); |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | static const struct rpc_call_ops nfs4_open_ops = { |
| 1426 | .rpc_call_prepare = nfs4_open_prepare, |
| 1427 | .rpc_call_done = nfs4_open_done, |
| 1428 | .rpc_release = nfs4_open_release, |
| 1429 | }; |
| 1430 | |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 1431 | static const struct rpc_call_ops nfs4_recover_open_ops = { |
| 1432 | .rpc_call_prepare = nfs4_recover_open_prepare, |
| 1433 | .rpc_call_done = nfs4_open_done, |
| 1434 | .rpc_release = nfs4_open_release, |
| 1435 | }; |
| 1436 | |
| 1437 | static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover) |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1438 | { |
| 1439 | struct inode *dir = data->dir->d_inode; |
| 1440 | struct nfs_server *server = NFS_SERVER(dir); |
| 1441 | struct nfs_openargs *o_arg = &data->o_arg; |
| 1442 | struct nfs_openres *o_res = &data->o_res; |
| 1443 | struct rpc_task *task; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 1444 | struct rpc_message msg = { |
| 1445 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN], |
| 1446 | .rpc_argp = o_arg, |
| 1447 | .rpc_resp = o_res, |
| 1448 | .rpc_cred = data->owner->so_cred, |
| 1449 | }; |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1450 | struct rpc_task_setup task_setup_data = { |
| 1451 | .rpc_client = server->client, |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 1452 | .rpc_message = &msg, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1453 | .callback_ops = &nfs4_open_ops, |
| 1454 | .callback_data = data, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 1455 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1456 | .flags = RPC_TASK_ASYNC, |
| 1457 | }; |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1458 | int status; |
| 1459 | |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 1460 | kref_get(&data->kref); |
Trond Myklebust | 3e30991 | 2007-07-07 13:19:59 -0400 | [diff] [blame] | 1461 | data->rpc_done = 0; |
| 1462 | data->rpc_status = 0; |
Trond Myklebust | 2ced46c | 2007-07-03 23:48:13 -0400 | [diff] [blame] | 1463 | data->cancelled = 0; |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 1464 | if (isrecover) |
| 1465 | task_setup_data.callback_ops = &nfs4_recover_open_ops; |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1466 | task = rpc_run_task(&task_setup_data); |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 1467 | if (IS_ERR(task)) |
| 1468 | return PTR_ERR(task); |
| 1469 | status = nfs4_wait_for_completion_rpc_task(task); |
| 1470 | if (status != 0) { |
| 1471 | data->cancelled = 1; |
| 1472 | smp_wmb(); |
| 1473 | } else |
| 1474 | status = data->rpc_status; |
| 1475 | rpc_put_task(task); |
| 1476 | |
| 1477 | return status; |
| 1478 | } |
| 1479 | |
| 1480 | static int _nfs4_recover_proc_open(struct nfs4_opendata *data) |
| 1481 | { |
| 1482 | struct inode *dir = data->dir->d_inode; |
| 1483 | struct nfs_openres *o_res = &data->o_res; |
| 1484 | int status; |
| 1485 | |
| 1486 | status = nfs4_run_open_task(data, 1); |
| 1487 | if (status != 0 || !data->rpc_done) |
| 1488 | return status; |
| 1489 | |
| 1490 | nfs_refresh_inode(dir, o_res->dir_attr); |
| 1491 | |
| 1492 | if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { |
| 1493 | status = _nfs4_proc_open_confirm(data); |
| 1494 | if (status != 0) |
| 1495 | return status; |
| 1496 | } |
| 1497 | |
| 1498 | return status; |
| 1499 | } |
| 1500 | |
| 1501 | /* |
| 1502 | * Note: On error, nfs4_proc_open will free the struct nfs4_opendata |
| 1503 | */ |
| 1504 | static int _nfs4_proc_open(struct nfs4_opendata *data) |
| 1505 | { |
| 1506 | struct inode *dir = data->dir->d_inode; |
| 1507 | struct nfs_server *server = NFS_SERVER(dir); |
| 1508 | struct nfs_openargs *o_arg = &data->o_arg; |
| 1509 | struct nfs_openres *o_res = &data->o_res; |
| 1510 | int status; |
| 1511 | |
| 1512 | status = nfs4_run_open_task(data, 0); |
Trond Myklebust | 3e30991 | 2007-07-07 13:19:59 -0400 | [diff] [blame] | 1513 | if (status != 0 || !data->rpc_done) |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1514 | return status; |
| 1515 | |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 1516 | if (o_arg->open_flags & O_CREAT) { |
| 1517 | update_changeattr(dir, &o_res->cinfo); |
| 1518 | nfs_post_op_update_inode(dir, o_res->dir_attr); |
| 1519 | } else |
| 1520 | nfs_refresh_inode(dir, o_res->dir_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { |
Trond Myklebust | cdd4e68 | 2006-01-03 09:55:12 +0100 | [diff] [blame] | 1522 | status = _nfs4_proc_open_confirm(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | if (status != 0) |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1524 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | } |
| 1526 | if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) |
Trond Myklebust | 9936781 | 2007-07-17 21:52:41 -0400 | [diff] [blame] | 1527 | _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr); |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1528 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Trond Myklebust | 10afec9 | 2007-05-14 17:16:04 -0400 | [diff] [blame] | 1531 | static int nfs4_recover_expired_lease(struct nfs_server *server) |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 1532 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 1533 | struct nfs_client *clp = server->nfs_client; |
Trond Myklebust | a78cb57 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 1534 | unsigned int loop; |
Trond Myklebust | 6b30954 | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 1535 | int ret; |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 1536 | |
Trond Myklebust | a78cb57 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 1537 | for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) { |
Trond Myklebust | 65de872 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 1538 | ret = nfs4_wait_clnt_recover(clp); |
Trond Myklebust | 6b30954 | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 1539 | if (ret != 0) |
Trond Myklebust | a78cb57 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 1540 | break; |
Trond Myklebust | e598d84 | 2008-12-23 15:21:42 -0500 | [diff] [blame] | 1541 | if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) && |
| 1542 | !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state)) |
Trond Myklebust | 6b30954 | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 1543 | break; |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 1544 | nfs4_schedule_state_recovery(clp); |
Trond Myklebust | a78cb57 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 1545 | ret = -EIO; |
Trond Myklebust | 6b30954 | 2006-09-14 14:03:14 -0400 | [diff] [blame] | 1546 | } |
Trond Myklebust | a78cb57 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 1547 | return ret; |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 1548 | } |
| 1549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | /* |
| 1551 | * OPEN_EXPIRED: |
| 1552 | * reclaim state on the server after a network partition. |
| 1553 | * Assumes caller holds the appropriate lock |
| 1554 | */ |
Trond Myklebust | 539cd03 | 2007-06-05 11:46:42 -0400 | [diff] [blame] | 1555 | static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | { |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 1557 | struct nfs4_opendata *opendata; |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1558 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | |
Trond Myklebust | 6f220ed | 2007-07-17 21:50:45 -0400 | [diff] [blame] | 1560 | opendata = nfs4_open_recoverdata_alloc(ctx, state); |
| 1561 | if (IS_ERR(opendata)) |
| 1562 | return PTR_ERR(opendata); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1563 | ret = nfs4_open_recover(opendata, state); |
Trond Myklebust | 35d0577 | 2008-04-05 15:54:17 -0400 | [diff] [blame] | 1564 | if (ret == -ESTALE) |
Trond Myklebust | 539cd03 | 2007-06-05 11:46:42 -0400 | [diff] [blame] | 1565 | d_drop(ctx->path.dentry); |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 1566 | nfs4_opendata_put(opendata); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1567 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
Trond Myklebust | a9ed2e2 | 2009-12-03 15:53:21 -0500 | [diff] [blame] | 1570 | static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 1571 | { |
Trond Myklebust | 539cd03 | 2007-06-05 11:46:42 -0400 | [diff] [blame] | 1572 | struct nfs_server *server = NFS_SERVER(state->inode); |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 1573 | struct nfs4_exception exception = { }; |
| 1574 | int err; |
| 1575 | |
| 1576 | do { |
Trond Myklebust | 539cd03 | 2007-06-05 11:46:42 -0400 | [diff] [blame] | 1577 | err = _nfs4_open_expired(ctx, state); |
Trond Myklebust | a9ed2e2 | 2009-12-03 15:53:21 -0500 | [diff] [blame] | 1578 | switch (err) { |
| 1579 | default: |
| 1580 | goto out; |
| 1581 | case -NFS4ERR_GRACE: |
| 1582 | case -NFS4ERR_DELAY: |
| 1583 | nfs4_handle_exception(server, err, &exception); |
| 1584 | err = 0; |
| 1585 | } |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 1586 | } while (exception.retry); |
Trond Myklebust | a9ed2e2 | 2009-12-03 15:53:21 -0500 | [diff] [blame] | 1587 | out: |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 1588 | return err; |
| 1589 | } |
| 1590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) |
| 1592 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | struct nfs_open_context *ctx; |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1594 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1596 | ctx = nfs4_state_find_open_context(state); |
| 1597 | if (IS_ERR(ctx)) |
| 1598 | return PTR_ERR(ctx); |
Trond Myklebust | 539cd03 | 2007-06-05 11:46:42 -0400 | [diff] [blame] | 1599 | ret = nfs4_do_open_expired(ctx, state); |
Trond Myklebust | 864472e | 2006-01-03 09:55:15 +0100 | [diff] [blame] | 1600 | put_nfs_open_context(ctx); |
| 1601 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | /* |
Jeff Layton | aa53ed5 | 2007-06-05 14:49:03 -0400 | [diff] [blame] | 1605 | * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-* |
| 1606 | * fields corresponding to attributes that were used to store the verifier. |
| 1607 | * Make sure we clobber those fields in the later setattr call |
| 1608 | */ |
| 1609 | static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr) |
| 1610 | { |
| 1611 | if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) && |
| 1612 | !(sattr->ia_valid & ATTR_ATIME_SET)) |
| 1613 | sattr->ia_valid |= ATTR_ATIME; |
| 1614 | |
| 1615 | if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) && |
| 1616 | !(sattr->ia_valid & ATTR_MTIME_SET)) |
| 1617 | sattr->ia_valid |= ATTR_MTIME; |
| 1618 | } |
| 1619 | |
| 1620 | /* |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1621 | * Returns a referenced nfs4_state |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | */ |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1623 | static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | { |
| 1625 | struct nfs4_state_owner *sp; |
| 1626 | struct nfs4_state *state = NULL; |
| 1627 | struct nfs_server *server = NFS_SERVER(dir); |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 1628 | struct nfs4_opendata *opendata; |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 1629 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | |
| 1631 | /* Protect against reboot recovery conflicts */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | status = -ENOMEM; |
| 1633 | if (!(sp = nfs4_get_state_owner(server, cred))) { |
| 1634 | dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n"); |
| 1635 | goto out_err; |
| 1636 | } |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 1637 | status = nfs4_recover_expired_lease(server); |
| 1638 | if (status != 0) |
Trond Myklebust | b4454fe | 2006-01-03 09:55:25 +0100 | [diff] [blame] | 1639 | goto err_put_state_owner; |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 1640 | if (path->dentry->d_inode != NULL) |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1641 | nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 1642 | status = -ENOMEM; |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1643 | opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr); |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 1644 | if (opendata == NULL) |
Trond Myklebust | 95d35cb | 2008-12-23 15:21:45 -0500 | [diff] [blame] | 1645 | goto err_put_state_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 1647 | if (path->dentry->d_inode != NULL) |
| 1648 | opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp); |
| 1649 | |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1650 | status = _nfs4_proc_open(opendata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | if (status != 0) |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 1652 | goto err_opendata_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | |
Jeff Layton | aa53ed5 | 2007-06-05 14:49:03 -0400 | [diff] [blame] | 1654 | if (opendata->o_arg.open_flags & O_EXCL) |
| 1655 | nfs4_exclusive_attrset(opendata, sattr); |
| 1656 | |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 1657 | state = nfs4_opendata_to_nfs4_state(opendata); |
Trond Myklebust | 1b370bc | 2007-07-07 08:04:47 -0400 | [diff] [blame] | 1658 | status = PTR_ERR(state); |
| 1659 | if (IS_ERR(state)) |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 1660 | goto err_opendata_put; |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 1661 | nfs4_opendata_put(opendata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | nfs4_put_state_owner(sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | *res = state; |
| 1664 | return 0; |
Trond Myklebust | c6d00e6 | 2007-06-17 16:02:44 -0400 | [diff] [blame] | 1665 | err_opendata_put: |
| 1666 | nfs4_opendata_put(opendata); |
Trond Myklebust | e56e0b78 | 2006-01-03 09:55:08 +0100 | [diff] [blame] | 1667 | err_put_state_owner: |
| 1668 | nfs4_put_state_owner(sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | out_err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | *res = NULL; |
| 1671 | return status; |
| 1672 | } |
| 1673 | |
| 1674 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1675 | static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | { |
| 1677 | struct nfs4_exception exception = { }; |
| 1678 | struct nfs4_state *res; |
| 1679 | int status; |
| 1680 | |
| 1681 | do { |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1682 | status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | if (status == 0) |
| 1684 | break; |
| 1685 | /* NOTE: BAD_SEQID means the server and client disagree about the |
| 1686 | * book-keeping w.r.t. state-changing operations |
| 1687 | * (OPEN/CLOSE/LOCK/LOCKU...) |
| 1688 | * It is actually a sign of a bug on the client or on the server. |
| 1689 | * |
| 1690 | * If we receive a BAD_SEQID error in the particular case of |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1691 | * doing an OPEN, we assume that nfs_increment_open_seqid() will |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | * have unhashed the old state_owner for us, and that we can |
| 1693 | * therefore safely retry using a new one. We should still warn |
| 1694 | * the user though... |
| 1695 | */ |
| 1696 | if (status == -NFS4ERR_BAD_SEQID) { |
Trond Myklebust | 6f43ddc | 2007-07-08 16:49:11 -0400 | [diff] [blame] | 1697 | printk(KERN_WARNING "NFS: v4 server %s " |
| 1698 | " returned a bad sequence-id error!\n", |
| 1699 | NFS_SERVER(dir)->nfs_client->cl_hostname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | exception.retry = 1; |
| 1701 | continue; |
| 1702 | } |
Trond Myklebust | 550f574 | 2005-10-18 14:20:21 -0700 | [diff] [blame] | 1703 | /* |
| 1704 | * BAD_STATEID on OPEN means that the server cancelled our |
| 1705 | * state before it received the OPEN_CONFIRM. |
| 1706 | * Recover by retrying the request as per the discussion |
| 1707 | * on Page 181 of RFC3530. |
| 1708 | */ |
| 1709 | if (status == -NFS4ERR_BAD_STATEID) { |
| 1710 | exception.retry = 1; |
| 1711 | continue; |
| 1712 | } |
Trond Myklebust | aac00a8 | 2007-07-05 19:02:21 -0400 | [diff] [blame] | 1713 | if (status == -EAGAIN) { |
| 1714 | /* We must have found a delegation */ |
| 1715 | exception.retry = 1; |
| 1716 | continue; |
| 1717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir), |
| 1719 | status, &exception)); |
| 1720 | } while (exception.retry); |
| 1721 | return res; |
| 1722 | } |
| 1723 | |
Trond Myklebust | 659bfcd | 2008-06-10 19:39:41 -0400 | [diff] [blame] | 1724 | static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, |
| 1725 | struct nfs_fattr *fattr, struct iattr *sattr, |
| 1726 | struct nfs4_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | { |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 1728 | struct nfs_server *server = NFS_SERVER(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | struct nfs_setattrargs arg = { |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 1730 | .fh = NFS_FH(inode), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | .iap = sattr, |
| 1732 | .server = server, |
| 1733 | .bitmask = server->attr_bitmask, |
| 1734 | }; |
| 1735 | struct nfs_setattrres res = { |
| 1736 | .fattr = fattr, |
| 1737 | .server = server, |
| 1738 | }; |
| 1739 | struct rpc_message msg = { |
Trond Myklebust | 659bfcd | 2008-06-10 19:39:41 -0400 | [diff] [blame] | 1740 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], |
| 1741 | .rpc_argp = &arg, |
| 1742 | .rpc_resp = &res, |
| 1743 | .rpc_cred = cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | }; |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 1745 | unsigned long timestamp = jiffies; |
Trond Myklebust | 65e4308 | 2005-08-16 11:49:44 -0400 | [diff] [blame] | 1746 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1748 | nfs_fattr_init(fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 1750 | if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) { |
| 1751 | /* Use that stateid */ |
| 1752 | } else if (state != NULL) { |
Trond Myklebust | 08e9eac | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 1753 | nfs4_copy_stateid(&arg.stateid, state, current->files); |
| 1754 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid)); |
| 1756 | |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 1757 | status = nfs4_call_sync(server, &msg, &arg, &res, 1); |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 1758 | if (status == 0 && state != NULL) |
| 1759 | renew_lease(server, timestamp); |
Trond Myklebust | 65e4308 | 2005-08-16 11:49:44 -0400 | [diff] [blame] | 1760 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
Trond Myklebust | 659bfcd | 2008-06-10 19:39:41 -0400 | [diff] [blame] | 1763 | static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, |
| 1764 | struct nfs_fattr *fattr, struct iattr *sattr, |
| 1765 | struct nfs4_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | { |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 1767 | struct nfs_server *server = NFS_SERVER(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | struct nfs4_exception exception = { }; |
| 1769 | int err; |
| 1770 | do { |
| 1771 | err = nfs4_handle_exception(server, |
Trond Myklebust | 659bfcd | 2008-06-10 19:39:41 -0400 | [diff] [blame] | 1772 | _nfs4_do_setattr(inode, cred, fattr, sattr, state), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | &exception); |
| 1774 | } while (exception.retry); |
| 1775 | return err; |
| 1776 | } |
| 1777 | |
| 1778 | struct nfs4_closedata { |
Trond Myklebust | 4a35bd4 | 2007-06-05 10:31:33 -0400 | [diff] [blame] | 1779 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | struct inode *inode; |
| 1781 | struct nfs4_state *state; |
| 1782 | struct nfs_closeargs arg; |
| 1783 | struct nfs_closeres res; |
Trond Myklebust | 516a6af | 2005-10-27 22:12:41 -0400 | [diff] [blame] | 1784 | struct nfs_fattr fattr; |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 1785 | unsigned long timestamp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | }; |
| 1787 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1788 | static void nfs4_free_closedata(void *data) |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1789 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1790 | struct nfs4_closedata *calldata = data; |
| 1791 | struct nfs4_state_owner *sp = calldata->state->owner; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1792 | |
| 1793 | nfs4_put_open_state(calldata->state); |
| 1794 | nfs_free_seqid(calldata->arg.seqid); |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1795 | nfs4_put_state_owner(sp); |
Jan Blunck | 31f31db1 | 2008-05-02 13:42:45 -0700 | [diff] [blame] | 1796 | path_put(&calldata->path); |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1797 | kfree(calldata); |
| 1798 | } |
| 1799 | |
Trond Myklebust | 88069f7 | 2009-12-08 08:33:16 -0500 | [diff] [blame] | 1800 | static void nfs4_close_clear_stateid_flags(struct nfs4_state *state, |
| 1801 | fmode_t fmode) |
| 1802 | { |
| 1803 | spin_lock(&state->owner->so_lock); |
| 1804 | if (!(fmode & FMODE_READ)) |
| 1805 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); |
| 1806 | if (!(fmode & FMODE_WRITE)) |
| 1807 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); |
| 1808 | clear_bit(NFS_O_RDWR_STATE, &state->flags); |
| 1809 | spin_unlock(&state->owner->so_lock); |
| 1810 | } |
| 1811 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1812 | static void nfs4_close_done(struct rpc_task *task, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1814 | struct nfs4_closedata *calldata = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | struct nfs4_state *state = calldata->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | struct nfs_server *server = NFS_SERVER(calldata->inode); |
| 1817 | |
Andy Adamson | 19ddab0 | 2009-04-01 09:22:20 -0400 | [diff] [blame] | 1818 | nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status); |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 1819 | if (RPC_ASSASSINATED(task)) |
| 1820 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | /* hmm. we are done with the inode, and in the process of freeing |
| 1822 | * the state_owner. we keep this around to process errors |
| 1823 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | switch (task->tk_status) { |
| 1825 | case 0: |
Trond Myklebust | 45328c3 | 2007-07-26 17:47:34 -0400 | [diff] [blame] | 1826 | nfs_set_open_stateid(state, &calldata->res.stateid, 0); |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 1827 | renew_lease(server, calldata->timestamp); |
Trond Myklebust | 88069f7 | 2009-12-08 08:33:16 -0500 | [diff] [blame] | 1828 | nfs4_close_clear_stateid_flags(state, |
| 1829 | calldata->arg.fmode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | break; |
| 1831 | case -NFS4ERR_STALE_STATEID: |
Trond Myklebust | 9e33bed | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 1832 | case -NFS4ERR_OLD_STATEID: |
| 1833 | case -NFS4ERR_BAD_STATEID: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | case -NFS4ERR_EXPIRED: |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1835 | if (calldata->arg.fmode == 0) |
Trond Myklebust | 9e33bed | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 1836 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | default: |
Trond Myklebust | 72211db | 2009-12-15 14:47:36 -0500 | [diff] [blame] | 1838 | if (nfs4_async_handle_error(task, server, state) == -EAGAIN) |
| 1839 | rpc_restart_call_prepare(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | } |
Trond Myklebust | 72211db | 2009-12-15 14:47:36 -0500 | [diff] [blame] | 1841 | nfs_release_seqid(calldata->arg.seqid); |
Trond Myklebust | 516a6af | 2005-10-27 22:12:41 -0400 | [diff] [blame] | 1842 | nfs_refresh_inode(calldata->inode, calldata->res.fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 1845 | static void nfs4_close_prepare(struct rpc_task *task, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | { |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 1847 | struct nfs4_closedata *calldata = data; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1848 | struct nfs4_state *state = calldata->state; |
Trond Myklebust | 88069f7 | 2009-12-08 08:33:16 -0500 | [diff] [blame] | 1849 | int call_close = 0; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1850 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1851 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1852 | return; |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 1853 | |
Trond Myklebust | 88069f7 | 2009-12-08 08:33:16 -0500 | [diff] [blame] | 1854 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE]; |
| 1855 | calldata->arg.fmode = FMODE_READ|FMODE_WRITE; |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 1856 | spin_lock(&state->owner->so_lock); |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 1857 | /* Calculate the change in open mode */ |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 1858 | if (state->n_rdwr == 0) { |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 1859 | if (state->n_rdonly == 0) { |
Trond Myklebust | 88069f7 | 2009-12-08 08:33:16 -0500 | [diff] [blame] | 1860 | call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags); |
| 1861 | call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags); |
| 1862 | calldata->arg.fmode &= ~FMODE_READ; |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 1863 | } |
| 1864 | if (state->n_wronly == 0) { |
Trond Myklebust | 88069f7 | 2009-12-08 08:33:16 -0500 | [diff] [blame] | 1865 | call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags); |
| 1866 | call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags); |
| 1867 | calldata->arg.fmode &= ~FMODE_WRITE; |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 1868 | } |
Trond Myklebust | e761692 | 2006-01-03 09:55:13 +0100 | [diff] [blame] | 1869 | } |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 1870 | spin_unlock(&state->owner->so_lock); |
Trond Myklebust | 88069f7 | 2009-12-08 08:33:16 -0500 | [diff] [blame] | 1871 | |
| 1872 | if (!call_close) { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1873 | /* Note: exit _without_ calling nfs4_close_done */ |
| 1874 | task->tk_action = NULL; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1875 | return; |
| 1876 | } |
Trond Myklebust | 88069f7 | 2009-12-08 08:33:16 -0500 | [diff] [blame] | 1877 | |
| 1878 | if (calldata->arg.fmode == 0) |
| 1879 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE]; |
| 1880 | |
Trond Myklebust | 516a6af | 2005-10-27 22:12:41 -0400 | [diff] [blame] | 1881 | nfs_fattr_init(calldata->res.fattr); |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 1882 | calldata->timestamp = jiffies; |
Andy Adamson | 19ddab0 | 2009-04-01 09:22:20 -0400 | [diff] [blame] | 1883 | if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client, |
| 1884 | &calldata->arg.seq_args, &calldata->res.seq_res, |
| 1885 | 1, task)) |
| 1886 | return; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 1887 | rpc_call_start(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1890 | static const struct rpc_call_ops nfs4_close_ops = { |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 1891 | .rpc_call_prepare = nfs4_close_prepare, |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1892 | .rpc_call_done = nfs4_close_done, |
| 1893 | .rpc_release = nfs4_free_closedata, |
| 1894 | }; |
| 1895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | /* |
| 1897 | * It is possible for data to be read/written from a mem-mapped file |
| 1898 | * after the sys_close call (which hits the vfs layer as a flush). |
| 1899 | * This means that we can't safely call nfsv4 close on a file until |
| 1900 | * the inode is cleared. This in turn means that we are not good |
| 1901 | * NFSv4 citizens - we do not indicate to the server to update the file's |
| 1902 | * share state even when we are done with one of the three share |
| 1903 | * stateid's in the inode. |
| 1904 | * |
| 1905 | * NOTE: Caller must be holding the sp->so_owner semaphore! |
| 1906 | */ |
Trond Myklebust | a49c3c7 | 2007-10-18 18:03:27 -0400 | [diff] [blame] | 1907 | int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | { |
Trond Myklebust | 4a35bd4 | 2007-06-05 10:31:33 -0400 | [diff] [blame] | 1909 | struct nfs_server *server = NFS_SERVER(state->inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | struct nfs4_closedata *calldata; |
Trond Myklebust | b39e625 | 2007-06-11 23:05:07 -0400 | [diff] [blame] | 1911 | struct nfs4_state_owner *sp = state->owner; |
| 1912 | struct rpc_task *task; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 1913 | struct rpc_message msg = { |
| 1914 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE], |
| 1915 | .rpc_cred = state->owner->so_cred, |
| 1916 | }; |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1917 | struct rpc_task_setup task_setup_data = { |
| 1918 | .rpc_client = server->client, |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 1919 | .rpc_message = &msg, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1920 | .callback_ops = &nfs4_close_ops, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 1921 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1922 | .flags = RPC_TASK_ASYNC, |
| 1923 | }; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1924 | int status = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | |
Andy Adamson | 19ddab0 | 2009-04-01 09:22:20 -0400 | [diff] [blame] | 1926 | calldata = kzalloc(sizeof(*calldata), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | if (calldata == NULL) |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1928 | goto out; |
Trond Myklebust | 4a35bd4 | 2007-06-05 10:31:33 -0400 | [diff] [blame] | 1929 | calldata->inode = state->inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | calldata->state = state; |
Trond Myklebust | 4a35bd4 | 2007-06-05 10:31:33 -0400 | [diff] [blame] | 1931 | calldata->arg.fh = NFS_FH(state->inode); |
Trond Myklebust | 003707c | 2007-07-05 18:07:55 -0400 | [diff] [blame] | 1932 | calldata->arg.stateid = &state->open_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | /* Serialization for the sequence id */ |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1934 | calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid); |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1935 | if (calldata->arg.seqid == NULL) |
| 1936 | goto out_free_calldata; |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1937 | calldata->arg.fmode = 0; |
Trond Myklebust | a65318b | 2009-03-11 14:10:28 -0400 | [diff] [blame] | 1938 | calldata->arg.bitmask = server->cache_consistency_bitmask; |
Trond Myklebust | 516a6af | 2005-10-27 22:12:41 -0400 | [diff] [blame] | 1939 | calldata->res.fattr = &calldata->fattr; |
Trond Myklebust | c1d5193 | 2008-04-07 13:20:54 -0400 | [diff] [blame] | 1940 | calldata->res.seqid = calldata->arg.seqid; |
Trond Myklebust | 516a6af | 2005-10-27 22:12:41 -0400 | [diff] [blame] | 1941 | calldata->res.server = server; |
Andy Adamson | 5f7dbd5 | 2009-04-01 09:22:05 -0400 | [diff] [blame] | 1942 | calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; |
Trond Myklebust | 4a35bd4 | 2007-06-05 10:31:33 -0400 | [diff] [blame] | 1943 | calldata->path.mnt = mntget(path->mnt); |
| 1944 | calldata->path.dentry = dget(path->dentry); |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1945 | |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 1946 | msg.rpc_argp = &calldata->arg, |
| 1947 | msg.rpc_resp = &calldata->res, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1948 | task_setup_data.callback_data = calldata; |
| 1949 | task = rpc_run_task(&task_setup_data); |
Trond Myklebust | b39e625 | 2007-06-11 23:05:07 -0400 | [diff] [blame] | 1950 | if (IS_ERR(task)) |
| 1951 | return PTR_ERR(task); |
Trond Myklebust | a49c3c7 | 2007-10-18 18:03:27 -0400 | [diff] [blame] | 1952 | status = 0; |
| 1953 | if (wait) |
| 1954 | status = rpc_wait_for_completion_task(task); |
Trond Myklebust | b39e625 | 2007-06-11 23:05:07 -0400 | [diff] [blame] | 1955 | rpc_put_task(task); |
Trond Myklebust | a49c3c7 | 2007-10-18 18:03:27 -0400 | [diff] [blame] | 1956 | return status; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1957 | out_free_calldata: |
| 1958 | kfree(calldata); |
| 1959 | out: |
Trond Myklebust | b39e625 | 2007-06-11 23:05:07 -0400 | [diff] [blame] | 1960 | nfs4_put_open_state(state); |
| 1961 | nfs4_put_state_owner(sp); |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 1962 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1965 | static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode) |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1966 | { |
| 1967 | struct file *filp; |
Trond Myklebust | 1b45c46 | 2007-07-03 13:04:56 -0400 | [diff] [blame] | 1968 | int ret; |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1969 | |
Trond Myklebust | 1b45c46 | 2007-07-03 13:04:56 -0400 | [diff] [blame] | 1970 | /* If the open_intent is for execute, we have an extra check to make */ |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1971 | if (fmode & FMODE_EXEC) { |
Trond Myklebust | af22f94 | 2007-08-10 17:45:10 -0400 | [diff] [blame] | 1972 | ret = nfs_may_open(state->inode, |
Trond Myklebust | 1b45c46 | 2007-07-03 13:04:56 -0400 | [diff] [blame] | 1973 | state->owner->so_cred, |
| 1974 | nd->intent.open.flags); |
| 1975 | if (ret < 0) |
| 1976 | goto out_close; |
| 1977 | } |
Trond Myklebust | 4a35bd4 | 2007-06-05 10:31:33 -0400 | [diff] [blame] | 1978 | filp = lookup_instantiate_filp(nd, path->dentry, NULL); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1979 | if (!IS_ERR(filp)) { |
| 1980 | struct nfs_open_context *ctx; |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 1981 | ctx = nfs_file_open_context(filp); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1982 | ctx->state = state; |
Trond Myklebust | 95cf959 | 2006-04-18 13:14:06 -0400 | [diff] [blame] | 1983 | return 0; |
| 1984 | } |
Trond Myklebust | 1b45c46 | 2007-07-03 13:04:56 -0400 | [diff] [blame] | 1985 | ret = PTR_ERR(filp); |
| 1986 | out_close: |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 1987 | nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE)); |
Trond Myklebust | 1b45c46 | 2007-07-03 13:04:56 -0400 | [diff] [blame] | 1988 | return ret; |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | struct dentry * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
| 1993 | { |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 1994 | struct path path = { |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1995 | .mnt = nd->path.mnt, |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 1996 | .dentry = dentry, |
| 1997 | }; |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1998 | struct dentry *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | struct iattr attr; |
| 2000 | struct rpc_cred *cred; |
| 2001 | struct nfs4_state *state; |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2002 | struct dentry *res; |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 2003 | fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | |
| 2005 | if (nd->flags & LOOKUP_CREATE) { |
| 2006 | attr.ia_mode = nd->intent.open.create_mode; |
| 2007 | attr.ia_valid = ATTR_MODE; |
| 2008 | if (!IS_POSIXACL(dir)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 2009 | attr.ia_mode &= ~current_umask(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | } else { |
| 2011 | attr.ia_valid = 0; |
| 2012 | BUG_ON(nd->intent.open.flags & O_CREAT); |
| 2013 | } |
| 2014 | |
Trond Myklebust | 98a8e32 | 2008-03-12 12:25:28 -0400 | [diff] [blame] | 2015 | cred = rpc_lookup_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | if (IS_ERR(cred)) |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2017 | return (struct dentry *)cred; |
Trond Myklebust | 565277f | 2007-10-15 18:17:53 -0400 | [diff] [blame] | 2018 | parent = dentry->d_parent; |
| 2019 | /* Protect against concurrent sillydeletes */ |
| 2020 | nfs_block_sillyrename(parent); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 2021 | state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | put_rpccred(cred); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2023 | if (IS_ERR(state)) { |
Trond Myklebust | d75340c | 2007-10-01 21:42:01 -0400 | [diff] [blame] | 2024 | if (PTR_ERR(state) == -ENOENT) { |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2025 | d_add(dentry, NULL); |
Trond Myklebust | d75340c | 2007-10-01 21:42:01 -0400 | [diff] [blame] | 2026 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 2027 | } |
Trond Myklebust | 565277f | 2007-10-15 18:17:53 -0400 | [diff] [blame] | 2028 | nfs_unblock_sillyrename(parent); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2029 | return (struct dentry *)state; |
| 2030 | } |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 2031 | res = d_add_unique(dentry, igrab(state->inode)); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2032 | if (res != NULL) |
Trond Myklebust | deee936 | 2007-08-27 11:33:00 -0400 | [diff] [blame] | 2033 | path.dentry = res; |
Trond Myklebust | d75340c | 2007-10-01 21:42:01 -0400 | [diff] [blame] | 2034 | nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir)); |
Trond Myklebust | 565277f | 2007-10-15 18:17:53 -0400 | [diff] [blame] | 2035 | nfs_unblock_sillyrename(parent); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 2036 | nfs4_intent_set_file(nd, &path, state, fmode); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2037 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | int |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2041 | nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | { |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 2043 | struct path path = { |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 2044 | .mnt = nd->path.mnt, |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 2045 | .dentry = dentry, |
| 2046 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | struct rpc_cred *cred; |
| 2048 | struct nfs4_state *state; |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 2049 | fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | |
Trond Myklebust | 98a8e32 | 2008-03-12 12:25:28 -0400 | [diff] [blame] | 2051 | cred = rpc_lookup_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | if (IS_ERR(cred)) |
| 2053 | return PTR_ERR(cred); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 2054 | state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | put_rpccred(cred); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2056 | if (IS_ERR(state)) { |
| 2057 | switch (PTR_ERR(state)) { |
| 2058 | case -EPERM: |
| 2059 | case -EACCES: |
| 2060 | case -EDQUOT: |
| 2061 | case -ENOSPC: |
| 2062 | case -EROFS: |
| 2063 | lookup_instantiate_filp(nd, (struct dentry *)state, NULL); |
| 2064 | return 1; |
Chuck Lever | b87c0ad | 2006-10-19 23:28:42 -0700 | [diff] [blame] | 2065 | default: |
| 2066 | goto out_drop; |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2067 | } |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2068 | } |
Trond Myklebust | 24ac23a | 2006-01-03 09:55:11 +0100 | [diff] [blame] | 2069 | if (state->inode == dentry->d_inode) { |
Trond Myklebust | d75340c | 2007-10-01 21:42:01 -0400 | [diff] [blame] | 2070 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 2071 | nfs4_intent_set_file(nd, &path, state, fmode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | return 1; |
| 2073 | } |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 2074 | nfs4_close_sync(&path, state, fmode); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2075 | out_drop: |
| 2076 | d_drop(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | return 0; |
| 2078 | } |
| 2079 | |
Trond Myklebust | 1185a55 | 2009-12-03 15:54:02 -0500 | [diff] [blame] | 2080 | static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync) |
Trond Myklebust | 7fe5c39 | 2009-03-19 15:35:50 -0400 | [diff] [blame] | 2081 | { |
| 2082 | if (ctx->state == NULL) |
| 2083 | return; |
| 2084 | if (is_sync) |
| 2085 | nfs4_close_sync(&ctx->path, ctx->state, ctx->mode); |
| 2086 | else |
| 2087 | nfs4_close_state(&ctx->path, ctx->state, ctx->mode); |
| 2088 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | |
| 2090 | static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
| 2091 | { |
Benny Halevy | 43652ad | 2009-04-01 09:21:54 -0400 | [diff] [blame] | 2092 | struct nfs4_server_caps_arg args = { |
| 2093 | .fhandle = fhandle, |
| 2094 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | struct nfs4_server_caps_res res = {}; |
| 2096 | struct rpc_message msg = { |
| 2097 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS], |
Benny Halevy | 43652ad | 2009-04-01 09:21:54 -0400 | [diff] [blame] | 2098 | .rpc_argp = &args, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | .rpc_resp = &res, |
| 2100 | }; |
| 2101 | int status; |
| 2102 | |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 2103 | status = nfs4_call_sync(server, &msg, &args, &res, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | if (status == 0) { |
| 2105 | memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask)); |
Trond Myklebust | 62ab460 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 2106 | server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS| |
| 2107 | NFS_CAP_SYMLINKS|NFS_CAP_FILEID| |
| 2108 | NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER| |
| 2109 | NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME| |
| 2110 | NFS_CAP_CTIME|NFS_CAP_MTIME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | if (res.attr_bitmask[0] & FATTR4_WORD0_ACL) |
| 2112 | server->caps |= NFS_CAP_ACLS; |
| 2113 | if (res.has_links != 0) |
| 2114 | server->caps |= NFS_CAP_HARDLINKS; |
| 2115 | if (res.has_symlinks != 0) |
| 2116 | server->caps |= NFS_CAP_SYMLINKS; |
Trond Myklebust | 62ab460 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 2117 | if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID) |
| 2118 | server->caps |= NFS_CAP_FILEID; |
| 2119 | if (res.attr_bitmask[1] & FATTR4_WORD1_MODE) |
| 2120 | server->caps |= NFS_CAP_MODE; |
| 2121 | if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS) |
| 2122 | server->caps |= NFS_CAP_NLINK; |
| 2123 | if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER) |
| 2124 | server->caps |= NFS_CAP_OWNER; |
| 2125 | if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP) |
| 2126 | server->caps |= NFS_CAP_OWNER_GROUP; |
| 2127 | if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS) |
| 2128 | server->caps |= NFS_CAP_ATIME; |
| 2129 | if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA) |
| 2130 | server->caps |= NFS_CAP_CTIME; |
| 2131 | if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY) |
| 2132 | server->caps |= NFS_CAP_MTIME; |
| 2133 | |
Trond Myklebust | a65318b | 2009-03-11 14:10:28 -0400 | [diff] [blame] | 2134 | memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask)); |
| 2135 | server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE; |
| 2136 | server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | server->acl_bitmask = res.acl_bitmask; |
| 2138 | } |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 2139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | return status; |
| 2141 | } |
| 2142 | |
Trond Myklebust | 55a9759 | 2006-06-09 09:34:19 -0400 | [diff] [blame] | 2143 | int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | { |
| 2145 | struct nfs4_exception exception = { }; |
| 2146 | int err; |
| 2147 | do { |
| 2148 | err = nfs4_handle_exception(server, |
| 2149 | _nfs4_server_capabilities(server, fhandle), |
| 2150 | &exception); |
| 2151 | } while (exception.retry); |
| 2152 | return err; |
| 2153 | } |
| 2154 | |
| 2155 | static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2156 | struct nfs_fsinfo *info) |
| 2157 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | struct nfs4_lookup_root_arg args = { |
| 2159 | .bitmask = nfs4_fattr_bitmap, |
| 2160 | }; |
| 2161 | struct nfs4_lookup_res res = { |
| 2162 | .server = server, |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 2163 | .fattr = info->fattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | .fh = fhandle, |
| 2165 | }; |
| 2166 | struct rpc_message msg = { |
| 2167 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT], |
| 2168 | .rpc_argp = &args, |
| 2169 | .rpc_resp = &res, |
| 2170 | }; |
Benny Halevy | 008f55d | 2009-04-01 09:22:50 -0400 | [diff] [blame] | 2171 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 2172 | nfs_fattr_init(info->fattr); |
Trond Myklebust | fccba80 | 2009-07-21 16:48:07 -0400 | [diff] [blame] | 2173 | return nfs4_call_sync(server, &msg, &args, &res, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2177 | struct nfs_fsinfo *info) |
| 2178 | { |
| 2179 | struct nfs4_exception exception = { }; |
| 2180 | int err; |
| 2181 | do { |
| 2182 | err = nfs4_handle_exception(server, |
| 2183 | _nfs4_lookup_root(server, fhandle, info), |
| 2184 | &exception); |
| 2185 | } while (exception.retry); |
| 2186 | return err; |
| 2187 | } |
| 2188 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 2189 | /* |
| 2190 | * get the file handle for the "/" directory on the server |
| 2191 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle, |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 2193 | struct nfs_fsinfo *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | int status; |
| 2196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | status = nfs4_lookup_root(server, fhandle, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | if (status == 0) |
| 2199 | status = nfs4_server_capabilities(server, fhandle); |
| 2200 | if (status == 0) |
| 2201 | status = nfs4_do_fsinfo(server, fhandle, info); |
Trond Myklebust | c12e87f | 2006-03-13 21:20:47 -0800 | [diff] [blame] | 2202 | return nfs4_map_errors(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
Manoj Naik | 6b97fd3 | 2006-06-09 09:34:29 -0400 | [diff] [blame] | 2205 | /* |
| 2206 | * Get locations and (maybe) other attributes of a referral. |
| 2207 | * Note that we'll actually follow the referral later when |
| 2208 | * we detect fsid mismatch in inode revalidation |
| 2209 | */ |
Trond Myklebust | 56659e9 | 2007-07-17 21:52:39 -0400 | [diff] [blame] | 2210 | static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle) |
Manoj Naik | 6b97fd3 | 2006-06-09 09:34:29 -0400 | [diff] [blame] | 2211 | { |
| 2212 | int status = -ENOMEM; |
| 2213 | struct page *page = NULL; |
| 2214 | struct nfs4_fs_locations *locations = NULL; |
Manoj Naik | 6b97fd3 | 2006-06-09 09:34:29 -0400 | [diff] [blame] | 2215 | |
| 2216 | page = alloc_page(GFP_KERNEL); |
| 2217 | if (page == NULL) |
| 2218 | goto out; |
| 2219 | locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); |
| 2220 | if (locations == NULL) |
| 2221 | goto out; |
| 2222 | |
Trond Myklebust | c228fd3 | 2007-01-13 02:28:11 -0500 | [diff] [blame] | 2223 | status = nfs4_proc_fs_locations(dir, name, locations, page); |
Manoj Naik | 6b97fd3 | 2006-06-09 09:34:29 -0400 | [diff] [blame] | 2224 | if (status != 0) |
| 2225 | goto out; |
| 2226 | /* Make sure server returned a different fsid for the referral */ |
| 2227 | if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) { |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 2228 | dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name); |
Manoj Naik | 6b97fd3 | 2006-06-09 09:34:29 -0400 | [diff] [blame] | 2229 | status = -EIO; |
| 2230 | goto out; |
| 2231 | } |
| 2232 | |
| 2233 | memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr)); |
| 2234 | fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL; |
| 2235 | if (!fattr->mode) |
| 2236 | fattr->mode = S_IFDIR; |
| 2237 | memset(fhandle, 0, sizeof(struct nfs_fh)); |
| 2238 | out: |
| 2239 | if (page) |
| 2240 | __free_page(page); |
| 2241 | if (locations) |
| 2242 | kfree(locations); |
| 2243 | return status; |
| 2244 | } |
| 2245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
| 2247 | { |
| 2248 | struct nfs4_getattr_arg args = { |
| 2249 | .fh = fhandle, |
| 2250 | .bitmask = server->attr_bitmask, |
| 2251 | }; |
| 2252 | struct nfs4_getattr_res res = { |
| 2253 | .fattr = fattr, |
| 2254 | .server = server, |
| 2255 | }; |
| 2256 | struct rpc_message msg = { |
| 2257 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], |
| 2258 | .rpc_argp = &args, |
| 2259 | .rpc_resp = &res, |
| 2260 | }; |
| 2261 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 2262 | nfs_fattr_init(fattr); |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 2263 | return nfs4_call_sync(server, &msg, &args, &res, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
| 2267 | { |
| 2268 | struct nfs4_exception exception = { }; |
| 2269 | int err; |
| 2270 | do { |
| 2271 | err = nfs4_handle_exception(server, |
| 2272 | _nfs4_proc_getattr(server, fhandle, fattr), |
| 2273 | &exception); |
| 2274 | } while (exception.retry); |
| 2275 | return err; |
| 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * The file is not closed if it is opened due to the a request to change |
| 2280 | * the size of the file. The open call will not be needed once the |
| 2281 | * VFS layer lookup-intents are implemented. |
| 2282 | * |
| 2283 | * Close is called when the inode is destroyed. |
| 2284 | * If we haven't opened the file for O_WRONLY, we |
| 2285 | * need to in the size_change case to obtain a stateid. |
| 2286 | * |
| 2287 | * Got race? |
| 2288 | * Because OPEN is always done by name in nfsv4, it is |
| 2289 | * possible that we opened a different file by the same |
| 2290 | * name. We can recognize this race condition, but we |
| 2291 | * can't do anything about it besides returning an error. |
| 2292 | * |
| 2293 | * This will be fixed with VFS changes (lookup-intent). |
| 2294 | */ |
| 2295 | static int |
| 2296 | nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr, |
| 2297 | struct iattr *sattr) |
| 2298 | { |
Trond Myklebust | 08e9eac | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 2299 | struct inode *inode = dentry->d_inode; |
Trond Myklebust | 659bfcd | 2008-06-10 19:39:41 -0400 | [diff] [blame] | 2300 | struct rpc_cred *cred = NULL; |
Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 2301 | struct nfs4_state *state = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | int status; |
| 2303 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 2304 | nfs_fattr_init(fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | |
Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 2306 | /* Search for an existing open(O_WRITE) file */ |
Trond Myklebust | 659bfcd | 2008-06-10 19:39:41 -0400 | [diff] [blame] | 2307 | if (sattr->ia_valid & ATTR_FILE) { |
| 2308 | struct nfs_open_context *ctx; |
Trond Myklebust | 08e9eac | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 2309 | |
Trond Myklebust | 659bfcd | 2008-06-10 19:39:41 -0400 | [diff] [blame] | 2310 | ctx = nfs_file_open_context(sattr->ia_file); |
Neil Brown | 504e518 | 2008-10-16 14:15:16 +1100 | [diff] [blame] | 2311 | if (ctx) { |
| 2312 | cred = ctx->cred; |
| 2313 | state = ctx->state; |
| 2314 | } |
Trond Myklebust | 659bfcd | 2008-06-10 19:39:41 -0400 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | status = nfs4_do_setattr(inode, cred, fattr, sattr, state); |
Trond Myklebust | 65e4308 | 2005-08-16 11:49:44 -0400 | [diff] [blame] | 2318 | if (status == 0) |
| 2319 | nfs_setattr_update_inode(inode, sattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | return status; |
| 2321 | } |
| 2322 | |
Trond Myklebust | 56659e9 | 2007-07-17 21:52:39 -0400 | [diff] [blame] | 2323 | static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh, |
| 2324 | const struct qstr *name, struct nfs_fh *fhandle, |
David Howells | 2b3de44 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 2325 | struct nfs_fattr *fattr) |
| 2326 | { |
| 2327 | int status; |
| 2328 | struct nfs4_lookup_arg args = { |
| 2329 | .bitmask = server->attr_bitmask, |
| 2330 | .dir_fh = dirfh, |
| 2331 | .name = name, |
| 2332 | }; |
| 2333 | struct nfs4_lookup_res res = { |
| 2334 | .server = server, |
| 2335 | .fattr = fattr, |
| 2336 | .fh = fhandle, |
| 2337 | }; |
| 2338 | struct rpc_message msg = { |
| 2339 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP], |
| 2340 | .rpc_argp = &args, |
| 2341 | .rpc_resp = &res, |
| 2342 | }; |
| 2343 | |
| 2344 | nfs_fattr_init(fattr); |
| 2345 | |
| 2346 | dprintk("NFS call lookupfh %s\n", name->name); |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 2347 | status = nfs4_call_sync(server, &msg, &args, &res, 0); |
David Howells | 2b3de44 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 2348 | dprintk("NFS reply lookupfh: %d\n", status); |
David Howells | 2b3de44 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 2349 | return status; |
| 2350 | } |
| 2351 | |
| 2352 | static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh, |
| 2353 | struct qstr *name, struct nfs_fh *fhandle, |
| 2354 | struct nfs_fattr *fattr) |
| 2355 | { |
| 2356 | struct nfs4_exception exception = { }; |
| 2357 | int err; |
| 2358 | do { |
Trond Myklebust | 4e56e08 | 2007-07-01 18:13:52 -0400 | [diff] [blame] | 2359 | err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr); |
| 2360 | /* FIXME: !!!! */ |
| 2361 | if (err == -NFS4ERR_MOVED) { |
| 2362 | err = -EREMOTE; |
| 2363 | break; |
| 2364 | } |
| 2365 | err = nfs4_handle_exception(server, err, &exception); |
David Howells | 2b3de44 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 2366 | } while (exception.retry); |
| 2367 | return err; |
| 2368 | } |
| 2369 | |
Trond Myklebust | 56659e9 | 2007-07-17 21:52:39 -0400 | [diff] [blame] | 2370 | static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
| 2372 | { |
Trond Myklebust | 4e56e08 | 2007-07-01 18:13:52 -0400 | [diff] [blame] | 2373 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | |
| 2375 | dprintk("NFS call lookup %s\n", name->name); |
Trond Myklebust | 4e56e08 | 2007-07-01 18:13:52 -0400 | [diff] [blame] | 2376 | status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr); |
Manoj Naik | 6b97fd3 | 2006-06-09 09:34:29 -0400 | [diff] [blame] | 2377 | if (status == -NFS4ERR_MOVED) |
| 2378 | status = nfs4_get_referral(dir, name, fattr, fhandle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | dprintk("NFS reply lookup: %d\n", status); |
| 2380 | return status; |
| 2381 | } |
| 2382 | |
| 2383 | static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
| 2384 | { |
| 2385 | struct nfs4_exception exception = { }; |
| 2386 | int err; |
| 2387 | do { |
| 2388 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 2389 | _nfs4_proc_lookup(dir, name, fhandle, fattr), |
| 2390 | &exception); |
| 2391 | } while (exception.retry); |
| 2392 | return err; |
| 2393 | } |
| 2394 | |
| 2395 | static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) |
| 2396 | { |
Trond Myklebust | 76b3299 | 2007-08-10 17:45:11 -0400 | [diff] [blame] | 2397 | struct nfs_server *server = NFS_SERVER(inode); |
| 2398 | struct nfs_fattr fattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | struct nfs4_accessargs args = { |
| 2400 | .fh = NFS_FH(inode), |
Trond Myklebust | 76b3299 | 2007-08-10 17:45:11 -0400 | [diff] [blame] | 2401 | .bitmask = server->attr_bitmask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | }; |
Trond Myklebust | 76b3299 | 2007-08-10 17:45:11 -0400 | [diff] [blame] | 2403 | struct nfs4_accessres res = { |
| 2404 | .server = server, |
| 2405 | .fattr = &fattr, |
| 2406 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | struct rpc_message msg = { |
| 2408 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS], |
| 2409 | .rpc_argp = &args, |
| 2410 | .rpc_resp = &res, |
| 2411 | .rpc_cred = entry->cred, |
| 2412 | }; |
| 2413 | int mode = entry->mask; |
| 2414 | int status; |
| 2415 | |
| 2416 | /* |
| 2417 | * Determine which access bits we want to ask for... |
| 2418 | */ |
| 2419 | if (mode & MAY_READ) |
| 2420 | args.access |= NFS4_ACCESS_READ; |
| 2421 | if (S_ISDIR(inode->i_mode)) { |
| 2422 | if (mode & MAY_WRITE) |
| 2423 | args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE; |
| 2424 | if (mode & MAY_EXEC) |
| 2425 | args.access |= NFS4_ACCESS_LOOKUP; |
| 2426 | } else { |
| 2427 | if (mode & MAY_WRITE) |
| 2428 | args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND; |
| 2429 | if (mode & MAY_EXEC) |
| 2430 | args.access |= NFS4_ACCESS_EXECUTE; |
| 2431 | } |
Trond Myklebust | 76b3299 | 2007-08-10 17:45:11 -0400 | [diff] [blame] | 2432 | nfs_fattr_init(&fattr); |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 2433 | status = nfs4_call_sync(server, &msg, &args, &res, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | if (!status) { |
| 2435 | entry->mask = 0; |
| 2436 | if (res.access & NFS4_ACCESS_READ) |
| 2437 | entry->mask |= MAY_READ; |
| 2438 | if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE)) |
| 2439 | entry->mask |= MAY_WRITE; |
| 2440 | if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE)) |
| 2441 | entry->mask |= MAY_EXEC; |
Trond Myklebust | 76b3299 | 2007-08-10 17:45:11 -0400 | [diff] [blame] | 2442 | nfs_refresh_inode(inode, &fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | } |
| 2444 | return status; |
| 2445 | } |
| 2446 | |
| 2447 | static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) |
| 2448 | { |
| 2449 | struct nfs4_exception exception = { }; |
| 2450 | int err; |
| 2451 | do { |
| 2452 | err = nfs4_handle_exception(NFS_SERVER(inode), |
| 2453 | _nfs4_proc_access(inode, entry), |
| 2454 | &exception); |
| 2455 | } while (exception.retry); |
| 2456 | return err; |
| 2457 | } |
| 2458 | |
| 2459 | /* |
| 2460 | * TODO: For the time being, we don't try to get any attributes |
| 2461 | * along with any of the zero-copy operations READ, READDIR, |
| 2462 | * READLINK, WRITE. |
| 2463 | * |
| 2464 | * In the case of the first three, we want to put the GETATTR |
| 2465 | * after the read-type operation -- this is because it is hard |
| 2466 | * to predict the length of a GETATTR response in v4, and thus |
| 2467 | * align the READ data correctly. This means that the GETATTR |
| 2468 | * may end up partially falling into the page cache, and we should |
| 2469 | * shift it into the 'tail' of the xdr_buf before processing. |
| 2470 | * To do this efficiently, we need to know the total length |
| 2471 | * of data received, which doesn't seem to be available outside |
| 2472 | * of the RPC layer. |
| 2473 | * |
| 2474 | * In the case of WRITE, we also want to put the GETATTR after |
| 2475 | * the operation -- in this case because we want to make sure |
| 2476 | * we get the post-operation mtime and size. This means that |
| 2477 | * we can't use xdr_encode_pages() as written: we need a variant |
| 2478 | * of it which would leave room in the 'tail' iovec. |
| 2479 | * |
| 2480 | * Both of these changes to the XDR layer would in fact be quite |
| 2481 | * minor, but I decided to leave them for a subsequent patch. |
| 2482 | */ |
| 2483 | static int _nfs4_proc_readlink(struct inode *inode, struct page *page, |
| 2484 | unsigned int pgbase, unsigned int pglen) |
| 2485 | { |
| 2486 | struct nfs4_readlink args = { |
| 2487 | .fh = NFS_FH(inode), |
| 2488 | .pgbase = pgbase, |
| 2489 | .pglen = pglen, |
| 2490 | .pages = &page, |
| 2491 | }; |
Benny Halevy | f50c700 | 2009-04-01 09:21:55 -0400 | [diff] [blame] | 2492 | struct nfs4_readlink_res res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | struct rpc_message msg = { |
| 2494 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK], |
| 2495 | .rpc_argp = &args, |
Benny Halevy | f50c700 | 2009-04-01 09:21:55 -0400 | [diff] [blame] | 2496 | .rpc_resp = &res, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | }; |
| 2498 | |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 2499 | return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | static int nfs4_proc_readlink(struct inode *inode, struct page *page, |
| 2503 | unsigned int pgbase, unsigned int pglen) |
| 2504 | { |
| 2505 | struct nfs4_exception exception = { }; |
| 2506 | int err; |
| 2507 | do { |
| 2508 | err = nfs4_handle_exception(NFS_SERVER(inode), |
| 2509 | _nfs4_proc_readlink(inode, page, pgbase, pglen), |
| 2510 | &exception); |
| 2511 | } while (exception.retry); |
| 2512 | return err; |
| 2513 | } |
| 2514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | /* |
| 2516 | * Got race? |
| 2517 | * We will need to arrange for the VFS layer to provide an atomic open. |
| 2518 | * Until then, this create/open method is prone to inefficiency and race |
| 2519 | * conditions due to the lookup, create, and open VFS calls from sys_open() |
| 2520 | * placed on the wire. |
| 2521 | * |
| 2522 | * Given the above sorry state of affairs, I'm simply sending an OPEN. |
| 2523 | * The file will be opened again in the subsequent VFS open call |
| 2524 | * (nfs4_proc_file_open). |
| 2525 | * |
| 2526 | * The open for read will just hang around to be used by any process that |
| 2527 | * opens the file O_RDONLY. This will all be resolved with the VFS changes. |
| 2528 | */ |
| 2529 | |
| 2530 | static int |
| 2531 | nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2532 | int flags, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | { |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 2534 | struct path path = { |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 2535 | .mnt = nd->path.mnt, |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 2536 | .dentry = dentry, |
| 2537 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | struct nfs4_state *state; |
| 2539 | struct rpc_cred *cred; |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 2540 | fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | int status = 0; |
| 2542 | |
Trond Myklebust | 98a8e32 | 2008-03-12 12:25:28 -0400 | [diff] [blame] | 2543 | cred = rpc_lookup_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | if (IS_ERR(cred)) { |
| 2545 | status = PTR_ERR(cred); |
| 2546 | goto out; |
| 2547 | } |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 2548 | state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred); |
Trond Myklebust | d4d9cdc | 2007-10-02 18:38:53 -0400 | [diff] [blame] | 2549 | d_drop(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | if (IS_ERR(state)) { |
| 2551 | status = PTR_ERR(state); |
Trond Myklebust | 659bfcd | 2008-06-10 19:39:41 -0400 | [diff] [blame] | 2552 | goto out_putcred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | } |
Trond Myklebust | d4d9cdc | 2007-10-02 18:38:53 -0400 | [diff] [blame] | 2554 | d_add(dentry, igrab(state->inode)); |
Trond Myklebust | d75340c | 2007-10-01 21:42:01 -0400 | [diff] [blame] | 2555 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | if (flags & O_EXCL) { |
| 2557 | struct nfs_fattr fattr; |
Trond Myklebust | 659bfcd | 2008-06-10 19:39:41 -0400 | [diff] [blame] | 2558 | status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2559 | if (status == 0) |
Trond Myklebust | 65e4308 | 2005-08-16 11:49:44 -0400 | [diff] [blame] | 2560 | nfs_setattr_update_inode(state->inode, sattr); |
Jeff Layton | aa53ed5 | 2007-06-05 14:49:03 -0400 | [diff] [blame] | 2561 | nfs_post_op_update_inode(state->inode, &fattr); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2562 | } |
Trond Myklebust | ad389da | 2007-06-05 12:30:00 -0400 | [diff] [blame] | 2563 | if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0) |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 2564 | status = nfs4_intent_set_file(nd, &path, state, fmode); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 2565 | else |
Trond Myklebust | dc0b027 | 2008-12-23 15:21:56 -0500 | [diff] [blame] | 2566 | nfs4_close_sync(&path, state, fmode); |
Trond Myklebust | 659bfcd | 2008-06-10 19:39:41 -0400 | [diff] [blame] | 2567 | out_putcred: |
| 2568 | put_rpccred(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | out: |
| 2570 | return status; |
| 2571 | } |
| 2572 | |
| 2573 | static int _nfs4_proc_remove(struct inode *dir, struct qstr *name) |
| 2574 | { |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2575 | struct nfs_server *server = NFS_SERVER(dir); |
Trond Myklebust | 4fdc17b | 2007-07-14 15:39:57 -0400 | [diff] [blame] | 2576 | struct nfs_removeargs args = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | .fh = NFS_FH(dir), |
Trond Myklebust | 4fdc17b | 2007-07-14 15:39:57 -0400 | [diff] [blame] | 2578 | .name.len = name->len, |
| 2579 | .name.name = name->name, |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2580 | .bitmask = server->attr_bitmask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | }; |
Trond Myklebust | 4fdc17b | 2007-07-14 15:39:57 -0400 | [diff] [blame] | 2582 | struct nfs_removeres res = { |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2583 | .server = server, |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2584 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | struct rpc_message msg = { |
Trond Myklebust | 4fdc17b | 2007-07-14 15:39:57 -0400 | [diff] [blame] | 2586 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE], |
| 2587 | .rpc_argp = &args, |
| 2588 | .rpc_resp = &res, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | }; |
| 2590 | int status; |
| 2591 | |
Trond Myklebust | 4fdc17b | 2007-07-14 15:39:57 -0400 | [diff] [blame] | 2592 | nfs_fattr_init(&res.dir_attr); |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 2593 | status = nfs4_call_sync(server, &msg, &args, &res, 1); |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2594 | if (status == 0) { |
| 2595 | update_changeattr(dir, &res.cinfo); |
Trond Myklebust | 4fdc17b | 2007-07-14 15:39:57 -0400 | [diff] [blame] | 2596 | nfs_post_op_update_inode(dir, &res.dir_attr); |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2597 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | return status; |
| 2599 | } |
| 2600 | |
| 2601 | static int nfs4_proc_remove(struct inode *dir, struct qstr *name) |
| 2602 | { |
| 2603 | struct nfs4_exception exception = { }; |
| 2604 | int err; |
| 2605 | do { |
| 2606 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 2607 | _nfs4_proc_remove(dir, name), |
| 2608 | &exception); |
| 2609 | } while (exception.retry); |
| 2610 | return err; |
| 2611 | } |
| 2612 | |
Trond Myklebust | e4eff1a | 2007-07-14 15:39:58 -0400 | [diff] [blame] | 2613 | static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | { |
Trond Myklebust | e4eff1a | 2007-07-14 15:39:58 -0400 | [diff] [blame] | 2615 | struct nfs_server *server = NFS_SERVER(dir); |
| 2616 | struct nfs_removeargs *args = msg->rpc_argp; |
| 2617 | struct nfs_removeres *res = msg->rpc_resp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | |
Trond Myklebust | a65318b | 2009-03-11 14:10:28 -0400 | [diff] [blame] | 2619 | args->bitmask = server->cache_consistency_bitmask; |
Trond Myklebust | e4eff1a | 2007-07-14 15:39:58 -0400 | [diff] [blame] | 2620 | res->server = server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | } |
| 2623 | |
Trond Myklebust | e4eff1a | 2007-07-14 15:39:58 -0400 | [diff] [blame] | 2624 | static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | { |
Trond Myklebust | e4eff1a | 2007-07-14 15:39:58 -0400 | [diff] [blame] | 2626 | struct nfs_removeres *res = task->tk_msg.rpc_resp; |
| 2627 | |
Andy Adamson | 472cfbd | 2009-04-01 09:22:24 -0400 | [diff] [blame] | 2628 | nfs4_sequence_done(res->server, &res->seq_res, task->tk_status); |
Trond Myklebust | 9e33bed | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 2629 | if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN) |
Trond Myklebust | e4eff1a | 2007-07-14 15:39:58 -0400 | [diff] [blame] | 2630 | return 0; |
| 2631 | update_changeattr(dir, &res->cinfo); |
| 2632 | nfs_post_op_update_inode(dir, &res->dir_attr); |
| 2633 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name, |
| 2637 | struct inode *new_dir, struct qstr *new_name) |
| 2638 | { |
Trond Myklebust | 6caf2c8 | 2005-10-27 22:12:43 -0400 | [diff] [blame] | 2639 | struct nfs_server *server = NFS_SERVER(old_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | struct nfs4_rename_arg arg = { |
| 2641 | .old_dir = NFS_FH(old_dir), |
| 2642 | .new_dir = NFS_FH(new_dir), |
| 2643 | .old_name = old_name, |
| 2644 | .new_name = new_name, |
Trond Myklebust | 6caf2c8 | 2005-10-27 22:12:43 -0400 | [diff] [blame] | 2645 | .bitmask = server->attr_bitmask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | }; |
Trond Myklebust | 6caf2c8 | 2005-10-27 22:12:43 -0400 | [diff] [blame] | 2647 | struct nfs_fattr old_fattr, new_fattr; |
| 2648 | struct nfs4_rename_res res = { |
| 2649 | .server = server, |
| 2650 | .old_fattr = &old_fattr, |
| 2651 | .new_fattr = &new_fattr, |
| 2652 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | struct rpc_message msg = { |
| 2654 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME], |
| 2655 | .rpc_argp = &arg, |
| 2656 | .rpc_resp = &res, |
| 2657 | }; |
| 2658 | int status; |
| 2659 | |
Trond Myklebust | 6caf2c8 | 2005-10-27 22:12:43 -0400 | [diff] [blame] | 2660 | nfs_fattr_init(res.old_fattr); |
| 2661 | nfs_fattr_init(res.new_fattr); |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 2662 | status = nfs4_call_sync(server, &msg, &arg, &res, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | |
| 2664 | if (!status) { |
| 2665 | update_changeattr(old_dir, &res.old_cinfo); |
Trond Myklebust | 6caf2c8 | 2005-10-27 22:12:43 -0400 | [diff] [blame] | 2666 | nfs_post_op_update_inode(old_dir, res.old_fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | update_changeattr(new_dir, &res.new_cinfo); |
Trond Myklebust | 6caf2c8 | 2005-10-27 22:12:43 -0400 | [diff] [blame] | 2668 | nfs_post_op_update_inode(new_dir, res.new_fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | } |
| 2670 | return status; |
| 2671 | } |
| 2672 | |
| 2673 | static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name, |
| 2674 | struct inode *new_dir, struct qstr *new_name) |
| 2675 | { |
| 2676 | struct nfs4_exception exception = { }; |
| 2677 | int err; |
| 2678 | do { |
| 2679 | err = nfs4_handle_exception(NFS_SERVER(old_dir), |
| 2680 | _nfs4_proc_rename(old_dir, old_name, |
| 2681 | new_dir, new_name), |
| 2682 | &exception); |
| 2683 | } while (exception.retry); |
| 2684 | return err; |
| 2685 | } |
| 2686 | |
| 2687 | static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) |
| 2688 | { |
Trond Myklebust | 91ba2ee | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 2689 | struct nfs_server *server = NFS_SERVER(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | struct nfs4_link_arg arg = { |
| 2691 | .fh = NFS_FH(inode), |
| 2692 | .dir_fh = NFS_FH(dir), |
| 2693 | .name = name, |
Trond Myklebust | 91ba2ee | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 2694 | .bitmask = server->attr_bitmask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | }; |
Trond Myklebust | 91ba2ee | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 2696 | struct nfs_fattr fattr, dir_attr; |
| 2697 | struct nfs4_link_res res = { |
| 2698 | .server = server, |
| 2699 | .fattr = &fattr, |
| 2700 | .dir_attr = &dir_attr, |
| 2701 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | struct rpc_message msg = { |
| 2703 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK], |
| 2704 | .rpc_argp = &arg, |
Trond Myklebust | 91ba2ee | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 2705 | .rpc_resp = &res, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | }; |
| 2707 | int status; |
| 2708 | |
Trond Myklebust | 91ba2ee | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 2709 | nfs_fattr_init(res.fattr); |
| 2710 | nfs_fattr_init(res.dir_attr); |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 2711 | status = nfs4_call_sync(server, &msg, &arg, &res, 1); |
Trond Myklebust | 91ba2ee | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 2712 | if (!status) { |
| 2713 | update_changeattr(dir, &res.cinfo); |
| 2714 | nfs_post_op_update_inode(dir, res.dir_attr); |
Trond Myklebust | 73a3d07 | 2006-05-25 01:40:47 -0400 | [diff] [blame] | 2715 | nfs_post_op_update_inode(inode, res.fattr); |
Trond Myklebust | 91ba2ee | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 2716 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | |
| 2718 | return status; |
| 2719 | } |
| 2720 | |
| 2721 | static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) |
| 2722 | { |
| 2723 | struct nfs4_exception exception = { }; |
| 2724 | int err; |
| 2725 | do { |
| 2726 | err = nfs4_handle_exception(NFS_SERVER(inode), |
| 2727 | _nfs4_proc_link(inode, dir, name), |
| 2728 | &exception); |
| 2729 | } while (exception.retry); |
| 2730 | return err; |
| 2731 | } |
| 2732 | |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2733 | struct nfs4_createdata { |
| 2734 | struct rpc_message msg; |
| 2735 | struct nfs4_create_arg arg; |
| 2736 | struct nfs4_create_res res; |
| 2737 | struct nfs_fh fh; |
| 2738 | struct nfs_fattr fattr; |
| 2739 | struct nfs_fattr dir_fattr; |
| 2740 | }; |
| 2741 | |
| 2742 | static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir, |
| 2743 | struct qstr *name, struct iattr *sattr, u32 ftype) |
| 2744 | { |
| 2745 | struct nfs4_createdata *data; |
| 2746 | |
| 2747 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 2748 | if (data != NULL) { |
| 2749 | struct nfs_server *server = NFS_SERVER(dir); |
| 2750 | |
| 2751 | data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE]; |
| 2752 | data->msg.rpc_argp = &data->arg; |
| 2753 | data->msg.rpc_resp = &data->res; |
| 2754 | data->arg.dir_fh = NFS_FH(dir); |
| 2755 | data->arg.server = server; |
| 2756 | data->arg.name = name; |
| 2757 | data->arg.attrs = sattr; |
| 2758 | data->arg.ftype = ftype; |
| 2759 | data->arg.bitmask = server->attr_bitmask; |
| 2760 | data->res.server = server; |
| 2761 | data->res.fh = &data->fh; |
| 2762 | data->res.fattr = &data->fattr; |
| 2763 | data->res.dir_fattr = &data->dir_fattr; |
| 2764 | nfs_fattr_init(data->res.fattr); |
| 2765 | nfs_fattr_init(data->res.dir_fattr); |
| 2766 | } |
| 2767 | return data; |
| 2768 | } |
| 2769 | |
| 2770 | static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data) |
| 2771 | { |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 2772 | int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg, |
| 2773 | &data->arg, &data->res, 1); |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2774 | if (status == 0) { |
| 2775 | update_changeattr(dir, &data->res.dir_cinfo); |
| 2776 | nfs_post_op_update_inode(dir, data->res.dir_fattr); |
| 2777 | status = nfs_instantiate(dentry, data->res.fh, data->res.fattr); |
| 2778 | } |
| 2779 | return status; |
| 2780 | } |
| 2781 | |
| 2782 | static void nfs4_free_createdata(struct nfs4_createdata *data) |
| 2783 | { |
| 2784 | kfree(data); |
| 2785 | } |
| 2786 | |
Chuck Lever | 4f390c1 | 2006-08-22 20:06:22 -0400 | [diff] [blame] | 2787 | static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, |
Chuck Lever | 94a6d75 | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 2788 | struct page *page, unsigned int len, struct iattr *sattr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | { |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2790 | struct nfs4_createdata *data; |
| 2791 | int status = -ENAMETOOLONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | |
Chuck Lever | 94a6d75 | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 2793 | if (len > NFS4_MAXPATHLEN) |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2794 | goto out; |
Chuck Lever | 4f390c1 | 2006-08-22 20:06:22 -0400 | [diff] [blame] | 2795 | |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2796 | status = -ENOMEM; |
| 2797 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK); |
| 2798 | if (data == NULL) |
| 2799 | goto out; |
| 2800 | |
| 2801 | data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK]; |
| 2802 | data->arg.u.symlink.pages = &page; |
| 2803 | data->arg.u.symlink.len = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2805 | status = nfs4_do_create(dir, dentry, data); |
| 2806 | |
| 2807 | nfs4_free_createdata(data); |
| 2808 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | return status; |
| 2810 | } |
| 2811 | |
Chuck Lever | 4f390c1 | 2006-08-22 20:06:22 -0400 | [diff] [blame] | 2812 | static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, |
Chuck Lever | 94a6d75 | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 2813 | struct page *page, unsigned int len, struct iattr *sattr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | { |
| 2815 | struct nfs4_exception exception = { }; |
| 2816 | int err; |
| 2817 | do { |
| 2818 | err = nfs4_handle_exception(NFS_SERVER(dir), |
Chuck Lever | 94a6d75 | 2006-08-22 20:06:23 -0400 | [diff] [blame] | 2819 | _nfs4_proc_symlink(dir, dentry, page, |
| 2820 | len, sattr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | &exception); |
| 2822 | } while (exception.retry); |
| 2823 | return err; |
| 2824 | } |
| 2825 | |
| 2826 | static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, |
| 2827 | struct iattr *sattr) |
| 2828 | { |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2829 | struct nfs4_createdata *data; |
| 2830 | int status = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2832 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR); |
| 2833 | if (data == NULL) |
| 2834 | goto out; |
| 2835 | |
| 2836 | status = nfs4_do_create(dir, dentry, data); |
| 2837 | |
| 2838 | nfs4_free_createdata(data); |
| 2839 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | return status; |
| 2841 | } |
| 2842 | |
| 2843 | static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, |
| 2844 | struct iattr *sattr) |
| 2845 | { |
| 2846 | struct nfs4_exception exception = { }; |
| 2847 | int err; |
| 2848 | do { |
| 2849 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 2850 | _nfs4_proc_mkdir(dir, dentry, sattr), |
| 2851 | &exception); |
| 2852 | } while (exception.retry); |
| 2853 | return err; |
| 2854 | } |
| 2855 | |
| 2856 | static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, |
| 2857 | u64 cookie, struct page *page, unsigned int count, int plus) |
| 2858 | { |
| 2859 | struct inode *dir = dentry->d_inode; |
| 2860 | struct nfs4_readdir_arg args = { |
| 2861 | .fh = NFS_FH(dir), |
| 2862 | .pages = &page, |
| 2863 | .pgbase = 0, |
| 2864 | .count = count, |
Trond Myklebust | 96d25e5 | 2009-11-11 16:15:42 +0900 | [diff] [blame] | 2865 | .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | }; |
| 2867 | struct nfs4_readdir_res res; |
| 2868 | struct rpc_message msg = { |
| 2869 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR], |
| 2870 | .rpc_argp = &args, |
| 2871 | .rpc_resp = &res, |
| 2872 | .rpc_cred = cred, |
| 2873 | }; |
| 2874 | int status; |
| 2875 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 2876 | dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__, |
Trond Myklebust | eadf459 | 2005-06-22 17:16:39 +0000 | [diff] [blame] | 2877 | dentry->d_parent->d_name.name, |
| 2878 | dentry->d_name.name, |
| 2879 | (unsigned long long)cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args); |
| 2881 | res.pgbase = args.pgbase; |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 2882 | status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | if (status == 0) |
| 2884 | memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE); |
Trond Myklebust | c481299 | 2007-09-28 17:11:45 -0400 | [diff] [blame] | 2885 | |
| 2886 | nfs_invalidate_atime(dir); |
| 2887 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 2888 | dprintk("%s: returns %d\n", __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | return status; |
| 2890 | } |
| 2891 | |
| 2892 | static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, |
| 2893 | u64 cookie, struct page *page, unsigned int count, int plus) |
| 2894 | { |
| 2895 | struct nfs4_exception exception = { }; |
| 2896 | int err; |
| 2897 | do { |
| 2898 | err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), |
| 2899 | _nfs4_proc_readdir(dentry, cred, cookie, |
| 2900 | page, count, plus), |
| 2901 | &exception); |
| 2902 | } while (exception.retry); |
| 2903 | return err; |
| 2904 | } |
| 2905 | |
| 2906 | static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, |
| 2907 | struct iattr *sattr, dev_t rdev) |
| 2908 | { |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2909 | struct nfs4_createdata *data; |
| 2910 | int mode = sattr->ia_mode; |
| 2911 | int status = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | |
| 2913 | BUG_ON(!(sattr->ia_valid & ATTR_MODE)); |
| 2914 | BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode)); |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2915 | |
| 2916 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK); |
| 2917 | if (data == NULL) |
| 2918 | goto out; |
| 2919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | if (S_ISFIFO(mode)) |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2921 | data->arg.ftype = NF4FIFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | else if (S_ISBLK(mode)) { |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2923 | data->arg.ftype = NF4BLK; |
| 2924 | data->arg.u.device.specdata1 = MAJOR(rdev); |
| 2925 | data->arg.u.device.specdata2 = MINOR(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | } |
| 2927 | else if (S_ISCHR(mode)) { |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2928 | data->arg.ftype = NF4CHR; |
| 2929 | data->arg.u.device.specdata1 = MAJOR(rdev); |
| 2930 | data->arg.u.device.specdata2 = MINOR(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | |
Trond Myklebust | 57dc9a5 | 2008-06-20 15:35:32 -0400 | [diff] [blame] | 2933 | status = nfs4_do_create(dir, dentry, data); |
| 2934 | |
| 2935 | nfs4_free_createdata(data); |
| 2936 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | return status; |
| 2938 | } |
| 2939 | |
| 2940 | static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, |
| 2941 | struct iattr *sattr, dev_t rdev) |
| 2942 | { |
| 2943 | struct nfs4_exception exception = { }; |
| 2944 | int err; |
| 2945 | do { |
| 2946 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 2947 | _nfs4_proc_mknod(dir, dentry, sattr, rdev), |
| 2948 | &exception); |
| 2949 | } while (exception.retry); |
| 2950 | return err; |
| 2951 | } |
| 2952 | |
| 2953 | static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2954 | struct nfs_fsstat *fsstat) |
| 2955 | { |
| 2956 | struct nfs4_statfs_arg args = { |
| 2957 | .fh = fhandle, |
| 2958 | .bitmask = server->attr_bitmask, |
| 2959 | }; |
Benny Halevy | 24ad148 | 2009-04-01 09:21:56 -0400 | [diff] [blame] | 2960 | struct nfs4_statfs_res res = { |
| 2961 | .fsstat = fsstat, |
| 2962 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | struct rpc_message msg = { |
| 2964 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS], |
| 2965 | .rpc_argp = &args, |
Benny Halevy | 24ad148 | 2009-04-01 09:21:56 -0400 | [diff] [blame] | 2966 | .rpc_resp = &res, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | }; |
| 2968 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 2969 | nfs_fattr_init(fsstat->fattr); |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 2970 | return nfs4_call_sync(server, &msg, &args, &res, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat) |
| 2974 | { |
| 2975 | struct nfs4_exception exception = { }; |
| 2976 | int err; |
| 2977 | do { |
| 2978 | err = nfs4_handle_exception(server, |
| 2979 | _nfs4_proc_statfs(server, fhandle, fsstat), |
| 2980 | &exception); |
| 2981 | } while (exception.retry); |
| 2982 | return err; |
| 2983 | } |
| 2984 | |
| 2985 | static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2986 | struct nfs_fsinfo *fsinfo) |
| 2987 | { |
| 2988 | struct nfs4_fsinfo_arg args = { |
| 2989 | .fh = fhandle, |
| 2990 | .bitmask = server->attr_bitmask, |
| 2991 | }; |
Benny Halevy | 3dda5e4 | 2009-04-01 09:21:57 -0400 | [diff] [blame] | 2992 | struct nfs4_fsinfo_res res = { |
| 2993 | .fsinfo = fsinfo, |
| 2994 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | struct rpc_message msg = { |
| 2996 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO], |
| 2997 | .rpc_argp = &args, |
Benny Halevy | 3dda5e4 | 2009-04-01 09:21:57 -0400 | [diff] [blame] | 2998 | .rpc_resp = &res, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | }; |
| 3000 | |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 3001 | return nfs4_call_sync(server, &msg, &args, &res, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) |
| 3005 | { |
| 3006 | struct nfs4_exception exception = { }; |
| 3007 | int err; |
| 3008 | |
| 3009 | do { |
| 3010 | err = nfs4_handle_exception(server, |
| 3011 | _nfs4_do_fsinfo(server, fhandle, fsinfo), |
| 3012 | &exception); |
| 3013 | } while (exception.retry); |
| 3014 | return err; |
| 3015 | } |
| 3016 | |
| 3017 | static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) |
| 3018 | { |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 3019 | nfs_fattr_init(fsinfo->fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | return nfs4_do_fsinfo(server, fhandle, fsinfo); |
| 3021 | } |
| 3022 | |
| 3023 | static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, |
| 3024 | struct nfs_pathconf *pathconf) |
| 3025 | { |
| 3026 | struct nfs4_pathconf_arg args = { |
| 3027 | .fh = fhandle, |
| 3028 | .bitmask = server->attr_bitmask, |
| 3029 | }; |
Benny Halevy | d45b298 | 2009-04-01 09:21:58 -0400 | [diff] [blame] | 3030 | struct nfs4_pathconf_res res = { |
| 3031 | .pathconf = pathconf, |
| 3032 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | struct rpc_message msg = { |
| 3034 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF], |
| 3035 | .rpc_argp = &args, |
Benny Halevy | d45b298 | 2009-04-01 09:21:58 -0400 | [diff] [blame] | 3036 | .rpc_resp = &res, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | }; |
| 3038 | |
| 3039 | /* None of the pathconf attributes are mandatory to implement */ |
| 3040 | if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) { |
| 3041 | memset(pathconf, 0, sizeof(*pathconf)); |
| 3042 | return 0; |
| 3043 | } |
| 3044 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 3045 | nfs_fattr_init(pathconf->fattr); |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 3046 | return nfs4_call_sync(server, &msg, &args, &res, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, |
| 3050 | struct nfs_pathconf *pathconf) |
| 3051 | { |
| 3052 | struct nfs4_exception exception = { }; |
| 3053 | int err; |
| 3054 | |
| 3055 | do { |
| 3056 | err = nfs4_handle_exception(server, |
| 3057 | _nfs4_proc_pathconf(server, fhandle, pathconf), |
| 3058 | &exception); |
| 3059 | } while (exception.retry); |
| 3060 | return err; |
| 3061 | } |
| 3062 | |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 3063 | static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | { |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 3065 | struct nfs_server *server = NFS_SERVER(data->inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | |
Andy Adamson | f11c88a | 2009-04-01 09:22:25 -0400 | [diff] [blame] | 3067 | dprintk("--> %s\n", __func__); |
| 3068 | |
Andy Adamson | f11c88a | 2009-04-01 09:22:25 -0400 | [diff] [blame] | 3069 | nfs4_sequence_done(server, &data->res.seq_res, task->tk_status); |
| 3070 | |
Trond Myklebust | 9e33bed | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 3071 | if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) { |
Trond Myklebust | 0110ee1 | 2009-12-07 09:00:24 -0500 | [diff] [blame] | 3072 | nfs_restart_rpc(task, server->nfs_client); |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 3073 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | } |
Trond Myklebust | 8850df9 | 2007-09-28 17:20:07 -0400 | [diff] [blame] | 3075 | |
| 3076 | nfs_invalidate_atime(data->inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | if (task->tk_status > 0) |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 3078 | renew_lease(server, data->timestamp); |
| 3079 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3080 | } |
| 3081 | |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 3082 | static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | data->timestamp = jiffies; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 3085 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | } |
| 3087 | |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 3088 | static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | struct inode *inode = data->inode; |
| 3091 | |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 3092 | nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res, |
| 3093 | task->tk_status); |
| 3094 | |
Trond Myklebust | 9e33bed | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 3095 | if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) { |
Trond Myklebust | 0110ee1 | 2009-12-07 09:00:24 -0500 | [diff] [blame] | 3096 | nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 3097 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | } |
Trond Myklebust | 4f9838c | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 3099 | if (task->tk_status >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | renew_lease(NFS_SERVER(inode), data->timestamp); |
Trond Myklebust | 70ca885 | 2007-09-30 15:21:24 -0400 | [diff] [blame] | 3101 | nfs_post_op_update_inode_force_wcc(inode, data->res.fattr); |
Trond Myklebust | 4f9838c | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 3102 | } |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 3103 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | } |
| 3105 | |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 3106 | static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | { |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 3108 | struct nfs_server *server = NFS_SERVER(data->inode); |
| 3109 | |
Trond Myklebust | a65318b | 2009-03-11 14:10:28 -0400 | [diff] [blame] | 3110 | data->args.bitmask = server->cache_consistency_bitmask; |
Trond Myklebust | 4f9838c | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 3111 | data->res.server = server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | data->timestamp = jiffies; |
| 3113 | |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 3114 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | } |
| 3116 | |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 3117 | static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | struct inode *inode = data->inode; |
| 3120 | |
Andy Adamson | 21d9a85 | 2009-04-01 09:22:27 -0400 | [diff] [blame] | 3121 | nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res, |
| 3122 | task->tk_status); |
Trond Myklebust | 9e33bed | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 3123 | if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) { |
Trond Myklebust | 0110ee1 | 2009-12-07 09:00:24 -0500 | [diff] [blame] | 3124 | nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 3125 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | } |
Trond Myklebust | 9e08a3c | 2007-10-08 14:10:31 -0400 | [diff] [blame] | 3127 | nfs_refresh_inode(inode, data->res.fattr); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 3128 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | } |
| 3130 | |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 3131 | static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | { |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 3133 | struct nfs_server *server = NFS_SERVER(data->inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | |
Trond Myklebust | a65318b | 2009-03-11 14:10:28 -0400 | [diff] [blame] | 3135 | data->args.bitmask = server->cache_consistency_bitmask; |
Trond Myklebust | 4f9838c | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 3136 | data->res.server = server; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 3137 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | /* |
| 3141 | * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special |
| 3142 | * standalone procedure for queueing an asynchronous RENEW. |
| 3143 | */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 3144 | static void nfs4_renew_done(struct rpc_task *task, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | { |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 3146 | struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp; |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 3147 | unsigned long timestamp = (unsigned long)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | |
| 3149 | if (task->tk_status < 0) { |
Trond Myklebust | 95baa25 | 2009-05-26 14:51:00 -0400 | [diff] [blame] | 3150 | /* Unless we're shutting down, schedule state recovery! */ |
| 3151 | if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0) |
| 3152 | nfs4_schedule_state_recovery(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | return; |
| 3154 | } |
| 3155 | spin_lock(&clp->cl_lock); |
| 3156 | if (time_before(clp->cl_last_renewal,timestamp)) |
| 3157 | clp->cl_last_renewal = timestamp; |
| 3158 | spin_unlock(&clp->cl_lock); |
| 3159 | } |
| 3160 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 3161 | static const struct rpc_call_ops nfs4_renew_ops = { |
| 3162 | .rpc_call_done = nfs4_renew_done, |
| 3163 | }; |
| 3164 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 3165 | int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | { |
| 3167 | struct rpc_message msg = { |
| 3168 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], |
| 3169 | .rpc_argp = clp, |
Trond Myklebust | b4454fe | 2006-01-03 09:55:25 +0100 | [diff] [blame] | 3170 | .rpc_cred = cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | }; |
| 3172 | |
| 3173 | return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT, |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 3174 | &nfs4_renew_ops, (void *)jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | } |
| 3176 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 3177 | int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | { |
| 3179 | struct rpc_message msg = { |
| 3180 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], |
| 3181 | .rpc_argp = clp, |
Trond Myklebust | b4454fe | 2006-01-03 09:55:25 +0100 | [diff] [blame] | 3182 | .rpc_cred = cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | }; |
| 3184 | unsigned long now = jiffies; |
| 3185 | int status; |
| 3186 | |
| 3187 | status = rpc_call_sync(clp->cl_rpcclient, &msg, 0); |
| 3188 | if (status < 0) |
| 3189 | return status; |
| 3190 | spin_lock(&clp->cl_lock); |
| 3191 | if (time_before(clp->cl_last_renewal,now)) |
| 3192 | clp->cl_last_renewal = now; |
| 3193 | spin_unlock(&clp->cl_lock); |
| 3194 | return 0; |
| 3195 | } |
| 3196 | |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3197 | static inline int nfs4_server_supports_acls(struct nfs_server *server) |
| 3198 | { |
| 3199 | return (server->caps & NFS_CAP_ACLS) |
| 3200 | && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL) |
| 3201 | && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL); |
| 3202 | } |
| 3203 | |
| 3204 | /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that |
| 3205 | * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on |
| 3206 | * the stack. |
| 3207 | */ |
| 3208 | #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT) |
| 3209 | |
| 3210 | static void buf_to_pages(const void *buf, size_t buflen, |
| 3211 | struct page **pages, unsigned int *pgbase) |
| 3212 | { |
| 3213 | const void *p = buf; |
| 3214 | |
| 3215 | *pgbase = offset_in_page(buf); |
| 3216 | p -= *pgbase; |
| 3217 | while (p < buf + buflen) { |
| 3218 | *(pages++) = virt_to_page(p); |
| 3219 | p += PAGE_CACHE_SIZE; |
| 3220 | } |
| 3221 | } |
| 3222 | |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3223 | struct nfs4_cached_acl { |
| 3224 | int cached; |
| 3225 | size_t len; |
Andrew Morton | 3e9d415 | 2005-06-22 17:16:28 +0000 | [diff] [blame] | 3226 | char data[0]; |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3227 | }; |
| 3228 | |
| 3229 | static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl) |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3230 | { |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3231 | struct nfs_inode *nfsi = NFS_I(inode); |
| 3232 | |
| 3233 | spin_lock(&inode->i_lock); |
| 3234 | kfree(nfsi->nfs4_acl); |
| 3235 | nfsi->nfs4_acl = acl; |
| 3236 | spin_unlock(&inode->i_lock); |
| 3237 | } |
| 3238 | |
| 3239 | static void nfs4_zap_acl_attr(struct inode *inode) |
| 3240 | { |
| 3241 | nfs4_set_cached_acl(inode, NULL); |
| 3242 | } |
| 3243 | |
| 3244 | static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen) |
| 3245 | { |
| 3246 | struct nfs_inode *nfsi = NFS_I(inode); |
| 3247 | struct nfs4_cached_acl *acl; |
| 3248 | int ret = -ENOENT; |
| 3249 | |
| 3250 | spin_lock(&inode->i_lock); |
| 3251 | acl = nfsi->nfs4_acl; |
| 3252 | if (acl == NULL) |
| 3253 | goto out; |
| 3254 | if (buf == NULL) /* user is just asking for length */ |
| 3255 | goto out_len; |
| 3256 | if (acl->cached == 0) |
| 3257 | goto out; |
| 3258 | ret = -ERANGE; /* see getxattr(2) man page */ |
| 3259 | if (acl->len > buflen) |
| 3260 | goto out; |
| 3261 | memcpy(buf, acl->data, acl->len); |
| 3262 | out_len: |
| 3263 | ret = acl->len; |
| 3264 | out: |
| 3265 | spin_unlock(&inode->i_lock); |
| 3266 | return ret; |
| 3267 | } |
| 3268 | |
| 3269 | static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len) |
| 3270 | { |
| 3271 | struct nfs4_cached_acl *acl; |
| 3272 | |
| 3273 | if (buf && acl_len <= PAGE_SIZE) { |
| 3274 | acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL); |
| 3275 | if (acl == NULL) |
| 3276 | goto out; |
| 3277 | acl->cached = 1; |
| 3278 | memcpy(acl->data, buf, acl_len); |
| 3279 | } else { |
| 3280 | acl = kmalloc(sizeof(*acl), GFP_KERNEL); |
| 3281 | if (acl == NULL) |
| 3282 | goto out; |
| 3283 | acl->cached = 0; |
| 3284 | } |
| 3285 | acl->len = acl_len; |
| 3286 | out: |
| 3287 | nfs4_set_cached_acl(inode, acl); |
| 3288 | } |
| 3289 | |
Trond Myklebust | 16b4289 | 2006-08-24 12:27:15 -0400 | [diff] [blame] | 3290 | static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3291 | { |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3292 | struct page *pages[NFS4ACL_MAXPAGES]; |
| 3293 | struct nfs_getaclargs args = { |
| 3294 | .fh = NFS_FH(inode), |
| 3295 | .acl_pages = pages, |
| 3296 | .acl_len = buflen, |
| 3297 | }; |
Benny Halevy | 663c79b | 2009-04-01 09:21:59 -0400 | [diff] [blame] | 3298 | struct nfs_getaclres res = { |
| 3299 | .acl_len = buflen, |
| 3300 | }; |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3301 | void *resp_buf; |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3302 | struct rpc_message msg = { |
| 3303 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL], |
| 3304 | .rpc_argp = &args, |
Benny Halevy | 663c79b | 2009-04-01 09:21:59 -0400 | [diff] [blame] | 3305 | .rpc_resp = &res, |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3306 | }; |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3307 | struct page *localpage = NULL; |
| 3308 | int ret; |
| 3309 | |
| 3310 | if (buflen < PAGE_SIZE) { |
| 3311 | /* As long as we're doing a round trip to the server anyway, |
| 3312 | * let's be prepared for a page of acl data. */ |
| 3313 | localpage = alloc_page(GFP_KERNEL); |
| 3314 | resp_buf = page_address(localpage); |
| 3315 | if (localpage == NULL) |
| 3316 | return -ENOMEM; |
| 3317 | args.acl_pages[0] = localpage; |
| 3318 | args.acl_pgbase = 0; |
Benny Halevy | 663c79b | 2009-04-01 09:21:59 -0400 | [diff] [blame] | 3319 | args.acl_len = PAGE_SIZE; |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3320 | } else { |
| 3321 | resp_buf = buf; |
| 3322 | buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase); |
| 3323 | } |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 3324 | ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0); |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3325 | if (ret) |
| 3326 | goto out_free; |
Benny Halevy | 663c79b | 2009-04-01 09:21:59 -0400 | [diff] [blame] | 3327 | if (res.acl_len > args.acl_len) |
| 3328 | nfs4_write_cached_acl(inode, NULL, res.acl_len); |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3329 | else |
Benny Halevy | 663c79b | 2009-04-01 09:21:59 -0400 | [diff] [blame] | 3330 | nfs4_write_cached_acl(inode, resp_buf, res.acl_len); |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3331 | if (buf) { |
| 3332 | ret = -ERANGE; |
Benny Halevy | 663c79b | 2009-04-01 09:21:59 -0400 | [diff] [blame] | 3333 | if (res.acl_len > buflen) |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3334 | goto out_free; |
| 3335 | if (localpage) |
Benny Halevy | 663c79b | 2009-04-01 09:21:59 -0400 | [diff] [blame] | 3336 | memcpy(buf, resp_buf, res.acl_len); |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3337 | } |
Benny Halevy | 663c79b | 2009-04-01 09:21:59 -0400 | [diff] [blame] | 3338 | ret = res.acl_len; |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3339 | out_free: |
| 3340 | if (localpage) |
| 3341 | __free_page(localpage); |
| 3342 | return ret; |
| 3343 | } |
| 3344 | |
Trond Myklebust | 16b4289 | 2006-08-24 12:27:15 -0400 | [diff] [blame] | 3345 | static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) |
| 3346 | { |
| 3347 | struct nfs4_exception exception = { }; |
| 3348 | ssize_t ret; |
| 3349 | do { |
| 3350 | ret = __nfs4_get_acl_uncached(inode, buf, buflen); |
| 3351 | if (ret >= 0) |
| 3352 | break; |
| 3353 | ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception); |
| 3354 | } while (exception.retry); |
| 3355 | return ret; |
| 3356 | } |
| 3357 | |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3358 | static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen) |
| 3359 | { |
| 3360 | struct nfs_server *server = NFS_SERVER(inode); |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3361 | int ret; |
| 3362 | |
| 3363 | if (!nfs4_server_supports_acls(server)) |
| 3364 | return -EOPNOTSUPP; |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3365 | ret = nfs_revalidate_inode(server, inode); |
| 3366 | if (ret < 0) |
| 3367 | return ret; |
| 3368 | ret = nfs4_read_cached_acl(inode, buf, buflen); |
| 3369 | if (ret != -ENOENT) |
| 3370 | return ret; |
| 3371 | return nfs4_get_acl_uncached(inode, buf, buflen); |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Trond Myklebust | 16b4289 | 2006-08-24 12:27:15 -0400 | [diff] [blame] | 3374 | static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) |
J. Bruce Fields | 4b580ee | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3375 | { |
| 3376 | struct nfs_server *server = NFS_SERVER(inode); |
| 3377 | struct page *pages[NFS4ACL_MAXPAGES]; |
| 3378 | struct nfs_setaclargs arg = { |
| 3379 | .fh = NFS_FH(inode), |
| 3380 | .acl_pages = pages, |
| 3381 | .acl_len = buflen, |
| 3382 | }; |
Benny Halevy | 73c403a | 2009-04-01 09:22:01 -0400 | [diff] [blame] | 3383 | struct nfs_setaclres res; |
J. Bruce Fields | 4b580ee | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3384 | struct rpc_message msg = { |
| 3385 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL], |
| 3386 | .rpc_argp = &arg, |
Benny Halevy | 73c403a | 2009-04-01 09:22:01 -0400 | [diff] [blame] | 3387 | .rpc_resp = &res, |
J. Bruce Fields | 4b580ee | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3388 | }; |
| 3389 | int ret; |
| 3390 | |
| 3391 | if (!nfs4_server_supports_acls(server)) |
| 3392 | return -EOPNOTSUPP; |
Trond Myklebust | 642ac54 | 2005-10-18 14:20:19 -0700 | [diff] [blame] | 3393 | nfs_inode_return_delegation(inode); |
J. Bruce Fields | 4b580ee | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3394 | buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase); |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 3395 | ret = nfs4_call_sync(server, &msg, &arg, &res, 1); |
Trond Myklebust | f41f741 | 2008-06-11 17:39:04 -0400 | [diff] [blame] | 3396 | nfs_access_zap_cache(inode); |
| 3397 | nfs_zap_acl_cache(inode); |
J. Bruce Fields | 4b580ee | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3398 | return ret; |
| 3399 | } |
| 3400 | |
Trond Myklebust | 16b4289 | 2006-08-24 12:27:15 -0400 | [diff] [blame] | 3401 | static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) |
| 3402 | { |
| 3403 | struct nfs4_exception exception = { }; |
| 3404 | int err; |
| 3405 | do { |
| 3406 | err = nfs4_handle_exception(NFS_SERVER(inode), |
| 3407 | __nfs4_proc_set_acl(inode, buf, buflen), |
| 3408 | &exception); |
| 3409 | } while (exception.retry); |
| 3410 | return err; |
| 3411 | } |
| 3412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | static int |
Andy Adamson | 8328d59 | 2009-04-01 09:22:35 -0400 | [diff] [blame] | 3414 | _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | if (!clp || task->tk_status >= 0) |
| 3417 | return 0; |
| 3418 | switch(task->tk_status) { |
Trond Myklebust | 9e33bed | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 3419 | case -NFS4ERR_ADMIN_REVOKED: |
| 3420 | case -NFS4ERR_BAD_STATEID: |
| 3421 | case -NFS4ERR_OPENMODE: |
| 3422 | if (state == NULL) |
| 3423 | break; |
| 3424 | nfs4_state_mark_reclaim_nograce(clp, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | case -NFS4ERR_STALE_CLIENTID: |
| 3426 | case -NFS4ERR_STALE_STATEID: |
| 3427 | case -NFS4ERR_EXPIRED: |
Trond Myklebust | 5d00837 | 2008-02-22 16:34:17 -0500 | [diff] [blame] | 3428 | rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | nfs4_schedule_state_recovery(clp); |
Trond Myklebust | e005e80 | 2008-12-23 15:21:48 -0500 | [diff] [blame] | 3430 | if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0) |
Trond Myklebust | fda1393 | 2008-02-22 16:34:12 -0500 | [diff] [blame] | 3431 | rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3432 | task->tk_status = 0; |
| 3433 | return -EAGAIN; |
Andy Adamson | 4745e31 | 2009-04-01 09:22:42 -0400 | [diff] [blame] | 3434 | #if defined(CONFIG_NFS_V4_1) |
| 3435 | case -NFS4ERR_BADSESSION: |
| 3436 | case -NFS4ERR_BADSLOT: |
| 3437 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 3438 | case -NFS4ERR_DEADSESSION: |
| 3439 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
| 3440 | case -NFS4ERR_SEQ_FALSE_RETRY: |
| 3441 | case -NFS4ERR_SEQ_MISORDERED: |
| 3442 | dprintk("%s ERROR %d, Reset session\n", __func__, |
| 3443 | task->tk_status); |
Andy Adamson | 0b9e2d4 | 2009-12-04 16:02:14 -0500 | [diff] [blame] | 3444 | nfs4_schedule_state_recovery(clp); |
Andy Adamson | 4745e31 | 2009-04-01 09:22:42 -0400 | [diff] [blame] | 3445 | task->tk_status = 0; |
| 3446 | return -EAGAIN; |
| 3447 | #endif /* CONFIG_NFS_V4_1 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | case -NFS4ERR_DELAY: |
Andy Adamson | 8328d59 | 2009-04-01 09:22:35 -0400 | [diff] [blame] | 3449 | if (server) |
| 3450 | nfs_inc_server_stats(server, NFSIOS_DELAY); |
Chuck Lever | 006ea73 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 3451 | case -NFS4ERR_GRACE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
| 3453 | task->tk_status = 0; |
| 3454 | return -EAGAIN; |
| 3455 | case -NFS4ERR_OLD_STATEID: |
| 3456 | task->tk_status = 0; |
| 3457 | return -EAGAIN; |
| 3458 | } |
| 3459 | task->tk_status = nfs4_map_errors(task->tk_status); |
| 3460 | return 0; |
| 3461 | } |
| 3462 | |
Andy Adamson | 8328d59 | 2009-04-01 09:22:35 -0400 | [diff] [blame] | 3463 | static int |
| 3464 | nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state) |
| 3465 | { |
| 3466 | return _nfs4_async_handle_error(task, server, server->nfs_client, state); |
| 3467 | } |
| 3468 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 3469 | int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | { |
| 3471 | nfs4_verifier sc_verifier; |
| 3472 | struct nfs4_setclientid setclientid = { |
| 3473 | .sc_verifier = &sc_verifier, |
| 3474 | .sc_prog = program, |
| 3475 | }; |
| 3476 | struct rpc_message msg = { |
| 3477 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID], |
| 3478 | .rpc_argp = &setclientid, |
| 3479 | .rpc_resp = clp, |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 3480 | .rpc_cred = cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | }; |
Al Viro | bc4785c | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 3482 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | int loop = 0; |
| 3484 | int status; |
| 3485 | |
Al Viro | bc4785c | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 3486 | p = (__be32*)sc_verifier.data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | *p++ = htonl((u32)clp->cl_boot_time.tv_sec); |
| 3488 | *p = htonl((u32)clp->cl_boot_time.tv_nsec); |
| 3489 | |
| 3490 | for(;;) { |
| 3491 | setclientid.sc_name_len = scnprintf(setclientid.sc_name, |
Trond Myklebust | 69dd716 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 3492 | sizeof(setclientid.sc_name), "%s/%s %s %s %u", |
Chuck Lever | d4d3c50 | 2007-12-10 14:57:09 -0500 | [diff] [blame] | 3493 | clp->cl_ipaddr, |
| 3494 | rpc_peeraddr2str(clp->cl_rpcclient, |
| 3495 | RPC_DISPLAY_ADDR), |
Trond Myklebust | 69dd716 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 3496 | rpc_peeraddr2str(clp->cl_rpcclient, |
| 3497 | RPC_DISPLAY_PROTO), |
Trond Myklebust | 78ea323 | 2008-04-07 20:49:28 -0400 | [diff] [blame] | 3498 | clp->cl_rpcclient->cl_auth->au_ops->au_name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | clp->cl_id_uniquifier); |
| 3500 | setclientid.sc_netid_len = scnprintf(setclientid.sc_netid, |
Chuck Lever | d4d3c50 | 2007-12-10 14:57:09 -0500 | [diff] [blame] | 3501 | sizeof(setclientid.sc_netid), |
| 3502 | rpc_peeraddr2str(clp->cl_rpcclient, |
| 3503 | RPC_DISPLAY_NETID)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr, |
Chuck Lever | d4d3c50 | 2007-12-10 14:57:09 -0500 | [diff] [blame] | 3505 | sizeof(setclientid.sc_uaddr), "%s.%u.%u", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | clp->cl_ipaddr, port >> 8, port & 255); |
| 3507 | |
| 3508 | status = rpc_call_sync(clp->cl_rpcclient, &msg, 0); |
| 3509 | if (status != -NFS4ERR_CLID_INUSE) |
| 3510 | break; |
| 3511 | if (signalled()) |
| 3512 | break; |
| 3513 | if (loop++ & 1) |
| 3514 | ssleep(clp->cl_lease_time + 1); |
| 3515 | else |
| 3516 | if (++clp->cl_id_uniquifier == 0) |
| 3517 | break; |
| 3518 | } |
| 3519 | return status; |
| 3520 | } |
| 3521 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 3522 | static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | { |
| 3524 | struct nfs_fsinfo fsinfo; |
| 3525 | struct rpc_message msg = { |
| 3526 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM], |
| 3527 | .rpc_argp = clp, |
| 3528 | .rpc_resp = &fsinfo, |
Trond Myklebust | 286d7d6 | 2006-01-03 09:55:26 +0100 | [diff] [blame] | 3529 | .rpc_cred = cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | }; |
| 3531 | unsigned long now; |
| 3532 | int status; |
| 3533 | |
| 3534 | now = jiffies; |
| 3535 | status = rpc_call_sync(clp->cl_rpcclient, &msg, 0); |
| 3536 | if (status == 0) { |
| 3537 | spin_lock(&clp->cl_lock); |
| 3538 | clp->cl_lease_time = fsinfo.lease_time * HZ; |
| 3539 | clp->cl_last_renewal = now; |
| 3540 | spin_unlock(&clp->cl_lock); |
| 3541 | } |
| 3542 | return status; |
| 3543 | } |
| 3544 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 3545 | int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred) |
Trond Myklebust | 51581f3 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 3546 | { |
Benny Halevy | 77e0367 | 2008-06-24 20:25:57 +0300 | [diff] [blame] | 3547 | long timeout = 0; |
Trond Myklebust | 51581f3 | 2006-03-20 13:44:47 -0500 | [diff] [blame] | 3548 | int err; |
| 3549 | do { |
| 3550 | err = _nfs4_proc_setclientid_confirm(clp, cred); |
| 3551 | switch (err) { |
| 3552 | case 0: |
| 3553 | return err; |
| 3554 | case -NFS4ERR_RESOURCE: |
| 3555 | /* The IBM lawyers misread another document! */ |
| 3556 | case -NFS4ERR_DELAY: |
| 3557 | err = nfs4_delay(clp->cl_rpcclient, &timeout); |
| 3558 | } |
| 3559 | } while (err == 0); |
| 3560 | return err; |
| 3561 | } |
| 3562 | |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3563 | struct nfs4_delegreturndata { |
| 3564 | struct nfs4_delegreturnargs args; |
Trond Myklebust | fa178f2 | 2006-01-03 09:55:38 +0100 | [diff] [blame] | 3565 | struct nfs4_delegreturnres res; |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3566 | struct nfs_fh fh; |
| 3567 | nfs4_stateid stateid; |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 3568 | unsigned long timestamp; |
Trond Myklebust | fa178f2 | 2006-01-03 09:55:38 +0100 | [diff] [blame] | 3569 | struct nfs_fattr fattr; |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3570 | int rpc_status; |
| 3571 | }; |
| 3572 | |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3573 | static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata) |
| 3574 | { |
| 3575 | struct nfs4_delegreturndata *data = calldata; |
Andy Adamson | 938e101 | 2009-04-01 09:22:28 -0400 | [diff] [blame] | 3576 | |
Trond Myklebust | d61e612 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 3577 | nfs4_sequence_done(data->res.server, &data->res.seq_res, |
| 3578 | task->tk_status); |
Andy Adamson | 938e101 | 2009-04-01 09:22:28 -0400 | [diff] [blame] | 3579 | |
Ricardo Labiaga | 7970886 | 2009-12-07 09:23:21 -0500 | [diff] [blame] | 3580 | switch (task->tk_status) { |
| 3581 | case -NFS4ERR_STALE_STATEID: |
| 3582 | case -NFS4ERR_EXPIRED: |
| 3583 | case 0: |
Trond Myklebust | fa178f2 | 2006-01-03 09:55:38 +0100 | [diff] [blame] | 3584 | renew_lease(data->res.server, data->timestamp); |
Ricardo Labiaga | 7970886 | 2009-12-07 09:23:21 -0500 | [diff] [blame] | 3585 | break; |
| 3586 | default: |
| 3587 | if (nfs4_async_handle_error(task, data->res.server, NULL) == |
| 3588 | -EAGAIN) { |
| 3589 | nfs_restart_rpc(task, data->res.server->nfs_client); |
| 3590 | return; |
| 3591 | } |
| 3592 | } |
| 3593 | data->rpc_status = task->tk_status; |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | static void nfs4_delegreturn_release(void *calldata) |
| 3597 | { |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3598 | kfree(calldata); |
| 3599 | } |
| 3600 | |
Andy Adamson | 938e101 | 2009-04-01 09:22:28 -0400 | [diff] [blame] | 3601 | #if defined(CONFIG_NFS_V4_1) |
| 3602 | static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data) |
| 3603 | { |
| 3604 | struct nfs4_delegreturndata *d_data; |
| 3605 | |
| 3606 | d_data = (struct nfs4_delegreturndata *)data; |
| 3607 | |
| 3608 | if (nfs4_setup_sequence(d_data->res.server->nfs_client, |
| 3609 | &d_data->args.seq_args, |
| 3610 | &d_data->res.seq_res, 1, task)) |
| 3611 | return; |
| 3612 | rpc_call_start(task); |
| 3613 | } |
| 3614 | #endif /* CONFIG_NFS_V4_1 */ |
| 3615 | |
Jesper Juhl | c8d149f | 2006-03-20 13:44:07 -0500 | [diff] [blame] | 3616 | static const struct rpc_call_ops nfs4_delegreturn_ops = { |
Andy Adamson | 938e101 | 2009-04-01 09:22:28 -0400 | [diff] [blame] | 3617 | #if defined(CONFIG_NFS_V4_1) |
| 3618 | .rpc_call_prepare = nfs4_delegreturn_prepare, |
| 3619 | #endif /* CONFIG_NFS_V4_1 */ |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3620 | .rpc_call_done = nfs4_delegreturn_done, |
| 3621 | .rpc_release = nfs4_delegreturn_release, |
| 3622 | }; |
| 3623 | |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 3624 | static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync) |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3625 | { |
| 3626 | struct nfs4_delegreturndata *data; |
Trond Myklebust | fa178f2 | 2006-01-03 09:55:38 +0100 | [diff] [blame] | 3627 | struct nfs_server *server = NFS_SERVER(inode); |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3628 | struct rpc_task *task; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 3629 | struct rpc_message msg = { |
| 3630 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN], |
| 3631 | .rpc_cred = cred, |
| 3632 | }; |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 3633 | struct rpc_task_setup task_setup_data = { |
| 3634 | .rpc_client = server->client, |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 3635 | .rpc_message = &msg, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 3636 | .callback_ops = &nfs4_delegreturn_ops, |
| 3637 | .flags = RPC_TASK_ASYNC, |
| 3638 | }; |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 3639 | int status = 0; |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3640 | |
Andy Adamson | 938e101 | 2009-04-01 09:22:28 -0400 | [diff] [blame] | 3641 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3642 | if (data == NULL) |
| 3643 | return -ENOMEM; |
| 3644 | data->args.fhandle = &data->fh; |
| 3645 | data->args.stateid = &data->stateid; |
Trond Myklebust | fa178f2 | 2006-01-03 09:55:38 +0100 | [diff] [blame] | 3646 | data->args.bitmask = server->attr_bitmask; |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3647 | nfs_copy_fh(&data->fh, NFS_FH(inode)); |
| 3648 | memcpy(&data->stateid, stateid, sizeof(data->stateid)); |
Trond Myklebust | fa178f2 | 2006-01-03 09:55:38 +0100 | [diff] [blame] | 3649 | data->res.fattr = &data->fattr; |
| 3650 | data->res.server = server; |
Andy Adamson | 5f7dbd5 | 2009-04-01 09:22:05 -0400 | [diff] [blame] | 3651 | data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 3652 | nfs_fattr_init(data->res.fattr); |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 3653 | data->timestamp = jiffies; |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3654 | data->rpc_status = 0; |
| 3655 | |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 3656 | task_setup_data.callback_data = data; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 3657 | msg.rpc_argp = &data->args, |
| 3658 | msg.rpc_resp = &data->res, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 3659 | task = rpc_run_task(&task_setup_data); |
Trond Myklebust | 7a1218a | 2006-03-20 18:11:10 -0500 | [diff] [blame] | 3660 | if (IS_ERR(task)) |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3661 | return PTR_ERR(task); |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 3662 | if (!issync) |
| 3663 | goto out; |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3664 | status = nfs4_wait_for_completion_rpc_task(task); |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 3665 | if (status != 0) |
| 3666 | goto out; |
| 3667 | status = data->rpc_status; |
| 3668 | if (status != 0) |
| 3669 | goto out; |
| 3670 | nfs_refresh_inode(inode, &data->fattr); |
| 3671 | out: |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 3672 | rpc_put_task(task); |
Trond Myklebust | fe65040 | 2006-01-03 09:55:18 +0100 | [diff] [blame] | 3673 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3674 | } |
| 3675 | |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 3676 | int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | { |
| 3678 | struct nfs_server *server = NFS_SERVER(inode); |
| 3679 | struct nfs4_exception exception = { }; |
| 3680 | int err; |
| 3681 | do { |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 3682 | err = _nfs4_proc_delegreturn(inode, cred, stateid, issync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | switch (err) { |
| 3684 | case -NFS4ERR_STALE_STATEID: |
| 3685 | case -NFS4ERR_EXPIRED: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | case 0: |
| 3687 | return 0; |
| 3688 | } |
| 3689 | err = nfs4_handle_exception(server, err, &exception); |
| 3690 | } while (exception.retry); |
| 3691 | return err; |
| 3692 | } |
| 3693 | |
| 3694 | #define NFS4_LOCK_MINTIMEOUT (1 * HZ) |
| 3695 | #define NFS4_LOCK_MAXTIMEOUT (30 * HZ) |
| 3696 | |
| 3697 | /* |
| 3698 | * sleep, with exponential backoff, and retry the LOCK operation. |
| 3699 | */ |
| 3700 | static unsigned long |
| 3701 | nfs4_set_lock_task_retry(unsigned long timeout) |
| 3702 | { |
Matthew Wilcox | 150030b | 2007-12-06 16:24:39 -0500 | [diff] [blame] | 3703 | schedule_timeout_killable(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | timeout <<= 1; |
| 3705 | if (timeout > NFS4_LOCK_MAXTIMEOUT) |
| 3706 | return NFS4_LOCK_MAXTIMEOUT; |
| 3707 | return timeout; |
| 3708 | } |
| 3709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 3711 | { |
| 3712 | struct inode *inode = state->inode; |
| 3713 | struct nfs_server *server = NFS_SERVER(inode); |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 3714 | struct nfs_client *clp = server->nfs_client; |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3715 | struct nfs_lockt_args arg = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | .fh = NFS_FH(inode), |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3717 | .fl = request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3718 | }; |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3719 | struct nfs_lockt_res res = { |
| 3720 | .denied = request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | }; |
| 3722 | struct rpc_message msg = { |
| 3723 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT], |
| 3724 | .rpc_argp = &arg, |
| 3725 | .rpc_resp = &res, |
| 3726 | .rpc_cred = state->owner->so_cred, |
| 3727 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | struct nfs4_lock_state *lsp; |
| 3729 | int status; |
| 3730 | |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3731 | arg.lock_owner.clientid = clp->cl_clientid; |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 3732 | status = nfs4_set_lock_state(state, request); |
| 3733 | if (status != 0) |
| 3734 | goto out; |
| 3735 | lsp = request->fl_u.nfs4_fl.owner; |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 3736 | arg.lock_owner.id = lsp->ls_id.id; |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 3737 | status = nfs4_call_sync(server, &msg, &arg, &res, 1); |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3738 | switch (status) { |
| 3739 | case 0: |
| 3740 | request->fl_type = F_UNLCK; |
| 3741 | break; |
| 3742 | case -NFS4ERR_DENIED: |
| 3743 | status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3744 | } |
J. Bruce Fields | 70cc648 | 2007-02-22 18:48:53 -0500 | [diff] [blame] | 3745 | request->fl_ops->fl_release_private(request); |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 3746 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3747 | return status; |
| 3748 | } |
| 3749 | |
| 3750 | static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 3751 | { |
| 3752 | struct nfs4_exception exception = { }; |
| 3753 | int err; |
| 3754 | |
| 3755 | do { |
| 3756 | err = nfs4_handle_exception(NFS_SERVER(state->inode), |
| 3757 | _nfs4_proc_getlk(state, cmd, request), |
| 3758 | &exception); |
| 3759 | } while (exception.retry); |
| 3760 | return err; |
| 3761 | } |
| 3762 | |
| 3763 | static int do_vfs_lock(struct file *file, struct file_lock *fl) |
| 3764 | { |
| 3765 | int res = 0; |
| 3766 | switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { |
| 3767 | case FL_POSIX: |
| 3768 | res = posix_lock_file_wait(file, fl); |
| 3769 | break; |
| 3770 | case FL_FLOCK: |
| 3771 | res = flock_lock_file_wait(file, fl); |
| 3772 | break; |
| 3773 | default: |
| 3774 | BUG(); |
| 3775 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | return res; |
| 3777 | } |
| 3778 | |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3779 | struct nfs4_unlockdata { |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3780 | struct nfs_locku_args arg; |
| 3781 | struct nfs_locku_res res; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3782 | struct nfs4_lock_state *lsp; |
| 3783 | struct nfs_open_context *ctx; |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3784 | struct file_lock fl; |
| 3785 | const struct nfs_server *server; |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 3786 | unsigned long timestamp; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3787 | }; |
| 3788 | |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3789 | static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl, |
| 3790 | struct nfs_open_context *ctx, |
| 3791 | struct nfs4_lock_state *lsp, |
| 3792 | struct nfs_seqid *seqid) |
| 3793 | { |
| 3794 | struct nfs4_unlockdata *p; |
| 3795 | struct inode *inode = lsp->ls_state->inode; |
| 3796 | |
Andy Adamson | a893693 | 2009-04-01 09:22:23 -0400 | [diff] [blame] | 3797 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3798 | if (p == NULL) |
| 3799 | return NULL; |
| 3800 | p->arg.fh = NFS_FH(inode); |
| 3801 | p->arg.fl = &p->fl; |
| 3802 | p->arg.seqid = seqid; |
Trond Myklebust | c1d5193 | 2008-04-07 13:20:54 -0400 | [diff] [blame] | 3803 | p->res.seqid = seqid; |
Andy Adamson | 5f7dbd5 | 2009-04-01 09:22:05 -0400 | [diff] [blame] | 3804 | p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3805 | p->arg.stateid = &lsp->ls_stateid; |
| 3806 | p->lsp = lsp; |
| 3807 | atomic_inc(&lsp->ls_count); |
| 3808 | /* Ensure we don't close file until we're done freeing locks! */ |
| 3809 | p->ctx = get_nfs_open_context(ctx); |
| 3810 | memcpy(&p->fl, fl, sizeof(p->fl)); |
| 3811 | p->server = NFS_SERVER(inode); |
| 3812 | return p; |
| 3813 | } |
| 3814 | |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame] | 3815 | static void nfs4_locku_release_calldata(void *data) |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3816 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 3817 | struct nfs4_unlockdata *calldata = data; |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3818 | nfs_free_seqid(calldata->arg.seqid); |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame] | 3819 | nfs4_put_lock_state(calldata->lsp); |
| 3820 | put_nfs_open_context(calldata->ctx); |
| 3821 | kfree(calldata); |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3822 | } |
| 3823 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 3824 | static void nfs4_locku_done(struct rpc_task *task, void *data) |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3825 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 3826 | struct nfs4_unlockdata *calldata = data; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3827 | |
Andy Adamson | a893693 | 2009-04-01 09:22:23 -0400 | [diff] [blame] | 3828 | nfs4_sequence_done(calldata->server, &calldata->res.seq_res, |
| 3829 | task->tk_status); |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame] | 3830 | if (RPC_ASSASSINATED(task)) |
| 3831 | return; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3832 | switch (task->tk_status) { |
| 3833 | case 0: |
| 3834 | memcpy(calldata->lsp->ls_stateid.data, |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3835 | calldata->res.stateid.data, |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3836 | sizeof(calldata->lsp->ls_stateid.data)); |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 3837 | renew_lease(calldata->server, calldata->timestamp); |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3838 | break; |
Trond Myklebust | 9e33bed | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 3839 | case -NFS4ERR_BAD_STATEID: |
| 3840 | case -NFS4ERR_OLD_STATEID: |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3841 | case -NFS4ERR_STALE_STATEID: |
| 3842 | case -NFS4ERR_EXPIRED: |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3843 | break; |
| 3844 | default: |
Trond Myklebust | 9e33bed | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 3845 | if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN) |
Trond Myklebust | 0110ee1 | 2009-12-07 09:00:24 -0500 | [diff] [blame] | 3846 | nfs_restart_rpc(task, |
Trond Myklebust | d61e612 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 3847 | calldata->server->nfs_client); |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3848 | } |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3849 | } |
| 3850 | |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 3851 | static void nfs4_locku_prepare(struct rpc_task *task, void *data) |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3852 | { |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 3853 | struct nfs4_unlockdata *calldata = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3854 | |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3855 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3856 | return; |
| 3857 | if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 3858 | /* Note: exit _without_ running nfs4_locku_done */ |
| 3859 | task->tk_action = NULL; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3860 | return; |
| 3861 | } |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 3862 | calldata->timestamp = jiffies; |
Andy Adamson | a893693 | 2009-04-01 09:22:23 -0400 | [diff] [blame] | 3863 | if (nfs4_setup_sequence(calldata->server->nfs_client, |
| 3864 | &calldata->arg.seq_args, |
| 3865 | &calldata->res.seq_res, 1, task)) |
| 3866 | return; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 3867 | rpc_call_start(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3868 | } |
| 3869 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 3870 | static const struct rpc_call_ops nfs4_locku_ops = { |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 3871 | .rpc_call_prepare = nfs4_locku_prepare, |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 3872 | .rpc_call_done = nfs4_locku_done, |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame] | 3873 | .rpc_release = nfs4_locku_release_calldata, |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 3874 | }; |
| 3875 | |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3876 | static struct rpc_task *nfs4_do_unlck(struct file_lock *fl, |
| 3877 | struct nfs_open_context *ctx, |
| 3878 | struct nfs4_lock_state *lsp, |
| 3879 | struct nfs_seqid *seqid) |
| 3880 | { |
| 3881 | struct nfs4_unlockdata *data; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 3882 | struct rpc_message msg = { |
| 3883 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU], |
| 3884 | .rpc_cred = ctx->cred, |
| 3885 | }; |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 3886 | struct rpc_task_setup task_setup_data = { |
| 3887 | .rpc_client = NFS_CLIENT(lsp->ls_state->inode), |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 3888 | .rpc_message = &msg, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 3889 | .callback_ops = &nfs4_locku_ops, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 3890 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 3891 | .flags = RPC_TASK_ASYNC, |
| 3892 | }; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3893 | |
Frank Filz | 137d6ac | 2007-07-09 15:32:29 -0700 | [diff] [blame] | 3894 | /* Ensure this is an unlock - when canceling a lock, the |
| 3895 | * canceled lock is passed in, and it won't be an unlock. |
| 3896 | */ |
| 3897 | fl->fl_type = F_UNLCK; |
| 3898 | |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3899 | data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid); |
| 3900 | if (data == NULL) { |
| 3901 | nfs_free_seqid(seqid); |
| 3902 | return ERR_PTR(-ENOMEM); |
| 3903 | } |
| 3904 | |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 3905 | msg.rpc_argp = &data->arg, |
| 3906 | msg.rpc_resp = &data->res, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 3907 | task_setup_data.callback_data = data; |
| 3908 | return rpc_run_task(&task_setup_data); |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3909 | } |
| 3910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 3912 | { |
Trond Myklebust | 19e03c5 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 3913 | struct nfs_inode *nfsi = NFS_I(state->inode); |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3914 | struct nfs_seqid *seqid; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3915 | struct nfs4_lock_state *lsp; |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame] | 3916 | struct rpc_task *task; |
| 3917 | int status = 0; |
Trond Myklebust | 536ff0f | 2008-04-04 15:08:02 -0400 | [diff] [blame] | 3918 | unsigned char fl_flags = request->fl_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3919 | |
Trond Myklebust | 9b07357 | 2006-06-29 16:38:34 -0400 | [diff] [blame] | 3920 | status = nfs4_set_lock_state(state, request); |
| 3921 | /* Unlock _before_ we do the RPC call */ |
| 3922 | request->fl_flags |= FL_EXISTS; |
Trond Myklebust | 19e03c5 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 3923 | down_read(&nfsi->rwsem); |
| 3924 | if (do_vfs_lock(request->fl_file, request) == -ENOENT) { |
| 3925 | up_read(&nfsi->rwsem); |
Trond Myklebust | 9b07357 | 2006-06-29 16:38:34 -0400 | [diff] [blame] | 3926 | goto out; |
Trond Myklebust | 19e03c5 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 3927 | } |
| 3928 | up_read(&nfsi->rwsem); |
Trond Myklebust | 9b07357 | 2006-06-29 16:38:34 -0400 | [diff] [blame] | 3929 | if (status != 0) |
| 3930 | goto out; |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 3931 | /* Is this a delegated lock? */ |
| 3932 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) |
Trond Myklebust | 9b07357 | 2006-06-29 16:38:34 -0400 | [diff] [blame] | 3933 | goto out; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3934 | lsp = request->fl_u.nfs4_fl.owner; |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3935 | seqid = nfs_alloc_seqid(&lsp->ls_seqid); |
Trond Myklebust | 9b07357 | 2006-06-29 16:38:34 -0400 | [diff] [blame] | 3936 | status = -ENOMEM; |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3937 | if (seqid == NULL) |
Trond Myklebust | 9b07357 | 2006-06-29 16:38:34 -0400 | [diff] [blame] | 3938 | goto out; |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 3939 | task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid); |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3940 | status = PTR_ERR(task); |
| 3941 | if (IS_ERR(task)) |
Trond Myklebust | 9b07357 | 2006-06-29 16:38:34 -0400 | [diff] [blame] | 3942 | goto out; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3943 | status = nfs4_wait_for_completion_rpc_task(task); |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 3944 | rpc_put_task(task); |
Trond Myklebust | 9b07357 | 2006-06-29 16:38:34 -0400 | [diff] [blame] | 3945 | out: |
Trond Myklebust | 536ff0f | 2008-04-04 15:08:02 -0400 | [diff] [blame] | 3946 | request->fl_flags = fl_flags; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3947 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | } |
| 3949 | |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3950 | struct nfs4_lockdata { |
| 3951 | struct nfs_lock_args arg; |
Trond Myklebust | 911d1aa | 2006-01-03 09:55:16 +0100 | [diff] [blame] | 3952 | struct nfs_lock_res res; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3953 | struct nfs4_lock_state *lsp; |
| 3954 | struct nfs_open_context *ctx; |
| 3955 | struct file_lock fl; |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 3956 | unsigned long timestamp; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3957 | int rpc_status; |
| 3958 | int cancelled; |
Andy Adamson | 66179ef | 2009-04-01 09:22:22 -0400 | [diff] [blame] | 3959 | struct nfs_server *server; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3960 | }; |
| 3961 | |
| 3962 | static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl, |
| 3963 | struct nfs_open_context *ctx, struct nfs4_lock_state *lsp) |
| 3964 | { |
| 3965 | struct nfs4_lockdata *p; |
| 3966 | struct inode *inode = lsp->ls_state->inode; |
| 3967 | struct nfs_server *server = NFS_SERVER(inode); |
| 3968 | |
| 3969 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 3970 | if (p == NULL) |
| 3971 | return NULL; |
| 3972 | |
| 3973 | p->arg.fh = NFS_FH(inode); |
| 3974 | p->arg.fl = &p->fl; |
Trond Myklebust | 2f74c0a | 2008-01-08 17:56:07 -0500 | [diff] [blame] | 3975 | p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid); |
| 3976 | if (p->arg.open_seqid == NULL) |
| 3977 | goto out_free; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3978 | p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid); |
| 3979 | if (p->arg.lock_seqid == NULL) |
Trond Myklebust | 2f74c0a | 2008-01-08 17:56:07 -0500 | [diff] [blame] | 3980 | goto out_free_seqid; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3981 | p->arg.lock_stateid = &lsp->ls_stateid; |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 3982 | p->arg.lock_owner.clientid = server->nfs_client->cl_clientid; |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 3983 | p->arg.lock_owner.id = lsp->ls_id.id; |
Trond Myklebust | c1d5193 | 2008-04-07 13:20:54 -0400 | [diff] [blame] | 3984 | p->res.lock_seqid = p->arg.lock_seqid; |
Andy Adamson | 5f7dbd5 | 2009-04-01 09:22:05 -0400 | [diff] [blame] | 3985 | p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3986 | p->lsp = lsp; |
Andy Adamson | 66179ef | 2009-04-01 09:22:22 -0400 | [diff] [blame] | 3987 | p->server = server; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3988 | atomic_inc(&lsp->ls_count); |
| 3989 | p->ctx = get_nfs_open_context(ctx); |
| 3990 | memcpy(&p->fl, fl, sizeof(p->fl)); |
| 3991 | return p; |
Trond Myklebust | 2f74c0a | 2008-01-08 17:56:07 -0500 | [diff] [blame] | 3992 | out_free_seqid: |
| 3993 | nfs_free_seqid(p->arg.open_seqid); |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 3994 | out_free: |
| 3995 | kfree(p); |
| 3996 | return NULL; |
| 3997 | } |
| 3998 | |
| 3999 | static void nfs4_lock_prepare(struct rpc_task *task, void *calldata) |
| 4000 | { |
| 4001 | struct nfs4_lockdata *data = calldata; |
| 4002 | struct nfs4_state *state = data->lsp->ls_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 4004 | dprintk("%s: begin!\n", __func__); |
Trond Myklebust | 2f74c0a | 2008-01-08 17:56:07 -0500 | [diff] [blame] | 4005 | if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0) |
| 4006 | return; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4007 | /* Do we need to do an open_to_lock_owner? */ |
| 4008 | if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) { |
Trond Myklebust | e6e2197 | 2008-01-02 16:27:16 -0500 | [diff] [blame] | 4009 | if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) |
| 4010 | return; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4011 | data->arg.open_stateid = &state->stateid; |
| 4012 | data->arg.new_lock_owner = 1; |
Trond Myklebust | c1d5193 | 2008-04-07 13:20:54 -0400 | [diff] [blame] | 4013 | data->res.open_seqid = data->arg.open_seqid; |
Trond Myklebust | 2f74c0a | 2008-01-08 17:56:07 -0500 | [diff] [blame] | 4014 | } else |
| 4015 | data->arg.new_lock_owner = 0; |
Trond Myklebust | 26e976a | 2006-01-03 09:55:21 +0100 | [diff] [blame] | 4016 | data->timestamp = jiffies; |
Andy Adamson | 66179ef | 2009-04-01 09:22:22 -0400 | [diff] [blame] | 4017 | if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args, |
| 4018 | &data->res.seq_res, 1, task)) |
| 4019 | return; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 4020 | rpc_call_start(task); |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 4021 | dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status); |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4022 | } |
| 4023 | |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 4024 | static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata) |
| 4025 | { |
| 4026 | rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED); |
| 4027 | nfs4_lock_prepare(task, calldata); |
| 4028 | } |
| 4029 | |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4030 | static void nfs4_lock_done(struct rpc_task *task, void *calldata) |
| 4031 | { |
| 4032 | struct nfs4_lockdata *data = calldata; |
| 4033 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 4034 | dprintk("%s: begin!\n", __func__); |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4035 | |
Trond Myklebust | d61e612 | 2009-12-05 19:32:19 -0500 | [diff] [blame] | 4036 | nfs4_sequence_done(data->server, &data->res.seq_res, |
| 4037 | task->tk_status); |
Andy Adamson | 66179ef | 2009-04-01 09:22:22 -0400 | [diff] [blame] | 4038 | |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4039 | data->rpc_status = task->tk_status; |
| 4040 | if (RPC_ASSASSINATED(task)) |
| 4041 | goto out; |
| 4042 | if (data->arg.new_lock_owner != 0) { |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4043 | if (data->rpc_status == 0) |
| 4044 | nfs_confirm_seqid(&data->lsp->ls_seqid, 0); |
| 4045 | else |
| 4046 | goto out; |
| 4047 | } |
| 4048 | if (data->rpc_status == 0) { |
| 4049 | memcpy(data->lsp->ls_stateid.data, data->res.stateid.data, |
| 4050 | sizeof(data->lsp->ls_stateid.data)); |
| 4051 | data->lsp->ls_flags |= NFS_LOCK_INITIALIZED; |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 4052 | renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp); |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4053 | } |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4054 | out: |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 4055 | dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status); |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | static void nfs4_lock_release(void *calldata) |
| 4059 | { |
| 4060 | struct nfs4_lockdata *data = calldata; |
| 4061 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 4062 | dprintk("%s: begin!\n", __func__); |
Trond Myklebust | 2f74c0a | 2008-01-08 17:56:07 -0500 | [diff] [blame] | 4063 | nfs_free_seqid(data->arg.open_seqid); |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4064 | if (data->cancelled != 0) { |
| 4065 | struct rpc_task *task; |
| 4066 | task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp, |
| 4067 | data->arg.lock_seqid); |
| 4068 | if (!IS_ERR(task)) |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 4069 | rpc_put_task(task); |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 4070 | dprintk("%s: cancelling lock!\n", __func__); |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4071 | } else |
| 4072 | nfs_free_seqid(data->arg.lock_seqid); |
| 4073 | nfs4_put_lock_state(data->lsp); |
| 4074 | put_nfs_open_context(data->ctx); |
| 4075 | kfree(data); |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 4076 | dprintk("%s: done!\n", __func__); |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | static const struct rpc_call_ops nfs4_lock_ops = { |
| 4080 | .rpc_call_prepare = nfs4_lock_prepare, |
| 4081 | .rpc_call_done = nfs4_lock_done, |
| 4082 | .rpc_release = nfs4_lock_release, |
| 4083 | }; |
| 4084 | |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 4085 | static const struct rpc_call_ops nfs4_recover_lock_ops = { |
| 4086 | .rpc_call_prepare = nfs4_recover_lock_prepare, |
| 4087 | .rpc_call_done = nfs4_lock_done, |
| 4088 | .rpc_release = nfs4_lock_release, |
| 4089 | }; |
| 4090 | |
Alexandros Batsakis | afe6c27 | 2009-12-09 01:50:14 -0800 | [diff] [blame] | 4091 | static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type) |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4092 | { |
| 4093 | struct nfs4_lockdata *data; |
| 4094 | struct rpc_task *task; |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 4095 | struct rpc_message msg = { |
| 4096 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK], |
| 4097 | .rpc_cred = state->owner->so_cred, |
| 4098 | }; |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 4099 | struct rpc_task_setup task_setup_data = { |
| 4100 | .rpc_client = NFS_CLIENT(state->inode), |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 4101 | .rpc_message = &msg, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 4102 | .callback_ops = &nfs4_lock_ops, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 4103 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 4104 | .flags = RPC_TASK_ASYNC, |
| 4105 | }; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4106 | int ret; |
| 4107 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 4108 | dprintk("%s: begin!\n", __func__); |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 4109 | data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file), |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4110 | fl->fl_u.nfs4_fl.owner); |
| 4111 | if (data == NULL) |
| 4112 | return -ENOMEM; |
| 4113 | if (IS_SETLKW(cmd)) |
| 4114 | data->arg.block = 1; |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 4115 | if (recovery_type > NFS_LOCK_NEW) { |
| 4116 | if (recovery_type == NFS_LOCK_RECLAIM) |
Alexandros Batsakis | afe6c27 | 2009-12-09 01:50:14 -0800 | [diff] [blame] | 4117 | data->arg.reclaim = NFS_LOCK_RECLAIM; |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 4118 | task_setup_data.callback_ops = &nfs4_recover_lock_ops; |
| 4119 | } |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 4120 | msg.rpc_argp = &data->arg, |
| 4121 | msg.rpc_resp = &data->res, |
Trond Myklebust | c970aa8 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 4122 | task_setup_data.callback_data = data; |
| 4123 | task = rpc_run_task(&task_setup_data); |
Trond Myklebust | 7a1218a | 2006-03-20 18:11:10 -0500 | [diff] [blame] | 4124 | if (IS_ERR(task)) |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4125 | return PTR_ERR(task); |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4126 | ret = nfs4_wait_for_completion_rpc_task(task); |
| 4127 | if (ret == 0) { |
| 4128 | ret = data->rpc_status; |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4129 | } else |
| 4130 | data->cancelled = 1; |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 4131 | rpc_put_task(task); |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 4132 | dprintk("%s: done, ret = %d!\n", __func__, ret); |
Trond Myklebust | a5d16a4 | 2006-01-03 09:55:17 +0100 | [diff] [blame] | 4133 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request) |
| 4137 | { |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 4138 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 4139 | struct nfs4_exception exception = { }; |
| 4140 | int err; |
| 4141 | |
| 4142 | do { |
Trond Myklebust | 42a2d13 | 2006-06-29 16:38:36 -0400 | [diff] [blame] | 4143 | /* Cache the lock if possible... */ |
| 4144 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) |
| 4145 | return 0; |
Alexandros Batsakis | afe6c27 | 2009-12-09 01:50:14 -0800 | [diff] [blame] | 4146 | err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM); |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 4147 | if (err != -NFS4ERR_DELAY) |
| 4148 | break; |
| 4149 | nfs4_handle_exception(server, err, &exception); |
| 4150 | } while (exception.retry); |
| 4151 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request) |
| 4155 | { |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 4156 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 4157 | struct nfs4_exception exception = { }; |
| 4158 | int err; |
| 4159 | |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 4160 | err = nfs4_set_lock_state(state, request); |
| 4161 | if (err != 0) |
| 4162 | return err; |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 4163 | do { |
Trond Myklebust | 42a2d13 | 2006-06-29 16:38:36 -0400 | [diff] [blame] | 4164 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) |
| 4165 | return 0; |
Alexandros Batsakis | afe6c27 | 2009-12-09 01:50:14 -0800 | [diff] [blame] | 4166 | err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED); |
Trond Myklebust | a9ed2e2 | 2009-12-03 15:53:21 -0500 | [diff] [blame] | 4167 | switch (err) { |
| 4168 | default: |
| 4169 | goto out; |
| 4170 | case -NFS4ERR_GRACE: |
| 4171 | case -NFS4ERR_DELAY: |
| 4172 | nfs4_handle_exception(server, err, &exception); |
| 4173 | err = 0; |
| 4174 | } |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 4175 | } while (exception.retry); |
Trond Myklebust | a9ed2e2 | 2009-12-03 15:53:21 -0500 | [diff] [blame] | 4176 | out: |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 4177 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 4181 | { |
Trond Myklebust | 19e03c5 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 4182 | struct nfs_inode *nfsi = NFS_I(state->inode); |
Trond Myklebust | 01c3b86 | 2006-06-29 16:38:39 -0400 | [diff] [blame] | 4183 | unsigned char fl_flags = request->fl_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | int status; |
| 4185 | |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 4186 | /* Is this a delegated open? */ |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 4187 | status = nfs4_set_lock_state(state, request); |
| 4188 | if (status != 0) |
| 4189 | goto out; |
Trond Myklebust | 01c3b86 | 2006-06-29 16:38:39 -0400 | [diff] [blame] | 4190 | request->fl_flags |= FL_ACCESS; |
| 4191 | status = do_vfs_lock(request->fl_file, request); |
| 4192 | if (status < 0) |
| 4193 | goto out; |
Trond Myklebust | 19e03c5 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 4194 | down_read(&nfsi->rwsem); |
Trond Myklebust | 01c3b86 | 2006-06-29 16:38:39 -0400 | [diff] [blame] | 4195 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { |
Trond Myklebust | 01c3b86 | 2006-06-29 16:38:39 -0400 | [diff] [blame] | 4196 | /* Yes: cache locks! */ |
Trond Myklebust | 01c3b86 | 2006-06-29 16:38:39 -0400 | [diff] [blame] | 4197 | /* ...but avoid races with delegation recall... */ |
Trond Myklebust | 19e03c5 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 4198 | request->fl_flags = fl_flags & ~FL_SLEEP; |
| 4199 | status = do_vfs_lock(request->fl_file, request); |
| 4200 | goto out_unlock; |
Trond Myklebust | 01c3b86 | 2006-06-29 16:38:39 -0400 | [diff] [blame] | 4201 | } |
Alexandros Batsakis | afe6c27 | 2009-12-09 01:50:14 -0800 | [diff] [blame] | 4202 | status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW); |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 4203 | if (status != 0) |
Trond Myklebust | 01c3b86 | 2006-06-29 16:38:39 -0400 | [diff] [blame] | 4204 | goto out_unlock; |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 4205 | /* Note: we always want to sleep here! */ |
Trond Myklebust | 01c3b86 | 2006-06-29 16:38:39 -0400 | [diff] [blame] | 4206 | request->fl_flags = fl_flags | FL_SLEEP; |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 4207 | if (do_vfs_lock(request->fl_file, request) < 0) |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 4208 | printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__); |
Trond Myklebust | 01c3b86 | 2006-06-29 16:38:39 -0400 | [diff] [blame] | 4209 | out_unlock: |
Trond Myklebust | 19e03c5 | 2008-12-23 15:21:44 -0500 | [diff] [blame] | 4210 | up_read(&nfsi->rwsem); |
Trond Myklebust | 01c3b86 | 2006-06-29 16:38:39 -0400 | [diff] [blame] | 4211 | out: |
| 4212 | request->fl_flags = fl_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | return status; |
| 4214 | } |
| 4215 | |
| 4216 | static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 4217 | { |
| 4218 | struct nfs4_exception exception = { }; |
| 4219 | int err; |
| 4220 | |
| 4221 | do { |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 4222 | err = _nfs4_proc_setlk(state, cmd, request); |
| 4223 | if (err == -NFS4ERR_DENIED) |
| 4224 | err = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | err = nfs4_handle_exception(NFS_SERVER(state->inode), |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 4226 | err, &exception); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | } while (exception.retry); |
| 4228 | return err; |
| 4229 | } |
| 4230 | |
| 4231 | static int |
| 4232 | nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request) |
| 4233 | { |
| 4234 | struct nfs_open_context *ctx; |
| 4235 | struct nfs4_state *state; |
| 4236 | unsigned long timeout = NFS4_LOCK_MINTIMEOUT; |
| 4237 | int status; |
| 4238 | |
| 4239 | /* verify open state */ |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 4240 | ctx = nfs_file_open_context(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | state = ctx->state; |
| 4242 | |
| 4243 | if (request->fl_start < 0 || request->fl_end < 0) |
| 4244 | return -EINVAL; |
| 4245 | |
Trond Myklebust | d953126 | 2009-07-21 19:22:38 -0400 | [diff] [blame] | 4246 | if (IS_GETLK(cmd)) { |
| 4247 | if (state != NULL) |
| 4248 | return nfs4_proc_getlk(state, F_GETLK, request); |
| 4249 | return 0; |
| 4250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | |
| 4252 | if (!(IS_SETLK(cmd) || IS_SETLKW(cmd))) |
| 4253 | return -EINVAL; |
| 4254 | |
Trond Myklebust | d953126 | 2009-07-21 19:22:38 -0400 | [diff] [blame] | 4255 | if (request->fl_type == F_UNLCK) { |
| 4256 | if (state != NULL) |
| 4257 | return nfs4_proc_unlck(state, cmd, request); |
| 4258 | return 0; |
| 4259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | |
Trond Myklebust | d953126 | 2009-07-21 19:22:38 -0400 | [diff] [blame] | 4261 | if (state == NULL) |
| 4262 | return -ENOLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | do { |
| 4264 | status = nfs4_proc_setlk(state, cmd, request); |
| 4265 | if ((status != -EAGAIN) || IS_SETLK(cmd)) |
| 4266 | break; |
| 4267 | timeout = nfs4_set_lock_task_retry(timeout); |
| 4268 | status = -ERESTARTSYS; |
| 4269 | if (signalled()) |
| 4270 | break; |
| 4271 | } while(status < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | return status; |
| 4273 | } |
| 4274 | |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 4275 | int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl) |
| 4276 | { |
| 4277 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 4278 | struct nfs4_exception exception = { }; |
| 4279 | int err; |
| 4280 | |
| 4281 | err = nfs4_set_lock_state(state, fl); |
| 4282 | if (err != 0) |
| 4283 | goto out; |
| 4284 | do { |
Alexandros Batsakis | afe6c27 | 2009-12-09 01:50:14 -0800 | [diff] [blame] | 4285 | err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW); |
Trond Myklebust | d512220 | 2009-06-17 13:22:58 -0700 | [diff] [blame] | 4286 | switch (err) { |
| 4287 | default: |
| 4288 | printk(KERN_ERR "%s: unhandled error %d.\n", |
| 4289 | __func__, err); |
| 4290 | case 0: |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 4291 | case -ESTALE: |
Trond Myklebust | d512220 | 2009-06-17 13:22:58 -0700 | [diff] [blame] | 4292 | goto out; |
| 4293 | case -NFS4ERR_EXPIRED: |
| 4294 | case -NFS4ERR_STALE_CLIENTID: |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 4295 | case -NFS4ERR_STALE_STATEID: |
Ricardo Labiaga | 74e7bb7 | 2009-12-07 09:48:30 -0500 | [diff] [blame] | 4296 | case -NFS4ERR_BADSESSION: |
| 4297 | case -NFS4ERR_BADSLOT: |
| 4298 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 4299 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
| 4300 | case -NFS4ERR_DEADSESSION: |
Trond Myklebust | d512220 | 2009-06-17 13:22:58 -0700 | [diff] [blame] | 4301 | nfs4_schedule_state_recovery(server->nfs_client); |
| 4302 | goto out; |
Trond Myklebust | 965b5d6 | 2009-06-17 13:22:59 -0700 | [diff] [blame] | 4303 | case -ERESTARTSYS: |
| 4304 | /* |
| 4305 | * The show must go on: exit, but mark the |
| 4306 | * stateid as needing recovery. |
| 4307 | */ |
| 4308 | case -NFS4ERR_ADMIN_REVOKED: |
| 4309 | case -NFS4ERR_BAD_STATEID: |
| 4310 | case -NFS4ERR_OPENMODE: |
| 4311 | nfs4_state_mark_reclaim_nograce(server->nfs_client, state); |
| 4312 | err = 0; |
| 4313 | goto out; |
| 4314 | case -ENOMEM: |
| 4315 | case -NFS4ERR_DENIED: |
| 4316 | /* kill_proc(fl->fl_pid, SIGLOST, 1); */ |
| 4317 | err = 0; |
| 4318 | goto out; |
Trond Myklebust | d512220 | 2009-06-17 13:22:58 -0700 | [diff] [blame] | 4319 | case -NFS4ERR_DELAY: |
| 4320 | break; |
| 4321 | } |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 4322 | err = nfs4_handle_exception(server, err, &exception); |
| 4323 | } while (exception.retry); |
| 4324 | out: |
| 4325 | return err; |
| 4326 | } |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 4327 | |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 4328 | #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl" |
| 4329 | |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 4330 | int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf, |
| 4331 | size_t buflen, int flags) |
| 4332 | { |
J. Bruce Fields | 4b580ee | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 4333 | struct inode *inode = dentry->d_inode; |
| 4334 | |
| 4335 | if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0) |
| 4336 | return -EOPNOTSUPP; |
| 4337 | |
J. Bruce Fields | 4b580ee | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 4338 | return nfs4_proc_set_acl(inode, buf, buflen); |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
| 4341 | /* The getxattr man page suggests returning -ENODATA for unknown attributes, |
| 4342 | * and that's what we'll do for e.g. user attributes that haven't been set. |
| 4343 | * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported |
| 4344 | * attributes in kernel-managed attribute namespaces. */ |
| 4345 | ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf, |
| 4346 | size_t buflen) |
| 4347 | { |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 4348 | struct inode *inode = dentry->d_inode; |
| 4349 | |
| 4350 | if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0) |
| 4351 | return -EOPNOTSUPP; |
| 4352 | |
| 4353 | return nfs4_proc_get_acl(inode, buf, buflen); |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen) |
| 4357 | { |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 4358 | size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1; |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 4359 | |
J. Bruce Fields | 096455a | 2006-03-20 23:23:42 -0500 | [diff] [blame] | 4360 | if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode))) |
| 4361 | return 0; |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 4362 | if (buf && buflen < len) |
| 4363 | return -ERANGE; |
| 4364 | if (buf) |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 4365 | memcpy(buf, XATTR_NAME_NFSV4_ACL, len); |
| 4366 | return len; |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Trond Myklebust | 69aaaae | 2009-03-11 14:10:28 -0400 | [diff] [blame] | 4369 | static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr) |
| 4370 | { |
| 4371 | if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) && |
| 4372 | (fattr->valid & NFS_ATTR_FATTR_FSID) && |
| 4373 | (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL))) |
| 4374 | return; |
| 4375 | |
| 4376 | fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | |
| 4377 | NFS_ATTR_FATTR_NLINK; |
| 4378 | fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; |
| 4379 | fattr->nlink = 2; |
| 4380 | } |
| 4381 | |
Trond Myklebust | 56659e9 | 2007-07-17 21:52:39 -0400 | [diff] [blame] | 4382 | int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name, |
Manoj Naik | 7aaa0b3 | 2006-06-09 09:34:23 -0400 | [diff] [blame] | 4383 | struct nfs4_fs_locations *fs_locations, struct page *page) |
Trond Myklebust | 683b57b | 2006-06-09 09:34:22 -0400 | [diff] [blame] | 4384 | { |
| 4385 | struct nfs_server *server = NFS_SERVER(dir); |
| 4386 | u32 bitmask[2] = { |
Manoj Naik | 361e624 | 2006-06-09 09:34:24 -0400 | [diff] [blame] | 4387 | [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, |
| 4388 | [1] = FATTR4_WORD1_MOUNTED_ON_FILEID, |
Trond Myklebust | 683b57b | 2006-06-09 09:34:22 -0400 | [diff] [blame] | 4389 | }; |
| 4390 | struct nfs4_fs_locations_arg args = { |
| 4391 | .dir_fh = NFS_FH(dir), |
Trond Myklebust | c228fd3 | 2007-01-13 02:28:11 -0500 | [diff] [blame] | 4392 | .name = name, |
Trond Myklebust | 683b57b | 2006-06-09 09:34:22 -0400 | [diff] [blame] | 4393 | .page = page, |
| 4394 | .bitmask = bitmask, |
| 4395 | }; |
Benny Halevy | 2295846 | 2009-04-01 09:22:02 -0400 | [diff] [blame] | 4396 | struct nfs4_fs_locations_res res = { |
| 4397 | .fs_locations = fs_locations, |
| 4398 | }; |
Trond Myklebust | 683b57b | 2006-06-09 09:34:22 -0400 | [diff] [blame] | 4399 | struct rpc_message msg = { |
| 4400 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], |
| 4401 | .rpc_argp = &args, |
Benny Halevy | 2295846 | 2009-04-01 09:22:02 -0400 | [diff] [blame] | 4402 | .rpc_resp = &res, |
Trond Myklebust | 683b57b | 2006-06-09 09:34:22 -0400 | [diff] [blame] | 4403 | }; |
| 4404 | int status; |
| 4405 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 4406 | dprintk("%s: start\n", __func__); |
Trond Myklebust | c228fd3 | 2007-01-13 02:28:11 -0500 | [diff] [blame] | 4407 | nfs_fattr_init(&fs_locations->fattr); |
Trond Myklebust | 683b57b | 2006-06-09 09:34:22 -0400 | [diff] [blame] | 4408 | fs_locations->server = server; |
Manoj Naik | 830b8e3 | 2006-06-09 09:34:25 -0400 | [diff] [blame] | 4409 | fs_locations->nlocations = 0; |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame] | 4410 | status = nfs4_call_sync(server, &msg, &args, &res, 0); |
Trond Myklebust | 69aaaae | 2009-03-11 14:10:28 -0400 | [diff] [blame] | 4411 | nfs_fixup_referral_attributes(&fs_locations->fattr); |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 4412 | dprintk("%s: returned status = %d\n", __func__, status); |
Trond Myklebust | 683b57b | 2006-06-09 09:34:22 -0400 | [diff] [blame] | 4413 | return status; |
| 4414 | } |
| 4415 | |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 4416 | #ifdef CONFIG_NFS_V4_1 |
Benny Halevy | 99fe60d | 2009-04-01 09:22:29 -0400 | [diff] [blame] | 4417 | /* |
| 4418 | * nfs4_proc_exchange_id() |
| 4419 | * |
| 4420 | * Since the clientid has expired, all compounds using sessions |
| 4421 | * associated with the stale clientid will be returning |
| 4422 | * NFS4ERR_BADSESSION in the sequence operation, and will therefore |
| 4423 | * be in some phase of session reset. |
| 4424 | */ |
Andy Adamson | 4d643d1 | 2009-12-04 15:52:24 -0500 | [diff] [blame] | 4425 | int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred) |
Benny Halevy | 99fe60d | 2009-04-01 09:22:29 -0400 | [diff] [blame] | 4426 | { |
| 4427 | nfs4_verifier verifier; |
| 4428 | struct nfs41_exchange_id_args args = { |
| 4429 | .client = clp, |
| 4430 | .flags = clp->cl_exchange_flags, |
| 4431 | }; |
| 4432 | struct nfs41_exchange_id_res res = { |
| 4433 | .client = clp, |
| 4434 | }; |
| 4435 | int status; |
| 4436 | struct rpc_message msg = { |
| 4437 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID], |
| 4438 | .rpc_argp = &args, |
| 4439 | .rpc_resp = &res, |
| 4440 | .rpc_cred = cred, |
| 4441 | }; |
| 4442 | __be32 *p; |
| 4443 | |
| 4444 | dprintk("--> %s\n", __func__); |
| 4445 | BUG_ON(clp == NULL); |
Andy Adamson | a7b7210 | 2009-04-01 09:22:46 -0400 | [diff] [blame] | 4446 | |
Alexandros Batsakis | 7b183d0 | 2009-12-05 13:33:25 -0500 | [diff] [blame] | 4447 | /* Remove server-only flags */ |
| 4448 | args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R; |
| 4449 | |
Benny Halevy | 99fe60d | 2009-04-01 09:22:29 -0400 | [diff] [blame] | 4450 | p = (u32 *)verifier.data; |
| 4451 | *p++ = htonl((u32)clp->cl_boot_time.tv_sec); |
| 4452 | *p = htonl((u32)clp->cl_boot_time.tv_nsec); |
| 4453 | args.verifier = &verifier; |
| 4454 | |
| 4455 | while (1) { |
| 4456 | args.id_len = scnprintf(args.id, sizeof(args.id), |
| 4457 | "%s/%s %u", |
| 4458 | clp->cl_ipaddr, |
| 4459 | rpc_peeraddr2str(clp->cl_rpcclient, |
| 4460 | RPC_DISPLAY_ADDR), |
| 4461 | clp->cl_id_uniquifier); |
| 4462 | |
| 4463 | status = rpc_call_sync(clp->cl_rpcclient, &msg, 0); |
| 4464 | |
| 4465 | if (status != NFS4ERR_CLID_INUSE) |
| 4466 | break; |
| 4467 | |
| 4468 | if (signalled()) |
| 4469 | break; |
| 4470 | |
| 4471 | if (++clp->cl_id_uniquifier == 0) |
| 4472 | break; |
| 4473 | } |
| 4474 | |
| 4475 | dprintk("<-- %s status= %d\n", __func__, status); |
| 4476 | return status; |
| 4477 | } |
| 4478 | |
Andy Adamson | 2050f0c | 2009-04-01 09:22:30 -0400 | [diff] [blame] | 4479 | struct nfs4_get_lease_time_data { |
| 4480 | struct nfs4_get_lease_time_args *args; |
| 4481 | struct nfs4_get_lease_time_res *res; |
| 4482 | struct nfs_client *clp; |
| 4483 | }; |
| 4484 | |
| 4485 | static void nfs4_get_lease_time_prepare(struct rpc_task *task, |
| 4486 | void *calldata) |
| 4487 | { |
| 4488 | int ret; |
| 4489 | struct nfs4_get_lease_time_data *data = |
| 4490 | (struct nfs4_get_lease_time_data *)calldata; |
| 4491 | |
| 4492 | dprintk("--> %s\n", __func__); |
Alexandros Batsakis | 689cf5c | 2009-12-14 21:27:56 -0800 | [diff] [blame] | 4493 | rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED); |
Andy Adamson | 2050f0c | 2009-04-01 09:22:30 -0400 | [diff] [blame] | 4494 | /* just setup sequence, do not trigger session recovery |
| 4495 | since we're invoked within one */ |
| 4496 | ret = nfs41_setup_sequence(data->clp->cl_session, |
Alexandros Batsakis | 689cf5c | 2009-12-14 21:27:56 -0800 | [diff] [blame] | 4497 | &data->args->la_seq_args, |
| 4498 | &data->res->lr_seq_res, 0, task); |
Andy Adamson | 2050f0c | 2009-04-01 09:22:30 -0400 | [diff] [blame] | 4499 | |
| 4500 | BUG_ON(ret == -EAGAIN); |
| 4501 | rpc_call_start(task); |
| 4502 | dprintk("<-- %s\n", __func__); |
| 4503 | } |
| 4504 | |
| 4505 | /* |
| 4506 | * Called from nfs4_state_manager thread for session setup, so don't recover |
| 4507 | * from sequence operation or clientid errors. |
| 4508 | */ |
| 4509 | static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata) |
| 4510 | { |
| 4511 | struct nfs4_get_lease_time_data *data = |
| 4512 | (struct nfs4_get_lease_time_data *)calldata; |
| 4513 | |
| 4514 | dprintk("--> %s\n", __func__); |
| 4515 | nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status); |
| 4516 | switch (task->tk_status) { |
| 4517 | case -NFS4ERR_DELAY: |
| 4518 | case -NFS4ERR_GRACE: |
| 4519 | dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status); |
| 4520 | rpc_delay(task, NFS4_POLL_RETRY_MIN); |
| 4521 | task->tk_status = 0; |
Trond Myklebust | 0110ee1 | 2009-12-07 09:00:24 -0500 | [diff] [blame] | 4522 | nfs_restart_rpc(task, data->clp); |
Andy Adamson | 2050f0c | 2009-04-01 09:22:30 -0400 | [diff] [blame] | 4523 | return; |
| 4524 | } |
Andy Adamson | 2050f0c | 2009-04-01 09:22:30 -0400 | [diff] [blame] | 4525 | dprintk("<-- %s\n", __func__); |
| 4526 | } |
| 4527 | |
| 4528 | struct rpc_call_ops nfs4_get_lease_time_ops = { |
| 4529 | .rpc_call_prepare = nfs4_get_lease_time_prepare, |
| 4530 | .rpc_call_done = nfs4_get_lease_time_done, |
| 4531 | }; |
| 4532 | |
| 4533 | int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo) |
| 4534 | { |
| 4535 | struct rpc_task *task; |
| 4536 | struct nfs4_get_lease_time_args args; |
| 4537 | struct nfs4_get_lease_time_res res = { |
| 4538 | .lr_fsinfo = fsinfo, |
| 4539 | }; |
| 4540 | struct nfs4_get_lease_time_data data = { |
| 4541 | .args = &args, |
| 4542 | .res = &res, |
| 4543 | .clp = clp, |
| 4544 | }; |
| 4545 | struct rpc_message msg = { |
| 4546 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME], |
| 4547 | .rpc_argp = &args, |
| 4548 | .rpc_resp = &res, |
| 4549 | }; |
| 4550 | struct rpc_task_setup task_setup = { |
| 4551 | .rpc_client = clp->cl_rpcclient, |
| 4552 | .rpc_message = &msg, |
| 4553 | .callback_ops = &nfs4_get_lease_time_ops, |
| 4554 | .callback_data = &data |
| 4555 | }; |
| 4556 | int status; |
| 4557 | |
| 4558 | res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; |
| 4559 | dprintk("--> %s\n", __func__); |
| 4560 | task = rpc_run_task(&task_setup); |
| 4561 | |
| 4562 | if (IS_ERR(task)) |
| 4563 | status = PTR_ERR(task); |
| 4564 | else { |
| 4565 | status = task->tk_status; |
| 4566 | rpc_put_task(task); |
| 4567 | } |
| 4568 | dprintk("<-- %s return %d\n", __func__, status); |
| 4569 | |
| 4570 | return status; |
| 4571 | } |
| 4572 | |
Ricardo Labiaga | b73dafa | 2009-04-01 09:23:31 -0400 | [diff] [blame] | 4573 | /* |
| 4574 | * Reset a slot table |
| 4575 | */ |
| 4576 | static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots, |
| 4577 | int old_max_slots, int ivalue) |
Andy Adamson | ac72b7b | 2009-04-01 09:22:37 -0400 | [diff] [blame] | 4578 | { |
Ricardo Labiaga | b73dafa | 2009-04-01 09:23:31 -0400 | [diff] [blame] | 4579 | int i; |
Andy Adamson | ac72b7b | 2009-04-01 09:22:37 -0400 | [diff] [blame] | 4580 | int ret = 0; |
| 4581 | |
Ricardo Labiaga | b73dafa | 2009-04-01 09:23:31 -0400 | [diff] [blame] | 4582 | dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl); |
Andy Adamson | ac72b7b | 2009-04-01 09:22:37 -0400 | [diff] [blame] | 4583 | |
Ricardo Labiaga | b73dafa | 2009-04-01 09:23:31 -0400 | [diff] [blame] | 4584 | /* |
| 4585 | * Until we have dynamic slot table adjustment, insist |
| 4586 | * upon the same slot table size |
| 4587 | */ |
Andy Adamson | ac72b7b | 2009-04-01 09:22:37 -0400 | [diff] [blame] | 4588 | if (max_slots != old_max_slots) { |
| 4589 | dprintk("%s reset slot table does't match old\n", |
| 4590 | __func__); |
| 4591 | ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */ |
| 4592 | goto out; |
| 4593 | } |
| 4594 | spin_lock(&tbl->slot_tbl_lock); |
| 4595 | for (i = 0; i < max_slots; ++i) |
Ricardo Labiaga | b73dafa | 2009-04-01 09:23:31 -0400 | [diff] [blame] | 4596 | tbl->slots[i].seq_nr = ivalue; |
Andy Adamson | ac72b7b | 2009-04-01 09:22:37 -0400 | [diff] [blame] | 4597 | spin_unlock(&tbl->slot_tbl_lock); |
| 4598 | dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__, |
| 4599 | tbl, tbl->slots, tbl->max_slots); |
| 4600 | out: |
| 4601 | dprintk("<-- %s: return %d\n", __func__, ret); |
| 4602 | return ret; |
| 4603 | } |
| 4604 | |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4605 | /* |
Ricardo Labiaga | b73dafa | 2009-04-01 09:23:31 -0400 | [diff] [blame] | 4606 | * Reset the forechannel and backchannel slot tables |
| 4607 | */ |
| 4608 | static int nfs4_reset_slot_tables(struct nfs4_session *session) |
| 4609 | { |
| 4610 | int status; |
| 4611 | |
| 4612 | status = nfs4_reset_slot_table(&session->fc_slot_table, |
| 4613 | session->fc_attrs.max_reqs, |
| 4614 | session->fc_slot_table.max_slots, |
| 4615 | 1); |
Ricardo Labiaga | f8625a6 | 2009-04-01 09:23:33 -0400 | [diff] [blame] | 4616 | if (status) |
| 4617 | return status; |
| 4618 | |
| 4619 | status = nfs4_reset_slot_table(&session->bc_slot_table, |
| 4620 | session->bc_attrs.max_reqs, |
| 4621 | session->bc_slot_table.max_slots, |
| 4622 | 0); |
Ricardo Labiaga | b73dafa | 2009-04-01 09:23:31 -0400 | [diff] [blame] | 4623 | return status; |
| 4624 | } |
| 4625 | |
Ricardo Labiaga | f8625a6 | 2009-04-01 09:23:33 -0400 | [diff] [blame] | 4626 | /* Destroy the slot table */ |
| 4627 | static void nfs4_destroy_slot_tables(struct nfs4_session *session) |
| 4628 | { |
| 4629 | if (session->fc_slot_table.slots != NULL) { |
| 4630 | kfree(session->fc_slot_table.slots); |
| 4631 | session->fc_slot_table.slots = NULL; |
| 4632 | } |
| 4633 | if (session->bc_slot_table.slots != NULL) { |
| 4634 | kfree(session->bc_slot_table.slots); |
| 4635 | session->bc_slot_table.slots = NULL; |
| 4636 | } |
| 4637 | return; |
| 4638 | } |
| 4639 | |
Ricardo Labiaga | b73dafa | 2009-04-01 09:23:31 -0400 | [diff] [blame] | 4640 | /* |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4641 | * Initialize slot table |
| 4642 | */ |
Ricardo Labiaga | 050047c | 2009-04-01 09:23:32 -0400 | [diff] [blame] | 4643 | static int nfs4_init_slot_table(struct nfs4_slot_table *tbl, |
| 4644 | int max_slots, int ivalue) |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4645 | { |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4646 | struct nfs4_slot *slot; |
| 4647 | int ret = -ENOMEM; |
| 4648 | |
| 4649 | BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE); |
| 4650 | |
Ricardo Labiaga | 050047c | 2009-04-01 09:23:32 -0400 | [diff] [blame] | 4651 | dprintk("--> %s: max_reqs=%u\n", __func__, max_slots); |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4652 | |
| 4653 | slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL); |
| 4654 | if (!slot) |
| 4655 | goto out; |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4656 | ret = 0; |
| 4657 | |
| 4658 | spin_lock(&tbl->slot_tbl_lock); |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4659 | tbl->max_slots = max_slots; |
| 4660 | tbl->slots = slot; |
| 4661 | tbl->highest_used_slotid = -1; /* no slot is currently used */ |
| 4662 | spin_unlock(&tbl->slot_tbl_lock); |
| 4663 | dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__, |
| 4664 | tbl, tbl->slots, tbl->max_slots); |
| 4665 | out: |
| 4666 | dprintk("<-- %s: return %d\n", __func__, ret); |
| 4667 | return ret; |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4668 | } |
| 4669 | |
Ricardo Labiaga | 050047c | 2009-04-01 09:23:32 -0400 | [diff] [blame] | 4670 | /* |
| 4671 | * Initialize the forechannel and backchannel tables |
| 4672 | */ |
| 4673 | static int nfs4_init_slot_tables(struct nfs4_session *session) |
| 4674 | { |
Trond Myklebust | f26468f | 2009-12-05 19:32:11 -0500 | [diff] [blame] | 4675 | struct nfs4_slot_table *tbl; |
| 4676 | int status = 0; |
Ricardo Labiaga | 050047c | 2009-04-01 09:23:32 -0400 | [diff] [blame] | 4677 | |
Trond Myklebust | f26468f | 2009-12-05 19:32:11 -0500 | [diff] [blame] | 4678 | tbl = &session->fc_slot_table; |
| 4679 | if (tbl->slots == NULL) { |
| 4680 | status = nfs4_init_slot_table(tbl, |
| 4681 | session->fc_attrs.max_reqs, 1); |
| 4682 | if (status) |
| 4683 | return status; |
| 4684 | } |
Ricardo Labiaga | 050047c | 2009-04-01 09:23:32 -0400 | [diff] [blame] | 4685 | |
Trond Myklebust | f26468f | 2009-12-05 19:32:11 -0500 | [diff] [blame] | 4686 | tbl = &session->bc_slot_table; |
| 4687 | if (tbl->slots == NULL) { |
| 4688 | status = nfs4_init_slot_table(tbl, |
| 4689 | session->bc_attrs.max_reqs, 0); |
| 4690 | if (status) |
| 4691 | nfs4_destroy_slot_tables(session); |
| 4692 | } |
Ricardo Labiaga | f8625a6 | 2009-04-01 09:23:33 -0400 | [diff] [blame] | 4693 | |
| 4694 | return status; |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 4695 | } |
| 4696 | |
| 4697 | struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp) |
| 4698 | { |
| 4699 | struct nfs4_session *session; |
| 4700 | struct nfs4_slot_table *tbl; |
| 4701 | |
| 4702 | session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL); |
| 4703 | if (!session) |
| 4704 | return NULL; |
Andy Adamson | 76db6d9 | 2009-04-01 09:22:38 -0400 | [diff] [blame] | 4705 | |
Andy Adamson | 76db6d9 | 2009-04-01 09:22:38 -0400 | [diff] [blame] | 4706 | /* |
| 4707 | * The create session reply races with the server back |
| 4708 | * channel probe. Mark the client NFS_CS_SESSION_INITING |
| 4709 | * so that the client back channel can find the |
| 4710 | * nfs_client struct |
| 4711 | */ |
| 4712 | clp->cl_cons_state = NFS_CS_SESSION_INITING; |
Andy Adamson | ea028ac | 2009-12-04 15:55:38 -0500 | [diff] [blame] | 4713 | init_completion(&session->complete); |
Andy Adamson | 76db6d9 | 2009-04-01 09:22:38 -0400 | [diff] [blame] | 4714 | |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 4715 | tbl = &session->fc_slot_table; |
Ricardo Labiaga | 9dfdf40 | 2009-12-06 12:57:34 -0500 | [diff] [blame] | 4716 | tbl->highest_used_slotid = -1; |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 4717 | spin_lock_init(&tbl->slot_tbl_lock); |
Alexandros Batsakis | 689cf5c | 2009-12-14 21:27:56 -0800 | [diff] [blame] | 4718 | rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table"); |
Ricardo Labiaga | f8625a6 | 2009-04-01 09:23:33 -0400 | [diff] [blame] | 4719 | |
| 4720 | tbl = &session->bc_slot_table; |
Ricardo Labiaga | 9dfdf40 | 2009-12-06 12:57:34 -0500 | [diff] [blame] | 4721 | tbl->highest_used_slotid = -1; |
Ricardo Labiaga | f8625a6 | 2009-04-01 09:23:33 -0400 | [diff] [blame] | 4722 | spin_lock_init(&tbl->slot_tbl_lock); |
| 4723 | rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table"); |
| 4724 | |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 4725 | session->clp = clp; |
| 4726 | return session; |
| 4727 | } |
| 4728 | |
| 4729 | void nfs4_destroy_session(struct nfs4_session *session) |
| 4730 | { |
Andy Adamson | 5a0ffe5 | 2009-04-01 09:23:18 -0400 | [diff] [blame] | 4731 | nfs4_proc_destroy_session(session); |
| 4732 | dprintk("%s Destroy backchannel for xprt %p\n", |
| 4733 | __func__, session->clp->cl_rpcclient->cl_xprt); |
| 4734 | xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt, |
| 4735 | NFS41_BC_MIN_CALLBACKS); |
Ricardo Labiaga | f8625a6 | 2009-04-01 09:23:33 -0400 | [diff] [blame] | 4736 | nfs4_destroy_slot_tables(session); |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 4737 | kfree(session); |
| 4738 | } |
| 4739 | |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4740 | /* |
| 4741 | * Initialize the values to be used by the client in CREATE_SESSION |
| 4742 | * If nfs4_init_session set the fore channel request and response sizes, |
| 4743 | * use them. |
| 4744 | * |
| 4745 | * Set the back channel max_resp_sz_cached to zero to force the client to |
| 4746 | * always set csa_cachethis to FALSE because the current implementation |
| 4747 | * of the back channel DRC only supports caching the CB_SEQUENCE operation. |
| 4748 | */ |
| 4749 | static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args) |
| 4750 | { |
| 4751 | struct nfs4_session *session = args->client->cl_session; |
| 4752 | unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz, |
| 4753 | mxresp_sz = session->fc_attrs.max_resp_sz; |
| 4754 | |
| 4755 | if (mxrqst_sz == 0) |
| 4756 | mxrqst_sz = NFS_MAX_FILE_IO_SIZE; |
| 4757 | if (mxresp_sz == 0) |
| 4758 | mxresp_sz = NFS_MAX_FILE_IO_SIZE; |
| 4759 | /* Fore channel attributes */ |
| 4760 | args->fc_attrs.headerpadsz = 0; |
| 4761 | args->fc_attrs.max_rqst_sz = mxrqst_sz; |
| 4762 | args->fc_attrs.max_resp_sz = mxresp_sz; |
| 4763 | args->fc_attrs.max_resp_sz_cached = mxresp_sz; |
| 4764 | args->fc_attrs.max_ops = NFS4_MAX_OPS; |
| 4765 | args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs; |
| 4766 | |
| 4767 | dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u " |
| 4768 | "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n", |
| 4769 | __func__, |
| 4770 | args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz, |
| 4771 | args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops, |
| 4772 | args->fc_attrs.max_reqs); |
| 4773 | |
| 4774 | /* Back channel attributes */ |
| 4775 | args->bc_attrs.headerpadsz = 0; |
| 4776 | args->bc_attrs.max_rqst_sz = PAGE_SIZE; |
| 4777 | args->bc_attrs.max_resp_sz = PAGE_SIZE; |
| 4778 | args->bc_attrs.max_resp_sz_cached = 0; |
| 4779 | args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS; |
| 4780 | args->bc_attrs.max_reqs = 1; |
| 4781 | |
| 4782 | dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u " |
| 4783 | "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n", |
| 4784 | __func__, |
| 4785 | args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz, |
| 4786 | args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops, |
| 4787 | args->bc_attrs.max_reqs); |
| 4788 | } |
| 4789 | |
Andy Adamson | 8d35301 | 2009-04-01 09:22:32 -0400 | [diff] [blame] | 4790 | static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd) |
| 4791 | { |
| 4792 | if (rcvd <= sent) |
| 4793 | return 0; |
| 4794 | printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. " |
| 4795 | "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd); |
| 4796 | return -EINVAL; |
| 4797 | } |
| 4798 | |
| 4799 | #define _verify_fore_channel_attr(_name_) \ |
| 4800 | _verify_channel_attr("fore", #_name_, \ |
| 4801 | args->fc_attrs._name_, \ |
| 4802 | session->fc_attrs._name_) |
| 4803 | |
| 4804 | #define _verify_back_channel_attr(_name_) \ |
| 4805 | _verify_channel_attr("back", #_name_, \ |
| 4806 | args->bc_attrs._name_, \ |
| 4807 | session->bc_attrs._name_) |
| 4808 | |
| 4809 | /* |
| 4810 | * The server is not allowed to increase the fore channel header pad size, |
| 4811 | * maximum response size, or maximum number of operations. |
| 4812 | * |
| 4813 | * The back channel attributes are only negotiatied down: We send what the |
| 4814 | * (back channel) server insists upon. |
| 4815 | */ |
| 4816 | static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args, |
| 4817 | struct nfs4_session *session) |
| 4818 | { |
| 4819 | int ret = 0; |
| 4820 | |
| 4821 | ret |= _verify_fore_channel_attr(headerpadsz); |
| 4822 | ret |= _verify_fore_channel_attr(max_resp_sz); |
| 4823 | ret |= _verify_fore_channel_attr(max_ops); |
| 4824 | |
| 4825 | ret |= _verify_back_channel_attr(headerpadsz); |
| 4826 | ret |= _verify_back_channel_attr(max_rqst_sz); |
| 4827 | ret |= _verify_back_channel_attr(max_resp_sz); |
| 4828 | ret |= _verify_back_channel_attr(max_resp_sz_cached); |
| 4829 | ret |= _verify_back_channel_attr(max_ops); |
| 4830 | ret |= _verify_back_channel_attr(max_reqs); |
| 4831 | |
| 4832 | return ret; |
| 4833 | } |
| 4834 | |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4835 | static int _nfs4_proc_create_session(struct nfs_client *clp) |
| 4836 | { |
| 4837 | struct nfs4_session *session = clp->cl_session; |
| 4838 | struct nfs41_create_session_args args = { |
| 4839 | .client = clp, |
| 4840 | .cb_program = NFS4_CALLBACK, |
| 4841 | }; |
| 4842 | struct nfs41_create_session_res res = { |
| 4843 | .client = clp, |
| 4844 | }; |
| 4845 | struct rpc_message msg = { |
| 4846 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION], |
| 4847 | .rpc_argp = &args, |
| 4848 | .rpc_resp = &res, |
| 4849 | }; |
| 4850 | int status; |
| 4851 | |
| 4852 | nfs4_init_channel_attrs(&args); |
Andy Adamson | 0f91421 | 2009-04-01 09:23:16 -0400 | [diff] [blame] | 4853 | args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN); |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4854 | |
| 4855 | status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0); |
| 4856 | |
Andy Adamson | 8d35301 | 2009-04-01 09:22:32 -0400 | [diff] [blame] | 4857 | if (!status) |
| 4858 | /* Verify the session's negotiated channel_attrs values */ |
| 4859 | status = nfs4_verify_channel_attrs(&args, session); |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4860 | if (!status) { |
| 4861 | /* Increment the clientid slot sequence id */ |
| 4862 | clp->cl_seqid++; |
| 4863 | } |
| 4864 | |
| 4865 | return status; |
| 4866 | } |
| 4867 | |
| 4868 | /* |
| 4869 | * Issues a CREATE_SESSION operation to the server. |
| 4870 | * It is the responsibility of the caller to verify the session is |
| 4871 | * expired before calling this routine. |
| 4872 | */ |
Trond Myklebust | f26468f | 2009-12-05 19:32:11 -0500 | [diff] [blame] | 4873 | int nfs4_proc_create_session(struct nfs_client *clp) |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4874 | { |
| 4875 | int status; |
| 4876 | unsigned *ptr; |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4877 | struct nfs4_session *session = clp->cl_session; |
| 4878 | |
| 4879 | dprintk("--> %s clp=%p session=%p\n", __func__, clp, session); |
| 4880 | |
| 4881 | status = _nfs4_proc_create_session(clp); |
| 4882 | if (status) |
| 4883 | goto out; |
| 4884 | |
Trond Myklebust | f26468f | 2009-12-05 19:32:11 -0500 | [diff] [blame] | 4885 | /* Init and reset the fore channel */ |
| 4886 | status = nfs4_init_slot_tables(session); |
| 4887 | dprintk("slot table initialization returned %d\n", status); |
| 4888 | if (status) |
| 4889 | goto out; |
| 4890 | status = nfs4_reset_slot_tables(session); |
| 4891 | dprintk("slot table reset returned %d\n", status); |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4892 | if (status) |
| 4893 | goto out; |
| 4894 | |
| 4895 | ptr = (unsigned *)&session->sess_id.data[0]; |
| 4896 | dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__, |
| 4897 | clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]); |
Andy Adamson | fc93158 | 2009-04-01 09:22:31 -0400 | [diff] [blame] | 4898 | out: |
| 4899 | dprintk("<-- %s\n", __func__); |
| 4900 | return status; |
| 4901 | } |
| 4902 | |
Andy Adamson | 0f3e66c | 2009-04-01 09:22:34 -0400 | [diff] [blame] | 4903 | /* |
| 4904 | * Issue the over-the-wire RPC DESTROY_SESSION. |
| 4905 | * The caller must serialize access to this routine. |
| 4906 | */ |
| 4907 | int nfs4_proc_destroy_session(struct nfs4_session *session) |
| 4908 | { |
| 4909 | int status = 0; |
| 4910 | struct rpc_message msg; |
| 4911 | |
| 4912 | dprintk("--> nfs4_proc_destroy_session\n"); |
| 4913 | |
| 4914 | /* session is still being setup */ |
| 4915 | if (session->clp->cl_cons_state != NFS_CS_READY) |
| 4916 | return status; |
| 4917 | |
| 4918 | msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION]; |
| 4919 | msg.rpc_argp = session; |
| 4920 | msg.rpc_resp = NULL; |
| 4921 | msg.rpc_cred = NULL; |
| 4922 | status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0); |
| 4923 | |
| 4924 | if (status) |
| 4925 | printk(KERN_WARNING |
| 4926 | "Got error %d from the server on DESTROY_SESSION. " |
| 4927 | "Session has been destroyed regardless...\n", status); |
| 4928 | |
| 4929 | dprintk("<-- nfs4_proc_destroy_session\n"); |
| 4930 | return status; |
| 4931 | } |
| 4932 | |
Trond Myklebust | fccba80 | 2009-07-21 16:48:07 -0400 | [diff] [blame] | 4933 | int nfs4_init_session(struct nfs_server *server) |
| 4934 | { |
| 4935 | struct nfs_client *clp = server->nfs_client; |
Alexandros Batsakis | 2449ea2 | 2009-12-05 13:36:55 -0500 | [diff] [blame] | 4936 | struct nfs4_session *session; |
Andy Adamson | 68bf05e | 2009-12-15 12:55:02 -0500 | [diff] [blame] | 4937 | unsigned int rsize, wsize; |
Trond Myklebust | fccba80 | 2009-07-21 16:48:07 -0400 | [diff] [blame] | 4938 | int ret; |
| 4939 | |
| 4940 | if (!nfs4_has_session(clp)) |
| 4941 | return 0; |
| 4942 | |
Andy Adamson | 68bf05e | 2009-12-15 12:55:02 -0500 | [diff] [blame] | 4943 | rsize = server->rsize; |
| 4944 | if (rsize == 0) |
| 4945 | rsize = NFS_MAX_FILE_IO_SIZE; |
| 4946 | wsize = server->wsize; |
| 4947 | if (wsize == 0) |
| 4948 | wsize = NFS_MAX_FILE_IO_SIZE; |
| 4949 | |
Alexandros Batsakis | 2449ea2 | 2009-12-05 13:36:55 -0500 | [diff] [blame] | 4950 | session = clp->cl_session; |
Andy Adamson | 68bf05e | 2009-12-15 12:55:02 -0500 | [diff] [blame] | 4951 | session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead; |
| 4952 | session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead; |
Alexandros Batsakis | 2449ea2 | 2009-12-05 13:36:55 -0500 | [diff] [blame] | 4953 | |
Trond Myklebust | fccba80 | 2009-07-21 16:48:07 -0400 | [diff] [blame] | 4954 | ret = nfs4_recover_expired_lease(server); |
| 4955 | if (!ret) |
| 4956 | ret = nfs4_check_client_ready(clp); |
| 4957 | return ret; |
| 4958 | } |
| 4959 | |
Andy Adamson | fc01cea | 2009-04-01 09:22:36 -0400 | [diff] [blame] | 4960 | /* |
| 4961 | * Renew the cl_session lease. |
| 4962 | */ |
| 4963 | static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred) |
| 4964 | { |
| 4965 | struct nfs4_sequence_args args; |
| 4966 | struct nfs4_sequence_res res; |
| 4967 | |
| 4968 | struct rpc_message msg = { |
| 4969 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE], |
| 4970 | .rpc_argp = &args, |
| 4971 | .rpc_resp = &res, |
| 4972 | .rpc_cred = cred, |
| 4973 | }; |
| 4974 | |
| 4975 | args.sa_cache_this = 0; |
| 4976 | |
| 4977 | return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args, |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 4978 | &res, args.sa_cache_this, 1); |
Andy Adamson | fc01cea | 2009-04-01 09:22:36 -0400 | [diff] [blame] | 4979 | } |
| 4980 | |
| 4981 | void nfs41_sequence_call_done(struct rpc_task *task, void *data) |
| 4982 | { |
| 4983 | struct nfs_client *clp = (struct nfs_client *)data; |
| 4984 | |
| 4985 | nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status); |
| 4986 | |
| 4987 | if (task->tk_status < 0) { |
| 4988 | dprintk("%s ERROR %d\n", __func__, task->tk_status); |
| 4989 | |
| 4990 | if (_nfs4_async_handle_error(task, NULL, clp, NULL) |
| 4991 | == -EAGAIN) { |
Trond Myklebust | 0110ee1 | 2009-12-07 09:00:24 -0500 | [diff] [blame] | 4992 | nfs_restart_rpc(task, clp); |
Andy Adamson | fc01cea | 2009-04-01 09:22:36 -0400 | [diff] [blame] | 4993 | return; |
| 4994 | } |
| 4995 | } |
Andy Adamson | fc01cea | 2009-04-01 09:22:36 -0400 | [diff] [blame] | 4996 | dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred); |
| 4997 | |
Andy Adamson | fc01cea | 2009-04-01 09:22:36 -0400 | [diff] [blame] | 4998 | kfree(task->tk_msg.rpc_argp); |
| 4999 | kfree(task->tk_msg.rpc_resp); |
| 5000 | |
| 5001 | dprintk("<-- %s\n", __func__); |
| 5002 | } |
| 5003 | |
| 5004 | static void nfs41_sequence_prepare(struct rpc_task *task, void *data) |
| 5005 | { |
| 5006 | struct nfs_client *clp; |
| 5007 | struct nfs4_sequence_args *args; |
| 5008 | struct nfs4_sequence_res *res; |
| 5009 | |
| 5010 | clp = (struct nfs_client *)data; |
| 5011 | args = task->tk_msg.rpc_argp; |
| 5012 | res = task->tk_msg.rpc_resp; |
| 5013 | |
| 5014 | if (nfs4_setup_sequence(clp, args, res, 0, task)) |
| 5015 | return; |
| 5016 | rpc_call_start(task); |
| 5017 | } |
| 5018 | |
| 5019 | static const struct rpc_call_ops nfs41_sequence_ops = { |
| 5020 | .rpc_call_done = nfs41_sequence_call_done, |
| 5021 | .rpc_call_prepare = nfs41_sequence_prepare, |
| 5022 | }; |
| 5023 | |
| 5024 | static int nfs41_proc_async_sequence(struct nfs_client *clp, |
| 5025 | struct rpc_cred *cred) |
| 5026 | { |
| 5027 | struct nfs4_sequence_args *args; |
| 5028 | struct nfs4_sequence_res *res; |
| 5029 | struct rpc_message msg = { |
| 5030 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE], |
| 5031 | .rpc_cred = cred, |
| 5032 | }; |
| 5033 | |
| 5034 | args = kzalloc(sizeof(*args), GFP_KERNEL); |
| 5035 | if (!args) |
| 5036 | return -ENOMEM; |
| 5037 | res = kzalloc(sizeof(*res), GFP_KERNEL); |
| 5038 | if (!res) { |
| 5039 | kfree(args); |
| 5040 | return -ENOMEM; |
| 5041 | } |
| 5042 | res->sr_slotid = NFS4_MAX_SLOT_TABLE; |
| 5043 | msg.rpc_argp = args; |
| 5044 | msg.rpc_resp = res; |
| 5045 | |
| 5046 | return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT, |
| 5047 | &nfs41_sequence_ops, (void *)clp); |
| 5048 | } |
| 5049 | |
Ricardo Labiaga | fce5c83 | 2009-12-05 16:08:41 -0500 | [diff] [blame] | 5050 | struct nfs4_reclaim_complete_data { |
| 5051 | struct nfs_client *clp; |
| 5052 | struct nfs41_reclaim_complete_args arg; |
| 5053 | struct nfs41_reclaim_complete_res res; |
| 5054 | }; |
| 5055 | |
| 5056 | static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data) |
| 5057 | { |
| 5058 | struct nfs4_reclaim_complete_data *calldata = data; |
| 5059 | |
Alexandros Batsakis | b257957 | 2009-12-14 21:27:57 -0800 | [diff] [blame] | 5060 | rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED); |
Ricardo Labiaga | fce5c83 | 2009-12-05 16:08:41 -0500 | [diff] [blame] | 5061 | if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args, |
| 5062 | &calldata->res.seq_res, 0, task)) |
| 5063 | return; |
| 5064 | |
| 5065 | rpc_call_start(task); |
| 5066 | } |
| 5067 | |
| 5068 | static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data) |
| 5069 | { |
| 5070 | struct nfs4_reclaim_complete_data *calldata = data; |
| 5071 | struct nfs_client *clp = calldata->clp; |
| 5072 | struct nfs4_sequence_res *res = &calldata->res.seq_res; |
| 5073 | |
| 5074 | dprintk("--> %s\n", __func__); |
| 5075 | nfs41_sequence_done(clp, res, task->tk_status); |
| 5076 | switch (task->tk_status) { |
| 5077 | case 0: |
| 5078 | case -NFS4ERR_COMPLETE_ALREADY: |
| 5079 | break; |
| 5080 | case -NFS4ERR_BADSESSION: |
| 5081 | case -NFS4ERR_DEADSESSION: |
| 5082 | /* |
| 5083 | * Handle the session error, but do not retry the operation, as |
| 5084 | * we have no way of telling whether the clientid had to be |
| 5085 | * reset before we got our reply. If reset, a new wave of |
| 5086 | * reclaim operations will follow, containing their own reclaim |
| 5087 | * complete. We don't want our retry to get on the way of |
| 5088 | * recovery by incorrectly indicating to the server that we're |
| 5089 | * done reclaiming state since the process had to be restarted. |
| 5090 | */ |
| 5091 | _nfs4_async_handle_error(task, NULL, clp, NULL); |
| 5092 | break; |
| 5093 | default: |
| 5094 | if (_nfs4_async_handle_error( |
| 5095 | task, NULL, clp, NULL) == -EAGAIN) { |
| 5096 | rpc_restart_call_prepare(task); |
| 5097 | return; |
| 5098 | } |
| 5099 | } |
Ricardo Labiaga | fce5c83 | 2009-12-05 16:08:41 -0500 | [diff] [blame] | 5100 | |
| 5101 | dprintk("<-- %s\n", __func__); |
| 5102 | } |
| 5103 | |
| 5104 | static void nfs4_free_reclaim_complete_data(void *data) |
| 5105 | { |
| 5106 | struct nfs4_reclaim_complete_data *calldata = data; |
| 5107 | |
| 5108 | kfree(calldata); |
| 5109 | } |
| 5110 | |
| 5111 | static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = { |
| 5112 | .rpc_call_prepare = nfs4_reclaim_complete_prepare, |
| 5113 | .rpc_call_done = nfs4_reclaim_complete_done, |
| 5114 | .rpc_release = nfs4_free_reclaim_complete_data, |
| 5115 | }; |
| 5116 | |
| 5117 | /* |
| 5118 | * Issue a global reclaim complete. |
| 5119 | */ |
| 5120 | static int nfs41_proc_reclaim_complete(struct nfs_client *clp) |
| 5121 | { |
| 5122 | struct nfs4_reclaim_complete_data *calldata; |
| 5123 | struct rpc_task *task; |
| 5124 | struct rpc_message msg = { |
| 5125 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE], |
| 5126 | }; |
| 5127 | struct rpc_task_setup task_setup_data = { |
| 5128 | .rpc_client = clp->cl_rpcclient, |
| 5129 | .rpc_message = &msg, |
| 5130 | .callback_ops = &nfs4_reclaim_complete_call_ops, |
| 5131 | .flags = RPC_TASK_ASYNC, |
| 5132 | }; |
| 5133 | int status = -ENOMEM; |
| 5134 | |
| 5135 | dprintk("--> %s\n", __func__); |
| 5136 | calldata = kzalloc(sizeof(*calldata), GFP_KERNEL); |
| 5137 | if (calldata == NULL) |
| 5138 | goto out; |
| 5139 | calldata->clp = clp; |
| 5140 | calldata->arg.one_fs = 0; |
| 5141 | calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; |
| 5142 | |
| 5143 | msg.rpc_argp = &calldata->arg; |
| 5144 | msg.rpc_resp = &calldata->res; |
| 5145 | task_setup_data.callback_data = calldata; |
| 5146 | task = rpc_run_task(&task_setup_data); |
| 5147 | if (IS_ERR(task)) |
| 5148 | status = PTR_ERR(task); |
| 5149 | rpc_put_task(task); |
| 5150 | out: |
| 5151 | dprintk("<-- %s status=%d\n", __func__, status); |
| 5152 | return status; |
| 5153 | } |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 5154 | #endif /* CONFIG_NFS_V4_1 */ |
| 5155 | |
Andy Adamson | 591d71c | 2009-04-01 09:22:47 -0400 | [diff] [blame] | 5156 | struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = { |
Trond Myklebust | 7eff03a | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 5157 | .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 5158 | .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | .recover_open = nfs4_open_reclaim, |
| 5160 | .recover_lock = nfs4_lock_reclaim, |
Andy Adamson | 591d71c | 2009-04-01 09:22:47 -0400 | [diff] [blame] | 5161 | .establish_clid = nfs4_init_clientid, |
Andy Adamson | 90a1661 | 2009-04-01 09:22:48 -0400 | [diff] [blame] | 5162 | .get_clid_cred = nfs4_get_setclientid_cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | }; |
| 5164 | |
Andy Adamson | 591d71c | 2009-04-01 09:22:47 -0400 | [diff] [blame] | 5165 | #if defined(CONFIG_NFS_V4_1) |
| 5166 | struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = { |
| 5167 | .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, |
| 5168 | .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, |
| 5169 | .recover_open = nfs4_open_reclaim, |
| 5170 | .recover_lock = nfs4_lock_reclaim, |
Andy Adamson | 4d643d1 | 2009-12-04 15:52:24 -0500 | [diff] [blame] | 5171 | .establish_clid = nfs41_init_clientid, |
Andy Adamson | b4b8260 | 2009-04-01 09:22:49 -0400 | [diff] [blame] | 5172 | .get_clid_cred = nfs4_get_exchange_id_cred, |
Ricardo Labiaga | fce5c83 | 2009-12-05 16:08:41 -0500 | [diff] [blame] | 5173 | .reclaim_complete = nfs41_proc_reclaim_complete, |
Andy Adamson | 591d71c | 2009-04-01 09:22:47 -0400 | [diff] [blame] | 5174 | }; |
| 5175 | #endif /* CONFIG_NFS_V4_1 */ |
| 5176 | |
| 5177 | struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = { |
Trond Myklebust | 7eff03a | 2008-12-23 15:21:43 -0500 | [diff] [blame] | 5178 | .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, |
Trond Myklebust | b79a4a1 | 2008-12-23 15:21:41 -0500 | [diff] [blame] | 5179 | .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | .recover_open = nfs4_open_expired, |
| 5181 | .recover_lock = nfs4_lock_expired, |
Andy Adamson | 591d71c | 2009-04-01 09:22:47 -0400 | [diff] [blame] | 5182 | .establish_clid = nfs4_init_clientid, |
Andy Adamson | 90a1661 | 2009-04-01 09:22:48 -0400 | [diff] [blame] | 5183 | .get_clid_cred = nfs4_get_setclientid_cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | }; |
| 5185 | |
Andy Adamson | 591d71c | 2009-04-01 09:22:47 -0400 | [diff] [blame] | 5186 | #if defined(CONFIG_NFS_V4_1) |
| 5187 | struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = { |
| 5188 | .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, |
| 5189 | .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, |
| 5190 | .recover_open = nfs4_open_expired, |
| 5191 | .recover_lock = nfs4_lock_expired, |
Andy Adamson | 4d643d1 | 2009-12-04 15:52:24 -0500 | [diff] [blame] | 5192 | .establish_clid = nfs41_init_clientid, |
Andy Adamson | b4b8260 | 2009-04-01 09:22:49 -0400 | [diff] [blame] | 5193 | .get_clid_cred = nfs4_get_exchange_id_cred, |
Andy Adamson | 591d71c | 2009-04-01 09:22:47 -0400 | [diff] [blame] | 5194 | }; |
| 5195 | #endif /* CONFIG_NFS_V4_1 */ |
| 5196 | |
Benny Halevy | 29fba38 | 2009-04-01 09:22:44 -0400 | [diff] [blame] | 5197 | struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = { |
| 5198 | .sched_state_renewal = nfs4_proc_async_renew, |
Andy Adamson | a7b7210 | 2009-04-01 09:22:46 -0400 | [diff] [blame] | 5199 | .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked, |
Benny Halevy | 8e69514f | 2009-04-01 09:22:45 -0400 | [diff] [blame] | 5200 | .renew_lease = nfs4_proc_renew, |
Benny Halevy | 29fba38 | 2009-04-01 09:22:44 -0400 | [diff] [blame] | 5201 | }; |
| 5202 | |
| 5203 | #if defined(CONFIG_NFS_V4_1) |
| 5204 | struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = { |
| 5205 | .sched_state_renewal = nfs41_proc_async_sequence, |
Andy Adamson | a7b7210 | 2009-04-01 09:22:46 -0400 | [diff] [blame] | 5206 | .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked, |
Benny Halevy | 8e69514f | 2009-04-01 09:22:45 -0400 | [diff] [blame] | 5207 | .renew_lease = nfs4_proc_sequence, |
Benny Halevy | 29fba38 | 2009-04-01 09:22:44 -0400 | [diff] [blame] | 5208 | }; |
| 5209 | #endif |
| 5210 | |
| 5211 | /* |
| 5212 | * Per minor version reboot and network partition recovery ops |
| 5213 | */ |
| 5214 | |
Andy Adamson | 591d71c | 2009-04-01 09:22:47 -0400 | [diff] [blame] | 5215 | struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = { |
| 5216 | &nfs40_reboot_recovery_ops, |
| 5217 | #if defined(CONFIG_NFS_V4_1) |
| 5218 | &nfs41_reboot_recovery_ops, |
| 5219 | #endif |
| 5220 | }; |
| 5221 | |
| 5222 | struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = { |
| 5223 | &nfs40_nograce_recovery_ops, |
| 5224 | #if defined(CONFIG_NFS_V4_1) |
| 5225 | &nfs41_nograce_recovery_ops, |
| 5226 | #endif |
| 5227 | }; |
| 5228 | |
Benny Halevy | 29fba38 | 2009-04-01 09:22:44 -0400 | [diff] [blame] | 5229 | struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = { |
| 5230 | &nfs40_state_renewal_ops, |
| 5231 | #if defined(CONFIG_NFS_V4_1) |
| 5232 | &nfs41_state_renewal_ops, |
| 5233 | #endif |
| 5234 | }; |
| 5235 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 5236 | static const struct inode_operations nfs4_file_inode_operations = { |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 5237 | .permission = nfs_permission, |
| 5238 | .getattr = nfs_getattr, |
| 5239 | .setattr = nfs_setattr, |
| 5240 | .getxattr = nfs4_getxattr, |
| 5241 | .setxattr = nfs4_setxattr, |
| 5242 | .listxattr = nfs4_listxattr, |
| 5243 | }; |
| 5244 | |
David Howells | 509de81 | 2006-08-22 20:06:11 -0400 | [diff] [blame] | 5245 | const struct nfs_rpc_ops nfs_v4_clientops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | .version = 4, /* protocol version */ |
| 5247 | .dentry_ops = &nfs4_dentry_operations, |
| 5248 | .dir_inode_ops = &nfs4_dir_inode_operations, |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 5249 | .file_inode_ops = &nfs4_file_inode_operations, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | .getroot = nfs4_proc_get_root, |
| 5251 | .getattr = nfs4_proc_getattr, |
| 5252 | .setattr = nfs4_proc_setattr, |
David Howells | 2b3de44 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 5253 | .lookupfh = nfs4_proc_lookupfh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | .lookup = nfs4_proc_lookup, |
| 5255 | .access = nfs4_proc_access, |
| 5256 | .readlink = nfs4_proc_readlink, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | .create = nfs4_proc_create, |
| 5258 | .remove = nfs4_proc_remove, |
| 5259 | .unlink_setup = nfs4_proc_unlink_setup, |
| 5260 | .unlink_done = nfs4_proc_unlink_done, |
| 5261 | .rename = nfs4_proc_rename, |
| 5262 | .link = nfs4_proc_link, |
| 5263 | .symlink = nfs4_proc_symlink, |
| 5264 | .mkdir = nfs4_proc_mkdir, |
| 5265 | .rmdir = nfs4_proc_remove, |
| 5266 | .readdir = nfs4_proc_readdir, |
| 5267 | .mknod = nfs4_proc_mknod, |
| 5268 | .statfs = nfs4_proc_statfs, |
| 5269 | .fsinfo = nfs4_proc_fsinfo, |
| 5270 | .pathconf = nfs4_proc_pathconf, |
David Howells | e9326dc | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 5271 | .set_capabilities = nfs4_server_capabilities, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | .decode_dirent = nfs4_decode_dirent, |
| 5273 | .read_setup = nfs4_proc_read_setup, |
Trond Myklebust | ec06c09 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 5274 | .read_done = nfs4_read_done, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | .write_setup = nfs4_proc_write_setup, |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 5276 | .write_done = nfs4_write_done, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | .commit_setup = nfs4_proc_commit_setup, |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 5278 | .commit_done = nfs4_commit_done, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | .lock = nfs4_proc_lock, |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 5280 | .clear_acl_cache = nfs4_zap_acl_attr, |
Trond Myklebust | 7fe5c39 | 2009-03-19 15:35:50 -0400 | [diff] [blame] | 5281 | .close_context = nfs4_close_context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | }; |
| 5283 | |
| 5284 | /* |
| 5285 | * Local variables: |
| 5286 | * c-basic-offset: 8 |
| 5287 | * End: |
| 5288 | */ |