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