blob: 3b59c5ded3fbf6f99c18142a6af0856cfe0ce1f6 [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 Myklebust24ac23a2006-01-03 09:55:11 +0100388
389 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
390 goto out;
391 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500392 if (IS_ERR(inode))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100393 goto out;
394 state = nfs4_get_open_state(inode, data->owner);
395 if (state == NULL)
396 goto put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400397 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400398 int delegation_flags = 0;
399
Trond Myklebust003707c2007-07-05 18:07:55 -0400400 rcu_read_lock();
401 delegation = rcu_dereference(NFS_I(inode)->delegation);
402 if (delegation)
403 delegation_flags = delegation->flags;
404 rcu_read_unlock();
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400405 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
406 nfs_inode_set_delegation(state->inode,
407 data->owner->so_cred,
408 &data->o_res);
409 else
410 nfs_inode_reclaim_delegation(state->inode,
411 data->owner->so_cred,
412 &data->o_res);
413 }
Trond Myklebust003707c2007-07-05 18:07:55 -0400414 rcu_read_lock();
415 delegation = rcu_dereference(NFS_I(inode)->delegation);
416 if (delegation != NULL)
417 deleg_stateid = &delegation->stateid;
418 update_open_stateid(state, &data->o_res.stateid, deleg_stateid, data->o_arg.open_flags);
419 rcu_read_unlock();
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100420put_inode:
421 iput(inode);
422out:
423 return state;
424}
425
Trond Myklebust864472e2006-01-03 09:55:15 +0100426static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
427{
428 struct nfs_inode *nfsi = NFS_I(state->inode);
429 struct nfs_open_context *ctx;
430
431 spin_lock(&state->inode->i_lock);
432 list_for_each_entry(ctx, &nfsi->open_files, list) {
433 if (ctx->state != state)
434 continue;
435 get_nfs_open_context(ctx);
436 spin_unlock(&state->inode->i_lock);
437 return ctx;
438 }
439 spin_unlock(&state->inode->i_lock);
440 return ERR_PTR(-ENOENT);
441}
442
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400443static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100444{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400445 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100446 int ret;
447
448 opendata->o_arg.open_flags = openflags;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400449 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
450 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
451 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100452 ret = _nfs4_proc_open(opendata);
453 if (ret != 0)
454 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400455 newstate = nfs4_opendata_to_nfs4_state(opendata);
456 if (newstate != NULL)
457 nfs4_close_state(&opendata->path, newstate, openflags);
458 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100459 return 0;
460}
461
462static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
463{
Trond Myklebust864472e2006-01-03 09:55:15 +0100464 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100465 int ret;
466
467 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400468 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100469 smp_rmb();
470 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400471 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100472 if (ret != 0)
473 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400474 if (newstate != state)
475 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100476 }
477 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400478 ret = nfs4_open_recover_helper(opendata, 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_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400485 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &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 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100491 return 0;
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
495 * OPEN_RECLAIM:
496 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400498static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Trond Myklebust864472e2006-01-03 09:55:15 +0100500 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
501 struct nfs4_opendata *opendata;
502 int delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 int status;
504
505 if (delegation != NULL) {
506 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
507 memcpy(&state->stateid, &delegation->stateid,
508 sizeof(state->stateid));
509 set_bit(NFS_DELEGATED_STATE, &state->flags);
510 return 0;
511 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100512 delegation_type = delegation->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Trond Myklebustad389da2007-06-05 12:30:00 -0400514 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
Trond Myklebust864472e2006-01-03 09:55:15 +0100515 if (opendata == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700516 return -ENOMEM;
Trond Myklebust864472e2006-01-03 09:55:15 +0100517 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
518 opendata->o_arg.fh = NFS_FH(state->inode);
519 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
520 opendata->o_arg.u.delegation_type = delegation_type;
521 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400522 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return status;
524}
525
Trond Myklebust539cd032007-06-05 11:46:42 -0400526static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct nfs_server *server = NFS_SERVER(state->inode);
529 struct nfs4_exception exception = { };
530 int err;
531 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400532 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000533 if (err != -NFS4ERR_DELAY)
534 break;
535 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 } while (exception.retry);
537 return err;
538}
539
Trond Myklebust864472e2006-01-03 09:55:15 +0100540static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
541{
542 struct nfs_open_context *ctx;
543 int ret;
544
545 ctx = nfs4_state_find_open_context(state);
546 if (IS_ERR(ctx))
547 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400548 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100549 put_nfs_open_context(ctx);
550 return ret;
551}
552
Trond Myklebust13437e12007-07-06 15:10:43 -0400553static 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 -0700554{
555 struct nfs4_state_owner *sp = state->owner;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100556 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100557 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Trond Myklebustad389da2007-06-05 12:30:00 -0400559 opendata = nfs4_opendata_alloc(&ctx->path, sp, 0, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100560 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100561 return -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100562 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -0400563 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100564 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100565 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400566 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100567 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Trond Myklebust13437e12007-07-06 15:10:43 -0400570int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -0400573 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 int err;
575 do {
Trond Myklebust13437e12007-07-06 15:10:43 -0400576 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 switch (err) {
578 case 0:
579 return err;
580 case -NFS4ERR_STALE_CLIENTID:
581 case -NFS4ERR_STALE_STATEID:
582 case -NFS4ERR_EXPIRED:
583 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -0400584 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return err;
586 }
587 err = nfs4_handle_exception(server, err, &exception);
588 } while (exception.retry);
589 return err;
590}
591
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100592static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100594 struct nfs4_opendata *data = calldata;
595 struct rpc_message msg = {
596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
597 .rpc_argp = &data->c_arg,
598 .rpc_resp = &data->c_res,
599 .rpc_cred = data->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 };
Trond Myklebust26e976a2006-01-03 09:55:21 +0100601 data->timestamp = jiffies;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100602 rpc_call_setup(task, &msg, 0);
603}
604
605static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
606{
607 struct nfs4_opendata *data = calldata;
608
609 data->rpc_status = task->tk_status;
610 if (RPC_ASSASSINATED(task))
611 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100612 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100613 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
614 sizeof(data->o_res.stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +0100615 renew_lease(data->o_res.server, data->timestamp);
616 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100617 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400618 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100619}
620
621static void nfs4_open_confirm_release(void *calldata)
622{
623 struct nfs4_opendata *data = calldata;
624 struct nfs4_state *state = NULL;
625
626 /* If this request hasn't been cancelled, do nothing */
627 if (data->cancelled == 0)
628 goto out_free;
629 /* In case of error, no cleanup! */
630 if (data->rpc_status != 0)
631 goto out_free;
632 nfs_confirm_seqid(&data->owner->so_seqid, 0);
633 state = nfs4_opendata_to_nfs4_state(data);
634 if (state != NULL)
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400635 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100636out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400637 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100638}
639
640static const struct rpc_call_ops nfs4_open_confirm_ops = {
641 .rpc_call_prepare = nfs4_open_confirm_prepare,
642 .rpc_call_done = nfs4_open_confirm_done,
643 .rpc_release = nfs4_open_confirm_release,
644};
645
646/*
647 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
648 */
649static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
650{
651 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
652 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int status;
654
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400655 kref_get(&data->kref);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500656 /*
657 * If rpc_run_task() ends up calling ->rpc_release(), we
658 * want to ensure that it takes the 'error' code path.
659 */
660 data->rpc_status = -ENOMEM;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100661 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500662 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100663 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100664 status = nfs4_wait_for_completion_rpc_task(task);
665 if (status != 0) {
666 data->cancelled = 1;
667 smp_wmb();
668 } else
669 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500670 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return status;
672}
673
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100674static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100676 struct nfs4_opendata *data = calldata;
677 struct nfs4_state_owner *sp = data->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 struct rpc_message msg = {
679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100680 .rpc_argp = &data->o_arg,
681 .rpc_resp = &data->o_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 .rpc_cred = sp->so_cred,
683 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100684
685 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
686 return;
687 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400688 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100689 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust864472e2006-01-03 09:55:15 +0100690 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
691 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust26e976a2006-01-03 09:55:21 +0100692 data->timestamp = jiffies;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100693 rpc_call_setup(task, &msg, 0);
694}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100696static void nfs4_open_done(struct rpc_task *task, void *calldata)
697{
698 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100700 data->rpc_status = task->tk_status;
701 if (RPC_ASSASSINATED(task))
702 return;
703 if (task->tk_status == 0) {
704 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700705 case S_IFREG:
706 break;
707 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100708 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700709 break;
710 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100711 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700712 break;
713 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100714 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700715 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100716 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -0400717 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
718 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700719 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100720 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
721}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700722
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100723static void nfs4_open_release(void *calldata)
724{
725 struct nfs4_opendata *data = calldata;
726 struct nfs4_state *state = NULL;
727
728 /* If this request hasn't been cancelled, do nothing */
729 if (data->cancelled == 0)
730 goto out_free;
731 /* In case of error, no cleanup! */
732 if (data->rpc_status != 0)
733 goto out_free;
734 /* In case we need an open_confirm, no cleanup! */
735 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
736 goto out_free;
737 nfs_confirm_seqid(&data->owner->so_seqid, 0);
738 state = nfs4_opendata_to_nfs4_state(data);
739 if (state != NULL)
Trond Myklebust4a35bd42007-06-05 10:31:33 -0400740 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100741out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400742 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100743}
744
745static const struct rpc_call_ops nfs4_open_ops = {
746 .rpc_call_prepare = nfs4_open_prepare,
747 .rpc_call_done = nfs4_open_done,
748 .rpc_release = nfs4_open_release,
749};
750
751/*
752 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
753 */
754static int _nfs4_proc_open(struct nfs4_opendata *data)
755{
756 struct inode *dir = data->dir->d_inode;
757 struct nfs_server *server = NFS_SERVER(dir);
758 struct nfs_openargs *o_arg = &data->o_arg;
759 struct nfs_openres *o_res = &data->o_res;
760 struct rpc_task *task;
761 int status;
762
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400763 kref_get(&data->kref);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500764 /*
765 * If rpc_run_task() ends up calling ->rpc_release(), we
766 * want to ensure that it takes the 'error' code path.
767 */
768 data->rpc_status = -ENOMEM;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400769 data->cancelled = 0;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100770 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500771 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100772 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100773 status = nfs4_wait_for_completion_rpc_task(task);
774 if (status != 0) {
775 data->cancelled = 1;
776 smp_wmb();
777 } else
778 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500779 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100781 return status;
782
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400783 if (o_arg->open_flags & O_CREAT) {
784 update_changeattr(dir, &o_res->cinfo);
785 nfs_post_op_update_inode(dir, o_res->dir_attr);
786 } else
787 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100789 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100791 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Howells8fa5c002006-08-22 20:06:12 -0400794 return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
799{
800 struct nfs_access_entry cache;
801 int mask = 0;
802 int status;
803
804 if (openflags & FMODE_READ)
805 mask |= MAY_READ;
806 if (openflags & FMODE_WRITE)
807 mask |= MAY_WRITE;
Trond Myklebust1b45c462007-07-03 13:04:56 -0400808 if (openflags & FMODE_EXEC)
809 mask |= MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 status = nfs_access_get_cached(inode, cred, &cache);
811 if (status == 0)
812 goto out;
813
814 /* Be clever: ask server to check for all possible rights */
815 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
816 cache.cred = cred;
817 cache.jiffies = jiffies;
818 status = _nfs4_proc_access(inode, &cache);
819 if (status != 0)
820 return status;
821 nfs_access_add_cache(inode, &cache);
822out:
823 if ((cache.mask & mask) == mask)
824 return 0;
825 return -EACCES;
826}
827
Trond Myklebust10afec92007-05-14 17:16:04 -0400828static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +0100829{
David Howells7539bba2006-08-22 20:06:09 -0400830 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -0400831 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +0100832
Trond Myklebust6b309542006-09-14 14:03:14 -0400833 for (;;) {
834 ret = nfs4_wait_clnt_recover(server->client, clp);
835 if (ret != 0)
836 return ret;
837 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
838 break;
Trond Myklebust58d97142006-01-03 09:55:24 +0100839 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -0400840 }
841 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +0100842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844/*
845 * OPEN_EXPIRED:
846 * reclaim state on the server after a network partition.
847 * Assumes caller holds the appropriate lock
848 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400849static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 struct inode *inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 struct nfs4_opendata *opendata;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100854 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
Trond Myklebust864472e2006-01-03 09:55:15 +0100855 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
Trond Myklebust539cd032007-06-05 11:46:42 -0400858 ret = _nfs4_do_access(inode, ctx->cred, openflags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100859 if (ret < 0)
860 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
862 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Trond Myklebustad389da2007-06-05 12:30:00 -0400865 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100866 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100867 return -ENOMEM;
868 ret = nfs4_open_recover(opendata, state);
869 if (ret == -ESTALE) {
870 /* Invalidate the state owner so we don't ever use it again */
Trond Myklebust539cd032007-06-05 11:46:42 -0400871 nfs4_drop_state_owner(state->owner);
872 d_drop(ctx->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400874 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100875 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Trond Myklebust539cd032007-06-05 11:46:42 -0400878static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +0000879{
Trond Myklebust539cd032007-06-05 11:46:42 -0400880 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000881 struct nfs4_exception exception = { };
882 int err;
883
884 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400885 err = _nfs4_open_expired(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000886 if (err == -NFS4ERR_DELAY)
887 nfs4_handle_exception(server, err, &exception);
888 } while (exception.retry);
889 return err;
890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
893{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +0100895 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Trond Myklebust864472e2006-01-03 09:55:15 +0100897 ctx = nfs4_state_find_open_context(state);
898 if (IS_ERR(ctx))
899 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400900 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100901 put_nfs_open_context(ctx);
902 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100906 * Returns a referenced nfs4_state if there is an open delegation on the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 */
908static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
909{
910 struct nfs_delegation *delegation;
911 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -0400912 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct nfs_inode *nfsi = NFS_I(inode);
914 struct nfs4_state_owner *sp = NULL;
915 struct nfs4_state *state = NULL;
916 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
917 int err;
918
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100919 err = -ENOMEM;
920 if (!(sp = nfs4_get_state_owner(server, cred))) {
921 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
922 return err;
923 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100924 err = nfs4_recover_expired_lease(server);
925 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100926 goto out_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /* Protect against reboot recovery - NOTE ORDER! */
928 down_read(&clp->cl_sem);
929 /* Protect against delegation recall */
930 down_read(&nfsi->rwsem);
931 delegation = NFS_I(inode)->delegation;
932 err = -ENOENT;
933 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
934 goto out_err;
935 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 state = nfs4_get_open_state(inode, sp);
937 if (state == NULL)
938 goto out_err;
939
940 err = -ENOENT;
941 if ((state->state & open_flags) == open_flags) {
942 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100943 update_open_stateflags(state, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 spin_unlock(&inode->i_lock);
945 goto out_ok;
946 } else if (state->state != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100947 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 lock_kernel();
950 err = _nfs4_do_access(inode, cred, open_flags);
951 unlock_kernel();
952 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100953 goto out_put_open_state;
Trond Myklebust003707c2007-07-05 18:07:55 -0400954 update_open_stateid(state, NULL, &delegation->stateid, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 nfs4_put_state_owner(sp);
957 up_read(&nfsi->rwsem);
958 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 *res = state;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100960 return 0;
961out_put_open_state:
962 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 up_read(&nfsi->rwsem);
965 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700966 if (err != -EACCES)
967 nfs_inode_return_delegation(inode);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100968out_put_state_owner:
969 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return err;
971}
972
973static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
974{
975 struct nfs4_exception exception = { };
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500976 struct nfs4_state *res = ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 int err;
978
979 do {
980 err = _nfs4_open_delegated(inode, flags, cred, &res);
981 if (err == 0)
982 break;
983 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
984 err, &exception));
985 } while (exception.retry);
986 return res;
987}
988
989/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -0400990 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
991 * fields corresponding to attributes that were used to store the verifier.
992 * Make sure we clobber those fields in the later setattr call
993 */
994static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
995{
996 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
997 !(sattr->ia_valid & ATTR_ATIME_SET))
998 sattr->ia_valid |= ATTR_ATIME;
999
1000 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1001 !(sattr->ia_valid & ATTR_MTIME_SET))
1002 sattr->ia_valid |= ATTR_MTIME;
1003}
1004
1005/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001006 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 */
Trond Myklebustad389da2007-06-05 12:30:00 -04001008static 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 -07001009{
1010 struct nfs4_state_owner *sp;
1011 struct nfs4_state *state = NULL;
1012 struct nfs_server *server = NFS_SERVER(dir);
David Howells7539bba2006-08-22 20:06:09 -04001013 struct nfs_client *clp = server->nfs_client;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001014 struct nfs4_opendata *opendata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 status = -ENOMEM;
1019 if (!(sp = nfs4_get_state_owner(server, cred))) {
1020 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1021 goto out_err;
1022 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001023 status = nfs4_recover_expired_lease(server);
1024 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001025 goto err_put_state_owner;
Trond Myklebust58d97142006-01-03 09:55:24 +01001026 down_read(&clp->cl_sem);
1027 status = -ENOMEM;
Trond Myklebustad389da2007-06-05 12:30:00 -04001028 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001029 if (opendata == NULL)
Trond Myklebust76723de2006-09-15 08:11:51 -04001030 goto err_release_rwsem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001032 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001034 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001036 if (opendata->o_arg.open_flags & O_EXCL)
1037 nfs4_exclusive_attrset(opendata, sattr);
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 status = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001040 state = nfs4_opendata_to_nfs4_state(opendata);
1041 if (state == NULL)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001042 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001043 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 nfs4_put_state_owner(sp);
1045 up_read(&clp->cl_sem);
1046 *res = state;
1047 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001048err_opendata_put:
1049 nfs4_opendata_put(opendata);
Trond Myklebust76723de2006-09-15 08:11:51 -04001050err_release_rwsem:
1051 up_read(&clp->cl_sem);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052err_put_state_owner:
1053 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 *res = NULL;
1056 return status;
1057}
1058
1059
Trond Myklebustad389da2007-06-05 12:30:00 -04001060static 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 -07001061{
1062 struct nfs4_exception exception = { };
1063 struct nfs4_state *res;
1064 int status;
1065
1066 do {
Trond Myklebustad389da2007-06-05 12:30:00 -04001067 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (status == 0)
1069 break;
1070 /* NOTE: BAD_SEQID means the server and client disagree about the
1071 * book-keeping w.r.t. state-changing operations
1072 * (OPEN/CLOSE/LOCK/LOCKU...)
1073 * It is actually a sign of a bug on the client or on the server.
1074 *
1075 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001076 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * have unhashed the old state_owner for us, and that we can
1078 * therefore safely retry using a new one. We should still warn
1079 * the user though...
1080 */
1081 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001082 printk(KERN_WARNING "NFS: v4 server %s "
1083 " returned a bad sequence-id error!\n",
1084 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 exception.retry = 1;
1086 continue;
1087 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001088 /*
1089 * BAD_STATEID on OPEN means that the server cancelled our
1090 * state before it received the OPEN_CONFIRM.
1091 * Recover by retrying the request as per the discussion
1092 * on Page 181 of RFC3530.
1093 */
1094 if (status == -NFS4ERR_BAD_STATEID) {
1095 exception.retry = 1;
1096 continue;
1097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1099 status, &exception));
1100 } while (exception.retry);
1101 return res;
1102}
1103
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001104static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1105 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001107 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001109 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 .iap = sattr,
1111 .server = server,
1112 .bitmask = server->attr_bitmask,
1113 };
1114 struct nfs_setattrres res = {
1115 .fattr = fattr,
1116 .server = server,
1117 };
1118 struct rpc_message msg = {
1119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1120 .rpc_argp = &arg,
1121 .rpc_resp = &res,
1122 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001123 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001124 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Trond Myklebust0e574af2005-10-27 22:12:38 -04001126 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001128 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1129 /* Use that stateid */
1130 } else if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001132 nfs4_copy_stateid(&arg.stateid, state, current->files);
1133 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1135
Trond Myklebust65e43082005-08-16 11:49:44 -04001136 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001137 if (status == 0 && state != NULL)
1138 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001139 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001142static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1143 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001145 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 struct nfs4_exception exception = { };
1147 int err;
1148 do {
1149 err = nfs4_handle_exception(server,
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001150 _nfs4_do_setattr(inode, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 &exception);
1152 } while (exception.retry);
1153 return err;
1154}
1155
1156struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001157 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct inode *inode;
1159 struct nfs4_state *state;
1160 struct nfs_closeargs arg;
1161 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001162 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001163 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164};
1165
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001166static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001167{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001168 struct nfs4_closedata *calldata = data;
1169 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001170
1171 nfs4_put_open_state(calldata->state);
1172 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001173 nfs4_put_state_owner(sp);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001174 dput(calldata->path.dentry);
1175 mntput(calldata->path.mnt);
Trond Myklebust95121352005-10-18 14:20:12 -07001176 kfree(calldata);
1177}
1178
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001179static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001181 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct nfs_server *server = NFS_SERVER(calldata->inode);
1184
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001185 if (RPC_ASSASSINATED(task))
1186 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /* hmm. we are done with the inode, and in the process of freeing
1188 * the state_owner. we keep this around to process errors
1189 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001190 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 switch (task->tk_status) {
1192 case 0:
Trond Myklebust003707c2007-07-05 18:07:55 -04001193 nfs_set_open_stateid(state, &calldata->res.stateid, calldata->arg.open_flags);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001194 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 break;
1196 case -NFS4ERR_STALE_STATEID:
1197 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 break;
1199 default:
1200 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1201 rpc_restart_call(task);
1202 return;
1203 }
1204 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001205 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001208static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001210 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001211 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 struct rpc_message msg = {
1213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1214 .rpc_argp = &calldata->arg,
1215 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001216 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 };
Trond Myklebust003707c2007-07-05 18:07:55 -04001218 int clear_rd, clear_wr, clear_rdwr;
1219 int mode;
Trond Myklebust95121352005-10-18 14:20:12 -07001220
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001221 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001222 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001223
1224 mode = FMODE_READ|FMODE_WRITE;
1225 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001226 spin_lock(&state->owner->so_lock);
1227 spin_lock(&calldata->inode->i_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001228 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001229 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001230 if (state->n_rdonly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001231 mode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001232 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1233 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1234 }
1235 if (state->n_wronly == 0) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001236 mode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001237 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1238 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1239 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001240 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001241 spin_unlock(&calldata->inode->i_lock);
1242 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001243 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001244 /* Note: exit _without_ calling nfs4_close_done */
1245 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001246 return;
1247 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001248 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -07001249 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -07001251 calldata->arg.open_flags = mode;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001252 calldata->timestamp = jiffies;
Trond Myklebust95121352005-10-18 14:20:12 -07001253 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001256static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001257 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001258 .rpc_call_done = nfs4_close_done,
1259 .rpc_release = nfs4_free_closedata,
1260};
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262/*
1263 * It is possible for data to be read/written from a mem-mapped file
1264 * after the sys_close call (which hits the vfs layer as a flush).
1265 * This means that we can't safely call nfsv4 close on a file until
1266 * the inode is cleared. This in turn means that we are not good
1267 * NFSv4 citizens - we do not indicate to the server to update the file's
1268 * share state even when we are done with one of the three share
1269 * stateid's in the inode.
1270 *
1271 * NOTE: Caller must be holding the sp->so_owner semaphore!
1272 */
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001273int nfs4_do_close(struct path *path, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001275 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001277 struct nfs4_state_owner *sp = state->owner;
1278 struct rpc_task *task;
Trond Myklebust95121352005-10-18 14:20:12 -07001279 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Trond Myklebust95121352005-10-18 14:20:12 -07001281 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001283 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001284 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001286 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001287 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001289 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001290 if (calldata->arg.seqid == NULL)
1291 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001292 calldata->arg.bitmask = server->attr_bitmask;
1293 calldata->res.fattr = &calldata->fattr;
1294 calldata->res.server = server;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001295 calldata->path.mnt = mntget(path->mnt);
1296 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001297
Trond Myklebustb39e6252007-06-11 23:05:07 -04001298 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata);
1299 if (IS_ERR(task))
1300 return PTR_ERR(task);
1301 rpc_put_task(task);
1302 return 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001303out_free_calldata:
1304 kfree(calldata);
1305out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001306 nfs4_put_open_state(state);
1307 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001308 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001311static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
Trond Myklebust02a913a2005-10-18 14:20:17 -07001312{
1313 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001314 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001315
Trond Myklebust1b45c462007-07-03 13:04:56 -04001316 /* If the open_intent is for execute, we have an extra check to make */
1317 if (nd->intent.open.flags & FMODE_EXEC) {
1318 ret = _nfs4_do_access(state->inode,
1319 state->owner->so_cred,
1320 nd->intent.open.flags);
1321 if (ret < 0)
1322 goto out_close;
1323 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001324 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001325 if (!IS_ERR(filp)) {
1326 struct nfs_open_context *ctx;
1327 ctx = (struct nfs_open_context *)filp->private_data;
1328 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001329 return 0;
1330 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001331 ret = PTR_ERR(filp);
1332out_close:
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001333 nfs4_close_state(path, state, nd->intent.open.flags);
Trond Myklebust1b45c462007-07-03 13:04:56 -04001334 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001335}
1336
1337struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1339{
Trond Myklebustad389da2007-06-05 12:30:00 -04001340 struct path path = {
1341 .mnt = nd->mnt,
1342 .dentry = dentry,
1343 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct iattr attr;
1345 struct rpc_cred *cred;
1346 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001347 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 if (nd->flags & LOOKUP_CREATE) {
1350 attr.ia_mode = nd->intent.open.create_mode;
1351 attr.ia_valid = ATTR_MODE;
1352 if (!IS_POSIXACL(dir))
1353 attr.ia_mode &= ~current->fs->umask;
1354 } else {
1355 attr.ia_valid = 0;
1356 BUG_ON(nd->intent.open.flags & O_CREAT);
1357 }
1358
David Howells1f163412006-08-22 20:06:11 -04001359 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001361 return (struct dentry *)cred;
Trond Myklebustad389da2007-06-05 12:30:00 -04001362 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001364 if (IS_ERR(state)) {
1365 if (PTR_ERR(state) == -ENOENT)
1366 d_add(dentry, NULL);
1367 return (struct dentry *)state;
1368 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001369 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001370 if (res != NULL)
1371 dentry = res;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001372 nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001373 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
1376int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001377nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Trond Myklebustad389da2007-06-05 12:30:00 -04001379 struct path path = {
1380 .mnt = nd->mnt,
1381 .dentry = dentry,
1382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 struct rpc_cred *cred;
1384 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
David Howells1f163412006-08-22 20:06:11 -04001386 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (IS_ERR(cred))
1388 return PTR_ERR(cred);
1389 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1390 if (IS_ERR(state))
Trond Myklebustad389da2007-06-05 12:30:00 -04001391 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001393 if (IS_ERR(state)) {
1394 switch (PTR_ERR(state)) {
1395 case -EPERM:
1396 case -EACCES:
1397 case -EDQUOT:
1398 case -ENOSPC:
1399 case -EROFS:
1400 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1401 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001402 default:
1403 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001404 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001405 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001406 if (state->inode == dentry->d_inode) {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001407 nfs4_intent_set_file(nd, &path, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return 1;
1409 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001410 nfs4_close_state(&path, state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001411out_drop:
1412 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return 0;
1414}
1415
1416
1417static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1418{
1419 struct nfs4_server_caps_res res = {};
1420 struct rpc_message msg = {
1421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1422 .rpc_argp = fhandle,
1423 .rpc_resp = &res,
1424 };
1425 int status;
1426
1427 status = rpc_call_sync(server->client, &msg, 0);
1428 if (status == 0) {
1429 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1430 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1431 server->caps |= NFS_CAP_ACLS;
1432 if (res.has_links != 0)
1433 server->caps |= NFS_CAP_HARDLINKS;
1434 if (res.has_symlinks != 0)
1435 server->caps |= NFS_CAP_SYMLINKS;
1436 server->acl_bitmask = res.acl_bitmask;
1437 }
1438 return status;
1439}
1440
Trond Myklebust55a97592006-06-09 09:34:19 -04001441int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 struct nfs4_exception exception = { };
1444 int err;
1445 do {
1446 err = nfs4_handle_exception(server,
1447 _nfs4_server_capabilities(server, fhandle),
1448 &exception);
1449 } while (exception.retry);
1450 return err;
1451}
1452
1453static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1454 struct nfs_fsinfo *info)
1455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 struct nfs4_lookup_root_arg args = {
1457 .bitmask = nfs4_fattr_bitmap,
1458 };
1459 struct nfs4_lookup_res res = {
1460 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001461 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 .fh = fhandle,
1463 };
1464 struct rpc_message msg = {
1465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1466 .rpc_argp = &args,
1467 .rpc_resp = &res,
1468 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001469 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return rpc_call_sync(server->client, &msg, 0);
1471}
1472
1473static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1474 struct nfs_fsinfo *info)
1475{
1476 struct nfs4_exception exception = { };
1477 int err;
1478 do {
1479 err = nfs4_handle_exception(server,
1480 _nfs4_lookup_root(server, fhandle, info),
1481 &exception);
1482 } while (exception.retry);
1483 return err;
1484}
1485
David Howells54ceac42006-08-22 20:06:13 -04001486/*
1487 * get the file handle for the "/" directory on the server
1488 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001490 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 int status;
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (status == 0)
1496 status = nfs4_server_capabilities(server, fhandle);
1497 if (status == 0)
1498 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001499 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Manoj Naik6b97fd32006-06-09 09:34:29 -04001502/*
1503 * Get locations and (maybe) other attributes of a referral.
1504 * Note that we'll actually follow the referral later when
1505 * we detect fsid mismatch in inode revalidation
1506 */
1507static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1508{
1509 int status = -ENOMEM;
1510 struct page *page = NULL;
1511 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001512
1513 page = alloc_page(GFP_KERNEL);
1514 if (page == NULL)
1515 goto out;
1516 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1517 if (locations == NULL)
1518 goto out;
1519
Trond Myklebustc228fd32007-01-13 02:28:11 -05001520 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001521 if (status != 0)
1522 goto out;
1523 /* Make sure server returned a different fsid for the referral */
1524 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1525 dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
1526 status = -EIO;
1527 goto out;
1528 }
1529
1530 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1531 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1532 if (!fattr->mode)
1533 fattr->mode = S_IFDIR;
1534 memset(fhandle, 0, sizeof(struct nfs_fh));
1535out:
1536 if (page)
1537 __free_page(page);
1538 if (locations)
1539 kfree(locations);
1540 return status;
1541}
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1544{
1545 struct nfs4_getattr_arg args = {
1546 .fh = fhandle,
1547 .bitmask = server->attr_bitmask,
1548 };
1549 struct nfs4_getattr_res res = {
1550 .fattr = fattr,
1551 .server = server,
1552 };
1553 struct rpc_message msg = {
1554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1555 .rpc_argp = &args,
1556 .rpc_resp = &res,
1557 };
1558
Trond Myklebust0e574af2005-10-27 22:12:38 -04001559 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return rpc_call_sync(server->client, &msg, 0);
1561}
1562
1563static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1564{
1565 struct nfs4_exception exception = { };
1566 int err;
1567 do {
1568 err = nfs4_handle_exception(server,
1569 _nfs4_proc_getattr(server, fhandle, fattr),
1570 &exception);
1571 } while (exception.retry);
1572 return err;
1573}
1574
1575/*
1576 * The file is not closed if it is opened due to the a request to change
1577 * the size of the file. The open call will not be needed once the
1578 * VFS layer lookup-intents are implemented.
1579 *
1580 * Close is called when the inode is destroyed.
1581 * If we haven't opened the file for O_WRONLY, we
1582 * need to in the size_change case to obtain a stateid.
1583 *
1584 * Got race?
1585 * Because OPEN is always done by name in nfsv4, it is
1586 * possible that we opened a different file by the same
1587 * name. We can recognize this race condition, but we
1588 * can't do anything about it besides returning an error.
1589 *
1590 * This will be fixed with VFS changes (lookup-intent).
1591 */
1592static int
1593nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1594 struct iattr *sattr)
1595{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001596 struct rpc_cred *cred;
1597 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001598 struct nfs_open_context *ctx;
1599 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 int status;
1601
Trond Myklebust0e574af2005-10-27 22:12:38 -04001602 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
David Howells1f163412006-08-22 20:06:11 -04001604 cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001605 if (IS_ERR(cred))
1606 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001607
1608 /* Search for an existing open(O_WRITE) file */
1609 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1610 if (ctx != NULL)
1611 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001612
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001613 status = nfs4_do_setattr(inode, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001614 if (status == 0)
1615 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001616 if (ctx != NULL)
1617 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001618 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return status;
1620}
1621
David Howells2b3de442006-08-22 20:06:09 -04001622static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1623 struct qstr *name, struct nfs_fh *fhandle,
1624 struct nfs_fattr *fattr)
1625{
1626 int status;
1627 struct nfs4_lookup_arg args = {
1628 .bitmask = server->attr_bitmask,
1629 .dir_fh = dirfh,
1630 .name = name,
1631 };
1632 struct nfs4_lookup_res res = {
1633 .server = server,
1634 .fattr = fattr,
1635 .fh = fhandle,
1636 };
1637 struct rpc_message msg = {
1638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1639 .rpc_argp = &args,
1640 .rpc_resp = &res,
1641 };
1642
1643 nfs_fattr_init(fattr);
1644
1645 dprintk("NFS call lookupfh %s\n", name->name);
1646 status = rpc_call_sync(server->client, &msg, 0);
1647 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04001648 return status;
1649}
1650
1651static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1652 struct qstr *name, struct nfs_fh *fhandle,
1653 struct nfs_fattr *fattr)
1654{
1655 struct nfs4_exception exception = { };
1656 int err;
1657 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04001658 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1659 /* FIXME: !!!! */
1660 if (err == -NFS4ERR_MOVED) {
1661 err = -EREMOTE;
1662 break;
1663 }
1664 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04001665 } while (exception.retry);
1666 return err;
1667}
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1670 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1671{
Trond Myklebust4e56e082007-07-01 18:13:52 -04001672 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04001675 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001676 if (status == -NFS4ERR_MOVED)
1677 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 dprintk("NFS reply lookup: %d\n", status);
1679 return status;
1680}
1681
1682static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1683{
1684 struct nfs4_exception exception = { };
1685 int err;
1686 do {
1687 err = nfs4_handle_exception(NFS_SERVER(dir),
1688 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1689 &exception);
1690 } while (exception.retry);
1691 return err;
1692}
1693
1694static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1695{
1696 struct nfs4_accessargs args = {
1697 .fh = NFS_FH(inode),
1698 };
1699 struct nfs4_accessres res = { 0 };
1700 struct rpc_message msg = {
1701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1702 .rpc_argp = &args,
1703 .rpc_resp = &res,
1704 .rpc_cred = entry->cred,
1705 };
1706 int mode = entry->mask;
1707 int status;
1708
1709 /*
1710 * Determine which access bits we want to ask for...
1711 */
1712 if (mode & MAY_READ)
1713 args.access |= NFS4_ACCESS_READ;
1714 if (S_ISDIR(inode->i_mode)) {
1715 if (mode & MAY_WRITE)
1716 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1717 if (mode & MAY_EXEC)
1718 args.access |= NFS4_ACCESS_LOOKUP;
1719 } else {
1720 if (mode & MAY_WRITE)
1721 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1722 if (mode & MAY_EXEC)
1723 args.access |= NFS4_ACCESS_EXECUTE;
1724 }
1725 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1726 if (!status) {
1727 entry->mask = 0;
1728 if (res.access & NFS4_ACCESS_READ)
1729 entry->mask |= MAY_READ;
1730 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1731 entry->mask |= MAY_WRITE;
1732 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1733 entry->mask |= MAY_EXEC;
1734 }
1735 return status;
1736}
1737
1738static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1739{
1740 struct nfs4_exception exception = { };
1741 int err;
1742 do {
1743 err = nfs4_handle_exception(NFS_SERVER(inode),
1744 _nfs4_proc_access(inode, entry),
1745 &exception);
1746 } while (exception.retry);
1747 return err;
1748}
1749
1750/*
1751 * TODO: For the time being, we don't try to get any attributes
1752 * along with any of the zero-copy operations READ, READDIR,
1753 * READLINK, WRITE.
1754 *
1755 * In the case of the first three, we want to put the GETATTR
1756 * after the read-type operation -- this is because it is hard
1757 * to predict the length of a GETATTR response in v4, and thus
1758 * align the READ data correctly. This means that the GETATTR
1759 * may end up partially falling into the page cache, and we should
1760 * shift it into the 'tail' of the xdr_buf before processing.
1761 * To do this efficiently, we need to know the total length
1762 * of data received, which doesn't seem to be available outside
1763 * of the RPC layer.
1764 *
1765 * In the case of WRITE, we also want to put the GETATTR after
1766 * the operation -- in this case because we want to make sure
1767 * we get the post-operation mtime and size. This means that
1768 * we can't use xdr_encode_pages() as written: we need a variant
1769 * of it which would leave room in the 'tail' iovec.
1770 *
1771 * Both of these changes to the XDR layer would in fact be quite
1772 * minor, but I decided to leave them for a subsequent patch.
1773 */
1774static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1775 unsigned int pgbase, unsigned int pglen)
1776{
1777 struct nfs4_readlink args = {
1778 .fh = NFS_FH(inode),
1779 .pgbase = pgbase,
1780 .pglen = pglen,
1781 .pages = &page,
1782 };
1783 struct rpc_message msg = {
1784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1785 .rpc_argp = &args,
1786 .rpc_resp = NULL,
1787 };
1788
1789 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1790}
1791
1792static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1793 unsigned int pgbase, unsigned int pglen)
1794{
1795 struct nfs4_exception exception = { };
1796 int err;
1797 do {
1798 err = nfs4_handle_exception(NFS_SERVER(inode),
1799 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1800 &exception);
1801 } while (exception.retry);
1802 return err;
1803}
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805/*
1806 * Got race?
1807 * We will need to arrange for the VFS layer to provide an atomic open.
1808 * Until then, this create/open method is prone to inefficiency and race
1809 * conditions due to the lookup, create, and open VFS calls from sys_open()
1810 * placed on the wire.
1811 *
1812 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1813 * The file will be opened again in the subsequent VFS open call
1814 * (nfs4_proc_file_open).
1815 *
1816 * The open for read will just hang around to be used by any process that
1817 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1818 */
1819
1820static int
1821nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001822 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Trond Myklebustad389da2007-06-05 12:30:00 -04001824 struct path path = {
1825 .mnt = nd->mnt,
1826 .dentry = dentry,
1827 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 struct nfs4_state *state;
1829 struct rpc_cred *cred;
1830 int status = 0;
1831
David Howells1f163412006-08-22 20:06:11 -04001832 cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (IS_ERR(cred)) {
1834 status = PTR_ERR(cred);
1835 goto out;
1836 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001837 state = nfs4_do_open(dir, &path, flags, sattr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 put_rpccred(cred);
1839 if (IS_ERR(state)) {
1840 status = PTR_ERR(state);
1841 goto out;
1842 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001843 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (flags & O_EXCL) {
1845 struct nfs_fattr fattr;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001846 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001847 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001848 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001849 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001850 }
Trond Myklebustad389da2007-06-05 12:30:00 -04001851 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001852 status = nfs4_intent_set_file(nd, &path, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001853 else
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001854 nfs4_close_state(&path, state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855out:
1856 return status;
1857}
1858
1859static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1860{
Trond Myklebust16e42952005-10-27 22:12:44 -04001861 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 struct nfs4_remove_arg args = {
1863 .fh = NFS_FH(dir),
1864 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001865 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001867 struct nfs_fattr dir_attr;
1868 struct nfs4_remove_res res = {
1869 .server = server,
1870 .dir_attr = &dir_attr,
1871 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 struct rpc_message msg = {
1873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1874 .rpc_argp = &args,
1875 .rpc_resp = &res,
1876 };
1877 int status;
1878
Trond Myklebust16e42952005-10-27 22:12:44 -04001879 nfs_fattr_init(res.dir_attr);
1880 status = rpc_call_sync(server->client, &msg, 0);
1881 if (status == 0) {
1882 update_changeattr(dir, &res.cinfo);
1883 nfs_post_op_update_inode(dir, res.dir_attr);
1884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return status;
1886}
1887
1888static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1889{
1890 struct nfs4_exception exception = { };
1891 int err;
1892 do {
1893 err = nfs4_handle_exception(NFS_SERVER(dir),
1894 _nfs4_proc_remove(dir, name),
1895 &exception);
1896 } while (exception.retry);
1897 return err;
1898}
1899
1900struct unlink_desc {
1901 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001902 struct nfs4_remove_res res;
1903 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904};
1905
1906static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1907 struct qstr *name)
1908{
Trond Myklebust16e42952005-10-27 22:12:44 -04001909 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 struct unlink_desc *up;
1911
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001912 up = kmalloc(sizeof(*up), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (!up)
1914 return -ENOMEM;
1915
1916 up->args.fh = NFS_FH(dir->d_inode);
1917 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001918 up->args.bitmask = server->attr_bitmask;
1919 up->res.server = server;
1920 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1923 msg->rpc_argp = &up->args;
1924 msg->rpc_resp = &up->res;
1925 return 0;
1926}
1927
1928static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1929{
1930 struct rpc_message *msg = &task->tk_msg;
1931 struct unlink_desc *up;
1932
1933 if (msg->rpc_resp != NULL) {
1934 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001935 update_changeattr(dir->d_inode, &up->res.cinfo);
1936 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 kfree(up);
1938 msg->rpc_resp = NULL;
1939 msg->rpc_argp = NULL;
1940 }
1941 return 0;
1942}
1943
1944static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1945 struct inode *new_dir, struct qstr *new_name)
1946{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001947 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct nfs4_rename_arg arg = {
1949 .old_dir = NFS_FH(old_dir),
1950 .new_dir = NFS_FH(new_dir),
1951 .old_name = old_name,
1952 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001953 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001955 struct nfs_fattr old_fattr, new_fattr;
1956 struct nfs4_rename_res res = {
1957 .server = server,
1958 .old_fattr = &old_fattr,
1959 .new_fattr = &new_fattr,
1960 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 struct rpc_message msg = {
1962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1963 .rpc_argp = &arg,
1964 .rpc_resp = &res,
1965 };
1966 int status;
1967
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001968 nfs_fattr_init(res.old_fattr);
1969 nfs_fattr_init(res.new_fattr);
1970 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 if (!status) {
1973 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001974 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001976 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978 return status;
1979}
1980
1981static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1982 struct inode *new_dir, struct qstr *new_name)
1983{
1984 struct nfs4_exception exception = { };
1985 int err;
1986 do {
1987 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1988 _nfs4_proc_rename(old_dir, old_name,
1989 new_dir, new_name),
1990 &exception);
1991 } while (exception.retry);
1992 return err;
1993}
1994
1995static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1996{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001997 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 struct nfs4_link_arg arg = {
1999 .fh = NFS_FH(inode),
2000 .dir_fh = NFS_FH(dir),
2001 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002002 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002004 struct nfs_fattr fattr, dir_attr;
2005 struct nfs4_link_res res = {
2006 .server = server,
2007 .fattr = &fattr,
2008 .dir_attr = &dir_attr,
2009 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 struct rpc_message msg = {
2011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2012 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002013 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 };
2015 int status;
2016
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002017 nfs_fattr_init(res.fattr);
2018 nfs_fattr_init(res.dir_attr);
2019 status = rpc_call_sync(server->client, &msg, 0);
2020 if (!status) {
2021 update_changeattr(dir, &res.cinfo);
2022 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002023 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 return status;
2027}
2028
2029static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2030{
2031 struct nfs4_exception exception = { };
2032 int err;
2033 do {
2034 err = nfs4_handle_exception(NFS_SERVER(inode),
2035 _nfs4_proc_link(inode, dir, name),
2036 &exception);
2037 } while (exception.retry);
2038 return err;
2039}
2040
Chuck Lever4f390c12006-08-22 20:06:22 -04002041static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002042 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever4f390c12006-08-22 20:06:22 -04002045 struct nfs_fh fhandle;
2046 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 struct nfs4_create_arg arg = {
2048 .dir_fh = NFS_FH(dir),
2049 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002050 .name = &dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 .attrs = sattr,
2052 .ftype = NF4LNK,
2053 .bitmask = server->attr_bitmask,
2054 };
2055 struct nfs4_create_res res = {
2056 .server = server,
Chuck Lever4f390c12006-08-22 20:06:22 -04002057 .fh = &fhandle,
2058 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002059 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 };
2061 struct rpc_message msg = {
2062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2063 .rpc_argp = &arg,
2064 .rpc_resp = &res,
2065 };
2066 int status;
2067
Chuck Lever94a6d752006-08-22 20:06:23 -04002068 if (len > NFS4_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -04002070
Chuck Lever94a6d752006-08-22 20:06:23 -04002071 arg.u.symlink.pages = &page;
2072 arg.u.symlink.len = len;
Chuck Lever4f390c12006-08-22 20:06:22 -04002073 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002074 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Chuck Lever4f390c12006-08-22 20:06:22 -04002077 if (!status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 update_changeattr(dir, &res.dir_cinfo);
Chuck Lever4f390c12006-08-22 20:06:22 -04002079 nfs_post_op_update_inode(dir, res.dir_fattr);
2080 status = nfs_instantiate(dentry, &fhandle, &fattr);
2081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return status;
2083}
2084
Chuck Lever4f390c12006-08-22 20:06:22 -04002085static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002086 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 struct nfs4_exception exception = { };
2089 int err;
2090 do {
2091 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002092 _nfs4_proc_symlink(dir, dentry, page,
2093 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 &exception);
2095 } while (exception.retry);
2096 return err;
2097}
2098
2099static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2100 struct iattr *sattr)
2101{
2102 struct nfs_server *server = NFS_SERVER(dir);
2103 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002104 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 struct nfs4_create_arg arg = {
2106 .dir_fh = NFS_FH(dir),
2107 .server = server,
2108 .name = &dentry->d_name,
2109 .attrs = sattr,
2110 .ftype = NF4DIR,
2111 .bitmask = server->attr_bitmask,
2112 };
2113 struct nfs4_create_res res = {
2114 .server = server,
2115 .fh = &fhandle,
2116 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002117 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 };
2119 struct rpc_message msg = {
2120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2121 .rpc_argp = &arg,
2122 .rpc_resp = &res,
2123 };
2124 int status;
2125
Trond Myklebust0e574af2005-10-27 22:12:38 -04002126 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002127 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2130 if (!status) {
2131 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002132 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 status = nfs_instantiate(dentry, &fhandle, &fattr);
2134 }
2135 return status;
2136}
2137
2138static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2139 struct iattr *sattr)
2140{
2141 struct nfs4_exception exception = { };
2142 int err;
2143 do {
2144 err = nfs4_handle_exception(NFS_SERVER(dir),
2145 _nfs4_proc_mkdir(dir, dentry, sattr),
2146 &exception);
2147 } while (exception.retry);
2148 return err;
2149}
2150
2151static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2152 u64 cookie, struct page *page, unsigned int count, int plus)
2153{
2154 struct inode *dir = dentry->d_inode;
2155 struct nfs4_readdir_arg args = {
2156 .fh = NFS_FH(dir),
2157 .pages = &page,
2158 .pgbase = 0,
2159 .count = count,
2160 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2161 };
2162 struct nfs4_readdir_res res;
2163 struct rpc_message msg = {
2164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2165 .rpc_argp = &args,
2166 .rpc_resp = &res,
2167 .rpc_cred = cred,
2168 };
2169 int status;
2170
Trond Myklebusteadf4592005-06-22 17:16:39 +00002171 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2172 dentry->d_parent->d_name.name,
2173 dentry->d_name.name,
2174 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2176 res.pgbase = args.pgbase;
2177 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2178 if (status == 0)
2179 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebusteadf4592005-06-22 17:16:39 +00002180 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return status;
2182}
2183
2184static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2185 u64 cookie, struct page *page, unsigned int count, int plus)
2186{
2187 struct nfs4_exception exception = { };
2188 int err;
2189 do {
2190 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2191 _nfs4_proc_readdir(dentry, cred, cookie,
2192 page, count, plus),
2193 &exception);
2194 } while (exception.retry);
2195 return err;
2196}
2197
2198static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2199 struct iattr *sattr, dev_t rdev)
2200{
2201 struct nfs_server *server = NFS_SERVER(dir);
2202 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002203 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 struct nfs4_create_arg arg = {
2205 .dir_fh = NFS_FH(dir),
2206 .server = server,
2207 .name = &dentry->d_name,
2208 .attrs = sattr,
2209 .bitmask = server->attr_bitmask,
2210 };
2211 struct nfs4_create_res res = {
2212 .server = server,
2213 .fh = &fh,
2214 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002215 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 };
2217 struct rpc_message msg = {
2218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2219 .rpc_argp = &arg,
2220 .rpc_resp = &res,
2221 };
2222 int status;
2223 int mode = sattr->ia_mode;
2224
Trond Myklebust0e574af2005-10-27 22:12:38 -04002225 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002226 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2229 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2230 if (S_ISFIFO(mode))
2231 arg.ftype = NF4FIFO;
2232 else if (S_ISBLK(mode)) {
2233 arg.ftype = NF4BLK;
2234 arg.u.device.specdata1 = MAJOR(rdev);
2235 arg.u.device.specdata2 = MINOR(rdev);
2236 }
2237 else if (S_ISCHR(mode)) {
2238 arg.ftype = NF4CHR;
2239 arg.u.device.specdata1 = MAJOR(rdev);
2240 arg.u.device.specdata2 = MINOR(rdev);
2241 }
2242 else
2243 arg.ftype = NF4SOCK;
2244
2245 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2246 if (status == 0) {
2247 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002248 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 status = nfs_instantiate(dentry, &fh, &fattr);
2250 }
2251 return status;
2252}
2253
2254static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2255 struct iattr *sattr, dev_t rdev)
2256{
2257 struct nfs4_exception exception = { };
2258 int err;
2259 do {
2260 err = nfs4_handle_exception(NFS_SERVER(dir),
2261 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2262 &exception);
2263 } while (exception.retry);
2264 return err;
2265}
2266
2267static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2268 struct nfs_fsstat *fsstat)
2269{
2270 struct nfs4_statfs_arg args = {
2271 .fh = fhandle,
2272 .bitmask = server->attr_bitmask,
2273 };
2274 struct rpc_message msg = {
2275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2276 .rpc_argp = &args,
2277 .rpc_resp = fsstat,
2278 };
2279
Trond Myklebust0e574af2005-10-27 22:12:38 -04002280 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 return rpc_call_sync(server->client, &msg, 0);
2282}
2283
2284static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2285{
2286 struct nfs4_exception exception = { };
2287 int err;
2288 do {
2289 err = nfs4_handle_exception(server,
2290 _nfs4_proc_statfs(server, fhandle, fsstat),
2291 &exception);
2292 } while (exception.retry);
2293 return err;
2294}
2295
2296static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2297 struct nfs_fsinfo *fsinfo)
2298{
2299 struct nfs4_fsinfo_arg args = {
2300 .fh = fhandle,
2301 .bitmask = server->attr_bitmask,
2302 };
2303 struct rpc_message msg = {
2304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2305 .rpc_argp = &args,
2306 .rpc_resp = fsinfo,
2307 };
2308
2309 return rpc_call_sync(server->client, &msg, 0);
2310}
2311
2312static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2313{
2314 struct nfs4_exception exception = { };
2315 int err;
2316
2317 do {
2318 err = nfs4_handle_exception(server,
2319 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2320 &exception);
2321 } while (exception.retry);
2322 return err;
2323}
2324
2325static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2326{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002327 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2329}
2330
2331static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2332 struct nfs_pathconf *pathconf)
2333{
2334 struct nfs4_pathconf_arg args = {
2335 .fh = fhandle,
2336 .bitmask = server->attr_bitmask,
2337 };
2338 struct rpc_message msg = {
2339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2340 .rpc_argp = &args,
2341 .rpc_resp = pathconf,
2342 };
2343
2344 /* None of the pathconf attributes are mandatory to implement */
2345 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2346 memset(pathconf, 0, sizeof(*pathconf));
2347 return 0;
2348 }
2349
Trond Myklebust0e574af2005-10-27 22:12:38 -04002350 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return rpc_call_sync(server->client, &msg, 0);
2352}
2353
2354static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2355 struct nfs_pathconf *pathconf)
2356{
2357 struct nfs4_exception exception = { };
2358 int err;
2359
2360 do {
2361 err = nfs4_handle_exception(server,
2362 _nfs4_proc_pathconf(server, fhandle, pathconf),
2363 &exception);
2364 } while (exception.retry);
2365 return err;
2366}
2367
Trond Myklebustec06c092006-03-20 13:44:27 -05002368static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Trond Myklebustec06c092006-03-20 13:44:27 -05002370 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Trond Myklebustec06c092006-03-20 13:44:27 -05002372 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002374 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002377 renew_lease(server, data->timestamp);
2378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
Trond Myklebustec06c092006-03-20 13:44:27 -05002381static void nfs4_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 struct rpc_message msg = {
2384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2385 .rpc_argp = &data->args,
2386 .rpc_resp = &data->res,
2387 .rpc_cred = data->cred,
2388 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 data->timestamp = jiffies;
2391
Trond Myklebustec06c092006-03-20 13:44:27 -05002392 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
Trond Myklebust788e7a82006-03-20 13:44:27 -05002395static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 struct inode *inode = data->inode;
2398
2399 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2400 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002401 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002403 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002405 nfs_post_op_update_inode(inode, data->res.fattr);
2406 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
Trond Myklebust788e7a82006-03-20 13:44:27 -05002410static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 struct rpc_message msg = {
2413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2414 .rpc_argp = &data->args,
2415 .rpc_resp = &data->res,
2416 .rpc_cred = data->cred,
2417 };
2418 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002419 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 int stable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 if (how & FLUSH_STABLE) {
2423 if (!NFS_I(inode)->ncommit)
2424 stable = NFS_FILE_SYNC;
2425 else
2426 stable = NFS_DATA_SYNC;
2427 } else
2428 stable = NFS_UNSTABLE;
2429 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002430 data->args.bitmask = server->attr_bitmask;
2431 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 data->timestamp = jiffies;
2434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -05002436 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
2438
Trond Myklebust788e7a82006-03-20 13:44:27 -05002439static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 struct inode *inode = data->inode;
2442
2443 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2444 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002445 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002447 if (task->tk_status >= 0)
2448 nfs_post_op_update_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
Trond Myklebust788e7a82006-03-20 13:44:27 -05002452static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 struct rpc_message msg = {
2455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2456 .rpc_argp = &data->args,
2457 .rpc_resp = &data->res,
2458 .rpc_cred = data->cred,
2459 };
Trond Myklebust788e7a82006-03-20 13:44:27 -05002460 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002462 data->args.bitmask = server->attr_bitmask;
2463 data->res.server = server;
2464
Trond Myklebust788e7a82006-03-20 13:44:27 -05002465 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
2468/*
2469 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2470 * standalone procedure for queueing an asynchronous RENEW.
2471 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002472static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
David Howellsadfa6f92006-08-22 20:06:08 -04002474 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002475 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 if (task->tk_status < 0) {
2478 switch (task->tk_status) {
2479 case -NFS4ERR_STALE_CLIENTID:
2480 case -NFS4ERR_EXPIRED:
2481 case -NFS4ERR_CB_PATH_DOWN:
2482 nfs4_schedule_state_recovery(clp);
2483 }
2484 return;
2485 }
2486 spin_lock(&clp->cl_lock);
2487 if (time_before(clp->cl_last_renewal,timestamp))
2488 clp->cl_last_renewal = timestamp;
2489 spin_unlock(&clp->cl_lock);
2490}
2491
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002492static const struct rpc_call_ops nfs4_renew_ops = {
2493 .rpc_call_done = nfs4_renew_done,
2494};
2495
David Howellsadfa6f92006-08-22 20:06:08 -04002496int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 struct rpc_message msg = {
2499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2500 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002501 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 };
2503
2504 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002505 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
David Howellsadfa6f92006-08-22 20:06:08 -04002508int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
2510 struct rpc_message msg = {
2511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2512 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002513 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 };
2515 unsigned long now = jiffies;
2516 int status;
2517
2518 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2519 if (status < 0)
2520 return status;
2521 spin_lock(&clp->cl_lock);
2522 if (time_before(clp->cl_last_renewal,now))
2523 clp->cl_last_renewal = now;
2524 spin_unlock(&clp->cl_lock);
2525 return 0;
2526}
2527
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002528static inline int nfs4_server_supports_acls(struct nfs_server *server)
2529{
2530 return (server->caps & NFS_CAP_ACLS)
2531 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2532 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2533}
2534
2535/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2536 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2537 * the stack.
2538 */
2539#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2540
2541static void buf_to_pages(const void *buf, size_t buflen,
2542 struct page **pages, unsigned int *pgbase)
2543{
2544 const void *p = buf;
2545
2546 *pgbase = offset_in_page(buf);
2547 p -= *pgbase;
2548 while (p < buf + buflen) {
2549 *(pages++) = virt_to_page(p);
2550 p += PAGE_CACHE_SIZE;
2551 }
2552}
2553
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002554struct nfs4_cached_acl {
2555 int cached;
2556 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002557 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002558};
2559
2560static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002561{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002562 struct nfs_inode *nfsi = NFS_I(inode);
2563
2564 spin_lock(&inode->i_lock);
2565 kfree(nfsi->nfs4_acl);
2566 nfsi->nfs4_acl = acl;
2567 spin_unlock(&inode->i_lock);
2568}
2569
2570static void nfs4_zap_acl_attr(struct inode *inode)
2571{
2572 nfs4_set_cached_acl(inode, NULL);
2573}
2574
2575static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2576{
2577 struct nfs_inode *nfsi = NFS_I(inode);
2578 struct nfs4_cached_acl *acl;
2579 int ret = -ENOENT;
2580
2581 spin_lock(&inode->i_lock);
2582 acl = nfsi->nfs4_acl;
2583 if (acl == NULL)
2584 goto out;
2585 if (buf == NULL) /* user is just asking for length */
2586 goto out_len;
2587 if (acl->cached == 0)
2588 goto out;
2589 ret = -ERANGE; /* see getxattr(2) man page */
2590 if (acl->len > buflen)
2591 goto out;
2592 memcpy(buf, acl->data, acl->len);
2593out_len:
2594 ret = acl->len;
2595out:
2596 spin_unlock(&inode->i_lock);
2597 return ret;
2598}
2599
2600static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2601{
2602 struct nfs4_cached_acl *acl;
2603
2604 if (buf && acl_len <= PAGE_SIZE) {
2605 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2606 if (acl == NULL)
2607 goto out;
2608 acl->cached = 1;
2609 memcpy(acl->data, buf, acl_len);
2610 } else {
2611 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2612 if (acl == NULL)
2613 goto out;
2614 acl->cached = 0;
2615 }
2616 acl->len = acl_len;
2617out:
2618 nfs4_set_cached_acl(inode, acl);
2619}
2620
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002621static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002622{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002623 struct page *pages[NFS4ACL_MAXPAGES];
2624 struct nfs_getaclargs args = {
2625 .fh = NFS_FH(inode),
2626 .acl_pages = pages,
2627 .acl_len = buflen,
2628 };
2629 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002630 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002631 struct rpc_message msg = {
2632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2633 .rpc_argp = &args,
2634 .rpc_resp = &resp_len,
2635 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002636 struct page *localpage = NULL;
2637 int ret;
2638
2639 if (buflen < PAGE_SIZE) {
2640 /* As long as we're doing a round trip to the server anyway,
2641 * let's be prepared for a page of acl data. */
2642 localpage = alloc_page(GFP_KERNEL);
2643 resp_buf = page_address(localpage);
2644 if (localpage == NULL)
2645 return -ENOMEM;
2646 args.acl_pages[0] = localpage;
2647 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002648 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002649 } else {
2650 resp_buf = buf;
2651 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2652 }
2653 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2654 if (ret)
2655 goto out_free;
2656 if (resp_len > args.acl_len)
2657 nfs4_write_cached_acl(inode, NULL, resp_len);
2658 else
2659 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2660 if (buf) {
2661 ret = -ERANGE;
2662 if (resp_len > buflen)
2663 goto out_free;
2664 if (localpage)
2665 memcpy(buf, resp_buf, resp_len);
2666 }
2667 ret = resp_len;
2668out_free:
2669 if (localpage)
2670 __free_page(localpage);
2671 return ret;
2672}
2673
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002674static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2675{
2676 struct nfs4_exception exception = { };
2677 ssize_t ret;
2678 do {
2679 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2680 if (ret >= 0)
2681 break;
2682 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2683 } while (exception.retry);
2684 return ret;
2685}
2686
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002687static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2688{
2689 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002690 int ret;
2691
2692 if (!nfs4_server_supports_acls(server))
2693 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002694 ret = nfs_revalidate_inode(server, inode);
2695 if (ret < 0)
2696 return ret;
2697 ret = nfs4_read_cached_acl(inode, buf, buflen);
2698 if (ret != -ENOENT)
2699 return ret;
2700 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002701}
2702
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002703static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002704{
2705 struct nfs_server *server = NFS_SERVER(inode);
2706 struct page *pages[NFS4ACL_MAXPAGES];
2707 struct nfs_setaclargs arg = {
2708 .fh = NFS_FH(inode),
2709 .acl_pages = pages,
2710 .acl_len = buflen,
2711 };
2712 struct rpc_message msg = {
2713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2714 .rpc_argp = &arg,
2715 .rpc_resp = NULL,
2716 };
2717 int ret;
2718
2719 if (!nfs4_server_supports_acls(server))
2720 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002721 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002722 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
David Howells1f163412006-08-22 20:06:11 -04002723 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
J. Bruce Fields08efa202007-05-01 10:56:25 -04002724 nfs_zap_caches(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002725 return ret;
2726}
2727
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002728static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2729{
2730 struct nfs4_exception exception = { };
2731 int err;
2732 do {
2733 err = nfs4_handle_exception(NFS_SERVER(inode),
2734 __nfs4_proc_set_acl(inode, buf, buflen),
2735 &exception);
2736 } while (exception.retry);
2737 return err;
2738}
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002741nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
David Howells7539bba2006-08-22 20:06:09 -04002743 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 if (!clp || task->tk_status >= 0)
2746 return 0;
2747 switch(task->tk_status) {
2748 case -NFS4ERR_STALE_CLIENTID:
2749 case -NFS4ERR_STALE_STATEID:
2750 case -NFS4ERR_EXPIRED:
2751 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2752 nfs4_schedule_state_recovery(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002753 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 rpc_wake_up_task(task);
2755 task->tk_status = 0;
2756 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 case -NFS4ERR_DELAY:
Chuck Lever006ea732006-03-20 13:44:14 -05002758 nfs_inc_server_stats((struct nfs_server *) server,
2759 NFSIOS_DELAY);
2760 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2762 task->tk_status = 0;
2763 return -EAGAIN;
2764 case -NFS4ERR_OLD_STATEID:
2765 task->tk_status = 0;
2766 return -EAGAIN;
2767 }
2768 task->tk_status = nfs4_map_errors(task->tk_status);
2769 return 0;
2770}
2771
Trond Myklebust433fbe42006-01-03 09:55:22 +01002772static int nfs4_wait_bit_interruptible(void *word)
2773{
2774 if (signal_pending(current))
2775 return -ERESTARTSYS;
2776 schedule();
2777 return 0;
2778}
2779
David Howellsadfa6f92006-08-22 20:06:08 -04002780static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 sigset_t oldset;
Trond Myklebust433fbe42006-01-03 09:55:22 +01002783 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 might_sleep();
2786
Trond Myklebuste1485822006-12-13 16:43:13 -05002787 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002790 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2791 nfs4_wait_bit_interruptible,
2792 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 rpc_clnt_sigunmask(clnt, &oldset);
Trond Myklebuste1485822006-12-13 16:43:13 -05002794
2795 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return res;
2797}
2798
2799static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2800{
2801 sigset_t oldset;
2802 int res = 0;
2803
2804 might_sleep();
2805
2806 if (*timeout <= 0)
2807 *timeout = NFS4_POLL_RETRY_MIN;
2808 if (*timeout > NFS4_POLL_RETRY_MAX)
2809 *timeout = NFS4_POLL_RETRY_MAX;
2810 rpc_clnt_sigmask(clnt, &oldset);
2811 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002812 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (signalled())
2814 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002815 } else
2816 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 rpc_clnt_sigunmask(clnt, &oldset);
2818 *timeout <<= 1;
2819 return res;
2820}
2821
2822/* This is the error handling routine for processes that are allowed
2823 * to sleep.
2824 */
Trond Myklebust10afec92007-05-14 17:16:04 -04002825static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
David Howells7539bba2006-08-22 20:06:09 -04002827 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 int ret = errorcode;
2829
2830 exception->retry = 0;
2831 switch(errorcode) {
2832 case 0:
2833 return 0;
2834 case -NFS4ERR_STALE_CLIENTID:
2835 case -NFS4ERR_STALE_STATEID:
2836 case -NFS4ERR_EXPIRED:
Trond Myklebust433fbe42006-01-03 09:55:22 +01002837 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 ret = nfs4_wait_clnt_recover(server->client, clp);
2839 if (ret == 0)
2840 exception->retry = 1;
2841 break;
Trond Myklebustc5149832006-09-15 08:25:04 -04002842 case -NFS4ERR_FILE_OPEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 case -NFS4ERR_GRACE:
2844 case -NFS4ERR_DELAY:
2845 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002846 if (ret != 0)
2847 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002849 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
2851 /* We failed to handle the error */
2852 return nfs4_map_errors(ret);
2853}
2854
David Howellsadfa6f92006-08-22 20:06:08 -04002855int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
2857 nfs4_verifier sc_verifier;
2858 struct nfs4_setclientid setclientid = {
2859 .sc_verifier = &sc_verifier,
2860 .sc_prog = program,
2861 };
2862 struct rpc_message msg = {
2863 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2864 .rpc_argp = &setclientid,
2865 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002866 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 };
Al Virobc4785c2006-10-19 23:28:51 -07002868 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 int loop = 0;
2870 int status;
2871
Al Virobc4785c2006-10-19 23:28:51 -07002872 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2874 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2875
2876 for(;;) {
2877 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2878 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
David Howells24c8dbb2006-08-22 20:06:10 -04002879 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
Trond Myklebust286d7d62006-01-03 09:55:26 +01002880 cred->cr_ops->cr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 clp->cl_id_uniquifier);
2882 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2883 sizeof(setclientid.sc_netid), "tcp");
2884 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2885 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2886 clp->cl_ipaddr, port >> 8, port & 255);
2887
2888 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2889 if (status != -NFS4ERR_CLID_INUSE)
2890 break;
2891 if (signalled())
2892 break;
2893 if (loop++ & 1)
2894 ssleep(clp->cl_lease_time + 1);
2895 else
2896 if (++clp->cl_id_uniquifier == 0)
2897 break;
2898 }
2899 return status;
2900}
2901
David Howellsadfa6f92006-08-22 20:06:08 -04002902static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
2904 struct nfs_fsinfo fsinfo;
2905 struct rpc_message msg = {
2906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2907 .rpc_argp = clp,
2908 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002909 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 };
2911 unsigned long now;
2912 int status;
2913
2914 now = jiffies;
2915 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2916 if (status == 0) {
2917 spin_lock(&clp->cl_lock);
2918 clp->cl_lease_time = fsinfo.lease_time * HZ;
2919 clp->cl_last_renewal = now;
Trond Myklebust58d97142006-01-03 09:55:24 +01002920 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 spin_unlock(&clp->cl_lock);
2922 }
2923 return status;
2924}
2925
David Howellsadfa6f92006-08-22 20:06:08 -04002926int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05002927{
2928 long timeout;
2929 int err;
2930 do {
2931 err = _nfs4_proc_setclientid_confirm(clp, cred);
2932 switch (err) {
2933 case 0:
2934 return err;
2935 case -NFS4ERR_RESOURCE:
2936 /* The IBM lawyers misread another document! */
2937 case -NFS4ERR_DELAY:
2938 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2939 }
2940 } while (err == 0);
2941 return err;
2942}
2943
Trond Myklebustfe650402006-01-03 09:55:18 +01002944struct nfs4_delegreturndata {
2945 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002946 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01002947 struct nfs_fh fh;
2948 nfs4_stateid stateid;
2949 struct rpc_cred *cred;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002950 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002951 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01002952 int rpc_status;
2953};
2954
2955static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
Trond Myklebustfe650402006-01-03 09:55:18 +01002957 struct nfs4_delegreturndata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 struct rpc_message msg = {
2959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
Trond Myklebustfe650402006-01-03 09:55:18 +01002960 .rpc_argp = &data->args,
Trond Myklebustfa178f22006-01-03 09:55:38 +01002961 .rpc_resp = &data->res,
Trond Myklebustfe650402006-01-03 09:55:18 +01002962 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 };
Trond Myklebustfa178f22006-01-03 09:55:38 +01002964 nfs_fattr_init(data->res.fattr);
Trond Myklebustfe650402006-01-03 09:55:18 +01002965 rpc_call_setup(task, &msg, 0);
2966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Trond Myklebustfe650402006-01-03 09:55:18 +01002968static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2969{
2970 struct nfs4_delegreturndata *data = calldata;
2971 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002972 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01002973 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01002974}
2975
2976static void nfs4_delegreturn_release(void *calldata)
2977{
2978 struct nfs4_delegreturndata *data = calldata;
2979
2980 put_rpccred(data->cred);
2981 kfree(calldata);
2982}
2983
Jesper Juhlc8d149f2006-03-20 13:44:07 -05002984static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01002985 .rpc_call_prepare = nfs4_delegreturn_prepare,
2986 .rpc_call_done = nfs4_delegreturn_done,
2987 .rpc_release = nfs4_delegreturn_release,
2988};
2989
2990static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2991{
2992 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002993 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01002994 struct rpc_task *task;
2995 int status;
2996
2997 data = kmalloc(sizeof(*data), GFP_KERNEL);
2998 if (data == NULL)
2999 return -ENOMEM;
3000 data->args.fhandle = &data->fh;
3001 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003002 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003003 nfs_copy_fh(&data->fh, NFS_FH(inode));
3004 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003005 data->res.fattr = &data->fattr;
3006 data->res.server = server;
Trond Myklebustfe650402006-01-03 09:55:18 +01003007 data->cred = get_rpccred(cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003008 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003009 data->rpc_status = 0;
3010
3011 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003012 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003013 return PTR_ERR(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003014 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebustfa178f22006-01-03 09:55:38 +01003015 if (status == 0) {
Trond Myklebustfe650402006-01-03 09:55:18 +01003016 status = data->rpc_status;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003017 if (status == 0)
3018 nfs_post_op_update_inode(inode, &data->fattr);
3019 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003020 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003021 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
3024int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
3025{
3026 struct nfs_server *server = NFS_SERVER(inode);
3027 struct nfs4_exception exception = { };
3028 int err;
3029 do {
3030 err = _nfs4_proc_delegreturn(inode, cred, stateid);
3031 switch (err) {
3032 case -NFS4ERR_STALE_STATEID:
3033 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 case 0:
3035 return 0;
3036 }
3037 err = nfs4_handle_exception(server, err, &exception);
3038 } while (exception.retry);
3039 return err;
3040}
3041
3042#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3043#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3044
3045/*
3046 * sleep, with exponential backoff, and retry the LOCK operation.
3047 */
3048static unsigned long
3049nfs4_set_lock_task_retry(unsigned long timeout)
3050{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003051 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 timeout <<= 1;
3053 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3054 return NFS4_LOCK_MAXTIMEOUT;
3055 return timeout;
3056}
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3059{
3060 struct inode *inode = state->inode;
3061 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003062 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003063 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003065 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003067 struct nfs_lockt_res res = {
3068 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 };
3070 struct rpc_message msg = {
3071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3072 .rpc_argp = &arg,
3073 .rpc_resp = &res,
3074 .rpc_cred = state->owner->so_cred,
3075 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 struct nfs4_lock_state *lsp;
3077 int status;
3078
3079 down_read(&clp->cl_sem);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003080 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003081 status = nfs4_set_lock_state(state, request);
3082 if (status != 0)
3083 goto out;
3084 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003085 arg.lock_owner.id = lsp->ls_id.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003087 switch (status) {
3088 case 0:
3089 request->fl_type = F_UNLCK;
3090 break;
3091 case -NFS4ERR_DENIED:
3092 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003094 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003095out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 up_read(&clp->cl_sem);
3097 return status;
3098}
3099
3100static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3101{
3102 struct nfs4_exception exception = { };
3103 int err;
3104
3105 do {
3106 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3107 _nfs4_proc_getlk(state, cmd, request),
3108 &exception);
3109 } while (exception.retry);
3110 return err;
3111}
3112
3113static int do_vfs_lock(struct file *file, struct file_lock *fl)
3114{
3115 int res = 0;
3116 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3117 case FL_POSIX:
3118 res = posix_lock_file_wait(file, fl);
3119 break;
3120 case FL_FLOCK:
3121 res = flock_lock_file_wait(file, fl);
3122 break;
3123 default:
3124 BUG();
3125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 return res;
3127}
3128
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003129struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003130 struct nfs_locku_args arg;
3131 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003132 struct nfs4_lock_state *lsp;
3133 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003134 struct file_lock fl;
3135 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003136 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003137};
3138
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003139static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3140 struct nfs_open_context *ctx,
3141 struct nfs4_lock_state *lsp,
3142 struct nfs_seqid *seqid)
3143{
3144 struct nfs4_unlockdata *p;
3145 struct inode *inode = lsp->ls_state->inode;
3146
3147 p = kmalloc(sizeof(*p), GFP_KERNEL);
3148 if (p == NULL)
3149 return NULL;
3150 p->arg.fh = NFS_FH(inode);
3151 p->arg.fl = &p->fl;
3152 p->arg.seqid = seqid;
3153 p->arg.stateid = &lsp->ls_stateid;
3154 p->lsp = lsp;
3155 atomic_inc(&lsp->ls_count);
3156 /* Ensure we don't close file until we're done freeing locks! */
3157 p->ctx = get_nfs_open_context(ctx);
3158 memcpy(&p->fl, fl, sizeof(p->fl));
3159 p->server = NFS_SERVER(inode);
3160 return p;
3161}
3162
Trond Myklebust06f814a2006-01-03 09:55:07 +01003163static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003164{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003165 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003166 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003167 nfs4_put_lock_state(calldata->lsp);
3168 put_nfs_open_context(calldata->ctx);
3169 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003170}
3171
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003172static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003173{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003174 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003175
Trond Myklebust06f814a2006-01-03 09:55:07 +01003176 if (RPC_ASSASSINATED(task))
3177 return;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003178 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003179 switch (task->tk_status) {
3180 case 0:
3181 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003182 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003183 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003184 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003185 break;
3186 case -NFS4ERR_STALE_STATEID:
3187 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003188 break;
3189 default:
Trond Myklebusta6a352e2006-12-13 16:43:06 -05003190 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003191 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003192 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003193}
3194
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003195static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003196{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003197 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 struct rpc_message msg = {
3199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003200 .rpc_argp = &calldata->arg,
3201 .rpc_resp = &calldata->res,
3202 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003205 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003206 return;
3207 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003208 /* Note: exit _without_ running nfs4_locku_done */
3209 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003210 return;
3211 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003212 calldata->timestamp = jiffies;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003213 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214}
3215
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003216static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003217 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003218 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003219 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003220};
3221
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003222static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3223 struct nfs_open_context *ctx,
3224 struct nfs4_lock_state *lsp,
3225 struct nfs_seqid *seqid)
3226{
3227 struct nfs4_unlockdata *data;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003228
3229 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3230 if (data == NULL) {
3231 nfs_free_seqid(seqid);
3232 return ERR_PTR(-ENOMEM);
3233 }
3234
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003235 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003236}
3237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3239{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003240 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003241 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003242 struct rpc_task *task;
3243 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Trond Myklebust9b073572006-06-29 16:38:34 -04003245 status = nfs4_set_lock_state(state, request);
3246 /* Unlock _before_ we do the RPC call */
3247 request->fl_flags |= FL_EXISTS;
3248 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3249 goto out;
3250 if (status != 0)
3251 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003252 /* Is this a delegated lock? */
3253 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003254 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003255 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003256 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003257 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003258 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003259 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003260 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3261 status = PTR_ERR(task);
3262 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003263 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003264 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003265 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003266out:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003267 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268}
3269
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003270struct nfs4_lockdata {
3271 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003272 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003273 struct nfs4_lock_state *lsp;
3274 struct nfs_open_context *ctx;
3275 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003276 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003277 int rpc_status;
3278 int cancelled;
3279};
3280
3281static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3282 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3283{
3284 struct nfs4_lockdata *p;
3285 struct inode *inode = lsp->ls_state->inode;
3286 struct nfs_server *server = NFS_SERVER(inode);
3287
3288 p = kzalloc(sizeof(*p), GFP_KERNEL);
3289 if (p == NULL)
3290 return NULL;
3291
3292 p->arg.fh = NFS_FH(inode);
3293 p->arg.fl = &p->fl;
3294 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3295 if (p->arg.lock_seqid == NULL)
3296 goto out_free;
3297 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003298 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003299 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003300 p->lsp = lsp;
3301 atomic_inc(&lsp->ls_count);
3302 p->ctx = get_nfs_open_context(ctx);
3303 memcpy(&p->fl, fl, sizeof(p->fl));
3304 return p;
3305out_free:
3306 kfree(p);
3307 return NULL;
3308}
3309
3310static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3311{
3312 struct nfs4_lockdata *data = calldata;
3313 struct nfs4_state *state = data->lsp->ls_state;
3314 struct nfs4_state_owner *sp = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 struct rpc_message msg = {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3317 .rpc_argp = &data->arg,
3318 .rpc_resp = &data->res,
3319 .rpc_cred = sp->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003322 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3323 return;
3324 dprintk("%s: begin!\n", __FUNCTION__);
3325 /* Do we need to do an open_to_lock_owner? */
3326 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3327 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3328 if (data->arg.open_seqid == NULL) {
3329 data->rpc_status = -ENOMEM;
3330 task->tk_action = NULL;
Trond Myklebust06735b32005-10-18 14:20:15 -07003331 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07003332 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003333 data->arg.open_stateid = &state->stateid;
3334 data->arg.new_lock_owner = 1;
3335 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003336 data->timestamp = jiffies;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003337 rpc_call_setup(task, &msg, 0);
Trond Myklebust06735b32005-10-18 14:20:15 -07003338out:
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003339 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3340}
3341
3342static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3343{
3344 struct nfs4_lockdata *data = calldata;
3345
3346 dprintk("%s: begin!\n", __FUNCTION__);
3347
3348 data->rpc_status = task->tk_status;
3349 if (RPC_ASSASSINATED(task))
3350 goto out;
3351 if (data->arg.new_lock_owner != 0) {
3352 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3353 if (data->rpc_status == 0)
3354 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3355 else
3356 goto out;
3357 }
3358 if (data->rpc_status == 0) {
3359 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3360 sizeof(data->lsp->ls_stateid.data));
3361 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003362 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003363 }
3364 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3365out:
3366 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3367}
3368
3369static void nfs4_lock_release(void *calldata)
3370{
3371 struct nfs4_lockdata *data = calldata;
3372
3373 dprintk("%s: begin!\n", __FUNCTION__);
3374 if (data->arg.open_seqid != NULL)
3375 nfs_free_seqid(data->arg.open_seqid);
3376 if (data->cancelled != 0) {
3377 struct rpc_task *task;
3378 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3379 data->arg.lock_seqid);
3380 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003381 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003382 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3383 } else
3384 nfs_free_seqid(data->arg.lock_seqid);
3385 nfs4_put_lock_state(data->lsp);
3386 put_nfs_open_context(data->ctx);
3387 kfree(data);
3388 dprintk("%s: done!\n", __FUNCTION__);
3389}
3390
3391static const struct rpc_call_ops nfs4_lock_ops = {
3392 .rpc_call_prepare = nfs4_lock_prepare,
3393 .rpc_call_done = nfs4_lock_done,
3394 .rpc_release = nfs4_lock_release,
3395};
3396
3397static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3398{
3399 struct nfs4_lockdata *data;
3400 struct rpc_task *task;
3401 int ret;
3402
3403 dprintk("%s: begin!\n", __FUNCTION__);
3404 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3405 fl->fl_u.nfs4_fl.owner);
3406 if (data == NULL)
3407 return -ENOMEM;
3408 if (IS_SETLKW(cmd))
3409 data->arg.block = 1;
3410 if (reclaim != 0)
3411 data->arg.reclaim = 1;
3412 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3413 &nfs4_lock_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003414 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003415 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003416 ret = nfs4_wait_for_completion_rpc_task(task);
3417 if (ret == 0) {
3418 ret = data->rpc_status;
3419 if (ret == -NFS4ERR_DENIED)
3420 ret = -EAGAIN;
3421 } else
3422 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003423 rpc_put_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003424 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3425 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
3428static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3429{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003430 struct nfs_server *server = NFS_SERVER(state->inode);
3431 struct nfs4_exception exception = { };
3432 int err;
3433
3434 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003435 /* Cache the lock if possible... */
3436 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3437 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003438 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3439 if (err != -NFS4ERR_DELAY)
3440 break;
3441 nfs4_handle_exception(server, err, &exception);
3442 } while (exception.retry);
3443 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444}
3445
3446static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3447{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003448 struct nfs_server *server = NFS_SERVER(state->inode);
3449 struct nfs4_exception exception = { };
3450 int err;
3451
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003452 err = nfs4_set_lock_state(state, request);
3453 if (err != 0)
3454 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003455 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003456 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3457 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003458 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3459 if (err != -NFS4ERR_DELAY)
3460 break;
3461 nfs4_handle_exception(server, err, &exception);
3462 } while (exception.retry);
3463 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464}
3465
3466static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3467{
David Howellsadfa6f92006-08-22 20:06:08 -04003468 struct nfs_client *clp = state->owner->so_client;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003469 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 int status;
3471
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003472 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003473 status = nfs4_set_lock_state(state, request);
3474 if (status != 0)
3475 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003476 request->fl_flags |= FL_ACCESS;
3477 status = do_vfs_lock(request->fl_file, request);
3478 if (status < 0)
3479 goto out;
3480 down_read(&clp->cl_sem);
3481 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3482 struct nfs_inode *nfsi = NFS_I(state->inode);
3483 /* Yes: cache locks! */
3484 down_read(&nfsi->rwsem);
3485 /* ...but avoid races with delegation recall... */
3486 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3487 request->fl_flags = fl_flags & ~FL_SLEEP;
3488 status = do_vfs_lock(request->fl_file, request);
3489 up_read(&nfsi->rwsem);
3490 goto out_unlock;
3491 }
3492 up_read(&nfsi->rwsem);
3493 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003494 status = _nfs4_do_setlk(state, cmd, request, 0);
3495 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003496 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003497 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003498 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003499 if (do_vfs_lock(request->fl_file, request) < 0)
3500 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003501out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 up_read(&clp->cl_sem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003503out:
3504 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 return status;
3506}
3507
3508static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3509{
3510 struct nfs4_exception exception = { };
3511 int err;
3512
3513 do {
3514 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3515 _nfs4_proc_setlk(state, cmd, request),
3516 &exception);
3517 } while (exception.retry);
3518 return err;
3519}
3520
3521static int
3522nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3523{
3524 struct nfs_open_context *ctx;
3525 struct nfs4_state *state;
3526 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3527 int status;
3528
3529 /* verify open state */
3530 ctx = (struct nfs_open_context *)filp->private_data;
3531 state = ctx->state;
3532
3533 if (request->fl_start < 0 || request->fl_end < 0)
3534 return -EINVAL;
3535
3536 if (IS_GETLK(cmd))
3537 return nfs4_proc_getlk(state, F_GETLK, request);
3538
3539 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3540 return -EINVAL;
3541
3542 if (request->fl_type == F_UNLCK)
3543 return nfs4_proc_unlck(state, cmd, request);
3544
3545 do {
3546 status = nfs4_proc_setlk(state, cmd, request);
3547 if ((status != -EAGAIN) || IS_SETLK(cmd))
3548 break;
3549 timeout = nfs4_set_lock_task_retry(timeout);
3550 status = -ERESTARTSYS;
3551 if (signalled())
3552 break;
3553 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 return status;
3555}
3556
Trond Myklebust888e6942005-11-04 15:38:11 -05003557int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3558{
3559 struct nfs_server *server = NFS_SERVER(state->inode);
3560 struct nfs4_exception exception = { };
3561 int err;
3562
3563 err = nfs4_set_lock_state(state, fl);
3564 if (err != 0)
3565 goto out;
3566 do {
3567 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3568 if (err != -NFS4ERR_DELAY)
3569 break;
3570 err = nfs4_handle_exception(server, err, &exception);
3571 } while (exception.retry);
3572out:
3573 return err;
3574}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003575
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003576#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3577
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003578int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3579 size_t buflen, int flags)
3580{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003581 struct inode *inode = dentry->d_inode;
3582
3583 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3584 return -EOPNOTSUPP;
3585
3586 if (!S_ISREG(inode->i_mode) &&
3587 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3588 return -EPERM;
3589
3590 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003591}
3592
3593/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3594 * and that's what we'll do for e.g. user attributes that haven't been set.
3595 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3596 * attributes in kernel-managed attribute namespaces. */
3597ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3598 size_t buflen)
3599{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003600 struct inode *inode = dentry->d_inode;
3601
3602 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3603 return -EOPNOTSUPP;
3604
3605 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003606}
3607
3608ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3609{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003610 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003611
J. Bruce Fields096455a2006-03-20 23:23:42 -05003612 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3613 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003614 if (buf && buflen < len)
3615 return -ERANGE;
3616 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003617 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3618 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003619}
3620
Trond Myklebustc228fd32007-01-13 02:28:11 -05003621int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003622 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003623{
3624 struct nfs_server *server = NFS_SERVER(dir);
3625 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003626 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3627 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003628 };
3629 struct nfs4_fs_locations_arg args = {
3630 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003631 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003632 .page = page,
3633 .bitmask = bitmask,
3634 };
3635 struct rpc_message msg = {
3636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3637 .rpc_argp = &args,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003638 .rpc_resp = fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003639 };
3640 int status;
3641
3642 dprintk("%s: start\n", __FUNCTION__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003643 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003644 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003645 fs_locations->nlocations = 0;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003646 status = rpc_call_sync(server->client, &msg, 0);
3647 dprintk("%s: returned status = %d\n", __FUNCTION__, status);
3648 return status;
3649}
3650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3652 .recover_open = nfs4_open_reclaim,
3653 .recover_lock = nfs4_lock_reclaim,
3654};
3655
3656struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3657 .recover_open = nfs4_open_expired,
3658 .recover_lock = nfs4_lock_expired,
3659};
3660
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003661static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003662 .permission = nfs_permission,
3663 .getattr = nfs_getattr,
3664 .setattr = nfs_setattr,
3665 .getxattr = nfs4_getxattr,
3666 .setxattr = nfs4_setxattr,
3667 .listxattr = nfs4_listxattr,
3668};
3669
David Howells509de812006-08-22 20:06:11 -04003670const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 .version = 4, /* protocol version */
3672 .dentry_ops = &nfs4_dentry_operations,
3673 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003674 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 .getroot = nfs4_proc_get_root,
3676 .getattr = nfs4_proc_getattr,
3677 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04003678 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 .lookup = nfs4_proc_lookup,
3680 .access = nfs4_proc_access,
3681 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 .create = nfs4_proc_create,
3683 .remove = nfs4_proc_remove,
3684 .unlink_setup = nfs4_proc_unlink_setup,
3685 .unlink_done = nfs4_proc_unlink_done,
3686 .rename = nfs4_proc_rename,
3687 .link = nfs4_proc_link,
3688 .symlink = nfs4_proc_symlink,
3689 .mkdir = nfs4_proc_mkdir,
3690 .rmdir = nfs4_proc_remove,
3691 .readdir = nfs4_proc_readdir,
3692 .mknod = nfs4_proc_mknod,
3693 .statfs = nfs4_proc_statfs,
3694 .fsinfo = nfs4_proc_fsinfo,
3695 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04003696 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 .decode_dirent = nfs4_decode_dirent,
3698 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003699 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003701 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003703 .commit_done = nfs4_commit_done,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003704 .file_open = nfs_open,
3705 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003707 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708};
3709
3710/*
3711 * Local variables:
3712 * c-basic-offset: 8
3713 * End:
3714 */