callback_proc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 <linux/rcupdate.h>
  12. #include "nfs4_fs.h"
  13. #include "callback.h"
  14. #include "delegation.h"
  15. #include "internal.h"
  16. #include "pnfs.h"
  17. #include "nfs4session.h"
  18. #ifdef NFS_DEBUG
  19. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  20. #endif
  21. __be32 nfs4_callback_getattr(struct cb_getattrargs *args,
  22. struct cb_getattrres *res,
  23. struct cb_process_state *cps)
  24. {
  25. struct nfs_delegation *delegation;
  26. struct nfs_inode *nfsi;
  27. struct inode *inode;
  28. res->status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
  29. if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
  30. goto out;
  31. res->bitmap[0] = res->bitmap[1] = 0;
  32. res->status = htonl(NFS4ERR_BADHANDLE);
  33. dprintk_rcu("NFS: GETATTR callback request from %s\n",
  34. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  35. inode = nfs_delegation_find_inode(cps->clp, &args->fh);
  36. if (inode == NULL)
  37. goto out;
  38. nfsi = NFS_I(inode);
  39. rcu_read_lock();
  40. delegation = rcu_dereference(nfsi->delegation);
  41. if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
  42. goto out_iput;
  43. res->size = i_size_read(inode);
  44. res->change_attr = delegation->change_attr;
  45. if (nfsi->npages != 0)
  46. res->change_attr++;
  47. res->ctime = inode->i_ctime;
  48. res->mtime = inode->i_mtime;
  49. res->bitmap[0] = (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) &
  50. args->bitmap[0];
  51. res->bitmap[1] = (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) &
  52. args->bitmap[1];
  53. res->status = 0;
  54. out_iput:
  55. rcu_read_unlock();
  56. iput(inode);
  57. out:
  58. dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
  59. return res->status;
  60. }
  61. __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy,
  62. struct cb_process_state *cps)
  63. {
  64. struct inode *inode;
  65. __be32 res;
  66. res = htonl(NFS4ERR_OP_NOT_IN_SESSION);
  67. if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
  68. goto out;
  69. dprintk_rcu("NFS: RECALL callback request from %s\n",
  70. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  71. res = htonl(NFS4ERR_BADHANDLE);
  72. inode = nfs_delegation_find_inode(cps->clp, &args->fh);
  73. if (inode == NULL)
  74. goto out;
  75. /* Set up a helper thread to actually return the delegation */
  76. switch (nfs_async_inode_return_delegation(inode, &args->stateid)) {
  77. case 0:
  78. res = 0;
  79. break;
  80. case -ENOENT:
  81. res = htonl(NFS4ERR_BAD_STATEID);
  82. break;
  83. default:
  84. res = htonl(NFS4ERR_RESOURCE);
  85. }
  86. iput(inode);
  87. out:
  88. dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
  89. return res;
  90. }
  91. #if defined(CONFIG_NFS_V4_1)
  92. /*
  93. * Lookup a layout by filehandle.
  94. *
  95. * Note: gets a refcount on the layout hdr and on its respective inode.
  96. * Caller must put the layout hdr and the inode.
  97. *
  98. * TODO: keep track of all layouts (and delegations) in a hash table
  99. * hashed by filehandle.
  100. */
  101. static struct pnfs_layout_hdr * get_layout_by_fh_locked(struct nfs_client *clp, struct nfs_fh *fh)
  102. {
  103. struct nfs_server *server;
  104. struct inode *ino;
  105. struct pnfs_layout_hdr *lo;
  106. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  107. list_for_each_entry(lo, &server->layouts, plh_layouts) {
  108. if (nfs_compare_fh(fh, &NFS_I(lo->plh_inode)->fh))
  109. continue;
  110. ino = igrab(lo->plh_inode);
  111. if (!ino)
  112. continue;
  113. spin_lock(&ino->i_lock);
  114. /* Is this layout in the process of being freed? */
  115. if (NFS_I(ino)->layout != lo) {
  116. spin_unlock(&ino->i_lock);
  117. iput(ino);
  118. continue;
  119. }
  120. pnfs_get_layout_hdr(lo);
  121. spin_unlock(&ino->i_lock);
  122. return lo;
  123. }
  124. }
  125. return NULL;
  126. }
  127. static struct pnfs_layout_hdr * get_layout_by_fh(struct nfs_client *clp, struct nfs_fh *fh)
  128. {
  129. struct pnfs_layout_hdr *lo;
  130. spin_lock(&clp->cl_lock);
  131. rcu_read_lock();
  132. lo = get_layout_by_fh_locked(clp, fh);
  133. rcu_read_unlock();
  134. spin_unlock(&clp->cl_lock);
  135. return lo;
  136. }
  137. static u32 initiate_file_draining(struct nfs_client *clp,
  138. struct cb_layoutrecallargs *args)
  139. {
  140. struct inode *ino;
  141. struct pnfs_layout_hdr *lo;
  142. u32 rv = NFS4ERR_NOMATCHING_LAYOUT;
  143. LIST_HEAD(free_me_list);
  144. lo = get_layout_by_fh(clp, &args->cbl_fh);
  145. if (!lo)
  146. return NFS4ERR_NOMATCHING_LAYOUT;
  147. ino = lo->plh_inode;
  148. spin_lock(&ino->i_lock);
  149. if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
  150. pnfs_mark_matching_lsegs_invalid(lo, &free_me_list,
  151. &args->cbl_range))
  152. rv = NFS4ERR_DELAY;
  153. else
  154. rv = NFS4ERR_NOMATCHING_LAYOUT;
  155. pnfs_set_layout_stateid(lo, &args->cbl_stateid, true);
  156. spin_unlock(&ino->i_lock);
  157. pnfs_free_lseg_list(&free_me_list);
  158. pnfs_put_layout_hdr(lo);
  159. iput(ino);
  160. return rv;
  161. }
  162. static u32 initiate_bulk_draining(struct nfs_client *clp,
  163. struct cb_layoutrecallargs *args)
  164. {
  165. int stat;
  166. if (args->cbl_recall_type == RETURN_FSID)
  167. stat = pnfs_destroy_layouts_byfsid(clp, &args->cbl_fsid, true);
  168. else
  169. stat = pnfs_destroy_layouts_byclid(clp, true);
  170. if (stat != 0)
  171. return NFS4ERR_DELAY;
  172. return NFS4ERR_NOMATCHING_LAYOUT;
  173. }
  174. static u32 do_callback_layoutrecall(struct nfs_client *clp,
  175. struct cb_layoutrecallargs *args)
  176. {
  177. u32 res;
  178. dprintk("%s enter, type=%i\n", __func__, args->cbl_recall_type);
  179. if (args->cbl_recall_type == RETURN_FILE)
  180. res = initiate_file_draining(clp, args);
  181. else
  182. res = initiate_bulk_draining(clp, args);
  183. dprintk("%s returning %i\n", __func__, res);
  184. return res;
  185. }
  186. __be32 nfs4_callback_layoutrecall(struct cb_layoutrecallargs *args,
  187. void *dummy, struct cb_process_state *cps)
  188. {
  189. u32 res;
  190. dprintk("%s: -->\n", __func__);
  191. if (cps->clp)
  192. res = do_callback_layoutrecall(cps->clp, args);
  193. else
  194. res = NFS4ERR_OP_NOT_IN_SESSION;
  195. dprintk("%s: exit with status = %d\n", __func__, res);
  196. return cpu_to_be32(res);
  197. }
  198. static void pnfs_recall_all_layouts(struct nfs_client *clp)
  199. {
  200. struct cb_layoutrecallargs args;
  201. /* Pretend we got a CB_LAYOUTRECALL(ALL) */
  202. memset(&args, 0, sizeof(args));
  203. args.cbl_recall_type = RETURN_ALL;
  204. /* FIXME we ignore errors, what should we do? */
  205. do_callback_layoutrecall(clp, &args);
  206. }
  207. __be32 nfs4_callback_devicenotify(struct cb_devicenotifyargs *args,
  208. void *dummy, struct cb_process_state *cps)
  209. {
  210. int i;
  211. __be32 res = 0;
  212. struct nfs_client *clp = cps->clp;
  213. struct nfs_server *server = NULL;
  214. dprintk("%s: -->\n", __func__);
  215. if (!clp) {
  216. res = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
  217. goto out;
  218. }
  219. for (i = 0; i < args->ndevs; i++) {
  220. struct cb_devicenotifyitem *dev = &args->devs[i];
  221. if (!server ||
  222. server->pnfs_curr_ld->id != dev->cbd_layout_type) {
  223. rcu_read_lock();
  224. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  225. if (server->pnfs_curr_ld &&
  226. server->pnfs_curr_ld->id == dev->cbd_layout_type) {
  227. rcu_read_unlock();
  228. goto found;
  229. }
  230. rcu_read_unlock();
  231. dprintk("%s: layout type %u not found\n",
  232. __func__, dev->cbd_layout_type);
  233. continue;
  234. }
  235. found:
  236. if (dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE)
  237. dprintk("%s: NOTIFY_DEVICEID4_CHANGE not supported, "
  238. "deleting instead\n", __func__);
  239. nfs4_delete_deviceid(server->pnfs_curr_ld, clp, &dev->cbd_dev_id);
  240. }
  241. out:
  242. kfree(args->devs);
  243. dprintk("%s: exit with status = %u\n",
  244. __func__, be32_to_cpu(res));
  245. return res;
  246. }
  247. /*
  248. * Validate the sequenceID sent by the server.
  249. * Return success if the sequenceID is one more than what we last saw on
  250. * this slot, accounting for wraparound. Increments the slot's sequence.
  251. *
  252. * We don't yet implement a duplicate request cache, instead we set the
  253. * back channel ca_maxresponsesize_cached to zero. This is OK for now
  254. * since we only currently implement idempotent callbacks anyway.
  255. *
  256. * We have a single slot backchannel at this time, so we don't bother
  257. * checking the used_slots bit array on the table. The lower layer guarantees
  258. * a single outstanding callback request at a time.
  259. */
  260. static __be32
  261. validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
  262. {
  263. struct nfs4_slot *slot;
  264. dprintk("%s enter. slotid %d seqid %d\n",
  265. __func__, args->csa_slotid, args->csa_sequenceid);
  266. if (args->csa_slotid >= NFS41_BC_MAX_CALLBACKS)
  267. return htonl(NFS4ERR_BADSLOT);
  268. slot = tbl->slots + args->csa_slotid;
  269. dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
  270. /* Normal */
  271. if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
  272. slot->seq_nr++;
  273. goto out_ok;
  274. }
  275. /* Replay */
  276. if (args->csa_sequenceid == slot->seq_nr) {
  277. dprintk("%s seqid %d is a replay\n",
  278. __func__, args->csa_sequenceid);
  279. /* Signal process_op to set this error on next op */
  280. if (args->csa_cachethis == 0)
  281. return htonl(NFS4ERR_RETRY_UNCACHED_REP);
  282. /* The ca_maxresponsesize_cached is 0 with no DRC */
  283. else if (args->csa_cachethis == 1)
  284. return htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
  285. }
  286. /* Wraparound */
  287. if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
  288. slot->seq_nr = 1;
  289. goto out_ok;
  290. }
  291. /* Misordered request */
  292. return htonl(NFS4ERR_SEQ_MISORDERED);
  293. out_ok:
  294. tbl->highest_used_slotid = args->csa_slotid;
  295. return htonl(NFS4_OK);
  296. }
  297. /*
  298. * For each referring call triple, check the session's slot table for
  299. * a match. If the slot is in use and the sequence numbers match, the
  300. * client is still waiting for a response to the original request.
  301. */
  302. static bool referring_call_exists(struct nfs_client *clp,
  303. uint32_t nrclists,
  304. struct referring_call_list *rclists)
  305. {
  306. bool status = 0;
  307. int i, j;
  308. struct nfs4_session *session;
  309. struct nfs4_slot_table *tbl;
  310. struct referring_call_list *rclist;
  311. struct referring_call *ref;
  312. /*
  313. * XXX When client trunking is implemented, this becomes
  314. * a session lookup from within the loop
  315. */
  316. session = clp->cl_session;
  317. tbl = &session->fc_slot_table;
  318. for (i = 0; i < nrclists; i++) {
  319. rclist = &rclists[i];
  320. if (memcmp(session->sess_id.data,
  321. rclist->rcl_sessionid.data,
  322. NFS4_MAX_SESSIONID_LEN) != 0)
  323. continue;
  324. for (j = 0; j < rclist->rcl_nrefcalls; j++) {
  325. ref = &rclist->rcl_refcalls[j];
  326. dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u "
  327. "slotid %u\n", __func__,
  328. ((u32 *)&rclist->rcl_sessionid.data)[0],
  329. ((u32 *)&rclist->rcl_sessionid.data)[1],
  330. ((u32 *)&rclist->rcl_sessionid.data)[2],
  331. ((u32 *)&rclist->rcl_sessionid.data)[3],
  332. ref->rc_sequenceid, ref->rc_slotid);
  333. spin_lock(&tbl->slot_tbl_lock);
  334. status = (test_bit(ref->rc_slotid, tbl->used_slots) &&
  335. tbl->slots[ref->rc_slotid].seq_nr ==
  336. ref->rc_sequenceid);
  337. spin_unlock(&tbl->slot_tbl_lock);
  338. if (status)
  339. goto out;
  340. }
  341. }
  342. out:
  343. return status;
  344. }
  345. __be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
  346. struct cb_sequenceres *res,
  347. struct cb_process_state *cps)
  348. {
  349. struct nfs4_slot_table *tbl;
  350. struct nfs_client *clp;
  351. int i;
  352. __be32 status = htonl(NFS4ERR_BADSESSION);
  353. clp = nfs4_find_client_sessionid(cps->net, args->csa_addr,
  354. &args->csa_sessionid, cps->minorversion);
  355. if (clp == NULL)
  356. goto out;
  357. tbl = &clp->cl_session->bc_slot_table;
  358. spin_lock(&tbl->slot_tbl_lock);
  359. /* state manager is resetting the session */
  360. if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
  361. spin_unlock(&tbl->slot_tbl_lock);
  362. status = htonl(NFS4ERR_DELAY);
  363. /* Return NFS4ERR_BADSESSION if we're draining the session
  364. * in order to reset it.
  365. */
  366. if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  367. status = htonl(NFS4ERR_BADSESSION);
  368. goto out;
  369. }
  370. status = validate_seqid(&clp->cl_session->bc_slot_table, args);
  371. spin_unlock(&tbl->slot_tbl_lock);
  372. if (status)
  373. goto out;
  374. cps->slotid = args->csa_slotid;
  375. /*
  376. * Check for pending referring calls. If a match is found, a
  377. * related callback was received before the response to the original
  378. * call.
  379. */
  380. if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
  381. status = htonl(NFS4ERR_DELAY);
  382. goto out;
  383. }
  384. memcpy(&res->csr_sessionid, &args->csa_sessionid,
  385. sizeof(res->csr_sessionid));
  386. res->csr_sequenceid = args->csa_sequenceid;
  387. res->csr_slotid = args->csa_slotid;
  388. res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  389. res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  390. out:
  391. cps->clp = clp; /* put in nfs4_callback_compound */
  392. for (i = 0; i < args->csa_nrclists; i++)
  393. kfree(args->csa_rclists[i].rcl_refcalls);
  394. kfree(args->csa_rclists);
  395. if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
  396. cps->drc_status = status;
  397. status = 0;
  398. } else
  399. res->csr_status = status;
  400. dprintk("%s: exit with status = %d res->csr_status %d\n", __func__,
  401. ntohl(status), ntohl(res->csr_status));
  402. return status;
  403. }
  404. static bool
  405. validate_bitmap_values(unsigned long mask)
  406. {
  407. return (mask & ~RCA4_TYPE_MASK_ALL) == 0;
  408. }
  409. __be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy,
  410. struct cb_process_state *cps)
  411. {
  412. __be32 status;
  413. fmode_t flags = 0;
  414. status = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
  415. if (!cps->clp) /* set in cb_sequence */
  416. goto out;
  417. dprintk_rcu("NFS: RECALL_ANY callback request from %s\n",
  418. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  419. status = cpu_to_be32(NFS4ERR_INVAL);
  420. if (!validate_bitmap_values(args->craa_type_mask))
  421. goto out;
  422. status = cpu_to_be32(NFS4_OK);
  423. if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
  424. &args->craa_type_mask))
  425. flags = FMODE_READ;
  426. if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
  427. &args->craa_type_mask))
  428. flags |= FMODE_WRITE;
  429. if (test_bit(RCA4_TYPE_MASK_FILE_LAYOUT, (const unsigned long *)
  430. &args->craa_type_mask))
  431. pnfs_recall_all_layouts(cps->clp);
  432. if (flags)
  433. nfs_expire_unused_delegation_types(cps->clp, flags);
  434. out:
  435. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  436. return status;
  437. }
  438. /* Reduce the fore channel's max_slots to the target value */
  439. __be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy,
  440. struct cb_process_state *cps)
  441. {
  442. struct nfs4_slot_table *fc_tbl;
  443. __be32 status;
  444. status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
  445. if (!cps->clp) /* set in cb_sequence */
  446. goto out;
  447. dprintk_rcu("NFS: CB_RECALL_SLOT request from %s target highest slotid %d\n",
  448. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR),
  449. args->crsa_target_highest_slotid);
  450. fc_tbl = &cps->clp->cl_session->fc_slot_table;
  451. status = htonl(NFS4_OK);
  452. nfs41_set_target_slotid(fc_tbl, args->crsa_target_highest_slotid);
  453. nfs41_server_notify_target_slotid_update(cps->clp);
  454. out:
  455. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  456. return status;
  457. }
  458. #endif /* CONFIG_NFS_V4_1 */