dir_silly.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* AFS silly rename handling
  3. *
  4. * Copyright (C) 2019 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. * - Derived from NFS's sillyrename.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/fs.h>
  10. #include <linux/namei.h>
  11. #include <linux/fsnotify.h>
  12. #include "internal.h"
  13. static void afs_silly_rename_success(struct afs_operation *op)
  14. {
  15. _enter("op=%08x", op->debug_id);
  16. afs_check_dir_conflict(op, &op->file[0]);
  17. afs_vnode_commit_status(op, &op->file[0]);
  18. }
  19. static void afs_silly_rename_edit_dir(struct afs_operation *op)
  20. {
  21. struct afs_vnode_param *dvp = &op->file[0];
  22. struct afs_vnode *dvnode = dvp->vnode;
  23. struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry));
  24. struct dentry *old = op->dentry;
  25. struct dentry *new = op->dentry_2;
  26. spin_lock(&old->d_lock);
  27. old->d_flags |= DCACHE_NFSFS_RENAMED;
  28. spin_unlock(&old->d_lock);
  29. if (dvnode->silly_key != op->key) {
  30. key_put(dvnode->silly_key);
  31. dvnode->silly_key = key_get(op->key);
  32. }
  33. down_write(&dvnode->validate_lock);
  34. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
  35. dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) {
  36. afs_edit_dir_remove(dvnode, &old->d_name,
  37. afs_edit_dir_for_silly_0);
  38. afs_edit_dir_add(dvnode, &new->d_name,
  39. &vnode->fid, afs_edit_dir_for_silly_1);
  40. }
  41. up_write(&dvnode->validate_lock);
  42. }
  43. static const struct afs_operation_ops afs_silly_rename_operation = {
  44. .issue_afs_rpc = afs_fs_rename,
  45. .issue_yfs_rpc = yfs_fs_rename,
  46. .success = afs_silly_rename_success,
  47. .edit_dir = afs_silly_rename_edit_dir,
  48. };
  49. /*
  50. * Actually perform the silly rename step.
  51. */
  52. static int afs_do_silly_rename(struct afs_vnode *dvnode, struct afs_vnode *vnode,
  53. struct dentry *old, struct dentry *new,
  54. struct key *key)
  55. {
  56. struct afs_operation *op;
  57. _enter("%pd,%pd", old, new);
  58. op = afs_alloc_operation(key, dvnode->volume);
  59. if (IS_ERR(op))
  60. return PTR_ERR(op);
  61. afs_op_set_vnode(op, 0, dvnode);
  62. afs_op_set_vnode(op, 1, dvnode);
  63. op->file[0].dv_delta = 1;
  64. op->file[1].dv_delta = 1;
  65. op->file[0].modification = true;
  66. op->file[1].modification = true;
  67. op->file[0].update_ctime = true;
  68. op->file[1].update_ctime = true;
  69. op->dentry = old;
  70. op->dentry_2 = new;
  71. op->ops = &afs_silly_rename_operation;
  72. trace_afs_silly_rename(vnode, false);
  73. return afs_do_sync_operation(op);
  74. }
  75. /*
  76. * Perform silly-rename of a dentry.
  77. *
  78. * AFS is stateless and the server doesn't know when the client is holding a
  79. * file open. To prevent application problems when a file is unlinked while
  80. * it's still open, the client performs a "silly-rename". That is, it renames
  81. * the file to a hidden file in the same directory, and only performs the
  82. * unlink once the last reference to it is put.
  83. *
  84. * The final cleanup is done during dentry_iput.
  85. */
  86. int afs_sillyrename(struct afs_vnode *dvnode, struct afs_vnode *vnode,
  87. struct dentry *dentry, struct key *key)
  88. {
  89. static unsigned int sillycounter;
  90. struct dentry *sdentry = NULL;
  91. unsigned char silly[16];
  92. int ret = -EBUSY;
  93. _enter("");
  94. /* We don't allow a dentry to be silly-renamed twice. */
  95. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  96. return -EBUSY;
  97. sdentry = NULL;
  98. do {
  99. int slen;
  100. dput(sdentry);
  101. sillycounter++;
  102. /* Create a silly name. Note that the ".__afs" prefix is
  103. * understood by the salvager and must not be changed.
  104. */
  105. slen = scnprintf(silly, sizeof(silly), ".__afs%04X", sillycounter);
  106. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  107. /* N.B. Better to return EBUSY here ... it could be dangerous
  108. * to delete the file while it's in use.
  109. */
  110. if (IS_ERR(sdentry))
  111. goto out;
  112. } while (!d_is_negative(sdentry));
  113. ihold(&vnode->netfs.inode);
  114. ret = afs_do_silly_rename(dvnode, vnode, dentry, sdentry, key);
  115. switch (ret) {
  116. case 0:
  117. /* The rename succeeded. */
  118. set_bit(AFS_VNODE_SILLY_DELETED, &vnode->flags);
  119. d_move(dentry, sdentry);
  120. break;
  121. case -ERESTARTSYS:
  122. /* The result of the rename is unknown. Play it safe by forcing
  123. * a new lookup.
  124. */
  125. d_drop(dentry);
  126. d_drop(sdentry);
  127. }
  128. iput(&vnode->netfs.inode);
  129. dput(sdentry);
  130. out:
  131. _leave(" = %d", ret);
  132. return ret;
  133. }
  134. static void afs_silly_unlink_success(struct afs_operation *op)
  135. {
  136. _enter("op=%08x", op->debug_id);
  137. afs_check_dir_conflict(op, &op->file[0]);
  138. afs_vnode_commit_status(op, &op->file[0]);
  139. afs_vnode_commit_status(op, &op->file[1]);
  140. afs_update_dentry_version(op, &op->file[0], op->dentry);
  141. }
  142. static void afs_silly_unlink_edit_dir(struct afs_operation *op)
  143. {
  144. struct afs_vnode_param *dvp = &op->file[0];
  145. struct afs_vnode *dvnode = dvp->vnode;
  146. _enter("op=%08x", op->debug_id);
  147. down_write(&dvnode->validate_lock);
  148. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
  149. dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
  150. afs_edit_dir_remove(dvnode, &op->dentry->d_name,
  151. afs_edit_dir_for_unlink);
  152. up_write(&dvnode->validate_lock);
  153. }
  154. static const struct afs_operation_ops afs_silly_unlink_operation = {
  155. .issue_afs_rpc = afs_fs_remove_file,
  156. .issue_yfs_rpc = yfs_fs_remove_file,
  157. .success = afs_silly_unlink_success,
  158. .aborted = afs_check_for_remote_deletion,
  159. .edit_dir = afs_silly_unlink_edit_dir,
  160. };
  161. /*
  162. * Tell the server to remove a sillyrename file.
  163. */
  164. static int afs_do_silly_unlink(struct afs_vnode *dvnode, struct afs_vnode *vnode,
  165. struct dentry *dentry, struct key *key)
  166. {
  167. struct afs_operation *op;
  168. _enter("");
  169. op = afs_alloc_operation(NULL, dvnode->volume);
  170. if (IS_ERR(op))
  171. return PTR_ERR(op);
  172. afs_op_set_vnode(op, 0, dvnode);
  173. afs_op_set_vnode(op, 1, vnode);
  174. op->file[0].dv_delta = 1;
  175. op->file[0].modification = true;
  176. op->file[0].update_ctime = true;
  177. op->file[1].op_unlinked = true;
  178. op->file[1].update_ctime = true;
  179. op->dentry = dentry;
  180. op->ops = &afs_silly_unlink_operation;
  181. trace_afs_silly_rename(vnode, true);
  182. afs_begin_vnode_operation(op);
  183. afs_wait_for_operation(op);
  184. /* If there was a conflict with a third party, check the status of the
  185. * unlinked vnode.
  186. */
  187. if (op->error == 0 && (op->flags & AFS_OPERATION_DIR_CONFLICT)) {
  188. op->file[1].update_ctime = false;
  189. op->fetch_status.which = 1;
  190. op->ops = &afs_fetch_status_operation;
  191. afs_begin_vnode_operation(op);
  192. afs_wait_for_operation(op);
  193. }
  194. return afs_put_operation(op);
  195. }
  196. /*
  197. * Remove sillyrename file on iput.
  198. */
  199. int afs_silly_iput(struct dentry *dentry, struct inode *inode)
  200. {
  201. struct afs_vnode *dvnode = AFS_FS_I(d_inode(dentry->d_parent));
  202. struct afs_vnode *vnode = AFS_FS_I(inode);
  203. struct dentry *alias;
  204. int ret;
  205. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  206. _enter("%p{%pd},%llx", dentry, dentry, vnode->fid.vnode);
  207. down_read(&dvnode->rmdir_lock);
  208. alias = d_alloc_parallel(dentry->d_parent, &dentry->d_name, &wq);
  209. if (IS_ERR(alias)) {
  210. up_read(&dvnode->rmdir_lock);
  211. return 0;
  212. }
  213. if (!d_in_lookup(alias)) {
  214. /* We raced with lookup... See if we need to transfer the
  215. * sillyrename information to the aliased dentry.
  216. */
  217. ret = 0;
  218. spin_lock(&alias->d_lock);
  219. if (d_really_is_positive(alias) &&
  220. !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
  221. alias->d_flags |= DCACHE_NFSFS_RENAMED;
  222. ret = 1;
  223. }
  224. spin_unlock(&alias->d_lock);
  225. up_read(&dvnode->rmdir_lock);
  226. dput(alias);
  227. return ret;
  228. }
  229. /* Stop lock-release from complaining. */
  230. spin_lock(&vnode->lock);
  231. vnode->lock_state = AFS_VNODE_LOCK_DELETED;
  232. trace_afs_flock_ev(vnode, NULL, afs_flock_silly_delete, 0);
  233. spin_unlock(&vnode->lock);
  234. afs_do_silly_unlink(dvnode, vnode, dentry, dvnode->silly_key);
  235. up_read(&dvnode->rmdir_lock);
  236. d_lookup_done(alias);
  237. dput(alias);
  238. return 1;
  239. }