callback_proc.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. #ifdef NFS_DEBUG
  16. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  17. #endif
  18. __be32 nfs4_callback_getattr(struct cb_getattrargs *args,
  19. struct cb_getattrres *res,
  20. struct cb_process_state *cps)
  21. {
  22. struct nfs_delegation *delegation;
  23. struct nfs_inode *nfsi;
  24. struct inode *inode;
  25. res->status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
  26. if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
  27. goto out;
  28. res->bitmap[0] = res->bitmap[1] = 0;
  29. res->status = htonl(NFS4ERR_BADHANDLE);
  30. dprintk("NFS: GETATTR callback request from %s\n",
  31. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  32. inode = nfs_delegation_find_inode(cps->clp, &args->fh);
  33. if (inode == NULL)
  34. goto out;
  35. nfsi = NFS_I(inode);
  36. rcu_read_lock();
  37. delegation = rcu_dereference(nfsi->delegation);
  38. if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
  39. goto out_iput;
  40. res->size = i_size_read(inode);
  41. res->change_attr = delegation->change_attr;
  42. if (nfsi->npages != 0)
  43. res->change_attr++;
  44. res->ctime = inode->i_ctime;
  45. res->mtime = inode->i_mtime;
  46. res->bitmap[0] = (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) &
  47. args->bitmap[0];
  48. res->bitmap[1] = (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) &
  49. args->bitmap[1];
  50. res->status = 0;
  51. out_iput:
  52. rcu_read_unlock();
  53. iput(inode);
  54. out:
  55. dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
  56. return res->status;
  57. }
  58. __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy,
  59. struct cb_process_state *cps)
  60. {
  61. struct inode *inode;
  62. __be32 res;
  63. res = htonl(NFS4ERR_OP_NOT_IN_SESSION);
  64. if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
  65. goto out;
  66. dprintk("NFS: RECALL callback request from %s\n",
  67. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  68. res = htonl(NFS4ERR_BADHANDLE);
  69. inode = nfs_delegation_find_inode(cps->clp, &args->fh);
  70. if (inode == NULL)
  71. goto out;
  72. /* Set up a helper thread to actually return the delegation */
  73. switch (nfs_async_inode_return_delegation(inode, &args->stateid)) {
  74. case 0:
  75. res = 0;
  76. break;
  77. case -ENOENT:
  78. if (res != 0)
  79. res = htonl(NFS4ERR_BAD_STATEID);
  80. break;
  81. default:
  82. res = htonl(NFS4ERR_RESOURCE);
  83. }
  84. iput(inode);
  85. out:
  86. dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
  87. return res;
  88. }
  89. int nfs4_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
  90. {
  91. if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
  92. sizeof(delegation->stateid.data)) != 0)
  93. return 0;
  94. return 1;
  95. }
  96. #if defined(CONFIG_NFS_V4_1)
  97. int nfs41_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
  98. {
  99. if (delegation == NULL)
  100. return 0;
  101. if (stateid->stateid.seqid != 0)
  102. return 0;
  103. if (memcmp(&delegation->stateid.stateid.other,
  104. &stateid->stateid.other,
  105. NFS4_STATEID_OTHER_SIZE))
  106. return 0;
  107. return 1;
  108. }
  109. /*
  110. * Validate the sequenceID sent by the server.
  111. * Return success if the sequenceID is one more than what we last saw on
  112. * this slot, accounting for wraparound. Increments the slot's sequence.
  113. *
  114. * We don't yet implement a duplicate request cache, instead we set the
  115. * back channel ca_maxresponsesize_cached to zero. This is OK for now
  116. * since we only currently implement idempotent callbacks anyway.
  117. *
  118. * We have a single slot backchannel at this time, so we don't bother
  119. * checking the used_slots bit array on the table. The lower layer guarantees
  120. * a single outstanding callback request at a time.
  121. */
  122. static __be32
  123. validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
  124. {
  125. struct nfs4_slot *slot;
  126. dprintk("%s enter. slotid %d seqid %d\n",
  127. __func__, args->csa_slotid, args->csa_sequenceid);
  128. if (args->csa_slotid > NFS41_BC_MAX_CALLBACKS)
  129. return htonl(NFS4ERR_BADSLOT);
  130. slot = tbl->slots + args->csa_slotid;
  131. dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
  132. /* Normal */
  133. if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
  134. slot->seq_nr++;
  135. return htonl(NFS4_OK);
  136. }
  137. /* Replay */
  138. if (args->csa_sequenceid == slot->seq_nr) {
  139. dprintk("%s seqid %d is a replay\n",
  140. __func__, args->csa_sequenceid);
  141. /* Signal process_op to set this error on next op */
  142. if (args->csa_cachethis == 0)
  143. return htonl(NFS4ERR_RETRY_UNCACHED_REP);
  144. /* The ca_maxresponsesize_cached is 0 with no DRC */
  145. else if (args->csa_cachethis == 1)
  146. return htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
  147. }
  148. /* Wraparound */
  149. if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
  150. slot->seq_nr = 1;
  151. return htonl(NFS4_OK);
  152. }
  153. /* Misordered request */
  154. return htonl(NFS4ERR_SEQ_MISORDERED);
  155. }
  156. /*
  157. * For each referring call triple, check the session's slot table for
  158. * a match. If the slot is in use and the sequence numbers match, the
  159. * client is still waiting for a response to the original request.
  160. */
  161. static bool referring_call_exists(struct nfs_client *clp,
  162. uint32_t nrclists,
  163. struct referring_call_list *rclists)
  164. {
  165. bool status = 0;
  166. int i, j;
  167. struct nfs4_session *session;
  168. struct nfs4_slot_table *tbl;
  169. struct referring_call_list *rclist;
  170. struct referring_call *ref;
  171. /*
  172. * XXX When client trunking is implemented, this becomes
  173. * a session lookup from within the loop
  174. */
  175. session = clp->cl_session;
  176. tbl = &session->fc_slot_table;
  177. for (i = 0; i < nrclists; i++) {
  178. rclist = &rclists[i];
  179. if (memcmp(session->sess_id.data,
  180. rclist->rcl_sessionid.data,
  181. NFS4_MAX_SESSIONID_LEN) != 0)
  182. continue;
  183. for (j = 0; j < rclist->rcl_nrefcalls; j++) {
  184. ref = &rclist->rcl_refcalls[j];
  185. dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u "
  186. "slotid %u\n", __func__,
  187. ((u32 *)&rclist->rcl_sessionid.data)[0],
  188. ((u32 *)&rclist->rcl_sessionid.data)[1],
  189. ((u32 *)&rclist->rcl_sessionid.data)[2],
  190. ((u32 *)&rclist->rcl_sessionid.data)[3],
  191. ref->rc_sequenceid, ref->rc_slotid);
  192. spin_lock(&tbl->slot_tbl_lock);
  193. status = (test_bit(ref->rc_slotid, tbl->used_slots) &&
  194. tbl->slots[ref->rc_slotid].seq_nr ==
  195. ref->rc_sequenceid);
  196. spin_unlock(&tbl->slot_tbl_lock);
  197. if (status)
  198. goto out;
  199. }
  200. }
  201. out:
  202. return status;
  203. }
  204. __be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
  205. struct cb_sequenceres *res,
  206. struct cb_process_state *cps)
  207. {
  208. struct nfs_client *clp;
  209. int i;
  210. __be32 status;
  211. cps->clp = NULL;
  212. status = htonl(NFS4ERR_BADSESSION);
  213. /* Incoming session must match the callback session */
  214. if (memcmp(&args->csa_sessionid, cps->svc_sid, NFS4_MAX_SESSIONID_LEN))
  215. goto out;
  216. clp = nfs4_find_client_sessionid(args->csa_addr,
  217. &args->csa_sessionid, 1);
  218. if (clp == NULL)
  219. goto out;
  220. /* state manager is resetting the session */
  221. if (test_bit(NFS4_SESSION_DRAINING, &clp->cl_session->session_state)) {
  222. status = NFS4ERR_DELAY;
  223. goto out;
  224. }
  225. status = validate_seqid(&clp->cl_session->bc_slot_table, args);
  226. if (status)
  227. goto out;
  228. /*
  229. * Check for pending referring calls. If a match is found, a
  230. * related callback was received before the response to the original
  231. * call.
  232. */
  233. if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
  234. status = htonl(NFS4ERR_DELAY);
  235. goto out;
  236. }
  237. memcpy(&res->csr_sessionid, &args->csa_sessionid,
  238. sizeof(res->csr_sessionid));
  239. res->csr_sequenceid = args->csa_sequenceid;
  240. res->csr_slotid = args->csa_slotid;
  241. res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  242. res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  243. nfs4_cb_take_slot(clp);
  244. cps->clp = clp; /* put in nfs4_callback_compound */
  245. out:
  246. for (i = 0; i < args->csa_nrclists; i++)
  247. kfree(args->csa_rclists[i].rcl_refcalls);
  248. kfree(args->csa_rclists);
  249. if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
  250. cps->drc_status = status;
  251. status = 0;
  252. } else
  253. res->csr_status = status;
  254. dprintk("%s: exit with status = %d res->csr_status %d\n", __func__,
  255. ntohl(status), ntohl(res->csr_status));
  256. return status;
  257. }
  258. __be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy,
  259. struct cb_process_state *cps)
  260. {
  261. __be32 status;
  262. fmode_t flags = 0;
  263. status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
  264. if (!cps->clp) /* set in cb_sequence */
  265. goto out;
  266. dprintk("NFS: RECALL_ANY callback request from %s\n",
  267. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  268. if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
  269. &args->craa_type_mask))
  270. flags = FMODE_READ;
  271. if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
  272. &args->craa_type_mask))
  273. flags |= FMODE_WRITE;
  274. if (flags)
  275. nfs_expire_all_delegation_types(cps->clp, flags);
  276. status = htonl(NFS4_OK);
  277. out:
  278. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  279. return status;
  280. }
  281. /* Reduce the fore channel's max_slots to the target value */
  282. __be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy,
  283. struct cb_process_state *cps)
  284. {
  285. struct nfs4_slot_table *fc_tbl;
  286. __be32 status;
  287. status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
  288. if (!cps->clp) /* set in cb_sequence */
  289. goto out;
  290. dprintk("NFS: CB_RECALL_SLOT request from %s target max slots %d\n",
  291. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR),
  292. args->crsa_target_max_slots);
  293. fc_tbl = &cps->clp->cl_session->fc_slot_table;
  294. status = htonl(NFS4ERR_BAD_HIGH_SLOT);
  295. if (args->crsa_target_max_slots > fc_tbl->max_slots ||
  296. args->crsa_target_max_slots < 1)
  297. goto out;
  298. status = htonl(NFS4_OK);
  299. if (args->crsa_target_max_slots == fc_tbl->max_slots)
  300. goto out;
  301. fc_tbl->target_max_slots = args->crsa_target_max_slots;
  302. nfs41_handle_recall_slot(cps->clp);
  303. out:
  304. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  305. return status;
  306. }
  307. #endif /* CONFIG_NFS_V4_1 */