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