blob: 3adf8b266461fbf3562d7876ed57f50de2f974df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/unlink.c
3 *
4 * nfs sillydelete handling
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
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>
Linus Torvaldsb35e7042007-10-19 19:59:18 -070014#include <linux/sched.h>
15#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Steve Dicksonef818a22007-11-08 04:05:04 -050017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019struct nfs_unlinkdata {
Trond Myklebust565277f2007-10-15 18:17:53 -040020 struct hlist_node list;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040021 struct nfs_removeargs args;
22 struct nfs_removeres res;
23 struct inode *dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/**
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040028 * nfs_free_unlinkdata - release data from a sillydelete operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * @data: pointer to unlink structure.
30 */
31static void
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040032nfs_free_unlinkdata(struct nfs_unlinkdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040034 iput(data->dir);
35 put_rpccred(data->cred);
36 kfree(data->args.name.name);
37 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
40#define NAME_ALLOC_LEN(len) ((len+16) & ~15)
41/**
42 * nfs_copy_dname - copy dentry name to data structure
43 * @dentry: pointer to dentry
44 * @data: nfs_unlinkdata
45 */
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040046static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 char *str;
49 int len = dentry->d_name.len;
50
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040051 str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (!str)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040053 return -ENOMEM;
54 data->args.name.len = len;
55 data->args.name.name = str;
56 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Trond Myklebust565277f2007-10-15 18:17:53 -040059static void nfs_free_dname(struct nfs_unlinkdata *data)
60{
61 kfree(data->args.name.name);
62 data->args.name.name = NULL;
63 data->args.name.len = 0;
64}
65
66static void nfs_dec_sillycount(struct inode *dir)
67{
68 struct nfs_inode *nfsi = NFS_I(dir);
69 if (atomic_dec_return(&nfsi->silly_count) == 1)
70 wake_up(&nfsi->waitqueue);
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * nfs_async_unlink_done - Sillydelete post-processing
75 * @task: rpc_task of the sillydelete
76 *
77 * Do the directory attribute update.
78 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +010079static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040081 struct nfs_unlinkdata *data = calldata;
82 struct inode *dir = data->dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040084 if (!NFS_PROTO(dir)->unlink_done(task, dir))
85 rpc_restart_call(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88/**
89 * nfs_async_unlink_release - Release the sillydelete data.
90 * @task: rpc_task of the sillydelete
91 *
92 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
93 * rpc_task would be freed too.
94 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +010095static void nfs_async_unlink_release(void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Trond Myklebust963d8fe2006-01-03 09:55:04 +010097 struct nfs_unlinkdata *data = calldata;
Trond Myklebust565277f2007-10-15 18:17:53 -040098
99 nfs_dec_sillycount(data->dir);
Steve Dicksonef818a22007-11-08 04:05:04 -0500100 nfs_sb_deactive(NFS_SERVER(data->dir));
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400101 nfs_free_unlinkdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100104static const struct rpc_call_ops nfs_unlink_ops = {
105 .rpc_call_done = nfs_async_unlink_done,
106 .rpc_release = nfs_async_unlink_release,
107};
108
Trond Myklebust565277f2007-10-15 18:17:53 -0400109static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400110{
Trond Myklebust5138fde2007-07-14 15:40:01 -0400111 struct rpc_message msg = {
112 .rpc_argp = &data->args,
113 .rpc_resp = &data->res,
114 .rpc_cred = data->cred,
115 };
Trond Myklebustc970aa82007-07-14 15:39:59 -0400116 struct rpc_task_setup task_setup_data = {
Trond Myklebust5138fde2007-07-14 15:40:01 -0400117 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400118 .callback_ops = &nfs_unlink_ops,
119 .callback_data = data,
120 .flags = RPC_TASK_ASYNC,
121 };
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400122 struct rpc_task *task;
Trond Myklebust565277f2007-10-15 18:17:53 -0400123 struct dentry *alias;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400124
Trond Myklebust565277f2007-10-15 18:17:53 -0400125 alias = d_lookup(parent, &data->args.name);
126 if (alias != NULL) {
127 int ret = 0;
Trond Myklebust609005c2008-01-28 19:42:59 -0500128
Trond Myklebust565277f2007-10-15 18:17:53 -0400129 /*
130 * Hey, we raced with lookup... See if we need to transfer
131 * the sillyrename information to the aliased dentry.
132 */
133 nfs_free_dname(data);
134 spin_lock(&alias->d_lock);
Trond Myklebust609005c2008-01-28 19:42:59 -0500135 if (alias->d_inode != NULL &&
136 !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
Trond Myklebust565277f2007-10-15 18:17:53 -0400137 alias->d_fsdata = data;
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500138 alias->d_flags |= DCACHE_NFSFS_RENAMED;
Trond Myklebust565277f2007-10-15 18:17:53 -0400139 ret = 1;
140 }
141 spin_unlock(&alias->d_lock);
142 nfs_dec_sillycount(dir);
143 dput(alias);
144 return ret;
145 }
146 data->dir = igrab(dir);
147 if (!data->dir) {
148 nfs_dec_sillycount(dir);
149 return 0;
150 }
Steve Dicksonef818a22007-11-08 04:05:04 -0500151 nfs_sb_active(NFS_SERVER(dir));
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400152 data->args.fh = NFS_FH(dir);
153 nfs_fattr_init(&data->res.dir_attr);
154
Trond Myklebust5138fde2007-07-14 15:40:01 -0400155 NFS_PROTO(dir)->unlink_setup(&msg, dir);
Trond Myklebustc970aa82007-07-14 15:39:59 -0400156
Trond Myklebust5138fde2007-07-14 15:40:01 -0400157 task_setup_data.rpc_client = NFS_CLIENT(dir);
Trond Myklebustc970aa82007-07-14 15:39:59 -0400158 task = rpc_run_task(&task_setup_data);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400159 if (!IS_ERR(task))
160 rpc_put_task(task);
161 return 1;
Trond Myklebust565277f2007-10-15 18:17:53 -0400162}
163
164static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
165{
166 struct dentry *parent;
167 struct inode *dir;
168 int ret = 0;
169
170
171 parent = dget_parent(dentry);
172 if (parent == NULL)
173 goto out_free;
174 dir = parent->d_inode;
Trond Myklebust55b70a02007-10-21 12:02:22 -0400175 if (nfs_copy_dname(dentry, data) != 0)
Trond Myklebust565277f2007-10-15 18:17:53 -0400176 goto out_dput;
177 /* Non-exclusive lock protects against concurrent lookup() calls */
178 spin_lock(&dir->i_lock);
179 if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
180 /* Deferred delete */
181 hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
182 spin_unlock(&dir->i_lock);
183 ret = 1;
184 goto out_dput;
185 }
186 spin_unlock(&dir->i_lock);
187 ret = nfs_do_call_unlink(parent, dir, data);
188out_dput:
189 dput(parent);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400190out_free:
Trond Myklebust565277f2007-10-15 18:17:53 -0400191 return ret;
192}
193
194void nfs_block_sillyrename(struct dentry *dentry)
195{
196 struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
197
198 wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
199}
200
201void nfs_unblock_sillyrename(struct dentry *dentry)
202{
203 struct inode *dir = dentry->d_inode;
204 struct nfs_inode *nfsi = NFS_I(dir);
205 struct nfs_unlinkdata *data;
206
207 atomic_inc(&nfsi->silly_count);
208 spin_lock(&dir->i_lock);
209 while (!hlist_empty(&nfsi->silly_list)) {
210 if (!atomic_inc_not_zero(&nfsi->silly_count))
211 break;
212 data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
213 hlist_del(&data->list);
214 spin_unlock(&dir->i_lock);
215 if (nfs_do_call_unlink(dentry, dir, data) == 0)
216 nfs_free_unlinkdata(data);
217 spin_lock(&dir->i_lock);
218 }
219 spin_unlock(&dir->i_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/**
223 * nfs_async_unlink - asynchronous unlinking of a file
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400224 * @dir: parent directory of dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * @dentry: dentry to unlink
226 */
227int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400228nfs_async_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400230 struct nfs_unlinkdata *data;
231 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Eric Sesterhennbd647542006-03-20 13:44:10 -0500233 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400234 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Trond Myklebust98a8e322008-03-12 12:25:28 -0400237 data->cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (IS_ERR(data->cred)) {
239 status = PTR_ERR(data->cred);
240 goto out_free;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400243 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 spin_lock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400245 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
246 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400248 dentry->d_fsdata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 spin_unlock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400250 return 0;
251out_unlock:
252 spin_unlock(&dentry->d_lock);
253 put_rpccred(data->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254out_free:
255 kfree(data);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400256out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return status;
258}
259
260/**
261 * nfs_complete_unlink - Initialize completion of the sillydelete
262 * @dentry: dentry to delete
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400263 * @inode: inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 *
265 * Since we're most likely to be called by dentry_iput(), we
266 * only use the dentry to find the sillydelete. We then copy the name
267 * into the qstr.
268 */
269void
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400270nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400272 struct nfs_unlinkdata *data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 spin_lock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400275 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
276 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
277 data = dentry->d_fsdata;
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 spin_unlock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400280
281 if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
282 nfs_free_unlinkdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}