unlink.c 6.6 KB

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