callback_proc.c 10 KB

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