blob: ecc295347775e2b2f31a508da1172e307898d234 [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 Myklebust744d18d2008-07-27 18:03:19 -040098 struct super_block *sb = data->dir->i_sb;
Trond Myklebust565277f2007-10-15 18:17:53 -040099
100 nfs_dec_sillycount(data->dir);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400101 nfs_free_unlinkdata(data);
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400102 nfs_sb_deactive(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100105static const struct rpc_call_ops nfs_unlink_ops = {
106 .rpc_call_done = nfs_async_unlink_done,
107 .rpc_release = nfs_async_unlink_release,
108};
109
Trond Myklebust565277f2007-10-15 18:17:53 -0400110static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400111{
Trond Myklebust5138fde2007-07-14 15:40:01 -0400112 struct rpc_message msg = {
113 .rpc_argp = &data->args,
114 .rpc_resp = &data->res,
115 .rpc_cred = data->cred,
116 };
Trond Myklebustc970aa82007-07-14 15:39:59 -0400117 struct rpc_task_setup task_setup_data = {
Trond Myklebust5138fde2007-07-14 15:40:01 -0400118 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400119 .callback_ops = &nfs_unlink_ops,
120 .callback_data = data,
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400121 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400122 .flags = RPC_TASK_ASYNC,
123 };
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400124 struct rpc_task *task;
Trond Myklebust565277f2007-10-15 18:17:53 -0400125 struct dentry *alias;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400126
Trond Myklebust565277f2007-10-15 18:17:53 -0400127 alias = d_lookup(parent, &data->args.name);
128 if (alias != NULL) {
129 int ret = 0;
Trond Myklebust609005c32008-01-28 19:42:59 -0500130
Trond Myklebust565277f2007-10-15 18:17:53 -0400131 /*
132 * Hey, we raced with lookup... See if we need to transfer
133 * the sillyrename information to the aliased dentry.
134 */
135 nfs_free_dname(data);
136 spin_lock(&alias->d_lock);
Trond Myklebust609005c32008-01-28 19:42:59 -0500137 if (alias->d_inode != NULL &&
138 !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
Trond Myklebust565277f2007-10-15 18:17:53 -0400139 alias->d_fsdata = data;
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500140 alias->d_flags |= DCACHE_NFSFS_RENAMED;
Trond Myklebust565277f2007-10-15 18:17:53 -0400141 ret = 1;
142 }
143 spin_unlock(&alias->d_lock);
144 nfs_dec_sillycount(dir);
145 dput(alias);
146 return ret;
147 }
148 data->dir = igrab(dir);
149 if (!data->dir) {
150 nfs_dec_sillycount(dir);
151 return 0;
152 }
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400153 nfs_sb_active(dir->i_sb);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400154 data->args.fh = NFS_FH(dir);
155 nfs_fattr_init(&data->res.dir_attr);
156
Trond Myklebust5138fde2007-07-14 15:40:01 -0400157 NFS_PROTO(dir)->unlink_setup(&msg, dir);
Trond Myklebustc970aa82007-07-14 15:39:59 -0400158
Trond Myklebust5138fde2007-07-14 15:40:01 -0400159 task_setup_data.rpc_client = NFS_CLIENT(dir);
Trond Myklebustc970aa82007-07-14 15:39:59 -0400160 task = rpc_run_task(&task_setup_data);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400161 if (!IS_ERR(task))
162 rpc_put_task(task);
163 return 1;
Trond Myklebust565277f2007-10-15 18:17:53 -0400164}
165
166static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
167{
168 struct dentry *parent;
169 struct inode *dir;
170 int ret = 0;
171
172
173 parent = dget_parent(dentry);
174 if (parent == NULL)
175 goto out_free;
176 dir = parent->d_inode;
Trond Myklebust55b70a02007-10-21 12:02:22 -0400177 if (nfs_copy_dname(dentry, data) != 0)
Trond Myklebust565277f2007-10-15 18:17:53 -0400178 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;
187 }
188 spin_unlock(&dir->i_lock);
189 ret = nfs_do_call_unlink(parent, dir, data);
190out_dput:
191 dput(parent);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400192out_free:
Trond Myklebust565277f2007-10-15 18:17:53 -0400193 return ret;
194}
195
196void nfs_block_sillyrename(struct dentry *dentry)
197{
198 struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
199
200 wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
201}
202
203void nfs_unblock_sillyrename(struct dentry *dentry)
204{
205 struct inode *dir = dentry->d_inode;
206 struct nfs_inode *nfsi = NFS_I(dir);
207 struct nfs_unlinkdata *data;
208
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);
220 }
221 spin_unlock(&dir->i_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/**
225 * nfs_async_unlink - asynchronous unlinking of a file
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400226 * @dir: parent directory of dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * @dentry: dentry to unlink
228 */
229int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400230nfs_async_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400232 struct nfs_unlinkdata *data;
233 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Eric Sesterhennbd647542006-03-20 13:44:10 -0500235 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400236 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Trond Myklebust98a8e322008-03-12 12:25:28 -0400239 data->cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (IS_ERR(data->cred)) {
241 status = PTR_ERR(data->cred);
242 goto out_free;
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400245 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 spin_lock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400247 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
248 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400250 dentry->d_fsdata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 spin_unlock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400252 return 0;
253out_unlock:
254 spin_unlock(&dentry->d_lock);
255 put_rpccred(data->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256out_free:
257 kfree(data);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400258out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return status;
260}
261
262/**
263 * nfs_complete_unlink - Initialize completion of the sillydelete
264 * @dentry: dentry to delete
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400265 * @inode: inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 *
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.
270 */
271void
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400272nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400274 struct nfs_unlinkdata *data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 spin_lock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400277 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
278 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
279 data = dentry->d_fsdata;
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 spin_unlock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400282
283 if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
284 nfs_free_unlinkdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}