callback_proc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 "nfs4_fs.h"
  11. #include "callback.h"
  12. #include "delegation.h"
  13. #include "internal.h"
  14. #ifdef NFS_DEBUG
  15. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  16. #endif
  17. __be32 nfs4_callback_getattr(struct cb_getattrargs *args, struct cb_getattrres *res)
  18. {
  19. struct nfs_client *clp;
  20. struct nfs_delegation *delegation;
  21. struct nfs_inode *nfsi;
  22. struct inode *inode;
  23. res->bitmap[0] = res->bitmap[1] = 0;
  24. res->status = htonl(NFS4ERR_BADHANDLE);
  25. clp = nfs_find_client(args->addr, 4);
  26. if (clp == NULL)
  27. goto out;
  28. dprintk("NFS: GETATTR callback request from %s\n",
  29. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  30. inode = nfs_delegation_find_inode(clp, &args->fh);
  31. if (inode == NULL)
  32. goto out_putclient;
  33. nfsi = NFS_I(inode);
  34. down_read(&nfsi->rwsem);
  35. delegation = nfsi->delegation;
  36. if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
  37. goto out_iput;
  38. res->size = i_size_read(inode);
  39. res->change_attr = delegation->change_attr;
  40. if (nfsi->npages != 0)
  41. res->change_attr++;
  42. res->ctime = inode->i_ctime;
  43. res->mtime = inode->i_mtime;
  44. res->bitmap[0] = (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) &
  45. args->bitmap[0];
  46. res->bitmap[1] = (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) &
  47. args->bitmap[1];
  48. res->status = 0;
  49. out_iput:
  50. up_read(&nfsi->rwsem);
  51. iput(inode);
  52. out_putclient:
  53. nfs_put_client(clp);
  54. out:
  55. dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
  56. return res->status;
  57. }
  58. static int (*nfs_validate_delegation_stateid(struct nfs_client *clp))(struct nfs_delegation *, const nfs4_stateid *)
  59. {
  60. #if defined(CONFIG_NFS_V4_1)
  61. if (clp->cl_minorversion > 0)
  62. return nfs41_validate_delegation_stateid;
  63. #endif
  64. return nfs4_validate_delegation_stateid;
  65. }
  66. __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy)
  67. {
  68. struct nfs_client *clp;
  69. struct inode *inode;
  70. __be32 res;
  71. res = htonl(NFS4ERR_BADHANDLE);
  72. clp = nfs_find_client(args->addr, 4);
  73. if (clp == NULL)
  74. goto out;
  75. dprintk("NFS: RECALL callback request from %s\n",
  76. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  77. do {
  78. struct nfs_client *prev = clp;
  79. inode = nfs_delegation_find_inode(clp, &args->fh);
  80. if (inode != NULL) {
  81. /* Set up a helper thread to actually return the delegation */
  82. switch (nfs_async_inode_return_delegation(inode, &args->stateid,
  83. nfs_validate_delegation_stateid(clp))) {
  84. case 0:
  85. res = 0;
  86. break;
  87. case -ENOENT:
  88. if (res != 0)
  89. res = htonl(NFS4ERR_BAD_STATEID);
  90. break;
  91. default:
  92. res = htonl(NFS4ERR_RESOURCE);
  93. }
  94. iput(inode);
  95. }
  96. clp = nfs_find_client_next(prev);
  97. nfs_put_client(prev);
  98. } while (clp != NULL);
  99. out:
  100. dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
  101. return res;
  102. }
  103. int nfs4_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
  104. {
  105. if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
  106. sizeof(delegation->stateid.data)) != 0)
  107. return 0;
  108. return 1;
  109. }
  110. #if defined(CONFIG_NFS_V4_1)
  111. int nfs41_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
  112. {
  113. if (delegation == NULL)
  114. return 0;
  115. /* seqid is 4-bytes long */
  116. if (((u32 *) &stateid->data)[0] != 0)
  117. return 0;
  118. if (memcmp(&delegation->stateid.data[4], &stateid->data[4],
  119. sizeof(stateid->data)-4))
  120. return 0;
  121. return 1;
  122. }
  123. /*
  124. * Validate the sequenceID sent by the server.
  125. * Return success if the sequenceID is one more than what we last saw on
  126. * this slot, accounting for wraparound. Increments the slot's sequence.
  127. *
  128. * We don't yet implement a duplicate request cache, so at this time
  129. * we will log replays, and process them as if we had not seen them before,
  130. * but we don't bump the sequence in the slot. Not too worried about it,
  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 int
  138. validate_seqid(struct nfs4_slot_table *tbl, u32 slotid, u32 seqid)
  139. {
  140. struct nfs4_slot *slot;
  141. dprintk("%s enter. slotid %d seqid %d\n",
  142. __func__, slotid, seqid);
  143. if (slotid > NFS41_BC_MAX_CALLBACKS)
  144. return htonl(NFS4ERR_BADSLOT);
  145. slot = tbl->slots + slotid;
  146. dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
  147. /* Normal */
  148. if (likely(seqid == slot->seq_nr + 1)) {
  149. slot->seq_nr++;
  150. return htonl(NFS4_OK);
  151. }
  152. /* Replay */
  153. if (seqid == slot->seq_nr) {
  154. dprintk("%s seqid %d is a replay - no DRC available\n",
  155. __func__, seqid);
  156. return htonl(NFS4_OK);
  157. }
  158. /* Wraparound */
  159. if (seqid == 1 && (slot->seq_nr + 1) == 0) {
  160. slot->seq_nr = 1;
  161. return htonl(NFS4_OK);
  162. }
  163. /* Misordered request */
  164. return htonl(NFS4ERR_SEQ_MISORDERED);
  165. }
  166. /*
  167. * Returns a pointer to a held 'struct nfs_client' that matches the server's
  168. * address, major version number, and session ID. It is the caller's
  169. * responsibility to release the returned reference.
  170. *
  171. * Returns NULL if there are no connections with sessions, or if no session
  172. * matches the one of interest.
  173. */
  174. static struct nfs_client *find_client_with_session(
  175. const struct sockaddr *addr, u32 nfsversion,
  176. struct nfs4_sessionid *sessionid)
  177. {
  178. struct nfs_client *clp;
  179. clp = nfs_find_client(addr, 4);
  180. if (clp == NULL)
  181. return NULL;
  182. do {
  183. struct nfs_client *prev = clp;
  184. if (clp->cl_session != NULL) {
  185. if (memcmp(clp->cl_session->sess_id.data,
  186. sessionid->data,
  187. NFS4_MAX_SESSIONID_LEN) == 0) {
  188. /* Returns a held reference to clp */
  189. return clp;
  190. }
  191. }
  192. clp = nfs_find_client_next(prev);
  193. nfs_put_client(prev);
  194. } while (clp != NULL);
  195. return NULL;
  196. }
  197. /*
  198. * For each referring call triple, check the session's slot table for
  199. * a match. If the slot is in use and the sequence numbers match, the
  200. * client is still waiting for a response to the original request.
  201. */
  202. static bool referring_call_exists(struct nfs_client *clp,
  203. uint32_t nrclists,
  204. struct referring_call_list *rclists)
  205. {
  206. bool status = 0;
  207. int i, j;
  208. struct nfs4_session *session;
  209. struct nfs4_slot_table *tbl;
  210. struct referring_call_list *rclist;
  211. struct referring_call *ref;
  212. /*
  213. * XXX When client trunking is implemented, this becomes
  214. * a session lookup from within the loop
  215. */
  216. session = clp->cl_session;
  217. tbl = &session->fc_slot_table;
  218. for (i = 0; i < nrclists; i++) {
  219. rclist = &rclists[i];
  220. if (memcmp(session->sess_id.data,
  221. rclist->rcl_sessionid.data,
  222. NFS4_MAX_SESSIONID_LEN) != 0)
  223. continue;
  224. for (j = 0; j < rclist->rcl_nrefcalls; j++) {
  225. ref = &rclist->rcl_refcalls[j];
  226. dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u "
  227. "slotid %u\n", __func__,
  228. ((u32 *)&rclist->rcl_sessionid.data)[0],
  229. ((u32 *)&rclist->rcl_sessionid.data)[1],
  230. ((u32 *)&rclist->rcl_sessionid.data)[2],
  231. ((u32 *)&rclist->rcl_sessionid.data)[3],
  232. ref->rc_sequenceid, ref->rc_slotid);
  233. spin_lock(&tbl->slot_tbl_lock);
  234. status = (test_bit(ref->rc_slotid, tbl->used_slots) &&
  235. tbl->slots[ref->rc_slotid].seq_nr ==
  236. ref->rc_sequenceid);
  237. spin_unlock(&tbl->slot_tbl_lock);
  238. if (status)
  239. goto out;
  240. }
  241. }
  242. out:
  243. return status;
  244. }
  245. unsigned nfs4_callback_sequence(struct cb_sequenceargs *args,
  246. struct cb_sequenceres *res)
  247. {
  248. struct nfs_client *clp;
  249. int i, status;
  250. status = htonl(NFS4ERR_BADSESSION);
  251. clp = find_client_with_session(args->csa_addr, 4, &args->csa_sessionid);
  252. if (clp == NULL)
  253. goto out;
  254. status = validate_seqid(&clp->cl_session->bc_slot_table,
  255. args->csa_slotid, args->csa_sequenceid);
  256. if (status)
  257. goto out_putclient;
  258. /*
  259. * Check for pending referring calls. If a match is found, a
  260. * related callback was received before the response to the original
  261. * call.
  262. */
  263. if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
  264. status = htonl(NFS4ERR_DELAY);
  265. goto out_putclient;
  266. }
  267. memcpy(&res->csr_sessionid, &args->csa_sessionid,
  268. sizeof(res->csr_sessionid));
  269. res->csr_sequenceid = args->csa_sequenceid;
  270. res->csr_slotid = args->csa_slotid;
  271. res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  272. res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  273. out_putclient:
  274. nfs_put_client(clp);
  275. out:
  276. for (i = 0; i < args->csa_nrclists; i++)
  277. kfree(args->csa_rclists[i].rcl_refcalls);
  278. kfree(args->csa_rclists);
  279. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  280. res->csr_status = status;
  281. return res->csr_status;
  282. }
  283. unsigned nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy)
  284. {
  285. struct nfs_client *clp;
  286. int 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. #endif /* CONFIG_NFS_V4_1 */