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