callback_proc.c 15 KB

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