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