unlink.c 6.5 KB

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