unlink.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 *task;
  115. struct dentry *alias;
  116. alias = d_lookup(parent, &data->args.name);
  117. if (alias != NULL) {
  118. int ret = 0;
  119. /*
  120. * Hey, we raced with lookup... See if we need to transfer
  121. * the sillyrename information to the aliased dentry.
  122. */
  123. nfs_free_dname(data);
  124. spin_lock(&alias->d_lock);
  125. if (alias->d_inode != NULL &&
  126. !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
  127. alias->d_fsdata = data;
  128. alias->d_flags |= DCACHE_NFSFS_RENAMED;
  129. ret = 1;
  130. }
  131. spin_unlock(&alias->d_lock);
  132. nfs_dec_sillycount(dir);
  133. dput(alias);
  134. return ret;
  135. }
  136. data->dir = igrab(dir);
  137. if (!data->dir) {
  138. nfs_dec_sillycount(dir);
  139. return 0;
  140. }
  141. nfs_sb_active(NFS_SERVER(dir));
  142. data->args.fh = NFS_FH(dir);
  143. nfs_fattr_init(&data->res.dir_attr);
  144. task = rpc_run_task(NFS_CLIENT(dir), RPC_TASK_ASYNC, &nfs_unlink_ops, data);
  145. if (!IS_ERR(task))
  146. rpc_put_task(task);
  147. return 1;
  148. }
  149. static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
  150. {
  151. struct dentry *parent;
  152. struct inode *dir;
  153. int ret = 0;
  154. parent = dget_parent(dentry);
  155. if (parent == NULL)
  156. goto out_free;
  157. dir = parent->d_inode;
  158. if (nfs_copy_dname(dentry, data) != 0)
  159. goto out_dput;
  160. /* Non-exclusive lock protects against concurrent lookup() calls */
  161. spin_lock(&dir->i_lock);
  162. if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
  163. /* Deferred delete */
  164. hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
  165. spin_unlock(&dir->i_lock);
  166. ret = 1;
  167. goto out_dput;
  168. }
  169. spin_unlock(&dir->i_lock);
  170. ret = nfs_do_call_unlink(parent, dir, data);
  171. out_dput:
  172. dput(parent);
  173. out_free:
  174. return ret;
  175. }
  176. void nfs_block_sillyrename(struct dentry *dentry)
  177. {
  178. struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
  179. wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
  180. }
  181. void nfs_unblock_sillyrename(struct dentry *dentry)
  182. {
  183. struct inode *dir = dentry->d_inode;
  184. struct nfs_inode *nfsi = NFS_I(dir);
  185. struct nfs_unlinkdata *data;
  186. atomic_inc(&nfsi->silly_count);
  187. spin_lock(&dir->i_lock);
  188. while (!hlist_empty(&nfsi->silly_list)) {
  189. if (!atomic_inc_not_zero(&nfsi->silly_count))
  190. break;
  191. data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
  192. hlist_del(&data->list);
  193. spin_unlock(&dir->i_lock);
  194. if (nfs_do_call_unlink(dentry, dir, data) == 0)
  195. nfs_free_unlinkdata(data);
  196. spin_lock(&dir->i_lock);
  197. }
  198. spin_unlock(&dir->i_lock);
  199. }
  200. /**
  201. * nfs_async_unlink - asynchronous unlinking of a file
  202. * @dir: parent directory of dentry
  203. * @dentry: dentry to unlink
  204. */
  205. int
  206. nfs_async_unlink(struct inode *dir, struct dentry *dentry)
  207. {
  208. struct nfs_unlinkdata *data;
  209. int status = -ENOMEM;
  210. data = kzalloc(sizeof(*data), GFP_KERNEL);
  211. if (data == NULL)
  212. goto out;
  213. data->cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
  214. if (IS_ERR(data->cred)) {
  215. status = PTR_ERR(data->cred);
  216. goto out_free;
  217. }
  218. status = -EBUSY;
  219. spin_lock(&dentry->d_lock);
  220. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  221. goto out_unlock;
  222. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  223. dentry->d_fsdata = data;
  224. spin_unlock(&dentry->d_lock);
  225. return 0;
  226. out_unlock:
  227. spin_unlock(&dentry->d_lock);
  228. put_rpccred(data->cred);
  229. out_free:
  230. kfree(data);
  231. out:
  232. return status;
  233. }
  234. /**
  235. * nfs_complete_unlink - Initialize completion of the sillydelete
  236. * @dentry: dentry to delete
  237. * @inode: inode
  238. *
  239. * Since we're most likely to be called by dentry_iput(), we
  240. * only use the dentry to find the sillydelete. We then copy the name
  241. * into the qstr.
  242. */
  243. void
  244. nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
  245. {
  246. struct nfs_unlinkdata *data = NULL;
  247. spin_lock(&dentry->d_lock);
  248. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  249. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  250. data = dentry->d_fsdata;
  251. }
  252. spin_unlock(&dentry->d_lock);
  253. if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
  254. nfs_free_unlinkdata(data);
  255. }