unlink.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_PROTO(dir)->unlink_setup(&msg, dir);
  62. rpc_call_setup(task, &msg, 0);
  63. }
  64. /**
  65. * nfs_async_unlink_done - Sillydelete post-processing
  66. * @task: rpc_task of the sillydelete
  67. *
  68. * Do the directory attribute update.
  69. */
  70. static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
  71. {
  72. struct nfs_unlinkdata *data = calldata;
  73. struct inode *dir = data->dir;
  74. if (!NFS_PROTO(dir)->unlink_done(task, dir))
  75. rpc_restart_call(task);
  76. }
  77. /**
  78. * nfs_async_unlink_release - Release the sillydelete data.
  79. * @task: rpc_task of the sillydelete
  80. *
  81. * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
  82. * rpc_task would be freed too.
  83. */
  84. static void nfs_async_unlink_release(void *calldata)
  85. {
  86. struct nfs_unlinkdata *data = calldata;
  87. nfs_free_unlinkdata(data);
  88. }
  89. static const struct rpc_call_ops nfs_unlink_ops = {
  90. .rpc_call_prepare = nfs_async_unlink_init,
  91. .rpc_call_done = nfs_async_unlink_done,
  92. .rpc_release = nfs_async_unlink_release,
  93. };
  94. static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
  95. {
  96. struct rpc_task *task;
  97. struct dentry *parent;
  98. struct inode *dir;
  99. if (nfs_copy_dname(dentry, data) < 0)
  100. goto out_free;
  101. parent = dget_parent(dentry);
  102. if (parent == NULL)
  103. goto out_free;
  104. dir = igrab(parent->d_inode);
  105. dput(parent);
  106. if (dir == NULL)
  107. goto out_free;
  108. data->dir = dir;
  109. data->args.fh = NFS_FH(dir);
  110. nfs_fattr_init(&data->res.dir_attr);
  111. task = rpc_run_task(NFS_CLIENT(dir), RPC_TASK_ASYNC, &nfs_unlink_ops, data);
  112. if (!IS_ERR(task))
  113. rpc_put_task(task);
  114. return 1;
  115. out_free:
  116. return 0;
  117. }
  118. /**
  119. * nfs_async_unlink - asynchronous unlinking of a file
  120. * @dir: parent directory of dentry
  121. * @dentry: dentry to unlink
  122. */
  123. int
  124. nfs_async_unlink(struct inode *dir, struct dentry *dentry)
  125. {
  126. struct nfs_unlinkdata *data;
  127. int status = -ENOMEM;
  128. data = kzalloc(sizeof(*data), GFP_KERNEL);
  129. if (data == NULL)
  130. goto out;
  131. data->cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
  132. if (IS_ERR(data->cred)) {
  133. status = PTR_ERR(data->cred);
  134. goto out_free;
  135. }
  136. status = -EBUSY;
  137. spin_lock(&dentry->d_lock);
  138. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  139. goto out_unlock;
  140. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  141. dentry->d_fsdata = data;
  142. spin_unlock(&dentry->d_lock);
  143. return 0;
  144. out_unlock:
  145. spin_unlock(&dentry->d_lock);
  146. put_rpccred(data->cred);
  147. out_free:
  148. kfree(data);
  149. out:
  150. return status;
  151. }
  152. /**
  153. * nfs_complete_unlink - Initialize completion of the sillydelete
  154. * @dentry: dentry to delete
  155. * @inode: inode
  156. *
  157. * Since we're most likely to be called by dentry_iput(), we
  158. * only use the dentry to find the sillydelete. We then copy the name
  159. * into the qstr.
  160. */
  161. void
  162. nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
  163. {
  164. struct nfs_unlinkdata *data = NULL;
  165. spin_lock(&dentry->d_lock);
  166. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  167. dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
  168. data = dentry->d_fsdata;
  169. }
  170. spin_unlock(&dentry->d_lock);
  171. if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
  172. nfs_free_unlinkdata(data);
  173. }