2 * linux/fs/nfs/unlink.c
4 * nfs sillydelete handling
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/swait.h>
16 #include <linux/namei.h>
17 #include <linux/fsnotify.h>
22 #include "delegation.h"
27 * nfs_free_unlinkdata - release data from a sillydelete operation.
28 * @data: pointer to unlink structure.
31 nfs_free_unlinkdata(struct nfs_unlinkdata *data)
33 put_rpccred(data->cred);
34 kfree(data->args.name.name);
39 * nfs_async_unlink_done - Sillydelete post-processing
40 * @task: rpc_task of the sillydelete
42 * Do the directory attribute update.
44 static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
46 struct nfs_unlinkdata *data = calldata;
47 struct inode *dir = d_inode(data->dentry->d_parent);
49 trace_nfs_sillyrename_unlink(data, task->tk_status);
50 if (!NFS_PROTO(dir)->unlink_done(task, dir))
51 rpc_restart_call_prepare(task);
54 #ifdef CONFIG_PREEMPT_RT_BASE
55 static void nfs_down_anon(struct semaphore *sema)
60 static void nfs_up_anon(struct semaphore *sema)
66 static void nfs_down_anon(struct rw_semaphore *rwsem)
68 down_read_non_owner(rwsem);
71 static void nfs_up_anon(struct rw_semaphore *rwsem)
73 up_read_non_owner(rwsem);
78 * nfs_async_unlink_release - Release the sillydelete data.
79 * @task: rpc_task of the sillydelete
81 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
82 * rpc_task would be freed too.
84 static void nfs_async_unlink_release(void *calldata)
86 struct nfs_unlinkdata *data = calldata;
87 struct dentry *dentry = data->dentry;
88 struct super_block *sb = dentry->d_sb;
90 nfs_up_anon(&NFS_I(d_inode(dentry->d_parent))->rmdir_sem);
91 d_lookup_done(dentry);
92 nfs_free_unlinkdata(data);
97 static void nfs_unlink_prepare(struct rpc_task *task, void *calldata)
99 struct nfs_unlinkdata *data = calldata;
100 struct inode *dir = d_inode(data->dentry->d_parent);
101 NFS_PROTO(dir)->unlink_rpc_prepare(task, data);
104 static const struct rpc_call_ops nfs_unlink_ops = {
105 .rpc_call_done = nfs_async_unlink_done,
106 .rpc_release = nfs_async_unlink_release,
107 .rpc_call_prepare = nfs_unlink_prepare,
110 static void nfs_do_call_unlink(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 = {
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 inode *dir = d_inode(data->dentry->d_parent);
126 nfs_sb_active(dir->i_sb);
127 data->args.fh = NFS_FH(dir);
128 nfs_fattr_init(data->res.dir_attr);
130 NFS_PROTO(dir)->unlink_setup(&msg, dir);
132 task_setup_data.rpc_client = NFS_CLIENT(dir);
133 task = rpc_run_task(&task_setup_data);
135 rpc_put_task_async(task);
138 static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
140 struct inode *dir = d_inode(dentry->d_parent);
141 struct dentry *alias;
143 nfs_down_anon(&NFS_I(dir)->rmdir_sem);
144 alias = d_alloc_parallel(dentry->d_parent, &data->args.name, &data->wq);
146 nfs_up_anon(&NFS_I(dir)->rmdir_sem);
149 if (!d_in_lookup(alias)) {
151 void *devname_garbage = NULL;
154 * Hey, we raced with lookup... See if we need to transfer
155 * the sillyrename information to the aliased dentry.
157 spin_lock(&alias->d_lock);
158 if (d_really_is_positive(alias) &&
159 !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
160 devname_garbage = alias->d_fsdata;
161 alias->d_fsdata = data;
162 alias->d_flags |= DCACHE_NFSFS_RENAMED;
166 spin_unlock(&alias->d_lock);
168 nfs_up_anon(&NFS_I(dir)->rmdir_sem);
170 * If we'd displaced old cached devname, free it. At that
171 * point dentry is definitely not a root, so we won't need
174 kfree(devname_garbage);
177 data->dentry = alias;
178 nfs_do_call_unlink(data);
183 * nfs_async_unlink - asynchronous unlinking of a file
184 * @dir: parent directory of dentry
185 * @dentry: dentry to unlink
188 nfs_async_unlink(struct dentry *dentry, const struct qstr *name)
190 struct nfs_unlinkdata *data;
191 int status = -ENOMEM;
192 void *devname_garbage = NULL;
194 data = kzalloc(sizeof(*data), GFP_KERNEL);
197 data->args.name.name = kstrdup(name->name, GFP_KERNEL);
198 if (!data->args.name.name)
200 data->args.name.len = name->len;
202 data->cred = rpc_lookup_cred();
203 if (IS_ERR(data->cred)) {
204 status = PTR_ERR(data->cred);
207 data->res.dir_attr = &data->dir_attr;
208 init_swait_queue_head(&data->wq);
211 spin_lock(&dentry->d_lock);
212 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
214 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
215 devname_garbage = dentry->d_fsdata;
216 dentry->d_fsdata = data;
217 spin_unlock(&dentry->d_lock);
219 * If we'd displaced old cached devname, free it. At that
220 * point dentry is definitely not a root, so we won't need
223 kfree(devname_garbage);
226 spin_unlock(&dentry->d_lock);
227 put_rpccred(data->cred);
229 kfree(data->args.name.name);
237 * nfs_complete_unlink - Initialize completion of the sillydelete
238 * @dentry: dentry to delete
241 * Since we're most likely to be called by dentry_iput(), we
242 * only use the dentry to find the sillydelete. We then copy the name
246 nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
248 struct nfs_unlinkdata *data;
250 spin_lock(&dentry->d_lock);
251 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
252 data = dentry->d_fsdata;
253 dentry->d_fsdata = NULL;
254 spin_unlock(&dentry->d_lock);
256 if (NFS_STALE(inode) || !nfs_call_unlink(dentry, data))
257 nfs_free_unlinkdata(data);
260 /* Cancel a queued async unlink. Called when a sillyrename run fails. */
262 nfs_cancel_async_unlink(struct dentry *dentry)
264 spin_lock(&dentry->d_lock);
265 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
266 struct nfs_unlinkdata *data = dentry->d_fsdata;
268 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
269 dentry->d_fsdata = NULL;
270 spin_unlock(&dentry->d_lock);
271 nfs_free_unlinkdata(data);
274 spin_unlock(&dentry->d_lock);
278 * nfs_async_rename_done - Sillyrename post-processing
279 * @task: rpc_task of the sillyrename
280 * @calldata: nfs_renamedata for the sillyrename
282 * Do the directory attribute updates and the d_move
284 static void nfs_async_rename_done(struct rpc_task *task, void *calldata)
286 struct nfs_renamedata *data = calldata;
287 struct inode *old_dir = data->old_dir;
288 struct inode *new_dir = data->new_dir;
289 struct dentry *old_dentry = data->old_dentry;
291 trace_nfs_sillyrename_rename(old_dir, old_dentry,
292 new_dir, data->new_dentry, task->tk_status);
293 if (!NFS_PROTO(old_dir)->rename_done(task, old_dir, new_dir)) {
294 rpc_restart_call_prepare(task);
299 data->complete(task, data);
303 * nfs_async_rename_release - Release the sillyrename data.
304 * @calldata: the struct nfs_renamedata to be released
306 static void nfs_async_rename_release(void *calldata)
308 struct nfs_renamedata *data = calldata;
309 struct super_block *sb = data->old_dir->i_sb;
311 if (d_really_is_positive(data->old_dentry))
312 nfs_mark_for_revalidate(d_inode(data->old_dentry));
314 dput(data->old_dentry);
315 dput(data->new_dentry);
319 put_rpccred(data->cred);
323 static void nfs_rename_prepare(struct rpc_task *task, void *calldata)
325 struct nfs_renamedata *data = calldata;
326 NFS_PROTO(data->old_dir)->rename_rpc_prepare(task, data);
329 static const struct rpc_call_ops nfs_rename_ops = {
330 .rpc_call_done = nfs_async_rename_done,
331 .rpc_release = nfs_async_rename_release,
332 .rpc_call_prepare = nfs_rename_prepare,
336 * nfs_async_rename - perform an asynchronous rename operation
337 * @old_dir: directory that currently holds the dentry to be renamed
338 * @new_dir: target directory for the rename
339 * @old_dentry: original dentry to be renamed
340 * @new_dentry: dentry to which the old_dentry should be renamed
342 * It's expected that valid references to the dentries and inodes are held
345 nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
346 struct dentry *old_dentry, struct dentry *new_dentry,
347 void (*complete)(struct rpc_task *, struct nfs_renamedata *))
349 struct nfs_renamedata *data;
350 struct rpc_message msg = { };
351 struct rpc_task_setup task_setup_data = {
353 .callback_ops = &nfs_rename_ops,
354 .workqueue = nfsiod_workqueue,
355 .rpc_client = NFS_CLIENT(old_dir),
356 .flags = RPC_TASK_ASYNC,
359 data = kzalloc(sizeof(*data), GFP_KERNEL);
361 return ERR_PTR(-ENOMEM);
362 task_setup_data.callback_data = data;
364 data->cred = rpc_lookup_cred();
365 if (IS_ERR(data->cred)) {
366 struct rpc_task *task = ERR_CAST(data->cred);
371 msg.rpc_argp = &data->args;
372 msg.rpc_resp = &data->res;
373 msg.rpc_cred = data->cred;
375 /* set up nfs_renamedata */
376 data->old_dir = old_dir;
378 data->new_dir = new_dir;
380 data->old_dentry = dget(old_dentry);
381 data->new_dentry = dget(new_dentry);
382 nfs_fattr_init(&data->old_fattr);
383 nfs_fattr_init(&data->new_fattr);
384 data->complete = complete;
386 /* set up nfs_renameargs */
387 data->args.old_dir = NFS_FH(old_dir);
388 data->args.old_name = &old_dentry->d_name;
389 data->args.new_dir = NFS_FH(new_dir);
390 data->args.new_name = &new_dentry->d_name;
392 /* set up nfs_renameres */
393 data->res.old_fattr = &data->old_fattr;
394 data->res.new_fattr = &data->new_fattr;
396 nfs_sb_active(old_dir->i_sb);
398 NFS_PROTO(data->old_dir)->rename_setup(&msg, old_dir);
400 return rpc_run_task(&task_setup_data);
404 * Perform tasks needed when a sillyrename is done such as cancelling the
405 * queued async unlink if it failed.
408 nfs_complete_sillyrename(struct rpc_task *task, struct nfs_renamedata *data)
410 struct dentry *dentry = data->old_dentry;
412 if (task->tk_status != 0) {
413 nfs_cancel_async_unlink(dentry);
418 * vfs_unlink and the like do not issue this when a file is
419 * sillyrenamed, so do it here.
421 fsnotify_nameremove(dentry, 0);
424 #define SILLYNAME_PREFIX ".nfs"
425 #define SILLYNAME_PREFIX_LEN ((unsigned)sizeof(SILLYNAME_PREFIX) - 1)
426 #define SILLYNAME_FILEID_LEN ((unsigned)sizeof(u64) << 1)
427 #define SILLYNAME_COUNTER_LEN ((unsigned)sizeof(unsigned int) << 1)
428 #define SILLYNAME_LEN (SILLYNAME_PREFIX_LEN + \
429 SILLYNAME_FILEID_LEN + \
430 SILLYNAME_COUNTER_LEN)
433 * nfs_sillyrename - Perform a silly-rename of a dentry
434 * @dir: inode of directory that contains dentry
435 * @dentry: dentry to be sillyrenamed
437 * NFSv2/3 is stateless and the server doesn't know when the client is
438 * holding a file open. To prevent application problems when a file is
439 * unlinked while it's still open, the client performs a "silly-rename".
440 * That is, it renames the file to a hidden file in the same directory,
441 * and only performs the unlink once the last reference to it is put.
443 * The final cleanup is done during dentry_iput.
445 * (Note: NFSv4 is stateful, and has opens, so in theory an NFSv4 server
446 * could take responsibility for keeping open files referenced. The server
447 * would also need to ensure that opened-but-deleted files were kept over
448 * reboots. However, we may not assume a server does so. (RFC 5661
449 * does provide an OPEN4_RESULT_PRESERVE_UNLINKED flag that a server can
450 * use to advertise that it does this; some day we may take advantage of
454 nfs_sillyrename(struct inode *dir, struct dentry *dentry)
456 static unsigned int sillycounter;
457 unsigned char silly[SILLYNAME_LEN + 1];
458 unsigned long long fileid;
459 struct dentry *sdentry;
460 struct rpc_task *task;
463 dfprintk(VFS, "NFS: silly-rename(%pd2, ct=%d)\n",
464 dentry, d_count(dentry));
465 nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
468 * We don't allow a dentry to be silly-renamed twice.
470 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
473 fileid = NFS_FILEID(d_inode(dentry));
475 /* Return delegation in anticipation of the rename */
476 NFS_PROTO(d_inode(dentry))->return_delegation(d_inode(dentry));
483 slen = scnprintf(silly, sizeof(silly),
484 SILLYNAME_PREFIX "%0*llx%0*x",
485 SILLYNAME_FILEID_LEN, fileid,
486 SILLYNAME_COUNTER_LEN, sillycounter);
488 dfprintk(VFS, "NFS: trying to rename %pd to %s\n",
491 sdentry = lookup_one_len(silly, dentry->d_parent, slen);
493 * N.B. Better to return EBUSY here ... it could be
494 * dangerous to delete the file while it's in use.
498 } while (d_inode(sdentry) != NULL); /* need negative lookup */
500 /* queue unlink first. Can't do this from rpc_release as it
501 * has to allocate memory
503 error = nfs_async_unlink(dentry, &sdentry->d_name);
507 /* run the rename task, undo unlink if it fails */
508 task = nfs_async_rename(dir, dir, dentry, sdentry,
509 nfs_complete_sillyrename);
512 nfs_cancel_async_unlink(dentry);
516 /* wait for the RPC task to complete, unless a SIGKILL intervenes */
517 error = rpc_wait_for_completion_task(task);
519 error = task->tk_status;
522 /* The rename succeeded */
523 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
524 d_move(dentry, sdentry);
527 /* The result of the rename is unknown. Play it safe by
528 * forcing a new lookup */