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