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