unlink.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. if (data->name.name != NULL)
  49. kfree(data->name.name);
  50. kfree(data);
  51. }
  52. }
  53. #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
  54. /**
  55. * nfs_copy_dname - copy dentry name to data structure
  56. * @dentry: pointer to dentry
  57. * @data: nfs_unlinkdata
  58. */
  59. static inline void
  60. nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
  61. {
  62. char *str;
  63. int len = dentry->d_name.len;
  64. str = kmalloc(NAME_ALLOC_LEN(len), GFP_KERNEL);
  65. if (!str)
  66. return;
  67. memcpy(str, dentry->d_name.name, len);
  68. if (!data->name.len) {
  69. data->name.len = len;
  70. data->name.name = str;
  71. } else
  72. kfree(str);
  73. }
  74. /**
  75. * nfs_async_unlink_init - Initialize the RPC info
  76. * @task: rpc_task of the sillydelete
  77. *
  78. * We delay initializing RPC info until after the call to dentry_iput()
  79. * in order to minimize races against rename().
  80. */
  81. static void
  82. nfs_async_unlink_init(struct rpc_task *task)
  83. {
  84. struct nfs_unlinkdata *data = (struct nfs_unlinkdata *)task->tk_calldata;
  85. struct dentry *dir = data->dir;
  86. struct rpc_message msg = {
  87. .rpc_cred = data->cred,
  88. };
  89. int status = -ENOENT;
  90. if (!data->name.len)
  91. goto out_err;
  92. status = NFS_PROTO(dir->d_inode)->unlink_setup(&msg, dir, &data->name);
  93. if (status < 0)
  94. goto out_err;
  95. nfs_begin_data_update(dir->d_inode);
  96. rpc_call_setup(task, &msg, 0);
  97. return;
  98. out_err:
  99. rpc_exit(task, status);
  100. }
  101. /**
  102. * nfs_async_unlink_done - Sillydelete post-processing
  103. * @task: rpc_task of the sillydelete
  104. *
  105. * Do the directory attribute update.
  106. */
  107. static void
  108. nfs_async_unlink_done(struct rpc_task *task)
  109. {
  110. struct nfs_unlinkdata *data = (struct nfs_unlinkdata *)task->tk_calldata;
  111. struct dentry *dir = data->dir;
  112. struct inode *dir_i;
  113. if (!dir)
  114. return;
  115. dir_i = dir->d_inode;
  116. nfs_end_data_update(dir_i);
  117. if (NFS_PROTO(dir_i)->unlink_done(dir, task))
  118. return;
  119. put_rpccred(data->cred);
  120. data->cred = NULL;
  121. dput(dir);
  122. }
  123. /**
  124. * nfs_async_unlink_release - Release the sillydelete data.
  125. * @task: rpc_task of the sillydelete
  126. *
  127. * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
  128. * rpc_task would be freed too.
  129. */
  130. static void
  131. nfs_async_unlink_release(struct rpc_task *task)
  132. {
  133. struct nfs_unlinkdata *data = (struct nfs_unlinkdata *)task->tk_calldata;
  134. nfs_put_unlinkdata(data);
  135. }
  136. /**
  137. * nfs_async_unlink - asynchronous unlinking of a file
  138. * @dentry: dentry to unlink
  139. */
  140. int
  141. nfs_async_unlink(struct dentry *dentry)
  142. {
  143. struct dentry *dir = dentry->d_parent;
  144. struct nfs_unlinkdata *data;
  145. struct rpc_task *task;
  146. struct rpc_clnt *clnt = NFS_CLIENT(dir->d_inode);
  147. int status = -ENOMEM;
  148. data = kmalloc(sizeof(*data), GFP_KERNEL);
  149. if (!data)
  150. goto out;
  151. memset(data, 0, sizeof(*data));
  152. data->cred = rpcauth_lookupcred(clnt->cl_auth, 0);
  153. if (IS_ERR(data->cred)) {
  154. status = PTR_ERR(data->cred);
  155. goto out_free;
  156. }
  157. data->dir = dget(dir);
  158. data->dentry = dentry;
  159. data->next = nfs_deletes;
  160. nfs_deletes = data;
  161. data->count = 1;
  162. task = &data->task;
  163. rpc_init_task(task, clnt, nfs_async_unlink_done , RPC_TASK_ASYNC);
  164. task->tk_calldata = data;
  165. task->tk_action = nfs_async_unlink_init;
  166. task->tk_release = nfs_async_unlink_release;
  167. spin_lock(&dentry->d_lock);
  168. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  169. spin_unlock(&dentry->d_lock);
  170. rpc_sleep_on(&nfs_delete_queue, task, NULL, NULL);
  171. status = 0;
  172. out:
  173. return status;
  174. out_free:
  175. kfree(data);
  176. return status;
  177. }
  178. /**
  179. * nfs_complete_unlink - Initialize completion of the sillydelete
  180. * @dentry: dentry to delete
  181. *
  182. * Since we're most likely to be called by dentry_iput(), we
  183. * only use the dentry to find the sillydelete. We then copy the name
  184. * into the qstr.
  185. */
  186. void
  187. nfs_complete_unlink(struct dentry *dentry)
  188. {
  189. struct nfs_unlinkdata *data;
  190. for(data = nfs_deletes; data != NULL; data = data->next) {
  191. if (dentry == data->dentry)
  192. break;
  193. }
  194. if (!data)
  195. return;
  196. data->count++;
  197. nfs_copy_dname(dentry, data);
  198. spin_lock(&dentry->d_lock);
  199. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  200. spin_unlock(&dentry->d_lock);
  201. rpc_wake_up_task(&data->task);
  202. nfs_put_unlinkdata(data);
  203. }