blob: bb939edd4c998cb98b7aeb56ae1aa308e4d9009d [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>
Jeff Layton779c5172010-09-17 17:31:30 -040016#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Steve Dicksonef818a22007-11-08 04:05:04 -050018#include "internal.h"
Andy Adamson472cfbd2009-04-01 09:22:24 -040019#include "nfs4_fs.h"
Jeff Layton779c5172010-09-17 17:31:30 -040020#include "iostat.h"
21#include "delegation.h"
Steve Dicksonef818a22007-11-08 04:05:04 -050022
Trond Myklebust70ded202013-08-21 12:08:45 -040023#include "nfstrace.h"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/**
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040026 * nfs_free_unlinkdata - release data from a sillydelete operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * @data: pointer to unlink structure.
28 */
29static void
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040030nfs_free_unlinkdata(struct nfs_unlinkdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040032 iput(data->dir);
33 put_rpccred(data->cred);
34 kfree(data->args.name.name);
35 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38#define NAME_ALLOC_LEN(len) ((len+16) & ~15)
39/**
40 * nfs_copy_dname - copy dentry name to data structure
41 * @dentry: pointer to dentry
42 * @data: nfs_unlinkdata
43 */
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040044static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 char *str;
47 int len = dentry->d_name.len;
48
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040049 str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (!str)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040051 return -ENOMEM;
52 data->args.name.len = len;
53 data->args.name.name = str;
54 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Trond Myklebust565277f2007-10-15 18:17:53 -040057static void nfs_free_dname(struct nfs_unlinkdata *data)
58{
59 kfree(data->args.name.name);
60 data->args.name.name = NULL;
61 data->args.name.len = 0;
62}
63
64static void nfs_dec_sillycount(struct inode *dir)
65{
66 struct nfs_inode *nfsi = NFS_I(dir);
67 if (atomic_dec_return(&nfsi->silly_count) == 1)
68 wake_up(&nfsi->waitqueue);
69}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * nfs_async_unlink_done - Sillydelete post-processing
73 * @task: rpc_task of the sillydelete
74 *
75 * Do the directory attribute update.
76 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +010077static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040079 struct nfs_unlinkdata *data = calldata;
80 struct inode *dir = data->dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Trond Myklebust70ded202013-08-21 12:08:45 -040082 trace_nfs_sillyrename_unlink(data, task->tk_status);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040083 if (!NFS_PROTO(dir)->unlink_done(task, dir))
Trond Myklebustd00c5d42011-10-19 12:17:29 -070084 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/**
88 * nfs_async_unlink_release - Release the sillydelete data.
89 * @task: rpc_task of the sillydelete
90 *
91 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
92 * rpc_task would be freed too.
93 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +010094static void nfs_async_unlink_release(void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Trond Myklebust963d8fe2006-01-03 09:55:04 +010096 struct nfs_unlinkdata *data = calldata;
Trond Myklebust744d18d2008-07-27 18:03:19 -040097 struct super_block *sb = data->dir->i_sb;
Trond Myklebust565277f2007-10-15 18:17:53 -040098
99 nfs_dec_sillycount(data->dir);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400100 nfs_free_unlinkdata(data);
Trond Myklebust322b2b92013-01-11 16:39:51 -0500101 nfs_sb_deactive(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Trond Myklebust17280172012-03-11 13:11:00 -0400104static void nfs_unlink_prepare(struct rpc_task *task, void *calldata)
Andy Adamson472cfbd2009-04-01 09:22:24 -0400105{
106 struct nfs_unlinkdata *data = calldata;
Bryan Schumaker34e137c2012-03-19 14:54:41 -0400107 NFS_PROTO(data->dir)->unlink_rpc_prepare(task, data);
Andy Adamson472cfbd2009-04-01 09:22:24 -0400108}
Andy Adamson472cfbd2009-04-01 09:22:24 -0400109
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100110static const struct rpc_call_ops nfs_unlink_ops = {
111 .rpc_call_done = nfs_async_unlink_done,
112 .rpc_release = nfs_async_unlink_release,
Andy Adamson472cfbd2009-04-01 09:22:24 -0400113 .rpc_call_prepare = nfs_unlink_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100114};
115
Trond Myklebust565277f2007-10-15 18:17:53 -0400116static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400117{
Trond Myklebust5138fde2007-07-14 15:40:01 -0400118 struct rpc_message msg = {
119 .rpc_argp = &data->args,
120 .rpc_resp = &data->res,
121 .rpc_cred = data->cred,
122 };
Trond Myklebustc970aa82007-07-14 15:39:59 -0400123 struct rpc_task_setup task_setup_data = {
Trond Myklebust5138fde2007-07-14 15:40:01 -0400124 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400125 .callback_ops = &nfs_unlink_ops,
126 .callback_data = data,
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400127 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -0400128 .flags = RPC_TASK_ASYNC,
129 };
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400130 struct rpc_task *task;
Trond Myklebust565277f2007-10-15 18:17:53 -0400131 struct dentry *alias;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400132
Trond Myklebust565277f2007-10-15 18:17:53 -0400133 alias = d_lookup(parent, &data->args.name);
134 if (alias != NULL) {
Jeff Layton73ca1002011-07-18 11:26:30 -0400135 int ret;
Al Virob1942c52011-03-16 05:44:14 -0400136 void *devname_garbage = NULL;
Trond Myklebust609005c2008-01-28 19:42:59 -0500137
Trond Myklebust565277f2007-10-15 18:17:53 -0400138 /*
139 * Hey, we raced with lookup... See if we need to transfer
140 * the sillyrename information to the aliased dentry.
141 */
142 nfs_free_dname(data);
Jeff Layton73ca1002011-07-18 11:26:30 -0400143 ret = nfs_copy_dname(alias, data);
Trond Myklebust565277f2007-10-15 18:17:53 -0400144 spin_lock(&alias->d_lock);
Jeff Layton73ca1002011-07-18 11:26:30 -0400145 if (ret == 0 && alias->d_inode != NULL &&
Trond Myklebust609005c2008-01-28 19:42:59 -0500146 !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
Al Virob1942c52011-03-16 05:44:14 -0400147 devname_garbage = alias->d_fsdata;
Trond Myklebust565277f2007-10-15 18:17:53 -0400148 alias->d_fsdata = data;
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500149 alias->d_flags |= DCACHE_NFSFS_RENAMED;
Trond Myklebust565277f2007-10-15 18:17:53 -0400150 ret = 1;
Jeff Layton73ca1002011-07-18 11:26:30 -0400151 } else
152 ret = 0;
Trond Myklebust565277f2007-10-15 18:17:53 -0400153 spin_unlock(&alias->d_lock);
154 nfs_dec_sillycount(dir);
155 dput(alias);
Al Virob1942c52011-03-16 05:44:14 -0400156 /*
157 * If we'd displaced old cached devname, free it. At that
158 * point dentry is definitely not a root, so we won't need
159 * that anymore.
160 */
Jeff Layton73ca1002011-07-18 11:26:30 -0400161 kfree(devname_garbage);
Trond Myklebust565277f2007-10-15 18:17:53 -0400162 return ret;
163 }
164 data->dir = igrab(dir);
165 if (!data->dir) {
166 nfs_dec_sillycount(dir);
167 return 0;
168 }
Trond Myklebust1daef0a2008-07-27 18:19:01 -0400169 nfs_sb_active(dir->i_sb);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400170 data->args.fh = NFS_FH(dir);
Trond Myklebustd3468902010-04-16 16:22:50 -0400171 nfs_fattr_init(data->res.dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400172
Trond Myklebust5138fde2007-07-14 15:40:01 -0400173 NFS_PROTO(dir)->unlink_setup(&msg, dir);
Trond Myklebustc970aa82007-07-14 15:39:59 -0400174
Trond Myklebust5138fde2007-07-14 15:40:01 -0400175 task_setup_data.rpc_client = NFS_CLIENT(dir);
Trond Myklebustc970aa82007-07-14 15:39:59 -0400176 task = rpc_run_task(&task_setup_data);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400177 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -0800178 rpc_put_task_async(task);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400179 return 1;
Trond Myklebust565277f2007-10-15 18:17:53 -0400180}
181
182static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
183{
184 struct dentry *parent;
185 struct inode *dir;
186 int ret = 0;
187
188
189 parent = dget_parent(dentry);
190 if (parent == NULL)
191 goto out_free;
192 dir = parent->d_inode;
Trond Myklebust565277f2007-10-15 18:17:53 -0400193 /* Non-exclusive lock protects against concurrent lookup() calls */
194 spin_lock(&dir->i_lock);
195 if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
196 /* Deferred delete */
197 hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
198 spin_unlock(&dir->i_lock);
199 ret = 1;
200 goto out_dput;
201 }
202 spin_unlock(&dir->i_lock);
203 ret = nfs_do_call_unlink(parent, dir, data);
204out_dput:
205 dput(parent);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400206out_free:
Trond Myklebust565277f2007-10-15 18:17:53 -0400207 return ret;
208}
209
Trond Myklebustba6c0592013-08-30 12:24:25 -0400210void nfs_wait_on_sillyrename(struct dentry *dentry)
211{
212 struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
213
214 wait_event(nfsi->waitqueue, atomic_read(&nfsi->silly_count) <= 1);
215}
216
Trond Myklebust565277f2007-10-15 18:17:53 -0400217void nfs_block_sillyrename(struct dentry *dentry)
218{
219 struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
220
221 wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
222}
223
224void nfs_unblock_sillyrename(struct dentry *dentry)
225{
226 struct inode *dir = dentry->d_inode;
227 struct nfs_inode *nfsi = NFS_I(dir);
228 struct nfs_unlinkdata *data;
229
230 atomic_inc(&nfsi->silly_count);
231 spin_lock(&dir->i_lock);
232 while (!hlist_empty(&nfsi->silly_list)) {
233 if (!atomic_inc_not_zero(&nfsi->silly_count))
234 break;
235 data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
236 hlist_del(&data->list);
237 spin_unlock(&dir->i_lock);
238 if (nfs_do_call_unlink(dentry, dir, data) == 0)
239 nfs_free_unlinkdata(data);
240 spin_lock(&dir->i_lock);
241 }
242 spin_unlock(&dir->i_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/**
246 * nfs_async_unlink - asynchronous unlinking of a file
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400247 * @dir: parent directory of dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * @dentry: dentry to unlink
249 */
Jeff Layton779c5172010-09-17 17:31:30 -0400250static int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400251nfs_async_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400253 struct nfs_unlinkdata *data;
254 int status = -ENOMEM;
Al Virob1942c52011-03-16 05:44:14 -0400255 void *devname_garbage = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Eric Sesterhennbd647542006-03-20 13:44:10 -0500257 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400258 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Trond Myklebust98a8e322008-03-12 12:25:28 -0400261 data->cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (IS_ERR(data->cred)) {
263 status = PTR_ERR(data->cred);
264 goto out_free;
265 }
Trond Myklebustd3468902010-04-16 16:22:50 -0400266 data->res.dir_attr = &data->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400268 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spin_lock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400270 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
271 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
Al Virob1942c52011-03-16 05:44:14 -0400273 devname_garbage = dentry->d_fsdata;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400274 dentry->d_fsdata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 spin_unlock(&dentry->d_lock);
Al Virob1942c52011-03-16 05:44:14 -0400276 /*
277 * If we'd displaced old cached devname, free it. At that
278 * point dentry is definitely not a root, so we won't need
279 * that anymore.
280 */
Tim Gardner96aa1542013-02-12 13:03:42 -0700281 kfree(devname_garbage);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400282 return 0;
283out_unlock:
284 spin_unlock(&dentry->d_lock);
285 put_rpccred(data->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286out_free:
287 kfree(data);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400288out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return status;
290}
291
292/**
293 * nfs_complete_unlink - Initialize completion of the sillydelete
294 * @dentry: dentry to delete
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400295 * @inode: inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * Since we're most likely to be called by dentry_iput(), we
298 * only use the dentry to find the sillydelete. We then copy the name
299 * into the qstr.
300 */
301void
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400302nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400304 struct nfs_unlinkdata *data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 spin_lock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400307 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
308 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
309 data = dentry->d_fsdata;
Al Virob1942c52011-03-16 05:44:14 -0400310 dentry->d_fsdata = NULL;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 spin_unlock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400313
314 if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
315 nfs_free_unlinkdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
Jeff Layton779c5172010-09-17 17:31:30 -0400317
Jeff Laytond3d41522010-09-17 17:31:57 -0400318/* Cancel a queued async unlink. Called when a sillyrename run fails. */
319static void
320nfs_cancel_async_unlink(struct dentry *dentry)
321{
322 spin_lock(&dentry->d_lock);
323 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
324 struct nfs_unlinkdata *data = dentry->d_fsdata;
325
326 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
Al Virob1942c52011-03-16 05:44:14 -0400327 dentry->d_fsdata = NULL;
Jeff Laytond3d41522010-09-17 17:31:57 -0400328 spin_unlock(&dentry->d_lock);
329 nfs_free_unlinkdata(data);
330 return;
331 }
332 spin_unlock(&dentry->d_lock);
333}
334
Jeff Laytond3d41522010-09-17 17:31:57 -0400335/**
336 * nfs_async_rename_done - Sillyrename post-processing
337 * @task: rpc_task of the sillyrename
338 * @calldata: nfs_renamedata for the sillyrename
339 *
340 * Do the directory attribute updates and the d_move
341 */
342static void nfs_async_rename_done(struct rpc_task *task, void *calldata)
343{
344 struct nfs_renamedata *data = calldata;
345 struct inode *old_dir = data->old_dir;
346 struct inode *new_dir = data->new_dir;
Jeff Layton73ca1002011-07-18 11:26:30 -0400347 struct dentry *old_dentry = data->old_dentry;
Jeff Laytond3d41522010-09-17 17:31:57 -0400348
Trond Myklebust70ded202013-08-21 12:08:45 -0400349 trace_nfs_sillyrename_rename(old_dir, old_dentry,
350 new_dir, data->new_dentry, task->tk_status);
Jeff Laytond3d41522010-09-17 17:31:57 -0400351 if (!NFS_PROTO(old_dir)->rename_done(task, old_dir, new_dir)) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700352 rpc_restart_call_prepare(task);
Jeff Laytond3d41522010-09-17 17:31:57 -0400353 return;
354 }
355
Trond Myklebust5a7a6132013-02-22 12:53:43 -0500356 if (task->tk_status != 0)
Jeff Layton73ca1002011-07-18 11:26:30 -0400357 nfs_cancel_async_unlink(old_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -0400358}
359
360/**
361 * nfs_async_rename_release - Release the sillyrename data.
362 * @calldata: the struct nfs_renamedata to be released
363 */
364static void nfs_async_rename_release(void *calldata)
365{
366 struct nfs_renamedata *data = calldata;
367 struct super_block *sb = data->old_dir->i_sb;
368
369 if (data->old_dentry->d_inode)
370 nfs_mark_for_revalidate(data->old_dentry->d_inode);
371
372 dput(data->old_dentry);
373 dput(data->new_dentry);
374 iput(data->old_dir);
375 iput(data->new_dir);
376 nfs_sb_deactive(sb);
377 put_rpccred(data->cred);
378 kfree(data);
379}
380
Jeff Laytond3d41522010-09-17 17:31:57 -0400381static void nfs_rename_prepare(struct rpc_task *task, void *calldata)
382{
383 struct nfs_renamedata *data = calldata;
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -0400384 NFS_PROTO(data->old_dir)->rename_rpc_prepare(task, data);
Jeff Laytond3d41522010-09-17 17:31:57 -0400385}
Jeff Laytond3d41522010-09-17 17:31:57 -0400386
387static const struct rpc_call_ops nfs_rename_ops = {
388 .rpc_call_done = nfs_async_rename_done,
389 .rpc_release = nfs_async_rename_release,
Jeff Laytond3d41522010-09-17 17:31:57 -0400390 .rpc_call_prepare = nfs_rename_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -0400391};
392
393/**
394 * nfs_async_rename - perform an asynchronous rename operation
395 * @old_dir: directory that currently holds the dentry to be renamed
396 * @new_dir: target directory for the rename
397 * @old_dentry: original dentry to be renamed
398 * @new_dentry: dentry to which the old_dentry should be renamed
399 *
400 * It's expected that valid references to the dentries and inodes are held
401 */
402static struct rpc_task *
403nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
404 struct dentry *old_dentry, struct dentry *new_dentry)
405{
406 struct nfs_renamedata *data;
407 struct rpc_message msg = { };
408 struct rpc_task_setup task_setup_data = {
409 .rpc_message = &msg,
410 .callback_ops = &nfs_rename_ops,
411 .workqueue = nfsiod_workqueue,
412 .rpc_client = NFS_CLIENT(old_dir),
413 .flags = RPC_TASK_ASYNC,
414 };
Jeff Laytond3d41522010-09-17 17:31:57 -0400415
Benny Halevydfb4f3092010-09-24 09:17:01 -0400416 data = kzalloc(sizeof(*data), GFP_KERNEL);
Jeff Laytond3d41522010-09-17 17:31:57 -0400417 if (data == NULL)
418 return ERR_PTR(-ENOMEM);
Trond Myklebust1174dd12010-12-21 10:52:24 -0500419 task_setup_data.callback_data = data;
Jeff Laytond3d41522010-09-17 17:31:57 -0400420
421 data->cred = rpc_lookup_cred();
422 if (IS_ERR(data->cred)) {
Trond Myklebustf7732d62010-09-21 16:52:40 -0400423 struct rpc_task *task = ERR_CAST(data->cred);
Jeff Laytond3d41522010-09-17 17:31:57 -0400424 kfree(data);
425 return task;
426 }
427
428 msg.rpc_argp = &data->args;
429 msg.rpc_resp = &data->res;
430 msg.rpc_cred = data->cred;
431
432 /* set up nfs_renamedata */
433 data->old_dir = old_dir;
Al Viroa4118ee2010-10-27 11:00:08 -0400434 ihold(old_dir);
Jeff Laytond3d41522010-09-17 17:31:57 -0400435 data->new_dir = new_dir;
Al Viroa4118ee2010-10-27 11:00:08 -0400436 ihold(new_dir);
Jeff Laytond3d41522010-09-17 17:31:57 -0400437 data->old_dentry = dget(old_dentry);
438 data->new_dentry = dget(new_dentry);
439 nfs_fattr_init(&data->old_fattr);
440 nfs_fattr_init(&data->new_fattr);
441
442 /* set up nfs_renameargs */
443 data->args.old_dir = NFS_FH(old_dir);
444 data->args.old_name = &old_dentry->d_name;
445 data->args.new_dir = NFS_FH(new_dir);
446 data->args.new_name = &new_dentry->d_name;
447
448 /* set up nfs_renameres */
449 data->res.old_fattr = &data->old_fattr;
450 data->res.new_fattr = &data->new_fattr;
451
452 nfs_sb_active(old_dir->i_sb);
453
454 NFS_PROTO(data->old_dir)->rename_setup(&msg, old_dir);
455
Trond Myklebustf7732d62010-09-21 16:52:40 -0400456 return rpc_run_task(&task_setup_data);
Jeff Laytond3d41522010-09-17 17:31:57 -0400457}
458
Trond Myklebustc2dd1372013-08-21 19:54:08 -0400459#define SILLYNAME_PREFIX ".nfs"
460#define SILLYNAME_PREFIX_LEN ((unsigned)sizeof(SILLYNAME_PREFIX) - 1)
461#define SILLYNAME_FILEID_LEN ((unsigned)sizeof(u64) << 1)
462#define SILLYNAME_COUNTER_LEN ((unsigned)sizeof(unsigned int) << 1)
463#define SILLYNAME_LEN (SILLYNAME_PREFIX_LEN + \
464 SILLYNAME_FILEID_LEN + \
465 SILLYNAME_COUNTER_LEN)
466
Jeff Layton779c5172010-09-17 17:31:30 -0400467/**
468 * nfs_sillyrename - Perform a silly-rename of a dentry
469 * @dir: inode of directory that contains dentry
470 * @dentry: dentry to be sillyrenamed
471 *
472 * NFSv2/3 is stateless and the server doesn't know when the client is
473 * holding a file open. To prevent application problems when a file is
474 * unlinked while it's still open, the client performs a "silly-rename".
475 * That is, it renames the file to a hidden file in the same directory,
476 * and only performs the unlink once the last reference to it is put.
477 *
478 * The final cleanup is done during dentry_iput.
J. Bruce Fields674e4052011-07-15 19:09:08 -0400479 *
480 * (Note: NFSv4 is stateful, and has opens, so in theory an NFSv4 server
481 * could take responsibility for keeping open files referenced. The server
482 * would also need to ensure that opened-but-deleted files were kept over
483 * reboots. However, we may not assume a server does so. (RFC 5661
484 * does provide an OPEN4_RESULT_PRESERVE_UNLINKED flag that a server can
485 * use to advertise that it does this; some day we may take advantage of
486 * it.))
Jeff Layton779c5172010-09-17 17:31:30 -0400487 */
488int
489nfs_sillyrename(struct inode *dir, struct dentry *dentry)
490{
491 static unsigned int sillycounter;
Trond Myklebustc2dd1372013-08-21 19:54:08 -0400492 unsigned char silly[SILLYNAME_LEN + 1];
493 unsigned long long fileid;
Jeff Layton779c5172010-09-17 17:31:30 -0400494 struct dentry *sdentry;
Jeff Laytond3d41522010-09-17 17:31:57 -0400495 struct rpc_task *task;
Jeff Layton779c5172010-09-17 17:31:30 -0400496 int error = -EIO;
497
498 dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
499 dentry->d_parent->d_name.name, dentry->d_name.name,
Al Viro84d08fa2013-07-05 18:59:33 +0400500 d_count(dentry));
Jeff Layton779c5172010-09-17 17:31:30 -0400501 nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
502
503 /*
504 * We don't allow a dentry to be silly-renamed twice.
505 */
506 error = -EBUSY;
507 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
508 goto out;
509
Trond Myklebustc2dd1372013-08-21 19:54:08 -0400510 fileid = NFS_FILEID(dentry->d_inode);
Jeff Layton779c5172010-09-17 17:31:30 -0400511
512 /* Return delegation in anticipation of the rename */
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400513 NFS_PROTO(dentry->d_inode)->return_delegation(dentry->d_inode);
Jeff Layton779c5172010-09-17 17:31:30 -0400514
515 sdentry = NULL;
516 do {
Trond Myklebustc2dd1372013-08-21 19:54:08 -0400517 int slen;
Jeff Layton779c5172010-09-17 17:31:30 -0400518 dput(sdentry);
519 sillycounter++;
Trond Myklebustc2dd1372013-08-21 19:54:08 -0400520 slen = scnprintf(silly, sizeof(silly),
521 SILLYNAME_PREFIX "%0*llx%0*x",
522 SILLYNAME_FILEID_LEN, fileid,
523 SILLYNAME_COUNTER_LEN, sillycounter);
Jeff Layton779c5172010-09-17 17:31:30 -0400524
525 dfprintk(VFS, "NFS: trying to rename %s to %s\n",
526 dentry->d_name.name, silly);
527
528 sdentry = lookup_one_len(silly, dentry->d_parent, slen);
529 /*
530 * N.B. Better to return EBUSY here ... it could be
531 * dangerous to delete the file while it's in use.
532 */
533 if (IS_ERR(sdentry))
534 goto out;
535 } while (sdentry->d_inode != NULL); /* need negative lookup */
536
Jeff Laytond3d41522010-09-17 17:31:57 -0400537 /* queue unlink first. Can't do this from rpc_release as it
538 * has to allocate memory
539 */
540 error = nfs_async_unlink(dir, dentry);
541 if (error)
542 goto out_dput;
543
Jeff Layton73ca1002011-07-18 11:26:30 -0400544 /* populate unlinkdata with the right dname */
545 error = nfs_copy_dname(sdentry,
546 (struct nfs_unlinkdata *)dentry->d_fsdata);
547 if (error) {
548 nfs_cancel_async_unlink(dentry);
549 goto out_dput;
550 }
551
Jeff Laytond3d41522010-09-17 17:31:57 -0400552 /* run the rename task, undo unlink if it fails */
553 task = nfs_async_rename(dir, dir, dentry, sdentry);
554 if (IS_ERR(task)) {
555 error = -EBUSY;
556 nfs_cancel_async_unlink(dentry);
557 goto out_dput;
Jeff Layton779c5172010-09-17 17:31:30 -0400558 }
Jeff Laytond3d41522010-09-17 17:31:57 -0400559
560 /* wait for the RPC task to complete, unless a SIGKILL intervenes */
561 error = rpc_wait_for_completion_task(task);
562 if (error == 0)
563 error = task->tk_status;
Trond Myklebust5a7a6132013-02-22 12:53:43 -0500564 switch (error) {
565 case 0:
566 /* The rename succeeded */
567 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
568 d_move(dentry, sdentry);
569 break;
570 case -ERESTARTSYS:
571 /* The result of the rename is unknown. Play it safe by
572 * forcing a new lookup */
573 d_drop(dentry);
574 d_drop(sdentry);
575 }
Jeff Laytond3d41522010-09-17 17:31:57 -0400576 rpc_put_task(task);
577out_dput:
Jeff Layton779c5172010-09-17 17:31:30 -0400578 dput(sdentry);
579out:
580 return error;
581}