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