unlink.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * linux/fs/nfs/unlink.c
  3. *
  4. * nfs sillydelete handling
  5. *
  6. * NOTE: we rely on holding the BKL for list manipulation protection.
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/string.h>
  10. #include <linux/dcache.h>
  11. #include <linux/sunrpc/sched.h>
  12. #include <linux/sunrpc/clnt.h>
  13. #include <linux/nfs_fs.h>
  14. struct nfs_unlinkdata {
  15. struct nfs_unlinkdata *next;
  16. struct dentry *dir, *dentry;
  17. struct qstr name;
  18. struct rpc_task task;
  19. struct rpc_cred *cred;
  20. unsigned int count;
  21. };
  22. static struct nfs_unlinkdata *nfs_deletes;
  23. static RPC_WAITQ(nfs_delete_queue, "nfs_delete_queue");
  24. /**
  25. * nfs_detach_unlinkdata - Remove asynchronous unlink from global list
  26. * @data: pointer to descriptor
  27. */
  28. static inline void
  29. nfs_detach_unlinkdata(struct nfs_unlinkdata *data)
  30. {
  31. struct nfs_unlinkdata **q;
  32. for (q = &nfs_deletes; *q != NULL; q = &((*q)->next)) {
  33. if (*q == data) {
  34. *q = data->next;
  35. break;
  36. }
  37. }
  38. }
  39. /**
  40. * nfs_put_unlinkdata - release data from a sillydelete operation.
  41. * @data: pointer to unlink structure.
  42. */
  43. static void
  44. nfs_put_unlinkdata(struct nfs_unlinkdata *data)
  45. {
  46. if (--data->count == 0) {
  47. nfs_detach_unlinkdata(data);
  48. kfree(data->name.name);
  49. kfree(data);
  50. }
  51. }
  52. #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
  53. /**
  54. * nfs_copy_dname - copy dentry name to data structure
  55. * @dentry: pointer to dentry
  56. * @data: nfs_unlinkdata
  57. */
  58. static inline void
  59. nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
  60. {
  61. char *str;
  62. int len = dentry->d_name.len;
  63. str = kmalloc(NAME_ALLOC_LEN(len), GFP_KERNEL);
  64. if (!str)
  65. return;
  66. memcpy(str, dentry->d_name.name, len);
  67. if (!data->name.len) {
  68. data->name.len = len;
  69. data->name.name = str;
  70. } else
  71. kfree(str);
  72. }
  73. /**
  74. * nfs_async_unlink_init - Initialize the RPC info
  75. * @task: rpc_task of the sillydelete
  76. *
  77. * We delay initializing RPC info until after the call to dentry_iput()
  78. * in order to minimize races against rename().
  79. */
  80. static void
  81. nfs_async_unlink_init(struct rpc_task *task)
  82. {
  83. struct nfs_unlinkdata *data = (struct nfs_unlinkdata *)task->tk_calldata;
  84. struct dentry *dir = data->dir;
  85. struct rpc_message msg = {
  86. .rpc_cred = data->cred,
  87. };
  88. int status = -ENOENT;
  89. if (!data->name.len)
  90. goto out_err;
  91. status = NFS_PROTO(dir->d_inode)->unlink_setup(&msg, dir, &data->name);
  92. if (status < 0)
  93. goto out_err;
  94. nfs_begin_data_update(dir->d_inode);
  95. rpc_call_setup(task, &msg, 0);
  96. return;
  97. out_err:
  98. rpc_exit(task, status);
  99. }
  100. /**
  101. * nfs_async_unlink_done - Sillydelete post-processing
  102. * @task: rpc_task of the sillydelete
  103. *
  104. * Do the directory attribute update.
  105. */
  106. static void
  107. nfs_async_unlink_done(struct rpc_task *task)
  108. {
  109. struct nfs_unlinkdata *data = (struct nfs_unlinkdata *)task->tk_calldata;
  110. struct dentry *dir = data->dir;
  111. struct inode *dir_i;
  112. if (!dir)
  113. return;
  114. dir_i = dir->d_inode;
  115. nfs_end_data_update(dir_i);
  116. if (NFS_PROTO(dir_i)->unlink_done(dir, task))
  117. return;
  118. put_rpccred(data->cred);
  119. data->cred = NULL;
  120. dput(dir);
  121. }
  122. /**
  123. * nfs_async_unlink_release - Release the sillydelete data.
  124. * @task: rpc_task of the sillydelete
  125. *
  126. * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
  127. * rpc_task would be freed too.
  128. */
  129. static void
  130. nfs_async_unlink_release(struct rpc_task *task)
  131. {
  132. struct nfs_unlinkdata *data = (struct nfs_unlinkdata *)task->tk_calldata;
  133. nfs_put_unlinkdata(data);
  134. }
  135. /**
  136. * nfs_async_unlink - asynchronous unlinking of a file
  137. * @dentry: dentry to unlink
  138. */
  139. int
  140. nfs_async_unlink(struct dentry *dentry)
  141. {
  142. struct dentry *dir = dentry->d_parent;
  143. struct nfs_unlinkdata *data;
  144. struct rpc_task *task;
  145. struct rpc_clnt *clnt = NFS_CLIENT(dir->d_inode);
  146. int status = -ENOMEM;
  147. data = kmalloc(sizeof(*data), GFP_KERNEL);
  148. if (!data)
  149. goto out;
  150. memset(data, 0, sizeof(*data));
  151. data->cred = rpcauth_lookupcred(clnt->cl_auth, 0);
  152. if (IS_ERR(data->cred)) {
  153. status = PTR_ERR(data->cred);
  154. goto out_free;
  155. }
  156. data->dir = dget(dir);
  157. data->dentry = dentry;
  158. data->next = nfs_deletes;
  159. nfs_deletes = data;
  160. data->count = 1;
  161. task = &data->task;
  162. rpc_init_task(task, clnt, nfs_async_unlink_done , RPC_TASK_ASYNC);
  163. task->tk_calldata = data;
  164. task->tk_action = nfs_async_unlink_init;
  165. task->tk_release = nfs_async_unlink_release;
  166. spin_lock(&dentry->d_lock);
  167. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  168. spin_unlock(&dentry->d_lock);
  169. rpc_sleep_on(&nfs_delete_queue, task, NULL, NULL);
  170. status = 0;
  171. out:
  172. return status;
  173. out_free:
  174. kfree(data);
  175. return status;
  176. }
  177. /**
  178. * nfs_complete_unlink - Initialize completion of the sillydelete
  179. * @dentry: dentry to delete
  180. *
  181. * Since we're most likely to be called by dentry_iput(), we
  182. * only use the dentry to find the sillydelete. We then copy the name
  183. * into the qstr.
  184. */
  185. void
  186. nfs_complete_unlink(struct dentry *dentry)
  187. {
  188. struct nfs_unlinkdata *data;
  189. for(data = nfs_deletes; data != NULL; data = data->next) {
  190. if (dentry == data->dentry)
  191. break;
  192. }
  193. if (!data)
  194. return;
  195. data->count++;
  196. nfs_copy_dname(dentry, data);
  197. spin_lock(&dentry->d_lock);
  198. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  199. spin_unlock(&dentry->d_lock);
  200. rpc_wake_up_task(&data->task);
  201. nfs_put_unlinkdata(data);
  202. }