unlink.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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_init - Initialize the RPC info
  66. * task: rpc_task of the sillydelete
  67. */
  68. static void nfs_async_unlink_init(struct rpc_task *task, void *calldata)
  69. {
  70. struct nfs_unlinkdata *data = calldata;
  71. struct inode *dir = data->dir;
  72. struct rpc_message msg = {
  73. .rpc_argp = &data->args,
  74. .rpc_resp = &data->res,
  75. .rpc_cred = data->cred,
  76. };
  77. NFS_PROTO(dir)->unlink_setup(&msg, dir);
  78. rpc_call_setup(task, &msg, 0);
  79. }
  80. /**
  81. * nfs_async_unlink_done - Sillydelete post-processing
  82. * @task: rpc_task of the sillydelete
  83. *
  84. * Do the directory attribute update.
  85. */
  86. static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
  87. {
  88. struct nfs_unlinkdata *data = calldata;
  89. struct inode *dir = data->dir;
  90. if (!NFS_PROTO(dir)->unlink_done(task, dir))
  91. rpc_restart_call(task);
  92. }
  93. /**
  94. * nfs_async_unlink_release - Release the sillydelete data.
  95. * @task: rpc_task of the sillydelete
  96. *
  97. * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
  98. * rpc_task would be freed too.
  99. */
  100. static void nfs_async_unlink_release(void *calldata)
  101. {
  102. struct nfs_unlinkdata *data = calldata;
  103. nfs_dec_sillycount(data->dir);
  104. nfs_sb_deactive(NFS_SERVER(data->dir));
  105. nfs_free_unlinkdata(data);
  106. }
  107. static const struct rpc_call_ops nfs_unlink_ops = {
  108. .rpc_call_prepare = nfs_async_unlink_init,
  109. .rpc_call_done = nfs_async_unlink_done,
  110. .rpc_release = nfs_async_unlink_release,
  111. };
  112. static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
  113. {
  114. struct rpc_task_setup task_setup_data = {
  115. .callback_ops = &nfs_unlink_ops,
  116. .callback_data = data,
  117. .flags = RPC_TASK_ASYNC,
  118. };
  119. struct rpc_task *task;
  120. struct dentry *alias;
  121. alias = d_lookup(parent, &data->args.name);
  122. if (alias != NULL) {
  123. int ret = 0;
  124. /*
  125. * Hey, we raced with lookup... See if we need to transfer
  126. * the sillyrename information to the aliased dentry.
  127. */
  128. nfs_free_dname(data);
  129. spin_lock(&alias->d_lock);
  130. if (alias->d_inode != NULL &&
  131. !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
  132. alias->d_fsdata = data;
  133. alias->d_flags |= DCACHE_NFSFS_RENAMED;
  134. ret = 1;
  135. }
  136. spin_unlock(&alias->d_lock);
  137. nfs_dec_sillycount(dir);
  138. dput(alias);
  139. return ret;
  140. }
  141. data->dir = igrab(dir);
  142. if (!data->dir) {
  143. nfs_dec_sillycount(dir);
  144. return 0;
  145. }
  146. nfs_sb_active(NFS_SERVER(dir));
  147. data->args.fh = NFS_FH(dir);
  148. nfs_fattr_init(&data->res.dir_attr);
  149. task_setup_data.rpc_client = NFS_CLIENT(dir);
  150. task = rpc_run_task(&task_setup_data);
  151. if (!IS_ERR(task))
  152. rpc_put_task(task);
  153. return 1;
  154. }
  155. static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
  156. {
  157. struct dentry *parent;
  158. struct inode *dir;
  159. int ret = 0;
  160. parent = dget_parent(dentry);
  161. if (parent == NULL)
  162. goto out_free;
  163. dir = parent->d_inode;
  164. if (nfs_copy_dname(dentry, data) != 0)
  165. goto out_dput;
  166. /* Non-exclusive lock protects against concurrent lookup() calls */
  167. spin_lock(&dir->i_lock);
  168. if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
  169. /* Deferred delete */
  170. hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
  171. spin_unlock(&dir->i_lock);
  172. ret = 1;
  173. goto out_dput;
  174. }
  175. spin_unlock(&dir->i_lock);
  176. ret = nfs_do_call_unlink(parent, dir, data);
  177. out_dput:
  178. dput(parent);
  179. out_free:
  180. return ret;
  181. }
  182. void nfs_block_sillyrename(struct dentry *dentry)
  183. {
  184. struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
  185. wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
  186. }
  187. void nfs_unblock_sillyrename(struct dentry *dentry)
  188. {
  189. struct inode *dir = dentry->d_inode;
  190. struct nfs_inode *nfsi = NFS_I(dir);
  191. struct nfs_unlinkdata *data;
  192. atomic_inc(&nfsi->silly_count);
  193. spin_lock(&dir->i_lock);
  194. while (!hlist_empty(&nfsi->silly_list)) {
  195. if (!atomic_inc_not_zero(&nfsi->silly_count))
  196. break;
  197. data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
  198. hlist_del(&data->list);
  199. spin_unlock(&dir->i_lock);
  200. if (nfs_do_call_unlink(dentry, dir, data) == 0)
  201. nfs_free_unlinkdata(data);
  202. spin_lock(&dir->i_lock);
  203. }
  204. spin_unlock(&dir->i_lock);
  205. }
  206. /**
  207. * nfs_async_unlink - asynchronous unlinking of a file
  208. * @dir: parent directory of dentry
  209. * @dentry: dentry to unlink
  210. */
  211. int
  212. nfs_async_unlink(struct inode *dir, struct dentry *dentry)
  213. {
  214. struct nfs_unlinkdata *data;
  215. int status = -ENOMEM;
  216. data = kzalloc(sizeof(*data), GFP_KERNEL);
  217. if (data == NULL)
  218. goto out;
  219. data->cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
  220. if (IS_ERR(data->cred)) {
  221. status = PTR_ERR(data->cred);
  222. goto out_free;
  223. }
  224. status = -EBUSY;
  225. spin_lock(&dentry->d_lock);
  226. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  227. goto out_unlock;
  228. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  229. dentry->d_fsdata = data;
  230. spin_unlock(&dentry->d_lock);
  231. return 0;
  232. out_unlock:
  233. spin_unlock(&dentry->d_lock);
  234. put_rpccred(data->cred);
  235. out_free:
  236. kfree(data);
  237. out:
  238. return status;
  239. }
  240. /**
  241. * nfs_complete_unlink - Initialize completion of the sillydelete
  242. * @dentry: dentry to delete
  243. * @inode: inode
  244. *
  245. * Since we're most likely to be called by dentry_iput(), we
  246. * only use the dentry to find the sillydelete. We then copy the name
  247. * into the qstr.
  248. */
  249. void
  250. nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
  251. {
  252. struct nfs_unlinkdata *data = NULL;
  253. spin_lock(&dentry->d_lock);
  254. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  255. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  256. data = dentry->d_fsdata;
  257. }
  258. spin_unlock(&dentry->d_lock);
  259. if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
  260. nfs_free_unlinkdata(data);
  261. }