unlink.c 7.2 KB

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