fs_operation.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Fileserver-directed operation handling.
  3. *
  4. * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/slab.h>
  9. #include <linux/fs.h>
  10. #include "internal.h"
  11. static atomic_t afs_operation_debug_counter;
  12. /*
  13. * Create an operation against a volume.
  14. */
  15. struct afs_operation *afs_alloc_operation(struct key *key, struct afs_volume *volume)
  16. {
  17. struct afs_operation *op;
  18. _enter("");
  19. op = kzalloc(sizeof(*op), GFP_KERNEL);
  20. if (!op)
  21. return ERR_PTR(-ENOMEM);
  22. if (!key) {
  23. key = afs_request_key(volume->cell);
  24. if (IS_ERR(key)) {
  25. kfree(op);
  26. return ERR_CAST(key);
  27. }
  28. } else {
  29. key_get(key);
  30. }
  31. op->key = key;
  32. op->volume = afs_get_volume(volume, afs_volume_trace_get_new_op);
  33. op->net = volume->cell->net;
  34. op->cb_v_break = volume->cb_v_break;
  35. op->debug_id = atomic_inc_return(&afs_operation_debug_counter);
  36. op->error = -EDESTADDRREQ;
  37. op->ac.error = SHRT_MAX;
  38. _leave(" = [op=%08x]", op->debug_id);
  39. return op;
  40. }
  41. /*
  42. * Lock the vnode(s) being operated upon.
  43. */
  44. static bool afs_get_io_locks(struct afs_operation *op)
  45. {
  46. struct afs_vnode *vnode = op->file[0].vnode;
  47. struct afs_vnode *vnode2 = op->file[1].vnode;
  48. _enter("");
  49. if (op->flags & AFS_OPERATION_UNINTR) {
  50. mutex_lock(&vnode->io_lock);
  51. op->flags |= AFS_OPERATION_LOCK_0;
  52. _leave(" = t [1]");
  53. return true;
  54. }
  55. if (!vnode2 || !op->file[1].need_io_lock || vnode == vnode2)
  56. vnode2 = NULL;
  57. if (vnode2 > vnode)
  58. swap(vnode, vnode2);
  59. if (mutex_lock_interruptible(&vnode->io_lock) < 0) {
  60. op->error = -ERESTARTSYS;
  61. op->flags |= AFS_OPERATION_STOP;
  62. _leave(" = f [I 0]");
  63. return false;
  64. }
  65. op->flags |= AFS_OPERATION_LOCK_0;
  66. if (vnode2) {
  67. if (mutex_lock_interruptible_nested(&vnode2->io_lock, 1) < 0) {
  68. op->error = -ERESTARTSYS;
  69. op->flags |= AFS_OPERATION_STOP;
  70. mutex_unlock(&vnode->io_lock);
  71. op->flags &= ~AFS_OPERATION_LOCK_0;
  72. _leave(" = f [I 1]");
  73. return false;
  74. }
  75. op->flags |= AFS_OPERATION_LOCK_1;
  76. }
  77. _leave(" = t [2]");
  78. return true;
  79. }
  80. static void afs_drop_io_locks(struct afs_operation *op)
  81. {
  82. struct afs_vnode *vnode = op->file[0].vnode;
  83. struct afs_vnode *vnode2 = op->file[1].vnode;
  84. _enter("");
  85. if (op->flags & AFS_OPERATION_LOCK_1)
  86. mutex_unlock(&vnode2->io_lock);
  87. if (op->flags & AFS_OPERATION_LOCK_0)
  88. mutex_unlock(&vnode->io_lock);
  89. }
  90. static void afs_prepare_vnode(struct afs_operation *op, struct afs_vnode_param *vp,
  91. unsigned int index)
  92. {
  93. struct afs_vnode *vnode = vp->vnode;
  94. if (vnode) {
  95. vp->fid = vnode->fid;
  96. vp->dv_before = vnode->status.data_version;
  97. vp->cb_break_before = afs_calc_vnode_cb_break(vnode);
  98. if (vnode->lock_state != AFS_VNODE_LOCK_NONE)
  99. op->flags |= AFS_OPERATION_CUR_ONLY;
  100. if (vp->modification)
  101. set_bit(AFS_VNODE_MODIFYING, &vnode->flags);
  102. }
  103. if (vp->fid.vnode)
  104. _debug("PREP[%u] {%llx:%llu.%u}",
  105. index, vp->fid.vid, vp->fid.vnode, vp->fid.unique);
  106. }
  107. /*
  108. * Begin an operation on the fileserver.
  109. *
  110. * Fileserver operations are serialised on the server by vnode, so we serialise
  111. * them here also using the io_lock.
  112. */
  113. bool afs_begin_vnode_operation(struct afs_operation *op)
  114. {
  115. struct afs_vnode *vnode = op->file[0].vnode;
  116. ASSERT(vnode);
  117. _enter("");
  118. if (op->file[0].need_io_lock)
  119. if (!afs_get_io_locks(op))
  120. return false;
  121. afs_prepare_vnode(op, &op->file[0], 0);
  122. afs_prepare_vnode(op, &op->file[1], 1);
  123. op->cb_v_break = op->volume->cb_v_break;
  124. _leave(" = true");
  125. return true;
  126. }
  127. /*
  128. * Tidy up a filesystem cursor and unlock the vnode.
  129. */
  130. static void afs_end_vnode_operation(struct afs_operation *op)
  131. {
  132. _enter("");
  133. if (op->error == -EDESTADDRREQ ||
  134. op->error == -EADDRNOTAVAIL ||
  135. op->error == -ENETUNREACH ||
  136. op->error == -EHOSTUNREACH)
  137. afs_dump_edestaddrreq(op);
  138. afs_drop_io_locks(op);
  139. if (op->error == -ECONNABORTED)
  140. op->error = afs_abort_to_error(op->ac.abort_code);
  141. }
  142. /*
  143. * Wait for an in-progress operation to complete.
  144. */
  145. void afs_wait_for_operation(struct afs_operation *op)
  146. {
  147. _enter("");
  148. while (afs_select_fileserver(op)) {
  149. op->cb_s_break = op->server->cb_s_break;
  150. if (test_bit(AFS_SERVER_FL_IS_YFS, &op->server->flags) &&
  151. op->ops->issue_yfs_rpc)
  152. op->ops->issue_yfs_rpc(op);
  153. else if (op->ops->issue_afs_rpc)
  154. op->ops->issue_afs_rpc(op);
  155. else
  156. op->ac.error = -ENOTSUPP;
  157. if (op->call)
  158. op->error = afs_wait_for_call_to_complete(op->call, &op->ac);
  159. }
  160. switch (op->error) {
  161. case 0:
  162. _debug("success");
  163. op->ops->success(op);
  164. break;
  165. case -ECONNABORTED:
  166. if (op->ops->aborted)
  167. op->ops->aborted(op);
  168. fallthrough;
  169. default:
  170. if (op->ops->failed)
  171. op->ops->failed(op);
  172. break;
  173. }
  174. afs_end_vnode_operation(op);
  175. if (op->error == 0 && op->ops->edit_dir) {
  176. _debug("edit_dir");
  177. op->ops->edit_dir(op);
  178. }
  179. _leave("");
  180. }
  181. /*
  182. * Dispose of an operation.
  183. */
  184. int afs_put_operation(struct afs_operation *op)
  185. {
  186. int i, ret = op->error;
  187. _enter("op=%08x,%d", op->debug_id, ret);
  188. if (op->ops && op->ops->put)
  189. op->ops->put(op);
  190. if (op->file[0].modification)
  191. clear_bit(AFS_VNODE_MODIFYING, &op->file[0].vnode->flags);
  192. if (op->file[1].modification && op->file[1].vnode != op->file[0].vnode)
  193. clear_bit(AFS_VNODE_MODIFYING, &op->file[1].vnode->flags);
  194. if (op->file[0].put_vnode)
  195. iput(&op->file[0].vnode->netfs.inode);
  196. if (op->file[1].put_vnode)
  197. iput(&op->file[1].vnode->netfs.inode);
  198. if (op->more_files) {
  199. for (i = 0; i < op->nr_files - 2; i++)
  200. if (op->more_files[i].put_vnode)
  201. iput(&op->more_files[i].vnode->netfs.inode);
  202. kfree(op->more_files);
  203. }
  204. afs_end_cursor(&op->ac);
  205. afs_put_serverlist(op->net, op->server_list);
  206. afs_put_volume(op->net, op->volume, afs_volume_trace_put_put_op);
  207. key_put(op->key);
  208. kfree(op);
  209. return ret;
  210. }
  211. int afs_do_sync_operation(struct afs_operation *op)
  212. {
  213. afs_begin_vnode_operation(op);
  214. afs_wait_for_operation(op);
  215. return afs_put_operation(op);
  216. }