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
261 ino = nfs_layout_find_inode(clp, &args->cbl_fh, &args->cbl_stateid);
262 if (IS_ERR(ino)) {
263 if (ino == ERR_PTR(-EAGAIN))
264 rv = NFS4ERR_DELAY;
265 goto out_noput;
266 }
267
268 pnfs_layoutcommit_inode(ino, false);
269
270
271 spin_lock(&ino->i_lock);
272 lo = NFS_I(ino)->layout;
273 if (!lo) {
274 spin_unlock(&ino->i_lock);
275 goto out;
276 }
277 pnfs_get_layout_hdr(lo);
278 rv = pnfs_check_callback_stateid(lo, &args->cbl_stateid, cps);
279 if (rv != NFS_OK)
280 goto unlock;
281
282 /*
283 * Enforce RFC5661 Section 12.5.5.2.1.5 (Bulk Recall and Return)
284 */
285 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
286 rv = NFS4ERR_DELAY;
287 goto unlock;
288 }
289
290 pnfs_set_layout_stateid(lo, &args->cbl_stateid, NULL, true);
291 switch (pnfs_mark_matching_lsegs_return(lo, &free_me_list,
292 &args->cbl_range,
293 be32_to_cpu(args->cbl_stateid.seqid))) {
294 case 0:
295 case -EBUSY:
296 /* There are layout segments that need to be returned */
297 rv = NFS4_OK;
298 break;
299 case -ENOENT:
300 set_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags);
301 /* Embrace your forgetfulness! */
302 rv = NFS4ERR_NOMATCHING_LAYOUT;
303
304 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
305 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo,
306 &args->cbl_range);
307 }
308 }
309 unlock:
310 spin_unlock(&ino->i_lock);
311 pnfs_free_lseg_list(&free_me_list);
312 /* Free all lsegs that are attached to commit buckets */
313 nfs_commit_inode(ino, 0);
314 pnfs_put_layout_hdr(lo);
315 out:
316 nfs_iput_and_deactive(ino);
317 out_noput:
318 trace_nfs4_cb_layoutrecall_file(clp, &args->cbl_fh, ino,
319 &args->cbl_stateid, -rv);
320 return rv;
321 }
322
initiate_bulk_draining(struct nfs_client * clp,struct cb_layoutrecallargs * args)323 static u32 initiate_bulk_draining(struct nfs_client *clp,
324 struct cb_layoutrecallargs *args)
325 {
326 int stat;
327
328 if (args->cbl_recall_type == RETURN_FSID)
329 stat = pnfs_layout_destroy_byfsid(clp, &args->cbl_fsid,
330 PNFS_LAYOUT_BULK_RETURN);
331 else
332 stat = pnfs_layout_destroy_byclid(clp, PNFS_LAYOUT_BULK_RETURN);
333 if (stat != 0)
334 return NFS4ERR_DELAY;
335 return NFS4ERR_NOMATCHING_LAYOUT;
336 }
337
do_callback_layoutrecall(struct nfs_client * clp,struct cb_layoutrecallargs * args,struct cb_process_state * cps)338 static u32 do_callback_layoutrecall(struct nfs_client *clp,
339 struct cb_layoutrecallargs *args,
340 struct cb_process_state *cps)
341 {
342 if (args->cbl_recall_type == RETURN_FILE)
343 return initiate_file_draining(clp, args, cps);
344 return initiate_bulk_draining(clp, args);
345 }
346
nfs4_callback_layoutrecall(void * argp,void * resp,struct cb_process_state * cps)347 __be32 nfs4_callback_layoutrecall(void *argp, void *resp,
348 struct cb_process_state *cps)
349 {
350 struct cb_layoutrecallargs *args = argp;
351 u32 res = NFS4ERR_OP_NOT_IN_SESSION;
352
353 if (cps->clp)
354 res = do_callback_layoutrecall(cps->clp, args, cps);
355 return cpu_to_be32(res);
356 }
357
pnfs_recall_all_layouts(struct nfs_client * clp,struct cb_process_state * cps)358 static void pnfs_recall_all_layouts(struct nfs_client *clp,
359 struct cb_process_state *cps)
360 {
361 struct cb_layoutrecallargs args;
362
363 /* Pretend we got a CB_LAYOUTRECALL(ALL) */
364 memset(&args, 0, sizeof(args));
365 args.cbl_recall_type = RETURN_ALL;
366 /* FIXME we ignore errors, what should we do? */
367 do_callback_layoutrecall(clp, &args, cps);
368 }
369
nfs4_callback_devicenotify(void * argp,void * resp,struct cb_process_state * cps)370 __be32 nfs4_callback_devicenotify(void *argp, void *resp,
371 struct cb_process_state *cps)
372 {
373 struct cb_devicenotifyargs *args = argp;
374 const struct pnfs_layoutdriver_type *ld = NULL;
375 uint32_t i;
376 __be32 res = 0;
377
378 if (!cps->clp) {
379 res = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
380 goto out;
381 }
382
383 for (i = 0; i < args->ndevs; i++) {
384 struct cb_devicenotifyitem *dev = &args->devs[i];
385
386 if (!ld || ld->id != dev->cbd_layout_type) {
387 pnfs_put_layoutdriver(ld);
388 ld = pnfs_find_layoutdriver(dev->cbd_layout_type);
389 if (!ld)
390 continue;
391 }
392 nfs4_delete_deviceid(ld, cps->clp, &dev->cbd_dev_id);
393 }
394 pnfs_put_layoutdriver(ld);
395 out:
396 kfree(args->devs);
397 return res;
398 }
399
400 /*
401 * Validate the sequenceID sent by the server.
402 * Return success if the sequenceID is one more than what we last saw on
403 * this slot, accounting for wraparound. Increments the slot's sequence.
404 *
405 * We don't yet implement a duplicate request cache, instead we set the
406 * back channel ca_maxresponsesize_cached to zero. This is OK for now
407 * since we only currently implement idempotent callbacks anyway.
408 *
409 * We have a single slot backchannel at this time, so we don't bother
410 * checking the used_slots bit array on the table. The lower layer guarantees
411 * a single outstanding callback request at a time.
412 */
413 static __be32
validate_seqid(const struct nfs4_slot_table * tbl,const struct nfs4_slot * slot,const struct cb_sequenceargs * args)414 validate_seqid(const struct nfs4_slot_table *tbl, const struct nfs4_slot *slot,
415 const struct cb_sequenceargs * args)
416 {
417 __be32 ret;
418
419 ret = cpu_to_be32(NFS4ERR_BADSLOT);
420 if (args->csa_slotid > tbl->server_highest_slotid)
421 goto out_err;
422
423 /* Replay */
424 if (args->csa_sequenceid == slot->seq_nr) {
425 ret = cpu_to_be32(NFS4ERR_DELAY);
426 if (nfs4_test_locked_slot(tbl, slot->slot_nr))
427 goto out_err;
428
429 /* Signal process_op to set this error on next op */
430 ret = cpu_to_be32(NFS4ERR_RETRY_UNCACHED_REP);
431 if (args->csa_cachethis == 0)
432 goto out_err;
433
434 /* Liar! We never allowed you to set csa_cachethis != 0 */
435 ret = cpu_to_be32(NFS4ERR_SEQ_FALSE_RETRY);
436 goto out_err;
437 }
438
439 /* Note: wraparound relies on seq_nr being of type u32 */
440 /* Misordered request */
441 ret = cpu_to_be32(NFS4ERR_SEQ_MISORDERED);
442 if (args->csa_sequenceid != slot->seq_nr + 1)
443 goto out_err;
444
445 return cpu_to_be32(NFS4_OK);
446
447 out_err:
448 trace_nfs4_cb_seqid_err(args, ret);
449 return ret;
450 }
451
452 /*
453 * For each referring call triple, check the session's slot table for
454 * a match. If the slot is in use and the sequence numbers match, the
455 * client is still waiting for a response to the original request.
456 */
referring_call_exists(struct nfs_client * clp,uint32_t nrclists,struct referring_call_list * rclists,spinlock_t * lock)457 static int referring_call_exists(struct nfs_client *clp,
458 uint32_t nrclists,
459 struct referring_call_list *rclists,
460 spinlock_t *lock)
461 __releases(lock)
462 __acquires(lock)
463 {
464 int status = 0;
465 int found = 0;
466 int i, j;
467 struct nfs4_session *session;
468 struct nfs4_slot_table *tbl;
469 struct referring_call_list *rclist;
470 struct referring_call *ref;
471
472 /*
473 * XXX When client trunking is implemented, this becomes
474 * a session lookup from within the loop
475 */
476 session = clp->cl_session;
477 tbl = &session->fc_slot_table;
478
479 for (i = 0; i < nrclists; i++) {
480 rclist = &rclists[i];
481 if (memcmp(session->sess_id.data,
482 rclist->rcl_sessionid.data,
483 NFS4_MAX_SESSIONID_LEN) != 0)
484 continue;
485
486 for (j = 0; j < rclist->rcl_nrefcalls; j++) {
487 ref = &rclist->rcl_refcalls[j];
488 spin_unlock(lock);
489 status = nfs4_slot_wait_on_seqid(tbl, ref->rc_slotid,
490 ref->rc_sequenceid, HZ >> 1) < 0;
491 spin_lock(lock);
492 if (status)
493 goto out;
494 found++;
495 }
496 }
497
498 out:
499 return status < 0 ? status : found;
500 }
501
nfs4_callback_sequence(void * argp,void * resp,struct cb_process_state * cps)502 __be32 nfs4_callback_sequence(void *argp, void *resp,
503 struct cb_process_state *cps)
504 {
505 struct cb_sequenceargs *args = argp;
506 struct cb_sequenceres *res = resp;
507 struct nfs4_slot_table *tbl;
508 struct nfs4_slot *slot;
509 struct nfs_client *clp;
510 int ret;
511 int i;
512 __be32 status = htonl(NFS4ERR_BADSESSION);
513
514 clp = nfs4_find_client_sessionid(cps->net, args->csa_addr,
515 &args->csa_sessionid, cps->minorversion);
516 if (clp == NULL)
517 goto out;
518
519 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
520 goto out;
521
522 tbl = &clp->cl_session->bc_slot_table;
523
524 /* Set up res before grabbing the spinlock */
525 memcpy(&res->csr_sessionid, &args->csa_sessionid,
526 sizeof(res->csr_sessionid));
527 res->csr_sequenceid = args->csa_sequenceid;
528 res->csr_slotid = args->csa_slotid;
529
530 spin_lock(&tbl->slot_tbl_lock);
531 /* state manager is resetting the session */
532 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
533 status = htonl(NFS4ERR_DELAY);
534 /* Return NFS4ERR_BADSESSION if we're draining the session
535 * in order to reset it.
536 */
537 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
538 status = htonl(NFS4ERR_BADSESSION);
539 goto out_unlock;
540 }
541
542 status = htonl(NFS4ERR_BADSLOT);
543 slot = nfs4_lookup_slot(tbl, args->csa_slotid);
544 if (IS_ERR(slot))
545 goto out_unlock;
546
547 res->csr_highestslotid = tbl->server_highest_slotid;
548 res->csr_target_highestslotid = tbl->target_highest_slotid;
549
550 status = validate_seqid(tbl, slot, args);
551 if (status)
552 goto out_unlock;
553 if (!nfs4_try_to_lock_slot(tbl, slot)) {
554 status = htonl(NFS4ERR_DELAY);
555 goto out_unlock;
556 }
557 cps->slot = slot;
558
559 /* The ca_maxresponsesize_cached is 0 with no DRC */
560 if (args->csa_cachethis != 0) {
561 status = htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
562 goto out_unlock;
563 }
564
565 /*
566 * Check for pending referring calls. If a match is found, a
567 * related callback was received before the response to the original
568 * call.
569 */
570 ret = referring_call_exists(clp, args->csa_nrclists, args->csa_rclists,
571 &tbl->slot_tbl_lock);
572 if (ret < 0) {
573 status = htonl(NFS4ERR_DELAY);
574 goto out_unlock;
575 }
576 cps->referring_calls = ret;
577
578 /*
579 * RFC5661 20.9.3
580 * If CB_SEQUENCE returns an error, then the state of the slot
581 * (sequence ID, cached reply) MUST NOT change.
582 */
583 slot->seq_nr = args->csa_sequenceid;
584 out_unlock:
585 spin_unlock(&tbl->slot_tbl_lock);
586
587 out:
588 cps->clp = clp; /* put in nfs4_callback_compound */
589 for (i = 0; i < args->csa_nrclists; i++)
590 kfree(args->csa_rclists[i].rcl_refcalls);
591 kfree(args->csa_rclists);
592
593 if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
594 cps->drc_status = status;
595 status = 0;
596 } else
597 res->csr_status = status;
598
599 trace_nfs4_cb_sequence(args, res, status);
600 return status;
601 }
602
603 static bool
validate_bitmap_values(unsigned int mask)604 validate_bitmap_values(unsigned int mask)
605 {
606 return (mask & ~RCA4_TYPE_MASK_ALL) == 0;
607 }
608
nfs4_callback_recallany(void * argp,void * resp,struct cb_process_state * cps)609 __be32 nfs4_callback_recallany(void *argp, void *resp,
610 struct cb_process_state *cps)
611 {
612 struct cb_recallanyargs *args = argp;
613 __be32 status;
614 fmode_t flags = 0;
615 bool schedule_manager = false;
616
617 status = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
618 if (!cps->clp) /* set in cb_sequence */
619 goto out;
620
621 dprintk_rcu("NFS: RECALL_ANY callback request from %s\n",
622 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
623
624 status = cpu_to_be32(NFS4ERR_INVAL);
625 if (!validate_bitmap_values(args->craa_type_mask))
626 goto out;
627
628 status = cpu_to_be32(NFS4_OK);
629 if (args->craa_type_mask & BIT(RCA4_TYPE_MASK_RDATA_DLG))
630 flags = FMODE_READ;
631 if (args->craa_type_mask & BIT(RCA4_TYPE_MASK_WDATA_DLG))
632 flags |= FMODE_WRITE;
633 if (flags)
634 nfs_expire_unused_delegation_types(cps->clp, flags);
635
636 if (args->craa_type_mask & BIT(RCA4_TYPE_MASK_FILE_LAYOUT))
637 pnfs_recall_all_layouts(cps->clp, cps);
638
639 if (args->craa_type_mask & BIT(PNFS_FF_RCA4_TYPE_MASK_READ)) {
640 set_bit(NFS4CLNT_RECALL_ANY_LAYOUT_READ, &cps->clp->cl_state);
641 schedule_manager = true;
642 }
643 if (args->craa_type_mask & BIT(PNFS_FF_RCA4_TYPE_MASK_RW)) {
644 set_bit(NFS4CLNT_RECALL_ANY_LAYOUT_RW, &cps->clp->cl_state);
645 schedule_manager = true;
646 }
647 if (schedule_manager)
648 nfs4_schedule_state_manager(cps->clp);
649
650 out:
651 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
652 return status;
653 }
654
655 /* Reduce the fore channel's max_slots to the target value */
nfs4_callback_recallslot(void * argp,void * resp,struct cb_process_state * cps)656 __be32 nfs4_callback_recallslot(void *argp, void *resp,
657 struct cb_process_state *cps)
658 {
659 struct cb_recallslotargs *args = argp;
660 struct nfs4_slot_table *fc_tbl;
661 __be32 status;
662
663 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
664 if (!cps->clp) /* set in cb_sequence */
665 goto out;
666
667 dprintk_rcu("NFS: CB_RECALL_SLOT request from %s target highest slotid %u\n",
668 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR),
669 args->crsa_target_highest_slotid);
670
671 fc_tbl = &cps->clp->cl_session->fc_slot_table;
672
673 status = htonl(NFS4_OK);
674
675 nfs41_set_target_slotid(fc_tbl, args->crsa_target_highest_slotid);
676 nfs41_notify_server(cps->clp);
677 out:
678 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
679 return status;
680 }
681
nfs4_callback_notify_lock(void * argp,void * resp,struct cb_process_state * cps)682 __be32 nfs4_callback_notify_lock(void *argp, void *resp,
683 struct cb_process_state *cps)
684 {
685 struct cb_notify_lock_args *args = argp;
686
687 if (!cps->clp) /* set in cb_sequence */
688 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
689
690 dprintk_rcu("NFS: CB_NOTIFY_LOCK request from %s\n",
691 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
692
693 /* Don't wake anybody if the string looked bogus */
694 if (args->cbnl_valid)
695 __wake_up(&cps->clp->cl_lock_waitq, TASK_NORMAL, 0, args);
696
697 return htonl(NFS4_OK);
698 }
699 #ifdef CONFIG_NFS_V4_2
nfs4_copy_cb_args(struct nfs4_copy_state * cp_state,struct cb_offloadargs * args)700 static void nfs4_copy_cb_args(struct nfs4_copy_state *cp_state,
701 struct cb_offloadargs *args)
702 {
703 cp_state->count = args->wr_count;
704 cp_state->error = args->error;
705 if (!args->error) {
706 cp_state->verf.committed = args->wr_writeverf.committed;
707 memcpy(&cp_state->verf.verifier.data[0],
708 &args->wr_writeverf.verifier.data[0],
709 NFS4_VERIFIER_SIZE);
710 }
711 }
712
nfs4_callback_offload(void * data,void * dummy,struct cb_process_state * cps)713 __be32 nfs4_callback_offload(void *data, void *dummy,
714 struct cb_process_state *cps)
715 {
716 struct cb_offloadargs *args = data;
717 struct nfs_server *server;
718 struct nfs4_copy_state *copy, *tmp_copy;
719 bool found = false;
720
721 copy = kzalloc_obj(struct nfs4_copy_state);
722 if (!copy)
723 return cpu_to_be32(NFS4ERR_DELAY);
724
725 spin_lock(&cps->clp->cl_lock);
726 rcu_read_lock();
727 list_for_each_entry_rcu(server, &cps->clp->cl_superblocks,
728 client_link) {
729 list_for_each_entry(tmp_copy, &server->ss_copies, copies) {
730 if (memcmp(args->coa_stateid.other,
731 tmp_copy->stateid.other,
732 sizeof(args->coa_stateid.other)))
733 continue;
734 nfs4_copy_cb_args(tmp_copy, args);
735 complete(&tmp_copy->completion);
736 found = true;
737 goto out;
738 }
739 }
740 out:
741 rcu_read_unlock();
742 if (!found) {
743 memcpy(©->stateid, &args->coa_stateid, NFS4_STATEID_SIZE);
744 nfs4_copy_cb_args(copy, args);
745 list_add_tail(©->copies, &cps->clp->pending_cb_stateids);
746 } else
747 kfree(copy);
748 spin_unlock(&cps->clp->cl_lock);
749
750 trace_nfs4_cb_offload(&args->coa_fh, &args->coa_stateid,
751 args->wr_count, args->error,
752 args->wr_writeverf.committed);
753 return 0;
754 }
755 #endif /* CONFIG_NFS_V4_2 */
756