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