unlink.c 7.1 KB

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