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