callback_proc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. struct nfs_server *server;
  166. struct pnfs_layout_hdr *lo;
  167. struct inode *ino;
  168. u32 rv = NFS4ERR_NOMATCHING_LAYOUT;
  169. struct pnfs_layout_hdr *tmp;
  170. LIST_HEAD(recall_list);
  171. LIST_HEAD(free_me_list);
  172. struct pnfs_layout_range range = {
  173. .iomode = IOMODE_ANY,
  174. .offset = 0,
  175. .length = NFS4_MAX_UINT64,
  176. };
  177. spin_lock(&clp->cl_lock);
  178. rcu_read_lock();
  179. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  180. if ((args->cbl_recall_type == RETURN_FSID) &&
  181. memcmp(&server->fsid, &args->cbl_fsid,
  182. sizeof(struct nfs_fsid)))
  183. continue;
  184. list_for_each_entry(lo, &server->layouts, plh_layouts) {
  185. ino = igrab(lo->plh_inode);
  186. if (!ino)
  187. continue;
  188. spin_lock(&ino->i_lock);
  189. /* Is this layout in the process of being freed? */
  190. if (NFS_I(ino)->layout != lo) {
  191. spin_unlock(&ino->i_lock);
  192. iput(ino);
  193. continue;
  194. }
  195. pnfs_get_layout_hdr(lo);
  196. spin_unlock(&ino->i_lock);
  197. list_add(&lo->plh_bulk_recall, &recall_list);
  198. }
  199. }
  200. rcu_read_unlock();
  201. spin_unlock(&clp->cl_lock);
  202. list_for_each_entry_safe(lo, tmp,
  203. &recall_list, plh_bulk_recall) {
  204. ino = lo->plh_inode;
  205. spin_lock(&ino->i_lock);
  206. set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
  207. if (pnfs_mark_matching_lsegs_invalid(lo, &free_me_list, &range))
  208. rv = NFS4ERR_DELAY;
  209. list_del_init(&lo->plh_bulk_recall);
  210. spin_unlock(&ino->i_lock);
  211. pnfs_free_lseg_list(&free_me_list);
  212. pnfs_put_layout_hdr(lo);
  213. iput(ino);
  214. }
  215. return rv;
  216. }
  217. static u32 do_callback_layoutrecall(struct nfs_client *clp,
  218. struct cb_layoutrecallargs *args)
  219. {
  220. u32 res;
  221. dprintk("%s enter, type=%i\n", __func__, args->cbl_recall_type);
  222. if (args->cbl_recall_type == RETURN_FILE)
  223. res = initiate_file_draining(clp, args);
  224. else
  225. res = initiate_bulk_draining(clp, args);
  226. dprintk("%s returning %i\n", __func__, res);
  227. return res;
  228. }
  229. __be32 nfs4_callback_layoutrecall(struct cb_layoutrecallargs *args,
  230. void *dummy, struct cb_process_state *cps)
  231. {
  232. u32 res;
  233. dprintk("%s: -->\n", __func__);
  234. if (cps->clp)
  235. res = do_callback_layoutrecall(cps->clp, args);
  236. else
  237. res = NFS4ERR_OP_NOT_IN_SESSION;
  238. dprintk("%s: exit with status = %d\n", __func__, res);
  239. return cpu_to_be32(res);
  240. }
  241. static void pnfs_recall_all_layouts(struct nfs_client *clp)
  242. {
  243. struct cb_layoutrecallargs args;
  244. /* Pretend we got a CB_LAYOUTRECALL(ALL) */
  245. memset(&args, 0, sizeof(args));
  246. args.cbl_recall_type = RETURN_ALL;
  247. /* FIXME we ignore errors, what should we do? */
  248. do_callback_layoutrecall(clp, &args);
  249. }
  250. __be32 nfs4_callback_devicenotify(struct cb_devicenotifyargs *args,
  251. void *dummy, struct cb_process_state *cps)
  252. {
  253. int i;
  254. __be32 res = 0;
  255. struct nfs_client *clp = cps->clp;
  256. struct nfs_server *server = NULL;
  257. dprintk("%s: -->\n", __func__);
  258. if (!clp) {
  259. res = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
  260. goto out;
  261. }
  262. for (i = 0; i < args->ndevs; i++) {
  263. struct cb_devicenotifyitem *dev = &args->devs[i];
  264. if (!server ||
  265. server->pnfs_curr_ld->id != dev->cbd_layout_type) {
  266. rcu_read_lock();
  267. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  268. if (server->pnfs_curr_ld &&
  269. server->pnfs_curr_ld->id == dev->cbd_layout_type) {
  270. rcu_read_unlock();
  271. goto found;
  272. }
  273. rcu_read_unlock();
  274. dprintk("%s: layout type %u not found\n",
  275. __func__, dev->cbd_layout_type);
  276. continue;
  277. }
  278. found:
  279. if (dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE)
  280. dprintk("%s: NOTIFY_DEVICEID4_CHANGE not supported, "
  281. "deleting instead\n", __func__);
  282. nfs4_delete_deviceid(server->pnfs_curr_ld, clp, &dev->cbd_dev_id);
  283. }
  284. out:
  285. kfree(args->devs);
  286. dprintk("%s: exit with status = %u\n",
  287. __func__, be32_to_cpu(res));
  288. return res;
  289. }
  290. /*
  291. * Validate the sequenceID sent by the server.
  292. * Return success if the sequenceID is one more than what we last saw on
  293. * this slot, accounting for wraparound. Increments the slot's sequence.
  294. *
  295. * We don't yet implement a duplicate request cache, instead we set the
  296. * back channel ca_maxresponsesize_cached to zero. This is OK for now
  297. * since we only currently implement idempotent callbacks anyway.
  298. *
  299. * We have a single slot backchannel at this time, so we don't bother
  300. * checking the used_slots bit array on the table. The lower layer guarantees
  301. * a single outstanding callback request at a time.
  302. */
  303. static __be32
  304. validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
  305. {
  306. struct nfs4_slot *slot;
  307. dprintk("%s enter. slotid %d seqid %d\n",
  308. __func__, args->csa_slotid, args->csa_sequenceid);
  309. if (args->csa_slotid >= NFS41_BC_MAX_CALLBACKS)
  310. return htonl(NFS4ERR_BADSLOT);
  311. slot = tbl->slots + args->csa_slotid;
  312. dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
  313. /* Normal */
  314. if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
  315. slot->seq_nr++;
  316. goto out_ok;
  317. }
  318. /* Replay */
  319. if (args->csa_sequenceid == slot->seq_nr) {
  320. dprintk("%s seqid %d is a replay\n",
  321. __func__, args->csa_sequenceid);
  322. /* Signal process_op to set this error on next op */
  323. if (args->csa_cachethis == 0)
  324. return htonl(NFS4ERR_RETRY_UNCACHED_REP);
  325. /* The ca_maxresponsesize_cached is 0 with no DRC */
  326. else if (args->csa_cachethis == 1)
  327. return htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
  328. }
  329. /* Wraparound */
  330. if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
  331. slot->seq_nr = 1;
  332. goto out_ok;
  333. }
  334. /* Misordered request */
  335. return htonl(NFS4ERR_SEQ_MISORDERED);
  336. out_ok:
  337. tbl->highest_used_slotid = args->csa_slotid;
  338. return htonl(NFS4_OK);
  339. }
  340. /*
  341. * For each referring call triple, check the session's slot table for
  342. * a match. If the slot is in use and the sequence numbers match, the
  343. * client is still waiting for a response to the original request.
  344. */
  345. static bool referring_call_exists(struct nfs_client *clp,
  346. uint32_t nrclists,
  347. struct referring_call_list *rclists)
  348. {
  349. bool status = 0;
  350. int i, j;
  351. struct nfs4_session *session;
  352. struct nfs4_slot_table *tbl;
  353. struct referring_call_list *rclist;
  354. struct referring_call *ref;
  355. /*
  356. * XXX When client trunking is implemented, this becomes
  357. * a session lookup from within the loop
  358. */
  359. session = clp->cl_session;
  360. tbl = &session->fc_slot_table;
  361. for (i = 0; i < nrclists; i++) {
  362. rclist = &rclists[i];
  363. if (memcmp(session->sess_id.data,
  364. rclist->rcl_sessionid.data,
  365. NFS4_MAX_SESSIONID_LEN) != 0)
  366. continue;
  367. for (j = 0; j < rclist->rcl_nrefcalls; j++) {
  368. ref = &rclist->rcl_refcalls[j];
  369. dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u "
  370. "slotid %u\n", __func__,
  371. ((u32 *)&rclist->rcl_sessionid.data)[0],
  372. ((u32 *)&rclist->rcl_sessionid.data)[1],
  373. ((u32 *)&rclist->rcl_sessionid.data)[2],
  374. ((u32 *)&rclist->rcl_sessionid.data)[3],
  375. ref->rc_sequenceid, ref->rc_slotid);
  376. spin_lock(&tbl->slot_tbl_lock);
  377. status = (test_bit(ref->rc_slotid, tbl->used_slots) &&
  378. tbl->slots[ref->rc_slotid].seq_nr ==
  379. ref->rc_sequenceid);
  380. spin_unlock(&tbl->slot_tbl_lock);
  381. if (status)
  382. goto out;
  383. }
  384. }
  385. out:
  386. return status;
  387. }
  388. __be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
  389. struct cb_sequenceres *res,
  390. struct cb_process_state *cps)
  391. {
  392. struct nfs4_slot_table *tbl;
  393. struct nfs_client *clp;
  394. int i;
  395. __be32 status = htonl(NFS4ERR_BADSESSION);
  396. clp = nfs4_find_client_sessionid(cps->net, args->csa_addr, &args->csa_sessionid);
  397. if (clp == NULL)
  398. goto out;
  399. tbl = &clp->cl_session->bc_slot_table;
  400. spin_lock(&tbl->slot_tbl_lock);
  401. /* state manager is resetting the session */
  402. if (test_bit(NFS4_SESSION_DRAINING, &clp->cl_session->session_state)) {
  403. spin_unlock(&tbl->slot_tbl_lock);
  404. status = htonl(NFS4ERR_DELAY);
  405. /* Return NFS4ERR_BADSESSION if we're draining the session
  406. * in order to reset it.
  407. */
  408. if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  409. status = htonl(NFS4ERR_BADSESSION);
  410. goto out;
  411. }
  412. status = validate_seqid(&clp->cl_session->bc_slot_table, args);
  413. spin_unlock(&tbl->slot_tbl_lock);
  414. if (status)
  415. goto out;
  416. cps->slotid = args->csa_slotid;
  417. /*
  418. * Check for pending referring calls. If a match is found, a
  419. * related callback was received before the response to the original
  420. * call.
  421. */
  422. if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
  423. status = htonl(NFS4ERR_DELAY);
  424. goto out;
  425. }
  426. memcpy(&res->csr_sessionid, &args->csa_sessionid,
  427. sizeof(res->csr_sessionid));
  428. res->csr_sequenceid = args->csa_sequenceid;
  429. res->csr_slotid = args->csa_slotid;
  430. res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  431. res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
  432. out:
  433. cps->clp = clp; /* put in nfs4_callback_compound */
  434. for (i = 0; i < args->csa_nrclists; i++)
  435. kfree(args->csa_rclists[i].rcl_refcalls);
  436. kfree(args->csa_rclists);
  437. if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
  438. cps->drc_status = status;
  439. status = 0;
  440. } else
  441. res->csr_status = status;
  442. dprintk("%s: exit with status = %d res->csr_status %d\n", __func__,
  443. ntohl(status), ntohl(res->csr_status));
  444. return status;
  445. }
  446. static bool
  447. validate_bitmap_values(unsigned long mask)
  448. {
  449. return (mask & ~RCA4_TYPE_MASK_ALL) == 0;
  450. }
  451. __be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy,
  452. struct cb_process_state *cps)
  453. {
  454. __be32 status;
  455. fmode_t flags = 0;
  456. status = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
  457. if (!cps->clp) /* set in cb_sequence */
  458. goto out;
  459. dprintk_rcu("NFS: RECALL_ANY callback request from %s\n",
  460. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  461. status = cpu_to_be32(NFS4ERR_INVAL);
  462. if (!validate_bitmap_values(args->craa_type_mask))
  463. goto out;
  464. status = cpu_to_be32(NFS4_OK);
  465. if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
  466. &args->craa_type_mask))
  467. flags = FMODE_READ;
  468. if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
  469. &args->craa_type_mask))
  470. flags |= FMODE_WRITE;
  471. if (test_bit(RCA4_TYPE_MASK_FILE_LAYOUT, (const unsigned long *)
  472. &args->craa_type_mask))
  473. pnfs_recall_all_layouts(cps->clp);
  474. if (flags)
  475. nfs_expire_all_delegation_types(cps->clp, flags);
  476. out:
  477. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  478. return status;
  479. }
  480. /* Reduce the fore channel's max_slots to the target value */
  481. __be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy,
  482. struct cb_process_state *cps)
  483. {
  484. struct nfs4_slot_table *fc_tbl;
  485. __be32 status;
  486. status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
  487. if (!cps->clp) /* set in cb_sequence */
  488. goto out;
  489. dprintk_rcu("NFS: CB_RECALL_SLOT request from %s target highest slotid %d\n",
  490. rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR),
  491. args->crsa_target_highest_slotid);
  492. fc_tbl = &cps->clp->cl_session->fc_slot_table;
  493. status = htonl(NFS4_OK);
  494. nfs41_set_target_slotid(fc_tbl, args->crsa_target_highest_slotid);
  495. nfs41_server_notify_target_slotid_update(cps->clp);
  496. out:
  497. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  498. return status;
  499. }
  500. #endif /* CONFIG_NFS_V4_1 */