callback_proc.c 15 KB

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