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