blob: 2f84adaad42784a8a07e6130884a827f3466134d [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;
Trond Myklebustd3468902010-04-16 16:22:50 -040026 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027};
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/**
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040030 * nfs_free_unlinkdata - release data from a sillydelete operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * @data: pointer to unlink structure.
32 */
33static void
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040034nfs_free_unlinkdata(struct nfs_unlinkdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040036 iput(data->dir);
37 put_rpccred(data->cred);
38 kfree(data->args.name.name);
39 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
42#define NAME_ALLOC_LEN(len) ((len+16) & ~15)
43/**
44 * nfs_copy_dname - copy dentry name to data structure
45 * @dentry: pointer to dentry
46 * @data: nfs_unlinkdata
47 */
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040048static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 char *str;
51 int len = dentry->d_name.len;
52
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040053 str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (!str)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040055 return -ENOMEM;
56 data->args.name.len = len;
57 data->args.name.name = str;
58 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Trond Myklebust565277f2007-10-15 18:17:53 -040061static void nfs_free_dname(struct nfs_unlinkdata *data)
62{
63 kfree(data->args.name.name);
64 data->args.name.name = NULL;
65 data->args.name.len = 0;
66}
67
68static void nfs_dec_sillycount(struct inode *dir)
69{
70 struct nfs_inode *nfsi = NFS_I(dir);
71 if (atomic_dec_return(&nfsi->silly_count) == 1)
72 wake_up(&nfsi->waitqueue);
73}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * nfs_async_unlink_done - Sillydelete post-processing
77 * @task: rpc_task of the sillydelete
78 *
79 * Do the directory attribute update.
80 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +010081static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040083 struct nfs_unlinkdata *data = calldata;
84 struct inode *dir = data->dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040086 if (!NFS_PROTO(dir)->unlink_done(task, dir))
Trond Myklebust0110ee12009-12-07 09:00:24 -050087 nfs_restart_rpc(task, NFS_SERVER(dir)->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/**
91 * nfs_async_unlink_release - Release the sillydelete data.
92 * @task: rpc_task of the sillydelete
93 *
94 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
95 * rpc_task would be freed too.
96 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +010097static void nfs_async_unlink_release(void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Trond Myklebust963d8fe2006-01-03 09:55:04 +010099 struct nfs_unlinkdata *data = calldata;
Trond Myklebust744d18d2008-07-27 18:03:19 -0400100 struct super_block *sb = data->dir->i_sb;
Trond Myklebust565277f2007-10-15 18:17:53 -0400101
102 nfs_dec_sillycount(data->dir);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400103 nfs_free_unlinkdata(data);
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400104 nfs_sb_deactive(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Andy Adamson472cfbd2009-04-01 09:22:24 -0400107#if defined(CONFIG_NFS_V4_1)
108void nfs_unlink_prepare(struct rpc_task *task, void *calldata)
109{
110 struct nfs_unlinkdata *data = calldata;
111 struct nfs_server *server = NFS_SERVER(data->dir);
112
Trond Myklebust035168a2010-06-16 09:52:26 -0400113 if (nfs4_setup_sequence(server, &data->args.seq_args,
Andy Adamson472cfbd2009-04-01 09:22:24 -0400114 &data->res.seq_res, 1, task))
115 return;
116 rpc_call_start(task);
117}
118#endif /* CONFIG_NFS_V4_1 */
119
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100120static const struct rpc_call_ops nfs_unlink_ops = {
121 .rpc_call_done = nfs_async_unlink_done,
122 .rpc_release = nfs_async_unlink_release,
Andy Adamson472cfbd2009-04-01 09:22:24 -0400123#if defined(CONFIG_NFS_V4_1)
124 .rpc_call_prepare = nfs_unlink_prepare,
125#endif /* CONFIG_NFS_V4_1 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100126};
127
Trond Myklebust565277f2007-10-15 18:17:53 -0400128static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400129{
Trond Myklebust5138fde2007-07-14 15:40:01 -0400130 struct rpc_message msg = {
131 .rpc_argp = &data->args,
132 .rpc_resp = &data->res,
133 .rpc_cred = data->cred,
134 };
Trond Myklebustc970aa82007-07-14 15:39:59 -0400135 struct rpc_task_setup task_setup_data = {
Trond Myklebust5138fde2007-07-14 15:40:01 -0400136 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400137 .callback_ops = &nfs_unlink_ops,
138 .callback_data = data,
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400139 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400140 .flags = RPC_TASK_ASYNC,
141 };
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400142 struct rpc_task *task;
Trond Myklebust565277f2007-10-15 18:17:53 -0400143 struct dentry *alias;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400144
Trond Myklebust565277f2007-10-15 18:17:53 -0400145 alias = d_lookup(parent, &data->args.name);
146 if (alias != NULL) {
147 int ret = 0;
Trond Myklebust609005c32008-01-28 19:42:59 -0500148
Trond Myklebust565277f2007-10-15 18:17:53 -0400149 /*
150 * Hey, we raced with lookup... See if we need to transfer
151 * the sillyrename information to the aliased dentry.
152 */
153 nfs_free_dname(data);
154 spin_lock(&alias->d_lock);
Trond Myklebust609005c32008-01-28 19:42:59 -0500155 if (alias->d_inode != NULL &&
156 !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
Trond Myklebust565277f2007-10-15 18:17:53 -0400157 alias->d_fsdata = data;
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500158 alias->d_flags |= DCACHE_NFSFS_RENAMED;
Trond Myklebust565277f2007-10-15 18:17:53 -0400159 ret = 1;
160 }
161 spin_unlock(&alias->d_lock);
162 nfs_dec_sillycount(dir);
163 dput(alias);
164 return ret;
165 }
166 data->dir = igrab(dir);
167 if (!data->dir) {
168 nfs_dec_sillycount(dir);
169 return 0;
170 }
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400171 nfs_sb_active(dir->i_sb);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400172 data->args.fh = NFS_FH(dir);
Trond Myklebustd3468902010-04-16 16:22:50 -0400173 nfs_fattr_init(data->res.dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400174
Trond Myklebust5138fde2007-07-14 15:40:01 -0400175 NFS_PROTO(dir)->unlink_setup(&msg, dir);
Trond Myklebustc970aa82007-07-14 15:39:59 -0400176
Trond Myklebust5138fde2007-07-14 15:40:01 -0400177 task_setup_data.rpc_client = NFS_CLIENT(dir);
Trond Myklebustc970aa82007-07-14 15:39:59 -0400178 task = rpc_run_task(&task_setup_data);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400179 if (!IS_ERR(task))
180 rpc_put_task(task);
181 return 1;
Trond Myklebust565277f2007-10-15 18:17:53 -0400182}
183
184static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
185{
186 struct dentry *parent;
187 struct inode *dir;
188 int ret = 0;
189
190
191 parent = dget_parent(dentry);
192 if (parent == NULL)
193 goto out_free;
194 dir = parent->d_inode;
Trond Myklebust55b70a02007-10-21 12:02:22 -0400195 if (nfs_copy_dname(dentry, data) != 0)
Trond Myklebust565277f2007-10-15 18:17:53 -0400196 goto out_dput;
197 /* Non-exclusive lock protects against concurrent lookup() calls */
198 spin_lock(&dir->i_lock);
199 if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
200 /* Deferred delete */
201 hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
202 spin_unlock(&dir->i_lock);
203 ret = 1;
204 goto out_dput;
205 }
206 spin_unlock(&dir->i_lock);
207 ret = nfs_do_call_unlink(parent, dir, data);
208out_dput:
209 dput(parent);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400210out_free:
Trond Myklebust565277f2007-10-15 18:17:53 -0400211 return ret;
212}
213
214void nfs_block_sillyrename(struct dentry *dentry)
215{
216 struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
217
218 wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
219}
220
221void nfs_unblock_sillyrename(struct dentry *dentry)
222{
223 struct inode *dir = dentry->d_inode;
224 struct nfs_inode *nfsi = NFS_I(dir);
225 struct nfs_unlinkdata *data;
226
227 atomic_inc(&nfsi->silly_count);
228 spin_lock(&dir->i_lock);
229 while (!hlist_empty(&nfsi->silly_list)) {
230 if (!atomic_inc_not_zero(&nfsi->silly_count))
231 break;
232 data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
233 hlist_del(&data->list);
234 spin_unlock(&dir->i_lock);
235 if (nfs_do_call_unlink(dentry, dir, data) == 0)
236 nfs_free_unlinkdata(data);
237 spin_lock(&dir->i_lock);
238 }
239 spin_unlock(&dir->i_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/**
243 * nfs_async_unlink - asynchronous unlinking of a file
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400244 * @dir: parent directory of dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * @dentry: dentry to unlink
246 */
247int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400248nfs_async_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400250 struct nfs_unlinkdata *data;
251 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Eric Sesterhennbd647542006-03-20 13:44:10 -0500253 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400254 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Trond Myklebust98a8e322008-03-12 12:25:28 -0400257 data->cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (IS_ERR(data->cred)) {
259 status = PTR_ERR(data->cred);
260 goto out_free;
261 }
Andy Adamson5f7dbd52009-04-01 09:22:05 -0400262 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebustd3468902010-04-16 16:22:50 -0400263 data->res.dir_attr = &data->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400265 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 spin_lock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400267 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
268 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400270 dentry->d_fsdata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 spin_unlock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400272 return 0;
273out_unlock:
274 spin_unlock(&dentry->d_lock);
275 put_rpccred(data->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276out_free:
277 kfree(data);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400278out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return status;
280}
281
282/**
283 * nfs_complete_unlink - Initialize completion of the sillydelete
284 * @dentry: dentry to delete
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400285 * @inode: inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 *
287 * Since we're most likely to be called by dentry_iput(), we
288 * only use the dentry to find the sillydelete. We then copy the name
289 * into the qstr.
290 */
291void
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400292nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400294 struct nfs_unlinkdata *data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 spin_lock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400297 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
298 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
299 data = dentry->d_fsdata;
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 spin_unlock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400302
303 if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
304 nfs_free_unlinkdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}