callback_proc.c 14 KB

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