Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / fs / nfs / callback_proc.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * linux/fs/nfs/callback_proc.c
4 *
5 * Copyright (C) 2004 Trond Myklebust
6 *
7 * NFSv4 callback procedures
8 */
aa6159ab
AS
9
10#include <linux/errno.h>
11#include <linux/math.h>
1da177e4
LT
12#include <linux/nfs4.h>
13#include <linux/nfs_fs.h>
5a0e3ad6 14#include <linux/slab.h>
2446ab60 15#include <linux/rcupdate.h>
aa6159ab
AS
16#include <linux/types.h>
17
4ce79717 18#include "nfs4_fs.h"
1da177e4
LT
19#include "callback.h"
20#include "delegation.h"
24c8dbbb 21#include "internal.h"
43f1b3da 22#include "pnfs.h"
73e39aaa 23#include "nfs4session.h"
ca8acf8d 24#include "nfs4trace.h"
1da177e4
LT
25
26#define NFSDBG_FACILITY NFSDBG_CALLBACK
c36fca52 27
f4dac4ad 28__be32 nfs4_callback_getattr(void *argp, void *resp,
c36fca52 29 struct cb_process_state *cps)
1da177e4 30{
f4dac4ad
CH
31 struct cb_getattrargs *args = argp;
32 struct cb_getattrres *res = resp;
1da177e4 33 struct nfs_delegation *delegation;
1da177e4 34 struct inode *inode;
1d98fe67 35
c36fca52
AA
36 res->status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
37 if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
38 goto out;
39
43df7110 40 memset(res->bitmap, 0, sizeof(res->bitmap));
1da177e4 41 res->status = htonl(NFS4ERR_BADHANDLE);
1d98fe67 42
2446ab60 43 dprintk_rcu("NFS: GETATTR callback request from %s\n",
c36fca52 44 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1d98fe67 45
c36fca52 46 inode = nfs_delegation_find_inode(cps->clp, &args->fh);
6c342655
TM
47 if (IS_ERR(inode)) {
48 if (inode == ERR_PTR(-EAGAIN))
49 res->status = htonl(NFS4ERR_DELAY);
249b2eef 50 trace_nfs4_cb_getattr(cps->clp, &args->fh, NULL,
7cd14861 51 -ntohl(res->status));
c36fca52 52 goto out;
7cd14861 53 }
761fe93c 54 rcu_read_lock();
5decae16 55 delegation = nfs4_get_valid_delegation(inode);
1da177e4
LT
56 if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
57 goto out_iput;
58 res->size = i_size_read(inode);
beb2a5ec 59 res->change_attr = delegation->change_attr;
a6b6d5b8 60 if (nfs_have_writebacks(inode))
beb2a5ec 61 res->change_attr++;
43df7110 62 res->atime = inode_get_atime(inode);
55e04e9c 63 res->ctime = inode_get_ctime(inode);
41d581a9 64 res->mtime = inode_get_mtime(inode);
43df7110
TM
65 res->bitmap[0] = (FATTR4_WORD0_CHANGE | FATTR4_WORD0_SIZE) &
66 args->bitmap[0];
67 res->bitmap[1] = (FATTR4_WORD1_TIME_ACCESS |
68 FATTR4_WORD1_TIME_METADATA |
69 FATTR4_WORD1_TIME_MODIFY) & args->bitmap[1];
70 res->bitmap[2] = (FATTR4_WORD2_TIME_DELEG_ACCESS |
71 FATTR4_WORD2_TIME_DELEG_MODIFY) & args->bitmap[2];
1da177e4
LT
72 res->status = 0;
73out_iput:
761fe93c 74 rcu_read_unlock();
7cd14861 75 trace_nfs4_cb_getattr(cps->clp, &args->fh, inode, -ntohl(res->status));
e39d8a18 76 nfs_iput_and_deactive(inode);
1da177e4 77out:
3110ff80 78 dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
1da177e4
LT
79 return res->status;
80}
81
f4dac4ad 82__be32 nfs4_callback_recall(void *argp, void *resp,
c36fca52 83 struct cb_process_state *cps)
1da177e4 84{
f4dac4ad 85 struct cb_recallargs *args = argp;
1da177e4 86 struct inode *inode;
e6f684f6 87 __be32 res;
1da177e4 88
c36fca52
AA
89 res = htonl(NFS4ERR_OP_NOT_IN_SESSION);
90 if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
1da177e4 91 goto out;
1d98fe67 92
2446ab60 93 dprintk_rcu("NFS: RECALL callback request from %s\n",
c36fca52
AA
94 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
95
96 res = htonl(NFS4ERR_BADHANDLE);
97 inode = nfs_delegation_find_inode(cps->clp, &args->fh);
6c342655
TM
98 if (IS_ERR(inode)) {
99 if (inode == ERR_PTR(-EAGAIN))
100 res = htonl(NFS4ERR_DELAY);
e07db907
TM
101 trace_nfs4_cb_recall(cps->clp, &args->fh, NULL,
102 &args->stateid, -ntohl(res));
c36fca52 103 goto out;
e07db907 104 }
c36fca52
AA
105 /* Set up a helper thread to actually return the delegation */
106 switch (nfs_async_inode_return_delegation(inode, &args->stateid)) {
107 case 0:
108 res = 0;
109 break;
110 case -ENOENT:
8e663f0e 111 res = htonl(NFS4ERR_BAD_STATEID);
c36fca52
AA
112 break;
113 default:
114 res = htonl(NFS4ERR_RESOURCE);
115 }
e07db907
TM
116 trace_nfs4_cb_recall(cps->clp, &args->fh, inode,
117 &args->stateid, -ntohl(res));
e39d8a18 118 nfs_iput_and_deactive(inode);
1da177e4 119out:
3110ff80 120 dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
1da177e4
LT
121 return res;
122}
d49433e1
BH
123
124#if defined(CONFIG_NFS_V4_1)
125
d36b7cf7 126/*
7b410d9c 127 * Lookup a layout inode by stateid
d36b7cf7 128 *
7b410d9c
TM
129 * Note: returns a refcount on the inode and superblock
130 */
131static struct inode *nfs_layout_find_inode_by_stateid(struct nfs_client *clp,
132 const nfs4_stateid *stateid)
58ac3e59 133 __must_hold(RCU)
7b410d9c
TM
134{
135 struct nfs_server *server;
136 struct inode *inode;
137 struct pnfs_layout_hdr *lo;
138
58ac3e59 139 rcu_read_lock();
7b410d9c 140 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
58ac3e59 141 list_for_each_entry_rcu(lo, &server->layouts, plh_layouts) {
d911c57a
TM
142 if (!pnfs_layout_is_valid(lo))
143 continue;
8926cc83 144 if (!nfs4_stateid_match_other(stateid, &lo->plh_stateid))
7b410d9c 145 continue;
8926cc83
TM
146 if (nfs_sb_active(server->super))
147 inode = igrab(lo->plh_inode);
148 else
149 inode = ERR_PTR(-EAGAIN);
58ac3e59
TM
150 rcu_read_unlock();
151 if (inode)
152 return inode;
153 nfs_sb_deactive(server->super);
154 return ERR_PTR(-EAGAIN);
7b410d9c
TM
155 }
156 }
58ac3e59 157 rcu_read_unlock();
ce5624f7 158 return ERR_PTR(-ENOENT);
7b410d9c
TM
159}
160
161/*
162 * Lookup a layout inode by filehandle.
163 *
164 * Note: returns a refcount on the inode and superblock
d36b7cf7 165 *
d36b7cf7 166 */
7b410d9c
TM
167static struct inode *nfs_layout_find_inode_by_fh(struct nfs_client *clp,
168 const struct nfs_fh *fh)
43f1b3da 169{
6382a441 170 struct nfs_server *server;
793b7fe5 171 struct nfs_inode *nfsi;
7b410d9c 172 struct inode *inode;
d36b7cf7 173 struct pnfs_layout_hdr *lo;
43f1b3da 174
58ac3e59 175 rcu_read_lock();
6382a441 176 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
58ac3e59 177 list_for_each_entry_rcu(lo, &server->layouts, plh_layouts) {
793b7fe5
TM
178 nfsi = NFS_I(lo->plh_inode);
179 if (nfs_compare_fh(fh, &nfsi->fh))
27999f25 180 continue;
793b7fe5 181 if (nfsi->layout != lo)
6382a441 182 continue;
8926cc83
TM
183 if (nfs_sb_active(server->super))
184 inode = igrab(lo->plh_inode);
185 else
186 inode = ERR_PTR(-EAGAIN);
58ac3e59
TM
187 rcu_read_unlock();
188 if (inode)
189 return inode;
190 nfs_sb_deactive(server->super);
191 return ERR_PTR(-EAGAIN);
6382a441 192 }
43f1b3da 193 }
58ac3e59 194 rcu_read_unlock();
ce5624f7 195 return ERR_PTR(-ENOENT);
d36b7cf7
BH
196}
197
7b410d9c
TM
198static struct inode *nfs_layout_find_inode(struct nfs_client *clp,
199 const struct nfs_fh *fh,
200 const nfs4_stateid *stateid)
d36b7cf7 201{
7b410d9c 202 struct inode *inode;
d36b7cf7 203
7b410d9c 204 inode = nfs_layout_find_inode_by_stateid(clp, stateid);
ce5624f7 205 if (inode == ERR_PTR(-ENOENT))
7b410d9c 206 inode = nfs_layout_find_inode_by_fh(clp, fh);
7b410d9c 207 return inode;
d36b7cf7
BH
208}
209
41c9127d
TM
210/*
211 * Enforce RFC5661 section 12.5.5.2.1. (Layout Recall and Return Sequencing)
212 */
793b7fe5 213static u32 pnfs_check_callback_stateid(struct pnfs_layout_hdr *lo,
dce72920
TM
214 const nfs4_stateid *new,
215 struct cb_process_state *cps)
41c9127d
TM
216{
217 u32 oldseq, newseq;
218
bd3d16a8 219 /* Is the stateid not initialised? */
793b7fe5 220 if (!pnfs_layout_is_valid(lo))
bd3d16a8 221 return NFS4ERR_NOMATCHING_LAYOUT;
793b7fe5
TM
222
223 /* Mismatched stateid? */
224 if (!nfs4_stateid_match_other(&lo->plh_stateid, new))
225 return NFS4ERR_BAD_STATEID;
226
41c9127d 227 newseq = be32_to_cpu(new->seqid);
793b7fe5 228 /* Are we already in a layout recall situation? */
dce72920
TM
229 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
230 return NFS4ERR_DELAY;
41c9127d 231
dce72920
TM
232 /*
233 * Check that the stateid matches what we think it should be.
234 * Note that if the server sent us a list of referring calls,
235 * and we know that those have completed, then we trust the
236 * stateid argument is correct.
237 */
793b7fe5 238 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
dce72920 239 if (newseq > oldseq + 1 && !cps->referring_calls)
793b7fe5 240 return NFS4ERR_DELAY;
dce72920 241
793b7fe5
TM
242 /* Crazy server! */
243 if (newseq <= oldseq)
244 return NFS4ERR_OLD_STATEID;
dce72920 245
793b7fe5 246 return NFS_OK;
41c9127d
TM
247}
248
d36b7cf7 249static u32 initiate_file_draining(struct nfs_client *clp,
dce72920
TM
250 struct cb_layoutrecallargs *args,
251 struct cb_process_state *cps)
d36b7cf7
BH
252{
253 struct inode *ino;
254 struct pnfs_layout_hdr *lo;
255 u32 rv = NFS4ERR_NOMATCHING_LAYOUT;
256 LIST_HEAD(free_me_list);
257
7b410d9c 258 ino = nfs_layout_find_inode(clp, &args->cbl_fh, &args->cbl_stateid);
ce5624f7
TM
259 if (IS_ERR(ino)) {
260 if (ino == ERR_PTR(-EAGAIN))
261 rv = NFS4ERR_DELAY;
262 goto out_noput;
263 }
43f1b3da 264
793b7fe5
TM
265 pnfs_layoutcommit_inode(ino, false);
266
7c5d1875
CH
267
268 spin_lock(&ino->i_lock);
7b410d9c
TM
269 lo = NFS_I(ino)->layout;
270 if (!lo) {
271 spin_unlock(&ino->i_lock);
272 goto out;
273 }
274 pnfs_get_layout_hdr(lo);
dce72920 275 rv = pnfs_check_callback_stateid(lo, &args->cbl_stateid, cps);
793b7fe5 276 if (rv != NFS_OK)
41c9127d 277 goto unlock;
7c5d1875 278
fc7ff367
TM
279 /*
280 * Enforce RFC5661 Section 12.5.5.2.1.5 (Bulk Recall and Return)
281 */
282 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
283 rv = NFS4ERR_DELAY;
284 goto unlock;
285 }
286
59b56394 287 pnfs_set_layout_stateid(lo, &args->cbl_stateid, NULL, true);
e0b7d420 288 switch (pnfs_mark_matching_lsegs_return(lo, &free_me_list,
6d597e17
JL
289 &args->cbl_range,
290 be32_to_cpu(args->cbl_stateid.seqid))) {
e0b7d420
TM
291 case 0:
292 case -EBUSY:
293 /* There are layout segments that need to be returned */
e0d92430 294 rv = NFS4_OK;
e0b7d420
TM
295 break;
296 case -ENOENT:
880265c7 297 set_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags);
e0b7d420
TM
298 /* Embrace your forgetfulness! */
299 rv = NFS4ERR_NOMATCHING_LAYOUT;
793b7fe5 300
e0b7d420
TM
301 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
302 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo,
303 &args->cbl_range);
304 }
c88953d8
CH
305 }
306unlock:
43f1b3da
FI
307 spin_unlock(&ino->i_lock);
308 pnfs_free_lseg_list(&free_me_list);
b20135d0
TM
309 /* Free all lsegs that are attached to commit buckets */
310 nfs_commit_inode(ino, 0);
70c3bd2b 311 pnfs_put_layout_hdr(lo);
7b410d9c 312out:
ce5624f7
TM
313 nfs_iput_and_deactive(ino);
314out_noput:
e07db907
TM
315 trace_nfs4_cb_layoutrecall_file(clp, &args->cbl_fh, ino,
316 &args->cbl_stateid, -rv);
43f1b3da
FI
317 return rv;
318}
319
320static u32 initiate_bulk_draining(struct nfs_client *clp,
321 struct cb_layoutrecallargs *args)
322{
fd9a8d71 323 int stat;
6382a441 324
fd9a8d71 325 if (args->cbl_recall_type == RETURN_FSID)
8adc8302
TM
326 stat = pnfs_layout_destroy_byfsid(clp, &args->cbl_fsid,
327 PNFS_LAYOUT_BULK_RETURN);
fd9a8d71 328 else
8adc8302 329 stat = pnfs_layout_destroy_byclid(clp, PNFS_LAYOUT_BULK_RETURN);
fd9a8d71
TM
330 if (stat != 0)
331 return NFS4ERR_DELAY;
332 return NFS4ERR_NOMATCHING_LAYOUT;
43f1b3da
FI
333}
334
335static u32 do_callback_layoutrecall(struct nfs_client *clp,
dce72920
TM
336 struct cb_layoutrecallargs *args,
337 struct cb_process_state *cps)
43f1b3da 338{
43f1b3da 339 if (args->cbl_recall_type == RETURN_FILE)
dce72920 340 return initiate_file_draining(clp, args, cps);
1a916ce0 341 return initiate_bulk_draining(clp, args);
43f1b3da
FI
342}
343
f4dac4ad
CH
344__be32 nfs4_callback_layoutrecall(void *argp, void *resp,
345 struct cb_process_state *cps)
f2a62561 346{
f4dac4ad 347 struct cb_layoutrecallargs *args = argp;
5694a4f8 348 u32 res = NFS4ERR_OP_NOT_IN_SESSION;
43f1b3da
FI
349
350 if (cps->clp)
dce72920 351 res = do_callback_layoutrecall(cps->clp, args, cps);
43f1b3da 352 return cpu_to_be32(res);
f2a62561
FI
353}
354
dce72920
TM
355static void pnfs_recall_all_layouts(struct nfs_client *clp,
356 struct cb_process_state *cps)
36840370
AB
357{
358 struct cb_layoutrecallargs args;
359
360 /* Pretend we got a CB_LAYOUTRECALL(ALL) */
361 memset(&args, 0, sizeof(args));
362 args.cbl_recall_type = RETURN_ALL;
363 /* FIXME we ignore errors, what should we do? */
dce72920 364 do_callback_layoutrecall(clp, &args, cps);
36840370
AB
365}
366
f4dac4ad
CH
367__be32 nfs4_callback_devicenotify(void *argp, void *resp,
368 struct cb_process_state *cps)
1be5683b 369{
f4dac4ad 370 struct cb_devicenotifyargs *args = argp;
7c9d845f 371 const struct pnfs_layoutdriver_type *ld = NULL;
b05bf5c6 372 uint32_t i;
1be5683b 373 __be32 res = 0;
1be5683b 374
7c9d845f 375 if (!cps->clp) {
1be5683b
ME
376 res = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
377 goto out;
378 }
379
380 for (i = 0; i < args->ndevs; i++) {
381 struct cb_devicenotifyitem *dev = &args->devs[i];
382
7c9d845f
TM
383 if (!ld || ld->id != dev->cbd_layout_type) {
384 pnfs_put_layoutdriver(ld);
385 ld = pnfs_find_layoutdriver(dev->cbd_layout_type);
386 if (!ld)
387 continue;
1be5683b 388 }
7c9d845f 389 nfs4_delete_deviceid(ld, cps->clp, &dev->cbd_dev_id);
1be5683b 390 }
7c9d845f 391 pnfs_put_layoutdriver(ld);
1be5683b
ME
392out:
393 kfree(args->devs);
1be5683b
ME
394 return res;
395}
396
68f3f901
RL
397/*
398 * Validate the sequenceID sent by the server.
399 * Return success if the sequenceID is one more than what we last saw on
400 * this slot, accounting for wraparound. Increments the slot's sequence.
401 *
4911096f
AA
402 * We don't yet implement a duplicate request cache, instead we set the
403 * back channel ca_maxresponsesize_cached to zero. This is OK for now
68f3f901
RL
404 * since we only currently implement idempotent callbacks anyway.
405 *
406 * We have a single slot backchannel at this time, so we don't bother
407 * checking the used_slots bit array on the table. The lower layer guarantees
408 * a single outstanding callback request at a time.
409 */
9733f0d9 410static __be32
5f83d86c
TM
411validate_seqid(const struct nfs4_slot_table *tbl, const struct nfs4_slot *slot,
412 const struct cb_sequenceargs * args)
68f3f901 413{
c5833f0d
CL
414 __be32 ret;
415
416 ret = cpu_to_be32(NFS4ERR_BADSLOT);
f4f58ed1 417 if (args->csa_slotid > tbl->server_highest_slotid)
c5833f0d 418 goto out_err;
68f3f901 419
68f3f901 420 /* Replay */
b2f28bd7 421 if (args->csa_sequenceid == slot->seq_nr) {
c5833f0d 422 ret = cpu_to_be32(NFS4ERR_DELAY);
810d82e6 423 if (nfs4_test_locked_slot(tbl, slot->slot_nr))
c5833f0d
CL
424 goto out_err;
425
4911096f 426 /* Signal process_op to set this error on next op */
c5833f0d 427 ret = cpu_to_be32(NFS4ERR_RETRY_UNCACHED_REP);
4911096f 428 if (args->csa_cachethis == 0)
c5833f0d 429 goto out_err;
4911096f 430
80f96427 431 /* Liar! We never allowed you to set csa_cachethis != 0 */
c5833f0d
CL
432 ret = cpu_to_be32(NFS4ERR_SEQ_FALSE_RETRY);
433 goto out_err;
68f3f901
RL
434 }
435
fc40724f 436 /* Note: wraparound relies on seq_nr being of type u32 */
68f3f901 437 /* Misordered request */
c5833f0d
CL
438 ret = cpu_to_be32(NFS4ERR_SEQ_MISORDERED);
439 if (args->csa_sequenceid != slot->seq_nr + 1)
440 goto out_err;
441
442 return cpu_to_be32(NFS4_OK);
443
444out_err:
445 trace_nfs4_cb_seqid_err(args, ret);
446 return ret;
68f3f901
RL
447}
448
a7989c3e
MS
449/*
450 * For each referring call triple, check the session's slot table for
451 * a match. If the slot is in use and the sequence numbers match, the
452 * client is still waiting for a response to the original request.
453 */
8618289c 454static int referring_call_exists(struct nfs_client *clp,
a7989c3e 455 uint32_t nrclists,
8618289c
TM
456 struct referring_call_list *rclists,
457 spinlock_t *lock)
458 __releases(lock)
459 __acquires(lock)
a7989c3e 460{
8618289c 461 int status = 0;
e3fd54e7 462 int found = 0;
a7989c3e
MS
463 int i, j;
464 struct nfs4_session *session;
465 struct nfs4_slot_table *tbl;
466 struct referring_call_list *rclist;
467 struct referring_call *ref;
468
469 /*
470 * XXX When client trunking is implemented, this becomes
471 * a session lookup from within the loop
472 */
473 session = clp->cl_session;
474 tbl = &session->fc_slot_table;
475
476 for (i = 0; i < nrclists; i++) {
477 rclist = &rclists[i];
478 if (memcmp(session->sess_id.data,
479 rclist->rcl_sessionid.data,
480 NFS4_MAX_SESSIONID_LEN) != 0)
481 continue;
482
483 for (j = 0; j < rclist->rcl_nrefcalls; j++) {
484 ref = &rclist->rcl_refcalls[j];
8618289c 485 spin_unlock(lock);
045d2a6d
TM
486 status = nfs4_slot_wait_on_seqid(tbl, ref->rc_slotid,
487 ref->rc_sequenceid, HZ >> 1) < 0;
8618289c 488 spin_lock(lock);
a7989c3e
MS
489 if (status)
490 goto out;
e3fd54e7 491 found++;
a7989c3e
MS
492 }
493 }
494
495out:
e3fd54e7 496 return status < 0 ? status : found;
a7989c3e
MS
497}
498
f4dac4ad 499__be32 nfs4_callback_sequence(void *argp, void *resp,
c36fca52 500 struct cb_process_state *cps)
d49433e1 501{
f4dac4ad
CH
502 struct cb_sequenceargs *args = argp;
503 struct cb_sequenceres *res = resp;
55a67399 504 struct nfs4_slot_table *tbl;
e937ee71 505 struct nfs4_slot *slot;
68f3f901 506 struct nfs_client *clp;
e3fd54e7 507 int ret;
9733f0d9 508 int i;
778be232 509 __be32 status = htonl(NFS4ERR_BADSESSION);
d49433e1 510
459de2ed
BS
511 clp = nfs4_find_client_sessionid(cps->net, args->csa_addr,
512 &args->csa_sessionid, cps->minorversion);
68f3f901
RL
513 if (clp == NULL)
514 goto out;
515
b1c0df5f
TM
516 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
517 goto out;
e937ee71 518
55a67399
TM
519 tbl = &clp->cl_session->bc_slot_table;
520
e5003b2f
TM
521 /* Set up res before grabbing the spinlock */
522 memcpy(&res->csr_sessionid, &args->csa_sessionid,
523 sizeof(res->csr_sessionid));
524 res->csr_sequenceid = args->csa_sequenceid;
525 res->csr_slotid = args->csa_slotid;
526
55a67399 527 spin_lock(&tbl->slot_tbl_lock);
42acd021 528 /* state manager is resetting the session */
774d5f14 529 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
55a67399 530 status = htonl(NFS4ERR_DELAY);
910ac68a
TM
531 /* Return NFS4ERR_BADSESSION if we're draining the session
532 * in order to reset it.
533 */
534 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
535 status = htonl(NFS4ERR_BADSESSION);
4e54ab8d 536 goto out_unlock;
42acd021
AA
537 }
538
810d82e6
TM
539 status = htonl(NFS4ERR_BADSLOT);
540 slot = nfs4_lookup_slot(tbl, args->csa_slotid);
541 if (IS_ERR(slot))
542 goto out_unlock;
e5003b2f
TM
543
544 res->csr_highestslotid = tbl->server_highest_slotid;
545 res->csr_target_highestslotid = tbl->target_highest_slotid;
546
5f83d86c
TM
547 status = validate_seqid(tbl, slot, args);
548 if (status)
549 goto out_unlock;
810d82e6
TM
550 if (!nfs4_try_to_lock_slot(tbl, slot)) {
551 status = htonl(NFS4ERR_DELAY);
552 goto out_unlock;
553 }
554 cps->slot = slot;
5f83d86c 555
80f96427 556 /* The ca_maxresponsesize_cached is 0 with no DRC */
c2985d00
OK
557 if (args->csa_cachethis != 0) {
558 status = htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
559 goto out_unlock;
560 }
80f96427 561
72ce2b3c
MS
562 /*
563 * Check for pending referring calls. If a match is found, a
564 * related callback was received before the response to the original
565 * call.
566 */
e3fd54e7
TM
567 ret = referring_call_exists(clp, args->csa_nrclists, args->csa_rclists,
568 &tbl->slot_tbl_lock);
569 if (ret < 0) {
72ce2b3c 570 status = htonl(NFS4ERR_DELAY);
4e54ab8d 571 goto out_unlock;
72ce2b3c 572 }
e3fd54e7 573 cps->referring_calls = ret;
72ce2b3c 574
e937ee71
KM
575 /*
576 * RFC5661 20.9.3
577 * If CB_SEQUENCE returns an error, then the state of the slot
578 * (sequence ID, cached reply) MUST NOT change.
579 */
5f83d86c 580 slot->seq_nr = args->csa_sequenceid;
4e54ab8d
TM
581out_unlock:
582 spin_unlock(&tbl->slot_tbl_lock);
e937ee71 583
68f3f901 584out:
2c4cdf8f 585 cps->clp = clp; /* put in nfs4_callback_compound */
72ce2b3c
MS
586 for (i = 0; i < args->csa_nrclists; i++)
587 kfree(args->csa_rclists[i].rcl_refcalls);
588 kfree(args->csa_rclists);
589
c36fca52
AA
590 if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
591 cps->drc_status = status;
592 status = 0;
593 } else
4911096f 594 res->csr_status = status;
c36fca52 595
2f92ae34 596 trace_nfs4_cb_sequence(args, res, status);
4911096f 597 return status;
d49433e1
BH
598}
599
36840370 600static bool
6d243a23 601validate_bitmap_values(unsigned int mask)
36840370
AB
602{
603 return (mask & ~RCA4_TYPE_MASK_ALL) == 0;
604}
605
f4dac4ad 606__be32 nfs4_callback_recallany(void *argp, void *resp,
c36fca52 607 struct cb_process_state *cps)
31f09607 608{
f4dac4ad 609 struct cb_recallanyargs *args = argp;
9733f0d9 610 __be32 status;
31f09607 611 fmode_t flags = 0;
b5fdf841 612 bool schedule_manager = false;
31f09607 613
36840370 614 status = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
c36fca52 615 if (!cps->clp) /* set in cb_sequence */
31f09607
AB
616 goto out;
617
2446ab60 618 dprintk_rcu("NFS: RECALL_ANY callback request from %s\n",
c36fca52 619 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
31f09607 620
36840370
AB
621 status = cpu_to_be32(NFS4ERR_INVAL);
622 if (!validate_bitmap_values(args->craa_type_mask))
623 goto out;
624
625 status = cpu_to_be32(NFS4_OK);
6d243a23 626 if (args->craa_type_mask & BIT(RCA4_TYPE_MASK_RDATA_DLG))
31f09607 627 flags = FMODE_READ;
6d243a23 628 if (args->craa_type_mask & BIT(RCA4_TYPE_MASK_WDATA_DLG))
31f09607 629 flags |= FMODE_WRITE;
31f09607 630 if (flags)
826e0013 631 nfs_expire_unused_delegation_types(cps->clp, flags);
6d243a23
TM
632
633 if (args->craa_type_mask & BIT(RCA4_TYPE_MASK_FILE_LAYOUT))
dce72920 634 pnfs_recall_all_layouts(cps->clp, cps);
b5fdf841
TM
635
636 if (args->craa_type_mask & BIT(PNFS_FF_RCA4_TYPE_MASK_READ)) {
637 set_bit(NFS4CLNT_RECALL_ANY_LAYOUT_READ, &cps->clp->cl_state);
638 schedule_manager = true;
639 }
640 if (args->craa_type_mask & BIT(PNFS_FF_RCA4_TYPE_MASK_RW)) {
641 set_bit(NFS4CLNT_RECALL_ANY_LAYOUT_RW, &cps->clp->cl_state);
642 schedule_manager = true;
643 }
644 if (schedule_manager)
645 nfs4_schedule_state_manager(cps->clp);
646
31f09607
AB
647out:
648 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
649 return status;
650}
b9efa1b2
AA
651
652/* Reduce the fore channel's max_slots to the target value */
f4dac4ad 653__be32 nfs4_callback_recallslot(void *argp, void *resp,
c36fca52 654 struct cb_process_state *cps)
b9efa1b2 655{
f4dac4ad 656 struct cb_recallslotargs *args = argp;
b9efa1b2 657 struct nfs4_slot_table *fc_tbl;
9733f0d9 658 __be32 status;
b9efa1b2
AA
659
660 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
c36fca52 661 if (!cps->clp) /* set in cb_sequence */
b9efa1b2
AA
662 goto out;
663
e8d92382 664 dprintk_rcu("NFS: CB_RECALL_SLOT request from %s target highest slotid %u\n",
c36fca52 665 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR),
d5fb4ce3 666 args->crsa_target_highest_slotid);
b9efa1b2 667
c36fca52 668 fc_tbl = &cps->clp->cl_session->fc_slot_table;
b9efa1b2 669
bae0ac0e 670 status = htonl(NFS4_OK);
b9efa1b2 671
d5fb4ce3 672 nfs41_set_target_slotid(fc_tbl, args->crsa_target_highest_slotid);
3f10a6af 673 nfs41_notify_server(cps->clp);
b9efa1b2
AA
674out:
675 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
676 return status;
677}
db783688 678
f4dac4ad 679__be32 nfs4_callback_notify_lock(void *argp, void *resp,
db783688
JL
680 struct cb_process_state *cps)
681{
f4dac4ad
CH
682 struct cb_notify_lock_args *args = argp;
683
db783688
JL
684 if (!cps->clp) /* set in cb_sequence */
685 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
686
687 dprintk_rcu("NFS: CB_NOTIFY_LOCK request from %s\n",
688 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
689
a1d617d8
JL
690 /* Don't wake anybody if the string looked bogus */
691 if (args->cbnl_valid)
692 __wake_up(&cps->clp->cl_lock_waitq, TASK_NORMAL, 0, args);
693
db783688
JL
694 return htonl(NFS4_OK);
695}
d49433e1 696#endif /* CONFIG_NFS_V4_1 */
5178a125 697#ifdef CONFIG_NFS_V4_2
62164f31
OK
698static void nfs4_copy_cb_args(struct nfs4_copy_state *cp_state,
699 struct cb_offloadargs *args)
700{
701 cp_state->count = args->wr_count;
702 cp_state->error = args->error;
703 if (!args->error) {
704 cp_state->verf.committed = args->wr_writeverf.committed;
705 memcpy(&cp_state->verf.verifier.data[0],
706 &args->wr_writeverf.verifier.data[0],
707 NFS4_VERIFIER_SIZE);
708 }
709}
710
711__be32 nfs4_callback_offload(void *data, void *dummy,
5178a125
OK
712 struct cb_process_state *cps)
713{
62164f31
OK
714 struct cb_offloadargs *args = data;
715 struct nfs_server *server;
99f2c555 716 struct nfs4_copy_state *copy, *tmp_copy;
bc0c9079 717 bool found = false;
62164f31 718
4fb547be 719 copy = kzalloc(sizeof(struct nfs4_copy_state), GFP_KERNEL);
99f2c555 720 if (!copy)
d2fc83c5 721 return cpu_to_be32(NFS4ERR_DELAY);
99f2c555 722
bc0c9079 723 spin_lock(&cps->clp->cl_lock);
62164f31
OK
724 rcu_read_lock();
725 list_for_each_entry_rcu(server, &cps->clp->cl_superblocks,
726 client_link) {
99f2c555 727 list_for_each_entry(tmp_copy, &server->ss_copies, copies) {
62164f31 728 if (memcmp(args->coa_stateid.other,
99f2c555 729 tmp_copy->stateid.other,
62164f31
OK
730 sizeof(args->coa_stateid.other)))
731 continue;
99f2c555
OK
732 nfs4_copy_cb_args(tmp_copy, args);
733 complete(&tmp_copy->completion);
bc0c9079 734 found = true;
62164f31
OK
735 goto out;
736 }
62164f31
OK
737 }
738out:
739 rcu_read_unlock();
bc0c9079 740 if (!found) {
bc0c9079
OK
741 memcpy(&copy->stateid, &args->coa_stateid, NFS4_STATEID_SIZE);
742 nfs4_copy_cb_args(copy, args);
743 list_add_tail(&copy->copies, &cps->clp->pending_cb_stateids);
99f2c555
OK
744 } else
745 kfree(copy);
bc0c9079 746 spin_unlock(&cps->clp->cl_lock);
62164f31 747
8db744ce
OK
748 trace_nfs4_cb_offload(&args->coa_fh, &args->coa_stateid,
749 args->wr_count, args->error,
750 args->wr_writeverf.committed);
5178a125
OK
751 return 0;
752}
753#endif /* CONFIG_NFS_V4_2 */