callback_proc.c 15 KB

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