unlink.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * linux/fs/nfs/unlink.c
  3. *
  4. * nfs sillydelete handling
  5. *
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/string.h>
  9. #include <linux/dcache.h>
  10. #include <linux/sunrpc/sched.h>
  11. #include <linux/sunrpc/clnt.h>
  12. #include <linux/nfs_fs.h>
  13. #include <linux/sched.h>
  14. #include <linux/wait.h>
  15. #include "internal.h"
  16. struct nfs_unlinkdata {
  17. struct hlist_node list;
  18. struct nfs_removeargs args;
  19. struct nfs_removeres res;
  20. struct inode *dir;
  21. struct rpc_cred *cred;
  22. };
  23. /**
  24. * nfs_free_unlinkdata - release data from a sillydelete operation.
  25. * @data: pointer to unlink structure.
  26. */
  27. static void
  28. nfs_free_unlinkdata(struct nfs_unlinkdata *data)
  29. {
  30. iput(data->dir);
  31. put_rpccred(data->cred);
  32. kfree(data->args.name.name);
  33. kfree(data);
  34. }
  35. #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
  36. /**
  37. * nfs_copy_dname - copy dentry name to data structure
  38. * @dentry: pointer to dentry
  39. * @data: nfs_unlinkdata
  40. */
  41. static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
  42. {
  43. char *str;
  44. int len = dentry->d_name.len;
  45. str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
  46. if (!str)
  47. return -ENOMEM;
  48. data->args.name.len = len;
  49. data->args.name.name = str;
  50. return 0;
  51. }
  52. static void nfs_free_dname(struct nfs_unlinkdata *data)
  53. {
  54. kfree(data->args.name.name);
  55. data->args.name.name = NULL;
  56. data->args.name.len = 0;
  57. }
  58. static void nfs_dec_sillycount(struct inode *dir)
  59. {
  60. struct nfs_inode *nfsi = NFS_I(dir);
  61. if (atomic_dec_return(&nfsi->silly_count) == 1)
  62. wake_up(&nfsi->waitqueue);
  63. }
  64. /**
  65. * nfs_async_unlink_done - Sillydelete post-processing
  66. * @task: rpc_task of the sillydelete
  67. *
  68. * Do the directory attribute update.
  69. */
  70. static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
  71. {
  72. struct nfs_unlinkdata *data = calldata;
  73. struct inode *dir = data->dir;
  74. if (!NFS_PROTO(dir)->unlink_done(task, dir))
  75. rpc_restart_call(task);
  76. }
  77. /**
  78. * nfs_async_unlink_release - Release the sillydelete data.
  79. * @task: rpc_task of the sillydelete
  80. *
  81. * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
  82. * rpc_task would be freed too.
  83. */
  84. static void nfs_async_unlink_release(void *calldata)
  85. {
  86. struct nfs_unlinkdata *data = calldata;
  87. struct super_block *sb = data->dir->i_sb;
  88. nfs_dec_sillycount(data->dir);
  89. nfs_free_unlinkdata(data);
  90. nfs_sb_deactive(sb);
  91. }
  92. static const struct rpc_call_ops nfs_unlink_ops = {
  93. .rpc_call_done = nfs_async_unlink_done,
  94. .rpc_release = nfs_async_unlink_release,
  95. };
  96. static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
  97. {
  98. struct rpc_message msg = {
  99. .rpc_argp = &data->args,
  100. .rpc_resp = &data->res,
  101. .rpc_cred = data->cred,
  102. };
  103. struct rpc_task_setup task_setup_data = {
  104. .rpc_message = &msg,
  105. .callback_ops = &nfs_unlink_ops,
  106. .callback_data = data,
  107. .workqueue = nfsiod_workqueue,
  108. .flags = RPC_TASK_ASYNC,
  109. };
  110. struct rpc_task *task;
  111. struct dentry *alias;
  112. alias = d_lookup(parent, &data->args.name);
  113. if (alias != NULL) {
  114. int ret = 0;
  115. /*
  116. * Hey, we raced with lookup... See if we need to transfer
  117. * the sillyrename information to the aliased dentry.
  118. */
  119. nfs_free_dname(data);
  120. spin_lock(&alias->d_lock);
  121. if (alias->d_inode != NULL &&
  122. !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
  123. alias->d_fsdata = data;
  124. alias->d_flags |= DCACHE_NFSFS_RENAMED;
  125. ret = 1;
  126. }
  127. spin_unlock(&alias->d_lock);
  128. nfs_dec_sillycount(dir);
  129. dput(alias);
  130. return ret;
  131. }
  132. data->dir = igrab(dir);
  133. if (!data->dir) {
  134. nfs_dec_sillycount(dir);
  135. return 0;
  136. }
  137. nfs_sb_active(dir->i_sb);
  138. data->args.fh = NFS_FH(dir);
  139. nfs_fattr_init(&data->res.dir_attr);
  140. NFS_PROTO(dir)->unlink_setup(&msg, dir);
  141. task_setup_data.rpc_client = NFS_CLIENT(dir);
  142. task = rpc_run_task(&task_setup_data);
  143. if (!IS_ERR(task))
  144. rpc_put_task(task);
  145. return 1;
  146. }
  147. static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
  148. {
  149. struct dentry *parent;
  150. struct inode *dir;
  151. int ret = 0;
  152. parent = dget_parent(dentry);
  153. if (parent == NULL)
  154. goto out_free;
  155. dir = parent->d_inode;
  156. if (nfs_copy_dname(dentry, data) != 0)
  157. goto out_dput;
  158. /* Non-exclusive lock protects against concurrent lookup() calls */
  159. spin_lock(&dir->i_lock);
  160. if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
  161. /* Deferred delete */
  162. hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
  163. spin_unlock(&dir->i_lock);
  164. ret = 1;
  165. goto out_dput;
  166. }
  167. spin_unlock(&dir->i_lock);
  168. ret = nfs_do_call_unlink(parent, dir, data);
  169. out_dput:
  170. dput(parent);
  171. out_free:
  172. return ret;
  173. }
  174. void nfs_block_sillyrename(struct dentry *dentry)
  175. {
  176. struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
  177. wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
  178. }
  179. void nfs_unblock_sillyrename(struct dentry *dentry)
  180. {
  181. struct inode *dir = dentry->d_inode;
  182. struct nfs_inode *nfsi = NFS_I(dir);
  183. struct nfs_unlinkdata *data;
  184. atomic_inc(&nfsi->silly_count);
  185. spin_lock(&dir->i_lock);
  186. while (!hlist_empty(&nfsi->silly_list)) {
  187. if (!atomic_inc_not_zero(&nfsi->silly_count))
  188. break;
  189. data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
  190. hlist_del(&data->list);
  191. spin_unlock(&dir->i_lock);
  192. if (nfs_do_call_unlink(dentry, dir, data) == 0)
  193. nfs_free_unlinkdata(data);
  194. spin_lock(&dir->i_lock);
  195. }
  196. spin_unlock(&dir->i_lock);
  197. }
  198. /**
  199. * nfs_async_unlink - asynchronous unlinking of a file
  200. * @dir: parent directory of dentry
  201. * @dentry: dentry to unlink
  202. */
  203. int
  204. nfs_async_unlink(struct inode *dir, struct dentry *dentry)
  205. {
  206. struct nfs_unlinkdata *data;
  207. int status = -ENOMEM;
  208. data = kzalloc(sizeof(*data), GFP_KERNEL);
  209. if (data == NULL)
  210. goto out;
  211. data->cred = rpc_lookup_cred();
  212. if (IS_ERR(data->cred)) {
  213. status = PTR_ERR(data->cred);
  214. goto out_free;
  215. }
  216. status = -EBUSY;
  217. spin_lock(&dentry->d_lock);
  218. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  219. goto out_unlock;
  220. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  221. dentry->d_fsdata = data;
  222. spin_unlock(&dentry->d_lock);
  223. return 0;
  224. out_unlock:
  225. spin_unlock(&dentry->d_lock);
  226. put_rpccred(data->cred);
  227. out_free:
  228. kfree(data);
  229. out:
  230. return status;
  231. }
  232. /**
  233. * nfs_complete_unlink - Initialize completion of the sillydelete
  234. * @dentry: dentry to delete
  235. * @inode: inode
  236. *
  237. * Since we're most likely to be called by dentry_iput(), we
  238. * only use the dentry to find the sillydelete. We then copy the name
  239. * into the qstr.
  240. */
  241. void
  242. nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
  243. {
  244. struct nfs_unlinkdata *data = NULL;
  245. spin_lock(&dentry->d_lock);
  246. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  247. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  248. data = dentry->d_fsdata;
  249. }
  250. spin_unlock(&dentry->d_lock);
  251. if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
  252. nfs_free_unlinkdata(data);
  253. }