blob: 6da3d3ff6edd652fdcaf5934ea5056acdd062135 [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"
Andy Adamson472cfbd2009-04-01 09:22:24 -040018#include "nfs4_fs.h"
Steve Dicksonef818a22007-11-08 04:05:04 -050019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020struct nfs_unlinkdata {
Trond Myklebust565277f2007-10-15 18:17:53 -040021 struct hlist_node list;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040022 struct nfs_removeargs args;
23 struct nfs_removeres res;
24 struct inode *dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/**
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040029 * nfs_free_unlinkdata - release data from a sillydelete operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * @data: pointer to unlink structure.
31 */
32static void
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040033nfs_free_unlinkdata(struct nfs_unlinkdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040035 iput(data->dir);
36 put_rpccred(data->cred);
37 kfree(data->args.name.name);
38 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41#define NAME_ALLOC_LEN(len) ((len+16) & ~15)
42/**
43 * nfs_copy_dname - copy dentry name to data structure
44 * @dentry: pointer to dentry
45 * @data: nfs_unlinkdata
46 */
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040047static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 char *str;
50 int len = dentry->d_name.len;
51
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040052 str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (!str)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040054 return -ENOMEM;
55 data->args.name.len = len;
56 data->args.name.name = str;
57 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Trond Myklebust565277f2007-10-15 18:17:53 -040060static void nfs_free_dname(struct nfs_unlinkdata *data)
61{
62 kfree(data->args.name.name);
63 data->args.name.name = NULL;
64 data->args.name.len = 0;
65}
66
67static void nfs_dec_sillycount(struct inode *dir)
68{
69 struct nfs_inode *nfsi = NFS_I(dir);
70 if (atomic_dec_return(&nfsi->silly_count) == 1)
71 wake_up(&nfsi->waitqueue);
72}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * nfs_async_unlink_done - Sillydelete post-processing
76 * @task: rpc_task of the sillydelete
77 *
78 * Do the directory attribute update.
79 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +010080static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040082 struct nfs_unlinkdata *data = calldata;
83 struct inode *dir = data->dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040085 if (!NFS_PROTO(dir)->unlink_done(task, dir))
Trond Myklebust0110ee12009-12-07 09:00:24 -050086 nfs_restart_rpc(task, NFS_SERVER(dir)->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/**
90 * nfs_async_unlink_release - Release the sillydelete data.
91 * @task: rpc_task of the sillydelete
92 *
93 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
94 * rpc_task would be freed too.
95 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +010096static void nfs_async_unlink_release(void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Trond Myklebust963d8fe2006-01-03 09:55:04 +010098 struct nfs_unlinkdata *data = calldata;
Trond Myklebust744d18d2008-07-27 18:03:19 -040099 struct super_block *sb = data->dir->i_sb;
Trond Myklebust565277f2007-10-15 18:17:53 -0400100
101 nfs_dec_sillycount(data->dir);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400102 nfs_free_unlinkdata(data);
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400103 nfs_sb_deactive(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Andy Adamson472cfbd2009-04-01 09:22:24 -0400106#if defined(CONFIG_NFS_V4_1)
107void nfs_unlink_prepare(struct rpc_task *task, void *calldata)
108{
109 struct nfs_unlinkdata *data = calldata;
110 struct nfs_server *server = NFS_SERVER(data->dir);
111
112 if (nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
113 &data->res.seq_res, 1, task))
114 return;
115 rpc_call_start(task);
116}
117#endif /* CONFIG_NFS_V4_1 */
118
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100119static const struct rpc_call_ops nfs_unlink_ops = {
120 .rpc_call_done = nfs_async_unlink_done,
121 .rpc_release = nfs_async_unlink_release,
Andy Adamson472cfbd2009-04-01 09:22:24 -0400122#if defined(CONFIG_NFS_V4_1)
123 .rpc_call_prepare = nfs_unlink_prepare,
124#endif /* CONFIG_NFS_V4_1 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100125};
126
Trond Myklebust565277f2007-10-15 18:17:53 -0400127static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400128{
Trond Myklebust5138fde2007-07-14 15:40:01 -0400129 struct rpc_message msg = {
130 .rpc_argp = &data->args,
131 .rpc_resp = &data->res,
132 .rpc_cred = data->cred,
133 };
Trond Myklebustc970aa82007-07-14 15:39:59 -0400134 struct rpc_task_setup task_setup_data = {
Trond Myklebust5138fde2007-07-14 15:40:01 -0400135 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400136 .callback_ops = &nfs_unlink_ops,
137 .callback_data = data,
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400138 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400139 .flags = RPC_TASK_ASYNC,
140 };
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400141 struct rpc_task *task;
Trond Myklebust565277f2007-10-15 18:17:53 -0400142 struct dentry *alias;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400143
Trond Myklebust565277f2007-10-15 18:17:53 -0400144 alias = d_lookup(parent, &data->args.name);
145 if (alias != NULL) {
146 int ret = 0;
Trond Myklebust609005c32008-01-28 19:42:59 -0500147
Trond Myklebust565277f2007-10-15 18:17:53 -0400148 /*
149 * Hey, we raced with lookup... See if we need to transfer
150 * the sillyrename information to the aliased dentry.
151 */
152 nfs_free_dname(data);
153 spin_lock(&alias->d_lock);
Trond Myklebust609005c32008-01-28 19:42:59 -0500154 if (alias->d_inode != NULL &&
155 !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
Trond Myklebust565277f2007-10-15 18:17:53 -0400156 alias->d_fsdata = data;
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500157 alias->d_flags |= DCACHE_NFSFS_RENAMED;
Trond Myklebust565277f2007-10-15 18:17:53 -0400158 ret = 1;
159 }
160 spin_unlock(&alias->d_lock);
161 nfs_dec_sillycount(dir);
162 dput(alias);
163 return ret;
164 }
165 data->dir = igrab(dir);
166 if (!data->dir) {
167 nfs_dec_sillycount(dir);
168 return 0;
169 }
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400170 nfs_sb_active(dir->i_sb);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400171 data->args.fh = NFS_FH(dir);
172 nfs_fattr_init(&data->res.dir_attr);
173
Trond Myklebust5138fde2007-07-14 15:40:01 -0400174 NFS_PROTO(dir)->unlink_setup(&msg, dir);
Trond Myklebustc970aa82007-07-14 15:39:59 -0400175
Trond Myklebust5138fde2007-07-14 15:40:01 -0400176 task_setup_data.rpc_client = NFS_CLIENT(dir);
Trond Myklebustc970aa82007-07-14 15:39:59 -0400177 task = rpc_run_task(&task_setup_data);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400178 if (!IS_ERR(task))
179 rpc_put_task(task);
180 return 1;
Trond Myklebust565277f2007-10-15 18:17:53 -0400181}
182
183static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
184{
185 struct dentry *parent;
186 struct inode *dir;
187 int ret = 0;
188
189
190 parent = dget_parent(dentry);
191 if (parent == NULL)
192 goto out_free;
193 dir = parent->d_inode;
Trond Myklebust55b70a02007-10-21 12:02:22 -0400194 if (nfs_copy_dname(dentry, data) != 0)
Trond Myklebust565277f2007-10-15 18:17:53 -0400195 goto out_dput;
196 /* Non-exclusive lock protects against concurrent lookup() calls */
197 spin_lock(&dir->i_lock);
198 if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
199 /* Deferred delete */
200 hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
201 spin_unlock(&dir->i_lock);
202 ret = 1;
203 goto out_dput;
204 }
205 spin_unlock(&dir->i_lock);
206 ret = nfs_do_call_unlink(parent, dir, data);
207out_dput:
208 dput(parent);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400209out_free:
Trond Myklebust565277f2007-10-15 18:17:53 -0400210 return ret;
211}
212
213void nfs_block_sillyrename(struct dentry *dentry)
214{
215 struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
216
217 wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
218}
219
220void nfs_unblock_sillyrename(struct dentry *dentry)
221{
222 struct inode *dir = dentry->d_inode;
223 struct nfs_inode *nfsi = NFS_I(dir);
224 struct nfs_unlinkdata *data;
225
226 atomic_inc(&nfsi->silly_count);
227 spin_lock(&dir->i_lock);
228 while (!hlist_empty(&nfsi->silly_list)) {
229 if (!atomic_inc_not_zero(&nfsi->silly_count))
230 break;
231 data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
232 hlist_del(&data->list);
233 spin_unlock(&dir->i_lock);
234 if (nfs_do_call_unlink(dentry, dir, data) == 0)
235 nfs_free_unlinkdata(data);
236 spin_lock(&dir->i_lock);
237 }
238 spin_unlock(&dir->i_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/**
242 * nfs_async_unlink - asynchronous unlinking of a file
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400243 * @dir: parent directory of dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * @dentry: dentry to unlink
245 */
246int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400247nfs_async_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400249 struct nfs_unlinkdata *data;
250 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Eric Sesterhennbd647542006-03-20 13:44:10 -0500252 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400253 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Trond Myklebust98a8e322008-03-12 12:25:28 -0400256 data->cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (IS_ERR(data->cred)) {
258 status = PTR_ERR(data->cred);
259 goto out_free;
260 }
Andy Adamson5f7dbd52009-04-01 09:22:05 -0400261 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400263 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 spin_lock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400265 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
266 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400268 dentry->d_fsdata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spin_unlock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400270 return 0;
271out_unlock:
272 spin_unlock(&dentry->d_lock);
273 put_rpccred(data->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274out_free:
275 kfree(data);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400276out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return status;
278}
279
280/**
281 * nfs_complete_unlink - Initialize completion of the sillydelete
282 * @dentry: dentry to delete
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400283 * @inode: inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 *
285 * Since we're most likely to be called by dentry_iput(), we
286 * only use the dentry to find the sillydelete. We then copy the name
287 * into the qstr.
288 */
289void
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400290nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400292 struct nfs_unlinkdata *data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 spin_lock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400295 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
296 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
297 data = dentry->d_fsdata;
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 spin_unlock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400300
301 if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
302 nfs_free_unlinkdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}