nfs40proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/nfs4.h>
  3. #include <linux/nfs.h>
  4. #include <linux/sunrpc/sched.h>
  5. #include <linux/nfs_fs.h>
  6. #include "internal.h"
  7. #include "nfs4_fs.h"
  8. #include "nfs40.h"
  9. #include "nfs4session.h"
  10. #include "nfs4trace.h"
  11. static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
  12. {
  13. struct nfs4_call_sync_data *data = calldata;
  14. nfs4_setup_sequence(data->seq_server->nfs_client,
  15. data->seq_args, data->seq_res, task);
  16. }
  17. static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
  18. {
  19. struct nfs4_call_sync_data *data = calldata;
  20. nfs4_sequence_done(task, data->seq_res);
  21. }
  22. static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
  23. {
  24. struct nfs4_slot *slot = res->sr_slot;
  25. struct nfs4_slot_table *tbl;
  26. tbl = slot->table;
  27. spin_lock(&tbl->slot_tbl_lock);
  28. if (!nfs41_wake_and_assign_slot(tbl, slot))
  29. nfs4_free_slot(tbl, slot);
  30. spin_unlock(&tbl->slot_tbl_lock);
  31. res->sr_slot = NULL;
  32. }
  33. static int nfs40_sequence_done(struct rpc_task *task,
  34. struct nfs4_sequence_res *res)
  35. {
  36. if (res->sr_slot != NULL)
  37. nfs40_sequence_free_slot(res);
  38. return 1;
  39. }
  40. static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
  41. {
  42. if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
  43. nfs_finish_clear_delegation_stateid(state, NULL);
  44. }
  45. static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  46. {
  47. /* NFSv4.0 doesn't allow for delegation recovery on open expire */
  48. nfs40_clear_delegation_stateid(state);
  49. nfs_state_clear_open_state_flags(state);
  50. return nfs4_open_expired(sp, state);
  51. }
  52. struct nfs4_renewdata {
  53. struct nfs_client *client;
  54. unsigned long timestamp;
  55. };
  56. /*
  57. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  58. * standalone procedure for queueing an asynchronous RENEW.
  59. */
  60. static void nfs4_renew_release(void *calldata)
  61. {
  62. struct nfs4_renewdata *data = calldata;
  63. struct nfs_client *clp = data->client;
  64. if (refcount_read(&clp->cl_count) > 1)
  65. nfs4_schedule_state_renewal(clp);
  66. nfs_put_client(clp);
  67. kfree(data);
  68. }
  69. static void nfs4_renew_done(struct rpc_task *task, void *calldata)
  70. {
  71. struct nfs4_renewdata *data = calldata;
  72. struct nfs_client *clp = data->client;
  73. unsigned long timestamp = data->timestamp;
  74. trace_nfs4_renew_async(clp, task->tk_status);
  75. switch (task->tk_status) {
  76. case 0:
  77. break;
  78. case -NFS4ERR_LEASE_MOVED:
  79. nfs4_schedule_lease_moved_recovery(clp);
  80. break;
  81. default:
  82. /* Unless we're shutting down, schedule state recovery! */
  83. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
  84. return;
  85. if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
  86. nfs4_schedule_lease_recovery(clp);
  87. return;
  88. }
  89. nfs4_schedule_path_down_recovery(clp);
  90. }
  91. do_renew_lease(clp, timestamp);
  92. }
  93. static const struct rpc_call_ops nfs4_renew_ops = {
  94. .rpc_call_done = nfs4_renew_done,
  95. .rpc_release = nfs4_renew_release,
  96. };
  97. static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
  98. {
  99. struct rpc_message msg = {
  100. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  101. .rpc_argp = clp,
  102. .rpc_cred = cred,
  103. };
  104. struct nfs4_renewdata *data;
  105. if (renew_flags == 0)
  106. return 0;
  107. if (!refcount_inc_not_zero(&clp->cl_count))
  108. return -EIO;
  109. data = kmalloc_obj(*data, GFP_NOFS);
  110. if (data == NULL) {
  111. nfs_put_client(clp);
  112. return -ENOMEM;
  113. }
  114. data->client = clp;
  115. data->timestamp = jiffies;
  116. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
  117. &nfs4_renew_ops, data);
  118. }
  119. static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
  120. {
  121. struct rpc_message msg = {
  122. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  123. .rpc_argp = clp,
  124. .rpc_cred = cred,
  125. };
  126. unsigned long now = jiffies;
  127. int status;
  128. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  129. if (status < 0)
  130. return status;
  131. do_renew_lease(clp, now);
  132. return 0;
  133. }
  134. static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
  135. nfs4_stateid *stateid,
  136. const struct cred *cred)
  137. {
  138. return -NFS4ERR_BAD_STATEID;
  139. }
  140. /*
  141. * This operation also signals the server that this client is
  142. * performing migration recovery. The server can stop returning
  143. * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
  144. * appended to this compound to identify the client ID which is
  145. * performing recovery.
  146. */
  147. static int _nfs40_proc_get_locations(struct nfs_server *server,
  148. struct nfs_fh *fhandle,
  149. struct nfs4_fs_locations *locations,
  150. struct page *page, const struct cred *cred)
  151. {
  152. struct rpc_clnt *clnt = server->client;
  153. struct nfs_client *clp = server->nfs_client;
  154. u32 bitmask[2] = {
  155. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  156. };
  157. struct nfs4_fs_locations_arg args = {
  158. .clientid = clp->cl_clientid,
  159. .fh = fhandle,
  160. .page = page,
  161. .bitmask = bitmask,
  162. .migration = 1, /* skip LOOKUP */
  163. .renew = 1, /* append RENEW */
  164. };
  165. struct nfs4_fs_locations_res res = {
  166. .fs_locations = locations,
  167. .migration = 1,
  168. .renew = 1,
  169. };
  170. struct rpc_message msg = {
  171. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  172. .rpc_argp = &args,
  173. .rpc_resp = &res,
  174. .rpc_cred = cred,
  175. };
  176. unsigned long now = jiffies;
  177. int status;
  178. nfs_fattr_init(locations->fattr);
  179. locations->server = server;
  180. locations->nlocations = 0;
  181. nfs4_init_sequence(clp, &args.seq_args, &res.seq_res, 0, 1);
  182. status = nfs4_call_sync_sequence(clnt, server, &msg,
  183. &args.seq_args, &res.seq_res);
  184. if (status)
  185. return status;
  186. renew_lease(server, now);
  187. return 0;
  188. }
  189. /*
  190. * This operation also signals the server that this client is
  191. * performing "lease moved" recovery. The server can stop
  192. * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
  193. * is appended to this compound to identify the client ID which is
  194. * performing recovery.
  195. */
  196. static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
  197. {
  198. struct nfs_server *server = NFS_SERVER(inode);
  199. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  200. struct rpc_clnt *clnt = server->client;
  201. struct nfs4_fsid_present_arg args = {
  202. .fh = NFS_FH(inode),
  203. .clientid = clp->cl_clientid,
  204. .renew = 1, /* append RENEW */
  205. };
  206. struct nfs4_fsid_present_res res = {
  207. .renew = 1,
  208. };
  209. struct rpc_message msg = {
  210. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
  211. .rpc_argp = &args,
  212. .rpc_resp = &res,
  213. .rpc_cred = cred,
  214. };
  215. unsigned long now = jiffies;
  216. int status;
  217. res.fh = nfs_alloc_fhandle();
  218. if (res.fh == NULL)
  219. return -ENOMEM;
  220. nfs4_init_sequence(clp, &args.seq_args, &res.seq_res, 0, 1);
  221. status = nfs4_call_sync_sequence(clnt, server, &msg,
  222. &args.seq_args, &res.seq_res);
  223. nfs_free_fhandle(res.fh);
  224. if (status)
  225. return status;
  226. do_renew_lease(clp, now);
  227. return 0;
  228. }
  229. struct nfs_release_lockowner_data {
  230. struct nfs4_lock_state *lsp;
  231. struct nfs_server *server;
  232. struct nfs_release_lockowner_args args;
  233. struct nfs_release_lockowner_res res;
  234. unsigned long timestamp;
  235. };
  236. static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
  237. {
  238. struct nfs_release_lockowner_data *data = calldata;
  239. struct nfs_server *server = data->server;
  240. nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
  241. &data->res.seq_res, task);
  242. data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
  243. data->timestamp = jiffies;
  244. }
  245. static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
  246. {
  247. struct nfs_release_lockowner_data *data = calldata;
  248. struct nfs_server *server = data->server;
  249. nfs40_sequence_done(task, &data->res.seq_res);
  250. switch (task->tk_status) {
  251. case 0:
  252. renew_lease(server, data->timestamp);
  253. break;
  254. case -NFS4ERR_STALE_CLIENTID:
  255. case -NFS4ERR_EXPIRED:
  256. nfs4_schedule_lease_recovery(server->nfs_client);
  257. break;
  258. case -NFS4ERR_LEASE_MOVED:
  259. case -NFS4ERR_DELAY:
  260. if (nfs4_async_handle_error(task, server,
  261. NULL, NULL) == -EAGAIN)
  262. rpc_restart_call_prepare(task);
  263. }
  264. }
  265. static void nfs4_release_lockowner_release(void *calldata)
  266. {
  267. struct nfs_release_lockowner_data *data = calldata;
  268. nfs4_free_lock_state(data->server, data->lsp);
  269. kfree(calldata);
  270. }
  271. static const struct rpc_call_ops nfs4_release_lockowner_ops = {
  272. .rpc_call_prepare = nfs4_release_lockowner_prepare,
  273. .rpc_call_done = nfs4_release_lockowner_done,
  274. .rpc_release = nfs4_release_lockowner_release,
  275. };
  276. static void
  277. nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
  278. {
  279. struct nfs_release_lockowner_data *data;
  280. struct nfs_client *clp = server->nfs_client;
  281. struct rpc_message msg = {
  282. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
  283. };
  284. if (clp->cl_mvops->minor_version != 0)
  285. return;
  286. data = kmalloc_obj(*data);
  287. if (!data)
  288. return;
  289. data->lsp = lsp;
  290. data->server = server;
  291. data->args.lock_owner.clientid = clp->cl_clientid;
  292. data->args.lock_owner.id = lsp->ls_seqid.owner_id;
  293. data->args.lock_owner.s_dev = server->s_dev;
  294. msg.rpc_argp = &data->args;
  295. msg.rpc_resp = &data->res;
  296. nfs4_init_sequence(clp, &data->args.seq_args, &data->res.seq_res, 0, 0);
  297. rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
  298. }
  299. static const struct rpc_call_ops nfs40_call_sync_ops = {
  300. .rpc_call_prepare = nfs40_call_sync_prepare,
  301. .rpc_call_done = nfs40_call_sync_done,
  302. };
  303. static const struct nfs4_sequence_slot_ops nfs40_sequence_slot_ops = {
  304. .process = nfs40_sequence_done,
  305. .done = nfs40_sequence_done,
  306. .free_slot = nfs40_sequence_free_slot,
  307. };
  308. static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
  309. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  310. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  311. .recover_open = nfs4_open_reclaim,
  312. .recover_lock = nfs4_lock_reclaim,
  313. .establish_clid = nfs4_init_clientid,
  314. .detect_trunking = nfs40_discover_server_trunking,
  315. };
  316. static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
  317. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  318. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  319. .recover_open = nfs40_open_expired,
  320. .recover_lock = nfs4_lock_expired,
  321. .establish_clid = nfs4_init_clientid,
  322. };
  323. static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
  324. .sched_state_renewal = nfs4_proc_async_renew,
  325. .get_state_renewal_cred = nfs4_get_renew_cred,
  326. .renew_lease = nfs4_proc_renew,
  327. };
  328. static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
  329. .get_locations = _nfs40_proc_get_locations,
  330. .fsid_present = _nfs40_proc_fsid_present,
  331. };
  332. const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
  333. .minor_version = 0,
  334. .init_caps = NFS_CAP_READDIRPLUS
  335. | NFS_CAP_ATOMIC_OPEN
  336. | NFS_CAP_POSIX_LOCK,
  337. .init_client = nfs40_init_client,
  338. .shutdown_client = nfs40_shutdown_client,
  339. .match_stateid = nfs4_match_stateid,
  340. .find_root_sec = nfs4_find_root_sec,
  341. .free_lock_state = nfs4_release_lockowner,
  342. .test_and_free_expired = nfs40_test_and_free_expired_stateid,
  343. .alloc_seqid = nfs_alloc_seqid,
  344. .call_sync_ops = &nfs40_call_sync_ops,
  345. .sequence_slot_ops = &nfs40_sequence_slot_ops,
  346. .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
  347. .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
  348. .state_renewal_ops = &nfs40_state_renewal_ops,
  349. .mig_recovery_ops = &nfs40_mig_recovery_ops,
  350. };