unlink.c 6.6 KB

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