callback_proc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * linux/fs/nfs/callback_proc.c
  3. *
  4. * Copyright (C) 2004 Trond Myklebust
  5. *
  6. * NFSv4 callback procedures
  7. */
  8. #include <linux/nfs4.h>
  9. #include <linux/nfs_fs.h>
  10. #include <linux/slab.h>
  11. #include <linux/rcupdate.h>
  12. #include "nfs4_fs.h"
  13. #include "callback.h"
  14. #include "delegation.h"
  15. #include "internal.h"
  16. #include "pnfs.h"
  17. #include "nfs4session.h"
  18. #include "nfs4trace.h"
  19. #ifdef NFS_DEBUG
  20. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  21. #endif
  22. __be32 nfs4_callback_getattr(struct cb_getattrargs *args,
  23. struct cb_getattrres *res,
  24. struct cb_process_state *cps)
  25. {
  26. struct nfs_delegation *delegation;
  27. struct nfs_inode *nfsi;
  28. struct inode *inode;
  29. res->status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
  30. if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
  31. goto out;
  32. res->bitmap[0] = res->bitmap[1] = 0;
  33. res->status = htonl(NFS4ERR_BADHANDLE);
  34. dprintk_rcu("NFS: GETATTR callback request from %s\n",
  35. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  36. inode = nfs_delegation_find_inode(cps->clp, &args->fh);
  37. if (inode == NULL) {
  38. trace_nfs4_cb_getattr(cps->clp, &args->fh, NULL,
  39. -ntohl(res->status));
  40. goto out;
  41. }
  42. nfsi = NFS_I(inode);
  43. rcu_read_lock();
  44. delegation = rcu_dereference(nfsi->delegation);
  45. if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
  46. goto out_iput;
  47. res->size = i_size_read(inode);
  48. res->change_attr = delegation->change_attr;
  49. if (nfsi->nrequests != 0)
  50. res->change_attr++;
  51. res->ctime = inode->i_ctime;
  52. res->mtime = inode->i_mtime;
  53. res->bitmap[0] = (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) &
  54. args->bitmap[0];
  55. res->bitmap[1] = (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) &
  56. args->bitmap[1];
  57. res->status = 0;
  58. out_iput:
  59. rcu_read_unlock();
  60. trace_nfs4_cb_getattr(cps->clp, &args->fh, inode, -ntohl(res->status));
  61. iput(inode);
  62. out:
  63. dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
  64. return res->status;
  65. }
  66. __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy,
  67. struct cb_process_state *cps)
  68. {
  69. struct inode *inode;
  70. __be32 res;
  71. res = htonl(NFS4ERR_OP_NOT_IN_SESSION);
  72. if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
  73. goto out;
  74. dprintk_rcu("NFS: RECALL callback request from %s\n",
  75. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  76. res = htonl(NFS4ERR_BADHANDLE);
  77. inode = nfs_delegation_find_inode(cps->clp, &args->fh);
  78. if (inode == NULL)
  79. goto out;
  80. /* Set up a helper thread to actually return the delegation */
  81. switch (nfs_async_inode_return_delegation(inode, &args->stateid)) {
  82. case 0:
  83. res = 0;
  84. break;
  85. case -ENOENT:
  86. res = htonl(NFS4ERR_BAD_STATEID);
  87. break;
  88. default:
  89. res = htonl(NFS4ERR_RESOURCE);
  90. }
  91. trace_nfs4_recall_delegation(inode, -ntohl(res));
  92. iput(inode);
  93. out:
  94. dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
  95. return res;
  96. }
  97. #if defined(CONFIG_NFS_V4_1)
  98. /*
  99. * Lookup a layout by filehandle.
  100. *
  101. * Note: gets a refcount on the layout hdr and on its respective inode.
  102. * Caller must put the layout hdr and the inode.
  103. *
  104. * TODO: keep track of all layouts (and delegations) in a hash table
  105. * hashed by filehandle.
  106. */
  107. static struct pnfs_layout_hdr * get_layout_by_fh_locked(struct nfs_client *clp,
  108. struct nfs_fh *fh, nfs4_stateid *stateid)
  109. {
  110. struct nfs_server *server;
  111. struct inode *ino;
  112. struct pnfs_layout_hdr *lo;
  113. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  114. list_for_each_entry(lo, &server->layouts, plh_layouts) {
  115. if (!nfs4_stateid_match_other(&lo->plh_stateid, stateid))
  116. continue;
  117. if (nfs_compare_fh(fh, &NFS_I(lo->plh_inode)->fh))
  118. continue;
  119. ino = igrab(lo->plh_inode);
  120. if (!ino)
  121. break;
  122. spin_lock(&ino->i_lock);
  123. /* Is this layout in the process of being freed? */
  124. if (NFS_I(ino)->layout != lo) {
  125. spin_unlock(&ino->i_lock);
  126. iput(ino);
  127. break;
  128. }
  129. pnfs_get_layout_hdr(lo);
  130. spin_unlock(&ino->i_lock);
  131. return lo;
  132. }
  133. }
  134. return NULL;
  135. }
  136. static struct pnfs_layout_hdr * get_layout_by_fh(struct nfs_client *clp,
  137. struct nfs_fh *fh, nfs4_stateid *stateid)
  138. {
  139. struct pnfs_layout_hdr *lo;
  140. spin_lock(&clp->cl_lock);
  141. rcu_read_lock();
  142. lo = get_layout_by_fh_locked(clp, fh, stateid);
  143. rcu_read_unlock();
  144. spin_unlock(&clp->cl_lock);
  145. return lo;
  146. }
  147. static u32 initiate_file_draining(struct nfs_client *clp,
  148. struct cb_layoutrecallargs *args)
  149. {
  150. struct inode *ino;
  151. struct pnfs_layout_hdr *lo;
  152. u32 rv = NFS4ERR_NOMATCHING_LAYOUT;
  153. LIST_HEAD(free_me_list);
  154. lo = get_layout_by_fh(clp, &args->cbl_fh, &args->cbl_stateid);
  155. if (!lo)
  156. goto out;
  157. ino = lo->plh_inode;
  158. spin_lock(&ino->i_lock);
  159. pnfs_set_layout_stateid(lo, &args->cbl_stateid, true);
  160. spin_unlock(&ino->i_lock);
  161. pnfs_layoutcommit_inode(ino, false);
  162. spin_lock(&ino->i_lock);
  163. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
  164. pnfs_mark_matching_lsegs_invalid(lo, &free_me_list,
  165. &args->cbl_range)) {
  166. rv = NFS4ERR_DELAY;
  167. goto unlock;
  168. }
  169. if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
  170. NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo,
  171. &args->cbl_range);
  172. }
  173. unlock:
  174. spin_unlock(&ino->i_lock);
  175. pnfs_free_lseg_list(&free_me_list);
  176. pnfs_put_layout_hdr(lo);
  177. trace_nfs4_cb_layoutrecall_inode(clp, &args->cbl_fh, ino, -rv);
  178. iput(ino);
  179. out:
  180. return rv;
  181. }
  182. static u32 initiate_bulk_draining(struct nfs_client *clp,
  183. struct cb_layoutrecallargs *args)
  184. {
  185. int stat;
  186. if (args->cbl_recall_type == RETURN_FSID)
  187. stat = pnfs_destroy_layouts_byfsid(clp, &args->cbl_fsid, true);
  188. else
  189. stat = pnfs_destroy_layouts_byclid(clp, true);
  190. if (stat != 0)
  191. return NFS4ERR_DELAY;
  192. return NFS4ERR_NOMATCHING_LAYOUT;
  193. }
  194. static u32 do_callback_layoutrecall(struct nfs_client *clp,
  195. struct cb_layoutrecallargs *args)
  196. {
  197. u32 res;
  198. dprintk("%s enter, type=%i\n", __func__, args->cbl_recall_type);
  199. if (args->cbl_recall_type == RETURN_FILE)
  200. res = initiate_file_draining(clp, args);
  201. else
  202. res = initiate_bulk_draining(clp, args);
  203. dprintk("%s returning %i\n", __func__, res);
  204. return res;
  205. }
  206. __be32 nfs4_callback_layoutrecall(struct cb_layoutrecallargs *args,
  207. void *dummy, struct cb_process_state *cps)
  208. {
  209. u32 res;
  210. dprintk("%s: -->\n", __func__);
  211. if (cps->clp)
  212. res = do_callback_layoutrecall(cps->clp, args);
  213. else
  214. res = NFS4ERR_OP_NOT_IN_SESSION;
  215. dprintk("%s: exit with status = %d\n", __func__, res);
  216. return cpu_to_be32(res);
  217. }
  218. static void pnfs_recall_all_layouts(struct nfs_client *clp)
  219. {
  220. struct cb_layoutrecallargs args;
  221. /* Pretend we got a CB_LAYOUTRECALL(ALL) */
  222. memset(&args, 0, sizeof(args));
  223. args.cbl_recall_type = RETURN_ALL;
  224. /* FIXME we ignore errors, what should we do? */
  225. do_callback_layoutrecall(clp, &args);
  226. }
  227. __be32 nfs4_callback_devicenotify(struct cb_devicenotifyargs *args,
  228. void *dummy, struct cb_process_state *cps)
  229. {
  230. int i;
  231. __be32 res = 0;
  232. struct nfs_client *clp = cps->clp;
  233. struct nfs_server *server = NULL;
  234. dprintk("%s: -->\n", __func__);
  235. if (!clp) {
  236. res = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
  237. goto out;
  238. }
  239. for (i = 0; i < args->ndevs; i++) {
  240. struct cb_devicenotifyitem *dev = &args->devs[i];
  241. if (!server ||
  242. server->pnfs_curr_ld->id != dev->cbd_layout_type) {
  243. rcu_read_lock();
  244. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  245. if (server->pnfs_curr_ld &&
  246. server->pnfs_curr_ld->id == dev->cbd_layout_type) {
  247. rcu_read_unlock();
  248. goto found;
  249. }
  250. rcu_read_unlock();
  251. dprintk("%s: layout type %u not found\n",
  252. __func__, dev->cbd_layout_type);
  253. continue;
  254. }
  255. found:
  256. nfs4_delete_deviceid(server->pnfs_curr_ld, clp, &dev->cbd_dev_id);
  257. }
  258. out:
  259. kfree(args->devs);
  260. dprintk("%s: exit with status = %u\n",
  261. __func__, be32_to_cpu(res));
  262. return res;
  263. }
  264. /*
  265. * Validate the sequenceID sent by the server.
  266. * Return success if the sequenceID is one more than what we last saw on
  267. * this slot, accounting for wraparound. Increments the slot's sequence.
  268. *
  269. * We don't yet implement a duplicate request cache, instead we set the
  270. * back channel ca_maxresponsesize_cached to zero. This is OK for now
  271. * since we only currently implement idempotent callbacks anyway.
  272. *
  273. * We have a single slot backchannel at this time, so we don't bother
  274. * checking the used_slots bit array on the table. The lower layer guarantees
  275. * a single outstanding callback request at a time.
  276. */
  277. static __be32
  278. validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
  279. {
  280. struct nfs4_slot *slot;
  281. dprintk("%s enter. slotid %u seqid %u\n",
  282. __func__, args->csa_slotid, args->csa_sequenceid);
  283. if (args->csa_slotid >= NFS41_BC_MAX_CALLBACKS)
  284. return htonl(NFS4ERR_BADSLOT);
  285. slot = tbl->slots + args->csa_slotid;
  286. dprintk("%s slot table seqid: %u\n", __func__, slot->seq_nr);
  287. /* Normal */
  288. if (likely(args->csa_sequenceid == slot->seq_nr + 1))
  289. goto out_ok;
  290. /* Replay */
  291. if (args->csa_sequenceid == slot->seq_nr) {
  292. dprintk("%s seqid %u is a replay\n",
  293. __func__, args->csa_sequenceid);
  294. /* Signal process_op to set this error on next op */
  295. if (args->csa_cachethis == 0)
  296. return htonl(NFS4ERR_RETRY_UNCACHED_REP);
  297. /* The ca_maxresponsesize_cached is 0 with no DRC */
  298. else if (args->csa_cachethis == 1)
  299. return htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
  300. }
  301. /* Wraparound */
  302. if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
  303. slot->seq_nr = 1;
  304. goto out_ok;
  305. }
  306. /* Misordered request */
  307. return htonl(NFS4ERR_SEQ_MISORDERED);
  308. out_ok:
  309. tbl->highest_used_slotid = args->csa_slotid;
  310. return htonl(NFS4_OK);
  311. }
  312. /*
  313. * For each referring call triple, check the session's slot table for
  314. * a match. If the slot is in use and the sequence numbers match, the
  315. * client is still waiting for a response to the original request.
  316. */
  317. static bool referring_call_exists(struct nfs_client *clp,
  318. uint32_t nrclists,
  319. struct referring_call_list *rclists)
  320. {
  321. bool status = 0;
  322. int i, j;
  323. struct nfs4_session *session;
  324. struct nfs4_slot_table *tbl;
  325. struct referring_call_list *rclist;
  326. struct referring_call *ref;
  327. /*
  328. * XXX When client trunking is implemented, this becomes
  329. * a session lookup from within the loop
  330. */
  331. session = clp->cl_session;
  332. tbl = &session->fc_slot_table;
  333. for (i = 0; i < nrclists; i++) {
  334. rclist = &rclists[i];
  335. if (memcmp(session->sess_id.data,
  336. rclist->rcl_sessionid.data,
  337. NFS4_MAX_SESSIONID_LEN) != 0)
  338. continue;
  339. for (j = 0; j < rclist->rcl_nrefcalls; j++) {
  340. ref = &rclist->rcl_refcalls[j];
  341. dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u "
  342. "slotid %u\n", __func__,
  343. ((u32 *)&rclist->rcl_sessionid.data)[0],
  344. ((u32 *)&rclist->rcl_sessionid.data)[1],
  345. ((u32 *)&rclist->rcl_sessionid.data)[2],
  346. ((u32 *)&rclist->rcl_sessionid.data)[3],
  347. ref->rc_sequenceid, ref->rc_slotid);
  348. spin_lock(&tbl->slot_tbl_lock);
  349. status = (test_bit(ref->rc_slotid, tbl->used_slots) &&
  350. tbl->slots[ref->rc_slotid].seq_nr ==
  351. ref->rc_sequenceid);
  352. spin_unlock(&tbl->slot_tbl_lock);
  353. if (status)
  354. goto out;
  355. }
  356. }
  357. out:
  358. return status;
  359. }
  360. __be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
  361. struct cb_sequenceres *res,
  362. struct cb_process_state *cps)
  363. {
  364. struct nfs4_slot_table *tbl;
  365. struct nfs4_slot *slot;
  366. struct nfs_client *clp;
  367. int i;
  368. __be32 status = htonl(NFS4ERR_BADSESSION);
  369. clp = nfs4_find_client_sessionid(cps->net, args->csa_addr,
  370. &args->csa_sessionid, cps->minorversion);
  371. if (clp == NULL)
  372. goto out;
  373. if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
  374. goto out;
  375. tbl = &clp->cl_session->bc_slot_table;
  376. slot = tbl->slots + args->csa_slotid;
  377. spin_lock(&tbl->slot_tbl_lock);
  378. /* state manager is resetting the session */
  379. if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
  380. status = htonl(NFS4ERR_DELAY);
  381. /* Return NFS4ERR_BADSESSION if we're draining the session
  382. * in order to reset it.
  383. */
  384. if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  385. status = htonl(NFS4ERR_BADSESSION);
  386. goto out_unlock;
  387. }
  388. memcpy(&res->csr_sessionid, &args->csa_sessionid,
  389. sizeof(res->csr_sessionid));
  390. res->csr_sequenceid = args->csa_sequenceid;
  391. res->csr_slotid = args->csa_slotid;
  392. res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  393. res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  394. status = validate_seqid(tbl, args);
  395. if (status)
  396. goto out_unlock;
  397. cps->slotid = args->csa_slotid;
  398. /*
  399. * Check for pending referring calls. If a match is found, a
  400. * related callback was received before the response to the original
  401. * call.
  402. */
  403. if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
  404. status = htonl(NFS4ERR_DELAY);
  405. goto out_unlock;
  406. }
  407. /*
  408. * RFC5661 20.9.3
  409. * If CB_SEQUENCE returns an error, then the state of the slot
  410. * (sequence ID, cached reply) MUST NOT change.
  411. */
  412. slot->seq_nr++;
  413. out_unlock:
  414. spin_unlock(&tbl->slot_tbl_lock);
  415. out:
  416. cps->clp = clp; /* put in nfs4_callback_compound */
  417. for (i = 0; i < args->csa_nrclists; i++)
  418. kfree(args->csa_rclists[i].rcl_refcalls);
  419. kfree(args->csa_rclists);
  420. if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
  421. cps->drc_status = status;
  422. status = 0;
  423. } else
  424. res->csr_status = status;
  425. trace_nfs4_cb_sequence(args, res, status);
  426. dprintk("%s: exit with status = %d res->csr_status %d\n", __func__,
  427. ntohl(status), ntohl(res->csr_status));
  428. return status;
  429. }
  430. static bool
  431. validate_bitmap_values(unsigned long mask)
  432. {
  433. return (mask & ~RCA4_TYPE_MASK_ALL) == 0;
  434. }
  435. __be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy,
  436. struct cb_process_state *cps)
  437. {
  438. __be32 status;
  439. fmode_t flags = 0;
  440. status = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
  441. if (!cps->clp) /* set in cb_sequence */
  442. goto out;
  443. dprintk_rcu("NFS: RECALL_ANY callback request from %s\n",
  444. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  445. status = cpu_to_be32(NFS4ERR_INVAL);
  446. if (!validate_bitmap_values(args->craa_type_mask))
  447. goto out;
  448. status = cpu_to_be32(NFS4_OK);
  449. if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
  450. &args->craa_type_mask))
  451. flags = FMODE_READ;
  452. if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
  453. &args->craa_type_mask))
  454. flags |= FMODE_WRITE;
  455. if (test_bit(RCA4_TYPE_MASK_FILE_LAYOUT, (const unsigned long *)
  456. &args->craa_type_mask))
  457. pnfs_recall_all_layouts(cps->clp);
  458. if (flags)
  459. nfs_expire_unused_delegation_types(cps->clp, flags);
  460. out:
  461. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  462. return status;
  463. }
  464. /* Reduce the fore channel's max_slots to the target value */
  465. __be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy,
  466. struct cb_process_state *cps)
  467. {
  468. struct nfs4_slot_table *fc_tbl;
  469. __be32 status;
  470. status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
  471. if (!cps->clp) /* set in cb_sequence */
  472. goto out;
  473. dprintk_rcu("NFS: CB_RECALL_SLOT request from %s target highest slotid %u\n",
  474. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR),
  475. args->crsa_target_highest_slotid);
  476. fc_tbl = &cps->clp->cl_session->fc_slot_table;
  477. status = htonl(NFS4_OK);
  478. nfs41_set_target_slotid(fc_tbl, args->crsa_target_highest_slotid);
  479. nfs41_notify_server(cps->clp);
  480. out:
  481. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  482. return status;
  483. }
  484. #endif /* CONFIG_NFS_V4_1 */