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