callback_proc.c 20 KB

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