Linux 2.6.28-rc5
[cris-mirror.git] / fs / nfs / unlink.c
blobecc295347775e2b2f31a508da1172e307898d234
1 /*
2 * linux/fs/nfs/unlink.c
4 * nfs sillydelete handling
6 */
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 #include <linux/sched.h>
15 #include <linux/wait.h>
17 #include "internal.h"
19 struct nfs_unlinkdata {
20 struct hlist_node list;
21 struct nfs_removeargs args;
22 struct nfs_removeres res;
23 struct inode *dir;
24 struct rpc_cred *cred;
27 /**
28 * nfs_free_unlinkdata - release data from a sillydelete operation.
29 * @data: pointer to unlink structure.
31 static void
32 nfs_free_unlinkdata(struct nfs_unlinkdata *data)
34 iput(data->dir);
35 put_rpccred(data->cred);
36 kfree(data->args.name.name);
37 kfree(data);
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
46 static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
48 char *str;
49 int len = dentry->d_name.len;
51 str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
52 if (!str)
53 return -ENOMEM;
54 data->args.name.len = len;
55 data->args.name.name = str;
56 return 0;
59 static void nfs_free_dname(struct nfs_unlinkdata *data)
61 kfree(data->args.name.name);
62 data->args.name.name = NULL;
63 data->args.name.len = 0;
66 static void nfs_dec_sillycount(struct inode *dir)
68 struct nfs_inode *nfsi = NFS_I(dir);
69 if (atomic_dec_return(&nfsi->silly_count) == 1)
70 wake_up(&nfsi->waitqueue);
73 /**
74 * nfs_async_unlink_done - Sillydelete post-processing
75 * @task: rpc_task of the sillydelete
77 * Do the directory attribute update.
79 static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
81 struct nfs_unlinkdata *data = calldata;
82 struct inode *dir = data->dir;
84 if (!NFS_PROTO(dir)->unlink_done(task, dir))
85 rpc_restart_call(task);
88 /**
89 * nfs_async_unlink_release - Release the sillydelete data.
90 * @task: rpc_task of the sillydelete
92 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
93 * rpc_task would be freed too.
95 static void nfs_async_unlink_release(void *calldata)
97 struct nfs_unlinkdata *data = calldata;
98 struct super_block *sb = data->dir->i_sb;
100 nfs_dec_sillycount(data->dir);
101 nfs_free_unlinkdata(data);
102 nfs_sb_deactive(sb);
105 static const struct rpc_call_ops nfs_unlink_ops = {
106 .rpc_call_done = nfs_async_unlink_done,
107 .rpc_release = nfs_async_unlink_release,
110 static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
112 struct rpc_message msg = {
113 .rpc_argp = &data->args,
114 .rpc_resp = &data->res,
115 .rpc_cred = data->cred,
117 struct rpc_task_setup task_setup_data = {
118 .rpc_message = &msg,
119 .callback_ops = &nfs_unlink_ops,
120 .callback_data = data,
121 .workqueue = nfsiod_workqueue,
122 .flags = RPC_TASK_ASYNC,
124 struct rpc_task *task;
125 struct dentry *alias;
127 alias = d_lookup(parent, &data->args.name);
128 if (alias != NULL) {
129 int ret = 0;
132 * Hey, we raced with lookup... See if we need to transfer
133 * the sillyrename information to the aliased dentry.
135 nfs_free_dname(data);
136 spin_lock(&alias->d_lock);
137 if (alias->d_inode != NULL &&
138 !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
139 alias->d_fsdata = data;
140 alias->d_flags |= DCACHE_NFSFS_RENAMED;
141 ret = 1;
143 spin_unlock(&alias->d_lock);
144 nfs_dec_sillycount(dir);
145 dput(alias);
146 return ret;
148 data->dir = igrab(dir);
149 if (!data->dir) {
150 nfs_dec_sillycount(dir);
151 return 0;
153 nfs_sb_active(dir->i_sb);
154 data->args.fh = NFS_FH(dir);
155 nfs_fattr_init(&data->res.dir_attr);
157 NFS_PROTO(dir)->unlink_setup(&msg, dir);
159 task_setup_data.rpc_client = NFS_CLIENT(dir);
160 task = rpc_run_task(&task_setup_data);
161 if (!IS_ERR(task))
162 rpc_put_task(task);
163 return 1;
166 static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
168 struct dentry *parent;
169 struct inode *dir;
170 int ret = 0;
173 parent = dget_parent(dentry);
174 if (parent == NULL)
175 goto out_free;
176 dir = parent->d_inode;
177 if (nfs_copy_dname(dentry, data) != 0)
178 goto out_dput;
179 /* Non-exclusive lock protects against concurrent lookup() calls */
180 spin_lock(&dir->i_lock);
181 if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
182 /* Deferred delete */
183 hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
184 spin_unlock(&dir->i_lock);
185 ret = 1;
186 goto out_dput;
188 spin_unlock(&dir->i_lock);
189 ret = nfs_do_call_unlink(parent, dir, data);
190 out_dput:
191 dput(parent);
192 out_free:
193 return ret;
196 void nfs_block_sillyrename(struct dentry *dentry)
198 struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
200 wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
203 void nfs_unblock_sillyrename(struct dentry *dentry)
205 struct inode *dir = dentry->d_inode;
206 struct nfs_inode *nfsi = NFS_I(dir);
207 struct nfs_unlinkdata *data;
209 atomic_inc(&nfsi->silly_count);
210 spin_lock(&dir->i_lock);
211 while (!hlist_empty(&nfsi->silly_list)) {
212 if (!atomic_inc_not_zero(&nfsi->silly_count))
213 break;
214 data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
215 hlist_del(&data->list);
216 spin_unlock(&dir->i_lock);
217 if (nfs_do_call_unlink(dentry, dir, data) == 0)
218 nfs_free_unlinkdata(data);
219 spin_lock(&dir->i_lock);
221 spin_unlock(&dir->i_lock);
225 * nfs_async_unlink - asynchronous unlinking of a file
226 * @dir: parent directory of dentry
227 * @dentry: dentry to unlink
230 nfs_async_unlink(struct inode *dir, struct dentry *dentry)
232 struct nfs_unlinkdata *data;
233 int status = -ENOMEM;
235 data = kzalloc(sizeof(*data), GFP_KERNEL);
236 if (data == NULL)
237 goto out;
239 data->cred = rpc_lookup_cred();
240 if (IS_ERR(data->cred)) {
241 status = PTR_ERR(data->cred);
242 goto out_free;
245 status = -EBUSY;
246 spin_lock(&dentry->d_lock);
247 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
248 goto out_unlock;
249 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
250 dentry->d_fsdata = data;
251 spin_unlock(&dentry->d_lock);
252 return 0;
253 out_unlock:
254 spin_unlock(&dentry->d_lock);
255 put_rpccred(data->cred);
256 out_free:
257 kfree(data);
258 out:
259 return status;
263 * nfs_complete_unlink - Initialize completion of the sillydelete
264 * @dentry: dentry to delete
265 * @inode: inode
267 * Since we're most likely to be called by dentry_iput(), we
268 * only use the dentry to find the sillydelete. We then copy the name
269 * into the qstr.
271 void
272 nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
274 struct nfs_unlinkdata *data = NULL;
276 spin_lock(&dentry->d_lock);
277 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
278 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
279 data = dentry->d_fsdata;
281 spin_unlock(&dentry->d_lock);
283 if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
284 nfs_free_unlinkdata(data);