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