callback_proc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy)
  59. {
  60. struct nfs_client *clp;
  61. struct inode *inode;
  62. __be32 res;
  63. res = htonl(NFS4ERR_BADHANDLE);
  64. clp = nfs_find_client(args->addr, 4);
  65. if (clp == NULL)
  66. goto out;
  67. dprintk("NFS: RECALL callback request from %s\n",
  68. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  69. do {
  70. struct nfs_client *prev = clp;
  71. inode = nfs_delegation_find_inode(clp, &args->fh);
  72. if (inode != NULL) {
  73. /* Set up a helper thread to actually return the delegation */
  74. switch(nfs_async_inode_return_delegation(inode, &args->stateid)) {
  75. case 0:
  76. res = 0;
  77. break;
  78. case -ENOENT:
  79. if (res != 0)
  80. res = htonl(NFS4ERR_BAD_STATEID);
  81. break;
  82. default:
  83. res = htonl(NFS4ERR_RESOURCE);
  84. }
  85. iput(inode);
  86. }
  87. clp = nfs_find_client_next(prev);
  88. nfs_put_client(prev);
  89. } while (clp != NULL);
  90. out:
  91. dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
  92. return res;
  93. }
  94. #if defined(CONFIG_NFS_V4_1)
  95. /*
  96. * Validate the sequenceID sent by the server.
  97. * Return success if the sequenceID is one more than what we last saw on
  98. * this slot, accounting for wraparound. Increments the slot's sequence.
  99. *
  100. * We don't yet implement a duplicate request cache, so at this time
  101. * we will log replays, and process them as if we had not seen them before,
  102. * but we don't bump the sequence in the slot. Not too worried about it,
  103. * since we only currently implement idempotent callbacks anyway.
  104. *
  105. * We have a single slot backchannel at this time, so we don't bother
  106. * checking the used_slots bit array on the table. The lower layer guarantees
  107. * a single outstanding callback request at a time.
  108. */
  109. static int
  110. validate_seqid(struct nfs4_slot_table *tbl, u32 slotid, u32 seqid)
  111. {
  112. struct nfs4_slot *slot;
  113. dprintk("%s enter. slotid %d seqid %d\n",
  114. __func__, slotid, seqid);
  115. if (slotid > NFS41_BC_MAX_CALLBACKS)
  116. return htonl(NFS4ERR_BADSLOT);
  117. slot = tbl->slots + slotid;
  118. dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
  119. /* Normal */
  120. if (likely(seqid == slot->seq_nr + 1)) {
  121. slot->seq_nr++;
  122. return htonl(NFS4_OK);
  123. }
  124. /* Replay */
  125. if (seqid == slot->seq_nr) {
  126. dprintk("%s seqid %d is a replay - no DRC available\n",
  127. __func__, seqid);
  128. return htonl(NFS4_OK);
  129. }
  130. /* Wraparound */
  131. if (seqid == 1 && (slot->seq_nr + 1) == 0) {
  132. slot->seq_nr = 1;
  133. return htonl(NFS4_OK);
  134. }
  135. /* Misordered request */
  136. return htonl(NFS4ERR_SEQ_MISORDERED);
  137. }
  138. /*
  139. * Returns a pointer to a held 'struct nfs_client' that matches the server's
  140. * address, major version number, and session ID. It is the caller's
  141. * responsibility to release the returned reference.
  142. *
  143. * Returns NULL if there are no connections with sessions, or if no session
  144. * matches the one of interest.
  145. */
  146. static struct nfs_client *find_client_with_session(
  147. const struct sockaddr *addr, u32 nfsversion,
  148. struct nfs4_sessionid *sessionid)
  149. {
  150. struct nfs_client *clp;
  151. clp = nfs_find_client(addr, 4);
  152. if (clp == NULL)
  153. return NULL;
  154. do {
  155. struct nfs_client *prev = clp;
  156. if (clp->cl_session != NULL) {
  157. if (memcmp(clp->cl_session->sess_id.data,
  158. sessionid->data,
  159. NFS4_MAX_SESSIONID_LEN) == 0) {
  160. /* Returns a held reference to clp */
  161. return clp;
  162. }
  163. }
  164. clp = nfs_find_client_next(prev);
  165. nfs_put_client(prev);
  166. } while (clp != NULL);
  167. return NULL;
  168. }
  169. /* FIXME: referring calls should be processed */
  170. unsigned nfs4_callback_sequence(struct cb_sequenceargs *args,
  171. struct cb_sequenceres *res)
  172. {
  173. struct nfs_client *clp;
  174. int i, status;
  175. for (i = 0; i < args->csa_nrclists; i++)
  176. kfree(args->csa_rclists[i].rcl_refcalls);
  177. kfree(args->csa_rclists);
  178. status = htonl(NFS4ERR_BADSESSION);
  179. clp = find_client_with_session(args->csa_addr, 4, &args->csa_sessionid);
  180. if (clp == NULL)
  181. goto out;
  182. status = validate_seqid(&clp->cl_session->bc_slot_table,
  183. args->csa_slotid, args->csa_sequenceid);
  184. if (status)
  185. goto out_putclient;
  186. memcpy(&res->csr_sessionid, &args->csa_sessionid,
  187. sizeof(res->csr_sessionid));
  188. res->csr_sequenceid = args->csa_sequenceid;
  189. res->csr_slotid = args->csa_slotid;
  190. res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  191. res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  192. out_putclient:
  193. nfs_put_client(clp);
  194. out:
  195. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  196. res->csr_status = status;
  197. return res->csr_status;
  198. }
  199. #endif /* CONFIG_NFS_V4_1 */