blob: 05afb7ba3bc454e727034eff24a4cb9c9efcde38 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Myklebust02a913a2005-10-18 14:20:17 -070050#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "delegation.h"
Chuck Lever006ea732006-03-20 13:44:14 -050054#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define NFSDBG_FACILITY NFSDBG_PROC
57
Trond Myklebust2066fe82006-09-15 08:30:46 -040058#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define NFS4_POLL_RETRY_MAX (15*HZ)
60
Trond Myklebustcdd4e682006-01-03 09:55:12 +010061struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010062static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070064static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070066static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
David Howellsadfa6f92006-08-22 20:06:08 -040067static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* Prevent leaks of NFSv4 errors into userland */
70int 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 */
83const 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
100const 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 Myklebust4ce79712005-06-22 17:16:21 +0000109const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 FATTR4_WORD0_MAXLINK
111 | FATTR4_WORD0_MAXNAME,
112 0
113};
114
115const 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 Naik830b8e32006-06-09 09:34:25 -0400122const 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 Virobc4785c2006-10-19 23:28:51 -0700141static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct nfs4_readdir_arg *readdir)
143{
Al Viro0dbb4c62006-10-19 23:28:49 -0700144 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 BUG_ON(readdir->count < 80);
147 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000148 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 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 Viro0dbb4c62006-10-19 23:28:49 -0700165 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
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 Myklebust26e976a2006-01-03 09:55:21 +0100196static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
David Howells7539bba2006-08-22 20:06:09 -0400198 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 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 Myklebust38478b22006-05-25 01:40:57 -0400205static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Trond Myklebust38478b22006-05-25 01:40:57 -0400207 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Trond Myklebust38478b22006-05-25 01:40:57 -0400209 spin_lock(&dir->i_lock);
210 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
212 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400213 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100216struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400217 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100218 struct nfs_openargs o_arg;
219 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100220 struct nfs_open_confirmargs c_arg;
221 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100222 struct nfs_fattr f_attr;
223 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400224 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100225 struct dentry *dir;
226 struct nfs4_state_owner *owner;
227 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100228 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400229 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100230 int rpc_status;
231 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100232};
233
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400234
235static void nfs4_init_opendata_res(struct nfs4_opendata *p)
236{
237 p->o_res.f_attr = &p->f_attr;
238 p->o_res.dir_attr = &p->dir_attr;
239 p->o_res.server = p->o_arg.server;
240 nfs_fattr_init(&p->f_attr);
241 nfs_fattr_init(&p->dir_attr);
242}
243
Trond Myklebustad389da2007-06-05 12:30:00 -0400244static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100245 struct nfs4_state_owner *sp, int flags,
246 const struct iattr *attrs)
247{
Trond Myklebustad389da2007-06-05 12:30:00 -0400248 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100249 struct inode *dir = parent->d_inode;
250 struct nfs_server *server = NFS_SERVER(dir);
251 struct nfs4_opendata *p;
252
253 p = kzalloc(sizeof(*p), GFP_KERNEL);
254 if (p == NULL)
255 goto err;
256 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
257 if (p->o_arg.seqid == NULL)
258 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400259 p->path.mnt = mntget(path->mnt);
260 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100261 p->dir = parent;
262 p->owner = sp;
263 atomic_inc(&sp->so_count);
264 p->o_arg.fh = NFS_FH(dir);
265 p->o_arg.open_flags = flags,
David Howells7539bba2006-08-22 20:06:09 -0400266 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400267 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400268 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100269 p->o_arg.server = server;
270 p->o_arg.bitmask = server->attr_bitmask;
271 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100272 if (flags & O_EXCL) {
273 u32 *s = (u32 *) p->o_arg.u.verifier.data;
274 s[0] = jiffies;
275 s[1] = current->pid;
276 } else if (flags & O_CREAT) {
277 p->o_arg.u.attrs = &p->attrs;
278 memcpy(&p->attrs, attrs, sizeof(p->attrs));
279 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100280 p->c_arg.fh = &p->o_res.fh;
281 p->c_arg.stateid = &p->o_res.stateid;
282 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400283 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400284 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100285 return p;
286err_free:
287 kfree(p);
288err:
289 dput(parent);
290 return NULL;
291}
292
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400293static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100294{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400295 struct nfs4_opendata *p = container_of(kref,
296 struct nfs4_opendata, kref);
297
298 nfs_free_seqid(p->o_arg.seqid);
299 nfs4_put_state_owner(p->owner);
300 dput(p->dir);
301 dput(p->path.dentry);
302 mntput(p->path.mnt);
303 kfree(p);
304}
305
306static void nfs4_opendata_put(struct nfs4_opendata *p)
307{
308 if (p != NULL)
309 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100310}
311
Trond Myklebust06f814a2006-01-03 09:55:07 +0100312static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
313{
314 sigset_t oldset;
315 int ret;
316
317 rpc_clnt_sigmask(task->tk_client, &oldset);
318 ret = rpc_wait_for_completion_task(task);
319 rpc_clnt_sigunmask(task->tk_client, &oldset);
320 return ret;
321}
322
Trond Myklebust003707c2007-07-05 18:07:55 -0400323static void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
Trond Myklebuste7616922006-01-03 09:55:13 +0100324{
325 switch (open_flags) {
326 case FMODE_WRITE:
327 state->n_wronly++;
328 break;
329 case FMODE_READ:
330 state->n_rdonly++;
331 break;
332 case FMODE_READ|FMODE_WRITE:
333 state->n_rdwr++;
334 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400335 nfs4_state_set_mode_locked(state, state->state | open_flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100336}
337
Trond Myklebust003707c2007-07-05 18:07:55 -0400338static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
339{
340 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
341 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
342 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
343 switch (open_flags) {
344 case FMODE_READ:
345 set_bit(NFS_O_RDONLY_STATE, &state->flags);
346 break;
347 case FMODE_WRITE:
348 set_bit(NFS_O_WRONLY_STATE, &state->flags);
349 break;
350 case FMODE_READ|FMODE_WRITE:
351 set_bit(NFS_O_RDWR_STATE, &state->flags);
352 }
353}
354
355static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
356{
357 spin_lock(&state->owner->so_lock);
358 spin_lock(&state->inode->i_lock);
359 nfs_set_open_stateid_locked(state, stateid, open_flags);
360 spin_unlock(&state->inode->i_lock);
361 spin_unlock(&state->owner->so_lock);
362}
363
364static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *deleg_stateid, int open_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 struct inode *inode = state->inode;
367
368 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebustd5308382005-11-04 15:33:38 -0500369 /* Protect against nfs4_find_state_byowner() */
Trond Myklebustec073422005-10-20 14:22:47 -0700370 spin_lock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 spin_lock(&inode->i_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400372 if (deleg_stateid != NULL) {
373 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
374 set_bit(NFS_DELEGATED_STATE, &state->flags);
375 }
376 if (open_stateid != NULL)
377 nfs_set_open_stateid_locked(state, open_stateid, open_flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100378 update_open_stateflags(state, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700380 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100383static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
384{
385 struct inode *inode;
386 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400387 struct nfs_delegation *delegation;
388 nfs4_stateid *deleg_stateid = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400389 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100390
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400391 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100392 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400393 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100394 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400395 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500396 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400397 goto err;
398 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100399 state = nfs4_get_open_state(inode, data->owner);
400 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400401 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400402 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400403 int delegation_flags = 0;
404
Trond Myklebust003707c2007-07-05 18:07:55 -0400405 rcu_read_lock();
406 delegation = rcu_dereference(NFS_I(inode)->delegation);
407 if (delegation)
408 delegation_flags = delegation->flags;
409 rcu_read_unlock();
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400410 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
411 nfs_inode_set_delegation(state->inode,
412 data->owner->so_cred,
413 &data->o_res);
414 else
415 nfs_inode_reclaim_delegation(state->inode,
416 data->owner->so_cred,
417 &data->o_res);
418 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400419 rcu_read_lock();
420 delegation = rcu_dereference(NFS_I(inode)->delegation);
421 if (delegation != NULL)
422 deleg_stateid = &delegation->stateid;
423 update_open_stateid(state, &data->o_res.stateid, deleg_stateid, data->o_arg.open_flags);
424 rcu_read_unlock();
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100425 iput(inode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100426 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400427err_put_inode:
428 iput(inode);
429err:
430 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100431}
432
Trond Myklebust864472e2006-01-03 09:55:15 +0100433static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
434{
435 struct nfs_inode *nfsi = NFS_I(state->inode);
436 struct nfs_open_context *ctx;
437
438 spin_lock(&state->inode->i_lock);
439 list_for_each_entry(ctx, &nfsi->open_files, list) {
440 if (ctx->state != state)
441 continue;
442 get_nfs_open_context(ctx);
443 spin_unlock(&state->inode->i_lock);
444 return ctx;
445 }
446 spin_unlock(&state->inode->i_lock);
447 return ERR_PTR(-ENOENT);
448}
449
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400450static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100451{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400452 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100453 int ret;
454
455 opendata->o_arg.open_flags = openflags;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400456 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
457 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
458 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100459 ret = _nfs4_proc_open(opendata);
460 if (ret != 0)
461 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400462 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400463 if (IS_ERR(newstate))
464 return PTR_ERR(newstate);
465 nfs4_close_state(&opendata->path, newstate, openflags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400466 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100467 return 0;
468}
469
470static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
471{
Trond Myklebust864472e2006-01-03 09:55:15 +0100472 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100473 int ret;
474
475 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400476 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100477 smp_rmb();
478 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400479 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100480 if (ret != 0)
481 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400482 if (newstate != state)
483 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100484 }
485 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400486 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100487 if (ret != 0)
488 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400489 if (newstate != state)
490 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100491 }
492 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400493 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100494 if (ret != 0)
495 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400496 if (newstate != state)
497 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100498 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100499 return 0;
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502/*
503 * OPEN_RECLAIM:
504 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400506static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Trond Myklebust864472e2006-01-03 09:55:15 +0100508 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
509 struct nfs4_opendata *opendata;
510 int delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 int status;
512
513 if (delegation != NULL) {
514 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
515 memcpy(&state->stateid, &delegation->stateid,
516 sizeof(state->stateid));
517 set_bit(NFS_DELEGATED_STATE, &state->flags);
518 return 0;
519 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100520 delegation_type = delegation->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Trond Myklebustad389da2007-06-05 12:30:00 -0400522 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
Trond Myklebust864472e2006-01-03 09:55:15 +0100523 if (opendata == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700524 return -ENOMEM;
Trond Myklebust864472e2006-01-03 09:55:15 +0100525 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
526 opendata->o_arg.fh = NFS_FH(state->inode);
527 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
528 opendata->o_arg.u.delegation_type = delegation_type;
529 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400530 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return status;
532}
533
Trond Myklebust539cd032007-06-05 11:46:42 -0400534static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct nfs_server *server = NFS_SERVER(state->inode);
537 struct nfs4_exception exception = { };
538 int err;
539 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400540 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000541 if (err != -NFS4ERR_DELAY)
542 break;
543 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } while (exception.retry);
545 return err;
546}
547
Trond Myklebust864472e2006-01-03 09:55:15 +0100548static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
549{
550 struct nfs_open_context *ctx;
551 int ret;
552
553 ctx = nfs4_state_find_open_context(state);
554 if (IS_ERR(ctx))
555 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400556 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100557 put_nfs_open_context(ctx);
558 return ret;
559}
560
Trond Myklebust13437e12007-07-06 15:10:43 -0400561static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct nfs4_state_owner *sp = state->owner;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100564 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100565 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Trond Myklebustad389da2007-06-05 12:30:00 -0400567 opendata = nfs4_opendata_alloc(&ctx->path, sp, 0, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100568 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100569 return -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100570 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -0400571 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100572 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100573 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400574 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100575 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Trond Myklebust13437e12007-07-06 15:10:43 -0400578int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -0400581 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int err;
583 do {
Trond Myklebust13437e12007-07-06 15:10:43 -0400584 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 switch (err) {
586 case 0:
587 return err;
588 case -NFS4ERR_STALE_CLIENTID:
589 case -NFS4ERR_STALE_STATEID:
590 case -NFS4ERR_EXPIRED:
591 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -0400592 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return err;
594 }
595 err = nfs4_handle_exception(server, err, &exception);
596 } while (exception.retry);
597 return err;
598}
599
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100600static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100602 struct nfs4_opendata *data = calldata;
603 struct rpc_message msg = {
604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
605 .rpc_argp = &data->c_arg,
606 .rpc_resp = &data->c_res,
607 .rpc_cred = data->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 };
Trond Myklebust26e976a2006-01-03 09:55:21 +0100609 data->timestamp = jiffies;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100610 rpc_call_setup(task, &msg, 0);
611}
612
613static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
614{
615 struct nfs4_opendata *data = calldata;
616
617 data->rpc_status = task->tk_status;
618 if (RPC_ASSASSINATED(task))
619 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100620 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100621 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
622 sizeof(data->o_res.stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +0100623 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -0400624 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100625 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100626 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400627 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100628}
629
630static void nfs4_open_confirm_release(void *calldata)
631{
632 struct nfs4_opendata *data = calldata;
633 struct nfs4_state *state = NULL;
634
635 /* If this request hasn't been cancelled, do nothing */
636 if (data->cancelled == 0)
637 goto out_free;
638 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -0400639 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100640 goto out_free;
641 nfs_confirm_seqid(&data->owner->so_seqid, 0);
642 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400643 if (!IS_ERR(state))
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400644 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100645out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400646 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100647}
648
649static const struct rpc_call_ops nfs4_open_confirm_ops = {
650 .rpc_call_prepare = nfs4_open_confirm_prepare,
651 .rpc_call_done = nfs4_open_confirm_done,
652 .rpc_release = nfs4_open_confirm_release,
653};
654
655/*
656 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
657 */
658static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
659{
660 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
661 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 int status;
663
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400664 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -0400665 data->rpc_done = 0;
666 data->rpc_status = 0;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100667 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500668 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100669 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100670 status = nfs4_wait_for_completion_rpc_task(task);
671 if (status != 0) {
672 data->cancelled = 1;
673 smp_wmb();
674 } else
675 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500676 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return status;
678}
679
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100680static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100682 struct nfs4_opendata *data = calldata;
683 struct nfs4_state_owner *sp = data->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct rpc_message msg = {
685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100686 .rpc_argp = &data->o_arg,
687 .rpc_resp = &data->o_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 .rpc_cred = sp->so_cred,
689 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100690
691 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
692 return;
693 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400694 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100695 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust864472e2006-01-03 09:55:15 +0100696 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
697 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust26e976a2006-01-03 09:55:21 +0100698 data->timestamp = jiffies;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100699 rpc_call_setup(task, &msg, 0);
700}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100702static void nfs4_open_done(struct rpc_task *task, void *calldata)
703{
704 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100706 data->rpc_status = task->tk_status;
707 if (RPC_ASSASSINATED(task))
708 return;
709 if (task->tk_status == 0) {
710 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700711 case S_IFREG:
712 break;
713 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100714 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700715 break;
716 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100717 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700718 break;
719 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100720 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700721 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100722 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400723 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
724 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700725 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100726 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
Trond Myklebust3e309912007-07-07 13:19:59 -0400727 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100728}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700729
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100730static void nfs4_open_release(void *calldata)
731{
732 struct nfs4_opendata *data = calldata;
733 struct nfs4_state *state = NULL;
734
735 /* If this request hasn't been cancelled, do nothing */
736 if (data->cancelled == 0)
737 goto out_free;
738 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -0400739 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100740 goto out_free;
741 /* In case we need an open_confirm, no cleanup! */
742 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
743 goto out_free;
744 nfs_confirm_seqid(&data->owner->so_seqid, 0);
745 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400746 if (!IS_ERR(state))
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400747 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100748out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400749 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100750}
751
752static const struct rpc_call_ops nfs4_open_ops = {
753 .rpc_call_prepare = nfs4_open_prepare,
754 .rpc_call_done = nfs4_open_done,
755 .rpc_release = nfs4_open_release,
756};
757
758/*
759 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
760 */
761static int _nfs4_proc_open(struct nfs4_opendata *data)
762{
763 struct inode *dir = data->dir->d_inode;
764 struct nfs_server *server = NFS_SERVER(dir);
765 struct nfs_openargs *o_arg = &data->o_arg;
766 struct nfs_openres *o_res = &data->o_res;
767 struct rpc_task *task;
768 int status;
769
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400770 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -0400771 data->rpc_done = 0;
772 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400773 data->cancelled = 0;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100774 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500775 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100776 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100777 status = nfs4_wait_for_completion_rpc_task(task);
778 if (status != 0) {
779 data->cancelled = 1;
780 smp_wmb();
781 } else
782 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500783 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -0400784 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100785 return status;
786
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400787 if (o_arg->open_flags & O_CREAT) {
788 update_changeattr(dir, &o_res->cinfo);
789 nfs_post_op_update_inode(dir, o_res->dir_attr);
790 } else
791 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100793 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100795 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Howells8fa5c002006-08-22 20:06:12 -0400798 return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
803{
804 struct nfs_access_entry cache;
805 int mask = 0;
806 int status;
807
808 if (openflags & FMODE_READ)
809 mask |= MAY_READ;
810 if (openflags & FMODE_WRITE)
811 mask |= MAY_WRITE;
Trond Myklebust1b45c462007-07-03 13:04:56 -0400812 if (openflags & FMODE_EXEC)
813 mask |= MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 status = nfs_access_get_cached(inode, cred, &cache);
815 if (status == 0)
816 goto out;
817
818 /* Be clever: ask server to check for all possible rights */
819 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
820 cache.cred = cred;
821 cache.jiffies = jiffies;
822 status = _nfs4_proc_access(inode, &cache);
823 if (status != 0)
824 return status;
825 nfs_access_add_cache(inode, &cache);
826out:
827 if ((cache.mask & mask) == mask)
828 return 0;
829 return -EACCES;
830}
831
Trond Myklebust10afec92007-05-14 17:16:04 -0400832static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +0100833{
David Howells7539bba2006-08-22 20:06:09 -0400834 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -0400835 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +0100836
Trond Myklebust6b309542006-09-14 14:03:14 -0400837 for (;;) {
838 ret = nfs4_wait_clnt_recover(server->client, clp);
839 if (ret != 0)
840 return ret;
841 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
842 break;
Trond Myklebust58d97142006-01-03 09:55:24 +0100843 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -0400844 }
845 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +0100846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848/*
849 * OPEN_EXPIRED:
850 * reclaim state on the server after a network partition.
851 * Assumes caller holds the appropriate lock
852 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400853static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct inode *inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857 struct nfs4_opendata *opendata;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100858 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
Trond Myklebust864472e2006-01-03 09:55:15 +0100859 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
Trond Myklebust539cd032007-06-05 11:46:42 -0400862 ret = _nfs4_do_access(inode, ctx->cred, openflags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100863 if (ret < 0)
864 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
866 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Trond Myklebustad389da2007-06-05 12:30:00 -0400869 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100871 return -ENOMEM;
872 ret = nfs4_open_recover(opendata, state);
873 if (ret == -ESTALE) {
874 /* Invalidate the state owner so we don't ever use it again */
Trond Myklebust539cd032007-06-05 11:46:42 -0400875 nfs4_drop_state_owner(state->owner);
876 d_drop(ctx->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400878 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100879 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Trond Myklebust539cd032007-06-05 11:46:42 -0400882static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +0000883{
Trond Myklebust539cd032007-06-05 11:46:42 -0400884 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000885 struct nfs4_exception exception = { };
886 int err;
887
888 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400889 err = _nfs4_open_expired(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000890 if (err == -NFS4ERR_DELAY)
891 nfs4_handle_exception(server, err, &exception);
892 } while (exception.retry);
893 return err;
894}
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
897{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +0100899 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Trond Myklebust864472e2006-01-03 09:55:15 +0100901 ctx = nfs4_state_find_open_context(state);
902 if (IS_ERR(ctx))
903 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400904 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100905 put_nfs_open_context(ctx);
906 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100910 * Returns a referenced nfs4_state if there is an open delegation on the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
912static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
913{
914 struct nfs_delegation *delegation;
915 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -0400916 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct nfs_inode *nfsi = NFS_I(inode);
918 struct nfs4_state_owner *sp = NULL;
919 struct nfs4_state *state = NULL;
920 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
921 int err;
922
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100923 err = -ENOMEM;
924 if (!(sp = nfs4_get_state_owner(server, cred))) {
925 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
926 return err;
927 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100928 err = nfs4_recover_expired_lease(server);
929 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100930 goto out_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* Protect against reboot recovery - NOTE ORDER! */
932 down_read(&clp->cl_sem);
933 /* Protect against delegation recall */
934 down_read(&nfsi->rwsem);
935 delegation = NFS_I(inode)->delegation;
936 err = -ENOENT;
937 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
938 goto out_err;
939 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 state = nfs4_get_open_state(inode, sp);
941 if (state == NULL)
942 goto out_err;
943
944 err = -ENOENT;
945 if ((state->state & open_flags) == open_flags) {
946 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100947 update_open_stateflags(state, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 spin_unlock(&inode->i_lock);
949 goto out_ok;
950 } else if (state->state != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100951 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 lock_kernel();
954 err = _nfs4_do_access(inode, cred, open_flags);
955 unlock_kernel();
956 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100957 goto out_put_open_state;
Trond Myklebust003707c2007-07-05 18:07:55 -0400958 update_open_stateid(state, NULL, &delegation->stateid, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 nfs4_put_state_owner(sp);
961 up_read(&nfsi->rwsem);
962 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 *res = state;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100964 return 0;
965out_put_open_state:
966 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 up_read(&nfsi->rwsem);
969 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700970 if (err != -EACCES)
971 nfs_inode_return_delegation(inode);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100972out_put_state_owner:
973 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return err;
975}
976
977static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
978{
979 struct nfs4_exception exception = { };
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500980 struct nfs4_state *res = ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 int err;
982
983 do {
984 err = _nfs4_open_delegated(inode, flags, cred, &res);
985 if (err == 0)
986 break;
987 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
988 err, &exception));
989 } while (exception.retry);
990 return res;
991}
992
993/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -0400994 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
995 * fields corresponding to attributes that were used to store the verifier.
996 * Make sure we clobber those fields in the later setattr call
997 */
998static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
999{
1000 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1001 !(sattr->ia_valid & ATTR_ATIME_SET))
1002 sattr->ia_valid |= ATTR_ATIME;
1003
1004 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1005 !(sattr->ia_valid & ATTR_MTIME_SET))
1006 sattr->ia_valid |= ATTR_MTIME;
1007}
1008
1009/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001010 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 */
Trond Myklebustad389da2007-06-05 12:30:00 -04001012static int _nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct nfs4_state_owner *sp;
1015 struct nfs4_state *state = NULL;
1016 struct nfs_server *server = NFS_SERVER(dir);
David Howells7539bba2006-08-22 20:06:09 -04001017 struct nfs_client *clp = server->nfs_client;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001018 struct nfs4_opendata *opendata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 status = -ENOMEM;
1023 if (!(sp = nfs4_get_state_owner(server, cred))) {
1024 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1025 goto out_err;
1026 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001027 status = nfs4_recover_expired_lease(server);
1028 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001029 goto err_put_state_owner;
Trond Myklebust58d97142006-01-03 09:55:24 +01001030 down_read(&clp->cl_sem);
1031 status = -ENOMEM;
Trond Myklebustad389da2007-06-05 12:30:00 -04001032 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001033 if (opendata == NULL)
Trond Myklebust76723de2006-09-15 08:11:51 -04001034 goto err_release_rwsem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001036 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001038 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001040 if (opendata->o_arg.open_flags & O_EXCL)
1041 nfs4_exclusive_attrset(opendata, sattr);
1042
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001043 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001044 status = PTR_ERR(state);
1045 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001046 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001047 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 nfs4_put_state_owner(sp);
1049 up_read(&clp->cl_sem);
1050 *res = state;
1051 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001052err_opendata_put:
1053 nfs4_opendata_put(opendata);
Trond Myklebust76723de2006-09-15 08:11:51 -04001054err_release_rwsem:
1055 up_read(&clp->cl_sem);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001056err_put_state_owner:
1057 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 *res = NULL;
1060 return status;
1061}
1062
1063
Trond Myklebustad389da2007-06-05 12:30:00 -04001064static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct nfs4_exception exception = { };
1067 struct nfs4_state *res;
1068 int status;
1069
1070 do {
Trond Myklebustad389da2007-06-05 12:30:00 -04001071 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (status == 0)
1073 break;
1074 /* NOTE: BAD_SEQID means the server and client disagree about the
1075 * book-keeping w.r.t. state-changing operations
1076 * (OPEN/CLOSE/LOCK/LOCKU...)
1077 * It is actually a sign of a bug on the client or on the server.
1078 *
1079 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001080 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 * have unhashed the old state_owner for us, and that we can
1082 * therefore safely retry using a new one. We should still warn
1083 * the user though...
1084 */
1085 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001086 printk(KERN_WARNING "NFS: v4 server %s "
1087 " returned a bad sequence-id error!\n",
1088 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 exception.retry = 1;
1090 continue;
1091 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001092 /*
1093 * BAD_STATEID on OPEN means that the server cancelled our
1094 * state before it received the OPEN_CONFIRM.
1095 * Recover by retrying the request as per the discussion
1096 * on Page 181 of RFC3530.
1097 */
1098 if (status == -NFS4ERR_BAD_STATEID) {
1099 exception.retry = 1;
1100 continue;
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1103 status, &exception));
1104 } while (exception.retry);
1105 return res;
1106}
1107
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001108static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1109 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001111 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001113 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 .iap = sattr,
1115 .server = server,
1116 .bitmask = server->attr_bitmask,
1117 };
1118 struct nfs_setattrres res = {
1119 .fattr = fattr,
1120 .server = server,
1121 };
1122 struct rpc_message msg = {
1123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1124 .rpc_argp = &arg,
1125 .rpc_resp = &res,
1126 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001127 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001128 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Trond Myklebust0e574af2005-10-27 22:12:38 -04001130 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001132 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1133 /* Use that stateid */
1134 } else if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001136 nfs4_copy_stateid(&arg.stateid, state, current->files);
1137 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1139
Trond Myklebust65e43082005-08-16 11:49:44 -04001140 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001141 if (status == 0 && state != NULL)
1142 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001143 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001146static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1147 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001149 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 struct nfs4_exception exception = { };
1151 int err;
1152 do {
1153 err = nfs4_handle_exception(server,
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001154 _nfs4_do_setattr(inode, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 &exception);
1156 } while (exception.retry);
1157 return err;
1158}
1159
1160struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001161 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct inode *inode;
1163 struct nfs4_state *state;
1164 struct nfs_closeargs arg;
1165 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001166 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001167 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168};
1169
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001170static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001171{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001172 struct nfs4_closedata *calldata = data;
1173 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001174
1175 nfs4_put_open_state(calldata->state);
1176 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001177 nfs4_put_state_owner(sp);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001178 dput(calldata->path.dentry);
1179 mntput(calldata->path.mnt);
Trond Myklebust95121352005-10-18 14:20:12 -07001180 kfree(calldata);
1181}
1182
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001183static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001185 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 struct nfs_server *server = NFS_SERVER(calldata->inode);
1188
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001189 if (RPC_ASSASSINATED(task))
1190 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /* hmm. we are done with the inode, and in the process of freeing
1192 * the state_owner. we keep this around to process errors
1193 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001194 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 switch (task->tk_status) {
1196 case 0:
Trond Myklebust003707c2007-07-05 18:07:55 -04001197 nfs_set_open_stateid(state, &calldata->res.stateid, calldata->arg.open_flags);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001198 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 break;
1200 case -NFS4ERR_STALE_STATEID:
1201 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 break;
1203 default:
1204 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1205 rpc_restart_call(task);
1206 return;
1207 }
1208 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001209 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001212static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001214 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001215 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 struct rpc_message msg = {
1217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1218 .rpc_argp = &calldata->arg,
1219 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001220 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 };
Trond Myklebust003707c2007-07-05 18:07:55 -04001222 int clear_rd, clear_wr, clear_rdwr;
1223 int mode;
Trond Myklebust95121352005-10-18 14:20:12 -07001224
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001225 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001226 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001227
1228 mode = FMODE_READ|FMODE_WRITE;
1229 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001230 spin_lock(&state->owner->so_lock);
1231 spin_lock(&calldata->inode->i_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001232 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001233 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001234 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001235 mode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001236 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1237 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1238 }
1239 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001240 mode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001241 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1242 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1243 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001244 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001245 spin_unlock(&calldata->inode->i_lock);
1246 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001247 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001248 /* Note: exit _without_ calling nfs4_close_done */
1249 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001250 return;
1251 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001252 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -07001253 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -07001255 calldata->arg.open_flags = mode;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001256 calldata->timestamp = jiffies;
Trond Myklebust95121352005-10-18 14:20:12 -07001257 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001260static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001261 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001262 .rpc_call_done = nfs4_close_done,
1263 .rpc_release = nfs4_free_closedata,
1264};
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266/*
1267 * It is possible for data to be read/written from a mem-mapped file
1268 * after the sys_close call (which hits the vfs layer as a flush).
1269 * This means that we can't safely call nfsv4 close on a file until
1270 * the inode is cleared. This in turn means that we are not good
1271 * NFSv4 citizens - we do not indicate to the server to update the file's
1272 * share state even when we are done with one of the three share
1273 * stateid's in the inode.
1274 *
1275 * NOTE: Caller must be holding the sp->so_owner semaphore!
1276 */
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001277int nfs4_do_close(struct path *path, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001279 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001281 struct nfs4_state_owner *sp = state->owner;
1282 struct rpc_task *task;
Trond Myklebust95121352005-10-18 14:20:12 -07001283 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Trond Myklebust95121352005-10-18 14:20:12 -07001285 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001287 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001288 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001290 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001291 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001293 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001294 if (calldata->arg.seqid == NULL)
1295 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001296 calldata->arg.bitmask = server->attr_bitmask;
1297 calldata->res.fattr = &calldata->fattr;
1298 calldata->res.server = server;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001299 calldata->path.mnt = mntget(path->mnt);
1300 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001301
Trond Myklebustb39e6252007-06-11 23:05:07 -04001302 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1303 if (IS_ERR(task))
1304 return PTR_ERR(task);
1305 rpc_put_task(task);
1306 return 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001307out_free_calldata:
1308 kfree(calldata);
1309out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001310 nfs4_put_open_state(state);
1311 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001312 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001315static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
Trond Myklebust02a913a2005-10-18 14:20:17 -07001316{
1317 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001318 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001319
Trond Myklebust1b45c462007-07-03 13:04:56 -04001320 /* If the open_intent is for execute, we have an extra check to make */
1321 if (nd->intent.open.flags & FMODE_EXEC) {
1322 ret = _nfs4_do_access(state->inode,
1323 state->owner->so_cred,
1324 nd->intent.open.flags);
1325 if (ret < 0)
1326 goto out_close;
1327 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001328 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001329 if (!IS_ERR(filp)) {
1330 struct nfs_open_context *ctx;
1331 ctx = (struct nfs_open_context *)filp->private_data;
1332 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001333 return 0;
1334 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001335 ret = PTR_ERR(filp);
1336out_close:
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001337 nfs4_close_state(path, state, nd->intent.open.flags);
Trond Myklebust1b45c462007-07-03 13:04:56 -04001338 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001339}
1340
1341struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1343{
Trond Myklebustad389da2007-06-05 12:30:00 -04001344 struct path path = {
1345 .mnt = nd->mnt,
1346 .dentry = dentry,
1347 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 struct iattr attr;
1349 struct rpc_cred *cred;
1350 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001351 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (nd->flags & LOOKUP_CREATE) {
1354 attr.ia_mode = nd->intent.open.create_mode;
1355 attr.ia_valid = ATTR_MODE;
1356 if (!IS_POSIXACL(dir))
1357 attr.ia_mode &= ~current->fs->umask;
1358 } else {
1359 attr.ia_valid = 0;
1360 BUG_ON(nd->intent.open.flags & O_CREAT);
1361 }
1362
David Howells1f163412006-08-22 20:06:11 -04001363 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001365 return (struct dentry *)cred;
Trond Myklebustad389da2007-06-05 12:30:00 -04001366 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001368 if (IS_ERR(state)) {
1369 if (PTR_ERR(state) == -ENOENT)
1370 d_add(dentry, NULL);
1371 return (struct dentry *)state;
1372 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001373 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001374 if (res != NULL)
1375 dentry = res;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001376 nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001377 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
1380int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001381nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Trond Myklebustad389da2007-06-05 12:30:00 -04001383 struct path path = {
1384 .mnt = nd->mnt,
1385 .dentry = dentry,
1386 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 struct rpc_cred *cred;
1388 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
David Howells1f163412006-08-22 20:06:11 -04001390 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (IS_ERR(cred))
1392 return PTR_ERR(cred);
1393 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1394 if (IS_ERR(state))
Trond Myklebustad389da2007-06-05 12:30:00 -04001395 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001397 if (IS_ERR(state)) {
1398 switch (PTR_ERR(state)) {
1399 case -EPERM:
1400 case -EACCES:
1401 case -EDQUOT:
1402 case -ENOSPC:
1403 case -EROFS:
1404 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1405 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001406 default:
1407 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001408 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001409 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410 if (state->inode == dentry->d_inode) {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001411 nfs4_intent_set_file(nd, &path, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return 1;
1413 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001414 nfs4_close_state(&path, state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001415out_drop:
1416 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return 0;
1418}
1419
1420
1421static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1422{
1423 struct nfs4_server_caps_res res = {};
1424 struct rpc_message msg = {
1425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1426 .rpc_argp = fhandle,
1427 .rpc_resp = &res,
1428 };
1429 int status;
1430
1431 status = rpc_call_sync(server->client, &msg, 0);
1432 if (status == 0) {
1433 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1434 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1435 server->caps |= NFS_CAP_ACLS;
1436 if (res.has_links != 0)
1437 server->caps |= NFS_CAP_HARDLINKS;
1438 if (res.has_symlinks != 0)
1439 server->caps |= NFS_CAP_SYMLINKS;
1440 server->acl_bitmask = res.acl_bitmask;
1441 }
1442 return status;
1443}
1444
Trond Myklebust55a97592006-06-09 09:34:19 -04001445int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 struct nfs4_exception exception = { };
1448 int err;
1449 do {
1450 err = nfs4_handle_exception(server,
1451 _nfs4_server_capabilities(server, fhandle),
1452 &exception);
1453 } while (exception.retry);
1454 return err;
1455}
1456
1457static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1458 struct nfs_fsinfo *info)
1459{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 struct nfs4_lookup_root_arg args = {
1461 .bitmask = nfs4_fattr_bitmap,
1462 };
1463 struct nfs4_lookup_res res = {
1464 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001465 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 .fh = fhandle,
1467 };
1468 struct rpc_message msg = {
1469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1470 .rpc_argp = &args,
1471 .rpc_resp = &res,
1472 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001473 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return rpc_call_sync(server->client, &msg, 0);
1475}
1476
1477static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1478 struct nfs_fsinfo *info)
1479{
1480 struct nfs4_exception exception = { };
1481 int err;
1482 do {
1483 err = nfs4_handle_exception(server,
1484 _nfs4_lookup_root(server, fhandle, info),
1485 &exception);
1486 } while (exception.retry);
1487 return err;
1488}
1489
David Howells54ceac42006-08-22 20:06:13 -04001490/*
1491 * get the file handle for the "/" directory on the server
1492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001494 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 int status;
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (status == 0)
1500 status = nfs4_server_capabilities(server, fhandle);
1501 if (status == 0)
1502 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001503 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Manoj Naik6b97fd32006-06-09 09:34:29 -04001506/*
1507 * Get locations and (maybe) other attributes of a referral.
1508 * Note that we'll actually follow the referral later when
1509 * we detect fsid mismatch in inode revalidation
1510 */
1511static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1512{
1513 int status = -ENOMEM;
1514 struct page *page = NULL;
1515 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001516
1517 page = alloc_page(GFP_KERNEL);
1518 if (page == NULL)
1519 goto out;
1520 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1521 if (locations == NULL)
1522 goto out;
1523
Trond Myklebustc228fd32007-01-13 02:28:11 -05001524 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001525 if (status != 0)
1526 goto out;
1527 /* Make sure server returned a different fsid for the referral */
1528 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1529 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1530 status = -EIO;
1531 goto out;
1532 }
1533
1534 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1535 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1536 if (!fattr->mode)
1537 fattr->mode = S_IFDIR;
1538 memset(fhandle, 0, sizeof(struct nfs_fh));
1539out:
1540 if (page)
1541 __free_page(page);
1542 if (locations)
1543 kfree(locations);
1544 return status;
1545}
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1548{
1549 struct nfs4_getattr_arg args = {
1550 .fh = fhandle,
1551 .bitmask = server->attr_bitmask,
1552 };
1553 struct nfs4_getattr_res res = {
1554 .fattr = fattr,
1555 .server = server,
1556 };
1557 struct rpc_message msg = {
1558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1559 .rpc_argp = &args,
1560 .rpc_resp = &res,
1561 };
1562
Trond Myklebust0e574af2005-10-27 22:12:38 -04001563 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return rpc_call_sync(server->client, &msg, 0);
1565}
1566
1567static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1568{
1569 struct nfs4_exception exception = { };
1570 int err;
1571 do {
1572 err = nfs4_handle_exception(server,
1573 _nfs4_proc_getattr(server, fhandle, fattr),
1574 &exception);
1575 } while (exception.retry);
1576 return err;
1577}
1578
1579/*
1580 * The file is not closed if it is opened due to the a request to change
1581 * the size of the file. The open call will not be needed once the
1582 * VFS layer lookup-intents are implemented.
1583 *
1584 * Close is called when the inode is destroyed.
1585 * If we haven't opened the file for O_WRONLY, we
1586 * need to in the size_change case to obtain a stateid.
1587 *
1588 * Got race?
1589 * Because OPEN is always done by name in nfsv4, it is
1590 * possible that we opened a different file by the same
1591 * name. We can recognize this race condition, but we
1592 * can't do anything about it besides returning an error.
1593 *
1594 * This will be fixed with VFS changes (lookup-intent).
1595 */
1596static int
1597nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1598 struct iattr *sattr)
1599{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001600 struct rpc_cred *cred;
1601 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001602 struct nfs_open_context *ctx;
1603 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 int status;
1605
Trond Myklebust0e574af2005-10-27 22:12:38 -04001606 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
David Howells1f163412006-08-22 20:06:11 -04001608 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001609 if (IS_ERR(cred))
1610 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001611
1612 /* Search for an existing open(O_WRITE) file */
1613 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1614 if (ctx != NULL)
1615 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001616
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001617 status = nfs4_do_setattr(inode, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001618 if (status == 0)
1619 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001620 if (ctx != NULL)
1621 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001622 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return status;
1624}
1625
David Howells2b3de442006-08-22 20:06:09 -04001626static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1627 struct qstr *name, struct nfs_fh *fhandle,
1628 struct nfs_fattr *fattr)
1629{
1630 int status;
1631 struct nfs4_lookup_arg args = {
1632 .bitmask = server->attr_bitmask,
1633 .dir_fh = dirfh,
1634 .name = name,
1635 };
1636 struct nfs4_lookup_res res = {
1637 .server = server,
1638 .fattr = fattr,
1639 .fh = fhandle,
1640 };
1641 struct rpc_message msg = {
1642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1643 .rpc_argp = &args,
1644 .rpc_resp = &res,
1645 };
1646
1647 nfs_fattr_init(fattr);
1648
1649 dprintk("NFS call lookupfh %s\n", name->name);
1650 status = rpc_call_sync(server->client, &msg, 0);
1651 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04001652 return status;
1653}
1654
1655static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1656 struct qstr *name, struct nfs_fh *fhandle,
1657 struct nfs_fattr *fattr)
1658{
1659 struct nfs4_exception exception = { };
1660 int err;
1661 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04001662 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1663 /* FIXME: !!!! */
1664 if (err == -NFS4ERR_MOVED) {
1665 err = -EREMOTE;
1666 break;
1667 }
1668 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04001669 } while (exception.retry);
1670 return err;
1671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1674 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1675{
Trond Myklebust4e56e082007-07-01 18:13:52 -04001676 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04001679 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001680 if (status == -NFS4ERR_MOVED)
1681 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 dprintk("NFS reply lookup: %d\n", status);
1683 return status;
1684}
1685
1686static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1687{
1688 struct nfs4_exception exception = { };
1689 int err;
1690 do {
1691 err = nfs4_handle_exception(NFS_SERVER(dir),
1692 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1693 &exception);
1694 } while (exception.retry);
1695 return err;
1696}
1697
1698static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1699{
1700 struct nfs4_accessargs args = {
1701 .fh = NFS_FH(inode),
1702 };
1703 struct nfs4_accessres res = { 0 };
1704 struct rpc_message msg = {
1705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1706 .rpc_argp = &args,
1707 .rpc_resp = &res,
1708 .rpc_cred = entry->cred,
1709 };
1710 int mode = entry->mask;
1711 int status;
1712
1713 /*
1714 * Determine which access bits we want to ask for...
1715 */
1716 if (mode & MAY_READ)
1717 args.access |= NFS4_ACCESS_READ;
1718 if (S_ISDIR(inode->i_mode)) {
1719 if (mode & MAY_WRITE)
1720 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1721 if (mode & MAY_EXEC)
1722 args.access |= NFS4_ACCESS_LOOKUP;
1723 } else {
1724 if (mode & MAY_WRITE)
1725 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1726 if (mode & MAY_EXEC)
1727 args.access |= NFS4_ACCESS_EXECUTE;
1728 }
1729 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1730 if (!status) {
1731 entry->mask = 0;
1732 if (res.access & NFS4_ACCESS_READ)
1733 entry->mask |= MAY_READ;
1734 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1735 entry->mask |= MAY_WRITE;
1736 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1737 entry->mask |= MAY_EXEC;
1738 }
1739 return status;
1740}
1741
1742static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1743{
1744 struct nfs4_exception exception = { };
1745 int err;
1746 do {
1747 err = nfs4_handle_exception(NFS_SERVER(inode),
1748 _nfs4_proc_access(inode, entry),
1749 &exception);
1750 } while (exception.retry);
1751 return err;
1752}
1753
1754/*
1755 * TODO: For the time being, we don't try to get any attributes
1756 * along with any of the zero-copy operations READ, READDIR,
1757 * READLINK, WRITE.
1758 *
1759 * In the case of the first three, we want to put the GETATTR
1760 * after the read-type operation -- this is because it is hard
1761 * to predict the length of a GETATTR response in v4, and thus
1762 * align the READ data correctly. This means that the GETATTR
1763 * may end up partially falling into the page cache, and we should
1764 * shift it into the 'tail' of the xdr_buf before processing.
1765 * To do this efficiently, we need to know the total length
1766 * of data received, which doesn't seem to be available outside
1767 * of the RPC layer.
1768 *
1769 * In the case of WRITE, we also want to put the GETATTR after
1770 * the operation -- in this case because we want to make sure
1771 * we get the post-operation mtime and size. This means that
1772 * we can't use xdr_encode_pages() as written: we need a variant
1773 * of it which would leave room in the 'tail' iovec.
1774 *
1775 * Both of these changes to the XDR layer would in fact be quite
1776 * minor, but I decided to leave them for a subsequent patch.
1777 */
1778static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1779 unsigned int pgbase, unsigned int pglen)
1780{
1781 struct nfs4_readlink args = {
1782 .fh = NFS_FH(inode),
1783 .pgbase = pgbase,
1784 .pglen = pglen,
1785 .pages = &page,
1786 };
1787 struct rpc_message msg = {
1788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1789 .rpc_argp = &args,
1790 .rpc_resp = NULL,
1791 };
1792
1793 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1794}
1795
1796static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1797 unsigned int pgbase, unsigned int pglen)
1798{
1799 struct nfs4_exception exception = { };
1800 int err;
1801 do {
1802 err = nfs4_handle_exception(NFS_SERVER(inode),
1803 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1804 &exception);
1805 } while (exception.retry);
1806 return err;
1807}
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809/*
1810 * Got race?
1811 * We will need to arrange for the VFS layer to provide an atomic open.
1812 * Until then, this create/open method is prone to inefficiency and race
1813 * conditions due to the lookup, create, and open VFS calls from sys_open()
1814 * placed on the wire.
1815 *
1816 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1817 * The file will be opened again in the subsequent VFS open call
1818 * (nfs4_proc_file_open).
1819 *
1820 * The open for read will just hang around to be used by any process that
1821 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1822 */
1823
1824static int
1825nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001826 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Trond Myklebustad389da2007-06-05 12:30:00 -04001828 struct path path = {
1829 .mnt = nd->mnt,
1830 .dentry = dentry,
1831 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 struct nfs4_state *state;
1833 struct rpc_cred *cred;
1834 int status = 0;
1835
David Howells1f163412006-08-22 20:06:11 -04001836 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (IS_ERR(cred)) {
1838 status = PTR_ERR(cred);
1839 goto out;
1840 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001841 state = nfs4_do_open(dir, &path, flags, sattr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 put_rpccred(cred);
1843 if (IS_ERR(state)) {
1844 status = PTR_ERR(state);
1845 goto out;
1846 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001847 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (flags & O_EXCL) {
1849 struct nfs_fattr fattr;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001850 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001851 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001852 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001853 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001854 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001855 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001856 status = nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001857 else
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001858 nfs4_close_state(&path, state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859out:
1860 return status;
1861}
1862
1863static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1864{
Trond Myklebust16e42952005-10-27 22:12:44 -04001865 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 struct nfs4_remove_arg args = {
1867 .fh = NFS_FH(dir),
1868 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001869 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001871 struct nfs_fattr dir_attr;
1872 struct nfs4_remove_res res = {
1873 .server = server,
1874 .dir_attr = &dir_attr,
1875 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 struct rpc_message msg = {
1877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1878 .rpc_argp = &args,
1879 .rpc_resp = &res,
1880 };
1881 int status;
1882
Trond Myklebust16e42952005-10-27 22:12:44 -04001883 nfs_fattr_init(res.dir_attr);
1884 status = rpc_call_sync(server->client, &msg, 0);
1885 if (status == 0) {
1886 update_changeattr(dir, &res.cinfo);
1887 nfs_post_op_update_inode(dir, res.dir_attr);
1888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return status;
1890}
1891
1892static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1893{
1894 struct nfs4_exception exception = { };
1895 int err;
1896 do {
1897 err = nfs4_handle_exception(NFS_SERVER(dir),
1898 _nfs4_proc_remove(dir, name),
1899 &exception);
1900 } while (exception.retry);
1901 return err;
1902}
1903
1904struct unlink_desc {
1905 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001906 struct nfs4_remove_res res;
1907 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908};
1909
1910static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1911 struct qstr *name)
1912{
Trond Myklebust16e42952005-10-27 22:12:44 -04001913 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct unlink_desc *up;
1915
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001916 up = kmalloc(sizeof(*up), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (!up)
1918 return -ENOMEM;
1919
1920 up->args.fh = NFS_FH(dir->d_inode);
1921 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001922 up->args.bitmask = server->attr_bitmask;
1923 up->res.server = server;
1924 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1927 msg->rpc_argp = &up->args;
1928 msg->rpc_resp = &up->res;
1929 return 0;
1930}
1931
1932static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1933{
1934 struct rpc_message *msg = &task->tk_msg;
1935 struct unlink_desc *up;
1936
1937 if (msg->rpc_resp != NULL) {
1938 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001939 update_changeattr(dir->d_inode, &up->res.cinfo);
1940 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 kfree(up);
1942 msg->rpc_resp = NULL;
1943 msg->rpc_argp = NULL;
1944 }
1945 return 0;
1946}
1947
1948static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1949 struct inode *new_dir, struct qstr *new_name)
1950{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001951 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct nfs4_rename_arg arg = {
1953 .old_dir = NFS_FH(old_dir),
1954 .new_dir = NFS_FH(new_dir),
1955 .old_name = old_name,
1956 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001957 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001959 struct nfs_fattr old_fattr, new_fattr;
1960 struct nfs4_rename_res res = {
1961 .server = server,
1962 .old_fattr = &old_fattr,
1963 .new_fattr = &new_fattr,
1964 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct rpc_message msg = {
1966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1967 .rpc_argp = &arg,
1968 .rpc_resp = &res,
1969 };
1970 int status;
1971
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001972 nfs_fattr_init(res.old_fattr);
1973 nfs_fattr_init(res.new_fattr);
1974 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 if (!status) {
1977 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001978 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001980 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982 return status;
1983}
1984
1985static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1986 struct inode *new_dir, struct qstr *new_name)
1987{
1988 struct nfs4_exception exception = { };
1989 int err;
1990 do {
1991 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1992 _nfs4_proc_rename(old_dir, old_name,
1993 new_dir, new_name),
1994 &exception);
1995 } while (exception.retry);
1996 return err;
1997}
1998
1999static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2000{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002001 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 struct nfs4_link_arg arg = {
2003 .fh = NFS_FH(inode),
2004 .dir_fh = NFS_FH(dir),
2005 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002006 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002008 struct nfs_fattr fattr, dir_attr;
2009 struct nfs4_link_res res = {
2010 .server = server,
2011 .fattr = &fattr,
2012 .dir_attr = &dir_attr,
2013 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 struct rpc_message msg = {
2015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2016 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002017 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 };
2019 int status;
2020
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002021 nfs_fattr_init(res.fattr);
2022 nfs_fattr_init(res.dir_attr);
2023 status = rpc_call_sync(server->client, &msg, 0);
2024 if (!status) {
2025 update_changeattr(dir, &res.cinfo);
2026 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002027 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 return status;
2031}
2032
2033static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2034{
2035 struct nfs4_exception exception = { };
2036 int err;
2037 do {
2038 err = nfs4_handle_exception(NFS_SERVER(inode),
2039 _nfs4_proc_link(inode, dir, name),
2040 &exception);
2041 } while (exception.retry);
2042 return err;
2043}
2044
Chuck Lever4f390c12006-08-22 20:06:22 -04002045static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002046 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever4f390c12006-08-22 20:06:22 -04002049 struct nfs_fh fhandle;
2050 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 struct nfs4_create_arg arg = {
2052 .dir_fh = NFS_FH(dir),
2053 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002054 .name = &dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 .attrs = sattr,
2056 .ftype = NF4LNK,
2057 .bitmask = server->attr_bitmask,
2058 };
2059 struct nfs4_create_res res = {
2060 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002061 .fh = &fhandle,
2062 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002063 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 };
2065 struct rpc_message msg = {
2066 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2067 .rpc_argp = &arg,
2068 .rpc_resp = &res,
2069 };
2070 int status;
2071
Chuck Lever94a6d752006-08-22 20:06:23 -04002072 if (len > NFS4_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -04002074
Chuck Lever94a6d752006-08-22 20:06:23 -04002075 arg.u.symlink.pages = &page;
2076 arg.u.symlink.len = len;
Chuck Lever4f390c12006-08-22 20:06:22 -04002077 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002078 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Chuck Lever4f390c12006-08-22 20:06:22 -04002081 if (!status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 update_changeattr(dir, &res.dir_cinfo);
Chuck Lever4f390c12006-08-22 20:06:22 -04002083 nfs_post_op_update_inode(dir, res.dir_fattr);
2084 status = nfs_instantiate(dentry, &fhandle, &fattr);
2085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return status;
2087}
2088
Chuck Lever4f390c12006-08-22 20:06:22 -04002089static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002090 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 struct nfs4_exception exception = { };
2093 int err;
2094 do {
2095 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002096 _nfs4_proc_symlink(dir, dentry, page,
2097 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 &exception);
2099 } while (exception.retry);
2100 return err;
2101}
2102
2103static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2104 struct iattr *sattr)
2105{
2106 struct nfs_server *server = NFS_SERVER(dir);
2107 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002108 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 struct nfs4_create_arg arg = {
2110 .dir_fh = NFS_FH(dir),
2111 .server = server,
2112 .name = &dentry->d_name,
2113 .attrs = sattr,
2114 .ftype = NF4DIR,
2115 .bitmask = server->attr_bitmask,
2116 };
2117 struct nfs4_create_res res = {
2118 .server = server,
2119 .fh = &fhandle,
2120 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002121 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 };
2123 struct rpc_message msg = {
2124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2125 .rpc_argp = &arg,
2126 .rpc_resp = &res,
2127 };
2128 int status;
2129
Trond Myklebust0e574af2005-10-27 22:12:38 -04002130 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002131 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2134 if (!status) {
2135 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002136 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 status = nfs_instantiate(dentry, &fhandle, &fattr);
2138 }
2139 return status;
2140}
2141
2142static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2143 struct iattr *sattr)
2144{
2145 struct nfs4_exception exception = { };
2146 int err;
2147 do {
2148 err = nfs4_handle_exception(NFS_SERVER(dir),
2149 _nfs4_proc_mkdir(dir, dentry, sattr),
2150 &exception);
2151 } while (exception.retry);
2152 return err;
2153}
2154
2155static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2156 u64 cookie, struct page *page, unsigned int count, int plus)
2157{
2158 struct inode *dir = dentry->d_inode;
2159 struct nfs4_readdir_arg args = {
2160 .fh = NFS_FH(dir),
2161 .pages = &page,
2162 .pgbase = 0,
2163 .count = count,
2164 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2165 };
2166 struct nfs4_readdir_res res;
2167 struct rpc_message msg = {
2168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2169 .rpc_argp = &args,
2170 .rpc_resp = &res,
2171 .rpc_cred = cred,
2172 };
2173 int status;
2174
Trond Myklebusteadf4592005-06-22 17:16:39 +00002175 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2176 dentry->d_parent->d_name.name,
2177 dentry->d_name.name,
2178 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2180 res.pgbase = args.pgbase;
2181 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2182 if (status == 0)
2183 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebusteadf4592005-06-22 17:16:39 +00002184 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return status;
2186}
2187
2188static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2189 u64 cookie, struct page *page, unsigned int count, int plus)
2190{
2191 struct nfs4_exception exception = { };
2192 int err;
2193 do {
2194 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2195 _nfs4_proc_readdir(dentry, cred, cookie,
2196 page, count, plus),
2197 &exception);
2198 } while (exception.retry);
2199 return err;
2200}
2201
2202static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2203 struct iattr *sattr, dev_t rdev)
2204{
2205 struct nfs_server *server = NFS_SERVER(dir);
2206 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002207 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 struct nfs4_create_arg arg = {
2209 .dir_fh = NFS_FH(dir),
2210 .server = server,
2211 .name = &dentry->d_name,
2212 .attrs = sattr,
2213 .bitmask = server->attr_bitmask,
2214 };
2215 struct nfs4_create_res res = {
2216 .server = server,
2217 .fh = &fh,
2218 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002219 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 };
2221 struct rpc_message msg = {
2222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2223 .rpc_argp = &arg,
2224 .rpc_resp = &res,
2225 };
2226 int status;
2227 int mode = sattr->ia_mode;
2228
Trond Myklebust0e574af2005-10-27 22:12:38 -04002229 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002230 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2233 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2234 if (S_ISFIFO(mode))
2235 arg.ftype = NF4FIFO;
2236 else if (S_ISBLK(mode)) {
2237 arg.ftype = NF4BLK;
2238 arg.u.device.specdata1 = MAJOR(rdev);
2239 arg.u.device.specdata2 = MINOR(rdev);
2240 }
2241 else if (S_ISCHR(mode)) {
2242 arg.ftype = NF4CHR;
2243 arg.u.device.specdata1 = MAJOR(rdev);
2244 arg.u.device.specdata2 = MINOR(rdev);
2245 }
2246 else
2247 arg.ftype = NF4SOCK;
2248
2249 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2250 if (status == 0) {
2251 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002252 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 status = nfs_instantiate(dentry, &fh, &fattr);
2254 }
2255 return status;
2256}
2257
2258static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2259 struct iattr *sattr, dev_t rdev)
2260{
2261 struct nfs4_exception exception = { };
2262 int err;
2263 do {
2264 err = nfs4_handle_exception(NFS_SERVER(dir),
2265 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2266 &exception);
2267 } while (exception.retry);
2268 return err;
2269}
2270
2271static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2272 struct nfs_fsstat *fsstat)
2273{
2274 struct nfs4_statfs_arg args = {
2275 .fh = fhandle,
2276 .bitmask = server->attr_bitmask,
2277 };
2278 struct rpc_message msg = {
2279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2280 .rpc_argp = &args,
2281 .rpc_resp = fsstat,
2282 };
2283
Trond Myklebust0e574af2005-10-27 22:12:38 -04002284 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return rpc_call_sync(server->client, &msg, 0);
2286}
2287
2288static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2289{
2290 struct nfs4_exception exception = { };
2291 int err;
2292 do {
2293 err = nfs4_handle_exception(server,
2294 _nfs4_proc_statfs(server, fhandle, fsstat),
2295 &exception);
2296 } while (exception.retry);
2297 return err;
2298}
2299
2300static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2301 struct nfs_fsinfo *fsinfo)
2302{
2303 struct nfs4_fsinfo_arg args = {
2304 .fh = fhandle,
2305 .bitmask = server->attr_bitmask,
2306 };
2307 struct rpc_message msg = {
2308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2309 .rpc_argp = &args,
2310 .rpc_resp = fsinfo,
2311 };
2312
2313 return rpc_call_sync(server->client, &msg, 0);
2314}
2315
2316static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2317{
2318 struct nfs4_exception exception = { };
2319 int err;
2320
2321 do {
2322 err = nfs4_handle_exception(server,
2323 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2324 &exception);
2325 } while (exception.retry);
2326 return err;
2327}
2328
2329static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2330{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002331 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2333}
2334
2335static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2336 struct nfs_pathconf *pathconf)
2337{
2338 struct nfs4_pathconf_arg args = {
2339 .fh = fhandle,
2340 .bitmask = server->attr_bitmask,
2341 };
2342 struct rpc_message msg = {
2343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2344 .rpc_argp = &args,
2345 .rpc_resp = pathconf,
2346 };
2347
2348 /* None of the pathconf attributes are mandatory to implement */
2349 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2350 memset(pathconf, 0, sizeof(*pathconf));
2351 return 0;
2352 }
2353
Trond Myklebust0e574af2005-10-27 22:12:38 -04002354 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return rpc_call_sync(server->client, &msg, 0);
2356}
2357
2358static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2359 struct nfs_pathconf *pathconf)
2360{
2361 struct nfs4_exception exception = { };
2362 int err;
2363
2364 do {
2365 err = nfs4_handle_exception(server,
2366 _nfs4_proc_pathconf(server, fhandle, pathconf),
2367 &exception);
2368 } while (exception.retry);
2369 return err;
2370}
2371
Trond Myklebustec06c092006-03-20 13:44:27 -05002372static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Trond Myklebustec06c092006-03-20 13:44:27 -05002374 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Trond Myklebustec06c092006-03-20 13:44:27 -05002376 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002378 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002381 renew_lease(server, data->timestamp);
2382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Trond Myklebustec06c092006-03-20 13:44:27 -05002385static void nfs4_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 struct rpc_message msg = {
2388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2389 .rpc_argp = &data->args,
2390 .rpc_resp = &data->res,
2391 .rpc_cred = data->cred,
2392 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 data->timestamp = jiffies;
2395
Trond Myklebustec06c092006-03-20 13:44:27 -05002396 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
Trond Myklebust788e7a82006-03-20 13:44:27 -05002399static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 struct inode *inode = data->inode;
2402
2403 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2404 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002405 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002407 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002409 nfs_post_op_update_inode(inode, data->res.fattr);
2410 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
Trond Myklebust788e7a82006-03-20 13:44:27 -05002414static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 struct rpc_message msg = {
2417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2418 .rpc_argp = &data->args,
2419 .rpc_resp = &data->res,
2420 .rpc_cred = data->cred,
2421 };
2422 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002423 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 int stable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 if (how & FLUSH_STABLE) {
2427 if (!NFS_I(inode)->ncommit)
2428 stable = NFS_FILE_SYNC;
2429 else
2430 stable = NFS_DATA_SYNC;
2431 } else
2432 stable = NFS_UNSTABLE;
2433 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002434 data->args.bitmask = server->attr_bitmask;
2435 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 data->timestamp = jiffies;
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -05002440 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
Trond Myklebust788e7a82006-03-20 13:44:27 -05002443static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 struct inode *inode = data->inode;
2446
2447 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2448 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002449 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002451 if (task->tk_status >= 0)
2452 nfs_post_op_update_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
Trond Myklebust788e7a82006-03-20 13:44:27 -05002456static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 struct rpc_message msg = {
2459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2460 .rpc_argp = &data->args,
2461 .rpc_resp = &data->res,
2462 .rpc_cred = data->cred,
2463 };
Trond Myklebust788e7a82006-03-20 13:44:27 -05002464 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002466 data->args.bitmask = server->attr_bitmask;
2467 data->res.server = server;
2468
Trond Myklebust788e7a82006-03-20 13:44:27 -05002469 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
2472/*
2473 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2474 * standalone procedure for queueing an asynchronous RENEW.
2475 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002476static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
David Howellsadfa6f92006-08-22 20:06:08 -04002478 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002479 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 if (task->tk_status < 0) {
2482 switch (task->tk_status) {
2483 case -NFS4ERR_STALE_CLIENTID:
2484 case -NFS4ERR_EXPIRED:
2485 case -NFS4ERR_CB_PATH_DOWN:
2486 nfs4_schedule_state_recovery(clp);
2487 }
2488 return;
2489 }
2490 spin_lock(&clp->cl_lock);
2491 if (time_before(clp->cl_last_renewal,timestamp))
2492 clp->cl_last_renewal = timestamp;
2493 spin_unlock(&clp->cl_lock);
2494}
2495
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002496static const struct rpc_call_ops nfs4_renew_ops = {
2497 .rpc_call_done = nfs4_renew_done,
2498};
2499
David Howellsadfa6f92006-08-22 20:06:08 -04002500int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
2502 struct rpc_message msg = {
2503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2504 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002505 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 };
2507
2508 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002509 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
David Howellsadfa6f92006-08-22 20:06:08 -04002512int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
2514 struct rpc_message msg = {
2515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2516 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002517 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 };
2519 unsigned long now = jiffies;
2520 int status;
2521
2522 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2523 if (status < 0)
2524 return status;
2525 spin_lock(&clp->cl_lock);
2526 if (time_before(clp->cl_last_renewal,now))
2527 clp->cl_last_renewal = now;
2528 spin_unlock(&clp->cl_lock);
2529 return 0;
2530}
2531
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002532static inline int nfs4_server_supports_acls(struct nfs_server *server)
2533{
2534 return (server->caps & NFS_CAP_ACLS)
2535 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2536 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2537}
2538
2539/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2540 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2541 * the stack.
2542 */
2543#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2544
2545static void buf_to_pages(const void *buf, size_t buflen,
2546 struct page **pages, unsigned int *pgbase)
2547{
2548 const void *p = buf;
2549
2550 *pgbase = offset_in_page(buf);
2551 p -= *pgbase;
2552 while (p < buf + buflen) {
2553 *(pages++) = virt_to_page(p);
2554 p += PAGE_CACHE_SIZE;
2555 }
2556}
2557
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002558struct nfs4_cached_acl {
2559 int cached;
2560 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002561 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002562};
2563
2564static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002565{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002566 struct nfs_inode *nfsi = NFS_I(inode);
2567
2568 spin_lock(&inode->i_lock);
2569 kfree(nfsi->nfs4_acl);
2570 nfsi->nfs4_acl = acl;
2571 spin_unlock(&inode->i_lock);
2572}
2573
2574static void nfs4_zap_acl_attr(struct inode *inode)
2575{
2576 nfs4_set_cached_acl(inode, NULL);
2577}
2578
2579static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2580{
2581 struct nfs_inode *nfsi = NFS_I(inode);
2582 struct nfs4_cached_acl *acl;
2583 int ret = -ENOENT;
2584
2585 spin_lock(&inode->i_lock);
2586 acl = nfsi->nfs4_acl;
2587 if (acl == NULL)
2588 goto out;
2589 if (buf == NULL) /* user is just asking for length */
2590 goto out_len;
2591 if (acl->cached == 0)
2592 goto out;
2593 ret = -ERANGE; /* see getxattr(2) man page */
2594 if (acl->len > buflen)
2595 goto out;
2596 memcpy(buf, acl->data, acl->len);
2597out_len:
2598 ret = acl->len;
2599out:
2600 spin_unlock(&inode->i_lock);
2601 return ret;
2602}
2603
2604static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2605{
2606 struct nfs4_cached_acl *acl;
2607
2608 if (buf && acl_len <= PAGE_SIZE) {
2609 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2610 if (acl == NULL)
2611 goto out;
2612 acl->cached = 1;
2613 memcpy(acl->data, buf, acl_len);
2614 } else {
2615 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2616 if (acl == NULL)
2617 goto out;
2618 acl->cached = 0;
2619 }
2620 acl->len = acl_len;
2621out:
2622 nfs4_set_cached_acl(inode, acl);
2623}
2624
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002625static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002626{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002627 struct page *pages[NFS4ACL_MAXPAGES];
2628 struct nfs_getaclargs args = {
2629 .fh = NFS_FH(inode),
2630 .acl_pages = pages,
2631 .acl_len = buflen,
2632 };
2633 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002634 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002635 struct rpc_message msg = {
2636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2637 .rpc_argp = &args,
2638 .rpc_resp = &resp_len,
2639 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002640 struct page *localpage = NULL;
2641 int ret;
2642
2643 if (buflen < PAGE_SIZE) {
2644 /* As long as we're doing a round trip to the server anyway,
2645 * let's be prepared for a page of acl data. */
2646 localpage = alloc_page(GFP_KERNEL);
2647 resp_buf = page_address(localpage);
2648 if (localpage == NULL)
2649 return -ENOMEM;
2650 args.acl_pages[0] = localpage;
2651 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002652 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002653 } else {
2654 resp_buf = buf;
2655 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2656 }
2657 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2658 if (ret)
2659 goto out_free;
2660 if (resp_len > args.acl_len)
2661 nfs4_write_cached_acl(inode, NULL, resp_len);
2662 else
2663 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2664 if (buf) {
2665 ret = -ERANGE;
2666 if (resp_len > buflen)
2667 goto out_free;
2668 if (localpage)
2669 memcpy(buf, resp_buf, resp_len);
2670 }
2671 ret = resp_len;
2672out_free:
2673 if (localpage)
2674 __free_page(localpage);
2675 return ret;
2676}
2677
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002678static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2679{
2680 struct nfs4_exception exception = { };
2681 ssize_t ret;
2682 do {
2683 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2684 if (ret >= 0)
2685 break;
2686 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2687 } while (exception.retry);
2688 return ret;
2689}
2690
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002691static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2692{
2693 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002694 int ret;
2695
2696 if (!nfs4_server_supports_acls(server))
2697 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002698 ret = nfs_revalidate_inode(server, inode);
2699 if (ret < 0)
2700 return ret;
2701 ret = nfs4_read_cached_acl(inode, buf, buflen);
2702 if (ret != -ENOENT)
2703 return ret;
2704 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002705}
2706
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002707static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002708{
2709 struct nfs_server *server = NFS_SERVER(inode);
2710 struct page *pages[NFS4ACL_MAXPAGES];
2711 struct nfs_setaclargs arg = {
2712 .fh = NFS_FH(inode),
2713 .acl_pages = pages,
2714 .acl_len = buflen,
2715 };
2716 struct rpc_message msg = {
2717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2718 .rpc_argp = &arg,
2719 .rpc_resp = NULL,
2720 };
2721 int ret;
2722
2723 if (!nfs4_server_supports_acls(server))
2724 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002725 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002726 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
David Howells1f163412006-08-22 20:06:11 -04002727 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
J. Bruce Fields08efa202007-05-01 10:56:25 -04002728 nfs_zap_caches(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002729 return ret;
2730}
2731
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002732static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2733{
2734 struct nfs4_exception exception = { };
2735 int err;
2736 do {
2737 err = nfs4_handle_exception(NFS_SERVER(inode),
2738 __nfs4_proc_set_acl(inode, buf, buflen),
2739 &exception);
2740 } while (exception.retry);
2741 return err;
2742}
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002745nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
David Howells7539bba2006-08-22 20:06:09 -04002747 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 if (!clp || task->tk_status >= 0)
2750 return 0;
2751 switch(task->tk_status) {
2752 case -NFS4ERR_STALE_CLIENTID:
2753 case -NFS4ERR_STALE_STATEID:
2754 case -NFS4ERR_EXPIRED:
2755 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2756 nfs4_schedule_state_recovery(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002757 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 rpc_wake_up_task(task);
2759 task->tk_status = 0;
2760 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 case -NFS4ERR_DELAY:
Chuck Lever006ea732006-03-20 13:44:14 -05002762 nfs_inc_server_stats((struct nfs_server *) server,
2763 NFSIOS_DELAY);
2764 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2766 task->tk_status = 0;
2767 return -EAGAIN;
2768 case -NFS4ERR_OLD_STATEID:
2769 task->tk_status = 0;
2770 return -EAGAIN;
2771 }
2772 task->tk_status = nfs4_map_errors(task->tk_status);
2773 return 0;
2774}
2775
Trond Myklebust433fbe42006-01-03 09:55:22 +01002776static int nfs4_wait_bit_interruptible(void *word)
2777{
2778 if (signal_pending(current))
2779 return -ERESTARTSYS;
2780 schedule();
2781 return 0;
2782}
2783
David Howellsadfa6f92006-08-22 20:06:08 -04002784static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 sigset_t oldset;
Trond Myklebust433fbe42006-01-03 09:55:22 +01002787 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 might_sleep();
2790
Trond Myklebuste1485822006-12-13 16:43:13 -05002791 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002794 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2795 nfs4_wait_bit_interruptible,
2796 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 rpc_clnt_sigunmask(clnt, &oldset);
Trond Myklebuste1485822006-12-13 16:43:13 -05002798
2799 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return res;
2801}
2802
2803static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2804{
2805 sigset_t oldset;
2806 int res = 0;
2807
2808 might_sleep();
2809
2810 if (*timeout <= 0)
2811 *timeout = NFS4_POLL_RETRY_MIN;
2812 if (*timeout > NFS4_POLL_RETRY_MAX)
2813 *timeout = NFS4_POLL_RETRY_MAX;
2814 rpc_clnt_sigmask(clnt, &oldset);
2815 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002816 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (signalled())
2818 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002819 } else
2820 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 rpc_clnt_sigunmask(clnt, &oldset);
2822 *timeout <<= 1;
2823 return res;
2824}
2825
2826/* This is the error handling routine for processes that are allowed
2827 * to sleep.
2828 */
Trond Myklebust10afec92007-05-14 17:16:04 -04002829static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
David Howells7539bba2006-08-22 20:06:09 -04002831 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 int ret = errorcode;
2833
2834 exception->retry = 0;
2835 switch(errorcode) {
2836 case 0:
2837 return 0;
2838 case -NFS4ERR_STALE_CLIENTID:
2839 case -NFS4ERR_STALE_STATEID:
2840 case -NFS4ERR_EXPIRED:
Trond Myklebust433fbe42006-01-03 09:55:22 +01002841 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 ret = nfs4_wait_clnt_recover(server->client, clp);
2843 if (ret == 0)
2844 exception->retry = 1;
2845 break;
Trond Myklebustc5149832006-09-15 08:25:04 -04002846 case -NFS4ERR_FILE_OPEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 case -NFS4ERR_GRACE:
2848 case -NFS4ERR_DELAY:
2849 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002850 if (ret != 0)
2851 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002853 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 }
2855 /* We failed to handle the error */
2856 return nfs4_map_errors(ret);
2857}
2858
David Howellsadfa6f92006-08-22 20:06:08 -04002859int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
2861 nfs4_verifier sc_verifier;
2862 struct nfs4_setclientid setclientid = {
2863 .sc_verifier = &sc_verifier,
2864 .sc_prog = program,
2865 };
2866 struct rpc_message msg = {
2867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2868 .rpc_argp = &setclientid,
2869 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002870 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 };
Al Virobc4785c2006-10-19 23:28:51 -07002872 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 int loop = 0;
2874 int status;
2875
Al Virobc4785c2006-10-19 23:28:51 -07002876 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2878 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2879
2880 for(;;) {
2881 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2882 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
David Howells24c8dbb2006-08-22 20:06:10 -04002883 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
Trond Myklebust286d7d62006-01-03 09:55:26 +01002884 cred->cr_ops->cr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 clp->cl_id_uniquifier);
2886 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2887 sizeof(setclientid.sc_netid), "tcp");
2888 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2889 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2890 clp->cl_ipaddr, port >> 8, port & 255);
2891
2892 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2893 if (status != -NFS4ERR_CLID_INUSE)
2894 break;
2895 if (signalled())
2896 break;
2897 if (loop++ & 1)
2898 ssleep(clp->cl_lease_time + 1);
2899 else
2900 if (++clp->cl_id_uniquifier == 0)
2901 break;
2902 }
2903 return status;
2904}
2905
David Howellsadfa6f92006-08-22 20:06:08 -04002906static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
2908 struct nfs_fsinfo fsinfo;
2909 struct rpc_message msg = {
2910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2911 .rpc_argp = clp,
2912 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002913 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 };
2915 unsigned long now;
2916 int status;
2917
2918 now = jiffies;
2919 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2920 if (status == 0) {
2921 spin_lock(&clp->cl_lock);
2922 clp->cl_lease_time = fsinfo.lease_time * HZ;
2923 clp->cl_last_renewal = now;
Trond Myklebust58d97142006-01-03 09:55:24 +01002924 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 spin_unlock(&clp->cl_lock);
2926 }
2927 return status;
2928}
2929
David Howellsadfa6f92006-08-22 20:06:08 -04002930int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05002931{
2932 long timeout;
2933 int err;
2934 do {
2935 err = _nfs4_proc_setclientid_confirm(clp, cred);
2936 switch (err) {
2937 case 0:
2938 return err;
2939 case -NFS4ERR_RESOURCE:
2940 /* The IBM lawyers misread another document! */
2941 case -NFS4ERR_DELAY:
2942 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2943 }
2944 } while (err == 0);
2945 return err;
2946}
2947
Trond Myklebustfe650402006-01-03 09:55:18 +01002948struct nfs4_delegreturndata {
2949 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002950 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01002951 struct nfs_fh fh;
2952 nfs4_stateid stateid;
2953 struct rpc_cred *cred;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002954 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002955 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01002956 int rpc_status;
2957};
2958
2959static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960{
Trond Myklebustfe650402006-01-03 09:55:18 +01002961 struct nfs4_delegreturndata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 struct rpc_message msg = {
2963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
Trond Myklebustfe650402006-01-03 09:55:18 +01002964 .rpc_argp = &data->args,
Trond Myklebustfa178f22006-01-03 09:55:38 +01002965 .rpc_resp = &data->res,
Trond Myklebustfe650402006-01-03 09:55:18 +01002966 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 };
Trond Myklebustfa178f22006-01-03 09:55:38 +01002968 nfs_fattr_init(data->res.fattr);
Trond Myklebustfe650402006-01-03 09:55:18 +01002969 rpc_call_setup(task, &msg, 0);
2970}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Trond Myklebustfe650402006-01-03 09:55:18 +01002972static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2973{
2974 struct nfs4_delegreturndata *data = calldata;
2975 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002976 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01002977 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01002978}
2979
2980static void nfs4_delegreturn_release(void *calldata)
2981{
2982 struct nfs4_delegreturndata *data = calldata;
2983
2984 put_rpccred(data->cred);
2985 kfree(calldata);
2986}
2987
Jesper Juhlc8d149f2006-03-20 13:44:07 -05002988static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01002989 .rpc_call_prepare = nfs4_delegreturn_prepare,
2990 .rpc_call_done = nfs4_delegreturn_done,
2991 .rpc_release = nfs4_delegreturn_release,
2992};
2993
2994static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2995{
2996 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002997 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01002998 struct rpc_task *task;
2999 int status;
3000
3001 data = kmalloc(sizeof(*data), GFP_KERNEL);
3002 if (data == NULL)
3003 return -ENOMEM;
3004 data->args.fhandle = &data->fh;
3005 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003006 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003007 nfs_copy_fh(&data->fh, NFS_FH(inode));
3008 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003009 data->res.fattr = &data->fattr;
3010 data->res.server = server;
Trond Myklebustfe650402006-01-03 09:55:18 +01003011 data->cred = get_rpccred(cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003012 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003013 data->rpc_status = 0;
3014
3015 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003016 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003017 return PTR_ERR(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003018 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebustfa178f22006-01-03 09:55:38 +01003019 if (status == 0) {
Trond Myklebustfe650402006-01-03 09:55:18 +01003020 status = data->rpc_status;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003021 if (status == 0)
3022 nfs_post_op_update_inode(inode, &data->fattr);
3023 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003024 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003025 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
3028int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
3029{
3030 struct nfs_server *server = NFS_SERVER(inode);
3031 struct nfs4_exception exception = { };
3032 int err;
3033 do {
3034 err = _nfs4_proc_delegreturn(inode, cred, stateid);
3035 switch (err) {
3036 case -NFS4ERR_STALE_STATEID:
3037 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 case 0:
3039 return 0;
3040 }
3041 err = nfs4_handle_exception(server, err, &exception);
3042 } while (exception.retry);
3043 return err;
3044}
3045
3046#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3047#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3048
3049/*
3050 * sleep, with exponential backoff, and retry the LOCK operation.
3051 */
3052static unsigned long
3053nfs4_set_lock_task_retry(unsigned long timeout)
3054{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003055 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 timeout <<= 1;
3057 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3058 return NFS4_LOCK_MAXTIMEOUT;
3059 return timeout;
3060}
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3063{
3064 struct inode *inode = state->inode;
3065 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003066 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003067 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003069 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003071 struct nfs_lockt_res res = {
3072 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 };
3074 struct rpc_message msg = {
3075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3076 .rpc_argp = &arg,
3077 .rpc_resp = &res,
3078 .rpc_cred = state->owner->so_cred,
3079 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 struct nfs4_lock_state *lsp;
3081 int status;
3082
3083 down_read(&clp->cl_sem);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003084 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003085 status = nfs4_set_lock_state(state, request);
3086 if (status != 0)
3087 goto out;
3088 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003089 arg.lock_owner.id = lsp->ls_id.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003091 switch (status) {
3092 case 0:
3093 request->fl_type = F_UNLCK;
3094 break;
3095 case -NFS4ERR_DENIED:
3096 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003098 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003099out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 up_read(&clp->cl_sem);
3101 return status;
3102}
3103
3104static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3105{
3106 struct nfs4_exception exception = { };
3107 int err;
3108
3109 do {
3110 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3111 _nfs4_proc_getlk(state, cmd, request),
3112 &exception);
3113 } while (exception.retry);
3114 return err;
3115}
3116
3117static int do_vfs_lock(struct file *file, struct file_lock *fl)
3118{
3119 int res = 0;
3120 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3121 case FL_POSIX:
3122 res = posix_lock_file_wait(file, fl);
3123 break;
3124 case FL_FLOCK:
3125 res = flock_lock_file_wait(file, fl);
3126 break;
3127 default:
3128 BUG();
3129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 return res;
3131}
3132
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003133struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003134 struct nfs_locku_args arg;
3135 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003136 struct nfs4_lock_state *lsp;
3137 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003138 struct file_lock fl;
3139 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003140 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003141};
3142
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003143static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3144 struct nfs_open_context *ctx,
3145 struct nfs4_lock_state *lsp,
3146 struct nfs_seqid *seqid)
3147{
3148 struct nfs4_unlockdata *p;
3149 struct inode *inode = lsp->ls_state->inode;
3150
3151 p = kmalloc(sizeof(*p), GFP_KERNEL);
3152 if (p == NULL)
3153 return NULL;
3154 p->arg.fh = NFS_FH(inode);
3155 p->arg.fl = &p->fl;
3156 p->arg.seqid = seqid;
3157 p->arg.stateid = &lsp->ls_stateid;
3158 p->lsp = lsp;
3159 atomic_inc(&lsp->ls_count);
3160 /* Ensure we don't close file until we're done freeing locks! */
3161 p->ctx = get_nfs_open_context(ctx);
3162 memcpy(&p->fl, fl, sizeof(p->fl));
3163 p->server = NFS_SERVER(inode);
3164 return p;
3165}
3166
Trond Myklebust06f814a2006-01-03 09:55:07 +01003167static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003168{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003169 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003170 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003171 nfs4_put_lock_state(calldata->lsp);
3172 put_nfs_open_context(calldata->ctx);
3173 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003174}
3175
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003176static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003177{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003178 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003179
Trond Myklebust06f814a2006-01-03 09:55:07 +01003180 if (RPC_ASSASSINATED(task))
3181 return;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003182 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003183 switch (task->tk_status) {
3184 case 0:
3185 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003186 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003187 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003188 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003189 break;
3190 case -NFS4ERR_STALE_STATEID:
3191 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003192 break;
3193 default:
Trond Myklebusta6a352e2006-12-13 16:43:06 -05003194 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003195 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003196 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003197}
3198
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003199static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003200{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003201 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 struct rpc_message msg = {
3203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003204 .rpc_argp = &calldata->arg,
3205 .rpc_resp = &calldata->res,
3206 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003209 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003210 return;
3211 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003212 /* Note: exit _without_ running nfs4_locku_done */
3213 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003214 return;
3215 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003216 calldata->timestamp = jiffies;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003217 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003220static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003221 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003222 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003223 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003224};
3225
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003226static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3227 struct nfs_open_context *ctx,
3228 struct nfs4_lock_state *lsp,
3229 struct nfs_seqid *seqid)
3230{
3231 struct nfs4_unlockdata *data;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003232
3233 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3234 if (data == NULL) {
3235 nfs_free_seqid(seqid);
3236 return ERR_PTR(-ENOMEM);
3237 }
3238
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003239 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003240}
3241
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3243{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003244 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003245 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003246 struct rpc_task *task;
3247 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Trond Myklebust9b073572006-06-29 16:38:34 -04003249 status = nfs4_set_lock_state(state, request);
3250 /* Unlock _before_ we do the RPC call */
3251 request->fl_flags |= FL_EXISTS;
3252 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3253 goto out;
3254 if (status != 0)
3255 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003256 /* Is this a delegated lock? */
3257 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003258 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003259 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003260 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003261 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003262 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003263 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003264 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3265 status = PTR_ERR(task);
3266 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003267 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003268 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003269 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003270out:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003271 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272}
3273
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003274struct nfs4_lockdata {
3275 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003276 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003277 struct nfs4_lock_state *lsp;
3278 struct nfs_open_context *ctx;
3279 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003280 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003281 int rpc_status;
3282 int cancelled;
3283};
3284
3285static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3286 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3287{
3288 struct nfs4_lockdata *p;
3289 struct inode *inode = lsp->ls_state->inode;
3290 struct nfs_server *server = NFS_SERVER(inode);
3291
3292 p = kzalloc(sizeof(*p), GFP_KERNEL);
3293 if (p == NULL)
3294 return NULL;
3295
3296 p->arg.fh = NFS_FH(inode);
3297 p->arg.fl = &p->fl;
3298 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3299 if (p->arg.lock_seqid == NULL)
3300 goto out_free;
3301 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003302 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003303 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003304 p->lsp = lsp;
3305 atomic_inc(&lsp->ls_count);
3306 p->ctx = get_nfs_open_context(ctx);
3307 memcpy(&p->fl, fl, sizeof(p->fl));
3308 return p;
3309out_free:
3310 kfree(p);
3311 return NULL;
3312}
3313
3314static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3315{
3316 struct nfs4_lockdata *data = calldata;
3317 struct nfs4_state *state = data->lsp->ls_state;
3318 struct nfs4_state_owner *sp = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 struct rpc_message msg = {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3321 .rpc_argp = &data->arg,
3322 .rpc_resp = &data->res,
3323 .rpc_cred = sp->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003326 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3327 return;
3328 dprintk("%s: begin!\n", __FUNCTION__);
3329 /* Do we need to do an open_to_lock_owner? */
3330 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3331 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3332 if (data->arg.open_seqid == NULL) {
3333 data->rpc_status = -ENOMEM;
3334 task->tk_action = NULL;
Trond Myklebust06735b32005-10-18 14:20:15 -07003335 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07003336 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003337 data->arg.open_stateid = &state->stateid;
3338 data->arg.new_lock_owner = 1;
3339 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003340 data->timestamp = jiffies;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003341 rpc_call_setup(task, &msg, 0);
Trond Myklebust06735b32005-10-18 14:20:15 -07003342out:
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003343 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3344}
3345
3346static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3347{
3348 struct nfs4_lockdata *data = calldata;
3349
3350 dprintk("%s: begin!\n", __FUNCTION__);
3351
3352 data->rpc_status = task->tk_status;
3353 if (RPC_ASSASSINATED(task))
3354 goto out;
3355 if (data->arg.new_lock_owner != 0) {
3356 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3357 if (data->rpc_status == 0)
3358 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3359 else
3360 goto out;
3361 }
3362 if (data->rpc_status == 0) {
3363 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3364 sizeof(data->lsp->ls_stateid.data));
3365 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003366 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003367 }
3368 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3369out:
3370 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3371}
3372
3373static void nfs4_lock_release(void *calldata)
3374{
3375 struct nfs4_lockdata *data = calldata;
3376
3377 dprintk("%s: begin!\n", __FUNCTION__);
3378 if (data->arg.open_seqid != NULL)
3379 nfs_free_seqid(data->arg.open_seqid);
3380 if (data->cancelled != 0) {
3381 struct rpc_task *task;
3382 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3383 data->arg.lock_seqid);
3384 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003385 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003386 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3387 } else
3388 nfs_free_seqid(data->arg.lock_seqid);
3389 nfs4_put_lock_state(data->lsp);
3390 put_nfs_open_context(data->ctx);
3391 kfree(data);
3392 dprintk("%s: done!\n", __FUNCTION__);
3393}
3394
3395static const struct rpc_call_ops nfs4_lock_ops = {
3396 .rpc_call_prepare = nfs4_lock_prepare,
3397 .rpc_call_done = nfs4_lock_done,
3398 .rpc_release = nfs4_lock_release,
3399};
3400
3401static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3402{
3403 struct nfs4_lockdata *data;
3404 struct rpc_task *task;
3405 int ret;
3406
3407 dprintk("%s: begin!\n", __FUNCTION__);
3408 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3409 fl->fl_u.nfs4_fl.owner);
3410 if (data == NULL)
3411 return -ENOMEM;
3412 if (IS_SETLKW(cmd))
3413 data->arg.block = 1;
3414 if (reclaim != 0)
3415 data->arg.reclaim = 1;
3416 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3417 &nfs4_lock_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003418 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003419 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003420 ret = nfs4_wait_for_completion_rpc_task(task);
3421 if (ret == 0) {
3422 ret = data->rpc_status;
3423 if (ret == -NFS4ERR_DENIED)
3424 ret = -EAGAIN;
3425 } else
3426 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003427 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003428 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
3432static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3433{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003434 struct nfs_server *server = NFS_SERVER(state->inode);
3435 struct nfs4_exception exception = { };
3436 int err;
3437
3438 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003439 /* Cache the lock if possible... */
3440 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3441 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003442 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3443 if (err != -NFS4ERR_DELAY)
3444 break;
3445 nfs4_handle_exception(server, err, &exception);
3446 } while (exception.retry);
3447 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448}
3449
3450static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3451{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003452 struct nfs_server *server = NFS_SERVER(state->inode);
3453 struct nfs4_exception exception = { };
3454 int err;
3455
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003456 err = nfs4_set_lock_state(state, request);
3457 if (err != 0)
3458 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003459 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003460 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3461 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003462 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3463 if (err != -NFS4ERR_DELAY)
3464 break;
3465 nfs4_handle_exception(server, err, &exception);
3466 } while (exception.retry);
3467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468}
3469
3470static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3471{
David Howellsadfa6f92006-08-22 20:06:08 -04003472 struct nfs_client *clp = state->owner->so_client;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003473 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 int status;
3475
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003476 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003477 status = nfs4_set_lock_state(state, request);
3478 if (status != 0)
3479 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003480 request->fl_flags |= FL_ACCESS;
3481 status = do_vfs_lock(request->fl_file, request);
3482 if (status < 0)
3483 goto out;
3484 down_read(&clp->cl_sem);
3485 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3486 struct nfs_inode *nfsi = NFS_I(state->inode);
3487 /* Yes: cache locks! */
3488 down_read(&nfsi->rwsem);
3489 /* ...but avoid races with delegation recall... */
3490 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3491 request->fl_flags = fl_flags & ~FL_SLEEP;
3492 status = do_vfs_lock(request->fl_file, request);
3493 up_read(&nfsi->rwsem);
3494 goto out_unlock;
3495 }
3496 up_read(&nfsi->rwsem);
3497 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003498 status = _nfs4_do_setlk(state, cmd, request, 0);
3499 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003500 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003501 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003502 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003503 if (do_vfs_lock(request->fl_file, request) < 0)
3504 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003505out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 up_read(&clp->cl_sem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003507out:
3508 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 return status;
3510}
3511
3512static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3513{
3514 struct nfs4_exception exception = { };
3515 int err;
3516
3517 do {
3518 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3519 _nfs4_proc_setlk(state, cmd, request),
3520 &exception);
3521 } while (exception.retry);
3522 return err;
3523}
3524
3525static int
3526nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3527{
3528 struct nfs_open_context *ctx;
3529 struct nfs4_state *state;
3530 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3531 int status;
3532
3533 /* verify open state */
3534 ctx = (struct nfs_open_context *)filp->private_data;
3535 state = ctx->state;
3536
3537 if (request->fl_start < 0 || request->fl_end < 0)
3538 return -EINVAL;
3539
3540 if (IS_GETLK(cmd))
3541 return nfs4_proc_getlk(state, F_GETLK, request);
3542
3543 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3544 return -EINVAL;
3545
3546 if (request->fl_type == F_UNLCK)
3547 return nfs4_proc_unlck(state, cmd, request);
3548
3549 do {
3550 status = nfs4_proc_setlk(state, cmd, request);
3551 if ((status != -EAGAIN) || IS_SETLK(cmd))
3552 break;
3553 timeout = nfs4_set_lock_task_retry(timeout);
3554 status = -ERESTARTSYS;
3555 if (signalled())
3556 break;
3557 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 return status;
3559}
3560
Trond Myklebust888e6942005-11-04 15:38:11 -05003561int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3562{
3563 struct nfs_server *server = NFS_SERVER(state->inode);
3564 struct nfs4_exception exception = { };
3565 int err;
3566
3567 err = nfs4_set_lock_state(state, fl);
3568 if (err != 0)
3569 goto out;
3570 do {
3571 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3572 if (err != -NFS4ERR_DELAY)
3573 break;
3574 err = nfs4_handle_exception(server, err, &exception);
3575 } while (exception.retry);
3576out:
3577 return err;
3578}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003579
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003580#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3581
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003582int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3583 size_t buflen, int flags)
3584{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003585 struct inode *inode = dentry->d_inode;
3586
3587 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3588 return -EOPNOTSUPP;
3589
3590 if (!S_ISREG(inode->i_mode) &&
3591 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3592 return -EPERM;
3593
3594 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003595}
3596
3597/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3598 * and that's what we'll do for e.g. user attributes that haven't been set.
3599 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3600 * attributes in kernel-managed attribute namespaces. */
3601ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3602 size_t buflen)
3603{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003604 struct inode *inode = dentry->d_inode;
3605
3606 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3607 return -EOPNOTSUPP;
3608
3609 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003610}
3611
3612ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3613{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003614 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003615
J. Bruce Fields096455a2006-03-20 23:23:42 -05003616 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3617 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003618 if (buf && buflen < len)
3619 return -ERANGE;
3620 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003621 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3622 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003623}
3624
Trond Myklebustc228fd32007-01-13 02:28:11 -05003625int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003626 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003627{
3628 struct nfs_server *server = NFS_SERVER(dir);
3629 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003630 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3631 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003632 };
3633 struct nfs4_fs_locations_arg args = {
3634 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003635 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003636 .page = page,
3637 .bitmask = bitmask,
3638 };
3639 struct rpc_message msg = {
3640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3641 .rpc_argp = &args,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003642 .rpc_resp = fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003643 };
3644 int status;
3645
3646 dprintk("%s: start\n", __FUNCTION__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003647 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003648 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003649 fs_locations->nlocations = 0;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003650 status = rpc_call_sync(server->client, &msg, 0);
3651 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3652 return status;
3653}
3654
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3656 .recover_open = nfs4_open_reclaim,
3657 .recover_lock = nfs4_lock_reclaim,
3658};
3659
3660struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3661 .recover_open = nfs4_open_expired,
3662 .recover_lock = nfs4_lock_expired,
3663};
3664
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003665static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003666 .permission = nfs_permission,
3667 .getattr = nfs_getattr,
3668 .setattr = nfs_setattr,
3669 .getxattr = nfs4_getxattr,
3670 .setxattr = nfs4_setxattr,
3671 .listxattr = nfs4_listxattr,
3672};
3673
David Howells509de812006-08-22 20:06:11 -04003674const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 .version = 4, /* protocol version */
3676 .dentry_ops = &nfs4_dentry_operations,
3677 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003678 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 .getroot = nfs4_proc_get_root,
3680 .getattr = nfs4_proc_getattr,
3681 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04003682 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 .lookup = nfs4_proc_lookup,
3684 .access = nfs4_proc_access,
3685 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 .create = nfs4_proc_create,
3687 .remove = nfs4_proc_remove,
3688 .unlink_setup = nfs4_proc_unlink_setup,
3689 .unlink_done = nfs4_proc_unlink_done,
3690 .rename = nfs4_proc_rename,
3691 .link = nfs4_proc_link,
3692 .symlink = nfs4_proc_symlink,
3693 .mkdir = nfs4_proc_mkdir,
3694 .rmdir = nfs4_proc_remove,
3695 .readdir = nfs4_proc_readdir,
3696 .mknod = nfs4_proc_mknod,
3697 .statfs = nfs4_proc_statfs,
3698 .fsinfo = nfs4_proc_fsinfo,
3699 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04003700 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 .decode_dirent = nfs4_decode_dirent,
3702 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003703 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003705 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003707 .commit_done = nfs4_commit_done,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003708 .file_open = nfs_open,
3709 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003711 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712};
3713
3714/*
3715 * Local variables:
3716 * c-basic-offset: 8
3717 * End:
3718 */