callback_proc.c 11 KB

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