unlink.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. struct nfs_unlinkdata {
  14. struct nfs_removeargs args;
  15. struct nfs_removeres res;
  16. struct inode *dir;
  17. struct rpc_cred *cred;
  18. };
  19. /**
  20. * nfs_free_unlinkdata - release data from a sillydelete operation.
  21. * @data: pointer to unlink structure.
  22. */
  23. static void
  24. nfs_free_unlinkdata(struct nfs_unlinkdata *data)
  25. {
  26. iput(data->dir);
  27. put_rpccred(data->cred);
  28. kfree(data->args.name.name);
  29. kfree(data);
  30. }
  31. #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
  32. /**
  33. * nfs_copy_dname - copy dentry name to data structure
  34. * @dentry: pointer to dentry
  35. * @data: nfs_unlinkdata
  36. */
  37. static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
  38. {
  39. char *str;
  40. int len = dentry->d_name.len;
  41. str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
  42. if (!str)
  43. return -ENOMEM;
  44. data->args.name.len = len;
  45. data->args.name.name = str;
  46. return 0;
  47. }
  48. /**
  49. * nfs_async_unlink_init - Initialize the RPC info
  50. * task: rpc_task of the sillydelete
  51. */
  52. static void nfs_async_unlink_init(struct rpc_task *task, void *calldata)
  53. {
  54. struct nfs_unlinkdata *data = calldata;
  55. struct inode *dir = data->dir;
  56. struct rpc_message msg = {
  57. .rpc_argp = &data->args,
  58. .rpc_resp = &data->res,
  59. .rpc_cred = data->cred,
  60. };
  61. nfs_begin_data_update(dir);
  62. NFS_PROTO(dir)->unlink_setup(&msg, dir);
  63. rpc_call_setup(task, &msg, 0);
  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. rpc_restart_call(task);
  77. else
  78. nfs_end_data_update(dir);
  79. }
  80. /**
  81. * nfs_async_unlink_release - Release the sillydelete data.
  82. * @task: rpc_task of the sillydelete
  83. *
  84. * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
  85. * rpc_task would be freed too.
  86. */
  87. static void nfs_async_unlink_release(void *calldata)
  88. {
  89. struct nfs_unlinkdata *data = calldata;
  90. nfs_free_unlinkdata(data);
  91. }
  92. static const struct rpc_call_ops nfs_unlink_ops = {
  93. .rpc_call_prepare = nfs_async_unlink_init,
  94. .rpc_call_done = nfs_async_unlink_done,
  95. .rpc_release = nfs_async_unlink_release,
  96. };
  97. static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
  98. {
  99. struct rpc_task *task;
  100. struct dentry *parent;
  101. struct inode *dir;
  102. if (nfs_copy_dname(dentry, data) < 0)
  103. goto out_free;
  104. parent = dget_parent(dentry);
  105. if (parent == NULL)
  106. goto out_free;
  107. dir = igrab(parent->d_inode);
  108. dput(parent);
  109. if (dir == NULL)
  110. goto out_free;
  111. data->dir = dir;
  112. data->args.fh = NFS_FH(dir);
  113. nfs_fattr_init(&data->res.dir_attr);
  114. task = rpc_run_task(NFS_CLIENT(dir), RPC_TASK_ASYNC, &nfs_unlink_ops, data);
  115. if (!IS_ERR(task))
  116. rpc_put_task(task);
  117. return 1;
  118. out_free:
  119. return 0;
  120. }
  121. /**
  122. * nfs_async_unlink - asynchronous unlinking of a file
  123. * @dir: parent directory of dentry
  124. * @dentry: dentry to unlink
  125. */
  126. int
  127. nfs_async_unlink(struct inode *dir, struct dentry *dentry)
  128. {
  129. struct nfs_unlinkdata *data;
  130. int status = -ENOMEM;
  131. data = kzalloc(sizeof(*data), GFP_KERNEL);
  132. if (data == NULL)
  133. goto out;
  134. data->cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
  135. if (IS_ERR(data->cred)) {
  136. status = PTR_ERR(data->cred);
  137. goto out_free;
  138. }
  139. status = -EBUSY;
  140. spin_lock(&dentry->d_lock);
  141. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  142. goto out_unlock;
  143. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  144. dentry->d_fsdata = data;
  145. spin_unlock(&dentry->d_lock);
  146. return 0;
  147. out_unlock:
  148. spin_unlock(&dentry->d_lock);
  149. put_rpccred(data->cred);
  150. out_free:
  151. kfree(data);
  152. out:
  153. return status;
  154. }
  155. /**
  156. * nfs_complete_unlink - Initialize completion of the sillydelete
  157. * @dentry: dentry to delete
  158. * @inode: inode
  159. *
  160. * Since we're most likely to be called by dentry_iput(), we
  161. * only use the dentry to find the sillydelete. We then copy the name
  162. * into the qstr.
  163. */
  164. void
  165. nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
  166. {
  167. struct nfs_unlinkdata *data = NULL;
  168. spin_lock(&dentry->d_lock);
  169. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  170. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  171. data = dentry->d_fsdata;
  172. }
  173. spin_unlock(&dentry->d_lock);
  174. if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
  175. nfs_free_unlinkdata(data);
  176. }