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