]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/nfs/nfs4proc.c
NFSv4.1: If slot allocation fails due to OOM, retry more quickly
[mirror_ubuntu-artful-kernel.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>
1da177e4
LT
39#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
652f89f6
TM
42#include <linux/ratelimit.h>
43#include <linux/printk.h>
5a0e3ad6 44#include <linux/slab.h>
1da177e4
LT
45#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
9b7160c5 50#include <linux/nfs_mount.h>
1da177e4 51#include <linux/namei.h>
02a913a7 52#include <linux/mount.h>
99fe60d0 53#include <linux/module.h>
6926afd1 54#include <linux/nfs_idmap.h>
5a0ffe54 55#include <linux/sunrpc/bc_xprt.h>
64c2ce8b 56#include <linux/xattr.h>
c7a360b0 57#include <linux/utsname.h>
d310310c 58#include <linux/freezer.h>
1da177e4 59
4ce79717 60#include "nfs4_fs.h"
1da177e4 61#include "delegation.h"
101070ca 62#include "internal.h"
006ea73e 63#include "iostat.h"
fc931582 64#include "callback.h"
b1f69b75 65#include "pnfs.h"
f092075d 66#include "netns.h"
1da177e4
LT
67
68#define NFSDBG_FACILITY NFSDBG_PROC
69
2066fe89 70#define NFS4_POLL_RETRY_MIN (HZ/10)
1da177e4
LT
71#define NFS4_POLL_RETRY_MAX (15*HZ)
72
a78cb57a
TM
73#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
cdd4e68b 75struct nfs4_opendata;
864472e9 76static int _nfs4_proc_open(struct nfs4_opendata *data);
b257957e 77static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
1da177e4 78static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
9e33bed5 79static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
81934ddb 80static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
bae36241 81static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
9936781d 82static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
0ab64e0e
TM
83static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
f062eb6c 86#ifdef CONFIG_NFS_V4_1
1cab0652
BS
87static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
f062eb6c 89#endif
1da177e4 90/* Prevent leaks of NFSv4 errors into userland */
46f72f57 91static int nfs4_map_errors(int err)
1da177e4 92{
52567b03
TM
93 if (err >= -1000)
94 return err;
95 switch (err) {
96 case -NFS4ERR_RESOURCE:
97 return -EREMOTEIO;
7ebb9315
BS
98 case -NFS4ERR_WRONGSEC:
99 return -EPERM;
3ddeb7c5
TM
100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
102 return -EINVAL;
fb13bfa7
TM
103 case -NFS4ERR_SHARE_DENIED:
104 return -EACCES;
f25efd85
SD
105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
6168f62c
WAA
107 case -NFS4ERR_ACCESS:
108 return -EACCES;
52567b03 109 default:
1da177e4 110 dprintk("%s could not handle NFSv4 error %d\n",
3110ff80 111 __func__, -err);
52567b03 112 break;
1da177e4 113 }
52567b03 114 return -EIO;
1da177e4
LT
115}
116
117/*
118 * This is our standard bitmap for GETATTR requests.
119 */
1549210f 120const u32 nfs4_fattr_bitmap[3] = {
1da177e4
LT
121 FATTR4_WORD0_TYPE
122 | FATTR4_WORD0_CHANGE
123 | FATTR4_WORD0_SIZE
124 | FATTR4_WORD0_FSID
125 | FATTR4_WORD0_FILEID,
126 FATTR4_WORD1_MODE
127 | FATTR4_WORD1_NUMLINKS
128 | FATTR4_WORD1_OWNER
129 | FATTR4_WORD1_OWNER_GROUP
130 | FATTR4_WORD1_RAWDEV
131 | FATTR4_WORD1_SPACE_USED
132 | FATTR4_WORD1_TIME_ACCESS
133 | FATTR4_WORD1_TIME_METADATA
134 | FATTR4_WORD1_TIME_MODIFY
135};
136
1549210f
TM
137static const u32 nfs4_pnfs_open_bitmap[3] = {
138 FATTR4_WORD0_TYPE
139 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_SIZE
141 | FATTR4_WORD0_FSID
142 | FATTR4_WORD0_FILEID,
143 FATTR4_WORD1_MODE
144 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY,
152 FATTR4_WORD2_MDSTHRESHOLD
153};
154
e23008ec
AA
155static const u32 nfs4_open_noattr_bitmap[3] = {
156 FATTR4_WORD0_TYPE
157 | FATTR4_WORD0_CHANGE
158 | FATTR4_WORD0_FILEID,
159};
160
1da177e4
LT
161const u32 nfs4_statfs_bitmap[2] = {
162 FATTR4_WORD0_FILES_AVAIL
163 | FATTR4_WORD0_FILES_FREE
164 | FATTR4_WORD0_FILES_TOTAL,
165 FATTR4_WORD1_SPACE_AVAIL
166 | FATTR4_WORD1_SPACE_FREE
167 | FATTR4_WORD1_SPACE_TOTAL
168};
169
4ce79717 170const u32 nfs4_pathconf_bitmap[2] = {
1da177e4
LT
171 FATTR4_WORD0_MAXLINK
172 | FATTR4_WORD0_MAXNAME,
173 0
174};
175
dae100c2 176const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
1da177e4
LT
177 | FATTR4_WORD0_MAXREAD
178 | FATTR4_WORD0_MAXWRITE
179 | FATTR4_WORD0_LEASE_TIME,
55b6e774 180 FATTR4_WORD1_TIME_DELTA
dae100c2
FI
181 | FATTR4_WORD1_FS_LAYOUT_TYPES,
182 FATTR4_WORD2_LAYOUT_BLKSIZE
1da177e4
LT
183};
184
830b8e33
MN
185const u32 nfs4_fs_locations_bitmap[2] = {
186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID
191 | FATTR4_WORD0_FS_LOCATIONS,
192 FATTR4_WORD1_MODE
193 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER
195 | FATTR4_WORD1_OWNER_GROUP
196 | FATTR4_WORD1_RAWDEV
197 | FATTR4_WORD1_SPACE_USED
198 | FATTR4_WORD1_TIME_ACCESS
199 | FATTR4_WORD1_TIME_METADATA
200 | FATTR4_WORD1_TIME_MODIFY
201 | FATTR4_WORD1_MOUNTED_ON_FILEID
202};
203
bc4785cd 204static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
1da177e4
LT
205 struct nfs4_readdir_arg *readdir)
206{
0dbb4c67 207 __be32 *start, *p;
1da177e4 208
1da177e4 209 if (cookie > 2) {
b7ef1956 210 readdir->cookie = cookie;
1da177e4
LT
211 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
212 return;
213 }
214
215 readdir->cookie = 0;
216 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
217 if (cookie == 2)
218 return;
219
220 /*
221 * NFSv4 servers do not return entries for '.' and '..'
222 * Therefore, we fake these entries here. We let '.'
223 * have cookie 0 and '..' have cookie 1. Note that
224 * when talking to the server, we always send cookie 0
225 * instead of 1 or 2.
226 */
2b86ce2d 227 start = p = kmap_atomic(*readdir->pages);
1da177e4
LT
228
229 if (cookie == 0) {
230 *p++ = xdr_one; /* next */
231 *p++ = xdr_zero; /* cookie, first word */
232 *p++ = xdr_one; /* cookie, second word */
233 *p++ = xdr_one; /* entry len */
234 memcpy(p, ".\0\0\0", 4); /* entry */
235 p++;
236 *p++ = xdr_one; /* bitmap length */
237 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
238 *p++ = htonl(8); /* attribute buffer length */
4e769b93 239 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
1da177e4
LT
240 }
241
242 *p++ = xdr_one; /* next */
243 *p++ = xdr_zero; /* cookie, first word */
244 *p++ = xdr_two; /* cookie, second word */
245 *p++ = xdr_two; /* entry len */
246 memcpy(p, "..\0\0", 4); /* entry */
247 p++;
248 *p++ = xdr_one; /* bitmap length */
249 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
250 *p++ = htonl(8); /* attribute buffer length */
4e769b93 251 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
1da177e4
LT
252
253 readdir->pgbase = (char *)p - (char *)start;
254 readdir->count -= readdir->pgbase;
2b86ce2d 255 kunmap_atomic(start);
1da177e4
LT
256}
257
65de872e
TM
258static int nfs4_wait_clnt_recover(struct nfs_client *clp)
259{
260 int res;
261
262 might_sleep();
263
e005e804 264 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
72cb77f4 265 nfs_wait_bit_killable, TASK_KILLABLE);
46a87b8a
CL
266 if (res)
267 return res;
268
269 if (clp->cl_cons_state < 0)
270 return clp->cl_cons_state;
271 return 0;
65de872e
TM
272}
273
274static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
275{
276 int res = 0;
277
278 might_sleep();
279
280 if (*timeout <= 0)
281 *timeout = NFS4_POLL_RETRY_MIN;
282 if (*timeout > NFS4_POLL_RETRY_MAX)
283 *timeout = NFS4_POLL_RETRY_MAX;
d310310c 284 freezable_schedule_timeout_killable(*timeout);
65de872e
TM
285 if (fatal_signal_pending(current))
286 res = -ERESTARTSYS;
287 *timeout <<= 1;
288 return res;
289}
290
291/* This is the error handling routine for processes that are allowed
292 * to sleep.
293 */
b064eca2 294static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
65de872e
TM
295{
296 struct nfs_client *clp = server->nfs_client;
9e33bed5 297 struct nfs4_state *state = exception->state;
3114ea7a 298 struct inode *inode = exception->inode;
65de872e
TM
299 int ret = errorcode;
300
301 exception->retry = 0;
302 switch(errorcode) {
303 case 0:
304 return 0;
3114ea7a 305 case -NFS4ERR_OPENMODE:
011e2a7f 306 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
57ec14c5 307 nfs4_inode_return_delegation(inode);
3114ea7a
TM
308 exception->retry = 1;
309 return 0;
310 }
311 if (state == NULL)
312 break;
313 nfs4_schedule_stateid_recovery(server, state);
314 goto wait_on_recovery;
a1d0b5ee 315 case -NFS4ERR_DELEG_REVOKED:
9e33bed5
TM
316 case -NFS4ERR_ADMIN_REVOKED:
317 case -NFS4ERR_BAD_STATEID:
9e33bed5
TM
318 if (state == NULL)
319 break;
14977489 320 nfs_remove_bad_delegation(state->inode);
0400a6b0
TM
321 nfs4_schedule_stateid_recovery(server, state);
322 goto wait_on_recovery;
0ced63d1
TM
323 case -NFS4ERR_EXPIRED:
324 if (state != NULL)
325 nfs4_schedule_stateid_recovery(server, state);
65de872e 326 case -NFS4ERR_STALE_STATEID:
a2c0b9e2 327 case -NFS4ERR_STALE_CLIENTID:
0400a6b0
TM
328 nfs4_schedule_lease_recovery(clp);
329 goto wait_on_recovery;
03391693 330#if defined(CONFIG_NFS_V4_1)
4745e315
AA
331 case -NFS4ERR_BADSESSION:
332 case -NFS4ERR_BADSLOT:
333 case -NFS4ERR_BAD_HIGH_SLOT:
334 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
335 case -NFS4ERR_DEADSESSION:
336 case -NFS4ERR_SEQ_FALSE_RETRY:
337 case -NFS4ERR_SEQ_MISORDERED:
338 dprintk("%s ERROR: %d Reset session\n", __func__,
339 errorcode);
9f594791 340 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
399f11c3 341 goto wait_on_recovery;
03391693 342#endif /* defined(CONFIG_NFS_V4_1) */
65de872e 343 case -NFS4ERR_FILE_OPEN:
44ed3556
N
344 if (exception->timeout > HZ) {
345 /* We have retried a decent amount, time to
346 * fail
347 */
348 ret = -EBUSY;
349 break;
350 }
65de872e
TM
351 case -NFS4ERR_GRACE:
352 case -NFS4ERR_DELAY:
2c643488 353 case -EKEYEXPIRED:
65de872e
TM
354 ret = nfs4_delay(server->client, &exception->timeout);
355 if (ret != 0)
356 break;
a8a4ae3a 357 case -NFS4ERR_RETRY_UNCACHED_REP:
65de872e
TM
358 case -NFS4ERR_OLD_STATEID:
359 exception->retry = 1;
b064eca2
TM
360 break;
361 case -NFS4ERR_BADOWNER:
362 /* The following works around a Linux server bug! */
363 case -NFS4ERR_BADNAME:
364 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
365 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
366 exception->retry = 1;
367 printk(KERN_WARNING "NFS: v4 server %s "
368 "does not accept raw "
369 "uid/gids. "
370 "Reenabling the idmapper.\n",
371 server->nfs_client->cl_hostname);
372 }
65de872e
TM
373 }
374 /* We failed to handle the error */
375 return nfs4_map_errors(ret);
0400a6b0 376wait_on_recovery:
a2c0b9e2
TM
377 ret = nfs4_wait_clnt_recover(clp);
378 if (ret == 0)
379 exception->retry = 1;
380 return ret;
65de872e
TM
381}
382
383
452e9352 384static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
1da177e4 385{
1da177e4
LT
386 spin_lock(&clp->cl_lock);
387 if (time_before(clp->cl_last_renewal,timestamp))
388 clp->cl_last_renewal = timestamp;
389 spin_unlock(&clp->cl_lock);
390}
391
452e9352
TM
392static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
393{
394 do_renew_lease(server->nfs_client, timestamp);
395}
396
cccef3b9
AA
397#if defined(CONFIG_NFS_V4_1)
398
87dda67e
TM
399/*
400 * nfs4_shrink_slot_table - free retired slots from the slot table
401 */
402static void nfs4_shrink_slot_table(struct nfs4_slot_table *tbl, u32 newsize)
403{
404 struct nfs4_slot **p;
405 if (newsize >= tbl->max_slots)
406 return;
407
408 p = &tbl->slots;
409 while (newsize--)
410 p = &(*p)->next;
411 while (*p) {
412 struct nfs4_slot *slot = *p;
413
414 *p = slot->next;
415 kfree(slot);
416 tbl->max_slots--;
417 }
418}
419
e2c4ab3c
AA
420/*
421 * nfs4_free_slot - free a slot and efficiently update slot table.
422 *
423 * freeing a slot is trivially done by clearing its respective bit
424 * in the bitmap.
425 * If the freed slotid equals highest_used_slotid we want to update it
426 * so that the server would be able to size down the slot table if needed,
427 * otherwise we know that the highest_used_slotid is still in use.
428 * When updating highest_used_slotid there may be "holes" in the bitmap
429 * so we need to scan down from highest_used_slotid to 0 looking for the now
430 * highest slotid in use.
45d43c29 431 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
35dc1d74
TM
432 *
433 * Must be called while holding tbl->slot_tbl_lock
e2c4ab3c
AA
434 */
435static void
f4af6e2a 436nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
e2c4ab3c 437{
f4af6e2a
TM
438 u32 slotid = slot->slot_nr;
439
e2c4ab3c
AA
440 /* clear used bit in bitmap */
441 __clear_bit(slotid, tbl->used_slots);
442
443 /* update highest_used_slotid when it is freed */
444 if (slotid == tbl->highest_used_slotid) {
f4af6e2a
TM
445 u32 new_max = find_last_bit(tbl->used_slots, slotid);
446 if (new_max < slotid)
447 tbl->highest_used_slotid = new_max;
e2c4ab3c 448 else
45d43c29 449 tbl->highest_used_slotid = NFS4_NO_SLOT;
e2c4ab3c 450 }
45d43c29
TM
451 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
452 slotid, tbl->highest_used_slotid);
e2c4ab3c
AA
453}
454
961a828d
TM
455bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
456{
457 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
458 return true;
e2c4ab3c
AA
459}
460
3bfb0fc5 461/*
42acd021 462 * Signal state manager thread if session fore channel is drained
3bfb0fc5 463 */
42acd021 464static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
3bfb0fc5 465{
a2118c33 466 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
961a828d
TM
467 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
468 nfs4_set_task_privileged, NULL);
3bfb0fc5
AB
469 return;
470 }
471
45d43c29 472 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
3bfb0fc5
AB
473 return;
474
42acd021
AA
475 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
476 complete(&ses->fc_slot_table.complete);
477}
478
479/*
480 * Signal state manager thread if session back channel is drained
481 */
482void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
483{
484 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
45d43c29 485 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
42acd021
AA
486 return;
487 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
488 complete(&ses->bc_slot_table.complete);
3bfb0fc5
AB
489}
490
d185a334 491static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
13615871 492{
e3725ec0 493 struct nfs4_session *session;
13615871
AA
494 struct nfs4_slot_table *tbl;
495
dfb4f309 496 if (!res->sr_slot) {
13615871
AA
497 /* just wake up the next guy waiting since
498 * we may have not consumed a slot after all */
691daf3b 499 dprintk("%s: No slot\n", __func__);
35dc1d74 500 return;
13615871 501 }
e3725ec0
TM
502 tbl = res->sr_slot->table;
503 session = tbl->session;
ea028ac9 504
35dc1d74 505 spin_lock(&tbl->slot_tbl_lock);
f4af6e2a 506 nfs4_free_slot(tbl, res->sr_slot);
e3725ec0 507 nfs4_check_drain_fc_complete(session);
35dc1d74 508 spin_unlock(&tbl->slot_tbl_lock);
dfb4f309 509 res->sr_slot = NULL;
13615871
AA
510}
511
464ee9f9
TM
512/* Update the client's idea of target_highest_slotid */
513static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
514 u32 target_highest_slotid)
515{
97e548a9
TM
516 unsigned int max_slotid, i;
517
464ee9f9
TM
518 if (tbl->target_highest_slotid == target_highest_slotid)
519 return;
520 tbl->target_highest_slotid = target_highest_slotid;
521 tbl->generation++;
97e548a9 522
87dda67e 523 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, tbl->target_highest_slotid);
97e548a9
TM
524 for (i = tbl->max_slotid + 1; i <= max_slotid; i++)
525 rpc_wake_up_next(&tbl->slot_tbl_waitq);
526 tbl->max_slotid = max_slotid;
464ee9f9
TM
527}
528
ce008c4b
TM
529void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
530 u32 target_highest_slotid)
531{
532 spin_lock(&tbl->slot_tbl_lock);
533 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
534 spin_unlock(&tbl->slot_tbl_lock);
535}
536
da0507b7
TM
537static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
538 u32 highest_slotid)
539{
da0507b7
TM
540 if (tbl->server_highest_slotid == highest_slotid)
541 return;
542 if (tbl->highest_used_slotid > highest_slotid)
543 return;
87dda67e
TM
544 /* Deallocate slots */
545 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
da0507b7
TM
546 tbl->server_highest_slotid = highest_slotid;
547}
548
464ee9f9
TM
549static void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
550 struct nfs4_slot *slot,
551 struct nfs4_sequence_res *res)
552{
553 spin_lock(&tbl->slot_tbl_lock);
554 if (tbl->generation != slot->generation)
555 goto out;
da0507b7 556 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
464ee9f9
TM
557 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
558out:
559 spin_unlock(&tbl->slot_tbl_lock);
560}
561
14516c3a 562static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
b0df806c 563{
e3725ec0 564 struct nfs4_session *session;
933602e3 565 struct nfs4_slot *slot;
b0df806c 566 unsigned long timestamp;
14516c3a 567 struct nfs_client *clp;
b0df806c
AA
568
569 /*
570 * sr_status remains 1 if an RPC level error occurred. The server
571 * may or may not have processed the sequence operation..
572 * Proceed as if the server received and processed the sequence
573 * operation.
574 */
575 if (res->sr_status == 1)
576 res->sr_status = NFS_OK;
577
468f8613
BS
578 /* don't increment the sequence number if the task wasn't sent */
579 if (!RPC_WAS_SENT(task))
b0df806c
AA
580 goto out;
581
933602e3 582 slot = res->sr_slot;
e3725ec0 583 session = slot->table->session;
933602e3 584
691daf3b 585 /* Check the SEQUENCE operation status */
14516c3a
TM
586 switch (res->sr_status) {
587 case 0:
b0df806c 588 /* Update the slot's sequence and clientid lease timer */
933602e3
TM
589 ++slot->seq_nr;
590 timestamp = slot->renewal_time;
e3725ec0 591 clp = session->clp;
452e9352 592 do_renew_lease(clp, timestamp);
0629e370 593 /* Check sequence flags */
b4410c2f
TM
594 if (res->sr_status_flags != 0)
595 nfs4_schedule_lease_recovery(clp);
464ee9f9 596 nfs41_update_target_slotid(slot->table, slot, res);
14516c3a
TM
597 break;
598 case -NFS4ERR_DELAY:
599 /* The server detected a resend of the RPC call and
600 * returned NFS4ERR_DELAY as per Section 2.10.6.2
601 * of RFC5661.
602 */
df2fabff 603 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
dfb4f309 604 __func__,
df2fabff 605 slot->slot_nr,
933602e3 606 slot->seq_nr);
14516c3a
TM
607 goto out_retry;
608 default:
609 /* Just update the slot sequence no. */
933602e3 610 ++slot->seq_nr;
b0df806c
AA
611 }
612out:
613 /* The session may be reset by one of the error handlers. */
614 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
d185a334 615 nfs41_sequence_free_slot(res);
14516c3a
TM
616 return 1;
617out_retry:
d05dd4e9 618 if (!rpc_restart_call(task))
14516c3a
TM
619 goto out;
620 rpc_delay(task, NFS4_POLL_RETRY_MAX);
621 return 0;
b0df806c
AA
622}
623
14516c3a
TM
624static int nfs4_sequence_done(struct rpc_task *task,
625 struct nfs4_sequence_res *res)
df896455 626{
e3725ec0 627 if (res->sr_slot == NULL)
14516c3a
TM
628 return 1;
629 return nfs41_sequence_done(task, res);
df896455
TM
630}
631
87dda67e
TM
632static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl,
633 u32 slotid, u32 seq_init, gfp_t gfp_mask)
634{
635 struct nfs4_slot *slot;
636
637 slot = kzalloc(sizeof(*slot), gfp_mask);
638 if (slot) {
639 slot->table = tbl;
640 slot->slot_nr = slotid;
641 slot->seq_nr = seq_init;
642 }
643 return slot;
644}
645
646static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl,
647 u32 slotid, u32 seq_init, gfp_t gfp_mask)
648{
649 struct nfs4_slot **p, *slot;
650
651 p = &tbl->slots;
652 for (;;) {
653 if (*p == NULL) {
654 *p = nfs4_new_slot(tbl, tbl->max_slots,
655 seq_init, gfp_mask);
656 if (*p == NULL)
657 break;
658 tbl->max_slots++;
659 }
660 slot = *p;
661 if (slot->slot_nr == slotid)
662 return slot;
663 p = &slot->next;
664 }
69d206b5 665 return ERR_PTR(-ENOMEM);
87dda67e
TM
666}
667
510b8175 668/*
2dc03b7f 669 * nfs4_alloc_slot - efficiently look for a free slot
510b8175 670 *
2dc03b7f 671 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
510b8175
BH
672 * If found, we mark the slot as used, update the highest_used_slotid,
673 * and respectively set up the sequence operation args.
fbcd4abc
AA
674 *
675 * Note: must be called with under the slot_tbl_lock.
510b8175 676 */
2dc03b7f 677static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
510b8175 678{
69d206b5 679 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
45d43c29 680 u32 slotid;
510b8175 681
45d43c29 682 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
510b8175 683 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
87dda67e 684 tbl->max_slotid + 1);
97e548a9
TM
685 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
686 if (slotid > tbl->max_slotid)
510b8175 687 goto out;
87dda67e 688 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
69d206b5 689 if (IS_ERR(ret))
87dda67e 690 goto out;
510b8175 691 __set_bit(slotid, tbl->used_slots);
45d43c29
TM
692 if (slotid > tbl->highest_used_slotid ||
693 tbl->highest_used_slotid == NFS4_NO_SLOT)
510b8175 694 tbl->highest_used_slotid = slotid;
2dc03b7f 695 ret->renewal_time = jiffies;
464ee9f9 696 ret->generation = tbl->generation;
2dc03b7f 697
510b8175
BH
698out:
699 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
2dc03b7f 700 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
69d206b5 701 !IS_ERR(ret) ? ret->slot_nr : -1);
2dc03b7f 702 return ret;
510b8175
BH
703}
704
9d12b216
TM
705static void nfs41_init_sequence(struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res, int cache_reply)
707{
2b2fa717 708 args->sa_slot = NULL;
9d12b216
TM
709 args->sa_cache_this = 0;
710 if (cache_reply)
711 args->sa_cache_this = 1;
9d12b216
TM
712 res->sr_slot = NULL;
713}
714
dc70d7b3 715int nfs41_setup_sequence(struct nfs4_session *session,
ce5039c1
AA
716 struct nfs4_sequence_args *args,
717 struct nfs4_sequence_res *res,
ce5039c1
AA
718 struct rpc_task *task)
719{
fbcd4abc
AA
720 struct nfs4_slot *slot;
721 struct nfs4_slot_table *tbl;
fbcd4abc
AA
722
723 dprintk("--> %s\n", __func__);
ce5039c1 724 /* slot already allocated? */
dfb4f309 725 if (res->sr_slot != NULL)
ce5039c1
AA
726 return 0;
727
fbcd4abc
AA
728 tbl = &session->fc_slot_table;
729
69d206b5
TM
730 task->tk_timeout = 0;
731
fbcd4abc 732 spin_lock(&tbl->slot_tbl_lock);
a2118c33 733 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
5601a00d 734 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
0b1c8fc4 735 /* The state manager will wait until the slot table is empty */
05f0d236 736 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
b069d94a 737 spin_unlock(&tbl->slot_tbl_lock);
0b1c8fc4 738 dprintk("%s session is draining\n", __func__);
05f0d236 739 return -EAGAIN;
b069d94a
AA
740 }
741
689cf5c1
AB
742 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
743 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
744 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
745 spin_unlock(&tbl->slot_tbl_lock);
746 dprintk("%s enforce FIFO order\n", __func__);
747 return -EAGAIN;
748 }
749
2dc03b7f 750 slot = nfs4_alloc_slot(tbl);
69d206b5
TM
751 if (IS_ERR(slot)) {
752 /* If out of memory, try again in 1/4 second */
753 if (slot == ERR_PTR(-ENOMEM))
754 task->tk_timeout = HZ >> 2;
fbcd4abc
AA
755 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
756 spin_unlock(&tbl->slot_tbl_lock);
757 dprintk("<-- %s: no free slots\n", __func__);
758 return -EAGAIN;
759 }
760 spin_unlock(&tbl->slot_tbl_lock);
761
689cf5c1 762 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
2b2fa717
TM
763
764 args->sa_slot = slot;
fbcd4abc 765
2dc03b7f
TM
766 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
767 slot->slot_nr, slot->seq_nr);
fbcd4abc 768
dfb4f309 769 res->sr_slot = slot;
2a6e26cd 770 res->sr_status_flags = 0;
fbcd4abc
AA
771 /*
772 * sr_status is only set in decode_sequence, and so will remain
773 * set to 1 if an rpc level failure occurs.
774 */
775 res->sr_status = 1;
ce5039c1
AA
776 return 0;
777}
dc70d7b3 778EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
ce5039c1 779
035168ab 780int nfs4_setup_sequence(const struct nfs_server *server,
ce5039c1
AA
781 struct nfs4_sequence_args *args,
782 struct nfs4_sequence_res *res,
ce5039c1
AA
783 struct rpc_task *task)
784{
035168ab 785 struct nfs4_session *session = nfs4_get_session(server);
ce5039c1
AA
786 int ret = 0;
787
9d12b216 788 if (session == NULL)
035168ab 789 goto out;
035168ab 790
df2fabff 791 dprintk("--> %s clp %p session %p sr_slot %d\n",
dfb4f309 792 __func__, session->clp, session, res->sr_slot ?
df2fabff 793 res->sr_slot->slot_nr : -1);
ce5039c1 794
9d12b216 795 ret = nfs41_setup_sequence(session, args, res, task);
ce5039c1
AA
796out:
797 dprintk("<-- %s status=%d\n", __func__, ret);
798 return ret;
799}
800
801struct nfs41_call_sync_data {
035168ab 802 const struct nfs_server *seq_server;
ce5039c1
AA
803 struct nfs4_sequence_args *seq_args;
804 struct nfs4_sequence_res *seq_res;
ce5039c1
AA
805};
806
807static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
808{
809 struct nfs41_call_sync_data *data = calldata;
810
035168ab
TM
811 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
812
813 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
9d12b216 814 data->seq_res, task))
ce5039c1
AA
815 return;
816 rpc_call_start(task);
817}
818
b257957e
AB
819static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
820{
821 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
822 nfs41_call_sync_prepare(task, calldata);
823}
824
69ab40c4
AA
825static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
826{
827 struct nfs41_call_sync_data *data = calldata;
828
14516c3a 829 nfs41_sequence_done(task, data->seq_res);
69ab40c4
AA
830}
831
17280175 832static const struct rpc_call_ops nfs41_call_sync_ops = {
ce5039c1 833 .rpc_call_prepare = nfs41_call_sync_prepare,
69ab40c4 834 .rpc_call_done = nfs41_call_sync_done,
ce5039c1
AA
835};
836
17280175 837static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
b257957e
AB
838 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
839 .rpc_call_done = nfs41_call_sync_done,
840};
841
7c513058
BS
842static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
843 struct nfs_server *server,
ce5039c1
AA
844 struct rpc_message *msg,
845 struct nfs4_sequence_args *args,
846 struct nfs4_sequence_res *res,
b257957e 847 int privileged)
ce5039c1
AA
848{
849 int ret;
850 struct rpc_task *task;
851 struct nfs41_call_sync_data data = {
035168ab 852 .seq_server = server,
ce5039c1
AA
853 .seq_args = args,
854 .seq_res = res,
ce5039c1
AA
855 };
856 struct rpc_task_setup task_setup = {
7c513058 857 .rpc_client = clnt,
ce5039c1
AA
858 .rpc_message = msg,
859 .callback_ops = &nfs41_call_sync_ops,
860 .callback_data = &data
861 };
862
b257957e
AB
863 if (privileged)
864 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
ce5039c1
AA
865 task = rpc_run_task(&task_setup);
866 if (IS_ERR(task))
867 ret = PTR_ERR(task);
868 else {
869 ret = task->tk_status;
870 rpc_put_task(task);
871 }
872 return ret;
873}
874
7c513058
BS
875int _nfs4_call_sync_session(struct rpc_clnt *clnt,
876 struct nfs_server *server,
cccef3b9
AA
877 struct rpc_message *msg,
878 struct nfs4_sequence_args *args,
879 struct nfs4_sequence_res *res,
880 int cache_reply)
881{
9d12b216
TM
882 nfs41_init_sequence(args, res, cache_reply);
883 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
cccef3b9
AA
884}
885
df896455 886#else
9d12b216
TM
887static inline
888void nfs41_init_sequence(struct nfs4_sequence_args *args,
889 struct nfs4_sequence_res *res, int cache_reply)
890{
891}
892
14516c3a
TM
893static int nfs4_sequence_done(struct rpc_task *task,
894 struct nfs4_sequence_res *res)
df896455 895{
14516c3a 896 return 1;
df896455 897}
cccef3b9
AA
898#endif /* CONFIG_NFS_V4_1 */
899
7c513058
BS
900int _nfs4_call_sync(struct rpc_clnt *clnt,
901 struct nfs_server *server,
cccef3b9
AA
902 struct rpc_message *msg,
903 struct nfs4_sequence_args *args,
904 struct nfs4_sequence_res *res,
905 int cache_reply)
906{
9d12b216 907 nfs41_init_sequence(args, res, cache_reply);
7c513058 908 return rpc_call_sync(clnt, msg, 0);
cccef3b9
AA
909}
910
e73b83f2 911static inline
7c513058
BS
912int nfs4_call_sync(struct rpc_clnt *clnt,
913 struct nfs_server *server,
e73b83f2
BS
914 struct rpc_message *msg,
915 struct nfs4_sequence_args *args,
916 struct nfs4_sequence_res *res,
917 int cache_reply)
918{
7c513058
BS
919 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
920 args, res, cache_reply);
e73b83f2 921}
cccef3b9 922
38478b24 923static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1da177e4 924{
38478b24 925 struct nfs_inode *nfsi = NFS_I(dir);
1da177e4 926
38478b24 927 spin_lock(&dir->i_lock);
359d7d1c 928 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
a9a4a87a 929 if (!cinfo->atomic || cinfo->before != dir->i_version)
bfc69a45 930 nfs_force_lookup_revalidate(dir);
a9a4a87a 931 dir->i_version = cinfo->after;
38478b24 932 spin_unlock(&dir->i_lock);
1da177e4
LT
933}
934
e56e0b78 935struct nfs4_opendata {
c6d00e63 936 struct kref kref;
e56e0b78
TM
937 struct nfs_openargs o_arg;
938 struct nfs_openres o_res;
cdd4e68b
TM
939 struct nfs_open_confirmargs c_arg;
940 struct nfs_open_confirmres c_res;
6926afd1
TM
941 struct nfs4_string owner_name;
942 struct nfs4_string group_name;
e56e0b78 943 struct nfs_fattr f_attr;
e56e0b78 944 struct dentry *dir;
82a2c1b7 945 struct dentry *dentry;
e56e0b78 946 struct nfs4_state_owner *owner;
aac00a8d 947 struct nfs4_state *state;
e56e0b78 948 struct iattr attrs;
26e976a8 949 unsigned long timestamp;
3e309914 950 unsigned int rpc_done : 1;
24ac23ab
TM
951 int rpc_status;
952 int cancelled;
e56e0b78
TM
953};
954
2ced46c2
TM
955
956static void nfs4_init_opendata_res(struct nfs4_opendata *p)
957{
958 p->o_res.f_attr = &p->f_attr;
c1d51931
TM
959 p->o_res.seqid = p->o_arg.seqid;
960 p->c_res.seqid = p->c_arg.seqid;
2ced46c2 961 p->o_res.server = p->o_arg.server;
5f657530 962 p->o_res.access_request = p->o_arg.access;
2ced46c2 963 nfs_fattr_init(&p->f_attr);
6926afd1 964 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
2ced46c2
TM
965}
966
82a2c1b7 967static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
dc0b027d 968 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
8535b2be
TM
969 const struct iattr *attrs,
970 gfp_t gfp_mask)
e56e0b78 971{
82a2c1b7 972 struct dentry *parent = dget_parent(dentry);
e56e0b78
TM
973 struct inode *dir = parent->d_inode;
974 struct nfs_server *server = NFS_SERVER(dir);
975 struct nfs4_opendata *p;
976
8535b2be 977 p = kzalloc(sizeof(*p), gfp_mask);
e56e0b78
TM
978 if (p == NULL)
979 goto err;
8535b2be 980 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
e56e0b78
TM
981 if (p->o_arg.seqid == NULL)
982 goto err_free;
82a2c1b7
AV
983 nfs_sb_active(dentry->d_sb);
984 p->dentry = dget(dentry);
e56e0b78
TM
985 p->dir = parent;
986 p->owner = sp;
987 atomic_inc(&sp->so_count);
988 p->o_arg.fh = NFS_FH(dir);
dc0b027d
TM
989 p->o_arg.open_flags = flags;
990 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
ae2bb032
WAA
991 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
992 * will return permission denied for all bits until close */
993 if (!(flags & O_EXCL)) {
994 /* ask server to check for all possible rights as results
995 * are cached */
996 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
997 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
ae2bb032 998 }
7539bbab 999 p->o_arg.clientid = server->nfs_client->cl_clientid;
95b72eb0
TM
1000 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1001 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
82a2c1b7 1002 p->o_arg.name = &dentry->d_name;
e56e0b78
TM
1003 p->o_arg.server = server;
1004 p->o_arg.bitmask = server->attr_bitmask;
1549210f 1005 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
e56e0b78 1006 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
536e43d1 1007 if (attrs != NULL && attrs->ia_valid != 0) {
cd93710e 1008 __be32 verf[2];
d77d76ff 1009
e56e0b78
TM
1010 p->o_arg.u.attrs = &p->attrs;
1011 memcpy(&p->attrs, attrs, sizeof(p->attrs));
cd93710e
CL
1012
1013 verf[0] = jiffies;
1014 verf[1] = current->pid;
1015 memcpy(p->o_arg.u.verifier.data, verf,
1016 sizeof(p->o_arg.u.verifier.data));
e56e0b78 1017 }
cdd4e68b
TM
1018 p->c_arg.fh = &p->o_res.fh;
1019 p->c_arg.stateid = &p->o_res.stateid;
1020 p->c_arg.seqid = p->o_arg.seqid;
2ced46c2 1021 nfs4_init_opendata_res(p);
c6d00e63 1022 kref_init(&p->kref);
e56e0b78
TM
1023 return p;
1024err_free:
1025 kfree(p);
1026err:
1027 dput(parent);
1028 return NULL;
1029}
1030
c6d00e63 1031static void nfs4_opendata_free(struct kref *kref)
e56e0b78 1032{
c6d00e63
TM
1033 struct nfs4_opendata *p = container_of(kref,
1034 struct nfs4_opendata, kref);
82a2c1b7 1035 struct super_block *sb = p->dentry->d_sb;
c6d00e63
TM
1036
1037 nfs_free_seqid(p->o_arg.seqid);
aac00a8d
TM
1038 if (p->state != NULL)
1039 nfs4_put_open_state(p->state);
c6d00e63
TM
1040 nfs4_put_state_owner(p->owner);
1041 dput(p->dir);
82a2c1b7
AV
1042 dput(p->dentry);
1043 nfs_sb_deactive(sb);
6926afd1 1044 nfs_fattr_free_names(&p->f_attr);
c6d00e63
TM
1045 kfree(p);
1046}
1047
1048static void nfs4_opendata_put(struct nfs4_opendata *p)
1049{
1050 if (p != NULL)
1051 kref_put(&p->kref, nfs4_opendata_free);
e56e0b78
TM
1052}
1053
06f814a3
TM
1054static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1055{
06f814a3
TM
1056 int ret;
1057
06f814a3 1058 ret = rpc_wait_for_completion_task(task);
06f814a3
TM
1059 return ret;
1060}
1061
dc0b027d 1062static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
6ee41268
TM
1063{
1064 int ret = 0;
dc0b027d 1065
536e43d1 1066 if (open_mode & (O_EXCL|O_TRUNC))
dc0b027d
TM
1067 goto out;
1068 switch (mode & (FMODE_READ|FMODE_WRITE)) {
6ee41268 1069 case FMODE_READ:
88069f77
TM
1070 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1071 && state->n_rdonly != 0;
6ee41268
TM
1072 break;
1073 case FMODE_WRITE:
88069f77
TM
1074 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1075 && state->n_wronly != 0;
6ee41268
TM
1076 break;
1077 case FMODE_READ|FMODE_WRITE:
88069f77
TM
1078 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1079 && state->n_rdwr != 0;
6ee41268 1080 }
dc0b027d 1081out:
6ee41268
TM
1082 return ret;
1083}
1084
dc0b027d 1085static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
aac00a8d 1086{
652f89f6
TM
1087 if (delegation == NULL)
1088 return 0;
dc0b027d 1089 if ((delegation->type & fmode) != fmode)
aac00a8d 1090 return 0;
15c831bf 1091 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
aac00a8d 1092 return 0;
b7391f44 1093 nfs_mark_delegation_referenced(delegation);
aac00a8d
TM
1094 return 1;
1095}
1096
dc0b027d 1097static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
e7616923 1098{
dc0b027d 1099 switch (fmode) {
e7616923
TM
1100 case FMODE_WRITE:
1101 state->n_wronly++;
1102 break;
1103 case FMODE_READ:
1104 state->n_rdonly++;
1105 break;
1106 case FMODE_READ|FMODE_WRITE:
1107 state->n_rdwr++;
1108 }
dc0b027d 1109 nfs4_state_set_mode_locked(state, state->state | fmode);
003707c7
TM
1110}
1111
dc0b027d 1112static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
003707c7
TM
1113{
1114 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
f597c537
TM
1115 nfs4_stateid_copy(&state->stateid, stateid);
1116 nfs4_stateid_copy(&state->open_stateid, stateid);
dc0b027d 1117 switch (fmode) {
003707c7
TM
1118 case FMODE_READ:
1119 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1120 break;
1121 case FMODE_WRITE:
1122 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1123 break;
1124 case FMODE_READ|FMODE_WRITE:
1125 set_bit(NFS_O_RDWR_STATE, &state->flags);
1126 }
e7616923
TM
1127}
1128
dc0b027d 1129static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
003707c7 1130{
8bda4e4c 1131 write_seqlock(&state->seqlock);
dc0b027d 1132 nfs_set_open_stateid_locked(state, stateid, fmode);
8bda4e4c 1133 write_sequnlock(&state->seqlock);
003707c7
TM
1134}
1135
dc0b027d 1136static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1da177e4 1137{
8bda4e4c
TM
1138 /*
1139 * Protect the call to nfs4_state_set_mode_locked and
1140 * serialise the stateid update
1141 */
1142 write_seqlock(&state->seqlock);
003707c7 1143 if (deleg_stateid != NULL) {
f597c537 1144 nfs4_stateid_copy(&state->stateid, deleg_stateid);
003707c7
TM
1145 set_bit(NFS_DELEGATED_STATE, &state->flags);
1146 }
1147 if (open_stateid != NULL)
dc0b027d 1148 nfs_set_open_stateid_locked(state, open_stateid, fmode);
8bda4e4c
TM
1149 write_sequnlock(&state->seqlock);
1150 spin_lock(&state->owner->so_lock);
dc0b027d 1151 update_open_stateflags(state, fmode);
ec073428 1152 spin_unlock(&state->owner->so_lock);
1da177e4
LT
1153}
1154
dc0b027d 1155static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
34310430
TM
1156{
1157 struct nfs_inode *nfsi = NFS_I(state->inode);
1158 struct nfs_delegation *deleg_cur;
1159 int ret = 0;
1160
dc0b027d 1161 fmode &= (FMODE_READ|FMODE_WRITE);
34310430
TM
1162
1163 rcu_read_lock();
1164 deleg_cur = rcu_dereference(nfsi->delegation);
1165 if (deleg_cur == NULL)
1166 goto no_delegation;
1167
1168 spin_lock(&deleg_cur->lock);
1169 if (nfsi->delegation != deleg_cur ||
dc0b027d 1170 (deleg_cur->type & fmode) != fmode)
34310430
TM
1171 goto no_delegation_unlock;
1172
1173 if (delegation == NULL)
1174 delegation = &deleg_cur->stateid;
f597c537 1175 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
34310430
TM
1176 goto no_delegation_unlock;
1177
b7391f44 1178 nfs_mark_delegation_referenced(deleg_cur);
dc0b027d 1179 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
34310430
TM
1180 ret = 1;
1181no_delegation_unlock:
1182 spin_unlock(&deleg_cur->lock);
1183no_delegation:
1184 rcu_read_unlock();
1185
1186 if (!ret && open_stateid != NULL) {
dc0b027d 1187 __update_open_stateid(state, open_stateid, NULL, fmode);
34310430
TM
1188 ret = 1;
1189 }
1190
1191 return ret;
1192}
1193
1194
dc0b027d 1195static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
aac00a8d
TM
1196{
1197 struct nfs_delegation *delegation;
1198
1199 rcu_read_lock();
1200 delegation = rcu_dereference(NFS_I(inode)->delegation);
dc0b027d 1201 if (delegation == NULL || (delegation->type & fmode) == fmode) {
aac00a8d
TM
1202 rcu_read_unlock();
1203 return;
1204 }
1205 rcu_read_unlock();
57ec14c5 1206 nfs4_inode_return_delegation(inode);
aac00a8d
TM
1207}
1208
6ee41268 1209static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
aac00a8d
TM
1210{
1211 struct nfs4_state *state = opendata->state;
1212 struct nfs_inode *nfsi = NFS_I(state->inode);
1213 struct nfs_delegation *delegation;
536e43d1 1214 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
dc0b027d 1215 fmode_t fmode = opendata->o_arg.fmode;
aac00a8d
TM
1216 nfs4_stateid stateid;
1217 int ret = -EAGAIN;
1218
aac00a8d 1219 for (;;) {
dc0b027d 1220 if (can_open_cached(state, fmode, open_mode)) {
6ee41268 1221 spin_lock(&state->owner->so_lock);
dc0b027d
TM
1222 if (can_open_cached(state, fmode, open_mode)) {
1223 update_open_stateflags(state, fmode);
6ee41268 1224 spin_unlock(&state->owner->so_lock);
6ee41268
TM
1225 goto out_return_state;
1226 }
1227 spin_unlock(&state->owner->so_lock);
1228 }
34310430
TM
1229 rcu_read_lock();
1230 delegation = rcu_dereference(nfsi->delegation);
652f89f6 1231 if (!can_open_delegated(delegation, fmode)) {
34310430 1232 rcu_read_unlock();
aac00a8d 1233 break;
34310430 1234 }
aac00a8d 1235 /* Save the delegation */
f597c537 1236 nfs4_stateid_copy(&stateid, &delegation->stateid);
aac00a8d 1237 rcu_read_unlock();
af22f94a 1238 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
aac00a8d
TM
1239 if (ret != 0)
1240 goto out;
1241 ret = -EAGAIN;
34310430
TM
1242
1243 /* Try to update the stateid using the delegation */
dc0b027d 1244 if (update_open_stateid(state, NULL, &stateid, fmode))
34310430 1245 goto out_return_state;
aac00a8d 1246 }
aac00a8d
TM
1247out:
1248 return ERR_PTR(ret);
1249out_return_state:
1250 atomic_inc(&state->count);
1251 return state;
1252}
1253
e23008ec
AA
1254static void
1255nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1256{
1257 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1258 struct nfs_delegation *delegation;
1259 int delegation_flags = 0;
1260
1261 rcu_read_lock();
1262 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1263 if (delegation)
1264 delegation_flags = delegation->flags;
1265 rcu_read_unlock();
1266 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1267 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1268 "returning a delegation for "
1269 "OPEN(CLAIM_DELEGATE_CUR)\n",
1270 clp->cl_hostname);
1271 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1272 nfs_inode_set_delegation(state->inode,
1273 data->owner->so_cred,
1274 &data->o_res);
1275 else
1276 nfs_inode_reclaim_delegation(state->inode,
1277 data->owner->so_cred,
1278 &data->o_res);
1279}
1280
1281/*
1282 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1283 * and update the nfs4_state.
1284 */
1285static struct nfs4_state *
1286_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1287{
1288 struct inode *inode = data->state->inode;
1289 struct nfs4_state *state = data->state;
1290 int ret;
1291
1292 if (!data->rpc_done) {
1293 ret = data->rpc_status;
1294 goto err;
1295 }
1296
1297 ret = -ESTALE;
1298 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1299 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1300 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1301 goto err;
1302
1303 ret = -ENOMEM;
1304 state = nfs4_get_open_state(inode, data->owner);
1305 if (state == NULL)
1306 goto err;
1307
1308 ret = nfs_refresh_inode(inode, &data->f_attr);
1309 if (ret)
1310 goto err;
1311
1312 if (data->o_res.delegation_type != 0)
1313 nfs4_opendata_check_deleg(data, state);
1314 update_open_stateid(state, &data->o_res.stateid, NULL,
1315 data->o_arg.fmode);
1316
1317 return state;
1318err:
1319 return ERR_PTR(ret);
1320
1321}
1322
1323static struct nfs4_state *
1324_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
24ac23ab
TM
1325{
1326 struct inode *inode;
1327 struct nfs4_state *state = NULL;
1b370bc2 1328 int ret;
24ac23ab 1329
aac00a8d 1330 if (!data->rpc_done) {
6ee41268 1331 state = nfs4_try_open_cached(data);
aac00a8d
TM
1332 goto out;
1333 }
1334
1b370bc2 1335 ret = -EAGAIN;
24ac23ab 1336 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1b370bc2 1337 goto err;
24ac23ab 1338 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1b370bc2 1339 ret = PTR_ERR(inode);
03f28e3a 1340 if (IS_ERR(inode))
1b370bc2
TM
1341 goto err;
1342 ret = -ENOMEM;
24ac23ab
TM
1343 state = nfs4_get_open_state(inode, data->owner);
1344 if (state == NULL)
1b370bc2 1345 goto err_put_inode;
e23008ec
AA
1346 if (data->o_res.delegation_type != 0)
1347 nfs4_opendata_check_deleg(data, state);
34310430 1348 update_open_stateid(state, &data->o_res.stateid, NULL,
dc0b027d 1349 data->o_arg.fmode);
24ac23ab 1350 iput(inode);
aac00a8d 1351out:
24ac23ab 1352 return state;
1b370bc2
TM
1353err_put_inode:
1354 iput(inode);
1355err:
1356 return ERR_PTR(ret);
24ac23ab
TM
1357}
1358
e23008ec
AA
1359static struct nfs4_state *
1360nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1361{
1362 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1363 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1364 return _nfs4_opendata_to_nfs4_state(data);
1365}
1366
864472e9
TM
1367static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1368{
1369 struct nfs_inode *nfsi = NFS_I(state->inode);
1370 struct nfs_open_context *ctx;
1371
1372 spin_lock(&state->inode->i_lock);
1373 list_for_each_entry(ctx, &nfsi->open_files, list) {
1374 if (ctx->state != state)
1375 continue;
1376 get_nfs_open_context(ctx);
1377 spin_unlock(&state->inode->i_lock);
1378 return ctx;
1379 }
1380 spin_unlock(&state->inode->i_lock);
1381 return ERR_PTR(-ENOENT);
1382}
1383
6f220ed5
TM
1384static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1385{
1386 struct nfs4_opendata *opendata;
1387
3d4ff43d 1388 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
6f220ed5
TM
1389 if (opendata == NULL)
1390 return ERR_PTR(-ENOMEM);
1391 opendata->state = state;
1392 atomic_inc(&state->count);
1393 return opendata;
1394}
1395
dc0b027d 1396static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
864472e9 1397{
2ced46c2 1398 struct nfs4_state *newstate;
864472e9
TM
1399 int ret;
1400
dc0b027d
TM
1401 opendata->o_arg.open_flags = 0;
1402 opendata->o_arg.fmode = fmode;
2ced46c2
TM
1403 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1404 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1405 nfs4_init_opendata_res(opendata);
b257957e 1406 ret = _nfs4_recover_proc_open(opendata);
864472e9
TM
1407 if (ret != 0)
1408 return ret;
2ced46c2 1409 newstate = nfs4_opendata_to_nfs4_state(opendata);
1b370bc2
TM
1410 if (IS_ERR(newstate))
1411 return PTR_ERR(newstate);
643168c2 1412 nfs4_close_state(newstate, fmode);
2ced46c2 1413 *res = newstate;
864472e9
TM
1414 return 0;
1415}
1416
1417static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1418{
864472e9 1419 struct nfs4_state *newstate;
864472e9
TM
1420 int ret;
1421
1422 /* memory barrier prior to reading state->n_* */
2ced46c2 1423 clear_bit(NFS_DELEGATED_STATE, &state->flags);
864472e9
TM
1424 smp_rmb();
1425 if (state->n_rdwr != 0) {
b0ed9dbc 1426 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2ced46c2 1427 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
864472e9
TM
1428 if (ret != 0)
1429 return ret;
2ced46c2
TM
1430 if (newstate != state)
1431 return -ESTALE;
864472e9
TM
1432 }
1433 if (state->n_wronly != 0) {
b0ed9dbc 1434 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2ced46c2 1435 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
864472e9
TM
1436 if (ret != 0)
1437 return ret;
2ced46c2
TM
1438 if (newstate != state)
1439 return -ESTALE;
864472e9
TM
1440 }
1441 if (state->n_rdonly != 0) {
b0ed9dbc 1442 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2ced46c2 1443 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
864472e9
TM
1444 if (ret != 0)
1445 return ret;
2ced46c2
TM
1446 if (newstate != state)
1447 return -ESTALE;
864472e9 1448 }
1ac7e2fd
TM
1449 /*
1450 * We may have performed cached opens for all three recoveries.
1451 * Check if we need to update the current stateid.
1452 */
1453 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
f597c537 1454 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
8bda4e4c 1455 write_seqlock(&state->seqlock);
1ac7e2fd 1456 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
f597c537 1457 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
8bda4e4c 1458 write_sequnlock(&state->seqlock);
1ac7e2fd 1459 }
864472e9
TM
1460 return 0;
1461}
1462
1da177e4
LT
1463/*
1464 * OPEN_RECLAIM:
1465 * reclaim state on the server after a reboot.
1da177e4 1466 */
539cd03a 1467static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1da177e4 1468{
1ac7e2fd 1469 struct nfs_delegation *delegation;
864472e9 1470 struct nfs4_opendata *opendata;
dc0b027d 1471 fmode_t delegation_type = 0;
1da177e4
LT
1472 int status;
1473
6f220ed5
TM
1474 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1475 if (IS_ERR(opendata))
1476 return PTR_ERR(opendata);
864472e9
TM
1477 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1478 opendata->o_arg.fh = NFS_FH(state->inode);
1ac7e2fd
TM
1479 rcu_read_lock();
1480 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
15c831bf 1481 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
65bbf6bd 1482 delegation_type = delegation->type;
1ac7e2fd 1483 rcu_read_unlock();
864472e9
TM
1484 opendata->o_arg.u.delegation_type = delegation_type;
1485 status = nfs4_open_recover(opendata, state);
c6d00e63 1486 nfs4_opendata_put(opendata);
1da177e4
LT
1487 return status;
1488}
1489
539cd03a 1490static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1da177e4
LT
1491{
1492 struct nfs_server *server = NFS_SERVER(state->inode);
1493 struct nfs4_exception exception = { };
1494 int err;
1495 do {
539cd03a 1496 err = _nfs4_do_open_reclaim(ctx, state);
168667c4 1497 if (err != -NFS4ERR_DELAY)
202b50dc
TM
1498 break;
1499 nfs4_handle_exception(server, err, &exception);
1da177e4
LT
1500 } while (exception.retry);
1501 return err;
1502}
1503
864472e9
TM
1504static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1505{
1506 struct nfs_open_context *ctx;
1507 int ret;
1508
1509 ctx = nfs4_state_find_open_context(state);
1510 if (IS_ERR(ctx))
1511 return PTR_ERR(ctx);
539cd03a 1512 ret = nfs4_do_open_reclaim(ctx, state);
864472e9
TM
1513 put_nfs_open_context(ctx);
1514 return ret;
1515}
1516
13437e12 1517static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1da177e4 1518{
e56e0b78 1519 struct nfs4_opendata *opendata;
864472e9 1520 int ret;
1da177e4 1521
6f220ed5
TM
1522 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1523 if (IS_ERR(opendata))
1524 return PTR_ERR(opendata);
e56e0b78 1525 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
f597c537 1526 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
864472e9 1527 ret = nfs4_open_recover(opendata, state);
c6d00e63 1528 nfs4_opendata_put(opendata);
864472e9 1529 return ret;
1da177e4
LT
1530}
1531
13437e12 1532int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1da177e4
LT
1533{
1534 struct nfs4_exception exception = { };
539cd03a 1535 struct nfs_server *server = NFS_SERVER(state->inode);
1da177e4
LT
1536 int err;
1537 do {
13437e12 1538 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1da177e4
LT
1539 switch (err) {
1540 case 0:
965b5d67
TM
1541 case -ENOENT:
1542 case -ESTALE:
1543 goto out;
bcfa49f6
RL
1544 case -NFS4ERR_BADSESSION:
1545 case -NFS4ERR_BADSLOT:
1546 case -NFS4ERR_BAD_HIGH_SLOT:
1547 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1548 case -NFS4ERR_DEADSESSION:
9f594791 1549 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
bcfa49f6 1550 goto out;
1da177e4
LT
1551 case -NFS4ERR_STALE_CLIENTID:
1552 case -NFS4ERR_STALE_STATEID:
1553 case -NFS4ERR_EXPIRED:
1554 /* Don't recall a delegation if it was lost */
0400a6b0 1555 nfs4_schedule_lease_recovery(server->nfs_client);
965b5d67
TM
1556 goto out;
1557 case -ERESTARTSYS:
1558 /*
1559 * The show must go on: exit, but mark the
1560 * stateid as needing recovery.
1561 */
a1d0b5ee 1562 case -NFS4ERR_DELEG_REVOKED:
965b5d67
TM
1563 case -NFS4ERR_ADMIN_REVOKED:
1564 case -NFS4ERR_BAD_STATEID:
a1d0b5ee
TM
1565 nfs_inode_find_state_and_recover(state->inode,
1566 stateid);
0400a6b0 1567 nfs4_schedule_stateid_recovery(server, state);
168667c4
TM
1568 case -EKEYEXPIRED:
1569 /*
1570 * User RPCSEC_GSS context has expired.
1571 * We cannot recover this stateid now, so
1572 * skip it and allow recovery thread to
1573 * proceed.
1574 */
965b5d67
TM
1575 case -ENOMEM:
1576 err = 0;
1577 goto out;
1da177e4
LT
1578 }
1579 err = nfs4_handle_exception(server, err, &exception);
1580 } while (exception.retry);
965b5d67 1581out:
1da177e4
LT
1582 return err;
1583}
1584
cdd4e68b
TM
1585static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1586{
1587 struct nfs4_opendata *data = calldata;
1588
1589 data->rpc_status = task->tk_status;
26e976a8 1590 if (data->rpc_status == 0) {
f597c537 1591 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
bb22629e 1592 nfs_confirm_seqid(&data->owner->so_seqid, 0);
26e976a8 1593 renew_lease(data->o_res.server, data->timestamp);
3e309914 1594 data->rpc_done = 1;
26e976a8 1595 }
cdd4e68b
TM
1596}
1597
1598static void nfs4_open_confirm_release(void *calldata)
1599{
1600 struct nfs4_opendata *data = calldata;
1601 struct nfs4_state *state = NULL;
1602
1603 /* If this request hasn't been cancelled, do nothing */
1604 if (data->cancelled == 0)
1605 goto out_free;
1606 /* In case of error, no cleanup! */
3e309914 1607 if (!data->rpc_done)
cdd4e68b 1608 goto out_free;
cdd4e68b 1609 state = nfs4_opendata_to_nfs4_state(data);
1b370bc2 1610 if (!IS_ERR(state))
643168c2 1611 nfs4_close_state(state, data->o_arg.fmode);
cdd4e68b 1612out_free:
c6d00e63 1613 nfs4_opendata_put(data);
cdd4e68b
TM
1614}
1615
1616static const struct rpc_call_ops nfs4_open_confirm_ops = {
cdd4e68b
TM
1617 .rpc_call_done = nfs4_open_confirm_done,
1618 .rpc_release = nfs4_open_confirm_release,
1619};
1620
1621/*
1622 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1623 */
1624static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1625{
1626 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1627 struct rpc_task *task;
5138fde0
TM
1628 struct rpc_message msg = {
1629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1630 .rpc_argp = &data->c_arg,
1631 .rpc_resp = &data->c_res,
1632 .rpc_cred = data->owner->so_cred,
1633 };
c970aa85
TM
1634 struct rpc_task_setup task_setup_data = {
1635 .rpc_client = server->client,
5138fde0 1636 .rpc_message = &msg,
c970aa85
TM
1637 .callback_ops = &nfs4_open_confirm_ops,
1638 .callback_data = data,
101070ca 1639 .workqueue = nfsiod_workqueue,
c970aa85
TM
1640 .flags = RPC_TASK_ASYNC,
1641 };
1da177e4
LT
1642 int status;
1643
c6d00e63 1644 kref_get(&data->kref);
3e309914
TM
1645 data->rpc_done = 0;
1646 data->rpc_status = 0;
5138fde0 1647 data->timestamp = jiffies;
c970aa85 1648 task = rpc_run_task(&task_setup_data);
7a1218a2 1649 if (IS_ERR(task))
cdd4e68b 1650 return PTR_ERR(task);
cdd4e68b
TM
1651 status = nfs4_wait_for_completion_rpc_task(task);
1652 if (status != 0) {
1653 data->cancelled = 1;
1654 smp_wmb();
1655 } else
1656 status = data->rpc_status;
e6b3c4db 1657 rpc_put_task(task);
1da177e4
LT
1658 return status;
1659}
1660
24ac23ab 1661static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1da177e4 1662{
24ac23ab
TM
1663 struct nfs4_opendata *data = calldata;
1664 struct nfs4_state_owner *sp = data->owner;
5138fde0 1665
24ac23ab
TM
1666 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1667 return;
aac00a8d
TM
1668 /*
1669 * Check if we still need to send an OPEN call, or if we can use
1670 * a delegation instead.
1671 */
1672 if (data->state != NULL) {
1673 struct nfs_delegation *delegation;
1674
dc0b027d 1675 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
6ee41268 1676 goto out_no_action;
aac00a8d
TM
1677 rcu_read_lock();
1678 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
652f89f6
TM
1679 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1680 can_open_delegated(delegation, data->o_arg.fmode))
1681 goto unlock_no_action;
aac00a8d
TM
1682 rcu_read_unlock();
1683 }
95b72eb0 1684 /* Update client id. */
1f0e890d 1685 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
6f220ed5 1686 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
5138fde0 1687 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
e23008ec 1688 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
6f220ed5
TM
1689 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1690 }
26e976a8 1691 data->timestamp = jiffies;
035168ab 1692 if (nfs4_setup_sequence(data->o_arg.server,
d898528c 1693 &data->o_arg.seq_args,
2240a9e2
TM
1694 &data->o_res.seq_res,
1695 task) != 0)
1696 nfs_release_seqid(data->o_arg.seqid);
1697 else
1698 rpc_call_start(task);
6ee41268 1699 return;
652f89f6
TM
1700unlock_no_action:
1701 rcu_read_unlock();
6ee41268
TM
1702out_no_action:
1703 task->tk_action = NULL;
1704
24ac23ab 1705}
1da177e4 1706
b257957e
AB
1707static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1708{
1709 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1710 nfs4_open_prepare(task, calldata);
1711}
1712
24ac23ab
TM
1713static void nfs4_open_done(struct rpc_task *task, void *calldata)
1714{
1715 struct nfs4_opendata *data = calldata;
1da177e4 1716
24ac23ab 1717 data->rpc_status = task->tk_status;
d898528c 1718
14516c3a
TM
1719 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1720 return;
d898528c 1721
24ac23ab 1722 if (task->tk_status == 0) {
807d66d8
TM
1723 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1724 switch (data->o_res.f_attr->mode & S_IFMT) {
6f926b5b
TM
1725 case S_IFREG:
1726 break;
1727 case S_IFLNK:
24ac23ab 1728 data->rpc_status = -ELOOP;
6f926b5b
TM
1729 break;
1730 case S_IFDIR:
24ac23ab 1731 data->rpc_status = -EISDIR;
6f926b5b
TM
1732 break;
1733 default:
24ac23ab 1734 data->rpc_status = -ENOTDIR;
807d66d8 1735 }
6f926b5b 1736 }
26e976a8 1737 renew_lease(data->o_res.server, data->timestamp);
0f9f95e0
TM
1738 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1739 nfs_confirm_seqid(&data->owner->so_seqid, 0);
6f926b5b 1740 }
3e309914 1741 data->rpc_done = 1;
24ac23ab 1742}
6f926b5b 1743
24ac23ab
TM
1744static void nfs4_open_release(void *calldata)
1745{
1746 struct nfs4_opendata *data = calldata;
1747 struct nfs4_state *state = NULL;
1748
1749 /* If this request hasn't been cancelled, do nothing */
1750 if (data->cancelled == 0)
1751 goto out_free;
1752 /* In case of error, no cleanup! */
3e309914 1753 if (data->rpc_status != 0 || !data->rpc_done)
24ac23ab
TM
1754 goto out_free;
1755 /* In case we need an open_confirm, no cleanup! */
1756 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1757 goto out_free;
24ac23ab 1758 state = nfs4_opendata_to_nfs4_state(data);
1b370bc2 1759 if (!IS_ERR(state))
643168c2 1760 nfs4_close_state(state, data->o_arg.fmode);
24ac23ab 1761out_free:
c6d00e63 1762 nfs4_opendata_put(data);
24ac23ab
TM
1763}
1764
1765static const struct rpc_call_ops nfs4_open_ops = {
1766 .rpc_call_prepare = nfs4_open_prepare,
1767 .rpc_call_done = nfs4_open_done,
1768 .rpc_release = nfs4_open_release,
1769};
1770
b257957e
AB
1771static const struct rpc_call_ops nfs4_recover_open_ops = {
1772 .rpc_call_prepare = nfs4_recover_open_prepare,
1773 .rpc_call_done = nfs4_open_done,
1774 .rpc_release = nfs4_open_release,
1775};
1776
1777static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
24ac23ab
TM
1778{
1779 struct inode *dir = data->dir->d_inode;
1780 struct nfs_server *server = NFS_SERVER(dir);
1781 struct nfs_openargs *o_arg = &data->o_arg;
1782 struct nfs_openres *o_res = &data->o_res;
1783 struct rpc_task *task;
5138fde0
TM
1784 struct rpc_message msg = {
1785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1786 .rpc_argp = o_arg,
1787 .rpc_resp = o_res,
1788 .rpc_cred = data->owner->so_cred,
1789 };
c970aa85
TM
1790 struct rpc_task_setup task_setup_data = {
1791 .rpc_client = server->client,
5138fde0 1792 .rpc_message = &msg,
c970aa85
TM
1793 .callback_ops = &nfs4_open_ops,
1794 .callback_data = data,
101070ca 1795 .workqueue = nfsiod_workqueue,
c970aa85
TM
1796 .flags = RPC_TASK_ASYNC,
1797 };
24ac23ab
TM
1798 int status;
1799
9d12b216 1800 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
c6d00e63 1801 kref_get(&data->kref);
3e309914
TM
1802 data->rpc_done = 0;
1803 data->rpc_status = 0;
2ced46c2 1804 data->cancelled = 0;
b257957e
AB
1805 if (isrecover)
1806 task_setup_data.callback_ops = &nfs4_recover_open_ops;
c970aa85 1807 task = rpc_run_task(&task_setup_data);
b257957e
AB
1808 if (IS_ERR(task))
1809 return PTR_ERR(task);
1810 status = nfs4_wait_for_completion_rpc_task(task);
1811 if (status != 0) {
1812 data->cancelled = 1;
1813 smp_wmb();
1814 } else
1815 status = data->rpc_status;
1816 rpc_put_task(task);
1817
1818 return status;
1819}
1820
1821static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1822{
1823 struct inode *dir = data->dir->d_inode;
1824 struct nfs_openres *o_res = &data->o_res;
1825 int status;
1826
1827 status = nfs4_run_open_task(data, 1);
1828 if (status != 0 || !data->rpc_done)
1829 return status;
1830
6926afd1
TM
1831 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1832
b257957e
AB
1833 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1834 status = _nfs4_proc_open_confirm(data);
1835 if (status != 0)
1836 return status;
1837 }
1838
1839 return status;
1840}
1841
6168f62c
WAA
1842static int nfs4_opendata_access(struct rpc_cred *cred,
1843 struct nfs4_opendata *opendata,
1844 struct nfs4_state *state, fmode_t fmode)
1845{
1846 struct nfs_access_entry cache;
1847 u32 mask;
1848
1849 /* access call failed or for some reason the server doesn't
1850 * support any access modes -- defer access call until later */
1851 if (opendata->o_res.access_supported == 0)
1852 return 0;
1853
1854 mask = 0;
bbd3a8ee
WAA
1855 /* don't check MAY_WRITE - a newly created file may not have
1856 * write mode bits, but POSIX allows the creating process to write */
6168f62c
WAA
1857 if (fmode & FMODE_READ)
1858 mask |= MAY_READ;
6168f62c
WAA
1859 if (fmode & FMODE_EXEC)
1860 mask |= MAY_EXEC;
1861
1862 cache.cred = cred;
1863 cache.jiffies = jiffies;
1864 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1865 nfs_access_add_cache(state->inode, &cache);
1866
bbd3a8ee 1867 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
6168f62c
WAA
1868 return 0;
1869
1870 /* even though OPEN succeeded, access is denied. Close the file */
1871 nfs4_close_state(state, fmode);
998f40b5 1872 return -EACCES;
6168f62c
WAA
1873}
1874
b257957e
AB
1875/*
1876 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1877 */
1878static int _nfs4_proc_open(struct nfs4_opendata *data)
1879{
1880 struct inode *dir = data->dir->d_inode;
1881 struct nfs_server *server = NFS_SERVER(dir);
1882 struct nfs_openargs *o_arg = &data->o_arg;
1883 struct nfs_openres *o_res = &data->o_res;
1884 int status;
1885
1886 status = nfs4_run_open_task(data, 0);
08ef7bd3
TM
1887 if (!data->rpc_done)
1888 return status;
1889 if (status != 0) {
1890 if (status == -NFS4ERR_BADNAME &&
1891 !(o_arg->open_flags & O_CREAT))
1892 return -ENOENT;
24ac23ab 1893 return status;
08ef7bd3 1894 }
24ac23ab 1895
6926afd1
TM
1896 nfs_fattr_map_and_free_names(server, &data->f_attr);
1897
90ff0c54 1898 if (o_arg->open_flags & O_CREAT)
56ae19f3 1899 update_changeattr(dir, &o_res->cinfo);
0df5dd4a
TM
1900 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1901 server->caps &= ~NFS_CAP_POSIX_LOCK;
1da177e4 1902 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
cdd4e68b 1903 status = _nfs4_proc_open_confirm(data);
1da177e4 1904 if (status != 0)
24ac23ab 1905 return status;
1da177e4
LT
1906 }
1907 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
9936781d 1908 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
24ac23ab 1909 return 0;
1da177e4
LT
1910}
1911
d83217c1 1912static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
58d9714a 1913{
a78cb57a 1914 unsigned int loop;
6b30954e 1915 int ret;
58d9714a 1916
a78cb57a 1917 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
65de872e 1918 ret = nfs4_wait_clnt_recover(clp);
6b30954e 1919 if (ret != 0)
a78cb57a 1920 break;
e598d843
TM
1921 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1922 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
6b30954e 1923 break;
0400a6b0 1924 nfs4_schedule_state_manager(clp);
a78cb57a 1925 ret = -EIO;
6b30954e 1926 }
a78cb57a 1927 return ret;
58d9714a
TM
1928}
1929
d83217c1
AA
1930static int nfs4_recover_expired_lease(struct nfs_server *server)
1931{
1932 return nfs4_client_recover_expired_lease(server->nfs_client);
1933}
1934
1da177e4
LT
1935/*
1936 * OPEN_EXPIRED:
1937 * reclaim state on the server after a network partition.
1938 * Assumes caller holds the appropriate lock
1939 */
539cd03a 1940static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1da177e4 1941{
e56e0b78 1942 struct nfs4_opendata *opendata;
864472e9 1943 int ret;
1da177e4 1944
6f220ed5
TM
1945 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1946 if (IS_ERR(opendata))
1947 return PTR_ERR(opendata);
864472e9 1948 ret = nfs4_open_recover(opendata, state);
35d05778 1949 if (ret == -ESTALE)
3d4ff43d 1950 d_drop(ctx->dentry);
c6d00e63 1951 nfs4_opendata_put(opendata);
864472e9 1952 return ret;
1da177e4
LT
1953}
1954
a9ed2e25 1955static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
202b50dc 1956{
539cd03a 1957 struct nfs_server *server = NFS_SERVER(state->inode);
202b50dc
TM
1958 struct nfs4_exception exception = { };
1959 int err;
1960
1961 do {
539cd03a 1962 err = _nfs4_open_expired(ctx, state);
a9ed2e25
TM
1963 switch (err) {
1964 default:
1965 goto out;
1966 case -NFS4ERR_GRACE:
1967 case -NFS4ERR_DELAY:
1968 nfs4_handle_exception(server, err, &exception);
1969 err = 0;
1970 }
202b50dc 1971 } while (exception.retry);
a9ed2e25 1972out:
202b50dc
TM
1973 return err;
1974}
1975
1da177e4
LT
1976static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1977{
1da177e4 1978 struct nfs_open_context *ctx;
864472e9 1979 int ret;
1da177e4 1980
864472e9
TM
1981 ctx = nfs4_state_find_open_context(state);
1982 if (IS_ERR(ctx))
1983 return PTR_ERR(ctx);
539cd03a 1984 ret = nfs4_do_open_expired(ctx, state);
864472e9
TM
1985 put_nfs_open_context(ctx);
1986 return ret;
1da177e4
LT
1987}
1988
f062eb6c 1989#if defined(CONFIG_NFS_V4_1)
3e60ffdd 1990static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
f062eb6c 1991{
f062eb6c 1992 struct nfs_server *server = NFS_SERVER(state->inode);
3e60ffdd
CL
1993 nfs4_stateid *stateid = &state->stateid;
1994 int status;
1995
1996 /* If a state reset has been done, test_stateid is unneeded */
1997 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1998 return;
1999
2000 status = nfs41_test_stateid(server, stateid);
2001 if (status != NFS_OK) {
2002 /* Free the stateid unless the server explicitly
2003 * informs us the stateid is unrecognized. */
2004 if (status != -NFS4ERR_BAD_STATEID)
2005 nfs41_free_stateid(server, stateid);
6938867e 2006 nfs_remove_bad_delegation(state->inode);
3e60ffdd 2007
6938867e
BS
2008 write_seqlock(&state->seqlock);
2009 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2010 write_sequnlock(&state->seqlock);
3e60ffdd
CL
2011 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2012 }
2013}
2014
2015/**
2016 * nfs41_check_open_stateid - possibly free an open stateid
2017 *
2018 * @state: NFSv4 state for an inode
2019 *
2020 * Returns NFS_OK if recovery for this stateid is now finished.
2021 * Otherwise a negative NFS4ERR value is returned.
2022 */
2023static int nfs41_check_open_stateid(struct nfs4_state *state)
2024{
2025 struct nfs_server *server = NFS_SERVER(state->inode);
fcb6d9c6 2026 nfs4_stateid *stateid = &state->open_stateid;
3e60ffdd
CL
2027 int status;
2028
2029 /* If a state reset has been done, test_stateid is unneeded */
2030 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2031 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2032 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2033 return -NFS4ERR_BAD_STATEID;
2034
2035 status = nfs41_test_stateid(server, stateid);
2036 if (status != NFS_OK) {
2037 /* Free the stateid unless the server explicitly
2038 * informs us the stateid is unrecognized. */
2039 if (status != -NFS4ERR_BAD_STATEID)
2040 nfs41_free_stateid(server, stateid);
2041
2042 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2043 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2044 clear_bit(NFS_O_RDWR_STATE, &state->flags);
b01dd1d8
BS
2045 }
2046 return status;
2047}
2048
2049static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2050{
eb64cf96 2051 int status;
b01dd1d8 2052
3e60ffdd
CL
2053 nfs41_clear_delegation_stateid(state);
2054 status = nfs41_check_open_stateid(state);
eb64cf96
CL
2055 if (status != NFS_OK)
2056 status = nfs4_open_expired(sp, state);
2057 return status;
f062eb6c
BS
2058}
2059#endif
2060
aa53ed54
JL
2061/*
2062 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2063 * fields corresponding to attributes that were used to store the verifier.
2064 * Make sure we clobber those fields in the later setattr call
2065 */
2066static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2067{
2068 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2069 !(sattr->ia_valid & ATTR_ATIME_SET))
2070 sattr->ia_valid |= ATTR_ATIME;
2071
2072 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2073 !(sattr->ia_valid & ATTR_MTIME_SET))
2074 sattr->ia_valid |= ATTR_MTIME;
2075}
2076
1da177e4 2077/*
24ac23ab 2078 * Returns a referenced nfs4_state
1da177e4 2079 */
82be417a
AA
2080static int _nfs4_do_open(struct inode *dir,
2081 struct dentry *dentry,
2082 fmode_t fmode,
2083 int flags,
2084 struct iattr *sattr,
2085 struct rpc_cred *cred,
2086 struct nfs4_state **res,
2087 struct nfs4_threshold **ctx_th)
1da177e4
LT
2088{
2089 struct nfs4_state_owner *sp;
2090 struct nfs4_state *state = NULL;
2091 struct nfs_server *server = NFS_SERVER(dir);
e56e0b78 2092 struct nfs4_opendata *opendata;
aac00a8d 2093 int status;
1da177e4
LT
2094
2095 /* Protect against reboot recovery conflicts */
1da177e4 2096 status = -ENOMEM;
d1e284d5
TM
2097 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2098 if (sp == NULL) {
1da177e4
LT
2099 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2100 goto out_err;
2101 }
58d9714a
TM
2102 status = nfs4_recover_expired_lease(server);
2103 if (status != 0)
b4454fe1 2104 goto err_put_state_owner;
82a2c1b7
AV
2105 if (dentry->d_inode != NULL)
2106 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
58d9714a 2107 status = -ENOMEM;
82a2c1b7 2108 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
e56e0b78 2109 if (opendata == NULL)
95d35cb4 2110 goto err_put_state_owner;
1da177e4 2111
82be417a
AA
2112 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2113 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2114 if (!opendata->f_attr.mdsthreshold)
2115 goto err_opendata_put;
1549210f 2116 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
82be417a 2117 }
82a2c1b7
AV
2118 if (dentry->d_inode != NULL)
2119 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
aac00a8d 2120
24ac23ab 2121 status = _nfs4_proc_open(opendata);
1da177e4 2122 if (status != 0)
c6d00e63 2123 goto err_opendata_put;
1da177e4 2124
24ac23ab 2125 state = nfs4_opendata_to_nfs4_state(opendata);
1b370bc2
TM
2126 status = PTR_ERR(state);
2127 if (IS_ERR(state))
c6d00e63 2128 goto err_opendata_put;
0df5dd4a 2129 if (server->caps & NFS_CAP_POSIX_LOCK)
8e469ebd 2130 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
0ab64e0e 2131
6168f62c
WAA
2132 status = nfs4_opendata_access(cred, opendata, state, fmode);
2133 if (status != 0)
2134 goto err_opendata_put;
2135
0ab64e0e
TM
2136 if (opendata->o_arg.open_flags & O_EXCL) {
2137 nfs4_exclusive_attrset(opendata, sattr);
2138
2139 nfs_fattr_init(opendata->o_res.f_attr);
2140 status = nfs4_do_setattr(state->inode, cred,
2141 opendata->o_res.f_attr, sattr,
2142 state);
2143 if (status == 0)
2144 nfs_setattr_update_inode(state->inode, sattr);
2145 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2146 }
82be417a
AA
2147
2148 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2149 *ctx_th = opendata->f_attr.mdsthreshold;
2150 else
2151 kfree(opendata->f_attr.mdsthreshold);
2152 opendata->f_attr.mdsthreshold = NULL;
2153
c6d00e63 2154 nfs4_opendata_put(opendata);
1da177e4 2155 nfs4_put_state_owner(sp);
1da177e4
LT
2156 *res = state;
2157 return 0;
c6d00e63 2158err_opendata_put:
82be417a 2159 kfree(opendata->f_attr.mdsthreshold);
c6d00e63 2160 nfs4_opendata_put(opendata);
e56e0b78
TM
2161err_put_state_owner:
2162 nfs4_put_state_owner(sp);
1da177e4 2163out_err:
1da177e4
LT
2164 *res = NULL;
2165 return status;
2166}
2167
2168
82be417a
AA
2169static struct nfs4_state *nfs4_do_open(struct inode *dir,
2170 struct dentry *dentry,
2171 fmode_t fmode,
2172 int flags,
2173 struct iattr *sattr,
2174 struct rpc_cred *cred,
2175 struct nfs4_threshold **ctx_th)
1da177e4
LT
2176{
2177 struct nfs4_exception exception = { };
2178 struct nfs4_state *res;
2179 int status;
2180
6168f62c 2181 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
1da177e4 2182 do {
82be417a
AA
2183 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2184 &res, ctx_th);
1da177e4
LT
2185 if (status == 0)
2186 break;
2187 /* NOTE: BAD_SEQID means the server and client disagree about the
2188 * book-keeping w.r.t. state-changing operations
2189 * (OPEN/CLOSE/LOCK/LOCKU...)
2190 * It is actually a sign of a bug on the client or on the server.
2191 *
2192 * If we receive a BAD_SEQID error in the particular case of
cee54fc9 2193 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1da177e4
LT
2194 * have unhashed the old state_owner for us, and that we can
2195 * therefore safely retry using a new one. We should still warn
2196 * the user though...
2197 */
2198 if (status == -NFS4ERR_BAD_SEQID) {
9a3ba432 2199 pr_warn_ratelimited("NFS: v4 server %s "
6f43ddcc
TM
2200 " returned a bad sequence-id error!\n",
2201 NFS_SERVER(dir)->nfs_client->cl_hostname);
1da177e4
LT
2202 exception.retry = 1;
2203 continue;
2204 }
550f5747
TM
2205 /*
2206 * BAD_STATEID on OPEN means that the server cancelled our
2207 * state before it received the OPEN_CONFIRM.
2208 * Recover by retrying the request as per the discussion
2209 * on Page 181 of RFC3530.
2210 */
2211 if (status == -NFS4ERR_BAD_STATEID) {
2212 exception.retry = 1;
2213 continue;
2214 }
aac00a8d
TM
2215 if (status == -EAGAIN) {
2216 /* We must have found a delegation */
2217 exception.retry = 1;
2218 continue;
2219 }
1da177e4
LT
2220 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2221 status, &exception));
2222 } while (exception.retry);
2223 return res;
2224}
2225
659bfcd6
TM
2226static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2227 struct nfs_fattr *fattr, struct iattr *sattr,
2228 struct nfs4_state *state)
1da177e4 2229{
3e4f6290 2230 struct nfs_server *server = NFS_SERVER(inode);
1da177e4 2231 struct nfs_setattrargs arg = {
3e4f6290 2232 .fh = NFS_FH(inode),
1da177e4
LT
2233 .iap = sattr,
2234 .server = server,
2235 .bitmask = server->attr_bitmask,
2236 };
2237 struct nfs_setattrres res = {
2238 .fattr = fattr,
2239 .server = server,
2240 };
2241 struct rpc_message msg = {
659bfcd6
TM
2242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2243 .rpc_argp = &arg,
2244 .rpc_resp = &res,
2245 .rpc_cred = cred,
1da177e4 2246 };
26e976a8 2247 unsigned long timestamp = jiffies;
65e4308d 2248 int status;
1da177e4 2249
0e574af1 2250 nfs_fattr_init(fattr);
1da177e4 2251
4fc8796d 2252 if (state != NULL) {
2a369153
TM
2253 struct nfs_lockowner lockowner = {
2254 .l_owner = current->files,
2255 .l_pid = current->tgid,
2256 };
4fc8796d 2257 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2a369153 2258 &lockowner);
4fc8796d
TM
2259 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2260 FMODE_WRITE)) {
3e4f6290 2261 /* Use that stateid */
08e9eac4 2262 } else
f597c537 2263 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
1da177e4 2264
7c513058 2265 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
26e976a8
TM
2266 if (status == 0 && state != NULL)
2267 renew_lease(server, timestamp);
65e4308d 2268 return status;
1da177e4
LT
2269}
2270
659bfcd6
TM
2271static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2272 struct nfs_fattr *fattr, struct iattr *sattr,
2273 struct nfs4_state *state)
1da177e4 2274{
3e4f6290 2275 struct nfs_server *server = NFS_SERVER(inode);
a1d0b5ee
TM
2276 struct nfs4_exception exception = {
2277 .state = state,
3114ea7a 2278 .inode = inode,
a1d0b5ee 2279 };
1da177e4
LT
2280 int err;
2281 do {
451146be
TM
2282 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2283 switch (err) {
2284 case -NFS4ERR_OPENMODE:
2285 if (state && !(state->state & FMODE_WRITE)) {
2286 err = -EBADF;
2287 if (sattr->ia_valid & ATTR_OPEN)
2288 err = -EACCES;
2289 goto out;
2290 }
2291 }
2292 err = nfs4_handle_exception(server, err, &exception);
1da177e4 2293 } while (exception.retry);
451146be 2294out:
1da177e4
LT
2295 return err;
2296}
2297
2298struct nfs4_closedata {
2299 struct inode *inode;
2300 struct nfs4_state *state;
2301 struct nfs_closeargs arg;
2302 struct nfs_closeres res;
516a6af6 2303 struct nfs_fattr fattr;
26e976a8 2304 unsigned long timestamp;
f7e8917a
FI
2305 bool roc;
2306 u32 roc_barrier;
1da177e4
LT
2307};
2308
963d8fe5 2309static void nfs4_free_closedata(void *data)
9512135d 2310{
963d8fe5
TM
2311 struct nfs4_closedata *calldata = data;
2312 struct nfs4_state_owner *sp = calldata->state->owner;
643168c2 2313 struct super_block *sb = calldata->state->inode->i_sb;
9512135d 2314
f7e8917a
FI
2315 if (calldata->roc)
2316 pnfs_roc_release(calldata->state->inode);
9512135d
TM
2317 nfs4_put_open_state(calldata->state);
2318 nfs_free_seqid(calldata->arg.seqid);
9512135d 2319 nfs4_put_state_owner(sp);
324d003b 2320 nfs_sb_deactive_async(sb);
9512135d
TM
2321 kfree(calldata);
2322}
2323
88069f77
TM
2324static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2325 fmode_t fmode)
2326{
2327 spin_lock(&state->owner->so_lock);
2328 if (!(fmode & FMODE_READ))
2329 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2330 if (!(fmode & FMODE_WRITE))
2331 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2332 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2333 spin_unlock(&state->owner->so_lock);
2334}
2335
963d8fe5 2336static void nfs4_close_done(struct rpc_task *task, void *data)
1da177e4 2337{
963d8fe5 2338 struct nfs4_closedata *calldata = data;
1da177e4 2339 struct nfs4_state *state = calldata->state;
1da177e4
LT
2340 struct nfs_server *server = NFS_SERVER(calldata->inode);
2341
a3ca5651 2342 dprintk("%s: begin!\n", __func__);
14516c3a
TM
2343 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2344 return;
1da177e4
LT
2345 /* hmm. we are done with the inode, and in the process of freeing
2346 * the state_owner. we keep this around to process errors
2347 */
1da177e4
LT
2348 switch (task->tk_status) {
2349 case 0:
f7e8917a
FI
2350 if (calldata->roc)
2351 pnfs_roc_set_barrier(state->inode,
2352 calldata->roc_barrier);
45328c35 2353 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
26e976a8 2354 renew_lease(server, calldata->timestamp);
88069f77
TM
2355 nfs4_close_clear_stateid_flags(state,
2356 calldata->arg.fmode);
1da177e4
LT
2357 break;
2358 case -NFS4ERR_STALE_STATEID:
9e33bed5
TM
2359 case -NFS4ERR_OLD_STATEID:
2360 case -NFS4ERR_BAD_STATEID:
1da177e4 2361 case -NFS4ERR_EXPIRED:
dc0b027d 2362 if (calldata->arg.fmode == 0)
9e33bed5 2363 break;
1da177e4 2364 default:
72211dbe
TM
2365 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2366 rpc_restart_call_prepare(task);
1da177e4 2367 }
72211dbe 2368 nfs_release_seqid(calldata->arg.seqid);
516a6af6 2369 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
a3ca5651 2370 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
1da177e4
LT
2371}
2372
4ce70ada 2373static void nfs4_close_prepare(struct rpc_task *task, void *data)
1da177e4 2374{
4ce70ada 2375 struct nfs4_closedata *calldata = data;
9512135d 2376 struct nfs4_state *state = calldata->state;
7fdab069 2377 struct inode *inode = calldata->inode;
88069f77 2378 int call_close = 0;
9512135d 2379
a3ca5651 2380 dprintk("%s: begin!\n", __func__);
963d8fe5 2381 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
9512135d 2382 return;
003707c7 2383
88069f77
TM
2384 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2385 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
4cecb76f 2386 spin_lock(&state->owner->so_lock);
003707c7 2387 /* Calculate the change in open mode */
e7616923 2388 if (state->n_rdwr == 0) {
003707c7 2389 if (state->n_rdonly == 0) {
88069f77
TM
2390 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2391 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2392 calldata->arg.fmode &= ~FMODE_READ;
003707c7
TM
2393 }
2394 if (state->n_wronly == 0) {
88069f77
TM
2395 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2396 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2397 calldata->arg.fmode &= ~FMODE_WRITE;
003707c7 2398 }
e7616923 2399 }
4cecb76f 2400 spin_unlock(&state->owner->so_lock);
88069f77
TM
2401
2402 if (!call_close) {
963d8fe5
TM
2403 /* Note: exit _without_ calling nfs4_close_done */
2404 task->tk_action = NULL;
a3ca5651 2405 goto out;
9512135d 2406 }
88069f77 2407
f7e8917a 2408 if (calldata->arg.fmode == 0) {
88069f77 2409 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
f7e8917a 2410 if (calldata->roc &&
7fdab069 2411 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
a3ca5651 2412 goto out;
f7e8917a 2413 }
88069f77 2414
516a6af6 2415 nfs_fattr_init(calldata->res.fattr);
26e976a8 2416 calldata->timestamp = jiffies;
7fdab069 2417 if (nfs4_setup_sequence(NFS_SERVER(inode),
9d12b216
TM
2418 &calldata->arg.seq_args,
2419 &calldata->res.seq_res,
2240a9e2
TM
2420 task) != 0)
2421 nfs_release_seqid(calldata->arg.seqid);
2422 else
2423 rpc_call_start(task);
a3ca5651
CL
2424out:
2425 dprintk("%s: done!\n", __func__);
1da177e4
LT
2426}
2427
963d8fe5 2428static const struct rpc_call_ops nfs4_close_ops = {
4ce70ada 2429 .rpc_call_prepare = nfs4_close_prepare,
963d8fe5
TM
2430 .rpc_call_done = nfs4_close_done,
2431 .rpc_release = nfs4_free_closedata,
2432};
2433
1da177e4
LT
2434/*
2435 * It is possible for data to be read/written from a mem-mapped file
2436 * after the sys_close call (which hits the vfs layer as a flush).
2437 * This means that we can't safely call nfsv4 close on a file until
2438 * the inode is cleared. This in turn means that we are not good
2439 * NFSv4 citizens - we do not indicate to the server to update the file's
2440 * share state even when we are done with one of the three share
2441 * stateid's in the inode.
2442 *
2443 * NOTE: Caller must be holding the sp->so_owner semaphore!
2444 */
1f7977c1 2445int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
1da177e4 2446{
4a35bd41 2447 struct nfs_server *server = NFS_SERVER(state->inode);
1da177e4 2448 struct nfs4_closedata *calldata;
b39e625b
TM
2449 struct nfs4_state_owner *sp = state->owner;
2450 struct rpc_task *task;
5138fde0
TM
2451 struct rpc_message msg = {
2452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2453 .rpc_cred = state->owner->so_cred,
2454 };
c970aa85
TM
2455 struct rpc_task_setup task_setup_data = {
2456 .rpc_client = server->client,
5138fde0 2457 .rpc_message = &msg,
c970aa85 2458 .callback_ops = &nfs4_close_ops,
101070ca 2459 .workqueue = nfsiod_workqueue,
c970aa85
TM
2460 .flags = RPC_TASK_ASYNC,
2461 };
9512135d 2462 int status = -ENOMEM;
1da177e4 2463
8535b2be 2464 calldata = kzalloc(sizeof(*calldata), gfp_mask);
1da177e4 2465 if (calldata == NULL)
9512135d 2466 goto out;
9d12b216 2467 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
4a35bd41 2468 calldata->inode = state->inode;
1da177e4 2469 calldata->state = state;
4a35bd41 2470 calldata->arg.fh = NFS_FH(state->inode);
003707c7 2471 calldata->arg.stateid = &state->open_stateid;
1da177e4 2472 /* Serialization for the sequence id */
8535b2be 2473 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
9512135d
TM
2474 if (calldata->arg.seqid == NULL)
2475 goto out_free_calldata;
dc0b027d 2476 calldata->arg.fmode = 0;
a65318bf 2477 calldata->arg.bitmask = server->cache_consistency_bitmask;
516a6af6 2478 calldata->res.fattr = &calldata->fattr;
c1d51931 2479 calldata->res.seqid = calldata->arg.seqid;
516a6af6 2480 calldata->res.server = server;
1f7977c1 2481 calldata->roc = pnfs_roc(state->inode);
643168c2 2482 nfs_sb_active(calldata->inode->i_sb);
9512135d 2483
1174dd1f
TM
2484 msg.rpc_argp = &calldata->arg;
2485 msg.rpc_resp = &calldata->res;
c970aa85
TM
2486 task_setup_data.callback_data = calldata;
2487 task = rpc_run_task(&task_setup_data);
b39e625b
TM
2488 if (IS_ERR(task))
2489 return PTR_ERR(task);
a49c3c77
TM
2490 status = 0;
2491 if (wait)
2492 status = rpc_wait_for_completion_task(task);
b39e625b 2493 rpc_put_task(task);
a49c3c77 2494 return status;
9512135d
TM
2495out_free_calldata:
2496 kfree(calldata);
2497out:
b39e625b
TM
2498 nfs4_put_open_state(state);
2499 nfs4_put_state_owner(sp);
9512135d 2500 return status;
1da177e4
LT
2501}
2502
2b484297 2503static struct inode *
cd9a1c0e 2504nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
1da177e4 2505{
1da177e4 2506 struct nfs4_state *state;
1da177e4 2507
565277f6 2508 /* Protect against concurrent sillydeletes */
82be417a
AA
2509 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2510 ctx->cred, &ctx->mdsthreshold);
f46e0bd3
TM
2511 if (IS_ERR(state))
2512 return ERR_CAST(state);
cd9a1c0e 2513 ctx->state = state;
f46e0bd3 2514 return igrab(state->inode);
1da177e4
LT
2515}
2516
1185a552 2517static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
7fe5c398
TM
2518{
2519 if (ctx->state == NULL)
2520 return;
2521 if (is_sync)
643168c2 2522 nfs4_close_sync(ctx->state, ctx->mode);
7fe5c398 2523 else
643168c2 2524 nfs4_close_state(ctx->state, ctx->mode);
7fe5c398 2525}
1da177e4
LT
2526
2527static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2528{
43652ad5
BH
2529 struct nfs4_server_caps_arg args = {
2530 .fhandle = fhandle,
2531 };
1da177e4
LT
2532 struct nfs4_server_caps_res res = {};
2533 struct rpc_message msg = {
2534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
43652ad5 2535 .rpc_argp = &args,
1da177e4
LT
2536 .rpc_resp = &res,
2537 };
2538 int status;
2539
7c513058 2540 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
2541 if (status == 0) {
2542 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
62ab460c
TM
2543 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2544 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2545 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2546 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2547 NFS_CAP_CTIME|NFS_CAP_MTIME);
1da177e4
LT
2548 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2549 server->caps |= NFS_CAP_ACLS;
2550 if (res.has_links != 0)
2551 server->caps |= NFS_CAP_HARDLINKS;
2552 if (res.has_symlinks != 0)
2553 server->caps |= NFS_CAP_SYMLINKS;
62ab460c
TM
2554 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2555 server->caps |= NFS_CAP_FILEID;
2556 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2557 server->caps |= NFS_CAP_MODE;
2558 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2559 server->caps |= NFS_CAP_NLINK;
2560 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2561 server->caps |= NFS_CAP_OWNER;
2562 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2563 server->caps |= NFS_CAP_OWNER_GROUP;
2564 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2565 server->caps |= NFS_CAP_ATIME;
2566 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2567 server->caps |= NFS_CAP_CTIME;
2568 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2569 server->caps |= NFS_CAP_MTIME;
2570
a65318bf
TM
2571 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2572 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2573 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1da177e4 2574 server->acl_bitmask = res.acl_bitmask;
264e6351 2575 server->fh_expire_type = res.fh_expire_type;
1da177e4 2576 }
cccef3b9 2577
1da177e4
LT
2578 return status;
2579}
2580
55a97593 2581int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1da177e4
LT
2582{
2583 struct nfs4_exception exception = { };
2584 int err;
2585 do {
2586 err = nfs4_handle_exception(server,
2587 _nfs4_server_capabilities(server, fhandle),
2588 &exception);
2589 } while (exception.retry);
2590 return err;
2591}
2592
2593static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2594 struct nfs_fsinfo *info)
2595{
1da177e4
LT
2596 struct nfs4_lookup_root_arg args = {
2597 .bitmask = nfs4_fattr_bitmap,
2598 };
2599 struct nfs4_lookup_res res = {
2600 .server = server,
0e574af1 2601 .fattr = info->fattr,
1da177e4
LT
2602 .fh = fhandle,
2603 };
2604 struct rpc_message msg = {
2605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2606 .rpc_argp = &args,
2607 .rpc_resp = &res,
2608 };
008f55d0 2609
0e574af1 2610 nfs_fattr_init(info->fattr);
7c513058 2611 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
2612}
2613
2614static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2615 struct nfs_fsinfo *info)
2616{
2617 struct nfs4_exception exception = { };
2618 int err;
2619 do {
fb8a5ba8
BS
2620 err = _nfs4_lookup_root(server, fhandle, info);
2621 switch (err) {
2622 case 0:
2623 case -NFS4ERR_WRONGSEC:
05e9cfb4 2624 goto out;
fb8a5ba8
BS
2625 default:
2626 err = nfs4_handle_exception(server, err, &exception);
2627 }
1da177e4 2628 } while (exception.retry);
05e9cfb4 2629out:
1da177e4
LT
2630 return err;
2631}
2632
8f70e95f
BS
2633static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2634 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2635{
2636 struct rpc_auth *auth;
2637 int ret;
2638
2639 auth = rpcauth_create(flavor, server->client);
e8d920c5 2640 if (IS_ERR(auth)) {
8f70e95f
BS
2641 ret = -EIO;
2642 goto out;
2643 }
2644 ret = nfs4_lookup_root(server, fhandle, info);
8f70e95f
BS
2645out:
2646 return ret;
2647}
2648
801a16dc 2649static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
54ceac45 2650 struct nfs_fsinfo *info)
1da177e4 2651{
8f70e95f 2652 int i, len, status = 0;
0fabee24 2653 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
1da177e4 2654
6a1a1e34 2655 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
4ea8fed5
TM
2656 if (len < 0)
2657 return len;
8f70e95f
BS
2658
2659 for (i = 0; i < len; i++) {
6a1a1e34
CL
2660 /* AUTH_UNIX is the default flavor if none was specified,
2661 * thus has already been tried. */
2662 if (flav_array[i] == RPC_AUTH_UNIX)
2663 continue;
2664
8f70e95f 2665 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
fb8a5ba8 2666 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
d1a8016a
BS
2667 continue;
2668 break;
8f70e95f 2669 }
fb8a5ba8
BS
2670 /*
2671 * -EACCESS could mean that the user doesn't have correct permissions
2672 * to access the mount. It could also mean that we tried to mount
2673 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2674 * existing mount programs don't handle -EACCES very well so it should
2675 * be mapped to -EPERM instead.
2676 */
2677 if (status == -EACCES)
2678 status = -EPERM;
801a16dc
BS
2679 return status;
2680}
2681
2682/*
2683 * get the file handle for the "/" directory on the server
2684 */
3028eb2b
BS
2685int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2686 struct nfs_fsinfo *info)
801a16dc 2687{
fca78d6d 2688 int minor_version = server->nfs_client->cl_minorversion;
801a16dc 2689 int status = nfs4_lookup_root(server, fhandle, info);
fb8a5ba8
BS
2690 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2691 /*
2692 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2693 * by nfs4_map_errors() as this function exits.
2694 */
fca78d6d 2695 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
1da177e4
LT
2696 if (status == 0)
2697 status = nfs4_server_capabilities(server, fhandle);
2698 if (status == 0)
2699 status = nfs4_do_fsinfo(server, fhandle, info);
c12e87f4 2700 return nfs4_map_errors(status);
1da177e4
LT
2701}
2702
bae36241
BS
2703static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2704 struct nfs_fsinfo *info)
2705{
2706 int error;
2707 struct nfs_fattr *fattr = info->fattr;
2708
2709 error = nfs4_server_capabilities(server, mntfh);
2710 if (error < 0) {
2711 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2712 return error;
2713 }
2714
2715 error = nfs4_proc_getattr(server, mntfh, fattr);
2716 if (error < 0) {
2717 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2718 return error;
2719 }
2720
2721 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2722 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2723 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2724
2725 return error;
2726}
2727
6b97fd3d
MN
2728/*
2729 * Get locations and (maybe) other attributes of a referral.
2730 * Note that we'll actually follow the referral later when
2731 * we detect fsid mismatch in inode revalidation
2732 */
f05d147f
BS
2733static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2734 const struct qstr *name, struct nfs_fattr *fattr,
2735 struct nfs_fh *fhandle)
6b97fd3d
MN
2736{
2737 int status = -ENOMEM;
2738 struct page *page = NULL;
2739 struct nfs4_fs_locations *locations = NULL;
6b97fd3d
MN
2740
2741 page = alloc_page(GFP_KERNEL);
2742 if (page == NULL)
2743 goto out;
2744 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2745 if (locations == NULL)
2746 goto out;
2747
f05d147f 2748 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
6b97fd3d
MN
2749 if (status != 0)
2750 goto out;
2751 /* Make sure server returned a different fsid for the referral */
2752 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
533eb461
AA
2753 dprintk("%s: server did not return a different fsid for"
2754 " a referral at %s\n", __func__, name->name);
6b97fd3d
MN
2755 status = -EIO;
2756 goto out;
2757 }
533eb461
AA
2758 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2759 nfs_fixup_referral_attributes(&locations->fattr);
6b97fd3d 2760
533eb461 2761 /* replace the lookup nfs_fattr with the locations nfs_fattr */
6b97fd3d 2762 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
6b97fd3d
MN
2763 memset(fhandle, 0, sizeof(struct nfs_fh));
2764out:
2765 if (page)
2766 __free_page(page);
5d7ca35a 2767 kfree(locations);
6b97fd3d
MN
2768 return status;
2769}
2770
1da177e4
LT
2771static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2772{
2773 struct nfs4_getattr_arg args = {
2774 .fh = fhandle,
2775 .bitmask = server->attr_bitmask,
2776 };
2777 struct nfs4_getattr_res res = {
2778 .fattr = fattr,
2779 .server = server,
2780 };
2781 struct rpc_message msg = {
2782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2783 .rpc_argp = &args,
2784 .rpc_resp = &res,
2785 };
2786
0e574af1 2787 nfs_fattr_init(fattr);
7c513058 2788 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
2789}
2790
2791static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2792{
2793 struct nfs4_exception exception = { };
2794 int err;
2795 do {
2796 err = nfs4_handle_exception(server,
2797 _nfs4_proc_getattr(server, fhandle, fattr),
2798 &exception);
2799 } while (exception.retry);
2800 return err;
2801}
2802
2803/*
2804 * The file is not closed if it is opened due to the a request to change
2805 * the size of the file. The open call will not be needed once the
2806 * VFS layer lookup-intents are implemented.
2807 *
2808 * Close is called when the inode is destroyed.
2809 * If we haven't opened the file for O_WRONLY, we
2810 * need to in the size_change case to obtain a stateid.
2811 *
2812 * Got race?
2813 * Because OPEN is always done by name in nfsv4, it is
2814 * possible that we opened a different file by the same
2815 * name. We can recognize this race condition, but we
2816 * can't do anything about it besides returning an error.
2817 *
2818 * This will be fixed with VFS changes (lookup-intent).
2819 */
2820static int
2821nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2822 struct iattr *sattr)
2823{
08e9eac4 2824 struct inode *inode = dentry->d_inode;
659bfcd6 2825 struct rpc_cred *cred = NULL;
d530838b 2826 struct nfs4_state *state = NULL;
1da177e4
LT
2827 int status;
2828
8a1636c4
BH
2829 if (pnfs_ld_layoutret_on_setattr(inode))
2830 pnfs_return_layout(inode);
2831
0e574af1 2832 nfs_fattr_init(fattr);
1da177e4 2833
2669940d
AA
2834 /* Deal with open(O_TRUNC) */
2835 if (sattr->ia_valid & ATTR_OPEN)
2836 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2837
2838 /* Optimization: if the end result is no change, don't RPC */
2839 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2840 return 0;
2841
d530838b 2842 /* Search for an existing open(O_WRITE) file */
659bfcd6
TM
2843 if (sattr->ia_valid & ATTR_FILE) {
2844 struct nfs_open_context *ctx;
2845
2846 ctx = nfs_file_open_context(sattr->ia_file);
504e5189
NB
2847 if (ctx) {
2848 cred = ctx->cred;
2849 state = ctx->state;
2850 }
659bfcd6 2851 }
08e9eac4 2852
659bfcd6 2853 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
65e4308d
TM
2854 if (status == 0)
2855 nfs_setattr_update_inode(inode, sattr);
1da177e4
LT
2856 return status;
2857}
2858
0c2e53f1
TM
2859static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2860 const struct qstr *name, struct nfs_fh *fhandle,
2861 struct nfs_fattr *fattr)
2b3de441 2862{
0c2e53f1 2863 struct nfs_server *server = NFS_SERVER(dir);
2b3de441
DH
2864 int status;
2865 struct nfs4_lookup_arg args = {
2866 .bitmask = server->attr_bitmask,
0c2e53f1 2867 .dir_fh = NFS_FH(dir),
2b3de441
DH
2868 .name = name,
2869 };
2870 struct nfs4_lookup_res res = {
2871 .server = server,
2872 .fattr = fattr,
2873 .fh = fhandle,
2874 };
2875 struct rpc_message msg = {
2876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2877 .rpc_argp = &args,
2878 .rpc_resp = &res,
2879 };
2880
2881 nfs_fattr_init(fattr);
2882
1da177e4 2883 dprintk("NFS call lookup %s\n", name->name);
0c2e53f1 2884 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
2885 dprintk("NFS reply lookup: %d\n", status);
2886 return status;
2887}
2888
72de53ec 2889static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
7ebb9315 2890{
7ebb9315 2891 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
72de53ec 2892 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
7ebb9315
BS
2893 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2894 fattr->nlink = 2;
2895}
2896
72de53ec
BS
2897static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2898 struct qstr *name, struct nfs_fh *fhandle,
2899 struct nfs_fattr *fattr)
1da177e4
LT
2900{
2901 struct nfs4_exception exception = { };
72de53ec 2902 struct rpc_clnt *client = *clnt;
1da177e4
LT
2903 int err;
2904 do {
72de53ec
BS
2905 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2906 switch (err) {
08ef7bd3 2907 case -NFS4ERR_BADNAME:
72de53ec
BS
2908 err = -ENOENT;
2909 goto out;
0c2e53f1 2910 case -NFS4ERR_MOVED:
f05d147f 2911 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
72de53ec 2912 goto out;
0c2e53f1 2913 case -NFS4ERR_WRONGSEC:
72de53ec
BS
2914 err = -EPERM;
2915 if (client != *clnt)
2916 goto out;
2917
2918 client = nfs4_create_sec_client(client, dir, name);
2919 if (IS_ERR(client))
2920 return PTR_ERR(client);
2921
2922 exception.retry = 1;
2923 break;
2924 default:
2925 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
0c2e53f1 2926 }
1da177e4 2927 } while (exception.retry);
72de53ec
BS
2928
2929out:
2930 if (err == 0)
2931 *clnt = client;
2932 else if (client != *clnt)
2933 rpc_shutdown_client(client);
2934
1da177e4
LT
2935 return err;
2936}
2937
80a16b21 2938static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
72de53ec
BS
2939 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2940{
2941 int status;
2942 struct rpc_clnt *client = NFS_CLIENT(dir);
2943
2944 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2945 if (client != NFS_CLIENT(dir)) {
2946 rpc_shutdown_client(client);
2947 nfs_fixup_secinfo_attributes(fattr);
2948 }
2949 return status;
2950}
2951
f05d147f
BS
2952struct rpc_clnt *
2953nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2954 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2955{
2956 int status;
2957 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2958
2959 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2960 if (status < 0) {
2961 rpc_shutdown_client(client);
2962 return ERR_PTR(status);
2963 }
2964 return client;
2965}
2966
1da177e4
LT
2967static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2968{
76b32999 2969 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
2970 struct nfs4_accessargs args = {
2971 .fh = NFS_FH(inode),
a4980e78 2972 .bitmask = server->cache_consistency_bitmask,
76b32999
TM
2973 };
2974 struct nfs4_accessres res = {
2975 .server = server,
1da177e4 2976 };
1da177e4
LT
2977 struct rpc_message msg = {
2978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2979 .rpc_argp = &args,
2980 .rpc_resp = &res,
2981 .rpc_cred = entry->cred,
2982 };
2983 int mode = entry->mask;
2984 int status;
2985
2986 /*
2987 * Determine which access bits we want to ask for...
2988 */
2989 if (mode & MAY_READ)
2990 args.access |= NFS4_ACCESS_READ;
2991 if (S_ISDIR(inode->i_mode)) {
2992 if (mode & MAY_WRITE)
2993 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2994 if (mode & MAY_EXEC)
2995 args.access |= NFS4_ACCESS_LOOKUP;
2996 } else {
2997 if (mode & MAY_WRITE)
2998 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2999 if (mode & MAY_EXEC)
3000 args.access |= NFS4_ACCESS_EXECUTE;
3001 }
c407d41a
TM
3002
3003 res.fattr = nfs_alloc_fattr();
3004 if (res.fattr == NULL)
3005 return -ENOMEM;
3006
7c513058 3007 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4 3008 if (!status) {
6168f62c 3009 nfs_access_set_mask(entry, res.access);
c407d41a 3010 nfs_refresh_inode(inode, res.fattr);
1da177e4 3011 }
c407d41a 3012 nfs_free_fattr(res.fattr);
1da177e4
LT
3013 return status;
3014}
3015
3016static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3017{
3018 struct nfs4_exception exception = { };
3019 int err;
3020 do {
3021 err = nfs4_handle_exception(NFS_SERVER(inode),
3022 _nfs4_proc_access(inode, entry),
3023 &exception);
3024 } while (exception.retry);
3025 return err;
3026}
3027
3028/*
3029 * TODO: For the time being, we don't try to get any attributes
3030 * along with any of the zero-copy operations READ, READDIR,
3031 * READLINK, WRITE.
3032 *
3033 * In the case of the first three, we want to put the GETATTR
3034 * after the read-type operation -- this is because it is hard
3035 * to predict the length of a GETATTR response in v4, and thus
3036 * align the READ data correctly. This means that the GETATTR
3037 * may end up partially falling into the page cache, and we should
3038 * shift it into the 'tail' of the xdr_buf before processing.
3039 * To do this efficiently, we need to know the total length
3040 * of data received, which doesn't seem to be available outside
3041 * of the RPC layer.
3042 *
3043 * In the case of WRITE, we also want to put the GETATTR after
3044 * the operation -- in this case because we want to make sure
140150db 3045 * we get the post-operation mtime and size.
1da177e4
LT
3046 *
3047 * Both of these changes to the XDR layer would in fact be quite
3048 * minor, but I decided to leave them for a subsequent patch.
3049 */
3050static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3051 unsigned int pgbase, unsigned int pglen)
3052{
3053 struct nfs4_readlink args = {
3054 .fh = NFS_FH(inode),
3055 .pgbase = pgbase,
3056 .pglen = pglen,
3057 .pages = &page,
3058 };
f50c7000 3059 struct nfs4_readlink_res res;
1da177e4
LT
3060 struct rpc_message msg = {
3061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3062 .rpc_argp = &args,
f50c7000 3063 .rpc_resp = &res,
1da177e4
LT
3064 };
3065
7c513058 3066 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
3067}
3068
3069static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3070 unsigned int pgbase, unsigned int pglen)
3071{
3072 struct nfs4_exception exception = { };
3073 int err;
3074 do {
3075 err = nfs4_handle_exception(NFS_SERVER(inode),
3076 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3077 &exception);
3078 } while (exception.retry);
3079 return err;
3080}
3081
1da177e4 3082/*
8867fe58 3083 * This is just for mknod. open(O_CREAT) will always do ->open_context().
1da177e4 3084 */
1da177e4
LT
3085static int
3086nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
8867fe58 3087 int flags)
1da177e4 3088{
8867fe58 3089 struct nfs_open_context *ctx;
1da177e4 3090 struct nfs4_state *state;
1da177e4
LT
3091 int status = 0;
3092
8867fe58
MS
3093 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3094 if (IS_ERR(ctx))
3095 return PTR_ERR(ctx);
3096
a8a5da99 3097 sattr->ia_mode &= ~current_umask();
8626e4a4
TM
3098 state = nfs4_do_open(dir, dentry, ctx->mode,
3099 flags, sattr, ctx->cred,
3100 &ctx->mdsthreshold);
d4d9cdcb 3101 d_drop(dentry);
1da177e4
LT
3102 if (IS_ERR(state)) {
3103 status = PTR_ERR(state);
c0204fd2 3104 goto out;
1da177e4 3105 }
d4d9cdcb 3106 d_add(dentry, igrab(state->inode));
d75340cc 3107 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
8867fe58 3108 ctx->state = state;
1da177e4 3109out:
8867fe58 3110 put_nfs_open_context(ctx);
1da177e4
LT
3111 return status;
3112}
3113
3114static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3115{
16e42959 3116 struct nfs_server *server = NFS_SERVER(dir);
4fdc17b2 3117 struct nfs_removeargs args = {
1da177e4 3118 .fh = NFS_FH(dir),
26fe5750 3119 .name = *name,
16e42959 3120 };
4fdc17b2 3121 struct nfs_removeres res = {
16e42959 3122 .server = server,
1da177e4 3123 };
1da177e4 3124 struct rpc_message msg = {
4fdc17b2
TM
3125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3126 .rpc_argp = &args,
3127 .rpc_resp = &res,
1da177e4 3128 };
778d2817 3129 int status;
1da177e4 3130
7c513058 3131 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
778d2817 3132 if (status == 0)
16e42959 3133 update_changeattr(dir, &res.cinfo);
1da177e4
LT
3134 return status;
3135}
3136
3137static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3138{
3139 struct nfs4_exception exception = { };
3140 int err;
3141 do {
3142 err = nfs4_handle_exception(NFS_SERVER(dir),
3143 _nfs4_proc_remove(dir, name),
3144 &exception);
3145 } while (exception.retry);
3146 return err;
3147}
3148
e4eff1a6 3149static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
1da177e4 3150{
e4eff1a6
TM
3151 struct nfs_server *server = NFS_SERVER(dir);
3152 struct nfs_removeargs *args = msg->rpc_argp;
3153 struct nfs_removeres *res = msg->rpc_resp;
1da177e4 3154
e4eff1a6 3155 res->server = server;
1da177e4 3156 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
9d12b216 3157 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
1da177e4
LT
3158}
3159
34e137cc
BS
3160static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3161{
3162 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3163 &data->args.seq_args,
3164 &data->res.seq_res,
3165 task))
3166 return;
3167 rpc_call_start(task);
1da177e4
LT
3168}
3169
e4eff1a6 3170static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
1da177e4 3171{
e4eff1a6
TM
3172 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3173
14516c3a
TM
3174 if (!nfs4_sequence_done(task, &res->seq_res))
3175 return 0;
9e33bed5 3176 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
e4eff1a6
TM
3177 return 0;
3178 update_changeattr(dir, &res->cinfo);
e4eff1a6 3179 return 1;
1da177e4
LT
3180}
3181
d3d4152a
JL
3182static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3183{
3184 struct nfs_server *server = NFS_SERVER(dir);
3185 struct nfs_renameargs *arg = msg->rpc_argp;
3186 struct nfs_renameres *res = msg->rpc_resp;
3187
3188 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
d3d4152a 3189 res->server = server;
9d12b216 3190 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
d3d4152a
JL
3191}
3192
c6bfa1a1
BS
3193static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3194{
3195 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3196 &data->args.seq_args,
3197 &data->res.seq_res,
3198 task))
3199 return;
3200 rpc_call_start(task);
d3d4152a
JL
3201}
3202
3203static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3204 struct inode *new_dir)
3205{
3206 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3207
3208 if (!nfs4_sequence_done(task, &res->seq_res))
3209 return 0;
3210 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3211 return 0;
3212
3213 update_changeattr(old_dir, &res->old_cinfo);
d3d4152a 3214 update_changeattr(new_dir, &res->new_cinfo);
d3d4152a
JL
3215 return 1;
3216}
3217
1da177e4
LT
3218static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3219 struct inode *new_dir, struct qstr *new_name)
3220{
6caf2c82 3221 struct nfs_server *server = NFS_SERVER(old_dir);
920769f0 3222 struct nfs_renameargs arg = {
1da177e4
LT
3223 .old_dir = NFS_FH(old_dir),
3224 .new_dir = NFS_FH(new_dir),
3225 .old_name = old_name,
3226 .new_name = new_name,
6caf2c82 3227 };
e8582a8b 3228 struct nfs_renameres res = {
6caf2c82 3229 .server = server,
1da177e4 3230 };
1da177e4
LT
3231 struct rpc_message msg = {
3232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3233 .rpc_argp = &arg,
3234 .rpc_resp = &res,
3235 };
011fff72 3236 int status = -ENOMEM;
1da177e4 3237
7c513058 3238 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1da177e4
LT
3239 if (!status) {
3240 update_changeattr(old_dir, &res.old_cinfo);
3241 update_changeattr(new_dir, &res.new_cinfo);
3242 }
3243 return status;
3244}
3245
3246static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3247 struct inode *new_dir, struct qstr *new_name)
3248{
3249 struct nfs4_exception exception = { };
3250 int err;
3251 do {
3252 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3253 _nfs4_proc_rename(old_dir, old_name,
3254 new_dir, new_name),
3255 &exception);
3256 } while (exception.retry);
3257 return err;
3258}
3259
3260static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3261{
91ba2eee 3262 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
3263 struct nfs4_link_arg arg = {
3264 .fh = NFS_FH(inode),
3265 .dir_fh = NFS_FH(dir),
3266 .name = name,
91ba2eee
TM
3267 .bitmask = server->attr_bitmask,
3268 };
91ba2eee
TM
3269 struct nfs4_link_res res = {
3270 .server = server,
1da177e4 3271 };
1da177e4
LT
3272 struct rpc_message msg = {
3273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3274 .rpc_argp = &arg,
91ba2eee 3275 .rpc_resp = &res,
1da177e4 3276 };
136f2627
TM
3277 int status = -ENOMEM;
3278
3279 res.fattr = nfs_alloc_fattr();
778d2817 3280 if (res.fattr == NULL)
136f2627 3281 goto out;
1da177e4 3282
7c513058 3283 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
91ba2eee
TM
3284 if (!status) {
3285 update_changeattr(dir, &res.cinfo);
73a3d07c 3286 nfs_post_op_update_inode(inode, res.fattr);
91ba2eee 3287 }
136f2627 3288out:
136f2627 3289 nfs_free_fattr(res.fattr);
1da177e4
LT
3290 return status;
3291}
3292
3293static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3294{
3295 struct nfs4_exception exception = { };
3296 int err;
3297 do {
3298 err = nfs4_handle_exception(NFS_SERVER(inode),
3299 _nfs4_proc_link(inode, dir, name),
3300 &exception);
3301 } while (exception.retry);
3302 return err;
3303}
3304
57dc9a57
TM
3305struct nfs4_createdata {
3306 struct rpc_message msg;
3307 struct nfs4_create_arg arg;
3308 struct nfs4_create_res res;
3309 struct nfs_fh fh;
3310 struct nfs_fattr fattr;
57dc9a57
TM
3311};
3312
3313static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3314 struct qstr *name, struct iattr *sattr, u32 ftype)
3315{
3316 struct nfs4_createdata *data;
3317
3318 data = kzalloc(sizeof(*data), GFP_KERNEL);
3319 if (data != NULL) {
3320 struct nfs_server *server = NFS_SERVER(dir);
3321
3322 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3323 data->msg.rpc_argp = &data->arg;
3324 data->msg.rpc_resp = &data->res;
3325 data->arg.dir_fh = NFS_FH(dir);
3326 data->arg.server = server;
3327 data->arg.name = name;
3328 data->arg.attrs = sattr;
3329 data->arg.ftype = ftype;
3330 data->arg.bitmask = server->attr_bitmask;
3331 data->res.server = server;
3332 data->res.fh = &data->fh;
3333 data->res.fattr = &data->fattr;
57dc9a57 3334 nfs_fattr_init(data->res.fattr);
57dc9a57
TM
3335 }
3336 return data;
3337}
3338
3339static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3340{
7c513058 3341 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
e73b83f2 3342 &data->arg.seq_args, &data->res.seq_res, 1);
57dc9a57
TM
3343 if (status == 0) {
3344 update_changeattr(dir, &data->res.dir_cinfo);
57dc9a57
TM
3345 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3346 }
3347 return status;
3348}
3349
3350static void nfs4_free_createdata(struct nfs4_createdata *data)
3351{
3352 kfree(data);
3353}
3354
4f390c15 3355static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
94a6d753 3356 struct page *page, unsigned int len, struct iattr *sattr)
1da177e4 3357{
57dc9a57
TM
3358 struct nfs4_createdata *data;
3359 int status = -ENAMETOOLONG;
1da177e4 3360
94a6d753 3361 if (len > NFS4_MAXPATHLEN)
57dc9a57 3362 goto out;
4f390c15 3363
57dc9a57
TM
3364 status = -ENOMEM;
3365 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3366 if (data == NULL)
3367 goto out;
3368
3369 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3370 data->arg.u.symlink.pages = &page;
3371 data->arg.u.symlink.len = len;
1da177e4 3372
57dc9a57
TM
3373 status = nfs4_do_create(dir, dentry, data);
3374
3375 nfs4_free_createdata(data);
3376out:
1da177e4
LT
3377 return status;
3378}
3379
4f390c15 3380static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
94a6d753 3381 struct page *page, unsigned int len, struct iattr *sattr)
1da177e4
LT
3382{
3383 struct nfs4_exception exception = { };
3384 int err;
3385 do {
3386 err = nfs4_handle_exception(NFS_SERVER(dir),
94a6d753
CL
3387 _nfs4_proc_symlink(dir, dentry, page,
3388 len, sattr),
1da177e4
LT
3389 &exception);
3390 } while (exception.retry);
3391 return err;
3392}
3393
3394static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3395 struct iattr *sattr)
3396{
57dc9a57
TM
3397 struct nfs4_createdata *data;
3398 int status = -ENOMEM;
1da177e4 3399
57dc9a57
TM
3400 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3401 if (data == NULL)
3402 goto out;
3403
3404 status = nfs4_do_create(dir, dentry, data);
3405
3406 nfs4_free_createdata(data);
3407out:
1da177e4
LT
3408 return status;
3409}
3410
3411static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3412 struct iattr *sattr)
3413{
3414 struct nfs4_exception exception = { };
3415 int err;
a8a5da99
AK
3416
3417 sattr->ia_mode &= ~current_umask();
1da177e4
LT
3418 do {
3419 err = nfs4_handle_exception(NFS_SERVER(dir),
3420 _nfs4_proc_mkdir(dir, dentry, sattr),
3421 &exception);
3422 } while (exception.retry);
3423 return err;
3424}
3425
3426static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
56e4ebf8 3427 u64 cookie, struct page **pages, unsigned int count, int plus)
1da177e4
LT
3428{
3429 struct inode *dir = dentry->d_inode;
3430 struct nfs4_readdir_arg args = {
3431 .fh = NFS_FH(dir),
56e4ebf8 3432 .pages = pages,
1da177e4
LT
3433 .pgbase = 0,
3434 .count = count,
96d25e53 3435 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
82f2e547 3436 .plus = plus,
1da177e4
LT
3437 };
3438 struct nfs4_readdir_res res;
3439 struct rpc_message msg = {
3440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3441 .rpc_argp = &args,
3442 .rpc_resp = &res,
3443 .rpc_cred = cred,
3444 };
3445 int status;
3446
3110ff80 3447 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
eadf4598
TM
3448 dentry->d_parent->d_name.name,
3449 dentry->d_name.name,
3450 (unsigned long long)cookie);
c3f52af3 3451 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
1da177e4 3452 res.pgbase = args.pgbase;
7c513058 3453 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
ac396128 3454 if (status >= 0) {
c3f52af3 3455 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
ac396128
TM
3456 status += args.pgbase;
3457 }
c4812998
TM
3458
3459 nfs_invalidate_atime(dir);
3460
3110ff80 3461 dprintk("%s: returns %d\n", __func__, status);
1da177e4
LT
3462 return status;
3463}
3464
3465static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
56e4ebf8 3466 u64 cookie, struct page **pages, unsigned int count, int plus)
1da177e4
LT
3467{
3468 struct nfs4_exception exception = { };
3469 int err;
3470 do {
3471 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3472 _nfs4_proc_readdir(dentry, cred, cookie,
56e4ebf8 3473 pages, count, plus),
1da177e4
LT
3474 &exception);
3475 } while (exception.retry);
3476 return err;
3477}
3478
3479static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3480 struct iattr *sattr, dev_t rdev)
3481{
57dc9a57
TM
3482 struct nfs4_createdata *data;
3483 int mode = sattr->ia_mode;
3484 int status = -ENOMEM;
1da177e4 3485
57dc9a57
TM
3486 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3487 if (data == NULL)
3488 goto out;
3489
1da177e4 3490 if (S_ISFIFO(mode))
57dc9a57 3491 data->arg.ftype = NF4FIFO;
1da177e4 3492 else if (S_ISBLK(mode)) {
57dc9a57
TM
3493 data->arg.ftype = NF4BLK;
3494 data->arg.u.device.specdata1 = MAJOR(rdev);
3495 data->arg.u.device.specdata2 = MINOR(rdev);
1da177e4
LT
3496 }
3497 else if (S_ISCHR(mode)) {
57dc9a57
TM
3498 data->arg.ftype = NF4CHR;
3499 data->arg.u.device.specdata1 = MAJOR(rdev);
3500 data->arg.u.device.specdata2 = MINOR(rdev);
4ea8fed5
TM
3501 } else if (!S_ISSOCK(mode)) {
3502 status = -EINVAL;
3503 goto out_free;
1da177e4 3504 }
1da177e4 3505
57dc9a57 3506 status = nfs4_do_create(dir, dentry, data);
4ea8fed5 3507out_free:
57dc9a57
TM
3508 nfs4_free_createdata(data);
3509out:
1da177e4
LT
3510 return status;
3511}
3512
3513static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3514 struct iattr *sattr, dev_t rdev)
3515{
3516 struct nfs4_exception exception = { };
3517 int err;
a8a5da99
AK
3518
3519 sattr->ia_mode &= ~current_umask();
1da177e4
LT
3520 do {
3521 err = nfs4_handle_exception(NFS_SERVER(dir),
3522 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3523 &exception);
3524 } while (exception.retry);
3525 return err;
3526}
3527
3528static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3529 struct nfs_fsstat *fsstat)
3530{
3531 struct nfs4_statfs_arg args = {
3532 .fh = fhandle,
3533 .bitmask = server->attr_bitmask,
3534 };
24ad148a
BH
3535 struct nfs4_statfs_res res = {
3536 .fsstat = fsstat,
3537 };
1da177e4
LT
3538 struct rpc_message msg = {
3539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3540 .rpc_argp = &args,
24ad148a 3541 .rpc_resp = &res,
1da177e4
LT
3542 };
3543
0e574af1 3544 nfs_fattr_init(fsstat->fattr);
7c513058 3545 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
3546}
3547
3548static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3549{
3550 struct nfs4_exception exception = { };
3551 int err;
3552 do {
3553 err = nfs4_handle_exception(server,
3554 _nfs4_proc_statfs(server, fhandle, fsstat),
3555 &exception);
3556 } while (exception.retry);
3557 return err;
3558}
3559
3560static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3561 struct nfs_fsinfo *fsinfo)
3562{
3563 struct nfs4_fsinfo_arg args = {
3564 .fh = fhandle,
3565 .bitmask = server->attr_bitmask,
3566 };
3dda5e43
BH
3567 struct nfs4_fsinfo_res res = {
3568 .fsinfo = fsinfo,
3569 };
1da177e4
LT
3570 struct rpc_message msg = {
3571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3572 .rpc_argp = &args,
3dda5e43 3573 .rpc_resp = &res,
1da177e4
LT
3574 };
3575
7c513058 3576 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
3577}
3578
3579static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3580{
3581 struct nfs4_exception exception = { };
3582 int err;
3583
3584 do {
3585 err = nfs4_handle_exception(server,
3586 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3587 &exception);
3588 } while (exception.retry);
3589 return err;
3590}
3591
3592static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3593{
e38eb650
BS
3594 int error;
3595
0e574af1 3596 nfs_fattr_init(fsinfo->fattr);
e38eb650 3597 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
dc182549
PT
3598 if (error == 0) {
3599 /* block layout checks this! */
3600 server->pnfs_blksize = fsinfo->blksize;
e38eb650 3601 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
dc182549 3602 }
e38eb650
BS
3603
3604 return error;
1da177e4
LT
3605}
3606
3607static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3608 struct nfs_pathconf *pathconf)
3609{
3610 struct nfs4_pathconf_arg args = {
3611 .fh = fhandle,
3612 .bitmask = server->attr_bitmask,
3613 };
d45b2989
BH
3614 struct nfs4_pathconf_res res = {
3615 .pathconf = pathconf,
3616 };
1da177e4
LT
3617 struct rpc_message msg = {
3618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3619 .rpc_argp = &args,
d45b2989 3620 .rpc_resp = &res,
1da177e4
LT
3621 };
3622
3623 /* None of the pathconf attributes are mandatory to implement */
3624 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3625 memset(pathconf, 0, sizeof(*pathconf));
3626 return 0;
3627 }
3628
0e574af1 3629 nfs_fattr_init(pathconf->fattr);
7c513058 3630 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
3631}
3632
3633static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3634 struct nfs_pathconf *pathconf)
3635{
3636 struct nfs4_exception exception = { };
3637 int err;
3638
3639 do {
3640 err = nfs4_handle_exception(server,
3641 _nfs4_proc_pathconf(server, fhandle, pathconf),
3642 &exception);
3643 } while (exception.retry);
3644 return err;
3645}
3646
d20581aa
BH
3647void __nfs4_read_done_cb(struct nfs_read_data *data)
3648{
cd841605 3649 nfs_invalidate_atime(data->header->inode);
d20581aa
BH
3650}
3651
cbdabc7f 3652static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
1da177e4 3653{
cd841605 3654 struct nfs_server *server = NFS_SERVER(data->header->inode);
1da177e4 3655
9e33bed5 3656 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
d00c5d43 3657 rpc_restart_call_prepare(task);
ec06c096 3658 return -EAGAIN;
1da177e4 3659 }
8850df99 3660
d20581aa 3661 __nfs4_read_done_cb(data);
1da177e4 3662 if (task->tk_status > 0)
ec06c096
TM
3663 renew_lease(server, data->timestamp);
3664 return 0;
1da177e4
LT
3665}
3666
cbdabc7f
AA
3667static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3668{
3669
3670 dprintk("--> %s\n", __func__);
3671
3672 if (!nfs4_sequence_done(task, &data->res.seq_res))
3673 return -EAGAIN;
3674
d20581aa
BH
3675 return data->read_done_cb ? data->read_done_cb(task, data) :
3676 nfs4_read_done_cb(task, data);
cbdabc7f
AA
3677}
3678
bdc7f021 3679static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
1da177e4 3680{
1da177e4 3681 data->timestamp = jiffies;
cbdabc7f 3682 data->read_done_cb = nfs4_read_done_cb;
bdc7f021 3683 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
9d12b216 3684 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
1da177e4
LT
3685}
3686
ea7c3303
BS
3687static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3688{
cd841605 3689 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
ea7c3303
BS
3690 &data->args.seq_args,
3691 &data->res.seq_res,
3692 task))
3693 return;
3694 rpc_call_start(task);
1da177e4
LT
3695}
3696
b029bc9b 3697static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
1da177e4 3698{
cd841605 3699 struct inode *inode = data->header->inode;
1da177e4 3700
9e33bed5 3701 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
d00c5d43 3702 rpc_restart_call_prepare(task);
788e7a89 3703 return -EAGAIN;
1da177e4 3704 }
4f9838c7 3705 if (task->tk_status >= 0) {
1da177e4 3706 renew_lease(NFS_SERVER(inode), data->timestamp);
5a37f851 3707 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4f9838c7 3708 }
788e7a89 3709 return 0;
1da177e4
LT
3710}
3711
b029bc9b
FI
3712static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3713{
3714 if (!nfs4_sequence_done(task, &data->res.seq_res))
3715 return -EAGAIN;
d20581aa
BH
3716 return data->write_done_cb ? data->write_done_cb(task, data) :
3717 nfs4_write_done_cb(task, data);
b029bc9b
FI
3718}
3719
5a37f851
TM
3720static
3721bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
a69aef14 3722{
5a37f851
TM
3723 const struct nfs_pgio_header *hdr = data->header;
3724
3725 /* Don't request attributes for pNFS or O_DIRECT writes */
3726 if (data->ds_clp != NULL || hdr->dreq != NULL)
3727 return false;
3728 /* Otherwise, request attributes if and only if we don't hold
3729 * a delegation
3730 */
011e2a7f 3731 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
a69aef14 3732}
a69aef14 3733
bdc7f021 3734static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
1da177e4 3735{
cd841605 3736 struct nfs_server *server = NFS_SERVER(data->header->inode);
bdc7f021 3737
5a37f851 3738 if (!nfs4_write_need_cache_consistency_data(data)) {
7ffd1064
FI
3739 data->args.bitmask = NULL;
3740 data->res.fattr = NULL;
3741 } else
3742 data->args.bitmask = server->cache_consistency_bitmask;
5a37f851 3743
b029bc9b
FI
3744 if (!data->write_done_cb)
3745 data->write_done_cb = nfs4_write_done_cb;
4f9838c7 3746 data->res.server = server;
1da177e4
LT
3747 data->timestamp = jiffies;
3748
bdc7f021 3749 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
9d12b216 3750 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
1da177e4
LT
3751}
3752
c6cb80d0
BS
3753static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3754{
cd841605 3755 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
c6cb80d0
BS
3756 &data->args.seq_args,
3757 &data->res.seq_res,
3758 task))
3759 return;
3760 rpc_call_start(task);
1da177e4
LT
3761}
3762
0b7c0153 3763static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
c6cb80d0
BS
3764{
3765 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3766 &data->args.seq_args,
3767 &data->res.seq_res,
3768 task))
3769 return;
3770 rpc_call_start(task);
1da177e4
LT
3771}
3772
0b7c0153 3773static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
1da177e4 3774{
1da177e4 3775 struct inode *inode = data->inode;
14516c3a 3776
9e33bed5 3777 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
d00c5d43 3778 rpc_restart_call_prepare(task);
788e7a89 3779 return -EAGAIN;
1da177e4 3780 }
788e7a89 3781 return 0;
1da177e4
LT
3782}
3783
0b7c0153 3784static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5f452431
FI
3785{
3786 if (!nfs4_sequence_done(task, &data->res.seq_res))
3787 return -EAGAIN;
0b7c0153 3788 return data->commit_done_cb(task, data);
5f452431
FI
3789}
3790
0b7c0153 3791static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
1da177e4 3792{
788e7a89 3793 struct nfs_server *server = NFS_SERVER(data->inode);
988b6dce 3794
0b7c0153
FI
3795 if (data->commit_done_cb == NULL)
3796 data->commit_done_cb = nfs4_commit_done_cb;
4f9838c7 3797 data->res.server = server;
bdc7f021 3798 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
9d12b216 3799 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
1da177e4
LT
3800}
3801
9bc4e3ca
CL
3802struct nfs4_renewdata {
3803 struct nfs_client *client;
3804 unsigned long timestamp;
3805};
3806
1da177e4
LT
3807/*
3808 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3809 * standalone procedure for queueing an asynchronous RENEW.
3810 */
9bc4e3ca 3811static void nfs4_renew_release(void *calldata)
dc96aef9 3812{
9bc4e3ca
CL
3813 struct nfs4_renewdata *data = calldata;
3814 struct nfs_client *clp = data->client;
dc96aef9 3815
0851de06
AB
3816 if (atomic_read(&clp->cl_count) > 1)
3817 nfs4_schedule_state_renewal(clp);
3818 nfs_put_client(clp);
9bc4e3ca 3819 kfree(data);
dc96aef9
AB
3820}
3821
9bc4e3ca 3822static void nfs4_renew_done(struct rpc_task *task, void *calldata)
1da177e4 3823{
9bc4e3ca
CL
3824 struct nfs4_renewdata *data = calldata;
3825 struct nfs_client *clp = data->client;
3826 unsigned long timestamp = data->timestamp;
1da177e4
LT
3827
3828 if (task->tk_status < 0) {
95baa25c 3829 /* Unless we're shutting down, schedule state recovery! */
042b60be
TM
3830 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3831 return;
3832 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
0400a6b0 3833 nfs4_schedule_lease_recovery(clp);
042b60be
TM
3834 return;
3835 }
3836 nfs4_schedule_path_down_recovery(clp);
1da177e4 3837 }
452e9352 3838 do_renew_lease(clp, timestamp);
1da177e4
LT
3839}
3840
963d8fe5
TM
3841static const struct rpc_call_ops nfs4_renew_ops = {
3842 .rpc_call_done = nfs4_renew_done,
dc96aef9 3843 .rpc_release = nfs4_renew_release,
963d8fe5
TM
3844};
3845
2f60ea6b 3846static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
1da177e4
LT
3847{
3848 struct rpc_message msg = {
3849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3850 .rpc_argp = clp,
b4454fe1 3851 .rpc_cred = cred,
1da177e4 3852 };
9bc4e3ca 3853 struct nfs4_renewdata *data;
1da177e4 3854
2f60ea6b
TM
3855 if (renew_flags == 0)
3856 return 0;
0851de06
AB
3857 if (!atomic_inc_not_zero(&clp->cl_count))
3858 return -EIO;
b569ad34 3859 data = kmalloc(sizeof(*data), GFP_NOFS);
9bc4e3ca
CL
3860 if (data == NULL)
3861 return -ENOMEM;
3862 data->client = clp;
3863 data->timestamp = jiffies;
1da177e4 3864 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
9bc4e3ca 3865 &nfs4_renew_ops, data);
1da177e4
LT
3866}
3867
8534d4ec 3868static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
1da177e4
LT
3869{
3870 struct rpc_message msg = {
3871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3872 .rpc_argp = clp,
b4454fe1 3873 .rpc_cred = cred,
1da177e4
LT
3874 };
3875 unsigned long now = jiffies;
3876 int status;
3877
3878 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3879 if (status < 0)
3880 return status;
452e9352 3881 do_renew_lease(clp, now);
1da177e4
LT
3882 return 0;
3883}
3884
aa1870af
BF
3885static inline int nfs4_server_supports_acls(struct nfs_server *server)
3886{
3887 return (server->caps & NFS_CAP_ACLS)
3888 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3889 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3890}
3891
21f498c2
TM
3892/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3893 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
aa1870af
BF
3894 * the stack.
3895 */
21f498c2 3896#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
aa1870af 3897
e9e3d724
NH
3898static int buf_to_pages_noslab(const void *buf, size_t buflen,
3899 struct page **pages, unsigned int *pgbase)
3900{
3901 struct page *newpage, **spages;
3902 int rc = 0;
3903 size_t len;
3904 spages = pages;
3905
3906 do {
21f498c2 3907 len = min_t(size_t, PAGE_SIZE, buflen);
e9e3d724
NH
3908 newpage = alloc_page(GFP_KERNEL);
3909
3910 if (newpage == NULL)
3911 goto unwind;
3912 memcpy(page_address(newpage), buf, len);
3913 buf += len;
3914 buflen -= len;
3915 *pages++ = newpage;
3916 rc++;
3917 } while (buflen != 0);
3918
3919 return rc;
3920
3921unwind:
3922 for(; rc > 0; rc--)
3923 __free_page(spages[rc-1]);
3924 return -ENOMEM;
3925}
3926
e50a1c2e
BF
3927struct nfs4_cached_acl {
3928 int cached;
3929 size_t len;
3e9d4154 3930 char data[0];
e50a1c2e
BF
3931};
3932
3933static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3934{
3935 struct nfs_inode *nfsi = NFS_I(inode);
3936
3937 spin_lock(&inode->i_lock);
3938 kfree(nfsi->nfs4_acl);
3939 nfsi->nfs4_acl = acl;
3940 spin_unlock(&inode->i_lock);
3941}
3942
3943static void nfs4_zap_acl_attr(struct inode *inode)
3944{
3945 nfs4_set_cached_acl(inode, NULL);
3946}
3947
3948static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3949{
3950 struct nfs_inode *nfsi = NFS_I(inode);
3951 struct nfs4_cached_acl *acl;
3952 int ret = -ENOENT;
3953
3954 spin_lock(&inode->i_lock);
3955 acl = nfsi->nfs4_acl;
3956 if (acl == NULL)
3957 goto out;
3958 if (buf == NULL) /* user is just asking for length */
3959 goto out_len;
3960 if (acl->cached == 0)
3961 goto out;
3962 ret = -ERANGE; /* see getxattr(2) man page */
3963 if (acl->len > buflen)
3964 goto out;
3965 memcpy(buf, acl->data, acl->len);
3966out_len:
3967 ret = acl->len;
3968out:
3969 spin_unlock(&inode->i_lock);
3970 return ret;
3971}
3972
5794d21e 3973static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
e50a1c2e
BF
3974{
3975 struct nfs4_cached_acl *acl;
b291f1b1 3976 size_t buflen = sizeof(*acl) + acl_len;
e50a1c2e 3977
1f1ea6c2 3978 if (buflen <= PAGE_SIZE) {
b291f1b1 3979 acl = kmalloc(buflen, GFP_KERNEL);
e50a1c2e
BF
3980 if (acl == NULL)
3981 goto out;
3982 acl->cached = 1;
5794d21e 3983 _copy_from_pages(acl->data, pages, pgbase, acl_len);
e50a1c2e
BF
3984 } else {
3985 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3986 if (acl == NULL)
3987 goto out;
3988 acl->cached = 0;
3989 }
3990 acl->len = acl_len;
3991out:
3992 nfs4_set_cached_acl(inode, acl);
3993}
3994
bf118a34
AA
3995/*
3996 * The getxattr API returns the required buffer length when called with a
3997 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3998 * the required buf. On a NULL buf, we send a page of data to the server
3999 * guessing that the ACL request can be serviced by a page. If so, we cache
4000 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4001 * the cache. If not so, we throw away the page, and cache the required
4002 * length. The next getxattr call will then produce another round trip to
4003 * the server, this time with the input buf of the required size.
4004 */
16b4289c 4005static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
aa1870af 4006{
bf118a34 4007 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
aa1870af
BF
4008 struct nfs_getaclargs args = {
4009 .fh = NFS_FH(inode),
4010 .acl_pages = pages,
4011 .acl_len = buflen,
4012 };
663c79b3
BH
4013 struct nfs_getaclres res = {
4014 .acl_len = buflen,
4015 };
aa1870af
BF
4016 struct rpc_message msg = {
4017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4018 .rpc_argp = &args,
663c79b3 4019 .rpc_resp = &res,
aa1870af 4020 };
21f498c2
TM
4021 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4022 int ret = -ENOMEM, i;
aa1870af 4023
bf118a34
AA
4024 /* As long as we're doing a round trip to the server anyway,
4025 * let's be prepared for a page of acl data. */
4026 if (npages == 0)
4027 npages = 1;
21f498c2
TM
4028 if (npages > ARRAY_SIZE(pages))
4029 return -ERANGE;
5a006899 4030
bf118a34
AA
4031 for (i = 0; i < npages; i++) {
4032 pages[i] = alloc_page(GFP_KERNEL);
4033 if (!pages[i])
4034 goto out_free;
e50a1c2e 4035 }
5a006899
SP
4036
4037 /* for decoding across pages */
4038 res.acl_scratch = alloc_page(GFP_KERNEL);
4039 if (!res.acl_scratch)
4040 goto out_free;
4041
bf118a34
AA
4042 args.acl_len = npages * PAGE_SIZE;
4043 args.acl_pgbase = 0;
5a006899 4044
de040bec 4045 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
bf118a34
AA
4046 __func__, buf, buflen, npages, args.acl_len);
4047 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4048 &msg, &args.seq_args, &res.seq_res, 0);
e50a1c2e
BF
4049 if (ret)
4050 goto out_free;
bf118a34 4051
1f1ea6c2
TM
4052 /* Handle the case where the passed-in buffer is too short */
4053 if (res.acl_flags & NFS4_ACL_TRUNC) {
4054 /* Did the user only issue a request for the acl length? */
4055 if (buf == NULL)
4056 goto out_ok;
e50a1c2e 4057 ret = -ERANGE;
1f1ea6c2 4058 goto out_free;
e50a1c2e 4059 }
1f1ea6c2
TM
4060 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4061 if (buf)
4062 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4063out_ok:
4064 ret = res.acl_len;
e50a1c2e 4065out_free:
bf118a34
AA
4066 for (i = 0; i < npages; i++)
4067 if (pages[i])
4068 __free_page(pages[i]);
331818f1
TM
4069 if (res.acl_scratch)
4070 __free_page(res.acl_scratch);
aa1870af
BF
4071 return ret;
4072}
4073
16b4289c
TM
4074static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4075{
4076 struct nfs4_exception exception = { };
4077 ssize_t ret;
4078 do {
4079 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4080 if (ret >= 0)
4081 break;
4082 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4083 } while (exception.retry);
4084 return ret;
4085}
4086
e50a1c2e
BF
4087static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4088{
4089 struct nfs_server *server = NFS_SERVER(inode);
4090 int ret;
4091
4092 if (!nfs4_server_supports_acls(server))
4093 return -EOPNOTSUPP;
4094 ret = nfs_revalidate_inode(server, inode);
4095 if (ret < 0)
4096 return ret;
08a22b39
AK
4097 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4098 nfs_zap_acl_cache(inode);
e50a1c2e
BF
4099 ret = nfs4_read_cached_acl(inode, buf, buflen);
4100 if (ret != -ENOENT)
bf118a34
AA
4101 /* -ENOENT is returned if there is no ACL or if there is an ACL
4102 * but no cached acl data, just the acl length */
e50a1c2e
BF
4103 return ret;
4104 return nfs4_get_acl_uncached(inode, buf, buflen);
4105}
4106
16b4289c 4107static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4b580ee3
BF
4108{
4109 struct nfs_server *server = NFS_SERVER(inode);
4110 struct page *pages[NFS4ACL_MAXPAGES];
4111 struct nfs_setaclargs arg = {
4112 .fh = NFS_FH(inode),
4113 .acl_pages = pages,
4114 .acl_len = buflen,
4115 };
73c403a9 4116 struct nfs_setaclres res;
4b580ee3
BF
4117 struct rpc_message msg = {
4118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4119 .rpc_argp = &arg,
73c403a9 4120 .rpc_resp = &res,
4b580ee3 4121 };
21f498c2 4122 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
e9e3d724 4123 int ret, i;
4b580ee3
BF
4124
4125 if (!nfs4_server_supports_acls(server))
4126 return -EOPNOTSUPP;
21f498c2
TM
4127 if (npages > ARRAY_SIZE(pages))
4128 return -ERANGE;
e9e3d724
NH
4129 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4130 if (i < 0)
4131 return i;
57ec14c5 4132 nfs4_inode_return_delegation(inode);
7c513058 4133 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
e9e3d724
NH
4134
4135 /*
4136 * Free each page after tx, so the only ref left is
4137 * held by the network stack
4138 */
4139 for (; i > 0; i--)
4140 put_page(pages[i-1]);
4141
08a22b39
AK
4142 /*
4143 * Acl update can result in inode attribute update.
4144 * so mark the attribute cache invalid.
4145 */
4146 spin_lock(&inode->i_lock);
4147 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4148 spin_unlock(&inode->i_lock);
f41f7418
TM
4149 nfs_access_zap_cache(inode);
4150 nfs_zap_acl_cache(inode);
4b580ee3
BF
4151 return ret;
4152}
4153
16b4289c
TM
4154static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4155{
4156 struct nfs4_exception exception = { };
4157 int err;
4158 do {
4159 err = nfs4_handle_exception(NFS_SERVER(inode),
4160 __nfs4_proc_set_acl(inode, buf, buflen),
4161 &exception);
4162 } while (exception.retry);
4163 return err;
4164}
4165
1da177e4 4166static int
aa5190d0 4167nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
1da177e4 4168{
aa5190d0
TM
4169 struct nfs_client *clp = server->nfs_client;
4170
4171 if (task->tk_status >= 0)
1da177e4
LT
4172 return 0;
4173 switch(task->tk_status) {
a1d0b5ee 4174 case -NFS4ERR_DELEG_REVOKED:
9e33bed5
TM
4175 case -NFS4ERR_ADMIN_REVOKED:
4176 case -NFS4ERR_BAD_STATEID:
14977489
TM
4177 if (state == NULL)
4178 break;
4179 nfs_remove_bad_delegation(state->inode);
9e33bed5
TM
4180 case -NFS4ERR_OPENMODE:
4181 if (state == NULL)
4182 break;
0400a6b0
TM
4183 nfs4_schedule_stateid_recovery(server, state);
4184 goto wait_on_recovery;
0ced63d1
TM
4185 case -NFS4ERR_EXPIRED:
4186 if (state != NULL)
4187 nfs4_schedule_stateid_recovery(server, state);
1da177e4 4188 case -NFS4ERR_STALE_STATEID:
a2c0b9e2 4189 case -NFS4ERR_STALE_CLIENTID:
0400a6b0
TM
4190 nfs4_schedule_lease_recovery(clp);
4191 goto wait_on_recovery;
4745e315
AA
4192#if defined(CONFIG_NFS_V4_1)
4193 case -NFS4ERR_BADSESSION:
4194 case -NFS4ERR_BADSLOT:
4195 case -NFS4ERR_BAD_HIGH_SLOT:
4196 case -NFS4ERR_DEADSESSION:
4197 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4198 case -NFS4ERR_SEQ_FALSE_RETRY:
4199 case -NFS4ERR_SEQ_MISORDERED:
4200 dprintk("%s ERROR %d, Reset session\n", __func__,
4201 task->tk_status);
9f594791 4202 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4745e315
AA
4203 task->tk_status = 0;
4204 return -EAGAIN;
4205#endif /* CONFIG_NFS_V4_1 */
1da177e4 4206 case -NFS4ERR_DELAY:
aa5190d0 4207 nfs_inc_server_stats(server, NFSIOS_DELAY);
006ea73e 4208 case -NFS4ERR_GRACE:
2c643488 4209 case -EKEYEXPIRED:
1da177e4
LT
4210 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4211 task->tk_status = 0;
4212 return -EAGAIN;
a8a4ae3a 4213 case -NFS4ERR_RETRY_UNCACHED_REP:
1da177e4
LT
4214 case -NFS4ERR_OLD_STATEID:
4215 task->tk_status = 0;
4216 return -EAGAIN;
4217 }
4218 task->tk_status = nfs4_map_errors(task->tk_status);
4219 return 0;
0400a6b0 4220wait_on_recovery:
a2c0b9e2 4221 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
a2c0b9e2
TM
4222 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4223 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4224 task->tk_status = 0;
4225 return -EAGAIN;
1da177e4
LT
4226}
4227
f092075d
CL
4228static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4229 nfs4_verifier *bootverf)
cd93710e
CL
4230{
4231 __be32 verf[2];
4232
2c820d9a
CL
4233 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4234 /* An impossible timestamp guarantees this value
4235 * will never match a generated boot time. */
4236 verf[0] = 0;
4237 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4238 } else {
f092075d
CL
4239 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4240 verf[0] = (__be32)nn->boot_time.tv_sec;
4241 verf[1] = (__be32)nn->boot_time.tv_nsec;
2c820d9a 4242 }
cd93710e
CL
4243 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4244}
4245
e984a55a
CL
4246static unsigned int
4247nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4248 char *buf, size_t len)
4249{
4250 unsigned int result;
4251
4252 rcu_read_lock();
4253 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4254 clp->cl_ipaddr,
4255 rpc_peeraddr2str(clp->cl_rpcclient,
4256 RPC_DISPLAY_ADDR),
4257 rpc_peeraddr2str(clp->cl_rpcclient,
4258 RPC_DISPLAY_PROTO));
4259 rcu_read_unlock();
4260 return result;
4261}
4262
4263static unsigned int
4264nfs4_init_uniform_client_string(const struct nfs_client *clp,
4265 char *buf, size_t len)
4266{
6f2ea7f2
CL
4267 char *nodename = clp->cl_rpcclient->cl_nodename;
4268
4269 if (nfs4_client_id_uniquifier[0] != '\0')
4270 nodename = nfs4_client_id_uniquifier;
e984a55a
CL
4271 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4272 clp->rpc_ops->version, clp->cl_minorversion,
6f2ea7f2 4273 nodename);
e984a55a
CL
4274}
4275
6bbb4ae8
CL
4276/**
4277 * nfs4_proc_setclientid - Negotiate client ID
4278 * @clp: state data structure
4279 * @program: RPC program for NFSv4 callback service
4280 * @port: IP port number for NFS4 callback service
4281 * @cred: RPC credential to use for this call
4282 * @res: where to place the result
4283 *
4284 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4285 */
bb8b27e5
TM
4286int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4287 unsigned short port, struct rpc_cred *cred,
4288 struct nfs4_setclientid_res *res)
1da177e4
LT
4289{
4290 nfs4_verifier sc_verifier;
4291 struct nfs4_setclientid setclientid = {
4292 .sc_verifier = &sc_verifier,
4293 .sc_prog = program,
f4eecd5d 4294 .sc_cb_ident = clp->cl_cb_ident,
1da177e4
LT
4295 };
4296 struct rpc_message msg = {
4297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4298 .rpc_argp = &setclientid,
bb8b27e5 4299 .rpc_resp = res,
286d7d6a 4300 .rpc_cred = cred,
1da177e4 4301 };
6bbb4ae8 4302 int status;
1da177e4 4303
de734831 4304 /* nfs_client_id4 */
f092075d 4305 nfs4_init_boot_verifier(clp, &sc_verifier);
e984a55a
CL
4306 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4307 setclientid.sc_name_len =
4308 nfs4_init_uniform_client_string(clp,
4309 setclientid.sc_name,
4310 sizeof(setclientid.sc_name));
4311 else
4312 setclientid.sc_name_len =
4313 nfs4_init_nonuniform_client_string(clp,
4314 setclientid.sc_name,
4315 sizeof(setclientid.sc_name));
de734831 4316 /* cb_client4 */
e984a55a 4317 rcu_read_lock();
de734831 4318 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
d4d3c507
CL
4319 sizeof(setclientid.sc_netid),
4320 rpc_peeraddr2str(clp->cl_rpcclient,
4321 RPC_DISPLAY_NETID));
de734831
CL
4322 rcu_read_unlock();
4323 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
d4d3c507 4324 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
1da177e4
LT
4325 clp->cl_ipaddr, port >> 8, port & 255);
4326
6bbb4ae8
CL
4327 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4328 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4329 setclientid.sc_name_len, setclientid.sc_name);
4330 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4331 dprintk("NFS reply setclientid: %d\n", status);
4332 return status;
1da177e4
LT
4333}
4334
6bbb4ae8
CL
4335/**
4336 * nfs4_proc_setclientid_confirm - Confirm client ID
4337 * @clp: state data structure
4338 * @res: result of a previous SETCLIENTID
4339 * @cred: RPC credential to use for this call
4340 *
4341 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4342 */
fd954ae1 4343int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
bb8b27e5
TM
4344 struct nfs4_setclientid_res *arg,
4345 struct rpc_cred *cred)
1da177e4
LT
4346{
4347 struct nfs_fsinfo fsinfo;
4348 struct rpc_message msg = {
4349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
bb8b27e5 4350 .rpc_argp = arg,
1da177e4 4351 .rpc_resp = &fsinfo,
286d7d6a 4352 .rpc_cred = cred,
1da177e4
LT
4353 };
4354 unsigned long now;
4355 int status;
4356
6bbb4ae8
CL
4357 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4358 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4359 clp->cl_clientid);
1da177e4 4360 now = jiffies;
1bd714f2 4361 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
1da177e4
LT
4362 if (status == 0) {
4363 spin_lock(&clp->cl_lock);
4364 clp->cl_lease_time = fsinfo.lease_time * HZ;
4365 clp->cl_last_renewal = now;
4366 spin_unlock(&clp->cl_lock);
4367 }
6bbb4ae8 4368 dprintk("NFS reply setclientid_confirm: %d\n", status);
1da177e4
LT
4369 return status;
4370}
4371
fe650407
TM
4372struct nfs4_delegreturndata {
4373 struct nfs4_delegreturnargs args;
fa178f29 4374 struct nfs4_delegreturnres res;
fe650407
TM
4375 struct nfs_fh fh;
4376 nfs4_stateid stateid;
26e976a8 4377 unsigned long timestamp;
fa178f29 4378 struct nfs_fattr fattr;
fe650407
TM
4379 int rpc_status;
4380};
4381
fe650407
TM
4382static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4383{
4384 struct nfs4_delegreturndata *data = calldata;
938e1010 4385
14516c3a
TM
4386 if (!nfs4_sequence_done(task, &data->res.seq_res))
4387 return;
938e1010 4388
79708861
RL
4389 switch (task->tk_status) {
4390 case -NFS4ERR_STALE_STATEID:
4391 case -NFS4ERR_EXPIRED:
4392 case 0:
fa178f29 4393 renew_lease(data->res.server, data->timestamp);
79708861
RL
4394 break;
4395 default:
4396 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4397 -EAGAIN) {
d00c5d43 4398 rpc_restart_call_prepare(task);
79708861
RL
4399 return;
4400 }
4401 }
4402 data->rpc_status = task->tk_status;
fe650407
TM
4403}
4404
4405static void nfs4_delegreturn_release(void *calldata)
4406{
fe650407
TM
4407 kfree(calldata);
4408}
4409
938e1010
AA
4410#if defined(CONFIG_NFS_V4_1)
4411static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4412{
4413 struct nfs4_delegreturndata *d_data;
4414
4415 d_data = (struct nfs4_delegreturndata *)data;
4416
035168ab 4417 if (nfs4_setup_sequence(d_data->res.server,
938e1010 4418 &d_data->args.seq_args,
9d12b216 4419 &d_data->res.seq_res, task))
938e1010
AA
4420 return;
4421 rpc_call_start(task);
4422}
4423#endif /* CONFIG_NFS_V4_1 */
4424
c8d149f3 4425static const struct rpc_call_ops nfs4_delegreturn_ops = {
938e1010
AA
4426#if defined(CONFIG_NFS_V4_1)
4427 .rpc_call_prepare = nfs4_delegreturn_prepare,
4428#endif /* CONFIG_NFS_V4_1 */
fe650407
TM
4429 .rpc_call_done = nfs4_delegreturn_done,
4430 .rpc_release = nfs4_delegreturn_release,
4431};
4432
e6f81075 4433static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
fe650407
TM
4434{
4435 struct nfs4_delegreturndata *data;
fa178f29 4436 struct nfs_server *server = NFS_SERVER(inode);
fe650407 4437 struct rpc_task *task;
5138fde0
TM
4438 struct rpc_message msg = {
4439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4440 .rpc_cred = cred,
4441 };
c970aa85
TM
4442 struct rpc_task_setup task_setup_data = {
4443 .rpc_client = server->client,
5138fde0 4444 .rpc_message = &msg,
c970aa85
TM
4445 .callback_ops = &nfs4_delegreturn_ops,
4446 .flags = RPC_TASK_ASYNC,
4447 };
e6f81075 4448 int status = 0;
fe650407 4449
8535b2be 4450 data = kzalloc(sizeof(*data), GFP_NOFS);
fe650407
TM
4451 if (data == NULL)
4452 return -ENOMEM;
9d12b216 4453 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
fe650407
TM
4454 data->args.fhandle = &data->fh;
4455 data->args.stateid = &data->stateid;
9e907fec 4456 data->args.bitmask = server->cache_consistency_bitmask;
fe650407 4457 nfs_copy_fh(&data->fh, NFS_FH(inode));
f597c537 4458 nfs4_stateid_copy(&data->stateid, stateid);
fa178f29
TM
4459 data->res.fattr = &data->fattr;
4460 data->res.server = server;
5138fde0 4461 nfs_fattr_init(data->res.fattr);
26e976a8 4462 data->timestamp = jiffies;
fe650407
TM
4463 data->rpc_status = 0;
4464
c970aa85 4465 task_setup_data.callback_data = data;
1174dd1f
TM
4466 msg.rpc_argp = &data->args;
4467 msg.rpc_resp = &data->res;
c970aa85 4468 task = rpc_run_task(&task_setup_data);
7a1218a2 4469 if (IS_ERR(task))
fe650407 4470 return PTR_ERR(task);
e6f81075
TM
4471 if (!issync)
4472 goto out;
fe650407 4473 status = nfs4_wait_for_completion_rpc_task(task);
e6f81075
TM
4474 if (status != 0)
4475 goto out;
4476 status = data->rpc_status;
e144cbcc
TM
4477 if (status == 0)
4478 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4479 else
4480 nfs_refresh_inode(inode, &data->fattr);
e6f81075 4481out:
e6b3c4db 4482 rpc_put_task(task);
fe650407 4483 return status;
1da177e4
LT
4484}
4485
e6f81075 4486int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
1da177e4
LT
4487{
4488 struct nfs_server *server = NFS_SERVER(inode);
4489 struct nfs4_exception exception = { };
4490 int err;
4491 do {
e6f81075 4492 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
1da177e4
LT
4493 switch (err) {
4494 case -NFS4ERR_STALE_STATEID:
4495 case -NFS4ERR_EXPIRED:
1da177e4
LT
4496 case 0:
4497 return 0;
4498 }
4499 err = nfs4_handle_exception(server, err, &exception);
4500 } while (exception.retry);
4501 return err;
4502}
4503
4504#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4505#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4506
4507/*
4508 * sleep, with exponential backoff, and retry the LOCK operation.
4509 */
4510static unsigned long
4511nfs4_set_lock_task_retry(unsigned long timeout)
4512{
d310310c 4513 freezable_schedule_timeout_killable(timeout);
1da177e4
LT
4514 timeout <<= 1;
4515 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4516 return NFS4_LOCK_MAXTIMEOUT;
4517 return timeout;
4518}
4519
1da177e4
LT
4520static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4521{
4522 struct inode *inode = state->inode;
4523 struct nfs_server *server = NFS_SERVER(inode);
7539bbab 4524 struct nfs_client *clp = server->nfs_client;
911d1aaf 4525 struct nfs_lockt_args arg = {
1da177e4 4526 .fh = NFS_FH(inode),
911d1aaf 4527 .fl = request,
1da177e4 4528 };
911d1aaf
TM
4529 struct nfs_lockt_res res = {
4530 .denied = request,
1da177e4
LT
4531 };
4532 struct rpc_message msg = {
4533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4534 .rpc_argp = &arg,
4535 .rpc_resp = &res,
4536 .rpc_cred = state->owner->so_cred,
4537 };
1da177e4
LT
4538 struct nfs4_lock_state *lsp;
4539 int status;
4540
911d1aaf 4541 arg.lock_owner.clientid = clp->cl_clientid;
8d0a8a9d
TM
4542 status = nfs4_set_lock_state(state, request);
4543 if (status != 0)
4544 goto out;
4545 lsp = request->fl_u.nfs4_fl.owner;
48c22eb2 4546 arg.lock_owner.id = lsp->ls_seqid.owner_id;
d035c36c 4547 arg.lock_owner.s_dev = server->s_dev;
7c513058 4548 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
911d1aaf
TM
4549 switch (status) {
4550 case 0:
4551 request->fl_type = F_UNLCK;
4552 break;
4553 case -NFS4ERR_DENIED:
4554 status = 0;
1da177e4 4555 }
70cc6487 4556 request->fl_ops->fl_release_private(request);
8d0a8a9d 4557out:
1da177e4
LT
4558 return status;
4559}
4560
4561static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4562{
4563 struct nfs4_exception exception = { };
4564 int err;
4565
4566 do {
4567 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4568 _nfs4_proc_getlk(state, cmd, request),
4569 &exception);
4570 } while (exception.retry);
4571 return err;
4572}
4573
4574static int do_vfs_lock(struct file *file, struct file_lock *fl)
4575{
4576 int res = 0;
4577 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4578 case FL_POSIX:
4579 res = posix_lock_file_wait(file, fl);
4580 break;
4581 case FL_FLOCK:
4582 res = flock_lock_file_wait(file, fl);
4583 break;
4584 default:
4585 BUG();
4586 }
1da177e4
LT
4587 return res;
4588}
4589
faf5f49c 4590struct nfs4_unlockdata {
911d1aaf
TM
4591 struct nfs_locku_args arg;
4592 struct nfs_locku_res res;
faf5f49c
TM
4593 struct nfs4_lock_state *lsp;
4594 struct nfs_open_context *ctx;
911d1aaf
TM
4595 struct file_lock fl;
4596 const struct nfs_server *server;
26e976a8 4597 unsigned long timestamp;
faf5f49c
TM
4598};
4599
911d1aaf
TM
4600static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4601 struct nfs_open_context *ctx,
4602 struct nfs4_lock_state *lsp,
4603 struct nfs_seqid *seqid)
4604{
4605 struct nfs4_unlockdata *p;
4606 struct inode *inode = lsp->ls_state->inode;
4607
8535b2be 4608 p = kzalloc(sizeof(*p), GFP_NOFS);
911d1aaf
TM
4609 if (p == NULL)
4610 return NULL;
4611 p->arg.fh = NFS_FH(inode);
4612 p->arg.fl = &p->fl;
4613 p->arg.seqid = seqid;
c1d51931 4614 p->res.seqid = seqid;
911d1aaf
TM
4615 p->arg.stateid = &lsp->ls_stateid;
4616 p->lsp = lsp;
4617 atomic_inc(&lsp->ls_count);
4618 /* Ensure we don't close file until we're done freeing locks! */
4619 p->ctx = get_nfs_open_context(ctx);
4620 memcpy(&p->fl, fl, sizeof(p->fl));
4621 p->server = NFS_SERVER(inode);
4622 return p;
4623}
4624
06f814a3 4625static void nfs4_locku_release_calldata(void *data)
faf5f49c 4626{
963d8fe5 4627 struct nfs4_unlockdata *calldata = data;
911d1aaf 4628 nfs_free_seqid(calldata->arg.seqid);
06f814a3
TM
4629 nfs4_put_lock_state(calldata->lsp);
4630 put_nfs_open_context(calldata->ctx);
4631 kfree(calldata);
faf5f49c
TM
4632}
4633
963d8fe5 4634static void nfs4_locku_done(struct rpc_task *task, void *data)
faf5f49c 4635{
963d8fe5 4636 struct nfs4_unlockdata *calldata = data;
faf5f49c 4637
14516c3a
TM
4638 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4639 return;
faf5f49c
TM
4640 switch (task->tk_status) {
4641 case 0:
f597c537
TM
4642 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4643 &calldata->res.stateid);
26e976a8 4644 renew_lease(calldata->server, calldata->timestamp);
faf5f49c 4645 break;
9e33bed5
TM
4646 case -NFS4ERR_BAD_STATEID:
4647 case -NFS4ERR_OLD_STATEID:
faf5f49c
TM
4648 case -NFS4ERR_STALE_STATEID:
4649 case -NFS4ERR_EXPIRED:
faf5f49c
TM
4650 break;
4651 default:
9e33bed5 4652 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
d00c5d43 4653 rpc_restart_call_prepare(task);
faf5f49c 4654 }
2b1bc308 4655 nfs_release_seqid(calldata->arg.seqid);
faf5f49c
TM
4656}
4657
4ce70ada 4658static void nfs4_locku_prepare(struct rpc_task *task, void *data)
faf5f49c 4659{
4ce70ada 4660 struct nfs4_unlockdata *calldata = data;
faf5f49c 4661
911d1aaf 4662 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
faf5f49c 4663 return;
795a88c9 4664 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
963d8fe5
TM
4665 /* Note: exit _without_ running nfs4_locku_done */
4666 task->tk_action = NULL;
faf5f49c
TM
4667 return;
4668 }
26e976a8 4669 calldata->timestamp = jiffies;
035168ab 4670 if (nfs4_setup_sequence(calldata->server,
a893693c 4671 &calldata->arg.seq_args,
2240a9e2
TM
4672 &calldata->res.seq_res,
4673 task) != 0)
4674 nfs_release_seqid(calldata->arg.seqid);
4675 else
4676 rpc_call_start(task);
faf5f49c
TM
4677}
4678
963d8fe5 4679static const struct rpc_call_ops nfs4_locku_ops = {
4ce70ada 4680 .rpc_call_prepare = nfs4_locku_prepare,
963d8fe5 4681 .rpc_call_done = nfs4_locku_done,
06f814a3 4682 .rpc_release = nfs4_locku_release_calldata,
963d8fe5
TM
4683};
4684
a5d16a4d
TM
4685static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4686 struct nfs_open_context *ctx,
4687 struct nfs4_lock_state *lsp,
4688 struct nfs_seqid *seqid)
4689{
4690 struct nfs4_unlockdata *data;
5138fde0
TM
4691 struct rpc_message msg = {
4692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4693 .rpc_cred = ctx->cred,
4694 };
c970aa85
TM
4695 struct rpc_task_setup task_setup_data = {
4696 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5138fde0 4697 .rpc_message = &msg,
c970aa85 4698 .callback_ops = &nfs4_locku_ops,
101070ca 4699 .workqueue = nfsiod_workqueue,
c970aa85
TM
4700 .flags = RPC_TASK_ASYNC,
4701 };
a5d16a4d 4702
137d6aca
FF
4703 /* Ensure this is an unlock - when canceling a lock, the
4704 * canceled lock is passed in, and it won't be an unlock.
4705 */
4706 fl->fl_type = F_UNLCK;
4707
a5d16a4d
TM
4708 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4709 if (data == NULL) {
4710 nfs_free_seqid(seqid);
4711 return ERR_PTR(-ENOMEM);
4712 }
4713
9d12b216 4714 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
1174dd1f
TM
4715 msg.rpc_argp = &data->arg;
4716 msg.rpc_resp = &data->res;
c970aa85
TM
4717 task_setup_data.callback_data = data;
4718 return rpc_run_task(&task_setup_data);
a5d16a4d
TM
4719}
4720
faf5f49c
TM
4721static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4722{
19e03c57 4723 struct nfs_inode *nfsi = NFS_I(state->inode);
911d1aaf 4724 struct nfs_seqid *seqid;
1da177e4 4725 struct nfs4_lock_state *lsp;
06f814a3
TM
4726 struct rpc_task *task;
4727 int status = 0;
536ff0f8 4728 unsigned char fl_flags = request->fl_flags;
faf5f49c 4729
8d0a8a9d 4730 status = nfs4_set_lock_state(state, request);
9b073574
TM
4731 /* Unlock _before_ we do the RPC call */
4732 request->fl_flags |= FL_EXISTS;
19e03c57
TM
4733 down_read(&nfsi->rwsem);
4734 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4735 up_read(&nfsi->rwsem);
9b073574 4736 goto out;
19e03c57
TM
4737 }
4738 up_read(&nfsi->rwsem);
8d0a8a9d 4739 if (status != 0)
9b073574
TM
4740 goto out;
4741 /* Is this a delegated lock? */
4742 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4743 goto out;
8d0a8a9d 4744 lsp = request->fl_u.nfs4_fl.owner;
8535b2be 4745 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
9b073574 4746 status = -ENOMEM;
911d1aaf 4747 if (seqid == NULL)
9b073574 4748 goto out;
cd3758e3 4749 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
a5d16a4d
TM
4750 status = PTR_ERR(task);
4751 if (IS_ERR(task))
9b073574 4752 goto out;
a5d16a4d 4753 status = nfs4_wait_for_completion_rpc_task(task);
e6b3c4db 4754 rpc_put_task(task);
9b073574 4755out:
536ff0f8 4756 request->fl_flags = fl_flags;
1da177e4
LT
4757 return status;
4758}
4759
a5d16a4d
TM
4760struct nfs4_lockdata {
4761 struct nfs_lock_args arg;
4762 struct nfs_lock_res res;
4763 struct nfs4_lock_state *lsp;
4764 struct nfs_open_context *ctx;
4765 struct file_lock fl;
26e976a8 4766 unsigned long timestamp;
a5d16a4d
TM
4767 int rpc_status;
4768 int cancelled;
66179efe 4769 struct nfs_server *server;
a5d16a4d
TM
4770};
4771
4772static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
8535b2be
TM
4773 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4774 gfp_t gfp_mask)
1da177e4 4775{
a5d16a4d
TM
4776 struct nfs4_lockdata *p;
4777 struct inode *inode = lsp->ls_state->inode;
1da177e4 4778 struct nfs_server *server = NFS_SERVER(inode);
a5d16a4d 4779
8535b2be 4780 p = kzalloc(sizeof(*p), gfp_mask);
a5d16a4d
TM
4781 if (p == NULL)
4782 return NULL;
4783
4784 p->arg.fh = NFS_FH(inode);
4785 p->arg.fl = &p->fl;
8535b2be 4786 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
2f74c0a0
TM
4787 if (p->arg.open_seqid == NULL)
4788 goto out_free;
8535b2be 4789 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
a5d16a4d 4790 if (p->arg.lock_seqid == NULL)
2f74c0a0 4791 goto out_free_seqid;
a5d16a4d 4792 p->arg.lock_stateid = &lsp->ls_stateid;
7539bbab 4793 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
48c22eb2 4794 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
d035c36c 4795 p->arg.lock_owner.s_dev = server->s_dev;
c1d51931 4796 p->res.lock_seqid = p->arg.lock_seqid;
a5d16a4d 4797 p->lsp = lsp;
66179efe 4798 p->server = server;
a5d16a4d
TM
4799 atomic_inc(&lsp->ls_count);
4800 p->ctx = get_nfs_open_context(ctx);
4801 memcpy(&p->fl, fl, sizeof(p->fl));
4802 return p;
2f74c0a0
TM
4803out_free_seqid:
4804 nfs_free_seqid(p->arg.open_seqid);
a5d16a4d
TM
4805out_free:
4806 kfree(p);
4807 return NULL;
4808}
4809
4810static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4811{
4812 struct nfs4_lockdata *data = calldata;
4813 struct nfs4_state *state = data->lsp->ls_state;
06735b34 4814
3110ff80 4815 dprintk("%s: begin!\n", __func__);
2f74c0a0
TM
4816 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4817 return;
a5d16a4d
TM
4818 /* Do we need to do an open_to_lock_owner? */
4819 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
e6e21970 4820 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
2240a9e2 4821 goto out_release_lock_seqid;
a5d16a4d
TM
4822 data->arg.open_stateid = &state->stateid;
4823 data->arg.new_lock_owner = 1;
c1d51931 4824 data->res.open_seqid = data->arg.open_seqid;
2f74c0a0
TM
4825 } else
4826 data->arg.new_lock_owner = 0;
26e976a8 4827 data->timestamp = jiffies;
035168ab
TM
4828 if (nfs4_setup_sequence(data->server,
4829 &data->arg.seq_args,
2240a9e2
TM
4830 &data->res.seq_res,
4831 task) == 0) {
4832 rpc_call_start(task);
66179efe 4833 return;
2240a9e2
TM
4834 }
4835 nfs_release_seqid(data->arg.open_seqid);
4836out_release_lock_seqid:
4837 nfs_release_seqid(data->arg.lock_seqid);
4838 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
a5d16a4d
TM
4839}
4840
b257957e
AB
4841static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4842{
4843 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4844 nfs4_lock_prepare(task, calldata);
4845}
4846
a5d16a4d
TM
4847static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4848{
4849 struct nfs4_lockdata *data = calldata;
4850
3110ff80 4851 dprintk("%s: begin!\n", __func__);
a5d16a4d 4852
14516c3a
TM
4853 if (!nfs4_sequence_done(task, &data->res.seq_res))
4854 return;
66179efe 4855
a5d16a4d 4856 data->rpc_status = task->tk_status;
a5d16a4d 4857 if (data->arg.new_lock_owner != 0) {
a5d16a4d
TM
4858 if (data->rpc_status == 0)
4859 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4860 else
4861 goto out;
4862 }
4863 if (data->rpc_status == 0) {
f597c537 4864 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
795a88c9 4865 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
3d4ff43d 4866 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
a5d16a4d 4867 }
a5d16a4d 4868out:
3110ff80 4869 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
a5d16a4d
TM
4870}
4871
4872static void nfs4_lock_release(void *calldata)
4873{
4874 struct nfs4_lockdata *data = calldata;
4875
3110ff80 4876 dprintk("%s: begin!\n", __func__);
2f74c0a0 4877 nfs_free_seqid(data->arg.open_seqid);
a5d16a4d
TM
4878 if (data->cancelled != 0) {
4879 struct rpc_task *task;
4880 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4881 data->arg.lock_seqid);
4882 if (!IS_ERR(task))
bf294b41 4883 rpc_put_task_async(task);
3110ff80 4884 dprintk("%s: cancelling lock!\n", __func__);
a5d16a4d
TM
4885 } else
4886 nfs_free_seqid(data->arg.lock_seqid);
4887 nfs4_put_lock_state(data->lsp);
4888 put_nfs_open_context(data->ctx);
4889 kfree(data);
3110ff80 4890 dprintk("%s: done!\n", __func__);
a5d16a4d
TM
4891}
4892
4893static const struct rpc_call_ops nfs4_lock_ops = {
4894 .rpc_call_prepare = nfs4_lock_prepare,
4895 .rpc_call_done = nfs4_lock_done,
4896 .rpc_release = nfs4_lock_release,
4897};
4898
b257957e
AB
4899static const struct rpc_call_ops nfs4_recover_lock_ops = {
4900 .rpc_call_prepare = nfs4_recover_lock_prepare,
4901 .rpc_call_done = nfs4_lock_done,
4902 .rpc_release = nfs4_lock_release,
4903};
4904
2bee72a6
TM
4905static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4906{
2bee72a6
TM
4907 switch (error) {
4908 case -NFS4ERR_ADMIN_REVOKED:
4909 case -NFS4ERR_BAD_STATEID:
ecac799a 4910 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
2bee72a6 4911 if (new_lock_owner != 0 ||
795a88c9 4912 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
ecac799a 4913 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
a2c0b9e2
TM
4914 break;
4915 case -NFS4ERR_STALE_STATEID:
a2c0b9e2 4916 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
ecac799a
TM
4917 case -NFS4ERR_EXPIRED:
4918 nfs4_schedule_lease_recovery(server->nfs_client);
2bee72a6
TM
4919 };
4920}
4921
afe6c27c 4922static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
a5d16a4d
TM
4923{
4924 struct nfs4_lockdata *data;
4925 struct rpc_task *task;
5138fde0
TM
4926 struct rpc_message msg = {
4927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4928 .rpc_cred = state->owner->so_cred,
4929 };
c970aa85
TM
4930 struct rpc_task_setup task_setup_data = {
4931 .rpc_client = NFS_CLIENT(state->inode),
5138fde0 4932 .rpc_message = &msg,
c970aa85 4933 .callback_ops = &nfs4_lock_ops,
101070ca 4934 .workqueue = nfsiod_workqueue,
c970aa85
TM
4935 .flags = RPC_TASK_ASYNC,
4936 };
a5d16a4d
TM
4937 int ret;
4938
3110ff80 4939 dprintk("%s: begin!\n", __func__);
cd3758e3 4940 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
8535b2be
TM
4941 fl->fl_u.nfs4_fl.owner,
4942 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
a5d16a4d
TM
4943 if (data == NULL)
4944 return -ENOMEM;
4945 if (IS_SETLKW(cmd))
4946 data->arg.block = 1;
b257957e
AB
4947 if (recovery_type > NFS_LOCK_NEW) {
4948 if (recovery_type == NFS_LOCK_RECLAIM)
afe6c27c 4949 data->arg.reclaim = NFS_LOCK_RECLAIM;
b257957e
AB
4950 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4951 }
9d12b216 4952 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
1174dd1f
TM
4953 msg.rpc_argp = &data->arg;
4954 msg.rpc_resp = &data->res;
c970aa85
TM
4955 task_setup_data.callback_data = data;
4956 task = rpc_run_task(&task_setup_data);
7a1218a2 4957 if (IS_ERR(task))
a5d16a4d 4958 return PTR_ERR(task);
a5d16a4d
TM
4959 ret = nfs4_wait_for_completion_rpc_task(task);
4960 if (ret == 0) {
4961 ret = data->rpc_status;
2bee72a6
TM
4962 if (ret)
4963 nfs4_handle_setlk_error(data->server, data->lsp,
4964 data->arg.new_lock_owner, ret);
a5d16a4d
TM
4965 } else
4966 data->cancelled = 1;
e6b3c4db 4967 rpc_put_task(task);
3110ff80 4968 dprintk("%s: done, ret = %d!\n", __func__, ret);
a5d16a4d 4969 return ret;
1da177e4
LT
4970}
4971
4972static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4973{
202b50dc 4974 struct nfs_server *server = NFS_SERVER(state->inode);
05ffe24f
TM
4975 struct nfs4_exception exception = {
4976 .inode = state->inode,
4977 };
202b50dc
TM
4978 int err;
4979
4980 do {
42a2d13e
TM
4981 /* Cache the lock if possible... */
4982 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4983 return 0;
afe6c27c 4984 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
168667c4 4985 if (err != -NFS4ERR_DELAY)
202b50dc
TM
4986 break;
4987 nfs4_handle_exception(server, err, &exception);
4988 } while (exception.retry);
4989 return err;
1da177e4
LT
4990}
4991
4992static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4993{
202b50dc 4994 struct nfs_server *server = NFS_SERVER(state->inode);
05ffe24f
TM
4995 struct nfs4_exception exception = {
4996 .inode = state->inode,
4997 };
202b50dc
TM
4998 int err;
4999
6bfc93ef
TM
5000 err = nfs4_set_lock_state(state, request);
5001 if (err != 0)
5002 return err;
202b50dc 5003 do {
42a2d13e
TM
5004 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5005 return 0;
afe6c27c 5006 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
a9ed2e25
TM
5007 switch (err) {
5008 default:
5009 goto out;
5010 case -NFS4ERR_GRACE:
5011 case -NFS4ERR_DELAY:
5012 nfs4_handle_exception(server, err, &exception);
5013 err = 0;
5014 }
202b50dc 5015 } while (exception.retry);
a9ed2e25 5016out:
202b50dc 5017 return err;
1da177e4
LT
5018}
5019
f062eb6c 5020#if defined(CONFIG_NFS_V4_1)
3e60ffdd
CL
5021/**
5022 * nfs41_check_expired_locks - possibly free a lock stateid
5023 *
5024 * @state: NFSv4 state for an inode
5025 *
5026 * Returns NFS_OK if recovery for this stateid is now finished.
5027 * Otherwise a negative NFS4ERR value is returned.
5028 */
b01dd1d8 5029static int nfs41_check_expired_locks(struct nfs4_state *state)
f062eb6c 5030{
eb64cf96 5031 int status, ret = -NFS4ERR_BAD_STATEID;
b01dd1d8 5032 struct nfs4_lock_state *lsp;
f062eb6c
BS
5033 struct nfs_server *server = NFS_SERVER(state->inode);
5034
b01dd1d8 5035 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
795a88c9 5036 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
b01dd1d8
BS
5037 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5038 if (status != NFS_OK) {
3e60ffdd
CL
5039 /* Free the stateid unless the server
5040 * informs us the stateid is unrecognized. */
89af2739
CL
5041 if (status != -NFS4ERR_BAD_STATEID)
5042 nfs41_free_stateid(server,
5043 &lsp->ls_stateid);
795a88c9 5044 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
b01dd1d8
BS
5045 ret = status;
5046 }
5047 }
5048 };
5049
5050 return ret;
5051}
5052
5053static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5054{
5055 int status = NFS_OK;
5056
5057 if (test_bit(LK_STATE_IN_USE, &state->flags))
5058 status = nfs41_check_expired_locks(state);
eb64cf96
CL
5059 if (status != NFS_OK)
5060 status = nfs4_lock_expired(state, request);
5061 return status;
f062eb6c
BS
5062}
5063#endif
5064
1da177e4
LT
5065static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5066{
19e03c57 5067 struct nfs_inode *nfsi = NFS_I(state->inode);
01c3b861 5068 unsigned char fl_flags = request->fl_flags;
8e469ebd 5069 int status = -ENOLCK;
1da177e4 5070
8e469ebd
TM
5071 if ((fl_flags & FL_POSIX) &&
5072 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5073 goto out;
6bfc93ef 5074 /* Is this a delegated open? */
6bfc93ef
TM
5075 status = nfs4_set_lock_state(state, request);
5076 if (status != 0)
5077 goto out;
01c3b861
TM
5078 request->fl_flags |= FL_ACCESS;
5079 status = do_vfs_lock(request->fl_file, request);
5080 if (status < 0)
5081 goto out;
19e03c57 5082 down_read(&nfsi->rwsem);
01c3b861 5083 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
01c3b861 5084 /* Yes: cache locks! */
01c3b861 5085 /* ...but avoid races with delegation recall... */
19e03c57
TM
5086 request->fl_flags = fl_flags & ~FL_SLEEP;
5087 status = do_vfs_lock(request->fl_file, request);
5088 goto out_unlock;
01c3b861 5089 }
afe6c27c 5090 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6bfc93ef 5091 if (status != 0)
01c3b861 5092 goto out_unlock;
6bfc93ef 5093 /* Note: we always want to sleep here! */
01c3b861 5094 request->fl_flags = fl_flags | FL_SLEEP;
6bfc93ef 5095 if (do_vfs_lock(request->fl_file, request) < 0)
a030889a
WAA
5096 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5097 "manager!\n", __func__);
01c3b861 5098out_unlock:
19e03c57 5099 up_read(&nfsi->rwsem);
01c3b861
TM
5100out:
5101 request->fl_flags = fl_flags;
1da177e4
LT
5102 return status;
5103}
5104
5105static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5106{
a1d0b5ee
TM
5107 struct nfs4_exception exception = {
5108 .state = state,
05ffe24f 5109 .inode = state->inode,
a1d0b5ee 5110 };
1da177e4
LT
5111 int err;
5112
5113 do {
965b5d67
TM
5114 err = _nfs4_proc_setlk(state, cmd, request);
5115 if (err == -NFS4ERR_DENIED)
5116 err = -EAGAIN;
1da177e4 5117 err = nfs4_handle_exception(NFS_SERVER(state->inode),
965b5d67 5118 err, &exception);
1da177e4
LT
5119 } while (exception.retry);
5120 return err;
5121}
5122
5123static int
5124nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5125{
5126 struct nfs_open_context *ctx;
5127 struct nfs4_state *state;
5128 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5129 int status;
5130
5131 /* verify open state */
cd3758e3 5132 ctx = nfs_file_open_context(filp);
1da177e4
LT
5133 state = ctx->state;
5134
5135 if (request->fl_start < 0 || request->fl_end < 0)
5136 return -EINVAL;
5137
d953126a
TM
5138 if (IS_GETLK(cmd)) {
5139 if (state != NULL)
5140 return nfs4_proc_getlk(state, F_GETLK, request);
5141 return 0;
5142 }
1da177e4
LT
5143
5144 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5145 return -EINVAL;
5146
d953126a
TM
5147 if (request->fl_type == F_UNLCK) {
5148 if (state != NULL)
5149 return nfs4_proc_unlck(state, cmd, request);
5150 return 0;
5151 }
1da177e4 5152
d953126a
TM
5153 if (state == NULL)
5154 return -ENOLCK;
55725513
TM
5155 /*
5156 * Don't rely on the VFS having checked the file open mode,
5157 * since it won't do this for flock() locks.
5158 */
f44106e2 5159 switch (request->fl_type) {
55725513
TM
5160 case F_RDLCK:
5161 if (!(filp->f_mode & FMODE_READ))
5162 return -EBADF;
5163 break;
5164 case F_WRLCK:
5165 if (!(filp->f_mode & FMODE_WRITE))
5166 return -EBADF;
5167 }
5168
1da177e4
LT
5169 do {
5170 status = nfs4_proc_setlk(state, cmd, request);
5171 if ((status != -EAGAIN) || IS_SETLK(cmd))
5172 break;
5173 timeout = nfs4_set_lock_task_retry(timeout);
5174 status = -ERESTARTSYS;
5175 if (signalled())
5176 break;
5177 } while(status < 0);
1da177e4
LT
5178 return status;
5179}
5180
888e694c
TM
5181int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5182{
5183 struct nfs_server *server = NFS_SERVER(state->inode);
5184 struct nfs4_exception exception = { };
5185 int err;
5186
5187 err = nfs4_set_lock_state(state, fl);
5188 if (err != 0)
5189 goto out;
5190 do {
afe6c27c 5191 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
d5122201
TM
5192 switch (err) {
5193 default:
a030889a
WAA
5194 printk(KERN_ERR "NFS: %s: unhandled error "
5195 "%d.\n", __func__, err);
d5122201 5196 case 0:
965b5d67 5197 case -ESTALE:
d5122201
TM
5198 goto out;
5199 case -NFS4ERR_EXPIRED:
0ced63d1 5200 nfs4_schedule_stateid_recovery(server, state);
d5122201 5201 case -NFS4ERR_STALE_CLIENTID:
965b5d67 5202 case -NFS4ERR_STALE_STATEID:
0400a6b0
TM
5203 nfs4_schedule_lease_recovery(server->nfs_client);
5204 goto out;
74e7bb73
RL
5205 case -NFS4ERR_BADSESSION:
5206 case -NFS4ERR_BADSLOT:
5207 case -NFS4ERR_BAD_HIGH_SLOT:
5208 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5209 case -NFS4ERR_DEADSESSION:
9f594791 5210 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
d5122201 5211 goto out;
965b5d67
TM
5212 case -ERESTARTSYS:
5213 /*
5214 * The show must go on: exit, but mark the
5215 * stateid as needing recovery.
5216 */
a1d0b5ee 5217 case -NFS4ERR_DELEG_REVOKED:
965b5d67
TM
5218 case -NFS4ERR_ADMIN_REVOKED:
5219 case -NFS4ERR_BAD_STATEID:
5220 case -NFS4ERR_OPENMODE:
0400a6b0 5221 nfs4_schedule_stateid_recovery(server, state);
965b5d67
TM
5222 err = 0;
5223 goto out;
168667c4
TM
5224 case -EKEYEXPIRED:
5225 /*
5226 * User RPCSEC_GSS context has expired.
5227 * We cannot recover this stateid now, so
5228 * skip it and allow recovery thread to
5229 * proceed.
5230 */
5231 err = 0;
5232 goto out;
965b5d67
TM
5233 case -ENOMEM:
5234 case -NFS4ERR_DENIED:
5235 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5236 err = 0;
5237 goto out;
d5122201
TM
5238 case -NFS4ERR_DELAY:
5239 break;
5240 }
888e694c
TM
5241 err = nfs4_handle_exception(server, err, &exception);
5242 } while (exception.retry);
5243out:
5244 return err;
5245}
6b3b5496 5246
cf470c3e
TM
5247struct nfs_release_lockowner_data {
5248 struct nfs4_lock_state *lsp;
5ae67c4f 5249 struct nfs_server *server;
cf470c3e
TM
5250 struct nfs_release_lockowner_args args;
5251};
5252
d3c7b7cc
TM
5253static void nfs4_release_lockowner_release(void *calldata)
5254{
cf470c3e 5255 struct nfs_release_lockowner_data *data = calldata;
5ae67c4f 5256 nfs4_free_lock_state(data->server, data->lsp);
d3c7b7cc
TM
5257 kfree(calldata);
5258}
5259
17280175 5260static const struct rpc_call_ops nfs4_release_lockowner_ops = {
d3c7b7cc
TM
5261 .rpc_release = nfs4_release_lockowner_release,
5262};
5263
cf470c3e 5264int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
d3c7b7cc
TM
5265{
5266 struct nfs_server *server = lsp->ls_state->owner->so_server;
cf470c3e 5267 struct nfs_release_lockowner_data *data;
d3c7b7cc
TM
5268 struct rpc_message msg = {
5269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5270 };
5271
5272 if (server->nfs_client->cl_mvops->minor_version != 0)
cf470c3e
TM
5273 return -EINVAL;
5274 data = kmalloc(sizeof(*data), GFP_NOFS);
5275 if (!data)
5276 return -ENOMEM;
5277 data->lsp = lsp;
5ae67c4f 5278 data->server = server;
cf470c3e
TM
5279 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5280 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5281 data->args.lock_owner.s_dev = server->s_dev;
5282 msg.rpc_argp = &data->args;
5283 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5284 return 0;
d3c7b7cc
TM
5285}
5286
aa1870af
BF
5287#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5288
64c2ce8b
AK
5289static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5290 const void *buf, size_t buflen,
5291 int flags, int type)
6b3b5496 5292{
64c2ce8b
AK
5293 if (strcmp(key, "") != 0)
5294 return -EINVAL;
4b580ee3 5295
64c2ce8b 5296 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
6b3b5496
BF
5297}
5298
64c2ce8b
AK
5299static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5300 void *buf, size_t buflen, int type)
6b3b5496 5301{
64c2ce8b
AK
5302 if (strcmp(key, "") != 0)
5303 return -EINVAL;
aa1870af 5304
64c2ce8b 5305 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
6b3b5496
BF
5306}
5307
64c2ce8b
AK
5308static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5309 size_t list_len, const char *name,
5310 size_t name_len, int type)
6b3b5496 5311{
64c2ce8b 5312 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
6b3b5496 5313
096455a2
BF
5314 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5315 return 0;
64c2ce8b
AK
5316
5317 if (list && len <= list_len)
5318 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
aa1870af 5319 return len;
6b3b5496
BF
5320}
5321
533eb461
AA
5322/*
5323 * nfs_fhget will use either the mounted_on_fileid or the fileid
5324 */
69aaaae1
TM
5325static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5326{
533eb461
AA
5327 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5328 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5329 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
81934ddb 5330 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
69aaaae1
TM
5331 return;
5332
5333 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
81934ddb 5334 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
69aaaae1
TM
5335 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5336 fattr->nlink = 2;
5337}
5338
f05d147f
BS
5339static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5340 const struct qstr *name,
5341 struct nfs4_fs_locations *fs_locations,
5342 struct page *page)
683b57b4
TM
5343{
5344 struct nfs_server *server = NFS_SERVER(dir);
5345 u32 bitmask[2] = {
361e624f 5346 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
683b57b4
TM
5347 };
5348 struct nfs4_fs_locations_arg args = {
5349 .dir_fh = NFS_FH(dir),
c228fd3a 5350 .name = name,
683b57b4
TM
5351 .page = page,
5352 .bitmask = bitmask,
5353 };
22958463
BH
5354 struct nfs4_fs_locations_res res = {
5355 .fs_locations = fs_locations,
5356 };
683b57b4
TM
5357 struct rpc_message msg = {
5358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5359 .rpc_argp = &args,
22958463 5360 .rpc_resp = &res,
683b57b4
TM
5361 };
5362 int status;
5363
3110ff80 5364 dprintk("%s: start\n", __func__);
533eb461
AA
5365
5366 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5367 * is not supported */
5368 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5369 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5370 else
5371 bitmask[0] |= FATTR4_WORD0_FILEID;
5372
c228fd3a 5373 nfs_fattr_init(&fs_locations->fattr);
683b57b4 5374 fs_locations->server = server;
830b8e33 5375 fs_locations->nlocations = 0;
f05d147f 5376 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
3110ff80 5377 dprintk("%s: returned status = %d\n", __func__, status);
683b57b4
TM
5378 return status;
5379}
5380
f05d147f
BS
5381int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5382 const struct qstr *name,
5383 struct nfs4_fs_locations *fs_locations,
5384 struct page *page)
db0a9593
BS
5385{
5386 struct nfs4_exception exception = { };
5387 int err;
5388 do {
5389 err = nfs4_handle_exception(NFS_SERVER(dir),
f05d147f 5390 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
db0a9593
BS
5391 &exception);
5392 } while (exception.retry);
5393 return err;
5394}
5395
5a5ea0d4
BS
5396static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5397{
5398 int status;
5399 struct nfs4_secinfo_arg args = {
5400 .dir_fh = NFS_FH(dir),
5401 .name = name,
5402 };
5403 struct nfs4_secinfo_res res = {
5404 .flavors = flavors,
5405 };
5406 struct rpc_message msg = {
5407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5408 .rpc_argp = &args,
5409 .rpc_resp = &res,
5410 };
5411
5412 dprintk("NFS call secinfo %s\n", name->name);
5413 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5414 dprintk("NFS reply secinfo: %d\n", status);
5415 return status;
5416}
5417
72de53ec
BS
5418int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5419 struct nfs4_secinfo_flavors *flavors)
5a5ea0d4
BS
5420{
5421 struct nfs4_exception exception = { };
5422 int err;
5423 do {
5424 err = nfs4_handle_exception(NFS_SERVER(dir),
5425 _nfs4_proc_secinfo(dir, name, flavors),
5426 &exception);
5427 } while (exception.retry);
5428 return err;
5429}
5430
557134a3 5431#ifdef CONFIG_NFS_V4_1
357f54d6
AA
5432/*
5433 * Check the exchange flags returned by the server for invalid flags, having
5434 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5435 * DS flags set.
5436 */
5437static int nfs4_check_cl_exchange_flags(u32 flags)
5438{
5439 if (flags & ~EXCHGID4_FLAG_MASK_R)
5440 goto out_inval;
5441 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5442 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5443 goto out_inval;
5444 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5445 goto out_inval;
5446 return NFS_OK;
5447out_inval:
5448 return -NFS4ERR_INVAL;
5449}
5450
78fe0f41 5451static bool
79d4e1f0
CL
5452nfs41_same_server_scope(struct nfs41_server_scope *a,
5453 struct nfs41_server_scope *b)
78fe0f41
WAA
5454{
5455 if (a->server_scope_sz == b->server_scope_sz &&
5456 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5457 return true;
5458
5459 return false;
5460}
5461
7c44f1ae
WAA
5462/*
5463 * nfs4_proc_bind_conn_to_session()
5464 *
5465 * The 4.1 client currently uses the same TCP connection for the
5466 * fore and backchannel.
5467 */
2cf047c9 5468int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7c44f1ae
WAA
5469{
5470 int status;
5471 struct nfs41_bind_conn_to_session_res res;
5472 struct rpc_message msg = {
5473 .rpc_proc =
5474 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5475 .rpc_argp = clp,
5476 .rpc_resp = &res,
2cf047c9 5477 .rpc_cred = cred,
7c44f1ae
WAA
5478 };
5479
5480 dprintk("--> %s\n", __func__);
7c44f1ae
WAA
5481
5482 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5483 if (unlikely(res.session == NULL)) {
5484 status = -ENOMEM;
5485 goto out;
5486 }
5487
5488 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5489 if (status == 0) {
5490 if (memcmp(res.session->sess_id.data,
5491 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5492 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5493 status = -EIO;
5494 goto out_session;
5495 }
5496 if (res.dir != NFS4_CDFS4_BOTH) {
5497 dprintk("NFS: %s: Unexpected direction from server\n",
5498 __func__);
5499 status = -EIO;
5500 goto out_session;
5501 }
5502 if (res.use_conn_in_rdma_mode) {
5503 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5504 __func__);
5505 status = -EIO;
5506 goto out_session;
5507 }
5508 }
5509out_session:
5510 kfree(res.session);
5511out:
5512 dprintk("<-- %s status= %d\n", __func__, status);
5513 return status;
5514}
5515
99fe60d0
BH
5516/*
5517 * nfs4_proc_exchange_id()
5518 *
6bbb4ae8
CL
5519 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5520 *
99fe60d0
BH
5521 * Since the clientid has expired, all compounds using sessions
5522 * associated with the stale clientid will be returning
5523 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5524 * be in some phase of session reset.
5525 */
4d643d1d 5526int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
99fe60d0
BH
5527{
5528 nfs4_verifier verifier;
5529 struct nfs41_exchange_id_args args = {
cd93710e 5530 .verifier = &verifier,
99fe60d0 5531 .client = clp,
357f54d6 5532 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
99fe60d0
BH
5533 };
5534 struct nfs41_exchange_id_res res = {
32b01310 5535 0
99fe60d0
BH
5536 };
5537 int status;
5538 struct rpc_message msg = {
5539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5540 .rpc_argp = &args,
5541 .rpc_resp = &res,
5542 .rpc_cred = cred,
5543 };
99fe60d0 5544
f092075d 5545 nfs4_init_boot_verifier(clp, &verifier);
e984a55a
CL
5546 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5547 sizeof(args.id));
6bbb4ae8
CL
5548 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5549 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5550 args.id_len, args.id);
99fe60d0 5551
acdeb69d 5552 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
bbafffd2 5553 GFP_NOFS);
acdeb69d 5554 if (unlikely(res.server_owner == NULL)) {
abe9a6d5
WAA
5555 status = -ENOMEM;
5556 goto out;
5557 }
78fe0f41 5558
79d4e1f0 5559 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
bbafffd2 5560 GFP_NOFS);
177313f1 5561 if (unlikely(res.server_scope == NULL)) {
abe9a6d5 5562 status = -ENOMEM;
acdeb69d 5563 goto out_server_owner;
abe9a6d5 5564 }
78fe0f41 5565
bbafffd2 5566 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
177313f1 5567 if (unlikely(res.impl_id == NULL)) {
7d2ed9ac
WAA
5568 status = -ENOMEM;
5569 goto out_server_scope;
5570 }
5571
1bd714f2 5572 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
177313f1 5573 if (status == 0)
32b01310 5574 status = nfs4_check_cl_exchange_flags(res.flags);
78fe0f41 5575
acdeb69d 5576 if (status == 0) {
32b01310
TM
5577 clp->cl_clientid = res.clientid;
5578 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5579 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5580 clp->cl_seqid = res.seqid;
5581
acdeb69d
CL
5582 kfree(clp->cl_serverowner);
5583 clp->cl_serverowner = res.server_owner;
5584 res.server_owner = NULL;
78fe0f41 5585
7d2ed9ac 5586 /* use the most recent implementation id */
59155546
CL
5587 kfree(clp->cl_implid);
5588 clp->cl_implid = res.impl_id;
7d2ed9ac 5589
177313f1 5590 if (clp->cl_serverscope != NULL &&
79d4e1f0 5591 !nfs41_same_server_scope(clp->cl_serverscope,
78fe0f41
WAA
5592 res.server_scope)) {
5593 dprintk("%s: server_scope mismatch detected\n",
5594 __func__);
5595 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
79d4e1f0
CL
5596 kfree(clp->cl_serverscope);
5597 clp->cl_serverscope = NULL;
78fe0f41
WAA
5598 }
5599
177313f1 5600 if (clp->cl_serverscope == NULL) {
79d4e1f0 5601 clp->cl_serverscope = res.server_scope;
abe9a6d5
WAA
5602 goto out;
5603 }
32b01310
TM
5604 } else
5605 kfree(res.impl_id);
7d2ed9ac 5606
acdeb69d
CL
5607out_server_owner:
5608 kfree(res.server_owner);
7d2ed9ac 5609out_server_scope:
abe9a6d5
WAA
5610 kfree(res.server_scope);
5611out:
177313f1 5612 if (clp->cl_implid != NULL)
6bbb4ae8 5613 dprintk("NFS reply exchange_id: Server Implementation ID: "
7d2ed9ac 5614 "domain: %s, name: %s, date: %llu,%u\n",
6bbb4ae8 5615 clp->cl_implid->domain, clp->cl_implid->name,
59155546
CL
5616 clp->cl_implid->date.seconds,
5617 clp->cl_implid->date.nseconds);
6bbb4ae8 5618 dprintk("NFS reply exchange_id: %d\n", status);
99fe60d0
BH
5619 return status;
5620}
5621
66245539
TM
5622static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5623 struct rpc_cred *cred)
5624{
5625 struct rpc_message msg = {
5626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5627 .rpc_argp = clp,
5628 .rpc_cred = cred,
5629 };
5630 int status;
5631
5632 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5633 if (status)
02c67525 5634 dprintk("NFS: Got error %d from the server %s on "
66245539
TM
5635 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5636 return status;
5637}
5638
5639static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5640 struct rpc_cred *cred)
5641{
5642 unsigned int loop;
5643 int ret;
5644
5645 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5646 ret = _nfs4_proc_destroy_clientid(clp, cred);
5647 switch (ret) {
5648 case -NFS4ERR_DELAY:
5649 case -NFS4ERR_CLIENTID_BUSY:
5650 ssleep(1);
5651 break;
5652 default:
5653 return ret;
5654 }
5655 }
5656 return 0;
5657}
5658
5659int nfs4_destroy_clientid(struct nfs_client *clp)
5660{
5661 struct rpc_cred *cred;
5662 int ret = 0;
5663
5664 if (clp->cl_mvops->minor_version < 1)
5665 goto out;
5666 if (clp->cl_exchange_flags == 0)
5667 goto out;
05f4c350
CL
5668 if (clp->cl_preserve_clid)
5669 goto out;
66245539
TM
5670 cred = nfs4_get_exchange_id_cred(clp);
5671 ret = nfs4_proc_destroy_clientid(clp, cred);
5672 if (cred)
5673 put_rpccred(cred);
5674 switch (ret) {
5675 case 0:
5676 case -NFS4ERR_STALE_CLIENTID:
5677 clp->cl_exchange_flags = 0;
5678 }
5679out:
5680 return ret;
5681}
5682
2050f0cc
AA
5683struct nfs4_get_lease_time_data {
5684 struct nfs4_get_lease_time_args *args;
5685 struct nfs4_get_lease_time_res *res;
5686 struct nfs_client *clp;
5687};
5688
5689static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5690 void *calldata)
5691{
5692 int ret;
5693 struct nfs4_get_lease_time_data *data =
5694 (struct nfs4_get_lease_time_data *)calldata;
5695
5696 dprintk("--> %s\n", __func__);
689cf5c1 5697 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
2050f0cc
AA
5698 /* just setup sequence, do not trigger session recovery
5699 since we're invoked within one */
5700 ret = nfs41_setup_sequence(data->clp->cl_session,
689cf5c1 5701 &data->args->la_seq_args,
9d12b216 5702 &data->res->lr_seq_res, task);
2050f0cc 5703
d3edcf96
TM
5704 if (ret != -EAGAIN)
5705 rpc_call_start(task);
2050f0cc
AA
5706 dprintk("<-- %s\n", __func__);
5707}
5708
5709/*
5710 * Called from nfs4_state_manager thread for session setup, so don't recover
5711 * from sequence operation or clientid errors.
5712 */
5713static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5714{
5715 struct nfs4_get_lease_time_data *data =
5716 (struct nfs4_get_lease_time_data *)calldata;
5717
5718 dprintk("--> %s\n", __func__);
14516c3a
TM
5719 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5720 return;
2050f0cc
AA
5721 switch (task->tk_status) {
5722 case -NFS4ERR_DELAY:
5723 case -NFS4ERR_GRACE:
5724 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5725 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5726 task->tk_status = 0;
a8a4ae3a
AA
5727 /* fall through */
5728 case -NFS4ERR_RETRY_UNCACHED_REP:
d00c5d43 5729 rpc_restart_call_prepare(task);
2050f0cc
AA
5730 return;
5731 }
2050f0cc
AA
5732 dprintk("<-- %s\n", __func__);
5733}
5734
17280175 5735static const struct rpc_call_ops nfs4_get_lease_time_ops = {
2050f0cc
AA
5736 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5737 .rpc_call_done = nfs4_get_lease_time_done,
5738};
5739
5740int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5741{
5742 struct rpc_task *task;
5743 struct nfs4_get_lease_time_args args;
5744 struct nfs4_get_lease_time_res res = {
5745 .lr_fsinfo = fsinfo,
5746 };
5747 struct nfs4_get_lease_time_data data = {
5748 .args = &args,
5749 .res = &res,
5750 .clp = clp,
5751 };
5752 struct rpc_message msg = {
5753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5754 .rpc_argp = &args,
5755 .rpc_resp = &res,
5756 };
5757 struct rpc_task_setup task_setup = {
5758 .rpc_client = clp->cl_rpcclient,
5759 .rpc_message = &msg,
5760 .callback_ops = &nfs4_get_lease_time_ops,
1bd714f2
TM
5761 .callback_data = &data,
5762 .flags = RPC_TASK_TIMEOUT,
2050f0cc
AA
5763 };
5764 int status;
5765
9d12b216 5766 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
2050f0cc
AA
5767 dprintk("--> %s\n", __func__);
5768 task = rpc_run_task(&task_setup);
5769
5770 if (IS_ERR(task))
5771 status = PTR_ERR(task);
5772 else {
5773 status = task->tk_status;
5774 rpc_put_task(task);
5775 }
5776 dprintk("<-- %s return %d\n", __func__, status);
5777
5778 return status;
5779}
5780
87dda67e
TM
5781static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
5782 u32 max_reqs, u32 ivalue)
f86f36a6 5783{
87dda67e
TM
5784 if (max_reqs <= tbl->max_slots)
5785 return 0;
69d206b5 5786 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
87dda67e
TM
5787 return 0;
5788 return -ENOMEM;
f86f36a6
TM
5789}
5790
87dda67e
TM
5791static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
5792 u32 server_highest_slotid,
f86f36a6
TM
5793 u32 ivalue)
5794{
87dda67e 5795 struct nfs4_slot **p;
f86f36a6 5796
87dda67e
TM
5797 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
5798 p = &tbl->slots;
5799 while (*p) {
5800 (*p)->seq_nr = ivalue;
5801 p = &(*p)->next;
f86f36a6 5802 }
f9b1ef5f 5803 tbl->highest_used_slotid = NFS4_NO_SLOT;
87dda67e
TM
5804 tbl->target_highest_slotid = server_highest_slotid;
5805 tbl->server_highest_slotid = server_highest_slotid;
5806 tbl->max_slotid = server_highest_slotid;
f86f36a6
TM
5807}
5808
b73dafa7 5809/*
f86f36a6 5810 * (re)Initialise a slot table
b73dafa7 5811 */
87dda67e
TM
5812static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
5813 u32 max_reqs, u32 ivalue)
ac72b7b3 5814{
87dda67e 5815 int ret;
ac72b7b3 5816
104aeba4
AA
5817 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5818 max_reqs, tbl->max_slots);
ac72b7b3 5819
87dda67e
TM
5820 if (max_reqs > NFS4_MAX_SLOT_TABLE)
5821 max_reqs = NFS4_MAX_SLOT_TABLE;
5822
5823 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
5824 if (ret)
5825 goto out;
5826
5827 spin_lock(&tbl->slot_tbl_lock);
5828 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
5829 spin_unlock(&tbl->slot_tbl_lock);
f86f36a6 5830
ac72b7b3
AA
5831 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5832 tbl, tbl->slots, tbl->max_slots);
5833out:
5834 dprintk("<-- %s: return %d\n", __func__, ret);
5835 return ret;
5836}
5837
f8625a6a
RL
5838/* Destroy the slot table */
5839static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5840{
87dda67e
TM
5841 nfs4_shrink_slot_table(&session->fc_slot_table, 0);
5842 nfs4_shrink_slot_table(&session->bc_slot_table, 0);
f8625a6a
RL
5843}
5844
050047ce 5845/*
aacd5537 5846 * Initialize or reset the forechannel and backchannel tables
050047ce 5847 */
aacd5537 5848static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
050047ce 5849{
f26468fb 5850 struct nfs4_slot_table *tbl;
aacd5537 5851 int status;
050047ce 5852
aacd5537
AA
5853 dprintk("--> %s\n", __func__);
5854 /* Fore channel */
5855 tbl = &ses->fc_slot_table;
e3725ec0 5856 tbl->session = ses;
f86f36a6
TM
5857 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5858 if (status) /* -ENOMEM */
5859 return status;
aacd5537
AA
5860 /* Back channel */
5861 tbl = &ses->bc_slot_table;
e3725ec0 5862 tbl->session = ses;
f86f36a6
TM
5863 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5864 if (status && tbl->slots == NULL)
5865 /* Fore and back channel share a connection so get
5866 * both slot tables or neither */
5867 nfs4_destroy_slot_tables(ses);
f8625a6a 5868 return status;
557134a3
AA
5869}
5870
5871struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5872{
5873 struct nfs4_session *session;
5874 struct nfs4_slot_table *tbl;
5875
8535b2be 5876 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
557134a3
AA
5877 if (!session)
5878 return NULL;
76db6d95 5879
557134a3 5880 tbl = &session->fc_slot_table;
45d43c29 5881 tbl->highest_used_slotid = NFS4_NO_SLOT;
557134a3 5882 spin_lock_init(&tbl->slot_tbl_lock);
689cf5c1 5883 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
42acd021 5884 init_completion(&tbl->complete);
f8625a6a
RL
5885
5886 tbl = &session->bc_slot_table;
45d43c29 5887 tbl->highest_used_slotid = NFS4_NO_SLOT;
f8625a6a
RL
5888 spin_lock_init(&tbl->slot_tbl_lock);
5889 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
42acd021 5890 init_completion(&tbl->complete);
f8625a6a 5891
1055d76d
TM
5892 session->session_state = 1<<NFS4_SESSION_INITING;
5893
557134a3
AA
5894 session->clp = clp;
5895 return session;
5896}
5897
5898void nfs4_destroy_session(struct nfs4_session *session)
5899{
2446ab60 5900 struct rpc_xprt *xprt;
848f5bda 5901 struct rpc_cred *cred;
2446ab60 5902
848f5bda
TM
5903 cred = nfs4_get_exchange_id_cred(session->clp);
5904 nfs4_proc_destroy_session(session, cred);
5905 if (cred)
5906 put_rpccred(cred);
2446ab60
TM
5907
5908 rcu_read_lock();
5909 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5910 rcu_read_unlock();
5a0ffe54 5911 dprintk("%s Destroy backchannel for xprt %p\n",
2446ab60
TM
5912 __func__, xprt);
5913 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
f8625a6a 5914 nfs4_destroy_slot_tables(session);
557134a3
AA
5915 kfree(session);
5916}
5917
fc931582
AA
5918/*
5919 * Initialize the values to be used by the client in CREATE_SESSION
5920 * If nfs4_init_session set the fore channel request and response sizes,
5921 * use them.
5922 *
5923 * Set the back channel max_resp_sz_cached to zero to force the client to
5924 * always set csa_cachethis to FALSE because the current implementation
5925 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5926 */
5927static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5928{
5929 struct nfs4_session *session = args->client->cl_session;
ae72ae67
TM
5930 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5931 mxresp_sz = session->fc_target_max_resp_sz;
fc931582
AA
5932
5933 if (mxrqst_sz == 0)
5934 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5935 if (mxresp_sz == 0)
5936 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5937 /* Fore channel attributes */
fc931582
AA
5938 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5939 args->fc_attrs.max_resp_sz = mxresp_sz;
fc931582 5940 args->fc_attrs.max_ops = NFS4_MAX_OPS;
ef159e91 5941 args->fc_attrs.max_reqs = max_session_slots;
fc931582
AA
5942
5943 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8e0d46e1 5944 "max_ops=%u max_reqs=%u\n",
fc931582
AA
5945 __func__,
5946 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8e0d46e1 5947 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
fc931582
AA
5948
5949 /* Back channel attributes */
fc931582
AA
5950 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5951 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5952 args->bc_attrs.max_resp_sz_cached = 0;
5953 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5954 args->bc_attrs.max_reqs = 1;
5955
5956 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5957 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5958 __func__,
5959 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5960 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5961 args->bc_attrs.max_reqs);
5962}
5963
43c2e885 5964static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
8d35301d 5965{
43c2e885
BF
5966 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5967 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5968
43c2e885
BF
5969 if (rcvd->max_resp_sz > sent->max_resp_sz)
5970 return -EINVAL;
5971 /*
5972 * Our requested max_ops is the minimum we need; we're not
5973 * prepared to break up compounds into smaller pieces than that.
5974 * So, no point even trying to continue if the server won't
5975 * cooperate:
5976 */
5977 if (rcvd->max_ops < sent->max_ops)
5978 return -EINVAL;
5979 if (rcvd->max_reqs == 0)
5980 return -EINVAL;
b4b9a0c1
VG
5981 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5982 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
43c2e885 5983 return 0;
8d35301d
AA
5984}
5985
43c2e885
BF
5986static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5987{
5988 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5989 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
8d35301d 5990
43c2e885
BF
5991 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5992 return -EINVAL;
5993 if (rcvd->max_resp_sz < sent->max_resp_sz)
5994 return -EINVAL;
5995 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5996 return -EINVAL;
5997 /* These would render the backchannel useless: */
b4b9a0c1 5998 if (rcvd->max_ops != sent->max_ops)
43c2e885 5999 return -EINVAL;
b4b9a0c1 6000 if (rcvd->max_reqs != sent->max_reqs)
43c2e885
BF
6001 return -EINVAL;
6002 return 0;
6003}
8d35301d 6004
8d35301d
AA
6005static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6006 struct nfs4_session *session)
6007{
43c2e885 6008 int ret;
8d35301d 6009
43c2e885
BF
6010 ret = nfs4_verify_fore_channel_attrs(args, session);
6011 if (ret)
6012 return ret;
6013 return nfs4_verify_back_channel_attrs(args, session);
8d35301d
AA
6014}
6015
848f5bda
TM
6016static int _nfs4_proc_create_session(struct nfs_client *clp,
6017 struct rpc_cred *cred)
fc931582
AA
6018{
6019 struct nfs4_session *session = clp->cl_session;
6020 struct nfs41_create_session_args args = {
6021 .client = clp,
6022 .cb_program = NFS4_CALLBACK,
6023 };
6024 struct nfs41_create_session_res res = {
6025 .client = clp,
6026 };
6027 struct rpc_message msg = {
6028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6029 .rpc_argp = &args,
6030 .rpc_resp = &res,
848f5bda 6031 .rpc_cred = cred,
fc931582
AA
6032 };
6033 int status;
6034
6035 nfs4_init_channel_attrs(&args);
0f91421e 6036 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
fc931582 6037
1bd714f2 6038 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
fc931582 6039
43095d39 6040 if (!status) {
8d35301d
AA
6041 /* Verify the session's negotiated channel_attrs values */
6042 status = nfs4_verify_channel_attrs(&args, session);
fc931582
AA
6043 /* Increment the clientid slot sequence id */
6044 clp->cl_seqid++;
6045 }
6046
6047 return status;
6048}
6049
6050/*
6051 * Issues a CREATE_SESSION operation to the server.
6052 * It is the responsibility of the caller to verify the session is
6053 * expired before calling this routine.
6054 */
848f5bda 6055int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
fc931582
AA
6056{
6057 int status;
6058 unsigned *ptr;
fc931582
AA
6059 struct nfs4_session *session = clp->cl_session;
6060
6061 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6062
848f5bda 6063 status = _nfs4_proc_create_session(clp, cred);
fc931582
AA
6064 if (status)
6065 goto out;
6066
aacd5537
AA
6067 /* Init or reset the session slot tables */
6068 status = nfs4_setup_session_slot_tables(session);
6069 dprintk("slot table setup returned %d\n", status);
fc931582
AA
6070 if (status)
6071 goto out;
6072
6073 ptr = (unsigned *)&session->sess_id.data[0];
6074 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6075 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
fc931582
AA
6076out:
6077 dprintk("<-- %s\n", __func__);
6078 return status;
6079}
6080
0f3e66c6
AA
6081/*
6082 * Issue the over-the-wire RPC DESTROY_SESSION.
6083 * The caller must serialize access to this routine.
6084 */
848f5bda
TM
6085int nfs4_proc_destroy_session(struct nfs4_session *session,
6086 struct rpc_cred *cred)
0f3e66c6 6087{
848f5bda
TM
6088 struct rpc_message msg = {
6089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6090 .rpc_argp = session,
6091 .rpc_cred = cred,
6092 };
0f3e66c6 6093 int status = 0;
0f3e66c6
AA
6094
6095 dprintk("--> nfs4_proc_destroy_session\n");
6096
6097 /* session is still being setup */
6098 if (session->clp->cl_cons_state != NFS_CS_READY)
6099 return status;
6100
1bd714f2 6101 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
0f3e66c6
AA
6102
6103 if (status)
08106ac7 6104 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
0f3e66c6
AA
6105 "Session has been destroyed regardless...\n", status);
6106
6107 dprintk("<-- nfs4_proc_destroy_session\n");
6108 return status;
6109}
6110
7b38c368
TM
6111/*
6112 * With sessions, the client is not marked ready until after a
6113 * successful EXCHANGE_ID and CREATE_SESSION.
6114 *
6115 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6116 * other versions of NFS can be tried.
6117 */
6118static int nfs41_check_session_ready(struct nfs_client *clp)
6119{
6120 int ret;
6121
6122 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6123 ret = nfs4_client_recover_expired_lease(clp);
6124 if (ret)
6125 return ret;
6126 }
6127 if (clp->cl_cons_state < NFS_CS_READY)
6128 return -EPROTONOSUPPORT;
54ac471c 6129 smp_rmb();
7b38c368
TM
6130 return 0;
6131}
6132
fccba804
TM
6133int nfs4_init_session(struct nfs_server *server)
6134{
6135 struct nfs_client *clp = server->nfs_client;
2449ea2e 6136 struct nfs4_session *session;
ae72ae67
TM
6137 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6138 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
fccba804
TM
6139
6140 if (!nfs4_has_session(clp))
6141 return 0;
6142
ae72ae67
TM
6143 if (server->rsize != 0)
6144 target_max_resp_sz = server->rsize;
6145 target_max_resp_sz += nfs41_maxread_overhead;
6146
6147 if (server->wsize != 0)
6148 target_max_rqst_sz = server->wsize;
6149 target_max_rqst_sz += nfs41_maxwrite_overhead;
6150
1055d76d 6151 session = clp->cl_session;
7b38c368
TM
6152 spin_lock(&clp->cl_lock);
6153 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
ae72ae67
TM
6154 /* Initialise targets and channel attributes */
6155 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6156 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6157 session->fc_target_max_resp_sz = target_max_resp_sz;
6158 session->fc_attrs.max_resp_sz = target_max_resp_sz;
688a9024
TM
6159 } else {
6160 /* Just adjust the targets */
6161 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6162 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6163 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6164 }
6165 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6166 session->fc_target_max_resp_sz = target_max_resp_sz;
6167 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6168 }
7b38c368
TM
6169 }
6170 spin_unlock(&clp->cl_lock);
2449ea2e 6171
688a9024
TM
6172 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6173 nfs4_schedule_lease_recovery(clp);
6174
7b38c368 6175 return nfs41_check_session_ready(clp);
fccba804
TM
6176}
6177
7b38c368 6178int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
d83217c1
AA
6179{
6180 struct nfs4_session *session = clp->cl_session;
6181 int ret;
6182
7b38c368
TM
6183 spin_lock(&clp->cl_lock);
6184 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6185 /*
6186 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6187 * DS lease to be equal to the MDS lease.
6188 */
6189 clp->cl_lease_time = lease_time;
6190 clp->cl_last_renewal = jiffies;
6191 }
6192 spin_unlock(&clp->cl_lock);
d83217c1 6193
7b38c368
TM
6194 ret = nfs41_check_session_ready(clp);
6195 if (ret)
6196 return ret;
6197 /* Test for the DS role */
6198 if (!is_ds_client(clp))
6199 return -ENODEV;
6200 return 0;
d83217c1
AA
6201}
6202EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6203
6204
fc01cea9
AA
6205/*
6206 * Renew the cl_session lease.
6207 */
d5f8d3fe
TM
6208struct nfs4_sequence_data {
6209 struct nfs_client *clp;
6210 struct nfs4_sequence_args args;
6211 struct nfs4_sequence_res res;
6212};
6213
dc96aef9
AB
6214static void nfs41_sequence_release(void *data)
6215{
d5f8d3fe
TM
6216 struct nfs4_sequence_data *calldata = data;
6217 struct nfs_client *clp = calldata->clp;
dc96aef9 6218
7135840f
AB
6219 if (atomic_read(&clp->cl_count) > 1)
6220 nfs4_schedule_state_renewal(clp);
6221 nfs_put_client(clp);
d5f8d3fe 6222 kfree(calldata);
dc96aef9
AB
6223}
6224
aa5190d0
TM
6225static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6226{
6227 switch(task->tk_status) {
6228 case -NFS4ERR_DELAY:
aa5190d0
TM
6229 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6230 return -EAGAIN;
6231 default:
0400a6b0 6232 nfs4_schedule_lease_recovery(clp);
aa5190d0
TM
6233 }
6234 return 0;
6235}
6236
dc96aef9 6237static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
fc01cea9 6238{
d5f8d3fe
TM
6239 struct nfs4_sequence_data *calldata = data;
6240 struct nfs_client *clp = calldata->clp;
fc01cea9 6241
14516c3a
TM
6242 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6243 return;
fc01cea9
AA
6244
6245 if (task->tk_status < 0) {
6246 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7135840f
AB
6247 if (atomic_read(&clp->cl_count) == 1)
6248 goto out;
fc01cea9 6249
aa5190d0
TM
6250 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6251 rpc_restart_call_prepare(task);
fc01cea9
AA
6252 return;
6253 }
6254 }
fc01cea9 6255 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7135840f 6256out:
fc01cea9
AA
6257 dprintk("<-- %s\n", __func__);
6258}
6259
6260static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6261{
d5f8d3fe
TM
6262 struct nfs4_sequence_data *calldata = data;
6263 struct nfs_client *clp = calldata->clp;
fc01cea9
AA
6264 struct nfs4_sequence_args *args;
6265 struct nfs4_sequence_res *res;
6266
fc01cea9
AA
6267 args = task->tk_msg.rpc_argp;
6268 res = task->tk_msg.rpc_resp;
6269
9d12b216 6270 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
fc01cea9
AA
6271 return;
6272 rpc_call_start(task);
6273}
6274
6bdb5f21
BS
6275static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6276{
6277 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6278 nfs41_sequence_prepare(task, data);
6279}
6280
fc01cea9
AA
6281static const struct rpc_call_ops nfs41_sequence_ops = {
6282 .rpc_call_done = nfs41_sequence_call_done,
6283 .rpc_call_prepare = nfs41_sequence_prepare,
dc96aef9 6284 .rpc_release = nfs41_sequence_release,
fc01cea9
AA
6285};
6286
6bdb5f21
BS
6287static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6288 .rpc_call_done = nfs41_sequence_call_done,
6289 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6290 .rpc_release = nfs41_sequence_release,
6291};
6292
6293static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6294 const struct rpc_call_ops *seq_ops)
fc01cea9 6295{
d5f8d3fe 6296 struct nfs4_sequence_data *calldata;
fc01cea9
AA
6297 struct rpc_message msg = {
6298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6299 .rpc_cred = cred,
6300 };
71ac6da9
TM
6301 struct rpc_task_setup task_setup_data = {
6302 .rpc_client = clp->cl_rpcclient,
6303 .rpc_message = &msg,
6bdb5f21 6304 .callback_ops = seq_ops,
71ac6da9
TM
6305 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6306 };
fc01cea9 6307
7135840f 6308 if (!atomic_inc_not_zero(&clp->cl_count))
71ac6da9 6309 return ERR_PTR(-EIO);
dfb4f309 6310 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
d5f8d3fe 6311 if (calldata == NULL) {
7135840f 6312 nfs_put_client(clp);
71ac6da9 6313 return ERR_PTR(-ENOMEM);
fc01cea9 6314 }
9d12b216 6315 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
d5f8d3fe
TM
6316 msg.rpc_argp = &calldata->args;
6317 msg.rpc_resp = &calldata->res;
6318 calldata->clp = clp;
71ac6da9 6319 task_setup_data.callback_data = calldata;
fc01cea9 6320
71ac6da9
TM
6321 return rpc_run_task(&task_setup_data);
6322}
6323
2f60ea6b 6324static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
71ac6da9
TM
6325{
6326 struct rpc_task *task;
6327 int ret = 0;
6328
2f60ea6b
TM
6329 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6330 return 0;
6bdb5f21 6331 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
71ac6da9
TM
6332 if (IS_ERR(task))
6333 ret = PTR_ERR(task);
6334 else
bf294b41 6335 rpc_put_task_async(task);
71ac6da9
TM
6336 dprintk("<-- %s status=%d\n", __func__, ret);
6337 return ret;
6338}
6339
6340static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6341{
6342 struct rpc_task *task;
6343 int ret;
6344
6bdb5f21 6345 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
71ac6da9
TM
6346 if (IS_ERR(task)) {
6347 ret = PTR_ERR(task);
6348 goto out;
6349 }
6350 ret = rpc_wait_for_completion_task(task);
b4410c2f
TM
6351 if (!ret) {
6352 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6353
6354 if (task->tk_status == 0)
6355 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
71ac6da9 6356 ret = task->tk_status;
b4410c2f 6357 }
71ac6da9
TM
6358 rpc_put_task(task);
6359out:
6360 dprintk("<-- %s status=%d\n", __func__, ret);
6361 return ret;
fc01cea9
AA
6362}
6363
fce5c838
RL
6364struct nfs4_reclaim_complete_data {
6365 struct nfs_client *clp;
6366 struct nfs41_reclaim_complete_args arg;
6367 struct nfs41_reclaim_complete_res res;
6368};
6369
6370static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6371{
6372 struct nfs4_reclaim_complete_data *calldata = data;
6373
b257957e 6374 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
035168ab
TM
6375 if (nfs41_setup_sequence(calldata->clp->cl_session,
6376 &calldata->arg.seq_args,
9d12b216 6377 &calldata->res.seq_res, task))
fce5c838
RL
6378 return;
6379
6380 rpc_call_start(task);
6381}
6382
aa5190d0
TM
6383static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6384{
6385 switch(task->tk_status) {
6386 case 0:
6387 case -NFS4ERR_COMPLETE_ALREADY:
6388 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6389 break;
6390 case -NFS4ERR_DELAY:
aa5190d0 6391 rpc_delay(task, NFS4_POLL_RETRY_MAX);
a8a4ae3a
AA
6392 /* fall through */
6393 case -NFS4ERR_RETRY_UNCACHED_REP:
aa5190d0
TM
6394 return -EAGAIN;
6395 default:
0400a6b0 6396 nfs4_schedule_lease_recovery(clp);
aa5190d0
TM
6397 }
6398 return 0;
6399}
6400
fce5c838
RL
6401static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6402{
6403 struct nfs4_reclaim_complete_data *calldata = data;
6404 struct nfs_client *clp = calldata->clp;
6405 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6406
6407 dprintk("--> %s\n", __func__);
14516c3a
TM
6408 if (!nfs41_sequence_done(task, res))
6409 return;
fce5c838 6410
aa5190d0
TM
6411 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6412 rpc_restart_call_prepare(task);
6413 return;
6414 }
fce5c838
RL
6415 dprintk("<-- %s\n", __func__);
6416}
6417
6418static void nfs4_free_reclaim_complete_data(void *data)
6419{
6420 struct nfs4_reclaim_complete_data *calldata = data;
6421
6422 kfree(calldata);
6423}
6424
6425static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6426 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6427 .rpc_call_done = nfs4_reclaim_complete_done,
6428 .rpc_release = nfs4_free_reclaim_complete_data,
6429};
6430
6431/*
6432 * Issue a global reclaim complete.
6433 */
6434static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6435{
6436 struct nfs4_reclaim_complete_data *calldata;
6437 struct rpc_task *task;
6438 struct rpc_message msg = {
6439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6440 };
6441 struct rpc_task_setup task_setup_data = {
6442 .rpc_client = clp->cl_rpcclient,
6443 .rpc_message = &msg,
6444 .callback_ops = &nfs4_reclaim_complete_call_ops,
6445 .flags = RPC_TASK_ASYNC,
6446 };
6447 int status = -ENOMEM;
6448
6449 dprintk("--> %s\n", __func__);
8535b2be 6450 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
fce5c838
RL
6451 if (calldata == NULL)
6452 goto out;
6453 calldata->clp = clp;
6454 calldata->arg.one_fs = 0;
fce5c838 6455
9d12b216 6456 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
fce5c838
RL
6457 msg.rpc_argp = &calldata->arg;
6458 msg.rpc_resp = &calldata->res;
6459 task_setup_data.callback_data = calldata;
6460 task = rpc_run_task(&task_setup_data);
acf82b85 6461 if (IS_ERR(task)) {
fce5c838 6462 status = PTR_ERR(task);
acf82b85
DC
6463 goto out;
6464 }
c34c32ea
AA
6465 status = nfs4_wait_for_completion_rpc_task(task);
6466 if (status == 0)
6467 status = task->tk_status;
fce5c838 6468 rpc_put_task(task);
acf82b85 6469 return 0;
fce5c838
RL
6470out:
6471 dprintk("<-- %s status=%d\n", __func__, status);
6472 return status;
6473}
b1f69b75
AA
6474
6475static void
6476nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6477{
6478 struct nfs4_layoutget *lgp = calldata;
c31663d4 6479 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
b1f69b75
AA
6480
6481 dprintk("--> %s\n", __func__);
c31663d4
FI
6482 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6483 * right now covering the LAYOUTGET we are about to send.
6484 * However, that is not so catastrophic, and there seems
6485 * to be no way to prevent it completely.
6486 */
b1f69b75 6487 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
9d12b216 6488 &lgp->res.seq_res, task))
b1f69b75 6489 return;
cf7d63f1
FI
6490 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6491 NFS_I(lgp->args.inode)->layout,
6492 lgp->args.ctx->state)) {
6493 rpc_exit(task, NFS4_OK);
6494 return;
6495 }
b1f69b75
AA
6496 rpc_call_start(task);
6497}
6498
6499static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6500{
6501 struct nfs4_layoutget *lgp = calldata;
ee314c2a
TM
6502 struct inode *inode = lgp->args.inode;
6503 struct nfs_server *server = NFS_SERVER(inode);
6504 struct pnfs_layout_hdr *lo;
6505 struct nfs4_state *state = NULL;
b1f69b75
AA
6506
6507 dprintk("--> %s\n", __func__);
6508
6509 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
ee314c2a 6510 goto out;
b1f69b75
AA
6511
6512 switch (task->tk_status) {
6513 case 0:
ee314c2a 6514 goto out;
b1f69b75
AA
6515 case -NFS4ERR_LAYOUTTRYLATER:
6516 case -NFS4ERR_RECALLCONFLICT:
6517 task->tk_status = -NFS4ERR_DELAY;
ee314c2a
TM
6518 break;
6519 case -NFS4ERR_EXPIRED:
6520 case -NFS4ERR_BAD_STATEID:
6521 spin_lock(&inode->i_lock);
6522 lo = NFS_I(inode)->layout;
6523 if (!lo || list_empty(&lo->plh_segs)) {
6524 spin_unlock(&inode->i_lock);
6525 /* If the open stateid was bad, then recover it. */
6526 state = lgp->args.ctx->state;
6527 } else {
6528 LIST_HEAD(head);
6529
6530 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6531 spin_unlock(&inode->i_lock);
6532 /* Mark the bad layout state as invalid, then
6533 * retry using the open stateid. */
6534 pnfs_free_lseg_list(&head);
b1f69b75
AA
6535 }
6536 }
ee314c2a
TM
6537 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6538 rpc_restart_call_prepare(task);
6539out:
b1f69b75
AA
6540 dprintk("<-- %s\n", __func__);
6541}
6542
8554116e
IK
6543static size_t max_response_pages(struct nfs_server *server)
6544{
6545 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6546 return nfs_page_array_len(0, max_resp_sz);
6547}
6548
6549static void nfs4_free_pages(struct page **pages, size_t size)
6550{
6551 int i;
6552
6553 if (!pages)
6554 return;
6555
6556 for (i = 0; i < size; i++) {
6557 if (!pages[i])
6558 break;
6559 __free_page(pages[i]);
6560 }
6561 kfree(pages);
6562}
6563
6564static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6565{
6566 struct page **pages;
6567 int i;
6568
6569 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6570 if (!pages) {
6571 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6572 return NULL;
6573 }
6574
6575 for (i = 0; i < size; i++) {
6576 pages[i] = alloc_page(gfp_flags);
6577 if (!pages[i]) {
6578 dprintk("%s: failed to allocate page\n", __func__);
6579 nfs4_free_pages(pages, size);
6580 return NULL;
6581 }
6582 }
6583
6584 return pages;
6585}
6586
b1f69b75
AA
6587static void nfs4_layoutget_release(void *calldata)
6588{
6589 struct nfs4_layoutget *lgp = calldata;
8554116e
IK
6590 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6591 size_t max_pages = max_response_pages(server);
b1f69b75
AA
6592
6593 dprintk("--> %s\n", __func__);
8554116e 6594 nfs4_free_pages(lgp->args.layout.pages, max_pages);
b1f69b75
AA
6595 put_nfs_open_context(lgp->args.ctx);
6596 kfree(calldata);
6597 dprintk("<-- %s\n", __func__);
6598}
6599
6600static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6601 .rpc_call_prepare = nfs4_layoutget_prepare,
6602 .rpc_call_done = nfs4_layoutget_done,
6603 .rpc_release = nfs4_layoutget_release,
6604};
6605
a0b0a6e3
TM
6606struct pnfs_layout_segment *
6607nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
b1f69b75
AA
6608{
6609 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8554116e 6610 size_t max_pages = max_response_pages(server);
b1f69b75
AA
6611 struct rpc_task *task;
6612 struct rpc_message msg = {
6613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6614 .rpc_argp = &lgp->args,
6615 .rpc_resp = &lgp->res,
6616 };
6617 struct rpc_task_setup task_setup_data = {
6618 .rpc_client = server->client,
6619 .rpc_message = &msg,
6620 .callback_ops = &nfs4_layoutget_call_ops,
6621 .callback_data = lgp,
6622 .flags = RPC_TASK_ASYNC,
6623 };
a0b0a6e3 6624 struct pnfs_layout_segment *lseg = NULL;
b1f69b75
AA
6625 int status = 0;
6626
6627 dprintk("--> %s\n", __func__);
6628
8554116e
IK
6629 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6630 if (!lgp->args.layout.pages) {
6631 nfs4_layoutget_release(lgp);
a0b0a6e3 6632 return ERR_PTR(-ENOMEM);
8554116e
IK
6633 }
6634 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6635
35124a09 6636 lgp->res.layoutp = &lgp->args.layout;
b1f69b75 6637 lgp->res.seq_res.sr_slot = NULL;
9d12b216 6638 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
b1f69b75
AA
6639 task = rpc_run_task(&task_setup_data);
6640 if (IS_ERR(task))
a0b0a6e3 6641 return ERR_CAST(task);
b1f69b75 6642 status = nfs4_wait_for_completion_rpc_task(task);
c31663d4
FI
6643 if (status == 0)
6644 status = task->tk_status;
6645 if (status == 0)
a0b0a6e3 6646 lseg = pnfs_layout_process(lgp);
b1f69b75
AA
6647 rpc_put_task(task);
6648 dprintk("<-- %s status=%d\n", __func__, status);
a0b0a6e3
TM
6649 if (status)
6650 return ERR_PTR(status);
6651 return lseg;
b1f69b75
AA
6652}
6653
cbe82603
BH
6654static void
6655nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6656{
6657 struct nfs4_layoutreturn *lrp = calldata;
6658
6659 dprintk("--> %s\n", __func__);
6660 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
9d12b216 6661 &lrp->res.seq_res, task))
cbe82603
BH
6662 return;
6663 rpc_call_start(task);
6664}
6665
6666static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6667{
6668 struct nfs4_layoutreturn *lrp = calldata;
6669 struct nfs_server *server;
6670
6671 dprintk("--> %s\n", __func__);
6672
6673 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6674 return;
6675
6676 server = NFS_SERVER(lrp->args.inode);
6677 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
d00c5d43 6678 rpc_restart_call_prepare(task);
cbe82603
BH
6679 return;
6680 }
cbe82603
BH
6681 dprintk("<-- %s\n", __func__);
6682}
6683
6684static void nfs4_layoutreturn_release(void *calldata)
6685{
6686 struct nfs4_layoutreturn *lrp = calldata;
849b286f 6687 struct pnfs_layout_hdr *lo = lrp->args.layout;
cbe82603
BH
6688
6689 dprintk("--> %s\n", __func__);
849b286f
TM
6690 spin_lock(&lo->plh_inode->i_lock);
6691 if (lrp->res.lrs_present)
6692 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6693 lo->plh_block_lgets--;
6694 spin_unlock(&lo->plh_inode->i_lock);
70c3bd2b 6695 pnfs_put_layout_hdr(lrp->args.layout);
cbe82603
BH
6696 kfree(calldata);
6697 dprintk("<-- %s\n", __func__);
6698}
6699
6700static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6701 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6702 .rpc_call_done = nfs4_layoutreturn_done,
6703 .rpc_release = nfs4_layoutreturn_release,
6704};
6705
6706int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6707{
6708 struct rpc_task *task;
6709 struct rpc_message msg = {
6710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6711 .rpc_argp = &lrp->args,
6712 .rpc_resp = &lrp->res,
6713 };
6714 struct rpc_task_setup task_setup_data = {
6715 .rpc_client = lrp->clp->cl_rpcclient,
6716 .rpc_message = &msg,
6717 .callback_ops = &nfs4_layoutreturn_call_ops,
6718 .callback_data = lrp,
6719 };
6720 int status;
6721
6722 dprintk("--> %s\n", __func__);
9d12b216 6723 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
cbe82603
BH
6724 task = rpc_run_task(&task_setup_data);
6725 if (IS_ERR(task))
6726 return PTR_ERR(task);
6727 status = task->tk_status;
6728 dprintk("<-- %s status=%d\n", __func__, status);
6729 rpc_put_task(task);
6730 return status;
6731}
6732
7f11d8d3
AA
6733/*
6734 * Retrieve the list of Data Server devices from the MDS.
6735 */
6736static int _nfs4_getdevicelist(struct nfs_server *server,
6737 const struct nfs_fh *fh,
6738 struct pnfs_devicelist *devlist)
6739{
6740 struct nfs4_getdevicelist_args args = {
6741 .fh = fh,
6742 .layoutclass = server->pnfs_curr_ld->id,
6743 };
6744 struct nfs4_getdevicelist_res res = {
6745 .devlist = devlist,
6746 };
6747 struct rpc_message msg = {
6748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6749 .rpc_argp = &args,
6750 .rpc_resp = &res,
6751 };
6752 int status;
6753
6754 dprintk("--> %s\n", __func__);
6755 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6756 &res.seq_res, 0);
6757 dprintk("<-- %s status=%d\n", __func__, status);
6758 return status;
6759}
6760
6761int nfs4_proc_getdevicelist(struct nfs_server *server,
6762 const struct nfs_fh *fh,
6763 struct pnfs_devicelist *devlist)
6764{
6765 struct nfs4_exception exception = { };
6766 int err;
6767
6768 do {
6769 err = nfs4_handle_exception(server,
6770 _nfs4_getdevicelist(server, fh, devlist),
6771 &exception);
6772 } while (exception.retry);
6773
6774 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6775 err, devlist->num_devs);
6776
6777 return err;
6778}
6779EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6780
b1f69b75
AA
6781static int
6782_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6783{
6784 struct nfs4_getdeviceinfo_args args = {
6785 .pdev = pdev,
6786 };
6787 struct nfs4_getdeviceinfo_res res = {
6788 .pdev = pdev,
6789 };
6790 struct rpc_message msg = {
6791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6792 .rpc_argp = &args,
6793 .rpc_resp = &res,
6794 };
6795 int status;
6796
6797 dprintk("--> %s\n", __func__);
7c513058 6798 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
b1f69b75
AA
6799 dprintk("<-- %s status=%d\n", __func__, status);
6800
6801 return status;
6802}
6803
6804int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6805{
6806 struct nfs4_exception exception = { };
6807 int err;
6808
6809 do {
6810 err = nfs4_handle_exception(server,
6811 _nfs4_proc_getdeviceinfo(server, pdev),
6812 &exception);
6813 } while (exception.retry);
6814 return err;
6815}
6816EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6817
863a3c6c
AA
6818static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6819{
6820 struct nfs4_layoutcommit_data *data = calldata;
6821 struct nfs_server *server = NFS_SERVER(data->args.inode);
6822
6823 if (nfs4_setup_sequence(server, &data->args.seq_args,
9d12b216 6824 &data->res.seq_res, task))
863a3c6c
AA
6825 return;
6826 rpc_call_start(task);
6827}
6828
6829static void
6830nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6831{
6832 struct nfs4_layoutcommit_data *data = calldata;
6833 struct nfs_server *server = NFS_SERVER(data->args.inode);
6834
6835 if (!nfs4_sequence_done(task, &data->res.seq_res))
6836 return;
6837
6838 switch (task->tk_status) { /* Just ignore these failures */
e59d27e0
TM
6839 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6840 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6841 case -NFS4ERR_BADLAYOUT: /* no layout */
6842 case -NFS4ERR_GRACE: /* loca_recalim always false */
863a3c6c 6843 task->tk_status = 0;
e59d27e0
TM
6844 break;
6845 case 0:
863a3c6c
AA
6846 nfs_post_op_update_inode_force_wcc(data->args.inode,
6847 data->res.fattr);
e59d27e0
TM
6848 break;
6849 default:
6850 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6851 rpc_restart_call_prepare(task);
6852 return;
6853 }
6854 }
863a3c6c
AA
6855}
6856
6857static void nfs4_layoutcommit_release(void *calldata)
6858{
6859 struct nfs4_layoutcommit_data *data = calldata;
a9bae566 6860 struct pnfs_layout_segment *lseg, *tmp;
92407e75 6861 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
863a3c6c 6862
db29c089 6863 pnfs_cleanup_layoutcommit(data);
863a3c6c 6864 /* Matched by references in pnfs_set_layoutcommit */
a9bae566
PT
6865 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6866 list_del_init(&lseg->pls_lc_list);
6867 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6868 &lseg->pls_flags))
9369a431 6869 pnfs_put_lseg(lseg);
a9bae566 6870 }
92407e75
PT
6871
6872 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6873 smp_mb__after_clear_bit();
6874 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6875
863a3c6c
AA
6876 put_rpccred(data->cred);
6877 kfree(data);
6878}
6879
6880static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6881 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6882 .rpc_call_done = nfs4_layoutcommit_done,
6883 .rpc_release = nfs4_layoutcommit_release,
6884};
6885
6886int
ef311537 6887nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
863a3c6c
AA
6888{
6889 struct rpc_message msg = {
6890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6891 .rpc_argp = &data->args,
6892 .rpc_resp = &data->res,
6893 .rpc_cred = data->cred,
6894 };
6895 struct rpc_task_setup task_setup_data = {
6896 .task = &data->task,
6897 .rpc_client = NFS_CLIENT(data->args.inode),
6898 .rpc_message = &msg,
6899 .callback_ops = &nfs4_layoutcommit_ops,
6900 .callback_data = data,
6901 .flags = RPC_TASK_ASYNC,
6902 };
6903 struct rpc_task *task;
6904 int status = 0;
6905
6906 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6907 "lbw: %llu inode %lu\n",
6908 data->task.tk_pid, sync,
6909 data->args.lastbytewritten,
6910 data->args.inode->i_ino);
6911
9d12b216 6912 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
863a3c6c
AA
6913 task = rpc_run_task(&task_setup_data);
6914 if (IS_ERR(task))
6915 return PTR_ERR(task);
ef311537 6916 if (sync == false)
863a3c6c
AA
6917 goto out;
6918 status = nfs4_wait_for_completion_rpc_task(task);
6919 if (status != 0)
6920 goto out;
6921 status = task->tk_status;
6922out:
6923 dprintk("%s: status %d\n", __func__, status);
6924 rpc_put_task(task);
6925 return status;
6926}
fca78d6d
BS
6927
6928static int
6929_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6930 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6931{
6932 struct nfs41_secinfo_no_name_args args = {
6933 .style = SECINFO_STYLE_CURRENT_FH,
6934 };
6935 struct nfs4_secinfo_res res = {
6936 .flavors = flavors,
6937 };
6938 struct rpc_message msg = {
6939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6940 .rpc_argp = &args,
6941 .rpc_resp = &res,
6942 };
6943 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6944}
6945
6946static int
6947nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6948 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6949{
6950 struct nfs4_exception exception = { };
6951 int err;
6952 do {
6953 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6954 switch (err) {
6955 case 0:
6956 case -NFS4ERR_WRONGSEC:
6957 case -NFS4ERR_NOTSUPP:
05e9cfb4 6958 goto out;
fca78d6d
BS
6959 default:
6960 err = nfs4_handle_exception(server, err, &exception);
6961 }
6962 } while (exception.retry);
05e9cfb4 6963out:
fca78d6d
BS
6964 return err;
6965}
6966
6967static int
6968nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6969 struct nfs_fsinfo *info)
6970{
6971 int err;
6972 struct page *page;
6973 rpc_authflavor_t flavor;
6974 struct nfs4_secinfo_flavors *flavors;
6975
6976 page = alloc_page(GFP_KERNEL);
6977 if (!page) {
6978 err = -ENOMEM;
6979 goto out;
6980 }
6981
6982 flavors = page_address(page);
6983 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6984
6985 /*
6986 * Fall back on "guess and check" method if
6987 * the server doesn't support SECINFO_NO_NAME
6988 */
6989 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6990 err = nfs4_find_root_sec(server, fhandle, info);
6991 goto out_freepage;
6992 }
6993 if (err)
6994 goto out_freepage;
6995
6996 flavor = nfs_find_best_sec(flavors);
6997 if (err == 0)
6998 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6999
7000out_freepage:
7001 put_page(page);
7002 if (err == -EACCES)
7003 return -EPERM;
7004out:
7005 return err;
7006}
1cab0652
BS
7007
7008static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
7d974794
BS
7009{
7010 int status;
7011 struct nfs41_test_stateid_args args = {
1cab0652 7012 .stateid = stateid,
7d974794
BS
7013 };
7014 struct nfs41_test_stateid_res res;
7015 struct rpc_message msg = {
7016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7017 .rpc_argp = &args,
7018 .rpc_resp = &res,
7019 };
1cab0652 7020
38527b15 7021 dprintk("NFS call test_stateid %p\n", stateid);
9d12b216 7022 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
1cab0652 7023 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
38527b15
CL
7024 if (status != NFS_OK) {
7025 dprintk("NFS reply test_stateid: failed, %d\n", status);
377e507d 7026 return status;
38527b15
CL
7027 }
7028 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
377e507d 7029 return -res.status;
7d974794
BS
7030}
7031
38527b15
CL
7032/**
7033 * nfs41_test_stateid - perform a TEST_STATEID operation
7034 *
7035 * @server: server / transport on which to perform the operation
7036 * @stateid: state ID to test
7037 *
7038 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7039 * Otherwise a negative NFS4ERR value is returned if the operation
7040 * failed or the state ID is not currently valid.
7041 */
1cab0652 7042static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
7d974794
BS
7043{
7044 struct nfs4_exception exception = { };
7045 int err;
7046 do {
377e507d
CL
7047 err = _nfs41_test_stateid(server, stateid);
7048 if (err != -NFS4ERR_DELAY)
7049 break;
7050 nfs4_handle_exception(server, err, &exception);
7d974794
BS
7051 } while (exception.retry);
7052 return err;
7053}
9aeda35f 7054
1cab0652 7055static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
9aeda35f 7056{
9aeda35f 7057 struct nfs41_free_stateid_args args = {
1cab0652 7058 .stateid = stateid,
9aeda35f
BS
7059 };
7060 struct nfs41_free_stateid_res res;
7061 struct rpc_message msg = {
7062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
7063 .rpc_argp = &args,
7064 .rpc_resp = &res,
7065 };
38527b15 7066 int status;
9aeda35f 7067
38527b15 7068 dprintk("NFS call free_stateid %p\n", stateid);
9d12b216 7069 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
38527b15 7070 status = nfs4_call_sync_sequence(server->client, server, &msg,
377e507d 7071 &args.seq_args, &res.seq_res, 1);
38527b15
CL
7072 dprintk("NFS reply free_stateid: %d\n", status);
7073 return status;
9aeda35f
BS
7074}
7075
38527b15
CL
7076/**
7077 * nfs41_free_stateid - perform a FREE_STATEID operation
7078 *
7079 * @server: server / transport on which to perform the operation
7080 * @stateid: state ID to release
7081 *
7082 * Returns NFS_OK if the server freed "stateid". Otherwise a
7083 * negative NFS4ERR value is returned.
7084 */
1cab0652 7085static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
9aeda35f
BS
7086{
7087 struct nfs4_exception exception = { };
7088 int err;
7089 do {
377e507d
CL
7090 err = _nfs4_free_stateid(server, stateid);
7091 if (err != -NFS4ERR_DELAY)
7092 break;
7093 nfs4_handle_exception(server, err, &exception);
9aeda35f
BS
7094 } while (exception.retry);
7095 return err;
7096}
36281caa
TM
7097
7098static bool nfs41_match_stateid(const nfs4_stateid *s1,
7099 const nfs4_stateid *s2)
7100{
2d2f24ad 7101 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
36281caa
TM
7102 return false;
7103
2d2f24ad 7104 if (s1->seqid == s2->seqid)
36281caa 7105 return true;
2d2f24ad 7106 if (s1->seqid == 0 || s2->seqid == 0)
36281caa
TM
7107 return true;
7108
7109 return false;
7110}
7111
557134a3
AA
7112#endif /* CONFIG_NFS_V4_1 */
7113
36281caa
TM
7114static bool nfs4_match_stateid(const nfs4_stateid *s1,
7115 const nfs4_stateid *s2)
7116{
f597c537 7117 return nfs4_stateid_match(s1, s2);
36281caa
TM
7118}
7119
7120
17280175 7121static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
7eff03ae 7122 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
b79a4a1b 7123 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
1da177e4
LT
7124 .recover_open = nfs4_open_reclaim,
7125 .recover_lock = nfs4_lock_reclaim,
591d71cb 7126 .establish_clid = nfs4_init_clientid,
90a16617 7127 .get_clid_cred = nfs4_get_setclientid_cred,
05f4c350 7128 .detect_trunking = nfs40_discover_server_trunking,
1da177e4
LT
7129};
7130
591d71cb 7131#if defined(CONFIG_NFS_V4_1)
17280175 7132static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
591d71cb
AA
7133 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7134 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7135 .recover_open = nfs4_open_reclaim,
7136 .recover_lock = nfs4_lock_reclaim,
4d643d1d 7137 .establish_clid = nfs41_init_clientid,
b4b82607 7138 .get_clid_cred = nfs4_get_exchange_id_cred,
fce5c838 7139 .reclaim_complete = nfs41_proc_reclaim_complete,
05f4c350 7140 .detect_trunking = nfs41_discover_server_trunking,
591d71cb
AA
7141};
7142#endif /* CONFIG_NFS_V4_1 */
7143
17280175 7144static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
591d71cb
AA
7145 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7146 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7147 .recover_open = nfs4_open_expired,
7148 .recover_lock = nfs4_lock_expired,
7149 .establish_clid = nfs4_init_clientid,
90a16617 7150 .get_clid_cred = nfs4_get_setclientid_cred,
591d71cb
AA
7151};
7152
7153#if defined(CONFIG_NFS_V4_1)
17280175 7154static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
7eff03ae 7155 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
b79a4a1b 7156 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
f062eb6c
BS
7157 .recover_open = nfs41_open_expired,
7158 .recover_lock = nfs41_lock_expired,
4d643d1d 7159 .establish_clid = nfs41_init_clientid,
b4b82607 7160 .get_clid_cred = nfs4_get_exchange_id_cred,
1da177e4 7161};
591d71cb 7162#endif /* CONFIG_NFS_V4_1 */
1da177e4 7163
17280175 7164static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
29fba38b 7165 .sched_state_renewal = nfs4_proc_async_renew,
a7b72103 7166 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8e69514f 7167 .renew_lease = nfs4_proc_renew,
29fba38b
BH
7168};
7169
7170#if defined(CONFIG_NFS_V4_1)
17280175 7171static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
29fba38b 7172 .sched_state_renewal = nfs41_proc_async_sequence,
a7b72103 7173 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8e69514f 7174 .renew_lease = nfs4_proc_sequence,
29fba38b
BH
7175};
7176#endif
7177
97dc1359
TM
7178static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7179 .minor_version = 0,
7180 .call_sync = _nfs4_call_sync,
36281caa 7181 .match_stateid = nfs4_match_stateid,
fca78d6d 7182 .find_root_sec = nfs4_find_root_sec,
c48f4f35
TM
7183 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7184 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7185 .state_renewal_ops = &nfs40_state_renewal_ops,
97dc1359
TM
7186};
7187
7188#if defined(CONFIG_NFS_V4_1)
7189static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7190 .minor_version = 1,
7191 .call_sync = _nfs4_call_sync_session,
36281caa 7192 .match_stateid = nfs41_match_stateid,
fca78d6d 7193 .find_root_sec = nfs41_find_root_sec,
c48f4f35
TM
7194 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7195 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7196 .state_renewal_ops = &nfs41_state_renewal_ops,
97dc1359
TM
7197};
7198#endif
7199
97dc1359
TM
7200const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7201 [0] = &nfs_v4_0_minor_ops,
7202#if defined(CONFIG_NFS_V4_1)
7203 [1] = &nfs_v4_1_minor_ops,
7204#endif
7205};
7206
73a79706
BS
7207const struct inode_operations nfs4_dir_inode_operations = {
7208 .create = nfs_create,
7209 .lookup = nfs_lookup,
7210 .atomic_open = nfs_atomic_open,
7211 .link = nfs_link,
7212 .unlink = nfs_unlink,
7213 .symlink = nfs_symlink,
7214 .mkdir = nfs_mkdir,
7215 .rmdir = nfs_rmdir,
7216 .mknod = nfs_mknod,
7217 .rename = nfs_rename,
7218 .permission = nfs_permission,
7219 .getattr = nfs_getattr,
7220 .setattr = nfs_setattr,
7221 .getxattr = generic_getxattr,
7222 .setxattr = generic_setxattr,
7223 .listxattr = generic_listxattr,
7224 .removexattr = generic_removexattr,
7225};
7226
92e1d5be 7227static const struct inode_operations nfs4_file_inode_operations = {
6b3b5496
BF
7228 .permission = nfs_permission,
7229 .getattr = nfs_getattr,
7230 .setattr = nfs_setattr,
64c2ce8b
AK
7231 .getxattr = generic_getxattr,
7232 .setxattr = generic_setxattr,
7233 .listxattr = generic_listxattr,
7234 .removexattr = generic_removexattr,
6b3b5496
BF
7235};
7236
509de811 7237const struct nfs_rpc_ops nfs_v4_clientops = {
1da177e4
LT
7238 .version = 4, /* protocol version */
7239 .dentry_ops = &nfs4_dentry_operations,
7240 .dir_inode_ops = &nfs4_dir_inode_operations,
6b3b5496 7241 .file_inode_ops = &nfs4_file_inode_operations,
1788ea6e 7242 .file_ops = &nfs4_file_operations,
1da177e4 7243 .getroot = nfs4_proc_get_root,
281cad46 7244 .submount = nfs4_submount,
ff9099f2 7245 .try_mount = nfs4_try_mount,
1da177e4
LT
7246 .getattr = nfs4_proc_getattr,
7247 .setattr = nfs4_proc_setattr,
7248 .lookup = nfs4_proc_lookup,
7249 .access = nfs4_proc_access,
7250 .readlink = nfs4_proc_readlink,
1da177e4
LT
7251 .create = nfs4_proc_create,
7252 .remove = nfs4_proc_remove,
7253 .unlink_setup = nfs4_proc_unlink_setup,
34e137cc 7254 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
1da177e4
LT
7255 .unlink_done = nfs4_proc_unlink_done,
7256 .rename = nfs4_proc_rename,
d3d4152a 7257 .rename_setup = nfs4_proc_rename_setup,
c6bfa1a1 7258 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
d3d4152a 7259 .rename_done = nfs4_proc_rename_done,
1da177e4
LT
7260 .link = nfs4_proc_link,
7261 .symlink = nfs4_proc_symlink,
7262 .mkdir = nfs4_proc_mkdir,
7263 .rmdir = nfs4_proc_remove,
7264 .readdir = nfs4_proc_readdir,
7265 .mknod = nfs4_proc_mknod,
7266 .statfs = nfs4_proc_statfs,
7267 .fsinfo = nfs4_proc_fsinfo,
7268 .pathconf = nfs4_proc_pathconf,
e9326dca 7269 .set_capabilities = nfs4_server_capabilities,
1da177e4
LT
7270 .decode_dirent = nfs4_decode_dirent,
7271 .read_setup = nfs4_proc_read_setup,
1abb5088 7272 .read_pageio_init = pnfs_pageio_init_read,
ea7c3303 7273 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
ec06c096 7274 .read_done = nfs4_read_done,
1da177e4 7275 .write_setup = nfs4_proc_write_setup,
57208fa7 7276 .write_pageio_init = pnfs_pageio_init_write,
c6cb80d0 7277 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
788e7a89 7278 .write_done = nfs4_write_done,
1da177e4 7279 .commit_setup = nfs4_proc_commit_setup,
0b7c0153 7280 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
788e7a89 7281 .commit_done = nfs4_commit_done,
1da177e4 7282 .lock = nfs4_proc_lock,
e50a1c2e 7283 .clear_acl_cache = nfs4_zap_acl_attr,
7fe5c398 7284 .close_context = nfs4_close_context,
2b484297 7285 .open_context = nfs4_atomic_open,
011e2a7f 7286 .have_delegation = nfs4_have_delegation,
57ec14c5 7287 .return_delegation = nfs4_inode_return_delegation,
6663ee7f 7288 .alloc_client = nfs4_alloc_client,
45a52a02 7289 .init_client = nfs4_init_client,
cdb7eced 7290 .free_client = nfs4_free_client,
1179acc6
BS
7291 .create_server = nfs4_create_server,
7292 .clone_server = nfs_clone_server,
1da177e4
LT
7293};
7294
64c2ce8b
AK
7295static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7296 .prefix = XATTR_NAME_NFSV4_ACL,
7297 .list = nfs4_xattr_list_nfs4_acl,
7298 .get = nfs4_xattr_get_nfs4_acl,
7299 .set = nfs4_xattr_set_nfs4_acl,
7300};
7301
7302const struct xattr_handler *nfs4_xattr_handlers[] = {
7303 &nfs4_xattr_nfs4_acl_handler,
7304 NULL
7305};
7306
1da177e4
LT
7307/*
7308 * Local variables:
7309 * c-basic-offset: 8
7310 * End:
7311 */