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