blob: 52ba7630794c138a8aa821794e03ea2db8586ea8 [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 Myklebust24ac23a2006-01-03 09:55:11 +0100229 int rpc_status;
230 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100231};
232
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400233
234static void nfs4_init_opendata_res(struct nfs4_opendata *p)
235{
236 p->o_res.f_attr = &p->f_attr;
237 p->o_res.dir_attr = &p->dir_attr;
238 p->o_res.server = p->o_arg.server;
239 nfs_fattr_init(&p->f_attr);
240 nfs_fattr_init(&p->dir_attr);
241}
242
Trond Myklebustad389da2007-06-05 12:30:00 -0400243static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100244 struct nfs4_state_owner *sp, int flags,
245 const struct iattr *attrs)
246{
Trond Myklebustad389da2007-06-05 12:30:00 -0400247 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100248 struct inode *dir = parent->d_inode;
249 struct nfs_server *server = NFS_SERVER(dir);
250 struct nfs4_opendata *p;
251
252 p = kzalloc(sizeof(*p), GFP_KERNEL);
253 if (p == NULL)
254 goto err;
255 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
256 if (p->o_arg.seqid == NULL)
257 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400258 p->path.mnt = mntget(path->mnt);
259 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100260 p->dir = parent;
261 p->owner = sp;
262 atomic_inc(&sp->so_count);
263 p->o_arg.fh = NFS_FH(dir);
264 p->o_arg.open_flags = flags,
David Howells7539bba2006-08-22 20:06:09 -0400265 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400266 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400267 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100268 p->o_arg.server = server;
269 p->o_arg.bitmask = server->attr_bitmask;
270 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100271 if (flags & O_EXCL) {
272 u32 *s = (u32 *) p->o_arg.u.verifier.data;
273 s[0] = jiffies;
274 s[1] = current->pid;
275 } else if (flags & O_CREAT) {
276 p->o_arg.u.attrs = &p->attrs;
277 memcpy(&p->attrs, attrs, sizeof(p->attrs));
278 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100279 p->c_arg.fh = &p->o_res.fh;
280 p->c_arg.stateid = &p->o_res.stateid;
281 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400282 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400283 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100284 return p;
285err_free:
286 kfree(p);
287err:
288 dput(parent);
289 return NULL;
290}
291
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400292static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100293{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400294 struct nfs4_opendata *p = container_of(kref,
295 struct nfs4_opendata, kref);
296
297 nfs_free_seqid(p->o_arg.seqid);
298 nfs4_put_state_owner(p->owner);
299 dput(p->dir);
300 dput(p->path.dentry);
301 mntput(p->path.mnt);
302 kfree(p);
303}
304
305static void nfs4_opendata_put(struct nfs4_opendata *p)
306{
307 if (p != NULL)
308 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100309}
310
Trond Myklebust06f814a2006-01-03 09:55:07 +0100311static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
312{
313 sigset_t oldset;
314 int ret;
315
316 rpc_clnt_sigmask(task->tk_client, &oldset);
317 ret = rpc_wait_for_completion_task(task);
318 rpc_clnt_sigunmask(task->tk_client, &oldset);
319 return ret;
320}
321
Trond Myklebust003707c2007-07-05 18:07:55 -0400322static void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
Trond Myklebuste7616922006-01-03 09:55:13 +0100323{
324 switch (open_flags) {
325 case FMODE_WRITE:
326 state->n_wronly++;
327 break;
328 case FMODE_READ:
329 state->n_rdonly++;
330 break;
331 case FMODE_READ|FMODE_WRITE:
332 state->n_rdwr++;
333 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400334 nfs4_state_set_mode_locked(state, state->state | open_flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100335}
336
Trond Myklebust003707c2007-07-05 18:07:55 -0400337static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
338{
339 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
340 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
341 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
342 switch (open_flags) {
343 case FMODE_READ:
344 set_bit(NFS_O_RDONLY_STATE, &state->flags);
345 break;
346 case FMODE_WRITE:
347 set_bit(NFS_O_WRONLY_STATE, &state->flags);
348 break;
349 case FMODE_READ|FMODE_WRITE:
350 set_bit(NFS_O_RDWR_STATE, &state->flags);
351 }
352}
353
354static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
355{
356 spin_lock(&state->owner->so_lock);
357 spin_lock(&state->inode->i_lock);
358 nfs_set_open_stateid_locked(state, stateid, open_flags);
359 spin_unlock(&state->inode->i_lock);
360 spin_unlock(&state->owner->so_lock);
361}
362
363static 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 -0700364{
365 struct inode *inode = state->inode;
366
367 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebustd5308382005-11-04 15:33:38 -0500368 /* Protect against nfs4_find_state_byowner() */
Trond Myklebustec073422005-10-20 14:22:47 -0700369 spin_lock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 spin_lock(&inode->i_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400371 if (deleg_stateid != NULL) {
372 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
373 set_bit(NFS_DELEGATED_STATE, &state->flags);
374 }
375 if (open_stateid != NULL)
376 nfs_set_open_stateid_locked(state, open_stateid, open_flags);
Trond Myklebuste7616922006-01-03 09:55:13 +0100377 update_open_stateflags(state, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700379 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100382static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
383{
384 struct inode *inode;
385 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400386 struct nfs_delegation *delegation;
387 nfs4_stateid *deleg_stateid = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400388 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100389
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400390 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100391 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400392 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100393 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400394 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500395 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400396 goto err;
397 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100398 state = nfs4_get_open_state(inode, data->owner);
399 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400400 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400401 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400402 int delegation_flags = 0;
403
Trond Myklebust003707c2007-07-05 18:07:55 -0400404 rcu_read_lock();
405 delegation = rcu_dereference(NFS_I(inode)->delegation);
406 if (delegation)
407 delegation_flags = delegation->flags;
408 rcu_read_unlock();
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400409 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
410 nfs_inode_set_delegation(state->inode,
411 data->owner->so_cred,
412 &data->o_res);
413 else
414 nfs_inode_reclaim_delegation(state->inode,
415 data->owner->so_cred,
416 &data->o_res);
417 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400418 rcu_read_lock();
419 delegation = rcu_dereference(NFS_I(inode)->delegation);
420 if (delegation != NULL)
421 deleg_stateid = &delegation->stateid;
422 update_open_stateid(state, &data->o_res.stateid, deleg_stateid, data->o_arg.open_flags);
423 rcu_read_unlock();
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100424 iput(inode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100425 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400426err_put_inode:
427 iput(inode);
428err:
429 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100430}
431
Trond Myklebust864472e2006-01-03 09:55:15 +0100432static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
433{
434 struct nfs_inode *nfsi = NFS_I(state->inode);
435 struct nfs_open_context *ctx;
436
437 spin_lock(&state->inode->i_lock);
438 list_for_each_entry(ctx, &nfsi->open_files, list) {
439 if (ctx->state != state)
440 continue;
441 get_nfs_open_context(ctx);
442 spin_unlock(&state->inode->i_lock);
443 return ctx;
444 }
445 spin_unlock(&state->inode->i_lock);
446 return ERR_PTR(-ENOENT);
447}
448
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400449static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100450{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400451 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100452 int ret;
453
454 opendata->o_arg.open_flags = openflags;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400455 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
456 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
457 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100458 ret = _nfs4_proc_open(opendata);
459 if (ret != 0)
460 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400461 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400462 if (IS_ERR(newstate))
463 return PTR_ERR(newstate);
464 nfs4_close_state(&opendata->path, newstate, openflags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400465 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100466 return 0;
467}
468
469static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
470{
Trond Myklebust864472e2006-01-03 09:55:15 +0100471 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100472 int ret;
473
474 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400475 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100476 smp_rmb();
477 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400478 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100479 if (ret != 0)
480 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400481 if (newstate != state)
482 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100483 }
484 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400485 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100486 if (ret != 0)
487 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400488 if (newstate != state)
489 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100490 }
491 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400492 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100493 if (ret != 0)
494 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400495 if (newstate != state)
496 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100497 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100498 return 0;
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
502 * OPEN_RECLAIM:
503 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400505static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Trond Myklebust864472e2006-01-03 09:55:15 +0100507 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
508 struct nfs4_opendata *opendata;
509 int delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int status;
511
512 if (delegation != NULL) {
513 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
514 memcpy(&state->stateid, &delegation->stateid,
515 sizeof(state->stateid));
516 set_bit(NFS_DELEGATED_STATE, &state->flags);
517 return 0;
518 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100519 delegation_type = delegation->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Trond Myklebustad389da2007-06-05 12:30:00 -0400521 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
Trond Myklebust864472e2006-01-03 09:55:15 +0100522 if (opendata == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700523 return -ENOMEM;
Trond Myklebust864472e2006-01-03 09:55:15 +0100524 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
525 opendata->o_arg.fh = NFS_FH(state->inode);
526 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
527 opendata->o_arg.u.delegation_type = delegation_type;
528 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400529 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return status;
531}
532
Trond Myklebust539cd032007-06-05 11:46:42 -0400533static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct nfs_server *server = NFS_SERVER(state->inode);
536 struct nfs4_exception exception = { };
537 int err;
538 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400539 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000540 if (err != -NFS4ERR_DELAY)
541 break;
542 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 } while (exception.retry);
544 return err;
545}
546
Trond Myklebust864472e2006-01-03 09:55:15 +0100547static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
548{
549 struct nfs_open_context *ctx;
550 int ret;
551
552 ctx = nfs4_state_find_open_context(state);
553 if (IS_ERR(ctx))
554 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400555 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100556 put_nfs_open_context(ctx);
557 return ret;
558}
559
Trond Myklebust13437e12007-07-06 15:10:43 -0400560static 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 -0700561{
562 struct nfs4_state_owner *sp = state->owner;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100563 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100564 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Trond Myklebustad389da2007-06-05 12:30:00 -0400566 opendata = nfs4_opendata_alloc(&ctx->path, sp, 0, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100567 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100568 return -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100569 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -0400570 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100571 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100572 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400573 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100574 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Trond Myklebust13437e12007-07-06 15:10:43 -0400577int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -0400580 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 int err;
582 do {
Trond Myklebust13437e12007-07-06 15:10:43 -0400583 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 switch (err) {
585 case 0:
586 return err;
587 case -NFS4ERR_STALE_CLIENTID:
588 case -NFS4ERR_STALE_STATEID:
589 case -NFS4ERR_EXPIRED:
590 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -0400591 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return err;
593 }
594 err = nfs4_handle_exception(server, err, &exception);
595 } while (exception.retry);
596 return err;
597}
598
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100599static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100601 struct nfs4_opendata *data = calldata;
602 struct rpc_message msg = {
603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
604 .rpc_argp = &data->c_arg,
605 .rpc_resp = &data->c_res,
606 .rpc_cred = data->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 };
Trond Myklebust26e976a2006-01-03 09:55:21 +0100608 data->timestamp = jiffies;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100609 rpc_call_setup(task, &msg, 0);
610}
611
612static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
613{
614 struct nfs4_opendata *data = calldata;
615
616 data->rpc_status = task->tk_status;
617 if (RPC_ASSASSINATED(task))
618 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100619 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100620 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
621 sizeof(data->o_res.stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +0100622 renew_lease(data->o_res.server, data->timestamp);
623 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100624 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400625 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100626}
627
628static void nfs4_open_confirm_release(void *calldata)
629{
630 struct nfs4_opendata *data = calldata;
631 struct nfs4_state *state = NULL;
632
633 /* If this request hasn't been cancelled, do nothing */
634 if (data->cancelled == 0)
635 goto out_free;
636 /* In case of error, no cleanup! */
637 if (data->rpc_status != 0)
638 goto out_free;
639 nfs_confirm_seqid(&data->owner->so_seqid, 0);
640 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400641 if (!IS_ERR(state))
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400642 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100643out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400644 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100645}
646
647static const struct rpc_call_ops nfs4_open_confirm_ops = {
648 .rpc_call_prepare = nfs4_open_confirm_prepare,
649 .rpc_call_done = nfs4_open_confirm_done,
650 .rpc_release = nfs4_open_confirm_release,
651};
652
653/*
654 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
655 */
656static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
657{
658 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
659 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 int status;
661
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400662 kref_get(&data->kref);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500663 /*
664 * If rpc_run_task() ends up calling ->rpc_release(), we
665 * want to ensure that it takes the 'error' code path.
666 */
667 data->rpc_status = -ENOMEM;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100668 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500669 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100670 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100671 status = nfs4_wait_for_completion_rpc_task(task);
672 if (status != 0) {
673 data->cancelled = 1;
674 smp_wmb();
675 } else
676 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500677 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return status;
679}
680
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100681static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100683 struct nfs4_opendata *data = calldata;
684 struct nfs4_state_owner *sp = data->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct rpc_message msg = {
686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100687 .rpc_argp = &data->o_arg,
688 .rpc_resp = &data->o_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 .rpc_cred = sp->so_cred,
690 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100691
692 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
693 return;
694 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400695 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100696 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust864472e2006-01-03 09:55:15 +0100697 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
698 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust26e976a2006-01-03 09:55:21 +0100699 data->timestamp = jiffies;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100700 rpc_call_setup(task, &msg, 0);
701}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100703static void nfs4_open_done(struct rpc_task *task, void *calldata)
704{
705 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100707 data->rpc_status = task->tk_status;
708 if (RPC_ASSASSINATED(task))
709 return;
710 if (task->tk_status == 0) {
711 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700712 case S_IFREG:
713 break;
714 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100715 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700716 break;
717 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100718 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700719 break;
720 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100721 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700722 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100723 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400724 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
725 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700726 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100727 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
728}
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! */
739 if (data->rpc_status != 0)
740 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 Myklebust7a1218a2006-03-20 18:11:10 -0500771 /*
772 * If rpc_run_task() ends up calling ->rpc_release(), we
773 * want to ensure that it takes the 'error' code path.
774 */
775 data->rpc_status = -ENOMEM;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400776 data->cancelled = 0;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100777 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500778 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100779 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100780 status = nfs4_wait_for_completion_rpc_task(task);
781 if (status != 0) {
782 data->cancelled = 1;
783 smp_wmb();
784 } else
785 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500786 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100788 return status;
789
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400790 if (o_arg->open_flags & O_CREAT) {
791 update_changeattr(dir, &o_res->cinfo);
792 nfs_post_op_update_inode(dir, o_res->dir_attr);
793 } else
794 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100796 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100798 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Howells8fa5c002006-08-22 20:06:12 -0400801 return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100802 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
806{
807 struct nfs_access_entry cache;
808 int mask = 0;
809 int status;
810
811 if (openflags & FMODE_READ)
812 mask |= MAY_READ;
813 if (openflags & FMODE_WRITE)
814 mask |= MAY_WRITE;
Trond Myklebust1b45c462007-07-03 13:04:56 -0400815 if (openflags & FMODE_EXEC)
816 mask |= MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 status = nfs_access_get_cached(inode, cred, &cache);
818 if (status == 0)
819 goto out;
820
821 /* Be clever: ask server to check for all possible rights */
822 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
823 cache.cred = cred;
824 cache.jiffies = jiffies;
825 status = _nfs4_proc_access(inode, &cache);
826 if (status != 0)
827 return status;
828 nfs_access_add_cache(inode, &cache);
829out:
830 if ((cache.mask & mask) == mask)
831 return 0;
832 return -EACCES;
833}
834
Trond Myklebust10afec92007-05-14 17:16:04 -0400835static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +0100836{
David Howells7539bba2006-08-22 20:06:09 -0400837 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -0400838 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +0100839
Trond Myklebust6b309542006-09-14 14:03:14 -0400840 for (;;) {
841 ret = nfs4_wait_clnt_recover(server->client, clp);
842 if (ret != 0)
843 return ret;
844 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
845 break;
Trond Myklebust58d97142006-01-03 09:55:24 +0100846 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -0400847 }
848 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +0100849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851/*
852 * OPEN_EXPIRED:
853 * reclaim state on the server after a network partition.
854 * Assumes caller holds the appropriate lock
855 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400856static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 struct inode *inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860 struct nfs4_opendata *opendata;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100861 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
Trond Myklebust864472e2006-01-03 09:55:15 +0100862 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
Trond Myklebust539cd032007-06-05 11:46:42 -0400865 ret = _nfs4_do_access(inode, ctx->cred, openflags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100866 if (ret < 0)
867 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
869 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Trond Myklebustad389da2007-06-05 12:30:00 -0400872 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100873 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100874 return -ENOMEM;
875 ret = nfs4_open_recover(opendata, state);
876 if (ret == -ESTALE) {
877 /* Invalidate the state owner so we don't ever use it again */
Trond Myklebust539cd032007-06-05 11:46:42 -0400878 nfs4_drop_state_owner(state->owner);
879 d_drop(ctx->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400881 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100882 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Trond Myklebust539cd032007-06-05 11:46:42 -0400885static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +0000886{
Trond Myklebust539cd032007-06-05 11:46:42 -0400887 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000888 struct nfs4_exception exception = { };
889 int err;
890
891 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400892 err = _nfs4_open_expired(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000893 if (err == -NFS4ERR_DELAY)
894 nfs4_handle_exception(server, err, &exception);
895 } while (exception.retry);
896 return err;
897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
900{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +0100902 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Trond Myklebust864472e2006-01-03 09:55:15 +0100904 ctx = nfs4_state_find_open_context(state);
905 if (IS_ERR(ctx))
906 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400907 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100908 put_nfs_open_context(ctx);
909 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
912/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100913 * Returns a referenced nfs4_state if there is an open delegation on the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 */
915static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
916{
917 struct nfs_delegation *delegation;
918 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -0400919 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct nfs_inode *nfsi = NFS_I(inode);
921 struct nfs4_state_owner *sp = NULL;
922 struct nfs4_state *state = NULL;
923 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
924 int err;
925
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100926 err = -ENOMEM;
927 if (!(sp = nfs4_get_state_owner(server, cred))) {
928 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
929 return err;
930 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100931 err = nfs4_recover_expired_lease(server);
932 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100933 goto out_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /* Protect against reboot recovery - NOTE ORDER! */
935 down_read(&clp->cl_sem);
936 /* Protect against delegation recall */
937 down_read(&nfsi->rwsem);
938 delegation = NFS_I(inode)->delegation;
939 err = -ENOENT;
940 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
941 goto out_err;
942 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 state = nfs4_get_open_state(inode, sp);
944 if (state == NULL)
945 goto out_err;
946
947 err = -ENOENT;
948 if ((state->state & open_flags) == open_flags) {
949 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100950 update_open_stateflags(state, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 spin_unlock(&inode->i_lock);
952 goto out_ok;
953 } else if (state->state != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100954 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 lock_kernel();
957 err = _nfs4_do_access(inode, cred, open_flags);
958 unlock_kernel();
959 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100960 goto out_put_open_state;
Trond Myklebust003707c2007-07-05 18:07:55 -0400961 update_open_stateid(state, NULL, &delegation->stateid, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 nfs4_put_state_owner(sp);
964 up_read(&nfsi->rwsem);
965 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 *res = state;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100967 return 0;
968out_put_open_state:
969 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 up_read(&nfsi->rwsem);
972 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700973 if (err != -EACCES)
974 nfs_inode_return_delegation(inode);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100975out_put_state_owner:
976 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return err;
978}
979
980static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
981{
982 struct nfs4_exception exception = { };
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500983 struct nfs4_state *res = ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 int err;
985
986 do {
987 err = _nfs4_open_delegated(inode, flags, cred, &res);
988 if (err == 0)
989 break;
990 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
991 err, &exception));
992 } while (exception.retry);
993 return res;
994}
995
996/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -0400997 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
998 * fields corresponding to attributes that were used to store the verifier.
999 * Make sure we clobber those fields in the later setattr call
1000 */
1001static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1002{
1003 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1004 !(sattr->ia_valid & ATTR_ATIME_SET))
1005 sattr->ia_valid |= ATTR_ATIME;
1006
1007 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1008 !(sattr->ia_valid & ATTR_MTIME_SET))
1009 sattr->ia_valid |= ATTR_MTIME;
1010}
1011
1012/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001013 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 */
Trond Myklebustad389da2007-06-05 12:30:00 -04001015static 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 -07001016{
1017 struct nfs4_state_owner *sp;
1018 struct nfs4_state *state = NULL;
1019 struct nfs_server *server = NFS_SERVER(dir);
David Howells7539bba2006-08-22 20:06:09 -04001020 struct nfs_client *clp = server->nfs_client;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001021 struct nfs4_opendata *opendata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 status = -ENOMEM;
1026 if (!(sp = nfs4_get_state_owner(server, cred))) {
1027 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1028 goto out_err;
1029 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001030 status = nfs4_recover_expired_lease(server);
1031 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001032 goto err_put_state_owner;
Trond Myklebust58d97142006-01-03 09:55:24 +01001033 down_read(&clp->cl_sem);
1034 status = -ENOMEM;
Trond Myklebustad389da2007-06-05 12:30:00 -04001035 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001036 if (opendata == NULL)
Trond Myklebust76723de2006-09-15 08:11:51 -04001037 goto err_release_rwsem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001039 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001041 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001043 if (opendata->o_arg.open_flags & O_EXCL)
1044 nfs4_exclusive_attrset(opendata, sattr);
1045
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001046 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001047 status = PTR_ERR(state);
1048 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001049 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001050 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 nfs4_put_state_owner(sp);
1052 up_read(&clp->cl_sem);
1053 *res = state;
1054 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001055err_opendata_put:
1056 nfs4_opendata_put(opendata);
Trond Myklebust76723de2006-09-15 08:11:51 -04001057err_release_rwsem:
1058 up_read(&clp->cl_sem);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001059err_put_state_owner:
1060 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 *res = NULL;
1063 return status;
1064}
1065
1066
Trond Myklebustad389da2007-06-05 12:30:00 -04001067static 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 -07001068{
1069 struct nfs4_exception exception = { };
1070 struct nfs4_state *res;
1071 int status;
1072
1073 do {
Trond Myklebustad389da2007-06-05 12:30:00 -04001074 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (status == 0)
1076 break;
1077 /* NOTE: BAD_SEQID means the server and client disagree about the
1078 * book-keeping w.r.t. state-changing operations
1079 * (OPEN/CLOSE/LOCK/LOCKU...)
1080 * It is actually a sign of a bug on the client or on the server.
1081 *
1082 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001083 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 * have unhashed the old state_owner for us, and that we can
1085 * therefore safely retry using a new one. We should still warn
1086 * the user though...
1087 */
1088 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001089 printk(KERN_WARNING "NFS: v4 server %s "
1090 " returned a bad sequence-id error!\n",
1091 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 exception.retry = 1;
1093 continue;
1094 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001095 /*
1096 * BAD_STATEID on OPEN means that the server cancelled our
1097 * state before it received the OPEN_CONFIRM.
1098 * Recover by retrying the request as per the discussion
1099 * on Page 181 of RFC3530.
1100 */
1101 if (status == -NFS4ERR_BAD_STATEID) {
1102 exception.retry = 1;
1103 continue;
1104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1106 status, &exception));
1107 } while (exception.retry);
1108 return res;
1109}
1110
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001111static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1112 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001114 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001116 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 .iap = sattr,
1118 .server = server,
1119 .bitmask = server->attr_bitmask,
1120 };
1121 struct nfs_setattrres res = {
1122 .fattr = fattr,
1123 .server = server,
1124 };
1125 struct rpc_message msg = {
1126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1127 .rpc_argp = &arg,
1128 .rpc_resp = &res,
1129 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001130 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001131 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Trond Myklebust0e574af2005-10-27 22:12:38 -04001133 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001135 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1136 /* Use that stateid */
1137 } else if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001139 nfs4_copy_stateid(&arg.stateid, state, current->files);
1140 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1142
Trond Myklebust65e43082005-08-16 11:49:44 -04001143 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001144 if (status == 0 && state != NULL)
1145 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001146 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001149static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1150 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001152 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct nfs4_exception exception = { };
1154 int err;
1155 do {
1156 err = nfs4_handle_exception(server,
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001157 _nfs4_do_setattr(inode, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 &exception);
1159 } while (exception.retry);
1160 return err;
1161}
1162
1163struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001164 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct inode *inode;
1166 struct nfs4_state *state;
1167 struct nfs_closeargs arg;
1168 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001169 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001170 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171};
1172
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001173static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001174{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001175 struct nfs4_closedata *calldata = data;
1176 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001177
1178 nfs4_put_open_state(calldata->state);
1179 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001180 nfs4_put_state_owner(sp);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001181 dput(calldata->path.dentry);
1182 mntput(calldata->path.mnt);
Trond Myklebust95121352005-10-18 14:20:12 -07001183 kfree(calldata);
1184}
1185
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001186static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001188 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 struct nfs_server *server = NFS_SERVER(calldata->inode);
1191
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001192 if (RPC_ASSASSINATED(task))
1193 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /* hmm. we are done with the inode, and in the process of freeing
1195 * the state_owner. we keep this around to process errors
1196 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001197 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 switch (task->tk_status) {
1199 case 0:
Trond Myklebust003707c2007-07-05 18:07:55 -04001200 nfs_set_open_stateid(state, &calldata->res.stateid, calldata->arg.open_flags);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001201 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 break;
1203 case -NFS4ERR_STALE_STATEID:
1204 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 break;
1206 default:
1207 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1208 rpc_restart_call(task);
1209 return;
1210 }
1211 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001212 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001215static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001217 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001218 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct rpc_message msg = {
1220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1221 .rpc_argp = &calldata->arg,
1222 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001223 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 };
Trond Myklebust003707c2007-07-05 18:07:55 -04001225 int clear_rd, clear_wr, clear_rdwr;
1226 int mode;
Trond Myklebust95121352005-10-18 14:20:12 -07001227
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001228 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001229 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001230
1231 mode = FMODE_READ|FMODE_WRITE;
1232 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001233 spin_lock(&state->owner->so_lock);
1234 spin_lock(&calldata->inode->i_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001235 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001236 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001237 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001238 mode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001239 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1240 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1241 }
1242 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001243 mode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001244 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1245 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1246 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001247 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001248 spin_unlock(&calldata->inode->i_lock);
1249 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001250 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001251 /* Note: exit _without_ calling nfs4_close_done */
1252 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001253 return;
1254 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001255 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -07001256 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -07001258 calldata->arg.open_flags = mode;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001259 calldata->timestamp = jiffies;
Trond Myklebust95121352005-10-18 14:20:12 -07001260 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001263static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001264 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001265 .rpc_call_done = nfs4_close_done,
1266 .rpc_release = nfs4_free_closedata,
1267};
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269/*
1270 * It is possible for data to be read/written from a mem-mapped file
1271 * after the sys_close call (which hits the vfs layer as a flush).
1272 * This means that we can't safely call nfsv4 close on a file until
1273 * the inode is cleared. This in turn means that we are not good
1274 * NFSv4 citizens - we do not indicate to the server to update the file's
1275 * share state even when we are done with one of the three share
1276 * stateid's in the inode.
1277 *
1278 * NOTE: Caller must be holding the sp->so_owner semaphore!
1279 */
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001280int nfs4_do_close(struct path *path, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001282 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001284 struct nfs4_state_owner *sp = state->owner;
1285 struct rpc_task *task;
Trond Myklebust95121352005-10-18 14:20:12 -07001286 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Trond Myklebust95121352005-10-18 14:20:12 -07001288 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001290 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001291 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001293 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001294 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001296 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001297 if (calldata->arg.seqid == NULL)
1298 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001299 calldata->arg.bitmask = server->attr_bitmask;
1300 calldata->res.fattr = &calldata->fattr;
1301 calldata->res.server = server;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001302 calldata->path.mnt = mntget(path->mnt);
1303 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001304
Trond Myklebustb39e6252007-06-11 23:05:07 -04001305 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1306 if (IS_ERR(task))
1307 return PTR_ERR(task);
1308 rpc_put_task(task);
1309 return 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001310out_free_calldata:
1311 kfree(calldata);
1312out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001313 nfs4_put_open_state(state);
1314 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001315 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001318static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
Trond Myklebust02a913a2005-10-18 14:20:17 -07001319{
1320 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001321 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001322
Trond Myklebust1b45c462007-07-03 13:04:56 -04001323 /* If the open_intent is for execute, we have an extra check to make */
1324 if (nd->intent.open.flags & FMODE_EXEC) {
1325 ret = _nfs4_do_access(state->inode,
1326 state->owner->so_cred,
1327 nd->intent.open.flags);
1328 if (ret < 0)
1329 goto out_close;
1330 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001331 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001332 if (!IS_ERR(filp)) {
1333 struct nfs_open_context *ctx;
1334 ctx = (struct nfs_open_context *)filp->private_data;
1335 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001336 return 0;
1337 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001338 ret = PTR_ERR(filp);
1339out_close:
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001340 nfs4_close_state(path, state, nd->intent.open.flags);
Trond Myklebust1b45c462007-07-03 13:04:56 -04001341 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001342}
1343
1344struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1346{
Trond Myklebustad389da2007-06-05 12:30:00 -04001347 struct path path = {
1348 .mnt = nd->mnt,
1349 .dentry = dentry,
1350 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 struct iattr attr;
1352 struct rpc_cred *cred;
1353 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001354 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 if (nd->flags & LOOKUP_CREATE) {
1357 attr.ia_mode = nd->intent.open.create_mode;
1358 attr.ia_valid = ATTR_MODE;
1359 if (!IS_POSIXACL(dir))
1360 attr.ia_mode &= ~current->fs->umask;
1361 } else {
1362 attr.ia_valid = 0;
1363 BUG_ON(nd->intent.open.flags & O_CREAT);
1364 }
1365
David Howells1f163412006-08-22 20:06:11 -04001366 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001368 return (struct dentry *)cred;
Trond Myklebustad389da2007-06-05 12:30:00 -04001369 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001371 if (IS_ERR(state)) {
1372 if (PTR_ERR(state) == -ENOENT)
1373 d_add(dentry, NULL);
1374 return (struct dentry *)state;
1375 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001376 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001377 if (res != NULL)
1378 dentry = res;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001379 nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001380 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
1383int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001384nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Trond Myklebustad389da2007-06-05 12:30:00 -04001386 struct path path = {
1387 .mnt = nd->mnt,
1388 .dentry = dentry,
1389 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 struct rpc_cred *cred;
1391 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
David Howells1f163412006-08-22 20:06:11 -04001393 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (IS_ERR(cred))
1395 return PTR_ERR(cred);
1396 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1397 if (IS_ERR(state))
Trond Myklebustad389da2007-06-05 12:30:00 -04001398 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001400 if (IS_ERR(state)) {
1401 switch (PTR_ERR(state)) {
1402 case -EPERM:
1403 case -EACCES:
1404 case -EDQUOT:
1405 case -ENOSPC:
1406 case -EROFS:
1407 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1408 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001409 default:
1410 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001411 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001412 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001413 if (state->inode == dentry->d_inode) {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001414 nfs4_intent_set_file(nd, &path, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return 1;
1416 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001417 nfs4_close_state(&path, state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001418out_drop:
1419 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return 0;
1421}
1422
1423
1424static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1425{
1426 struct nfs4_server_caps_res res = {};
1427 struct rpc_message msg = {
1428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1429 .rpc_argp = fhandle,
1430 .rpc_resp = &res,
1431 };
1432 int status;
1433
1434 status = rpc_call_sync(server->client, &msg, 0);
1435 if (status == 0) {
1436 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1437 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1438 server->caps |= NFS_CAP_ACLS;
1439 if (res.has_links != 0)
1440 server->caps |= NFS_CAP_HARDLINKS;
1441 if (res.has_symlinks != 0)
1442 server->caps |= NFS_CAP_SYMLINKS;
1443 server->acl_bitmask = res.acl_bitmask;
1444 }
1445 return status;
1446}
1447
Trond Myklebust55a97592006-06-09 09:34:19 -04001448int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 struct nfs4_exception exception = { };
1451 int err;
1452 do {
1453 err = nfs4_handle_exception(server,
1454 _nfs4_server_capabilities(server, fhandle),
1455 &exception);
1456 } while (exception.retry);
1457 return err;
1458}
1459
1460static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1461 struct nfs_fsinfo *info)
1462{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 struct nfs4_lookup_root_arg args = {
1464 .bitmask = nfs4_fattr_bitmap,
1465 };
1466 struct nfs4_lookup_res res = {
1467 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001468 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 .fh = fhandle,
1470 };
1471 struct rpc_message msg = {
1472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1473 .rpc_argp = &args,
1474 .rpc_resp = &res,
1475 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001476 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return rpc_call_sync(server->client, &msg, 0);
1478}
1479
1480static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1481 struct nfs_fsinfo *info)
1482{
1483 struct nfs4_exception exception = { };
1484 int err;
1485 do {
1486 err = nfs4_handle_exception(server,
1487 _nfs4_lookup_root(server, fhandle, info),
1488 &exception);
1489 } while (exception.retry);
1490 return err;
1491}
1492
David Howells54ceac42006-08-22 20:06:13 -04001493/*
1494 * get the file handle for the "/" directory on the server
1495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001497 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 int status;
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (status == 0)
1503 status = nfs4_server_capabilities(server, fhandle);
1504 if (status == 0)
1505 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001506 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Manoj Naik6b97fd32006-06-09 09:34:29 -04001509/*
1510 * Get locations and (maybe) other attributes of a referral.
1511 * Note that we'll actually follow the referral later when
1512 * we detect fsid mismatch in inode revalidation
1513 */
1514static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1515{
1516 int status = -ENOMEM;
1517 struct page *page = NULL;
1518 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001519
1520 page = alloc_page(GFP_KERNEL);
1521 if (page == NULL)
1522 goto out;
1523 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1524 if (locations == NULL)
1525 goto out;
1526
Trond Myklebustc228fd32007-01-13 02:28:11 -05001527 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001528 if (status != 0)
1529 goto out;
1530 /* Make sure server returned a different fsid for the referral */
1531 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1532 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1533 status = -EIO;
1534 goto out;
1535 }
1536
1537 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1538 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1539 if (!fattr->mode)
1540 fattr->mode = S_IFDIR;
1541 memset(fhandle, 0, sizeof(struct nfs_fh));
1542out:
1543 if (page)
1544 __free_page(page);
1545 if (locations)
1546 kfree(locations);
1547 return status;
1548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1551{
1552 struct nfs4_getattr_arg args = {
1553 .fh = fhandle,
1554 .bitmask = server->attr_bitmask,
1555 };
1556 struct nfs4_getattr_res res = {
1557 .fattr = fattr,
1558 .server = server,
1559 };
1560 struct rpc_message msg = {
1561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1562 .rpc_argp = &args,
1563 .rpc_resp = &res,
1564 };
1565
Trond Myklebust0e574af2005-10-27 22:12:38 -04001566 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return rpc_call_sync(server->client, &msg, 0);
1568}
1569
1570static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1571{
1572 struct nfs4_exception exception = { };
1573 int err;
1574 do {
1575 err = nfs4_handle_exception(server,
1576 _nfs4_proc_getattr(server, fhandle, fattr),
1577 &exception);
1578 } while (exception.retry);
1579 return err;
1580}
1581
1582/*
1583 * The file is not closed if it is opened due to the a request to change
1584 * the size of the file. The open call will not be needed once the
1585 * VFS layer lookup-intents are implemented.
1586 *
1587 * Close is called when the inode is destroyed.
1588 * If we haven't opened the file for O_WRONLY, we
1589 * need to in the size_change case to obtain a stateid.
1590 *
1591 * Got race?
1592 * Because OPEN is always done by name in nfsv4, it is
1593 * possible that we opened a different file by the same
1594 * name. We can recognize this race condition, but we
1595 * can't do anything about it besides returning an error.
1596 *
1597 * This will be fixed with VFS changes (lookup-intent).
1598 */
1599static int
1600nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1601 struct iattr *sattr)
1602{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001603 struct rpc_cred *cred;
1604 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001605 struct nfs_open_context *ctx;
1606 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 int status;
1608
Trond Myklebust0e574af2005-10-27 22:12:38 -04001609 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
David Howells1f163412006-08-22 20:06:11 -04001611 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001612 if (IS_ERR(cred))
1613 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001614
1615 /* Search for an existing open(O_WRITE) file */
1616 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1617 if (ctx != NULL)
1618 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001619
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001620 status = nfs4_do_setattr(inode, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001621 if (status == 0)
1622 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001623 if (ctx != NULL)
1624 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001625 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return status;
1627}
1628
David Howells2b3de442006-08-22 20:06:09 -04001629static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1630 struct qstr *name, struct nfs_fh *fhandle,
1631 struct nfs_fattr *fattr)
1632{
1633 int status;
1634 struct nfs4_lookup_arg args = {
1635 .bitmask = server->attr_bitmask,
1636 .dir_fh = dirfh,
1637 .name = name,
1638 };
1639 struct nfs4_lookup_res res = {
1640 .server = server,
1641 .fattr = fattr,
1642 .fh = fhandle,
1643 };
1644 struct rpc_message msg = {
1645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1646 .rpc_argp = &args,
1647 .rpc_resp = &res,
1648 };
1649
1650 nfs_fattr_init(fattr);
1651
1652 dprintk("NFS call lookupfh %s\n", name->name);
1653 status = rpc_call_sync(server->client, &msg, 0);
1654 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04001655 return status;
1656}
1657
1658static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1659 struct qstr *name, struct nfs_fh *fhandle,
1660 struct nfs_fattr *fattr)
1661{
1662 struct nfs4_exception exception = { };
1663 int err;
1664 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04001665 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1666 /* FIXME: !!!! */
1667 if (err == -NFS4ERR_MOVED) {
1668 err = -EREMOTE;
1669 break;
1670 }
1671 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04001672 } while (exception.retry);
1673 return err;
1674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1677 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1678{
Trond Myklebust4e56e082007-07-01 18:13:52 -04001679 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04001682 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001683 if (status == -NFS4ERR_MOVED)
1684 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 dprintk("NFS reply lookup: %d\n", status);
1686 return status;
1687}
1688
1689static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1690{
1691 struct nfs4_exception exception = { };
1692 int err;
1693 do {
1694 err = nfs4_handle_exception(NFS_SERVER(dir),
1695 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1696 &exception);
1697 } while (exception.retry);
1698 return err;
1699}
1700
1701static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1702{
1703 struct nfs4_accessargs args = {
1704 .fh = NFS_FH(inode),
1705 };
1706 struct nfs4_accessres res = { 0 };
1707 struct rpc_message msg = {
1708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1709 .rpc_argp = &args,
1710 .rpc_resp = &res,
1711 .rpc_cred = entry->cred,
1712 };
1713 int mode = entry->mask;
1714 int status;
1715
1716 /*
1717 * Determine which access bits we want to ask for...
1718 */
1719 if (mode & MAY_READ)
1720 args.access |= NFS4_ACCESS_READ;
1721 if (S_ISDIR(inode->i_mode)) {
1722 if (mode & MAY_WRITE)
1723 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1724 if (mode & MAY_EXEC)
1725 args.access |= NFS4_ACCESS_LOOKUP;
1726 } else {
1727 if (mode & MAY_WRITE)
1728 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1729 if (mode & MAY_EXEC)
1730 args.access |= NFS4_ACCESS_EXECUTE;
1731 }
1732 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1733 if (!status) {
1734 entry->mask = 0;
1735 if (res.access & NFS4_ACCESS_READ)
1736 entry->mask |= MAY_READ;
1737 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1738 entry->mask |= MAY_WRITE;
1739 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1740 entry->mask |= MAY_EXEC;
1741 }
1742 return status;
1743}
1744
1745static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1746{
1747 struct nfs4_exception exception = { };
1748 int err;
1749 do {
1750 err = nfs4_handle_exception(NFS_SERVER(inode),
1751 _nfs4_proc_access(inode, entry),
1752 &exception);
1753 } while (exception.retry);
1754 return err;
1755}
1756
1757/*
1758 * TODO: For the time being, we don't try to get any attributes
1759 * along with any of the zero-copy operations READ, READDIR,
1760 * READLINK, WRITE.
1761 *
1762 * In the case of the first three, we want to put the GETATTR
1763 * after the read-type operation -- this is because it is hard
1764 * to predict the length of a GETATTR response in v4, and thus
1765 * align the READ data correctly. This means that the GETATTR
1766 * may end up partially falling into the page cache, and we should
1767 * shift it into the 'tail' of the xdr_buf before processing.
1768 * To do this efficiently, we need to know the total length
1769 * of data received, which doesn't seem to be available outside
1770 * of the RPC layer.
1771 *
1772 * In the case of WRITE, we also want to put the GETATTR after
1773 * the operation -- in this case because we want to make sure
1774 * we get the post-operation mtime and size. This means that
1775 * we can't use xdr_encode_pages() as written: we need a variant
1776 * of it which would leave room in the 'tail' iovec.
1777 *
1778 * Both of these changes to the XDR layer would in fact be quite
1779 * minor, but I decided to leave them for a subsequent patch.
1780 */
1781static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1782 unsigned int pgbase, unsigned int pglen)
1783{
1784 struct nfs4_readlink args = {
1785 .fh = NFS_FH(inode),
1786 .pgbase = pgbase,
1787 .pglen = pglen,
1788 .pages = &page,
1789 };
1790 struct rpc_message msg = {
1791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1792 .rpc_argp = &args,
1793 .rpc_resp = NULL,
1794 };
1795
1796 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1797}
1798
1799static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1800 unsigned int pgbase, unsigned int pglen)
1801{
1802 struct nfs4_exception exception = { };
1803 int err;
1804 do {
1805 err = nfs4_handle_exception(NFS_SERVER(inode),
1806 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1807 &exception);
1808 } while (exception.retry);
1809 return err;
1810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812/*
1813 * Got race?
1814 * We will need to arrange for the VFS layer to provide an atomic open.
1815 * Until then, this create/open method is prone to inefficiency and race
1816 * conditions due to the lookup, create, and open VFS calls from sys_open()
1817 * placed on the wire.
1818 *
1819 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1820 * The file will be opened again in the subsequent VFS open call
1821 * (nfs4_proc_file_open).
1822 *
1823 * The open for read will just hang around to be used by any process that
1824 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1825 */
1826
1827static int
1828nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001829 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Trond Myklebustad389da2007-06-05 12:30:00 -04001831 struct path path = {
1832 .mnt = nd->mnt,
1833 .dentry = dentry,
1834 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 struct nfs4_state *state;
1836 struct rpc_cred *cred;
1837 int status = 0;
1838
David Howells1f163412006-08-22 20:06:11 -04001839 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (IS_ERR(cred)) {
1841 status = PTR_ERR(cred);
1842 goto out;
1843 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001844 state = nfs4_do_open(dir, &path, flags, sattr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 put_rpccred(cred);
1846 if (IS_ERR(state)) {
1847 status = PTR_ERR(state);
1848 goto out;
1849 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001850 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (flags & O_EXCL) {
1852 struct nfs_fattr fattr;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001853 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001854 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001855 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001856 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001857 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001858 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001859 status = nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001860 else
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001861 nfs4_close_state(&path, state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862out:
1863 return status;
1864}
1865
1866static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1867{
Trond Myklebust16e42952005-10-27 22:12:44 -04001868 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 struct nfs4_remove_arg args = {
1870 .fh = NFS_FH(dir),
1871 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001872 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001874 struct nfs_fattr dir_attr;
1875 struct nfs4_remove_res res = {
1876 .server = server,
1877 .dir_attr = &dir_attr,
1878 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 struct rpc_message msg = {
1880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1881 .rpc_argp = &args,
1882 .rpc_resp = &res,
1883 };
1884 int status;
1885
Trond Myklebust16e42952005-10-27 22:12:44 -04001886 nfs_fattr_init(res.dir_attr);
1887 status = rpc_call_sync(server->client, &msg, 0);
1888 if (status == 0) {
1889 update_changeattr(dir, &res.cinfo);
1890 nfs_post_op_update_inode(dir, res.dir_attr);
1891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return status;
1893}
1894
1895static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1896{
1897 struct nfs4_exception exception = { };
1898 int err;
1899 do {
1900 err = nfs4_handle_exception(NFS_SERVER(dir),
1901 _nfs4_proc_remove(dir, name),
1902 &exception);
1903 } while (exception.retry);
1904 return err;
1905}
1906
1907struct unlink_desc {
1908 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001909 struct nfs4_remove_res res;
1910 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911};
1912
1913static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1914 struct qstr *name)
1915{
Trond Myklebust16e42952005-10-27 22:12:44 -04001916 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 struct unlink_desc *up;
1918
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001919 up = kmalloc(sizeof(*up), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (!up)
1921 return -ENOMEM;
1922
1923 up->args.fh = NFS_FH(dir->d_inode);
1924 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001925 up->args.bitmask = server->attr_bitmask;
1926 up->res.server = server;
1927 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1930 msg->rpc_argp = &up->args;
1931 msg->rpc_resp = &up->res;
1932 return 0;
1933}
1934
1935static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1936{
1937 struct rpc_message *msg = &task->tk_msg;
1938 struct unlink_desc *up;
1939
1940 if (msg->rpc_resp != NULL) {
1941 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001942 update_changeattr(dir->d_inode, &up->res.cinfo);
1943 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 kfree(up);
1945 msg->rpc_resp = NULL;
1946 msg->rpc_argp = NULL;
1947 }
1948 return 0;
1949}
1950
1951static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1952 struct inode *new_dir, struct qstr *new_name)
1953{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001954 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct nfs4_rename_arg arg = {
1956 .old_dir = NFS_FH(old_dir),
1957 .new_dir = NFS_FH(new_dir),
1958 .old_name = old_name,
1959 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001960 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001962 struct nfs_fattr old_fattr, new_fattr;
1963 struct nfs4_rename_res res = {
1964 .server = server,
1965 .old_fattr = &old_fattr,
1966 .new_fattr = &new_fattr,
1967 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 struct rpc_message msg = {
1969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1970 .rpc_argp = &arg,
1971 .rpc_resp = &res,
1972 };
1973 int status;
1974
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001975 nfs_fattr_init(res.old_fattr);
1976 nfs_fattr_init(res.new_fattr);
1977 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 if (!status) {
1980 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001981 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001983 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985 return status;
1986}
1987
1988static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1989 struct inode *new_dir, struct qstr *new_name)
1990{
1991 struct nfs4_exception exception = { };
1992 int err;
1993 do {
1994 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1995 _nfs4_proc_rename(old_dir, old_name,
1996 new_dir, new_name),
1997 &exception);
1998 } while (exception.retry);
1999 return err;
2000}
2001
2002static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2003{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002004 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 struct nfs4_link_arg arg = {
2006 .fh = NFS_FH(inode),
2007 .dir_fh = NFS_FH(dir),
2008 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002009 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002011 struct nfs_fattr fattr, dir_attr;
2012 struct nfs4_link_res res = {
2013 .server = server,
2014 .fattr = &fattr,
2015 .dir_attr = &dir_attr,
2016 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 struct rpc_message msg = {
2018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2019 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002020 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 };
2022 int status;
2023
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002024 nfs_fattr_init(res.fattr);
2025 nfs_fattr_init(res.dir_attr);
2026 status = rpc_call_sync(server->client, &msg, 0);
2027 if (!status) {
2028 update_changeattr(dir, &res.cinfo);
2029 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002030 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 return status;
2034}
2035
2036static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2037{
2038 struct nfs4_exception exception = { };
2039 int err;
2040 do {
2041 err = nfs4_handle_exception(NFS_SERVER(inode),
2042 _nfs4_proc_link(inode, dir, name),
2043 &exception);
2044 } while (exception.retry);
2045 return err;
2046}
2047
Chuck Lever4f390c12006-08-22 20:06:22 -04002048static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002049 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever4f390c12006-08-22 20:06:22 -04002052 struct nfs_fh fhandle;
2053 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 struct nfs4_create_arg arg = {
2055 .dir_fh = NFS_FH(dir),
2056 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002057 .name = &dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 .attrs = sattr,
2059 .ftype = NF4LNK,
2060 .bitmask = server->attr_bitmask,
2061 };
2062 struct nfs4_create_res res = {
2063 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002064 .fh = &fhandle,
2065 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002066 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 };
2068 struct rpc_message msg = {
2069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2070 .rpc_argp = &arg,
2071 .rpc_resp = &res,
2072 };
2073 int status;
2074
Chuck Lever94a6d752006-08-22 20:06:23 -04002075 if (len > NFS4_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -04002077
Chuck Lever94a6d752006-08-22 20:06:23 -04002078 arg.u.symlink.pages = &page;
2079 arg.u.symlink.len = len;
Chuck Lever4f390c12006-08-22 20:06:22 -04002080 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002081 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Chuck Lever4f390c12006-08-22 20:06:22 -04002084 if (!status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 update_changeattr(dir, &res.dir_cinfo);
Chuck Lever4f390c12006-08-22 20:06:22 -04002086 nfs_post_op_update_inode(dir, res.dir_fattr);
2087 status = nfs_instantiate(dentry, &fhandle, &fattr);
2088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return status;
2090}
2091
Chuck Lever4f390c12006-08-22 20:06:22 -04002092static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002093 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 struct nfs4_exception exception = { };
2096 int err;
2097 do {
2098 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002099 _nfs4_proc_symlink(dir, dentry, page,
2100 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 &exception);
2102 } while (exception.retry);
2103 return err;
2104}
2105
2106static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2107 struct iattr *sattr)
2108{
2109 struct nfs_server *server = NFS_SERVER(dir);
2110 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002111 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 struct nfs4_create_arg arg = {
2113 .dir_fh = NFS_FH(dir),
2114 .server = server,
2115 .name = &dentry->d_name,
2116 .attrs = sattr,
2117 .ftype = NF4DIR,
2118 .bitmask = server->attr_bitmask,
2119 };
2120 struct nfs4_create_res res = {
2121 .server = server,
2122 .fh = &fhandle,
2123 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002124 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 };
2126 struct rpc_message msg = {
2127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2128 .rpc_argp = &arg,
2129 .rpc_resp = &res,
2130 };
2131 int status;
2132
Trond Myklebust0e574af2005-10-27 22:12:38 -04002133 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002134 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2137 if (!status) {
2138 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002139 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 status = nfs_instantiate(dentry, &fhandle, &fattr);
2141 }
2142 return status;
2143}
2144
2145static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2146 struct iattr *sattr)
2147{
2148 struct nfs4_exception exception = { };
2149 int err;
2150 do {
2151 err = nfs4_handle_exception(NFS_SERVER(dir),
2152 _nfs4_proc_mkdir(dir, dentry, sattr),
2153 &exception);
2154 } while (exception.retry);
2155 return err;
2156}
2157
2158static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2159 u64 cookie, struct page *page, unsigned int count, int plus)
2160{
2161 struct inode *dir = dentry->d_inode;
2162 struct nfs4_readdir_arg args = {
2163 .fh = NFS_FH(dir),
2164 .pages = &page,
2165 .pgbase = 0,
2166 .count = count,
2167 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2168 };
2169 struct nfs4_readdir_res res;
2170 struct rpc_message msg = {
2171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2172 .rpc_argp = &args,
2173 .rpc_resp = &res,
2174 .rpc_cred = cred,
2175 };
2176 int status;
2177
Trond Myklebusteadf4592005-06-22 17:16:39 +00002178 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2179 dentry->d_parent->d_name.name,
2180 dentry->d_name.name,
2181 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2183 res.pgbase = args.pgbase;
2184 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2185 if (status == 0)
2186 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebusteadf4592005-06-22 17:16:39 +00002187 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return status;
2189}
2190
2191static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2192 u64 cookie, struct page *page, unsigned int count, int plus)
2193{
2194 struct nfs4_exception exception = { };
2195 int err;
2196 do {
2197 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2198 _nfs4_proc_readdir(dentry, cred, cookie,
2199 page, count, plus),
2200 &exception);
2201 } while (exception.retry);
2202 return err;
2203}
2204
2205static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2206 struct iattr *sattr, dev_t rdev)
2207{
2208 struct nfs_server *server = NFS_SERVER(dir);
2209 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002210 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 struct nfs4_create_arg arg = {
2212 .dir_fh = NFS_FH(dir),
2213 .server = server,
2214 .name = &dentry->d_name,
2215 .attrs = sattr,
2216 .bitmask = server->attr_bitmask,
2217 };
2218 struct nfs4_create_res res = {
2219 .server = server,
2220 .fh = &fh,
2221 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002222 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 };
2224 struct rpc_message msg = {
2225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2226 .rpc_argp = &arg,
2227 .rpc_resp = &res,
2228 };
2229 int status;
2230 int mode = sattr->ia_mode;
2231
Trond Myklebust0e574af2005-10-27 22:12:38 -04002232 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002233 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2236 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2237 if (S_ISFIFO(mode))
2238 arg.ftype = NF4FIFO;
2239 else if (S_ISBLK(mode)) {
2240 arg.ftype = NF4BLK;
2241 arg.u.device.specdata1 = MAJOR(rdev);
2242 arg.u.device.specdata2 = MINOR(rdev);
2243 }
2244 else if (S_ISCHR(mode)) {
2245 arg.ftype = NF4CHR;
2246 arg.u.device.specdata1 = MAJOR(rdev);
2247 arg.u.device.specdata2 = MINOR(rdev);
2248 }
2249 else
2250 arg.ftype = NF4SOCK;
2251
2252 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2253 if (status == 0) {
2254 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002255 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 status = nfs_instantiate(dentry, &fh, &fattr);
2257 }
2258 return status;
2259}
2260
2261static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2262 struct iattr *sattr, dev_t rdev)
2263{
2264 struct nfs4_exception exception = { };
2265 int err;
2266 do {
2267 err = nfs4_handle_exception(NFS_SERVER(dir),
2268 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2269 &exception);
2270 } while (exception.retry);
2271 return err;
2272}
2273
2274static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2275 struct nfs_fsstat *fsstat)
2276{
2277 struct nfs4_statfs_arg args = {
2278 .fh = fhandle,
2279 .bitmask = server->attr_bitmask,
2280 };
2281 struct rpc_message msg = {
2282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2283 .rpc_argp = &args,
2284 .rpc_resp = fsstat,
2285 };
2286
Trond Myklebust0e574af2005-10-27 22:12:38 -04002287 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return rpc_call_sync(server->client, &msg, 0);
2289}
2290
2291static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2292{
2293 struct nfs4_exception exception = { };
2294 int err;
2295 do {
2296 err = nfs4_handle_exception(server,
2297 _nfs4_proc_statfs(server, fhandle, fsstat),
2298 &exception);
2299 } while (exception.retry);
2300 return err;
2301}
2302
2303static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2304 struct nfs_fsinfo *fsinfo)
2305{
2306 struct nfs4_fsinfo_arg args = {
2307 .fh = fhandle,
2308 .bitmask = server->attr_bitmask,
2309 };
2310 struct rpc_message msg = {
2311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2312 .rpc_argp = &args,
2313 .rpc_resp = fsinfo,
2314 };
2315
2316 return rpc_call_sync(server->client, &msg, 0);
2317}
2318
2319static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2320{
2321 struct nfs4_exception exception = { };
2322 int err;
2323
2324 do {
2325 err = nfs4_handle_exception(server,
2326 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2327 &exception);
2328 } while (exception.retry);
2329 return err;
2330}
2331
2332static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2333{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002334 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2336}
2337
2338static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2339 struct nfs_pathconf *pathconf)
2340{
2341 struct nfs4_pathconf_arg args = {
2342 .fh = fhandle,
2343 .bitmask = server->attr_bitmask,
2344 };
2345 struct rpc_message msg = {
2346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2347 .rpc_argp = &args,
2348 .rpc_resp = pathconf,
2349 };
2350
2351 /* None of the pathconf attributes are mandatory to implement */
2352 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2353 memset(pathconf, 0, sizeof(*pathconf));
2354 return 0;
2355 }
2356
Trond Myklebust0e574af2005-10-27 22:12:38 -04002357 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return rpc_call_sync(server->client, &msg, 0);
2359}
2360
2361static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2362 struct nfs_pathconf *pathconf)
2363{
2364 struct nfs4_exception exception = { };
2365 int err;
2366
2367 do {
2368 err = nfs4_handle_exception(server,
2369 _nfs4_proc_pathconf(server, fhandle, pathconf),
2370 &exception);
2371 } while (exception.retry);
2372 return err;
2373}
2374
Trond Myklebustec06c092006-03-20 13:44:27 -05002375static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Trond Myklebustec06c092006-03-20 13:44:27 -05002377 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Trond Myklebustec06c092006-03-20 13:44:27 -05002379 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002381 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002384 renew_lease(server, data->timestamp);
2385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
Trond Myklebustec06c092006-03-20 13:44:27 -05002388static void nfs4_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 struct rpc_message msg = {
2391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2392 .rpc_argp = &data->args,
2393 .rpc_resp = &data->res,
2394 .rpc_cred = data->cred,
2395 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 data->timestamp = jiffies;
2398
Trond Myklebustec06c092006-03-20 13:44:27 -05002399 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Trond Myklebust788e7a82006-03-20 13:44:27 -05002402static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 struct inode *inode = data->inode;
2405
2406 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2407 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002408 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002410 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002412 nfs_post_op_update_inode(inode, data->res.fattr);
2413 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
Trond Myklebust788e7a82006-03-20 13:44:27 -05002417static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 struct rpc_message msg = {
2420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2421 .rpc_argp = &data->args,
2422 .rpc_resp = &data->res,
2423 .rpc_cred = data->cred,
2424 };
2425 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002426 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 int stable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429 if (how & FLUSH_STABLE) {
2430 if (!NFS_I(inode)->ncommit)
2431 stable = NFS_FILE_SYNC;
2432 else
2433 stable = NFS_DATA_SYNC;
2434 } else
2435 stable = NFS_UNSTABLE;
2436 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002437 data->args.bitmask = server->attr_bitmask;
2438 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 data->timestamp = jiffies;
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -05002443 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
Trond Myklebust788e7a82006-03-20 13:44:27 -05002446static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 struct inode *inode = data->inode;
2449
2450 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2451 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002452 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002454 if (task->tk_status >= 0)
2455 nfs_post_op_update_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
Trond Myklebust788e7a82006-03-20 13:44:27 -05002459static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 struct rpc_message msg = {
2462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2463 .rpc_argp = &data->args,
2464 .rpc_resp = &data->res,
2465 .rpc_cred = data->cred,
2466 };
Trond Myklebust788e7a82006-03-20 13:44:27 -05002467 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002469 data->args.bitmask = server->attr_bitmask;
2470 data->res.server = server;
2471
Trond Myklebust788e7a82006-03-20 13:44:27 -05002472 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
2475/*
2476 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2477 * standalone procedure for queueing an asynchronous RENEW.
2478 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002479static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
David Howellsadfa6f92006-08-22 20:06:08 -04002481 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002482 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 if (task->tk_status < 0) {
2485 switch (task->tk_status) {
2486 case -NFS4ERR_STALE_CLIENTID:
2487 case -NFS4ERR_EXPIRED:
2488 case -NFS4ERR_CB_PATH_DOWN:
2489 nfs4_schedule_state_recovery(clp);
2490 }
2491 return;
2492 }
2493 spin_lock(&clp->cl_lock);
2494 if (time_before(clp->cl_last_renewal,timestamp))
2495 clp->cl_last_renewal = timestamp;
2496 spin_unlock(&clp->cl_lock);
2497}
2498
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002499static const struct rpc_call_ops nfs4_renew_ops = {
2500 .rpc_call_done = nfs4_renew_done,
2501};
2502
David Howellsadfa6f92006-08-22 20:06:08 -04002503int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
2505 struct rpc_message msg = {
2506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2507 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002508 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 };
2510
2511 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002512 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
David Howellsadfa6f92006-08-22 20:06:08 -04002515int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
2517 struct rpc_message msg = {
2518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2519 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002520 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 };
2522 unsigned long now = jiffies;
2523 int status;
2524
2525 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2526 if (status < 0)
2527 return status;
2528 spin_lock(&clp->cl_lock);
2529 if (time_before(clp->cl_last_renewal,now))
2530 clp->cl_last_renewal = now;
2531 spin_unlock(&clp->cl_lock);
2532 return 0;
2533}
2534
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002535static inline int nfs4_server_supports_acls(struct nfs_server *server)
2536{
2537 return (server->caps & NFS_CAP_ACLS)
2538 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2539 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2540}
2541
2542/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2543 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2544 * the stack.
2545 */
2546#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2547
2548static void buf_to_pages(const void *buf, size_t buflen,
2549 struct page **pages, unsigned int *pgbase)
2550{
2551 const void *p = buf;
2552
2553 *pgbase = offset_in_page(buf);
2554 p -= *pgbase;
2555 while (p < buf + buflen) {
2556 *(pages++) = virt_to_page(p);
2557 p += PAGE_CACHE_SIZE;
2558 }
2559}
2560
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002561struct nfs4_cached_acl {
2562 int cached;
2563 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002564 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002565};
2566
2567static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002568{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002569 struct nfs_inode *nfsi = NFS_I(inode);
2570
2571 spin_lock(&inode->i_lock);
2572 kfree(nfsi->nfs4_acl);
2573 nfsi->nfs4_acl = acl;
2574 spin_unlock(&inode->i_lock);
2575}
2576
2577static void nfs4_zap_acl_attr(struct inode *inode)
2578{
2579 nfs4_set_cached_acl(inode, NULL);
2580}
2581
2582static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2583{
2584 struct nfs_inode *nfsi = NFS_I(inode);
2585 struct nfs4_cached_acl *acl;
2586 int ret = -ENOENT;
2587
2588 spin_lock(&inode->i_lock);
2589 acl = nfsi->nfs4_acl;
2590 if (acl == NULL)
2591 goto out;
2592 if (buf == NULL) /* user is just asking for length */
2593 goto out_len;
2594 if (acl->cached == 0)
2595 goto out;
2596 ret = -ERANGE; /* see getxattr(2) man page */
2597 if (acl->len > buflen)
2598 goto out;
2599 memcpy(buf, acl->data, acl->len);
2600out_len:
2601 ret = acl->len;
2602out:
2603 spin_unlock(&inode->i_lock);
2604 return ret;
2605}
2606
2607static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2608{
2609 struct nfs4_cached_acl *acl;
2610
2611 if (buf && acl_len <= PAGE_SIZE) {
2612 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2613 if (acl == NULL)
2614 goto out;
2615 acl->cached = 1;
2616 memcpy(acl->data, buf, acl_len);
2617 } else {
2618 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2619 if (acl == NULL)
2620 goto out;
2621 acl->cached = 0;
2622 }
2623 acl->len = acl_len;
2624out:
2625 nfs4_set_cached_acl(inode, acl);
2626}
2627
Trond Myklebust16b42892006-08-24 12:27:15 -04002628static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002629{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002630 struct page *pages[NFS4ACL_MAXPAGES];
2631 struct nfs_getaclargs args = {
2632 .fh = NFS_FH(inode),
2633 .acl_pages = pages,
2634 .acl_len = buflen,
2635 };
2636 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002637 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002638 struct rpc_message msg = {
2639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2640 .rpc_argp = &args,
2641 .rpc_resp = &resp_len,
2642 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002643 struct page *localpage = NULL;
2644 int ret;
2645
2646 if (buflen < PAGE_SIZE) {
2647 /* As long as we're doing a round trip to the server anyway,
2648 * let's be prepared for a page of acl data. */
2649 localpage = alloc_page(GFP_KERNEL);
2650 resp_buf = page_address(localpage);
2651 if (localpage == NULL)
2652 return -ENOMEM;
2653 args.acl_pages[0] = localpage;
2654 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002655 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002656 } else {
2657 resp_buf = buf;
2658 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2659 }
2660 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2661 if (ret)
2662 goto out_free;
2663 if (resp_len > args.acl_len)
2664 nfs4_write_cached_acl(inode, NULL, resp_len);
2665 else
2666 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2667 if (buf) {
2668 ret = -ERANGE;
2669 if (resp_len > buflen)
2670 goto out_free;
2671 if (localpage)
2672 memcpy(buf, resp_buf, resp_len);
2673 }
2674 ret = resp_len;
2675out_free:
2676 if (localpage)
2677 __free_page(localpage);
2678 return ret;
2679}
2680
Trond Myklebust16b42892006-08-24 12:27:15 -04002681static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2682{
2683 struct nfs4_exception exception = { };
2684 ssize_t ret;
2685 do {
2686 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2687 if (ret >= 0)
2688 break;
2689 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2690 } while (exception.retry);
2691 return ret;
2692}
2693
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002694static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2695{
2696 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002697 int ret;
2698
2699 if (!nfs4_server_supports_acls(server))
2700 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002701 ret = nfs_revalidate_inode(server, inode);
2702 if (ret < 0)
2703 return ret;
2704 ret = nfs4_read_cached_acl(inode, buf, buflen);
2705 if (ret != -ENOENT)
2706 return ret;
2707 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002708}
2709
Trond Myklebust16b42892006-08-24 12:27:15 -04002710static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002711{
2712 struct nfs_server *server = NFS_SERVER(inode);
2713 struct page *pages[NFS4ACL_MAXPAGES];
2714 struct nfs_setaclargs arg = {
2715 .fh = NFS_FH(inode),
2716 .acl_pages = pages,
2717 .acl_len = buflen,
2718 };
2719 struct rpc_message msg = {
2720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2721 .rpc_argp = &arg,
2722 .rpc_resp = NULL,
2723 };
2724 int ret;
2725
2726 if (!nfs4_server_supports_acls(server))
2727 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002728 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002729 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
David Howells1f163412006-08-22 20:06:11 -04002730 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
J. Bruce Fields08efa202007-05-01 10:56:25 -04002731 nfs_zap_caches(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002732 return ret;
2733}
2734
Trond Myklebust16b42892006-08-24 12:27:15 -04002735static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2736{
2737 struct nfs4_exception exception = { };
2738 int err;
2739 do {
2740 err = nfs4_handle_exception(NFS_SERVER(inode),
2741 __nfs4_proc_set_acl(inode, buf, buflen),
2742 &exception);
2743 } while (exception.retry);
2744 return err;
2745}
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002748nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
David Howells7539bba2006-08-22 20:06:09 -04002750 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
2752 if (!clp || task->tk_status >= 0)
2753 return 0;
2754 switch(task->tk_status) {
2755 case -NFS4ERR_STALE_CLIENTID:
2756 case -NFS4ERR_STALE_STATEID:
2757 case -NFS4ERR_EXPIRED:
2758 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2759 nfs4_schedule_state_recovery(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002760 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 rpc_wake_up_task(task);
2762 task->tk_status = 0;
2763 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 case -NFS4ERR_DELAY:
Chuck Lever006ea732006-03-20 13:44:14 -05002765 nfs_inc_server_stats((struct nfs_server *) server,
2766 NFSIOS_DELAY);
2767 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2769 task->tk_status = 0;
2770 return -EAGAIN;
2771 case -NFS4ERR_OLD_STATEID:
2772 task->tk_status = 0;
2773 return -EAGAIN;
2774 }
2775 task->tk_status = nfs4_map_errors(task->tk_status);
2776 return 0;
2777}
2778
Trond Myklebust433fbe42006-01-03 09:55:22 +01002779static int nfs4_wait_bit_interruptible(void *word)
2780{
2781 if (signal_pending(current))
2782 return -ERESTARTSYS;
2783 schedule();
2784 return 0;
2785}
2786
David Howellsadfa6f92006-08-22 20:06:08 -04002787static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 sigset_t oldset;
Trond Myklebust433fbe42006-01-03 09:55:22 +01002790 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
2792 might_sleep();
2793
Trond Myklebuste148582e2006-12-13 16:43:13 -05002794 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002797 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2798 nfs4_wait_bit_interruptible,
2799 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 rpc_clnt_sigunmask(clnt, &oldset);
Trond Myklebuste148582e2006-12-13 16:43:13 -05002801
2802 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 return res;
2804}
2805
2806static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2807{
2808 sigset_t oldset;
2809 int res = 0;
2810
2811 might_sleep();
2812
2813 if (*timeout <= 0)
2814 *timeout = NFS4_POLL_RETRY_MIN;
2815 if (*timeout > NFS4_POLL_RETRY_MAX)
2816 *timeout = NFS4_POLL_RETRY_MAX;
2817 rpc_clnt_sigmask(clnt, &oldset);
2818 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002819 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (signalled())
2821 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002822 } else
2823 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 rpc_clnt_sigunmask(clnt, &oldset);
2825 *timeout <<= 1;
2826 return res;
2827}
2828
2829/* This is the error handling routine for processes that are allowed
2830 * to sleep.
2831 */
Trond Myklebust10afec92007-05-14 17:16:04 -04002832static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
David Howells7539bba2006-08-22 20:06:09 -04002834 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 int ret = errorcode;
2836
2837 exception->retry = 0;
2838 switch(errorcode) {
2839 case 0:
2840 return 0;
2841 case -NFS4ERR_STALE_CLIENTID:
2842 case -NFS4ERR_STALE_STATEID:
2843 case -NFS4ERR_EXPIRED:
Trond Myklebust433fbe42006-01-03 09:55:22 +01002844 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 ret = nfs4_wait_clnt_recover(server->client, clp);
2846 if (ret == 0)
2847 exception->retry = 1;
2848 break;
Trond Myklebustc5149832006-09-15 08:25:04 -04002849 case -NFS4ERR_FILE_OPEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 case -NFS4ERR_GRACE:
2851 case -NFS4ERR_DELAY:
2852 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002853 if (ret != 0)
2854 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002856 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 }
2858 /* We failed to handle the error */
2859 return nfs4_map_errors(ret);
2860}
2861
David Howellsadfa6f92006-08-22 20:06:08 -04002862int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 nfs4_verifier sc_verifier;
2865 struct nfs4_setclientid setclientid = {
2866 .sc_verifier = &sc_verifier,
2867 .sc_prog = program,
2868 };
2869 struct rpc_message msg = {
2870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2871 .rpc_argp = &setclientid,
2872 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002873 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 };
Al Virobc4785c2006-10-19 23:28:51 -07002875 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 int loop = 0;
2877 int status;
2878
Al Virobc4785c2006-10-19 23:28:51 -07002879 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2881 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2882
2883 for(;;) {
2884 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2885 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
David Howells24c8dbb2006-08-22 20:06:10 -04002886 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
Trond Myklebust286d7d62006-01-03 09:55:26 +01002887 cred->cr_ops->cr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 clp->cl_id_uniquifier);
2889 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2890 sizeof(setclientid.sc_netid), "tcp");
2891 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2892 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2893 clp->cl_ipaddr, port >> 8, port & 255);
2894
2895 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2896 if (status != -NFS4ERR_CLID_INUSE)
2897 break;
2898 if (signalled())
2899 break;
2900 if (loop++ & 1)
2901 ssleep(clp->cl_lease_time + 1);
2902 else
2903 if (++clp->cl_id_uniquifier == 0)
2904 break;
2905 }
2906 return status;
2907}
2908
David Howellsadfa6f92006-08-22 20:06:08 -04002909static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
2911 struct nfs_fsinfo fsinfo;
2912 struct rpc_message msg = {
2913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2914 .rpc_argp = clp,
2915 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002916 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 };
2918 unsigned long now;
2919 int status;
2920
2921 now = jiffies;
2922 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2923 if (status == 0) {
2924 spin_lock(&clp->cl_lock);
2925 clp->cl_lease_time = fsinfo.lease_time * HZ;
2926 clp->cl_last_renewal = now;
Trond Myklebust58d97142006-01-03 09:55:24 +01002927 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 spin_unlock(&clp->cl_lock);
2929 }
2930 return status;
2931}
2932
David Howellsadfa6f92006-08-22 20:06:08 -04002933int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05002934{
2935 long timeout;
2936 int err;
2937 do {
2938 err = _nfs4_proc_setclientid_confirm(clp, cred);
2939 switch (err) {
2940 case 0:
2941 return err;
2942 case -NFS4ERR_RESOURCE:
2943 /* The IBM lawyers misread another document! */
2944 case -NFS4ERR_DELAY:
2945 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2946 }
2947 } while (err == 0);
2948 return err;
2949}
2950
Trond Myklebustfe650402006-01-03 09:55:18 +01002951struct nfs4_delegreturndata {
2952 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002953 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01002954 struct nfs_fh fh;
2955 nfs4_stateid stateid;
2956 struct rpc_cred *cred;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002957 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002958 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01002959 int rpc_status;
2960};
2961
2962static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
Trond Myklebustfe650402006-01-03 09:55:18 +01002964 struct nfs4_delegreturndata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 struct rpc_message msg = {
2966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
Trond Myklebustfe650402006-01-03 09:55:18 +01002967 .rpc_argp = &data->args,
Trond Myklebustfa178f22006-01-03 09:55:38 +01002968 .rpc_resp = &data->res,
Trond Myklebustfe650402006-01-03 09:55:18 +01002969 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 };
Trond Myklebustfa178f22006-01-03 09:55:38 +01002971 nfs_fattr_init(data->res.fattr);
Trond Myklebustfe650402006-01-03 09:55:18 +01002972 rpc_call_setup(task, &msg, 0);
2973}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Trond Myklebustfe650402006-01-03 09:55:18 +01002975static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2976{
2977 struct nfs4_delegreturndata *data = calldata;
2978 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002979 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01002980 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01002981}
2982
2983static void nfs4_delegreturn_release(void *calldata)
2984{
2985 struct nfs4_delegreturndata *data = calldata;
2986
2987 put_rpccred(data->cred);
2988 kfree(calldata);
2989}
2990
Jesper Juhlc8d149f2006-03-20 13:44:07 -05002991static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01002992 .rpc_call_prepare = nfs4_delegreturn_prepare,
2993 .rpc_call_done = nfs4_delegreturn_done,
2994 .rpc_release = nfs4_delegreturn_release,
2995};
2996
2997static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2998{
2999 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003000 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003001 struct rpc_task *task;
3002 int status;
3003
3004 data = kmalloc(sizeof(*data), GFP_KERNEL);
3005 if (data == NULL)
3006 return -ENOMEM;
3007 data->args.fhandle = &data->fh;
3008 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003009 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003010 nfs_copy_fh(&data->fh, NFS_FH(inode));
3011 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003012 data->res.fattr = &data->fattr;
3013 data->res.server = server;
Trond Myklebustfe650402006-01-03 09:55:18 +01003014 data->cred = get_rpccred(cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003015 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003016 data->rpc_status = 0;
3017
3018 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003019 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003020 return PTR_ERR(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003021 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebustfa178f22006-01-03 09:55:38 +01003022 if (status == 0) {
Trond Myklebustfe650402006-01-03 09:55:18 +01003023 status = data->rpc_status;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003024 if (status == 0)
3025 nfs_post_op_update_inode(inode, &data->fattr);
3026 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003027 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003028 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
3031int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
3032{
3033 struct nfs_server *server = NFS_SERVER(inode);
3034 struct nfs4_exception exception = { };
3035 int err;
3036 do {
3037 err = _nfs4_proc_delegreturn(inode, cred, stateid);
3038 switch (err) {
3039 case -NFS4ERR_STALE_STATEID:
3040 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 case 0:
3042 return 0;
3043 }
3044 err = nfs4_handle_exception(server, err, &exception);
3045 } while (exception.retry);
3046 return err;
3047}
3048
3049#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3050#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3051
3052/*
3053 * sleep, with exponential backoff, and retry the LOCK operation.
3054 */
3055static unsigned long
3056nfs4_set_lock_task_retry(unsigned long timeout)
3057{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003058 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 timeout <<= 1;
3060 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3061 return NFS4_LOCK_MAXTIMEOUT;
3062 return timeout;
3063}
3064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3066{
3067 struct inode *inode = state->inode;
3068 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003069 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003070 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003072 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003074 struct nfs_lockt_res res = {
3075 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 };
3077 struct rpc_message msg = {
3078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3079 .rpc_argp = &arg,
3080 .rpc_resp = &res,
3081 .rpc_cred = state->owner->so_cred,
3082 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 struct nfs4_lock_state *lsp;
3084 int status;
3085
3086 down_read(&clp->cl_sem);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003087 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003088 status = nfs4_set_lock_state(state, request);
3089 if (status != 0)
3090 goto out;
3091 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003092 arg.lock_owner.id = lsp->ls_id.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003094 switch (status) {
3095 case 0:
3096 request->fl_type = F_UNLCK;
3097 break;
3098 case -NFS4ERR_DENIED:
3099 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003101 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003102out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 up_read(&clp->cl_sem);
3104 return status;
3105}
3106
3107static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3108{
3109 struct nfs4_exception exception = { };
3110 int err;
3111
3112 do {
3113 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3114 _nfs4_proc_getlk(state, cmd, request),
3115 &exception);
3116 } while (exception.retry);
3117 return err;
3118}
3119
3120static int do_vfs_lock(struct file *file, struct file_lock *fl)
3121{
3122 int res = 0;
3123 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3124 case FL_POSIX:
3125 res = posix_lock_file_wait(file, fl);
3126 break;
3127 case FL_FLOCK:
3128 res = flock_lock_file_wait(file, fl);
3129 break;
3130 default:
3131 BUG();
3132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 return res;
3134}
3135
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003136struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003137 struct nfs_locku_args arg;
3138 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003139 struct nfs4_lock_state *lsp;
3140 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003141 struct file_lock fl;
3142 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003143 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003144};
3145
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003146static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3147 struct nfs_open_context *ctx,
3148 struct nfs4_lock_state *lsp,
3149 struct nfs_seqid *seqid)
3150{
3151 struct nfs4_unlockdata *p;
3152 struct inode *inode = lsp->ls_state->inode;
3153
3154 p = kmalloc(sizeof(*p), GFP_KERNEL);
3155 if (p == NULL)
3156 return NULL;
3157 p->arg.fh = NFS_FH(inode);
3158 p->arg.fl = &p->fl;
3159 p->arg.seqid = seqid;
3160 p->arg.stateid = &lsp->ls_stateid;
3161 p->lsp = lsp;
3162 atomic_inc(&lsp->ls_count);
3163 /* Ensure we don't close file until we're done freeing locks! */
3164 p->ctx = get_nfs_open_context(ctx);
3165 memcpy(&p->fl, fl, sizeof(p->fl));
3166 p->server = NFS_SERVER(inode);
3167 return p;
3168}
3169
Trond Myklebust06f814a2006-01-03 09:55:07 +01003170static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003171{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003172 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003173 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003174 nfs4_put_lock_state(calldata->lsp);
3175 put_nfs_open_context(calldata->ctx);
3176 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003177}
3178
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003179static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003180{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003181 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003182
Trond Myklebust06f814a2006-01-03 09:55:07 +01003183 if (RPC_ASSASSINATED(task))
3184 return;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003185 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003186 switch (task->tk_status) {
3187 case 0:
3188 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003189 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003190 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003191 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003192 break;
3193 case -NFS4ERR_STALE_STATEID:
3194 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003195 break;
3196 default:
Trond Myklebusta6a352e2006-12-13 16:43:06 -05003197 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003198 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003199 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003200}
3201
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003202static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003203{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003204 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 struct rpc_message msg = {
3206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003207 .rpc_argp = &calldata->arg,
3208 .rpc_resp = &calldata->res,
3209 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003212 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003213 return;
3214 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003215 /* Note: exit _without_ running nfs4_locku_done */
3216 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003217 return;
3218 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003219 calldata->timestamp = jiffies;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003220 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003223static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003224 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003225 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003226 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003227};
3228
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003229static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3230 struct nfs_open_context *ctx,
3231 struct nfs4_lock_state *lsp,
3232 struct nfs_seqid *seqid)
3233{
3234 struct nfs4_unlockdata *data;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003235
3236 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3237 if (data == NULL) {
3238 nfs_free_seqid(seqid);
3239 return ERR_PTR(-ENOMEM);
3240 }
3241
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003242 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003243}
3244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3246{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003247 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003248 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003249 struct rpc_task *task;
3250 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Trond Myklebust9b073572006-06-29 16:38:34 -04003252 status = nfs4_set_lock_state(state, request);
3253 /* Unlock _before_ we do the RPC call */
3254 request->fl_flags |= FL_EXISTS;
3255 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3256 goto out;
3257 if (status != 0)
3258 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003259 /* Is this a delegated lock? */
3260 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003261 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003262 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003263 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003264 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003265 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003266 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003267 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3268 status = PTR_ERR(task);
3269 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003270 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003271 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003272 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003273out:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003274 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003277struct nfs4_lockdata {
3278 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003279 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003280 struct nfs4_lock_state *lsp;
3281 struct nfs_open_context *ctx;
3282 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003283 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003284 int rpc_status;
3285 int cancelled;
3286};
3287
3288static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3289 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3290{
3291 struct nfs4_lockdata *p;
3292 struct inode *inode = lsp->ls_state->inode;
3293 struct nfs_server *server = NFS_SERVER(inode);
3294
3295 p = kzalloc(sizeof(*p), GFP_KERNEL);
3296 if (p == NULL)
3297 return NULL;
3298
3299 p->arg.fh = NFS_FH(inode);
3300 p->arg.fl = &p->fl;
3301 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3302 if (p->arg.lock_seqid == NULL)
3303 goto out_free;
3304 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003305 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003306 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003307 p->lsp = lsp;
3308 atomic_inc(&lsp->ls_count);
3309 p->ctx = get_nfs_open_context(ctx);
3310 memcpy(&p->fl, fl, sizeof(p->fl));
3311 return p;
3312out_free:
3313 kfree(p);
3314 return NULL;
3315}
3316
3317static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3318{
3319 struct nfs4_lockdata *data = calldata;
3320 struct nfs4_state *state = data->lsp->ls_state;
3321 struct nfs4_state_owner *sp = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 struct rpc_message msg = {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3324 .rpc_argp = &data->arg,
3325 .rpc_resp = &data->res,
3326 .rpc_cred = sp->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003329 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3330 return;
3331 dprintk("%s: begin!\n", __FUNCTION__);
3332 /* Do we need to do an open_to_lock_owner? */
3333 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3334 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3335 if (data->arg.open_seqid == NULL) {
3336 data->rpc_status = -ENOMEM;
3337 task->tk_action = NULL;
Trond Myklebust06735b32005-10-18 14:20:15 -07003338 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07003339 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003340 data->arg.open_stateid = &state->stateid;
3341 data->arg.new_lock_owner = 1;
3342 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003343 data->timestamp = jiffies;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003344 rpc_call_setup(task, &msg, 0);
Trond Myklebust06735b32005-10-18 14:20:15 -07003345out:
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003346 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3347}
3348
3349static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3350{
3351 struct nfs4_lockdata *data = calldata;
3352
3353 dprintk("%s: begin!\n", __FUNCTION__);
3354
3355 data->rpc_status = task->tk_status;
3356 if (RPC_ASSASSINATED(task))
3357 goto out;
3358 if (data->arg.new_lock_owner != 0) {
3359 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3360 if (data->rpc_status == 0)
3361 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3362 else
3363 goto out;
3364 }
3365 if (data->rpc_status == 0) {
3366 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3367 sizeof(data->lsp->ls_stateid.data));
3368 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003369 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003370 }
3371 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3372out:
3373 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3374}
3375
3376static void nfs4_lock_release(void *calldata)
3377{
3378 struct nfs4_lockdata *data = calldata;
3379
3380 dprintk("%s: begin!\n", __FUNCTION__);
3381 if (data->arg.open_seqid != NULL)
3382 nfs_free_seqid(data->arg.open_seqid);
3383 if (data->cancelled != 0) {
3384 struct rpc_task *task;
3385 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3386 data->arg.lock_seqid);
3387 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003388 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003389 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3390 } else
3391 nfs_free_seqid(data->arg.lock_seqid);
3392 nfs4_put_lock_state(data->lsp);
3393 put_nfs_open_context(data->ctx);
3394 kfree(data);
3395 dprintk("%s: done!\n", __FUNCTION__);
3396}
3397
3398static const struct rpc_call_ops nfs4_lock_ops = {
3399 .rpc_call_prepare = nfs4_lock_prepare,
3400 .rpc_call_done = nfs4_lock_done,
3401 .rpc_release = nfs4_lock_release,
3402};
3403
3404static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3405{
3406 struct nfs4_lockdata *data;
3407 struct rpc_task *task;
3408 int ret;
3409
3410 dprintk("%s: begin!\n", __FUNCTION__);
3411 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3412 fl->fl_u.nfs4_fl.owner);
3413 if (data == NULL)
3414 return -ENOMEM;
3415 if (IS_SETLKW(cmd))
3416 data->arg.block = 1;
3417 if (reclaim != 0)
3418 data->arg.reclaim = 1;
3419 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3420 &nfs4_lock_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003421 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003422 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003423 ret = nfs4_wait_for_completion_rpc_task(task);
3424 if (ret == 0) {
3425 ret = data->rpc_status;
3426 if (ret == -NFS4ERR_DENIED)
3427 ret = -EAGAIN;
3428 } else
3429 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003430 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003431 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3432 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433}
3434
3435static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3436{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003437 struct nfs_server *server = NFS_SERVER(state->inode);
3438 struct nfs4_exception exception = { };
3439 int err;
3440
3441 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003442 /* Cache the lock if possible... */
3443 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3444 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003445 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3446 if (err != -NFS4ERR_DELAY)
3447 break;
3448 nfs4_handle_exception(server, err, &exception);
3449 } while (exception.retry);
3450 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451}
3452
3453static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3454{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003455 struct nfs_server *server = NFS_SERVER(state->inode);
3456 struct nfs4_exception exception = { };
3457 int err;
3458
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003459 err = nfs4_set_lock_state(state, request);
3460 if (err != 0)
3461 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003462 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003463 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3464 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003465 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3466 if (err != -NFS4ERR_DELAY)
3467 break;
3468 nfs4_handle_exception(server, err, &exception);
3469 } while (exception.retry);
3470 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
3473static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3474{
David Howellsadfa6f92006-08-22 20:06:08 -04003475 struct nfs_client *clp = state->owner->so_client;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003476 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 int status;
3478
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003479 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003480 status = nfs4_set_lock_state(state, request);
3481 if (status != 0)
3482 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003483 request->fl_flags |= FL_ACCESS;
3484 status = do_vfs_lock(request->fl_file, request);
3485 if (status < 0)
3486 goto out;
3487 down_read(&clp->cl_sem);
3488 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3489 struct nfs_inode *nfsi = NFS_I(state->inode);
3490 /* Yes: cache locks! */
3491 down_read(&nfsi->rwsem);
3492 /* ...but avoid races with delegation recall... */
3493 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3494 request->fl_flags = fl_flags & ~FL_SLEEP;
3495 status = do_vfs_lock(request->fl_file, request);
3496 up_read(&nfsi->rwsem);
3497 goto out_unlock;
3498 }
3499 up_read(&nfsi->rwsem);
3500 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003501 status = _nfs4_do_setlk(state, cmd, request, 0);
3502 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003503 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003504 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003505 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003506 if (do_vfs_lock(request->fl_file, request) < 0)
3507 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003508out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 up_read(&clp->cl_sem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003510out:
3511 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 return status;
3513}
3514
3515static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3516{
3517 struct nfs4_exception exception = { };
3518 int err;
3519
3520 do {
3521 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3522 _nfs4_proc_setlk(state, cmd, request),
3523 &exception);
3524 } while (exception.retry);
3525 return err;
3526}
3527
3528static int
3529nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3530{
3531 struct nfs_open_context *ctx;
3532 struct nfs4_state *state;
3533 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3534 int status;
3535
3536 /* verify open state */
3537 ctx = (struct nfs_open_context *)filp->private_data;
3538 state = ctx->state;
3539
3540 if (request->fl_start < 0 || request->fl_end < 0)
3541 return -EINVAL;
3542
3543 if (IS_GETLK(cmd))
3544 return nfs4_proc_getlk(state, F_GETLK, request);
3545
3546 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3547 return -EINVAL;
3548
3549 if (request->fl_type == F_UNLCK)
3550 return nfs4_proc_unlck(state, cmd, request);
3551
3552 do {
3553 status = nfs4_proc_setlk(state, cmd, request);
3554 if ((status != -EAGAIN) || IS_SETLK(cmd))
3555 break;
3556 timeout = nfs4_set_lock_task_retry(timeout);
3557 status = -ERESTARTSYS;
3558 if (signalled())
3559 break;
3560 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 return status;
3562}
3563
Trond Myklebust888e6942005-11-04 15:38:11 -05003564int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3565{
3566 struct nfs_server *server = NFS_SERVER(state->inode);
3567 struct nfs4_exception exception = { };
3568 int err;
3569
3570 err = nfs4_set_lock_state(state, fl);
3571 if (err != 0)
3572 goto out;
3573 do {
3574 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3575 if (err != -NFS4ERR_DELAY)
3576 break;
3577 err = nfs4_handle_exception(server, err, &exception);
3578 } while (exception.retry);
3579out:
3580 return err;
3581}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003582
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003583#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3584
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003585int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3586 size_t buflen, int flags)
3587{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003588 struct inode *inode = dentry->d_inode;
3589
3590 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3591 return -EOPNOTSUPP;
3592
3593 if (!S_ISREG(inode->i_mode) &&
3594 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3595 return -EPERM;
3596
3597 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003598}
3599
3600/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3601 * and that's what we'll do for e.g. user attributes that haven't been set.
3602 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3603 * attributes in kernel-managed attribute namespaces. */
3604ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3605 size_t buflen)
3606{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003607 struct inode *inode = dentry->d_inode;
3608
3609 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3610 return -EOPNOTSUPP;
3611
3612 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003613}
3614
3615ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3616{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003617 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003618
J. Bruce Fields096455a2006-03-20 23:23:42 -05003619 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3620 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003621 if (buf && buflen < len)
3622 return -ERANGE;
3623 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003624 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3625 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003626}
3627
Trond Myklebustc228fd32007-01-13 02:28:11 -05003628int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003629 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003630{
3631 struct nfs_server *server = NFS_SERVER(dir);
3632 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003633 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3634 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003635 };
3636 struct nfs4_fs_locations_arg args = {
3637 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003638 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003639 .page = page,
3640 .bitmask = bitmask,
3641 };
3642 struct rpc_message msg = {
3643 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3644 .rpc_argp = &args,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003645 .rpc_resp = fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003646 };
3647 int status;
3648
3649 dprintk("%s: start\n", __FUNCTION__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003650 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003651 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003652 fs_locations->nlocations = 0;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003653 status = rpc_call_sync(server->client, &msg, 0);
3654 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3655 return status;
3656}
3657
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3659 .recover_open = nfs4_open_reclaim,
3660 .recover_lock = nfs4_lock_reclaim,
3661};
3662
3663struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3664 .recover_open = nfs4_open_expired,
3665 .recover_lock = nfs4_lock_expired,
3666};
3667
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003668static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003669 .permission = nfs_permission,
3670 .getattr = nfs_getattr,
3671 .setattr = nfs_setattr,
3672 .getxattr = nfs4_getxattr,
3673 .setxattr = nfs4_setxattr,
3674 .listxattr = nfs4_listxattr,
3675};
3676
David Howells509de812006-08-22 20:06:11 -04003677const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 .version = 4, /* protocol version */
3679 .dentry_ops = &nfs4_dentry_operations,
3680 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003681 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 .getroot = nfs4_proc_get_root,
3683 .getattr = nfs4_proc_getattr,
3684 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04003685 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 .lookup = nfs4_proc_lookup,
3687 .access = nfs4_proc_access,
3688 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 .create = nfs4_proc_create,
3690 .remove = nfs4_proc_remove,
3691 .unlink_setup = nfs4_proc_unlink_setup,
3692 .unlink_done = nfs4_proc_unlink_done,
3693 .rename = nfs4_proc_rename,
3694 .link = nfs4_proc_link,
3695 .symlink = nfs4_proc_symlink,
3696 .mkdir = nfs4_proc_mkdir,
3697 .rmdir = nfs4_proc_remove,
3698 .readdir = nfs4_proc_readdir,
3699 .mknod = nfs4_proc_mknod,
3700 .statfs = nfs4_proc_statfs,
3701 .fsinfo = nfs4_proc_fsinfo,
3702 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04003703 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 .decode_dirent = nfs4_decode_dirent,
3705 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003706 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003708 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003710 .commit_done = nfs4_commit_done,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003711 .file_open = nfs_open,
3712 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003714 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715};
3716
3717/*
3718 * Local variables:
3719 * c-basic-offset: 8
3720 * End:
3721 */