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