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