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" |
| 54 | |
| 55 | #define NFSDBG_FACILITY NFSDBG_PROC |
| 56 | |
| 57 | #define NFS4_POLL_RETRY_MIN (1*HZ) |
| 58 | #define NFS4_POLL_RETRY_MAX (15*HZ) |
| 59 | |
Trond Myklebust | 0a8838f | 2005-10-18 14:20:14 -0700 | [diff] [blame] | 60 | static int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid, struct nfs_seqid *seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *); |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 62 | static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry); |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 64 | static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus); |
| 66 | extern struct rpc_procinfo nfs4_procedures[]; |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | /* Prevent leaks of NFSv4 errors into userland */ |
| 69 | int nfs4_map_errors(int err) |
| 70 | { |
| 71 | if (err < -1000) { |
| 72 | dprintk("%s could not handle NFSv4 error %d\n", |
| 73 | __FUNCTION__, -err); |
| 74 | return -EIO; |
| 75 | } |
| 76 | return err; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * This is our standard bitmap for GETATTR requests. |
| 81 | */ |
| 82 | const u32 nfs4_fattr_bitmap[2] = { |
| 83 | FATTR4_WORD0_TYPE |
| 84 | | FATTR4_WORD0_CHANGE |
| 85 | | FATTR4_WORD0_SIZE |
| 86 | | FATTR4_WORD0_FSID |
| 87 | | FATTR4_WORD0_FILEID, |
| 88 | FATTR4_WORD1_MODE |
| 89 | | FATTR4_WORD1_NUMLINKS |
| 90 | | FATTR4_WORD1_OWNER |
| 91 | | FATTR4_WORD1_OWNER_GROUP |
| 92 | | FATTR4_WORD1_RAWDEV |
| 93 | | FATTR4_WORD1_SPACE_USED |
| 94 | | FATTR4_WORD1_TIME_ACCESS |
| 95 | | FATTR4_WORD1_TIME_METADATA |
| 96 | | FATTR4_WORD1_TIME_MODIFY |
| 97 | }; |
| 98 | |
| 99 | const u32 nfs4_statfs_bitmap[2] = { |
| 100 | FATTR4_WORD0_FILES_AVAIL |
| 101 | | FATTR4_WORD0_FILES_FREE |
| 102 | | FATTR4_WORD0_FILES_TOTAL, |
| 103 | FATTR4_WORD1_SPACE_AVAIL |
| 104 | | FATTR4_WORD1_SPACE_FREE |
| 105 | | FATTR4_WORD1_SPACE_TOTAL |
| 106 | }; |
| 107 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 108 | const u32 nfs4_pathconf_bitmap[2] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | FATTR4_WORD0_MAXLINK |
| 110 | | FATTR4_WORD0_MAXNAME, |
| 111 | 0 |
| 112 | }; |
| 113 | |
| 114 | const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE |
| 115 | | FATTR4_WORD0_MAXREAD |
| 116 | | FATTR4_WORD0_MAXWRITE |
| 117 | | FATTR4_WORD0_LEASE_TIME, |
| 118 | 0 |
| 119 | }; |
| 120 | |
| 121 | static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry, |
| 122 | struct nfs4_readdir_arg *readdir) |
| 123 | { |
| 124 | u32 *start, *p; |
| 125 | |
| 126 | BUG_ON(readdir->count < 80); |
| 127 | if (cookie > 2) { |
Adrian Bunk | b7ef195 | 2005-06-22 17:16:28 +0000 | [diff] [blame] | 128 | readdir->cookie = cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier)); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | readdir->cookie = 0; |
| 134 | memset(&readdir->verifier, 0, sizeof(readdir->verifier)); |
| 135 | if (cookie == 2) |
| 136 | return; |
| 137 | |
| 138 | /* |
| 139 | * NFSv4 servers do not return entries for '.' and '..' |
| 140 | * Therefore, we fake these entries here. We let '.' |
| 141 | * have cookie 0 and '..' have cookie 1. Note that |
| 142 | * when talking to the server, we always send cookie 0 |
| 143 | * instead of 1 or 2. |
| 144 | */ |
| 145 | start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0); |
| 146 | |
| 147 | if (cookie == 0) { |
| 148 | *p++ = xdr_one; /* next */ |
| 149 | *p++ = xdr_zero; /* cookie, first word */ |
| 150 | *p++ = xdr_one; /* cookie, second word */ |
| 151 | *p++ = xdr_one; /* entry len */ |
| 152 | memcpy(p, ".\0\0\0", 4); /* entry */ |
| 153 | p++; |
| 154 | *p++ = xdr_one; /* bitmap length */ |
| 155 | *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */ |
| 156 | *p++ = htonl(8); /* attribute buffer length */ |
| 157 | p = xdr_encode_hyper(p, dentry->d_inode->i_ino); |
| 158 | } |
| 159 | |
| 160 | *p++ = xdr_one; /* next */ |
| 161 | *p++ = xdr_zero; /* cookie, first word */ |
| 162 | *p++ = xdr_two; /* cookie, second word */ |
| 163 | *p++ = xdr_two; /* entry len */ |
| 164 | memcpy(p, "..\0\0", 4); /* entry */ |
| 165 | p++; |
| 166 | *p++ = xdr_one; /* bitmap length */ |
| 167 | *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */ |
| 168 | *p++ = htonl(8); /* attribute buffer length */ |
| 169 | p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino); |
| 170 | |
| 171 | readdir->pgbase = (char *)p - (char *)start; |
| 172 | readdir->count -= readdir->pgbase; |
| 173 | kunmap_atomic(start, KM_USER0); |
| 174 | } |
| 175 | |
| 176 | static void |
| 177 | renew_lease(struct nfs_server *server, unsigned long timestamp) |
| 178 | { |
| 179 | struct nfs4_client *clp = server->nfs4_state; |
| 180 | spin_lock(&clp->cl_lock); |
| 181 | if (time_before(clp->cl_last_renewal,timestamp)) |
| 182 | clp->cl_last_renewal = timestamp; |
| 183 | spin_unlock(&clp->cl_lock); |
| 184 | } |
| 185 | |
| 186 | static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo) |
| 187 | { |
| 188 | struct nfs_inode *nfsi = NFS_I(inode); |
| 189 | |
Trond Myklebust | decf491 | 2005-10-27 22:12:39 -0400 | [diff] [blame] | 190 | spin_lock(&inode->i_lock); |
| 191 | nfsi->cache_validity |= NFS_INO_INVALID_ATTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | if (cinfo->before == nfsi->change_attr && cinfo->atomic) |
| 193 | nfsi->change_attr = cinfo->after; |
Trond Myklebust | decf491 | 2005-10-27 22:12:39 -0400 | [diff] [blame] | 194 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 197 | /* Helper for asynchronous RPC calls */ |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 198 | static int nfs4_call_async(struct rpc_clnt *clnt, |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 199 | const struct rpc_call_ops *tk_ops, void *calldata) |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 200 | { |
| 201 | struct rpc_task *task; |
| 202 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 203 | if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata))) |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 204 | return -ENOMEM; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 205 | rpc_execute(task); |
| 206 | return 0; |
| 207 | } |
| 208 | |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame^] | 209 | static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task) |
| 210 | { |
| 211 | sigset_t oldset; |
| 212 | int ret; |
| 213 | |
| 214 | rpc_clnt_sigmask(task->tk_client, &oldset); |
| 215 | ret = rpc_wait_for_completion_task(task); |
| 216 | rpc_clnt_sigunmask(task->tk_client, &oldset); |
| 217 | return ret; |
| 218 | } |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags) |
| 221 | { |
| 222 | struct inode *inode = state->inode; |
| 223 | |
| 224 | open_flags &= (FMODE_READ|FMODE_WRITE); |
Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 225 | /* Protect against nfs4_find_state_byowner() */ |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 226 | spin_lock(&state->owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | spin_lock(&inode->i_lock); |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 228 | memcpy(&state->stateid, stateid, sizeof(state->stateid)); |
| 229 | if ((open_flags & FMODE_WRITE)) |
| 230 | state->nwriters++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | if (open_flags & FMODE_READ) |
| 232 | state->nreaders++; |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 233 | nfs4_state_set_mode_locked(state, state->state | open_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | spin_unlock(&inode->i_lock); |
Trond Myklebust | ec07342 | 2005-10-20 14:22:47 -0700 | [diff] [blame] | 235 | spin_unlock(&state->owner->so_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* |
| 239 | * OPEN_RECLAIM: |
| 240 | * reclaim state on the server after a reboot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | */ |
| 242 | static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state) |
| 243 | { |
| 244 | struct inode *inode = state->inode; |
| 245 | struct nfs_server *server = NFS_SERVER(inode); |
| 246 | struct nfs_delegation *delegation = NFS_I(inode)->delegation; |
| 247 | struct nfs_openargs o_arg = { |
| 248 | .fh = NFS_FH(inode), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | .id = sp->so_id, |
| 250 | .open_flags = state->state, |
| 251 | .clientid = server->nfs4_state->cl_clientid, |
| 252 | .claim = NFS4_OPEN_CLAIM_PREVIOUS, |
| 253 | .bitmask = server->attr_bitmask, |
| 254 | }; |
| 255 | struct nfs_openres o_res = { |
| 256 | .server = server, /* Grrr */ |
| 257 | }; |
| 258 | struct rpc_message msg = { |
| 259 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR], |
| 260 | .rpc_argp = &o_arg, |
| 261 | .rpc_resp = &o_res, |
| 262 | .rpc_cred = sp->so_cred, |
| 263 | }; |
| 264 | int status; |
| 265 | |
| 266 | if (delegation != NULL) { |
| 267 | if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) { |
| 268 | memcpy(&state->stateid, &delegation->stateid, |
| 269 | sizeof(state->stateid)); |
| 270 | set_bit(NFS_DELEGATED_STATE, &state->flags); |
| 271 | return 0; |
| 272 | } |
| 273 | o_arg.u.delegation_type = delegation->type; |
| 274 | } |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 275 | o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid); |
| 276 | if (o_arg.seqid == NULL) |
| 277 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 279 | /* Confirm the sequence as being established */ |
| 280 | nfs_confirm_seqid(&sp->so_seqid, status); |
| 281 | nfs_increment_open_seqid(status, o_arg.seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | if (status == 0) { |
| 283 | memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid)); |
| 284 | if (o_res.delegation_type != 0) { |
| 285 | nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res); |
| 286 | /* Did the server issue an immediate delegation recall? */ |
| 287 | if (o_res.do_recall) |
| 288 | nfs_async_inode_return_delegation(inode, &o_res.stateid); |
| 289 | } |
| 290 | } |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 291 | nfs_free_seqid(o_arg.seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | clear_bit(NFS_DELEGATED_STATE, &state->flags); |
| 293 | /* Ensure we update the inode attributes */ |
| 294 | NFS_CACHEINV(inode); |
| 295 | return status; |
| 296 | } |
| 297 | |
| 298 | static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state) |
| 299 | { |
| 300 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 301 | struct nfs4_exception exception = { }; |
| 302 | int err; |
| 303 | do { |
| 304 | err = _nfs4_open_reclaim(sp, state); |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 305 | if (err != -NFS4ERR_DELAY) |
| 306 | break; |
| 307 | nfs4_handle_exception(server, err, &exception); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } while (exception.retry); |
| 309 | return err; |
| 310 | } |
| 311 | |
| 312 | static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state) |
| 313 | { |
| 314 | struct nfs4_state_owner *sp = state->owner; |
| 315 | struct inode *inode = dentry->d_inode; |
| 316 | struct nfs_server *server = NFS_SERVER(inode); |
| 317 | struct dentry *parent = dget_parent(dentry); |
| 318 | struct nfs_openargs arg = { |
| 319 | .fh = NFS_FH(parent->d_inode), |
| 320 | .clientid = server->nfs4_state->cl_clientid, |
| 321 | .name = &dentry->d_name, |
| 322 | .id = sp->so_id, |
| 323 | .server = server, |
| 324 | .bitmask = server->attr_bitmask, |
| 325 | .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR, |
| 326 | }; |
| 327 | struct nfs_openres res = { |
| 328 | .server = server, |
| 329 | }; |
| 330 | struct rpc_message msg = { |
| 331 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR], |
| 332 | .rpc_argp = &arg, |
| 333 | .rpc_resp = &res, |
| 334 | .rpc_cred = sp->so_cred, |
| 335 | }; |
| 336 | int status = 0; |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) |
| 339 | goto out; |
| 340 | if (state->state == 0) |
| 341 | goto out; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 342 | arg.seqid = nfs_alloc_seqid(&sp->so_seqid); |
| 343 | status = -ENOMEM; |
| 344 | if (arg.seqid == NULL) |
| 345 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | arg.open_flags = state->state; |
| 347 | memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data)); |
| 348 | status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 349 | nfs_increment_open_seqid(status, arg.seqid); |
Trond Myklebust | 0a8838f | 2005-10-18 14:20:14 -0700 | [diff] [blame] | 350 | if (status != 0) |
| 351 | goto out_free; |
| 352 | if(res.rflags & NFS4_OPEN_RESULT_CONFIRM) { |
| 353 | status = _nfs4_proc_open_confirm(server->client, NFS_FH(inode), |
| 354 | sp, &res.stateid, arg.seqid); |
| 355 | if (status != 0) |
| 356 | goto out_free; |
| 357 | } |
| 358 | nfs_confirm_seqid(&sp->so_seqid, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | if (status >= 0) { |
| 360 | memcpy(state->stateid.data, res.stateid.data, |
| 361 | sizeof(state->stateid.data)); |
| 362 | clear_bit(NFS_DELEGATED_STATE, &state->flags); |
| 363 | } |
Trond Myklebust | 0a8838f | 2005-10-18 14:20:14 -0700 | [diff] [blame] | 364 | out_free: |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 365 | nfs_free_seqid(arg.seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | dput(parent); |
| 368 | return status; |
| 369 | } |
| 370 | |
| 371 | int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state) |
| 372 | { |
| 373 | struct nfs4_exception exception = { }; |
| 374 | struct nfs_server *server = NFS_SERVER(dentry->d_inode); |
| 375 | int err; |
| 376 | do { |
| 377 | err = _nfs4_open_delegation_recall(dentry, state); |
| 378 | switch (err) { |
| 379 | case 0: |
| 380 | return err; |
| 381 | case -NFS4ERR_STALE_CLIENTID: |
| 382 | case -NFS4ERR_STALE_STATEID: |
| 383 | case -NFS4ERR_EXPIRED: |
| 384 | /* Don't recall a delegation if it was lost */ |
| 385 | nfs4_schedule_state_recovery(server->nfs4_state); |
| 386 | return err; |
| 387 | } |
| 388 | err = nfs4_handle_exception(server, err, &exception); |
| 389 | } while (exception.retry); |
| 390 | return err; |
| 391 | } |
| 392 | |
Trond Myklebust | 0a8838f | 2005-10-18 14:20:14 -0700 | [diff] [blame] | 393 | static int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid, struct nfs_seqid *seqid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
| 395 | struct nfs_open_confirmargs arg = { |
| 396 | .fh = fh, |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 397 | .seqid = seqid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | .stateid = *stateid, |
| 399 | }; |
| 400 | struct nfs_open_confirmres res; |
| 401 | struct rpc_message msg = { |
| 402 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM], |
| 403 | .rpc_argp = &arg, |
| 404 | .rpc_resp = &res, |
| 405 | .rpc_cred = sp->so_cred, |
| 406 | }; |
| 407 | int status; |
| 408 | |
| 409 | status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 410 | /* Confirm the sequence as being established */ |
| 411 | nfs_confirm_seqid(&sp->so_seqid, status); |
| 412 | nfs_increment_open_seqid(status, seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | if (status >= 0) |
| 414 | memcpy(stateid, &res.stateid, sizeof(*stateid)); |
| 415 | return status; |
| 416 | } |
| 417 | |
| 418 | static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res) |
| 419 | { |
| 420 | struct nfs_server *server = NFS_SERVER(dir); |
| 421 | struct rpc_message msg = { |
| 422 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN], |
| 423 | .rpc_argp = o_arg, |
| 424 | .rpc_resp = o_res, |
| 425 | .rpc_cred = sp->so_cred, |
| 426 | }; |
| 427 | int status; |
| 428 | |
| 429 | /* Update sequence id. The caller must serialize! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | o_arg->id = sp->so_id; |
| 431 | o_arg->clientid = sp->so_client->cl_clientid; |
| 432 | |
| 433 | status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR); |
Trond Myklebust | 6f926b5 | 2005-10-18 14:20:18 -0700 | [diff] [blame] | 434 | if (status == 0) { |
| 435 | /* OPEN on anything except a regular file is disallowed in NFSv4 */ |
| 436 | switch (o_res->f_attr->mode & S_IFMT) { |
| 437 | case S_IFREG: |
| 438 | break; |
| 439 | case S_IFLNK: |
| 440 | status = -ELOOP; |
| 441 | break; |
| 442 | case S_IFDIR: |
| 443 | status = -EISDIR; |
| 444 | break; |
| 445 | default: |
| 446 | status = -ENOTDIR; |
| 447 | } |
| 448 | } |
| 449 | |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 450 | nfs_increment_open_seqid(status, o_arg->seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | if (status != 0) |
| 452 | goto out; |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 453 | if (o_arg->open_flags & O_CREAT) { |
| 454 | update_changeattr(dir, &o_res->cinfo); |
| 455 | nfs_post_op_update_inode(dir, o_res->dir_attr); |
| 456 | } else |
| 457 | nfs_refresh_inode(dir, o_res->dir_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { |
| 459 | status = _nfs4_proc_open_confirm(server->client, &o_res->fh, |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 460 | sp, &o_res->stateid, o_arg->seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | if (status != 0) |
| 462 | goto out; |
| 463 | } |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 464 | nfs_confirm_seqid(&sp->so_seqid, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) |
| 466 | status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr); |
| 467 | out: |
| 468 | return status; |
| 469 | } |
| 470 | |
| 471 | static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags) |
| 472 | { |
| 473 | struct nfs_access_entry cache; |
| 474 | int mask = 0; |
| 475 | int status; |
| 476 | |
| 477 | if (openflags & FMODE_READ) |
| 478 | mask |= MAY_READ; |
| 479 | if (openflags & FMODE_WRITE) |
| 480 | mask |= MAY_WRITE; |
| 481 | status = nfs_access_get_cached(inode, cred, &cache); |
| 482 | if (status == 0) |
| 483 | goto out; |
| 484 | |
| 485 | /* Be clever: ask server to check for all possible rights */ |
| 486 | cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; |
| 487 | cache.cred = cred; |
| 488 | cache.jiffies = jiffies; |
| 489 | status = _nfs4_proc_access(inode, &cache); |
| 490 | if (status != 0) |
| 491 | return status; |
| 492 | nfs_access_add_cache(inode, &cache); |
| 493 | out: |
| 494 | if ((cache.mask & mask) == mask) |
| 495 | return 0; |
| 496 | return -EACCES; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * OPEN_EXPIRED: |
| 501 | * reclaim state on the server after a network partition. |
| 502 | * Assumes caller holds the appropriate lock |
| 503 | */ |
| 504 | static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry) |
| 505 | { |
| 506 | struct dentry *parent = dget_parent(dentry); |
| 507 | struct inode *dir = parent->d_inode; |
| 508 | struct inode *inode = state->inode; |
| 509 | struct nfs_server *server = NFS_SERVER(dir); |
| 510 | struct nfs_delegation *delegation = NFS_I(inode)->delegation; |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 511 | struct nfs_fattr f_attr, dir_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | struct nfs_openargs o_arg = { |
| 513 | .fh = NFS_FH(dir), |
| 514 | .open_flags = state->state, |
| 515 | .name = &dentry->d_name, |
| 516 | .bitmask = server->attr_bitmask, |
| 517 | .claim = NFS4_OPEN_CLAIM_NULL, |
| 518 | }; |
| 519 | struct nfs_openres o_res = { |
| 520 | .f_attr = &f_attr, |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 521 | .dir_attr = &dir_attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | .server = server, |
| 523 | }; |
| 524 | int status = 0; |
| 525 | |
| 526 | if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) { |
| 527 | status = _nfs4_do_access(inode, sp->so_cred, state->state); |
| 528 | if (status < 0) |
| 529 | goto out; |
| 530 | memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid)); |
| 531 | set_bit(NFS_DELEGATED_STATE, &state->flags); |
| 532 | goto out; |
| 533 | } |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 534 | o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid); |
| 535 | status = -ENOMEM; |
| 536 | if (o_arg.seqid == NULL) |
| 537 | goto out; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 538 | nfs_fattr_init(&f_attr); |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 539 | nfs_fattr_init(&dir_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | status = _nfs4_proc_open(dir, sp, &o_arg, &o_res); |
| 541 | if (status != 0) |
| 542 | goto out_nodeleg; |
| 543 | /* Check if files differ */ |
| 544 | if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT)) |
| 545 | goto out_stale; |
| 546 | /* Has the file handle changed? */ |
| 547 | if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) { |
| 548 | /* Verify if the change attributes are the same */ |
| 549 | if (f_attr.change_attr != NFS_I(inode)->change_attr) |
| 550 | goto out_stale; |
| 551 | if (nfs_size_to_loff_t(f_attr.size) != inode->i_size) |
| 552 | goto out_stale; |
| 553 | /* Lets just pretend that this is the same file */ |
| 554 | nfs_copy_fh(NFS_FH(inode), &o_res.fh); |
| 555 | NFS_I(inode)->fileid = f_attr.fileid; |
| 556 | } |
| 557 | memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid)); |
| 558 | if (o_res.delegation_type != 0) { |
| 559 | if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) |
| 560 | nfs_inode_set_delegation(inode, sp->so_cred, &o_res); |
| 561 | else |
| 562 | nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res); |
| 563 | } |
| 564 | out_nodeleg: |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 565 | nfs_free_seqid(o_arg.seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | clear_bit(NFS_DELEGATED_STATE, &state->flags); |
| 567 | out: |
| 568 | dput(parent); |
| 569 | return status; |
| 570 | out_stale: |
| 571 | status = -ESTALE; |
| 572 | /* Invalidate the state owner so we don't ever use it again */ |
| 573 | nfs4_drop_state_owner(sp); |
| 574 | d_drop(dentry); |
| 575 | /* Should we be trying to close that stateid? */ |
| 576 | goto out_nodeleg; |
| 577 | } |
| 578 | |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 579 | static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry) |
| 580 | { |
| 581 | struct nfs_server *server = NFS_SERVER(dentry->d_inode); |
| 582 | struct nfs4_exception exception = { }; |
| 583 | int err; |
| 584 | |
| 585 | do { |
| 586 | err = _nfs4_open_expired(sp, state, dentry); |
| 587 | if (err == -NFS4ERR_DELAY) |
| 588 | nfs4_handle_exception(server, err, &exception); |
| 589 | } while (exception.retry); |
| 590 | return err; |
| 591 | } |
| 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) |
| 594 | { |
| 595 | struct nfs_inode *nfsi = NFS_I(state->inode); |
| 596 | struct nfs_open_context *ctx; |
| 597 | int status; |
| 598 | |
| 599 | spin_lock(&state->inode->i_lock); |
| 600 | list_for_each_entry(ctx, &nfsi->open_files, list) { |
| 601 | if (ctx->state != state) |
| 602 | continue; |
| 603 | get_nfs_open_context(ctx); |
| 604 | spin_unlock(&state->inode->i_lock); |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 605 | status = nfs4_do_open_expired(sp, state, ctx->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | put_nfs_open_context(ctx); |
| 607 | return status; |
| 608 | } |
| 609 | spin_unlock(&state->inode->i_lock); |
| 610 | return -ENOENT; |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * Returns an nfs4_state + an extra reference to the inode |
| 615 | */ |
| 616 | static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res) |
| 617 | { |
| 618 | struct nfs_delegation *delegation; |
| 619 | struct nfs_server *server = NFS_SERVER(inode); |
| 620 | struct nfs4_client *clp = server->nfs4_state; |
| 621 | struct nfs_inode *nfsi = NFS_I(inode); |
| 622 | struct nfs4_state_owner *sp = NULL; |
| 623 | struct nfs4_state *state = NULL; |
| 624 | int open_flags = flags & (FMODE_READ|FMODE_WRITE); |
| 625 | int err; |
| 626 | |
| 627 | /* Protect against reboot recovery - NOTE ORDER! */ |
| 628 | down_read(&clp->cl_sem); |
| 629 | /* Protect against delegation recall */ |
| 630 | down_read(&nfsi->rwsem); |
| 631 | delegation = NFS_I(inode)->delegation; |
| 632 | err = -ENOENT; |
| 633 | if (delegation == NULL || (delegation->type & open_flags) != open_flags) |
| 634 | goto out_err; |
| 635 | err = -ENOMEM; |
| 636 | if (!(sp = nfs4_get_state_owner(server, cred))) { |
| 637 | dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__); |
| 638 | goto out_err; |
| 639 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | state = nfs4_get_open_state(inode, sp); |
| 641 | if (state == NULL) |
| 642 | goto out_err; |
| 643 | |
| 644 | err = -ENOENT; |
| 645 | if ((state->state & open_flags) == open_flags) { |
| 646 | spin_lock(&inode->i_lock); |
| 647 | if (open_flags & FMODE_READ) |
| 648 | state->nreaders++; |
| 649 | if (open_flags & FMODE_WRITE) |
| 650 | state->nwriters++; |
| 651 | spin_unlock(&inode->i_lock); |
| 652 | goto out_ok; |
| 653 | } else if (state->state != 0) |
| 654 | goto out_err; |
| 655 | |
| 656 | lock_kernel(); |
| 657 | err = _nfs4_do_access(inode, cred, open_flags); |
| 658 | unlock_kernel(); |
| 659 | if (err != 0) |
| 660 | goto out_err; |
| 661 | set_bit(NFS_DELEGATED_STATE, &state->flags); |
| 662 | update_open_stateid(state, &delegation->stateid, open_flags); |
| 663 | out_ok: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | nfs4_put_state_owner(sp); |
| 665 | up_read(&nfsi->rwsem); |
| 666 | up_read(&clp->cl_sem); |
| 667 | igrab(inode); |
| 668 | *res = state; |
| 669 | return 0; |
| 670 | out_err: |
| 671 | if (sp != NULL) { |
| 672 | if (state != NULL) |
| 673 | nfs4_put_open_state(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | nfs4_put_state_owner(sp); |
| 675 | } |
| 676 | up_read(&nfsi->rwsem); |
| 677 | up_read(&clp->cl_sem); |
Trond Myklebust | b8e5c4c | 2005-10-18 14:20:20 -0700 | [diff] [blame] | 678 | if (err != -EACCES) |
| 679 | nfs_inode_return_delegation(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | return err; |
| 681 | } |
| 682 | |
| 683 | static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred) |
| 684 | { |
| 685 | struct nfs4_exception exception = { }; |
| 686 | struct nfs4_state *res; |
| 687 | int err; |
| 688 | |
| 689 | do { |
| 690 | err = _nfs4_open_delegated(inode, flags, cred, &res); |
| 691 | if (err == 0) |
| 692 | break; |
| 693 | res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode), |
| 694 | err, &exception)); |
| 695 | } while (exception.retry); |
| 696 | return res; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * Returns an nfs4_state + an referenced inode |
| 701 | */ |
| 702 | static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res) |
| 703 | { |
| 704 | struct nfs4_state_owner *sp; |
| 705 | struct nfs4_state *state = NULL; |
| 706 | struct nfs_server *server = NFS_SERVER(dir); |
| 707 | struct nfs4_client *clp = server->nfs4_state; |
| 708 | struct inode *inode = NULL; |
| 709 | int status; |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 710 | struct nfs_fattr f_attr, dir_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | struct nfs_openargs o_arg = { |
| 712 | .fh = NFS_FH(dir), |
| 713 | .open_flags = flags, |
| 714 | .name = &dentry->d_name, |
| 715 | .server = server, |
| 716 | .bitmask = server->attr_bitmask, |
| 717 | .claim = NFS4_OPEN_CLAIM_NULL, |
| 718 | }; |
| 719 | struct nfs_openres o_res = { |
| 720 | .f_attr = &f_attr, |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 721 | .dir_attr = &dir_attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | .server = server, |
| 723 | }; |
| 724 | |
| 725 | /* Protect against reboot recovery conflicts */ |
| 726 | down_read(&clp->cl_sem); |
| 727 | status = -ENOMEM; |
| 728 | if (!(sp = nfs4_get_state_owner(server, cred))) { |
| 729 | dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n"); |
| 730 | goto out_err; |
| 731 | } |
| 732 | if (flags & O_EXCL) { |
| 733 | u32 *p = (u32 *) o_arg.u.verifier.data; |
| 734 | p[0] = jiffies; |
| 735 | p[1] = current->pid; |
| 736 | } else |
| 737 | o_arg.u.attrs = sattr; |
| 738 | /* Serialization for the sequence id */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 740 | o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid); |
| 741 | if (o_arg.seqid == NULL) |
| 742 | return -ENOMEM; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 743 | nfs_fattr_init(&f_attr); |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 744 | nfs_fattr_init(&dir_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | status = _nfs4_proc_open(dir, sp, &o_arg, &o_res); |
| 746 | if (status != 0) |
| 747 | goto out_err; |
| 748 | |
| 749 | status = -ENOMEM; |
| 750 | inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr); |
| 751 | if (!inode) |
| 752 | goto out_err; |
| 753 | state = nfs4_get_open_state(inode, sp); |
| 754 | if (!state) |
| 755 | goto out_err; |
| 756 | update_open_stateid(state, &o_res.stateid, flags); |
| 757 | if (o_res.delegation_type != 0) |
| 758 | nfs_inode_set_delegation(inode, cred, &o_res); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 759 | nfs_free_seqid(o_arg.seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | nfs4_put_state_owner(sp); |
| 761 | up_read(&clp->cl_sem); |
| 762 | *res = state; |
| 763 | return 0; |
| 764 | out_err: |
| 765 | if (sp != NULL) { |
| 766 | if (state != NULL) |
| 767 | nfs4_put_open_state(state); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 768 | nfs_free_seqid(o_arg.seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | nfs4_put_state_owner(sp); |
| 770 | } |
| 771 | /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */ |
| 772 | up_read(&clp->cl_sem); |
| 773 | if (inode != NULL) |
| 774 | iput(inode); |
| 775 | *res = NULL; |
| 776 | return status; |
| 777 | } |
| 778 | |
| 779 | |
| 780 | static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred) |
| 781 | { |
| 782 | struct nfs4_exception exception = { }; |
| 783 | struct nfs4_state *res; |
| 784 | int status; |
| 785 | |
| 786 | do { |
| 787 | status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res); |
| 788 | if (status == 0) |
| 789 | break; |
| 790 | /* NOTE: BAD_SEQID means the server and client disagree about the |
| 791 | * book-keeping w.r.t. state-changing operations |
| 792 | * (OPEN/CLOSE/LOCK/LOCKU...) |
| 793 | * It is actually a sign of a bug on the client or on the server. |
| 794 | * |
| 795 | * If we receive a BAD_SEQID error in the particular case of |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 796 | * doing an OPEN, we assume that nfs_increment_open_seqid() will |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | * have unhashed the old state_owner for us, and that we can |
| 798 | * therefore safely retry using a new one. We should still warn |
| 799 | * the user though... |
| 800 | */ |
| 801 | if (status == -NFS4ERR_BAD_SEQID) { |
| 802 | printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n"); |
| 803 | exception.retry = 1; |
| 804 | continue; |
| 805 | } |
Trond Myklebust | 550f574 | 2005-10-18 14:20:21 -0700 | [diff] [blame] | 806 | /* |
| 807 | * BAD_STATEID on OPEN means that the server cancelled our |
| 808 | * state before it received the OPEN_CONFIRM. |
| 809 | * Recover by retrying the request as per the discussion |
| 810 | * on Page 181 of RFC3530. |
| 811 | */ |
| 812 | if (status == -NFS4ERR_BAD_STATEID) { |
| 813 | exception.retry = 1; |
| 814 | continue; |
| 815 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir), |
| 817 | status, &exception)); |
| 818 | } while (exception.retry); |
| 819 | return res; |
| 820 | } |
| 821 | |
| 822 | static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr, |
| 823 | struct nfs_fh *fhandle, struct iattr *sattr, |
| 824 | struct nfs4_state *state) |
| 825 | { |
| 826 | struct nfs_setattrargs arg = { |
| 827 | .fh = fhandle, |
| 828 | .iap = sattr, |
| 829 | .server = server, |
| 830 | .bitmask = server->attr_bitmask, |
| 831 | }; |
| 832 | struct nfs_setattrres res = { |
| 833 | .fattr = fattr, |
| 834 | .server = server, |
| 835 | }; |
| 836 | struct rpc_message msg = { |
| 837 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], |
| 838 | .rpc_argp = &arg, |
| 839 | .rpc_resp = &res, |
| 840 | }; |
Trond Myklebust | 65e4308 | 2005-08-16 11:49:44 -0400 | [diff] [blame] | 841 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 843 | nfs_fattr_init(fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
Trond Myklebust | 08e9eac | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 845 | if (state != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | msg.rpc_cred = state->owner->so_cred; |
Trond Myklebust | 08e9eac | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 847 | nfs4_copy_stateid(&arg.stateid, state, current->files); |
| 848 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid)); |
| 850 | |
Trond Myklebust | 65e4308 | 2005-08-16 11:49:44 -0400 | [diff] [blame] | 851 | status = rpc_call_sync(server->client, &msg, 0); |
| 852 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr, |
| 856 | struct nfs_fh *fhandle, struct iattr *sattr, |
| 857 | struct nfs4_state *state) |
| 858 | { |
| 859 | struct nfs4_exception exception = { }; |
| 860 | int err; |
| 861 | do { |
| 862 | err = nfs4_handle_exception(server, |
| 863 | _nfs4_do_setattr(server, fattr, fhandle, sattr, |
| 864 | state), |
| 865 | &exception); |
| 866 | } while (exception.retry); |
| 867 | return err; |
| 868 | } |
| 869 | |
| 870 | struct nfs4_closedata { |
| 871 | struct inode *inode; |
| 872 | struct nfs4_state *state; |
| 873 | struct nfs_closeargs arg; |
| 874 | struct nfs_closeres res; |
Trond Myklebust | 516a6af | 2005-10-27 22:12:41 -0400 | [diff] [blame] | 875 | struct nfs_fattr fattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | }; |
| 877 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 878 | static void nfs4_free_closedata(void *data) |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 879 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 880 | struct nfs4_closedata *calldata = data; |
| 881 | struct nfs4_state_owner *sp = calldata->state->owner; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 882 | |
| 883 | nfs4_put_open_state(calldata->state); |
| 884 | nfs_free_seqid(calldata->arg.seqid); |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 885 | nfs4_put_state_owner(sp); |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 886 | kfree(calldata); |
| 887 | } |
| 888 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 889 | static void nfs4_close_done(struct rpc_task *task, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 891 | struct nfs4_closedata *calldata = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | struct nfs4_state *state = calldata->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | struct nfs_server *server = NFS_SERVER(calldata->inode); |
| 894 | |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 895 | if (RPC_ASSASSINATED(task)) |
| 896 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | /* hmm. we are done with the inode, and in the process of freeing |
| 898 | * the state_owner. we keep this around to process errors |
| 899 | */ |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 900 | nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | switch (task->tk_status) { |
| 902 | case 0: |
| 903 | memcpy(&state->stateid, &calldata->res.stateid, |
| 904 | sizeof(state->stateid)); |
| 905 | break; |
| 906 | case -NFS4ERR_STALE_STATEID: |
| 907 | case -NFS4ERR_EXPIRED: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | nfs4_schedule_state_recovery(server->nfs4_state); |
| 909 | break; |
| 910 | default: |
| 911 | if (nfs4_async_handle_error(task, server) == -EAGAIN) { |
| 912 | rpc_restart_call(task); |
| 913 | return; |
| 914 | } |
| 915 | } |
Trond Myklebust | 516a6af | 2005-10-27 22:12:41 -0400 | [diff] [blame] | 916 | nfs_refresh_inode(calldata->inode, calldata->res.fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 919 | static void nfs4_close_prepare(struct rpc_task *task, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | { |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 921 | struct nfs4_closedata *calldata = data; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 922 | struct nfs4_state *state = calldata->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | struct rpc_message msg = { |
| 924 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE], |
| 925 | .rpc_argp = &calldata->arg, |
| 926 | .rpc_resp = &calldata->res, |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 927 | .rpc_cred = state->owner->so_cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | }; |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 929 | int mode = 0, old_mode; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 930 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 931 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 932 | return; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 933 | /* Recalculate the new open mode in case someone reopened the file |
| 934 | * while we were waiting in line to be scheduled. |
| 935 | */ |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 936 | spin_lock(&state->owner->so_lock); |
| 937 | spin_lock(&calldata->inode->i_lock); |
| 938 | mode = old_mode = state->state; |
| 939 | if (state->nreaders == 0) |
| 940 | mode &= ~FMODE_READ; |
| 941 | if (state->nwriters == 0) |
| 942 | mode &= ~FMODE_WRITE; |
| 943 | nfs4_state_set_mode_locked(state, mode); |
| 944 | spin_unlock(&calldata->inode->i_lock); |
| 945 | spin_unlock(&state->owner->so_lock); |
| 946 | if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 947 | /* Note: exit _without_ calling nfs4_close_done */ |
| 948 | task->tk_action = NULL; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 949 | return; |
| 950 | } |
Trond Myklebust | 516a6af | 2005-10-27 22:12:41 -0400 | [diff] [blame] | 951 | nfs_fattr_init(calldata->res.fattr); |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 952 | if (mode != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE]; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 954 | calldata->arg.open_flags = mode; |
| 955 | rpc_call_setup(task, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 958 | static const struct rpc_call_ops nfs4_close_ops = { |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 959 | .rpc_call_prepare = nfs4_close_prepare, |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 960 | .rpc_call_done = nfs4_close_done, |
| 961 | .rpc_release = nfs4_free_closedata, |
| 962 | }; |
| 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | /* |
| 965 | * It is possible for data to be read/written from a mem-mapped file |
| 966 | * after the sys_close call (which hits the vfs layer as a flush). |
| 967 | * This means that we can't safely call nfsv4 close on a file until |
| 968 | * the inode is cleared. This in turn means that we are not good |
| 969 | * NFSv4 citizens - we do not indicate to the server to update the file's |
| 970 | * share state even when we are done with one of the three share |
| 971 | * stateid's in the inode. |
| 972 | * |
| 973 | * NOTE: Caller must be holding the sp->so_owner semaphore! |
| 974 | */ |
Trond Myklebust | 4cecb76 | 2005-11-04 15:32:58 -0500 | [diff] [blame] | 975 | int nfs4_do_close(struct inode *inode, struct nfs4_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | { |
Trond Myklebust | 516a6af | 2005-10-27 22:12:41 -0400 | [diff] [blame] | 977 | struct nfs_server *server = NFS_SERVER(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | struct nfs4_closedata *calldata; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 979 | int status = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 981 | calldata = kmalloc(sizeof(*calldata), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | if (calldata == NULL) |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 983 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | calldata->inode = inode; |
| 985 | calldata->state = state; |
| 986 | calldata->arg.fh = NFS_FH(inode); |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 987 | calldata->arg.stateid = &state->stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | /* Serialization for the sequence id */ |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 989 | calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid); |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 990 | if (calldata->arg.seqid == NULL) |
| 991 | goto out_free_calldata; |
Trond Myklebust | 516a6af | 2005-10-27 22:12:41 -0400 | [diff] [blame] | 992 | calldata->arg.bitmask = server->attr_bitmask; |
| 993 | calldata->res.fattr = &calldata->fattr; |
| 994 | calldata->res.server = server; |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 995 | |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 996 | status = nfs4_call_async(server->client, &nfs4_close_ops, calldata); |
Trond Myklebust | 9512135 | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 997 | if (status == 0) |
| 998 | goto out; |
| 999 | |
| 1000 | nfs_free_seqid(calldata->arg.seqid); |
| 1001 | out_free_calldata: |
| 1002 | kfree(calldata); |
| 1003 | out: |
| 1004 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1007 | static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state) |
| 1008 | { |
| 1009 | struct file *filp; |
| 1010 | |
| 1011 | filp = lookup_instantiate_filp(nd, dentry, NULL); |
| 1012 | if (!IS_ERR(filp)) { |
| 1013 | struct nfs_open_context *ctx; |
| 1014 | ctx = (struct nfs_open_context *)filp->private_data; |
| 1015 | ctx->state = state; |
| 1016 | } else |
| 1017 | nfs4_close_state(state, nd->intent.open.flags); |
| 1018 | } |
| 1019 | |
| 1020 | struct dentry * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
| 1022 | { |
| 1023 | struct iattr attr; |
| 1024 | struct rpc_cred *cred; |
| 1025 | struct nfs4_state *state; |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1026 | struct dentry *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | if (nd->flags & LOOKUP_CREATE) { |
| 1029 | attr.ia_mode = nd->intent.open.create_mode; |
| 1030 | attr.ia_valid = ATTR_MODE; |
| 1031 | if (!IS_POSIXACL(dir)) |
| 1032 | attr.ia_mode &= ~current->fs->umask; |
| 1033 | } else { |
| 1034 | attr.ia_valid = 0; |
| 1035 | BUG_ON(nd->intent.open.flags & O_CREAT); |
| 1036 | } |
| 1037 | |
| 1038 | cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0); |
| 1039 | if (IS_ERR(cred)) |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1040 | return (struct dentry *)cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred); |
| 1042 | put_rpccred(cred); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1043 | if (IS_ERR(state)) { |
| 1044 | if (PTR_ERR(state) == -ENOENT) |
| 1045 | d_add(dentry, NULL); |
| 1046 | return (struct dentry *)state; |
| 1047 | } |
| 1048 | res = d_add_unique(dentry, state->inode); |
| 1049 | if (res != NULL) |
| 1050 | dentry = res; |
| 1051 | nfs4_intent_set_file(nd, dentry, state); |
| 1052 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | int |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1056 | 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] | 1057 | { |
| 1058 | struct rpc_cred *cred; |
| 1059 | struct nfs4_state *state; |
| 1060 | struct inode *inode; |
| 1061 | |
| 1062 | cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0); |
| 1063 | if (IS_ERR(cred)) |
| 1064 | return PTR_ERR(cred); |
| 1065 | state = nfs4_open_delegated(dentry->d_inode, openflags, cred); |
| 1066 | if (IS_ERR(state)) |
| 1067 | state = nfs4_do_open(dir, dentry, openflags, NULL, cred); |
| 1068 | put_rpccred(cred); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1069 | if (IS_ERR(state)) { |
| 1070 | switch (PTR_ERR(state)) { |
| 1071 | case -EPERM: |
| 1072 | case -EACCES: |
| 1073 | case -EDQUOT: |
| 1074 | case -ENOSPC: |
| 1075 | case -EROFS: |
| 1076 | lookup_instantiate_filp(nd, (struct dentry *)state, NULL); |
| 1077 | return 1; |
| 1078 | case -ENOENT: |
| 1079 | if (dentry->d_inode == NULL) |
| 1080 | return 1; |
| 1081 | } |
| 1082 | goto out_drop; |
| 1083 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | inode = state->inode; |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1085 | iput(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | if (inode == dentry->d_inode) { |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1087 | nfs4_intent_set_file(nd, dentry, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | return 1; |
| 1089 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | nfs4_close_state(state, openflags); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1091 | out_drop: |
| 1092 | d_drop(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
| 1098 | { |
| 1099 | struct nfs4_server_caps_res res = {}; |
| 1100 | struct rpc_message msg = { |
| 1101 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS], |
| 1102 | .rpc_argp = fhandle, |
| 1103 | .rpc_resp = &res, |
| 1104 | }; |
| 1105 | int status; |
| 1106 | |
| 1107 | status = rpc_call_sync(server->client, &msg, 0); |
| 1108 | if (status == 0) { |
| 1109 | memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask)); |
| 1110 | if (res.attr_bitmask[0] & FATTR4_WORD0_ACL) |
| 1111 | server->caps |= NFS_CAP_ACLS; |
| 1112 | if (res.has_links != 0) |
| 1113 | server->caps |= NFS_CAP_HARDLINKS; |
| 1114 | if (res.has_symlinks != 0) |
| 1115 | server->caps |= NFS_CAP_SYMLINKS; |
| 1116 | server->acl_bitmask = res.acl_bitmask; |
| 1117 | } |
| 1118 | return status; |
| 1119 | } |
| 1120 | |
| 1121 | static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
| 1122 | { |
| 1123 | struct nfs4_exception exception = { }; |
| 1124 | int err; |
| 1125 | do { |
| 1126 | err = nfs4_handle_exception(server, |
| 1127 | _nfs4_server_capabilities(server, fhandle), |
| 1128 | &exception); |
| 1129 | } while (exception.retry); |
| 1130 | return err; |
| 1131 | } |
| 1132 | |
| 1133 | static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, |
| 1134 | struct nfs_fsinfo *info) |
| 1135 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | struct nfs4_lookup_root_arg args = { |
| 1137 | .bitmask = nfs4_fattr_bitmap, |
| 1138 | }; |
| 1139 | struct nfs4_lookup_res res = { |
| 1140 | .server = server, |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1141 | .fattr = info->fattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | .fh = fhandle, |
| 1143 | }; |
| 1144 | struct rpc_message msg = { |
| 1145 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT], |
| 1146 | .rpc_argp = &args, |
| 1147 | .rpc_resp = &res, |
| 1148 | }; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1149 | nfs_fattr_init(info->fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | return rpc_call_sync(server->client, &msg, 0); |
| 1151 | } |
| 1152 | |
| 1153 | static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, |
| 1154 | struct nfs_fsinfo *info) |
| 1155 | { |
| 1156 | struct nfs4_exception exception = { }; |
| 1157 | int err; |
| 1158 | do { |
| 1159 | err = nfs4_handle_exception(server, |
| 1160 | _nfs4_lookup_root(server, fhandle, info), |
| 1161 | &exception); |
| 1162 | } while (exception.retry); |
| 1163 | return err; |
| 1164 | } |
| 1165 | |
| 1166 | static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle, |
| 1167 | struct nfs_fsinfo *info) |
| 1168 | { |
| 1169 | struct nfs_fattr * fattr = info->fattr; |
| 1170 | unsigned char * p; |
| 1171 | struct qstr q; |
| 1172 | struct nfs4_lookup_arg args = { |
| 1173 | .dir_fh = fhandle, |
| 1174 | .name = &q, |
| 1175 | .bitmask = nfs4_fattr_bitmap, |
| 1176 | }; |
| 1177 | struct nfs4_lookup_res res = { |
| 1178 | .server = server, |
| 1179 | .fattr = fattr, |
| 1180 | .fh = fhandle, |
| 1181 | }; |
| 1182 | struct rpc_message msg = { |
| 1183 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP], |
| 1184 | .rpc_argp = &args, |
| 1185 | .rpc_resp = &res, |
| 1186 | }; |
| 1187 | int status; |
| 1188 | |
| 1189 | /* |
| 1190 | * Now we do a separate LOOKUP for each component of the mount path. |
| 1191 | * The LOOKUPs are done separately so that we can conveniently |
| 1192 | * catch an ERR_WRONGSEC if it occurs along the way... |
| 1193 | */ |
| 1194 | status = nfs4_lookup_root(server, fhandle, info); |
| 1195 | if (status) |
| 1196 | goto out; |
| 1197 | |
| 1198 | p = server->mnt_path; |
| 1199 | for (;;) { |
| 1200 | struct nfs4_exception exception = { }; |
| 1201 | |
| 1202 | while (*p == '/') |
| 1203 | p++; |
| 1204 | if (!*p) |
| 1205 | break; |
| 1206 | q.name = p; |
| 1207 | while (*p && (*p != '/')) |
| 1208 | p++; |
| 1209 | q.len = p - q.name; |
| 1210 | |
| 1211 | do { |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1212 | nfs_fattr_init(fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | status = nfs4_handle_exception(server, |
| 1214 | rpc_call_sync(server->client, &msg, 0), |
| 1215 | &exception); |
| 1216 | } while (exception.retry); |
| 1217 | if (status == 0) |
| 1218 | continue; |
| 1219 | if (status == -ENOENT) { |
| 1220 | printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path); |
| 1221 | printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n"); |
| 1222 | } |
| 1223 | break; |
| 1224 | } |
| 1225 | if (status == 0) |
| 1226 | status = nfs4_server_capabilities(server, fhandle); |
| 1227 | if (status == 0) |
| 1228 | status = nfs4_do_fsinfo(server, fhandle, info); |
| 1229 | out: |
| 1230 | return status; |
| 1231 | } |
| 1232 | |
| 1233 | static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
| 1234 | { |
| 1235 | struct nfs4_getattr_arg args = { |
| 1236 | .fh = fhandle, |
| 1237 | .bitmask = server->attr_bitmask, |
| 1238 | }; |
| 1239 | struct nfs4_getattr_res res = { |
| 1240 | .fattr = fattr, |
| 1241 | .server = server, |
| 1242 | }; |
| 1243 | struct rpc_message msg = { |
| 1244 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], |
| 1245 | .rpc_argp = &args, |
| 1246 | .rpc_resp = &res, |
| 1247 | }; |
| 1248 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1249 | nfs_fattr_init(fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | return rpc_call_sync(server->client, &msg, 0); |
| 1251 | } |
| 1252 | |
| 1253 | static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
| 1254 | { |
| 1255 | struct nfs4_exception exception = { }; |
| 1256 | int err; |
| 1257 | do { |
| 1258 | err = nfs4_handle_exception(server, |
| 1259 | _nfs4_proc_getattr(server, fhandle, fattr), |
| 1260 | &exception); |
| 1261 | } while (exception.retry); |
| 1262 | return err; |
| 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * The file is not closed if it is opened due to the a request to change |
| 1267 | * the size of the file. The open call will not be needed once the |
| 1268 | * VFS layer lookup-intents are implemented. |
| 1269 | * |
| 1270 | * Close is called when the inode is destroyed. |
| 1271 | * If we haven't opened the file for O_WRONLY, we |
| 1272 | * need to in the size_change case to obtain a stateid. |
| 1273 | * |
| 1274 | * Got race? |
| 1275 | * Because OPEN is always done by name in nfsv4, it is |
| 1276 | * possible that we opened a different file by the same |
| 1277 | * name. We can recognize this race condition, but we |
| 1278 | * can't do anything about it besides returning an error. |
| 1279 | * |
| 1280 | * This will be fixed with VFS changes (lookup-intent). |
| 1281 | */ |
| 1282 | static int |
| 1283 | nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr, |
| 1284 | struct iattr *sattr) |
| 1285 | { |
Trond Myklebust | 08e9eac | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 1286 | struct rpc_cred *cred; |
| 1287 | struct inode *inode = dentry->d_inode; |
Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 1288 | struct nfs_open_context *ctx; |
| 1289 | struct nfs4_state *state = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | int status; |
| 1291 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1292 | nfs_fattr_init(fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | |
Trond Myklebust | 08e9eac | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 1294 | cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0); |
| 1295 | if (IS_ERR(cred)) |
| 1296 | return PTR_ERR(cred); |
Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 1297 | |
| 1298 | /* Search for an existing open(O_WRITE) file */ |
| 1299 | ctx = nfs_find_open_context(inode, cred, FMODE_WRITE); |
| 1300 | if (ctx != NULL) |
| 1301 | state = ctx->state; |
Trond Myklebust | 08e9eac | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 1302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | status = nfs4_do_setattr(NFS_SERVER(inode), fattr, |
| 1304 | NFS_FH(inode), sattr, state); |
Trond Myklebust | 65e4308 | 2005-08-16 11:49:44 -0400 | [diff] [blame] | 1305 | if (status == 0) |
| 1306 | nfs_setattr_update_inode(inode, sattr); |
Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 1307 | if (ctx != NULL) |
| 1308 | put_nfs_open_context(ctx); |
Trond Myklebust | 08e9eac | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 1309 | put_rpccred(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | return status; |
| 1311 | } |
| 1312 | |
| 1313 | static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name, |
| 1314 | struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
| 1315 | { |
| 1316 | int status; |
| 1317 | struct nfs_server *server = NFS_SERVER(dir); |
| 1318 | struct nfs4_lookup_arg args = { |
| 1319 | .bitmask = server->attr_bitmask, |
| 1320 | .dir_fh = NFS_FH(dir), |
| 1321 | .name = name, |
| 1322 | }; |
| 1323 | struct nfs4_lookup_res res = { |
| 1324 | .server = server, |
| 1325 | .fattr = fattr, |
| 1326 | .fh = fhandle, |
| 1327 | }; |
| 1328 | struct rpc_message msg = { |
| 1329 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP], |
| 1330 | .rpc_argp = &args, |
| 1331 | .rpc_resp = &res, |
| 1332 | }; |
| 1333 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1334 | nfs_fattr_init(fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
| 1336 | dprintk("NFS call lookup %s\n", name->name); |
| 1337 | status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); |
| 1338 | dprintk("NFS reply lookup: %d\n", status); |
| 1339 | return status; |
| 1340 | } |
| 1341 | |
| 1342 | static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
| 1343 | { |
| 1344 | struct nfs4_exception exception = { }; |
| 1345 | int err; |
| 1346 | do { |
| 1347 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 1348 | _nfs4_proc_lookup(dir, name, fhandle, fattr), |
| 1349 | &exception); |
| 1350 | } while (exception.retry); |
| 1351 | return err; |
| 1352 | } |
| 1353 | |
| 1354 | static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) |
| 1355 | { |
| 1356 | struct nfs4_accessargs args = { |
| 1357 | .fh = NFS_FH(inode), |
| 1358 | }; |
| 1359 | struct nfs4_accessres res = { 0 }; |
| 1360 | struct rpc_message msg = { |
| 1361 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS], |
| 1362 | .rpc_argp = &args, |
| 1363 | .rpc_resp = &res, |
| 1364 | .rpc_cred = entry->cred, |
| 1365 | }; |
| 1366 | int mode = entry->mask; |
| 1367 | int status; |
| 1368 | |
| 1369 | /* |
| 1370 | * Determine which access bits we want to ask for... |
| 1371 | */ |
| 1372 | if (mode & MAY_READ) |
| 1373 | args.access |= NFS4_ACCESS_READ; |
| 1374 | if (S_ISDIR(inode->i_mode)) { |
| 1375 | if (mode & MAY_WRITE) |
| 1376 | args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE; |
| 1377 | if (mode & MAY_EXEC) |
| 1378 | args.access |= NFS4_ACCESS_LOOKUP; |
| 1379 | } else { |
| 1380 | if (mode & MAY_WRITE) |
| 1381 | args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND; |
| 1382 | if (mode & MAY_EXEC) |
| 1383 | args.access |= NFS4_ACCESS_EXECUTE; |
| 1384 | } |
| 1385 | status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0); |
| 1386 | if (!status) { |
| 1387 | entry->mask = 0; |
| 1388 | if (res.access & NFS4_ACCESS_READ) |
| 1389 | entry->mask |= MAY_READ; |
| 1390 | if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE)) |
| 1391 | entry->mask |= MAY_WRITE; |
| 1392 | if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE)) |
| 1393 | entry->mask |= MAY_EXEC; |
| 1394 | } |
| 1395 | return status; |
| 1396 | } |
| 1397 | |
| 1398 | static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) |
| 1399 | { |
| 1400 | struct nfs4_exception exception = { }; |
| 1401 | int err; |
| 1402 | do { |
| 1403 | err = nfs4_handle_exception(NFS_SERVER(inode), |
| 1404 | _nfs4_proc_access(inode, entry), |
| 1405 | &exception); |
| 1406 | } while (exception.retry); |
| 1407 | return err; |
| 1408 | } |
| 1409 | |
| 1410 | /* |
| 1411 | * TODO: For the time being, we don't try to get any attributes |
| 1412 | * along with any of the zero-copy operations READ, READDIR, |
| 1413 | * READLINK, WRITE. |
| 1414 | * |
| 1415 | * In the case of the first three, we want to put the GETATTR |
| 1416 | * after the read-type operation -- this is because it is hard |
| 1417 | * to predict the length of a GETATTR response in v4, and thus |
| 1418 | * align the READ data correctly. This means that the GETATTR |
| 1419 | * may end up partially falling into the page cache, and we should |
| 1420 | * shift it into the 'tail' of the xdr_buf before processing. |
| 1421 | * To do this efficiently, we need to know the total length |
| 1422 | * of data received, which doesn't seem to be available outside |
| 1423 | * of the RPC layer. |
| 1424 | * |
| 1425 | * In the case of WRITE, we also want to put the GETATTR after |
| 1426 | * the operation -- in this case because we want to make sure |
| 1427 | * we get the post-operation mtime and size. This means that |
| 1428 | * we can't use xdr_encode_pages() as written: we need a variant |
| 1429 | * of it which would leave room in the 'tail' iovec. |
| 1430 | * |
| 1431 | * Both of these changes to the XDR layer would in fact be quite |
| 1432 | * minor, but I decided to leave them for a subsequent patch. |
| 1433 | */ |
| 1434 | static int _nfs4_proc_readlink(struct inode *inode, struct page *page, |
| 1435 | unsigned int pgbase, unsigned int pglen) |
| 1436 | { |
| 1437 | struct nfs4_readlink args = { |
| 1438 | .fh = NFS_FH(inode), |
| 1439 | .pgbase = pgbase, |
| 1440 | .pglen = pglen, |
| 1441 | .pages = &page, |
| 1442 | }; |
| 1443 | struct rpc_message msg = { |
| 1444 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK], |
| 1445 | .rpc_argp = &args, |
| 1446 | .rpc_resp = NULL, |
| 1447 | }; |
| 1448 | |
| 1449 | return rpc_call_sync(NFS_CLIENT(inode), &msg, 0); |
| 1450 | } |
| 1451 | |
| 1452 | static int nfs4_proc_readlink(struct inode *inode, struct page *page, |
| 1453 | unsigned int pgbase, unsigned int pglen) |
| 1454 | { |
| 1455 | struct nfs4_exception exception = { }; |
| 1456 | int err; |
| 1457 | do { |
| 1458 | err = nfs4_handle_exception(NFS_SERVER(inode), |
| 1459 | _nfs4_proc_readlink(inode, page, pgbase, pglen), |
| 1460 | &exception); |
| 1461 | } while (exception.retry); |
| 1462 | return err; |
| 1463 | } |
| 1464 | |
| 1465 | static int _nfs4_proc_read(struct nfs_read_data *rdata) |
| 1466 | { |
| 1467 | int flags = rdata->flags; |
| 1468 | struct inode *inode = rdata->inode; |
| 1469 | struct nfs_fattr *fattr = rdata->res.fattr; |
| 1470 | struct nfs_server *server = NFS_SERVER(inode); |
| 1471 | struct rpc_message msg = { |
| 1472 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ], |
| 1473 | .rpc_argp = &rdata->args, |
| 1474 | .rpc_resp = &rdata->res, |
| 1475 | .rpc_cred = rdata->cred, |
| 1476 | }; |
| 1477 | unsigned long timestamp = jiffies; |
| 1478 | int status; |
| 1479 | |
| 1480 | dprintk("NFS call read %d @ %Ld\n", rdata->args.count, |
| 1481 | (long long) rdata->args.offset); |
| 1482 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1483 | nfs_fattr_init(fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | status = rpc_call_sync(server->client, &msg, flags); |
| 1485 | if (!status) |
| 1486 | renew_lease(server, timestamp); |
| 1487 | dprintk("NFS reply read: %d\n", status); |
| 1488 | return status; |
| 1489 | } |
| 1490 | |
| 1491 | static int nfs4_proc_read(struct nfs_read_data *rdata) |
| 1492 | { |
| 1493 | struct nfs4_exception exception = { }; |
| 1494 | int err; |
| 1495 | do { |
| 1496 | err = nfs4_handle_exception(NFS_SERVER(rdata->inode), |
| 1497 | _nfs4_proc_read(rdata), |
| 1498 | &exception); |
| 1499 | } while (exception.retry); |
| 1500 | return err; |
| 1501 | } |
| 1502 | |
| 1503 | static int _nfs4_proc_write(struct nfs_write_data *wdata) |
| 1504 | { |
| 1505 | int rpcflags = wdata->flags; |
| 1506 | struct inode *inode = wdata->inode; |
| 1507 | struct nfs_fattr *fattr = wdata->res.fattr; |
| 1508 | struct nfs_server *server = NFS_SERVER(inode); |
| 1509 | struct rpc_message msg = { |
| 1510 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE], |
| 1511 | .rpc_argp = &wdata->args, |
| 1512 | .rpc_resp = &wdata->res, |
| 1513 | .rpc_cred = wdata->cred, |
| 1514 | }; |
| 1515 | int status; |
| 1516 | |
| 1517 | dprintk("NFS call write %d @ %Ld\n", wdata->args.count, |
| 1518 | (long long) wdata->args.offset); |
| 1519 | |
Trond Myklebust | 3b6efee | 2005-12-03 15:20:21 -0500 | [diff] [blame] | 1520 | wdata->args.bitmask = server->attr_bitmask; |
| 1521 | wdata->res.server = server; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1522 | nfs_fattr_init(fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | status = rpc_call_sync(server->client, &msg, rpcflags); |
| 1524 | dprintk("NFS reply write: %d\n", status); |
Trond Myklebust | 3b6efee | 2005-12-03 15:20:21 -0500 | [diff] [blame] | 1525 | if (status < 0) |
| 1526 | return status; |
| 1527 | nfs_post_op_update_inode(inode, fattr); |
| 1528 | return wdata->res.count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | static int nfs4_proc_write(struct nfs_write_data *wdata) |
| 1532 | { |
| 1533 | struct nfs4_exception exception = { }; |
| 1534 | int err; |
| 1535 | do { |
| 1536 | err = nfs4_handle_exception(NFS_SERVER(wdata->inode), |
| 1537 | _nfs4_proc_write(wdata), |
| 1538 | &exception); |
| 1539 | } while (exception.retry); |
| 1540 | return err; |
| 1541 | } |
| 1542 | |
| 1543 | static int _nfs4_proc_commit(struct nfs_write_data *cdata) |
| 1544 | { |
| 1545 | struct inode *inode = cdata->inode; |
| 1546 | struct nfs_fattr *fattr = cdata->res.fattr; |
| 1547 | struct nfs_server *server = NFS_SERVER(inode); |
| 1548 | struct rpc_message msg = { |
| 1549 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT], |
| 1550 | .rpc_argp = &cdata->args, |
| 1551 | .rpc_resp = &cdata->res, |
| 1552 | .rpc_cred = cdata->cred, |
| 1553 | }; |
| 1554 | int status; |
| 1555 | |
| 1556 | dprintk("NFS call commit %d @ %Ld\n", cdata->args.count, |
| 1557 | (long long) cdata->args.offset); |
| 1558 | |
Trond Myklebust | 3b6efee | 2005-12-03 15:20:21 -0500 | [diff] [blame] | 1559 | cdata->args.bitmask = server->attr_bitmask; |
| 1560 | cdata->res.server = server; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1561 | nfs_fattr_init(fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | status = rpc_call_sync(server->client, &msg, 0); |
| 1563 | dprintk("NFS reply commit: %d\n", status); |
Trond Myklebust | 3b6efee | 2005-12-03 15:20:21 -0500 | [diff] [blame] | 1564 | if (status >= 0) |
| 1565 | nfs_post_op_update_inode(inode, fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | return status; |
| 1567 | } |
| 1568 | |
| 1569 | static int nfs4_proc_commit(struct nfs_write_data *cdata) |
| 1570 | { |
| 1571 | struct nfs4_exception exception = { }; |
| 1572 | int err; |
| 1573 | do { |
| 1574 | err = nfs4_handle_exception(NFS_SERVER(cdata->inode), |
| 1575 | _nfs4_proc_commit(cdata), |
| 1576 | &exception); |
| 1577 | } while (exception.retry); |
| 1578 | return err; |
| 1579 | } |
| 1580 | |
| 1581 | /* |
| 1582 | * Got race? |
| 1583 | * We will need to arrange for the VFS layer to provide an atomic open. |
| 1584 | * Until then, this create/open method is prone to inefficiency and race |
| 1585 | * conditions due to the lookup, create, and open VFS calls from sys_open() |
| 1586 | * placed on the wire. |
| 1587 | * |
| 1588 | * Given the above sorry state of affairs, I'm simply sending an OPEN. |
| 1589 | * The file will be opened again in the subsequent VFS open call |
| 1590 | * (nfs4_proc_file_open). |
| 1591 | * |
| 1592 | * The open for read will just hang around to be used by any process that |
| 1593 | * opens the file O_RDONLY. This will all be resolved with the VFS changes. |
| 1594 | */ |
| 1595 | |
| 1596 | static int |
| 1597 | nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1598 | int flags, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | { |
| 1600 | struct nfs4_state *state; |
| 1601 | struct rpc_cred *cred; |
| 1602 | int status = 0; |
| 1603 | |
| 1604 | cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0); |
| 1605 | if (IS_ERR(cred)) { |
| 1606 | status = PTR_ERR(cred); |
| 1607 | goto out; |
| 1608 | } |
| 1609 | state = nfs4_do_open(dir, dentry, flags, sattr, cred); |
| 1610 | put_rpccred(cred); |
| 1611 | if (IS_ERR(state)) { |
| 1612 | status = PTR_ERR(state); |
| 1613 | goto out; |
| 1614 | } |
| 1615 | d_instantiate(dentry, state->inode); |
| 1616 | if (flags & O_EXCL) { |
| 1617 | struct nfs_fattr fattr; |
| 1618 | status = nfs4_do_setattr(NFS_SERVER(dir), &fattr, |
| 1619 | NFS_FH(state->inode), sattr, state); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1620 | if (status == 0) |
Trond Myklebust | 65e4308 | 2005-08-16 11:49:44 -0400 | [diff] [blame] | 1621 | nfs_setattr_update_inode(state->inode, sattr); |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 1622 | } |
| 1623 | if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN)) |
| 1624 | nfs4_intent_set_file(nd, dentry, state); |
| 1625 | else |
| 1626 | nfs4_close_state(state, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | out: |
| 1628 | return status; |
| 1629 | } |
| 1630 | |
| 1631 | static int _nfs4_proc_remove(struct inode *dir, struct qstr *name) |
| 1632 | { |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 1633 | struct nfs_server *server = NFS_SERVER(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | struct nfs4_remove_arg args = { |
| 1635 | .fh = NFS_FH(dir), |
| 1636 | .name = name, |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 1637 | .bitmask = server->attr_bitmask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | }; |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 1639 | struct nfs_fattr dir_attr; |
| 1640 | struct nfs4_remove_res res = { |
| 1641 | .server = server, |
| 1642 | .dir_attr = &dir_attr, |
| 1643 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | struct rpc_message msg = { |
| 1645 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE], |
| 1646 | .rpc_argp = &args, |
| 1647 | .rpc_resp = &res, |
| 1648 | }; |
| 1649 | int status; |
| 1650 | |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 1651 | nfs_fattr_init(res.dir_attr); |
| 1652 | status = rpc_call_sync(server->client, &msg, 0); |
| 1653 | if (status == 0) { |
| 1654 | update_changeattr(dir, &res.cinfo); |
| 1655 | nfs_post_op_update_inode(dir, res.dir_attr); |
| 1656 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | return status; |
| 1658 | } |
| 1659 | |
| 1660 | static int nfs4_proc_remove(struct inode *dir, struct qstr *name) |
| 1661 | { |
| 1662 | struct nfs4_exception exception = { }; |
| 1663 | int err; |
| 1664 | do { |
| 1665 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 1666 | _nfs4_proc_remove(dir, name), |
| 1667 | &exception); |
| 1668 | } while (exception.retry); |
| 1669 | return err; |
| 1670 | } |
| 1671 | |
| 1672 | struct unlink_desc { |
| 1673 | struct nfs4_remove_arg args; |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 1674 | struct nfs4_remove_res res; |
| 1675 | struct nfs_fattr dir_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | }; |
| 1677 | |
| 1678 | static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, |
| 1679 | struct qstr *name) |
| 1680 | { |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 1681 | struct nfs_server *server = NFS_SERVER(dir->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | struct unlink_desc *up; |
| 1683 | |
| 1684 | up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL); |
| 1685 | if (!up) |
| 1686 | return -ENOMEM; |
| 1687 | |
| 1688 | up->args.fh = NFS_FH(dir->d_inode); |
| 1689 | up->args.name = name; |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 1690 | up->args.bitmask = server->attr_bitmask; |
| 1691 | up->res.server = server; |
| 1692 | up->res.dir_attr = &up->dir_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | |
| 1694 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE]; |
| 1695 | msg->rpc_argp = &up->args; |
| 1696 | msg->rpc_resp = &up->res; |
| 1697 | return 0; |
| 1698 | } |
| 1699 | |
| 1700 | static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task) |
| 1701 | { |
| 1702 | struct rpc_message *msg = &task->tk_msg; |
| 1703 | struct unlink_desc *up; |
| 1704 | |
| 1705 | if (msg->rpc_resp != NULL) { |
| 1706 | up = container_of(msg->rpc_resp, struct unlink_desc, res); |
Trond Myklebust | 16e4295 | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 1707 | update_changeattr(dir->d_inode, &up->res.cinfo); |
| 1708 | nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | kfree(up); |
| 1710 | msg->rpc_resp = NULL; |
| 1711 | msg->rpc_argp = NULL; |
| 1712 | } |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
| 1716 | static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name, |
| 1717 | struct inode *new_dir, struct qstr *new_name) |
| 1718 | { |
Trond Myklebust | 6caf2c8 | 2005-10-27 22:12:43 -0400 | [diff] [blame] | 1719 | struct nfs_server *server = NFS_SERVER(old_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | struct nfs4_rename_arg arg = { |
| 1721 | .old_dir = NFS_FH(old_dir), |
| 1722 | .new_dir = NFS_FH(new_dir), |
| 1723 | .old_name = old_name, |
| 1724 | .new_name = new_name, |
Trond Myklebust | 6caf2c8 | 2005-10-27 22:12:43 -0400 | [diff] [blame] | 1725 | .bitmask = server->attr_bitmask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | }; |
Trond Myklebust | 6caf2c8 | 2005-10-27 22:12:43 -0400 | [diff] [blame] | 1727 | struct nfs_fattr old_fattr, new_fattr; |
| 1728 | struct nfs4_rename_res res = { |
| 1729 | .server = server, |
| 1730 | .old_fattr = &old_fattr, |
| 1731 | .new_fattr = &new_fattr, |
| 1732 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | struct rpc_message msg = { |
| 1734 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME], |
| 1735 | .rpc_argp = &arg, |
| 1736 | .rpc_resp = &res, |
| 1737 | }; |
| 1738 | int status; |
| 1739 | |
Trond Myklebust | 6caf2c8 | 2005-10-27 22:12:43 -0400 | [diff] [blame] | 1740 | nfs_fattr_init(res.old_fattr); |
| 1741 | nfs_fattr_init(res.new_fattr); |
| 1742 | status = rpc_call_sync(server->client, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | |
| 1744 | if (!status) { |
| 1745 | update_changeattr(old_dir, &res.old_cinfo); |
Trond Myklebust | 6caf2c8 | 2005-10-27 22:12:43 -0400 | [diff] [blame] | 1746 | nfs_post_op_update_inode(old_dir, res.old_fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | update_changeattr(new_dir, &res.new_cinfo); |
Trond Myklebust | 6caf2c8 | 2005-10-27 22:12:43 -0400 | [diff] [blame] | 1748 | nfs_post_op_update_inode(new_dir, res.new_fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | } |
| 1750 | return status; |
| 1751 | } |
| 1752 | |
| 1753 | static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name, |
| 1754 | struct inode *new_dir, struct qstr *new_name) |
| 1755 | { |
| 1756 | struct nfs4_exception exception = { }; |
| 1757 | int err; |
| 1758 | do { |
| 1759 | err = nfs4_handle_exception(NFS_SERVER(old_dir), |
| 1760 | _nfs4_proc_rename(old_dir, old_name, |
| 1761 | new_dir, new_name), |
| 1762 | &exception); |
| 1763 | } while (exception.retry); |
| 1764 | return err; |
| 1765 | } |
| 1766 | |
| 1767 | static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) |
| 1768 | { |
Trond Myklebust | 91ba2ee | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 1769 | struct nfs_server *server = NFS_SERVER(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | struct nfs4_link_arg arg = { |
| 1771 | .fh = NFS_FH(inode), |
| 1772 | .dir_fh = NFS_FH(dir), |
| 1773 | .name = name, |
Trond Myklebust | 91ba2ee | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 1774 | .bitmask = server->attr_bitmask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | }; |
Trond Myklebust | 91ba2ee | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 1776 | struct nfs_fattr fattr, dir_attr; |
| 1777 | struct nfs4_link_res res = { |
| 1778 | .server = server, |
| 1779 | .fattr = &fattr, |
| 1780 | .dir_attr = &dir_attr, |
| 1781 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | struct rpc_message msg = { |
| 1783 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK], |
| 1784 | .rpc_argp = &arg, |
Trond Myklebust | 91ba2ee | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 1785 | .rpc_resp = &res, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | }; |
| 1787 | int status; |
| 1788 | |
Trond Myklebust | 91ba2ee | 2005-10-27 22:12:42 -0400 | [diff] [blame] | 1789 | nfs_fattr_init(res.fattr); |
| 1790 | nfs_fattr_init(res.dir_attr); |
| 1791 | status = rpc_call_sync(server->client, &msg, 0); |
| 1792 | if (!status) { |
| 1793 | update_changeattr(dir, &res.cinfo); |
| 1794 | nfs_post_op_update_inode(dir, res.dir_attr); |
| 1795 | nfs_refresh_inode(inode, res.fattr); |
| 1796 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | |
| 1798 | return status; |
| 1799 | } |
| 1800 | |
| 1801 | static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) |
| 1802 | { |
| 1803 | struct nfs4_exception exception = { }; |
| 1804 | int err; |
| 1805 | do { |
| 1806 | err = nfs4_handle_exception(NFS_SERVER(inode), |
| 1807 | _nfs4_proc_link(inode, dir, name), |
| 1808 | &exception); |
| 1809 | } while (exception.retry); |
| 1810 | return err; |
| 1811 | } |
| 1812 | |
| 1813 | static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name, |
| 1814 | struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle, |
| 1815 | struct nfs_fattr *fattr) |
| 1816 | { |
| 1817 | struct nfs_server *server = NFS_SERVER(dir); |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 1818 | struct nfs_fattr dir_fattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | struct nfs4_create_arg arg = { |
| 1820 | .dir_fh = NFS_FH(dir), |
| 1821 | .server = server, |
| 1822 | .name = name, |
| 1823 | .attrs = sattr, |
| 1824 | .ftype = NF4LNK, |
| 1825 | .bitmask = server->attr_bitmask, |
| 1826 | }; |
| 1827 | struct nfs4_create_res res = { |
| 1828 | .server = server, |
| 1829 | .fh = fhandle, |
| 1830 | .fattr = fattr, |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 1831 | .dir_fattr = &dir_fattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | }; |
| 1833 | struct rpc_message msg = { |
| 1834 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK], |
| 1835 | .rpc_argp = &arg, |
| 1836 | .rpc_resp = &res, |
| 1837 | }; |
| 1838 | int status; |
| 1839 | |
| 1840 | if (path->len > NFS4_MAXPATHLEN) |
| 1841 | return -ENAMETOOLONG; |
| 1842 | arg.u.symlink = path; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1843 | nfs_fattr_init(fattr); |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 1844 | nfs_fattr_init(&dir_fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | |
| 1846 | status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); |
| 1847 | if (!status) |
| 1848 | update_changeattr(dir, &res.dir_cinfo); |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 1849 | nfs_post_op_update_inode(dir, res.dir_fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | return status; |
| 1851 | } |
| 1852 | |
| 1853 | static int nfs4_proc_symlink(struct inode *dir, struct qstr *name, |
| 1854 | struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle, |
| 1855 | struct nfs_fattr *fattr) |
| 1856 | { |
| 1857 | struct nfs4_exception exception = { }; |
| 1858 | int err; |
| 1859 | do { |
| 1860 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 1861 | _nfs4_proc_symlink(dir, name, path, sattr, |
| 1862 | fhandle, fattr), |
| 1863 | &exception); |
| 1864 | } while (exception.retry); |
| 1865 | return err; |
| 1866 | } |
| 1867 | |
| 1868 | static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, |
| 1869 | struct iattr *sattr) |
| 1870 | { |
| 1871 | struct nfs_server *server = NFS_SERVER(dir); |
| 1872 | struct nfs_fh fhandle; |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 1873 | struct nfs_fattr fattr, dir_fattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | struct nfs4_create_arg arg = { |
| 1875 | .dir_fh = NFS_FH(dir), |
| 1876 | .server = server, |
| 1877 | .name = &dentry->d_name, |
| 1878 | .attrs = sattr, |
| 1879 | .ftype = NF4DIR, |
| 1880 | .bitmask = server->attr_bitmask, |
| 1881 | }; |
| 1882 | struct nfs4_create_res res = { |
| 1883 | .server = server, |
| 1884 | .fh = &fhandle, |
| 1885 | .fattr = &fattr, |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 1886 | .dir_fattr = &dir_fattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | }; |
| 1888 | struct rpc_message msg = { |
| 1889 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE], |
| 1890 | .rpc_argp = &arg, |
| 1891 | .rpc_resp = &res, |
| 1892 | }; |
| 1893 | int status; |
| 1894 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1895 | nfs_fattr_init(&fattr); |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 1896 | nfs_fattr_init(&dir_fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | |
| 1898 | status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); |
| 1899 | if (!status) { |
| 1900 | update_changeattr(dir, &res.dir_cinfo); |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 1901 | nfs_post_op_update_inode(dir, res.dir_fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | status = nfs_instantiate(dentry, &fhandle, &fattr); |
| 1903 | } |
| 1904 | return status; |
| 1905 | } |
| 1906 | |
| 1907 | static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, |
| 1908 | struct iattr *sattr) |
| 1909 | { |
| 1910 | struct nfs4_exception exception = { }; |
| 1911 | int err; |
| 1912 | do { |
| 1913 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 1914 | _nfs4_proc_mkdir(dir, dentry, sattr), |
| 1915 | &exception); |
| 1916 | } while (exception.retry); |
| 1917 | return err; |
| 1918 | } |
| 1919 | |
| 1920 | static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, |
| 1921 | u64 cookie, struct page *page, unsigned int count, int plus) |
| 1922 | { |
| 1923 | struct inode *dir = dentry->d_inode; |
| 1924 | struct nfs4_readdir_arg args = { |
| 1925 | .fh = NFS_FH(dir), |
| 1926 | .pages = &page, |
| 1927 | .pgbase = 0, |
| 1928 | .count = count, |
| 1929 | .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask, |
| 1930 | }; |
| 1931 | struct nfs4_readdir_res res; |
| 1932 | struct rpc_message msg = { |
| 1933 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR], |
| 1934 | .rpc_argp = &args, |
| 1935 | .rpc_resp = &res, |
| 1936 | .rpc_cred = cred, |
| 1937 | }; |
| 1938 | int status; |
| 1939 | |
Trond Myklebust | eadf459 | 2005-06-22 17:16:39 +0000 | [diff] [blame] | 1940 | dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__, |
| 1941 | dentry->d_parent->d_name.name, |
| 1942 | dentry->d_name.name, |
| 1943 | (unsigned long long)cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | lock_kernel(); |
| 1945 | nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args); |
| 1946 | res.pgbase = args.pgbase; |
| 1947 | status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); |
| 1948 | if (status == 0) |
| 1949 | memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE); |
| 1950 | unlock_kernel(); |
Trond Myklebust | eadf459 | 2005-06-22 17:16:39 +0000 | [diff] [blame] | 1951 | dprintk("%s: returns %d\n", __FUNCTION__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | return status; |
| 1953 | } |
| 1954 | |
| 1955 | static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, |
| 1956 | u64 cookie, struct page *page, unsigned int count, int plus) |
| 1957 | { |
| 1958 | struct nfs4_exception exception = { }; |
| 1959 | int err; |
| 1960 | do { |
| 1961 | err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), |
| 1962 | _nfs4_proc_readdir(dentry, cred, cookie, |
| 1963 | page, count, plus), |
| 1964 | &exception); |
| 1965 | } while (exception.retry); |
| 1966 | return err; |
| 1967 | } |
| 1968 | |
| 1969 | static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, |
| 1970 | struct iattr *sattr, dev_t rdev) |
| 1971 | { |
| 1972 | struct nfs_server *server = NFS_SERVER(dir); |
| 1973 | struct nfs_fh fh; |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 1974 | struct nfs_fattr fattr, dir_fattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | struct nfs4_create_arg arg = { |
| 1976 | .dir_fh = NFS_FH(dir), |
| 1977 | .server = server, |
| 1978 | .name = &dentry->d_name, |
| 1979 | .attrs = sattr, |
| 1980 | .bitmask = server->attr_bitmask, |
| 1981 | }; |
| 1982 | struct nfs4_create_res res = { |
| 1983 | .server = server, |
| 1984 | .fh = &fh, |
| 1985 | .fattr = &fattr, |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 1986 | .dir_fattr = &dir_fattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | }; |
| 1988 | struct rpc_message msg = { |
| 1989 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE], |
| 1990 | .rpc_argp = &arg, |
| 1991 | .rpc_resp = &res, |
| 1992 | }; |
| 1993 | int status; |
| 1994 | int mode = sattr->ia_mode; |
| 1995 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1996 | nfs_fattr_init(&fattr); |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 1997 | nfs_fattr_init(&dir_fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | |
| 1999 | BUG_ON(!(sattr->ia_valid & ATTR_MODE)); |
| 2000 | BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode)); |
| 2001 | if (S_ISFIFO(mode)) |
| 2002 | arg.ftype = NF4FIFO; |
| 2003 | else if (S_ISBLK(mode)) { |
| 2004 | arg.ftype = NF4BLK; |
| 2005 | arg.u.device.specdata1 = MAJOR(rdev); |
| 2006 | arg.u.device.specdata2 = MINOR(rdev); |
| 2007 | } |
| 2008 | else if (S_ISCHR(mode)) { |
| 2009 | arg.ftype = NF4CHR; |
| 2010 | arg.u.device.specdata1 = MAJOR(rdev); |
| 2011 | arg.u.device.specdata2 = MINOR(rdev); |
| 2012 | } |
| 2013 | else |
| 2014 | arg.ftype = NF4SOCK; |
| 2015 | |
| 2016 | status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); |
| 2017 | if (status == 0) { |
| 2018 | update_changeattr(dir, &res.dir_cinfo); |
Trond Myklebust | 56ae19f | 2005-10-27 22:12:40 -0400 | [diff] [blame] | 2019 | nfs_post_op_update_inode(dir, res.dir_fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | status = nfs_instantiate(dentry, &fh, &fattr); |
| 2021 | } |
| 2022 | return status; |
| 2023 | } |
| 2024 | |
| 2025 | static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, |
| 2026 | struct iattr *sattr, dev_t rdev) |
| 2027 | { |
| 2028 | struct nfs4_exception exception = { }; |
| 2029 | int err; |
| 2030 | do { |
| 2031 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 2032 | _nfs4_proc_mknod(dir, dentry, sattr, rdev), |
| 2033 | &exception); |
| 2034 | } while (exception.retry); |
| 2035 | return err; |
| 2036 | } |
| 2037 | |
| 2038 | static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2039 | struct nfs_fsstat *fsstat) |
| 2040 | { |
| 2041 | struct nfs4_statfs_arg args = { |
| 2042 | .fh = fhandle, |
| 2043 | .bitmask = server->attr_bitmask, |
| 2044 | }; |
| 2045 | struct rpc_message msg = { |
| 2046 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS], |
| 2047 | .rpc_argp = &args, |
| 2048 | .rpc_resp = fsstat, |
| 2049 | }; |
| 2050 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 2051 | nfs_fattr_init(fsstat->fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | return rpc_call_sync(server->client, &msg, 0); |
| 2053 | } |
| 2054 | |
| 2055 | static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat) |
| 2056 | { |
| 2057 | struct nfs4_exception exception = { }; |
| 2058 | int err; |
| 2059 | do { |
| 2060 | err = nfs4_handle_exception(server, |
| 2061 | _nfs4_proc_statfs(server, fhandle, fsstat), |
| 2062 | &exception); |
| 2063 | } while (exception.retry); |
| 2064 | return err; |
| 2065 | } |
| 2066 | |
| 2067 | static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2068 | struct nfs_fsinfo *fsinfo) |
| 2069 | { |
| 2070 | struct nfs4_fsinfo_arg args = { |
| 2071 | .fh = fhandle, |
| 2072 | .bitmask = server->attr_bitmask, |
| 2073 | }; |
| 2074 | struct rpc_message msg = { |
| 2075 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO], |
| 2076 | .rpc_argp = &args, |
| 2077 | .rpc_resp = fsinfo, |
| 2078 | }; |
| 2079 | |
| 2080 | return rpc_call_sync(server->client, &msg, 0); |
| 2081 | } |
| 2082 | |
| 2083 | static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) |
| 2084 | { |
| 2085 | struct nfs4_exception exception = { }; |
| 2086 | int err; |
| 2087 | |
| 2088 | do { |
| 2089 | err = nfs4_handle_exception(server, |
| 2090 | _nfs4_do_fsinfo(server, fhandle, fsinfo), |
| 2091 | &exception); |
| 2092 | } while (exception.retry); |
| 2093 | return err; |
| 2094 | } |
| 2095 | |
| 2096 | static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) |
| 2097 | { |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 2098 | nfs_fattr_init(fsinfo->fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | return nfs4_do_fsinfo(server, fhandle, fsinfo); |
| 2100 | } |
| 2101 | |
| 2102 | static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2103 | struct nfs_pathconf *pathconf) |
| 2104 | { |
| 2105 | struct nfs4_pathconf_arg args = { |
| 2106 | .fh = fhandle, |
| 2107 | .bitmask = server->attr_bitmask, |
| 2108 | }; |
| 2109 | struct rpc_message msg = { |
| 2110 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF], |
| 2111 | .rpc_argp = &args, |
| 2112 | .rpc_resp = pathconf, |
| 2113 | }; |
| 2114 | |
| 2115 | /* None of the pathconf attributes are mandatory to implement */ |
| 2116 | if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) { |
| 2117 | memset(pathconf, 0, sizeof(*pathconf)); |
| 2118 | return 0; |
| 2119 | } |
| 2120 | |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 2121 | nfs_fattr_init(pathconf->fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | return rpc_call_sync(server->client, &msg, 0); |
| 2123 | } |
| 2124 | |
| 2125 | static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2126 | struct nfs_pathconf *pathconf) |
| 2127 | { |
| 2128 | struct nfs4_exception exception = { }; |
| 2129 | int err; |
| 2130 | |
| 2131 | do { |
| 2132 | err = nfs4_handle_exception(server, |
| 2133 | _nfs4_proc_pathconf(server, fhandle, pathconf), |
| 2134 | &exception); |
| 2135 | } while (exception.retry); |
| 2136 | return err; |
| 2137 | } |
| 2138 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2139 | static void nfs4_read_done(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2141 | struct nfs_read_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | struct inode *inode = data->inode; |
| 2143 | |
| 2144 | if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) { |
| 2145 | rpc_restart_call(task); |
| 2146 | return; |
| 2147 | } |
| 2148 | if (task->tk_status > 0) |
| 2149 | renew_lease(NFS_SERVER(inode), data->timestamp); |
| 2150 | /* Call back common NFS readpage processing */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2151 | nfs_readpage_result(task, calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2154 | static const struct rpc_call_ops nfs4_read_ops = { |
| 2155 | .rpc_call_done = nfs4_read_done, |
| 2156 | .rpc_release = nfs_readdata_release, |
| 2157 | }; |
| 2158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | static void |
| 2160 | nfs4_proc_read_setup(struct nfs_read_data *data) |
| 2161 | { |
| 2162 | struct rpc_task *task = &data->task; |
| 2163 | struct rpc_message msg = { |
| 2164 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ], |
| 2165 | .rpc_argp = &data->args, |
| 2166 | .rpc_resp = &data->res, |
| 2167 | .rpc_cred = data->cred, |
| 2168 | }; |
| 2169 | struct inode *inode = data->inode; |
| 2170 | int flags; |
| 2171 | |
| 2172 | data->timestamp = jiffies; |
| 2173 | |
| 2174 | /* N.B. Do we need to test? Never called for swapfile inode */ |
| 2175 | flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0); |
| 2176 | |
| 2177 | /* Finalize the task. */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2178 | rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_read_ops, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | rpc_call_setup(task, &msg, 0); |
| 2180 | } |
| 2181 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2182 | static void nfs4_write_done(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2184 | struct nfs_write_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | struct inode *inode = data->inode; |
| 2186 | |
| 2187 | if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) { |
| 2188 | rpc_restart_call(task); |
| 2189 | return; |
| 2190 | } |
Trond Myklebust | 4f9838c | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2191 | if (task->tk_status >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | renew_lease(NFS_SERVER(inode), data->timestamp); |
Trond Myklebust | 4f9838c | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2193 | nfs_post_op_update_inode(inode, data->res.fattr); |
| 2194 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | /* Call back common NFS writeback processing */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2196 | nfs_writeback_done(task, calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | } |
| 2198 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2199 | static const struct rpc_call_ops nfs4_write_ops = { |
| 2200 | .rpc_call_done = nfs4_write_done, |
| 2201 | .rpc_release = nfs_writedata_release, |
| 2202 | }; |
| 2203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | static void |
| 2205 | nfs4_proc_write_setup(struct nfs_write_data *data, int how) |
| 2206 | { |
| 2207 | struct rpc_task *task = &data->task; |
| 2208 | struct rpc_message msg = { |
| 2209 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE], |
| 2210 | .rpc_argp = &data->args, |
| 2211 | .rpc_resp = &data->res, |
| 2212 | .rpc_cred = data->cred, |
| 2213 | }; |
| 2214 | struct inode *inode = data->inode; |
Trond Myklebust | 4f9838c | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2215 | struct nfs_server *server = NFS_SERVER(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | int stable; |
| 2217 | int flags; |
| 2218 | |
| 2219 | if (how & FLUSH_STABLE) { |
| 2220 | if (!NFS_I(inode)->ncommit) |
| 2221 | stable = NFS_FILE_SYNC; |
| 2222 | else |
| 2223 | stable = NFS_DATA_SYNC; |
| 2224 | } else |
| 2225 | stable = NFS_UNSTABLE; |
| 2226 | data->args.stable = stable; |
Trond Myklebust | 4f9838c | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2227 | data->args.bitmask = server->attr_bitmask; |
| 2228 | data->res.server = server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | |
| 2230 | data->timestamp = jiffies; |
| 2231 | |
| 2232 | /* Set the initial flags for the task. */ |
| 2233 | flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; |
| 2234 | |
| 2235 | /* Finalize the task. */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2236 | rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_write_ops, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | rpc_call_setup(task, &msg, 0); |
| 2238 | } |
| 2239 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2240 | static void nfs4_commit_done(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2242 | struct nfs_write_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | struct inode *inode = data->inode; |
| 2244 | |
| 2245 | if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) { |
| 2246 | rpc_restart_call(task); |
| 2247 | return; |
| 2248 | } |
Trond Myklebust | 4f9838c | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2249 | if (task->tk_status >= 0) |
| 2250 | nfs_post_op_update_inode(inode, data->res.fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | /* Call back common NFS writeback processing */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2252 | nfs_commit_done(task, calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2255 | static const struct rpc_call_ops nfs4_commit_ops = { |
| 2256 | .rpc_call_done = nfs4_commit_done, |
| 2257 | .rpc_release = nfs_commit_release, |
| 2258 | }; |
| 2259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | static void |
| 2261 | nfs4_proc_commit_setup(struct nfs_write_data *data, int how) |
| 2262 | { |
| 2263 | struct rpc_task *task = &data->task; |
| 2264 | struct rpc_message msg = { |
| 2265 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT], |
| 2266 | .rpc_argp = &data->args, |
| 2267 | .rpc_resp = &data->res, |
| 2268 | .rpc_cred = data->cred, |
| 2269 | }; |
| 2270 | struct inode *inode = data->inode; |
Trond Myklebust | 4f9838c | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2271 | struct nfs_server *server = NFS_SERVER(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | int flags; |
| 2273 | |
Trond Myklebust | 4f9838c | 2005-10-27 22:12:44 -0400 | [diff] [blame] | 2274 | data->args.bitmask = server->attr_bitmask; |
| 2275 | data->res.server = server; |
| 2276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | /* Set the initial flags for the task. */ |
| 2278 | flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; |
| 2279 | |
| 2280 | /* Finalize the task. */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2281 | rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_commit_ops, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | rpc_call_setup(task, &msg, 0); |
| 2283 | } |
| 2284 | |
| 2285 | /* |
| 2286 | * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special |
| 2287 | * standalone procedure for queueing an asynchronous RENEW. |
| 2288 | */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2289 | static void nfs4_renew_done(struct rpc_task *task, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | { |
| 2291 | struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp; |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2292 | unsigned long timestamp = (unsigned long)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | |
| 2294 | if (task->tk_status < 0) { |
| 2295 | switch (task->tk_status) { |
| 2296 | case -NFS4ERR_STALE_CLIENTID: |
| 2297 | case -NFS4ERR_EXPIRED: |
| 2298 | case -NFS4ERR_CB_PATH_DOWN: |
| 2299 | nfs4_schedule_state_recovery(clp); |
| 2300 | } |
| 2301 | return; |
| 2302 | } |
| 2303 | spin_lock(&clp->cl_lock); |
| 2304 | if (time_before(clp->cl_last_renewal,timestamp)) |
| 2305 | clp->cl_last_renewal = timestamp; |
| 2306 | spin_unlock(&clp->cl_lock); |
| 2307 | } |
| 2308 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2309 | static const struct rpc_call_ops nfs4_renew_ops = { |
| 2310 | .rpc_call_done = nfs4_renew_done, |
| 2311 | }; |
| 2312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | int |
| 2314 | nfs4_proc_async_renew(struct nfs4_client *clp) |
| 2315 | { |
| 2316 | struct rpc_message msg = { |
| 2317 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], |
| 2318 | .rpc_argp = clp, |
| 2319 | .rpc_cred = clp->cl_cred, |
| 2320 | }; |
| 2321 | |
| 2322 | return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT, |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2323 | &nfs4_renew_ops, (void *)jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | int |
| 2327 | nfs4_proc_renew(struct nfs4_client *clp) |
| 2328 | { |
| 2329 | struct rpc_message msg = { |
| 2330 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], |
| 2331 | .rpc_argp = clp, |
| 2332 | .rpc_cred = clp->cl_cred, |
| 2333 | }; |
| 2334 | unsigned long now = jiffies; |
| 2335 | int status; |
| 2336 | |
| 2337 | status = rpc_call_sync(clp->cl_rpcclient, &msg, 0); |
| 2338 | if (status < 0) |
| 2339 | return status; |
| 2340 | spin_lock(&clp->cl_lock); |
| 2341 | if (time_before(clp->cl_last_renewal,now)) |
| 2342 | clp->cl_last_renewal = now; |
| 2343 | spin_unlock(&clp->cl_lock); |
| 2344 | return 0; |
| 2345 | } |
| 2346 | |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 2347 | static inline int nfs4_server_supports_acls(struct nfs_server *server) |
| 2348 | { |
| 2349 | return (server->caps & NFS_CAP_ACLS) |
| 2350 | && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL) |
| 2351 | && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL); |
| 2352 | } |
| 2353 | |
| 2354 | /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that |
| 2355 | * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on |
| 2356 | * the stack. |
| 2357 | */ |
| 2358 | #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT) |
| 2359 | |
| 2360 | static void buf_to_pages(const void *buf, size_t buflen, |
| 2361 | struct page **pages, unsigned int *pgbase) |
| 2362 | { |
| 2363 | const void *p = buf; |
| 2364 | |
| 2365 | *pgbase = offset_in_page(buf); |
| 2366 | p -= *pgbase; |
| 2367 | while (p < buf + buflen) { |
| 2368 | *(pages++) = virt_to_page(p); |
| 2369 | p += PAGE_CACHE_SIZE; |
| 2370 | } |
| 2371 | } |
| 2372 | |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 2373 | struct nfs4_cached_acl { |
| 2374 | int cached; |
| 2375 | size_t len; |
Andrew Morton | 3e9d415 | 2005-06-22 17:16:28 +0000 | [diff] [blame] | 2376 | char data[0]; |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 2377 | }; |
| 2378 | |
| 2379 | 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] | 2380 | { |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 2381 | struct nfs_inode *nfsi = NFS_I(inode); |
| 2382 | |
| 2383 | spin_lock(&inode->i_lock); |
| 2384 | kfree(nfsi->nfs4_acl); |
| 2385 | nfsi->nfs4_acl = acl; |
| 2386 | spin_unlock(&inode->i_lock); |
| 2387 | } |
| 2388 | |
| 2389 | static void nfs4_zap_acl_attr(struct inode *inode) |
| 2390 | { |
| 2391 | nfs4_set_cached_acl(inode, NULL); |
| 2392 | } |
| 2393 | |
| 2394 | static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen) |
| 2395 | { |
| 2396 | struct nfs_inode *nfsi = NFS_I(inode); |
| 2397 | struct nfs4_cached_acl *acl; |
| 2398 | int ret = -ENOENT; |
| 2399 | |
| 2400 | spin_lock(&inode->i_lock); |
| 2401 | acl = nfsi->nfs4_acl; |
| 2402 | if (acl == NULL) |
| 2403 | goto out; |
| 2404 | if (buf == NULL) /* user is just asking for length */ |
| 2405 | goto out_len; |
| 2406 | if (acl->cached == 0) |
| 2407 | goto out; |
| 2408 | ret = -ERANGE; /* see getxattr(2) man page */ |
| 2409 | if (acl->len > buflen) |
| 2410 | goto out; |
| 2411 | memcpy(buf, acl->data, acl->len); |
| 2412 | out_len: |
| 2413 | ret = acl->len; |
| 2414 | out: |
| 2415 | spin_unlock(&inode->i_lock); |
| 2416 | return ret; |
| 2417 | } |
| 2418 | |
| 2419 | static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len) |
| 2420 | { |
| 2421 | struct nfs4_cached_acl *acl; |
| 2422 | |
| 2423 | if (buf && acl_len <= PAGE_SIZE) { |
| 2424 | acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL); |
| 2425 | if (acl == NULL) |
| 2426 | goto out; |
| 2427 | acl->cached = 1; |
| 2428 | memcpy(acl->data, buf, acl_len); |
| 2429 | } else { |
| 2430 | acl = kmalloc(sizeof(*acl), GFP_KERNEL); |
| 2431 | if (acl == NULL) |
| 2432 | goto out; |
| 2433 | acl->cached = 0; |
| 2434 | } |
| 2435 | acl->len = acl_len; |
| 2436 | out: |
| 2437 | nfs4_set_cached_acl(inode, acl); |
| 2438 | } |
| 2439 | |
| 2440 | static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) |
| 2441 | { |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 2442 | struct page *pages[NFS4ACL_MAXPAGES]; |
| 2443 | struct nfs_getaclargs args = { |
| 2444 | .fh = NFS_FH(inode), |
| 2445 | .acl_pages = pages, |
| 2446 | .acl_len = buflen, |
| 2447 | }; |
| 2448 | size_t resp_len = buflen; |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 2449 | void *resp_buf; |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 2450 | struct rpc_message msg = { |
| 2451 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL], |
| 2452 | .rpc_argp = &args, |
| 2453 | .rpc_resp = &resp_len, |
| 2454 | }; |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 2455 | struct page *localpage = NULL; |
| 2456 | int ret; |
| 2457 | |
| 2458 | if (buflen < PAGE_SIZE) { |
| 2459 | /* As long as we're doing a round trip to the server anyway, |
| 2460 | * let's be prepared for a page of acl data. */ |
| 2461 | localpage = alloc_page(GFP_KERNEL); |
| 2462 | resp_buf = page_address(localpage); |
| 2463 | if (localpage == NULL) |
| 2464 | return -ENOMEM; |
| 2465 | args.acl_pages[0] = localpage; |
| 2466 | args.acl_pgbase = 0; |
J. Bruce Fields | 1d95db8 | 2005-10-13 16:54:32 -0400 | [diff] [blame] | 2467 | resp_len = args.acl_len = PAGE_SIZE; |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 2468 | } else { |
| 2469 | resp_buf = buf; |
| 2470 | buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase); |
| 2471 | } |
| 2472 | ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0); |
| 2473 | if (ret) |
| 2474 | goto out_free; |
| 2475 | if (resp_len > args.acl_len) |
| 2476 | nfs4_write_cached_acl(inode, NULL, resp_len); |
| 2477 | else |
| 2478 | nfs4_write_cached_acl(inode, resp_buf, resp_len); |
| 2479 | if (buf) { |
| 2480 | ret = -ERANGE; |
| 2481 | if (resp_len > buflen) |
| 2482 | goto out_free; |
| 2483 | if (localpage) |
| 2484 | memcpy(buf, resp_buf, resp_len); |
| 2485 | } |
| 2486 | ret = resp_len; |
| 2487 | out_free: |
| 2488 | if (localpage) |
| 2489 | __free_page(localpage); |
| 2490 | return ret; |
| 2491 | } |
| 2492 | |
| 2493 | static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen) |
| 2494 | { |
| 2495 | struct nfs_server *server = NFS_SERVER(inode); |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 2496 | int ret; |
| 2497 | |
| 2498 | if (!nfs4_server_supports_acls(server)) |
| 2499 | return -EOPNOTSUPP; |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 2500 | ret = nfs_revalidate_inode(server, inode); |
| 2501 | if (ret < 0) |
| 2502 | return ret; |
| 2503 | ret = nfs4_read_cached_acl(inode, buf, buflen); |
| 2504 | if (ret != -ENOENT) |
| 2505 | return ret; |
| 2506 | return nfs4_get_acl_uncached(inode, buf, buflen); |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
J. Bruce Fields | 4b580ee | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 2509 | static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) |
| 2510 | { |
| 2511 | struct nfs_server *server = NFS_SERVER(inode); |
| 2512 | struct page *pages[NFS4ACL_MAXPAGES]; |
| 2513 | struct nfs_setaclargs arg = { |
| 2514 | .fh = NFS_FH(inode), |
| 2515 | .acl_pages = pages, |
| 2516 | .acl_len = buflen, |
| 2517 | }; |
| 2518 | struct rpc_message msg = { |
| 2519 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL], |
| 2520 | .rpc_argp = &arg, |
| 2521 | .rpc_resp = NULL, |
| 2522 | }; |
| 2523 | int ret; |
| 2524 | |
| 2525 | if (!nfs4_server_supports_acls(server)) |
| 2526 | return -EOPNOTSUPP; |
Trond Myklebust | 642ac54 | 2005-10-18 14:20:19 -0700 | [diff] [blame] | 2527 | nfs_inode_return_delegation(inode); |
J. Bruce Fields | 4b580ee | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 2528 | buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase); |
| 2529 | ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0); |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 2530 | if (ret == 0) |
| 2531 | nfs4_write_cached_acl(inode, buf, buflen); |
J. Bruce Fields | 4b580ee | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 2532 | return ret; |
| 2533 | } |
| 2534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | static int |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2536 | nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | { |
| 2538 | struct nfs4_client *clp = server->nfs4_state; |
| 2539 | |
| 2540 | if (!clp || task->tk_status >= 0) |
| 2541 | return 0; |
| 2542 | switch(task->tk_status) { |
| 2543 | case -NFS4ERR_STALE_CLIENTID: |
| 2544 | case -NFS4ERR_STALE_STATEID: |
| 2545 | case -NFS4ERR_EXPIRED: |
| 2546 | rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL); |
| 2547 | nfs4_schedule_state_recovery(clp); |
| 2548 | if (test_bit(NFS4CLNT_OK, &clp->cl_state)) |
| 2549 | rpc_wake_up_task(task); |
| 2550 | task->tk_status = 0; |
| 2551 | return -EAGAIN; |
| 2552 | case -NFS4ERR_GRACE: |
| 2553 | case -NFS4ERR_DELAY: |
| 2554 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
| 2555 | task->tk_status = 0; |
| 2556 | return -EAGAIN; |
| 2557 | case -NFS4ERR_OLD_STATEID: |
| 2558 | task->tk_status = 0; |
| 2559 | return -EAGAIN; |
| 2560 | } |
| 2561 | task->tk_status = nfs4_map_errors(task->tk_status); |
| 2562 | return 0; |
| 2563 | } |
| 2564 | |
| 2565 | static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp) |
| 2566 | { |
| 2567 | DEFINE_WAIT(wait); |
| 2568 | sigset_t oldset; |
| 2569 | int interruptible, res = 0; |
| 2570 | |
| 2571 | might_sleep(); |
| 2572 | |
| 2573 | rpc_clnt_sigmask(clnt, &oldset); |
| 2574 | interruptible = TASK_UNINTERRUPTIBLE; |
| 2575 | if (clnt->cl_intr) |
| 2576 | interruptible = TASK_INTERRUPTIBLE; |
| 2577 | prepare_to_wait(&clp->cl_waitq, &wait, interruptible); |
| 2578 | nfs4_schedule_state_recovery(clp); |
| 2579 | if (clnt->cl_intr && signalled()) |
| 2580 | res = -ERESTARTSYS; |
| 2581 | else if (!test_bit(NFS4CLNT_OK, &clp->cl_state)) |
| 2582 | schedule(); |
| 2583 | finish_wait(&clp->cl_waitq, &wait); |
| 2584 | rpc_clnt_sigunmask(clnt, &oldset); |
| 2585 | return res; |
| 2586 | } |
| 2587 | |
| 2588 | static int nfs4_delay(struct rpc_clnt *clnt, long *timeout) |
| 2589 | { |
| 2590 | sigset_t oldset; |
| 2591 | int res = 0; |
| 2592 | |
| 2593 | might_sleep(); |
| 2594 | |
| 2595 | if (*timeout <= 0) |
| 2596 | *timeout = NFS4_POLL_RETRY_MIN; |
| 2597 | if (*timeout > NFS4_POLL_RETRY_MAX) |
| 2598 | *timeout = NFS4_POLL_RETRY_MAX; |
| 2599 | rpc_clnt_sigmask(clnt, &oldset); |
| 2600 | if (clnt->cl_intr) { |
Nishanth Aravamudan | 041e0e3 | 2005-09-10 00:27:23 -0700 | [diff] [blame] | 2601 | schedule_timeout_interruptible(*timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | if (signalled()) |
| 2603 | res = -ERESTARTSYS; |
Nishanth Aravamudan | 041e0e3 | 2005-09-10 00:27:23 -0700 | [diff] [blame] | 2604 | } else |
| 2605 | schedule_timeout_uninterruptible(*timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | rpc_clnt_sigunmask(clnt, &oldset); |
| 2607 | *timeout <<= 1; |
| 2608 | return res; |
| 2609 | } |
| 2610 | |
| 2611 | /* This is the error handling routine for processes that are allowed |
| 2612 | * to sleep. |
| 2613 | */ |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2614 | int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | { |
| 2616 | struct nfs4_client *clp = server->nfs4_state; |
| 2617 | int ret = errorcode; |
| 2618 | |
| 2619 | exception->retry = 0; |
| 2620 | switch(errorcode) { |
| 2621 | case 0: |
| 2622 | return 0; |
| 2623 | case -NFS4ERR_STALE_CLIENTID: |
| 2624 | case -NFS4ERR_STALE_STATEID: |
| 2625 | case -NFS4ERR_EXPIRED: |
| 2626 | ret = nfs4_wait_clnt_recover(server->client, clp); |
| 2627 | if (ret == 0) |
| 2628 | exception->retry = 1; |
| 2629 | break; |
| 2630 | case -NFS4ERR_GRACE: |
| 2631 | case -NFS4ERR_DELAY: |
| 2632 | ret = nfs4_delay(server->client, &exception->timeout); |
Trond Myklebust | 2c56617 | 2005-11-04 15:33:50 -0500 | [diff] [blame] | 2633 | if (ret != 0) |
| 2634 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | case -NFS4ERR_OLD_STATEID: |
Trond Myklebust | 2c56617 | 2005-11-04 15:33:50 -0500 | [diff] [blame] | 2636 | exception->retry = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | } |
| 2638 | /* We failed to handle the error */ |
| 2639 | return nfs4_map_errors(ret); |
| 2640 | } |
| 2641 | |
| 2642 | int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port) |
| 2643 | { |
| 2644 | nfs4_verifier sc_verifier; |
| 2645 | struct nfs4_setclientid setclientid = { |
| 2646 | .sc_verifier = &sc_verifier, |
| 2647 | .sc_prog = program, |
| 2648 | }; |
| 2649 | struct rpc_message msg = { |
| 2650 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID], |
| 2651 | .rpc_argp = &setclientid, |
| 2652 | .rpc_resp = clp, |
| 2653 | .rpc_cred = clp->cl_cred, |
| 2654 | }; |
| 2655 | u32 *p; |
| 2656 | int loop = 0; |
| 2657 | int status; |
| 2658 | |
| 2659 | p = (u32*)sc_verifier.data; |
| 2660 | *p++ = htonl((u32)clp->cl_boot_time.tv_sec); |
| 2661 | *p = htonl((u32)clp->cl_boot_time.tv_nsec); |
| 2662 | |
| 2663 | for(;;) { |
| 2664 | setclientid.sc_name_len = scnprintf(setclientid.sc_name, |
| 2665 | sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u", |
| 2666 | clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr), |
| 2667 | clp->cl_cred->cr_ops->cr_name, |
| 2668 | clp->cl_id_uniquifier); |
| 2669 | setclientid.sc_netid_len = scnprintf(setclientid.sc_netid, |
| 2670 | sizeof(setclientid.sc_netid), "tcp"); |
| 2671 | setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr, |
| 2672 | sizeof(setclientid.sc_uaddr), "%s.%d.%d", |
| 2673 | clp->cl_ipaddr, port >> 8, port & 255); |
| 2674 | |
| 2675 | status = rpc_call_sync(clp->cl_rpcclient, &msg, 0); |
| 2676 | if (status != -NFS4ERR_CLID_INUSE) |
| 2677 | break; |
| 2678 | if (signalled()) |
| 2679 | break; |
| 2680 | if (loop++ & 1) |
| 2681 | ssleep(clp->cl_lease_time + 1); |
| 2682 | else |
| 2683 | if (++clp->cl_id_uniquifier == 0) |
| 2684 | break; |
| 2685 | } |
| 2686 | return status; |
| 2687 | } |
| 2688 | |
| 2689 | int |
| 2690 | nfs4_proc_setclientid_confirm(struct nfs4_client *clp) |
| 2691 | { |
| 2692 | struct nfs_fsinfo fsinfo; |
| 2693 | struct rpc_message msg = { |
| 2694 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM], |
| 2695 | .rpc_argp = clp, |
| 2696 | .rpc_resp = &fsinfo, |
| 2697 | .rpc_cred = clp->cl_cred, |
| 2698 | }; |
| 2699 | unsigned long now; |
| 2700 | int status; |
| 2701 | |
| 2702 | now = jiffies; |
| 2703 | status = rpc_call_sync(clp->cl_rpcclient, &msg, 0); |
| 2704 | if (status == 0) { |
| 2705 | spin_lock(&clp->cl_lock); |
| 2706 | clp->cl_lease_time = fsinfo.lease_time * HZ; |
| 2707 | clp->cl_last_renewal = now; |
| 2708 | spin_unlock(&clp->cl_lock); |
| 2709 | } |
| 2710 | return status; |
| 2711 | } |
| 2712 | |
| 2713 | static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid) |
| 2714 | { |
| 2715 | struct nfs4_delegreturnargs args = { |
| 2716 | .fhandle = NFS_FH(inode), |
| 2717 | .stateid = stateid, |
| 2718 | }; |
| 2719 | struct rpc_message msg = { |
| 2720 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN], |
| 2721 | .rpc_argp = &args, |
| 2722 | .rpc_cred = cred, |
| 2723 | }; |
| 2724 | |
| 2725 | return rpc_call_sync(NFS_CLIENT(inode), &msg, 0); |
| 2726 | } |
| 2727 | |
| 2728 | int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid) |
| 2729 | { |
| 2730 | struct nfs_server *server = NFS_SERVER(inode); |
| 2731 | struct nfs4_exception exception = { }; |
| 2732 | int err; |
| 2733 | do { |
| 2734 | err = _nfs4_proc_delegreturn(inode, cred, stateid); |
| 2735 | switch (err) { |
| 2736 | case -NFS4ERR_STALE_STATEID: |
| 2737 | case -NFS4ERR_EXPIRED: |
| 2738 | nfs4_schedule_state_recovery(server->nfs4_state); |
| 2739 | case 0: |
| 2740 | return 0; |
| 2741 | } |
| 2742 | err = nfs4_handle_exception(server, err, &exception); |
| 2743 | } while (exception.retry); |
| 2744 | return err; |
| 2745 | } |
| 2746 | |
| 2747 | #define NFS4_LOCK_MINTIMEOUT (1 * HZ) |
| 2748 | #define NFS4_LOCK_MAXTIMEOUT (30 * HZ) |
| 2749 | |
| 2750 | /* |
| 2751 | * sleep, with exponential backoff, and retry the LOCK operation. |
| 2752 | */ |
| 2753 | static unsigned long |
| 2754 | nfs4_set_lock_task_retry(unsigned long timeout) |
| 2755 | { |
Nishanth Aravamudan | 041e0e3 | 2005-09-10 00:27:23 -0700 | [diff] [blame] | 2756 | schedule_timeout_interruptible(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | timeout <<= 1; |
| 2758 | if (timeout > NFS4_LOCK_MAXTIMEOUT) |
| 2759 | return NFS4_LOCK_MAXTIMEOUT; |
| 2760 | return timeout; |
| 2761 | } |
| 2762 | |
| 2763 | static inline int |
| 2764 | nfs4_lck_type(int cmd, struct file_lock *request) |
| 2765 | { |
| 2766 | /* set lock type */ |
| 2767 | switch (request->fl_type) { |
| 2768 | case F_RDLCK: |
| 2769 | return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT; |
| 2770 | case F_WRLCK: |
| 2771 | return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT; |
| 2772 | case F_UNLCK: |
| 2773 | return NFS4_WRITE_LT; |
| 2774 | } |
| 2775 | BUG(); |
| 2776 | return 0; |
| 2777 | } |
| 2778 | |
| 2779 | static inline uint64_t |
| 2780 | nfs4_lck_length(struct file_lock *request) |
| 2781 | { |
| 2782 | if (request->fl_end == OFFSET_MAX) |
| 2783 | return ~(uint64_t)0; |
| 2784 | return request->fl_end - request->fl_start + 1; |
| 2785 | } |
| 2786 | |
| 2787 | static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 2788 | { |
| 2789 | struct inode *inode = state->inode; |
| 2790 | struct nfs_server *server = NFS_SERVER(inode); |
| 2791 | struct nfs4_client *clp = server->nfs4_state; |
| 2792 | struct nfs_lockargs arg = { |
| 2793 | .fh = NFS_FH(inode), |
| 2794 | .type = nfs4_lck_type(cmd, request), |
| 2795 | .offset = request->fl_start, |
| 2796 | .length = nfs4_lck_length(request), |
| 2797 | }; |
| 2798 | struct nfs_lockres res = { |
| 2799 | .server = server, |
| 2800 | }; |
| 2801 | struct rpc_message msg = { |
| 2802 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT], |
| 2803 | .rpc_argp = &arg, |
| 2804 | .rpc_resp = &res, |
| 2805 | .rpc_cred = state->owner->so_cred, |
| 2806 | }; |
| 2807 | struct nfs_lowner nlo; |
| 2808 | struct nfs4_lock_state *lsp; |
| 2809 | int status; |
| 2810 | |
| 2811 | down_read(&clp->cl_sem); |
| 2812 | nlo.clientid = clp->cl_clientid; |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 2813 | status = nfs4_set_lock_state(state, request); |
| 2814 | if (status != 0) |
| 2815 | goto out; |
| 2816 | lsp = request->fl_u.nfs4_fl.owner; |
| 2817 | nlo.id = lsp->ls_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | arg.u.lockt = &nlo; |
| 2819 | status = rpc_call_sync(server->client, &msg, 0); |
| 2820 | if (!status) { |
| 2821 | request->fl_type = F_UNLCK; |
| 2822 | } else if (status == -NFS4ERR_DENIED) { |
| 2823 | int64_t len, start, end; |
| 2824 | start = res.u.denied.offset; |
| 2825 | len = res.u.denied.length; |
| 2826 | end = start + len - 1; |
| 2827 | if (end < 0 || len == 0) |
| 2828 | request->fl_end = OFFSET_MAX; |
| 2829 | else |
| 2830 | request->fl_end = (loff_t)end; |
| 2831 | request->fl_start = (loff_t)start; |
| 2832 | request->fl_type = F_WRLCK; |
| 2833 | if (res.u.denied.type & 1) |
| 2834 | request->fl_type = F_RDLCK; |
| 2835 | request->fl_pid = 0; |
| 2836 | status = 0; |
| 2837 | } |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 2838 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | up_read(&clp->cl_sem); |
| 2840 | return status; |
| 2841 | } |
| 2842 | |
| 2843 | static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 2844 | { |
| 2845 | struct nfs4_exception exception = { }; |
| 2846 | int err; |
| 2847 | |
| 2848 | do { |
| 2849 | err = nfs4_handle_exception(NFS_SERVER(state->inode), |
| 2850 | _nfs4_proc_getlk(state, cmd, request), |
| 2851 | &exception); |
| 2852 | } while (exception.retry); |
| 2853 | return err; |
| 2854 | } |
| 2855 | |
| 2856 | static int do_vfs_lock(struct file *file, struct file_lock *fl) |
| 2857 | { |
| 2858 | int res = 0; |
| 2859 | switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { |
| 2860 | case FL_POSIX: |
| 2861 | res = posix_lock_file_wait(file, fl); |
| 2862 | break; |
| 2863 | case FL_FLOCK: |
| 2864 | res = flock_lock_file_wait(file, fl); |
| 2865 | break; |
| 2866 | default: |
| 2867 | BUG(); |
| 2868 | } |
| 2869 | if (res < 0) |
| 2870 | printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", |
| 2871 | __FUNCTION__); |
| 2872 | return res; |
| 2873 | } |
| 2874 | |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2875 | struct nfs4_unlockdata { |
| 2876 | struct nfs_lockargs arg; |
| 2877 | struct nfs_locku_opargs luargs; |
| 2878 | struct nfs_lockres res; |
| 2879 | struct nfs4_lock_state *lsp; |
| 2880 | struct nfs_open_context *ctx; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2881 | }; |
| 2882 | |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame^] | 2883 | static void nfs4_locku_release_calldata(void *data) |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2884 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2885 | struct nfs4_unlockdata *calldata = data; |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame^] | 2886 | nfs_free_seqid(calldata->luargs.seqid); |
| 2887 | nfs4_put_lock_state(calldata->lsp); |
| 2888 | put_nfs_open_context(calldata->ctx); |
| 2889 | kfree(calldata); |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2890 | } |
| 2891 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2892 | static void nfs4_locku_done(struct rpc_task *task, void *data) |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2893 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2894 | struct nfs4_unlockdata *calldata = data; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2895 | |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame^] | 2896 | if (RPC_ASSASSINATED(task)) |
| 2897 | return; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2898 | nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid); |
| 2899 | switch (task->tk_status) { |
| 2900 | case 0: |
| 2901 | memcpy(calldata->lsp->ls_stateid.data, |
| 2902 | calldata->res.u.stateid.data, |
| 2903 | sizeof(calldata->lsp->ls_stateid.data)); |
| 2904 | break; |
| 2905 | case -NFS4ERR_STALE_STATEID: |
| 2906 | case -NFS4ERR_EXPIRED: |
| 2907 | nfs4_schedule_state_recovery(calldata->res.server->nfs4_state); |
| 2908 | break; |
| 2909 | default: |
| 2910 | if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) { |
| 2911 | rpc_restart_call(task); |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2912 | } |
| 2913 | } |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2914 | } |
| 2915 | |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 2916 | static void nfs4_locku_prepare(struct rpc_task *task, void *data) |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2917 | { |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 2918 | struct nfs4_unlockdata *calldata = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | struct rpc_message msg = { |
| 2920 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU], |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2921 | .rpc_argp = &calldata->arg, |
| 2922 | .rpc_resp = &calldata->res, |
| 2923 | .rpc_cred = calldata->lsp->ls_state->owner->so_cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | }; |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 2925 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2927 | status = nfs_wait_on_sequence(calldata->luargs.seqid, task); |
| 2928 | if (status != 0) |
| 2929 | return; |
| 2930 | if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2931 | /* Note: exit _without_ running nfs4_locku_done */ |
| 2932 | task->tk_action = NULL; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2933 | return; |
| 2934 | } |
| 2935 | rpc_call_setup(task, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | } |
| 2937 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2938 | static const struct rpc_call_ops nfs4_locku_ops = { |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 2939 | .rpc_call_prepare = nfs4_locku_prepare, |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2940 | .rpc_call_done = nfs4_locku_done, |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame^] | 2941 | .rpc_release = nfs4_locku_release_calldata, |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 2942 | }; |
| 2943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 2945 | { |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2946 | struct nfs4_unlockdata *calldata; |
| 2947 | struct inode *inode = state->inode; |
| 2948 | struct nfs_server *server = NFS_SERVER(inode); |
| 2949 | struct nfs4_lock_state *lsp; |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame^] | 2950 | struct rpc_task *task; |
| 2951 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 2953 | /* Is this a delegated lock? */ |
| 2954 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame^] | 2955 | goto out; |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 2956 | |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2957 | status = nfs4_set_lock_state(state, request); |
| 2958 | if (status != 0) |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame^] | 2959 | goto out; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2960 | lsp = request->fl_u.nfs4_fl.owner; |
| 2961 | /* We might have lost the locks! */ |
| 2962 | if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame^] | 2963 | goto out; |
| 2964 | status = -ENOMEM; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2965 | calldata = kmalloc(sizeof(*calldata), GFP_KERNEL); |
| 2966 | if (calldata == NULL) |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame^] | 2967 | goto out; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2968 | calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid); |
| 2969 | if (calldata->luargs.seqid == NULL) { |
| 2970 | kfree(calldata); |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame^] | 2971 | goto out; |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2972 | } |
| 2973 | calldata->luargs.stateid = &lsp->ls_stateid; |
| 2974 | calldata->arg.fh = NFS_FH(inode); |
| 2975 | calldata->arg.type = nfs4_lck_type(cmd, request); |
| 2976 | calldata->arg.offset = request->fl_start; |
| 2977 | calldata->arg.length = nfs4_lck_length(request); |
| 2978 | calldata->arg.u.locku = &calldata->luargs; |
| 2979 | calldata->res.server = server; |
| 2980 | calldata->lsp = lsp; |
| 2981 | atomic_inc(&lsp->ls_count); |
| 2982 | |
| 2983 | /* Ensure we don't close file until we're done freeing locks! */ |
| 2984 | calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data); |
| 2985 | |
Trond Myklebust | 06f814a | 2006-01-03 09:55:07 +0100 | [diff] [blame^] | 2986 | task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_locku_ops, calldata); |
| 2987 | if (!IS_ERR(task)) { |
| 2988 | status = nfs4_wait_for_completion_rpc_task(task); |
| 2989 | rpc_release_task(task); |
| 2990 | } else { |
| 2991 | status = PTR_ERR(task); |
| 2992 | nfs4_locku_release_calldata(calldata); |
| 2993 | } |
| 2994 | out: |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2995 | do_vfs_lock(request->fl_file, request); |
Trond Myklebust | faf5f49 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 2996 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim) |
| 3000 | { |
| 3001 | struct inode *inode = state->inode; |
| 3002 | struct nfs_server *server = NFS_SERVER(inode); |
Trond Myklebust | 8d0a8a9 | 2005-06-22 17:16:32 +0000 | [diff] [blame] | 3003 | struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner; |
Trond Myklebust | 06735b3 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3004 | struct nfs_lock_opargs largs = { |
| 3005 | .lock_stateid = &lsp->ls_stateid, |
| 3006 | .open_stateid = &state->stateid, |
| 3007 | .lock_owner = { |
| 3008 | .clientid = server->nfs4_state->cl_clientid, |
| 3009 | .id = lsp->ls_id, |
| 3010 | }, |
| 3011 | .reclaim = reclaim, |
| 3012 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | struct nfs_lockargs arg = { |
| 3014 | .fh = NFS_FH(inode), |
| 3015 | .type = nfs4_lck_type(cmd, request), |
| 3016 | .offset = request->fl_start, |
| 3017 | .length = nfs4_lck_length(request), |
Trond Myklebust | 06735b3 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3018 | .u = { |
| 3019 | .lock = &largs, |
| 3020 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | }; |
| 3022 | struct nfs_lockres res = { |
| 3023 | .server = server, |
| 3024 | }; |
| 3025 | struct rpc_message msg = { |
| 3026 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK], |
| 3027 | .rpc_argp = &arg, |
| 3028 | .rpc_resp = &res, |
| 3029 | .rpc_cred = state->owner->so_cred, |
| 3030 | }; |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 3031 | int status = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | |
Trond Myklebust | 06735b3 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3033 | largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid); |
| 3034 | if (largs.lock_seqid == NULL) |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 3035 | return -ENOMEM; |
| 3036 | if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | struct nfs4_state_owner *owner = state->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | |
Trond Myklebust | 06735b3 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3039 | largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid); |
| 3040 | if (largs.open_seqid == NULL) |
| 3041 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | largs.new_lock_owner = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR); |
Trond Myklebust | 06735b3 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3044 | /* increment open seqid on success, and seqid mutating errors */ |
| 3045 | if (largs.new_lock_owner != 0) { |
| 3046 | nfs_increment_open_seqid(status, largs.open_seqid); |
| 3047 | if (status == 0) |
| 3048 | nfs_confirm_seqid(&lsp->ls_seqid, 0); |
| 3049 | } |
| 3050 | nfs_free_seqid(largs.open_seqid); |
| 3051 | } else |
| 3052 | status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR); |
| 3053 | /* increment lock seqid on success, and seqid mutating errors*/ |
| 3054 | nfs_increment_lock_seqid(status, largs.lock_seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | /* save the returned stateid. */ |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 3056 | if (status == 0) { |
Trond Myklebust | 06735b3 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3057 | memcpy(lsp->ls_stateid.data, res.u.stateid.data, |
| 3058 | sizeof(lsp->ls_stateid.data)); |
Trond Myklebust | cee54fc | 2005-10-18 14:20:12 -0700 | [diff] [blame] | 3059 | lsp->ls_flags |= NFS_LOCK_INITIALIZED; |
| 3060 | } else if (status == -NFS4ERR_DENIED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | status = -EAGAIN; |
Trond Myklebust | 06735b3 | 2005-10-18 14:20:15 -0700 | [diff] [blame] | 3062 | out: |
| 3063 | nfs_free_seqid(largs.lock_seqid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | return status; |
| 3065 | } |
| 3066 | |
| 3067 | static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request) |
| 3068 | { |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 3069 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 3070 | struct nfs4_exception exception = { }; |
| 3071 | int err; |
| 3072 | |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 3073 | /* Cache the lock if possible... */ |
| 3074 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) |
| 3075 | return 0; |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 3076 | do { |
| 3077 | err = _nfs4_do_setlk(state, F_SETLK, request, 1); |
| 3078 | if (err != -NFS4ERR_DELAY) |
| 3079 | break; |
| 3080 | nfs4_handle_exception(server, err, &exception); |
| 3081 | } while (exception.retry); |
| 3082 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | } |
| 3084 | |
| 3085 | static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request) |
| 3086 | { |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 3087 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 3088 | struct nfs4_exception exception = { }; |
| 3089 | int err; |
| 3090 | |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 3091 | err = nfs4_set_lock_state(state, request); |
| 3092 | if (err != 0) |
| 3093 | return err; |
Trond Myklebust | 202b50d | 2005-06-22 17:16:29 +0000 | [diff] [blame] | 3094 | do { |
| 3095 | err = _nfs4_do_setlk(state, F_SETLK, request, 0); |
| 3096 | if (err != -NFS4ERR_DELAY) |
| 3097 | break; |
| 3098 | nfs4_handle_exception(server, err, &exception); |
| 3099 | } while (exception.retry); |
| 3100 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 3104 | { |
| 3105 | struct nfs4_client *clp = state->owner->so_client; |
| 3106 | int status; |
| 3107 | |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 3108 | /* Is this a delegated open? */ |
Trond Myklebust | ff60406 | 2005-11-25 17:10:01 -0500 | [diff] [blame] | 3109 | if (NFS_I(state->inode)->delegation_state != 0) { |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 3110 | /* Yes: cache locks! */ |
| 3111 | status = do_vfs_lock(request->fl_file, request); |
| 3112 | /* ...but avoid races with delegation recall... */ |
| 3113 | if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags)) |
Trond Myklebust | ff60406 | 2005-11-25 17:10:01 -0500 | [diff] [blame] | 3114 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | } |
Trond Myklebust | ff60406 | 2005-11-25 17:10:01 -0500 | [diff] [blame] | 3116 | down_read(&clp->cl_sem); |
Trond Myklebust | 6bfc93e | 2005-11-04 15:39:36 -0500 | [diff] [blame] | 3117 | status = nfs4_set_lock_state(state, request); |
| 3118 | if (status != 0) |
| 3119 | goto out; |
| 3120 | status = _nfs4_do_setlk(state, cmd, request, 0); |
| 3121 | if (status != 0) |
| 3122 | goto out; |
| 3123 | /* Note: we always want to sleep here! */ |
| 3124 | request->fl_flags |= FL_SLEEP; |
| 3125 | if (do_vfs_lock(request->fl_file, request) < 0) |
| 3126 | printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__); |
| 3127 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | up_read(&clp->cl_sem); |
| 3129 | return status; |
| 3130 | } |
| 3131 | |
| 3132 | static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 3133 | { |
| 3134 | struct nfs4_exception exception = { }; |
| 3135 | int err; |
| 3136 | |
| 3137 | do { |
| 3138 | err = nfs4_handle_exception(NFS_SERVER(state->inode), |
| 3139 | _nfs4_proc_setlk(state, cmd, request), |
| 3140 | &exception); |
| 3141 | } while (exception.retry); |
| 3142 | return err; |
| 3143 | } |
| 3144 | |
| 3145 | static int |
| 3146 | nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request) |
| 3147 | { |
| 3148 | struct nfs_open_context *ctx; |
| 3149 | struct nfs4_state *state; |
| 3150 | unsigned long timeout = NFS4_LOCK_MINTIMEOUT; |
| 3151 | int status; |
| 3152 | |
| 3153 | /* verify open state */ |
| 3154 | ctx = (struct nfs_open_context *)filp->private_data; |
| 3155 | state = ctx->state; |
| 3156 | |
| 3157 | if (request->fl_start < 0 || request->fl_end < 0) |
| 3158 | return -EINVAL; |
| 3159 | |
| 3160 | if (IS_GETLK(cmd)) |
| 3161 | return nfs4_proc_getlk(state, F_GETLK, request); |
| 3162 | |
| 3163 | if (!(IS_SETLK(cmd) || IS_SETLKW(cmd))) |
| 3164 | return -EINVAL; |
| 3165 | |
| 3166 | if (request->fl_type == F_UNLCK) |
| 3167 | return nfs4_proc_unlck(state, cmd, request); |
| 3168 | |
| 3169 | do { |
| 3170 | status = nfs4_proc_setlk(state, cmd, request); |
| 3171 | if ((status != -EAGAIN) || IS_SETLK(cmd)) |
| 3172 | break; |
| 3173 | timeout = nfs4_set_lock_task_retry(timeout); |
| 3174 | status = -ERESTARTSYS; |
| 3175 | if (signalled()) |
| 3176 | break; |
| 3177 | } while(status < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | return status; |
| 3179 | } |
| 3180 | |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 3181 | int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl) |
| 3182 | { |
| 3183 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 3184 | struct nfs4_exception exception = { }; |
| 3185 | int err; |
| 3186 | |
| 3187 | err = nfs4_set_lock_state(state, fl); |
| 3188 | if (err != 0) |
| 3189 | goto out; |
| 3190 | do { |
| 3191 | err = _nfs4_do_setlk(state, F_SETLK, fl, 0); |
| 3192 | if (err != -NFS4ERR_DELAY) |
| 3193 | break; |
| 3194 | err = nfs4_handle_exception(server, err, &exception); |
| 3195 | } while (exception.retry); |
| 3196 | out: |
| 3197 | return err; |
| 3198 | } |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3199 | |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3200 | #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl" |
| 3201 | |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3202 | int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf, |
| 3203 | size_t buflen, int flags) |
| 3204 | { |
J. Bruce Fields | 4b580ee | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3205 | struct inode *inode = dentry->d_inode; |
| 3206 | |
| 3207 | if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0) |
| 3208 | return -EOPNOTSUPP; |
| 3209 | |
| 3210 | if (!S_ISREG(inode->i_mode) && |
| 3211 | (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX)) |
| 3212 | return -EPERM; |
| 3213 | |
| 3214 | return nfs4_proc_set_acl(inode, buf, buflen); |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | /* The getxattr man page suggests returning -ENODATA for unknown attributes, |
| 3218 | * and that's what we'll do for e.g. user attributes that haven't been set. |
| 3219 | * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported |
| 3220 | * attributes in kernel-managed attribute namespaces. */ |
| 3221 | ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf, |
| 3222 | size_t buflen) |
| 3223 | { |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3224 | struct inode *inode = dentry->d_inode; |
| 3225 | |
| 3226 | if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0) |
| 3227 | return -EOPNOTSUPP; |
| 3228 | |
| 3229 | return nfs4_proc_get_acl(inode, buf, buflen); |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen) |
| 3233 | { |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3234 | size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1; |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3235 | |
| 3236 | if (buf && buflen < len) |
| 3237 | return -ERANGE; |
| 3238 | if (buf) |
J. Bruce Fields | aa1870a | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3239 | memcpy(buf, XATTR_NAME_NFSV4_ACL, len); |
| 3240 | return len; |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3243 | struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = { |
| 3244 | .recover_open = nfs4_open_reclaim, |
| 3245 | .recover_lock = nfs4_lock_reclaim, |
| 3246 | }; |
| 3247 | |
| 3248 | struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = { |
| 3249 | .recover_open = nfs4_open_expired, |
| 3250 | .recover_lock = nfs4_lock_expired, |
| 3251 | }; |
| 3252 | |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3253 | static struct inode_operations nfs4_file_inode_operations = { |
| 3254 | .permission = nfs_permission, |
| 3255 | .getattr = nfs_getattr, |
| 3256 | .setattr = nfs_setattr, |
| 3257 | .getxattr = nfs4_getxattr, |
| 3258 | .setxattr = nfs4_setxattr, |
| 3259 | .listxattr = nfs4_listxattr, |
| 3260 | }; |
| 3261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | struct nfs_rpc_ops nfs_v4_clientops = { |
| 3263 | .version = 4, /* protocol version */ |
| 3264 | .dentry_ops = &nfs4_dentry_operations, |
| 3265 | .dir_inode_ops = &nfs4_dir_inode_operations, |
J. Bruce Fields | 6b3b549 | 2005-06-22 17:16:22 +0000 | [diff] [blame] | 3266 | .file_inode_ops = &nfs4_file_inode_operations, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | .getroot = nfs4_proc_get_root, |
| 3268 | .getattr = nfs4_proc_getattr, |
| 3269 | .setattr = nfs4_proc_setattr, |
| 3270 | .lookup = nfs4_proc_lookup, |
| 3271 | .access = nfs4_proc_access, |
| 3272 | .readlink = nfs4_proc_readlink, |
| 3273 | .read = nfs4_proc_read, |
| 3274 | .write = nfs4_proc_write, |
| 3275 | .commit = nfs4_proc_commit, |
| 3276 | .create = nfs4_proc_create, |
| 3277 | .remove = nfs4_proc_remove, |
| 3278 | .unlink_setup = nfs4_proc_unlink_setup, |
| 3279 | .unlink_done = nfs4_proc_unlink_done, |
| 3280 | .rename = nfs4_proc_rename, |
| 3281 | .link = nfs4_proc_link, |
| 3282 | .symlink = nfs4_proc_symlink, |
| 3283 | .mkdir = nfs4_proc_mkdir, |
| 3284 | .rmdir = nfs4_proc_remove, |
| 3285 | .readdir = nfs4_proc_readdir, |
| 3286 | .mknod = nfs4_proc_mknod, |
| 3287 | .statfs = nfs4_proc_statfs, |
| 3288 | .fsinfo = nfs4_proc_fsinfo, |
| 3289 | .pathconf = nfs4_proc_pathconf, |
| 3290 | .decode_dirent = nfs4_decode_dirent, |
| 3291 | .read_setup = nfs4_proc_read_setup, |
| 3292 | .write_setup = nfs4_proc_write_setup, |
| 3293 | .commit_setup = nfs4_proc_commit_setup, |
Trond Myklebust | 02a913a | 2005-10-18 14:20:17 -0700 | [diff] [blame] | 3294 | .file_open = nfs_open, |
| 3295 | .file_release = nfs_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | .lock = nfs4_proc_lock, |
J. Bruce Fields | e50a1c2 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 3297 | .clear_acl_cache = nfs4_zap_acl_attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | }; |
| 3299 | |
| 3300 | /* |
| 3301 | * Local variables: |
| 3302 | * c-basic-offset: 8 |
| 3303 | * End: |
| 3304 | */ |