callback_proc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /* FIXME: referring calls should be processed */
  198. unsigned nfs4_callback_sequence(struct cb_sequenceargs *args,
  199. struct cb_sequenceres *res)
  200. {
  201. struct nfs_client *clp;
  202. int i, status;
  203. for (i = 0; i < args->csa_nrclists; i++)
  204. kfree(args->csa_rclists[i].rcl_refcalls);
  205. kfree(args->csa_rclists);
  206. status = htonl(NFS4ERR_BADSESSION);
  207. clp = find_client_with_session(args->csa_addr, 4, &args->csa_sessionid);
  208. if (clp == NULL)
  209. goto out;
  210. status = validate_seqid(&clp->cl_session->bc_slot_table,
  211. args->csa_slotid, args->csa_sequenceid);
  212. if (status)
  213. goto out_putclient;
  214. memcpy(&res->csr_sessionid, &args->csa_sessionid,
  215. sizeof(res->csr_sessionid));
  216. res->csr_sequenceid = args->csa_sequenceid;
  217. res->csr_slotid = args->csa_slotid;
  218. res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  219. res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  220. out_putclient:
  221. nfs_put_client(clp);
  222. out:
  223. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  224. res->csr_status = status;
  225. return res->csr_status;
  226. }
  227. unsigned nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy)
  228. {
  229. struct nfs_client *clp;
  230. int status;
  231. fmode_t flags = 0;
  232. status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
  233. clp = nfs_find_client(args->craa_addr, 4);
  234. if (clp == NULL)
  235. goto out;
  236. dprintk("NFS: RECALL_ANY callback request from %s\n",
  237. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  238. if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
  239. &args->craa_type_mask))
  240. flags = FMODE_READ;
  241. if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
  242. &args->craa_type_mask))
  243. flags |= FMODE_WRITE;
  244. if (flags)
  245. nfs_expire_all_delegation_types(clp, flags);
  246. status = htonl(NFS4_OK);
  247. out:
  248. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  249. return status;
  250. }
  251. #endif /* CONFIG_NFS_V4_1 */