unlink.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 nfs_async_unlink_init(struct rpc_task *task, void *calldata)
  81. {
  82. struct nfs_unlinkdata *data = calldata;
  83. struct dentry *dir = data->dir;
  84. struct rpc_message msg = {
  85. .rpc_cred = data->cred,
  86. };
  87. int status = -ENOENT;
  88. if (!data->name.len)
  89. goto out_err;
  90. status = NFS_PROTO(dir->d_inode)->unlink_setup(&msg, dir, &data->name);
  91. if (status < 0)
  92. goto out_err;
  93. nfs_begin_data_update(dir->d_inode);
  94. rpc_call_setup(task, &msg, 0);
  95. return;
  96. out_err:
  97. rpc_exit(task, status);
  98. }
  99. /**
  100. * nfs_async_unlink_done - Sillydelete post-processing
  101. * @task: rpc_task of the sillydelete
  102. *
  103. * Do the directory attribute update.
  104. */
  105. static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
  106. {
  107. struct nfs_unlinkdata *data = calldata;
  108. struct dentry *dir = data->dir;
  109. struct inode *dir_i;
  110. if (!dir)
  111. return;
  112. dir_i = dir->d_inode;
  113. nfs_end_data_update(dir_i);
  114. if (NFS_PROTO(dir_i)->unlink_done(dir, task))
  115. return;
  116. put_rpccred(data->cred);
  117. data->cred = NULL;
  118. dput(dir);
  119. }
  120. /**
  121. * nfs_async_unlink_release - Release the sillydelete data.
  122. * @task: rpc_task of the sillydelete
  123. *
  124. * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
  125. * rpc_task would be freed too.
  126. */
  127. static void nfs_async_unlink_release(void *calldata)
  128. {
  129. struct nfs_unlinkdata *data = calldata;
  130. nfs_put_unlinkdata(data);
  131. }
  132. static const struct rpc_call_ops nfs_unlink_ops = {
  133. .rpc_call_prepare = nfs_async_unlink_init,
  134. .rpc_call_done = nfs_async_unlink_done,
  135. .rpc_release = nfs_async_unlink_release,
  136. };
  137. /**
  138. * nfs_async_unlink - asynchronous unlinking of a file
  139. * @dentry: dentry to unlink
  140. */
  141. int
  142. nfs_async_unlink(struct dentry *dentry)
  143. {
  144. struct dentry *dir = dentry->d_parent;
  145. struct nfs_unlinkdata *data;
  146. struct rpc_clnt *clnt = NFS_CLIENT(dir->d_inode);
  147. int status = -ENOMEM;
  148. data = kzalloc(sizeof(*data), GFP_KERNEL);
  149. if (!data)
  150. goto out;
  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. rpc_init_task(&data->task, clnt, RPC_TASK_ASYNC, &nfs_unlink_ops, data);
  162. spin_lock(&dentry->d_lock);
  163. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  164. spin_unlock(&dentry->d_lock);
  165. rpc_sleep_on(&nfs_delete_queue, &data->task, NULL, NULL);
  166. status = 0;
  167. out:
  168. return status;
  169. out_free:
  170. kfree(data);
  171. return status;
  172. }
  173. /**
  174. * nfs_complete_unlink - Initialize completion of the sillydelete
  175. * @dentry: dentry to delete
  176. *
  177. * Since we're most likely to be called by dentry_iput(), we
  178. * only use the dentry to find the sillydelete. We then copy the name
  179. * into the qstr.
  180. */
  181. void
  182. nfs_complete_unlink(struct dentry *dentry)
  183. {
  184. struct nfs_unlinkdata *data;
  185. for(data = nfs_deletes; data != NULL; data = data->next) {
  186. if (dentry == data->dentry)
  187. break;
  188. }
  189. if (!data)
  190. return;
  191. data->count++;
  192. nfs_copy_dname(dentry, data);
  193. spin_lock(&dentry->d_lock);
  194. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  195. spin_unlock(&dentry->d_lock);
  196. rpc_wake_up_task(&data->task);
  197. nfs_put_unlinkdata(data);
  198. }