callback_proc.c 11 KB

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